* config/i386/i386.md (fmodxf3): Enable for flag_finite_math_only only.
[official-gcc.git] / gcc / varasm.c
blob0b99b39c1b87f5b518fe562e7a57e2f1d91e343a
1 /* Output variables, constants and external declarations, for GNU compiler.
2 Copyright (C) 1987-2014 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 "tree.h"
34 #include "stor-layout.h"
35 #include "stringpool.h"
36 #include "varasm.h"
37 #include "flags.h"
38 #include "function.h"
39 #include "expr.h"
40 #include "hard-reg-set.h"
41 #include "regs.h"
42 #include "output.h"
43 #include "diagnostic-core.h"
44 #include "hashtab.h"
45 #include "ggc.h"
46 #include "langhooks.h"
47 #include "tm_p.h"
48 #include "debug.h"
49 #include "target.h"
50 #include "common/common-target.h"
51 #include "targhooks.h"
52 #include "cgraph.h"
53 #include "hash-set.h"
54 #include "asan.h"
55 #include "basic-block.h"
56 #include "rtl-iter.h"
58 #ifdef XCOFF_DEBUGGING_INFO
59 #include "xcoffout.h" /* Needed for external data
60 declarations for e.g. AIX 4.x. */
61 #endif
63 /* The (assembler) name of the first globally-visible object output. */
64 extern GTY(()) const char *first_global_object_name;
65 extern GTY(()) const char *weak_global_object_name;
67 const char *first_global_object_name;
68 const char *weak_global_object_name;
70 struct addr_const;
71 struct constant_descriptor_rtx;
72 struct rtx_constant_pool;
74 #define n_deferred_constants (crtl->varasm.deferred_constants)
76 /* Number for making the label on the next
77 constant that is stored in memory. */
79 static GTY(()) int const_labelno;
81 /* Carry information from ASM_DECLARE_OBJECT_NAME
82 to ASM_FINISH_DECLARE_OBJECT. */
84 int size_directive_output;
86 /* The last decl for which assemble_variable was called,
87 if it did ASM_DECLARE_OBJECT_NAME.
88 If the last call to assemble_variable didn't do that,
89 this holds 0. */
91 tree last_assemble_variable_decl;
93 /* The following global variable indicates if the first basic block
94 in a function belongs to the cold partition or not. */
96 bool first_function_block_is_cold;
98 /* We give all constants their own alias set. Perhaps redundant with
99 MEM_READONLY_P, but pre-dates it. */
101 static alias_set_type const_alias_set;
103 /* Whether we saw any functions with no_split_stack. */
105 static bool saw_no_split_stack;
107 static const char *strip_reg_name (const char *);
108 static int contains_pointers_p (tree);
109 #ifdef ASM_OUTPUT_EXTERNAL
110 static bool incorporeal_function_p (tree);
111 #endif
112 static void decode_addr_const (tree, struct addr_const *);
113 static hashval_t const_desc_hash (const void *);
114 static int const_desc_eq (const void *, const void *);
115 static hashval_t const_hash_1 (const tree);
116 static int compare_constant (const tree, const tree);
117 static void output_constant_def_contents (rtx);
118 static void output_addressed_constants (tree);
119 static unsigned HOST_WIDE_INT output_constant (tree, unsigned HOST_WIDE_INT,
120 unsigned int);
121 static void globalize_decl (tree);
122 static bool decl_readonly_section_1 (enum section_category);
123 #ifdef BSS_SECTION_ASM_OP
124 #ifdef ASM_OUTPUT_ALIGNED_BSS
125 static void asm_output_aligned_bss (FILE *, tree, const char *,
126 unsigned HOST_WIDE_INT, int)
127 ATTRIBUTE_UNUSED;
128 #endif
129 #endif /* BSS_SECTION_ASM_OP */
130 static void mark_weak (tree);
131 static void output_constant_pool (const char *, tree);
133 /* Well-known sections, each one associated with some sort of *_ASM_OP. */
134 section *text_section;
135 section *data_section;
136 section *readonly_data_section;
137 section *sdata_section;
138 section *ctors_section;
139 section *dtors_section;
140 section *bss_section;
141 section *sbss_section;
143 /* Various forms of common section. All are guaranteed to be nonnull. */
144 section *tls_comm_section;
145 section *comm_section;
146 section *lcomm_section;
148 /* A SECTION_NOSWITCH section used for declaring global BSS variables.
149 May be null. */
150 section *bss_noswitch_section;
152 /* The section that holds the main exception table, when known. The section
153 is set either by the target's init_sections hook or by the first call to
154 switch_to_exception_section. */
155 section *exception_section;
157 /* The section that holds the DWARF2 frame unwind information, when known.
158 The section is set either by the target's init_sections hook or by the
159 first call to switch_to_eh_frame_section. */
160 section *eh_frame_section;
162 /* asm_out_file's current section. This is NULL if no section has yet
163 been selected or if we lose track of what the current section is. */
164 section *in_section;
166 /* True if code for the current function is currently being directed
167 at the cold section. */
168 bool in_cold_section_p;
170 /* A linked list of all the unnamed sections. */
171 static GTY(()) section *unnamed_sections;
173 /* Return a nonzero value if DECL has a section attribute. */
174 #define IN_NAMED_SECTION(DECL) \
175 ((TREE_CODE (DECL) == FUNCTION_DECL || TREE_CODE (DECL) == VAR_DECL) \
176 && DECL_SECTION_NAME (DECL) != NULL)
178 /* Hash table of named sections. */
179 static GTY((param_is (section))) htab_t section_htab;
181 /* A table of object_blocks, indexed by section. */
182 static GTY((param_is (struct object_block))) htab_t object_block_htab;
184 /* The next number to use for internal anchor labels. */
185 static GTY(()) int anchor_labelno;
187 /* A pool of constants that can be shared between functions. */
188 static GTY(()) struct rtx_constant_pool *shared_constant_pool;
190 /* Helper routines for maintaining section_htab. */
192 static int
193 section_entry_eq (const void *p1, const void *p2)
195 const section *old = (const section *) p1;
196 const char *new_name = (const char *) p2;
198 return strcmp (old->named.name, new_name) == 0;
201 static hashval_t
202 section_entry_hash (const void *p)
204 const section *old = (const section *) p;
205 return htab_hash_string (old->named.name);
208 /* Return a hash value for section SECT. */
210 static hashval_t
211 hash_section (section *sect)
213 if (sect->common.flags & SECTION_NAMED)
214 return htab_hash_string (sect->named.name);
215 return sect->common.flags;
218 /* Helper routines for maintaining object_block_htab. */
220 static int
221 object_block_entry_eq (const void *p1, const void *p2)
223 const struct object_block *old = (const struct object_block *) p1;
224 const section *new_section = (const section *) p2;
226 return old->sect == new_section;
229 static hashval_t
230 object_block_entry_hash (const void *p)
232 const struct object_block *old = (const struct object_block *) p;
233 return hash_section (old->sect);
236 /* Return a new unnamed section with the given fields. */
238 section *
239 get_unnamed_section (unsigned int flags, void (*callback) (const void *),
240 const void *data)
242 section *sect;
244 sect = ggc_alloc<section> ();
245 sect->unnamed.common.flags = flags | SECTION_UNNAMED;
246 sect->unnamed.callback = callback;
247 sect->unnamed.data = data;
248 sect->unnamed.next = unnamed_sections;
250 unnamed_sections = sect;
251 return sect;
254 /* Return a SECTION_NOSWITCH section with the given fields. */
256 static section *
257 get_noswitch_section (unsigned int flags, noswitch_section_callback callback)
259 section *sect;
261 sect = ggc_alloc<section> ();
262 sect->noswitch.common.flags = flags | SECTION_NOSWITCH;
263 sect->noswitch.callback = callback;
265 return sect;
268 /* Return the named section structure associated with NAME. Create
269 a new section with the given fields if no such structure exists. */
271 section *
272 get_section (const char *name, unsigned int flags, tree decl)
274 section *sect, **slot;
276 slot = (section **)
277 htab_find_slot_with_hash (section_htab, name,
278 htab_hash_string (name), INSERT);
279 flags |= SECTION_NAMED;
280 if (*slot == NULL)
282 sect = ggc_alloc<section> ();
283 sect->named.common.flags = flags;
284 sect->named.name = ggc_strdup (name);
285 sect->named.decl = decl;
286 *slot = sect;
288 else
290 sect = *slot;
291 if ((sect->common.flags & ~SECTION_DECLARED) != flags
292 && ((sect->common.flags | flags) & SECTION_OVERRIDE) == 0)
294 /* It is fine if one of the section flags is
295 SECTION_WRITE | SECTION_RELRO and the other has none of these
296 flags (i.e. read-only) in named sections and either the
297 section hasn't been declared yet or has been declared as writable.
298 In that case just make sure the resulting flags are
299 SECTION_WRITE | SECTION_RELRO, ie. writable only because of
300 relocations. */
301 if (((sect->common.flags ^ flags) & (SECTION_WRITE | SECTION_RELRO))
302 == (SECTION_WRITE | SECTION_RELRO)
303 && (sect->common.flags
304 & ~(SECTION_DECLARED | SECTION_WRITE | SECTION_RELRO))
305 == (flags & ~(SECTION_WRITE | SECTION_RELRO))
306 && ((sect->common.flags & SECTION_DECLARED) == 0
307 || (sect->common.flags & SECTION_WRITE)))
309 sect->common.flags |= (SECTION_WRITE | SECTION_RELRO);
310 return sect;
312 /* Sanity check user variables for flag changes. */
313 if (sect->named.decl != NULL
314 && DECL_P (sect->named.decl)
315 && decl != sect->named.decl)
317 if (decl != NULL && DECL_P (decl))
318 error ("%+D causes a section type conflict with %D",
319 decl, sect->named.decl);
320 else
321 error ("section type conflict with %D", sect->named.decl);
322 inform (DECL_SOURCE_LOCATION (sect->named.decl),
323 "%qD was declared here", sect->named.decl);
325 else if (decl != NULL && DECL_P (decl))
326 error ("%+D causes a section type conflict", decl);
327 else
328 error ("section type conflict");
329 /* Make sure we don't error about one section multiple times. */
330 sect->common.flags |= SECTION_OVERRIDE;
333 return sect;
336 /* Return true if the current compilation mode benefits from having
337 objects grouped into blocks. */
339 static bool
340 use_object_blocks_p (void)
342 return flag_section_anchors;
345 /* Return the object_block structure for section SECT. Create a new
346 structure if we haven't created one already. Return null if SECT
347 itself is null. */
349 static struct object_block *
350 get_block_for_section (section *sect)
352 struct object_block *block;
353 void **slot;
355 if (sect == NULL)
356 return NULL;
358 slot = htab_find_slot_with_hash (object_block_htab, sect,
359 hash_section (sect), INSERT);
360 block = (struct object_block *) *slot;
361 if (block == NULL)
363 block = ggc_cleared_alloc<object_block> ();
364 block->sect = sect;
365 *slot = block;
367 return block;
370 /* Create a symbol with label LABEL and place it at byte offset
371 OFFSET in BLOCK. OFFSET can be negative if the symbol's offset
372 is not yet known. LABEL must be a garbage-collected string. */
374 static rtx
375 create_block_symbol (const char *label, struct object_block *block,
376 HOST_WIDE_INT offset)
378 rtx symbol;
379 unsigned int size;
381 /* Create the extended SYMBOL_REF. */
382 size = RTX_HDR_SIZE + sizeof (struct block_symbol);
383 symbol = (rtx) ggc_internal_alloc (size);
385 /* Initialize the normal SYMBOL_REF fields. */
386 memset (symbol, 0, size);
387 PUT_CODE (symbol, SYMBOL_REF);
388 PUT_MODE (symbol, Pmode);
389 XSTR (symbol, 0) = label;
390 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_HAS_BLOCK_INFO;
392 /* Initialize the block_symbol stuff. */
393 SYMBOL_REF_BLOCK (symbol) = block;
394 SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
396 return symbol;
399 /* Return a section with a particular name and with whatever SECTION_*
400 flags section_type_flags deems appropriate. The name of the section
401 is taken from NAME if nonnull, otherwise it is taken from DECL's
402 DECL_SECTION_NAME. DECL is the decl associated with the section
403 (see the section comment for details) and RELOC is as for
404 section_type_flags. */
406 section *
407 get_named_section (tree decl, const char *name, int reloc)
409 unsigned int flags;
411 if (name == NULL)
413 gcc_assert (decl && DECL_P (decl) && DECL_SECTION_NAME (decl));
414 name = DECL_SECTION_NAME (decl);
417 flags = targetm.section_type_flags (decl, name, reloc);
418 return get_section (name, flags, decl);
421 /* Worker for resolve_unique_section. */
423 static bool
424 set_implicit_section (struct symtab_node *n, void *data ATTRIBUTE_UNUSED)
426 n->implicit_section = true;
427 return false;
430 /* If required, set DECL_SECTION_NAME to a unique name. */
432 void
433 resolve_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED,
434 int flag_function_or_data_sections)
436 if (DECL_SECTION_NAME (decl) == NULL
437 && targetm_common.have_named_sections
438 && (flag_function_or_data_sections
439 || DECL_COMDAT_GROUP (decl)))
441 targetm.asm_out.unique_section (decl, reloc);
442 if (DECL_SECTION_NAME (decl))
443 symtab_node::get (decl)->call_for_symbol_and_aliases
444 (set_implicit_section, NULL, true);
448 #ifdef BSS_SECTION_ASM_OP
450 #ifdef ASM_OUTPUT_ALIGNED_BSS
452 /* Utility function for targets to use in implementing
453 ASM_OUTPUT_ALIGNED_BSS.
454 ??? It is believed that this function will work in most cases so such
455 support is localized here. */
457 static void
458 asm_output_aligned_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
459 const char *name, unsigned HOST_WIDE_INT size,
460 int align)
462 switch_to_section (bss_section);
463 ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
464 #ifdef ASM_DECLARE_OBJECT_NAME
465 last_assemble_variable_decl = decl;
466 ASM_DECLARE_OBJECT_NAME (file, name, decl);
467 #else
468 /* Standard thing is just output label for the object. */
469 ASM_OUTPUT_LABEL (file, name);
470 #endif /* ASM_DECLARE_OBJECT_NAME */
471 ASM_OUTPUT_SKIP (file, size ? size : 1);
474 #endif
476 #endif /* BSS_SECTION_ASM_OP */
478 #ifndef USE_SELECT_SECTION_FOR_FUNCTIONS
479 /* Return the hot section for function DECL. Return text_section for
480 null DECLs. */
482 static section *
483 hot_function_section (tree decl)
485 if (decl != NULL_TREE
486 && DECL_SECTION_NAME (decl) != NULL
487 && targetm_common.have_named_sections)
488 return get_named_section (decl, NULL, 0);
489 else
490 return text_section;
492 #endif
494 /* Return section for TEXT_SECTION_NAME if DECL or DECL_SECTION_NAME (DECL)
495 is NULL.
497 When DECL_SECTION_NAME is non-NULL and it is implicit section and
498 NAMED_SECTION_SUFFIX is non-NULL, then produce section called
499 concatenate the name with NAMED_SECTION_SUFFIX.
500 Otherwise produce "TEXT_SECTION_NAME.IMPLICIT_NAME". */
502 section *
503 get_named_text_section (tree decl,
504 const char *text_section_name,
505 const char *named_section_suffix)
507 if (decl && DECL_SECTION_NAME (decl))
509 if (named_section_suffix)
511 const char *dsn = DECL_SECTION_NAME (decl);
512 const char *stripped_name;
513 char *name, *buffer;
515 name = (char *) alloca (strlen (dsn) + 1);
516 memcpy (name, dsn,
517 strlen (dsn) + 1);
519 stripped_name = targetm.strip_name_encoding (name);
521 buffer = ACONCAT ((stripped_name, named_section_suffix, NULL));
522 return get_named_section (decl, buffer, 0);
524 else if (symtab_node::get (decl)->implicit_section)
526 const char *name;
528 /* Do not try to split gnu_linkonce functions. This gets somewhat
529 slipperly. */
530 if (DECL_COMDAT_GROUP (decl) && !HAVE_COMDAT_GROUP)
531 return NULL;
532 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
533 name = targetm.strip_name_encoding (name);
534 return get_named_section (decl, ACONCAT ((text_section_name, ".",
535 name, NULL)), 0);
537 else
538 return NULL;
540 return get_named_section (decl, text_section_name, 0);
543 /* Choose named function section based on its frequency. */
545 section *
546 default_function_section (tree decl, enum node_frequency freq,
547 bool startup, bool exit)
549 #if defined HAVE_LD_EH_GC_SECTIONS && defined HAVE_LD_EH_GC_SECTIONS_BUG
550 /* Old GNU linkers have buggy --gc-section support, which sometimes
551 results in .gcc_except_table* sections being garbage collected. */
552 if (decl
553 && symtab_node::get (decl)->implicit_section)
554 return NULL;
555 #endif
557 if (!flag_reorder_functions
558 || !targetm_common.have_named_sections)
559 return NULL;
560 /* Startup code should go to startup subsection unless it is
561 unlikely executed (this happens especially with function splitting
562 where we can split away unnecessary parts of static constructors. */
563 if (startup && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
565 /* If we do have a profile or(and) LTO phase is executed, we do not need
566 these ELF section. */
567 if (!in_lto_p || !flag_profile_values)
568 return get_named_text_section (decl, ".text.startup", NULL);
569 else
570 return NULL;
573 /* Similarly for exit. */
574 if (exit && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
575 return get_named_text_section (decl, ".text.exit", NULL);
577 /* Group cold functions together, similarly for hot code. */
578 switch (freq)
580 case NODE_FREQUENCY_UNLIKELY_EXECUTED:
581 return get_named_text_section (decl, ".text.unlikely", NULL);
582 case NODE_FREQUENCY_HOT:
583 /* If we do have a profile or(and) LTO phase is executed, we do not need
584 these ELF section. */
585 if (!in_lto_p || !flag_profile_values)
586 return get_named_text_section (decl, ".text.hot", NULL);
587 default:
588 return NULL;
592 /* Return the section for function DECL.
594 If DECL is NULL_TREE, return the text section. We can be passed
595 NULL_TREE under some circumstances by dbxout.c at least.
597 If FORCE_COLD is true, return cold function section ignoring
598 the frequency info of cgraph_node. */
600 static section *
601 function_section_1 (tree decl, bool force_cold)
603 section *section = NULL;
604 enum node_frequency freq = NODE_FREQUENCY_NORMAL;
605 bool startup = false, exit = false;
607 if (decl)
609 struct cgraph_node *node = cgraph_node::get (decl);
611 if (node)
613 freq = node->frequency;
614 startup = node->only_called_at_startup;
615 exit = node->only_called_at_exit;
618 if (force_cold)
619 freq = NODE_FREQUENCY_UNLIKELY_EXECUTED;
621 #ifdef USE_SELECT_SECTION_FOR_FUNCTIONS
622 if (decl != NULL_TREE
623 && DECL_SECTION_NAME (decl) != NULL)
625 if (targetm.asm_out.function_section)
626 section = targetm.asm_out.function_section (decl, freq,
627 startup, exit);
628 if (section)
629 return section;
630 return get_named_section (decl, NULL, 0);
632 else
633 return targetm.asm_out.select_section
634 (decl, freq == NODE_FREQUENCY_UNLIKELY_EXECUTED,
635 DECL_ALIGN (decl));
636 #else
637 if (targetm.asm_out.function_section)
638 section = targetm.asm_out.function_section (decl, freq, startup, exit);
639 if (section)
640 return section;
641 return hot_function_section (decl);
642 #endif
645 /* Return the section for function DECL.
647 If DECL is NULL_TREE, return the text section. We can be passed
648 NULL_TREE under some circumstances by dbxout.c at least. */
650 section *
651 function_section (tree decl)
653 /* Handle cases where function splitting code decides
654 to put function entry point into unlikely executed section
655 despite the fact that the function itself is not cold
656 (i.e. it is called rarely but contains a hot loop that is
657 better to live in hot subsection for the code locality). */
658 return function_section_1 (decl,
659 first_function_block_is_cold);
662 /* Return the section for the current function, take IN_COLD_SECTION_P
663 into account. */
665 section *
666 current_function_section (void)
668 return function_section_1 (current_function_decl, in_cold_section_p);
671 /* Tell assembler to switch to unlikely-to-be-executed text section. */
673 section *
674 unlikely_text_section (void)
676 return function_section_1 (current_function_decl, true);
679 /* When called within a function context, return true if the function
680 has been assigned a cold text section and if SECT is that section.
681 When called outside a function context, return true if SECT is the
682 default cold section. */
684 bool
685 unlikely_text_section_p (section *sect)
687 return sect == function_section_1 (current_function_decl, true);
690 /* Return the read-only data section associated with function DECL. */
692 section *
693 default_function_rodata_section (tree decl)
695 if (decl != NULL_TREE && DECL_SECTION_NAME (decl))
697 const char *name = DECL_SECTION_NAME (decl);
699 if (DECL_COMDAT_GROUP (decl) && HAVE_COMDAT_GROUP)
701 const char *dot;
702 size_t len;
703 char* rname;
705 dot = strchr (name + 1, '.');
706 if (!dot)
707 dot = name;
708 len = strlen (dot) + 8;
709 rname = (char *) alloca (len);
711 strcpy (rname, ".rodata");
712 strcat (rname, dot);
713 return get_section (rname, SECTION_LINKONCE, decl);
715 /* For .gnu.linkonce.t.foo we want to use .gnu.linkonce.r.foo. */
716 else if (DECL_COMDAT_GROUP (decl)
717 && strncmp (name, ".gnu.linkonce.t.", 16) == 0)
719 size_t len = strlen (name) + 1;
720 char *rname = (char *) alloca (len);
722 memcpy (rname, name, len);
723 rname[14] = 'r';
724 return get_section (rname, SECTION_LINKONCE, decl);
726 /* For .text.foo we want to use .rodata.foo. */
727 else if (flag_function_sections && flag_data_sections
728 && strncmp (name, ".text.", 6) == 0)
730 size_t len = strlen (name) + 1;
731 char *rname = (char *) alloca (len + 2);
733 memcpy (rname, ".rodata", 7);
734 memcpy (rname + 7, name + 5, len - 5);
735 return get_section (rname, 0, decl);
739 return readonly_data_section;
742 /* Return the read-only data section associated with function DECL
743 for targets where that section should be always the single
744 readonly data section. */
746 section *
747 default_no_function_rodata_section (tree decl ATTRIBUTE_UNUSED)
749 return readonly_data_section;
752 /* Return the section to use for string merging. */
754 static section *
755 mergeable_string_section (tree decl ATTRIBUTE_UNUSED,
756 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
757 unsigned int flags ATTRIBUTE_UNUSED)
759 HOST_WIDE_INT len;
761 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
762 && TREE_CODE (decl) == STRING_CST
763 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
764 && align <= 256
765 && (len = int_size_in_bytes (TREE_TYPE (decl))) > 0
766 && TREE_STRING_LENGTH (decl) >= len)
768 enum machine_mode mode;
769 unsigned int modesize;
770 const char *str;
771 HOST_WIDE_INT i;
772 int j, unit;
773 const char *prefix = targetm.asm_out.mergeable_rodata_prefix;
774 char *name = (char *) alloca (strlen (prefix) + 30);
776 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (decl)));
777 modesize = GET_MODE_BITSIZE (mode);
778 if (modesize >= 8 && modesize <= 256
779 && (modesize & (modesize - 1)) == 0)
781 if (align < modesize)
782 align = modesize;
784 str = TREE_STRING_POINTER (decl);
785 unit = GET_MODE_SIZE (mode);
787 /* Check for embedded NUL characters. */
788 for (i = 0; i < len; i += unit)
790 for (j = 0; j < unit; j++)
791 if (str[i + j] != '\0')
792 break;
793 if (j == unit)
794 break;
796 if (i == len - unit)
798 sprintf (name, "%s.str%d.%d", prefix,
799 modesize / 8, (int) (align / 8));
800 flags |= (modesize / 8) | SECTION_MERGE | SECTION_STRINGS;
801 return get_section (name, flags, NULL);
806 return readonly_data_section;
809 /* Return the section to use for constant merging. */
811 section *
812 mergeable_constant_section (enum machine_mode mode ATTRIBUTE_UNUSED,
813 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
814 unsigned int flags ATTRIBUTE_UNUSED)
816 unsigned int modesize = GET_MODE_BITSIZE (mode);
818 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
819 && mode != VOIDmode
820 && mode != BLKmode
821 && modesize <= align
822 && align >= 8
823 && align <= 256
824 && (align & (align - 1)) == 0)
826 const char *prefix = targetm.asm_out.mergeable_rodata_prefix;
827 char *name = (char *) alloca (strlen (prefix) + 30);
829 sprintf (name, "%s.cst%d", prefix, (int) (align / 8));
830 flags |= (align / 8) | SECTION_MERGE;
831 return get_section (name, flags, NULL);
833 return readonly_data_section;
836 /* Given NAME, a putative register name, discard any customary prefixes. */
838 static const char *
839 strip_reg_name (const char *name)
841 #ifdef REGISTER_PREFIX
842 if (!strncmp (name, REGISTER_PREFIX, strlen (REGISTER_PREFIX)))
843 name += strlen (REGISTER_PREFIX);
844 #endif
845 if (name[0] == '%' || name[0] == '#')
846 name++;
847 return name;
850 /* The user has asked for a DECL to have a particular name. Set (or
851 change) it in such a way that we don't prefix an underscore to
852 it. */
853 void
854 set_user_assembler_name (tree decl, const char *name)
856 char *starred = (char *) alloca (strlen (name) + 2);
857 starred[0] = '*';
858 strcpy (starred + 1, name);
859 symtab->change_decl_assembler_name (decl, get_identifier (starred));
860 SET_DECL_RTL (decl, NULL_RTX);
863 /* Decode an `asm' spec for a declaration as a register name.
864 Return the register number, or -1 if nothing specified,
865 or -2 if the ASMSPEC is not `cc' or `memory' and is not recognized,
866 or -3 if ASMSPEC is `cc' and is not recognized,
867 or -4 if ASMSPEC is `memory' and is not recognized.
868 Accept an exact spelling or a decimal number.
869 Prefixes such as % are optional. */
872 decode_reg_name_and_count (const char *asmspec, int *pnregs)
874 /* Presume just one register is clobbered. */
875 *pnregs = 1;
877 if (asmspec != 0)
879 int i;
881 /* Get rid of confusing prefixes. */
882 asmspec = strip_reg_name (asmspec);
884 /* Allow a decimal number as a "register name". */
885 for (i = strlen (asmspec) - 1; i >= 0; i--)
886 if (! ISDIGIT (asmspec[i]))
887 break;
888 if (asmspec[0] != 0 && i < 0)
890 i = atoi (asmspec);
891 if (i < FIRST_PSEUDO_REGISTER && i >= 0 && reg_names[i][0])
892 return i;
893 else
894 return -2;
897 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
898 if (reg_names[i][0]
899 && ! strcmp (asmspec, strip_reg_name (reg_names[i])))
900 return i;
902 #ifdef OVERLAPPING_REGISTER_NAMES
904 static const struct
906 const char *const name;
907 const int number;
908 const int nregs;
909 } table[] = OVERLAPPING_REGISTER_NAMES;
911 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
912 if (table[i].name[0]
913 && ! strcmp (asmspec, table[i].name))
915 *pnregs = table[i].nregs;
916 return table[i].number;
919 #endif /* OVERLAPPING_REGISTER_NAMES */
921 #ifdef ADDITIONAL_REGISTER_NAMES
923 static const struct { const char *const name; const int number; } table[]
924 = ADDITIONAL_REGISTER_NAMES;
926 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
927 if (table[i].name[0]
928 && ! strcmp (asmspec, table[i].name)
929 && reg_names[table[i].number][0])
930 return table[i].number;
932 #endif /* ADDITIONAL_REGISTER_NAMES */
934 if (!strcmp (asmspec, "memory"))
935 return -4;
937 if (!strcmp (asmspec, "cc"))
938 return -3;
940 return -2;
943 return -1;
947 decode_reg_name (const char *name)
949 int count;
950 return decode_reg_name_and_count (name, &count);
954 /* Return true if DECL's initializer is suitable for a BSS section. */
956 bool
957 bss_initializer_p (const_tree decl)
959 return (DECL_INITIAL (decl) == NULL
960 /* In LTO we have no errors in program; error_mark_node is used
961 to mark offlined constructors. */
962 || (DECL_INITIAL (decl) == error_mark_node
963 && !in_lto_p)
964 || (flag_zero_initialized_in_bss
965 /* Leave constant zeroes in .rodata so they
966 can be shared. */
967 && !TREE_READONLY (decl)
968 && initializer_zerop (DECL_INITIAL (decl))));
971 /* Compute the alignment of variable specified by DECL.
972 DONT_OUTPUT_DATA is from assemble_variable. */
974 void
975 align_variable (tree decl, bool dont_output_data)
977 unsigned int align = DECL_ALIGN (decl);
979 /* In the case for initialing an array whose length isn't specified,
980 where we have not yet been able to do the layout,
981 figure out the proper alignment now. */
982 if (dont_output_data && DECL_SIZE (decl) == 0
983 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
984 align = MAX (align, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (decl))));
986 /* Some object file formats have a maximum alignment which they support.
987 In particular, a.out format supports a maximum alignment of 4. */
988 if (align > MAX_OFILE_ALIGNMENT)
990 error ("alignment of %q+D is greater than maximum object "
991 "file alignment %d", decl,
992 MAX_OFILE_ALIGNMENT/BITS_PER_UNIT);
993 align = MAX_OFILE_ALIGNMENT;
996 if (! DECL_USER_ALIGN (decl))
998 #ifdef DATA_ABI_ALIGNMENT
999 unsigned int data_abi_align
1000 = DATA_ABI_ALIGNMENT (TREE_TYPE (decl), align);
1001 /* For backwards compatibility, don't assume the ABI alignment for
1002 TLS variables. */
1003 if (! DECL_THREAD_LOCAL_P (decl) || data_abi_align <= BITS_PER_WORD)
1004 align = data_abi_align;
1005 #endif
1007 /* On some machines, it is good to increase alignment sometimes.
1008 But as DECL_ALIGN is used both for actually emitting the variable
1009 and for code accessing the variable as guaranteed alignment, we
1010 can only increase the alignment if it is a performance optimization
1011 if the references to it must bind to the current definition. */
1012 if (decl_binds_to_current_def_p (decl)
1013 && !DECL_VIRTUAL_P (decl))
1015 #ifdef DATA_ALIGNMENT
1016 unsigned int data_align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
1017 /* Don't increase alignment too much for TLS variables - TLS space
1018 is too precious. */
1019 if (! DECL_THREAD_LOCAL_P (decl) || data_align <= BITS_PER_WORD)
1020 align = data_align;
1021 #endif
1022 #ifdef CONSTANT_ALIGNMENT
1023 if (DECL_INITIAL (decl) != 0
1024 /* In LTO we have no errors in program; error_mark_node is used
1025 to mark offlined constructors. */
1026 && (in_lto_p || DECL_INITIAL (decl) != error_mark_node))
1028 unsigned int const_align
1029 = CONSTANT_ALIGNMENT (DECL_INITIAL (decl), align);
1030 /* Don't increase alignment too much for TLS variables - TLS
1031 space is too precious. */
1032 if (! DECL_THREAD_LOCAL_P (decl) || const_align <= BITS_PER_WORD)
1033 align = const_align;
1035 #endif
1039 /* Reset the alignment in case we have made it tighter, so we can benefit
1040 from it in get_pointer_alignment. */
1041 DECL_ALIGN (decl) = align;
1044 /* Return DECL_ALIGN (decl), possibly increased for optimization purposes
1045 beyond what align_variable returned. */
1047 static unsigned int
1048 get_variable_align (tree decl)
1050 unsigned int align = DECL_ALIGN (decl);
1052 /* For user aligned vars or static vars align_variable already did
1053 everything. */
1054 if (DECL_USER_ALIGN (decl) || !TREE_PUBLIC (decl))
1055 return align;
1057 #ifdef DATA_ABI_ALIGNMENT
1058 if (DECL_THREAD_LOCAL_P (decl))
1059 align = DATA_ABI_ALIGNMENT (TREE_TYPE (decl), align);
1060 #endif
1062 /* For decls that bind to the current definition, align_variable
1063 did also everything, except for not assuming ABI required alignment
1064 of TLS variables. For other vars, increase the alignment here
1065 as an optimization. */
1066 if (!decl_binds_to_current_def_p (decl))
1068 /* On some machines, it is good to increase alignment sometimes. */
1069 #ifdef DATA_ALIGNMENT
1070 unsigned int data_align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
1071 /* Don't increase alignment too much for TLS variables - TLS space
1072 is too precious. */
1073 if (! DECL_THREAD_LOCAL_P (decl) || data_align <= BITS_PER_WORD)
1074 align = data_align;
1075 #endif
1076 #ifdef CONSTANT_ALIGNMENT
1077 if (DECL_INITIAL (decl) != 0
1078 /* In LTO we have no errors in program; error_mark_node is used
1079 to mark offlined constructors. */
1080 && (in_lto_p || DECL_INITIAL (decl) != error_mark_node))
1082 unsigned int const_align = CONSTANT_ALIGNMENT (DECL_INITIAL (decl),
1083 align);
1084 /* Don't increase alignment too much for TLS variables - TLS space
1085 is too precious. */
1086 if (! DECL_THREAD_LOCAL_P (decl) || const_align <= BITS_PER_WORD)
1087 align = const_align;
1089 #endif
1092 return align;
1095 /* Return the section into which the given VAR_DECL or CONST_DECL
1096 should be placed. PREFER_NOSWITCH_P is true if a noswitch
1097 section should be used wherever possible. */
1099 section *
1100 get_variable_section (tree decl, bool prefer_noswitch_p)
1102 addr_space_t as = ADDR_SPACE_GENERIC;
1103 int reloc;
1104 varpool_node *vnode = varpool_node::get (decl);
1105 if (vnode)
1107 vnode = vnode->ultimate_alias_target ();
1108 decl = vnode->decl;
1111 if (TREE_TYPE (decl) != error_mark_node)
1112 as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
1114 /* We need the constructor to figure out reloc flag. */
1115 if (vnode)
1116 vnode->get_constructor ();
1118 if (DECL_COMMON (decl))
1120 /* If the decl has been given an explicit section name, or it resides
1121 in a non-generic address space, then it isn't common, and shouldn't
1122 be handled as such. */
1123 gcc_assert (DECL_SECTION_NAME (decl) == NULL
1124 && ADDR_SPACE_GENERIC_P (as));
1125 if (DECL_THREAD_LOCAL_P (decl))
1126 return tls_comm_section;
1127 else if (TREE_PUBLIC (decl) && bss_initializer_p (decl))
1128 return comm_section;
1131 if (DECL_INITIAL (decl) == error_mark_node)
1132 reloc = contains_pointers_p (TREE_TYPE (decl)) ? 3 : 0;
1133 else if (DECL_INITIAL (decl))
1134 reloc = compute_reloc_for_constant (DECL_INITIAL (decl));
1135 else
1136 reloc = 0;
1138 resolve_unique_section (decl, reloc, flag_data_sections);
1139 if (IN_NAMED_SECTION (decl))
1140 return get_named_section (decl, NULL, reloc);
1142 if (ADDR_SPACE_GENERIC_P (as)
1143 && !DECL_THREAD_LOCAL_P (decl)
1144 && !(prefer_noswitch_p && targetm.have_switchable_bss_sections)
1145 && bss_initializer_p (decl))
1147 if (!TREE_PUBLIC (decl)
1148 && !((flag_sanitize & SANITIZE_ADDRESS)
1149 && asan_protect_global (decl)))
1150 return lcomm_section;
1151 if (bss_noswitch_section)
1152 return bss_noswitch_section;
1155 return targetm.asm_out.select_section (decl, reloc,
1156 get_variable_align (decl));
1159 /* Return the block into which object_block DECL should be placed. */
1161 static struct object_block *
1162 get_block_for_decl (tree decl)
1164 section *sect;
1166 if (TREE_CODE (decl) == VAR_DECL)
1168 /* The object must be defined in this translation unit. */
1169 if (DECL_EXTERNAL (decl))
1170 return NULL;
1172 /* There's no point using object blocks for something that is
1173 isolated by definition. */
1174 if (DECL_COMDAT_GROUP (decl))
1175 return NULL;
1178 /* We can only calculate block offsets if the decl has a known
1179 constant size. */
1180 if (DECL_SIZE_UNIT (decl) == NULL)
1181 return NULL;
1182 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (decl)))
1183 return NULL;
1185 /* Find out which section should contain DECL. We cannot put it into
1186 an object block if it requires a standalone definition. */
1187 if (TREE_CODE (decl) == VAR_DECL)
1188 align_variable (decl, 0);
1189 sect = get_variable_section (decl, true);
1190 if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
1191 return NULL;
1193 return get_block_for_section (sect);
1196 /* Make sure block symbol SYMBOL is in block BLOCK. */
1198 static void
1199 change_symbol_block (rtx symbol, struct object_block *block)
1201 if (block != SYMBOL_REF_BLOCK (symbol))
1203 gcc_assert (SYMBOL_REF_BLOCK_OFFSET (symbol) < 0);
1204 SYMBOL_REF_BLOCK (symbol) = block;
1208 /* Return true if it is possible to put DECL in an object_block. */
1210 static bool
1211 use_blocks_for_decl_p (tree decl)
1213 struct symtab_node *snode;
1215 /* Only data DECLs can be placed into object blocks. */
1216 if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != CONST_DECL)
1217 return false;
1219 /* Detect decls created by dw2_force_const_mem. Such decls are
1220 special because DECL_INITIAL doesn't specify the decl's true value.
1221 dw2_output_indirect_constants will instead call assemble_variable
1222 with dont_output_data set to 1 and then print the contents itself. */
1223 if (DECL_INITIAL (decl) == decl)
1224 return false;
1226 /* If this decl is an alias, then we don't want to emit a
1227 definition. */
1228 if (TREE_CODE (decl) == VAR_DECL
1229 && (snode = symtab_node::get (decl)) != NULL
1230 && snode->alias)
1231 return false;
1233 return targetm.use_blocks_for_decl_p (decl);
1236 /* Create the DECL_RTL for a VAR_DECL or FUNCTION_DECL. DECL should
1237 have static storage duration. In other words, it should not be an
1238 automatic variable, including PARM_DECLs.
1240 There is, however, one exception: this function handles variables
1241 explicitly placed in a particular register by the user.
1243 This is never called for PARM_DECL nodes. */
1245 void
1246 make_decl_rtl (tree decl)
1248 const char *name = 0;
1249 int reg_number;
1250 rtx x;
1252 /* Check that we are not being given an automatic variable. */
1253 gcc_assert (TREE_CODE (decl) != PARM_DECL
1254 && TREE_CODE (decl) != RESULT_DECL);
1256 /* A weak alias has TREE_PUBLIC set but not the other bits. */
1257 gcc_assert (TREE_CODE (decl) != VAR_DECL
1258 || TREE_STATIC (decl)
1259 || TREE_PUBLIC (decl)
1260 || DECL_EXTERNAL (decl)
1261 || DECL_REGISTER (decl));
1263 /* And that we were not given a type or a label. */
1264 gcc_assert (TREE_CODE (decl) != TYPE_DECL
1265 && TREE_CODE (decl) != LABEL_DECL);
1267 /* For a duplicate declaration, we can be called twice on the
1268 same DECL node. Don't discard the RTL already made. */
1269 if (DECL_RTL_SET_P (decl))
1271 /* If the old RTL had the wrong mode, fix the mode. */
1272 x = DECL_RTL (decl);
1273 if (GET_MODE (x) != DECL_MODE (decl))
1274 SET_DECL_RTL (decl, adjust_address_nv (x, DECL_MODE (decl), 0));
1276 if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1277 return;
1279 /* ??? Another way to do this would be to maintain a hashed
1280 table of such critters. Instead of adding stuff to a DECL
1281 to give certain attributes to it, we could use an external
1282 hash map from DECL to set of attributes. */
1284 /* Let the target reassign the RTL if it wants.
1285 This is necessary, for example, when one machine specific
1286 decl attribute overrides another. */
1287 targetm.encode_section_info (decl, DECL_RTL (decl), false);
1289 /* If the symbol has a SYMBOL_REF_BLOCK field, update it based
1290 on the new decl information. */
1291 if (MEM_P (x)
1292 && GET_CODE (XEXP (x, 0)) == SYMBOL_REF
1293 && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (x, 0)))
1294 change_symbol_block (XEXP (x, 0), get_block_for_decl (decl));
1296 return;
1299 /* If this variable belongs to the global constant pool, retrieve the
1300 pre-computed RTL or recompute it in LTO mode. */
1301 if (TREE_CODE (decl) == VAR_DECL && DECL_IN_CONSTANT_POOL (decl))
1303 SET_DECL_RTL (decl, output_constant_def (DECL_INITIAL (decl), 1));
1304 return;
1307 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
1309 if (name[0] != '*' && TREE_CODE (decl) != FUNCTION_DECL
1310 && DECL_REGISTER (decl))
1312 error ("register name not specified for %q+D", decl);
1314 else if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1316 const char *asmspec = name+1;
1317 enum machine_mode mode = DECL_MODE (decl);
1318 reg_number = decode_reg_name (asmspec);
1319 /* First detect errors in declaring global registers. */
1320 if (reg_number == -1)
1321 error ("register name not specified for %q+D", decl);
1322 else if (reg_number < 0)
1323 error ("invalid register name for %q+D", decl);
1324 else if (mode == BLKmode)
1325 error ("data type of %q+D isn%'t suitable for a register",
1326 decl);
1327 else if (!in_hard_reg_set_p (accessible_reg_set, mode, reg_number))
1328 error ("the register specified for %q+D cannot be accessed"
1329 " by the current target", decl);
1330 else if (!in_hard_reg_set_p (operand_reg_set, mode, reg_number))
1331 error ("the register specified for %q+D is not general enough"
1332 " to be used as a register variable", decl);
1333 else if (!HARD_REGNO_MODE_OK (reg_number, mode))
1334 error ("register specified for %q+D isn%'t suitable for data type",
1335 decl);
1336 /* Now handle properly declared static register variables. */
1337 else
1339 int nregs;
1341 if (DECL_INITIAL (decl) != 0 && TREE_STATIC (decl))
1343 DECL_INITIAL (decl) = 0;
1344 error ("global register variable has initial value");
1346 if (TREE_THIS_VOLATILE (decl))
1347 warning (OPT_Wvolatile_register_var,
1348 "optimization may eliminate reads and/or "
1349 "writes to register variables");
1351 /* If the user specified one of the eliminables registers here,
1352 e.g., FRAME_POINTER_REGNUM, we don't want to get this variable
1353 confused with that register and be eliminated. This usage is
1354 somewhat suspect... */
1356 SET_DECL_RTL (decl, gen_rtx_raw_REG (mode, reg_number));
1357 ORIGINAL_REGNO (DECL_RTL (decl)) = reg_number;
1358 REG_USERVAR_P (DECL_RTL (decl)) = 1;
1360 if (TREE_STATIC (decl))
1362 /* Make this register global, so not usable for anything
1363 else. */
1364 #ifdef ASM_DECLARE_REGISTER_GLOBAL
1365 name = IDENTIFIER_POINTER (DECL_NAME (decl));
1366 ASM_DECLARE_REGISTER_GLOBAL (asm_out_file, decl, reg_number, name);
1367 #endif
1368 nregs = hard_regno_nregs[reg_number][mode];
1369 while (nregs > 0)
1370 globalize_reg (decl, reg_number + --nregs);
1373 /* As a register variable, it has no section. */
1374 return;
1376 /* Avoid internal errors from invalid register
1377 specifications. */
1378 SET_DECL_ASSEMBLER_NAME (decl, NULL_TREE);
1379 DECL_HARD_REGISTER (decl) = 0;
1380 return;
1382 /* Now handle ordinary static variables and functions (in memory).
1383 Also handle vars declared register invalidly. */
1384 else if (name[0] == '*')
1386 #ifdef REGISTER_PREFIX
1387 if (strlen (REGISTER_PREFIX) != 0)
1389 reg_number = decode_reg_name (name);
1390 if (reg_number >= 0 || reg_number == -3)
1391 error ("register name given for non-register variable %q+D", decl);
1393 #endif
1396 /* Specifying a section attribute on a variable forces it into a
1397 non-.bss section, and thus it cannot be common. */
1398 /* FIXME: In general this code should not be necessary because
1399 visibility pass is doing the same work. But notice_global_symbol
1400 is called early and it needs to make DECL_RTL to get the name.
1401 we take care of recomputing the DECL_RTL after visibility is changed. */
1402 if (TREE_CODE (decl) == VAR_DECL
1403 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
1404 && DECL_SECTION_NAME (decl) != NULL
1405 && DECL_INITIAL (decl) == NULL_TREE
1406 && DECL_COMMON (decl))
1407 DECL_COMMON (decl) = 0;
1409 /* Variables can't be both common and weak. */
1410 if (TREE_CODE (decl) == VAR_DECL && DECL_WEAK (decl))
1411 DECL_COMMON (decl) = 0;
1413 if (use_object_blocks_p () && use_blocks_for_decl_p (decl))
1414 x = create_block_symbol (name, get_block_for_decl (decl), -1);
1415 else
1417 enum machine_mode address_mode = Pmode;
1418 if (TREE_TYPE (decl) != error_mark_node)
1420 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
1421 address_mode = targetm.addr_space.address_mode (as);
1423 x = gen_rtx_SYMBOL_REF (address_mode, name);
1425 SYMBOL_REF_WEAK (x) = DECL_WEAK (decl);
1426 SET_SYMBOL_REF_DECL (x, decl);
1428 x = gen_rtx_MEM (DECL_MODE (decl), x);
1429 if (TREE_CODE (decl) != FUNCTION_DECL)
1430 set_mem_attributes (x, decl, 1);
1431 SET_DECL_RTL (decl, x);
1433 /* Optionally set flags or add text to the name to record information
1434 such as that it is a function name.
1435 If the name is changed, the macro ASM_OUTPUT_LABELREF
1436 will have to know how to strip this information. */
1437 targetm.encode_section_info (decl, DECL_RTL (decl), true);
1440 /* Like make_decl_rtl, but inhibit creation of new alias sets when
1441 calling make_decl_rtl. Also, reset DECL_RTL before returning the
1442 rtl. */
1445 make_decl_rtl_for_debug (tree decl)
1447 unsigned int save_aliasing_flag;
1448 rtx rtl;
1450 if (DECL_RTL_SET_P (decl))
1451 return DECL_RTL (decl);
1453 /* Kludge alert! Somewhere down the call chain, make_decl_rtl will
1454 call new_alias_set. If running with -fcompare-debug, sometimes
1455 we do not want to create alias sets that will throw the alias
1456 numbers off in the comparison dumps. So... clearing
1457 flag_strict_aliasing will keep new_alias_set() from creating a
1458 new set. */
1459 save_aliasing_flag = flag_strict_aliasing;
1460 flag_strict_aliasing = 0;
1462 rtl = DECL_RTL (decl);
1463 /* Reset DECL_RTL back, as various parts of the compiler expects
1464 DECL_RTL set meaning it is actually going to be output. */
1465 SET_DECL_RTL (decl, NULL);
1467 flag_strict_aliasing = save_aliasing_flag;
1468 return rtl;
1471 /* Output a string of literal assembler code
1472 for an `asm' keyword used between functions. */
1474 void
1475 assemble_asm (tree string)
1477 const char *p;
1478 app_enable ();
1480 if (TREE_CODE (string) == ADDR_EXPR)
1481 string = TREE_OPERAND (string, 0);
1483 p = TREE_STRING_POINTER (string);
1484 fprintf (asm_out_file, "%s%s\n", p[0] == '\t' ? "" : "\t", p);
1487 /* Write the address of the entity given by SYMBOL to SEC. */
1488 void
1489 assemble_addr_to_section (rtx symbol, section *sec)
1491 switch_to_section (sec);
1492 assemble_align (POINTER_SIZE);
1493 assemble_integer (symbol, POINTER_SIZE_UNITS, POINTER_SIZE, 1);
1496 /* Return the numbered .ctors.N (if CONSTRUCTOR_P) or .dtors.N (if
1497 not) section for PRIORITY. */
1498 section *
1499 get_cdtor_priority_section (int priority, bool constructor_p)
1501 char buf[16];
1503 /* ??? This only works reliably with the GNU linker. */
1504 sprintf (buf, "%s.%.5u",
1505 constructor_p ? ".ctors" : ".dtors",
1506 /* Invert the numbering so the linker puts us in the proper
1507 order; constructors are run from right to left, and the
1508 linker sorts in increasing order. */
1509 MAX_INIT_PRIORITY - priority);
1510 return get_section (buf, SECTION_WRITE, NULL);
1513 void
1514 default_named_section_asm_out_destructor (rtx symbol, int priority)
1516 section *sec;
1518 if (priority != DEFAULT_INIT_PRIORITY)
1519 sec = get_cdtor_priority_section (priority,
1520 /*constructor_p=*/false);
1521 else
1522 sec = get_section (".dtors", SECTION_WRITE, NULL);
1524 assemble_addr_to_section (symbol, sec);
1527 #ifdef DTORS_SECTION_ASM_OP
1528 void
1529 default_dtor_section_asm_out_destructor (rtx symbol,
1530 int priority ATTRIBUTE_UNUSED)
1532 assemble_addr_to_section (symbol, dtors_section);
1534 #endif
1536 void
1537 default_named_section_asm_out_constructor (rtx symbol, int priority)
1539 section *sec;
1541 if (priority != DEFAULT_INIT_PRIORITY)
1542 sec = get_cdtor_priority_section (priority,
1543 /*constructor_p=*/true);
1544 else
1545 sec = get_section (".ctors", SECTION_WRITE, NULL);
1547 assemble_addr_to_section (symbol, sec);
1550 #ifdef CTORS_SECTION_ASM_OP
1551 void
1552 default_ctor_section_asm_out_constructor (rtx symbol,
1553 int priority ATTRIBUTE_UNUSED)
1555 assemble_addr_to_section (symbol, ctors_section);
1557 #endif
1559 /* CONSTANT_POOL_BEFORE_FUNCTION may be defined as an expression with
1560 a nonzero value if the constant pool should be output before the
1561 start of the function, or a zero value if the pool should output
1562 after the end of the function. The default is to put it before the
1563 start. */
1565 #ifndef CONSTANT_POOL_BEFORE_FUNCTION
1566 #define CONSTANT_POOL_BEFORE_FUNCTION 1
1567 #endif
1569 /* DECL is an object (either VAR_DECL or FUNCTION_DECL) which is going
1570 to be output to assembler.
1571 Set first_global_object_name and weak_global_object_name as appropriate. */
1573 void
1574 notice_global_symbol (tree decl)
1576 const char **type = &first_global_object_name;
1578 if (first_global_object_name
1579 || !TREE_PUBLIC (decl)
1580 || DECL_EXTERNAL (decl)
1581 || !DECL_NAME (decl)
1582 || (TREE_CODE (decl) != FUNCTION_DECL
1583 && (TREE_CODE (decl) != VAR_DECL
1584 || (DECL_COMMON (decl)
1585 && (DECL_INITIAL (decl) == 0
1586 || DECL_INITIAL (decl) == error_mark_node))))
1587 || !MEM_P (DECL_RTL (decl)))
1588 return;
1590 /* We win when global object is found, but it is useful to know about weak
1591 symbol as well so we can produce nicer unique names. */
1592 if (DECL_WEAK (decl) || DECL_ONE_ONLY (decl) || flag_shlib)
1593 type = &weak_global_object_name;
1595 if (!*type)
1597 const char *p;
1598 const char *name;
1599 rtx decl_rtl = DECL_RTL (decl);
1601 p = targetm.strip_name_encoding (XSTR (XEXP (decl_rtl, 0), 0));
1602 name = ggc_strdup (p);
1604 *type = name;
1608 /* If not using flag_reorder_blocks_and_partition, decide early whether the
1609 current function goes into the cold section, so that targets can use
1610 current_function_section during RTL expansion. DECL describes the
1611 function. */
1613 void
1614 decide_function_section (tree decl)
1616 first_function_block_is_cold = false;
1618 if (flag_reorder_blocks_and_partition)
1619 /* We will decide in assemble_start_function. */
1620 return;
1622 if (DECL_SECTION_NAME (decl))
1624 struct cgraph_node *node = cgraph_node::get (current_function_decl);
1625 /* Calls to function_section rely on first_function_block_is_cold
1626 being accurate. */
1627 first_function_block_is_cold = (node
1628 && node->frequency
1629 == NODE_FREQUENCY_UNLIKELY_EXECUTED);
1632 in_cold_section_p = first_function_block_is_cold;
1635 /* Output assembler code for the constant pool of a function and associated
1636 with defining the name of the function. DECL describes the function.
1637 NAME is the function's name. For the constant pool, we use the current
1638 constant pool data. */
1640 void
1641 assemble_start_function (tree decl, const char *fnname)
1643 int align;
1644 char tmp_label[100];
1645 bool hot_label_written = false;
1647 if (flag_reorder_blocks_and_partition)
1649 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTB", const_labelno);
1650 crtl->subsections.hot_section_label = ggc_strdup (tmp_label);
1651 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDB", const_labelno);
1652 crtl->subsections.cold_section_label = ggc_strdup (tmp_label);
1653 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTE", const_labelno);
1654 crtl->subsections.hot_section_end_label = ggc_strdup (tmp_label);
1655 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDE", const_labelno);
1656 crtl->subsections.cold_section_end_label = ggc_strdup (tmp_label);
1657 const_labelno++;
1659 else
1661 crtl->subsections.hot_section_label = NULL;
1662 crtl->subsections.cold_section_label = NULL;
1663 crtl->subsections.hot_section_end_label = NULL;
1664 crtl->subsections.cold_section_end_label = NULL;
1667 /* The following code does not need preprocessing in the assembler. */
1669 app_disable ();
1671 if (CONSTANT_POOL_BEFORE_FUNCTION)
1672 output_constant_pool (fnname, decl);
1674 /* Make sure the not and cold text (code) sections are properly
1675 aligned. This is necessary here in the case where the function
1676 has both hot and cold sections, because we don't want to re-set
1677 the alignment when the section switch happens mid-function. */
1679 if (flag_reorder_blocks_and_partition)
1681 first_function_block_is_cold = false;
1683 switch_to_section (unlikely_text_section ());
1684 assemble_align (DECL_ALIGN (decl));
1685 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_label);
1687 /* When the function starts with a cold section, we need to explicitly
1688 align the hot section and write out the hot section label.
1689 But if the current function is a thunk, we do not have a CFG. */
1690 if (!cfun->is_thunk
1691 && BB_PARTITION (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb) == BB_COLD_PARTITION)
1693 switch_to_section (text_section);
1694 assemble_align (DECL_ALIGN (decl));
1695 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
1696 hot_label_written = true;
1697 first_function_block_is_cold = true;
1699 in_cold_section_p = first_function_block_is_cold;
1703 /* Switch to the correct text section for the start of the function. */
1705 switch_to_section (function_section (decl));
1706 if (flag_reorder_blocks_and_partition
1707 && !hot_label_written)
1708 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
1710 /* Tell assembler to move to target machine's alignment for functions. */
1711 align = floor_log2 (DECL_ALIGN (decl) / BITS_PER_UNIT);
1712 if (align > 0)
1714 ASM_OUTPUT_ALIGN (asm_out_file, align);
1717 /* Handle a user-specified function alignment.
1718 Note that we still need to align to DECL_ALIGN, as above,
1719 because ASM_OUTPUT_MAX_SKIP_ALIGN might not do any alignment at all. */
1720 if (! DECL_USER_ALIGN (decl)
1721 && align_functions_log > align
1722 && optimize_function_for_speed_p (cfun))
1724 #ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
1725 ASM_OUTPUT_MAX_SKIP_ALIGN (asm_out_file,
1726 align_functions_log, align_functions - 1);
1727 #else
1728 ASM_OUTPUT_ALIGN (asm_out_file, align_functions_log);
1729 #endif
1732 #ifdef ASM_OUTPUT_FUNCTION_PREFIX
1733 ASM_OUTPUT_FUNCTION_PREFIX (asm_out_file, fnname);
1734 #endif
1736 if (!DECL_IGNORED_P (decl))
1737 (*debug_hooks->begin_function) (decl);
1739 /* Make function name accessible from other files, if appropriate. */
1741 if (TREE_PUBLIC (decl))
1743 notice_global_symbol (decl);
1745 globalize_decl (decl);
1747 maybe_assemble_visibility (decl);
1750 if (DECL_PRESERVE_P (decl))
1751 targetm.asm_out.mark_decl_preserved (fnname);
1753 /* Do any machine/system dependent processing of the function name. */
1754 #ifdef ASM_DECLARE_FUNCTION_NAME
1755 ASM_DECLARE_FUNCTION_NAME (asm_out_file, fnname, current_function_decl);
1756 #else
1757 /* Standard thing is just output label for the function. */
1758 ASM_OUTPUT_FUNCTION_LABEL (asm_out_file, fnname, current_function_decl);
1759 #endif /* ASM_DECLARE_FUNCTION_NAME */
1761 if (lookup_attribute ("no_split_stack", DECL_ATTRIBUTES (decl)))
1762 saw_no_split_stack = true;
1765 /* Output assembler code associated with defining the size of the
1766 function. DECL describes the function. NAME is the function's name. */
1768 void
1769 assemble_end_function (tree decl, const char *fnname ATTRIBUTE_UNUSED)
1771 #ifdef ASM_DECLARE_FUNCTION_SIZE
1772 /* We could have switched section in the middle of the function. */
1773 if (flag_reorder_blocks_and_partition)
1774 switch_to_section (function_section (decl));
1775 ASM_DECLARE_FUNCTION_SIZE (asm_out_file, fnname, decl);
1776 #endif
1777 if (! CONSTANT_POOL_BEFORE_FUNCTION)
1779 output_constant_pool (fnname, decl);
1780 switch_to_section (function_section (decl)); /* need to switch back */
1782 /* Output labels for end of hot/cold text sections (to be used by
1783 debug info.) */
1784 if (flag_reorder_blocks_and_partition)
1786 section *save_text_section;
1788 save_text_section = in_section;
1789 switch_to_section (unlikely_text_section ());
1790 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_end_label);
1791 if (first_function_block_is_cold)
1792 switch_to_section (text_section);
1793 else
1794 switch_to_section (function_section (decl));
1795 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_end_label);
1796 switch_to_section (save_text_section);
1800 /* Assemble code to leave SIZE bytes of zeros. */
1802 void
1803 assemble_zeros (unsigned HOST_WIDE_INT size)
1805 /* Do no output if -fsyntax-only. */
1806 if (flag_syntax_only)
1807 return;
1809 #ifdef ASM_NO_SKIP_IN_TEXT
1810 /* The `space' pseudo in the text section outputs nop insns rather than 0s,
1811 so we must output 0s explicitly in the text section. */
1812 if (ASM_NO_SKIP_IN_TEXT && (in_section->common.flags & SECTION_CODE) != 0)
1814 unsigned HOST_WIDE_INT i;
1815 for (i = 0; i < size; i++)
1816 assemble_integer (const0_rtx, 1, BITS_PER_UNIT, 1);
1818 else
1819 #endif
1820 if (size > 0)
1821 ASM_OUTPUT_SKIP (asm_out_file, size);
1824 /* Assemble an alignment pseudo op for an ALIGN-bit boundary. */
1826 void
1827 assemble_align (int align)
1829 if (align > BITS_PER_UNIT)
1831 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
1835 /* Assemble a string constant with the specified C string as contents. */
1837 void
1838 assemble_string (const char *p, int size)
1840 int pos = 0;
1841 int maximum = 2000;
1843 /* If the string is very long, split it up. */
1845 while (pos < size)
1847 int thissize = size - pos;
1848 if (thissize > maximum)
1849 thissize = maximum;
1851 ASM_OUTPUT_ASCII (asm_out_file, p, thissize);
1853 pos += thissize;
1854 p += thissize;
1859 /* A noswitch_section_callback for lcomm_section. */
1861 static bool
1862 emit_local (tree decl ATTRIBUTE_UNUSED,
1863 const char *name ATTRIBUTE_UNUSED,
1864 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1865 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1867 #if defined ASM_OUTPUT_ALIGNED_DECL_LOCAL
1868 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, decl, name,
1869 size, DECL_ALIGN (decl));
1870 return true;
1871 #elif defined ASM_OUTPUT_ALIGNED_LOCAL
1872 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, DECL_ALIGN (decl));
1873 return true;
1874 #else
1875 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
1876 return false;
1877 #endif
1880 /* A noswitch_section_callback for bss_noswitch_section. */
1882 #if defined ASM_OUTPUT_ALIGNED_BSS
1883 static bool
1884 emit_bss (tree decl ATTRIBUTE_UNUSED,
1885 const char *name ATTRIBUTE_UNUSED,
1886 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1887 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1889 #if defined ASM_OUTPUT_ALIGNED_BSS
1890 ASM_OUTPUT_ALIGNED_BSS (asm_out_file, decl, name, size,
1891 get_variable_align (decl));
1892 return true;
1893 #endif
1895 #endif
1897 /* A noswitch_section_callback for comm_section. */
1899 static bool
1900 emit_common (tree decl ATTRIBUTE_UNUSED,
1901 const char *name ATTRIBUTE_UNUSED,
1902 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1903 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1905 #if defined ASM_OUTPUT_ALIGNED_DECL_COMMON
1906 ASM_OUTPUT_ALIGNED_DECL_COMMON (asm_out_file, decl, name,
1907 size, get_variable_align (decl));
1908 return true;
1909 #elif defined ASM_OUTPUT_ALIGNED_COMMON
1910 ASM_OUTPUT_ALIGNED_COMMON (asm_out_file, name, size,
1911 get_variable_align (decl));
1912 return true;
1913 #else
1914 ASM_OUTPUT_COMMON (asm_out_file, name, size, rounded);
1915 return false;
1916 #endif
1919 /* A noswitch_section_callback for tls_comm_section. */
1921 static bool
1922 emit_tls_common (tree decl ATTRIBUTE_UNUSED,
1923 const char *name ATTRIBUTE_UNUSED,
1924 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1925 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1927 #ifdef ASM_OUTPUT_TLS_COMMON
1928 ASM_OUTPUT_TLS_COMMON (asm_out_file, decl, name, size);
1929 return true;
1930 #else
1931 sorry ("thread-local COMMON data not implemented");
1932 return true;
1933 #endif
1936 /* Assemble DECL given that it belongs in SECTION_NOSWITCH section SECT.
1937 NAME is the name of DECL's SYMBOL_REF. */
1939 static void
1940 assemble_noswitch_variable (tree decl, const char *name, section *sect,
1941 unsigned int align)
1943 unsigned HOST_WIDE_INT size, rounded;
1945 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
1946 rounded = size;
1948 if ((flag_sanitize & SANITIZE_ADDRESS) && asan_protect_global (decl))
1949 size += asan_red_zone_size (size);
1951 /* Don't allocate zero bytes of common,
1952 since that means "undefined external" in the linker. */
1953 if (size == 0)
1954 rounded = 1;
1956 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
1957 so that each uninitialized object starts on such a boundary. */
1958 rounded += (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1;
1959 rounded = (rounded / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
1960 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
1962 if (!sect->noswitch.callback (decl, name, size, rounded)
1963 && (unsigned HOST_WIDE_INT) (align / BITS_PER_UNIT) > rounded)
1964 error ("requested alignment for %q+D is greater than "
1965 "implemented alignment of %wu", decl, rounded);
1968 /* A subroutine of assemble_variable. Output the label and contents of
1969 DECL, whose address is a SYMBOL_REF with name NAME. DONT_OUTPUT_DATA
1970 is as for assemble_variable. */
1972 static void
1973 assemble_variable_contents (tree decl, const char *name,
1974 bool dont_output_data)
1976 /* Do any machine/system dependent processing of the object. */
1977 #ifdef ASM_DECLARE_OBJECT_NAME
1978 last_assemble_variable_decl = decl;
1979 ASM_DECLARE_OBJECT_NAME (asm_out_file, name, decl);
1980 #else
1981 /* Standard thing is just output label for the object. */
1982 ASM_OUTPUT_LABEL (asm_out_file, name);
1983 #endif /* ASM_DECLARE_OBJECT_NAME */
1985 if (!dont_output_data)
1987 /* Caller is supposed to use varpool_get_constructor when it wants
1988 to output the body. */
1989 gcc_assert (!in_lto_p || DECL_INITIAL (decl) != error_mark_node);
1990 if (DECL_INITIAL (decl)
1991 && DECL_INITIAL (decl) != error_mark_node
1992 && !initializer_zerop (DECL_INITIAL (decl)))
1993 /* Output the actual data. */
1994 output_constant (DECL_INITIAL (decl),
1995 tree_to_uhwi (DECL_SIZE_UNIT (decl)),
1996 get_variable_align (decl));
1997 else
1998 /* Leave space for it. */
1999 assemble_zeros (tree_to_uhwi (DECL_SIZE_UNIT (decl)));
2003 /* Assemble everything that is needed for a variable or function declaration.
2004 Not used for automatic variables, and not used for function definitions.
2005 Should not be called for variables of incomplete structure type.
2007 TOP_LEVEL is nonzero if this variable has file scope.
2008 AT_END is nonzero if this is the special handling, at end of compilation,
2009 to define things that have had only tentative definitions.
2010 DONT_OUTPUT_DATA if nonzero means don't actually output the
2011 initial value (that will be done by the caller). */
2013 void
2014 assemble_variable (tree decl, int top_level ATTRIBUTE_UNUSED,
2015 int at_end ATTRIBUTE_UNUSED, int dont_output_data)
2017 const char *name;
2018 rtx decl_rtl, symbol;
2019 section *sect;
2020 unsigned int align;
2021 bool asan_protected = false;
2023 /* This function is supposed to handle VARIABLES. Ensure we have one. */
2024 gcc_assert (TREE_CODE (decl) == VAR_DECL);
2026 /* Emulated TLS had better not get this far. */
2027 gcc_checking_assert (targetm.have_tls || !DECL_THREAD_LOCAL_P (decl));
2029 last_assemble_variable_decl = 0;
2031 /* Normally no need to say anything here for external references,
2032 since assemble_external is called by the language-specific code
2033 when a declaration is first seen. */
2035 if (DECL_EXTERNAL (decl))
2036 return;
2038 /* Do nothing for global register variables. */
2039 if (DECL_RTL_SET_P (decl) && REG_P (DECL_RTL (decl)))
2041 TREE_ASM_WRITTEN (decl) = 1;
2042 return;
2045 /* If type was incomplete when the variable was declared,
2046 see if it is complete now. */
2048 if (DECL_SIZE (decl) == 0)
2049 layout_decl (decl, 0);
2051 /* Still incomplete => don't allocate it; treat the tentative defn
2052 (which is what it must have been) as an `extern' reference. */
2054 if (!dont_output_data && DECL_SIZE (decl) == 0)
2056 error ("storage size of %q+D isn%'t known", decl);
2057 TREE_ASM_WRITTEN (decl) = 1;
2058 return;
2061 /* The first declaration of a variable that comes through this function
2062 decides whether it is global (in C, has external linkage)
2063 or local (in C, has internal linkage). So do nothing more
2064 if this function has already run. */
2066 if (TREE_ASM_WRITTEN (decl))
2067 return;
2069 /* Make sure targetm.encode_section_info is invoked before we set
2070 ASM_WRITTEN. */
2071 decl_rtl = DECL_RTL (decl);
2073 TREE_ASM_WRITTEN (decl) = 1;
2075 /* Do no output if -fsyntax-only. */
2076 if (flag_syntax_only)
2077 return;
2079 if (! dont_output_data
2080 && ! valid_constant_size_p (DECL_SIZE_UNIT (decl)))
2082 error ("size of variable %q+D is too large", decl);
2083 return;
2086 gcc_assert (MEM_P (decl_rtl));
2087 gcc_assert (GET_CODE (XEXP (decl_rtl, 0)) == SYMBOL_REF);
2088 symbol = XEXP (decl_rtl, 0);
2090 /* If this symbol belongs to the tree constant pool, output the constant
2091 if it hasn't already been written. */
2092 if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
2094 tree decl = SYMBOL_REF_DECL (symbol);
2095 if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
2096 output_constant_def_contents (symbol);
2097 return;
2100 app_disable ();
2102 name = XSTR (symbol, 0);
2103 if (TREE_PUBLIC (decl) && DECL_NAME (decl))
2104 notice_global_symbol (decl);
2106 /* Compute the alignment of this data. */
2108 align_variable (decl, dont_output_data);
2110 if ((flag_sanitize & SANITIZE_ADDRESS)
2111 && asan_protect_global (decl))
2113 asan_protected = true;
2114 DECL_ALIGN (decl) = MAX (DECL_ALIGN (decl),
2115 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
2118 set_mem_align (decl_rtl, DECL_ALIGN (decl));
2120 align = get_variable_align (decl);
2122 if (TREE_PUBLIC (decl))
2123 maybe_assemble_visibility (decl);
2125 if (DECL_PRESERVE_P (decl))
2126 targetm.asm_out.mark_decl_preserved (name);
2128 /* First make the assembler name(s) global if appropriate. */
2129 sect = get_variable_section (decl, false);
2130 if (TREE_PUBLIC (decl)
2131 && (sect->common.flags & SECTION_COMMON) == 0)
2132 globalize_decl (decl);
2134 /* Output any data that we will need to use the address of. */
2135 if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node)
2136 output_addressed_constants (DECL_INITIAL (decl));
2138 /* dbxout.c needs to know this. */
2139 if (sect && (sect->common.flags & SECTION_CODE) != 0)
2140 DECL_IN_TEXT_SECTION (decl) = 1;
2142 /* If the decl is part of an object_block, make sure that the decl
2143 has been positioned within its block, but do not write out its
2144 definition yet. output_object_blocks will do that later. */
2145 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
2147 gcc_assert (!dont_output_data);
2148 place_block_symbol (symbol);
2150 else if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
2151 assemble_noswitch_variable (decl, name, sect, align);
2152 else
2154 /* The following bit of code ensures that vtable_map
2155 variables are not only in the comdat section, but that
2156 each variable has its own unique comdat name. If this
2157 code is removed, the variables end up in the same section
2158 with a single comdat name.
2160 FIXME: resolve_unique_section needs to deal better with
2161 decls with both DECL_SECTION_NAME and DECL_ONE_ONLY. Once
2162 that is fixed, this if-else statement can be replaced with
2163 a single call to "switch_to_section (sect)". */
2164 if (sect->named.name
2165 && (strcmp (sect->named.name, ".vtable_map_vars") == 0))
2167 #if defined (OBJECT_FORMAT_ELF)
2168 targetm.asm_out.named_section (sect->named.name,
2169 sect->named.common.flags
2170 | SECTION_LINKONCE,
2171 DECL_NAME (decl));
2172 in_section = sect;
2173 #else
2174 switch_to_section (sect);
2175 #endif
2177 else
2178 switch_to_section (sect);
2179 if (align > BITS_PER_UNIT)
2180 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
2181 assemble_variable_contents (decl, name, dont_output_data);
2182 if (asan_protected)
2184 unsigned HOST_WIDE_INT int size
2185 = tree_to_uhwi (DECL_SIZE_UNIT (decl));
2186 assemble_zeros (asan_red_zone_size (size));
2192 /* Given a function declaration (FN_DECL), this function assembles the
2193 function into the .preinit_array section. */
2195 void
2196 assemble_vtv_preinit_initializer (tree fn_decl)
2198 section *sect;
2199 unsigned flags = SECTION_WRITE;
2200 rtx symbol = XEXP (DECL_RTL (fn_decl), 0);
2202 flags |= SECTION_NOTYPE;
2203 sect = get_section (".preinit_array", flags, fn_decl);
2204 switch_to_section (sect);
2205 assemble_addr_to_section (symbol, sect);
2208 /* Return 1 if type TYPE contains any pointers. */
2210 static int
2211 contains_pointers_p (tree type)
2213 switch (TREE_CODE (type))
2215 case POINTER_TYPE:
2216 case REFERENCE_TYPE:
2217 /* I'm not sure whether OFFSET_TYPE needs this treatment,
2218 so I'll play safe and return 1. */
2219 case OFFSET_TYPE:
2220 return 1;
2222 case RECORD_TYPE:
2223 case UNION_TYPE:
2224 case QUAL_UNION_TYPE:
2226 tree fields;
2227 /* For a type that has fields, see if the fields have pointers. */
2228 for (fields = TYPE_FIELDS (type); fields; fields = DECL_CHAIN (fields))
2229 if (TREE_CODE (fields) == FIELD_DECL
2230 && contains_pointers_p (TREE_TYPE (fields)))
2231 return 1;
2232 return 0;
2235 case ARRAY_TYPE:
2236 /* An array type contains pointers if its element type does. */
2237 return contains_pointers_p (TREE_TYPE (type));
2239 default:
2240 return 0;
2244 /* We delay assemble_external processing until
2245 the compilation unit is finalized. This is the best we can do for
2246 right now (i.e. stage 3 of GCC 4.0) - the right thing is to delay
2247 it all the way to final. See PR 17982 for further discussion. */
2248 static GTY(()) tree pending_assemble_externals;
2250 #ifdef ASM_OUTPUT_EXTERNAL
2251 /* Some targets delay some output to final using TARGET_ASM_FILE_END.
2252 As a result, assemble_external can be called after the list of externals
2253 is processed and the pointer set destroyed. */
2254 static bool pending_assemble_externals_processed;
2256 /* Avoid O(external_decls**2) lookups in the pending_assemble_externals
2257 TREE_LIST in assemble_external. */
2258 static hash_set<tree> *pending_assemble_externals_set;
2260 /* True if DECL is a function decl for which no out-of-line copy exists.
2261 It is assumed that DECL's assembler name has been set. */
2263 static bool
2264 incorporeal_function_p (tree decl)
2266 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
2268 const char *name;
2270 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
2271 && (DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA
2272 || DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA_WITH_ALIGN))
2273 return true;
2275 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2276 /* Atomic or sync builtins which have survived this far will be
2277 resolved externally and therefore are not incorporeal. */
2278 if (strncmp (name, "__builtin_", 10) == 0)
2279 return true;
2281 return false;
2284 /* Actually do the tests to determine if this is necessary, and invoke
2285 ASM_OUTPUT_EXTERNAL. */
2286 static void
2287 assemble_external_real (tree decl)
2289 rtx rtl = DECL_RTL (decl);
2291 if (MEM_P (rtl) && GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF
2292 && !SYMBOL_REF_USED (XEXP (rtl, 0))
2293 && !incorporeal_function_p (decl))
2295 /* Some systems do require some output. */
2296 SYMBOL_REF_USED (XEXP (rtl, 0)) = 1;
2297 ASM_OUTPUT_EXTERNAL (asm_out_file, decl, XSTR (XEXP (rtl, 0), 0));
2300 #endif
2302 void
2303 process_pending_assemble_externals (void)
2305 #ifdef ASM_OUTPUT_EXTERNAL
2306 tree list;
2307 for (list = pending_assemble_externals; list; list = TREE_CHAIN (list))
2308 assemble_external_real (TREE_VALUE (list));
2310 pending_assemble_externals = 0;
2311 pending_assemble_externals_processed = true;
2312 delete pending_assemble_externals_set;
2313 #endif
2316 /* This TREE_LIST contains any weak symbol declarations waiting
2317 to be emitted. */
2318 static GTY(()) tree weak_decls;
2320 /* Output something to declare an external symbol to the assembler,
2321 and qualifiers such as weakness. (Most assemblers don't need
2322 extern declaration, so we normally output nothing.) Do nothing if
2323 DECL is not external. */
2325 void
2326 assemble_external (tree decl ATTRIBUTE_UNUSED)
2328 /* Make sure that the ASM_OUT_FILE is open.
2329 If it's not, we should not be calling this function. */
2330 gcc_assert (asm_out_file);
2332 /* In a perfect world, the following condition would be true.
2333 Sadly, the Java and Go front ends emit assembly *from the front end*,
2334 bypassing the call graph. See PR52739. Fix before GCC 4.8. */
2335 #if 0
2336 /* This function should only be called if we are expanding, or have
2337 expanded, to RTL.
2338 Ideally, only final.c would be calling this function, but it is
2339 not clear whether that would break things somehow. See PR 17982
2340 for further discussion. */
2341 gcc_assert (state == EXPANSION
2342 || state == FINISHED);
2343 #endif
2345 if (!DECL_P (decl) || !DECL_EXTERNAL (decl) || !TREE_PUBLIC (decl))
2346 return;
2348 /* We want to output annotation for weak and external symbols at
2349 very last to check if they are references or not. */
2351 if (TARGET_SUPPORTS_WEAK
2352 && DECL_WEAK (decl)
2353 /* TREE_STATIC is a weird and abused creature which is not
2354 generally the right test for whether an entity has been
2355 locally emitted, inlined or otherwise not-really-extern, but
2356 for declarations that can be weak, it happens to be
2357 match. */
2358 && !TREE_STATIC (decl)
2359 && lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
2360 && value_member (decl, weak_decls) == NULL_TREE)
2361 weak_decls = tree_cons (NULL, decl, weak_decls);
2363 #ifdef ASM_OUTPUT_EXTERNAL
2364 if (pending_assemble_externals_processed)
2366 assemble_external_real (decl);
2367 return;
2370 if (! pending_assemble_externals_set->add (decl))
2371 pending_assemble_externals = tree_cons (NULL, decl,
2372 pending_assemble_externals);
2373 #endif
2376 /* Similar, for calling a library function FUN. */
2378 void
2379 assemble_external_libcall (rtx fun)
2381 /* Declare library function name external when first used, if nec. */
2382 if (! SYMBOL_REF_USED (fun))
2384 SYMBOL_REF_USED (fun) = 1;
2385 targetm.asm_out.external_libcall (fun);
2389 /* Assemble a label named NAME. */
2391 void
2392 assemble_label (FILE *file, const char *name)
2394 ASM_OUTPUT_LABEL (file, name);
2397 /* Set the symbol_referenced flag for ID. */
2398 void
2399 mark_referenced (tree id)
2401 TREE_SYMBOL_REFERENCED (id) = 1;
2404 /* Set the symbol_referenced flag for DECL and notify callgraph. */
2405 void
2406 mark_decl_referenced (tree decl)
2408 if (TREE_CODE (decl) == FUNCTION_DECL)
2410 /* Extern inline functions don't become needed when referenced.
2411 If we know a method will be emitted in other TU and no new
2412 functions can be marked reachable, just use the external
2413 definition. */
2414 struct cgraph_node *node = cgraph_node::get_create (decl);
2415 if (!DECL_EXTERNAL (decl)
2416 && !node->definition)
2417 node->mark_force_output ();
2419 else if (TREE_CODE (decl) == VAR_DECL)
2421 varpool_node *node = varpool_node::get_create (decl);
2422 /* C++ frontend use mark_decl_references to force COMDAT variables
2423 to be output that might appear dead otherwise. */
2424 node->force_output = true;
2426 /* else do nothing - we can get various sorts of CST nodes here,
2427 which do not need to be marked. */
2431 /* Follow the IDENTIFIER_TRANSPARENT_ALIAS chain starting at *ALIAS
2432 until we find an identifier that is not itself a transparent alias.
2433 Modify the alias passed to it by reference (and all aliases on the
2434 way to the ultimate target), such that they do not have to be
2435 followed again, and return the ultimate target of the alias
2436 chain. */
2438 static inline tree
2439 ultimate_transparent_alias_target (tree *alias)
2441 tree target = *alias;
2443 if (IDENTIFIER_TRANSPARENT_ALIAS (target))
2445 gcc_assert (TREE_CHAIN (target));
2446 target = ultimate_transparent_alias_target (&TREE_CHAIN (target));
2447 gcc_assert (! IDENTIFIER_TRANSPARENT_ALIAS (target)
2448 && ! TREE_CHAIN (target));
2449 *alias = target;
2452 return target;
2455 /* Output to FILE (an assembly file) a reference to NAME. If NAME
2456 starts with a *, the rest of NAME is output verbatim. Otherwise
2457 NAME is transformed in a target-specific way (usually by the
2458 addition of an underscore). */
2460 void
2461 assemble_name_raw (FILE *file, const char *name)
2463 if (name[0] == '*')
2464 fputs (&name[1], file);
2465 else
2466 ASM_OUTPUT_LABELREF (file, name);
2469 /* Like assemble_name_raw, but should be used when NAME might refer to
2470 an entity that is also represented as a tree (like a function or
2471 variable). If NAME does refer to such an entity, that entity will
2472 be marked as referenced. */
2474 void
2475 assemble_name (FILE *file, const char *name)
2477 const char *real_name;
2478 tree id;
2480 real_name = targetm.strip_name_encoding (name);
2482 id = maybe_get_identifier (real_name);
2483 if (id)
2485 tree id_orig = id;
2487 mark_referenced (id);
2488 ultimate_transparent_alias_target (&id);
2489 if (id != id_orig)
2490 name = IDENTIFIER_POINTER (id);
2491 gcc_assert (! TREE_CHAIN (id));
2494 assemble_name_raw (file, name);
2497 /* Allocate SIZE bytes writable static space with a gensym name
2498 and return an RTX to refer to its address. */
2501 assemble_static_space (unsigned HOST_WIDE_INT size)
2503 char name[12];
2504 const char *namestring;
2505 rtx x;
2507 ASM_GENERATE_INTERNAL_LABEL (name, "LF", const_labelno);
2508 ++const_labelno;
2509 namestring = ggc_strdup (name);
2511 x = gen_rtx_SYMBOL_REF (Pmode, namestring);
2512 SYMBOL_REF_FLAGS (x) = SYMBOL_FLAG_LOCAL;
2514 #ifdef ASM_OUTPUT_ALIGNED_DECL_LOCAL
2515 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, NULL_TREE, name, size,
2516 BIGGEST_ALIGNMENT);
2517 #else
2518 #ifdef ASM_OUTPUT_ALIGNED_LOCAL
2519 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, BIGGEST_ALIGNMENT);
2520 #else
2522 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
2523 so that each uninitialized object starts on such a boundary. */
2524 /* Variable `rounded' might or might not be used in ASM_OUTPUT_LOCAL. */
2525 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED
2526 = ((size + (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1)
2527 / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
2528 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
2529 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
2531 #endif
2532 #endif
2533 return x;
2536 /* Assemble the static constant template for function entry trampolines.
2537 This is done at most once per compilation.
2538 Returns an RTX for the address of the template. */
2540 static GTY(()) rtx initial_trampoline;
2543 assemble_trampoline_template (void)
2545 char label[256];
2546 const char *name;
2547 int align;
2548 rtx symbol;
2550 gcc_assert (targetm.asm_out.trampoline_template != NULL);
2552 if (initial_trampoline)
2553 return initial_trampoline;
2555 /* By default, put trampoline templates in read-only data section. */
2557 #ifdef TRAMPOLINE_SECTION
2558 switch_to_section (TRAMPOLINE_SECTION);
2559 #else
2560 switch_to_section (readonly_data_section);
2561 #endif
2563 /* Write the assembler code to define one. */
2564 align = floor_log2 (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
2565 if (align > 0)
2566 ASM_OUTPUT_ALIGN (asm_out_file, align);
2568 targetm.asm_out.internal_label (asm_out_file, "LTRAMP", 0);
2569 targetm.asm_out.trampoline_template (asm_out_file);
2571 /* Record the rtl to refer to it. */
2572 ASM_GENERATE_INTERNAL_LABEL (label, "LTRAMP", 0);
2573 name = ggc_strdup (label);
2574 symbol = gen_rtx_SYMBOL_REF (Pmode, name);
2575 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_LOCAL;
2577 initial_trampoline = gen_const_mem (BLKmode, symbol);
2578 set_mem_align (initial_trampoline, TRAMPOLINE_ALIGNMENT);
2579 set_mem_size (initial_trampoline, TRAMPOLINE_SIZE);
2581 return initial_trampoline;
2584 /* A and B are either alignments or offsets. Return the minimum alignment
2585 that may be assumed after adding the two together. */
2587 static inline unsigned
2588 min_align (unsigned int a, unsigned int b)
2590 return (a | b) & -(a | b);
2593 /* Return the assembler directive for creating a given kind of integer
2594 object. SIZE is the number of bytes in the object and ALIGNED_P
2595 indicates whether it is known to be aligned. Return NULL if the
2596 assembly dialect has no such directive.
2598 The returned string should be printed at the start of a new line and
2599 be followed immediately by the object's initial value. */
2601 const char *
2602 integer_asm_op (int size, int aligned_p)
2604 struct asm_int_op *ops;
2606 if (aligned_p)
2607 ops = &targetm.asm_out.aligned_op;
2608 else
2609 ops = &targetm.asm_out.unaligned_op;
2611 switch (size)
2613 case 1:
2614 return targetm.asm_out.byte_op;
2615 case 2:
2616 return ops->hi;
2617 case 4:
2618 return ops->si;
2619 case 8:
2620 return ops->di;
2621 case 16:
2622 return ops->ti;
2623 default:
2624 return NULL;
2628 /* Use directive OP to assemble an integer object X. Print OP at the
2629 start of the line, followed immediately by the value of X. */
2631 void
2632 assemble_integer_with_op (const char *op, rtx x)
2634 fputs (op, asm_out_file);
2635 output_addr_const (asm_out_file, x);
2636 fputc ('\n', asm_out_file);
2639 /* The default implementation of the asm_out.integer target hook. */
2641 bool
2642 default_assemble_integer (rtx x ATTRIBUTE_UNUSED,
2643 unsigned int size ATTRIBUTE_UNUSED,
2644 int aligned_p ATTRIBUTE_UNUSED)
2646 const char *op = integer_asm_op (size, aligned_p);
2647 /* Avoid GAS bugs for large values. Specifically negative values whose
2648 absolute value fits in a bfd_vma, but not in a bfd_signed_vma. */
2649 if (size > UNITS_PER_WORD && size > POINTER_SIZE_UNITS)
2650 return false;
2651 return op && (assemble_integer_with_op (op, x), true);
2654 /* Assemble the integer constant X into an object of SIZE bytes. ALIGN is
2655 the alignment of the integer in bits. Return 1 if we were able to output
2656 the constant, otherwise 0. We must be able to output the constant,
2657 if FORCE is nonzero. */
2659 bool
2660 assemble_integer (rtx x, unsigned int size, unsigned int align, int force)
2662 int aligned_p;
2664 aligned_p = (align >= MIN (size * BITS_PER_UNIT, BIGGEST_ALIGNMENT));
2666 /* See if the target hook can handle this kind of object. */
2667 if (targetm.asm_out.integer (x, size, aligned_p))
2668 return true;
2670 /* If the object is a multi-byte one, try splitting it up. Split
2671 it into words it if is multi-word, otherwise split it into bytes. */
2672 if (size > 1)
2674 enum machine_mode omode, imode;
2675 unsigned int subalign;
2676 unsigned int subsize, i;
2677 enum mode_class mclass;
2679 subsize = size > UNITS_PER_WORD? UNITS_PER_WORD : 1;
2680 subalign = MIN (align, subsize * BITS_PER_UNIT);
2681 if (GET_CODE (x) == CONST_FIXED)
2682 mclass = GET_MODE_CLASS (GET_MODE (x));
2683 else
2684 mclass = MODE_INT;
2686 omode = mode_for_size (subsize * BITS_PER_UNIT, mclass, 0);
2687 imode = mode_for_size (size * BITS_PER_UNIT, mclass, 0);
2689 for (i = 0; i < size; i += subsize)
2691 rtx partial = simplify_subreg (omode, x, imode, i);
2692 if (!partial || !assemble_integer (partial, subsize, subalign, 0))
2693 break;
2695 if (i == size)
2696 return true;
2698 /* If we've printed some of it, but not all of it, there's no going
2699 back now. */
2700 gcc_assert (!i);
2703 gcc_assert (!force);
2705 return false;
2708 void
2709 assemble_real (REAL_VALUE_TYPE d, enum machine_mode mode, unsigned int align)
2711 long data[4] = {0, 0, 0, 0};
2712 int i;
2713 int bitsize, nelts, nunits, units_per;
2715 /* This is hairy. We have a quantity of known size. real_to_target
2716 will put it into an array of *host* longs, 32 bits per element
2717 (even if long is more than 32 bits). We need to determine the
2718 number of array elements that are occupied (nelts) and the number
2719 of *target* min-addressable units that will be occupied in the
2720 object file (nunits). We cannot assume that 32 divides the
2721 mode's bitsize (size * BITS_PER_UNIT) evenly.
2723 size * BITS_PER_UNIT is used here to make sure that padding bits
2724 (which might appear at either end of the value; real_to_target
2725 will include the padding bits in its output array) are included. */
2727 nunits = GET_MODE_SIZE (mode);
2728 bitsize = nunits * BITS_PER_UNIT;
2729 nelts = CEIL (bitsize, 32);
2730 units_per = 32 / BITS_PER_UNIT;
2732 real_to_target (data, &d, mode);
2734 /* Put out the first word with the specified alignment. */
2735 assemble_integer (GEN_INT (data[0]), MIN (nunits, units_per), align, 1);
2736 nunits -= units_per;
2738 /* Subsequent words need only 32-bit alignment. */
2739 align = min_align (align, 32);
2741 for (i = 1; i < nelts; i++)
2743 assemble_integer (GEN_INT (data[i]), MIN (nunits, units_per), align, 1);
2744 nunits -= units_per;
2748 /* Given an expression EXP with a constant value,
2749 reduce it to the sum of an assembler symbol and an integer.
2750 Store them both in the structure *VALUE.
2751 EXP must be reducible. */
2753 struct addr_const {
2754 rtx base;
2755 HOST_WIDE_INT offset;
2758 static void
2759 decode_addr_const (tree exp, struct addr_const *value)
2761 tree target = TREE_OPERAND (exp, 0);
2762 int offset = 0;
2763 rtx x;
2765 while (1)
2767 if (TREE_CODE (target) == COMPONENT_REF
2768 && tree_fits_shwi_p (byte_position (TREE_OPERAND (target, 1))))
2770 offset += int_byte_position (TREE_OPERAND (target, 1));
2771 target = TREE_OPERAND (target, 0);
2773 else if (TREE_CODE (target) == ARRAY_REF
2774 || TREE_CODE (target) == ARRAY_RANGE_REF)
2776 offset += (tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (target)))
2777 * tree_to_shwi (TREE_OPERAND (target, 1)));
2778 target = TREE_OPERAND (target, 0);
2780 else if (TREE_CODE (target) == MEM_REF
2781 && TREE_CODE (TREE_OPERAND (target, 0)) == ADDR_EXPR)
2783 offset += mem_ref_offset (target).to_short_addr ();
2784 target = TREE_OPERAND (TREE_OPERAND (target, 0), 0);
2786 else if (TREE_CODE (target) == INDIRECT_REF
2787 && TREE_CODE (TREE_OPERAND (target, 0)) == NOP_EXPR
2788 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (target, 0), 0))
2789 == ADDR_EXPR)
2790 target = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (target, 0), 0), 0);
2791 else
2792 break;
2795 switch (TREE_CODE (target))
2797 case VAR_DECL:
2798 case FUNCTION_DECL:
2799 x = DECL_RTL (target);
2800 break;
2802 case LABEL_DECL:
2803 x = gen_rtx_MEM (FUNCTION_MODE,
2804 gen_rtx_LABEL_REF (Pmode, force_label_rtx (target)));
2805 break;
2807 case REAL_CST:
2808 case FIXED_CST:
2809 case STRING_CST:
2810 case COMPLEX_CST:
2811 case CONSTRUCTOR:
2812 case INTEGER_CST:
2813 x = output_constant_def (target, 1);
2814 break;
2816 default:
2817 gcc_unreachable ();
2820 gcc_assert (MEM_P (x));
2821 x = XEXP (x, 0);
2823 value->base = x;
2824 value->offset = offset;
2828 static GTY((param_is (struct constant_descriptor_tree)))
2829 htab_t const_desc_htab;
2831 static void maybe_output_constant_def_contents (struct constant_descriptor_tree *, int);
2833 /* Constant pool accessor function. */
2835 htab_t
2836 constant_pool_htab (void)
2838 return const_desc_htab;
2841 /* Compute a hash code for a constant expression. */
2843 static hashval_t
2844 const_desc_hash (const void *ptr)
2846 return ((const struct constant_descriptor_tree *)ptr)->hash;
2849 static hashval_t
2850 const_hash_1 (const tree exp)
2852 const char *p;
2853 hashval_t hi;
2854 int len, i;
2855 enum tree_code code = TREE_CODE (exp);
2857 /* Either set P and LEN to the address and len of something to hash and
2858 exit the switch or return a value. */
2860 switch (code)
2862 case INTEGER_CST:
2863 p = (char *) &TREE_INT_CST_ELT (exp, 0);
2864 len = TREE_INT_CST_NUNITS (exp) * sizeof (HOST_WIDE_INT);
2865 break;
2867 case REAL_CST:
2868 return real_hash (TREE_REAL_CST_PTR (exp));
2870 case FIXED_CST:
2871 return fixed_hash (TREE_FIXED_CST_PTR (exp));
2873 case STRING_CST:
2874 p = TREE_STRING_POINTER (exp);
2875 len = TREE_STRING_LENGTH (exp);
2876 break;
2878 case COMPLEX_CST:
2879 return (const_hash_1 (TREE_REALPART (exp)) * 5
2880 + const_hash_1 (TREE_IMAGPART (exp)));
2882 case VECTOR_CST:
2884 unsigned i;
2886 hi = 7 + VECTOR_CST_NELTS (exp);
2888 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
2889 hi = hi * 563 + const_hash_1 (VECTOR_CST_ELT (exp, i));
2891 return hi;
2894 case CONSTRUCTOR:
2896 unsigned HOST_WIDE_INT idx;
2897 tree value;
2899 hi = 5 + int_size_in_bytes (TREE_TYPE (exp));
2901 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
2902 if (value)
2903 hi = hi * 603 + const_hash_1 (value);
2905 return hi;
2908 case ADDR_EXPR:
2909 case FDESC_EXPR:
2911 struct addr_const value;
2913 decode_addr_const (exp, &value);
2914 switch (GET_CODE (value.base))
2916 case SYMBOL_REF:
2917 /* Don't hash the address of the SYMBOL_REF;
2918 only use the offset and the symbol name. */
2919 hi = value.offset;
2920 p = XSTR (value.base, 0);
2921 for (i = 0; p[i] != 0; i++)
2922 hi = ((hi * 613) + (unsigned) (p[i]));
2923 break;
2925 case LABEL_REF:
2926 hi = (value.offset
2927 + CODE_LABEL_NUMBER (LABEL_REF_LABEL (value.base)) * 13);
2928 break;
2930 default:
2931 gcc_unreachable ();
2934 return hi;
2936 case PLUS_EXPR:
2937 case POINTER_PLUS_EXPR:
2938 case MINUS_EXPR:
2939 return (const_hash_1 (TREE_OPERAND (exp, 0)) * 9
2940 + const_hash_1 (TREE_OPERAND (exp, 1)));
2942 CASE_CONVERT:
2943 return const_hash_1 (TREE_OPERAND (exp, 0)) * 7 + 2;
2945 default:
2946 /* A language specific constant. Just hash the code. */
2947 return code;
2950 /* Compute hashing function. */
2951 hi = len;
2952 for (i = 0; i < len; i++)
2953 hi = ((hi * 613) + (unsigned) (p[i]));
2955 return hi;
2958 /* Wrapper of compare_constant, for the htab interface. */
2959 static int
2960 const_desc_eq (const void *p1, const void *p2)
2962 const struct constant_descriptor_tree *const c1
2963 = (const struct constant_descriptor_tree *) p1;
2964 const struct constant_descriptor_tree *const c2
2965 = (const struct constant_descriptor_tree *) p2;
2966 if (c1->hash != c2->hash)
2967 return 0;
2968 return compare_constant (c1->value, c2->value);
2971 /* Compare t1 and t2, and return 1 only if they are known to result in
2972 the same bit pattern on output. */
2974 static int
2975 compare_constant (const tree t1, const tree t2)
2977 enum tree_code typecode;
2979 if (t1 == NULL_TREE)
2980 return t2 == NULL_TREE;
2981 if (t2 == NULL_TREE)
2982 return 0;
2984 if (TREE_CODE (t1) != TREE_CODE (t2))
2985 return 0;
2987 switch (TREE_CODE (t1))
2989 case INTEGER_CST:
2990 /* Integer constants are the same only if the same width of type. */
2991 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
2992 return 0;
2993 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
2994 return 0;
2995 return tree_int_cst_equal (t1, t2);
2997 case REAL_CST:
2998 /* Real constants are the same only if the same width of type. */
2999 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3000 return 0;
3002 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
3004 case FIXED_CST:
3005 /* Fixed constants are the same only if the same width of type. */
3006 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3007 return 0;
3009 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
3011 case STRING_CST:
3012 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
3013 return 0;
3015 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
3016 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
3017 TREE_STRING_LENGTH (t1)));
3019 case COMPLEX_CST:
3020 return (compare_constant (TREE_REALPART (t1), TREE_REALPART (t2))
3021 && compare_constant (TREE_IMAGPART (t1), TREE_IMAGPART (t2)));
3023 case VECTOR_CST:
3025 unsigned i;
3027 if (VECTOR_CST_NELTS (t1) != VECTOR_CST_NELTS (t2))
3028 return 0;
3030 for (i = 0; i < VECTOR_CST_NELTS (t1); ++i)
3031 if (!compare_constant (VECTOR_CST_ELT (t1, i),
3032 VECTOR_CST_ELT (t2, i)))
3033 return 0;
3035 return 1;
3038 case CONSTRUCTOR:
3040 vec<constructor_elt, va_gc> *v1, *v2;
3041 unsigned HOST_WIDE_INT idx;
3043 typecode = TREE_CODE (TREE_TYPE (t1));
3044 if (typecode != TREE_CODE (TREE_TYPE (t2)))
3045 return 0;
3047 if (typecode == ARRAY_TYPE)
3049 HOST_WIDE_INT size_1 = int_size_in_bytes (TREE_TYPE (t1));
3050 /* For arrays, check that the sizes all match. */
3051 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))
3052 || size_1 == -1
3053 || size_1 != int_size_in_bytes (TREE_TYPE (t2)))
3054 return 0;
3056 else
3058 /* For record and union constructors, require exact type
3059 equality. */
3060 if (TREE_TYPE (t1) != TREE_TYPE (t2))
3061 return 0;
3064 v1 = CONSTRUCTOR_ELTS (t1);
3065 v2 = CONSTRUCTOR_ELTS (t2);
3066 if (vec_safe_length (v1) != vec_safe_length (v2))
3067 return 0;
3069 for (idx = 0; idx < vec_safe_length (v1); ++idx)
3071 constructor_elt *c1 = &(*v1)[idx];
3072 constructor_elt *c2 = &(*v2)[idx];
3074 /* Check that each value is the same... */
3075 if (!compare_constant (c1->value, c2->value))
3076 return 0;
3077 /* ... and that they apply to the same fields! */
3078 if (typecode == ARRAY_TYPE)
3080 if (!compare_constant (c1->index, c2->index))
3081 return 0;
3083 else
3085 if (c1->index != c2->index)
3086 return 0;
3090 return 1;
3093 case ADDR_EXPR:
3094 case FDESC_EXPR:
3096 struct addr_const value1, value2;
3097 enum rtx_code code;
3098 int ret;
3100 decode_addr_const (t1, &value1);
3101 decode_addr_const (t2, &value2);
3103 if (value1.offset != value2.offset)
3104 return 0;
3106 code = GET_CODE (value1.base);
3107 if (code != GET_CODE (value2.base))
3108 return 0;
3110 switch (code)
3112 case SYMBOL_REF:
3113 ret = (strcmp (XSTR (value1.base, 0), XSTR (value2.base, 0)) == 0);
3114 break;
3116 case LABEL_REF:
3117 ret = (CODE_LABEL_NUMBER (LABEL_REF_LABEL (value1.base))
3118 == CODE_LABEL_NUMBER (LABEL_REF_LABEL (value2.base)));
3119 break;
3121 default:
3122 gcc_unreachable ();
3124 return ret;
3127 case PLUS_EXPR:
3128 case POINTER_PLUS_EXPR:
3129 case MINUS_EXPR:
3130 case RANGE_EXPR:
3131 return (compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
3132 && compare_constant (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
3134 CASE_CONVERT:
3135 case VIEW_CONVERT_EXPR:
3136 return compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3138 default:
3139 return 0;
3142 gcc_unreachable ();
3145 /* Return the section into which constant EXP should be placed. */
3147 static section *
3148 get_constant_section (tree exp, unsigned int align)
3150 return targetm.asm_out.select_section (exp,
3151 compute_reloc_for_constant (exp),
3152 align);
3155 /* Return the size of constant EXP in bytes. */
3157 static HOST_WIDE_INT
3158 get_constant_size (tree exp)
3160 HOST_WIDE_INT size;
3162 size = int_size_in_bytes (TREE_TYPE (exp));
3163 if (TREE_CODE (exp) == STRING_CST)
3164 size = MAX (TREE_STRING_LENGTH (exp), size);
3165 return size;
3168 /* Subroutine of output_constant_def:
3169 No constant equal to EXP is known to have been output.
3170 Make a constant descriptor to enter EXP in the hash table.
3171 Assign the label number and construct RTL to refer to the
3172 constant's location in memory.
3173 Caller is responsible for updating the hash table. */
3175 static struct constant_descriptor_tree *
3176 build_constant_desc (tree exp)
3178 struct constant_descriptor_tree *desc;
3179 rtx symbol, rtl;
3180 char label[256];
3181 int labelno;
3182 tree decl;
3184 desc = ggc_alloc<constant_descriptor_tree> ();
3185 desc->value = exp;
3187 /* Create a string containing the label name, in LABEL. */
3188 labelno = const_labelno++;
3189 ASM_GENERATE_INTERNAL_LABEL (label, "LC", labelno);
3191 /* Construct the VAR_DECL associated with the constant. */
3192 decl = build_decl (UNKNOWN_LOCATION, VAR_DECL, get_identifier (label),
3193 TREE_TYPE (exp));
3194 DECL_ARTIFICIAL (decl) = 1;
3195 DECL_IGNORED_P (decl) = 1;
3196 TREE_READONLY (decl) = 1;
3197 TREE_STATIC (decl) = 1;
3198 TREE_ADDRESSABLE (decl) = 1;
3199 /* We don't set the RTL yet as this would cause varpool to assume that the
3200 variable is referenced. Moreover, it would just be dropped in LTO mode.
3201 Instead we set the flag that will be recognized in make_decl_rtl. */
3202 DECL_IN_CONSTANT_POOL (decl) = 1;
3203 DECL_INITIAL (decl) = desc->value;
3204 /* ??? CONSTANT_ALIGNMENT hasn't been updated for vector types on most
3205 architectures so use DATA_ALIGNMENT as well, except for strings. */
3206 if (TREE_CODE (exp) == STRING_CST)
3208 #ifdef CONSTANT_ALIGNMENT
3209 DECL_ALIGN (decl) = CONSTANT_ALIGNMENT (exp, DECL_ALIGN (decl));
3210 #endif
3212 else
3213 align_variable (decl, 0);
3215 /* Now construct the SYMBOL_REF and the MEM. */
3216 if (use_object_blocks_p ())
3218 section *sect = get_constant_section (exp, DECL_ALIGN (decl));
3219 symbol = create_block_symbol (ggc_strdup (label),
3220 get_block_for_section (sect), -1);
3222 else
3223 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3224 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3225 SET_SYMBOL_REF_DECL (symbol, decl);
3226 TREE_CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3228 rtl = gen_const_mem (TYPE_MODE (TREE_TYPE (exp)), symbol);
3229 set_mem_attributes (rtl, exp, 1);
3230 set_mem_alias_set (rtl, 0);
3231 set_mem_alias_set (rtl, const_alias_set);
3233 /* We cannot share RTX'es in pool entries.
3234 Mark this piece of RTL as required for unsharing. */
3235 RTX_FLAG (rtl, used) = 1;
3237 /* Set flags or add text to the name to record information, such as
3238 that it is a local symbol. If the name is changed, the macro
3239 ASM_OUTPUT_LABELREF will have to know how to strip this
3240 information. This call might invalidate our local variable
3241 SYMBOL; we can't use it afterward. */
3242 targetm.encode_section_info (exp, rtl, true);
3244 desc->rtl = rtl;
3246 return desc;
3249 /* Return an rtx representing a reference to constant data in memory
3250 for the constant expression EXP.
3252 If assembler code for such a constant has already been output,
3253 return an rtx to refer to it.
3254 Otherwise, output such a constant in memory
3255 and generate an rtx for it.
3257 If DEFER is nonzero, this constant can be deferred and output only
3258 if referenced in the function after all optimizations.
3260 `const_desc_table' records which constants already have label strings. */
3263 output_constant_def (tree exp, int defer)
3265 struct constant_descriptor_tree *desc;
3266 struct constant_descriptor_tree key;
3267 void **loc;
3269 /* Look up EXP in the table of constant descriptors. If we didn't find
3270 it, create a new one. */
3271 key.value = exp;
3272 key.hash = const_hash_1 (exp);
3273 loc = htab_find_slot_with_hash (const_desc_htab, &key, key.hash, INSERT);
3275 desc = (struct constant_descriptor_tree *) *loc;
3276 if (desc == 0)
3278 desc = build_constant_desc (exp);
3279 desc->hash = key.hash;
3280 *loc = desc;
3283 maybe_output_constant_def_contents (desc, defer);
3284 return desc->rtl;
3287 /* Subroutine of output_constant_def: Decide whether or not we need to
3288 output the constant DESC now, and if so, do it. */
3289 static void
3290 maybe_output_constant_def_contents (struct constant_descriptor_tree *desc,
3291 int defer)
3293 rtx symbol = XEXP (desc->rtl, 0);
3294 tree exp = desc->value;
3296 if (flag_syntax_only)
3297 return;
3299 if (TREE_ASM_WRITTEN (exp))
3300 /* Already output; don't do it again. */
3301 return;
3303 /* We can always defer constants as long as the context allows
3304 doing so. */
3305 if (defer)
3307 /* Increment n_deferred_constants if it exists. It needs to be at
3308 least as large as the number of constants actually referred to
3309 by the function. If it's too small we'll stop looking too early
3310 and fail to emit constants; if it's too large we'll only look
3311 through the entire function when we could have stopped earlier. */
3312 if (cfun)
3313 n_deferred_constants++;
3314 return;
3317 output_constant_def_contents (symbol);
3320 /* Subroutine of output_constant_def_contents. Output the definition
3321 of constant EXP, which is pointed to by label LABEL. ALIGN is the
3322 constant's alignment in bits. */
3324 static void
3325 assemble_constant_contents (tree exp, const char *label, unsigned int align)
3327 HOST_WIDE_INT size;
3329 size = get_constant_size (exp);
3331 /* Do any machine/system dependent processing of the constant. */
3332 targetm.asm_out.declare_constant_name (asm_out_file, label, exp, size);
3334 /* Output the value of EXP. */
3335 output_constant (exp, size, align);
3338 /* We must output the constant data referred to by SYMBOL; do so. */
3340 static void
3341 output_constant_def_contents (rtx symbol)
3343 tree decl = SYMBOL_REF_DECL (symbol);
3344 tree exp = DECL_INITIAL (decl);
3345 unsigned int align;
3346 bool asan_protected = false;
3348 /* Make sure any other constants whose addresses appear in EXP
3349 are assigned label numbers. */
3350 output_addressed_constants (exp);
3352 /* We are no longer deferring this constant. */
3353 TREE_ASM_WRITTEN (decl) = TREE_ASM_WRITTEN (exp) = 1;
3355 if ((flag_sanitize & SANITIZE_ADDRESS)
3356 && TREE_CODE (exp) == STRING_CST
3357 && asan_protect_global (exp))
3359 asan_protected = true;
3360 DECL_ALIGN (decl) = MAX (DECL_ALIGN (decl),
3361 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
3364 /* If the constant is part of an object block, make sure that the
3365 decl has been positioned within its block, but do not write out
3366 its definition yet. output_object_blocks will do that later. */
3367 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
3368 place_block_symbol (symbol);
3369 else
3371 align = DECL_ALIGN (decl);
3372 switch_to_section (get_constant_section (exp, align));
3373 if (align > BITS_PER_UNIT)
3374 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
3375 assemble_constant_contents (exp, XSTR (symbol, 0), align);
3376 if (asan_protected)
3378 HOST_WIDE_INT size = get_constant_size (exp);
3379 assemble_zeros (asan_red_zone_size (size));
3384 /* Look up EXP in the table of constant descriptors. Return the rtl
3385 if it has been emitted, else null. */
3388 lookup_constant_def (tree exp)
3390 struct constant_descriptor_tree *desc;
3391 struct constant_descriptor_tree key;
3393 key.value = exp;
3394 key.hash = const_hash_1 (exp);
3395 desc = (struct constant_descriptor_tree *)
3396 htab_find_with_hash (const_desc_htab, &key, key.hash);
3398 return (desc ? desc->rtl : NULL_RTX);
3401 /* Return a tree representing a reference to constant data in memory
3402 for the constant expression EXP.
3404 This is the counterpart of output_constant_def at the Tree level. */
3406 tree
3407 tree_output_constant_def (tree exp)
3409 struct constant_descriptor_tree *desc, key;
3410 void **loc;
3411 tree decl;
3413 /* Look up EXP in the table of constant descriptors. If we didn't find
3414 it, create a new one. */
3415 key.value = exp;
3416 key.hash = const_hash_1 (exp);
3417 loc = htab_find_slot_with_hash (const_desc_htab, &key, key.hash, INSERT);
3419 desc = (struct constant_descriptor_tree *) *loc;
3420 if (desc == 0)
3422 desc = build_constant_desc (exp);
3423 desc->hash = key.hash;
3424 *loc = desc;
3427 decl = SYMBOL_REF_DECL (XEXP (desc->rtl, 0));
3428 varpool_node::finalize_decl (decl);
3429 return decl;
3432 /* Used in the hash tables to avoid outputting the same constant
3433 twice. Unlike 'struct constant_descriptor_tree', RTX constants
3434 are output once per function, not once per file. */
3435 /* ??? Only a few targets need per-function constant pools. Most
3436 can use one per-file pool. Should add a targetm bit to tell the
3437 difference. */
3439 struct GTY(()) rtx_constant_pool {
3440 /* Pointers to first and last constant in pool, as ordered by offset. */
3441 struct constant_descriptor_rtx *first;
3442 struct constant_descriptor_rtx *last;
3444 /* Hash facility for making memory-constants from constant rtl-expressions.
3445 It is used on RISC machines where immediate integer arguments and
3446 constant addresses are restricted so that such constants must be stored
3447 in memory. */
3448 htab_t GTY((param_is (struct constant_descriptor_rtx))) const_rtx_htab;
3450 /* Current offset in constant pool (does not include any
3451 machine-specific header). */
3452 HOST_WIDE_INT offset;
3455 struct GTY((chain_next ("%h.next"))) constant_descriptor_rtx {
3456 struct constant_descriptor_rtx *next;
3457 rtx mem;
3458 rtx sym;
3459 rtx constant;
3460 HOST_WIDE_INT offset;
3461 hashval_t hash;
3462 enum machine_mode mode;
3463 unsigned int align;
3464 int labelno;
3465 int mark;
3468 /* Hash and compare functions for const_rtx_htab. */
3470 static hashval_t
3471 const_desc_rtx_hash (const void *ptr)
3473 const struct constant_descriptor_rtx *const desc
3474 = (const struct constant_descriptor_rtx *) ptr;
3475 return desc->hash;
3478 static int
3479 const_desc_rtx_eq (const void *a, const void *b)
3481 const struct constant_descriptor_rtx *const x
3482 = (const struct constant_descriptor_rtx *) a;
3483 const struct constant_descriptor_rtx *const y
3484 = (const struct constant_descriptor_rtx *) b;
3486 if (x->mode != y->mode)
3487 return 0;
3488 return rtx_equal_p (x->constant, y->constant);
3491 /* Hash one component of a constant. */
3493 static hashval_t
3494 const_rtx_hash_1 (const_rtx x)
3496 unsigned HOST_WIDE_INT hwi;
3497 enum machine_mode mode;
3498 enum rtx_code code;
3499 hashval_t h;
3500 int i;
3502 code = GET_CODE (x);
3503 mode = GET_MODE (x);
3504 h = (hashval_t) code * 1048573 + mode;
3506 switch (code)
3508 case CONST_INT:
3509 hwi = INTVAL (x);
3511 fold_hwi:
3513 int shift = sizeof (hashval_t) * CHAR_BIT;
3514 const int n = sizeof (HOST_WIDE_INT) / sizeof (hashval_t);
3516 h ^= (hashval_t) hwi;
3517 for (i = 1; i < n; ++i)
3519 hwi >>= shift;
3520 h ^= (hashval_t) hwi;
3523 break;
3525 case CONST_WIDE_INT:
3526 hwi = GET_MODE_PRECISION (mode);
3528 for (i = 0; i < CONST_WIDE_INT_NUNITS (x); i++)
3529 hwi ^= CONST_WIDE_INT_ELT (x, i);
3530 goto fold_hwi;
3533 case CONST_DOUBLE:
3534 if (TARGET_SUPPORTS_WIDE_INT == 0 && mode == VOIDmode)
3536 hwi = CONST_DOUBLE_LOW (x) ^ CONST_DOUBLE_HIGH (x);
3537 goto fold_hwi;
3539 else
3540 h ^= real_hash (CONST_DOUBLE_REAL_VALUE (x));
3541 break;
3543 case CONST_FIXED:
3544 h ^= fixed_hash (CONST_FIXED_VALUE (x));
3545 break;
3547 case SYMBOL_REF:
3548 h ^= htab_hash_string (XSTR (x, 0));
3549 break;
3551 case LABEL_REF:
3552 h = h * 251 + CODE_LABEL_NUMBER (LABEL_REF_LABEL (x));
3553 break;
3555 case UNSPEC:
3556 case UNSPEC_VOLATILE:
3557 h = h * 251 + XINT (x, 1);
3558 break;
3560 default:
3561 break;
3564 return h;
3567 /* Compute a hash value for X, which should be a constant. */
3569 static hashval_t
3570 const_rtx_hash (rtx x)
3572 hashval_t h = 0;
3573 subrtx_iterator::array_type array;
3574 FOR_EACH_SUBRTX (iter, array, x, ALL)
3575 h = h * 509 + const_rtx_hash_1 (*iter);
3576 return h;
3580 /* Create and return a new rtx constant pool. */
3582 static struct rtx_constant_pool *
3583 create_constant_pool (void)
3585 struct rtx_constant_pool *pool;
3587 pool = ggc_alloc<rtx_constant_pool> ();
3588 pool->const_rtx_htab = htab_create_ggc (31, const_desc_rtx_hash,
3589 const_desc_rtx_eq, NULL);
3590 pool->first = NULL;
3591 pool->last = NULL;
3592 pool->offset = 0;
3593 return pool;
3596 /* Initialize constant pool hashing for a new function. */
3598 void
3599 init_varasm_status (void)
3601 crtl->varasm.pool = create_constant_pool ();
3602 crtl->varasm.deferred_constants = 0;
3605 /* Given a MINUS expression, simplify it if both sides
3606 include the same symbol. */
3609 simplify_subtraction (rtx x)
3611 rtx r = simplify_rtx (x);
3612 return r ? r : x;
3615 /* Given a constant rtx X, make (or find) a memory constant for its value
3616 and return a MEM rtx to refer to it in memory. */
3619 force_const_mem (enum machine_mode mode, rtx x)
3621 struct constant_descriptor_rtx *desc, tmp;
3622 struct rtx_constant_pool *pool;
3623 char label[256];
3624 rtx def, symbol;
3625 hashval_t hash;
3626 unsigned int align;
3627 void **slot;
3629 /* If we're not allowed to drop X into the constant pool, don't. */
3630 if (targetm.cannot_force_const_mem (mode, x))
3631 return NULL_RTX;
3633 /* Record that this function has used a constant pool entry. */
3634 crtl->uses_const_pool = 1;
3636 /* Decide which pool to use. */
3637 pool = (targetm.use_blocks_for_constant_p (mode, x)
3638 ? shared_constant_pool
3639 : crtl->varasm.pool);
3641 /* Lookup the value in the hashtable. */
3642 tmp.constant = x;
3643 tmp.mode = mode;
3644 hash = const_rtx_hash (x);
3645 slot = htab_find_slot_with_hash (pool->const_rtx_htab, &tmp, hash, INSERT);
3646 desc = (struct constant_descriptor_rtx *) *slot;
3648 /* If the constant was already present, return its memory. */
3649 if (desc)
3650 return copy_rtx (desc->mem);
3652 /* Otherwise, create a new descriptor. */
3653 desc = ggc_alloc<constant_descriptor_rtx> ();
3654 *slot = desc;
3656 /* Align the location counter as required by EXP's data type. */
3657 align = GET_MODE_ALIGNMENT (mode == VOIDmode ? word_mode : mode);
3658 #ifdef CONSTANT_ALIGNMENT
3660 tree type = lang_hooks.types.type_for_mode (mode, 0);
3661 if (type != NULL_TREE)
3662 align = CONSTANT_ALIGNMENT (make_tree (type, x), align);
3664 #endif
3666 pool->offset += (align / BITS_PER_UNIT) - 1;
3667 pool->offset &= ~ ((align / BITS_PER_UNIT) - 1);
3669 desc->next = NULL;
3670 desc->constant = copy_rtx (tmp.constant);
3671 desc->offset = pool->offset;
3672 desc->hash = hash;
3673 desc->mode = mode;
3674 desc->align = align;
3675 desc->labelno = const_labelno;
3676 desc->mark = 0;
3678 pool->offset += GET_MODE_SIZE (mode);
3679 if (pool->last)
3680 pool->last->next = desc;
3681 else
3682 pool->first = pool->last = desc;
3683 pool->last = desc;
3685 /* Create a string containing the label name, in LABEL. */
3686 ASM_GENERATE_INTERNAL_LABEL (label, "LC", const_labelno);
3687 ++const_labelno;
3689 /* Construct the SYMBOL_REF. Make sure to mark it as belonging to
3690 the constants pool. */
3691 if (use_object_blocks_p () && targetm.use_blocks_for_constant_p (mode, x))
3693 section *sect = targetm.asm_out.select_rtx_section (mode, x, align);
3694 symbol = create_block_symbol (ggc_strdup (label),
3695 get_block_for_section (sect), -1);
3697 else
3698 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3699 desc->sym = symbol;
3700 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3701 CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3702 SET_SYMBOL_REF_CONSTANT (symbol, desc);
3704 /* Construct the MEM. */
3705 desc->mem = def = gen_const_mem (mode, symbol);
3706 set_mem_attributes (def, lang_hooks.types.type_for_mode (mode, 0), 1);
3707 set_mem_align (def, align);
3709 /* If we're dropping a label to the constant pool, make sure we
3710 don't delete it. */
3711 if (GET_CODE (x) == LABEL_REF)
3712 LABEL_PRESERVE_P (XEXP (x, 0)) = 1;
3714 return copy_rtx (def);
3717 /* Given a constant pool SYMBOL_REF, return the corresponding constant. */
3720 get_pool_constant (const_rtx addr)
3722 return SYMBOL_REF_CONSTANT (addr)->constant;
3725 /* Given a constant pool SYMBOL_REF, return the corresponding constant
3726 and whether it has been output or not. */
3729 get_pool_constant_mark (rtx addr, bool *pmarked)
3731 struct constant_descriptor_rtx *desc;
3733 desc = SYMBOL_REF_CONSTANT (addr);
3734 *pmarked = (desc->mark != 0);
3735 return desc->constant;
3738 /* Similar, return the mode. */
3740 enum machine_mode
3741 get_pool_mode (const_rtx addr)
3743 return SYMBOL_REF_CONSTANT (addr)->mode;
3746 /* Return the size of the constant pool. */
3749 get_pool_size (void)
3751 return crtl->varasm.pool->offset;
3754 /* Worker function for output_constant_pool_1. Emit assembly for X
3755 in MODE with known alignment ALIGN. */
3757 static void
3758 output_constant_pool_2 (enum machine_mode mode, rtx x, unsigned int align)
3760 switch (GET_MODE_CLASS (mode))
3762 case MODE_FLOAT:
3763 case MODE_DECIMAL_FLOAT:
3765 REAL_VALUE_TYPE r;
3767 gcc_assert (CONST_DOUBLE_AS_FLOAT_P (x));
3768 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
3769 assemble_real (r, mode, align);
3770 break;
3773 case MODE_INT:
3774 case MODE_PARTIAL_INT:
3775 case MODE_FRACT:
3776 case MODE_UFRACT:
3777 case MODE_ACCUM:
3778 case MODE_UACCUM:
3779 assemble_integer (x, GET_MODE_SIZE (mode), align, 1);
3780 break;
3782 case MODE_VECTOR_FLOAT:
3783 case MODE_VECTOR_INT:
3784 case MODE_VECTOR_FRACT:
3785 case MODE_VECTOR_UFRACT:
3786 case MODE_VECTOR_ACCUM:
3787 case MODE_VECTOR_UACCUM:
3789 int i, units;
3790 enum machine_mode submode = GET_MODE_INNER (mode);
3791 unsigned int subalign = MIN (align, GET_MODE_BITSIZE (submode));
3793 gcc_assert (GET_CODE (x) == CONST_VECTOR);
3794 units = CONST_VECTOR_NUNITS (x);
3796 for (i = 0; i < units; i++)
3798 rtx elt = CONST_VECTOR_ELT (x, i);
3799 output_constant_pool_2 (submode, elt, i ? subalign : align);
3802 break;
3804 default:
3805 gcc_unreachable ();
3809 /* Worker function for output_constant_pool. Emit constant DESC,
3810 giving it ALIGN bits of alignment. */
3812 static void
3813 output_constant_pool_1 (struct constant_descriptor_rtx *desc,
3814 unsigned int align)
3816 rtx x, tmp;
3818 x = desc->constant;
3820 /* See if X is a LABEL_REF (or a CONST referring to a LABEL_REF)
3821 whose CODE_LABEL has been deleted. This can occur if a jump table
3822 is eliminated by optimization. If so, write a constant of zero
3823 instead. Note that this can also happen by turning the
3824 CODE_LABEL into a NOTE. */
3825 /* ??? This seems completely and utterly wrong. Certainly it's
3826 not true for NOTE_INSN_DELETED_LABEL, but I disbelieve proper
3827 functioning even with rtx_insn::deleted and friends. */
3829 tmp = x;
3830 switch (GET_CODE (tmp))
3832 case CONST:
3833 if (GET_CODE (XEXP (tmp, 0)) != PLUS
3834 || GET_CODE (XEXP (XEXP (tmp, 0), 0)) != LABEL_REF)
3835 break;
3836 tmp = XEXP (XEXP (tmp, 0), 0);
3837 /* FALLTHRU */
3839 case LABEL_REF:
3840 tmp = LABEL_REF_LABEL (tmp);
3841 gcc_assert (!as_a<rtx_insn *> (tmp)->deleted ());
3842 gcc_assert (!NOTE_P (tmp)
3843 || NOTE_KIND (tmp) != NOTE_INSN_DELETED);
3844 break;
3846 default:
3847 break;
3850 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3851 ASM_OUTPUT_SPECIAL_POOL_ENTRY (asm_out_file, x, desc->mode,
3852 align, desc->labelno, done);
3853 #endif
3855 assemble_align (align);
3857 /* Output the label. */
3858 targetm.asm_out.internal_label (asm_out_file, "LC", desc->labelno);
3860 /* Output the data. */
3861 output_constant_pool_2 (desc->mode, x, align);
3863 /* Make sure all constants in SECTION_MERGE and not SECTION_STRINGS
3864 sections have proper size. */
3865 if (align > GET_MODE_BITSIZE (desc->mode)
3866 && in_section
3867 && (in_section->common.flags & SECTION_MERGE))
3868 assemble_align (align);
3870 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3871 done:
3872 #endif
3873 return;
3876 /* Mark all constants that are referenced by SYMBOL_REFs in X.
3877 Emit referenced deferred strings. */
3879 static void
3880 mark_constants_in_pattern (rtx insn)
3882 subrtx_iterator::array_type array;
3883 FOR_EACH_SUBRTX (iter, array, PATTERN (insn), ALL)
3885 const_rtx x = *iter;
3886 if (GET_CODE (x) == SYMBOL_REF)
3888 if (CONSTANT_POOL_ADDRESS_P (x))
3890 struct constant_descriptor_rtx *desc = SYMBOL_REF_CONSTANT (x);
3891 if (desc->mark == 0)
3893 desc->mark = 1;
3894 iter.substitute (desc->constant);
3897 else if (TREE_CONSTANT_POOL_ADDRESS_P (x))
3899 tree decl = SYMBOL_REF_DECL (x);
3900 if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
3902 n_deferred_constants--;
3903 output_constant_def_contents (CONST_CAST_RTX (x));
3910 /* Look through appropriate parts of INSN, marking all entries in the
3911 constant pool which are actually being used. Entries that are only
3912 referenced by other constants are also marked as used. Emit
3913 deferred strings that are used. */
3915 static void
3916 mark_constants (rtx_insn *insn)
3918 if (!INSN_P (insn))
3919 return;
3921 /* Insns may appear inside a SEQUENCE. Only check the patterns of
3922 insns, not any notes that may be attached. We don't want to mark
3923 a constant just because it happens to appear in a REG_EQUIV note. */
3924 if (rtx_sequence *seq = dyn_cast <rtx_sequence *> (PATTERN (insn)))
3926 int i, n = seq->len ();
3927 for (i = 0; i < n; ++i)
3929 rtx subinsn = seq->element (i);
3930 if (INSN_P (subinsn))
3931 mark_constants_in_pattern (subinsn);
3934 else
3935 mark_constants_in_pattern (insn);
3938 /* Look through the instructions for this function, and mark all the
3939 entries in POOL which are actually being used. Emit deferred constants
3940 which have indeed been used. */
3942 static void
3943 mark_constant_pool (void)
3945 rtx_insn *insn;
3947 if (!crtl->uses_const_pool && n_deferred_constants == 0)
3948 return;
3950 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3951 mark_constants (insn);
3954 /* Write all the constants in POOL. */
3956 static void
3957 output_constant_pool_contents (struct rtx_constant_pool *pool)
3959 struct constant_descriptor_rtx *desc;
3961 for (desc = pool->first; desc ; desc = desc->next)
3962 if (desc->mark)
3964 /* If the constant is part of an object_block, make sure that
3965 the constant has been positioned within its block, but do not
3966 write out its definition yet. output_object_blocks will do
3967 that later. */
3968 if (SYMBOL_REF_HAS_BLOCK_INFO_P (desc->sym)
3969 && SYMBOL_REF_BLOCK (desc->sym))
3970 place_block_symbol (desc->sym);
3971 else
3973 switch_to_section (targetm.asm_out.select_rtx_section
3974 (desc->mode, desc->constant, desc->align));
3975 output_constant_pool_1 (desc, desc->align);
3980 /* Mark all constants that are used in the current function, then write
3981 out the function's private constant pool. */
3983 static void
3984 output_constant_pool (const char *fnname ATTRIBUTE_UNUSED,
3985 tree fndecl ATTRIBUTE_UNUSED)
3987 struct rtx_constant_pool *pool = crtl->varasm.pool;
3989 /* It is possible for gcc to call force_const_mem and then to later
3990 discard the instructions which refer to the constant. In such a
3991 case we do not need to output the constant. */
3992 mark_constant_pool ();
3994 #ifdef ASM_OUTPUT_POOL_PROLOGUE
3995 ASM_OUTPUT_POOL_PROLOGUE (asm_out_file, fnname, fndecl, pool->offset);
3996 #endif
3998 output_constant_pool_contents (pool);
4000 #ifdef ASM_OUTPUT_POOL_EPILOGUE
4001 ASM_OUTPUT_POOL_EPILOGUE (asm_out_file, fnname, fndecl, pool->offset);
4002 #endif
4005 /* Write the contents of the shared constant pool. */
4007 void
4008 output_shared_constant_pool (void)
4010 output_constant_pool_contents (shared_constant_pool);
4013 /* Determine what kind of relocations EXP may need. */
4016 compute_reloc_for_constant (tree exp)
4018 int reloc = 0, reloc2;
4019 tree tem;
4021 switch (TREE_CODE (exp))
4023 case ADDR_EXPR:
4024 case FDESC_EXPR:
4025 /* Go inside any operations that get_inner_reference can handle and see
4026 if what's inside is a constant: no need to do anything here for
4027 addresses of variables or functions. */
4028 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
4029 tem = TREE_OPERAND (tem, 0))
4032 if (TREE_CODE (tem) == MEM_REF
4033 && TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR)
4035 reloc = compute_reloc_for_constant (TREE_OPERAND (tem, 0));
4036 break;
4039 if (!targetm.binds_local_p (tem))
4040 reloc |= 2;
4041 else
4042 reloc |= 1;
4043 break;
4045 case PLUS_EXPR:
4046 case POINTER_PLUS_EXPR:
4047 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4048 reloc |= compute_reloc_for_constant (TREE_OPERAND (exp, 1));
4049 break;
4051 case MINUS_EXPR:
4052 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4053 reloc2 = compute_reloc_for_constant (TREE_OPERAND (exp, 1));
4054 /* The difference of two local labels is computable at link time. */
4055 if (reloc == 1 && reloc2 == 1)
4056 reloc = 0;
4057 else
4058 reloc |= reloc2;
4059 break;
4061 CASE_CONVERT:
4062 case VIEW_CONVERT_EXPR:
4063 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4064 break;
4066 case CONSTRUCTOR:
4068 unsigned HOST_WIDE_INT idx;
4069 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
4070 if (tem != 0)
4071 reloc |= compute_reloc_for_constant (tem);
4073 break;
4075 default:
4076 break;
4078 return reloc;
4081 /* Find all the constants whose addresses are referenced inside of EXP,
4082 and make sure assembler code with a label has been output for each one.
4083 Indicate whether an ADDR_EXPR has been encountered. */
4085 static void
4086 output_addressed_constants (tree exp)
4088 tree tem;
4090 switch (TREE_CODE (exp))
4092 case ADDR_EXPR:
4093 case FDESC_EXPR:
4094 /* Go inside any operations that get_inner_reference can handle and see
4095 if what's inside is a constant: no need to do anything here for
4096 addresses of variables or functions. */
4097 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
4098 tem = TREE_OPERAND (tem, 0))
4101 /* If we have an initialized CONST_DECL, retrieve the initializer. */
4102 if (TREE_CODE (tem) == CONST_DECL && DECL_INITIAL (tem))
4103 tem = DECL_INITIAL (tem);
4105 if (CONSTANT_CLASS_P (tem) || TREE_CODE (tem) == CONSTRUCTOR)
4106 output_constant_def (tem, 0);
4108 if (TREE_CODE (tem) == MEM_REF)
4109 output_addressed_constants (TREE_OPERAND (tem, 0));
4110 break;
4112 case PLUS_EXPR:
4113 case POINTER_PLUS_EXPR:
4114 case MINUS_EXPR:
4115 output_addressed_constants (TREE_OPERAND (exp, 1));
4116 /* Fall through. */
4118 CASE_CONVERT:
4119 case VIEW_CONVERT_EXPR:
4120 output_addressed_constants (TREE_OPERAND (exp, 0));
4121 break;
4123 case CONSTRUCTOR:
4125 unsigned HOST_WIDE_INT idx;
4126 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
4127 if (tem != 0)
4128 output_addressed_constants (tem);
4130 break;
4132 default:
4133 break;
4137 /* Whether a constructor CTOR is a valid static constant initializer if all
4138 its elements are. This used to be internal to initializer_constant_valid_p
4139 and has been exposed to let other functions like categorize_ctor_elements
4140 evaluate the property while walking a constructor for other purposes. */
4142 bool
4143 constructor_static_from_elts_p (const_tree ctor)
4145 return (TREE_CONSTANT (ctor)
4146 && (TREE_CODE (TREE_TYPE (ctor)) == UNION_TYPE
4147 || TREE_CODE (TREE_TYPE (ctor)) == RECORD_TYPE
4148 || TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE));
4151 static tree initializer_constant_valid_p_1 (tree value, tree endtype,
4152 tree *cache);
4154 /* A subroutine of initializer_constant_valid_p. VALUE is a MINUS_EXPR,
4155 PLUS_EXPR or POINTER_PLUS_EXPR. This looks for cases of VALUE
4156 which are valid when ENDTYPE is an integer of any size; in
4157 particular, this does not accept a pointer minus a constant. This
4158 returns null_pointer_node if the VALUE is an absolute constant
4159 which can be used to initialize a static variable. Otherwise it
4160 returns NULL. */
4162 static tree
4163 narrowing_initializer_constant_valid_p (tree value, tree endtype, tree *cache)
4165 tree op0, op1;
4167 if (!INTEGRAL_TYPE_P (endtype))
4168 return NULL_TREE;
4170 op0 = TREE_OPERAND (value, 0);
4171 op1 = TREE_OPERAND (value, 1);
4173 /* Like STRIP_NOPS except allow the operand mode to widen. This
4174 works around a feature of fold that simplifies (int)(p1 - p2) to
4175 ((int)p1 - (int)p2) under the theory that the narrower operation
4176 is cheaper. */
4178 while (CONVERT_EXPR_P (op0)
4179 || TREE_CODE (op0) == NON_LVALUE_EXPR)
4181 tree inner = TREE_OPERAND (op0, 0);
4182 if (inner == error_mark_node
4183 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4184 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))
4185 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
4186 break;
4187 op0 = inner;
4190 while (CONVERT_EXPR_P (op1)
4191 || TREE_CODE (op1) == NON_LVALUE_EXPR)
4193 tree inner = TREE_OPERAND (op1, 0);
4194 if (inner == error_mark_node
4195 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4196 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op1)))
4197 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
4198 break;
4199 op1 = inner;
4202 op0 = initializer_constant_valid_p_1 (op0, endtype, cache);
4203 if (!op0)
4204 return NULL_TREE;
4206 op1 = initializer_constant_valid_p_1 (op1, endtype,
4207 cache ? cache + 2 : NULL);
4208 /* Both initializers must be known. */
4209 if (op1)
4211 if (op0 == op1
4212 && (op0 == null_pointer_node
4213 || TREE_CODE (value) == MINUS_EXPR))
4214 return null_pointer_node;
4216 /* Support differences between labels. */
4217 if (TREE_CODE (op0) == LABEL_DECL
4218 && TREE_CODE (op1) == LABEL_DECL)
4219 return null_pointer_node;
4221 if (TREE_CODE (op0) == STRING_CST
4222 && TREE_CODE (op1) == STRING_CST
4223 && operand_equal_p (op0, op1, 1))
4224 return null_pointer_node;
4227 return NULL_TREE;
4230 /* Helper function of initializer_constant_valid_p.
4231 Return nonzero if VALUE is a valid constant-valued expression
4232 for use in initializing a static variable; one that can be an
4233 element of a "constant" initializer.
4235 Return null_pointer_node if the value is absolute;
4236 if it is relocatable, return the variable that determines the relocation.
4237 We assume that VALUE has been folded as much as possible;
4238 therefore, we do not need to check for such things as
4239 arithmetic-combinations of integers.
4241 Use CACHE (pointer to 2 tree values) for caching if non-NULL. */
4243 static tree
4244 initializer_constant_valid_p_1 (tree value, tree endtype, tree *cache)
4246 tree ret;
4248 switch (TREE_CODE (value))
4250 case CONSTRUCTOR:
4251 if (constructor_static_from_elts_p (value))
4253 unsigned HOST_WIDE_INT idx;
4254 tree elt;
4255 bool absolute = true;
4257 if (cache && cache[0] == value)
4258 return cache[1];
4259 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
4261 tree reloc;
4262 reloc = initializer_constant_valid_p_1 (elt, TREE_TYPE (elt),
4263 NULL);
4264 if (!reloc)
4266 if (cache)
4268 cache[0] = value;
4269 cache[1] = NULL_TREE;
4271 return NULL_TREE;
4273 if (reloc != null_pointer_node)
4274 absolute = false;
4276 /* For a non-absolute relocation, there is no single
4277 variable that can be "the variable that determines the
4278 relocation." */
4279 if (cache)
4281 cache[0] = value;
4282 cache[1] = absolute ? null_pointer_node : error_mark_node;
4284 return absolute ? null_pointer_node : error_mark_node;
4287 return TREE_STATIC (value) ? null_pointer_node : NULL_TREE;
4289 case INTEGER_CST:
4290 case VECTOR_CST:
4291 case REAL_CST:
4292 case FIXED_CST:
4293 case STRING_CST:
4294 case COMPLEX_CST:
4295 return null_pointer_node;
4297 case ADDR_EXPR:
4298 case FDESC_EXPR:
4300 tree op0 = staticp (TREE_OPERAND (value, 0));
4301 if (op0)
4303 /* "&(*a).f" is like unto pointer arithmetic. If "a" turns out
4304 to be a constant, this is old-skool offsetof-like nonsense. */
4305 if (TREE_CODE (op0) == INDIRECT_REF
4306 && TREE_CONSTANT (TREE_OPERAND (op0, 0)))
4307 return null_pointer_node;
4308 /* Taking the address of a nested function involves a trampoline,
4309 unless we don't need or want one. */
4310 if (TREE_CODE (op0) == FUNCTION_DECL
4311 && DECL_STATIC_CHAIN (op0)
4312 && !TREE_NO_TRAMPOLINE (value))
4313 return NULL_TREE;
4314 /* "&{...}" requires a temporary to hold the constructed
4315 object. */
4316 if (TREE_CODE (op0) == CONSTRUCTOR)
4317 return NULL_TREE;
4319 return op0;
4322 case NON_LVALUE_EXPR:
4323 return initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4324 endtype, cache);
4326 case VIEW_CONVERT_EXPR:
4328 tree src = TREE_OPERAND (value, 0);
4329 tree src_type = TREE_TYPE (src);
4330 tree dest_type = TREE_TYPE (value);
4332 /* Allow view-conversions from aggregate to non-aggregate type only
4333 if the bit pattern is fully preserved afterwards; otherwise, the
4334 RTL expander won't be able to apply a subsequent transformation
4335 to the underlying constructor. */
4336 if (AGGREGATE_TYPE_P (src_type) && !AGGREGATE_TYPE_P (dest_type))
4338 if (TYPE_MODE (endtype) == TYPE_MODE (dest_type))
4339 return initializer_constant_valid_p_1 (src, endtype, cache);
4340 else
4341 return NULL_TREE;
4344 /* Allow all other kinds of view-conversion. */
4345 return initializer_constant_valid_p_1 (src, endtype, cache);
4348 CASE_CONVERT:
4350 tree src = TREE_OPERAND (value, 0);
4351 tree src_type = TREE_TYPE (src);
4352 tree dest_type = TREE_TYPE (value);
4354 /* Allow conversions between pointer types, floating-point
4355 types, and offset types. */
4356 if ((POINTER_TYPE_P (dest_type) && POINTER_TYPE_P (src_type))
4357 || (FLOAT_TYPE_P (dest_type) && FLOAT_TYPE_P (src_type))
4358 || (TREE_CODE (dest_type) == OFFSET_TYPE
4359 && TREE_CODE (src_type) == OFFSET_TYPE))
4360 return initializer_constant_valid_p_1 (src, endtype, cache);
4362 /* Allow length-preserving conversions between integer types. */
4363 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type)
4364 && (TYPE_PRECISION (dest_type) == TYPE_PRECISION (src_type)))
4365 return initializer_constant_valid_p_1 (src, endtype, cache);
4367 /* Allow conversions between other integer types only if
4368 explicit value. */
4369 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type))
4371 tree inner = initializer_constant_valid_p_1 (src, endtype, cache);
4372 if (inner == null_pointer_node)
4373 return null_pointer_node;
4374 break;
4377 /* Allow (int) &foo provided int is as wide as a pointer. */
4378 if (INTEGRAL_TYPE_P (dest_type) && POINTER_TYPE_P (src_type)
4379 && (TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type)))
4380 return initializer_constant_valid_p_1 (src, endtype, cache);
4382 /* Likewise conversions from int to pointers, but also allow
4383 conversions from 0. */
4384 if ((POINTER_TYPE_P (dest_type)
4385 || TREE_CODE (dest_type) == OFFSET_TYPE)
4386 && INTEGRAL_TYPE_P (src_type))
4388 if (TREE_CODE (src) == INTEGER_CST
4389 && TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type))
4390 return null_pointer_node;
4391 if (integer_zerop (src))
4392 return null_pointer_node;
4393 else if (TYPE_PRECISION (dest_type) <= TYPE_PRECISION (src_type))
4394 return initializer_constant_valid_p_1 (src, endtype, cache);
4397 /* Allow conversions to struct or union types if the value
4398 inside is okay. */
4399 if (TREE_CODE (dest_type) == RECORD_TYPE
4400 || TREE_CODE (dest_type) == UNION_TYPE)
4401 return initializer_constant_valid_p_1 (src, endtype, cache);
4403 break;
4405 case POINTER_PLUS_EXPR:
4406 case PLUS_EXPR:
4407 /* Any valid floating-point constants will have been folded by now;
4408 with -frounding-math we hit this with addition of two constants. */
4409 if (TREE_CODE (endtype) == REAL_TYPE)
4410 return NULL_TREE;
4411 if (cache && cache[0] == value)
4412 return cache[1];
4413 if (! INTEGRAL_TYPE_P (endtype)
4414 || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
4416 tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
4417 tree valid0
4418 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4419 endtype, ncache);
4420 tree valid1
4421 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
4422 endtype, ncache + 2);
4423 /* If either term is absolute, use the other term's relocation. */
4424 if (valid0 == null_pointer_node)
4425 ret = valid1;
4426 else if (valid1 == null_pointer_node)
4427 ret = valid0;
4428 /* Support narrowing pointer differences. */
4429 else
4430 ret = narrowing_initializer_constant_valid_p (value, endtype,
4431 ncache);
4433 else
4434 /* Support narrowing pointer differences. */
4435 ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
4436 if (cache)
4438 cache[0] = value;
4439 cache[1] = ret;
4441 return ret;
4443 case MINUS_EXPR:
4444 if (TREE_CODE (endtype) == REAL_TYPE)
4445 return NULL_TREE;
4446 if (cache && cache[0] == value)
4447 return cache[1];
4448 if (! INTEGRAL_TYPE_P (endtype)
4449 || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
4451 tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
4452 tree valid0
4453 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4454 endtype, ncache);
4455 tree valid1
4456 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
4457 endtype, ncache + 2);
4458 /* Win if second argument is absolute. */
4459 if (valid1 == null_pointer_node)
4460 ret = valid0;
4461 /* Win if both arguments have the same relocation.
4462 Then the value is absolute. */
4463 else if (valid0 == valid1 && valid0 != 0)
4464 ret = null_pointer_node;
4465 /* Since GCC guarantees that string constants are unique in the
4466 generated code, a subtraction between two copies of the same
4467 constant string is absolute. */
4468 else if (valid0 && TREE_CODE (valid0) == STRING_CST
4469 && valid1 && TREE_CODE (valid1) == STRING_CST
4470 && operand_equal_p (valid0, valid1, 1))
4471 ret = null_pointer_node;
4472 /* Support narrowing differences. */
4473 else
4474 ret = narrowing_initializer_constant_valid_p (value, endtype,
4475 ncache);
4477 else
4478 /* Support narrowing differences. */
4479 ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
4480 if (cache)
4482 cache[0] = value;
4483 cache[1] = ret;
4485 return ret;
4487 default:
4488 break;
4491 return NULL_TREE;
4494 /* Return nonzero if VALUE is a valid constant-valued expression
4495 for use in initializing a static variable; one that can be an
4496 element of a "constant" initializer.
4498 Return null_pointer_node if the value is absolute;
4499 if it is relocatable, return the variable that determines the relocation.
4500 We assume that VALUE has been folded as much as possible;
4501 therefore, we do not need to check for such things as
4502 arithmetic-combinations of integers. */
4503 tree
4504 initializer_constant_valid_p (tree value, tree endtype)
4506 return initializer_constant_valid_p_1 (value, endtype, NULL);
4509 /* Return true if VALUE is a valid constant-valued expression
4510 for use in initializing a static bit-field; one that can be
4511 an element of a "constant" initializer. */
4513 bool
4514 initializer_constant_valid_for_bitfield_p (tree value)
4516 /* For bitfields we support integer constants or possibly nested aggregates
4517 of such. */
4518 switch (TREE_CODE (value))
4520 case CONSTRUCTOR:
4522 unsigned HOST_WIDE_INT idx;
4523 tree elt;
4525 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
4526 if (!initializer_constant_valid_for_bitfield_p (elt))
4527 return false;
4528 return true;
4531 case INTEGER_CST:
4532 case REAL_CST:
4533 return true;
4535 case VIEW_CONVERT_EXPR:
4536 case NON_LVALUE_EXPR:
4537 return
4538 initializer_constant_valid_for_bitfield_p (TREE_OPERAND (value, 0));
4540 default:
4541 break;
4544 return false;
4547 /* output_constructor outer state of relevance in recursive calls, typically
4548 for nested aggregate bitfields. */
4550 typedef struct {
4551 unsigned int bit_offset; /* current position in ... */
4552 int byte; /* ... the outer byte buffer. */
4553 } oc_outer_state;
4555 static unsigned HOST_WIDE_INT
4556 output_constructor (tree, unsigned HOST_WIDE_INT, unsigned int,
4557 oc_outer_state *);
4559 /* Output assembler code for constant EXP, with no label.
4560 This includes the pseudo-op such as ".int" or ".byte", and a newline.
4561 Assumes output_addressed_constants has been done on EXP already.
4563 Generate at least SIZE bytes of assembler data, padding at the end
4564 with zeros if necessary. SIZE must always be specified. The returned
4565 value is the actual number of bytes of assembler data generated, which
4566 may be bigger than SIZE if the object contains a variable length field.
4568 SIZE is important for structure constructors,
4569 since trailing members may have been omitted from the constructor.
4570 It is also important for initialization of arrays from string constants
4571 since the full length of the string constant might not be wanted.
4572 It is also needed for initialization of unions, where the initializer's
4573 type is just one member, and that may not be as long as the union.
4575 There a case in which we would fail to output exactly SIZE bytes:
4576 for a structure constructor that wants to produce more than SIZE bytes.
4577 But such constructors will never be generated for any possible input.
4579 ALIGN is the alignment of the data in bits. */
4581 static unsigned HOST_WIDE_INT
4582 output_constant (tree exp, unsigned HOST_WIDE_INT size, unsigned int align)
4584 enum tree_code code;
4585 unsigned HOST_WIDE_INT thissize;
4587 if (size == 0 || flag_syntax_only)
4588 return size;
4590 /* See if we're trying to initialize a pointer in a non-default mode
4591 to the address of some declaration somewhere. If the target says
4592 the mode is valid for pointers, assume the target has a way of
4593 resolving it. */
4594 if (TREE_CODE (exp) == NOP_EXPR
4595 && POINTER_TYPE_P (TREE_TYPE (exp))
4596 && targetm.addr_space.valid_pointer_mode
4597 (TYPE_MODE (TREE_TYPE (exp)),
4598 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
4600 tree saved_type = TREE_TYPE (exp);
4602 /* Peel off any intermediate conversions-to-pointer for valid
4603 pointer modes. */
4604 while (TREE_CODE (exp) == NOP_EXPR
4605 && POINTER_TYPE_P (TREE_TYPE (exp))
4606 && targetm.addr_space.valid_pointer_mode
4607 (TYPE_MODE (TREE_TYPE (exp)),
4608 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
4609 exp = TREE_OPERAND (exp, 0);
4611 /* If what we're left with is the address of something, we can
4612 convert the address to the final type and output it that
4613 way. */
4614 if (TREE_CODE (exp) == ADDR_EXPR)
4615 exp = build1 (ADDR_EXPR, saved_type, TREE_OPERAND (exp, 0));
4616 /* Likewise for constant ints. */
4617 else if (TREE_CODE (exp) == INTEGER_CST)
4618 exp = wide_int_to_tree (saved_type, exp);
4622 /* Eliminate any conversions since we'll be outputting the underlying
4623 constant. */
4624 while (CONVERT_EXPR_P (exp)
4625 || TREE_CODE (exp) == NON_LVALUE_EXPR
4626 || TREE_CODE (exp) == VIEW_CONVERT_EXPR)
4628 HOST_WIDE_INT type_size = int_size_in_bytes (TREE_TYPE (exp));
4629 HOST_WIDE_INT op_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (exp, 0)));
4631 /* Make sure eliminating the conversion is really a no-op, except with
4632 VIEW_CONVERT_EXPRs to allow for wild Ada unchecked conversions and
4633 union types to allow for Ada unchecked unions. */
4634 if (type_size > op_size
4635 && TREE_CODE (exp) != VIEW_CONVERT_EXPR
4636 && TREE_CODE (TREE_TYPE (exp)) != UNION_TYPE)
4637 /* Keep the conversion. */
4638 break;
4639 else
4640 exp = TREE_OPERAND (exp, 0);
4643 code = TREE_CODE (TREE_TYPE (exp));
4644 thissize = int_size_in_bytes (TREE_TYPE (exp));
4646 /* Allow a constructor with no elements for any data type.
4647 This means to fill the space with zeros. */
4648 if (TREE_CODE (exp) == CONSTRUCTOR
4649 && vec_safe_is_empty (CONSTRUCTOR_ELTS (exp)))
4651 assemble_zeros (size);
4652 return size;
4655 if (TREE_CODE (exp) == FDESC_EXPR)
4657 #ifdef ASM_OUTPUT_FDESC
4658 HOST_WIDE_INT part = tree_to_shwi (TREE_OPERAND (exp, 1));
4659 tree decl = TREE_OPERAND (exp, 0);
4660 ASM_OUTPUT_FDESC (asm_out_file, decl, part);
4661 #else
4662 gcc_unreachable ();
4663 #endif
4664 return size;
4667 /* Now output the underlying data. If we've handling the padding, return.
4668 Otherwise, break and ensure SIZE is the size written. */
4669 switch (code)
4671 case BOOLEAN_TYPE:
4672 case INTEGER_TYPE:
4673 case ENUMERAL_TYPE:
4674 case POINTER_TYPE:
4675 case REFERENCE_TYPE:
4676 case OFFSET_TYPE:
4677 case FIXED_POINT_TYPE:
4678 case NULLPTR_TYPE:
4679 if (! assemble_integer (expand_expr (exp, NULL_RTX, VOIDmode,
4680 EXPAND_INITIALIZER),
4681 MIN (size, thissize), align, 0))
4682 error ("initializer for integer/fixed-point value is too complicated");
4683 break;
4685 case REAL_TYPE:
4686 if (TREE_CODE (exp) != REAL_CST)
4687 error ("initializer for floating value is not a floating constant");
4688 else
4689 assemble_real (TREE_REAL_CST (exp), TYPE_MODE (TREE_TYPE (exp)), align);
4690 break;
4692 case COMPLEX_TYPE:
4693 output_constant (TREE_REALPART (exp), thissize / 2, align);
4694 output_constant (TREE_IMAGPART (exp), thissize / 2,
4695 min_align (align, BITS_PER_UNIT * (thissize / 2)));
4696 break;
4698 case ARRAY_TYPE:
4699 case VECTOR_TYPE:
4700 switch (TREE_CODE (exp))
4702 case CONSTRUCTOR:
4703 return output_constructor (exp, size, align, NULL);
4704 case STRING_CST:
4705 thissize
4706 = MIN ((unsigned HOST_WIDE_INT)TREE_STRING_LENGTH (exp), size);
4707 assemble_string (TREE_STRING_POINTER (exp), thissize);
4708 break;
4709 case VECTOR_CST:
4711 enum machine_mode inner = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
4712 unsigned int nalign = MIN (align, GET_MODE_ALIGNMENT (inner));
4713 int elt_size = GET_MODE_SIZE (inner);
4714 output_constant (VECTOR_CST_ELT (exp, 0), elt_size, align);
4715 thissize = elt_size;
4716 for (unsigned int i = 1; i < VECTOR_CST_NELTS (exp); i++)
4718 output_constant (VECTOR_CST_ELT (exp, i), elt_size, nalign);
4719 thissize += elt_size;
4721 break;
4723 default:
4724 gcc_unreachable ();
4726 break;
4728 case RECORD_TYPE:
4729 case UNION_TYPE:
4730 gcc_assert (TREE_CODE (exp) == CONSTRUCTOR);
4731 return output_constructor (exp, size, align, NULL);
4733 case ERROR_MARK:
4734 return 0;
4736 default:
4737 gcc_unreachable ();
4740 if (size > thissize)
4741 assemble_zeros (size - thissize);
4743 return size;
4747 /* Subroutine of output_constructor, used for computing the size of
4748 arrays of unspecified length. VAL must be a CONSTRUCTOR of an array
4749 type with an unspecified upper bound. */
4751 static unsigned HOST_WIDE_INT
4752 array_size_for_constructor (tree val)
4754 tree max_index;
4755 unsigned HOST_WIDE_INT cnt;
4756 tree index, value, tmp;
4757 offset_int i;
4759 /* This code used to attempt to handle string constants that are not
4760 arrays of single-bytes, but nothing else does, so there's no point in
4761 doing it here. */
4762 if (TREE_CODE (val) == STRING_CST)
4763 return TREE_STRING_LENGTH (val);
4765 max_index = NULL_TREE;
4766 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (val), cnt, index, value)
4768 if (TREE_CODE (index) == RANGE_EXPR)
4769 index = TREE_OPERAND (index, 1);
4770 if (max_index == NULL_TREE || tree_int_cst_lt (max_index, index))
4771 max_index = index;
4774 if (max_index == NULL_TREE)
4775 return 0;
4777 /* Compute the total number of array elements. */
4778 tmp = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (val)));
4779 i = wi::to_offset (max_index) - wi::to_offset (tmp) + 1;
4781 /* Multiply by the array element unit size to find number of bytes. */
4782 i *= wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (val))));
4784 gcc_assert (wi::fits_uhwi_p (i));
4785 return i.to_uhwi ();
4788 /* Other datastructures + helpers for output_constructor. */
4790 /* output_constructor local state to support interaction with helpers. */
4792 typedef struct {
4794 /* Received arguments. */
4795 tree exp; /* Constructor expression. */
4796 tree type; /* Type of constructor expression. */
4797 unsigned HOST_WIDE_INT size; /* # bytes to output - pad if necessary. */
4798 unsigned int align; /* Known initial alignment. */
4799 tree min_index; /* Lower bound if specified for an array. */
4801 /* Output processing state. */
4802 HOST_WIDE_INT total_bytes; /* # bytes output so far / current position. */
4803 int byte; /* Part of a bitfield byte yet to be output. */
4804 int last_relative_index; /* Implicit or explicit index of the last
4805 array element output within a bitfield. */
4806 bool byte_buffer_in_use; /* Whether BYTE is in use. */
4808 /* Current element. */
4809 tree field; /* Current field decl in a record. */
4810 tree val; /* Current element value. */
4811 tree index; /* Current element index. */
4813 } oc_local_state;
4815 /* Helper for output_constructor. From the current LOCAL state, output a
4816 RANGE_EXPR element. */
4818 static void
4819 output_constructor_array_range (oc_local_state *local)
4821 unsigned HOST_WIDE_INT fieldsize
4822 = int_size_in_bytes (TREE_TYPE (local->type));
4824 HOST_WIDE_INT lo_index
4825 = tree_to_shwi (TREE_OPERAND (local->index, 0));
4826 HOST_WIDE_INT hi_index
4827 = tree_to_shwi (TREE_OPERAND (local->index, 1));
4828 HOST_WIDE_INT index;
4830 unsigned int align2
4831 = min_align (local->align, fieldsize * BITS_PER_UNIT);
4833 for (index = lo_index; index <= hi_index; index++)
4835 /* Output the element's initial value. */
4836 if (local->val == NULL_TREE)
4837 assemble_zeros (fieldsize);
4838 else
4839 fieldsize = output_constant (local->val, fieldsize, align2);
4841 /* Count its size. */
4842 local->total_bytes += fieldsize;
4846 /* Helper for output_constructor. From the current LOCAL state, output a
4847 field element that is not true bitfield or part of an outer one. */
4849 static void
4850 output_constructor_regular_field (oc_local_state *local)
4852 /* Field size and position. Since this structure is static, we know the
4853 positions are constant. */
4854 unsigned HOST_WIDE_INT fieldsize;
4855 HOST_WIDE_INT fieldpos;
4857 unsigned int align2;
4859 if (local->index != NULL_TREE)
4861 /* Perform the index calculation in modulo arithmetic but
4862 sign-extend the result because Ada has negative DECL_FIELD_OFFSETs
4863 but we are using an unsigned sizetype. */
4864 unsigned prec = TYPE_PRECISION (sizetype);
4865 offset_int idx = wi::sext (wi::to_offset (local->index)
4866 - wi::to_offset (local->min_index), prec);
4867 fieldpos = (idx * wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (local->val))))
4868 .to_short_addr ();
4870 else if (local->field != NULL_TREE)
4871 fieldpos = int_byte_position (local->field);
4872 else
4873 fieldpos = 0;
4875 /* Output any buffered-up bit-fields preceding this element. */
4876 if (local->byte_buffer_in_use)
4878 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
4879 local->total_bytes++;
4880 local->byte_buffer_in_use = false;
4883 /* Advance to offset of this element.
4884 Note no alignment needed in an array, since that is guaranteed
4885 if each element has the proper size. */
4886 if ((local->field != NULL_TREE || local->index != NULL_TREE)
4887 && fieldpos > local->total_bytes)
4889 assemble_zeros (fieldpos - local->total_bytes);
4890 local->total_bytes = fieldpos;
4893 /* Find the alignment of this element. */
4894 align2 = min_align (local->align, BITS_PER_UNIT * fieldpos);
4896 /* Determine size this element should occupy. */
4897 if (local->field)
4899 fieldsize = 0;
4901 /* If this is an array with an unspecified upper bound,
4902 the initializer determines the size. */
4903 /* ??? This ought to only checked if DECL_SIZE_UNIT is NULL,
4904 but we cannot do this until the deprecated support for
4905 initializing zero-length array members is removed. */
4906 if (TREE_CODE (TREE_TYPE (local->field)) == ARRAY_TYPE
4907 && TYPE_DOMAIN (TREE_TYPE (local->field))
4908 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (local->field))))
4910 fieldsize = array_size_for_constructor (local->val);
4911 /* Given a non-empty initialization, this field had
4912 better be last. */
4913 gcc_assert (!fieldsize || !DECL_CHAIN (local->field));
4915 else
4916 fieldsize = tree_to_uhwi (DECL_SIZE_UNIT (local->field));
4918 else
4919 fieldsize = int_size_in_bytes (TREE_TYPE (local->type));
4921 /* Output the element's initial value. */
4922 if (local->val == NULL_TREE)
4923 assemble_zeros (fieldsize);
4924 else
4925 fieldsize = output_constant (local->val, fieldsize, align2);
4927 /* Count its size. */
4928 local->total_bytes += fieldsize;
4931 /* Helper for output_constructor. From the LOCAL state, output an element
4932 that is a true bitfield or part of an outer one. BIT_OFFSET is the offset
4933 from the start of a possibly ongoing outer byte buffer. */
4935 static void
4936 output_constructor_bitfield (oc_local_state *local, unsigned int bit_offset)
4938 /* Bit size of this element. */
4939 HOST_WIDE_INT ebitsize
4940 = (local->field
4941 ? tree_to_uhwi (DECL_SIZE (local->field))
4942 : tree_to_uhwi (TYPE_SIZE (TREE_TYPE (local->type))));
4944 /* Relative index of this element if this is an array component. */
4945 HOST_WIDE_INT relative_index
4946 = (!local->field
4947 ? (local->index
4948 ? (tree_to_shwi (local->index)
4949 - tree_to_shwi (local->min_index))
4950 : local->last_relative_index + 1)
4951 : 0);
4953 /* Bit position of this element from the start of the containing
4954 constructor. */
4955 HOST_WIDE_INT constructor_relative_ebitpos
4956 = (local->field
4957 ? int_bit_position (local->field)
4958 : ebitsize * relative_index);
4960 /* Bit position of this element from the start of a possibly ongoing
4961 outer byte buffer. */
4962 HOST_WIDE_INT byte_relative_ebitpos
4963 = bit_offset + constructor_relative_ebitpos;
4965 /* From the start of a possibly ongoing outer byte buffer, offsets to
4966 the first bit of this element and to the first bit past the end of
4967 this element. */
4968 HOST_WIDE_INT next_offset = byte_relative_ebitpos;
4969 HOST_WIDE_INT end_offset = byte_relative_ebitpos + ebitsize;
4971 local->last_relative_index = relative_index;
4973 if (local->val == NULL_TREE)
4974 local->val = integer_zero_node;
4976 while (TREE_CODE (local->val) == VIEW_CONVERT_EXPR
4977 || TREE_CODE (local->val) == NON_LVALUE_EXPR)
4978 local->val = TREE_OPERAND (local->val, 0);
4980 if (TREE_CODE (local->val) != INTEGER_CST
4981 && TREE_CODE (local->val) != CONSTRUCTOR)
4983 error ("invalid initial value for member %qE", DECL_NAME (local->field));
4984 return;
4987 /* If this field does not start in this (or next) byte, skip some bytes. */
4988 if (next_offset / BITS_PER_UNIT != local->total_bytes)
4990 /* Output remnant of any bit field in previous bytes. */
4991 if (local->byte_buffer_in_use)
4993 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
4994 local->total_bytes++;
4995 local->byte_buffer_in_use = false;
4998 /* If still not at proper byte, advance to there. */
4999 if (next_offset / BITS_PER_UNIT != local->total_bytes)
5001 gcc_assert (next_offset / BITS_PER_UNIT >= local->total_bytes);
5002 assemble_zeros (next_offset / BITS_PER_UNIT - local->total_bytes);
5003 local->total_bytes = next_offset / BITS_PER_UNIT;
5007 /* Set up the buffer if necessary. */
5008 if (!local->byte_buffer_in_use)
5010 local->byte = 0;
5011 if (ebitsize > 0)
5012 local->byte_buffer_in_use = true;
5015 /* If this is nested constructor, recurse passing the bit offset and the
5016 pending data, then retrieve the new pending data afterwards. */
5017 if (TREE_CODE (local->val) == CONSTRUCTOR)
5019 oc_outer_state temp_state;
5020 temp_state.bit_offset = next_offset % BITS_PER_UNIT;
5021 temp_state.byte = local->byte;
5022 local->total_bytes
5023 += output_constructor (local->val, 0, 0, &temp_state);
5024 local->byte = temp_state.byte;
5025 return;
5028 /* Otherwise, we must split the element into pieces that fall within
5029 separate bytes, and combine each byte with previous or following
5030 bit-fields. */
5031 while (next_offset < end_offset)
5033 int this_time;
5034 int shift;
5035 HOST_WIDE_INT value;
5036 HOST_WIDE_INT next_byte = next_offset / BITS_PER_UNIT;
5037 HOST_WIDE_INT next_bit = next_offset % BITS_PER_UNIT;
5039 /* Advance from byte to byte within this element when necessary. */
5040 while (next_byte != local->total_bytes)
5042 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5043 local->total_bytes++;
5044 local->byte = 0;
5047 /* Number of bits we can process at once (all part of the same byte). */
5048 this_time = MIN (end_offset - next_offset, BITS_PER_UNIT - next_bit);
5049 if (BYTES_BIG_ENDIAN)
5051 /* On big-endian machine, take the most significant bits (of the
5052 bits that are significant) first and put them into bytes from
5053 the most significant end. */
5054 shift = end_offset - next_offset - this_time;
5056 /* Don't try to take a bunch of bits that cross
5057 the word boundary in the INTEGER_CST. We can
5058 only select bits from one element. */
5059 if ((shift / HOST_BITS_PER_WIDE_INT)
5060 != ((shift + this_time - 1) / HOST_BITS_PER_WIDE_INT))
5062 const int end = shift + this_time - 1;
5063 shift = end & -HOST_BITS_PER_WIDE_INT;
5064 this_time = end - shift + 1;
5067 /* Now get the bits from the appropriate constant word. */
5068 value = TREE_INT_CST_ELT (local->val, shift / HOST_BITS_PER_WIDE_INT);
5069 shift = shift & (HOST_BITS_PER_WIDE_INT - 1);
5071 /* Get the result. This works only when:
5072 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
5073 local->byte |= (((value >> shift)
5074 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
5075 << (BITS_PER_UNIT - this_time - next_bit));
5077 else
5079 /* On little-endian machines, take the least significant bits of
5080 the value first and pack them starting at the least significant
5081 bits of the bytes. */
5082 shift = next_offset - byte_relative_ebitpos;
5084 /* Don't try to take a bunch of bits that cross
5085 the word boundary in the INTEGER_CST. We can
5086 only select bits from one element. */
5087 if ((shift / HOST_BITS_PER_WIDE_INT)
5088 != ((shift + this_time - 1) / HOST_BITS_PER_WIDE_INT))
5089 this_time
5090 = HOST_BITS_PER_WIDE_INT - (shift & (HOST_BITS_PER_WIDE_INT - 1));
5092 /* Now get the bits from the appropriate constant word. */
5093 value = TREE_INT_CST_ELT (local->val, shift / HOST_BITS_PER_WIDE_INT);
5094 shift = shift & (HOST_BITS_PER_WIDE_INT - 1);
5096 /* Get the result. This works only when:
5097 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
5098 local->byte |= (((value >> shift)
5099 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
5100 << next_bit);
5103 next_offset += this_time;
5104 local->byte_buffer_in_use = true;
5108 /* Subroutine of output_constant, used for CONSTRUCTORs (aggregate constants).
5109 Generate at least SIZE bytes, padding if necessary. OUTER designates the
5110 caller output state of relevance in recursive invocations. */
5112 static unsigned HOST_WIDE_INT
5113 output_constructor (tree exp, unsigned HOST_WIDE_INT size,
5114 unsigned int align, oc_outer_state *outer)
5116 unsigned HOST_WIDE_INT cnt;
5117 constructor_elt *ce;
5119 oc_local_state local;
5121 /* Setup our local state to communicate with helpers. */
5122 local.exp = exp;
5123 local.type = TREE_TYPE (exp);
5124 local.size = size;
5125 local.align = align;
5126 if (TREE_CODE (local.type) == ARRAY_TYPE && TYPE_DOMAIN (local.type))
5127 local.min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (local.type));
5128 else
5129 local.min_index = NULL_TREE;
5131 local.total_bytes = 0;
5132 local.byte_buffer_in_use = outer != NULL;
5133 local.byte = outer ? outer->byte : 0;
5134 local.last_relative_index = -1;
5136 gcc_assert (HOST_BITS_PER_WIDE_INT >= BITS_PER_UNIT);
5138 /* As CE goes through the elements of the constant, FIELD goes through the
5139 structure fields if the constant is a structure. If the constant is a
5140 union, we override this by getting the field from the TREE_LIST element.
5141 But the constant could also be an array. Then FIELD is zero.
5143 There is always a maximum of one element in the chain LINK for unions
5144 (even if the initializer in a source program incorrectly contains
5145 more one). */
5147 if (TREE_CODE (local.type) == RECORD_TYPE)
5148 local.field = TYPE_FIELDS (local.type);
5149 else
5150 local.field = NULL_TREE;
5152 for (cnt = 0;
5153 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), cnt, &ce);
5154 cnt++, local.field = local.field ? DECL_CHAIN (local.field) : 0)
5156 local.val = ce->value;
5157 local.index = NULL_TREE;
5159 /* The element in a union constructor specifies the proper field
5160 or index. */
5161 if (RECORD_OR_UNION_TYPE_P (local.type) && ce->index != NULL_TREE)
5162 local.field = ce->index;
5164 else if (TREE_CODE (local.type) == ARRAY_TYPE)
5165 local.index = ce->index;
5167 if (local.field && flag_verbose_asm)
5168 fprintf (asm_out_file, "%s %s:\n",
5169 ASM_COMMENT_START,
5170 DECL_NAME (local.field)
5171 ? IDENTIFIER_POINTER (DECL_NAME (local.field))
5172 : "<anonymous>");
5174 /* Eliminate the marker that makes a cast not be an lvalue. */
5175 if (local.val != NULL_TREE)
5176 STRIP_NOPS (local.val);
5178 /* Output the current element, using the appropriate helper ... */
5180 /* For an array slice not part of an outer bitfield. */
5181 if (!outer
5182 && local.index != NULL_TREE
5183 && TREE_CODE (local.index) == RANGE_EXPR)
5184 output_constructor_array_range (&local);
5186 /* For a field that is neither a true bitfield nor part of an outer one,
5187 known to be at least byte aligned and multiple-of-bytes long. */
5188 else if (!outer
5189 && (local.field == NULL_TREE
5190 || !CONSTRUCTOR_BITFIELD_P (local.field)))
5191 output_constructor_regular_field (&local);
5193 /* For a true bitfield or part of an outer one. Only INTEGER_CSTs are
5194 supported for scalar fields, so we may need to convert first. */
5195 else
5197 if (TREE_CODE (local.val) == REAL_CST)
5198 local.val
5199 = fold_unary (VIEW_CONVERT_EXPR,
5200 build_nonstandard_integer_type
5201 (TYPE_PRECISION (TREE_TYPE (local.val)), 0),
5202 local.val);
5203 output_constructor_bitfield (&local, outer ? outer->bit_offset : 0);
5207 /* If we are not at toplevel, save the pending data for our caller.
5208 Otherwise output the pending data and padding zeros as needed. */
5209 if (outer)
5210 outer->byte = local.byte;
5211 else
5213 if (local.byte_buffer_in_use)
5215 assemble_integer (GEN_INT (local.byte), 1, BITS_PER_UNIT, 1);
5216 local.total_bytes++;
5219 if ((unsigned HOST_WIDE_INT)local.total_bytes < local.size)
5221 assemble_zeros (local.size - local.total_bytes);
5222 local.total_bytes = local.size;
5226 return local.total_bytes;
5229 /* Mark DECL as weak. */
5231 static void
5232 mark_weak (tree decl)
5234 if (DECL_WEAK (decl))
5235 return;
5237 struct symtab_node *n = symtab_node::get (decl);
5238 if (n && n->refuse_visibility_changes)
5239 error ("%+D declared weak after being used", decl);
5240 DECL_WEAK (decl) = 1;
5242 if (DECL_RTL_SET_P (decl)
5243 && MEM_P (DECL_RTL (decl))
5244 && XEXP (DECL_RTL (decl), 0)
5245 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == SYMBOL_REF)
5246 SYMBOL_REF_WEAK (XEXP (DECL_RTL (decl), 0)) = 1;
5249 /* Merge weak status between NEWDECL and OLDDECL. */
5251 void
5252 merge_weak (tree newdecl, tree olddecl)
5254 if (DECL_WEAK (newdecl) == DECL_WEAK (olddecl))
5256 if (DECL_WEAK (newdecl) && TARGET_SUPPORTS_WEAK)
5258 tree *pwd;
5259 /* We put the NEWDECL on the weak_decls list at some point
5260 and OLDDECL as well. Keep just OLDDECL on the list. */
5261 for (pwd = &weak_decls; *pwd; pwd = &TREE_CHAIN (*pwd))
5262 if (TREE_VALUE (*pwd) == newdecl)
5264 *pwd = TREE_CHAIN (*pwd);
5265 break;
5268 return;
5271 if (DECL_WEAK (newdecl))
5273 tree wd;
5275 /* NEWDECL is weak, but OLDDECL is not. */
5277 /* If we already output the OLDDECL, we're in trouble; we can't
5278 go back and make it weak. This should never happen in
5279 unit-at-a-time compilation. */
5280 gcc_assert (!TREE_ASM_WRITTEN (olddecl));
5282 /* If we've already generated rtl referencing OLDDECL, we may
5283 have done so in a way that will not function properly with
5284 a weak symbol. Again in unit-at-a-time this should be
5285 impossible. */
5286 gcc_assert (!TREE_USED (olddecl)
5287 || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (olddecl)));
5289 if (TARGET_SUPPORTS_WEAK)
5291 /* We put the NEWDECL on the weak_decls list at some point.
5292 Replace it with the OLDDECL. */
5293 for (wd = weak_decls; wd; wd = TREE_CHAIN (wd))
5294 if (TREE_VALUE (wd) == newdecl)
5296 TREE_VALUE (wd) = olddecl;
5297 break;
5299 /* We may not find the entry on the list. If NEWDECL is a
5300 weak alias, then we will have already called
5301 globalize_decl to remove the entry; in that case, we do
5302 not need to do anything. */
5305 /* Make the OLDDECL weak; it's OLDDECL that we'll be keeping. */
5306 mark_weak (olddecl);
5308 else
5309 /* OLDDECL was weak, but NEWDECL was not explicitly marked as
5310 weak. Just update NEWDECL to indicate that it's weak too. */
5311 mark_weak (newdecl);
5314 /* Declare DECL to be a weak symbol. */
5316 void
5317 declare_weak (tree decl)
5319 gcc_assert (TREE_CODE (decl) != FUNCTION_DECL || !TREE_ASM_WRITTEN (decl));
5320 if (! TREE_PUBLIC (decl))
5321 error ("weak declaration of %q+D must be public", decl);
5322 else if (!TARGET_SUPPORTS_WEAK)
5323 warning (0, "weak declaration of %q+D not supported", decl);
5325 mark_weak (decl);
5326 if (!lookup_attribute ("weak", DECL_ATTRIBUTES (decl)))
5327 DECL_ATTRIBUTES (decl)
5328 = tree_cons (get_identifier ("weak"), NULL, DECL_ATTRIBUTES (decl));
5331 static void
5332 weak_finish_1 (tree decl)
5334 #if defined (ASM_WEAKEN_DECL) || defined (ASM_WEAKEN_LABEL)
5335 const char *const name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5336 #endif
5338 if (! TREE_USED (decl))
5339 return;
5341 #ifdef ASM_WEAKEN_DECL
5342 ASM_WEAKEN_DECL (asm_out_file, decl, name, NULL);
5343 #else
5344 #ifdef ASM_WEAKEN_LABEL
5345 ASM_WEAKEN_LABEL (asm_out_file, name);
5346 #else
5347 #ifdef ASM_OUTPUT_WEAK_ALIAS
5349 static bool warn_once = 0;
5350 if (! warn_once)
5352 warning (0, "only weak aliases are supported in this configuration");
5353 warn_once = 1;
5355 return;
5357 #endif
5358 #endif
5359 #endif
5362 /* Fiven an assembly name, find the decl it is associated with. */
5363 static tree
5364 find_decl (tree target)
5366 symtab_node *node = symtab_node::get_for_asmname (target);
5367 if (node)
5368 return node->decl;
5369 return NULL_TREE;
5372 /* This TREE_LIST contains weakref targets. */
5374 static GTY(()) tree weakref_targets;
5376 /* Emit any pending weak declarations. */
5378 void
5379 weak_finish (void)
5381 tree t;
5383 for (t = weakref_targets; t; t = TREE_CHAIN (t))
5385 tree alias_decl = TREE_PURPOSE (t);
5386 tree target = ultimate_transparent_alias_target (&TREE_VALUE (t));
5388 if (! TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (alias_decl)))
5389 /* Remove alias_decl from the weak list, but leave entries for
5390 the target alone. */
5391 target = NULL_TREE;
5392 #ifndef ASM_OUTPUT_WEAKREF
5393 else if (! TREE_SYMBOL_REFERENCED (target))
5395 /* Use ASM_WEAKEN_LABEL only if ASM_WEAKEN_DECL is not
5396 defined, otherwise we and weak_finish_1 would use
5397 different macros. */
5398 # if defined ASM_WEAKEN_LABEL && ! defined ASM_WEAKEN_DECL
5399 ASM_WEAKEN_LABEL (asm_out_file, IDENTIFIER_POINTER (target));
5400 # else
5401 tree decl = find_decl (target);
5403 if (! decl)
5405 decl = build_decl (DECL_SOURCE_LOCATION (alias_decl),
5406 TREE_CODE (alias_decl), target,
5407 TREE_TYPE (alias_decl));
5409 DECL_EXTERNAL (decl) = 1;
5410 TREE_PUBLIC (decl) = 1;
5411 DECL_ARTIFICIAL (decl) = 1;
5412 TREE_NOTHROW (decl) = TREE_NOTHROW (alias_decl);
5413 TREE_USED (decl) = 1;
5416 weak_finish_1 (decl);
5417 # endif
5419 #endif
5422 tree *p;
5423 tree t2;
5425 /* Remove the alias and the target from the pending weak list
5426 so that we do not emit any .weak directives for the former,
5427 nor multiple .weak directives for the latter. */
5428 for (p = &weak_decls; (t2 = *p) ; )
5430 if (TREE_VALUE (t2) == alias_decl
5431 || target == DECL_ASSEMBLER_NAME (TREE_VALUE (t2)))
5432 *p = TREE_CHAIN (t2);
5433 else
5434 p = &TREE_CHAIN (t2);
5437 /* Remove other weakrefs to the same target, to speed things up. */
5438 for (p = &TREE_CHAIN (t); (t2 = *p) ; )
5440 if (target == ultimate_transparent_alias_target (&TREE_VALUE (t2)))
5441 *p = TREE_CHAIN (t2);
5442 else
5443 p = &TREE_CHAIN (t2);
5448 for (t = weak_decls; t; t = TREE_CHAIN (t))
5450 tree decl = TREE_VALUE (t);
5452 weak_finish_1 (decl);
5456 /* Emit the assembly bits to indicate that DECL is globally visible. */
5458 static void
5459 globalize_decl (tree decl)
5462 #if defined (ASM_WEAKEN_LABEL) || defined (ASM_WEAKEN_DECL)
5463 if (DECL_WEAK (decl))
5465 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
5466 tree *p, t;
5468 #ifdef ASM_WEAKEN_DECL
5469 ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
5470 #else
5471 ASM_WEAKEN_LABEL (asm_out_file, name);
5472 #endif
5474 /* Remove this function from the pending weak list so that
5475 we do not emit multiple .weak directives for it. */
5476 for (p = &weak_decls; (t = *p) ; )
5478 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
5479 *p = TREE_CHAIN (t);
5480 else
5481 p = &TREE_CHAIN (t);
5484 /* Remove weakrefs to the same target from the pending weakref
5485 list, for the same reason. */
5486 for (p = &weakref_targets; (t = *p) ; )
5488 if (DECL_ASSEMBLER_NAME (decl)
5489 == ultimate_transparent_alias_target (&TREE_VALUE (t)))
5490 *p = TREE_CHAIN (t);
5491 else
5492 p = &TREE_CHAIN (t);
5495 return;
5497 #endif
5499 targetm.asm_out.globalize_decl_name (asm_out_file, decl);
5502 vec<alias_pair, va_gc> *alias_pairs;
5504 /* Output the assembler code for a define (equate) using ASM_OUTPUT_DEF
5505 or ASM_OUTPUT_DEF_FROM_DECLS. The function defines the symbol whose
5506 tree node is DECL to have the value of the tree node TARGET. */
5508 void
5509 do_assemble_alias (tree decl, tree target)
5511 /* Emulated TLS had better not get this var. */
5512 gcc_assert (!(!targetm.have_tls
5513 && TREE_CODE (decl) == VAR_DECL
5514 && DECL_THREAD_LOCAL_P (decl)));
5516 if (TREE_ASM_WRITTEN (decl))
5517 return;
5519 /* We must force creation of DECL_RTL for debug info generation, even though
5520 we don't use it here. */
5521 make_decl_rtl (decl);
5523 TREE_ASM_WRITTEN (decl) = 1;
5524 TREE_ASM_WRITTEN (DECL_ASSEMBLER_NAME (decl)) = 1;
5526 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
5528 ultimate_transparent_alias_target (&target);
5530 if (!TREE_SYMBOL_REFERENCED (target))
5531 weakref_targets = tree_cons (decl, target, weakref_targets);
5533 #ifdef ASM_OUTPUT_WEAKREF
5534 ASM_OUTPUT_WEAKREF (asm_out_file, decl,
5535 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
5536 IDENTIFIER_POINTER (target));
5537 #else
5538 if (!TARGET_SUPPORTS_WEAK)
5540 error_at (DECL_SOURCE_LOCATION (decl),
5541 "weakref is not supported in this configuration");
5542 return;
5544 #endif
5545 return;
5548 #ifdef ASM_OUTPUT_DEF
5549 /* Make name accessible from other files, if appropriate. */
5551 if (TREE_PUBLIC (decl))
5553 globalize_decl (decl);
5554 maybe_assemble_visibility (decl);
5556 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
5558 #if defined (ASM_OUTPUT_TYPE_DIRECTIVE)
5559 if (targetm.has_ifunc_p ())
5560 ASM_OUTPUT_TYPE_DIRECTIVE
5561 (asm_out_file, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
5562 IFUNC_ASM_TYPE);
5563 else
5564 #endif
5565 error_at (DECL_SOURCE_LOCATION (decl),
5566 "ifunc is not supported on this target");
5569 # ifdef ASM_OUTPUT_DEF_FROM_DECLS
5570 ASM_OUTPUT_DEF_FROM_DECLS (asm_out_file, decl, target);
5571 # else
5572 ASM_OUTPUT_DEF (asm_out_file,
5573 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
5574 IDENTIFIER_POINTER (target));
5575 # endif
5576 #elif defined (ASM_OUTPUT_WEAK_ALIAS) || defined (ASM_WEAKEN_DECL)
5578 const char *name;
5579 tree *p, t;
5581 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5582 # ifdef ASM_WEAKEN_DECL
5583 ASM_WEAKEN_DECL (asm_out_file, decl, name, IDENTIFIER_POINTER (target));
5584 # else
5585 ASM_OUTPUT_WEAK_ALIAS (asm_out_file, name, IDENTIFIER_POINTER (target));
5586 # endif
5587 /* Remove this function from the pending weak list so that
5588 we do not emit multiple .weak directives for it. */
5589 for (p = &weak_decls; (t = *p) ; )
5590 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
5591 *p = TREE_CHAIN (t);
5592 else
5593 p = &TREE_CHAIN (t);
5595 /* Remove weakrefs to the same target from the pending weakref
5596 list, for the same reason. */
5597 for (p = &weakref_targets; (t = *p) ; )
5599 if (DECL_ASSEMBLER_NAME (decl)
5600 == ultimate_transparent_alias_target (&TREE_VALUE (t)))
5601 *p = TREE_CHAIN (t);
5602 else
5603 p = &TREE_CHAIN (t);
5606 #endif
5609 /* Emit an assembler directive to make the symbol for DECL an alias to
5610 the symbol for TARGET. */
5612 void
5613 assemble_alias (tree decl, tree target)
5615 tree target_decl;
5617 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
5619 tree alias = DECL_ASSEMBLER_NAME (decl);
5621 ultimate_transparent_alias_target (&target);
5623 if (alias == target)
5624 error ("weakref %q+D ultimately targets itself", decl);
5625 if (TREE_PUBLIC (decl))
5626 error ("weakref %q+D must have static linkage", decl);
5628 else
5630 #if !defined (ASM_OUTPUT_DEF)
5631 # if !defined(ASM_OUTPUT_WEAK_ALIAS) && !defined (ASM_WEAKEN_DECL)
5632 error_at (DECL_SOURCE_LOCATION (decl),
5633 "alias definitions not supported in this configuration");
5634 TREE_ASM_WRITTEN (decl) = 1;
5635 return;
5636 # else
5637 if (!DECL_WEAK (decl))
5639 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
5640 error_at (DECL_SOURCE_LOCATION (decl),
5641 "ifunc is not supported in this configuration");
5642 else
5643 error_at (DECL_SOURCE_LOCATION (decl),
5644 "only weak aliases are supported in this configuration");
5645 TREE_ASM_WRITTEN (decl) = 1;
5646 return;
5648 # endif
5649 #endif
5651 TREE_USED (decl) = 1;
5653 /* Allow aliases to aliases. */
5654 if (TREE_CODE (decl) == FUNCTION_DECL)
5655 cgraph_node::get_create (decl)->alias = true;
5656 else
5657 varpool_node::get_create (decl)->alias = true;
5659 /* If the target has already been emitted, we don't have to queue the
5660 alias. This saves a tad of memory. */
5661 if (symtab->global_info_ready)
5662 target_decl = find_decl (target);
5663 else
5664 target_decl= NULL;
5665 if ((target_decl && TREE_ASM_WRITTEN (target_decl))
5666 || symtab->state >= EXPANSION)
5667 do_assemble_alias (decl, target);
5668 else
5670 alias_pair p = {decl, target};
5671 vec_safe_push (alias_pairs, p);
5675 /* Record and output a table of translations from original function
5676 to its transaction aware clone. Note that tm_pure functions are
5677 considered to be their own clone. */
5679 static GTY((if_marked ("tree_map_marked_p"), param_is (struct tree_map)))
5680 htab_t tm_clone_hash;
5682 void
5683 record_tm_clone_pair (tree o, tree n)
5685 struct tree_map **slot, *h;
5687 if (tm_clone_hash == NULL)
5688 tm_clone_hash = htab_create_ggc (32, tree_map_hash, tree_map_eq, 0);
5690 h = ggc_alloc<tree_map> ();
5691 h->hash = htab_hash_pointer (o);
5692 h->base.from = o;
5693 h->to = n;
5695 slot = (struct tree_map **)
5696 htab_find_slot_with_hash (tm_clone_hash, h, h->hash, INSERT);
5697 *slot = h;
5700 tree
5701 get_tm_clone_pair (tree o)
5703 if (tm_clone_hash)
5705 struct tree_map *h, in;
5707 in.base.from = o;
5708 in.hash = htab_hash_pointer (o);
5709 h = (struct tree_map *) htab_find_with_hash (tm_clone_hash,
5710 &in, in.hash);
5711 if (h)
5712 return h->to;
5714 return NULL_TREE;
5717 typedef struct tm_alias_pair
5719 unsigned int uid;
5720 tree from;
5721 tree to;
5722 } tm_alias_pair;
5725 /* Helper function for finish_tm_clone_pairs. Dump a hash table entry
5726 into a VEC in INFO. */
5728 static int
5729 dump_tm_clone_to_vec (void **slot, void *info)
5731 struct tree_map *map = (struct tree_map *) *slot;
5732 vec<tm_alias_pair> *tm_alias_pairs = (vec<tm_alias_pair> *) info;
5733 tm_alias_pair p = {DECL_UID (map->base.from), map->base.from, map->to};
5734 tm_alias_pairs->safe_push (p);
5735 return 1;
5738 /* Dump the actual pairs to the .tm_clone_table section. */
5740 static void
5741 dump_tm_clone_pairs (vec<tm_alias_pair> tm_alias_pairs)
5743 unsigned i;
5744 tm_alias_pair *p;
5745 bool switched = false;
5747 FOR_EACH_VEC_ELT (tm_alias_pairs, i, p)
5749 tree src = p->from;
5750 tree dst = p->to;
5751 struct cgraph_node *src_n = cgraph_node::get (src);
5752 struct cgraph_node *dst_n = cgraph_node::get (dst);
5754 /* The function ipa_tm_create_version() marks the clone as needed if
5755 the original function was needed. But we also mark the clone as
5756 needed if we ever called the clone indirectly through
5757 TM_GETTMCLONE. If neither of these are true, we didn't generate
5758 a clone, and we didn't call it indirectly... no sense keeping it
5759 in the clone table. */
5760 if (!dst_n || !dst_n->definition)
5761 continue;
5763 /* This covers the case where we have optimized the original
5764 function away, and only access the transactional clone. */
5765 if (!src_n || !src_n->definition)
5766 continue;
5768 if (!switched)
5770 switch_to_section (targetm.asm_out.tm_clone_table_section ());
5771 assemble_align (POINTER_SIZE);
5772 switched = true;
5775 assemble_integer (XEXP (DECL_RTL (src), 0),
5776 POINTER_SIZE_UNITS, POINTER_SIZE, 1);
5777 assemble_integer (XEXP (DECL_RTL (dst), 0),
5778 POINTER_SIZE_UNITS, POINTER_SIZE, 1);
5782 /* Provide a default for the tm_clone_table section. */
5784 section *
5785 default_clone_table_section (void)
5787 return get_named_section (NULL, ".tm_clone_table", 3);
5790 /* Helper comparison function for qsorting by the DECL_UID stored in
5791 alias_pair->emitted_diags. */
5793 static int
5794 tm_alias_pair_cmp (const void *x, const void *y)
5796 const tm_alias_pair *p1 = (const tm_alias_pair *) x;
5797 const tm_alias_pair *p2 = (const tm_alias_pair *) y;
5798 if (p1->uid < p2->uid)
5799 return -1;
5800 if (p1->uid > p2->uid)
5801 return 1;
5802 return 0;
5805 void
5806 finish_tm_clone_pairs (void)
5808 vec<tm_alias_pair> tm_alias_pairs = vNULL;
5810 if (tm_clone_hash == NULL)
5811 return;
5813 /* We need a determenistic order for the .tm_clone_table, otherwise
5814 we will get bootstrap comparison failures, so dump the hash table
5815 to a vector, sort it, and dump the vector. */
5817 /* Dump the hashtable to a vector. */
5818 htab_traverse_noresize (tm_clone_hash, dump_tm_clone_to_vec,
5819 (void *) &tm_alias_pairs);
5820 /* Sort it. */
5821 tm_alias_pairs.qsort (tm_alias_pair_cmp);
5823 /* Dump it. */
5824 dump_tm_clone_pairs (tm_alias_pairs);
5826 htab_delete (tm_clone_hash);
5827 tm_clone_hash = NULL;
5828 tm_alias_pairs.release ();
5832 /* Emit an assembler directive to set symbol for DECL visibility to
5833 the visibility type VIS, which must not be VISIBILITY_DEFAULT. */
5835 void
5836 default_assemble_visibility (tree decl ATTRIBUTE_UNUSED,
5837 int vis ATTRIBUTE_UNUSED)
5839 #ifdef HAVE_GAS_HIDDEN
5840 static const char * const visibility_types[] = {
5841 NULL, "protected", "hidden", "internal"
5844 const char *name, *type;
5846 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5847 type = visibility_types[vis];
5849 fprintf (asm_out_file, "\t.%s\t", type);
5850 assemble_name (asm_out_file, name);
5851 fprintf (asm_out_file, "\n");
5852 #else
5853 if (!DECL_ARTIFICIAL (decl))
5854 warning (OPT_Wattributes, "visibility attribute not supported "
5855 "in this configuration; ignored");
5856 #endif
5859 /* A helper function to call assemble_visibility when needed for a decl. */
5862 maybe_assemble_visibility (tree decl)
5864 enum symbol_visibility vis = DECL_VISIBILITY (decl);
5866 if (vis != VISIBILITY_DEFAULT)
5868 targetm.asm_out.assemble_visibility (decl, vis);
5869 return 1;
5871 else
5872 return 0;
5875 /* Returns 1 if the target configuration supports defining public symbols
5876 so that one of them will be chosen at link time instead of generating a
5877 multiply-defined symbol error, whether through the use of weak symbols or
5878 a target-specific mechanism for having duplicates discarded. */
5881 supports_one_only (void)
5883 if (SUPPORTS_ONE_ONLY)
5884 return 1;
5885 return TARGET_SUPPORTS_WEAK;
5888 /* Set up DECL as a public symbol that can be defined in multiple
5889 translation units without generating a linker error. */
5891 void
5892 make_decl_one_only (tree decl, tree comdat_group)
5894 struct symtab_node *symbol;
5895 gcc_assert (TREE_CODE (decl) == VAR_DECL
5896 || TREE_CODE (decl) == FUNCTION_DECL);
5898 TREE_PUBLIC (decl) = 1;
5900 if (TREE_CODE (decl) == VAR_DECL)
5901 symbol = varpool_node::get_create (decl);
5902 else
5903 symbol = cgraph_node::get_create (decl);
5905 if (SUPPORTS_ONE_ONLY)
5907 #ifdef MAKE_DECL_ONE_ONLY
5908 MAKE_DECL_ONE_ONLY (decl);
5909 #endif
5910 symbol->set_comdat_group (comdat_group);
5912 else if (TREE_CODE (decl) == VAR_DECL
5913 && (DECL_INITIAL (decl) == 0
5914 || (!in_lto_p && DECL_INITIAL (decl) == error_mark_node)))
5915 DECL_COMMON (decl) = 1;
5916 else
5918 gcc_assert (TARGET_SUPPORTS_WEAK);
5919 DECL_WEAK (decl) = 1;
5923 void
5924 init_varasm_once (void)
5926 section_htab = htab_create_ggc (31, section_entry_hash,
5927 section_entry_eq, NULL);
5928 object_block_htab = htab_create_ggc (31, object_block_entry_hash,
5929 object_block_entry_eq, NULL);
5930 const_desc_htab = htab_create_ggc (1009, const_desc_hash,
5931 const_desc_eq, NULL);
5933 const_alias_set = new_alias_set ();
5934 shared_constant_pool = create_constant_pool ();
5936 #ifdef TEXT_SECTION_ASM_OP
5937 text_section = get_unnamed_section (SECTION_CODE, output_section_asm_op,
5938 TEXT_SECTION_ASM_OP);
5939 #endif
5941 #ifdef DATA_SECTION_ASM_OP
5942 data_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
5943 DATA_SECTION_ASM_OP);
5944 #endif
5946 #ifdef SDATA_SECTION_ASM_OP
5947 sdata_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
5948 SDATA_SECTION_ASM_OP);
5949 #endif
5951 #ifdef READONLY_DATA_SECTION_ASM_OP
5952 readonly_data_section = get_unnamed_section (0, output_section_asm_op,
5953 READONLY_DATA_SECTION_ASM_OP);
5954 #endif
5956 #ifdef CTORS_SECTION_ASM_OP
5957 ctors_section = get_unnamed_section (0, output_section_asm_op,
5958 CTORS_SECTION_ASM_OP);
5959 #endif
5961 #ifdef DTORS_SECTION_ASM_OP
5962 dtors_section = get_unnamed_section (0, output_section_asm_op,
5963 DTORS_SECTION_ASM_OP);
5964 #endif
5966 #ifdef BSS_SECTION_ASM_OP
5967 bss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
5968 output_section_asm_op,
5969 BSS_SECTION_ASM_OP);
5970 #endif
5972 #ifdef SBSS_SECTION_ASM_OP
5973 sbss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
5974 output_section_asm_op,
5975 SBSS_SECTION_ASM_OP);
5976 #endif
5978 tls_comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
5979 | SECTION_COMMON, emit_tls_common);
5980 lcomm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
5981 | SECTION_COMMON, emit_local);
5982 comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
5983 | SECTION_COMMON, emit_common);
5985 #if defined ASM_OUTPUT_ALIGNED_BSS
5986 bss_noswitch_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS,
5987 emit_bss);
5988 #endif
5990 targetm.asm_out.init_sections ();
5992 if (readonly_data_section == NULL)
5993 readonly_data_section = text_section;
5995 #ifdef ASM_OUTPUT_EXTERNAL
5996 pending_assemble_externals_set = new hash_set<tree>;
5997 #endif
6000 enum tls_model
6001 decl_default_tls_model (const_tree decl)
6003 enum tls_model kind;
6004 bool is_local;
6006 is_local = targetm.binds_local_p (decl);
6007 if (!flag_shlib)
6009 if (is_local)
6010 kind = TLS_MODEL_LOCAL_EXEC;
6011 else
6012 kind = TLS_MODEL_INITIAL_EXEC;
6015 /* Local dynamic is inefficient when we're not combining the
6016 parts of the address. */
6017 else if (optimize && is_local)
6018 kind = TLS_MODEL_LOCAL_DYNAMIC;
6019 else
6020 kind = TLS_MODEL_GLOBAL_DYNAMIC;
6021 if (kind < flag_tls_default)
6022 kind = flag_tls_default;
6024 return kind;
6027 /* Select a set of attributes for section NAME based on the properties
6028 of DECL and whether or not RELOC indicates that DECL's initializer
6029 might contain runtime relocations.
6031 We make the section read-only and executable for a function decl,
6032 read-only for a const data decl, and writable for a non-const data decl. */
6034 unsigned int
6035 default_section_type_flags (tree decl, const char *name, int reloc)
6037 unsigned int flags;
6039 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
6040 flags = SECTION_CODE;
6041 else if (decl)
6043 enum section_category category
6044 = categorize_decl_for_section (decl, reloc);
6045 if (decl_readonly_section_1 (category))
6046 flags = 0;
6047 else if (category == SECCAT_DATA_REL_RO
6048 || category == SECCAT_DATA_REL_RO_LOCAL)
6049 flags = SECTION_WRITE | SECTION_RELRO;
6050 else
6051 flags = SECTION_WRITE;
6053 else
6055 flags = SECTION_WRITE;
6056 if (strcmp (name, ".data.rel.ro") == 0
6057 || strcmp (name, ".data.rel.ro.local") == 0)
6058 flags |= SECTION_RELRO;
6061 if (decl && DECL_P (decl) && DECL_COMDAT_GROUP (decl))
6062 flags |= SECTION_LINKONCE;
6064 if (strcmp (name, ".vtable_map_vars") == 0)
6065 flags |= SECTION_LINKONCE;
6067 if (decl && TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
6068 flags |= SECTION_TLS | SECTION_WRITE;
6070 if (strcmp (name, ".bss") == 0
6071 || strncmp (name, ".bss.", 5) == 0
6072 || strncmp (name, ".gnu.linkonce.b.", 16) == 0
6073 || strcmp (name, ".persistent.bss") == 0
6074 || strcmp (name, ".sbss") == 0
6075 || strncmp (name, ".sbss.", 6) == 0
6076 || strncmp (name, ".gnu.linkonce.sb.", 17) == 0)
6077 flags |= SECTION_BSS;
6079 if (strcmp (name, ".tdata") == 0
6080 || strncmp (name, ".tdata.", 7) == 0
6081 || strncmp (name, ".gnu.linkonce.td.", 17) == 0)
6082 flags |= SECTION_TLS;
6084 if (strcmp (name, ".tbss") == 0
6085 || strncmp (name, ".tbss.", 6) == 0
6086 || strncmp (name, ".gnu.linkonce.tb.", 17) == 0)
6087 flags |= SECTION_TLS | SECTION_BSS;
6089 /* These three sections have special ELF types. They are neither
6090 SHT_PROGBITS nor SHT_NOBITS, so when changing sections we don't
6091 want to print a section type (@progbits or @nobits). If someone
6092 is silly enough to emit code or TLS variables to one of these
6093 sections, then don't handle them specially. */
6094 if (!(flags & (SECTION_CODE | SECTION_BSS | SECTION_TLS))
6095 && (strcmp (name, ".init_array") == 0
6096 || strcmp (name, ".fini_array") == 0
6097 || strcmp (name, ".preinit_array") == 0))
6098 flags |= SECTION_NOTYPE;
6100 return flags;
6103 /* Return true if the target supports some form of global BSS,
6104 either through bss_noswitch_section, or by selecting a BSS
6105 section in TARGET_ASM_SELECT_SECTION. */
6107 bool
6108 have_global_bss_p (void)
6110 return bss_noswitch_section || targetm.have_switchable_bss_sections;
6113 /* Output assembly to switch to section NAME with attribute FLAGS.
6114 Four variants for common object file formats. */
6116 void
6117 default_no_named_section (const char *name ATTRIBUTE_UNUSED,
6118 unsigned int flags ATTRIBUTE_UNUSED,
6119 tree decl ATTRIBUTE_UNUSED)
6121 /* Some object formats don't support named sections at all. The
6122 front-end should already have flagged this as an error. */
6123 gcc_unreachable ();
6126 #ifndef TLS_SECTION_ASM_FLAG
6127 #define TLS_SECTION_ASM_FLAG 'T'
6128 #endif
6130 void
6131 default_elf_asm_named_section (const char *name, unsigned int flags,
6132 tree decl ATTRIBUTE_UNUSED)
6134 char flagchars[10], *f = flagchars;
6136 /* If we have already declared this section, we can use an
6137 abbreviated form to switch back to it -- unless this section is
6138 part of a COMDAT groups, in which case GAS requires the full
6139 declaration every time. */
6140 if (!(HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6141 && (flags & SECTION_DECLARED))
6143 fprintf (asm_out_file, "\t.section\t%s\n", name);
6144 return;
6147 if (!(flags & SECTION_DEBUG))
6148 *f++ = 'a';
6149 if (flags & SECTION_EXCLUDE)
6150 *f++ = 'e';
6151 if (flags & SECTION_WRITE)
6152 *f++ = 'w';
6153 if (flags & SECTION_CODE)
6154 *f++ = 'x';
6155 if (flags & SECTION_SMALL)
6156 *f++ = 's';
6157 if (flags & SECTION_MERGE)
6158 *f++ = 'M';
6159 if (flags & SECTION_STRINGS)
6160 *f++ = 'S';
6161 if (flags & SECTION_TLS)
6162 *f++ = TLS_SECTION_ASM_FLAG;
6163 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6164 *f++ = 'G';
6165 *f = '\0';
6167 fprintf (asm_out_file, "\t.section\t%s,\"%s\"", name, flagchars);
6169 if (!(flags & SECTION_NOTYPE))
6171 const char *type;
6172 const char *format;
6174 if (flags & SECTION_BSS)
6175 type = "nobits";
6176 else
6177 type = "progbits";
6179 format = ",@%s";
6180 /* On platforms that use "@" as the assembly comment character,
6181 use "%" instead. */
6182 if (strcmp (ASM_COMMENT_START, "@") == 0)
6183 format = ",%%%s";
6184 fprintf (asm_out_file, format, type);
6186 if (flags & SECTION_ENTSIZE)
6187 fprintf (asm_out_file, ",%d", flags & SECTION_ENTSIZE);
6188 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6190 if (TREE_CODE (decl) == IDENTIFIER_NODE)
6191 fprintf (asm_out_file, ",%s,comdat", IDENTIFIER_POINTER (decl));
6192 else
6193 fprintf (asm_out_file, ",%s,comdat",
6194 IDENTIFIER_POINTER (DECL_COMDAT_GROUP (decl)));
6198 putc ('\n', asm_out_file);
6201 void
6202 default_coff_asm_named_section (const char *name, unsigned int flags,
6203 tree decl ATTRIBUTE_UNUSED)
6205 char flagchars[8], *f = flagchars;
6207 if (flags & SECTION_WRITE)
6208 *f++ = 'w';
6209 if (flags & SECTION_CODE)
6210 *f++ = 'x';
6211 *f = '\0';
6213 fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
6216 void
6217 default_pe_asm_named_section (const char *name, unsigned int flags,
6218 tree decl)
6220 default_coff_asm_named_section (name, flags, decl);
6222 if (flags & SECTION_LINKONCE)
6224 /* Functions may have been compiled at various levels of
6225 optimization so we can't use `same_size' here.
6226 Instead, have the linker pick one. */
6227 fprintf (asm_out_file, "\t.linkonce %s\n",
6228 (flags & SECTION_CODE ? "discard" : "same_size"));
6232 /* The lame default section selector. */
6234 section *
6235 default_select_section (tree decl, int reloc,
6236 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
6238 if (DECL_P (decl))
6240 if (decl_readonly_section (decl, reloc))
6241 return readonly_data_section;
6243 else if (TREE_CODE (decl) == CONSTRUCTOR)
6245 if (! ((flag_pic && reloc)
6246 || !TREE_READONLY (decl)
6247 || TREE_SIDE_EFFECTS (decl)
6248 || !TREE_CONSTANT (decl)))
6249 return readonly_data_section;
6251 else if (TREE_CODE (decl) == STRING_CST)
6252 return readonly_data_section;
6253 else if (! (flag_pic && reloc))
6254 return readonly_data_section;
6256 return data_section;
6259 enum section_category
6260 categorize_decl_for_section (const_tree decl, int reloc)
6262 enum section_category ret;
6264 if (TREE_CODE (decl) == FUNCTION_DECL)
6265 return SECCAT_TEXT;
6266 else if (TREE_CODE (decl) == STRING_CST)
6268 if ((flag_sanitize & SANITIZE_ADDRESS)
6269 && asan_protect_global (CONST_CAST_TREE (decl)))
6270 /* or !flag_merge_constants */
6271 return SECCAT_RODATA;
6272 else
6273 return SECCAT_RODATA_MERGE_STR;
6275 else if (TREE_CODE (decl) == VAR_DECL)
6277 if (bss_initializer_p (decl))
6278 ret = SECCAT_BSS;
6279 else if (! TREE_READONLY (decl)
6280 || TREE_SIDE_EFFECTS (decl)
6281 || ! TREE_CONSTANT (DECL_INITIAL (decl)))
6283 /* Here the reloc_rw_mask is not testing whether the section should
6284 be read-only or not, but whether the dynamic link will have to
6285 do something. If so, we wish to segregate the data in order to
6286 minimize cache misses inside the dynamic linker. */
6287 if (reloc & targetm.asm_out.reloc_rw_mask ())
6288 ret = reloc == 1 ? SECCAT_DATA_REL_LOCAL : SECCAT_DATA_REL;
6289 else
6290 ret = SECCAT_DATA;
6292 else if (reloc & targetm.asm_out.reloc_rw_mask ())
6293 ret = reloc == 1 ? SECCAT_DATA_REL_RO_LOCAL : SECCAT_DATA_REL_RO;
6294 else if (reloc || flag_merge_constants < 2
6295 || ((flag_sanitize & SANITIZE_ADDRESS)
6296 && asan_protect_global (CONST_CAST_TREE (decl))))
6297 /* C and C++ don't allow different variables to share the same
6298 location. -fmerge-all-constants allows even that (at the
6299 expense of not conforming). */
6300 ret = SECCAT_RODATA;
6301 else if (TREE_CODE (DECL_INITIAL (decl)) == STRING_CST)
6302 ret = SECCAT_RODATA_MERGE_STR_INIT;
6303 else
6304 ret = SECCAT_RODATA_MERGE_CONST;
6306 else if (TREE_CODE (decl) == CONSTRUCTOR)
6308 if ((reloc & targetm.asm_out.reloc_rw_mask ())
6309 || TREE_SIDE_EFFECTS (decl)
6310 || ! TREE_CONSTANT (decl))
6311 ret = SECCAT_DATA;
6312 else
6313 ret = SECCAT_RODATA;
6315 else
6316 ret = SECCAT_RODATA;
6318 /* There are no read-only thread-local sections. */
6319 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
6321 /* Note that this would be *just* SECCAT_BSS, except that there's
6322 no concept of a read-only thread-local-data section. */
6323 if (ret == SECCAT_BSS
6324 || (flag_zero_initialized_in_bss
6325 && initializer_zerop (DECL_INITIAL (decl))))
6326 ret = SECCAT_TBSS;
6327 else
6328 ret = SECCAT_TDATA;
6331 /* If the target uses small data sections, select it. */
6332 else if (targetm.in_small_data_p (decl))
6334 if (ret == SECCAT_BSS)
6335 ret = SECCAT_SBSS;
6336 else if (targetm.have_srodata_section && ret == SECCAT_RODATA)
6337 ret = SECCAT_SRODATA;
6338 else
6339 ret = SECCAT_SDATA;
6342 return ret;
6345 static bool
6346 decl_readonly_section_1 (enum section_category category)
6348 switch (category)
6350 case SECCAT_RODATA:
6351 case SECCAT_RODATA_MERGE_STR:
6352 case SECCAT_RODATA_MERGE_STR_INIT:
6353 case SECCAT_RODATA_MERGE_CONST:
6354 case SECCAT_SRODATA:
6355 return true;
6356 default:
6357 return false;
6361 bool
6362 decl_readonly_section (const_tree decl, int reloc)
6364 return decl_readonly_section_1 (categorize_decl_for_section (decl, reloc));
6367 /* Select a section based on the above categorization. */
6369 section *
6370 default_elf_select_section (tree decl, int reloc,
6371 unsigned HOST_WIDE_INT align)
6373 const char *sname;
6374 switch (categorize_decl_for_section (decl, reloc))
6376 case SECCAT_TEXT:
6377 /* We're not supposed to be called on FUNCTION_DECLs. */
6378 gcc_unreachable ();
6379 case SECCAT_RODATA:
6380 return readonly_data_section;
6381 case SECCAT_RODATA_MERGE_STR:
6382 return mergeable_string_section (decl, align, 0);
6383 case SECCAT_RODATA_MERGE_STR_INIT:
6384 return mergeable_string_section (DECL_INITIAL (decl), align, 0);
6385 case SECCAT_RODATA_MERGE_CONST:
6386 return mergeable_constant_section (DECL_MODE (decl), align, 0);
6387 case SECCAT_SRODATA:
6388 sname = ".sdata2";
6389 break;
6390 case SECCAT_DATA:
6391 return data_section;
6392 case SECCAT_DATA_REL:
6393 sname = ".data.rel";
6394 break;
6395 case SECCAT_DATA_REL_LOCAL:
6396 sname = ".data.rel.local";
6397 break;
6398 case SECCAT_DATA_REL_RO:
6399 sname = ".data.rel.ro";
6400 break;
6401 case SECCAT_DATA_REL_RO_LOCAL:
6402 sname = ".data.rel.ro.local";
6403 break;
6404 case SECCAT_SDATA:
6405 sname = ".sdata";
6406 break;
6407 case SECCAT_TDATA:
6408 sname = ".tdata";
6409 break;
6410 case SECCAT_BSS:
6411 if (bss_section)
6412 return bss_section;
6413 sname = ".bss";
6414 break;
6415 case SECCAT_SBSS:
6416 sname = ".sbss";
6417 break;
6418 case SECCAT_TBSS:
6419 sname = ".tbss";
6420 break;
6421 default:
6422 gcc_unreachable ();
6425 return get_named_section (decl, sname, reloc);
6428 /* Construct a unique section name based on the decl name and the
6429 categorization performed above. */
6431 void
6432 default_unique_section (tree decl, int reloc)
6434 /* We only need to use .gnu.linkonce if we don't have COMDAT groups. */
6435 bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
6436 const char *prefix, *name, *linkonce;
6437 char *string;
6439 switch (categorize_decl_for_section (decl, reloc))
6441 case SECCAT_TEXT:
6442 prefix = one_only ? ".t" : ".text";
6443 break;
6444 case SECCAT_RODATA:
6445 case SECCAT_RODATA_MERGE_STR:
6446 case SECCAT_RODATA_MERGE_STR_INIT:
6447 case SECCAT_RODATA_MERGE_CONST:
6448 prefix = one_only ? ".r" : ".rodata";
6449 break;
6450 case SECCAT_SRODATA:
6451 prefix = one_only ? ".s2" : ".sdata2";
6452 break;
6453 case SECCAT_DATA:
6454 prefix = one_only ? ".d" : ".data";
6455 break;
6456 case SECCAT_DATA_REL:
6457 prefix = one_only ? ".d.rel" : ".data.rel";
6458 break;
6459 case SECCAT_DATA_REL_LOCAL:
6460 prefix = one_only ? ".d.rel.local" : ".data.rel.local";
6461 break;
6462 case SECCAT_DATA_REL_RO:
6463 prefix = one_only ? ".d.rel.ro" : ".data.rel.ro";
6464 break;
6465 case SECCAT_DATA_REL_RO_LOCAL:
6466 prefix = one_only ? ".d.rel.ro.local" : ".data.rel.ro.local";
6467 break;
6468 case SECCAT_SDATA:
6469 prefix = one_only ? ".s" : ".sdata";
6470 break;
6471 case SECCAT_BSS:
6472 prefix = one_only ? ".b" : ".bss";
6473 break;
6474 case SECCAT_SBSS:
6475 prefix = one_only ? ".sb" : ".sbss";
6476 break;
6477 case SECCAT_TDATA:
6478 prefix = one_only ? ".td" : ".tdata";
6479 break;
6480 case SECCAT_TBSS:
6481 prefix = one_only ? ".tb" : ".tbss";
6482 break;
6483 default:
6484 gcc_unreachable ();
6487 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
6488 name = targetm.strip_name_encoding (name);
6490 /* If we're using one_only, then there needs to be a .gnu.linkonce
6491 prefix to the section name. */
6492 linkonce = one_only ? ".gnu.linkonce" : "";
6494 string = ACONCAT ((linkonce, prefix, ".", name, NULL));
6496 set_decl_section_name (decl, string);
6499 /* Subroutine of compute_reloc_for_rtx for leaf rtxes. */
6501 static int
6502 compute_reloc_for_rtx_1 (const_rtx x)
6504 switch (GET_CODE (x))
6506 case SYMBOL_REF:
6507 return SYMBOL_REF_LOCAL_P (x) ? 1 : 2;
6508 case LABEL_REF:
6509 return 1;
6510 default:
6511 return 0;
6515 /* Like compute_reloc_for_constant, except for an RTX. The return value
6516 is a mask for which bit 1 indicates a global relocation, and bit 0
6517 indicates a local relocation. */
6519 static int
6520 compute_reloc_for_rtx (const_rtx x)
6522 switch (GET_CODE (x))
6524 case SYMBOL_REF:
6525 case LABEL_REF:
6526 return compute_reloc_for_rtx_1 (x);
6528 case CONST:
6530 int reloc = 0;
6531 subrtx_iterator::array_type array;
6532 FOR_EACH_SUBRTX (iter, array, x, ALL)
6533 reloc |= compute_reloc_for_rtx_1 (*iter);
6534 return reloc;
6537 default:
6538 return 0;
6542 section *
6543 default_select_rtx_section (enum machine_mode mode ATTRIBUTE_UNUSED,
6544 rtx x,
6545 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
6547 if (compute_reloc_for_rtx (x) & targetm.asm_out.reloc_rw_mask ())
6548 return data_section;
6549 else
6550 return readonly_data_section;
6553 section *
6554 default_elf_select_rtx_section (enum machine_mode mode, rtx x,
6555 unsigned HOST_WIDE_INT align)
6557 int reloc = compute_reloc_for_rtx (x);
6559 /* ??? Handle small data here somehow. */
6561 if (reloc & targetm.asm_out.reloc_rw_mask ())
6563 if (reloc == 1)
6564 return get_named_section (NULL, ".data.rel.ro.local", 1);
6565 else
6566 return get_named_section (NULL, ".data.rel.ro", 3);
6569 return mergeable_constant_section (mode, align, 0);
6572 /* Set the generally applicable flags on the SYMBOL_REF for EXP. */
6574 void
6575 default_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
6577 rtx symbol;
6578 int flags;
6580 /* Careful not to prod global register variables. */
6581 if (!MEM_P (rtl))
6582 return;
6583 symbol = XEXP (rtl, 0);
6584 if (GET_CODE (symbol) != SYMBOL_REF)
6585 return;
6587 flags = SYMBOL_REF_FLAGS (symbol) & SYMBOL_FLAG_HAS_BLOCK_INFO;
6588 if (TREE_CODE (decl) == FUNCTION_DECL)
6589 flags |= SYMBOL_FLAG_FUNCTION;
6590 if (targetm.binds_local_p (decl))
6591 flags |= SYMBOL_FLAG_LOCAL;
6592 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
6593 flags |= DECL_TLS_MODEL (decl) << SYMBOL_FLAG_TLS_SHIFT;
6594 else if (targetm.in_small_data_p (decl))
6595 flags |= SYMBOL_FLAG_SMALL;
6596 /* ??? Why is DECL_EXTERNAL ever set for non-PUBLIC names? Without
6597 being PUBLIC, the thing *must* be defined in this translation unit.
6598 Prevent this buglet from being propagated into rtl code as well. */
6599 if (DECL_P (decl) && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl))
6600 flags |= SYMBOL_FLAG_EXTERNAL;
6602 SYMBOL_REF_FLAGS (symbol) = flags;
6605 /* By default, we do nothing for encode_section_info, so we need not
6606 do anything but discard the '*' marker. */
6608 const char *
6609 default_strip_name_encoding (const char *str)
6611 return str + (*str == '*');
6614 #ifdef ASM_OUTPUT_DEF
6615 /* The default implementation of TARGET_ASM_OUTPUT_ANCHOR. Define the
6616 anchor relative to ".", the current section position. */
6618 void
6619 default_asm_output_anchor (rtx symbol)
6621 char buffer[100];
6623 sprintf (buffer, "*. + " HOST_WIDE_INT_PRINT_DEC,
6624 SYMBOL_REF_BLOCK_OFFSET (symbol));
6625 ASM_OUTPUT_DEF (asm_out_file, XSTR (symbol, 0), buffer);
6627 #endif
6629 /* The default implementation of TARGET_USE_ANCHORS_FOR_SYMBOL_P. */
6631 bool
6632 default_use_anchors_for_symbol_p (const_rtx symbol)
6634 section *sect;
6635 tree decl;
6637 /* Don't use anchors for mergeable sections. The linker might move
6638 the objects around. */
6639 sect = SYMBOL_REF_BLOCK (symbol)->sect;
6640 if (sect->common.flags & SECTION_MERGE)
6641 return false;
6643 /* Don't use anchors for small data sections. The small data register
6644 acts as an anchor for such sections. */
6645 if (sect->common.flags & SECTION_SMALL)
6646 return false;
6648 decl = SYMBOL_REF_DECL (symbol);
6649 if (decl && DECL_P (decl))
6651 /* Don't use section anchors for decls that might be defined or
6652 usurped by other modules. */
6653 if (TREE_PUBLIC (decl) && !decl_binds_to_current_def_p (decl))
6654 return false;
6656 /* Don't use section anchors for decls that will be placed in a
6657 small data section. */
6658 /* ??? Ideally, this check would be redundant with the SECTION_SMALL
6659 one above. The problem is that we only use SECTION_SMALL for
6660 sections that should be marked as small in the section directive. */
6661 if (targetm.in_small_data_p (decl))
6662 return false;
6664 return true;
6667 /* Return true when RESOLUTION indicate that symbol will be bound to the
6668 definition provided by current .o file. */
6670 static bool
6671 resolution_to_local_definition_p (enum ld_plugin_symbol_resolution resolution)
6673 return (resolution == LDPR_PREVAILING_DEF
6674 || resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
6675 || resolution == LDPR_PREVAILING_DEF_IRONLY);
6678 /* Return true when RESOLUTION indicate that symbol will be bound locally
6679 within current executable or DSO. */
6681 static bool
6682 resolution_local_p (enum ld_plugin_symbol_resolution resolution)
6684 return (resolution == LDPR_PREVAILING_DEF
6685 || resolution == LDPR_PREVAILING_DEF_IRONLY
6686 || resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
6687 || resolution == LDPR_PREEMPTED_REG
6688 || resolution == LDPR_PREEMPTED_IR
6689 || resolution == LDPR_RESOLVED_IR
6690 || resolution == LDPR_RESOLVED_EXEC);
6693 /* Assume ELF-ish defaults, since that's pretty much the most liberal
6694 wrt cross-module name binding. */
6696 bool
6697 default_binds_local_p (const_tree exp)
6699 return default_binds_local_p_1 (exp, flag_shlib);
6702 bool
6703 default_binds_local_p_1 (const_tree exp, int shlib)
6705 bool local_p;
6706 bool resolved_locally = false;
6707 bool resolved_to_local_def = false;
6709 /* With resolution file in hands, take look into resolutions.
6710 We can't just return true for resolved_locally symbols,
6711 because dynamic linking might overwrite symbols
6712 in shared libraries. */
6713 if (TREE_CODE (exp) == VAR_DECL && TREE_PUBLIC (exp)
6714 && (TREE_STATIC (exp) || DECL_EXTERNAL (exp)))
6716 varpool_node *vnode = varpool_node::get (exp);
6717 if (vnode && (resolution_local_p (vnode->resolution) || vnode->in_other_partition))
6718 resolved_locally = true;
6719 if (vnode
6720 && resolution_to_local_definition_p (vnode->resolution))
6721 resolved_to_local_def = true;
6723 else if (TREE_CODE (exp) == FUNCTION_DECL && TREE_PUBLIC (exp))
6725 struct cgraph_node *node = cgraph_node::get (exp);
6726 if (node
6727 && (resolution_local_p (node->resolution) || node->in_other_partition))
6728 resolved_locally = true;
6729 if (node
6730 && resolution_to_local_definition_p (node->resolution))
6731 resolved_to_local_def = true;
6734 /* A non-decl is an entry in the constant pool. */
6735 if (!DECL_P (exp))
6736 local_p = true;
6737 /* Weakrefs may not bind locally, even though the weakref itself is always
6738 static and therefore local. Similarly, the resolver for ifunc functions
6739 might resolve to a non-local function.
6740 FIXME: We can resolve the weakref case more curefuly by looking at the
6741 weakref alias. */
6742 else if (lookup_attribute ("weakref", DECL_ATTRIBUTES (exp))
6743 || (TREE_CODE (exp) == FUNCTION_DECL
6744 && lookup_attribute ("ifunc", DECL_ATTRIBUTES (exp))))
6745 local_p = false;
6746 /* Static variables are always local. */
6747 else if (! TREE_PUBLIC (exp))
6748 local_p = true;
6749 /* A variable is local if the user has said explicitly that it will
6750 be. */
6751 else if ((DECL_VISIBILITY_SPECIFIED (exp)
6752 || resolved_to_local_def)
6753 && DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
6754 local_p = true;
6755 /* Variables defined outside this object might not be local. */
6756 else if (DECL_EXTERNAL (exp) && !resolved_locally)
6757 local_p = false;
6758 /* If defined in this object and visibility is not default, must be
6759 local. */
6760 else if (DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
6761 local_p = true;
6762 /* Default visibility weak data can be overridden by a strong symbol
6763 in another module and so are not local. */
6764 else if (DECL_WEAK (exp)
6765 && !resolved_locally)
6766 local_p = false;
6767 /* If PIC, then assume that any global name can be overridden by
6768 symbols resolved from other modules. */
6769 else if (shlib)
6770 local_p = false;
6771 /* Uninitialized COMMON variable may be unified with symbols
6772 resolved from other modules. */
6773 else if (DECL_COMMON (exp)
6774 && !resolved_locally
6775 && (DECL_INITIAL (exp) == NULL
6776 || (!in_lto_p && DECL_INITIAL (exp) == error_mark_node)))
6777 local_p = false;
6778 /* Otherwise we're left with initialized (or non-common) global data
6779 which is of necessity defined locally. */
6780 else
6781 local_p = true;
6783 return local_p;
6786 /* Return true when references to DECL must bind to current definition in
6787 final executable.
6789 The condition is usually equivalent to whether the function binds to the
6790 current module (shared library or executable), that is to binds_local_p.
6791 We use this fact to avoid need for another target hook and implement
6792 the logic using binds_local_p and just special cases where
6793 decl_binds_to_current_def_p is stronger than binds_local_p. In particular
6794 the weak definitions (that can be overwritten at linktime by other
6795 definition from different object file) and when resolution info is available
6796 we simply use the knowledge passed to us by linker plugin. */
6797 bool
6798 decl_binds_to_current_def_p (const_tree decl)
6800 gcc_assert (DECL_P (decl));
6801 if (!targetm.binds_local_p (decl))
6802 return false;
6803 if (!TREE_PUBLIC (decl))
6804 return true;
6805 /* When resolution is available, just use it. */
6806 if (TREE_CODE (decl) == VAR_DECL
6807 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
6809 varpool_node *vnode = varpool_node::get (decl);
6810 if (vnode
6811 && vnode->resolution != LDPR_UNKNOWN)
6812 return resolution_to_local_definition_p (vnode->resolution);
6814 else if (TREE_CODE (decl) == FUNCTION_DECL)
6816 struct cgraph_node *node = cgraph_node::get (decl);
6817 if (node
6818 && node->resolution != LDPR_UNKNOWN)
6819 return resolution_to_local_definition_p (node->resolution);
6821 /* Otherwise we have to assume the worst for DECL_WEAK (hidden weaks
6822 binds locally but still can be overwritten), DECL_COMMON (can be merged
6823 with a non-common definition somewhere in the same module) or
6824 DECL_EXTERNAL.
6825 This rely on fact that binds_local_p behave as decl_replaceable_p
6826 for all other declaration types. */
6827 if (DECL_WEAK (decl))
6828 return false;
6829 if (DECL_COMMON (decl)
6830 && (DECL_INITIAL (decl) == NULL
6831 || (!in_lto_p && DECL_INITIAL (decl) == error_mark_node)))
6832 return false;
6833 if (DECL_EXTERNAL (decl))
6834 return false;
6835 return true;
6838 /* A replaceable function or variable is one which may be replaced
6839 at link-time with an entirely different definition, provided that the
6840 replacement has the same type. For example, functions declared
6841 with __attribute__((weak)) on most systems are replaceable.
6843 COMDAT functions are not replaceable, since all definitions of the
6844 function must be equivalent. It is important that COMDAT functions
6845 not be treated as replaceable so that use of C++ template
6846 instantiations is not penalized. */
6848 bool
6849 decl_replaceable_p (tree decl)
6851 gcc_assert (DECL_P (decl));
6852 if (!TREE_PUBLIC (decl) || DECL_COMDAT (decl))
6853 return false;
6854 if (!flag_semantic_interposition
6855 && !DECL_WEAK (decl))
6856 return false;
6857 return !decl_binds_to_current_def_p (decl);
6860 /* Default function to output code that will globalize a label. A
6861 target must define GLOBAL_ASM_OP or provide its own function to
6862 globalize a label. */
6863 #ifdef GLOBAL_ASM_OP
6864 void
6865 default_globalize_label (FILE * stream, const char *name)
6867 fputs (GLOBAL_ASM_OP, stream);
6868 assemble_name (stream, name);
6869 putc ('\n', stream);
6871 #endif /* GLOBAL_ASM_OP */
6873 /* Default function to output code that will globalize a declaration. */
6874 void
6875 default_globalize_decl_name (FILE * stream, tree decl)
6877 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
6878 targetm.asm_out.globalize_label (stream, name);
6881 /* Default function to output a label for unwind information. The
6882 default is to do nothing. A target that needs nonlocal labels for
6883 unwind information must provide its own function to do this. */
6884 void
6885 default_emit_unwind_label (FILE * stream ATTRIBUTE_UNUSED,
6886 tree decl ATTRIBUTE_UNUSED,
6887 int for_eh ATTRIBUTE_UNUSED,
6888 int empty ATTRIBUTE_UNUSED)
6892 /* Default function to output a label to divide up the exception table.
6893 The default is to do nothing. A target that needs/wants to divide
6894 up the table must provide it's own function to do this. */
6895 void
6896 default_emit_except_table_label (FILE * stream ATTRIBUTE_UNUSED)
6900 /* This is how to output an internal numbered label where PREFIX is
6901 the class of label and LABELNO is the number within the class. */
6903 void
6904 default_generate_internal_label (char *buf, const char *prefix,
6905 unsigned long labelno)
6907 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
6910 /* This is how to output an internal numbered label where PREFIX is
6911 the class of label and LABELNO is the number within the class. */
6913 void
6914 default_internal_label (FILE *stream, const char *prefix,
6915 unsigned long labelno)
6917 char *const buf = (char *) alloca (40 + strlen (prefix));
6918 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
6919 ASM_OUTPUT_INTERNAL_LABEL (stream, buf);
6923 /* The default implementation of ASM_DECLARE_CONSTANT_NAME. */
6925 void
6926 default_asm_declare_constant_name (FILE *file, const char *name,
6927 const_tree exp ATTRIBUTE_UNUSED,
6928 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
6930 assemble_label (file, name);
6933 /* This is the default behavior at the beginning of a file. It's
6934 controlled by two other target-hook toggles. */
6935 void
6936 default_file_start (void)
6938 if (targetm.asm_file_start_app_off
6939 && !(flag_verbose_asm || flag_debug_asm || flag_dump_rtl_in_asm))
6940 fputs (ASM_APP_OFF, asm_out_file);
6942 if (targetm.asm_file_start_file_directive)
6943 output_file_directive (asm_out_file, main_input_filename);
6946 /* This is a generic routine suitable for use as TARGET_ASM_FILE_END
6947 which emits a special section directive used to indicate whether or
6948 not this object file needs an executable stack. This is primarily
6949 a GNU extension to ELF but could be used on other targets. */
6951 int trampolines_created;
6953 void
6954 file_end_indicate_exec_stack (void)
6956 unsigned int flags = SECTION_DEBUG;
6957 if (trampolines_created)
6958 flags |= SECTION_CODE;
6960 switch_to_section (get_section (".note.GNU-stack", flags, NULL));
6963 /* Emit a special section directive to indicate that this object file
6964 was compiled with -fsplit-stack. This is used to let the linker
6965 detect calls between split-stack code and non-split-stack code, so
6966 that it can modify the split-stack code to allocate a sufficiently
6967 large stack. We emit another special section if there are any
6968 functions in this file which have the no_split_stack attribute, to
6969 prevent the linker from warning about being unable to convert the
6970 functions if they call non-split-stack code. */
6972 void
6973 file_end_indicate_split_stack (void)
6975 if (flag_split_stack)
6977 switch_to_section (get_section (".note.GNU-split-stack", SECTION_DEBUG,
6978 NULL));
6979 if (saw_no_split_stack)
6980 switch_to_section (get_section (".note.GNU-no-split-stack",
6981 SECTION_DEBUG, NULL));
6985 /* Output DIRECTIVE (a C string) followed by a newline. This is used as
6986 a get_unnamed_section callback. */
6988 void
6989 output_section_asm_op (const void *directive)
6991 fprintf (asm_out_file, "%s\n", (const char *) directive);
6994 /* Emit assembly code to switch to section NEW_SECTION. Do nothing if
6995 the current section is NEW_SECTION. */
6997 void
6998 switch_to_section (section *new_section)
7000 if (in_section == new_section)
7001 return;
7003 if (new_section->common.flags & SECTION_FORGET)
7004 in_section = NULL;
7005 else
7006 in_section = new_section;
7008 switch (SECTION_STYLE (new_section))
7010 case SECTION_NAMED:
7011 targetm.asm_out.named_section (new_section->named.name,
7012 new_section->named.common.flags,
7013 new_section->named.decl);
7014 break;
7016 case SECTION_UNNAMED:
7017 new_section->unnamed.callback (new_section->unnamed.data);
7018 break;
7020 case SECTION_NOSWITCH:
7021 gcc_unreachable ();
7022 break;
7025 new_section->common.flags |= SECTION_DECLARED;
7028 /* If block symbol SYMBOL has not yet been assigned an offset, place
7029 it at the end of its block. */
7031 void
7032 place_block_symbol (rtx symbol)
7034 unsigned HOST_WIDE_INT size, mask, offset;
7035 struct constant_descriptor_rtx *desc;
7036 unsigned int alignment;
7037 struct object_block *block;
7038 tree decl;
7040 gcc_assert (SYMBOL_REF_BLOCK (symbol));
7041 if (SYMBOL_REF_BLOCK_OFFSET (symbol) >= 0)
7042 return;
7044 /* Work out the symbol's size and alignment. */
7045 if (CONSTANT_POOL_ADDRESS_P (symbol))
7047 desc = SYMBOL_REF_CONSTANT (symbol);
7048 alignment = desc->align;
7049 size = GET_MODE_SIZE (desc->mode);
7051 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
7053 decl = SYMBOL_REF_DECL (symbol);
7054 alignment = DECL_ALIGN (decl);
7055 size = get_constant_size (DECL_INITIAL (decl));
7056 if ((flag_sanitize & SANITIZE_ADDRESS)
7057 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
7058 && asan_protect_global (DECL_INITIAL (decl)))
7059 size += asan_red_zone_size (size);
7061 else
7063 struct symtab_node *snode;
7064 decl = SYMBOL_REF_DECL (symbol);
7066 snode = symtab_node::get (decl);
7067 if (snode->alias)
7069 rtx target = DECL_RTL (snode->ultimate_alias_target ()->decl);
7071 place_block_symbol (target);
7072 SYMBOL_REF_BLOCK_OFFSET (symbol) = SYMBOL_REF_BLOCK_OFFSET (target);
7073 return;
7075 alignment = get_variable_align (decl);
7076 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
7077 if ((flag_sanitize & SANITIZE_ADDRESS)
7078 && asan_protect_global (decl))
7080 size += asan_red_zone_size (size);
7081 alignment = MAX (alignment,
7082 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
7086 /* Calculate the object's offset from the start of the block. */
7087 block = SYMBOL_REF_BLOCK (symbol);
7088 mask = alignment / BITS_PER_UNIT - 1;
7089 offset = (block->size + mask) & ~mask;
7090 SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
7092 /* Record the block's new alignment and size. */
7093 block->alignment = MAX (block->alignment, alignment);
7094 block->size = offset + size;
7096 vec_safe_push (block->objects, symbol);
7099 /* Return the anchor that should be used to address byte offset OFFSET
7100 from the first object in BLOCK. MODEL is the TLS model used
7101 to access it. */
7104 get_section_anchor (struct object_block *block, HOST_WIDE_INT offset,
7105 enum tls_model model)
7107 char label[100];
7108 unsigned int begin, middle, end;
7109 unsigned HOST_WIDE_INT min_offset, max_offset, range, bias, delta;
7110 rtx anchor;
7112 /* Work out the anchor's offset. Use an offset of 0 for the first
7113 anchor so that we don't pessimize the case where we take the address
7114 of a variable at the beginning of the block. This is particularly
7115 useful when a block has only one variable assigned to it.
7117 We try to place anchors RANGE bytes apart, so there can then be
7118 anchors at +/-RANGE, +/-2 * RANGE, and so on, up to the limits of
7119 a ptr_mode offset. With some target settings, the lowest such
7120 anchor might be out of range for the lowest ptr_mode offset;
7121 likewise the highest anchor for the highest offset. Use anchors
7122 at the extreme ends of the ptr_mode range in such cases.
7124 All arithmetic uses unsigned integers in order to avoid
7125 signed overflow. */
7126 max_offset = (unsigned HOST_WIDE_INT) targetm.max_anchor_offset;
7127 min_offset = (unsigned HOST_WIDE_INT) targetm.min_anchor_offset;
7128 range = max_offset - min_offset + 1;
7129 if (range == 0)
7130 offset = 0;
7131 else
7133 bias = 1 << (GET_MODE_BITSIZE (ptr_mode) - 1);
7134 if (offset < 0)
7136 delta = -(unsigned HOST_WIDE_INT) offset + max_offset;
7137 delta -= delta % range;
7138 if (delta > bias)
7139 delta = bias;
7140 offset = (HOST_WIDE_INT) (-delta);
7142 else
7144 delta = (unsigned HOST_WIDE_INT) offset - min_offset;
7145 delta -= delta % range;
7146 if (delta > bias - 1)
7147 delta = bias - 1;
7148 offset = (HOST_WIDE_INT) delta;
7152 /* Do a binary search to see if there's already an anchor we can use.
7153 Set BEGIN to the new anchor's index if not. */
7154 begin = 0;
7155 end = vec_safe_length (block->anchors);
7156 while (begin != end)
7158 middle = (end + begin) / 2;
7159 anchor = (*block->anchors)[middle];
7160 if (SYMBOL_REF_BLOCK_OFFSET (anchor) > offset)
7161 end = middle;
7162 else if (SYMBOL_REF_BLOCK_OFFSET (anchor) < offset)
7163 begin = middle + 1;
7164 else if (SYMBOL_REF_TLS_MODEL (anchor) > model)
7165 end = middle;
7166 else if (SYMBOL_REF_TLS_MODEL (anchor) < model)
7167 begin = middle + 1;
7168 else
7169 return anchor;
7172 /* Create a new anchor with a unique label. */
7173 ASM_GENERATE_INTERNAL_LABEL (label, "LANCHOR", anchor_labelno++);
7174 anchor = create_block_symbol (ggc_strdup (label), block, offset);
7175 SYMBOL_REF_FLAGS (anchor) |= SYMBOL_FLAG_LOCAL | SYMBOL_FLAG_ANCHOR;
7176 SYMBOL_REF_FLAGS (anchor) |= model << SYMBOL_FLAG_TLS_SHIFT;
7178 /* Insert it at index BEGIN. */
7179 vec_safe_insert (block->anchors, begin, anchor);
7180 return anchor;
7183 /* Output the objects in BLOCK. */
7185 static void
7186 output_object_block (struct object_block *block)
7188 struct constant_descriptor_rtx *desc;
7189 unsigned int i;
7190 HOST_WIDE_INT offset;
7191 tree decl;
7192 rtx symbol;
7194 if (!block->objects)
7195 return;
7197 /* Switch to the section and make sure that the first byte is
7198 suitably aligned. */
7199 switch_to_section (block->sect);
7200 assemble_align (block->alignment);
7202 /* Define the values of all anchors relative to the current section
7203 position. */
7204 FOR_EACH_VEC_SAFE_ELT (block->anchors, i, symbol)
7205 targetm.asm_out.output_anchor (symbol);
7207 /* Output the objects themselves. */
7208 offset = 0;
7209 FOR_EACH_VEC_ELT (*block->objects, i, symbol)
7211 /* Move to the object's offset, padding with zeros if necessary. */
7212 assemble_zeros (SYMBOL_REF_BLOCK_OFFSET (symbol) - offset);
7213 offset = SYMBOL_REF_BLOCK_OFFSET (symbol);
7214 if (CONSTANT_POOL_ADDRESS_P (symbol))
7216 desc = SYMBOL_REF_CONSTANT (symbol);
7217 output_constant_pool_1 (desc, 1);
7218 offset += GET_MODE_SIZE (desc->mode);
7220 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
7222 HOST_WIDE_INT size;
7223 decl = SYMBOL_REF_DECL (symbol);
7224 assemble_constant_contents (DECL_INITIAL (decl), XSTR (symbol, 0),
7225 DECL_ALIGN (decl));
7226 size = get_constant_size (DECL_INITIAL (decl));
7227 offset += size;
7228 if ((flag_sanitize & SANITIZE_ADDRESS)
7229 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
7230 && asan_protect_global (DECL_INITIAL (decl)))
7232 size = asan_red_zone_size (size);
7233 assemble_zeros (size);
7234 offset += size;
7237 else
7239 HOST_WIDE_INT size;
7240 decl = SYMBOL_REF_DECL (symbol);
7241 assemble_variable_contents (decl, XSTR (symbol, 0), false);
7242 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
7243 offset += size;
7244 if ((flag_sanitize & SANITIZE_ADDRESS)
7245 && asan_protect_global (decl))
7247 size = asan_red_zone_size (size);
7248 assemble_zeros (size);
7249 offset += size;
7255 /* A htab_traverse callback used to call output_object_block for
7256 each member of object_block_htab. */
7258 static int
7259 output_object_block_htab (void **slot, void *data ATTRIBUTE_UNUSED)
7261 output_object_block ((struct object_block *) (*slot));
7262 return 1;
7265 /* Output the definitions of all object_blocks. */
7267 void
7268 output_object_blocks (void)
7270 htab_traverse (object_block_htab, output_object_block_htab, NULL);
7273 /* This function provides a possible implementation of the
7274 TARGET_ASM_RECORD_GCC_SWITCHES target hook for ELF targets. When triggered
7275 by -frecord-gcc-switches it creates a new mergeable, string section in the
7276 assembler output file called TARGET_ASM_RECORD_GCC_SWITCHES_SECTION which
7277 contains the switches in ASCII format.
7279 FIXME: This code does not correctly handle double quote characters
7280 that appear inside strings, (it strips them rather than preserving them).
7281 FIXME: ASM_OUTPUT_ASCII, as defined in config/elfos.h will not emit NUL
7282 characters - instead it treats them as sub-string separators. Since
7283 we want to emit NUL strings terminators into the object file we have to use
7284 ASM_OUTPUT_SKIP. */
7287 elf_record_gcc_switches (print_switch_type type, const char * name)
7289 switch (type)
7291 case SWITCH_TYPE_PASSED:
7292 ASM_OUTPUT_ASCII (asm_out_file, name, strlen (name));
7293 ASM_OUTPUT_SKIP (asm_out_file, (unsigned HOST_WIDE_INT) 1);
7294 break;
7296 case SWITCH_TYPE_DESCRIPTIVE:
7297 if (name == NULL)
7299 /* Distinguish between invocations where name is NULL. */
7300 static bool started = false;
7302 if (!started)
7304 section * sec;
7306 sec = get_section (targetm.asm_out.record_gcc_switches_section,
7307 SECTION_DEBUG
7308 | SECTION_MERGE
7309 | SECTION_STRINGS
7310 | (SECTION_ENTSIZE & 1),
7311 NULL);
7312 switch_to_section (sec);
7313 started = true;
7317 default:
7318 break;
7321 /* The return value is currently ignored by the caller, but must be 0.
7322 For -fverbose-asm the return value would be the number of characters
7323 emitted into the assembler file. */
7324 return 0;
7327 /* Emit text to declare externally defined symbols. It is needed to
7328 properly support non-default visibility. */
7329 void
7330 default_elf_asm_output_external (FILE *file ATTRIBUTE_UNUSED,
7331 tree decl,
7332 const char *name ATTRIBUTE_UNUSED)
7334 /* We output the name if and only if TREE_SYMBOL_REFERENCED is
7335 set in order to avoid putting out names that are never really
7336 used. */
7337 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
7338 && targetm.binds_local_p (decl))
7339 maybe_assemble_visibility (decl);
7342 /* The default hook for TARGET_ASM_OUTPUT_SOURCE_FILENAME. */
7344 void
7345 default_asm_output_source_filename (FILE *file, const char *name)
7347 #ifdef ASM_OUTPUT_SOURCE_FILENAME
7348 ASM_OUTPUT_SOURCE_FILENAME (file, name);
7349 #else
7350 fprintf (file, "\t.file\t");
7351 output_quoted_string (file, name);
7352 putc ('\n', file);
7353 #endif
7356 /* Output a file name in the form wanted by System V. */
7358 void
7359 output_file_directive (FILE *asm_file, const char *input_name)
7361 int len;
7362 const char *na;
7364 if (input_name == NULL)
7365 input_name = "<stdin>";
7366 else
7367 input_name = remap_debug_filename (input_name);
7369 len = strlen (input_name);
7370 na = input_name + len;
7372 /* NA gets INPUT_NAME sans directory names. */
7373 while (na > input_name)
7375 if (IS_DIR_SEPARATOR (na[-1]))
7376 break;
7377 na--;
7380 targetm.asm_out.output_source_filename (asm_file, na);
7383 /* Create a DEBUG_EXPR_DECL / DEBUG_EXPR pair from RTL expression
7384 EXP. */
7386 make_debug_expr_from_rtl (const_rtx exp)
7388 tree ddecl = make_node (DEBUG_EXPR_DECL), type;
7389 enum machine_mode mode = GET_MODE (exp);
7390 rtx dval;
7392 DECL_ARTIFICIAL (ddecl) = 1;
7393 if (REG_P (exp) && REG_EXPR (exp))
7394 type = TREE_TYPE (REG_EXPR (exp));
7395 else if (MEM_P (exp) && MEM_EXPR (exp))
7396 type = TREE_TYPE (MEM_EXPR (exp));
7397 else
7398 type = NULL_TREE;
7399 if (type && TYPE_MODE (type) == mode)
7400 TREE_TYPE (ddecl) = type;
7401 else
7402 TREE_TYPE (ddecl) = lang_hooks.types.type_for_mode (mode, 1);
7403 DECL_MODE (ddecl) = mode;
7404 dval = gen_rtx_DEBUG_EXPR (mode);
7405 DEBUG_EXPR_TREE_DECL (dval) = ddecl;
7406 SET_DECL_RTL (ddecl, dval);
7407 return dval;
7410 #ifdef ELF_ASCII_ESCAPES
7411 /* Default ASM_OUTPUT_LIMITED_STRING for ELF targets. */
7413 void
7414 default_elf_asm_output_limited_string (FILE *f, const char *s)
7416 int escape;
7417 unsigned char c;
7419 fputs (STRING_ASM_OP, f);
7420 putc ('"', f);
7421 while (*s != '\0')
7423 c = *s;
7424 escape = ELF_ASCII_ESCAPES[c];
7425 switch (escape)
7427 case 0:
7428 putc (c, f);
7429 break;
7430 case 1:
7431 /* TODO: Print in hex with fast function, important for -flto. */
7432 fprintf (f, "\\%03o", c);
7433 break;
7434 default:
7435 putc ('\\', f);
7436 putc (escape, f);
7437 break;
7439 s++;
7441 putc ('\"', f);
7442 putc ('\n', f);
7445 /* Default ASM_OUTPUT_ASCII for ELF targets. */
7447 void
7448 default_elf_asm_output_ascii (FILE *f, const char *s, unsigned int len)
7450 const char *limit = s + len;
7451 const char *last_null = NULL;
7452 unsigned bytes_in_chunk = 0;
7453 unsigned char c;
7454 int escape;
7456 for (; s < limit; s++)
7458 const char *p;
7460 if (bytes_in_chunk >= 60)
7462 putc ('\"', f);
7463 putc ('\n', f);
7464 bytes_in_chunk = 0;
7467 if (s > last_null)
7469 for (p = s; p < limit && *p != '\0'; p++)
7470 continue;
7471 last_null = p;
7473 else
7474 p = last_null;
7476 if (p < limit && (p - s) <= (long) ELF_STRING_LIMIT)
7478 if (bytes_in_chunk > 0)
7480 putc ('\"', f);
7481 putc ('\n', f);
7482 bytes_in_chunk = 0;
7485 default_elf_asm_output_limited_string (f, s);
7486 s = p;
7488 else
7490 if (bytes_in_chunk == 0)
7491 fputs (ASCII_DATA_ASM_OP "\"", f);
7493 c = *s;
7494 escape = ELF_ASCII_ESCAPES[c];
7495 switch (escape)
7497 case 0:
7498 putc (c, f);
7499 bytes_in_chunk++;
7500 break;
7501 case 1:
7502 /* TODO: Print in hex with fast function, important for -flto. */
7503 fprintf (f, "\\%03o", c);
7504 bytes_in_chunk += 4;
7505 break;
7506 default:
7507 putc ('\\', f);
7508 putc (escape, f);
7509 bytes_in_chunk += 2;
7510 break;
7516 if (bytes_in_chunk > 0)
7518 putc ('\"', f);
7519 putc ('\n', f);
7522 #endif
7524 static GTY(()) section *elf_init_array_section;
7525 static GTY(()) section *elf_fini_array_section;
7527 static section *
7528 get_elf_initfini_array_priority_section (int priority,
7529 bool constructor_p)
7531 section *sec;
7532 if (priority != DEFAULT_INIT_PRIORITY)
7534 char buf[18];
7535 sprintf (buf, "%s.%.5u",
7536 constructor_p ? ".init_array" : ".fini_array",
7537 priority);
7538 sec = get_section (buf, SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
7540 else
7542 if (constructor_p)
7544 if (elf_init_array_section == NULL)
7545 elf_init_array_section
7546 = get_section (".init_array",
7547 SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
7548 sec = elf_init_array_section;
7550 else
7552 if (elf_fini_array_section == NULL)
7553 elf_fini_array_section
7554 = get_section (".fini_array",
7555 SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
7556 sec = elf_fini_array_section;
7559 return sec;
7562 /* Use .init_array section for constructors. */
7564 void
7565 default_elf_init_array_asm_out_constructor (rtx symbol, int priority)
7567 section *sec = get_elf_initfini_array_priority_section (priority,
7568 true);
7569 assemble_addr_to_section (symbol, sec);
7572 /* Use .fini_array section for destructors. */
7574 void
7575 default_elf_fini_array_asm_out_destructor (rtx symbol, int priority)
7577 section *sec = get_elf_initfini_array_priority_section (priority,
7578 false);
7579 assemble_addr_to_section (symbol, sec);
7582 /* Default TARGET_ASM_OUTPUT_IDENT hook.
7584 This is a bit of a cheat. The real default is a no-op, but this
7585 hook is the default for all targets with a .ident directive. */
7587 void
7588 default_asm_output_ident_directive (const char *ident_str)
7590 const char *ident_asm_op = "\t.ident\t";
7592 /* If we are still in the front end, do not write out the string
7593 to asm_out_file. Instead, add a fake top-level asm statement.
7594 This allows the front ends to use this hook without actually
7595 writing to asm_out_file, to handle #ident or Pragma Ident. */
7596 if (symtab->state == PARSING)
7598 char *buf = ACONCAT ((ident_asm_op, "\"", ident_str, "\"\n", NULL));
7599 symtab->finalize_toplevel_asm (build_string (strlen (buf), buf));
7601 else
7602 fprintf (asm_out_file, "%s\"%s\"\n", ident_asm_op, ident_str);
7605 #include "gt-varasm.h"