PR ipa/64481
[official-gcc.git] / gcc / varasm.c
blob9847ca603031ebda96ee86916567fd086fd932ea
1 /* Output variables, constants and external declarations, for GNU compiler.
2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
21 /* This file handles generation of all the assembler code
22 *except* the instructions of a function.
23 This includes declarations of variables and their initial values.
25 We also output the assembler code for constants stored in memory
26 and are responsible for combining constants with the same value. */
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tm.h"
32 #include "rtl.h"
33 #include "hash-set.h"
34 #include "machmode.h"
35 #include "vec.h"
36 #include "double-int.h"
37 #include "input.h"
38 #include "alias.h"
39 #include "symtab.h"
40 #include "wide-int.h"
41 #include "inchash.h"
42 #include "tree.h"
43 #include "fold-const.h"
44 #include "stor-layout.h"
45 #include "stringpool.h"
46 #include "varasm.h"
47 #include "flags.h"
48 #include "hard-reg-set.h"
49 #include "input.h"
50 #include "function.h"
51 #include "expr.h"
52 #include "regs.h"
53 #include "output.h"
54 #include "diagnostic-core.h"
55 #include "ggc.h"
56 #include "langhooks.h"
57 #include "tm_p.h"
58 #include "debug.h"
59 #include "target.h"
60 #include "common/common-target.h"
61 #include "targhooks.h"
62 #include "predict.h"
63 #include "dominance.h"
64 #include "cfg.h"
65 #include "basic-block.h"
66 #include "hash-map.h"
67 #include "is-a.h"
68 #include "plugin-api.h"
69 #include "ipa-ref.h"
70 #include "cgraph.h"
71 #include "asan.h"
72 #include "rtl-iter.h"
73 #include "tree-chkp.h"
75 #ifdef XCOFF_DEBUGGING_INFO
76 #include "xcoffout.h" /* Needed for external data
77 declarations for e.g. AIX 4.x. */
78 #endif
80 /* The (assembler) name of the first globally-visible object output. */
81 extern GTY(()) const char *first_global_object_name;
82 extern GTY(()) const char *weak_global_object_name;
84 const char *first_global_object_name;
85 const char *weak_global_object_name;
87 struct addr_const;
88 struct constant_descriptor_rtx;
89 struct rtx_constant_pool;
91 #define n_deferred_constants (crtl->varasm.deferred_constants)
93 /* Number for making the label on the next
94 constant that is stored in memory. */
96 static GTY(()) int const_labelno;
98 /* Carry information from ASM_DECLARE_OBJECT_NAME
99 to ASM_FINISH_DECLARE_OBJECT. */
101 int size_directive_output;
103 /* The last decl for which assemble_variable was called,
104 if it did ASM_DECLARE_OBJECT_NAME.
105 If the last call to assemble_variable didn't do that,
106 this holds 0. */
108 tree last_assemble_variable_decl;
110 /* The following global variable indicates if the first basic block
111 in a function belongs to the cold partition or not. */
113 bool first_function_block_is_cold;
115 /* Whether we saw any functions with no_split_stack. */
117 static bool saw_no_split_stack;
119 static const char *strip_reg_name (const char *);
120 static int contains_pointers_p (tree);
121 #ifdef ASM_OUTPUT_EXTERNAL
122 static bool incorporeal_function_p (tree);
123 #endif
124 static void decode_addr_const (tree, struct addr_const *);
125 static hashval_t const_hash_1 (const tree);
126 static int compare_constant (const tree, const tree);
127 static void output_constant_def_contents (rtx);
128 static void output_addressed_constants (tree);
129 static unsigned HOST_WIDE_INT output_constant (tree, unsigned HOST_WIDE_INT,
130 unsigned int);
131 static void globalize_decl (tree);
132 static bool decl_readonly_section_1 (enum section_category);
133 #ifdef BSS_SECTION_ASM_OP
134 #ifdef ASM_OUTPUT_ALIGNED_BSS
135 static void asm_output_aligned_bss (FILE *, tree, const char *,
136 unsigned HOST_WIDE_INT, int)
137 ATTRIBUTE_UNUSED;
138 #endif
139 #endif /* BSS_SECTION_ASM_OP */
140 static void mark_weak (tree);
141 static void output_constant_pool (const char *, tree);
143 /* Well-known sections, each one associated with some sort of *_ASM_OP. */
144 section *text_section;
145 section *data_section;
146 section *readonly_data_section;
147 section *sdata_section;
148 section *ctors_section;
149 section *dtors_section;
150 section *bss_section;
151 section *sbss_section;
153 /* Various forms of common section. All are guaranteed to be nonnull. */
154 section *tls_comm_section;
155 section *comm_section;
156 section *lcomm_section;
158 /* A SECTION_NOSWITCH section used for declaring global BSS variables.
159 May be null. */
160 section *bss_noswitch_section;
162 /* The section that holds the main exception table, when known. The section
163 is set either by the target's init_sections hook or by the first call to
164 switch_to_exception_section. */
165 section *exception_section;
167 /* The section that holds the DWARF2 frame unwind information, when known.
168 The section is set either by the target's init_sections hook or by the
169 first call to switch_to_eh_frame_section. */
170 section *eh_frame_section;
172 /* asm_out_file's current section. This is NULL if no section has yet
173 been selected or if we lose track of what the current section is. */
174 section *in_section;
176 /* True if code for the current function is currently being directed
177 at the cold section. */
178 bool in_cold_section_p;
180 /* A linked list of all the unnamed sections. */
181 static GTY(()) section *unnamed_sections;
183 /* Return a nonzero value if DECL has a section attribute. */
184 #define IN_NAMED_SECTION(DECL) \
185 ((TREE_CODE (DECL) == FUNCTION_DECL || TREE_CODE (DECL) == VAR_DECL) \
186 && DECL_SECTION_NAME (DECL) != NULL)
188 struct section_hasher : ggc_hasher<section *>
190 typedef const char *compare_type;
192 static hashval_t hash (section *);
193 static bool equal (section *, const char *);
196 /* Hash table of named sections. */
197 static GTY(()) hash_table<section_hasher> *section_htab;
199 struct object_block_hasher : ggc_hasher<object_block *>
201 typedef const section *compare_type;
203 static hashval_t hash (object_block *);
204 static bool equal (object_block *, const section *);
207 /* A table of object_blocks, indexed by section. */
208 static GTY(()) hash_table<object_block_hasher> *object_block_htab;
210 /* The next number to use for internal anchor labels. */
211 static GTY(()) int anchor_labelno;
213 /* A pool of constants that can be shared between functions. */
214 static GTY(()) struct rtx_constant_pool *shared_constant_pool;
216 /* Helper routines for maintaining section_htab. */
218 bool
219 section_hasher::equal (section *old, const char *new_name)
221 return strcmp (old->named.name, new_name) == 0;
224 hashval_t
225 section_hasher::hash (section *old)
227 return htab_hash_string (old->named.name);
230 /* Return a hash value for section SECT. */
232 static hashval_t
233 hash_section (section *sect)
235 if (sect->common.flags & SECTION_NAMED)
236 return htab_hash_string (sect->named.name);
237 return sect->common.flags;
240 /* Helper routines for maintaining object_block_htab. */
242 inline bool
243 object_block_hasher::equal (object_block *old, const section *new_section)
245 return old->sect == new_section;
248 hashval_t
249 object_block_hasher::hash (object_block *old)
251 return hash_section (old->sect);
254 /* Return a new unnamed section with the given fields. */
256 section *
257 get_unnamed_section (unsigned int flags, void (*callback) (const void *),
258 const void *data)
260 section *sect;
262 sect = ggc_alloc<section> ();
263 sect->unnamed.common.flags = flags | SECTION_UNNAMED;
264 sect->unnamed.callback = callback;
265 sect->unnamed.data = data;
266 sect->unnamed.next = unnamed_sections;
268 unnamed_sections = sect;
269 return sect;
272 /* Return a SECTION_NOSWITCH section with the given fields. */
274 static section *
275 get_noswitch_section (unsigned int flags, noswitch_section_callback callback)
277 section *sect;
279 sect = ggc_alloc<section> ();
280 sect->noswitch.common.flags = flags | SECTION_NOSWITCH;
281 sect->noswitch.callback = callback;
283 return sect;
286 /* Return the named section structure associated with NAME. Create
287 a new section with the given fields if no such structure exists. */
289 section *
290 get_section (const char *name, unsigned int flags, tree decl)
292 section *sect, **slot;
294 slot = section_htab->find_slot_with_hash (name, htab_hash_string (name),
295 INSERT);
296 flags |= SECTION_NAMED;
297 if (*slot == NULL)
299 sect = ggc_alloc<section> ();
300 sect->named.common.flags = flags;
301 sect->named.name = ggc_strdup (name);
302 sect->named.decl = decl;
303 *slot = sect;
305 else
307 sect = *slot;
308 if ((sect->common.flags & ~SECTION_DECLARED) != flags
309 && ((sect->common.flags | flags) & SECTION_OVERRIDE) == 0)
311 /* It is fine if one of the section flags is
312 SECTION_WRITE | SECTION_RELRO and the other has none of these
313 flags (i.e. read-only) in named sections and either the
314 section hasn't been declared yet or has been declared as writable.
315 In that case just make sure the resulting flags are
316 SECTION_WRITE | SECTION_RELRO, ie. writable only because of
317 relocations. */
318 if (((sect->common.flags ^ flags) & (SECTION_WRITE | SECTION_RELRO))
319 == (SECTION_WRITE | SECTION_RELRO)
320 && (sect->common.flags
321 & ~(SECTION_DECLARED | SECTION_WRITE | SECTION_RELRO))
322 == (flags & ~(SECTION_WRITE | SECTION_RELRO))
323 && ((sect->common.flags & SECTION_DECLARED) == 0
324 || (sect->common.flags & SECTION_WRITE)))
326 sect->common.flags |= (SECTION_WRITE | SECTION_RELRO);
327 return sect;
329 /* Sanity check user variables for flag changes. */
330 if (sect->named.decl != NULL
331 && DECL_P (sect->named.decl)
332 && decl != sect->named.decl)
334 if (decl != NULL && DECL_P (decl))
335 error ("%+D causes a section type conflict with %D",
336 decl, sect->named.decl);
337 else
338 error ("section type conflict with %D", sect->named.decl);
339 inform (DECL_SOURCE_LOCATION (sect->named.decl),
340 "%qD was declared here", sect->named.decl);
342 else if (decl != NULL && DECL_P (decl))
343 error ("%+D causes a section type conflict", decl);
344 else
345 error ("section type conflict");
346 /* Make sure we don't error about one section multiple times. */
347 sect->common.flags |= SECTION_OVERRIDE;
350 return sect;
353 /* Return true if the current compilation mode benefits from having
354 objects grouped into blocks. */
356 static bool
357 use_object_blocks_p (void)
359 return flag_section_anchors;
362 /* Return the object_block structure for section SECT. Create a new
363 structure if we haven't created one already. Return null if SECT
364 itself is null. */
366 static struct object_block *
367 get_block_for_section (section *sect)
369 struct object_block *block;
371 if (sect == NULL)
372 return NULL;
374 object_block **slot
375 = object_block_htab->find_slot_with_hash (sect, hash_section (sect),
376 INSERT);
377 block = *slot;
378 if (block == NULL)
380 block = ggc_cleared_alloc<object_block> ();
381 block->sect = sect;
382 *slot = block;
384 return block;
387 /* Create a symbol with label LABEL and place it at byte offset
388 OFFSET in BLOCK. OFFSET can be negative if the symbol's offset
389 is not yet known. LABEL must be a garbage-collected string. */
391 static rtx
392 create_block_symbol (const char *label, struct object_block *block,
393 HOST_WIDE_INT offset)
395 rtx symbol;
396 unsigned int size;
398 /* Create the extended SYMBOL_REF. */
399 size = RTX_HDR_SIZE + sizeof (struct block_symbol);
400 symbol = (rtx) ggc_internal_alloc (size);
402 /* Initialize the normal SYMBOL_REF fields. */
403 memset (symbol, 0, size);
404 PUT_CODE (symbol, SYMBOL_REF);
405 PUT_MODE (symbol, Pmode);
406 XSTR (symbol, 0) = label;
407 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_HAS_BLOCK_INFO;
409 /* Initialize the block_symbol stuff. */
410 SYMBOL_REF_BLOCK (symbol) = block;
411 SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
413 return symbol;
416 /* Return a section with a particular name and with whatever SECTION_*
417 flags section_type_flags deems appropriate. The name of the section
418 is taken from NAME if nonnull, otherwise it is taken from DECL's
419 DECL_SECTION_NAME. DECL is the decl associated with the section
420 (see the section comment for details) and RELOC is as for
421 section_type_flags. */
423 section *
424 get_named_section (tree decl, const char *name, int reloc)
426 unsigned int flags;
428 if (name == NULL)
430 gcc_assert (decl && DECL_P (decl) && DECL_SECTION_NAME (decl));
431 name = DECL_SECTION_NAME (decl);
434 flags = targetm.section_type_flags (decl, name, reloc);
435 return get_section (name, flags, decl);
438 /* Worker for resolve_unique_section. */
440 static bool
441 set_implicit_section (struct symtab_node *n, void *data ATTRIBUTE_UNUSED)
443 n->implicit_section = true;
444 return false;
447 /* If required, set DECL_SECTION_NAME to a unique name. */
449 void
450 resolve_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED,
451 int flag_function_or_data_sections)
453 if (DECL_SECTION_NAME (decl) == NULL
454 && targetm_common.have_named_sections
455 && (flag_function_or_data_sections
456 || DECL_COMDAT_GROUP (decl)))
458 targetm.asm_out.unique_section (decl, reloc);
459 if (DECL_SECTION_NAME (decl))
460 symtab_node::get (decl)->call_for_symbol_and_aliases
461 (set_implicit_section, NULL, true);
465 #ifdef BSS_SECTION_ASM_OP
467 #ifdef ASM_OUTPUT_ALIGNED_BSS
469 /* Utility function for targets to use in implementing
470 ASM_OUTPUT_ALIGNED_BSS.
471 ??? It is believed that this function will work in most cases so such
472 support is localized here. */
474 static void
475 asm_output_aligned_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
476 const char *name, unsigned HOST_WIDE_INT size,
477 int align)
479 switch_to_section (bss_section);
480 ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
481 #ifdef ASM_DECLARE_OBJECT_NAME
482 last_assemble_variable_decl = decl;
483 ASM_DECLARE_OBJECT_NAME (file, name, decl);
484 #else
485 /* Standard thing is just output label for the object. */
486 ASM_OUTPUT_LABEL (file, name);
487 #endif /* ASM_DECLARE_OBJECT_NAME */
488 ASM_OUTPUT_SKIP (file, size ? size : 1);
491 #endif
493 #endif /* BSS_SECTION_ASM_OP */
495 #ifndef USE_SELECT_SECTION_FOR_FUNCTIONS
496 /* Return the hot section for function DECL. Return text_section for
497 null DECLs. */
499 static section *
500 hot_function_section (tree decl)
502 if (decl != NULL_TREE
503 && DECL_SECTION_NAME (decl) != NULL
504 && targetm_common.have_named_sections)
505 return get_named_section (decl, NULL, 0);
506 else
507 return text_section;
509 #endif
511 /* Return section for TEXT_SECTION_NAME if DECL or DECL_SECTION_NAME (DECL)
512 is NULL.
514 When DECL_SECTION_NAME is non-NULL and it is implicit section and
515 NAMED_SECTION_SUFFIX is non-NULL, then produce section called
516 concatenate the name with NAMED_SECTION_SUFFIX.
517 Otherwise produce "TEXT_SECTION_NAME.IMPLICIT_NAME". */
519 section *
520 get_named_text_section (tree decl,
521 const char *text_section_name,
522 const char *named_section_suffix)
524 if (decl && DECL_SECTION_NAME (decl))
526 if (named_section_suffix)
528 const char *dsn = DECL_SECTION_NAME (decl);
529 const char *stripped_name;
530 char *name, *buffer;
532 name = (char *) alloca (strlen (dsn) + 1);
533 memcpy (name, dsn,
534 strlen (dsn) + 1);
536 stripped_name = targetm.strip_name_encoding (name);
538 buffer = ACONCAT ((stripped_name, named_section_suffix, NULL));
539 return get_named_section (decl, buffer, 0);
541 else if (symtab_node::get (decl)->implicit_section)
543 const char *name;
545 /* Do not try to split gnu_linkonce functions. This gets somewhat
546 slipperly. */
547 if (DECL_COMDAT_GROUP (decl) && !HAVE_COMDAT_GROUP)
548 return NULL;
549 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
550 name = targetm.strip_name_encoding (name);
551 return get_named_section (decl, ACONCAT ((text_section_name, ".",
552 name, NULL)), 0);
554 else
555 return NULL;
557 return get_named_section (decl, text_section_name, 0);
560 /* Choose named function section based on its frequency. */
562 section *
563 default_function_section (tree decl, enum node_frequency freq,
564 bool startup, bool exit)
566 #if defined HAVE_LD_EH_GC_SECTIONS && defined HAVE_LD_EH_GC_SECTIONS_BUG
567 /* Old GNU linkers have buggy --gc-section support, which sometimes
568 results in .gcc_except_table* sections being garbage collected. */
569 if (decl
570 && symtab_node::get (decl)->implicit_section)
571 return NULL;
572 #endif
574 if (!flag_reorder_functions
575 || !targetm_common.have_named_sections)
576 return NULL;
577 /* Startup code should go to startup subsection unless it is
578 unlikely executed (this happens especially with function splitting
579 where we can split away unnecessary parts of static constructors. */
580 if (startup && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
582 /* If we do have a profile or(and) LTO phase is executed, we do not need
583 these ELF section. */
584 if (!in_lto_p || !flag_profile_values)
585 return get_named_text_section (decl, ".text.startup", NULL);
586 else
587 return NULL;
590 /* Similarly for exit. */
591 if (exit && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
592 return get_named_text_section (decl, ".text.exit", NULL);
594 /* Group cold functions together, similarly for hot code. */
595 switch (freq)
597 case NODE_FREQUENCY_UNLIKELY_EXECUTED:
598 return get_named_text_section (decl, ".text.unlikely", NULL);
599 case NODE_FREQUENCY_HOT:
600 /* If we do have a profile or(and) LTO phase is executed, we do not need
601 these ELF section. */
602 if (!in_lto_p || !flag_profile_values)
603 return get_named_text_section (decl, ".text.hot", NULL);
604 default:
605 return NULL;
609 /* Return the section for function DECL.
611 If DECL is NULL_TREE, return the text section. We can be passed
612 NULL_TREE under some circumstances by dbxout.c at least.
614 If FORCE_COLD is true, return cold function section ignoring
615 the frequency info of cgraph_node. */
617 static section *
618 function_section_1 (tree decl, bool force_cold)
620 section *section = NULL;
621 enum node_frequency freq = NODE_FREQUENCY_NORMAL;
622 bool startup = false, exit = false;
624 if (decl)
626 struct cgraph_node *node = cgraph_node::get (decl);
628 if (node)
630 freq = node->frequency;
631 startup = node->only_called_at_startup;
632 exit = node->only_called_at_exit;
635 if (force_cold)
636 freq = NODE_FREQUENCY_UNLIKELY_EXECUTED;
638 #ifdef USE_SELECT_SECTION_FOR_FUNCTIONS
639 if (decl != NULL_TREE
640 && DECL_SECTION_NAME (decl) != NULL)
642 if (targetm.asm_out.function_section)
643 section = targetm.asm_out.function_section (decl, freq,
644 startup, exit);
645 if (section)
646 return section;
647 return get_named_section (decl, NULL, 0);
649 else
650 return targetm.asm_out.select_section
651 (decl, freq == NODE_FREQUENCY_UNLIKELY_EXECUTED,
652 DECL_ALIGN (decl));
653 #else
654 if (targetm.asm_out.function_section)
655 section = targetm.asm_out.function_section (decl, freq, startup, exit);
656 if (section)
657 return section;
658 return hot_function_section (decl);
659 #endif
662 /* Return the section for function DECL.
664 If DECL is NULL_TREE, return the text section. We can be passed
665 NULL_TREE under some circumstances by dbxout.c at least. */
667 section *
668 function_section (tree decl)
670 /* Handle cases where function splitting code decides
671 to put function entry point into unlikely executed section
672 despite the fact that the function itself is not cold
673 (i.e. it is called rarely but contains a hot loop that is
674 better to live in hot subsection for the code locality). */
675 return function_section_1 (decl,
676 first_function_block_is_cold);
679 /* Return the section for the current function, take IN_COLD_SECTION_P
680 into account. */
682 section *
683 current_function_section (void)
685 return function_section_1 (current_function_decl, in_cold_section_p);
688 /* Tell assembler to switch to unlikely-to-be-executed text section. */
690 section *
691 unlikely_text_section (void)
693 return function_section_1 (current_function_decl, true);
696 /* When called within a function context, return true if the function
697 has been assigned a cold text section and if SECT is that section.
698 When called outside a function context, return true if SECT is the
699 default cold section. */
701 bool
702 unlikely_text_section_p (section *sect)
704 return sect == function_section_1 (current_function_decl, true);
707 /* Return the read-only data section associated with function DECL. */
709 section *
710 default_function_rodata_section (tree decl)
712 if (decl != NULL_TREE && DECL_SECTION_NAME (decl))
714 const char *name = DECL_SECTION_NAME (decl);
716 if (DECL_COMDAT_GROUP (decl) && HAVE_COMDAT_GROUP)
718 const char *dot;
719 size_t len;
720 char* rname;
722 dot = strchr (name + 1, '.');
723 if (!dot)
724 dot = name;
725 len = strlen (dot) + 8;
726 rname = (char *) alloca (len);
728 strcpy (rname, ".rodata");
729 strcat (rname, dot);
730 return get_section (rname, SECTION_LINKONCE, decl);
732 /* For .gnu.linkonce.t.foo we want to use .gnu.linkonce.r.foo. */
733 else if (DECL_COMDAT_GROUP (decl)
734 && strncmp (name, ".gnu.linkonce.t.", 16) == 0)
736 size_t len = strlen (name) + 1;
737 char *rname = (char *) alloca (len);
739 memcpy (rname, name, len);
740 rname[14] = 'r';
741 return get_section (rname, SECTION_LINKONCE, decl);
743 /* For .text.foo we want to use .rodata.foo. */
744 else if (flag_function_sections && flag_data_sections
745 && strncmp (name, ".text.", 6) == 0)
747 size_t len = strlen (name) + 1;
748 char *rname = (char *) alloca (len + 2);
750 memcpy (rname, ".rodata", 7);
751 memcpy (rname + 7, name + 5, len - 5);
752 return get_section (rname, 0, decl);
756 return readonly_data_section;
759 /* Return the read-only data section associated with function DECL
760 for targets where that section should be always the single
761 readonly data section. */
763 section *
764 default_no_function_rodata_section (tree decl ATTRIBUTE_UNUSED)
766 return readonly_data_section;
769 /* Return the section to use for string merging. */
771 static section *
772 mergeable_string_section (tree decl ATTRIBUTE_UNUSED,
773 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
774 unsigned int flags ATTRIBUTE_UNUSED)
776 HOST_WIDE_INT len;
778 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
779 && TREE_CODE (decl) == STRING_CST
780 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
781 && align <= 256
782 && (len = int_size_in_bytes (TREE_TYPE (decl))) > 0
783 && TREE_STRING_LENGTH (decl) >= len)
785 machine_mode mode;
786 unsigned int modesize;
787 const char *str;
788 HOST_WIDE_INT i;
789 int j, unit;
790 const char *prefix = targetm.asm_out.mergeable_rodata_prefix;
791 char *name = (char *) alloca (strlen (prefix) + 30);
793 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (decl)));
794 modesize = GET_MODE_BITSIZE (mode);
795 if (modesize >= 8 && modesize <= 256
796 && (modesize & (modesize - 1)) == 0)
798 if (align < modesize)
799 align = modesize;
801 str = TREE_STRING_POINTER (decl);
802 unit = GET_MODE_SIZE (mode);
804 /* Check for embedded NUL characters. */
805 for (i = 0; i < len; i += unit)
807 for (j = 0; j < unit; j++)
808 if (str[i + j] != '\0')
809 break;
810 if (j == unit)
811 break;
813 if (i == len - unit)
815 sprintf (name, "%s.str%d.%d", prefix,
816 modesize / 8, (int) (align / 8));
817 flags |= (modesize / 8) | SECTION_MERGE | SECTION_STRINGS;
818 return get_section (name, flags, NULL);
823 return readonly_data_section;
826 /* Return the section to use for constant merging. */
828 section *
829 mergeable_constant_section (machine_mode mode ATTRIBUTE_UNUSED,
830 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
831 unsigned int flags ATTRIBUTE_UNUSED)
833 unsigned int modesize = GET_MODE_BITSIZE (mode);
835 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
836 && mode != VOIDmode
837 && mode != BLKmode
838 && modesize <= align
839 && align >= 8
840 && align <= 256
841 && (align & (align - 1)) == 0)
843 const char *prefix = targetm.asm_out.mergeable_rodata_prefix;
844 char *name = (char *) alloca (strlen (prefix) + 30);
846 sprintf (name, "%s.cst%d", prefix, (int) (align / 8));
847 flags |= (align / 8) | SECTION_MERGE;
848 return get_section (name, flags, NULL);
850 return readonly_data_section;
853 /* Given NAME, a putative register name, discard any customary prefixes. */
855 static const char *
856 strip_reg_name (const char *name)
858 #ifdef REGISTER_PREFIX
859 if (!strncmp (name, REGISTER_PREFIX, strlen (REGISTER_PREFIX)))
860 name += strlen (REGISTER_PREFIX);
861 #endif
862 if (name[0] == '%' || name[0] == '#')
863 name++;
864 return name;
867 /* The user has asked for a DECL to have a particular name. Set (or
868 change) it in such a way that we don't prefix an underscore to
869 it. */
870 void
871 set_user_assembler_name (tree decl, const char *name)
873 char *starred = (char *) alloca (strlen (name) + 2);
874 starred[0] = '*';
875 strcpy (starred + 1, name);
876 symtab->change_decl_assembler_name (decl, get_identifier (starred));
877 SET_DECL_RTL (decl, NULL_RTX);
880 /* Decode an `asm' spec for a declaration as a register name.
881 Return the register number, or -1 if nothing specified,
882 or -2 if the ASMSPEC is not `cc' or `memory' and is not recognized,
883 or -3 if ASMSPEC is `cc' and is not recognized,
884 or -4 if ASMSPEC is `memory' and is not recognized.
885 Accept an exact spelling or a decimal number.
886 Prefixes such as % are optional. */
889 decode_reg_name_and_count (const char *asmspec, int *pnregs)
891 /* Presume just one register is clobbered. */
892 *pnregs = 1;
894 if (asmspec != 0)
896 int i;
898 /* Get rid of confusing prefixes. */
899 asmspec = strip_reg_name (asmspec);
901 /* Allow a decimal number as a "register name". */
902 for (i = strlen (asmspec) - 1; i >= 0; i--)
903 if (! ISDIGIT (asmspec[i]))
904 break;
905 if (asmspec[0] != 0 && i < 0)
907 i = atoi (asmspec);
908 if (i < FIRST_PSEUDO_REGISTER && i >= 0 && reg_names[i][0])
909 return i;
910 else
911 return -2;
914 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
915 if (reg_names[i][0]
916 && ! strcmp (asmspec, strip_reg_name (reg_names[i])))
917 return i;
919 #ifdef OVERLAPPING_REGISTER_NAMES
921 static const struct
923 const char *const name;
924 const int number;
925 const int nregs;
926 } table[] = OVERLAPPING_REGISTER_NAMES;
928 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
929 if (table[i].name[0]
930 && ! strcmp (asmspec, table[i].name))
932 *pnregs = table[i].nregs;
933 return table[i].number;
936 #endif /* OVERLAPPING_REGISTER_NAMES */
938 #ifdef ADDITIONAL_REGISTER_NAMES
940 static const struct { const char *const name; const int number; } table[]
941 = ADDITIONAL_REGISTER_NAMES;
943 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
944 if (table[i].name[0]
945 && ! strcmp (asmspec, table[i].name)
946 && reg_names[table[i].number][0])
947 return table[i].number;
949 #endif /* ADDITIONAL_REGISTER_NAMES */
951 if (!strcmp (asmspec, "memory"))
952 return -4;
954 if (!strcmp (asmspec, "cc"))
955 return -3;
957 return -2;
960 return -1;
964 decode_reg_name (const char *name)
966 int count;
967 return decode_reg_name_and_count (name, &count);
971 /* Return true if DECL's initializer is suitable for a BSS section. */
973 bool
974 bss_initializer_p (const_tree decl)
976 return (DECL_INITIAL (decl) == NULL
977 /* In LTO we have no errors in program; error_mark_node is used
978 to mark offlined constructors. */
979 || (DECL_INITIAL (decl) == error_mark_node
980 && !in_lto_p)
981 || (flag_zero_initialized_in_bss
982 /* Leave constant zeroes in .rodata so they
983 can be shared. */
984 && !TREE_READONLY (decl)
985 && initializer_zerop (DECL_INITIAL (decl))));
988 /* Compute the alignment of variable specified by DECL.
989 DONT_OUTPUT_DATA is from assemble_variable. */
991 void
992 align_variable (tree decl, bool dont_output_data)
994 unsigned int align = DECL_ALIGN (decl);
996 /* In the case for initialing an array whose length isn't specified,
997 where we have not yet been able to do the layout,
998 figure out the proper alignment now. */
999 if (dont_output_data && DECL_SIZE (decl) == 0
1000 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
1001 align = MAX (align, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (decl))));
1003 /* Some object file formats have a maximum alignment which they support.
1004 In particular, a.out format supports a maximum alignment of 4. */
1005 if (align > MAX_OFILE_ALIGNMENT)
1007 error ("alignment of %q+D is greater than maximum object "
1008 "file alignment %d", decl,
1009 MAX_OFILE_ALIGNMENT/BITS_PER_UNIT);
1010 align = MAX_OFILE_ALIGNMENT;
1013 if (! DECL_USER_ALIGN (decl))
1015 #ifdef DATA_ABI_ALIGNMENT
1016 unsigned int data_abi_align
1017 = DATA_ABI_ALIGNMENT (TREE_TYPE (decl), align);
1018 /* For backwards compatibility, don't assume the ABI alignment for
1019 TLS variables. */
1020 if (! DECL_THREAD_LOCAL_P (decl) || data_abi_align <= BITS_PER_WORD)
1021 align = data_abi_align;
1022 #endif
1024 /* On some machines, it is good to increase alignment sometimes.
1025 But as DECL_ALIGN is used both for actually emitting the variable
1026 and for code accessing the variable as guaranteed alignment, we
1027 can only increase the alignment if it is a performance optimization
1028 if the references to it must bind to the current definition. */
1029 if (decl_binds_to_current_def_p (decl)
1030 && !DECL_VIRTUAL_P (decl))
1032 #ifdef DATA_ALIGNMENT
1033 unsigned int data_align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
1034 /* Don't increase alignment too much for TLS variables - TLS space
1035 is too precious. */
1036 if (! DECL_THREAD_LOCAL_P (decl) || data_align <= BITS_PER_WORD)
1037 align = data_align;
1038 #endif
1039 #ifdef CONSTANT_ALIGNMENT
1040 if (DECL_INITIAL (decl) != 0
1041 /* In LTO we have no errors in program; error_mark_node is used
1042 to mark offlined constructors. */
1043 && (in_lto_p || DECL_INITIAL (decl) != error_mark_node))
1045 unsigned int const_align
1046 = CONSTANT_ALIGNMENT (DECL_INITIAL (decl), align);
1047 /* Don't increase alignment too much for TLS variables - TLS
1048 space is too precious. */
1049 if (! DECL_THREAD_LOCAL_P (decl) || const_align <= BITS_PER_WORD)
1050 align = const_align;
1052 #endif
1056 /* Reset the alignment in case we have made it tighter, so we can benefit
1057 from it in get_pointer_alignment. */
1058 DECL_ALIGN (decl) = align;
1061 /* Return DECL_ALIGN (decl), possibly increased for optimization purposes
1062 beyond what align_variable returned. */
1064 static unsigned int
1065 get_variable_align (tree decl)
1067 unsigned int align = DECL_ALIGN (decl);
1069 /* For user aligned vars or static vars align_variable already did
1070 everything. */
1071 if (DECL_USER_ALIGN (decl) || !TREE_PUBLIC (decl))
1072 return align;
1074 #ifdef DATA_ABI_ALIGNMENT
1075 if (DECL_THREAD_LOCAL_P (decl))
1076 align = DATA_ABI_ALIGNMENT (TREE_TYPE (decl), align);
1077 #endif
1079 /* For decls that bind to the current definition, align_variable
1080 did also everything, except for not assuming ABI required alignment
1081 of TLS variables. For other vars, increase the alignment here
1082 as an optimization. */
1083 if (!decl_binds_to_current_def_p (decl))
1085 /* On some machines, it is good to increase alignment sometimes. */
1086 #ifdef DATA_ALIGNMENT
1087 unsigned int data_align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
1088 /* Don't increase alignment too much for TLS variables - TLS space
1089 is too precious. */
1090 if (! DECL_THREAD_LOCAL_P (decl) || data_align <= BITS_PER_WORD)
1091 align = data_align;
1092 #endif
1093 #ifdef CONSTANT_ALIGNMENT
1094 if (DECL_INITIAL (decl) != 0
1095 /* In LTO we have no errors in program; error_mark_node is used
1096 to mark offlined constructors. */
1097 && (in_lto_p || DECL_INITIAL (decl) != error_mark_node))
1099 unsigned int const_align = CONSTANT_ALIGNMENT (DECL_INITIAL (decl),
1100 align);
1101 /* Don't increase alignment too much for TLS variables - TLS space
1102 is too precious. */
1103 if (! DECL_THREAD_LOCAL_P (decl) || const_align <= BITS_PER_WORD)
1104 align = const_align;
1106 #endif
1109 return align;
1112 /* Return the section into which the given VAR_DECL or CONST_DECL
1113 should be placed. PREFER_NOSWITCH_P is true if a noswitch
1114 section should be used wherever possible. */
1116 section *
1117 get_variable_section (tree decl, bool prefer_noswitch_p)
1119 addr_space_t as = ADDR_SPACE_GENERIC;
1120 int reloc;
1121 varpool_node *vnode = varpool_node::get (decl);
1122 if (vnode)
1124 vnode = vnode->ultimate_alias_target ();
1125 decl = vnode->decl;
1128 if (TREE_TYPE (decl) != error_mark_node)
1129 as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
1131 /* We need the constructor to figure out reloc flag. */
1132 if (vnode)
1133 vnode->get_constructor ();
1135 if (DECL_COMMON (decl))
1137 /* If the decl has been given an explicit section name, or it resides
1138 in a non-generic address space, then it isn't common, and shouldn't
1139 be handled as such. */
1140 gcc_assert (DECL_SECTION_NAME (decl) == NULL
1141 && ADDR_SPACE_GENERIC_P (as));
1142 if (DECL_THREAD_LOCAL_P (decl))
1143 return tls_comm_section;
1144 else if (TREE_PUBLIC (decl) && bss_initializer_p (decl))
1145 return comm_section;
1148 if (DECL_INITIAL (decl) == error_mark_node)
1149 reloc = contains_pointers_p (TREE_TYPE (decl)) ? 3 : 0;
1150 else if (DECL_INITIAL (decl))
1151 reloc = compute_reloc_for_constant (DECL_INITIAL (decl));
1152 else
1153 reloc = 0;
1155 resolve_unique_section (decl, reloc, flag_data_sections);
1156 if (IN_NAMED_SECTION (decl))
1157 return get_named_section (decl, NULL, reloc);
1159 if (ADDR_SPACE_GENERIC_P (as)
1160 && !DECL_THREAD_LOCAL_P (decl)
1161 && !(prefer_noswitch_p && targetm.have_switchable_bss_sections)
1162 && bss_initializer_p (decl))
1164 if (!TREE_PUBLIC (decl)
1165 && !((flag_sanitize & SANITIZE_ADDRESS)
1166 && asan_protect_global (decl)))
1167 return lcomm_section;
1168 if (bss_noswitch_section)
1169 return bss_noswitch_section;
1172 return targetm.asm_out.select_section (decl, reloc,
1173 get_variable_align (decl));
1176 /* Return the block into which object_block DECL should be placed. */
1178 static struct object_block *
1179 get_block_for_decl (tree decl)
1181 section *sect;
1183 if (TREE_CODE (decl) == VAR_DECL)
1185 /* The object must be defined in this translation unit. */
1186 if (DECL_EXTERNAL (decl))
1187 return NULL;
1189 /* There's no point using object blocks for something that is
1190 isolated by definition. */
1191 if (DECL_COMDAT_GROUP (decl))
1192 return NULL;
1195 /* We can only calculate block offsets if the decl has a known
1196 constant size. */
1197 if (DECL_SIZE_UNIT (decl) == NULL)
1198 return NULL;
1199 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (decl)))
1200 return NULL;
1202 /* Find out which section should contain DECL. We cannot put it into
1203 an object block if it requires a standalone definition. */
1204 if (TREE_CODE (decl) == VAR_DECL)
1205 align_variable (decl, 0);
1206 sect = get_variable_section (decl, true);
1207 if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
1208 return NULL;
1210 return get_block_for_section (sect);
1213 /* Make sure block symbol SYMBOL is in block BLOCK. */
1215 static void
1216 change_symbol_block (rtx symbol, struct object_block *block)
1218 if (block != SYMBOL_REF_BLOCK (symbol))
1220 gcc_assert (SYMBOL_REF_BLOCK_OFFSET (symbol) < 0);
1221 SYMBOL_REF_BLOCK (symbol) = block;
1225 /* Return true if it is possible to put DECL in an object_block. */
1227 static bool
1228 use_blocks_for_decl_p (tree decl)
1230 struct symtab_node *snode;
1232 /* Only data DECLs can be placed into object blocks. */
1233 if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != CONST_DECL)
1234 return false;
1236 /* Detect decls created by dw2_force_const_mem. Such decls are
1237 special because DECL_INITIAL doesn't specify the decl's true value.
1238 dw2_output_indirect_constants will instead call assemble_variable
1239 with dont_output_data set to 1 and then print the contents itself. */
1240 if (DECL_INITIAL (decl) == decl)
1241 return false;
1243 /* If this decl is an alias, then we don't want to emit a
1244 definition. */
1245 if (TREE_CODE (decl) == VAR_DECL
1246 && (snode = symtab_node::get (decl)) != NULL
1247 && snode->alias)
1248 return false;
1250 return targetm.use_blocks_for_decl_p (decl);
1253 /* Follow the IDENTIFIER_TRANSPARENT_ALIAS chain starting at *ALIAS
1254 until we find an identifier that is not itself a transparent alias.
1255 Modify the alias passed to it by reference (and all aliases on the
1256 way to the ultimate target), such that they do not have to be
1257 followed again, and return the ultimate target of the alias
1258 chain. */
1260 static inline tree
1261 ultimate_transparent_alias_target (tree *alias)
1263 tree target = *alias;
1265 if (IDENTIFIER_TRANSPARENT_ALIAS (target))
1267 gcc_assert (TREE_CHAIN (target));
1268 target = ultimate_transparent_alias_target (&TREE_CHAIN (target));
1269 gcc_assert (! IDENTIFIER_TRANSPARENT_ALIAS (target)
1270 && ! TREE_CHAIN (target));
1271 *alias = target;
1274 return target;
1277 /* Create the DECL_RTL for a VAR_DECL or FUNCTION_DECL. DECL should
1278 have static storage duration. In other words, it should not be an
1279 automatic variable, including PARM_DECLs.
1281 There is, however, one exception: this function handles variables
1282 explicitly placed in a particular register by the user.
1284 This is never called for PARM_DECL nodes. */
1286 void
1287 make_decl_rtl (tree decl)
1289 const char *name = 0;
1290 int reg_number;
1291 tree id;
1292 rtx x;
1294 /* Check that we are not being given an automatic variable. */
1295 gcc_assert (TREE_CODE (decl) != PARM_DECL
1296 && TREE_CODE (decl) != RESULT_DECL);
1298 /* A weak alias has TREE_PUBLIC set but not the other bits. */
1299 gcc_assert (TREE_CODE (decl) != VAR_DECL
1300 || TREE_STATIC (decl)
1301 || TREE_PUBLIC (decl)
1302 || DECL_EXTERNAL (decl)
1303 || DECL_REGISTER (decl));
1305 /* And that we were not given a type or a label. */
1306 gcc_assert (TREE_CODE (decl) != TYPE_DECL
1307 && TREE_CODE (decl) != LABEL_DECL);
1309 /* For a duplicate declaration, we can be called twice on the
1310 same DECL node. Don't discard the RTL already made. */
1311 if (DECL_RTL_SET_P (decl))
1313 /* If the old RTL had the wrong mode, fix the mode. */
1314 x = DECL_RTL (decl);
1315 if (GET_MODE (x) != DECL_MODE (decl))
1316 SET_DECL_RTL (decl, adjust_address_nv (x, DECL_MODE (decl), 0));
1318 if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1319 return;
1321 /* ??? Another way to do this would be to maintain a hashed
1322 table of such critters. Instead of adding stuff to a DECL
1323 to give certain attributes to it, we could use an external
1324 hash map from DECL to set of attributes. */
1326 /* Let the target reassign the RTL if it wants.
1327 This is necessary, for example, when one machine specific
1328 decl attribute overrides another. */
1329 targetm.encode_section_info (decl, DECL_RTL (decl), false);
1331 /* If the symbol has a SYMBOL_REF_BLOCK field, update it based
1332 on the new decl information. */
1333 if (MEM_P (x)
1334 && GET_CODE (XEXP (x, 0)) == SYMBOL_REF
1335 && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (x, 0)))
1336 change_symbol_block (XEXP (x, 0), get_block_for_decl (decl));
1338 return;
1341 /* If this variable belongs to the global constant pool, retrieve the
1342 pre-computed RTL or recompute it in LTO mode. */
1343 if (TREE_CODE (decl) == VAR_DECL && DECL_IN_CONSTANT_POOL (decl))
1345 SET_DECL_RTL (decl, output_constant_def (DECL_INITIAL (decl), 1));
1346 return;
1349 id = DECL_ASSEMBLER_NAME (decl);
1350 if (TREE_CODE (decl) == FUNCTION_DECL
1351 && cgraph_node::get (decl)
1352 && cgraph_node::get (decl)->instrumentation_clone)
1353 ultimate_transparent_alias_target (&id);
1354 name = IDENTIFIER_POINTER (id);
1356 if (name[0] != '*' && TREE_CODE (decl) != FUNCTION_DECL
1357 && DECL_REGISTER (decl))
1359 error ("register name not specified for %q+D", decl);
1361 else if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1363 const char *asmspec = name+1;
1364 machine_mode mode = DECL_MODE (decl);
1365 reg_number = decode_reg_name (asmspec);
1366 /* First detect errors in declaring global registers. */
1367 if (reg_number == -1)
1368 error ("register name not specified for %q+D", decl);
1369 else if (reg_number < 0)
1370 error ("invalid register name for %q+D", decl);
1371 else if (mode == BLKmode)
1372 error ("data type of %q+D isn%'t suitable for a register",
1373 decl);
1374 else if (!in_hard_reg_set_p (accessible_reg_set, mode, reg_number))
1375 error ("the register specified for %q+D cannot be accessed"
1376 " by the current target", decl);
1377 else if (!in_hard_reg_set_p (operand_reg_set, mode, reg_number))
1378 error ("the register specified for %q+D is not general enough"
1379 " to be used as a register variable", decl);
1380 else if (!HARD_REGNO_MODE_OK (reg_number, mode))
1381 error ("register specified for %q+D isn%'t suitable for data type",
1382 decl);
1383 /* Now handle properly declared static register variables. */
1384 else
1386 int nregs;
1388 if (DECL_INITIAL (decl) != 0 && TREE_STATIC (decl))
1390 DECL_INITIAL (decl) = 0;
1391 error ("global register variable has initial value");
1393 if (TREE_THIS_VOLATILE (decl))
1394 warning (OPT_Wvolatile_register_var,
1395 "optimization may eliminate reads and/or "
1396 "writes to register variables");
1398 /* If the user specified one of the eliminables registers here,
1399 e.g., FRAME_POINTER_REGNUM, we don't want to get this variable
1400 confused with that register and be eliminated. This usage is
1401 somewhat suspect... */
1403 SET_DECL_RTL (decl, gen_rtx_raw_REG (mode, reg_number));
1404 ORIGINAL_REGNO (DECL_RTL (decl)) = reg_number;
1405 REG_USERVAR_P (DECL_RTL (decl)) = 1;
1407 if (TREE_STATIC (decl))
1409 /* Make this register global, so not usable for anything
1410 else. */
1411 #ifdef ASM_DECLARE_REGISTER_GLOBAL
1412 name = IDENTIFIER_POINTER (DECL_NAME (decl));
1413 ASM_DECLARE_REGISTER_GLOBAL (asm_out_file, decl, reg_number, name);
1414 #endif
1415 nregs = hard_regno_nregs[reg_number][mode];
1416 while (nregs > 0)
1417 globalize_reg (decl, reg_number + --nregs);
1420 /* As a register variable, it has no section. */
1421 return;
1423 /* Avoid internal errors from invalid register
1424 specifications. */
1425 SET_DECL_ASSEMBLER_NAME (decl, NULL_TREE);
1426 DECL_HARD_REGISTER (decl) = 0;
1427 return;
1429 /* Now handle ordinary static variables and functions (in memory).
1430 Also handle vars declared register invalidly. */
1431 else if (name[0] == '*')
1433 #ifdef REGISTER_PREFIX
1434 if (strlen (REGISTER_PREFIX) != 0)
1436 reg_number = decode_reg_name (name);
1437 if (reg_number >= 0 || reg_number == -3)
1438 error ("register name given for non-register variable %q+D", decl);
1440 #endif
1443 /* Specifying a section attribute on a variable forces it into a
1444 non-.bss section, and thus it cannot be common. */
1445 /* FIXME: In general this code should not be necessary because
1446 visibility pass is doing the same work. But notice_global_symbol
1447 is called early and it needs to make DECL_RTL to get the name.
1448 we take care of recomputing the DECL_RTL after visibility is changed. */
1449 if (TREE_CODE (decl) == VAR_DECL
1450 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
1451 && DECL_SECTION_NAME (decl) != NULL
1452 && DECL_INITIAL (decl) == NULL_TREE
1453 && DECL_COMMON (decl))
1454 DECL_COMMON (decl) = 0;
1456 /* Variables can't be both common and weak. */
1457 if (TREE_CODE (decl) == VAR_DECL && DECL_WEAK (decl))
1458 DECL_COMMON (decl) = 0;
1460 if (use_object_blocks_p () && use_blocks_for_decl_p (decl))
1461 x = create_block_symbol (name, get_block_for_decl (decl), -1);
1462 else
1464 machine_mode address_mode = Pmode;
1465 if (TREE_TYPE (decl) != error_mark_node)
1467 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
1468 address_mode = targetm.addr_space.address_mode (as);
1470 x = gen_rtx_SYMBOL_REF (address_mode, name);
1472 SYMBOL_REF_WEAK (x) = DECL_WEAK (decl);
1473 SET_SYMBOL_REF_DECL (x, decl);
1475 x = gen_rtx_MEM (DECL_MODE (decl), x);
1476 if (TREE_CODE (decl) != FUNCTION_DECL)
1477 set_mem_attributes (x, decl, 1);
1478 SET_DECL_RTL (decl, x);
1480 /* Optionally set flags or add text to the name to record information
1481 such as that it is a function name.
1482 If the name is changed, the macro ASM_OUTPUT_LABELREF
1483 will have to know how to strip this information. */
1484 targetm.encode_section_info (decl, DECL_RTL (decl), true);
1487 /* Like make_decl_rtl, but inhibit creation of new alias sets when
1488 calling make_decl_rtl. Also, reset DECL_RTL before returning the
1489 rtl. */
1492 make_decl_rtl_for_debug (tree decl)
1494 unsigned int save_aliasing_flag;
1495 rtx rtl;
1497 if (DECL_RTL_SET_P (decl))
1498 return DECL_RTL (decl);
1500 /* Kludge alert! Somewhere down the call chain, make_decl_rtl will
1501 call new_alias_set. If running with -fcompare-debug, sometimes
1502 we do not want to create alias sets that will throw the alias
1503 numbers off in the comparison dumps. So... clearing
1504 flag_strict_aliasing will keep new_alias_set() from creating a
1505 new set. */
1506 save_aliasing_flag = flag_strict_aliasing;
1507 flag_strict_aliasing = 0;
1509 rtl = DECL_RTL (decl);
1510 /* Reset DECL_RTL back, as various parts of the compiler expects
1511 DECL_RTL set meaning it is actually going to be output. */
1512 SET_DECL_RTL (decl, NULL);
1514 flag_strict_aliasing = save_aliasing_flag;
1515 return rtl;
1518 /* Output a string of literal assembler code
1519 for an `asm' keyword used between functions. */
1521 void
1522 assemble_asm (tree string)
1524 const char *p;
1525 app_enable ();
1527 if (TREE_CODE (string) == ADDR_EXPR)
1528 string = TREE_OPERAND (string, 0);
1530 p = TREE_STRING_POINTER (string);
1531 fprintf (asm_out_file, "%s%s\n", p[0] == '\t' ? "" : "\t", p);
1534 /* Write the address of the entity given by SYMBOL to SEC. */
1535 void
1536 assemble_addr_to_section (rtx symbol, section *sec)
1538 switch_to_section (sec);
1539 assemble_align (POINTER_SIZE);
1540 assemble_integer (symbol, POINTER_SIZE_UNITS, POINTER_SIZE, 1);
1543 /* Return the numbered .ctors.N (if CONSTRUCTOR_P) or .dtors.N (if
1544 not) section for PRIORITY. */
1545 section *
1546 get_cdtor_priority_section (int priority, bool constructor_p)
1548 char buf[16];
1550 /* ??? This only works reliably with the GNU linker. */
1551 sprintf (buf, "%s.%.5u",
1552 constructor_p ? ".ctors" : ".dtors",
1553 /* Invert the numbering so the linker puts us in the proper
1554 order; constructors are run from right to left, and the
1555 linker sorts in increasing order. */
1556 MAX_INIT_PRIORITY - priority);
1557 return get_section (buf, SECTION_WRITE, NULL);
1560 void
1561 default_named_section_asm_out_destructor (rtx symbol, int priority)
1563 section *sec;
1565 if (priority != DEFAULT_INIT_PRIORITY)
1566 sec = get_cdtor_priority_section (priority,
1567 /*constructor_p=*/false);
1568 else
1569 sec = get_section (".dtors", SECTION_WRITE, NULL);
1571 assemble_addr_to_section (symbol, sec);
1574 #ifdef DTORS_SECTION_ASM_OP
1575 void
1576 default_dtor_section_asm_out_destructor (rtx symbol,
1577 int priority ATTRIBUTE_UNUSED)
1579 assemble_addr_to_section (symbol, dtors_section);
1581 #endif
1583 void
1584 default_named_section_asm_out_constructor (rtx symbol, int priority)
1586 section *sec;
1588 if (priority != DEFAULT_INIT_PRIORITY)
1589 sec = get_cdtor_priority_section (priority,
1590 /*constructor_p=*/true);
1591 else
1592 sec = get_section (".ctors", SECTION_WRITE, NULL);
1594 assemble_addr_to_section (symbol, sec);
1597 #ifdef CTORS_SECTION_ASM_OP
1598 void
1599 default_ctor_section_asm_out_constructor (rtx symbol,
1600 int priority ATTRIBUTE_UNUSED)
1602 assemble_addr_to_section (symbol, ctors_section);
1604 #endif
1606 /* CONSTANT_POOL_BEFORE_FUNCTION may be defined as an expression with
1607 a nonzero value if the constant pool should be output before the
1608 start of the function, or a zero value if the pool should output
1609 after the end of the function. The default is to put it before the
1610 start. */
1612 #ifndef CONSTANT_POOL_BEFORE_FUNCTION
1613 #define CONSTANT_POOL_BEFORE_FUNCTION 1
1614 #endif
1616 /* DECL is an object (either VAR_DECL or FUNCTION_DECL) which is going
1617 to be output to assembler.
1618 Set first_global_object_name and weak_global_object_name as appropriate. */
1620 void
1621 notice_global_symbol (tree decl)
1623 const char **type = &first_global_object_name;
1625 if (first_global_object_name
1626 || !TREE_PUBLIC (decl)
1627 || DECL_EXTERNAL (decl)
1628 || !DECL_NAME (decl)
1629 || (TREE_CODE (decl) != FUNCTION_DECL
1630 && (TREE_CODE (decl) != VAR_DECL
1631 || (DECL_COMMON (decl)
1632 && (DECL_INITIAL (decl) == 0
1633 || DECL_INITIAL (decl) == error_mark_node))))
1634 || !MEM_P (DECL_RTL (decl)))
1635 return;
1637 /* We win when global object is found, but it is useful to know about weak
1638 symbol as well so we can produce nicer unique names. */
1639 if (DECL_WEAK (decl) || DECL_ONE_ONLY (decl) || flag_shlib)
1640 type = &weak_global_object_name;
1642 if (!*type)
1644 const char *p;
1645 const char *name;
1646 rtx decl_rtl = DECL_RTL (decl);
1648 p = targetm.strip_name_encoding (XSTR (XEXP (decl_rtl, 0), 0));
1649 name = ggc_strdup (p);
1651 *type = name;
1655 /* If not using flag_reorder_blocks_and_partition, decide early whether the
1656 current function goes into the cold section, so that targets can use
1657 current_function_section during RTL expansion. DECL describes the
1658 function. */
1660 void
1661 decide_function_section (tree decl)
1663 first_function_block_is_cold = false;
1665 if (flag_reorder_blocks_and_partition)
1666 /* We will decide in assemble_start_function. */
1667 return;
1669 if (DECL_SECTION_NAME (decl))
1671 struct cgraph_node *node = cgraph_node::get (current_function_decl);
1672 /* Calls to function_section rely on first_function_block_is_cold
1673 being accurate. */
1674 first_function_block_is_cold = (node
1675 && node->frequency
1676 == NODE_FREQUENCY_UNLIKELY_EXECUTED);
1679 in_cold_section_p = first_function_block_is_cold;
1682 /* Get the function's name, as described by its RTL. This may be
1683 different from the DECL_NAME name used in the source file. */
1684 const char *
1685 get_fnname_from_decl (tree decl)
1687 rtx x = DECL_RTL (decl);
1688 gcc_assert (MEM_P (x));
1689 x = XEXP (x, 0);
1690 gcc_assert (GET_CODE (x) == SYMBOL_REF);
1691 return XSTR (x, 0);
1694 /* Output assembler code for the constant pool of a function and associated
1695 with defining the name of the function. DECL describes the function.
1696 NAME is the function's name. For the constant pool, we use the current
1697 constant pool data. */
1699 void
1700 assemble_start_function (tree decl, const char *fnname)
1702 int align;
1703 char tmp_label[100];
1704 bool hot_label_written = false;
1706 if (flag_reorder_blocks_and_partition)
1708 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTB", const_labelno);
1709 crtl->subsections.hot_section_label = ggc_strdup (tmp_label);
1710 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDB", const_labelno);
1711 crtl->subsections.cold_section_label = ggc_strdup (tmp_label);
1712 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTE", const_labelno);
1713 crtl->subsections.hot_section_end_label = ggc_strdup (tmp_label);
1714 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDE", const_labelno);
1715 crtl->subsections.cold_section_end_label = ggc_strdup (tmp_label);
1716 const_labelno++;
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 /* Make sure the not and cold text (code) sections are properly
1734 aligned. This is necessary here in the case where the function
1735 has both hot and cold sections, because we don't want to re-set
1736 the alignment when the section switch happens mid-function. */
1738 if (flag_reorder_blocks_and_partition)
1740 first_function_block_is_cold = false;
1742 switch_to_section (unlikely_text_section ());
1743 assemble_align (DECL_ALIGN (decl));
1744 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_label);
1746 /* When the function starts with a cold section, we need to explicitly
1747 align the hot section and write out the hot section label.
1748 But if the current function is a thunk, we do not have a CFG. */
1749 if (!cfun->is_thunk
1750 && BB_PARTITION (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb) == BB_COLD_PARTITION)
1752 switch_to_section (text_section);
1753 assemble_align (DECL_ALIGN (decl));
1754 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
1755 hot_label_written = true;
1756 first_function_block_is_cold = true;
1758 in_cold_section_p = first_function_block_is_cold;
1762 /* Switch to the correct text section for the start of the function. */
1764 switch_to_section (function_section (decl));
1765 if (flag_reorder_blocks_and_partition
1766 && !hot_label_written)
1767 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
1769 /* Tell assembler to move to target machine's alignment for functions. */
1770 align = floor_log2 (DECL_ALIGN (decl) / BITS_PER_UNIT);
1771 if (align > 0)
1773 ASM_OUTPUT_ALIGN (asm_out_file, align);
1776 /* Handle a user-specified function alignment.
1777 Note that we still need to align to DECL_ALIGN, as above,
1778 because ASM_OUTPUT_MAX_SKIP_ALIGN might not do any alignment at all. */
1779 if (! DECL_USER_ALIGN (decl)
1780 && align_functions_log > align
1781 && optimize_function_for_speed_p (cfun))
1783 #ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
1784 ASM_OUTPUT_MAX_SKIP_ALIGN (asm_out_file,
1785 align_functions_log, align_functions - 1);
1786 #else
1787 ASM_OUTPUT_ALIGN (asm_out_file, align_functions_log);
1788 #endif
1791 #ifdef ASM_OUTPUT_FUNCTION_PREFIX
1792 ASM_OUTPUT_FUNCTION_PREFIX (asm_out_file, fnname);
1793 #endif
1795 if (!DECL_IGNORED_P (decl))
1796 (*debug_hooks->begin_function) (decl);
1798 /* Make function name accessible from other files, if appropriate. */
1800 if (TREE_PUBLIC (decl)
1801 || (cgraph_node::get (decl)->instrumentation_clone
1802 && cgraph_node::get (decl)->instrumented_version
1803 && TREE_PUBLIC (cgraph_node::get (decl)->instrumented_version->decl)))
1805 notice_global_symbol (decl);
1807 globalize_decl (decl);
1809 maybe_assemble_visibility (decl);
1812 if (DECL_PRESERVE_P (decl))
1813 targetm.asm_out.mark_decl_preserved (fnname);
1815 /* Do any machine/system dependent processing of the function name. */
1816 #ifdef ASM_DECLARE_FUNCTION_NAME
1817 ASM_DECLARE_FUNCTION_NAME (asm_out_file, fnname, current_function_decl);
1818 #else
1819 /* Standard thing is just output label for the function. */
1820 ASM_OUTPUT_FUNCTION_LABEL (asm_out_file, fnname, current_function_decl);
1821 #endif /* ASM_DECLARE_FUNCTION_NAME */
1823 if (lookup_attribute ("no_split_stack", DECL_ATTRIBUTES (decl)))
1824 saw_no_split_stack = true;
1827 /* Output assembler code associated with defining the size of the
1828 function. DECL describes the function. NAME is the function's name. */
1830 void
1831 assemble_end_function (tree decl, const char *fnname ATTRIBUTE_UNUSED)
1833 #ifdef ASM_DECLARE_FUNCTION_SIZE
1834 /* We could have switched section in the middle of the function. */
1835 if (flag_reorder_blocks_and_partition)
1836 switch_to_section (function_section (decl));
1837 ASM_DECLARE_FUNCTION_SIZE (asm_out_file, fnname, decl);
1838 #endif
1839 if (! CONSTANT_POOL_BEFORE_FUNCTION)
1841 output_constant_pool (fnname, decl);
1842 switch_to_section (function_section (decl)); /* need to switch back */
1844 /* Output labels for end of hot/cold text sections (to be used by
1845 debug info.) */
1846 if (flag_reorder_blocks_and_partition)
1848 section *save_text_section;
1850 save_text_section = in_section;
1851 switch_to_section (unlikely_text_section ());
1852 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_end_label);
1853 if (first_function_block_is_cold)
1854 switch_to_section (text_section);
1855 else
1856 switch_to_section (function_section (decl));
1857 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_end_label);
1858 switch_to_section (save_text_section);
1862 /* Assemble code to leave SIZE bytes of zeros. */
1864 void
1865 assemble_zeros (unsigned HOST_WIDE_INT size)
1867 /* Do no output if -fsyntax-only. */
1868 if (flag_syntax_only)
1869 return;
1871 #ifdef ASM_NO_SKIP_IN_TEXT
1872 /* The `space' pseudo in the text section outputs nop insns rather than 0s,
1873 so we must output 0s explicitly in the text section. */
1874 if (ASM_NO_SKIP_IN_TEXT && (in_section->common.flags & SECTION_CODE) != 0)
1876 unsigned HOST_WIDE_INT i;
1877 for (i = 0; i < size; i++)
1878 assemble_integer (const0_rtx, 1, BITS_PER_UNIT, 1);
1880 else
1881 #endif
1882 if (size > 0)
1883 ASM_OUTPUT_SKIP (asm_out_file, size);
1886 /* Assemble an alignment pseudo op for an ALIGN-bit boundary. */
1888 void
1889 assemble_align (int align)
1891 if (align > BITS_PER_UNIT)
1893 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
1897 /* Assemble a string constant with the specified C string as contents. */
1899 void
1900 assemble_string (const char *p, int size)
1902 int pos = 0;
1903 int maximum = 2000;
1905 /* If the string is very long, split it up. */
1907 while (pos < size)
1909 int thissize = size - pos;
1910 if (thissize > maximum)
1911 thissize = maximum;
1913 ASM_OUTPUT_ASCII (asm_out_file, p, thissize);
1915 pos += thissize;
1916 p += thissize;
1921 /* A noswitch_section_callback for lcomm_section. */
1923 static bool
1924 emit_local (tree decl ATTRIBUTE_UNUSED,
1925 const char *name ATTRIBUTE_UNUSED,
1926 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1927 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1929 #if defined ASM_OUTPUT_ALIGNED_DECL_LOCAL
1930 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, decl, name,
1931 size, DECL_ALIGN (decl));
1932 return true;
1933 #elif defined ASM_OUTPUT_ALIGNED_LOCAL
1934 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, DECL_ALIGN (decl));
1935 return true;
1936 #else
1937 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
1938 return false;
1939 #endif
1942 /* A noswitch_section_callback for bss_noswitch_section. */
1944 #if defined ASM_OUTPUT_ALIGNED_BSS
1945 static bool
1946 emit_bss (tree decl ATTRIBUTE_UNUSED,
1947 const char *name ATTRIBUTE_UNUSED,
1948 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1949 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1951 #if defined ASM_OUTPUT_ALIGNED_BSS
1952 ASM_OUTPUT_ALIGNED_BSS (asm_out_file, decl, name, size,
1953 get_variable_align (decl));
1954 return true;
1955 #endif
1957 #endif
1959 /* A noswitch_section_callback for comm_section. */
1961 static bool
1962 emit_common (tree decl ATTRIBUTE_UNUSED,
1963 const char *name ATTRIBUTE_UNUSED,
1964 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1965 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1967 #if defined ASM_OUTPUT_ALIGNED_DECL_COMMON
1968 ASM_OUTPUT_ALIGNED_DECL_COMMON (asm_out_file, decl, name,
1969 size, get_variable_align (decl));
1970 return true;
1971 #elif defined ASM_OUTPUT_ALIGNED_COMMON
1972 ASM_OUTPUT_ALIGNED_COMMON (asm_out_file, name, size,
1973 get_variable_align (decl));
1974 return true;
1975 #else
1976 ASM_OUTPUT_COMMON (asm_out_file, name, size, rounded);
1977 return false;
1978 #endif
1981 /* A noswitch_section_callback for tls_comm_section. */
1983 static bool
1984 emit_tls_common (tree decl ATTRIBUTE_UNUSED,
1985 const char *name ATTRIBUTE_UNUSED,
1986 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1987 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1989 #ifdef ASM_OUTPUT_TLS_COMMON
1990 ASM_OUTPUT_TLS_COMMON (asm_out_file, decl, name, size);
1991 return true;
1992 #else
1993 sorry ("thread-local COMMON data not implemented");
1994 return true;
1995 #endif
1998 /* Assemble DECL given that it belongs in SECTION_NOSWITCH section SECT.
1999 NAME is the name of DECL's SYMBOL_REF. */
2001 static void
2002 assemble_noswitch_variable (tree decl, const char *name, section *sect,
2003 unsigned int align)
2005 unsigned HOST_WIDE_INT size, rounded;
2007 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
2008 rounded = size;
2010 if ((flag_sanitize & SANITIZE_ADDRESS) && asan_protect_global (decl))
2011 size += asan_red_zone_size (size);
2013 /* Don't allocate zero bytes of common,
2014 since that means "undefined external" in the linker. */
2015 if (size == 0)
2016 rounded = 1;
2018 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
2019 so that each uninitialized object starts on such a boundary. */
2020 rounded += (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1;
2021 rounded = (rounded / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
2022 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
2024 if (!sect->noswitch.callback (decl, name, size, rounded)
2025 && (unsigned HOST_WIDE_INT) (align / BITS_PER_UNIT) > rounded)
2026 error ("requested alignment for %q+D is greater than "
2027 "implemented alignment of %wu", decl, rounded);
2030 /* A subroutine of assemble_variable. Output the label and contents of
2031 DECL, whose address is a SYMBOL_REF with name NAME. DONT_OUTPUT_DATA
2032 is as for assemble_variable. */
2034 static void
2035 assemble_variable_contents (tree decl, const char *name,
2036 bool dont_output_data)
2038 /* Do any machine/system dependent processing of the object. */
2039 #ifdef ASM_DECLARE_OBJECT_NAME
2040 last_assemble_variable_decl = decl;
2041 ASM_DECLARE_OBJECT_NAME (asm_out_file, name, decl);
2042 #else
2043 /* Standard thing is just output label for the object. */
2044 ASM_OUTPUT_LABEL (asm_out_file, name);
2045 #endif /* ASM_DECLARE_OBJECT_NAME */
2047 if (!dont_output_data)
2049 /* Caller is supposed to use varpool_get_constructor when it wants
2050 to output the body. */
2051 gcc_assert (!in_lto_p || DECL_INITIAL (decl) != error_mark_node);
2052 if (DECL_INITIAL (decl)
2053 && DECL_INITIAL (decl) != error_mark_node
2054 && !initializer_zerop (DECL_INITIAL (decl)))
2055 /* Output the actual data. */
2056 output_constant (DECL_INITIAL (decl),
2057 tree_to_uhwi (DECL_SIZE_UNIT (decl)),
2058 get_variable_align (decl));
2059 else
2060 /* Leave space for it. */
2061 assemble_zeros (tree_to_uhwi (DECL_SIZE_UNIT (decl)));
2062 targetm.asm_out.decl_end ();
2066 /* Write out assembly for the variable DECL, which is not defined in
2067 the current translation unit. */
2068 void
2069 assemble_undefined_decl (tree decl)
2071 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
2072 targetm.asm_out.assemble_undefined_decl (asm_out_file, name, decl);
2075 /* Assemble everything that is needed for a variable or function declaration.
2076 Not used for automatic variables, and not used for function definitions.
2077 Should not be called for variables of incomplete structure type.
2079 TOP_LEVEL is nonzero if this variable has file scope.
2080 AT_END is nonzero if this is the special handling, at end of compilation,
2081 to define things that have had only tentative definitions.
2082 DONT_OUTPUT_DATA if nonzero means don't actually output the
2083 initial value (that will be done by the caller). */
2085 void
2086 assemble_variable (tree decl, int top_level ATTRIBUTE_UNUSED,
2087 int at_end ATTRIBUTE_UNUSED, int dont_output_data)
2089 const char *name;
2090 rtx decl_rtl, symbol;
2091 section *sect;
2092 unsigned int align;
2093 bool asan_protected = false;
2095 /* This function is supposed to handle VARIABLES. Ensure we have one. */
2096 gcc_assert (TREE_CODE (decl) == VAR_DECL);
2098 /* Emulated TLS had better not get this far. */
2099 gcc_checking_assert (targetm.have_tls || !DECL_THREAD_LOCAL_P (decl));
2101 last_assemble_variable_decl = 0;
2103 /* Normally no need to say anything here for external references,
2104 since assemble_external is called by the language-specific code
2105 when a declaration is first seen. */
2107 if (DECL_EXTERNAL (decl))
2108 return;
2110 /* Do nothing for global register variables. */
2111 if (DECL_RTL_SET_P (decl) && REG_P (DECL_RTL (decl)))
2113 TREE_ASM_WRITTEN (decl) = 1;
2114 return;
2117 /* If type was incomplete when the variable was declared,
2118 see if it is complete now. */
2120 if (DECL_SIZE (decl) == 0)
2121 layout_decl (decl, 0);
2123 /* Still incomplete => don't allocate it; treat the tentative defn
2124 (which is what it must have been) as an `extern' reference. */
2126 if (!dont_output_data && DECL_SIZE (decl) == 0)
2128 error ("storage size of %q+D isn%'t known", decl);
2129 TREE_ASM_WRITTEN (decl) = 1;
2130 return;
2133 /* The first declaration of a variable that comes through this function
2134 decides whether it is global (in C, has external linkage)
2135 or local (in C, has internal linkage). So do nothing more
2136 if this function has already run. */
2138 if (TREE_ASM_WRITTEN (decl))
2139 return;
2141 /* Make sure targetm.encode_section_info is invoked before we set
2142 ASM_WRITTEN. */
2143 decl_rtl = DECL_RTL (decl);
2145 TREE_ASM_WRITTEN (decl) = 1;
2147 /* Do no output if -fsyntax-only. */
2148 if (flag_syntax_only)
2149 return;
2151 if (! dont_output_data
2152 && ! valid_constant_size_p (DECL_SIZE_UNIT (decl)))
2154 error ("size of variable %q+D is too large", decl);
2155 return;
2158 gcc_assert (MEM_P (decl_rtl));
2159 gcc_assert (GET_CODE (XEXP (decl_rtl, 0)) == SYMBOL_REF);
2160 symbol = XEXP (decl_rtl, 0);
2162 /* If this symbol belongs to the tree constant pool, output the constant
2163 if it hasn't already been written. */
2164 if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
2166 tree decl = SYMBOL_REF_DECL (symbol);
2167 if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
2168 output_constant_def_contents (symbol);
2169 return;
2172 app_disable ();
2174 name = XSTR (symbol, 0);
2175 if (TREE_PUBLIC (decl) && DECL_NAME (decl))
2176 notice_global_symbol (decl);
2178 /* Compute the alignment of this data. */
2180 align_variable (decl, dont_output_data);
2182 if ((flag_sanitize & SANITIZE_ADDRESS)
2183 && asan_protect_global (decl))
2185 asan_protected = true;
2186 DECL_ALIGN (decl) = MAX (DECL_ALIGN (decl),
2187 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
2190 set_mem_align (decl_rtl, DECL_ALIGN (decl));
2192 align = get_variable_align (decl);
2194 if (TREE_PUBLIC (decl))
2195 maybe_assemble_visibility (decl);
2197 if (DECL_PRESERVE_P (decl))
2198 targetm.asm_out.mark_decl_preserved (name);
2200 /* First make the assembler name(s) global if appropriate. */
2201 sect = get_variable_section (decl, false);
2202 if (TREE_PUBLIC (decl)
2203 && (sect->common.flags & SECTION_COMMON) == 0)
2204 globalize_decl (decl);
2206 /* Output any data that we will need to use the address of. */
2207 if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node)
2208 output_addressed_constants (DECL_INITIAL (decl));
2210 /* dbxout.c needs to know this. */
2211 if (sect && (sect->common.flags & SECTION_CODE) != 0)
2212 DECL_IN_TEXT_SECTION (decl) = 1;
2214 /* If the decl is part of an object_block, make sure that the decl
2215 has been positioned within its block, but do not write out its
2216 definition yet. output_object_blocks will do that later. */
2217 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
2219 gcc_assert (!dont_output_data);
2220 place_block_symbol (symbol);
2222 else if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
2223 assemble_noswitch_variable (decl, name, sect, align);
2224 else
2226 /* The following bit of code ensures that vtable_map
2227 variables are not only in the comdat section, but that
2228 each variable has its own unique comdat name. If this
2229 code is removed, the variables end up in the same section
2230 with a single comdat name.
2232 FIXME: resolve_unique_section needs to deal better with
2233 decls with both DECL_SECTION_NAME and DECL_ONE_ONLY. Once
2234 that is fixed, this if-else statement can be replaced with
2235 a single call to "switch_to_section (sect)". */
2236 if (sect->named.name
2237 && (strcmp (sect->named.name, ".vtable_map_vars") == 0))
2239 #if defined (OBJECT_FORMAT_ELF)
2240 targetm.asm_out.named_section (sect->named.name,
2241 sect->named.common.flags
2242 | SECTION_LINKONCE,
2243 DECL_NAME (decl));
2244 in_section = sect;
2245 #else
2246 switch_to_section (sect);
2247 #endif
2249 else
2250 switch_to_section (sect);
2251 if (align > BITS_PER_UNIT)
2252 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
2253 assemble_variable_contents (decl, name, dont_output_data);
2254 if (asan_protected)
2256 unsigned HOST_WIDE_INT int size
2257 = tree_to_uhwi (DECL_SIZE_UNIT (decl));
2258 assemble_zeros (asan_red_zone_size (size));
2264 /* Given a function declaration (FN_DECL), this function assembles the
2265 function into the .preinit_array section. */
2267 void
2268 assemble_vtv_preinit_initializer (tree fn_decl)
2270 section *sect;
2271 unsigned flags = SECTION_WRITE;
2272 rtx symbol = XEXP (DECL_RTL (fn_decl), 0);
2274 flags |= SECTION_NOTYPE;
2275 sect = get_section (".preinit_array", flags, fn_decl);
2276 switch_to_section (sect);
2277 assemble_addr_to_section (symbol, sect);
2280 /* Return 1 if type TYPE contains any pointers. */
2282 static int
2283 contains_pointers_p (tree type)
2285 switch (TREE_CODE (type))
2287 case POINTER_TYPE:
2288 case REFERENCE_TYPE:
2289 /* I'm not sure whether OFFSET_TYPE needs this treatment,
2290 so I'll play safe and return 1. */
2291 case OFFSET_TYPE:
2292 return 1;
2294 case RECORD_TYPE:
2295 case UNION_TYPE:
2296 case QUAL_UNION_TYPE:
2298 tree fields;
2299 /* For a type that has fields, see if the fields have pointers. */
2300 for (fields = TYPE_FIELDS (type); fields; fields = DECL_CHAIN (fields))
2301 if (TREE_CODE (fields) == FIELD_DECL
2302 && contains_pointers_p (TREE_TYPE (fields)))
2303 return 1;
2304 return 0;
2307 case ARRAY_TYPE:
2308 /* An array type contains pointers if its element type does. */
2309 return contains_pointers_p (TREE_TYPE (type));
2311 default:
2312 return 0;
2316 /* We delay assemble_external processing until
2317 the compilation unit is finalized. This is the best we can do for
2318 right now (i.e. stage 3 of GCC 4.0) - the right thing is to delay
2319 it all the way to final. See PR 17982 for further discussion. */
2320 static GTY(()) tree pending_assemble_externals;
2322 #ifdef ASM_OUTPUT_EXTERNAL
2323 /* Some targets delay some output to final using TARGET_ASM_FILE_END.
2324 As a result, assemble_external can be called after the list of externals
2325 is processed and the pointer set destroyed. */
2326 static bool pending_assemble_externals_processed;
2328 /* Avoid O(external_decls**2) lookups in the pending_assemble_externals
2329 TREE_LIST in assemble_external. */
2330 static hash_set<tree> *pending_assemble_externals_set;
2332 /* True if DECL is a function decl for which no out-of-line copy exists.
2333 It is assumed that DECL's assembler name has been set. */
2335 static bool
2336 incorporeal_function_p (tree decl)
2338 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
2340 const char *name;
2342 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
2343 && (DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA
2344 || DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA_WITH_ALIGN))
2345 return true;
2347 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2348 /* Atomic or sync builtins which have survived this far will be
2349 resolved externally and therefore are not incorporeal. */
2350 if (strncmp (name, "__builtin_", 10) == 0)
2351 return true;
2353 return false;
2356 /* Actually do the tests to determine if this is necessary, and invoke
2357 ASM_OUTPUT_EXTERNAL. */
2358 static void
2359 assemble_external_real (tree decl)
2361 rtx rtl = DECL_RTL (decl);
2363 if (MEM_P (rtl) && GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF
2364 && !SYMBOL_REF_USED (XEXP (rtl, 0))
2365 && !incorporeal_function_p (decl))
2367 /* Some systems do require some output. */
2368 SYMBOL_REF_USED (XEXP (rtl, 0)) = 1;
2369 ASM_OUTPUT_EXTERNAL (asm_out_file, decl, XSTR (XEXP (rtl, 0), 0));
2372 #endif
2374 void
2375 process_pending_assemble_externals (void)
2377 #ifdef ASM_OUTPUT_EXTERNAL
2378 tree list;
2379 for (list = pending_assemble_externals; list; list = TREE_CHAIN (list))
2380 assemble_external_real (TREE_VALUE (list));
2382 pending_assemble_externals = 0;
2383 pending_assemble_externals_processed = true;
2384 delete pending_assemble_externals_set;
2385 #endif
2388 /* This TREE_LIST contains any weak symbol declarations waiting
2389 to be emitted. */
2390 static GTY(()) tree weak_decls;
2392 /* Output something to declare an external symbol to the assembler,
2393 and qualifiers such as weakness. (Most assemblers don't need
2394 extern declaration, so we normally output nothing.) Do nothing if
2395 DECL is not external. */
2397 void
2398 assemble_external (tree decl ATTRIBUTE_UNUSED)
2400 /* Make sure that the ASM_OUT_FILE is open.
2401 If it's not, we should not be calling this function. */
2402 gcc_assert (asm_out_file);
2404 /* In a perfect world, the following condition would be true.
2405 Sadly, the Java and Go front ends emit assembly *from the front end*,
2406 bypassing the call graph. See PR52739. Fix before GCC 4.8. */
2407 #if 0
2408 /* This function should only be called if we are expanding, or have
2409 expanded, to RTL.
2410 Ideally, only final.c would be calling this function, but it is
2411 not clear whether that would break things somehow. See PR 17982
2412 for further discussion. */
2413 gcc_assert (state == EXPANSION
2414 || state == FINISHED);
2415 #endif
2417 if (!DECL_P (decl) || !DECL_EXTERNAL (decl) || !TREE_PUBLIC (decl))
2418 return;
2420 /* We want to output annotation for weak and external symbols at
2421 very last to check if they are references or not. */
2423 if (TARGET_SUPPORTS_WEAK
2424 && DECL_WEAK (decl)
2425 /* TREE_STATIC is a weird and abused creature which is not
2426 generally the right test for whether an entity has been
2427 locally emitted, inlined or otherwise not-really-extern, but
2428 for declarations that can be weak, it happens to be
2429 match. */
2430 && !TREE_STATIC (decl)
2431 && lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
2432 && value_member (decl, weak_decls) == NULL_TREE)
2433 weak_decls = tree_cons (NULL, decl, weak_decls);
2435 #ifdef ASM_OUTPUT_EXTERNAL
2436 if (pending_assemble_externals_processed)
2438 assemble_external_real (decl);
2439 return;
2442 if (! pending_assemble_externals_set->add (decl))
2443 pending_assemble_externals = tree_cons (NULL, decl,
2444 pending_assemble_externals);
2445 #endif
2448 /* Similar, for calling a library function FUN. */
2450 void
2451 assemble_external_libcall (rtx fun)
2453 /* Declare library function name external when first used, if nec. */
2454 if (! SYMBOL_REF_USED (fun))
2456 SYMBOL_REF_USED (fun) = 1;
2457 targetm.asm_out.external_libcall (fun);
2461 /* Assemble a label named NAME. */
2463 void
2464 assemble_label (FILE *file, const char *name)
2466 ASM_OUTPUT_LABEL (file, name);
2469 /* Set the symbol_referenced flag for ID. */
2470 void
2471 mark_referenced (tree id)
2473 TREE_SYMBOL_REFERENCED (id) = 1;
2476 /* Set the symbol_referenced flag for DECL and notify callgraph. */
2477 void
2478 mark_decl_referenced (tree decl)
2480 if (TREE_CODE (decl) == FUNCTION_DECL)
2482 /* Extern inline functions don't become needed when referenced.
2483 If we know a method will be emitted in other TU and no new
2484 functions can be marked reachable, just use the external
2485 definition. */
2486 struct cgraph_node *node = cgraph_node::get_create (decl);
2487 if (!DECL_EXTERNAL (decl)
2488 && !node->definition)
2489 node->mark_force_output ();
2491 else if (TREE_CODE (decl) == VAR_DECL)
2493 varpool_node *node = varpool_node::get_create (decl);
2494 /* C++ frontend use mark_decl_references to force COMDAT variables
2495 to be output that might appear dead otherwise. */
2496 node->force_output = true;
2498 /* else do nothing - we can get various sorts of CST nodes here,
2499 which do not need to be marked. */
2503 /* Output to FILE (an assembly file) a reference to NAME. If NAME
2504 starts with a *, the rest of NAME is output verbatim. Otherwise
2505 NAME is transformed in a target-specific way (usually by the
2506 addition of an underscore). */
2508 void
2509 assemble_name_raw (FILE *file, const char *name)
2511 if (name[0] == '*')
2512 fputs (&name[1], file);
2513 else
2514 ASM_OUTPUT_LABELREF (file, name);
2517 /* Like assemble_name_raw, but should be used when NAME might refer to
2518 an entity that is also represented as a tree (like a function or
2519 variable). If NAME does refer to such an entity, that entity will
2520 be marked as referenced. */
2522 void
2523 assemble_name (FILE *file, const char *name)
2525 const char *real_name;
2526 tree id;
2528 real_name = targetm.strip_name_encoding (name);
2530 id = maybe_get_identifier (real_name);
2531 if (id)
2533 tree id_orig = id;
2535 mark_referenced (id);
2536 ultimate_transparent_alias_target (&id);
2537 if (id != id_orig)
2538 name = IDENTIFIER_POINTER (id);
2539 gcc_assert (! TREE_CHAIN (id));
2542 assemble_name_raw (file, name);
2545 /* Allocate SIZE bytes writable static space with a gensym name
2546 and return an RTX to refer to its address. */
2549 assemble_static_space (unsigned HOST_WIDE_INT size)
2551 char name[12];
2552 const char *namestring;
2553 rtx x;
2555 ASM_GENERATE_INTERNAL_LABEL (name, "LF", const_labelno);
2556 ++const_labelno;
2557 namestring = ggc_strdup (name);
2559 x = gen_rtx_SYMBOL_REF (Pmode, namestring);
2560 SYMBOL_REF_FLAGS (x) = SYMBOL_FLAG_LOCAL;
2562 #ifdef ASM_OUTPUT_ALIGNED_DECL_LOCAL
2563 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, NULL_TREE, name, size,
2564 BIGGEST_ALIGNMENT);
2565 #else
2566 #ifdef ASM_OUTPUT_ALIGNED_LOCAL
2567 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, BIGGEST_ALIGNMENT);
2568 #else
2570 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
2571 so that each uninitialized object starts on such a boundary. */
2572 /* Variable `rounded' might or might not be used in ASM_OUTPUT_LOCAL. */
2573 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED
2574 = ((size + (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1)
2575 / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
2576 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
2577 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
2579 #endif
2580 #endif
2581 return x;
2584 /* Assemble the static constant template for function entry trampolines.
2585 This is done at most once per compilation.
2586 Returns an RTX for the address of the template. */
2588 static GTY(()) rtx initial_trampoline;
2591 assemble_trampoline_template (void)
2593 char label[256];
2594 const char *name;
2595 int align;
2596 rtx symbol;
2598 gcc_assert (targetm.asm_out.trampoline_template != NULL);
2600 if (initial_trampoline)
2601 return initial_trampoline;
2603 /* By default, put trampoline templates in read-only data section. */
2605 #ifdef TRAMPOLINE_SECTION
2606 switch_to_section (TRAMPOLINE_SECTION);
2607 #else
2608 switch_to_section (readonly_data_section);
2609 #endif
2611 /* Write the assembler code to define one. */
2612 align = floor_log2 (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
2613 if (align > 0)
2614 ASM_OUTPUT_ALIGN (asm_out_file, align);
2616 targetm.asm_out.internal_label (asm_out_file, "LTRAMP", 0);
2617 targetm.asm_out.trampoline_template (asm_out_file);
2619 /* Record the rtl to refer to it. */
2620 ASM_GENERATE_INTERNAL_LABEL (label, "LTRAMP", 0);
2621 name = ggc_strdup (label);
2622 symbol = gen_rtx_SYMBOL_REF (Pmode, name);
2623 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_LOCAL;
2625 initial_trampoline = gen_const_mem (BLKmode, symbol);
2626 set_mem_align (initial_trampoline, TRAMPOLINE_ALIGNMENT);
2627 set_mem_size (initial_trampoline, TRAMPOLINE_SIZE);
2629 return initial_trampoline;
2632 /* A and B are either alignments or offsets. Return the minimum alignment
2633 that may be assumed after adding the two together. */
2635 static inline unsigned
2636 min_align (unsigned int a, unsigned int b)
2638 return (a | b) & -(a | b);
2641 /* Return the assembler directive for creating a given kind of integer
2642 object. SIZE is the number of bytes in the object and ALIGNED_P
2643 indicates whether it is known to be aligned. Return NULL if the
2644 assembly dialect has no such directive.
2646 The returned string should be printed at the start of a new line and
2647 be followed immediately by the object's initial value. */
2649 const char *
2650 integer_asm_op (int size, int aligned_p)
2652 struct asm_int_op *ops;
2654 if (aligned_p)
2655 ops = &targetm.asm_out.aligned_op;
2656 else
2657 ops = &targetm.asm_out.unaligned_op;
2659 switch (size)
2661 case 1:
2662 return targetm.asm_out.byte_op;
2663 case 2:
2664 return ops->hi;
2665 case 4:
2666 return ops->si;
2667 case 8:
2668 return ops->di;
2669 case 16:
2670 return ops->ti;
2671 default:
2672 return NULL;
2676 /* Use directive OP to assemble an integer object X. Print OP at the
2677 start of the line, followed immediately by the value of X. */
2679 void
2680 assemble_integer_with_op (const char *op, rtx x)
2682 fputs (op, asm_out_file);
2683 output_addr_const (asm_out_file, x);
2684 fputc ('\n', asm_out_file);
2687 /* The default implementation of the asm_out.integer target hook. */
2689 bool
2690 default_assemble_integer (rtx x ATTRIBUTE_UNUSED,
2691 unsigned int size ATTRIBUTE_UNUSED,
2692 int aligned_p ATTRIBUTE_UNUSED)
2694 const char *op = integer_asm_op (size, aligned_p);
2695 /* Avoid GAS bugs for large values. Specifically negative values whose
2696 absolute value fits in a bfd_vma, but not in a bfd_signed_vma. */
2697 if (size > UNITS_PER_WORD && size > POINTER_SIZE_UNITS)
2698 return false;
2699 return op && (assemble_integer_with_op (op, x), true);
2702 /* Assemble the integer constant X into an object of SIZE bytes. ALIGN is
2703 the alignment of the integer in bits. Return 1 if we were able to output
2704 the constant, otherwise 0. We must be able to output the constant,
2705 if FORCE is nonzero. */
2707 bool
2708 assemble_integer (rtx x, unsigned int size, unsigned int align, int force)
2710 int aligned_p;
2712 aligned_p = (align >= MIN (size * BITS_PER_UNIT, BIGGEST_ALIGNMENT));
2714 /* See if the target hook can handle this kind of object. */
2715 if (targetm.asm_out.integer (x, size, aligned_p))
2716 return true;
2718 /* If the object is a multi-byte one, try splitting it up. Split
2719 it into words it if is multi-word, otherwise split it into bytes. */
2720 if (size > 1)
2722 machine_mode omode, imode;
2723 unsigned int subalign;
2724 unsigned int subsize, i;
2725 enum mode_class mclass;
2727 subsize = size > UNITS_PER_WORD? UNITS_PER_WORD : 1;
2728 subalign = MIN (align, subsize * BITS_PER_UNIT);
2729 if (GET_CODE (x) == CONST_FIXED)
2730 mclass = GET_MODE_CLASS (GET_MODE (x));
2731 else
2732 mclass = MODE_INT;
2734 omode = mode_for_size (subsize * BITS_PER_UNIT, mclass, 0);
2735 imode = mode_for_size (size * BITS_PER_UNIT, mclass, 0);
2737 for (i = 0; i < size; i += subsize)
2739 rtx partial = simplify_subreg (omode, x, imode, i);
2740 if (!partial || !assemble_integer (partial, subsize, subalign, 0))
2741 break;
2743 if (i == size)
2744 return true;
2746 /* If we've printed some of it, but not all of it, there's no going
2747 back now. */
2748 gcc_assert (!i);
2751 gcc_assert (!force);
2753 return false;
2756 void
2757 assemble_real (REAL_VALUE_TYPE d, machine_mode mode, unsigned int align)
2759 long data[4] = {0, 0, 0, 0};
2760 int i;
2761 int bitsize, nelts, nunits, units_per;
2763 /* This is hairy. We have a quantity of known size. real_to_target
2764 will put it into an array of *host* longs, 32 bits per element
2765 (even if long is more than 32 bits). We need to determine the
2766 number of array elements that are occupied (nelts) and the number
2767 of *target* min-addressable units that will be occupied in the
2768 object file (nunits). We cannot assume that 32 divides the
2769 mode's bitsize (size * BITS_PER_UNIT) evenly.
2771 size * BITS_PER_UNIT is used here to make sure that padding bits
2772 (which might appear at either end of the value; real_to_target
2773 will include the padding bits in its output array) are included. */
2775 nunits = GET_MODE_SIZE (mode);
2776 bitsize = nunits * BITS_PER_UNIT;
2777 nelts = CEIL (bitsize, 32);
2778 units_per = 32 / BITS_PER_UNIT;
2780 real_to_target (data, &d, mode);
2782 /* Put out the first word with the specified alignment. */
2783 assemble_integer (GEN_INT (data[0]), MIN (nunits, units_per), align, 1);
2784 nunits -= units_per;
2786 /* Subsequent words need only 32-bit alignment. */
2787 align = min_align (align, 32);
2789 for (i = 1; i < nelts; i++)
2791 assemble_integer (GEN_INT (data[i]), MIN (nunits, units_per), align, 1);
2792 nunits -= units_per;
2796 /* Given an expression EXP with a constant value,
2797 reduce it to the sum of an assembler symbol and an integer.
2798 Store them both in the structure *VALUE.
2799 EXP must be reducible. */
2801 struct addr_const {
2802 rtx base;
2803 HOST_WIDE_INT offset;
2806 static void
2807 decode_addr_const (tree exp, struct addr_const *value)
2809 tree target = TREE_OPERAND (exp, 0);
2810 int offset = 0;
2811 rtx x;
2813 while (1)
2815 if (TREE_CODE (target) == COMPONENT_REF
2816 && tree_fits_shwi_p (byte_position (TREE_OPERAND (target, 1))))
2818 offset += int_byte_position (TREE_OPERAND (target, 1));
2819 target = TREE_OPERAND (target, 0);
2821 else if (TREE_CODE (target) == ARRAY_REF
2822 || TREE_CODE (target) == ARRAY_RANGE_REF)
2824 offset += (tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (target)))
2825 * tree_to_shwi (TREE_OPERAND (target, 1)));
2826 target = TREE_OPERAND (target, 0);
2828 else if (TREE_CODE (target) == MEM_REF
2829 && TREE_CODE (TREE_OPERAND (target, 0)) == ADDR_EXPR)
2831 offset += mem_ref_offset (target).to_short_addr ();
2832 target = TREE_OPERAND (TREE_OPERAND (target, 0), 0);
2834 else if (TREE_CODE (target) == INDIRECT_REF
2835 && TREE_CODE (TREE_OPERAND (target, 0)) == NOP_EXPR
2836 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (target, 0), 0))
2837 == ADDR_EXPR)
2838 target = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (target, 0), 0), 0);
2839 else
2840 break;
2843 switch (TREE_CODE (target))
2845 case VAR_DECL:
2846 case FUNCTION_DECL:
2847 x = DECL_RTL (target);
2848 break;
2850 case LABEL_DECL:
2851 x = gen_rtx_MEM (FUNCTION_MODE,
2852 gen_rtx_LABEL_REF (Pmode, force_label_rtx (target)));
2853 break;
2855 case REAL_CST:
2856 case FIXED_CST:
2857 case STRING_CST:
2858 case COMPLEX_CST:
2859 case CONSTRUCTOR:
2860 case INTEGER_CST:
2861 x = output_constant_def (target, 1);
2862 break;
2864 default:
2865 gcc_unreachable ();
2868 gcc_assert (MEM_P (x));
2869 x = XEXP (x, 0);
2871 value->base = x;
2872 value->offset = offset;
2875 static GTY(()) hash_table<tree_descriptor_hasher> *const_desc_htab;
2877 static void maybe_output_constant_def_contents (struct constant_descriptor_tree *, int);
2879 /* Constant pool accessor function. */
2881 hash_table<tree_descriptor_hasher> *
2882 constant_pool_htab (void)
2884 return const_desc_htab;
2887 /* Compute a hash code for a constant expression. */
2889 hashval_t
2890 tree_descriptor_hasher::hash (constant_descriptor_tree *ptr)
2892 return ptr->hash;
2895 static hashval_t
2896 const_hash_1 (const tree exp)
2898 const char *p;
2899 hashval_t hi;
2900 int len, i;
2901 enum tree_code code = TREE_CODE (exp);
2903 /* Either set P and LEN to the address and len of something to hash and
2904 exit the switch or return a value. */
2906 switch (code)
2908 case INTEGER_CST:
2909 p = (char *) &TREE_INT_CST_ELT (exp, 0);
2910 len = TREE_INT_CST_NUNITS (exp) * sizeof (HOST_WIDE_INT);
2911 break;
2913 case REAL_CST:
2914 return real_hash (TREE_REAL_CST_PTR (exp));
2916 case FIXED_CST:
2917 return fixed_hash (TREE_FIXED_CST_PTR (exp));
2919 case STRING_CST:
2920 p = TREE_STRING_POINTER (exp);
2921 len = TREE_STRING_LENGTH (exp);
2922 break;
2924 case COMPLEX_CST:
2925 return (const_hash_1 (TREE_REALPART (exp)) * 5
2926 + const_hash_1 (TREE_IMAGPART (exp)));
2928 case VECTOR_CST:
2930 unsigned i;
2932 hi = 7 + VECTOR_CST_NELTS (exp);
2934 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
2935 hi = hi * 563 + const_hash_1 (VECTOR_CST_ELT (exp, i));
2937 return hi;
2940 case CONSTRUCTOR:
2942 unsigned HOST_WIDE_INT idx;
2943 tree value;
2945 hi = 5 + int_size_in_bytes (TREE_TYPE (exp));
2947 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
2948 if (value)
2949 hi = hi * 603 + const_hash_1 (value);
2951 return hi;
2954 case ADDR_EXPR:
2955 case FDESC_EXPR:
2957 struct addr_const value;
2959 decode_addr_const (exp, &value);
2960 switch (GET_CODE (value.base))
2962 case SYMBOL_REF:
2963 /* Don't hash the address of the SYMBOL_REF;
2964 only use the offset and the symbol name. */
2965 hi = value.offset;
2966 p = XSTR (value.base, 0);
2967 for (i = 0; p[i] != 0; i++)
2968 hi = ((hi * 613) + (unsigned) (p[i]));
2969 break;
2971 case LABEL_REF:
2972 hi = (value.offset
2973 + CODE_LABEL_NUMBER (LABEL_REF_LABEL (value.base)) * 13);
2974 break;
2976 default:
2977 gcc_unreachable ();
2980 return hi;
2982 case PLUS_EXPR:
2983 case POINTER_PLUS_EXPR:
2984 case MINUS_EXPR:
2985 return (const_hash_1 (TREE_OPERAND (exp, 0)) * 9
2986 + const_hash_1 (TREE_OPERAND (exp, 1)));
2988 CASE_CONVERT:
2989 return const_hash_1 (TREE_OPERAND (exp, 0)) * 7 + 2;
2991 default:
2992 /* A language specific constant. Just hash the code. */
2993 return code;
2996 /* Compute hashing function. */
2997 hi = len;
2998 for (i = 0; i < len; i++)
2999 hi = ((hi * 613) + (unsigned) (p[i]));
3001 return hi;
3004 /* Wrapper of compare_constant, for the htab interface. */
3005 bool
3006 tree_descriptor_hasher::equal (constant_descriptor_tree *c1,
3007 constant_descriptor_tree *c2)
3009 if (c1->hash != c2->hash)
3010 return 0;
3011 return compare_constant (c1->value, c2->value);
3014 /* Compare t1 and t2, and return 1 only if they are known to result in
3015 the same bit pattern on output. */
3017 static int
3018 compare_constant (const tree t1, const tree t2)
3020 enum tree_code typecode;
3022 if (t1 == NULL_TREE)
3023 return t2 == NULL_TREE;
3024 if (t2 == NULL_TREE)
3025 return 0;
3027 if (TREE_CODE (t1) != TREE_CODE (t2))
3028 return 0;
3030 switch (TREE_CODE (t1))
3032 case INTEGER_CST:
3033 /* Integer constants are the same only if the same width of type. */
3034 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3035 return 0;
3036 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
3037 return 0;
3038 return tree_int_cst_equal (t1, t2);
3040 case REAL_CST:
3041 /* Real constants are the same only if the same width of type. */
3042 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3043 return 0;
3045 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
3047 case FIXED_CST:
3048 /* Fixed constants are the same only if the same width of type. */
3049 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3050 return 0;
3052 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
3054 case STRING_CST:
3055 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
3056 return 0;
3058 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
3059 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
3060 TREE_STRING_LENGTH (t1)));
3062 case COMPLEX_CST:
3063 return (compare_constant (TREE_REALPART (t1), TREE_REALPART (t2))
3064 && compare_constant (TREE_IMAGPART (t1), TREE_IMAGPART (t2)));
3066 case VECTOR_CST:
3068 unsigned i;
3070 if (VECTOR_CST_NELTS (t1) != VECTOR_CST_NELTS (t2))
3071 return 0;
3073 for (i = 0; i < VECTOR_CST_NELTS (t1); ++i)
3074 if (!compare_constant (VECTOR_CST_ELT (t1, i),
3075 VECTOR_CST_ELT (t2, i)))
3076 return 0;
3078 return 1;
3081 case CONSTRUCTOR:
3083 vec<constructor_elt, va_gc> *v1, *v2;
3084 unsigned HOST_WIDE_INT idx;
3086 typecode = TREE_CODE (TREE_TYPE (t1));
3087 if (typecode != TREE_CODE (TREE_TYPE (t2)))
3088 return 0;
3090 if (typecode == ARRAY_TYPE)
3092 HOST_WIDE_INT size_1 = int_size_in_bytes (TREE_TYPE (t1));
3093 /* For arrays, check that the sizes all match. */
3094 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))
3095 || size_1 == -1
3096 || size_1 != int_size_in_bytes (TREE_TYPE (t2)))
3097 return 0;
3099 else
3101 /* For record and union constructors, require exact type
3102 equality. */
3103 if (TREE_TYPE (t1) != TREE_TYPE (t2))
3104 return 0;
3107 v1 = CONSTRUCTOR_ELTS (t1);
3108 v2 = CONSTRUCTOR_ELTS (t2);
3109 if (vec_safe_length (v1) != vec_safe_length (v2))
3110 return 0;
3112 for (idx = 0; idx < vec_safe_length (v1); ++idx)
3114 constructor_elt *c1 = &(*v1)[idx];
3115 constructor_elt *c2 = &(*v2)[idx];
3117 /* Check that each value is the same... */
3118 if (!compare_constant (c1->value, c2->value))
3119 return 0;
3120 /* ... and that they apply to the same fields! */
3121 if (typecode == ARRAY_TYPE)
3123 if (!compare_constant (c1->index, c2->index))
3124 return 0;
3126 else
3128 if (c1->index != c2->index)
3129 return 0;
3133 return 1;
3136 case ADDR_EXPR:
3137 case FDESC_EXPR:
3139 struct addr_const value1, value2;
3140 enum rtx_code code;
3141 int ret;
3143 decode_addr_const (t1, &value1);
3144 decode_addr_const (t2, &value2);
3146 if (value1.offset != value2.offset)
3147 return 0;
3149 code = GET_CODE (value1.base);
3150 if (code != GET_CODE (value2.base))
3151 return 0;
3153 switch (code)
3155 case SYMBOL_REF:
3156 ret = (strcmp (XSTR (value1.base, 0), XSTR (value2.base, 0)) == 0);
3157 break;
3159 case LABEL_REF:
3160 ret = (CODE_LABEL_NUMBER (LABEL_REF_LABEL (value1.base))
3161 == CODE_LABEL_NUMBER (LABEL_REF_LABEL (value2.base)));
3162 break;
3164 default:
3165 gcc_unreachable ();
3167 return ret;
3170 case PLUS_EXPR:
3171 case POINTER_PLUS_EXPR:
3172 case MINUS_EXPR:
3173 case RANGE_EXPR:
3174 return (compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
3175 && compare_constant (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
3177 CASE_CONVERT:
3178 case VIEW_CONVERT_EXPR:
3179 return compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3181 default:
3182 return 0;
3185 gcc_unreachable ();
3188 /* Return the section into which constant EXP should be placed. */
3190 static section *
3191 get_constant_section (tree exp, unsigned int align)
3193 return targetm.asm_out.select_section (exp,
3194 compute_reloc_for_constant (exp),
3195 align);
3198 /* Return the size of constant EXP in bytes. */
3200 static HOST_WIDE_INT
3201 get_constant_size (tree exp)
3203 HOST_WIDE_INT size;
3205 size = int_size_in_bytes (TREE_TYPE (exp));
3206 if (TREE_CODE (exp) == STRING_CST)
3207 size = MAX (TREE_STRING_LENGTH (exp), size);
3208 return size;
3211 /* Subroutine of output_constant_def:
3212 No constant equal to EXP is known to have been output.
3213 Make a constant descriptor to enter EXP in the hash table.
3214 Assign the label number and construct RTL to refer to the
3215 constant's location in memory.
3216 Caller is responsible for updating the hash table. */
3218 static struct constant_descriptor_tree *
3219 build_constant_desc (tree exp)
3221 struct constant_descriptor_tree *desc;
3222 rtx symbol, rtl;
3223 char label[256];
3224 int labelno;
3225 tree decl;
3227 desc = ggc_alloc<constant_descriptor_tree> ();
3228 desc->value = exp;
3230 /* Create a string containing the label name, in LABEL. */
3231 labelno = const_labelno++;
3232 ASM_GENERATE_INTERNAL_LABEL (label, "LC", labelno);
3234 /* Construct the VAR_DECL associated with the constant. */
3235 decl = build_decl (UNKNOWN_LOCATION, VAR_DECL, get_identifier (label),
3236 TREE_TYPE (exp));
3237 DECL_ARTIFICIAL (decl) = 1;
3238 DECL_IGNORED_P (decl) = 1;
3239 TREE_READONLY (decl) = 1;
3240 TREE_STATIC (decl) = 1;
3241 TREE_ADDRESSABLE (decl) = 1;
3242 /* We don't set the RTL yet as this would cause varpool to assume that the
3243 variable is referenced. Moreover, it would just be dropped in LTO mode.
3244 Instead we set the flag that will be recognized in make_decl_rtl. */
3245 DECL_IN_CONSTANT_POOL (decl) = 1;
3246 DECL_INITIAL (decl) = desc->value;
3247 /* ??? CONSTANT_ALIGNMENT hasn't been updated for vector types on most
3248 architectures so use DATA_ALIGNMENT as well, except for strings. */
3249 if (TREE_CODE (exp) == STRING_CST)
3251 #ifdef CONSTANT_ALIGNMENT
3252 DECL_ALIGN (decl) = CONSTANT_ALIGNMENT (exp, DECL_ALIGN (decl));
3253 #endif
3255 else
3256 align_variable (decl, 0);
3258 /* Now construct the SYMBOL_REF and the MEM. */
3259 if (use_object_blocks_p ())
3261 section *sect = get_constant_section (exp, DECL_ALIGN (decl));
3262 symbol = create_block_symbol (ggc_strdup (label),
3263 get_block_for_section (sect), -1);
3265 else
3266 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3267 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3268 SET_SYMBOL_REF_DECL (symbol, decl);
3269 TREE_CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3271 rtl = gen_const_mem (TYPE_MODE (TREE_TYPE (exp)), symbol);
3272 set_mem_attributes (rtl, exp, 1);
3273 set_mem_alias_set (rtl, 0);
3275 /* We cannot share RTX'es in pool entries.
3276 Mark this piece of RTL as required for unsharing. */
3277 RTX_FLAG (rtl, used) = 1;
3279 /* Set flags or add text to the name to record information, such as
3280 that it is a local symbol. If the name is changed, the macro
3281 ASM_OUTPUT_LABELREF will have to know how to strip this
3282 information. This call might invalidate our local variable
3283 SYMBOL; we can't use it afterward. */
3284 targetm.encode_section_info (exp, rtl, true);
3286 desc->rtl = rtl;
3288 return desc;
3291 /* Return an rtx representing a reference to constant data in memory
3292 for the constant expression EXP.
3294 If assembler code for such a constant has already been output,
3295 return an rtx to refer to it.
3296 Otherwise, output such a constant in memory
3297 and generate an rtx for it.
3299 If DEFER is nonzero, this constant can be deferred and output only
3300 if referenced in the function after all optimizations.
3302 `const_desc_table' records which constants already have label strings. */
3305 output_constant_def (tree exp, int defer)
3307 struct constant_descriptor_tree *desc;
3308 struct constant_descriptor_tree key;
3310 /* Look up EXP in the table of constant descriptors. If we didn't find
3311 it, create a new one. */
3312 key.value = exp;
3313 key.hash = const_hash_1 (exp);
3314 constant_descriptor_tree **loc
3315 = const_desc_htab->find_slot_with_hash (&key, key.hash, INSERT);
3317 desc = *loc;
3318 if (desc == 0)
3320 desc = build_constant_desc (exp);
3321 desc->hash = key.hash;
3322 *loc = desc;
3325 maybe_output_constant_def_contents (desc, defer);
3326 return desc->rtl;
3329 /* Subroutine of output_constant_def: Decide whether or not we need to
3330 output the constant DESC now, and if so, do it. */
3331 static void
3332 maybe_output_constant_def_contents (struct constant_descriptor_tree *desc,
3333 int defer)
3335 rtx symbol = XEXP (desc->rtl, 0);
3336 tree exp = desc->value;
3338 if (flag_syntax_only)
3339 return;
3341 if (TREE_ASM_WRITTEN (exp))
3342 /* Already output; don't do it again. */
3343 return;
3345 /* We can always defer constants as long as the context allows
3346 doing so. */
3347 if (defer)
3349 /* Increment n_deferred_constants if it exists. It needs to be at
3350 least as large as the number of constants actually referred to
3351 by the function. If it's too small we'll stop looking too early
3352 and fail to emit constants; if it's too large we'll only look
3353 through the entire function when we could have stopped earlier. */
3354 if (cfun)
3355 n_deferred_constants++;
3356 return;
3359 output_constant_def_contents (symbol);
3362 /* Subroutine of output_constant_def_contents. Output the definition
3363 of constant EXP, which is pointed to by label LABEL. ALIGN is the
3364 constant's alignment in bits. */
3366 static void
3367 assemble_constant_contents (tree exp, const char *label, unsigned int align)
3369 HOST_WIDE_INT size;
3371 size = get_constant_size (exp);
3373 /* Do any machine/system dependent processing of the constant. */
3374 targetm.asm_out.declare_constant_name (asm_out_file, label, exp, size);
3376 /* Output the value of EXP. */
3377 output_constant (exp, size, align);
3379 targetm.asm_out.decl_end ();
3382 /* We must output the constant data referred to by SYMBOL; do so. */
3384 static void
3385 output_constant_def_contents (rtx symbol)
3387 tree decl = SYMBOL_REF_DECL (symbol);
3388 tree exp = DECL_INITIAL (decl);
3389 unsigned int align;
3390 bool asan_protected = false;
3392 /* Make sure any other constants whose addresses appear in EXP
3393 are assigned label numbers. */
3394 output_addressed_constants (exp);
3396 /* We are no longer deferring this constant. */
3397 TREE_ASM_WRITTEN (decl) = TREE_ASM_WRITTEN (exp) = 1;
3399 if ((flag_sanitize & SANITIZE_ADDRESS)
3400 && TREE_CODE (exp) == STRING_CST
3401 && asan_protect_global (exp))
3403 asan_protected = true;
3404 DECL_ALIGN (decl) = MAX (DECL_ALIGN (decl),
3405 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
3408 /* If the constant is part of an object block, make sure that the
3409 decl has been positioned within its block, but do not write out
3410 its definition yet. output_object_blocks will do that later. */
3411 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
3412 place_block_symbol (symbol);
3413 else
3415 align = DECL_ALIGN (decl);
3416 switch_to_section (get_constant_section (exp, align));
3417 if (align > BITS_PER_UNIT)
3418 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
3419 assemble_constant_contents (exp, XSTR (symbol, 0), align);
3420 if (asan_protected)
3422 HOST_WIDE_INT size = get_constant_size (exp);
3423 assemble_zeros (asan_red_zone_size (size));
3428 /* Look up EXP in the table of constant descriptors. Return the rtl
3429 if it has been emitted, else null. */
3432 lookup_constant_def (tree exp)
3434 struct constant_descriptor_tree key;
3436 key.value = exp;
3437 key.hash = const_hash_1 (exp);
3438 constant_descriptor_tree *desc
3439 = const_desc_htab->find_with_hash (&key, key.hash);
3441 return (desc ? desc->rtl : NULL_RTX);
3444 /* Return a tree representing a reference to constant data in memory
3445 for the constant expression EXP.
3447 This is the counterpart of output_constant_def at the Tree level. */
3449 tree
3450 tree_output_constant_def (tree exp)
3452 struct constant_descriptor_tree *desc, key;
3453 tree decl;
3455 /* Look up EXP in the table of constant descriptors. If we didn't find
3456 it, create a new one. */
3457 key.value = exp;
3458 key.hash = const_hash_1 (exp);
3459 constant_descriptor_tree **loc
3460 = const_desc_htab->find_slot_with_hash (&key, key.hash, INSERT);
3462 desc = *loc;
3463 if (desc == 0)
3465 desc = build_constant_desc (exp);
3466 desc->hash = key.hash;
3467 *loc = desc;
3470 decl = SYMBOL_REF_DECL (XEXP (desc->rtl, 0));
3471 varpool_node::finalize_decl (decl);
3472 return decl;
3475 struct GTY((chain_next ("%h.next"), for_user)) constant_descriptor_rtx {
3476 struct constant_descriptor_rtx *next;
3477 rtx mem;
3478 rtx sym;
3479 rtx constant;
3480 HOST_WIDE_INT offset;
3481 hashval_t hash;
3482 machine_mode mode;
3483 unsigned int align;
3484 int labelno;
3485 int mark;
3488 struct const_rtx_desc_hasher : ggc_hasher<constant_descriptor_rtx *>
3490 static hashval_t hash (constant_descriptor_rtx *);
3491 static bool equal (constant_descriptor_rtx *, constant_descriptor_rtx *);
3494 /* Used in the hash tables to avoid outputting the same constant
3495 twice. Unlike 'struct constant_descriptor_tree', RTX constants
3496 are output once per function, not once per file. */
3497 /* ??? Only a few targets need per-function constant pools. Most
3498 can use one per-file pool. Should add a targetm bit to tell the
3499 difference. */
3501 struct GTY(()) rtx_constant_pool {
3502 /* Pointers to first and last constant in pool, as ordered by offset. */
3503 struct constant_descriptor_rtx *first;
3504 struct constant_descriptor_rtx *last;
3506 /* Hash facility for making memory-constants from constant rtl-expressions.
3507 It is used on RISC machines where immediate integer arguments and
3508 constant addresses are restricted so that such constants must be stored
3509 in memory. */
3510 hash_table<const_rtx_desc_hasher> *const_rtx_htab;
3512 /* Current offset in constant pool (does not include any
3513 machine-specific header). */
3514 HOST_WIDE_INT offset;
3517 /* Hash and compare functions for const_rtx_htab. */
3519 hashval_t
3520 const_rtx_desc_hasher::hash (constant_descriptor_rtx *desc)
3522 return desc->hash;
3525 bool
3526 const_rtx_desc_hasher::equal (constant_descriptor_rtx *x,
3527 constant_descriptor_rtx *y)
3529 if (x->mode != y->mode)
3530 return 0;
3531 return rtx_equal_p (x->constant, y->constant);
3534 /* Hash one component of a constant. */
3536 static hashval_t
3537 const_rtx_hash_1 (const_rtx x)
3539 unsigned HOST_WIDE_INT hwi;
3540 machine_mode mode;
3541 enum rtx_code code;
3542 hashval_t h;
3543 int i;
3545 code = GET_CODE (x);
3546 mode = GET_MODE (x);
3547 h = (hashval_t) code * 1048573 + mode;
3549 switch (code)
3551 case CONST_INT:
3552 hwi = INTVAL (x);
3554 fold_hwi:
3556 int shift = sizeof (hashval_t) * CHAR_BIT;
3557 const int n = sizeof (HOST_WIDE_INT) / sizeof (hashval_t);
3559 h ^= (hashval_t) hwi;
3560 for (i = 1; i < n; ++i)
3562 hwi >>= shift;
3563 h ^= (hashval_t) hwi;
3566 break;
3568 case CONST_WIDE_INT:
3569 hwi = GET_MODE_PRECISION (mode);
3571 for (i = 0; i < CONST_WIDE_INT_NUNITS (x); i++)
3572 hwi ^= CONST_WIDE_INT_ELT (x, i);
3573 goto fold_hwi;
3576 case CONST_DOUBLE:
3577 if (TARGET_SUPPORTS_WIDE_INT == 0 && mode == VOIDmode)
3579 hwi = CONST_DOUBLE_LOW (x) ^ CONST_DOUBLE_HIGH (x);
3580 goto fold_hwi;
3582 else
3583 h ^= real_hash (CONST_DOUBLE_REAL_VALUE (x));
3584 break;
3586 case CONST_FIXED:
3587 h ^= fixed_hash (CONST_FIXED_VALUE (x));
3588 break;
3590 case SYMBOL_REF:
3591 h ^= htab_hash_string (XSTR (x, 0));
3592 break;
3594 case LABEL_REF:
3595 h = h * 251 + CODE_LABEL_NUMBER (LABEL_REF_LABEL (x));
3596 break;
3598 case UNSPEC:
3599 case UNSPEC_VOLATILE:
3600 h = h * 251 + XINT (x, 1);
3601 break;
3603 default:
3604 break;
3607 return h;
3610 /* Compute a hash value for X, which should be a constant. */
3612 static hashval_t
3613 const_rtx_hash (rtx x)
3615 hashval_t h = 0;
3616 subrtx_iterator::array_type array;
3617 FOR_EACH_SUBRTX (iter, array, x, ALL)
3618 h = h * 509 + const_rtx_hash_1 (*iter);
3619 return h;
3623 /* Create and return a new rtx constant pool. */
3625 static struct rtx_constant_pool *
3626 create_constant_pool (void)
3628 struct rtx_constant_pool *pool;
3630 pool = ggc_alloc<rtx_constant_pool> ();
3631 pool->const_rtx_htab = hash_table<const_rtx_desc_hasher>::create_ggc (31);
3632 pool->first = NULL;
3633 pool->last = NULL;
3634 pool->offset = 0;
3635 return pool;
3638 /* Initialize constant pool hashing for a new function. */
3640 void
3641 init_varasm_status (void)
3643 crtl->varasm.pool = create_constant_pool ();
3644 crtl->varasm.deferred_constants = 0;
3647 /* Given a MINUS expression, simplify it if both sides
3648 include the same symbol. */
3651 simplify_subtraction (rtx x)
3653 rtx r = simplify_rtx (x);
3654 return r ? r : x;
3657 /* Given a constant rtx X, make (or find) a memory constant for its value
3658 and return a MEM rtx to refer to it in memory. */
3661 force_const_mem (machine_mode mode, rtx x)
3663 struct constant_descriptor_rtx *desc, tmp;
3664 struct rtx_constant_pool *pool;
3665 char label[256];
3666 rtx def, symbol;
3667 hashval_t hash;
3668 unsigned int align;
3669 constant_descriptor_rtx **slot;
3671 /* If we're not allowed to drop X into the constant pool, don't. */
3672 if (targetm.cannot_force_const_mem (mode, x))
3673 return NULL_RTX;
3675 /* Record that this function has used a constant pool entry. */
3676 crtl->uses_const_pool = 1;
3678 /* Decide which pool to use. */
3679 pool = (targetm.use_blocks_for_constant_p (mode, x)
3680 ? shared_constant_pool
3681 : crtl->varasm.pool);
3683 /* Lookup the value in the hashtable. */
3684 tmp.constant = x;
3685 tmp.mode = mode;
3686 hash = const_rtx_hash (x);
3687 slot = pool->const_rtx_htab->find_slot_with_hash (&tmp, hash, INSERT);
3688 desc = *slot;
3690 /* If the constant was already present, return its memory. */
3691 if (desc)
3692 return copy_rtx (desc->mem);
3694 /* Otherwise, create a new descriptor. */
3695 desc = ggc_alloc<constant_descriptor_rtx> ();
3696 *slot = desc;
3698 /* Align the location counter as required by EXP's data type. */
3699 align = GET_MODE_ALIGNMENT (mode == VOIDmode ? word_mode : mode);
3700 #ifdef CONSTANT_ALIGNMENT
3702 tree type = lang_hooks.types.type_for_mode (mode, 0);
3703 if (type != NULL_TREE)
3704 align = CONSTANT_ALIGNMENT (make_tree (type, x), align);
3706 #endif
3708 pool->offset += (align / BITS_PER_UNIT) - 1;
3709 pool->offset &= ~ ((align / BITS_PER_UNIT) - 1);
3711 desc->next = NULL;
3712 desc->constant = copy_rtx (tmp.constant);
3713 desc->offset = pool->offset;
3714 desc->hash = hash;
3715 desc->mode = mode;
3716 desc->align = align;
3717 desc->labelno = const_labelno;
3718 desc->mark = 0;
3720 pool->offset += GET_MODE_SIZE (mode);
3721 if (pool->last)
3722 pool->last->next = desc;
3723 else
3724 pool->first = pool->last = desc;
3725 pool->last = desc;
3727 /* Create a string containing the label name, in LABEL. */
3728 ASM_GENERATE_INTERNAL_LABEL (label, "LC", const_labelno);
3729 ++const_labelno;
3731 /* Construct the SYMBOL_REF. Make sure to mark it as belonging to
3732 the constants pool. */
3733 if (use_object_blocks_p () && targetm.use_blocks_for_constant_p (mode, x))
3735 section *sect = targetm.asm_out.select_rtx_section (mode, x, align);
3736 symbol = create_block_symbol (ggc_strdup (label),
3737 get_block_for_section (sect), -1);
3739 else
3740 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3741 desc->sym = symbol;
3742 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3743 CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3744 SET_SYMBOL_REF_CONSTANT (symbol, desc);
3746 /* Construct the MEM. */
3747 desc->mem = def = gen_const_mem (mode, symbol);
3748 set_mem_attributes (def, lang_hooks.types.type_for_mode (mode, 0), 1);
3749 set_mem_align (def, align);
3751 /* If we're dropping a label to the constant pool, make sure we
3752 don't delete it. */
3753 if (GET_CODE (x) == LABEL_REF)
3754 LABEL_PRESERVE_P (XEXP (x, 0)) = 1;
3756 return copy_rtx (def);
3759 /* Given a constant pool SYMBOL_REF, return the corresponding constant. */
3762 get_pool_constant (const_rtx addr)
3764 return SYMBOL_REF_CONSTANT (addr)->constant;
3767 /* Given a constant pool SYMBOL_REF, return the corresponding constant
3768 and whether it has been output or not. */
3771 get_pool_constant_mark (rtx addr, bool *pmarked)
3773 struct constant_descriptor_rtx *desc;
3775 desc = SYMBOL_REF_CONSTANT (addr);
3776 *pmarked = (desc->mark != 0);
3777 return desc->constant;
3780 /* Similar, return the mode. */
3782 machine_mode
3783 get_pool_mode (const_rtx addr)
3785 return SYMBOL_REF_CONSTANT (addr)->mode;
3788 /* Return the size of the constant pool. */
3791 get_pool_size (void)
3793 return crtl->varasm.pool->offset;
3796 /* Worker function for output_constant_pool_1. Emit assembly for X
3797 in MODE with known alignment ALIGN. */
3799 static void
3800 output_constant_pool_2 (machine_mode mode, rtx x, unsigned int align)
3802 switch (GET_MODE_CLASS (mode))
3804 case MODE_FLOAT:
3805 case MODE_DECIMAL_FLOAT:
3807 REAL_VALUE_TYPE r;
3809 gcc_assert (CONST_DOUBLE_AS_FLOAT_P (x));
3810 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
3811 assemble_real (r, mode, align);
3812 break;
3815 case MODE_INT:
3816 case MODE_PARTIAL_INT:
3817 case MODE_FRACT:
3818 case MODE_UFRACT:
3819 case MODE_ACCUM:
3820 case MODE_UACCUM:
3821 case MODE_POINTER_BOUNDS:
3822 assemble_integer (x, GET_MODE_SIZE (mode), align, 1);
3823 break;
3825 case MODE_VECTOR_FLOAT:
3826 case MODE_VECTOR_INT:
3827 case MODE_VECTOR_FRACT:
3828 case MODE_VECTOR_UFRACT:
3829 case MODE_VECTOR_ACCUM:
3830 case MODE_VECTOR_UACCUM:
3832 int i, units;
3833 machine_mode submode = GET_MODE_INNER (mode);
3834 unsigned int subalign = MIN (align, GET_MODE_BITSIZE (submode));
3836 gcc_assert (GET_CODE (x) == CONST_VECTOR);
3837 units = CONST_VECTOR_NUNITS (x);
3839 for (i = 0; i < units; i++)
3841 rtx elt = CONST_VECTOR_ELT (x, i);
3842 output_constant_pool_2 (submode, elt, i ? subalign : align);
3845 break;
3847 default:
3848 gcc_unreachable ();
3852 /* Worker function for output_constant_pool. Emit constant DESC,
3853 giving it ALIGN bits of alignment. */
3855 static void
3856 output_constant_pool_1 (struct constant_descriptor_rtx *desc,
3857 unsigned int align)
3859 rtx x, tmp;
3861 x = desc->constant;
3863 /* See if X is a LABEL_REF (or a CONST referring to a LABEL_REF)
3864 whose CODE_LABEL has been deleted. This can occur if a jump table
3865 is eliminated by optimization. If so, write a constant of zero
3866 instead. Note that this can also happen by turning the
3867 CODE_LABEL into a NOTE. */
3868 /* ??? This seems completely and utterly wrong. Certainly it's
3869 not true for NOTE_INSN_DELETED_LABEL, but I disbelieve proper
3870 functioning even with rtx_insn::deleted and friends. */
3872 tmp = x;
3873 switch (GET_CODE (tmp))
3875 case CONST:
3876 if (GET_CODE (XEXP (tmp, 0)) != PLUS
3877 || GET_CODE (XEXP (XEXP (tmp, 0), 0)) != LABEL_REF)
3878 break;
3879 tmp = XEXP (XEXP (tmp, 0), 0);
3880 /* FALLTHRU */
3882 case LABEL_REF:
3883 tmp = LABEL_REF_LABEL (tmp);
3884 gcc_assert (!as_a<rtx_insn *> (tmp)->deleted ());
3885 gcc_assert (!NOTE_P (tmp)
3886 || NOTE_KIND (tmp) != NOTE_INSN_DELETED);
3887 break;
3889 default:
3890 break;
3893 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3894 ASM_OUTPUT_SPECIAL_POOL_ENTRY (asm_out_file, x, desc->mode,
3895 align, desc->labelno, done);
3896 #endif
3898 assemble_align (align);
3900 /* Output the label. */
3901 targetm.asm_out.internal_label (asm_out_file, "LC", desc->labelno);
3903 /* Output the data. */
3904 output_constant_pool_2 (desc->mode, x, align);
3906 /* Make sure all constants in SECTION_MERGE and not SECTION_STRINGS
3907 sections have proper size. */
3908 if (align > GET_MODE_BITSIZE (desc->mode)
3909 && in_section
3910 && (in_section->common.flags & SECTION_MERGE))
3911 assemble_align (align);
3913 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3914 done:
3915 #endif
3916 return;
3919 /* Mark all constants that are referenced by SYMBOL_REFs in X.
3920 Emit referenced deferred strings. */
3922 static void
3923 mark_constants_in_pattern (rtx insn)
3925 subrtx_iterator::array_type array;
3926 FOR_EACH_SUBRTX (iter, array, PATTERN (insn), ALL)
3928 const_rtx x = *iter;
3929 if (GET_CODE (x) == SYMBOL_REF)
3931 if (CONSTANT_POOL_ADDRESS_P (x))
3933 struct constant_descriptor_rtx *desc = SYMBOL_REF_CONSTANT (x);
3934 if (desc->mark == 0)
3936 desc->mark = 1;
3937 iter.substitute (desc->constant);
3940 else if (TREE_CONSTANT_POOL_ADDRESS_P (x))
3942 tree decl = SYMBOL_REF_DECL (x);
3943 if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
3945 n_deferred_constants--;
3946 output_constant_def_contents (CONST_CAST_RTX (x));
3953 /* Look through appropriate parts of INSN, marking all entries in the
3954 constant pool which are actually being used. Entries that are only
3955 referenced by other constants are also marked as used. Emit
3956 deferred strings that are used. */
3958 static void
3959 mark_constants (rtx_insn *insn)
3961 if (!INSN_P (insn))
3962 return;
3964 /* Insns may appear inside a SEQUENCE. Only check the patterns of
3965 insns, not any notes that may be attached. We don't want to mark
3966 a constant just because it happens to appear in a REG_EQUIV note. */
3967 if (rtx_sequence *seq = dyn_cast <rtx_sequence *> (PATTERN (insn)))
3969 int i, n = seq->len ();
3970 for (i = 0; i < n; ++i)
3972 rtx subinsn = seq->element (i);
3973 if (INSN_P (subinsn))
3974 mark_constants_in_pattern (subinsn);
3977 else
3978 mark_constants_in_pattern (insn);
3981 /* Look through the instructions for this function, and mark all the
3982 entries in POOL which are actually being used. Emit deferred constants
3983 which have indeed been used. */
3985 static void
3986 mark_constant_pool (void)
3988 rtx_insn *insn;
3990 if (!crtl->uses_const_pool && n_deferred_constants == 0)
3991 return;
3993 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3994 mark_constants (insn);
3997 /* Write all the constants in POOL. */
3999 static void
4000 output_constant_pool_contents (struct rtx_constant_pool *pool)
4002 struct constant_descriptor_rtx *desc;
4004 for (desc = pool->first; desc ; desc = desc->next)
4005 if (desc->mark)
4007 /* If the constant is part of an object_block, make sure that
4008 the constant has been positioned within its block, but do not
4009 write out its definition yet. output_object_blocks will do
4010 that later. */
4011 if (SYMBOL_REF_HAS_BLOCK_INFO_P (desc->sym)
4012 && SYMBOL_REF_BLOCK (desc->sym))
4013 place_block_symbol (desc->sym);
4014 else
4016 switch_to_section (targetm.asm_out.select_rtx_section
4017 (desc->mode, desc->constant, desc->align));
4018 output_constant_pool_1 (desc, desc->align);
4023 /* Mark all constants that are used in the current function, then write
4024 out the function's private constant pool. */
4026 static void
4027 output_constant_pool (const char *fnname ATTRIBUTE_UNUSED,
4028 tree fndecl ATTRIBUTE_UNUSED)
4030 struct rtx_constant_pool *pool = crtl->varasm.pool;
4032 /* It is possible for gcc to call force_const_mem and then to later
4033 discard the instructions which refer to the constant. In such a
4034 case we do not need to output the constant. */
4035 mark_constant_pool ();
4037 #ifdef ASM_OUTPUT_POOL_PROLOGUE
4038 ASM_OUTPUT_POOL_PROLOGUE (asm_out_file, fnname, fndecl, pool->offset);
4039 #endif
4041 output_constant_pool_contents (pool);
4043 #ifdef ASM_OUTPUT_POOL_EPILOGUE
4044 ASM_OUTPUT_POOL_EPILOGUE (asm_out_file, fnname, fndecl, pool->offset);
4045 #endif
4048 /* Write the contents of the shared constant pool. */
4050 void
4051 output_shared_constant_pool (void)
4053 output_constant_pool_contents (shared_constant_pool);
4056 /* Determine what kind of relocations EXP may need. */
4059 compute_reloc_for_constant (tree exp)
4061 int reloc = 0, reloc2;
4062 tree tem;
4064 switch (TREE_CODE (exp))
4066 case ADDR_EXPR:
4067 case FDESC_EXPR:
4068 /* Go inside any operations that get_inner_reference can handle and see
4069 if what's inside is a constant: no need to do anything here for
4070 addresses of variables or functions. */
4071 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
4072 tem = TREE_OPERAND (tem, 0))
4075 if (TREE_CODE (tem) == MEM_REF
4076 && TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR)
4078 reloc = compute_reloc_for_constant (TREE_OPERAND (tem, 0));
4079 break;
4082 if (!targetm.binds_local_p (tem))
4083 reloc |= 2;
4084 else
4085 reloc |= 1;
4086 break;
4088 case PLUS_EXPR:
4089 case POINTER_PLUS_EXPR:
4090 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4091 reloc |= compute_reloc_for_constant (TREE_OPERAND (exp, 1));
4092 break;
4094 case MINUS_EXPR:
4095 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4096 reloc2 = compute_reloc_for_constant (TREE_OPERAND (exp, 1));
4097 /* The difference of two local labels is computable at link time. */
4098 if (reloc == 1 && reloc2 == 1)
4099 reloc = 0;
4100 else
4101 reloc |= reloc2;
4102 break;
4104 CASE_CONVERT:
4105 case VIEW_CONVERT_EXPR:
4106 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4107 break;
4109 case CONSTRUCTOR:
4111 unsigned HOST_WIDE_INT idx;
4112 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
4113 if (tem != 0)
4114 reloc |= compute_reloc_for_constant (tem);
4116 break;
4118 default:
4119 break;
4121 return reloc;
4124 /* Find all the constants whose addresses are referenced inside of EXP,
4125 and make sure assembler code with a label has been output for each one.
4126 Indicate whether an ADDR_EXPR has been encountered. */
4128 static void
4129 output_addressed_constants (tree exp)
4131 tree tem;
4133 switch (TREE_CODE (exp))
4135 case ADDR_EXPR:
4136 case FDESC_EXPR:
4137 /* Go inside any operations that get_inner_reference can handle and see
4138 if what's inside is a constant: no need to do anything here for
4139 addresses of variables or functions. */
4140 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
4141 tem = TREE_OPERAND (tem, 0))
4144 /* If we have an initialized CONST_DECL, retrieve the initializer. */
4145 if (TREE_CODE (tem) == CONST_DECL && DECL_INITIAL (tem))
4146 tem = DECL_INITIAL (tem);
4148 if (CONSTANT_CLASS_P (tem) || TREE_CODE (tem) == CONSTRUCTOR)
4149 output_constant_def (tem, 0);
4151 if (TREE_CODE (tem) == MEM_REF)
4152 output_addressed_constants (TREE_OPERAND (tem, 0));
4153 break;
4155 case PLUS_EXPR:
4156 case POINTER_PLUS_EXPR:
4157 case MINUS_EXPR:
4158 output_addressed_constants (TREE_OPERAND (exp, 1));
4159 /* Fall through. */
4161 CASE_CONVERT:
4162 case VIEW_CONVERT_EXPR:
4163 output_addressed_constants (TREE_OPERAND (exp, 0));
4164 break;
4166 case CONSTRUCTOR:
4168 unsigned HOST_WIDE_INT idx;
4169 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
4170 if (tem != 0)
4171 output_addressed_constants (tem);
4173 break;
4175 default:
4176 break;
4180 /* Whether a constructor CTOR is a valid static constant initializer if all
4181 its elements are. This used to be internal to initializer_constant_valid_p
4182 and has been exposed to let other functions like categorize_ctor_elements
4183 evaluate the property while walking a constructor for other purposes. */
4185 bool
4186 constructor_static_from_elts_p (const_tree ctor)
4188 return (TREE_CONSTANT (ctor)
4189 && (TREE_CODE (TREE_TYPE (ctor)) == UNION_TYPE
4190 || TREE_CODE (TREE_TYPE (ctor)) == RECORD_TYPE
4191 || TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE));
4194 static tree initializer_constant_valid_p_1 (tree value, tree endtype,
4195 tree *cache);
4197 /* A subroutine of initializer_constant_valid_p. VALUE is a MINUS_EXPR,
4198 PLUS_EXPR or POINTER_PLUS_EXPR. This looks for cases of VALUE
4199 which are valid when ENDTYPE is an integer of any size; in
4200 particular, this does not accept a pointer minus a constant. This
4201 returns null_pointer_node if the VALUE is an absolute constant
4202 which can be used to initialize a static variable. Otherwise it
4203 returns NULL. */
4205 static tree
4206 narrowing_initializer_constant_valid_p (tree value, tree endtype, tree *cache)
4208 tree op0, op1;
4210 if (!INTEGRAL_TYPE_P (endtype))
4211 return NULL_TREE;
4213 op0 = TREE_OPERAND (value, 0);
4214 op1 = TREE_OPERAND (value, 1);
4216 /* Like STRIP_NOPS except allow the operand mode to widen. This
4217 works around a feature of fold that simplifies (int)(p1 - p2) to
4218 ((int)p1 - (int)p2) under the theory that the narrower operation
4219 is cheaper. */
4221 while (CONVERT_EXPR_P (op0)
4222 || TREE_CODE (op0) == NON_LVALUE_EXPR)
4224 tree inner = TREE_OPERAND (op0, 0);
4225 if (inner == error_mark_node
4226 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4227 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))
4228 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
4229 break;
4230 op0 = inner;
4233 while (CONVERT_EXPR_P (op1)
4234 || TREE_CODE (op1) == NON_LVALUE_EXPR)
4236 tree inner = TREE_OPERAND (op1, 0);
4237 if (inner == error_mark_node
4238 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4239 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op1)))
4240 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
4241 break;
4242 op1 = inner;
4245 op0 = initializer_constant_valid_p_1 (op0, endtype, cache);
4246 if (!op0)
4247 return NULL_TREE;
4249 op1 = initializer_constant_valid_p_1 (op1, endtype,
4250 cache ? cache + 2 : NULL);
4251 /* Both initializers must be known. */
4252 if (op1)
4254 if (op0 == op1
4255 && (op0 == null_pointer_node
4256 || TREE_CODE (value) == MINUS_EXPR))
4257 return null_pointer_node;
4259 /* Support differences between labels. */
4260 if (TREE_CODE (op0) == LABEL_DECL
4261 && TREE_CODE (op1) == LABEL_DECL)
4262 return null_pointer_node;
4264 if (TREE_CODE (op0) == STRING_CST
4265 && TREE_CODE (op1) == STRING_CST
4266 && operand_equal_p (op0, op1, 1))
4267 return null_pointer_node;
4270 return NULL_TREE;
4273 /* Helper function of initializer_constant_valid_p.
4274 Return nonzero if VALUE is a valid constant-valued expression
4275 for use in initializing a static variable; one that can be an
4276 element of a "constant" initializer.
4278 Return null_pointer_node if the value is absolute;
4279 if it is relocatable, return the variable that determines the relocation.
4280 We assume that VALUE has been folded as much as possible;
4281 therefore, we do not need to check for such things as
4282 arithmetic-combinations of integers.
4284 Use CACHE (pointer to 2 tree values) for caching if non-NULL. */
4286 static tree
4287 initializer_constant_valid_p_1 (tree value, tree endtype, tree *cache)
4289 tree ret;
4291 switch (TREE_CODE (value))
4293 case CONSTRUCTOR:
4294 if (constructor_static_from_elts_p (value))
4296 unsigned HOST_WIDE_INT idx;
4297 tree elt;
4298 bool absolute = true;
4300 if (cache && cache[0] == value)
4301 return cache[1];
4302 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
4304 tree reloc;
4305 reloc = initializer_constant_valid_p_1 (elt, TREE_TYPE (elt),
4306 NULL);
4307 if (!reloc)
4309 if (cache)
4311 cache[0] = value;
4312 cache[1] = NULL_TREE;
4314 return NULL_TREE;
4316 if (reloc != null_pointer_node)
4317 absolute = false;
4319 /* For a non-absolute relocation, there is no single
4320 variable that can be "the variable that determines the
4321 relocation." */
4322 if (cache)
4324 cache[0] = value;
4325 cache[1] = absolute ? null_pointer_node : error_mark_node;
4327 return absolute ? null_pointer_node : error_mark_node;
4330 return TREE_STATIC (value) ? null_pointer_node : NULL_TREE;
4332 case INTEGER_CST:
4333 case VECTOR_CST:
4334 case REAL_CST:
4335 case FIXED_CST:
4336 case STRING_CST:
4337 case COMPLEX_CST:
4338 return null_pointer_node;
4340 case ADDR_EXPR:
4341 case FDESC_EXPR:
4343 tree op0 = staticp (TREE_OPERAND (value, 0));
4344 if (op0)
4346 /* "&(*a).f" is like unto pointer arithmetic. If "a" turns out
4347 to be a constant, this is old-skool offsetof-like nonsense. */
4348 if (TREE_CODE (op0) == INDIRECT_REF
4349 && TREE_CONSTANT (TREE_OPERAND (op0, 0)))
4350 return null_pointer_node;
4351 /* Taking the address of a nested function involves a trampoline,
4352 unless we don't need or want one. */
4353 if (TREE_CODE (op0) == FUNCTION_DECL
4354 && DECL_STATIC_CHAIN (op0)
4355 && !TREE_NO_TRAMPOLINE (value))
4356 return NULL_TREE;
4357 /* "&{...}" requires a temporary to hold the constructed
4358 object. */
4359 if (TREE_CODE (op0) == CONSTRUCTOR)
4360 return NULL_TREE;
4362 return op0;
4365 case NON_LVALUE_EXPR:
4366 return initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4367 endtype, cache);
4369 case VIEW_CONVERT_EXPR:
4371 tree src = TREE_OPERAND (value, 0);
4372 tree src_type = TREE_TYPE (src);
4373 tree dest_type = TREE_TYPE (value);
4375 /* Allow view-conversions from aggregate to non-aggregate type only
4376 if the bit pattern is fully preserved afterwards; otherwise, the
4377 RTL expander won't be able to apply a subsequent transformation
4378 to the underlying constructor. */
4379 if (AGGREGATE_TYPE_P (src_type) && !AGGREGATE_TYPE_P (dest_type))
4381 if (TYPE_MODE (endtype) == TYPE_MODE (dest_type))
4382 return initializer_constant_valid_p_1 (src, endtype, cache);
4383 else
4384 return NULL_TREE;
4387 /* Allow all other kinds of view-conversion. */
4388 return initializer_constant_valid_p_1 (src, endtype, cache);
4391 CASE_CONVERT:
4393 tree src = TREE_OPERAND (value, 0);
4394 tree src_type = TREE_TYPE (src);
4395 tree dest_type = TREE_TYPE (value);
4397 /* Allow conversions between pointer types, floating-point
4398 types, and offset types. */
4399 if ((POINTER_TYPE_P (dest_type) && POINTER_TYPE_P (src_type))
4400 || (FLOAT_TYPE_P (dest_type) && FLOAT_TYPE_P (src_type))
4401 || (TREE_CODE (dest_type) == OFFSET_TYPE
4402 && TREE_CODE (src_type) == OFFSET_TYPE))
4403 return initializer_constant_valid_p_1 (src, endtype, cache);
4405 /* Allow length-preserving conversions between integer types. */
4406 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type)
4407 && (TYPE_PRECISION (dest_type) == TYPE_PRECISION (src_type)))
4408 return initializer_constant_valid_p_1 (src, endtype, cache);
4410 /* Allow conversions between other integer types only if
4411 explicit value. */
4412 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type))
4414 tree inner = initializer_constant_valid_p_1 (src, endtype, cache);
4415 if (inner == null_pointer_node)
4416 return null_pointer_node;
4417 break;
4420 /* Allow (int) &foo provided int is as wide as a pointer. */
4421 if (INTEGRAL_TYPE_P (dest_type) && POINTER_TYPE_P (src_type)
4422 && (TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type)))
4423 return initializer_constant_valid_p_1 (src, endtype, cache);
4425 /* Likewise conversions from int to pointers, but also allow
4426 conversions from 0. */
4427 if ((POINTER_TYPE_P (dest_type)
4428 || TREE_CODE (dest_type) == OFFSET_TYPE)
4429 && INTEGRAL_TYPE_P (src_type))
4431 if (TREE_CODE (src) == INTEGER_CST
4432 && TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type))
4433 return null_pointer_node;
4434 if (integer_zerop (src))
4435 return null_pointer_node;
4436 else if (TYPE_PRECISION (dest_type) <= TYPE_PRECISION (src_type))
4437 return initializer_constant_valid_p_1 (src, endtype, cache);
4440 /* Allow conversions to struct or union types if the value
4441 inside is okay. */
4442 if (TREE_CODE (dest_type) == RECORD_TYPE
4443 || TREE_CODE (dest_type) == UNION_TYPE)
4444 return initializer_constant_valid_p_1 (src, endtype, cache);
4446 break;
4448 case POINTER_PLUS_EXPR:
4449 case PLUS_EXPR:
4450 /* Any valid floating-point constants will have been folded by now;
4451 with -frounding-math we hit this with addition of two constants. */
4452 if (TREE_CODE (endtype) == REAL_TYPE)
4453 return NULL_TREE;
4454 if (cache && cache[0] == value)
4455 return cache[1];
4456 if (! INTEGRAL_TYPE_P (endtype)
4457 || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
4459 tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
4460 tree valid0
4461 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4462 endtype, ncache);
4463 tree valid1
4464 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
4465 endtype, ncache + 2);
4466 /* If either term is absolute, use the other term's relocation. */
4467 if (valid0 == null_pointer_node)
4468 ret = valid1;
4469 else if (valid1 == null_pointer_node)
4470 ret = valid0;
4471 /* Support narrowing pointer differences. */
4472 else
4473 ret = narrowing_initializer_constant_valid_p (value, endtype,
4474 ncache);
4476 else
4477 /* Support narrowing pointer differences. */
4478 ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
4479 if (cache)
4481 cache[0] = value;
4482 cache[1] = ret;
4484 return ret;
4486 case MINUS_EXPR:
4487 if (TREE_CODE (endtype) == REAL_TYPE)
4488 return NULL_TREE;
4489 if (cache && cache[0] == value)
4490 return cache[1];
4491 if (! INTEGRAL_TYPE_P (endtype)
4492 || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
4494 tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
4495 tree valid0
4496 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4497 endtype, ncache);
4498 tree valid1
4499 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
4500 endtype, ncache + 2);
4501 /* Win if second argument is absolute. */
4502 if (valid1 == null_pointer_node)
4503 ret = valid0;
4504 /* Win if both arguments have the same relocation.
4505 Then the value is absolute. */
4506 else if (valid0 == valid1 && valid0 != 0)
4507 ret = null_pointer_node;
4508 /* Since GCC guarantees that string constants are unique in the
4509 generated code, a subtraction between two copies of the same
4510 constant string is absolute. */
4511 else if (valid0 && TREE_CODE (valid0) == STRING_CST
4512 && valid1 && TREE_CODE (valid1) == STRING_CST
4513 && operand_equal_p (valid0, valid1, 1))
4514 ret = null_pointer_node;
4515 /* Support narrowing differences. */
4516 else
4517 ret = narrowing_initializer_constant_valid_p (value, endtype,
4518 ncache);
4520 else
4521 /* Support narrowing differences. */
4522 ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
4523 if (cache)
4525 cache[0] = value;
4526 cache[1] = ret;
4528 return ret;
4530 default:
4531 break;
4534 return NULL_TREE;
4537 /* Return nonzero if VALUE is a valid constant-valued expression
4538 for use in initializing a static variable; one that can be an
4539 element of a "constant" initializer.
4541 Return null_pointer_node if the value is absolute;
4542 if it is relocatable, return the variable that determines the relocation.
4543 We assume that VALUE has been folded as much as possible;
4544 therefore, we do not need to check for such things as
4545 arithmetic-combinations of integers. */
4546 tree
4547 initializer_constant_valid_p (tree value, tree endtype)
4549 return initializer_constant_valid_p_1 (value, endtype, NULL);
4552 /* Return true if VALUE is a valid constant-valued expression
4553 for use in initializing a static bit-field; one that can be
4554 an element of a "constant" initializer. */
4556 bool
4557 initializer_constant_valid_for_bitfield_p (tree value)
4559 /* For bitfields we support integer constants or possibly nested aggregates
4560 of such. */
4561 switch (TREE_CODE (value))
4563 case CONSTRUCTOR:
4565 unsigned HOST_WIDE_INT idx;
4566 tree elt;
4568 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
4569 if (!initializer_constant_valid_for_bitfield_p (elt))
4570 return false;
4571 return true;
4574 case INTEGER_CST:
4575 case REAL_CST:
4576 return true;
4578 case VIEW_CONVERT_EXPR:
4579 case NON_LVALUE_EXPR:
4580 return
4581 initializer_constant_valid_for_bitfield_p (TREE_OPERAND (value, 0));
4583 default:
4584 break;
4587 return false;
4590 /* output_constructor outer state of relevance in recursive calls, typically
4591 for nested aggregate bitfields. */
4593 typedef struct {
4594 unsigned int bit_offset; /* current position in ... */
4595 int byte; /* ... the outer byte buffer. */
4596 } oc_outer_state;
4598 static unsigned HOST_WIDE_INT
4599 output_constructor (tree, unsigned HOST_WIDE_INT, unsigned int,
4600 oc_outer_state *);
4602 /* Output assembler code for constant EXP, with no label.
4603 This includes the pseudo-op such as ".int" or ".byte", and a newline.
4604 Assumes output_addressed_constants has been done on EXP already.
4606 Generate at least SIZE bytes of assembler data, padding at the end
4607 with zeros if necessary. SIZE must always be specified. The returned
4608 value is the actual number of bytes of assembler data generated, which
4609 may be bigger than SIZE if the object contains a variable length field.
4611 SIZE is important for structure constructors,
4612 since trailing members may have been omitted from the constructor.
4613 It is also important for initialization of arrays from string constants
4614 since the full length of the string constant might not be wanted.
4615 It is also needed for initialization of unions, where the initializer's
4616 type is just one member, and that may not be as long as the union.
4618 There a case in which we would fail to output exactly SIZE bytes:
4619 for a structure constructor that wants to produce more than SIZE bytes.
4620 But such constructors will never be generated for any possible input.
4622 ALIGN is the alignment of the data in bits. */
4624 static unsigned HOST_WIDE_INT
4625 output_constant (tree exp, unsigned HOST_WIDE_INT size, unsigned int align)
4627 enum tree_code code;
4628 unsigned HOST_WIDE_INT thissize;
4630 if (size == 0 || flag_syntax_only)
4631 return size;
4633 /* See if we're trying to initialize a pointer in a non-default mode
4634 to the address of some declaration somewhere. If the target says
4635 the mode is valid for pointers, assume the target has a way of
4636 resolving it. */
4637 if (TREE_CODE (exp) == NOP_EXPR
4638 && POINTER_TYPE_P (TREE_TYPE (exp))
4639 && targetm.addr_space.valid_pointer_mode
4640 (TYPE_MODE (TREE_TYPE (exp)),
4641 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
4643 tree saved_type = TREE_TYPE (exp);
4645 /* Peel off any intermediate conversions-to-pointer for valid
4646 pointer modes. */
4647 while (TREE_CODE (exp) == NOP_EXPR
4648 && POINTER_TYPE_P (TREE_TYPE (exp))
4649 && targetm.addr_space.valid_pointer_mode
4650 (TYPE_MODE (TREE_TYPE (exp)),
4651 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
4652 exp = TREE_OPERAND (exp, 0);
4654 /* If what we're left with is the address of something, we can
4655 convert the address to the final type and output it that
4656 way. */
4657 if (TREE_CODE (exp) == ADDR_EXPR)
4658 exp = build1 (ADDR_EXPR, saved_type, TREE_OPERAND (exp, 0));
4659 /* Likewise for constant ints. */
4660 else if (TREE_CODE (exp) == INTEGER_CST)
4661 exp = wide_int_to_tree (saved_type, exp);
4665 /* Eliminate any conversions since we'll be outputting the underlying
4666 constant. */
4667 while (CONVERT_EXPR_P (exp)
4668 || TREE_CODE (exp) == NON_LVALUE_EXPR
4669 || TREE_CODE (exp) == VIEW_CONVERT_EXPR)
4671 HOST_WIDE_INT type_size = int_size_in_bytes (TREE_TYPE (exp));
4672 HOST_WIDE_INT op_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (exp, 0)));
4674 /* Make sure eliminating the conversion is really a no-op, except with
4675 VIEW_CONVERT_EXPRs to allow for wild Ada unchecked conversions and
4676 union types to allow for Ada unchecked unions. */
4677 if (type_size > op_size
4678 && TREE_CODE (exp) != VIEW_CONVERT_EXPR
4679 && TREE_CODE (TREE_TYPE (exp)) != UNION_TYPE)
4680 /* Keep the conversion. */
4681 break;
4682 else
4683 exp = TREE_OPERAND (exp, 0);
4686 code = TREE_CODE (TREE_TYPE (exp));
4687 thissize = int_size_in_bytes (TREE_TYPE (exp));
4689 /* Allow a constructor with no elements for any data type.
4690 This means to fill the space with zeros. */
4691 if (TREE_CODE (exp) == CONSTRUCTOR
4692 && vec_safe_is_empty (CONSTRUCTOR_ELTS (exp)))
4694 assemble_zeros (size);
4695 return size;
4698 if (TREE_CODE (exp) == FDESC_EXPR)
4700 #ifdef ASM_OUTPUT_FDESC
4701 HOST_WIDE_INT part = tree_to_shwi (TREE_OPERAND (exp, 1));
4702 tree decl = TREE_OPERAND (exp, 0);
4703 ASM_OUTPUT_FDESC (asm_out_file, decl, part);
4704 #else
4705 gcc_unreachable ();
4706 #endif
4707 return size;
4710 /* Now output the underlying data. If we've handling the padding, return.
4711 Otherwise, break and ensure SIZE is the size written. */
4712 switch (code)
4714 case BOOLEAN_TYPE:
4715 case INTEGER_TYPE:
4716 case ENUMERAL_TYPE:
4717 case POINTER_TYPE:
4718 case REFERENCE_TYPE:
4719 case OFFSET_TYPE:
4720 case FIXED_POINT_TYPE:
4721 case POINTER_BOUNDS_TYPE:
4722 case NULLPTR_TYPE:
4723 if (! assemble_integer (expand_expr (exp, NULL_RTX, VOIDmode,
4724 EXPAND_INITIALIZER),
4725 MIN (size, thissize), align, 0))
4726 error ("initializer for integer/fixed-point value is too complicated");
4727 break;
4729 case REAL_TYPE:
4730 if (TREE_CODE (exp) != REAL_CST)
4731 error ("initializer for floating value is not a floating constant");
4732 else
4733 assemble_real (TREE_REAL_CST (exp), TYPE_MODE (TREE_TYPE (exp)), align);
4734 break;
4736 case COMPLEX_TYPE:
4737 output_constant (TREE_REALPART (exp), thissize / 2, align);
4738 output_constant (TREE_IMAGPART (exp), thissize / 2,
4739 min_align (align, BITS_PER_UNIT * (thissize / 2)));
4740 break;
4742 case ARRAY_TYPE:
4743 case VECTOR_TYPE:
4744 switch (TREE_CODE (exp))
4746 case CONSTRUCTOR:
4747 return output_constructor (exp, size, align, NULL);
4748 case STRING_CST:
4749 thissize
4750 = MIN ((unsigned HOST_WIDE_INT)TREE_STRING_LENGTH (exp), size);
4751 assemble_string (TREE_STRING_POINTER (exp), thissize);
4752 break;
4753 case VECTOR_CST:
4755 machine_mode inner = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
4756 unsigned int nalign = MIN (align, GET_MODE_ALIGNMENT (inner));
4757 int elt_size = GET_MODE_SIZE (inner);
4758 output_constant (VECTOR_CST_ELT (exp, 0), elt_size, align);
4759 thissize = elt_size;
4760 for (unsigned int i = 1; i < VECTOR_CST_NELTS (exp); i++)
4762 output_constant (VECTOR_CST_ELT (exp, i), elt_size, nalign);
4763 thissize += elt_size;
4765 break;
4767 default:
4768 gcc_unreachable ();
4770 break;
4772 case RECORD_TYPE:
4773 case UNION_TYPE:
4774 gcc_assert (TREE_CODE (exp) == CONSTRUCTOR);
4775 return output_constructor (exp, size, align, NULL);
4777 case ERROR_MARK:
4778 return 0;
4780 default:
4781 gcc_unreachable ();
4784 if (size > thissize)
4785 assemble_zeros (size - thissize);
4787 return size;
4791 /* Subroutine of output_constructor, used for computing the size of
4792 arrays of unspecified length. VAL must be a CONSTRUCTOR of an array
4793 type with an unspecified upper bound. */
4795 static unsigned HOST_WIDE_INT
4796 array_size_for_constructor (tree val)
4798 tree max_index;
4799 unsigned HOST_WIDE_INT cnt;
4800 tree index, value, tmp;
4801 offset_int i;
4803 /* This code used to attempt to handle string constants that are not
4804 arrays of single-bytes, but nothing else does, so there's no point in
4805 doing it here. */
4806 if (TREE_CODE (val) == STRING_CST)
4807 return TREE_STRING_LENGTH (val);
4809 max_index = NULL_TREE;
4810 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (val), cnt, index, value)
4812 if (TREE_CODE (index) == RANGE_EXPR)
4813 index = TREE_OPERAND (index, 1);
4814 if (max_index == NULL_TREE || tree_int_cst_lt (max_index, index))
4815 max_index = index;
4818 if (max_index == NULL_TREE)
4819 return 0;
4821 /* Compute the total number of array elements. */
4822 tmp = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (val)));
4823 i = wi::to_offset (max_index) - wi::to_offset (tmp) + 1;
4825 /* Multiply by the array element unit size to find number of bytes. */
4826 i *= wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (val))));
4828 gcc_assert (wi::fits_uhwi_p (i));
4829 return i.to_uhwi ();
4832 /* Other datastructures + helpers for output_constructor. */
4834 /* output_constructor local state to support interaction with helpers. */
4836 typedef struct {
4838 /* Received arguments. */
4839 tree exp; /* Constructor expression. */
4840 tree type; /* Type of constructor expression. */
4841 unsigned HOST_WIDE_INT size; /* # bytes to output - pad if necessary. */
4842 unsigned int align; /* Known initial alignment. */
4843 tree min_index; /* Lower bound if specified for an array. */
4845 /* Output processing state. */
4846 HOST_WIDE_INT total_bytes; /* # bytes output so far / current position. */
4847 int byte; /* Part of a bitfield byte yet to be output. */
4848 int last_relative_index; /* Implicit or explicit index of the last
4849 array element output within a bitfield. */
4850 bool byte_buffer_in_use; /* Whether BYTE is in use. */
4852 /* Current element. */
4853 tree field; /* Current field decl in a record. */
4854 tree val; /* Current element value. */
4855 tree index; /* Current element index. */
4857 } oc_local_state;
4859 /* Helper for output_constructor. From the current LOCAL state, output a
4860 RANGE_EXPR element. */
4862 static void
4863 output_constructor_array_range (oc_local_state *local)
4865 unsigned HOST_WIDE_INT fieldsize
4866 = int_size_in_bytes (TREE_TYPE (local->type));
4868 HOST_WIDE_INT lo_index
4869 = tree_to_shwi (TREE_OPERAND (local->index, 0));
4870 HOST_WIDE_INT hi_index
4871 = tree_to_shwi (TREE_OPERAND (local->index, 1));
4872 HOST_WIDE_INT index;
4874 unsigned int align2
4875 = min_align (local->align, fieldsize * BITS_PER_UNIT);
4877 for (index = lo_index; index <= hi_index; index++)
4879 /* Output the element's initial value. */
4880 if (local->val == NULL_TREE)
4881 assemble_zeros (fieldsize);
4882 else
4883 fieldsize = output_constant (local->val, fieldsize, align2);
4885 /* Count its size. */
4886 local->total_bytes += fieldsize;
4890 /* Helper for output_constructor. From the current LOCAL state, output a
4891 field element that is not true bitfield or part of an outer one. */
4893 static void
4894 output_constructor_regular_field (oc_local_state *local)
4896 /* Field size and position. Since this structure is static, we know the
4897 positions are constant. */
4898 unsigned HOST_WIDE_INT fieldsize;
4899 HOST_WIDE_INT fieldpos;
4901 unsigned int align2;
4903 if (local->index != NULL_TREE)
4905 /* Perform the index calculation in modulo arithmetic but
4906 sign-extend the result because Ada has negative DECL_FIELD_OFFSETs
4907 but we are using an unsigned sizetype. */
4908 unsigned prec = TYPE_PRECISION (sizetype);
4909 offset_int idx = wi::sext (wi::to_offset (local->index)
4910 - wi::to_offset (local->min_index), prec);
4911 fieldpos = (idx * wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (local->val))))
4912 .to_short_addr ();
4914 else if (local->field != NULL_TREE)
4915 fieldpos = int_byte_position (local->field);
4916 else
4917 fieldpos = 0;
4919 /* Output any buffered-up bit-fields preceding this element. */
4920 if (local->byte_buffer_in_use)
4922 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
4923 local->total_bytes++;
4924 local->byte_buffer_in_use = false;
4927 /* Advance to offset of this element.
4928 Note no alignment needed in an array, since that is guaranteed
4929 if each element has the proper size. */
4930 if ((local->field != NULL_TREE || local->index != NULL_TREE)
4931 && fieldpos > local->total_bytes)
4933 assemble_zeros (fieldpos - local->total_bytes);
4934 local->total_bytes = fieldpos;
4937 /* Find the alignment of this element. */
4938 align2 = min_align (local->align, BITS_PER_UNIT * fieldpos);
4940 /* Determine size this element should occupy. */
4941 if (local->field)
4943 fieldsize = 0;
4945 /* If this is an array with an unspecified upper bound,
4946 the initializer determines the size. */
4947 /* ??? This ought to only checked if DECL_SIZE_UNIT is NULL,
4948 but we cannot do this until the deprecated support for
4949 initializing zero-length array members is removed. */
4950 if (TREE_CODE (TREE_TYPE (local->field)) == ARRAY_TYPE
4951 && TYPE_DOMAIN (TREE_TYPE (local->field))
4952 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (local->field))))
4954 fieldsize = array_size_for_constructor (local->val);
4955 /* Given a non-empty initialization, this field had
4956 better be last. */
4957 gcc_assert (!fieldsize || !DECL_CHAIN (local->field));
4959 else
4960 fieldsize = tree_to_uhwi (DECL_SIZE_UNIT (local->field));
4962 else
4963 fieldsize = int_size_in_bytes (TREE_TYPE (local->type));
4965 /* Output the element's initial value. */
4966 if (local->val == NULL_TREE)
4967 assemble_zeros (fieldsize);
4968 else
4969 fieldsize = output_constant (local->val, fieldsize, align2);
4971 /* Count its size. */
4972 local->total_bytes += fieldsize;
4975 /* Helper for output_constructor. From the LOCAL state, output an element
4976 that is a true bitfield or part of an outer one. BIT_OFFSET is the offset
4977 from the start of a possibly ongoing outer byte buffer. */
4979 static void
4980 output_constructor_bitfield (oc_local_state *local, unsigned int bit_offset)
4982 /* Bit size of this element. */
4983 HOST_WIDE_INT ebitsize
4984 = (local->field
4985 ? tree_to_uhwi (DECL_SIZE (local->field))
4986 : tree_to_uhwi (TYPE_SIZE (TREE_TYPE (local->type))));
4988 /* Relative index of this element if this is an array component. */
4989 HOST_WIDE_INT relative_index
4990 = (!local->field
4991 ? (local->index
4992 ? (tree_to_shwi (local->index)
4993 - tree_to_shwi (local->min_index))
4994 : local->last_relative_index + 1)
4995 : 0);
4997 /* Bit position of this element from the start of the containing
4998 constructor. */
4999 HOST_WIDE_INT constructor_relative_ebitpos
5000 = (local->field
5001 ? int_bit_position (local->field)
5002 : ebitsize * relative_index);
5004 /* Bit position of this element from the start of a possibly ongoing
5005 outer byte buffer. */
5006 HOST_WIDE_INT byte_relative_ebitpos
5007 = bit_offset + constructor_relative_ebitpos;
5009 /* From the start of a possibly ongoing outer byte buffer, offsets to
5010 the first bit of this element and to the first bit past the end of
5011 this element. */
5012 HOST_WIDE_INT next_offset = byte_relative_ebitpos;
5013 HOST_WIDE_INT end_offset = byte_relative_ebitpos + ebitsize;
5015 local->last_relative_index = relative_index;
5017 if (local->val == NULL_TREE)
5018 local->val = integer_zero_node;
5020 while (TREE_CODE (local->val) == VIEW_CONVERT_EXPR
5021 || TREE_CODE (local->val) == NON_LVALUE_EXPR)
5022 local->val = TREE_OPERAND (local->val, 0);
5024 if (TREE_CODE (local->val) != INTEGER_CST
5025 && TREE_CODE (local->val) != CONSTRUCTOR)
5027 error ("invalid initial value for member %qE", DECL_NAME (local->field));
5028 return;
5031 /* If this field does not start in this (or next) byte, skip some bytes. */
5032 if (next_offset / BITS_PER_UNIT != local->total_bytes)
5034 /* Output remnant of any bit field in previous bytes. */
5035 if (local->byte_buffer_in_use)
5037 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5038 local->total_bytes++;
5039 local->byte_buffer_in_use = false;
5042 /* If still not at proper byte, advance to there. */
5043 if (next_offset / BITS_PER_UNIT != local->total_bytes)
5045 gcc_assert (next_offset / BITS_PER_UNIT >= local->total_bytes);
5046 assemble_zeros (next_offset / BITS_PER_UNIT - local->total_bytes);
5047 local->total_bytes = next_offset / BITS_PER_UNIT;
5051 /* Set up the buffer if necessary. */
5052 if (!local->byte_buffer_in_use)
5054 local->byte = 0;
5055 if (ebitsize > 0)
5056 local->byte_buffer_in_use = true;
5059 /* If this is nested constructor, recurse passing the bit offset and the
5060 pending data, then retrieve the new pending data afterwards. */
5061 if (TREE_CODE (local->val) == CONSTRUCTOR)
5063 oc_outer_state temp_state;
5064 temp_state.bit_offset = next_offset % BITS_PER_UNIT;
5065 temp_state.byte = local->byte;
5066 local->total_bytes
5067 += output_constructor (local->val, 0, 0, &temp_state);
5068 local->byte = temp_state.byte;
5069 return;
5072 /* Otherwise, we must split the element into pieces that fall within
5073 separate bytes, and combine each byte with previous or following
5074 bit-fields. */
5075 while (next_offset < end_offset)
5077 int this_time;
5078 int shift;
5079 HOST_WIDE_INT value;
5080 HOST_WIDE_INT next_byte = next_offset / BITS_PER_UNIT;
5081 HOST_WIDE_INT next_bit = next_offset % BITS_PER_UNIT;
5083 /* Advance from byte to byte within this element when necessary. */
5084 while (next_byte != local->total_bytes)
5086 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5087 local->total_bytes++;
5088 local->byte = 0;
5091 /* Number of bits we can process at once (all part of the same byte). */
5092 this_time = MIN (end_offset - next_offset, BITS_PER_UNIT - next_bit);
5093 if (BYTES_BIG_ENDIAN)
5095 /* On big-endian machine, take the most significant bits (of the
5096 bits that are significant) first and put them into bytes from
5097 the most significant end. */
5098 shift = end_offset - next_offset - this_time;
5100 /* Don't try to take a bunch of bits that cross
5101 the word boundary in the INTEGER_CST. We can
5102 only select bits from one element. */
5103 if ((shift / HOST_BITS_PER_WIDE_INT)
5104 != ((shift + this_time - 1) / HOST_BITS_PER_WIDE_INT))
5106 const int end = shift + this_time - 1;
5107 shift = end & -HOST_BITS_PER_WIDE_INT;
5108 this_time = end - shift + 1;
5111 /* Now get the bits from the appropriate constant word. */
5112 value = TREE_INT_CST_ELT (local->val, shift / HOST_BITS_PER_WIDE_INT);
5113 shift = shift & (HOST_BITS_PER_WIDE_INT - 1);
5115 /* Get the result. This works only when:
5116 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
5117 local->byte |= (((value >> shift)
5118 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
5119 << (BITS_PER_UNIT - this_time - next_bit));
5121 else
5123 /* On little-endian machines, take the least significant bits of
5124 the value first and pack them starting at the least significant
5125 bits of the bytes. */
5126 shift = next_offset - byte_relative_ebitpos;
5128 /* Don't try to take a bunch of bits that cross
5129 the word boundary in the INTEGER_CST. We can
5130 only select bits from one element. */
5131 if ((shift / HOST_BITS_PER_WIDE_INT)
5132 != ((shift + this_time - 1) / HOST_BITS_PER_WIDE_INT))
5133 this_time
5134 = HOST_BITS_PER_WIDE_INT - (shift & (HOST_BITS_PER_WIDE_INT - 1));
5136 /* Now get the bits from the appropriate constant word. */
5137 value = TREE_INT_CST_ELT (local->val, shift / HOST_BITS_PER_WIDE_INT);
5138 shift = shift & (HOST_BITS_PER_WIDE_INT - 1);
5140 /* Get the result. This works only when:
5141 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
5142 local->byte |= (((value >> shift)
5143 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
5144 << next_bit);
5147 next_offset += this_time;
5148 local->byte_buffer_in_use = true;
5152 /* Subroutine of output_constant, used for CONSTRUCTORs (aggregate constants).
5153 Generate at least SIZE bytes, padding if necessary. OUTER designates the
5154 caller output state of relevance in recursive invocations. */
5156 static unsigned HOST_WIDE_INT
5157 output_constructor (tree exp, unsigned HOST_WIDE_INT size,
5158 unsigned int align, oc_outer_state *outer)
5160 unsigned HOST_WIDE_INT cnt;
5161 constructor_elt *ce;
5163 oc_local_state local;
5165 /* Setup our local state to communicate with helpers. */
5166 local.exp = exp;
5167 local.type = TREE_TYPE (exp);
5168 local.size = size;
5169 local.align = align;
5170 if (TREE_CODE (local.type) == ARRAY_TYPE && TYPE_DOMAIN (local.type))
5171 local.min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (local.type));
5172 else
5173 local.min_index = NULL_TREE;
5175 local.total_bytes = 0;
5176 local.byte_buffer_in_use = outer != NULL;
5177 local.byte = outer ? outer->byte : 0;
5178 local.last_relative_index = -1;
5180 gcc_assert (HOST_BITS_PER_WIDE_INT >= BITS_PER_UNIT);
5182 /* As CE goes through the elements of the constant, FIELD goes through the
5183 structure fields if the constant is a structure. If the constant is a
5184 union, we override this by getting the field from the TREE_LIST element.
5185 But the constant could also be an array. Then FIELD is zero.
5187 There is always a maximum of one element in the chain LINK for unions
5188 (even if the initializer in a source program incorrectly contains
5189 more one). */
5191 if (TREE_CODE (local.type) == RECORD_TYPE)
5192 local.field = TYPE_FIELDS (local.type);
5193 else
5194 local.field = NULL_TREE;
5196 for (cnt = 0;
5197 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), cnt, &ce);
5198 cnt++, local.field = local.field ? DECL_CHAIN (local.field) : 0)
5200 local.val = ce->value;
5201 local.index = NULL_TREE;
5203 /* The element in a union constructor specifies the proper field
5204 or index. */
5205 if (RECORD_OR_UNION_TYPE_P (local.type) && ce->index != NULL_TREE)
5206 local.field = ce->index;
5208 else if (TREE_CODE (local.type) == ARRAY_TYPE)
5209 local.index = ce->index;
5211 if (local.field && flag_verbose_asm)
5212 fprintf (asm_out_file, "%s %s:\n",
5213 ASM_COMMENT_START,
5214 DECL_NAME (local.field)
5215 ? IDENTIFIER_POINTER (DECL_NAME (local.field))
5216 : "<anonymous>");
5218 /* Eliminate the marker that makes a cast not be an lvalue. */
5219 if (local.val != NULL_TREE)
5220 STRIP_NOPS (local.val);
5222 /* Output the current element, using the appropriate helper ... */
5224 /* For an array slice not part of an outer bitfield. */
5225 if (!outer
5226 && local.index != NULL_TREE
5227 && TREE_CODE (local.index) == RANGE_EXPR)
5228 output_constructor_array_range (&local);
5230 /* For a field that is neither a true bitfield nor part of an outer one,
5231 known to be at least byte aligned and multiple-of-bytes long. */
5232 else if (!outer
5233 && (local.field == NULL_TREE
5234 || !CONSTRUCTOR_BITFIELD_P (local.field)))
5235 output_constructor_regular_field (&local);
5237 /* For a true bitfield or part of an outer one. Only INTEGER_CSTs are
5238 supported for scalar fields, so we may need to convert first. */
5239 else
5241 if (TREE_CODE (local.val) == REAL_CST)
5242 local.val
5243 = fold_unary (VIEW_CONVERT_EXPR,
5244 build_nonstandard_integer_type
5245 (TYPE_PRECISION (TREE_TYPE (local.val)), 0),
5246 local.val);
5247 output_constructor_bitfield (&local, outer ? outer->bit_offset : 0);
5251 /* If we are not at toplevel, save the pending data for our caller.
5252 Otherwise output the pending data and padding zeros as needed. */
5253 if (outer)
5254 outer->byte = local.byte;
5255 else
5257 if (local.byte_buffer_in_use)
5259 assemble_integer (GEN_INT (local.byte), 1, BITS_PER_UNIT, 1);
5260 local.total_bytes++;
5263 if ((unsigned HOST_WIDE_INT)local.total_bytes < local.size)
5265 assemble_zeros (local.size - local.total_bytes);
5266 local.total_bytes = local.size;
5270 return local.total_bytes;
5273 /* Mark DECL as weak. */
5275 static void
5276 mark_weak (tree decl)
5278 if (DECL_WEAK (decl))
5279 return;
5281 struct symtab_node *n = symtab_node::get (decl);
5282 if (n && n->refuse_visibility_changes)
5283 error ("%+D declared weak after being used", decl);
5284 DECL_WEAK (decl) = 1;
5286 if (DECL_RTL_SET_P (decl)
5287 && MEM_P (DECL_RTL (decl))
5288 && XEXP (DECL_RTL (decl), 0)
5289 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == SYMBOL_REF)
5290 SYMBOL_REF_WEAK (XEXP (DECL_RTL (decl), 0)) = 1;
5293 /* Merge weak status between NEWDECL and OLDDECL. */
5295 void
5296 merge_weak (tree newdecl, tree olddecl)
5298 if (DECL_WEAK (newdecl) == DECL_WEAK (olddecl))
5300 if (DECL_WEAK (newdecl) && TARGET_SUPPORTS_WEAK)
5302 tree *pwd;
5303 /* We put the NEWDECL on the weak_decls list at some point
5304 and OLDDECL as well. Keep just OLDDECL on the list. */
5305 for (pwd = &weak_decls; *pwd; pwd = &TREE_CHAIN (*pwd))
5306 if (TREE_VALUE (*pwd) == newdecl)
5308 *pwd = TREE_CHAIN (*pwd);
5309 break;
5312 return;
5315 if (DECL_WEAK (newdecl))
5317 tree wd;
5319 /* NEWDECL is weak, but OLDDECL is not. */
5321 /* If we already output the OLDDECL, we're in trouble; we can't
5322 go back and make it weak. This should never happen in
5323 unit-at-a-time compilation. */
5324 gcc_assert (!TREE_ASM_WRITTEN (olddecl));
5326 /* If we've already generated rtl referencing OLDDECL, we may
5327 have done so in a way that will not function properly with
5328 a weak symbol. Again in unit-at-a-time this should be
5329 impossible. */
5330 gcc_assert (!TREE_USED (olddecl)
5331 || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (olddecl)));
5333 if (TARGET_SUPPORTS_WEAK)
5335 /* We put the NEWDECL on the weak_decls list at some point.
5336 Replace it with the OLDDECL. */
5337 for (wd = weak_decls; wd; wd = TREE_CHAIN (wd))
5338 if (TREE_VALUE (wd) == newdecl)
5340 TREE_VALUE (wd) = olddecl;
5341 break;
5343 /* We may not find the entry on the list. If NEWDECL is a
5344 weak alias, then we will have already called
5345 globalize_decl to remove the entry; in that case, we do
5346 not need to do anything. */
5349 /* Make the OLDDECL weak; it's OLDDECL that we'll be keeping. */
5350 mark_weak (olddecl);
5352 else
5353 /* OLDDECL was weak, but NEWDECL was not explicitly marked as
5354 weak. Just update NEWDECL to indicate that it's weak too. */
5355 mark_weak (newdecl);
5358 /* Declare DECL to be a weak symbol. */
5360 void
5361 declare_weak (tree decl)
5363 gcc_assert (TREE_CODE (decl) != FUNCTION_DECL || !TREE_ASM_WRITTEN (decl));
5364 if (! TREE_PUBLIC (decl))
5365 error ("weak declaration of %q+D must be public", decl);
5366 else if (!TARGET_SUPPORTS_WEAK)
5367 warning (0, "weak declaration of %q+D not supported", decl);
5369 mark_weak (decl);
5370 if (!lookup_attribute ("weak", DECL_ATTRIBUTES (decl)))
5371 DECL_ATTRIBUTES (decl)
5372 = tree_cons (get_identifier ("weak"), NULL, DECL_ATTRIBUTES (decl));
5375 static void
5376 weak_finish_1 (tree decl)
5378 #if defined (ASM_WEAKEN_DECL) || defined (ASM_WEAKEN_LABEL)
5379 const char *const name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5380 #endif
5382 if (! TREE_USED (decl))
5383 return;
5385 #ifdef ASM_WEAKEN_DECL
5386 ASM_WEAKEN_DECL (asm_out_file, decl, name, NULL);
5387 #else
5388 #ifdef ASM_WEAKEN_LABEL
5389 ASM_WEAKEN_LABEL (asm_out_file, name);
5390 #else
5391 #ifdef ASM_OUTPUT_WEAK_ALIAS
5393 static bool warn_once = 0;
5394 if (! warn_once)
5396 warning (0, "only weak aliases are supported in this configuration");
5397 warn_once = 1;
5399 return;
5401 #endif
5402 #endif
5403 #endif
5406 /* Fiven an assembly name, find the decl it is associated with. */
5407 static tree
5408 find_decl (tree target)
5410 symtab_node *node = symtab_node::get_for_asmname (target);
5411 if (node)
5412 return node->decl;
5413 return NULL_TREE;
5416 /* This TREE_LIST contains weakref targets. */
5418 static GTY(()) tree weakref_targets;
5420 /* Emit any pending weak declarations. */
5422 void
5423 weak_finish (void)
5425 tree t;
5427 for (t = weakref_targets; t; t = TREE_CHAIN (t))
5429 tree alias_decl = TREE_PURPOSE (t);
5430 tree target = ultimate_transparent_alias_target (&TREE_VALUE (t));
5432 if (! TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (alias_decl)))
5433 /* Remove alias_decl from the weak list, but leave entries for
5434 the target alone. */
5435 target = NULL_TREE;
5436 #ifndef ASM_OUTPUT_WEAKREF
5437 else if (! TREE_SYMBOL_REFERENCED (target))
5439 /* Use ASM_WEAKEN_LABEL only if ASM_WEAKEN_DECL is not
5440 defined, otherwise we and weak_finish_1 would use
5441 different macros. */
5442 # if defined ASM_WEAKEN_LABEL && ! defined ASM_WEAKEN_DECL
5443 ASM_WEAKEN_LABEL (asm_out_file, IDENTIFIER_POINTER (target));
5444 # else
5445 tree decl = find_decl (target);
5447 if (! decl)
5449 decl = build_decl (DECL_SOURCE_LOCATION (alias_decl),
5450 TREE_CODE (alias_decl), target,
5451 TREE_TYPE (alias_decl));
5453 DECL_EXTERNAL (decl) = 1;
5454 TREE_PUBLIC (decl) = 1;
5455 DECL_ARTIFICIAL (decl) = 1;
5456 TREE_NOTHROW (decl) = TREE_NOTHROW (alias_decl);
5457 TREE_USED (decl) = 1;
5460 weak_finish_1 (decl);
5461 # endif
5463 #endif
5466 tree *p;
5467 tree t2;
5469 /* Remove the alias and the target from the pending weak list
5470 so that we do not emit any .weak directives for the former,
5471 nor multiple .weak directives for the latter. */
5472 for (p = &weak_decls; (t2 = *p) ; )
5474 if (TREE_VALUE (t2) == alias_decl
5475 || target == DECL_ASSEMBLER_NAME (TREE_VALUE (t2)))
5476 *p = TREE_CHAIN (t2);
5477 else
5478 p = &TREE_CHAIN (t2);
5481 /* Remove other weakrefs to the same target, to speed things up. */
5482 for (p = &TREE_CHAIN (t); (t2 = *p) ; )
5484 if (target == ultimate_transparent_alias_target (&TREE_VALUE (t2)))
5485 *p = TREE_CHAIN (t2);
5486 else
5487 p = &TREE_CHAIN (t2);
5492 for (t = weak_decls; t; t = TREE_CHAIN (t))
5494 tree decl = TREE_VALUE (t);
5496 weak_finish_1 (decl);
5500 /* Emit the assembly bits to indicate that DECL is globally visible. */
5502 static void
5503 globalize_decl (tree decl)
5506 #if defined (ASM_WEAKEN_LABEL) || defined (ASM_WEAKEN_DECL)
5507 if (DECL_WEAK (decl))
5509 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
5510 tree *p, t;
5512 #ifdef ASM_WEAKEN_DECL
5513 ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
5514 #else
5515 ASM_WEAKEN_LABEL (asm_out_file, name);
5516 #endif
5518 /* Remove this function from the pending weak list so that
5519 we do not emit multiple .weak directives for it. */
5520 for (p = &weak_decls; (t = *p) ; )
5522 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
5523 *p = TREE_CHAIN (t);
5524 else
5525 p = &TREE_CHAIN (t);
5528 /* Remove weakrefs to the same target from the pending weakref
5529 list, for the same reason. */
5530 for (p = &weakref_targets; (t = *p) ; )
5532 if (DECL_ASSEMBLER_NAME (decl)
5533 == ultimate_transparent_alias_target (&TREE_VALUE (t)))
5534 *p = TREE_CHAIN (t);
5535 else
5536 p = &TREE_CHAIN (t);
5539 return;
5541 #endif
5543 targetm.asm_out.globalize_decl_name (asm_out_file, decl);
5546 vec<alias_pair, va_gc> *alias_pairs;
5548 /* Output the assembler code for a define (equate) using ASM_OUTPUT_DEF
5549 or ASM_OUTPUT_DEF_FROM_DECLS. The function defines the symbol whose
5550 tree node is DECL to have the value of the tree node TARGET. */
5552 void
5553 do_assemble_alias (tree decl, tree target)
5555 tree id;
5557 /* Emulated TLS had better not get this var. */
5558 gcc_assert (!(!targetm.have_tls
5559 && TREE_CODE (decl) == VAR_DECL
5560 && DECL_THREAD_LOCAL_P (decl)));
5562 if (TREE_ASM_WRITTEN (decl))
5563 return;
5565 id = DECL_ASSEMBLER_NAME (decl);
5566 ultimate_transparent_alias_target (&id);
5568 /* We must force creation of DECL_RTL for debug info generation, even though
5569 we don't use it here. */
5570 make_decl_rtl (decl);
5572 TREE_ASM_WRITTEN (decl) = 1;
5573 TREE_ASM_WRITTEN (DECL_ASSEMBLER_NAME (decl)) = 1;
5574 TREE_ASM_WRITTEN (id) = 1;
5576 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
5578 ultimate_transparent_alias_target (&target);
5580 if (!TREE_SYMBOL_REFERENCED (target))
5581 weakref_targets = tree_cons (decl, target, weakref_targets);
5583 #ifdef ASM_OUTPUT_WEAKREF
5584 ASM_OUTPUT_WEAKREF (asm_out_file, decl,
5585 IDENTIFIER_POINTER (id),
5586 IDENTIFIER_POINTER (target));
5587 #else
5588 if (!TARGET_SUPPORTS_WEAK)
5590 error_at (DECL_SOURCE_LOCATION (decl),
5591 "weakref is not supported in this configuration");
5592 return;
5594 #endif
5595 return;
5598 #ifdef ASM_OUTPUT_DEF
5599 tree orig_decl = decl;
5601 if (TREE_CODE (decl) == FUNCTION_DECL
5602 && cgraph_node::get (decl)->instrumentation_clone
5603 && cgraph_node::get (decl)->instrumented_version)
5604 orig_decl = cgraph_node::get (decl)->instrumented_version->decl;
5606 /* Make name accessible from other files, if appropriate. */
5608 if (TREE_PUBLIC (decl) || TREE_PUBLIC (orig_decl))
5610 globalize_decl (decl);
5611 maybe_assemble_visibility (decl);
5613 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
5615 #if defined (ASM_OUTPUT_TYPE_DIRECTIVE)
5616 if (targetm.has_ifunc_p ())
5617 ASM_OUTPUT_TYPE_DIRECTIVE
5618 (asm_out_file, IDENTIFIER_POINTER (id),
5619 IFUNC_ASM_TYPE);
5620 else
5621 #endif
5622 error_at (DECL_SOURCE_LOCATION (decl),
5623 "ifunc is not supported on this target");
5626 # ifdef ASM_OUTPUT_DEF_FROM_DECLS
5627 ASM_OUTPUT_DEF_FROM_DECLS (asm_out_file, decl, target);
5628 # else
5629 ASM_OUTPUT_DEF (asm_out_file,
5630 IDENTIFIER_POINTER (id),
5631 IDENTIFIER_POINTER (target));
5632 # endif
5633 #elif defined (ASM_OUTPUT_WEAK_ALIAS) || defined (ASM_WEAKEN_DECL)
5635 const char *name;
5636 tree *p, t;
5638 name = IDENTIFIER_POINTER (id);
5639 # ifdef ASM_WEAKEN_DECL
5640 ASM_WEAKEN_DECL (asm_out_file, decl, name, IDENTIFIER_POINTER (target));
5641 # else
5642 ASM_OUTPUT_WEAK_ALIAS (asm_out_file, name, IDENTIFIER_POINTER (target));
5643 # endif
5644 /* Remove this function from the pending weak list so that
5645 we do not emit multiple .weak directives for it. */
5646 for (p = &weak_decls; (t = *p) ; )
5647 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t))
5648 || id == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
5649 *p = TREE_CHAIN (t);
5650 else
5651 p = &TREE_CHAIN (t);
5653 /* Remove weakrefs to the same target from the pending weakref
5654 list, for the same reason. */
5655 for (p = &weakref_targets; (t = *p) ; )
5657 if (id == ultimate_transparent_alias_target (&TREE_VALUE (t)))
5658 *p = TREE_CHAIN (t);
5659 else
5660 p = &TREE_CHAIN (t);
5663 #endif
5666 /* Emit an assembler directive to make the symbol for DECL an alias to
5667 the symbol for TARGET. */
5669 void
5670 assemble_alias (tree decl, tree target)
5672 tree target_decl;
5674 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
5676 tree alias = DECL_ASSEMBLER_NAME (decl);
5678 ultimate_transparent_alias_target (&target);
5680 if (alias == target)
5681 error ("weakref %q+D ultimately targets itself", decl);
5682 if (TREE_PUBLIC (decl))
5683 error ("weakref %q+D must have static linkage", decl);
5685 else
5687 #if !defined (ASM_OUTPUT_DEF)
5688 # if !defined(ASM_OUTPUT_WEAK_ALIAS) && !defined (ASM_WEAKEN_DECL)
5689 error_at (DECL_SOURCE_LOCATION (decl),
5690 "alias definitions not supported in this configuration");
5691 TREE_ASM_WRITTEN (decl) = 1;
5692 return;
5693 # else
5694 if (!DECL_WEAK (decl))
5696 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
5697 error_at (DECL_SOURCE_LOCATION (decl),
5698 "ifunc is not supported in this configuration");
5699 else
5700 error_at (DECL_SOURCE_LOCATION (decl),
5701 "only weak aliases are supported in this configuration");
5702 TREE_ASM_WRITTEN (decl) = 1;
5703 return;
5705 # endif
5706 #endif
5708 TREE_USED (decl) = 1;
5710 /* Allow aliases to aliases. */
5711 if (TREE_CODE (decl) == FUNCTION_DECL)
5712 cgraph_node::get_create (decl)->alias = true;
5713 else
5714 varpool_node::get_create (decl)->alias = true;
5716 /* If the target has already been emitted, we don't have to queue the
5717 alias. This saves a tad of memory. */
5718 if (symtab->global_info_ready)
5719 target_decl = find_decl (target);
5720 else
5721 target_decl= NULL;
5722 if ((target_decl && TREE_ASM_WRITTEN (target_decl))
5723 || symtab->state >= EXPANSION)
5724 do_assemble_alias (decl, target);
5725 else
5727 alias_pair p = {decl, target};
5728 vec_safe_push (alias_pairs, p);
5732 /* Record and output a table of translations from original function
5733 to its transaction aware clone. Note that tm_pure functions are
5734 considered to be their own clone. */
5736 struct tm_clone_hasher : ggc_cache_hasher<tree_map *>
5738 static hashval_t hash (tree_map *m) { return tree_map_hash (m); }
5739 static bool equal (tree_map *a, tree_map *b) { return tree_map_eq (a, b); }
5741 static void handle_cache_entry (tree_map *&e)
5743 if (e != HTAB_EMPTY_ENTRY || e != HTAB_DELETED_ENTRY)
5745 extern void gt_ggc_mx (tree_map *&);
5746 if (ggc_marked_p (e->base.from))
5747 gt_ggc_mx (e);
5748 else
5749 e = static_cast<tree_map *> (HTAB_DELETED_ENTRY);
5754 static GTY((cache))
5755 hash_table<tm_clone_hasher> *tm_clone_hash;
5757 void
5758 record_tm_clone_pair (tree o, tree n)
5760 struct tree_map **slot, *h;
5762 if (tm_clone_hash == NULL)
5763 tm_clone_hash = hash_table<tm_clone_hasher>::create_ggc (32);
5765 h = ggc_alloc<tree_map> ();
5766 h->hash = htab_hash_pointer (o);
5767 h->base.from = o;
5768 h->to = n;
5770 slot = tm_clone_hash->find_slot_with_hash (h, h->hash, INSERT);
5771 *slot = h;
5774 tree
5775 get_tm_clone_pair (tree o)
5777 if (tm_clone_hash)
5779 struct tree_map *h, in;
5781 in.base.from = o;
5782 in.hash = htab_hash_pointer (o);
5783 h = tm_clone_hash->find_with_hash (&in, in.hash);
5784 if (h)
5785 return h->to;
5787 return NULL_TREE;
5790 typedef struct tm_alias_pair
5792 unsigned int uid;
5793 tree from;
5794 tree to;
5795 } tm_alias_pair;
5798 /* Dump the actual pairs to the .tm_clone_table section. */
5800 static void
5801 dump_tm_clone_pairs (vec<tm_alias_pair> tm_alias_pairs)
5803 unsigned i;
5804 tm_alias_pair *p;
5805 bool switched = false;
5807 FOR_EACH_VEC_ELT (tm_alias_pairs, i, p)
5809 tree src = p->from;
5810 tree dst = p->to;
5811 struct cgraph_node *src_n = cgraph_node::get (src);
5812 struct cgraph_node *dst_n = cgraph_node::get (dst);
5814 /* The function ipa_tm_create_version() marks the clone as needed if
5815 the original function was needed. But we also mark the clone as
5816 needed if we ever called the clone indirectly through
5817 TM_GETTMCLONE. If neither of these are true, we didn't generate
5818 a clone, and we didn't call it indirectly... no sense keeping it
5819 in the clone table. */
5820 if (!dst_n || !dst_n->definition)
5821 continue;
5823 /* This covers the case where we have optimized the original
5824 function away, and only access the transactional clone. */
5825 if (!src_n || !src_n->definition)
5826 continue;
5828 if (!switched)
5830 switch_to_section (targetm.asm_out.tm_clone_table_section ());
5831 assemble_align (POINTER_SIZE);
5832 switched = true;
5835 assemble_integer (XEXP (DECL_RTL (src), 0),
5836 POINTER_SIZE_UNITS, POINTER_SIZE, 1);
5837 assemble_integer (XEXP (DECL_RTL (dst), 0),
5838 POINTER_SIZE_UNITS, POINTER_SIZE, 1);
5842 /* Provide a default for the tm_clone_table section. */
5844 section *
5845 default_clone_table_section (void)
5847 return get_named_section (NULL, ".tm_clone_table", 3);
5850 /* Helper comparison function for qsorting by the DECL_UID stored in
5851 alias_pair->emitted_diags. */
5853 static int
5854 tm_alias_pair_cmp (const void *x, const void *y)
5856 const tm_alias_pair *p1 = (const tm_alias_pair *) x;
5857 const tm_alias_pair *p2 = (const tm_alias_pair *) y;
5858 if (p1->uid < p2->uid)
5859 return -1;
5860 if (p1->uid > p2->uid)
5861 return 1;
5862 return 0;
5865 void
5866 finish_tm_clone_pairs (void)
5868 vec<tm_alias_pair> tm_alias_pairs = vNULL;
5870 if (tm_clone_hash == NULL)
5871 return;
5873 /* We need a determenistic order for the .tm_clone_table, otherwise
5874 we will get bootstrap comparison failures, so dump the hash table
5875 to a vector, sort it, and dump the vector. */
5877 /* Dump the hashtable to a vector. */
5878 tree_map *map;
5879 hash_table<tm_clone_hasher>::iterator iter;
5880 FOR_EACH_HASH_TABLE_ELEMENT (*tm_clone_hash, map, tree_map *, iter)
5882 tm_alias_pair p = {DECL_UID (map->base.from), map->base.from, map->to};
5883 tm_alias_pairs.safe_push (p);
5885 /* Sort it. */
5886 tm_alias_pairs.qsort (tm_alias_pair_cmp);
5888 /* Dump it. */
5889 dump_tm_clone_pairs (tm_alias_pairs);
5891 tm_clone_hash->empty ();
5892 tm_clone_hash = NULL;
5893 tm_alias_pairs.release ();
5897 /* Emit an assembler directive to set symbol for DECL visibility to
5898 the visibility type VIS, which must not be VISIBILITY_DEFAULT. */
5900 void
5901 default_assemble_visibility (tree decl ATTRIBUTE_UNUSED,
5902 int vis ATTRIBUTE_UNUSED)
5904 #ifdef HAVE_GAS_HIDDEN
5905 static const char * const visibility_types[] = {
5906 NULL, "protected", "hidden", "internal"
5909 const char *name, *type;
5911 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5912 type = visibility_types[vis];
5914 fprintf (asm_out_file, "\t.%s\t", type);
5915 assemble_name (asm_out_file, name);
5916 fprintf (asm_out_file, "\n");
5917 #else
5918 if (!DECL_ARTIFICIAL (decl))
5919 warning (OPT_Wattributes, "visibility attribute not supported "
5920 "in this configuration; ignored");
5921 #endif
5924 /* A helper function to call assemble_visibility when needed for a decl. */
5927 maybe_assemble_visibility (tree decl)
5929 enum symbol_visibility vis = DECL_VISIBILITY (decl);
5931 if (TREE_CODE (decl) == FUNCTION_DECL
5932 && cgraph_node::get (decl)
5933 && cgraph_node::get (decl)->instrumentation_clone
5934 && cgraph_node::get (decl)->instrumented_version)
5935 vis = DECL_VISIBILITY (cgraph_node::get (decl)->instrumented_version->decl);
5937 if (vis != VISIBILITY_DEFAULT)
5939 targetm.asm_out.assemble_visibility (decl, vis);
5940 return 1;
5942 else
5943 return 0;
5946 /* Returns 1 if the target configuration supports defining public symbols
5947 so that one of them will be chosen at link time instead of generating a
5948 multiply-defined symbol error, whether through the use of weak symbols or
5949 a target-specific mechanism for having duplicates discarded. */
5952 supports_one_only (void)
5954 if (SUPPORTS_ONE_ONLY)
5955 return 1;
5956 return TARGET_SUPPORTS_WEAK;
5959 /* Set up DECL as a public symbol that can be defined in multiple
5960 translation units without generating a linker error. */
5962 void
5963 make_decl_one_only (tree decl, tree comdat_group)
5965 struct symtab_node *symbol;
5966 gcc_assert (TREE_CODE (decl) == VAR_DECL
5967 || TREE_CODE (decl) == FUNCTION_DECL);
5969 TREE_PUBLIC (decl) = 1;
5971 if (TREE_CODE (decl) == VAR_DECL)
5972 symbol = varpool_node::get_create (decl);
5973 else
5974 symbol = cgraph_node::get_create (decl);
5976 if (SUPPORTS_ONE_ONLY)
5978 #ifdef MAKE_DECL_ONE_ONLY
5979 MAKE_DECL_ONE_ONLY (decl);
5980 #endif
5981 symbol->set_comdat_group (comdat_group);
5983 else if (TREE_CODE (decl) == VAR_DECL
5984 && (DECL_INITIAL (decl) == 0
5985 || (!in_lto_p && DECL_INITIAL (decl) == error_mark_node)))
5986 DECL_COMMON (decl) = 1;
5987 else
5989 gcc_assert (TARGET_SUPPORTS_WEAK);
5990 DECL_WEAK (decl) = 1;
5994 void
5995 init_varasm_once (void)
5997 section_htab = hash_table<section_hasher>::create_ggc (31);
5998 object_block_htab = hash_table<object_block_hasher>::create_ggc (31);
5999 const_desc_htab = hash_table<tree_descriptor_hasher>::create_ggc (1009);
6001 shared_constant_pool = create_constant_pool ();
6003 #ifdef TEXT_SECTION_ASM_OP
6004 text_section = get_unnamed_section (SECTION_CODE, output_section_asm_op,
6005 TEXT_SECTION_ASM_OP);
6006 #endif
6008 #ifdef DATA_SECTION_ASM_OP
6009 data_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
6010 DATA_SECTION_ASM_OP);
6011 #endif
6013 #ifdef SDATA_SECTION_ASM_OP
6014 sdata_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
6015 SDATA_SECTION_ASM_OP);
6016 #endif
6018 #ifdef READONLY_DATA_SECTION_ASM_OP
6019 readonly_data_section = get_unnamed_section (0, output_section_asm_op,
6020 READONLY_DATA_SECTION_ASM_OP);
6021 #endif
6023 #ifdef CTORS_SECTION_ASM_OP
6024 ctors_section = get_unnamed_section (0, output_section_asm_op,
6025 CTORS_SECTION_ASM_OP);
6026 #endif
6028 #ifdef DTORS_SECTION_ASM_OP
6029 dtors_section = get_unnamed_section (0, output_section_asm_op,
6030 DTORS_SECTION_ASM_OP);
6031 #endif
6033 #ifdef BSS_SECTION_ASM_OP
6034 bss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
6035 output_section_asm_op,
6036 BSS_SECTION_ASM_OP);
6037 #endif
6039 #ifdef SBSS_SECTION_ASM_OP
6040 sbss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
6041 output_section_asm_op,
6042 SBSS_SECTION_ASM_OP);
6043 #endif
6045 tls_comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6046 | SECTION_COMMON, emit_tls_common);
6047 lcomm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6048 | SECTION_COMMON, emit_local);
6049 comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6050 | SECTION_COMMON, emit_common);
6052 #if defined ASM_OUTPUT_ALIGNED_BSS
6053 bss_noswitch_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS,
6054 emit_bss);
6055 #endif
6057 targetm.asm_out.init_sections ();
6059 if (readonly_data_section == NULL)
6060 readonly_data_section = text_section;
6062 #ifdef ASM_OUTPUT_EXTERNAL
6063 pending_assemble_externals_set = new hash_set<tree>;
6064 #endif
6067 enum tls_model
6068 decl_default_tls_model (const_tree decl)
6070 enum tls_model kind;
6071 bool is_local;
6073 is_local = targetm.binds_local_p (decl);
6074 if (!flag_shlib)
6076 if (is_local)
6077 kind = TLS_MODEL_LOCAL_EXEC;
6078 else
6079 kind = TLS_MODEL_INITIAL_EXEC;
6082 /* Local dynamic is inefficient when we're not combining the
6083 parts of the address. */
6084 else if (optimize && is_local)
6085 kind = TLS_MODEL_LOCAL_DYNAMIC;
6086 else
6087 kind = TLS_MODEL_GLOBAL_DYNAMIC;
6088 if (kind < flag_tls_default)
6089 kind = flag_tls_default;
6091 return kind;
6094 /* Select a set of attributes for section NAME based on the properties
6095 of DECL and whether or not RELOC indicates that DECL's initializer
6096 might contain runtime relocations.
6098 We make the section read-only and executable for a function decl,
6099 read-only for a const data decl, and writable for a non-const data decl. */
6101 unsigned int
6102 default_section_type_flags (tree decl, const char *name, int reloc)
6104 unsigned int flags;
6106 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
6107 flags = SECTION_CODE;
6108 else if (decl)
6110 enum section_category category
6111 = categorize_decl_for_section (decl, reloc);
6112 if (decl_readonly_section_1 (category))
6113 flags = 0;
6114 else if (category == SECCAT_DATA_REL_RO
6115 || category == SECCAT_DATA_REL_RO_LOCAL)
6116 flags = SECTION_WRITE | SECTION_RELRO;
6117 else
6118 flags = SECTION_WRITE;
6120 else
6122 flags = SECTION_WRITE;
6123 if (strcmp (name, ".data.rel.ro") == 0
6124 || strcmp (name, ".data.rel.ro.local") == 0)
6125 flags |= SECTION_RELRO;
6128 if (decl && DECL_P (decl) && DECL_COMDAT_GROUP (decl))
6129 flags |= SECTION_LINKONCE;
6131 if (strcmp (name, ".vtable_map_vars") == 0)
6132 flags |= SECTION_LINKONCE;
6134 if (decl && TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
6135 flags |= SECTION_TLS | SECTION_WRITE;
6137 if (strcmp (name, ".bss") == 0
6138 || strncmp (name, ".bss.", 5) == 0
6139 || strncmp (name, ".gnu.linkonce.b.", 16) == 0
6140 || strcmp (name, ".persistent.bss") == 0
6141 || strcmp (name, ".sbss") == 0
6142 || strncmp (name, ".sbss.", 6) == 0
6143 || strncmp (name, ".gnu.linkonce.sb.", 17) == 0)
6144 flags |= SECTION_BSS;
6146 if (strcmp (name, ".tdata") == 0
6147 || strncmp (name, ".tdata.", 7) == 0
6148 || strncmp (name, ".gnu.linkonce.td.", 17) == 0)
6149 flags |= SECTION_TLS;
6151 if (strcmp (name, ".tbss") == 0
6152 || strncmp (name, ".tbss.", 6) == 0
6153 || strncmp (name, ".gnu.linkonce.tb.", 17) == 0)
6154 flags |= SECTION_TLS | SECTION_BSS;
6156 /* These three sections have special ELF types. They are neither
6157 SHT_PROGBITS nor SHT_NOBITS, so when changing sections we don't
6158 want to print a section type (@progbits or @nobits). If someone
6159 is silly enough to emit code or TLS variables to one of these
6160 sections, then don't handle them specially. */
6161 if (!(flags & (SECTION_CODE | SECTION_BSS | SECTION_TLS))
6162 && (strcmp (name, ".init_array") == 0
6163 || strcmp (name, ".fini_array") == 0
6164 || strcmp (name, ".preinit_array") == 0))
6165 flags |= SECTION_NOTYPE;
6167 return flags;
6170 /* Return true if the target supports some form of global BSS,
6171 either through bss_noswitch_section, or by selecting a BSS
6172 section in TARGET_ASM_SELECT_SECTION. */
6174 bool
6175 have_global_bss_p (void)
6177 return bss_noswitch_section || targetm.have_switchable_bss_sections;
6180 /* Output assembly to switch to section NAME with attribute FLAGS.
6181 Four variants for common object file formats. */
6183 void
6184 default_no_named_section (const char *name ATTRIBUTE_UNUSED,
6185 unsigned int flags ATTRIBUTE_UNUSED,
6186 tree decl ATTRIBUTE_UNUSED)
6188 /* Some object formats don't support named sections at all. The
6189 front-end should already have flagged this as an error. */
6190 gcc_unreachable ();
6193 #ifndef TLS_SECTION_ASM_FLAG
6194 #define TLS_SECTION_ASM_FLAG 'T'
6195 #endif
6197 void
6198 default_elf_asm_named_section (const char *name, unsigned int flags,
6199 tree decl ATTRIBUTE_UNUSED)
6201 char flagchars[10], *f = flagchars;
6203 /* If we have already declared this section, we can use an
6204 abbreviated form to switch back to it -- unless this section is
6205 part of a COMDAT groups, in which case GAS requires the full
6206 declaration every time. */
6207 if (!(HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6208 && (flags & SECTION_DECLARED))
6210 fprintf (asm_out_file, "\t.section\t%s\n", name);
6211 return;
6214 if (!(flags & SECTION_DEBUG))
6215 *f++ = 'a';
6216 #if defined (HAVE_GAS_SECTION_EXCLUDE) && HAVE_GAS_SECTION_EXCLUDE == 1
6217 if (flags & SECTION_EXCLUDE)
6218 *f++ = 'e';
6219 #endif
6220 if (flags & SECTION_WRITE)
6221 *f++ = 'w';
6222 if (flags & SECTION_CODE)
6223 *f++ = 'x';
6224 if (flags & SECTION_SMALL)
6225 *f++ = 's';
6226 if (flags & SECTION_MERGE)
6227 *f++ = 'M';
6228 if (flags & SECTION_STRINGS)
6229 *f++ = 'S';
6230 if (flags & SECTION_TLS)
6231 *f++ = TLS_SECTION_ASM_FLAG;
6232 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6233 *f++ = 'G';
6234 *f = '\0';
6236 fprintf (asm_out_file, "\t.section\t%s,\"%s\"", name, flagchars);
6238 if (!(flags & SECTION_NOTYPE))
6240 const char *type;
6241 const char *format;
6243 if (flags & SECTION_BSS)
6244 type = "nobits";
6245 else
6246 type = "progbits";
6248 format = ",@%s";
6249 /* On platforms that use "@" as the assembly comment character,
6250 use "%" instead. */
6251 if (strcmp (ASM_COMMENT_START, "@") == 0)
6252 format = ",%%%s";
6253 fprintf (asm_out_file, format, type);
6255 if (flags & SECTION_ENTSIZE)
6256 fprintf (asm_out_file, ",%d", flags & SECTION_ENTSIZE);
6257 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6259 if (TREE_CODE (decl) == IDENTIFIER_NODE)
6260 fprintf (asm_out_file, ",%s,comdat", IDENTIFIER_POINTER (decl));
6261 else
6262 fprintf (asm_out_file, ",%s,comdat",
6263 IDENTIFIER_POINTER (DECL_COMDAT_GROUP (decl)));
6267 putc ('\n', asm_out_file);
6270 void
6271 default_coff_asm_named_section (const char *name, unsigned int flags,
6272 tree decl ATTRIBUTE_UNUSED)
6274 char flagchars[8], *f = flagchars;
6276 if (flags & SECTION_WRITE)
6277 *f++ = 'w';
6278 if (flags & SECTION_CODE)
6279 *f++ = 'x';
6280 *f = '\0';
6282 fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
6285 void
6286 default_pe_asm_named_section (const char *name, unsigned int flags,
6287 tree decl)
6289 default_coff_asm_named_section (name, flags, decl);
6291 if (flags & SECTION_LINKONCE)
6293 /* Functions may have been compiled at various levels of
6294 optimization so we can't use `same_size' here.
6295 Instead, have the linker pick one. */
6296 fprintf (asm_out_file, "\t.linkonce %s\n",
6297 (flags & SECTION_CODE ? "discard" : "same_size"));
6301 /* The lame default section selector. */
6303 section *
6304 default_select_section (tree decl, int reloc,
6305 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
6307 if (DECL_P (decl))
6309 if (decl_readonly_section (decl, reloc))
6310 return readonly_data_section;
6312 else if (TREE_CODE (decl) == CONSTRUCTOR)
6314 if (! ((flag_pic && reloc)
6315 || !TREE_READONLY (decl)
6316 || TREE_SIDE_EFFECTS (decl)
6317 || !TREE_CONSTANT (decl)))
6318 return readonly_data_section;
6320 else if (TREE_CODE (decl) == STRING_CST)
6321 return readonly_data_section;
6322 else if (! (flag_pic && reloc))
6323 return readonly_data_section;
6325 return data_section;
6328 enum section_category
6329 categorize_decl_for_section (const_tree decl, int reloc)
6331 enum section_category ret;
6333 if (TREE_CODE (decl) == FUNCTION_DECL)
6334 return SECCAT_TEXT;
6335 else if (TREE_CODE (decl) == STRING_CST)
6337 if ((flag_sanitize & SANITIZE_ADDRESS)
6338 && asan_protect_global (CONST_CAST_TREE (decl)))
6339 /* or !flag_merge_constants */
6340 return SECCAT_RODATA;
6341 else
6342 return SECCAT_RODATA_MERGE_STR;
6344 else if (TREE_CODE (decl) == VAR_DECL)
6346 if (bss_initializer_p (decl))
6347 ret = SECCAT_BSS;
6348 else if (! TREE_READONLY (decl)
6349 || TREE_SIDE_EFFECTS (decl)
6350 || ! TREE_CONSTANT (DECL_INITIAL (decl)))
6352 /* Here the reloc_rw_mask is not testing whether the section should
6353 be read-only or not, but whether the dynamic link will have to
6354 do something. If so, we wish to segregate the data in order to
6355 minimize cache misses inside the dynamic linker. */
6356 if (reloc & targetm.asm_out.reloc_rw_mask ())
6357 ret = reloc == 1 ? SECCAT_DATA_REL_LOCAL : SECCAT_DATA_REL;
6358 else
6359 ret = SECCAT_DATA;
6361 else if (reloc & targetm.asm_out.reloc_rw_mask ())
6362 ret = reloc == 1 ? SECCAT_DATA_REL_RO_LOCAL : SECCAT_DATA_REL_RO;
6363 else if (reloc || flag_merge_constants < 2
6364 || ((flag_sanitize & SANITIZE_ADDRESS)
6365 && asan_protect_global (CONST_CAST_TREE (decl))))
6366 /* C and C++ don't allow different variables to share the same
6367 location. -fmerge-all-constants allows even that (at the
6368 expense of not conforming). */
6369 ret = SECCAT_RODATA;
6370 else if (TREE_CODE (DECL_INITIAL (decl)) == STRING_CST)
6371 ret = SECCAT_RODATA_MERGE_STR_INIT;
6372 else
6373 ret = SECCAT_RODATA_MERGE_CONST;
6375 else if (TREE_CODE (decl) == CONSTRUCTOR)
6377 if ((reloc & targetm.asm_out.reloc_rw_mask ())
6378 || TREE_SIDE_EFFECTS (decl)
6379 || ! TREE_CONSTANT (decl))
6380 ret = SECCAT_DATA;
6381 else
6382 ret = SECCAT_RODATA;
6384 else
6385 ret = SECCAT_RODATA;
6387 /* There are no read-only thread-local sections. */
6388 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
6390 /* Note that this would be *just* SECCAT_BSS, except that there's
6391 no concept of a read-only thread-local-data section. */
6392 if (ret == SECCAT_BSS
6393 || (flag_zero_initialized_in_bss
6394 && initializer_zerop (DECL_INITIAL (decl))))
6395 ret = SECCAT_TBSS;
6396 else
6397 ret = SECCAT_TDATA;
6400 /* If the target uses small data sections, select it. */
6401 else if (targetm.in_small_data_p (decl))
6403 if (ret == SECCAT_BSS)
6404 ret = SECCAT_SBSS;
6405 else if (targetm.have_srodata_section && ret == SECCAT_RODATA)
6406 ret = SECCAT_SRODATA;
6407 else
6408 ret = SECCAT_SDATA;
6411 return ret;
6414 static bool
6415 decl_readonly_section_1 (enum section_category category)
6417 switch (category)
6419 case SECCAT_RODATA:
6420 case SECCAT_RODATA_MERGE_STR:
6421 case SECCAT_RODATA_MERGE_STR_INIT:
6422 case SECCAT_RODATA_MERGE_CONST:
6423 case SECCAT_SRODATA:
6424 return true;
6425 default:
6426 return false;
6430 bool
6431 decl_readonly_section (const_tree decl, int reloc)
6433 return decl_readonly_section_1 (categorize_decl_for_section (decl, reloc));
6436 /* Select a section based on the above categorization. */
6438 section *
6439 default_elf_select_section (tree decl, int reloc,
6440 unsigned HOST_WIDE_INT align)
6442 const char *sname;
6443 switch (categorize_decl_for_section (decl, reloc))
6445 case SECCAT_TEXT:
6446 /* We're not supposed to be called on FUNCTION_DECLs. */
6447 gcc_unreachable ();
6448 case SECCAT_RODATA:
6449 return readonly_data_section;
6450 case SECCAT_RODATA_MERGE_STR:
6451 return mergeable_string_section (decl, align, 0);
6452 case SECCAT_RODATA_MERGE_STR_INIT:
6453 return mergeable_string_section (DECL_INITIAL (decl), align, 0);
6454 case SECCAT_RODATA_MERGE_CONST:
6455 return mergeable_constant_section (DECL_MODE (decl), align, 0);
6456 case SECCAT_SRODATA:
6457 sname = ".sdata2";
6458 break;
6459 case SECCAT_DATA:
6460 return data_section;
6461 case SECCAT_DATA_REL:
6462 sname = ".data.rel";
6463 break;
6464 case SECCAT_DATA_REL_LOCAL:
6465 sname = ".data.rel.local";
6466 break;
6467 case SECCAT_DATA_REL_RO:
6468 sname = ".data.rel.ro";
6469 break;
6470 case SECCAT_DATA_REL_RO_LOCAL:
6471 sname = ".data.rel.ro.local";
6472 break;
6473 case SECCAT_SDATA:
6474 sname = ".sdata";
6475 break;
6476 case SECCAT_TDATA:
6477 sname = ".tdata";
6478 break;
6479 case SECCAT_BSS:
6480 if (bss_section)
6481 return bss_section;
6482 sname = ".bss";
6483 break;
6484 case SECCAT_SBSS:
6485 sname = ".sbss";
6486 break;
6487 case SECCAT_TBSS:
6488 sname = ".tbss";
6489 break;
6490 default:
6491 gcc_unreachable ();
6494 return get_named_section (decl, sname, reloc);
6497 /* Construct a unique section name based on the decl name and the
6498 categorization performed above. */
6500 void
6501 default_unique_section (tree decl, int reloc)
6503 /* We only need to use .gnu.linkonce if we don't have COMDAT groups. */
6504 bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
6505 const char *prefix, *name, *linkonce;
6506 char *string;
6507 tree id;
6509 switch (categorize_decl_for_section (decl, reloc))
6511 case SECCAT_TEXT:
6512 prefix = one_only ? ".t" : ".text";
6513 break;
6514 case SECCAT_RODATA:
6515 case SECCAT_RODATA_MERGE_STR:
6516 case SECCAT_RODATA_MERGE_STR_INIT:
6517 case SECCAT_RODATA_MERGE_CONST:
6518 prefix = one_only ? ".r" : ".rodata";
6519 break;
6520 case SECCAT_SRODATA:
6521 prefix = one_only ? ".s2" : ".sdata2";
6522 break;
6523 case SECCAT_DATA:
6524 prefix = one_only ? ".d" : ".data";
6525 break;
6526 case SECCAT_DATA_REL:
6527 prefix = one_only ? ".d.rel" : ".data.rel";
6528 break;
6529 case SECCAT_DATA_REL_LOCAL:
6530 prefix = one_only ? ".d.rel.local" : ".data.rel.local";
6531 break;
6532 case SECCAT_DATA_REL_RO:
6533 prefix = one_only ? ".d.rel.ro" : ".data.rel.ro";
6534 break;
6535 case SECCAT_DATA_REL_RO_LOCAL:
6536 prefix = one_only ? ".d.rel.ro.local" : ".data.rel.ro.local";
6537 break;
6538 case SECCAT_SDATA:
6539 prefix = one_only ? ".s" : ".sdata";
6540 break;
6541 case SECCAT_BSS:
6542 prefix = one_only ? ".b" : ".bss";
6543 break;
6544 case SECCAT_SBSS:
6545 prefix = one_only ? ".sb" : ".sbss";
6546 break;
6547 case SECCAT_TDATA:
6548 prefix = one_only ? ".td" : ".tdata";
6549 break;
6550 case SECCAT_TBSS:
6551 prefix = one_only ? ".tb" : ".tbss";
6552 break;
6553 default:
6554 gcc_unreachable ();
6557 id = DECL_ASSEMBLER_NAME (decl);
6558 ultimate_transparent_alias_target (&id);
6559 name = IDENTIFIER_POINTER (id);
6560 name = targetm.strip_name_encoding (name);
6562 /* If we're using one_only, then there needs to be a .gnu.linkonce
6563 prefix to the section name. */
6564 linkonce = one_only ? ".gnu.linkonce" : "";
6566 string = ACONCAT ((linkonce, prefix, ".", name, NULL));
6568 set_decl_section_name (decl, string);
6571 /* Subroutine of compute_reloc_for_rtx for leaf rtxes. */
6573 static int
6574 compute_reloc_for_rtx_1 (const_rtx x)
6576 switch (GET_CODE (x))
6578 case SYMBOL_REF:
6579 return SYMBOL_REF_LOCAL_P (x) ? 1 : 2;
6580 case LABEL_REF:
6581 return 1;
6582 default:
6583 return 0;
6587 /* Like compute_reloc_for_constant, except for an RTX. The return value
6588 is a mask for which bit 1 indicates a global relocation, and bit 0
6589 indicates a local relocation. */
6591 static int
6592 compute_reloc_for_rtx (const_rtx x)
6594 switch (GET_CODE (x))
6596 case SYMBOL_REF:
6597 case LABEL_REF:
6598 return compute_reloc_for_rtx_1 (x);
6600 case CONST:
6602 int reloc = 0;
6603 subrtx_iterator::array_type array;
6604 FOR_EACH_SUBRTX (iter, array, x, ALL)
6605 reloc |= compute_reloc_for_rtx_1 (*iter);
6606 return reloc;
6609 default:
6610 return 0;
6614 section *
6615 default_select_rtx_section (machine_mode mode ATTRIBUTE_UNUSED,
6616 rtx x,
6617 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
6619 if (compute_reloc_for_rtx (x) & targetm.asm_out.reloc_rw_mask ())
6620 return data_section;
6621 else
6622 return readonly_data_section;
6625 section *
6626 default_elf_select_rtx_section (machine_mode mode, rtx x,
6627 unsigned HOST_WIDE_INT align)
6629 int reloc = compute_reloc_for_rtx (x);
6631 /* ??? Handle small data here somehow. */
6633 if (reloc & targetm.asm_out.reloc_rw_mask ())
6635 if (reloc == 1)
6636 return get_named_section (NULL, ".data.rel.ro.local", 1);
6637 else
6638 return get_named_section (NULL, ".data.rel.ro", 3);
6641 return mergeable_constant_section (mode, align, 0);
6644 /* Set the generally applicable flags on the SYMBOL_REF for EXP. */
6646 void
6647 default_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
6649 rtx symbol;
6650 int flags;
6652 /* Careful not to prod global register variables. */
6653 if (!MEM_P (rtl))
6654 return;
6655 symbol = XEXP (rtl, 0);
6656 if (GET_CODE (symbol) != SYMBOL_REF)
6657 return;
6659 flags = SYMBOL_REF_FLAGS (symbol) & SYMBOL_FLAG_HAS_BLOCK_INFO;
6660 if (TREE_CODE (decl) == FUNCTION_DECL)
6661 flags |= SYMBOL_FLAG_FUNCTION;
6662 if (targetm.binds_local_p (decl))
6663 flags |= SYMBOL_FLAG_LOCAL;
6664 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
6665 flags |= DECL_TLS_MODEL (decl) << SYMBOL_FLAG_TLS_SHIFT;
6666 else if (targetm.in_small_data_p (decl))
6667 flags |= SYMBOL_FLAG_SMALL;
6668 /* ??? Why is DECL_EXTERNAL ever set for non-PUBLIC names? Without
6669 being PUBLIC, the thing *must* be defined in this translation unit.
6670 Prevent this buglet from being propagated into rtl code as well. */
6671 if (DECL_P (decl) && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl))
6672 flags |= SYMBOL_FLAG_EXTERNAL;
6674 SYMBOL_REF_FLAGS (symbol) = flags;
6677 /* By default, we do nothing for encode_section_info, so we need not
6678 do anything but discard the '*' marker. */
6680 const char *
6681 default_strip_name_encoding (const char *str)
6683 return str + (*str == '*');
6686 #ifdef ASM_OUTPUT_DEF
6687 /* The default implementation of TARGET_ASM_OUTPUT_ANCHOR. Define the
6688 anchor relative to ".", the current section position. */
6690 void
6691 default_asm_output_anchor (rtx symbol)
6693 char buffer[100];
6695 sprintf (buffer, "*. + " HOST_WIDE_INT_PRINT_DEC,
6696 SYMBOL_REF_BLOCK_OFFSET (symbol));
6697 ASM_OUTPUT_DEF (asm_out_file, XSTR (symbol, 0), buffer);
6699 #endif
6701 /* The default implementation of TARGET_USE_ANCHORS_FOR_SYMBOL_P. */
6703 bool
6704 default_use_anchors_for_symbol_p (const_rtx symbol)
6706 section *sect;
6707 tree decl;
6709 /* Don't use anchors for mergeable sections. The linker might move
6710 the objects around. */
6711 sect = SYMBOL_REF_BLOCK (symbol)->sect;
6712 if (sect->common.flags & SECTION_MERGE)
6713 return false;
6715 /* Don't use anchors for small data sections. The small data register
6716 acts as an anchor for such sections. */
6717 if (sect->common.flags & SECTION_SMALL)
6718 return false;
6720 decl = SYMBOL_REF_DECL (symbol);
6721 if (decl && DECL_P (decl))
6723 /* Don't use section anchors for decls that might be defined or
6724 usurped by other modules. */
6725 if (TREE_PUBLIC (decl) && !decl_binds_to_current_def_p (decl))
6726 return false;
6728 /* Don't use section anchors for decls that will be placed in a
6729 small data section. */
6730 /* ??? Ideally, this check would be redundant with the SECTION_SMALL
6731 one above. The problem is that we only use SECTION_SMALL for
6732 sections that should be marked as small in the section directive. */
6733 if (targetm.in_small_data_p (decl))
6734 return false;
6736 return true;
6739 /* Return true when RESOLUTION indicate that symbol will be bound to the
6740 definition provided by current .o file. */
6742 static bool
6743 resolution_to_local_definition_p (enum ld_plugin_symbol_resolution resolution)
6745 return (resolution == LDPR_PREVAILING_DEF
6746 || resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
6747 || resolution == LDPR_PREVAILING_DEF_IRONLY);
6750 /* Return true when RESOLUTION indicate that symbol will be bound locally
6751 within current executable or DSO. */
6753 static bool
6754 resolution_local_p (enum ld_plugin_symbol_resolution resolution)
6756 return (resolution == LDPR_PREVAILING_DEF
6757 || resolution == LDPR_PREVAILING_DEF_IRONLY
6758 || resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
6759 || resolution == LDPR_PREEMPTED_REG
6760 || resolution == LDPR_PREEMPTED_IR
6761 || resolution == LDPR_RESOLVED_IR
6762 || resolution == LDPR_RESOLVED_EXEC);
6765 /* Assume ELF-ish defaults, since that's pretty much the most liberal
6766 wrt cross-module name binding. */
6768 bool
6769 default_binds_local_p (const_tree exp)
6771 return default_binds_local_p_1 (exp, flag_shlib);
6774 bool
6775 default_binds_local_p_1 (const_tree exp, int shlib)
6777 bool local_p;
6778 bool resolved_locally = false;
6779 bool resolved_to_local_def = false;
6781 /* With resolution file in hands, take look into resolutions.
6782 We can't just return true for resolved_locally symbols,
6783 because dynamic linking might overwrite symbols
6784 in shared libraries. */
6785 if (TREE_CODE (exp) == VAR_DECL && TREE_PUBLIC (exp)
6786 && (TREE_STATIC (exp) || DECL_EXTERNAL (exp)))
6788 varpool_node *vnode = varpool_node::get (exp);
6789 if (vnode && (resolution_local_p (vnode->resolution) || vnode->in_other_partition))
6790 resolved_locally = true;
6791 if (vnode
6792 && resolution_to_local_definition_p (vnode->resolution))
6793 resolved_to_local_def = true;
6795 else if (TREE_CODE (exp) == FUNCTION_DECL && TREE_PUBLIC (exp))
6797 struct cgraph_node *node = cgraph_node::get (exp);
6798 if (node
6799 && (resolution_local_p (node->resolution) || node->in_other_partition))
6800 resolved_locally = true;
6801 if (node
6802 && resolution_to_local_definition_p (node->resolution))
6803 resolved_to_local_def = true;
6806 /* A non-decl is an entry in the constant pool. */
6807 if (!DECL_P (exp))
6808 local_p = true;
6809 /* Weakrefs may not bind locally, even though the weakref itself is always
6810 static and therefore local. Similarly, the resolver for ifunc functions
6811 might resolve to a non-local function.
6812 FIXME: We can resolve the weakref case more curefuly by looking at the
6813 weakref alias. */
6814 else if (lookup_attribute ("weakref", DECL_ATTRIBUTES (exp))
6815 || (TREE_CODE (exp) == FUNCTION_DECL
6816 && lookup_attribute ("ifunc", DECL_ATTRIBUTES (exp))))
6817 local_p = false;
6818 /* Static variables are always local. */
6819 else if (! TREE_PUBLIC (exp))
6820 local_p = true;
6821 /* A variable is local if the user has said explicitly that it will
6822 be. */
6823 else if ((DECL_VISIBILITY_SPECIFIED (exp)
6824 || resolved_to_local_def)
6825 && DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
6826 local_p = true;
6827 /* Variables defined outside this object might not be local. */
6828 else if (DECL_EXTERNAL (exp) && !resolved_locally)
6829 local_p = false;
6830 /* If defined in this object and visibility is not default, must be
6831 local. */
6832 else if (DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
6833 local_p = true;
6834 /* Default visibility weak data can be overridden by a strong symbol
6835 in another module and so are not local. */
6836 else if (DECL_WEAK (exp)
6837 && !resolved_locally)
6838 local_p = false;
6839 /* If PIC, then assume that any global name can be overridden by
6840 symbols resolved from other modules. */
6841 else if (shlib)
6842 local_p = false;
6843 /* Uninitialized COMMON variable may be unified with symbols
6844 resolved from other modules. */
6845 else if (DECL_COMMON (exp)
6846 && !resolved_locally
6847 && (DECL_INITIAL (exp) == NULL
6848 || (!in_lto_p && DECL_INITIAL (exp) == error_mark_node)))
6849 local_p = false;
6850 /* Otherwise we're left with initialized (or non-common) global data
6851 which is of necessity defined locally. */
6852 else
6853 local_p = true;
6855 return local_p;
6858 /* Return true when references to DECL must bind to current definition in
6859 final executable.
6861 The condition is usually equivalent to whether the function binds to the
6862 current module (shared library or executable), that is to binds_local_p.
6863 We use this fact to avoid need for another target hook and implement
6864 the logic using binds_local_p and just special cases where
6865 decl_binds_to_current_def_p is stronger than binds_local_p. In particular
6866 the weak definitions (that can be overwritten at linktime by other
6867 definition from different object file) and when resolution info is available
6868 we simply use the knowledge passed to us by linker plugin. */
6869 bool
6870 decl_binds_to_current_def_p (const_tree decl)
6872 gcc_assert (DECL_P (decl));
6873 if (!targetm.binds_local_p (decl))
6874 return false;
6875 if (!TREE_PUBLIC (decl))
6876 return true;
6877 /* When resolution is available, just use it. */
6878 if (TREE_CODE (decl) == VAR_DECL
6879 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
6881 varpool_node *vnode = varpool_node::get (decl);
6882 if (vnode
6883 && vnode->resolution != LDPR_UNKNOWN)
6884 return resolution_to_local_definition_p (vnode->resolution);
6886 else if (TREE_CODE (decl) == FUNCTION_DECL)
6888 struct cgraph_node *node = cgraph_node::get (decl);
6889 if (node
6890 && node->resolution != LDPR_UNKNOWN)
6891 return resolution_to_local_definition_p (node->resolution);
6893 /* Otherwise we have to assume the worst for DECL_WEAK (hidden weaks
6894 binds locally but still can be overwritten), DECL_COMMON (can be merged
6895 with a non-common definition somewhere in the same module) or
6896 DECL_EXTERNAL.
6897 This rely on fact that binds_local_p behave as decl_replaceable_p
6898 for all other declaration types. */
6899 if (DECL_WEAK (decl))
6900 return false;
6901 if (DECL_COMMON (decl)
6902 && (DECL_INITIAL (decl) == NULL
6903 || (!in_lto_p && DECL_INITIAL (decl) == error_mark_node)))
6904 return false;
6905 if (DECL_EXTERNAL (decl))
6906 return false;
6907 return true;
6910 /* A replaceable function or variable is one which may be replaced
6911 at link-time with an entirely different definition, provided that the
6912 replacement has the same type. For example, functions declared
6913 with __attribute__((weak)) on most systems are replaceable.
6915 COMDAT functions are not replaceable, since all definitions of the
6916 function must be equivalent. It is important that COMDAT functions
6917 not be treated as replaceable so that use of C++ template
6918 instantiations is not penalized. */
6920 bool
6921 decl_replaceable_p (tree decl)
6923 gcc_assert (DECL_P (decl));
6924 if (!TREE_PUBLIC (decl) || DECL_COMDAT (decl))
6925 return false;
6926 if (!flag_semantic_interposition
6927 && !DECL_WEAK (decl))
6928 return false;
6929 return !decl_binds_to_current_def_p (decl);
6932 /* Default function to output code that will globalize a label. A
6933 target must define GLOBAL_ASM_OP or provide its own function to
6934 globalize a label. */
6935 #ifdef GLOBAL_ASM_OP
6936 void
6937 default_globalize_label (FILE * stream, const char *name)
6939 fputs (GLOBAL_ASM_OP, stream);
6940 assemble_name (stream, name);
6941 putc ('\n', stream);
6943 #endif /* GLOBAL_ASM_OP */
6945 /* Default function to output code that will globalize a declaration. */
6946 void
6947 default_globalize_decl_name (FILE * stream, tree decl)
6949 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
6950 targetm.asm_out.globalize_label (stream, name);
6953 /* Default function to output a label for unwind information. The
6954 default is to do nothing. A target that needs nonlocal labels for
6955 unwind information must provide its own function to do this. */
6956 void
6957 default_emit_unwind_label (FILE * stream ATTRIBUTE_UNUSED,
6958 tree decl ATTRIBUTE_UNUSED,
6959 int for_eh ATTRIBUTE_UNUSED,
6960 int empty ATTRIBUTE_UNUSED)
6964 /* Default function to output a label to divide up the exception table.
6965 The default is to do nothing. A target that needs/wants to divide
6966 up the table must provide it's own function to do this. */
6967 void
6968 default_emit_except_table_label (FILE * stream ATTRIBUTE_UNUSED)
6972 /* This is how to output an internal numbered label where PREFIX is
6973 the class of label and LABELNO is the number within the class. */
6975 void
6976 default_generate_internal_label (char *buf, const char *prefix,
6977 unsigned long labelno)
6979 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
6982 /* This is how to output an internal numbered label where PREFIX is
6983 the class of label and LABELNO is the number within the class. */
6985 void
6986 default_internal_label (FILE *stream, const char *prefix,
6987 unsigned long labelno)
6989 char *const buf = (char *) alloca (40 + strlen (prefix));
6990 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
6991 ASM_OUTPUT_INTERNAL_LABEL (stream, buf);
6995 /* The default implementation of ASM_DECLARE_CONSTANT_NAME. */
6997 void
6998 default_asm_declare_constant_name (FILE *file, const char *name,
6999 const_tree exp ATTRIBUTE_UNUSED,
7000 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
7002 assemble_label (file, name);
7005 /* This is the default behavior at the beginning of a file. It's
7006 controlled by two other target-hook toggles. */
7007 void
7008 default_file_start (void)
7010 if (targetm.asm_file_start_app_off
7011 && !(flag_verbose_asm || flag_debug_asm || flag_dump_rtl_in_asm))
7012 fputs (ASM_APP_OFF, asm_out_file);
7014 if (targetm.asm_file_start_file_directive)
7015 output_file_directive (asm_out_file, main_input_filename);
7018 /* This is a generic routine suitable for use as TARGET_ASM_FILE_END
7019 which emits a special section directive used to indicate whether or
7020 not this object file needs an executable stack. This is primarily
7021 a GNU extension to ELF but could be used on other targets. */
7023 int trampolines_created;
7025 void
7026 file_end_indicate_exec_stack (void)
7028 unsigned int flags = SECTION_DEBUG;
7029 if (trampolines_created)
7030 flags |= SECTION_CODE;
7032 switch_to_section (get_section (".note.GNU-stack", flags, NULL));
7035 /* Emit a special section directive to indicate that this object file
7036 was compiled with -fsplit-stack. This is used to let the linker
7037 detect calls between split-stack code and non-split-stack code, so
7038 that it can modify the split-stack code to allocate a sufficiently
7039 large stack. We emit another special section if there are any
7040 functions in this file which have the no_split_stack attribute, to
7041 prevent the linker from warning about being unable to convert the
7042 functions if they call non-split-stack code. */
7044 void
7045 file_end_indicate_split_stack (void)
7047 if (flag_split_stack)
7049 switch_to_section (get_section (".note.GNU-split-stack", SECTION_DEBUG,
7050 NULL));
7051 if (saw_no_split_stack)
7052 switch_to_section (get_section (".note.GNU-no-split-stack",
7053 SECTION_DEBUG, NULL));
7057 /* Output DIRECTIVE (a C string) followed by a newline. This is used as
7058 a get_unnamed_section callback. */
7060 void
7061 output_section_asm_op (const void *directive)
7063 fprintf (asm_out_file, "%s\n", (const char *) directive);
7066 /* Emit assembly code to switch to section NEW_SECTION. Do nothing if
7067 the current section is NEW_SECTION. */
7069 void
7070 switch_to_section (section *new_section)
7072 if (in_section == new_section)
7073 return;
7075 if (new_section->common.flags & SECTION_FORGET)
7076 in_section = NULL;
7077 else
7078 in_section = new_section;
7080 switch (SECTION_STYLE (new_section))
7082 case SECTION_NAMED:
7083 targetm.asm_out.named_section (new_section->named.name,
7084 new_section->named.common.flags,
7085 new_section->named.decl);
7086 break;
7088 case SECTION_UNNAMED:
7089 new_section->unnamed.callback (new_section->unnamed.data);
7090 break;
7092 case SECTION_NOSWITCH:
7093 gcc_unreachable ();
7094 break;
7097 new_section->common.flags |= SECTION_DECLARED;
7100 /* If block symbol SYMBOL has not yet been assigned an offset, place
7101 it at the end of its block. */
7103 void
7104 place_block_symbol (rtx symbol)
7106 unsigned HOST_WIDE_INT size, mask, offset;
7107 struct constant_descriptor_rtx *desc;
7108 unsigned int alignment;
7109 struct object_block *block;
7110 tree decl;
7112 gcc_assert (SYMBOL_REF_BLOCK (symbol));
7113 if (SYMBOL_REF_BLOCK_OFFSET (symbol) >= 0)
7114 return;
7116 /* Work out the symbol's size and alignment. */
7117 if (CONSTANT_POOL_ADDRESS_P (symbol))
7119 desc = SYMBOL_REF_CONSTANT (symbol);
7120 alignment = desc->align;
7121 size = GET_MODE_SIZE (desc->mode);
7123 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
7125 decl = SYMBOL_REF_DECL (symbol);
7126 alignment = DECL_ALIGN (decl);
7127 size = get_constant_size (DECL_INITIAL (decl));
7128 if ((flag_sanitize & SANITIZE_ADDRESS)
7129 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
7130 && asan_protect_global (DECL_INITIAL (decl)))
7131 size += asan_red_zone_size (size);
7133 else
7135 struct symtab_node *snode;
7136 decl = SYMBOL_REF_DECL (symbol);
7138 snode = symtab_node::get (decl);
7139 if (snode->alias)
7141 rtx target = DECL_RTL (snode->ultimate_alias_target ()->decl);
7143 place_block_symbol (target);
7144 SYMBOL_REF_BLOCK_OFFSET (symbol) = SYMBOL_REF_BLOCK_OFFSET (target);
7145 return;
7147 alignment = get_variable_align (decl);
7148 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
7149 if ((flag_sanitize & SANITIZE_ADDRESS)
7150 && asan_protect_global (decl))
7152 size += asan_red_zone_size (size);
7153 alignment = MAX (alignment,
7154 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
7158 /* Calculate the object's offset from the start of the block. */
7159 block = SYMBOL_REF_BLOCK (symbol);
7160 mask = alignment / BITS_PER_UNIT - 1;
7161 offset = (block->size + mask) & ~mask;
7162 SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
7164 /* Record the block's new alignment and size. */
7165 block->alignment = MAX (block->alignment, alignment);
7166 block->size = offset + size;
7168 vec_safe_push (block->objects, symbol);
7171 /* Return the anchor that should be used to address byte offset OFFSET
7172 from the first object in BLOCK. MODEL is the TLS model used
7173 to access it. */
7176 get_section_anchor (struct object_block *block, HOST_WIDE_INT offset,
7177 enum tls_model model)
7179 char label[100];
7180 unsigned int begin, middle, end;
7181 unsigned HOST_WIDE_INT min_offset, max_offset, range, bias, delta;
7182 rtx anchor;
7184 /* Work out the anchor's offset. Use an offset of 0 for the first
7185 anchor so that we don't pessimize the case where we take the address
7186 of a variable at the beginning of the block. This is particularly
7187 useful when a block has only one variable assigned to it.
7189 We try to place anchors RANGE bytes apart, so there can then be
7190 anchors at +/-RANGE, +/-2 * RANGE, and so on, up to the limits of
7191 a ptr_mode offset. With some target settings, the lowest such
7192 anchor might be out of range for the lowest ptr_mode offset;
7193 likewise the highest anchor for the highest offset. Use anchors
7194 at the extreme ends of the ptr_mode range in such cases.
7196 All arithmetic uses unsigned integers in order to avoid
7197 signed overflow. */
7198 max_offset = (unsigned HOST_WIDE_INT) targetm.max_anchor_offset;
7199 min_offset = (unsigned HOST_WIDE_INT) targetm.min_anchor_offset;
7200 range = max_offset - min_offset + 1;
7201 if (range == 0)
7202 offset = 0;
7203 else
7205 bias = HOST_WIDE_INT_1U << (GET_MODE_BITSIZE (ptr_mode) - 1);
7206 if (offset < 0)
7208 delta = -(unsigned HOST_WIDE_INT) offset + max_offset;
7209 delta -= delta % range;
7210 if (delta > bias)
7211 delta = bias;
7212 offset = (HOST_WIDE_INT) (-delta);
7214 else
7216 delta = (unsigned HOST_WIDE_INT) offset - min_offset;
7217 delta -= delta % range;
7218 if (delta > bias - 1)
7219 delta = bias - 1;
7220 offset = (HOST_WIDE_INT) delta;
7224 /* Do a binary search to see if there's already an anchor we can use.
7225 Set BEGIN to the new anchor's index if not. */
7226 begin = 0;
7227 end = vec_safe_length (block->anchors);
7228 while (begin != end)
7230 middle = (end + begin) / 2;
7231 anchor = (*block->anchors)[middle];
7232 if (SYMBOL_REF_BLOCK_OFFSET (anchor) > offset)
7233 end = middle;
7234 else if (SYMBOL_REF_BLOCK_OFFSET (anchor) < offset)
7235 begin = middle + 1;
7236 else if (SYMBOL_REF_TLS_MODEL (anchor) > model)
7237 end = middle;
7238 else if (SYMBOL_REF_TLS_MODEL (anchor) < model)
7239 begin = middle + 1;
7240 else
7241 return anchor;
7244 /* Create a new anchor with a unique label. */
7245 ASM_GENERATE_INTERNAL_LABEL (label, "LANCHOR", anchor_labelno++);
7246 anchor = create_block_symbol (ggc_strdup (label), block, offset);
7247 SYMBOL_REF_FLAGS (anchor) |= SYMBOL_FLAG_LOCAL | SYMBOL_FLAG_ANCHOR;
7248 SYMBOL_REF_FLAGS (anchor) |= model << SYMBOL_FLAG_TLS_SHIFT;
7250 /* Insert it at index BEGIN. */
7251 vec_safe_insert (block->anchors, begin, anchor);
7252 return anchor;
7255 /* Output the objects in BLOCK. */
7257 static void
7258 output_object_block (struct object_block *block)
7260 struct constant_descriptor_rtx *desc;
7261 unsigned int i;
7262 HOST_WIDE_INT offset;
7263 tree decl;
7264 rtx symbol;
7266 if (!block->objects)
7267 return;
7269 /* Switch to the section and make sure that the first byte is
7270 suitably aligned. */
7271 switch_to_section (block->sect);
7272 assemble_align (block->alignment);
7274 /* Define the values of all anchors relative to the current section
7275 position. */
7276 FOR_EACH_VEC_SAFE_ELT (block->anchors, i, symbol)
7277 targetm.asm_out.output_anchor (symbol);
7279 /* Output the objects themselves. */
7280 offset = 0;
7281 FOR_EACH_VEC_ELT (*block->objects, i, symbol)
7283 /* Move to the object's offset, padding with zeros if necessary. */
7284 assemble_zeros (SYMBOL_REF_BLOCK_OFFSET (symbol) - offset);
7285 offset = SYMBOL_REF_BLOCK_OFFSET (symbol);
7286 if (CONSTANT_POOL_ADDRESS_P (symbol))
7288 desc = SYMBOL_REF_CONSTANT (symbol);
7289 output_constant_pool_1 (desc, 1);
7290 offset += GET_MODE_SIZE (desc->mode);
7292 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
7294 HOST_WIDE_INT size;
7295 decl = SYMBOL_REF_DECL (symbol);
7296 assemble_constant_contents (DECL_INITIAL (decl), XSTR (symbol, 0),
7297 DECL_ALIGN (decl));
7298 size = get_constant_size (DECL_INITIAL (decl));
7299 offset += size;
7300 if ((flag_sanitize & SANITIZE_ADDRESS)
7301 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
7302 && asan_protect_global (DECL_INITIAL (decl)))
7304 size = asan_red_zone_size (size);
7305 assemble_zeros (size);
7306 offset += size;
7309 else
7311 HOST_WIDE_INT size;
7312 decl = SYMBOL_REF_DECL (symbol);
7313 assemble_variable_contents (decl, XSTR (symbol, 0), false);
7314 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
7315 offset += size;
7316 if ((flag_sanitize & SANITIZE_ADDRESS)
7317 && asan_protect_global (decl))
7319 size = asan_red_zone_size (size);
7320 assemble_zeros (size);
7321 offset += size;
7327 /* A htab_traverse callback used to call output_object_block for
7328 each member of object_block_htab. */
7331 output_object_block_htab (object_block **slot, void *)
7333 output_object_block (*slot);
7334 return 1;
7337 /* Output the definitions of all object_blocks. */
7339 void
7340 output_object_blocks (void)
7342 object_block_htab->traverse<void *, output_object_block_htab> (NULL);
7345 /* This function provides a possible implementation of the
7346 TARGET_ASM_RECORD_GCC_SWITCHES target hook for ELF targets. When triggered
7347 by -frecord-gcc-switches it creates a new mergeable, string section in the
7348 assembler output file called TARGET_ASM_RECORD_GCC_SWITCHES_SECTION which
7349 contains the switches in ASCII format.
7351 FIXME: This code does not correctly handle double quote characters
7352 that appear inside strings, (it strips them rather than preserving them).
7353 FIXME: ASM_OUTPUT_ASCII, as defined in config/elfos.h will not emit NUL
7354 characters - instead it treats them as sub-string separators. Since
7355 we want to emit NUL strings terminators into the object file we have to use
7356 ASM_OUTPUT_SKIP. */
7359 elf_record_gcc_switches (print_switch_type type, const char * name)
7361 switch (type)
7363 case SWITCH_TYPE_PASSED:
7364 ASM_OUTPUT_ASCII (asm_out_file, name, strlen (name));
7365 ASM_OUTPUT_SKIP (asm_out_file, (unsigned HOST_WIDE_INT) 1);
7366 break;
7368 case SWITCH_TYPE_DESCRIPTIVE:
7369 if (name == NULL)
7371 /* Distinguish between invocations where name is NULL. */
7372 static bool started = false;
7374 if (!started)
7376 section * sec;
7378 sec = get_section (targetm.asm_out.record_gcc_switches_section,
7379 SECTION_DEBUG
7380 | SECTION_MERGE
7381 | SECTION_STRINGS
7382 | (SECTION_ENTSIZE & 1),
7383 NULL);
7384 switch_to_section (sec);
7385 started = true;
7389 default:
7390 break;
7393 /* The return value is currently ignored by the caller, but must be 0.
7394 For -fverbose-asm the return value would be the number of characters
7395 emitted into the assembler file. */
7396 return 0;
7399 /* Emit text to declare externally defined symbols. It is needed to
7400 properly support non-default visibility. */
7401 void
7402 default_elf_asm_output_external (FILE *file ATTRIBUTE_UNUSED,
7403 tree decl,
7404 const char *name ATTRIBUTE_UNUSED)
7406 /* We output the name if and only if TREE_SYMBOL_REFERENCED is
7407 set in order to avoid putting out names that are never really
7408 used. */
7409 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
7410 && targetm.binds_local_p (decl))
7411 maybe_assemble_visibility (decl);
7414 /* The default hook for TARGET_ASM_OUTPUT_SOURCE_FILENAME. */
7416 void
7417 default_asm_output_source_filename (FILE *file, const char *name)
7419 #ifdef ASM_OUTPUT_SOURCE_FILENAME
7420 ASM_OUTPUT_SOURCE_FILENAME (file, name);
7421 #else
7422 fprintf (file, "\t.file\t");
7423 output_quoted_string (file, name);
7424 putc ('\n', file);
7425 #endif
7428 /* Output a file name in the form wanted by System V. */
7430 void
7431 output_file_directive (FILE *asm_file, const char *input_name)
7433 int len;
7434 const char *na;
7436 if (input_name == NULL)
7437 input_name = "<stdin>";
7438 else
7439 input_name = remap_debug_filename (input_name);
7441 len = strlen (input_name);
7442 na = input_name + len;
7444 /* NA gets INPUT_NAME sans directory names. */
7445 while (na > input_name)
7447 if (IS_DIR_SEPARATOR (na[-1]))
7448 break;
7449 na--;
7452 targetm.asm_out.output_source_filename (asm_file, na);
7455 /* Create a DEBUG_EXPR_DECL / DEBUG_EXPR pair from RTL expression
7456 EXP. */
7458 make_debug_expr_from_rtl (const_rtx exp)
7460 tree ddecl = make_node (DEBUG_EXPR_DECL), type;
7461 machine_mode mode = GET_MODE (exp);
7462 rtx dval;
7464 DECL_ARTIFICIAL (ddecl) = 1;
7465 if (REG_P (exp) && REG_EXPR (exp))
7466 type = TREE_TYPE (REG_EXPR (exp));
7467 else if (MEM_P (exp) && MEM_EXPR (exp))
7468 type = TREE_TYPE (MEM_EXPR (exp));
7469 else
7470 type = NULL_TREE;
7471 if (type && TYPE_MODE (type) == mode)
7472 TREE_TYPE (ddecl) = type;
7473 else
7474 TREE_TYPE (ddecl) = lang_hooks.types.type_for_mode (mode, 1);
7475 DECL_MODE (ddecl) = mode;
7476 dval = gen_rtx_DEBUG_EXPR (mode);
7477 DEBUG_EXPR_TREE_DECL (dval) = ddecl;
7478 SET_DECL_RTL (ddecl, dval);
7479 return dval;
7482 #ifdef ELF_ASCII_ESCAPES
7483 /* Default ASM_OUTPUT_LIMITED_STRING for ELF targets. */
7485 void
7486 default_elf_asm_output_limited_string (FILE *f, const char *s)
7488 int escape;
7489 unsigned char c;
7491 fputs (STRING_ASM_OP, f);
7492 putc ('"', f);
7493 while (*s != '\0')
7495 c = *s;
7496 escape = ELF_ASCII_ESCAPES[c];
7497 switch (escape)
7499 case 0:
7500 putc (c, f);
7501 break;
7502 case 1:
7503 /* TODO: Print in hex with fast function, important for -flto. */
7504 fprintf (f, "\\%03o", c);
7505 break;
7506 default:
7507 putc ('\\', f);
7508 putc (escape, f);
7509 break;
7511 s++;
7513 putc ('\"', f);
7514 putc ('\n', f);
7517 /* Default ASM_OUTPUT_ASCII for ELF targets. */
7519 void
7520 default_elf_asm_output_ascii (FILE *f, const char *s, unsigned int len)
7522 const char *limit = s + len;
7523 const char *last_null = NULL;
7524 unsigned bytes_in_chunk = 0;
7525 unsigned char c;
7526 int escape;
7528 for (; s < limit; s++)
7530 const char *p;
7532 if (bytes_in_chunk >= 60)
7534 putc ('\"', f);
7535 putc ('\n', f);
7536 bytes_in_chunk = 0;
7539 if (s > last_null)
7541 for (p = s; p < limit && *p != '\0'; p++)
7542 continue;
7543 last_null = p;
7545 else
7546 p = last_null;
7548 if (p < limit && (p - s) <= (long) ELF_STRING_LIMIT)
7550 if (bytes_in_chunk > 0)
7552 putc ('\"', f);
7553 putc ('\n', f);
7554 bytes_in_chunk = 0;
7557 default_elf_asm_output_limited_string (f, s);
7558 s = p;
7560 else
7562 if (bytes_in_chunk == 0)
7563 fputs (ASCII_DATA_ASM_OP "\"", f);
7565 c = *s;
7566 escape = ELF_ASCII_ESCAPES[c];
7567 switch (escape)
7569 case 0:
7570 putc (c, f);
7571 bytes_in_chunk++;
7572 break;
7573 case 1:
7574 /* TODO: Print in hex with fast function, important for -flto. */
7575 fprintf (f, "\\%03o", c);
7576 bytes_in_chunk += 4;
7577 break;
7578 default:
7579 putc ('\\', f);
7580 putc (escape, f);
7581 bytes_in_chunk += 2;
7582 break;
7588 if (bytes_in_chunk > 0)
7590 putc ('\"', f);
7591 putc ('\n', f);
7594 #endif
7596 static GTY(()) section *elf_init_array_section;
7597 static GTY(()) section *elf_fini_array_section;
7599 static section *
7600 get_elf_initfini_array_priority_section (int priority,
7601 bool constructor_p)
7603 section *sec;
7604 if (priority != DEFAULT_INIT_PRIORITY)
7606 char buf[18];
7607 sprintf (buf, "%s.%.5u",
7608 constructor_p ? ".init_array" : ".fini_array",
7609 priority);
7610 sec = get_section (buf, SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
7612 else
7614 if (constructor_p)
7616 if (elf_init_array_section == NULL)
7617 elf_init_array_section
7618 = get_section (".init_array",
7619 SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
7620 sec = elf_init_array_section;
7622 else
7624 if (elf_fini_array_section == NULL)
7625 elf_fini_array_section
7626 = get_section (".fini_array",
7627 SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
7628 sec = elf_fini_array_section;
7631 return sec;
7634 /* Use .init_array section for constructors. */
7636 void
7637 default_elf_init_array_asm_out_constructor (rtx symbol, int priority)
7639 section *sec = get_elf_initfini_array_priority_section (priority,
7640 true);
7641 assemble_addr_to_section (symbol, sec);
7644 /* Use .fini_array section for destructors. */
7646 void
7647 default_elf_fini_array_asm_out_destructor (rtx symbol, int priority)
7649 section *sec = get_elf_initfini_array_priority_section (priority,
7650 false);
7651 assemble_addr_to_section (symbol, sec);
7654 /* Default TARGET_ASM_OUTPUT_IDENT hook.
7656 This is a bit of a cheat. The real default is a no-op, but this
7657 hook is the default for all targets with a .ident directive. */
7659 void
7660 default_asm_output_ident_directive (const char *ident_str)
7662 const char *ident_asm_op = "\t.ident\t";
7664 /* If we are still in the front end, do not write out the string
7665 to asm_out_file. Instead, add a fake top-level asm statement.
7666 This allows the front ends to use this hook without actually
7667 writing to asm_out_file, to handle #ident or Pragma Ident. */
7668 if (symtab->state == PARSING)
7670 char *buf = ACONCAT ((ident_asm_op, "\"", ident_str, "\"\n", NULL));
7671 symtab->finalize_toplevel_asm (build_string (strlen (buf), buf));
7673 else
7674 fprintf (asm_out_file, "%s\"%s\"\n", ident_asm_op, ident_str);
7677 #include "gt-varasm.h"