Fix bootstrap/PR63632
[official-gcc.git] / gcc / varasm.c
blobd4b5c6b027a0faac86f6de750313006d0566adb4
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 "hashtab.h"
39 #include "hash-set.h"
40 #include "vec.h"
41 #include "machmode.h"
42 #include "hard-reg-set.h"
43 #include "input.h"
44 #include "function.h"
45 #include "expr.h"
46 #include "regs.h"
47 #include "output.h"
48 #include "diagnostic-core.h"
49 #include "ggc.h"
50 #include "langhooks.h"
51 #include "tm_p.h"
52 #include "debug.h"
53 #include "target.h"
54 #include "common/common-target.h"
55 #include "targhooks.h"
56 #include "cgraph.h"
57 #include "asan.h"
58 #include "basic-block.h"
59 #include "rtl-iter.h"
61 #ifdef XCOFF_DEBUGGING_INFO
62 #include "xcoffout.h" /* Needed for external data
63 declarations for e.g. AIX 4.x. */
64 #endif
66 /* The (assembler) name of the first globally-visible object output. */
67 extern GTY(()) const char *first_global_object_name;
68 extern GTY(()) const char *weak_global_object_name;
70 const char *first_global_object_name;
71 const char *weak_global_object_name;
73 struct addr_const;
74 struct constant_descriptor_rtx;
75 struct rtx_constant_pool;
77 #define n_deferred_constants (crtl->varasm.deferred_constants)
79 /* Number for making the label on the next
80 constant that is stored in memory. */
82 static GTY(()) int const_labelno;
84 /* Carry information from ASM_DECLARE_OBJECT_NAME
85 to ASM_FINISH_DECLARE_OBJECT. */
87 int size_directive_output;
89 /* The last decl for which assemble_variable was called,
90 if it did ASM_DECLARE_OBJECT_NAME.
91 If the last call to assemble_variable didn't do that,
92 this holds 0. */
94 tree last_assemble_variable_decl;
96 /* The following global variable indicates if the first basic block
97 in a function belongs to the cold partition or not. */
99 bool first_function_block_is_cold;
101 /* Whether we saw any functions with no_split_stack. */
103 static bool saw_no_split_stack;
105 static const char *strip_reg_name (const char *);
106 static int contains_pointers_p (tree);
107 #ifdef ASM_OUTPUT_EXTERNAL
108 static bool incorporeal_function_p (tree);
109 #endif
110 static void decode_addr_const (tree, struct addr_const *);
111 static hashval_t const_hash_1 (const tree);
112 static int compare_constant (const tree, const tree);
113 static void output_constant_def_contents (rtx);
114 static void output_addressed_constants (tree);
115 static unsigned HOST_WIDE_INT output_constant (tree, unsigned HOST_WIDE_INT,
116 unsigned int);
117 static void globalize_decl (tree);
118 static bool decl_readonly_section_1 (enum section_category);
119 #ifdef BSS_SECTION_ASM_OP
120 #ifdef ASM_OUTPUT_ALIGNED_BSS
121 static void asm_output_aligned_bss (FILE *, tree, const char *,
122 unsigned HOST_WIDE_INT, int)
123 ATTRIBUTE_UNUSED;
124 #endif
125 #endif /* BSS_SECTION_ASM_OP */
126 static void mark_weak (tree);
127 static void output_constant_pool (const char *, tree);
129 /* Well-known sections, each one associated with some sort of *_ASM_OP. */
130 section *text_section;
131 section *data_section;
132 section *readonly_data_section;
133 section *sdata_section;
134 section *ctors_section;
135 section *dtors_section;
136 section *bss_section;
137 section *sbss_section;
139 /* Various forms of common section. All are guaranteed to be nonnull. */
140 section *tls_comm_section;
141 section *comm_section;
142 section *lcomm_section;
144 /* A SECTION_NOSWITCH section used for declaring global BSS variables.
145 May be null. */
146 section *bss_noswitch_section;
148 /* The section that holds the main exception table, when known. The section
149 is set either by the target's init_sections hook or by the first call to
150 switch_to_exception_section. */
151 section *exception_section;
153 /* The section that holds the DWARF2 frame unwind information, when known.
154 The section is set either by the target's init_sections hook or by the
155 first call to switch_to_eh_frame_section. */
156 section *eh_frame_section;
158 /* asm_out_file's current section. This is NULL if no section has yet
159 been selected or if we lose track of what the current section is. */
160 section *in_section;
162 /* True if code for the current function is currently being directed
163 at the cold section. */
164 bool in_cold_section_p;
166 /* A linked list of all the unnamed sections. */
167 static GTY(()) section *unnamed_sections;
169 /* Return a nonzero value if DECL has a section attribute. */
170 #define IN_NAMED_SECTION(DECL) \
171 ((TREE_CODE (DECL) == FUNCTION_DECL || TREE_CODE (DECL) == VAR_DECL) \
172 && DECL_SECTION_NAME (DECL) != NULL)
174 struct section_hasher : ggc_hasher<section *>
176 typedef const char *compare_type;
178 static hashval_t hash (section *);
179 static bool equal (section *, const char *);
182 /* Hash table of named sections. */
183 static GTY(()) hash_table<section_hasher> *section_htab;
185 struct object_block_hasher : ggc_hasher<object_block *>
187 typedef const section *compare_type;
189 static hashval_t hash (object_block *);
190 static bool equal (object_block *, const section *);
193 /* A table of object_blocks, indexed by section. */
194 static GTY(()) hash_table<object_block_hasher> *object_block_htab;
196 /* The next number to use for internal anchor labels. */
197 static GTY(()) int anchor_labelno;
199 /* A pool of constants that can be shared between functions. */
200 static GTY(()) struct rtx_constant_pool *shared_constant_pool;
202 /* Helper routines for maintaining section_htab. */
204 bool
205 section_hasher::equal (section *old, const char *new_name)
207 return strcmp (old->named.name, new_name) == 0;
210 hashval_t
211 section_hasher::hash (section *old)
213 return htab_hash_string (old->named.name);
216 /* Return a hash value for section SECT. */
218 static hashval_t
219 hash_section (section *sect)
221 if (sect->common.flags & SECTION_NAMED)
222 return htab_hash_string (sect->named.name);
223 return sect->common.flags;
226 /* Helper routines for maintaining object_block_htab. */
228 inline bool
229 object_block_hasher::equal (object_block *old, const section *new_section)
231 return old->sect == new_section;
234 hashval_t
235 object_block_hasher::hash (object_block *old)
237 return hash_section (old->sect);
240 /* Return a new unnamed section with the given fields. */
242 section *
243 get_unnamed_section (unsigned int flags, void (*callback) (const void *),
244 const void *data)
246 section *sect;
248 sect = ggc_alloc<section> ();
249 sect->unnamed.common.flags = flags | SECTION_UNNAMED;
250 sect->unnamed.callback = callback;
251 sect->unnamed.data = data;
252 sect->unnamed.next = unnamed_sections;
254 unnamed_sections = sect;
255 return sect;
258 /* Return a SECTION_NOSWITCH section with the given fields. */
260 static section *
261 get_noswitch_section (unsigned int flags, noswitch_section_callback callback)
263 section *sect;
265 sect = ggc_alloc<section> ();
266 sect->noswitch.common.flags = flags | SECTION_NOSWITCH;
267 sect->noswitch.callback = callback;
269 return sect;
272 /* Return the named section structure associated with NAME. Create
273 a new section with the given fields if no such structure exists. */
275 section *
276 get_section (const char *name, unsigned int flags, tree decl)
278 section *sect, **slot;
280 slot = section_htab->find_slot_with_hash (name, htab_hash_string (name),
281 INSERT);
282 flags |= SECTION_NAMED;
283 if (*slot == NULL)
285 sect = ggc_alloc<section> ();
286 sect->named.common.flags = flags;
287 sect->named.name = ggc_strdup (name);
288 sect->named.decl = decl;
289 *slot = sect;
291 else
293 sect = *slot;
294 if ((sect->common.flags & ~SECTION_DECLARED) != flags
295 && ((sect->common.flags | flags) & SECTION_OVERRIDE) == 0)
297 /* It is fine if one of the section flags is
298 SECTION_WRITE | SECTION_RELRO and the other has none of these
299 flags (i.e. read-only) in named sections and either the
300 section hasn't been declared yet or has been declared as writable.
301 In that case just make sure the resulting flags are
302 SECTION_WRITE | SECTION_RELRO, ie. writable only because of
303 relocations. */
304 if (((sect->common.flags ^ flags) & (SECTION_WRITE | SECTION_RELRO))
305 == (SECTION_WRITE | SECTION_RELRO)
306 && (sect->common.flags
307 & ~(SECTION_DECLARED | SECTION_WRITE | SECTION_RELRO))
308 == (flags & ~(SECTION_WRITE | SECTION_RELRO))
309 && ((sect->common.flags & SECTION_DECLARED) == 0
310 || (sect->common.flags & SECTION_WRITE)))
312 sect->common.flags |= (SECTION_WRITE | SECTION_RELRO);
313 return sect;
315 /* Sanity check user variables for flag changes. */
316 if (sect->named.decl != NULL
317 && DECL_P (sect->named.decl)
318 && decl != sect->named.decl)
320 if (decl != NULL && DECL_P (decl))
321 error ("%+D causes a section type conflict with %D",
322 decl, sect->named.decl);
323 else
324 error ("section type conflict with %D", sect->named.decl);
325 inform (DECL_SOURCE_LOCATION (sect->named.decl),
326 "%qD was declared here", sect->named.decl);
328 else if (decl != NULL && DECL_P (decl))
329 error ("%+D causes a section type conflict", decl);
330 else
331 error ("section type conflict");
332 /* Make sure we don't error about one section multiple times. */
333 sect->common.flags |= SECTION_OVERRIDE;
336 return sect;
339 /* Return true if the current compilation mode benefits from having
340 objects grouped into blocks. */
342 static bool
343 use_object_blocks_p (void)
345 return flag_section_anchors;
348 /* Return the object_block structure for section SECT. Create a new
349 structure if we haven't created one already. Return null if SECT
350 itself is null. */
352 static struct object_block *
353 get_block_for_section (section *sect)
355 struct object_block *block;
357 if (sect == NULL)
358 return NULL;
360 object_block **slot
361 = object_block_htab->find_slot_with_hash (sect, hash_section (sect),
362 INSERT);
363 block = *slot;
364 if (block == NULL)
366 block = ggc_cleared_alloc<object_block> ();
367 block->sect = sect;
368 *slot = block;
370 return block;
373 /* Create a symbol with label LABEL and place it at byte offset
374 OFFSET in BLOCK. OFFSET can be negative if the symbol's offset
375 is not yet known. LABEL must be a garbage-collected string. */
377 static rtx
378 create_block_symbol (const char *label, struct object_block *block,
379 HOST_WIDE_INT offset)
381 rtx symbol;
382 unsigned int size;
384 /* Create the extended SYMBOL_REF. */
385 size = RTX_HDR_SIZE + sizeof (struct block_symbol);
386 symbol = (rtx) ggc_internal_alloc (size);
388 /* Initialize the normal SYMBOL_REF fields. */
389 memset (symbol, 0, size);
390 PUT_CODE (symbol, SYMBOL_REF);
391 PUT_MODE (symbol, Pmode);
392 XSTR (symbol, 0) = label;
393 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_HAS_BLOCK_INFO;
395 /* Initialize the block_symbol stuff. */
396 SYMBOL_REF_BLOCK (symbol) = block;
397 SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
399 return symbol;
402 /* Return a section with a particular name and with whatever SECTION_*
403 flags section_type_flags deems appropriate. The name of the section
404 is taken from NAME if nonnull, otherwise it is taken from DECL's
405 DECL_SECTION_NAME. DECL is the decl associated with the section
406 (see the section comment for details) and RELOC is as for
407 section_type_flags. */
409 section *
410 get_named_section (tree decl, const char *name, int reloc)
412 unsigned int flags;
414 if (name == NULL)
416 gcc_assert (decl && DECL_P (decl) && DECL_SECTION_NAME (decl));
417 name = DECL_SECTION_NAME (decl);
420 flags = targetm.section_type_flags (decl, name, reloc);
421 return get_section (name, flags, decl);
424 /* Worker for resolve_unique_section. */
426 static bool
427 set_implicit_section (struct symtab_node *n, void *data ATTRIBUTE_UNUSED)
429 n->implicit_section = true;
430 return false;
433 /* If required, set DECL_SECTION_NAME to a unique name. */
435 void
436 resolve_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED,
437 int flag_function_or_data_sections)
439 if (DECL_SECTION_NAME (decl) == NULL
440 && targetm_common.have_named_sections
441 && (flag_function_or_data_sections
442 || DECL_COMDAT_GROUP (decl)))
444 targetm.asm_out.unique_section (decl, reloc);
445 if (DECL_SECTION_NAME (decl))
446 symtab_node::get (decl)->call_for_symbol_and_aliases
447 (set_implicit_section, NULL, true);
451 #ifdef BSS_SECTION_ASM_OP
453 #ifdef ASM_OUTPUT_ALIGNED_BSS
455 /* Utility function for targets to use in implementing
456 ASM_OUTPUT_ALIGNED_BSS.
457 ??? It is believed that this function will work in most cases so such
458 support is localized here. */
460 static void
461 asm_output_aligned_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
462 const char *name, unsigned HOST_WIDE_INT size,
463 int align)
465 switch_to_section (bss_section);
466 ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
467 #ifdef ASM_DECLARE_OBJECT_NAME
468 last_assemble_variable_decl = decl;
469 ASM_DECLARE_OBJECT_NAME (file, name, decl);
470 #else
471 /* Standard thing is just output label for the object. */
472 ASM_OUTPUT_LABEL (file, name);
473 #endif /* ASM_DECLARE_OBJECT_NAME */
474 ASM_OUTPUT_SKIP (file, size ? size : 1);
477 #endif
479 #endif /* BSS_SECTION_ASM_OP */
481 #ifndef USE_SELECT_SECTION_FOR_FUNCTIONS
482 /* Return the hot section for function DECL. Return text_section for
483 null DECLs. */
485 static section *
486 hot_function_section (tree decl)
488 if (decl != NULL_TREE
489 && DECL_SECTION_NAME (decl) != NULL
490 && targetm_common.have_named_sections)
491 return get_named_section (decl, NULL, 0);
492 else
493 return text_section;
495 #endif
497 /* Return section for TEXT_SECTION_NAME if DECL or DECL_SECTION_NAME (DECL)
498 is NULL.
500 When DECL_SECTION_NAME is non-NULL and it is implicit section and
501 NAMED_SECTION_SUFFIX is non-NULL, then produce section called
502 concatenate the name with NAMED_SECTION_SUFFIX.
503 Otherwise produce "TEXT_SECTION_NAME.IMPLICIT_NAME". */
505 section *
506 get_named_text_section (tree decl,
507 const char *text_section_name,
508 const char *named_section_suffix)
510 if (decl && DECL_SECTION_NAME (decl))
512 if (named_section_suffix)
514 const char *dsn = DECL_SECTION_NAME (decl);
515 const char *stripped_name;
516 char *name, *buffer;
518 name = (char *) alloca (strlen (dsn) + 1);
519 memcpy (name, dsn,
520 strlen (dsn) + 1);
522 stripped_name = targetm.strip_name_encoding (name);
524 buffer = ACONCAT ((stripped_name, named_section_suffix, NULL));
525 return get_named_section (decl, buffer, 0);
527 else if (symtab_node::get (decl)->implicit_section)
529 const char *name;
531 /* Do not try to split gnu_linkonce functions. This gets somewhat
532 slipperly. */
533 if (DECL_COMDAT_GROUP (decl) && !HAVE_COMDAT_GROUP)
534 return NULL;
535 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
536 name = targetm.strip_name_encoding (name);
537 return get_named_section (decl, ACONCAT ((text_section_name, ".",
538 name, NULL)), 0);
540 else
541 return NULL;
543 return get_named_section (decl, text_section_name, 0);
546 /* Choose named function section based on its frequency. */
548 section *
549 default_function_section (tree decl, enum node_frequency freq,
550 bool startup, bool exit)
552 #if defined HAVE_LD_EH_GC_SECTIONS && defined HAVE_LD_EH_GC_SECTIONS_BUG
553 /* Old GNU linkers have buggy --gc-section support, which sometimes
554 results in .gcc_except_table* sections being garbage collected. */
555 if (decl
556 && symtab_node::get (decl)->implicit_section)
557 return NULL;
558 #endif
560 if (!flag_reorder_functions
561 || !targetm_common.have_named_sections)
562 return NULL;
563 /* Startup code should go to startup subsection unless it is
564 unlikely executed (this happens especially with function splitting
565 where we can split away unnecessary parts of static constructors. */
566 if (startup && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
568 /* If we do have a profile or(and) LTO phase is executed, we do not need
569 these ELF section. */
570 if (!in_lto_p || !flag_profile_values)
571 return get_named_text_section (decl, ".text.startup", NULL);
572 else
573 return NULL;
576 /* Similarly for exit. */
577 if (exit && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
578 return get_named_text_section (decl, ".text.exit", NULL);
580 /* Group cold functions together, similarly for hot code. */
581 switch (freq)
583 case NODE_FREQUENCY_UNLIKELY_EXECUTED:
584 return get_named_text_section (decl, ".text.unlikely", NULL);
585 case NODE_FREQUENCY_HOT:
586 /* If we do have a profile or(and) LTO phase is executed, we do not need
587 these ELF section. */
588 if (!in_lto_p || !flag_profile_values)
589 return get_named_text_section (decl, ".text.hot", NULL);
590 default:
591 return NULL;
595 /* Return the section for function DECL.
597 If DECL is NULL_TREE, return the text section. We can be passed
598 NULL_TREE under some circumstances by dbxout.c at least.
600 If FORCE_COLD is true, return cold function section ignoring
601 the frequency info of cgraph_node. */
603 static section *
604 function_section_1 (tree decl, bool force_cold)
606 section *section = NULL;
607 enum node_frequency freq = NODE_FREQUENCY_NORMAL;
608 bool startup = false, exit = false;
610 if (decl)
612 struct cgraph_node *node = cgraph_node::get (decl);
614 if (node)
616 freq = node->frequency;
617 startup = node->only_called_at_startup;
618 exit = node->only_called_at_exit;
621 if (force_cold)
622 freq = NODE_FREQUENCY_UNLIKELY_EXECUTED;
624 #ifdef USE_SELECT_SECTION_FOR_FUNCTIONS
625 if (decl != NULL_TREE
626 && DECL_SECTION_NAME (decl) != NULL)
628 if (targetm.asm_out.function_section)
629 section = targetm.asm_out.function_section (decl, freq,
630 startup, exit);
631 if (section)
632 return section;
633 return get_named_section (decl, NULL, 0);
635 else
636 return targetm.asm_out.select_section
637 (decl, freq == NODE_FREQUENCY_UNLIKELY_EXECUTED,
638 DECL_ALIGN (decl));
639 #else
640 if (targetm.asm_out.function_section)
641 section = targetm.asm_out.function_section (decl, freq, startup, exit);
642 if (section)
643 return section;
644 return hot_function_section (decl);
645 #endif
648 /* Return the section for function DECL.
650 If DECL is NULL_TREE, return the text section. We can be passed
651 NULL_TREE under some circumstances by dbxout.c at least. */
653 section *
654 function_section (tree decl)
656 /* Handle cases where function splitting code decides
657 to put function entry point into unlikely executed section
658 despite the fact that the function itself is not cold
659 (i.e. it is called rarely but contains a hot loop that is
660 better to live in hot subsection for the code locality). */
661 return function_section_1 (decl,
662 first_function_block_is_cold);
665 /* Return the section for the current function, take IN_COLD_SECTION_P
666 into account. */
668 section *
669 current_function_section (void)
671 return function_section_1 (current_function_decl, in_cold_section_p);
674 /* Tell assembler to switch to unlikely-to-be-executed text section. */
676 section *
677 unlikely_text_section (void)
679 return function_section_1 (current_function_decl, true);
682 /* When called within a function context, return true if the function
683 has been assigned a cold text section and if SECT is that section.
684 When called outside a function context, return true if SECT is the
685 default cold section. */
687 bool
688 unlikely_text_section_p (section *sect)
690 return sect == function_section_1 (current_function_decl, true);
693 /* Return the read-only data section associated with function DECL. */
695 section *
696 default_function_rodata_section (tree decl)
698 if (decl != NULL_TREE && DECL_SECTION_NAME (decl))
700 const char *name = DECL_SECTION_NAME (decl);
702 if (DECL_COMDAT_GROUP (decl) && HAVE_COMDAT_GROUP)
704 const char *dot;
705 size_t len;
706 char* rname;
708 dot = strchr (name + 1, '.');
709 if (!dot)
710 dot = name;
711 len = strlen (dot) + 8;
712 rname = (char *) alloca (len);
714 strcpy (rname, ".rodata");
715 strcat (rname, dot);
716 return get_section (rname, SECTION_LINKONCE, decl);
718 /* For .gnu.linkonce.t.foo we want to use .gnu.linkonce.r.foo. */
719 else if (DECL_COMDAT_GROUP (decl)
720 && strncmp (name, ".gnu.linkonce.t.", 16) == 0)
722 size_t len = strlen (name) + 1;
723 char *rname = (char *) alloca (len);
725 memcpy (rname, name, len);
726 rname[14] = 'r';
727 return get_section (rname, SECTION_LINKONCE, decl);
729 /* For .text.foo we want to use .rodata.foo. */
730 else if (flag_function_sections && flag_data_sections
731 && strncmp (name, ".text.", 6) == 0)
733 size_t len = strlen (name) + 1;
734 char *rname = (char *) alloca (len + 2);
736 memcpy (rname, ".rodata", 7);
737 memcpy (rname + 7, name + 5, len - 5);
738 return get_section (rname, 0, decl);
742 return readonly_data_section;
745 /* Return the read-only data section associated with function DECL
746 for targets where that section should be always the single
747 readonly data section. */
749 section *
750 default_no_function_rodata_section (tree decl ATTRIBUTE_UNUSED)
752 return readonly_data_section;
755 /* Return the section to use for string merging. */
757 static section *
758 mergeable_string_section (tree decl ATTRIBUTE_UNUSED,
759 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
760 unsigned int flags ATTRIBUTE_UNUSED)
762 HOST_WIDE_INT len;
764 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
765 && TREE_CODE (decl) == STRING_CST
766 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
767 && align <= 256
768 && (len = int_size_in_bytes (TREE_TYPE (decl))) > 0
769 && TREE_STRING_LENGTH (decl) >= len)
771 enum machine_mode mode;
772 unsigned int modesize;
773 const char *str;
774 HOST_WIDE_INT i;
775 int j, unit;
776 const char *prefix = targetm.asm_out.mergeable_rodata_prefix;
777 char *name = (char *) alloca (strlen (prefix) + 30);
779 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (decl)));
780 modesize = GET_MODE_BITSIZE (mode);
781 if (modesize >= 8 && modesize <= 256
782 && (modesize & (modesize - 1)) == 0)
784 if (align < modesize)
785 align = modesize;
787 str = TREE_STRING_POINTER (decl);
788 unit = GET_MODE_SIZE (mode);
790 /* Check for embedded NUL characters. */
791 for (i = 0; i < len; i += unit)
793 for (j = 0; j < unit; j++)
794 if (str[i + j] != '\0')
795 break;
796 if (j == unit)
797 break;
799 if (i == len - unit)
801 sprintf (name, "%s.str%d.%d", prefix,
802 modesize / 8, (int) (align / 8));
803 flags |= (modesize / 8) | SECTION_MERGE | SECTION_STRINGS;
804 return get_section (name, flags, NULL);
809 return readonly_data_section;
812 /* Return the section to use for constant merging. */
814 section *
815 mergeable_constant_section (enum machine_mode mode ATTRIBUTE_UNUSED,
816 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
817 unsigned int flags ATTRIBUTE_UNUSED)
819 unsigned int modesize = GET_MODE_BITSIZE (mode);
821 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
822 && mode != VOIDmode
823 && mode != BLKmode
824 && modesize <= align
825 && align >= 8
826 && align <= 256
827 && (align & (align - 1)) == 0)
829 const char *prefix = targetm.asm_out.mergeable_rodata_prefix;
830 char *name = (char *) alloca (strlen (prefix) + 30);
832 sprintf (name, "%s.cst%d", prefix, (int) (align / 8));
833 flags |= (align / 8) | SECTION_MERGE;
834 return get_section (name, flags, NULL);
836 return readonly_data_section;
839 /* Given NAME, a putative register name, discard any customary prefixes. */
841 static const char *
842 strip_reg_name (const char *name)
844 #ifdef REGISTER_PREFIX
845 if (!strncmp (name, REGISTER_PREFIX, strlen (REGISTER_PREFIX)))
846 name += strlen (REGISTER_PREFIX);
847 #endif
848 if (name[0] == '%' || name[0] == '#')
849 name++;
850 return name;
853 /* The user has asked for a DECL to have a particular name. Set (or
854 change) it in such a way that we don't prefix an underscore to
855 it. */
856 void
857 set_user_assembler_name (tree decl, const char *name)
859 char *starred = (char *) alloca (strlen (name) + 2);
860 starred[0] = '*';
861 strcpy (starred + 1, name);
862 symtab->change_decl_assembler_name (decl, get_identifier (starred));
863 SET_DECL_RTL (decl, NULL_RTX);
866 /* Decode an `asm' spec for a declaration as a register name.
867 Return the register number, or -1 if nothing specified,
868 or -2 if the ASMSPEC is not `cc' or `memory' and is not recognized,
869 or -3 if ASMSPEC is `cc' and is not recognized,
870 or -4 if ASMSPEC is `memory' and is not recognized.
871 Accept an exact spelling or a decimal number.
872 Prefixes such as % are optional. */
875 decode_reg_name_and_count (const char *asmspec, int *pnregs)
877 /* Presume just one register is clobbered. */
878 *pnregs = 1;
880 if (asmspec != 0)
882 int i;
884 /* Get rid of confusing prefixes. */
885 asmspec = strip_reg_name (asmspec);
887 /* Allow a decimal number as a "register name". */
888 for (i = strlen (asmspec) - 1; i >= 0; i--)
889 if (! ISDIGIT (asmspec[i]))
890 break;
891 if (asmspec[0] != 0 && i < 0)
893 i = atoi (asmspec);
894 if (i < FIRST_PSEUDO_REGISTER && i >= 0 && reg_names[i][0])
895 return i;
896 else
897 return -2;
900 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
901 if (reg_names[i][0]
902 && ! strcmp (asmspec, strip_reg_name (reg_names[i])))
903 return i;
905 #ifdef OVERLAPPING_REGISTER_NAMES
907 static const struct
909 const char *const name;
910 const int number;
911 const int nregs;
912 } table[] = OVERLAPPING_REGISTER_NAMES;
914 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
915 if (table[i].name[0]
916 && ! strcmp (asmspec, table[i].name))
918 *pnregs = table[i].nregs;
919 return table[i].number;
922 #endif /* OVERLAPPING_REGISTER_NAMES */
924 #ifdef ADDITIONAL_REGISTER_NAMES
926 static const struct { const char *const name; const int number; } table[]
927 = ADDITIONAL_REGISTER_NAMES;
929 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
930 if (table[i].name[0]
931 && ! strcmp (asmspec, table[i].name)
932 && reg_names[table[i].number][0])
933 return table[i].number;
935 #endif /* ADDITIONAL_REGISTER_NAMES */
937 if (!strcmp (asmspec, "memory"))
938 return -4;
940 if (!strcmp (asmspec, "cc"))
941 return -3;
943 return -2;
946 return -1;
950 decode_reg_name (const char *name)
952 int count;
953 return decode_reg_name_and_count (name, &count);
957 /* Return true if DECL's initializer is suitable for a BSS section. */
959 bool
960 bss_initializer_p (const_tree decl)
962 return (DECL_INITIAL (decl) == NULL
963 /* In LTO we have no errors in program; error_mark_node is used
964 to mark offlined constructors. */
965 || (DECL_INITIAL (decl) == error_mark_node
966 && !in_lto_p)
967 || (flag_zero_initialized_in_bss
968 /* Leave constant zeroes in .rodata so they
969 can be shared. */
970 && !TREE_READONLY (decl)
971 && initializer_zerop (DECL_INITIAL (decl))));
974 /* Compute the alignment of variable specified by DECL.
975 DONT_OUTPUT_DATA is from assemble_variable. */
977 void
978 align_variable (tree decl, bool dont_output_data)
980 unsigned int align = DECL_ALIGN (decl);
982 /* In the case for initialing an array whose length isn't specified,
983 where we have not yet been able to do the layout,
984 figure out the proper alignment now. */
985 if (dont_output_data && DECL_SIZE (decl) == 0
986 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
987 align = MAX (align, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (decl))));
989 /* Some object file formats have a maximum alignment which they support.
990 In particular, a.out format supports a maximum alignment of 4. */
991 if (align > MAX_OFILE_ALIGNMENT)
993 error ("alignment of %q+D is greater than maximum object "
994 "file alignment %d", decl,
995 MAX_OFILE_ALIGNMENT/BITS_PER_UNIT);
996 align = MAX_OFILE_ALIGNMENT;
999 if (! DECL_USER_ALIGN (decl))
1001 #ifdef DATA_ABI_ALIGNMENT
1002 unsigned int data_abi_align
1003 = DATA_ABI_ALIGNMENT (TREE_TYPE (decl), align);
1004 /* For backwards compatibility, don't assume the ABI alignment for
1005 TLS variables. */
1006 if (! DECL_THREAD_LOCAL_P (decl) || data_abi_align <= BITS_PER_WORD)
1007 align = data_abi_align;
1008 #endif
1010 /* On some machines, it is good to increase alignment sometimes.
1011 But as DECL_ALIGN is used both for actually emitting the variable
1012 and for code accessing the variable as guaranteed alignment, we
1013 can only increase the alignment if it is a performance optimization
1014 if the references to it must bind to the current definition. */
1015 if (decl_binds_to_current_def_p (decl)
1016 && !DECL_VIRTUAL_P (decl))
1018 #ifdef DATA_ALIGNMENT
1019 unsigned int data_align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
1020 /* Don't increase alignment too much for TLS variables - TLS space
1021 is too precious. */
1022 if (! DECL_THREAD_LOCAL_P (decl) || data_align <= BITS_PER_WORD)
1023 align = data_align;
1024 #endif
1025 #ifdef CONSTANT_ALIGNMENT
1026 if (DECL_INITIAL (decl) != 0
1027 /* In LTO we have no errors in program; error_mark_node is used
1028 to mark offlined constructors. */
1029 && (in_lto_p || DECL_INITIAL (decl) != error_mark_node))
1031 unsigned int const_align
1032 = CONSTANT_ALIGNMENT (DECL_INITIAL (decl), align);
1033 /* Don't increase alignment too much for TLS variables - TLS
1034 space is too precious. */
1035 if (! DECL_THREAD_LOCAL_P (decl) || const_align <= BITS_PER_WORD)
1036 align = const_align;
1038 #endif
1042 /* Reset the alignment in case we have made it tighter, so we can benefit
1043 from it in get_pointer_alignment. */
1044 DECL_ALIGN (decl) = align;
1047 /* Return DECL_ALIGN (decl), possibly increased for optimization purposes
1048 beyond what align_variable returned. */
1050 static unsigned int
1051 get_variable_align (tree decl)
1053 unsigned int align = DECL_ALIGN (decl);
1055 /* For user aligned vars or static vars align_variable already did
1056 everything. */
1057 if (DECL_USER_ALIGN (decl) || !TREE_PUBLIC (decl))
1058 return align;
1060 #ifdef DATA_ABI_ALIGNMENT
1061 if (DECL_THREAD_LOCAL_P (decl))
1062 align = DATA_ABI_ALIGNMENT (TREE_TYPE (decl), align);
1063 #endif
1065 /* For decls that bind to the current definition, align_variable
1066 did also everything, except for not assuming ABI required alignment
1067 of TLS variables. For other vars, increase the alignment here
1068 as an optimization. */
1069 if (!decl_binds_to_current_def_p (decl))
1071 /* On some machines, it is good to increase alignment sometimes. */
1072 #ifdef DATA_ALIGNMENT
1073 unsigned int data_align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
1074 /* Don't increase alignment too much for TLS variables - TLS space
1075 is too precious. */
1076 if (! DECL_THREAD_LOCAL_P (decl) || data_align <= BITS_PER_WORD)
1077 align = data_align;
1078 #endif
1079 #ifdef CONSTANT_ALIGNMENT
1080 if (DECL_INITIAL (decl) != 0
1081 /* In LTO we have no errors in program; error_mark_node is used
1082 to mark offlined constructors. */
1083 && (in_lto_p || DECL_INITIAL (decl) != error_mark_node))
1085 unsigned int const_align = CONSTANT_ALIGNMENT (DECL_INITIAL (decl),
1086 align);
1087 /* Don't increase alignment too much for TLS variables - TLS space
1088 is too precious. */
1089 if (! DECL_THREAD_LOCAL_P (decl) || const_align <= BITS_PER_WORD)
1090 align = const_align;
1092 #endif
1095 return align;
1098 /* Return the section into which the given VAR_DECL or CONST_DECL
1099 should be placed. PREFER_NOSWITCH_P is true if a noswitch
1100 section should be used wherever possible. */
1102 section *
1103 get_variable_section (tree decl, bool prefer_noswitch_p)
1105 addr_space_t as = ADDR_SPACE_GENERIC;
1106 int reloc;
1107 varpool_node *vnode = varpool_node::get (decl);
1108 if (vnode)
1110 vnode = vnode->ultimate_alias_target ();
1111 decl = vnode->decl;
1114 if (TREE_TYPE (decl) != error_mark_node)
1115 as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
1117 /* We need the constructor to figure out reloc flag. */
1118 if (vnode)
1119 vnode->get_constructor ();
1121 if (DECL_COMMON (decl))
1123 /* If the decl has been given an explicit section name, or it resides
1124 in a non-generic address space, then it isn't common, and shouldn't
1125 be handled as such. */
1126 gcc_assert (DECL_SECTION_NAME (decl) == NULL
1127 && ADDR_SPACE_GENERIC_P (as));
1128 if (DECL_THREAD_LOCAL_P (decl))
1129 return tls_comm_section;
1130 else if (TREE_PUBLIC (decl) && bss_initializer_p (decl))
1131 return comm_section;
1134 if (DECL_INITIAL (decl) == error_mark_node)
1135 reloc = contains_pointers_p (TREE_TYPE (decl)) ? 3 : 0;
1136 else if (DECL_INITIAL (decl))
1137 reloc = compute_reloc_for_constant (DECL_INITIAL (decl));
1138 else
1139 reloc = 0;
1141 resolve_unique_section (decl, reloc, flag_data_sections);
1142 if (IN_NAMED_SECTION (decl))
1143 return get_named_section (decl, NULL, reloc);
1145 if (ADDR_SPACE_GENERIC_P (as)
1146 && !DECL_THREAD_LOCAL_P (decl)
1147 && !(prefer_noswitch_p && targetm.have_switchable_bss_sections)
1148 && bss_initializer_p (decl))
1150 if (!TREE_PUBLIC (decl)
1151 && !((flag_sanitize & SANITIZE_ADDRESS)
1152 && asan_protect_global (decl)))
1153 return lcomm_section;
1154 if (bss_noswitch_section)
1155 return bss_noswitch_section;
1158 return targetm.asm_out.select_section (decl, reloc,
1159 get_variable_align (decl));
1162 /* Return the block into which object_block DECL should be placed. */
1164 static struct object_block *
1165 get_block_for_decl (tree decl)
1167 section *sect;
1169 if (TREE_CODE (decl) == VAR_DECL)
1171 /* The object must be defined in this translation unit. */
1172 if (DECL_EXTERNAL (decl))
1173 return NULL;
1175 /* There's no point using object blocks for something that is
1176 isolated by definition. */
1177 if (DECL_COMDAT_GROUP (decl))
1178 return NULL;
1181 /* We can only calculate block offsets if the decl has a known
1182 constant size. */
1183 if (DECL_SIZE_UNIT (decl) == NULL)
1184 return NULL;
1185 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (decl)))
1186 return NULL;
1188 /* Find out which section should contain DECL. We cannot put it into
1189 an object block if it requires a standalone definition. */
1190 if (TREE_CODE (decl) == VAR_DECL)
1191 align_variable (decl, 0);
1192 sect = get_variable_section (decl, true);
1193 if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
1194 return NULL;
1196 return get_block_for_section (sect);
1199 /* Make sure block symbol SYMBOL is in block BLOCK. */
1201 static void
1202 change_symbol_block (rtx symbol, struct object_block *block)
1204 if (block != SYMBOL_REF_BLOCK (symbol))
1206 gcc_assert (SYMBOL_REF_BLOCK_OFFSET (symbol) < 0);
1207 SYMBOL_REF_BLOCK (symbol) = block;
1211 /* Return true if it is possible to put DECL in an object_block. */
1213 static bool
1214 use_blocks_for_decl_p (tree decl)
1216 struct symtab_node *snode;
1218 /* Only data DECLs can be placed into object blocks. */
1219 if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != CONST_DECL)
1220 return false;
1222 /* Detect decls created by dw2_force_const_mem. Such decls are
1223 special because DECL_INITIAL doesn't specify the decl's true value.
1224 dw2_output_indirect_constants will instead call assemble_variable
1225 with dont_output_data set to 1 and then print the contents itself. */
1226 if (DECL_INITIAL (decl) == decl)
1227 return false;
1229 /* If this decl is an alias, then we don't want to emit a
1230 definition. */
1231 if (TREE_CODE (decl) == VAR_DECL
1232 && (snode = symtab_node::get (decl)) != NULL
1233 && snode->alias)
1234 return false;
1236 return targetm.use_blocks_for_decl_p (decl);
1239 /* Create the DECL_RTL for a VAR_DECL or FUNCTION_DECL. DECL should
1240 have static storage duration. In other words, it should not be an
1241 automatic variable, including PARM_DECLs.
1243 There is, however, one exception: this function handles variables
1244 explicitly placed in a particular register by the user.
1246 This is never called for PARM_DECL nodes. */
1248 void
1249 make_decl_rtl (tree decl)
1251 const char *name = 0;
1252 int reg_number;
1253 rtx x;
1255 /* Check that we are not being given an automatic variable. */
1256 gcc_assert (TREE_CODE (decl) != PARM_DECL
1257 && TREE_CODE (decl) != RESULT_DECL);
1259 /* A weak alias has TREE_PUBLIC set but not the other bits. */
1260 gcc_assert (TREE_CODE (decl) != VAR_DECL
1261 || TREE_STATIC (decl)
1262 || TREE_PUBLIC (decl)
1263 || DECL_EXTERNAL (decl)
1264 || DECL_REGISTER (decl));
1266 /* And that we were not given a type or a label. */
1267 gcc_assert (TREE_CODE (decl) != TYPE_DECL
1268 && TREE_CODE (decl) != LABEL_DECL);
1270 /* For a duplicate declaration, we can be called twice on the
1271 same DECL node. Don't discard the RTL already made. */
1272 if (DECL_RTL_SET_P (decl))
1274 /* If the old RTL had the wrong mode, fix the mode. */
1275 x = DECL_RTL (decl);
1276 if (GET_MODE (x) != DECL_MODE (decl))
1277 SET_DECL_RTL (decl, adjust_address_nv (x, DECL_MODE (decl), 0));
1279 if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1280 return;
1282 /* ??? Another way to do this would be to maintain a hashed
1283 table of such critters. Instead of adding stuff to a DECL
1284 to give certain attributes to it, we could use an external
1285 hash map from DECL to set of attributes. */
1287 /* Let the target reassign the RTL if it wants.
1288 This is necessary, for example, when one machine specific
1289 decl attribute overrides another. */
1290 targetm.encode_section_info (decl, DECL_RTL (decl), false);
1292 /* If the symbol has a SYMBOL_REF_BLOCK field, update it based
1293 on the new decl information. */
1294 if (MEM_P (x)
1295 && GET_CODE (XEXP (x, 0)) == SYMBOL_REF
1296 && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (x, 0)))
1297 change_symbol_block (XEXP (x, 0), get_block_for_decl (decl));
1299 return;
1302 /* If this variable belongs to the global constant pool, retrieve the
1303 pre-computed RTL or recompute it in LTO mode. */
1304 if (TREE_CODE (decl) == VAR_DECL && DECL_IN_CONSTANT_POOL (decl))
1306 SET_DECL_RTL (decl, output_constant_def (DECL_INITIAL (decl), 1));
1307 return;
1310 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
1312 if (name[0] != '*' && TREE_CODE (decl) != FUNCTION_DECL
1313 && DECL_REGISTER (decl))
1315 error ("register name not specified for %q+D", decl);
1317 else if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1319 const char *asmspec = name+1;
1320 enum machine_mode mode = DECL_MODE (decl);
1321 reg_number = decode_reg_name (asmspec);
1322 /* First detect errors in declaring global registers. */
1323 if (reg_number == -1)
1324 error ("register name not specified for %q+D", decl);
1325 else if (reg_number < 0)
1326 error ("invalid register name for %q+D", decl);
1327 else if (mode == BLKmode)
1328 error ("data type of %q+D isn%'t suitable for a register",
1329 decl);
1330 else if (!in_hard_reg_set_p (accessible_reg_set, mode, reg_number))
1331 error ("the register specified for %q+D cannot be accessed"
1332 " by the current target", decl);
1333 else if (!in_hard_reg_set_p (operand_reg_set, mode, reg_number))
1334 error ("the register specified for %q+D is not general enough"
1335 " to be used as a register variable", decl);
1336 else if (!HARD_REGNO_MODE_OK (reg_number, mode))
1337 error ("register specified for %q+D isn%'t suitable for data type",
1338 decl);
1339 /* Now handle properly declared static register variables. */
1340 else
1342 int nregs;
1344 if (DECL_INITIAL (decl) != 0 && TREE_STATIC (decl))
1346 DECL_INITIAL (decl) = 0;
1347 error ("global register variable has initial value");
1349 if (TREE_THIS_VOLATILE (decl))
1350 warning (OPT_Wvolatile_register_var,
1351 "optimization may eliminate reads and/or "
1352 "writes to register variables");
1354 /* If the user specified one of the eliminables registers here,
1355 e.g., FRAME_POINTER_REGNUM, we don't want to get this variable
1356 confused with that register and be eliminated. This usage is
1357 somewhat suspect... */
1359 SET_DECL_RTL (decl, gen_rtx_raw_REG (mode, reg_number));
1360 ORIGINAL_REGNO (DECL_RTL (decl)) = reg_number;
1361 REG_USERVAR_P (DECL_RTL (decl)) = 1;
1363 if (TREE_STATIC (decl))
1365 /* Make this register global, so not usable for anything
1366 else. */
1367 #ifdef ASM_DECLARE_REGISTER_GLOBAL
1368 name = IDENTIFIER_POINTER (DECL_NAME (decl));
1369 ASM_DECLARE_REGISTER_GLOBAL (asm_out_file, decl, reg_number, name);
1370 #endif
1371 nregs = hard_regno_nregs[reg_number][mode];
1372 while (nregs > 0)
1373 globalize_reg (decl, reg_number + --nregs);
1376 /* As a register variable, it has no section. */
1377 return;
1379 /* Avoid internal errors from invalid register
1380 specifications. */
1381 SET_DECL_ASSEMBLER_NAME (decl, NULL_TREE);
1382 DECL_HARD_REGISTER (decl) = 0;
1383 return;
1385 /* Now handle ordinary static variables and functions (in memory).
1386 Also handle vars declared register invalidly. */
1387 else if (name[0] == '*')
1389 #ifdef REGISTER_PREFIX
1390 if (strlen (REGISTER_PREFIX) != 0)
1392 reg_number = decode_reg_name (name);
1393 if (reg_number >= 0 || reg_number == -3)
1394 error ("register name given for non-register variable %q+D", decl);
1396 #endif
1399 /* Specifying a section attribute on a variable forces it into a
1400 non-.bss section, and thus it cannot be common. */
1401 /* FIXME: In general this code should not be necessary because
1402 visibility pass is doing the same work. But notice_global_symbol
1403 is called early and it needs to make DECL_RTL to get the name.
1404 we take care of recomputing the DECL_RTL after visibility is changed. */
1405 if (TREE_CODE (decl) == VAR_DECL
1406 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
1407 && DECL_SECTION_NAME (decl) != NULL
1408 && DECL_INITIAL (decl) == NULL_TREE
1409 && DECL_COMMON (decl))
1410 DECL_COMMON (decl) = 0;
1412 /* Variables can't be both common and weak. */
1413 if (TREE_CODE (decl) == VAR_DECL && DECL_WEAK (decl))
1414 DECL_COMMON (decl) = 0;
1416 if (use_object_blocks_p () && use_blocks_for_decl_p (decl))
1417 x = create_block_symbol (name, get_block_for_decl (decl), -1);
1418 else
1420 enum machine_mode address_mode = Pmode;
1421 if (TREE_TYPE (decl) != error_mark_node)
1423 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
1424 address_mode = targetm.addr_space.address_mode (as);
1426 x = gen_rtx_SYMBOL_REF (address_mode, name);
1428 SYMBOL_REF_WEAK (x) = DECL_WEAK (decl);
1429 SET_SYMBOL_REF_DECL (x, decl);
1431 x = gen_rtx_MEM (DECL_MODE (decl), x);
1432 if (TREE_CODE (decl) != FUNCTION_DECL)
1433 set_mem_attributes (x, decl, 1);
1434 SET_DECL_RTL (decl, x);
1436 /* Optionally set flags or add text to the name to record information
1437 such as that it is a function name.
1438 If the name is changed, the macro ASM_OUTPUT_LABELREF
1439 will have to know how to strip this information. */
1440 targetm.encode_section_info (decl, DECL_RTL (decl), true);
1443 /* Like make_decl_rtl, but inhibit creation of new alias sets when
1444 calling make_decl_rtl. Also, reset DECL_RTL before returning the
1445 rtl. */
1448 make_decl_rtl_for_debug (tree decl)
1450 unsigned int save_aliasing_flag;
1451 rtx rtl;
1453 if (DECL_RTL_SET_P (decl))
1454 return DECL_RTL (decl);
1456 /* Kludge alert! Somewhere down the call chain, make_decl_rtl will
1457 call new_alias_set. If running with -fcompare-debug, sometimes
1458 we do not want to create alias sets that will throw the alias
1459 numbers off in the comparison dumps. So... clearing
1460 flag_strict_aliasing will keep new_alias_set() from creating a
1461 new set. */
1462 save_aliasing_flag = flag_strict_aliasing;
1463 flag_strict_aliasing = 0;
1465 rtl = DECL_RTL (decl);
1466 /* Reset DECL_RTL back, as various parts of the compiler expects
1467 DECL_RTL set meaning it is actually going to be output. */
1468 SET_DECL_RTL (decl, NULL);
1470 flag_strict_aliasing = save_aliasing_flag;
1471 return rtl;
1474 /* Output a string of literal assembler code
1475 for an `asm' keyword used between functions. */
1477 void
1478 assemble_asm (tree string)
1480 const char *p;
1481 app_enable ();
1483 if (TREE_CODE (string) == ADDR_EXPR)
1484 string = TREE_OPERAND (string, 0);
1486 p = TREE_STRING_POINTER (string);
1487 fprintf (asm_out_file, "%s%s\n", p[0] == '\t' ? "" : "\t", p);
1490 /* Write the address of the entity given by SYMBOL to SEC. */
1491 void
1492 assemble_addr_to_section (rtx symbol, section *sec)
1494 switch_to_section (sec);
1495 assemble_align (POINTER_SIZE);
1496 assemble_integer (symbol, POINTER_SIZE_UNITS, POINTER_SIZE, 1);
1499 /* Return the numbered .ctors.N (if CONSTRUCTOR_P) or .dtors.N (if
1500 not) section for PRIORITY. */
1501 section *
1502 get_cdtor_priority_section (int priority, bool constructor_p)
1504 char buf[16];
1506 /* ??? This only works reliably with the GNU linker. */
1507 sprintf (buf, "%s.%.5u",
1508 constructor_p ? ".ctors" : ".dtors",
1509 /* Invert the numbering so the linker puts us in the proper
1510 order; constructors are run from right to left, and the
1511 linker sorts in increasing order. */
1512 MAX_INIT_PRIORITY - priority);
1513 return get_section (buf, SECTION_WRITE, NULL);
1516 void
1517 default_named_section_asm_out_destructor (rtx symbol, int priority)
1519 section *sec;
1521 if (priority != DEFAULT_INIT_PRIORITY)
1522 sec = get_cdtor_priority_section (priority,
1523 /*constructor_p=*/false);
1524 else
1525 sec = get_section (".dtors", SECTION_WRITE, NULL);
1527 assemble_addr_to_section (symbol, sec);
1530 #ifdef DTORS_SECTION_ASM_OP
1531 void
1532 default_dtor_section_asm_out_destructor (rtx symbol,
1533 int priority ATTRIBUTE_UNUSED)
1535 assemble_addr_to_section (symbol, dtors_section);
1537 #endif
1539 void
1540 default_named_section_asm_out_constructor (rtx symbol, int priority)
1542 section *sec;
1544 if (priority != DEFAULT_INIT_PRIORITY)
1545 sec = get_cdtor_priority_section (priority,
1546 /*constructor_p=*/true);
1547 else
1548 sec = get_section (".ctors", SECTION_WRITE, NULL);
1550 assemble_addr_to_section (symbol, sec);
1553 #ifdef CTORS_SECTION_ASM_OP
1554 void
1555 default_ctor_section_asm_out_constructor (rtx symbol,
1556 int priority ATTRIBUTE_UNUSED)
1558 assemble_addr_to_section (symbol, ctors_section);
1560 #endif
1562 /* CONSTANT_POOL_BEFORE_FUNCTION may be defined as an expression with
1563 a nonzero value if the constant pool should be output before the
1564 start of the function, or a zero value if the pool should output
1565 after the end of the function. The default is to put it before the
1566 start. */
1568 #ifndef CONSTANT_POOL_BEFORE_FUNCTION
1569 #define CONSTANT_POOL_BEFORE_FUNCTION 1
1570 #endif
1572 /* DECL is an object (either VAR_DECL or FUNCTION_DECL) which is going
1573 to be output to assembler.
1574 Set first_global_object_name and weak_global_object_name as appropriate. */
1576 void
1577 notice_global_symbol (tree decl)
1579 const char **type = &first_global_object_name;
1581 if (first_global_object_name
1582 || !TREE_PUBLIC (decl)
1583 || DECL_EXTERNAL (decl)
1584 || !DECL_NAME (decl)
1585 || (TREE_CODE (decl) != FUNCTION_DECL
1586 && (TREE_CODE (decl) != VAR_DECL
1587 || (DECL_COMMON (decl)
1588 && (DECL_INITIAL (decl) == 0
1589 || DECL_INITIAL (decl) == error_mark_node))))
1590 || !MEM_P (DECL_RTL (decl)))
1591 return;
1593 /* We win when global object is found, but it is useful to know about weak
1594 symbol as well so we can produce nicer unique names. */
1595 if (DECL_WEAK (decl) || DECL_ONE_ONLY (decl) || flag_shlib)
1596 type = &weak_global_object_name;
1598 if (!*type)
1600 const char *p;
1601 const char *name;
1602 rtx decl_rtl = DECL_RTL (decl);
1604 p = targetm.strip_name_encoding (XSTR (XEXP (decl_rtl, 0), 0));
1605 name = ggc_strdup (p);
1607 *type = name;
1611 /* If not using flag_reorder_blocks_and_partition, decide early whether the
1612 current function goes into the cold section, so that targets can use
1613 current_function_section during RTL expansion. DECL describes the
1614 function. */
1616 void
1617 decide_function_section (tree decl)
1619 first_function_block_is_cold = false;
1621 if (flag_reorder_blocks_and_partition)
1622 /* We will decide in assemble_start_function. */
1623 return;
1625 if (DECL_SECTION_NAME (decl))
1627 struct cgraph_node *node = cgraph_node::get (current_function_decl);
1628 /* Calls to function_section rely on first_function_block_is_cold
1629 being accurate. */
1630 first_function_block_is_cold = (node
1631 && node->frequency
1632 == NODE_FREQUENCY_UNLIKELY_EXECUTED);
1635 in_cold_section_p = first_function_block_is_cold;
1638 /* Output assembler code for the constant pool of a function and associated
1639 with defining the name of the function. DECL describes the function.
1640 NAME is the function's name. For the constant pool, we use the current
1641 constant pool data. */
1643 void
1644 assemble_start_function (tree decl, const char *fnname)
1646 int align;
1647 char tmp_label[100];
1648 bool hot_label_written = false;
1650 if (flag_reorder_blocks_and_partition)
1652 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTB", const_labelno);
1653 crtl->subsections.hot_section_label = ggc_strdup (tmp_label);
1654 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDB", const_labelno);
1655 crtl->subsections.cold_section_label = ggc_strdup (tmp_label);
1656 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTE", const_labelno);
1657 crtl->subsections.hot_section_end_label = ggc_strdup (tmp_label);
1658 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDE", const_labelno);
1659 crtl->subsections.cold_section_end_label = ggc_strdup (tmp_label);
1660 const_labelno++;
1662 else
1664 crtl->subsections.hot_section_label = NULL;
1665 crtl->subsections.cold_section_label = NULL;
1666 crtl->subsections.hot_section_end_label = NULL;
1667 crtl->subsections.cold_section_end_label = NULL;
1670 /* The following code does not need preprocessing in the assembler. */
1672 app_disable ();
1674 if (CONSTANT_POOL_BEFORE_FUNCTION)
1675 output_constant_pool (fnname, decl);
1677 /* Make sure the not and cold text (code) sections are properly
1678 aligned. This is necessary here in the case where the function
1679 has both hot and cold sections, because we don't want to re-set
1680 the alignment when the section switch happens mid-function. */
1682 if (flag_reorder_blocks_and_partition)
1684 first_function_block_is_cold = false;
1686 switch_to_section (unlikely_text_section ());
1687 assemble_align (DECL_ALIGN (decl));
1688 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_label);
1690 /* When the function starts with a cold section, we need to explicitly
1691 align the hot section and write out the hot section label.
1692 But if the current function is a thunk, we do not have a CFG. */
1693 if (!cfun->is_thunk
1694 && BB_PARTITION (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb) == BB_COLD_PARTITION)
1696 switch_to_section (text_section);
1697 assemble_align (DECL_ALIGN (decl));
1698 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
1699 hot_label_written = true;
1700 first_function_block_is_cold = true;
1702 in_cold_section_p = first_function_block_is_cold;
1706 /* Switch to the correct text section for the start of the function. */
1708 switch_to_section (function_section (decl));
1709 if (flag_reorder_blocks_and_partition
1710 && !hot_label_written)
1711 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
1713 /* Tell assembler to move to target machine's alignment for functions. */
1714 align = floor_log2 (DECL_ALIGN (decl) / BITS_PER_UNIT);
1715 if (align > 0)
1717 ASM_OUTPUT_ALIGN (asm_out_file, align);
1720 /* Handle a user-specified function alignment.
1721 Note that we still need to align to DECL_ALIGN, as above,
1722 because ASM_OUTPUT_MAX_SKIP_ALIGN might not do any alignment at all. */
1723 if (! DECL_USER_ALIGN (decl)
1724 && align_functions_log > align
1725 && optimize_function_for_speed_p (cfun))
1727 #ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
1728 ASM_OUTPUT_MAX_SKIP_ALIGN (asm_out_file,
1729 align_functions_log, align_functions - 1);
1730 #else
1731 ASM_OUTPUT_ALIGN (asm_out_file, align_functions_log);
1732 #endif
1735 #ifdef ASM_OUTPUT_FUNCTION_PREFIX
1736 ASM_OUTPUT_FUNCTION_PREFIX (asm_out_file, fnname);
1737 #endif
1739 if (!DECL_IGNORED_P (decl))
1740 (*debug_hooks->begin_function) (decl);
1742 /* Make function name accessible from other files, if appropriate. */
1744 if (TREE_PUBLIC (decl))
1746 notice_global_symbol (decl);
1748 globalize_decl (decl);
1750 maybe_assemble_visibility (decl);
1753 if (DECL_PRESERVE_P (decl))
1754 targetm.asm_out.mark_decl_preserved (fnname);
1756 /* Do any machine/system dependent processing of the function name. */
1757 #ifdef ASM_DECLARE_FUNCTION_NAME
1758 ASM_DECLARE_FUNCTION_NAME (asm_out_file, fnname, current_function_decl);
1759 #else
1760 /* Standard thing is just output label for the function. */
1761 ASM_OUTPUT_FUNCTION_LABEL (asm_out_file, fnname, current_function_decl);
1762 #endif /* ASM_DECLARE_FUNCTION_NAME */
1764 if (lookup_attribute ("no_split_stack", DECL_ATTRIBUTES (decl)))
1765 saw_no_split_stack = true;
1768 /* Output assembler code associated with defining the size of the
1769 function. DECL describes the function. NAME is the function's name. */
1771 void
1772 assemble_end_function (tree decl, const char *fnname ATTRIBUTE_UNUSED)
1774 #ifdef ASM_DECLARE_FUNCTION_SIZE
1775 /* We could have switched section in the middle of the function. */
1776 if (flag_reorder_blocks_and_partition)
1777 switch_to_section (function_section (decl));
1778 ASM_DECLARE_FUNCTION_SIZE (asm_out_file, fnname, decl);
1779 #endif
1780 if (! CONSTANT_POOL_BEFORE_FUNCTION)
1782 output_constant_pool (fnname, decl);
1783 switch_to_section (function_section (decl)); /* need to switch back */
1785 /* Output labels for end of hot/cold text sections (to be used by
1786 debug info.) */
1787 if (flag_reorder_blocks_and_partition)
1789 section *save_text_section;
1791 save_text_section = in_section;
1792 switch_to_section (unlikely_text_section ());
1793 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_end_label);
1794 if (first_function_block_is_cold)
1795 switch_to_section (text_section);
1796 else
1797 switch_to_section (function_section (decl));
1798 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_end_label);
1799 switch_to_section (save_text_section);
1803 /* Assemble code to leave SIZE bytes of zeros. */
1805 void
1806 assemble_zeros (unsigned HOST_WIDE_INT size)
1808 /* Do no output if -fsyntax-only. */
1809 if (flag_syntax_only)
1810 return;
1812 #ifdef ASM_NO_SKIP_IN_TEXT
1813 /* The `space' pseudo in the text section outputs nop insns rather than 0s,
1814 so we must output 0s explicitly in the text section. */
1815 if (ASM_NO_SKIP_IN_TEXT && (in_section->common.flags & SECTION_CODE) != 0)
1817 unsigned HOST_WIDE_INT i;
1818 for (i = 0; i < size; i++)
1819 assemble_integer (const0_rtx, 1, BITS_PER_UNIT, 1);
1821 else
1822 #endif
1823 if (size > 0)
1824 ASM_OUTPUT_SKIP (asm_out_file, size);
1827 /* Assemble an alignment pseudo op for an ALIGN-bit boundary. */
1829 void
1830 assemble_align (int align)
1832 if (align > BITS_PER_UNIT)
1834 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
1838 /* Assemble a string constant with the specified C string as contents. */
1840 void
1841 assemble_string (const char *p, int size)
1843 int pos = 0;
1844 int maximum = 2000;
1846 /* If the string is very long, split it up. */
1848 while (pos < size)
1850 int thissize = size - pos;
1851 if (thissize > maximum)
1852 thissize = maximum;
1854 ASM_OUTPUT_ASCII (asm_out_file, p, thissize);
1856 pos += thissize;
1857 p += thissize;
1862 /* A noswitch_section_callback for lcomm_section. */
1864 static bool
1865 emit_local (tree decl ATTRIBUTE_UNUSED,
1866 const char *name ATTRIBUTE_UNUSED,
1867 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1868 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1870 #if defined ASM_OUTPUT_ALIGNED_DECL_LOCAL
1871 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, decl, name,
1872 size, DECL_ALIGN (decl));
1873 return true;
1874 #elif defined ASM_OUTPUT_ALIGNED_LOCAL
1875 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, DECL_ALIGN (decl));
1876 return true;
1877 #else
1878 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
1879 return false;
1880 #endif
1883 /* A noswitch_section_callback for bss_noswitch_section. */
1885 #if defined ASM_OUTPUT_ALIGNED_BSS
1886 static bool
1887 emit_bss (tree decl ATTRIBUTE_UNUSED,
1888 const char *name ATTRIBUTE_UNUSED,
1889 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1890 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1892 #if defined ASM_OUTPUT_ALIGNED_BSS
1893 ASM_OUTPUT_ALIGNED_BSS (asm_out_file, decl, name, size,
1894 get_variable_align (decl));
1895 return true;
1896 #endif
1898 #endif
1900 /* A noswitch_section_callback for comm_section. */
1902 static bool
1903 emit_common (tree decl ATTRIBUTE_UNUSED,
1904 const char *name ATTRIBUTE_UNUSED,
1905 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1906 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1908 #if defined ASM_OUTPUT_ALIGNED_DECL_COMMON
1909 ASM_OUTPUT_ALIGNED_DECL_COMMON (asm_out_file, decl, name,
1910 size, get_variable_align (decl));
1911 return true;
1912 #elif defined ASM_OUTPUT_ALIGNED_COMMON
1913 ASM_OUTPUT_ALIGNED_COMMON (asm_out_file, name, size,
1914 get_variable_align (decl));
1915 return true;
1916 #else
1917 ASM_OUTPUT_COMMON (asm_out_file, name, size, rounded);
1918 return false;
1919 #endif
1922 /* A noswitch_section_callback for tls_comm_section. */
1924 static bool
1925 emit_tls_common (tree decl ATTRIBUTE_UNUSED,
1926 const char *name ATTRIBUTE_UNUSED,
1927 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1928 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1930 #ifdef ASM_OUTPUT_TLS_COMMON
1931 ASM_OUTPUT_TLS_COMMON (asm_out_file, decl, name, size);
1932 return true;
1933 #else
1934 sorry ("thread-local COMMON data not implemented");
1935 return true;
1936 #endif
1939 /* Assemble DECL given that it belongs in SECTION_NOSWITCH section SECT.
1940 NAME is the name of DECL's SYMBOL_REF. */
1942 static void
1943 assemble_noswitch_variable (tree decl, const char *name, section *sect,
1944 unsigned int align)
1946 unsigned HOST_WIDE_INT size, rounded;
1948 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
1949 rounded = size;
1951 if ((flag_sanitize & SANITIZE_ADDRESS) && asan_protect_global (decl))
1952 size += asan_red_zone_size (size);
1954 /* Don't allocate zero bytes of common,
1955 since that means "undefined external" in the linker. */
1956 if (size == 0)
1957 rounded = 1;
1959 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
1960 so that each uninitialized object starts on such a boundary. */
1961 rounded += (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1;
1962 rounded = (rounded / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
1963 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
1965 if (!sect->noswitch.callback (decl, name, size, rounded)
1966 && (unsigned HOST_WIDE_INT) (align / BITS_PER_UNIT) > rounded)
1967 error ("requested alignment for %q+D is greater than "
1968 "implemented alignment of %wu", decl, rounded);
1971 /* A subroutine of assemble_variable. Output the label and contents of
1972 DECL, whose address is a SYMBOL_REF with name NAME. DONT_OUTPUT_DATA
1973 is as for assemble_variable. */
1975 static void
1976 assemble_variable_contents (tree decl, const char *name,
1977 bool dont_output_data)
1979 /* Do any machine/system dependent processing of the object. */
1980 #ifdef ASM_DECLARE_OBJECT_NAME
1981 last_assemble_variable_decl = decl;
1982 ASM_DECLARE_OBJECT_NAME (asm_out_file, name, decl);
1983 #else
1984 /* Standard thing is just output label for the object. */
1985 ASM_OUTPUT_LABEL (asm_out_file, name);
1986 #endif /* ASM_DECLARE_OBJECT_NAME */
1988 if (!dont_output_data)
1990 /* Caller is supposed to use varpool_get_constructor when it wants
1991 to output the body. */
1992 gcc_assert (!in_lto_p || DECL_INITIAL (decl) != error_mark_node);
1993 if (DECL_INITIAL (decl)
1994 && DECL_INITIAL (decl) != error_mark_node
1995 && !initializer_zerop (DECL_INITIAL (decl)))
1996 /* Output the actual data. */
1997 output_constant (DECL_INITIAL (decl),
1998 tree_to_uhwi (DECL_SIZE_UNIT (decl)),
1999 get_variable_align (decl));
2000 else
2001 /* Leave space for it. */
2002 assemble_zeros (tree_to_uhwi (DECL_SIZE_UNIT (decl)));
2006 /* Assemble everything that is needed for a variable or function declaration.
2007 Not used for automatic variables, and not used for function definitions.
2008 Should not be called for variables of incomplete structure type.
2010 TOP_LEVEL is nonzero if this variable has file scope.
2011 AT_END is nonzero if this is the special handling, at end of compilation,
2012 to define things that have had only tentative definitions.
2013 DONT_OUTPUT_DATA if nonzero means don't actually output the
2014 initial value (that will be done by the caller). */
2016 void
2017 assemble_variable (tree decl, int top_level ATTRIBUTE_UNUSED,
2018 int at_end ATTRIBUTE_UNUSED, int dont_output_data)
2020 const char *name;
2021 rtx decl_rtl, symbol;
2022 section *sect;
2023 unsigned int align;
2024 bool asan_protected = false;
2026 /* This function is supposed to handle VARIABLES. Ensure we have one. */
2027 gcc_assert (TREE_CODE (decl) == VAR_DECL);
2029 /* Emulated TLS had better not get this far. */
2030 gcc_checking_assert (targetm.have_tls || !DECL_THREAD_LOCAL_P (decl));
2032 last_assemble_variable_decl = 0;
2034 /* Normally no need to say anything here for external references,
2035 since assemble_external is called by the language-specific code
2036 when a declaration is first seen. */
2038 if (DECL_EXTERNAL (decl))
2039 return;
2041 /* Do nothing for global register variables. */
2042 if (DECL_RTL_SET_P (decl) && REG_P (DECL_RTL (decl)))
2044 TREE_ASM_WRITTEN (decl) = 1;
2045 return;
2048 /* If type was incomplete when the variable was declared,
2049 see if it is complete now. */
2051 if (DECL_SIZE (decl) == 0)
2052 layout_decl (decl, 0);
2054 /* Still incomplete => don't allocate it; treat the tentative defn
2055 (which is what it must have been) as an `extern' reference. */
2057 if (!dont_output_data && DECL_SIZE (decl) == 0)
2059 error ("storage size of %q+D isn%'t known", decl);
2060 TREE_ASM_WRITTEN (decl) = 1;
2061 return;
2064 /* The first declaration of a variable that comes through this function
2065 decides whether it is global (in C, has external linkage)
2066 or local (in C, has internal linkage). So do nothing more
2067 if this function has already run. */
2069 if (TREE_ASM_WRITTEN (decl))
2070 return;
2072 /* Make sure targetm.encode_section_info is invoked before we set
2073 ASM_WRITTEN. */
2074 decl_rtl = DECL_RTL (decl);
2076 TREE_ASM_WRITTEN (decl) = 1;
2078 /* Do no output if -fsyntax-only. */
2079 if (flag_syntax_only)
2080 return;
2082 if (! dont_output_data
2083 && ! valid_constant_size_p (DECL_SIZE_UNIT (decl)))
2085 error ("size of variable %q+D is too large", decl);
2086 return;
2089 gcc_assert (MEM_P (decl_rtl));
2090 gcc_assert (GET_CODE (XEXP (decl_rtl, 0)) == SYMBOL_REF);
2091 symbol = XEXP (decl_rtl, 0);
2093 /* If this symbol belongs to the tree constant pool, output the constant
2094 if it hasn't already been written. */
2095 if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
2097 tree decl = SYMBOL_REF_DECL (symbol);
2098 if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
2099 output_constant_def_contents (symbol);
2100 return;
2103 app_disable ();
2105 name = XSTR (symbol, 0);
2106 if (TREE_PUBLIC (decl) && DECL_NAME (decl))
2107 notice_global_symbol (decl);
2109 /* Compute the alignment of this data. */
2111 align_variable (decl, dont_output_data);
2113 if ((flag_sanitize & SANITIZE_ADDRESS)
2114 && asan_protect_global (decl))
2116 asan_protected = true;
2117 DECL_ALIGN (decl) = MAX (DECL_ALIGN (decl),
2118 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
2121 set_mem_align (decl_rtl, DECL_ALIGN (decl));
2123 align = get_variable_align (decl);
2125 if (TREE_PUBLIC (decl))
2126 maybe_assemble_visibility (decl);
2128 if (DECL_PRESERVE_P (decl))
2129 targetm.asm_out.mark_decl_preserved (name);
2131 /* First make the assembler name(s) global if appropriate. */
2132 sect = get_variable_section (decl, false);
2133 if (TREE_PUBLIC (decl)
2134 && (sect->common.flags & SECTION_COMMON) == 0)
2135 globalize_decl (decl);
2137 /* Output any data that we will need to use the address of. */
2138 if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node)
2139 output_addressed_constants (DECL_INITIAL (decl));
2141 /* dbxout.c needs to know this. */
2142 if (sect && (sect->common.flags & SECTION_CODE) != 0)
2143 DECL_IN_TEXT_SECTION (decl) = 1;
2145 /* If the decl is part of an object_block, make sure that the decl
2146 has been positioned within its block, but do not write out its
2147 definition yet. output_object_blocks will do that later. */
2148 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
2150 gcc_assert (!dont_output_data);
2151 place_block_symbol (symbol);
2153 else if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
2154 assemble_noswitch_variable (decl, name, sect, align);
2155 else
2157 /* The following bit of code ensures that vtable_map
2158 variables are not only in the comdat section, but that
2159 each variable has its own unique comdat name. If this
2160 code is removed, the variables end up in the same section
2161 with a single comdat name.
2163 FIXME: resolve_unique_section needs to deal better with
2164 decls with both DECL_SECTION_NAME and DECL_ONE_ONLY. Once
2165 that is fixed, this if-else statement can be replaced with
2166 a single call to "switch_to_section (sect)". */
2167 if (sect->named.name
2168 && (strcmp (sect->named.name, ".vtable_map_vars") == 0))
2170 #if defined (OBJECT_FORMAT_ELF)
2171 targetm.asm_out.named_section (sect->named.name,
2172 sect->named.common.flags
2173 | SECTION_LINKONCE,
2174 DECL_NAME (decl));
2175 in_section = sect;
2176 #else
2177 switch_to_section (sect);
2178 #endif
2180 else
2181 switch_to_section (sect);
2182 if (align > BITS_PER_UNIT)
2183 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
2184 assemble_variable_contents (decl, name, dont_output_data);
2185 if (asan_protected)
2187 unsigned HOST_WIDE_INT int size
2188 = tree_to_uhwi (DECL_SIZE_UNIT (decl));
2189 assemble_zeros (asan_red_zone_size (size));
2195 /* Given a function declaration (FN_DECL), this function assembles the
2196 function into the .preinit_array section. */
2198 void
2199 assemble_vtv_preinit_initializer (tree fn_decl)
2201 section *sect;
2202 unsigned flags = SECTION_WRITE;
2203 rtx symbol = XEXP (DECL_RTL (fn_decl), 0);
2205 flags |= SECTION_NOTYPE;
2206 sect = get_section (".preinit_array", flags, fn_decl);
2207 switch_to_section (sect);
2208 assemble_addr_to_section (symbol, sect);
2211 /* Return 1 if type TYPE contains any pointers. */
2213 static int
2214 contains_pointers_p (tree type)
2216 switch (TREE_CODE (type))
2218 case POINTER_TYPE:
2219 case REFERENCE_TYPE:
2220 /* I'm not sure whether OFFSET_TYPE needs this treatment,
2221 so I'll play safe and return 1. */
2222 case OFFSET_TYPE:
2223 return 1;
2225 case RECORD_TYPE:
2226 case UNION_TYPE:
2227 case QUAL_UNION_TYPE:
2229 tree fields;
2230 /* For a type that has fields, see if the fields have pointers. */
2231 for (fields = TYPE_FIELDS (type); fields; fields = DECL_CHAIN (fields))
2232 if (TREE_CODE (fields) == FIELD_DECL
2233 && contains_pointers_p (TREE_TYPE (fields)))
2234 return 1;
2235 return 0;
2238 case ARRAY_TYPE:
2239 /* An array type contains pointers if its element type does. */
2240 return contains_pointers_p (TREE_TYPE (type));
2242 default:
2243 return 0;
2247 /* We delay assemble_external processing until
2248 the compilation unit is finalized. This is the best we can do for
2249 right now (i.e. stage 3 of GCC 4.0) - the right thing is to delay
2250 it all the way to final. See PR 17982 for further discussion. */
2251 static GTY(()) tree pending_assemble_externals;
2253 #ifdef ASM_OUTPUT_EXTERNAL
2254 /* Some targets delay some output to final using TARGET_ASM_FILE_END.
2255 As a result, assemble_external can be called after the list of externals
2256 is processed and the pointer set destroyed. */
2257 static bool pending_assemble_externals_processed;
2259 /* Avoid O(external_decls**2) lookups in the pending_assemble_externals
2260 TREE_LIST in assemble_external. */
2261 static hash_set<tree> *pending_assemble_externals_set;
2263 /* True if DECL is a function decl for which no out-of-line copy exists.
2264 It is assumed that DECL's assembler name has been set. */
2266 static bool
2267 incorporeal_function_p (tree decl)
2269 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
2271 const char *name;
2273 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
2274 && (DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA
2275 || DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA_WITH_ALIGN))
2276 return true;
2278 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2279 /* Atomic or sync builtins which have survived this far will be
2280 resolved externally and therefore are not incorporeal. */
2281 if (strncmp (name, "__builtin_", 10) == 0)
2282 return true;
2284 return false;
2287 /* Actually do the tests to determine if this is necessary, and invoke
2288 ASM_OUTPUT_EXTERNAL. */
2289 static void
2290 assemble_external_real (tree decl)
2292 rtx rtl = DECL_RTL (decl);
2294 if (MEM_P (rtl) && GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF
2295 && !SYMBOL_REF_USED (XEXP (rtl, 0))
2296 && !incorporeal_function_p (decl))
2298 /* Some systems do require some output. */
2299 SYMBOL_REF_USED (XEXP (rtl, 0)) = 1;
2300 ASM_OUTPUT_EXTERNAL (asm_out_file, decl, XSTR (XEXP (rtl, 0), 0));
2303 #endif
2305 void
2306 process_pending_assemble_externals (void)
2308 #ifdef ASM_OUTPUT_EXTERNAL
2309 tree list;
2310 for (list = pending_assemble_externals; list; list = TREE_CHAIN (list))
2311 assemble_external_real (TREE_VALUE (list));
2313 pending_assemble_externals = 0;
2314 pending_assemble_externals_processed = true;
2315 delete pending_assemble_externals_set;
2316 #endif
2319 /* This TREE_LIST contains any weak symbol declarations waiting
2320 to be emitted. */
2321 static GTY(()) tree weak_decls;
2323 /* Output something to declare an external symbol to the assembler,
2324 and qualifiers such as weakness. (Most assemblers don't need
2325 extern declaration, so we normally output nothing.) Do nothing if
2326 DECL is not external. */
2328 void
2329 assemble_external (tree decl ATTRIBUTE_UNUSED)
2331 /* Make sure that the ASM_OUT_FILE is open.
2332 If it's not, we should not be calling this function. */
2333 gcc_assert (asm_out_file);
2335 /* In a perfect world, the following condition would be true.
2336 Sadly, the Java and Go front ends emit assembly *from the front end*,
2337 bypassing the call graph. See PR52739. Fix before GCC 4.8. */
2338 #if 0
2339 /* This function should only be called if we are expanding, or have
2340 expanded, to RTL.
2341 Ideally, only final.c would be calling this function, but it is
2342 not clear whether that would break things somehow. See PR 17982
2343 for further discussion. */
2344 gcc_assert (state == EXPANSION
2345 || state == FINISHED);
2346 #endif
2348 if (!DECL_P (decl) || !DECL_EXTERNAL (decl) || !TREE_PUBLIC (decl))
2349 return;
2351 /* We want to output annotation for weak and external symbols at
2352 very last to check if they are references or not. */
2354 if (TARGET_SUPPORTS_WEAK
2355 && DECL_WEAK (decl)
2356 /* TREE_STATIC is a weird and abused creature which is not
2357 generally the right test for whether an entity has been
2358 locally emitted, inlined or otherwise not-really-extern, but
2359 for declarations that can be weak, it happens to be
2360 match. */
2361 && !TREE_STATIC (decl)
2362 && lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
2363 && value_member (decl, weak_decls) == NULL_TREE)
2364 weak_decls = tree_cons (NULL, decl, weak_decls);
2366 #ifdef ASM_OUTPUT_EXTERNAL
2367 if (pending_assemble_externals_processed)
2369 assemble_external_real (decl);
2370 return;
2373 if (! pending_assemble_externals_set->add (decl))
2374 pending_assemble_externals = tree_cons (NULL, decl,
2375 pending_assemble_externals);
2376 #endif
2379 /* Similar, for calling a library function FUN. */
2381 void
2382 assemble_external_libcall (rtx fun)
2384 /* Declare library function name external when first used, if nec. */
2385 if (! SYMBOL_REF_USED (fun))
2387 SYMBOL_REF_USED (fun) = 1;
2388 targetm.asm_out.external_libcall (fun);
2392 /* Assemble a label named NAME. */
2394 void
2395 assemble_label (FILE *file, const char *name)
2397 ASM_OUTPUT_LABEL (file, name);
2400 /* Set the symbol_referenced flag for ID. */
2401 void
2402 mark_referenced (tree id)
2404 TREE_SYMBOL_REFERENCED (id) = 1;
2407 /* Set the symbol_referenced flag for DECL and notify callgraph. */
2408 void
2409 mark_decl_referenced (tree decl)
2411 if (TREE_CODE (decl) == FUNCTION_DECL)
2413 /* Extern inline functions don't become needed when referenced.
2414 If we know a method will be emitted in other TU and no new
2415 functions can be marked reachable, just use the external
2416 definition. */
2417 struct cgraph_node *node = cgraph_node::get_create (decl);
2418 if (!DECL_EXTERNAL (decl)
2419 && !node->definition)
2420 node->mark_force_output ();
2422 else if (TREE_CODE (decl) == VAR_DECL)
2424 varpool_node *node = varpool_node::get_create (decl);
2425 /* C++ frontend use mark_decl_references to force COMDAT variables
2426 to be output that might appear dead otherwise. */
2427 node->force_output = true;
2429 /* else do nothing - we can get various sorts of CST nodes here,
2430 which do not need to be marked. */
2434 /* Follow the IDENTIFIER_TRANSPARENT_ALIAS chain starting at *ALIAS
2435 until we find an identifier that is not itself a transparent alias.
2436 Modify the alias passed to it by reference (and all aliases on the
2437 way to the ultimate target), such that they do not have to be
2438 followed again, and return the ultimate target of the alias
2439 chain. */
2441 static inline tree
2442 ultimate_transparent_alias_target (tree *alias)
2444 tree target = *alias;
2446 if (IDENTIFIER_TRANSPARENT_ALIAS (target))
2448 gcc_assert (TREE_CHAIN (target));
2449 target = ultimate_transparent_alias_target (&TREE_CHAIN (target));
2450 gcc_assert (! IDENTIFIER_TRANSPARENT_ALIAS (target)
2451 && ! TREE_CHAIN (target));
2452 *alias = target;
2455 return target;
2458 /* Output to FILE (an assembly file) a reference to NAME. If NAME
2459 starts with a *, the rest of NAME is output verbatim. Otherwise
2460 NAME is transformed in a target-specific way (usually by the
2461 addition of an underscore). */
2463 void
2464 assemble_name_raw (FILE *file, const char *name)
2466 if (name[0] == '*')
2467 fputs (&name[1], file);
2468 else
2469 ASM_OUTPUT_LABELREF (file, name);
2472 /* Like assemble_name_raw, but should be used when NAME might refer to
2473 an entity that is also represented as a tree (like a function or
2474 variable). If NAME does refer to such an entity, that entity will
2475 be marked as referenced. */
2477 void
2478 assemble_name (FILE *file, const char *name)
2480 const char *real_name;
2481 tree id;
2483 real_name = targetm.strip_name_encoding (name);
2485 id = maybe_get_identifier (real_name);
2486 if (id)
2488 tree id_orig = id;
2490 mark_referenced (id);
2491 ultimate_transparent_alias_target (&id);
2492 if (id != id_orig)
2493 name = IDENTIFIER_POINTER (id);
2494 gcc_assert (! TREE_CHAIN (id));
2497 assemble_name_raw (file, name);
2500 /* Allocate SIZE bytes writable static space with a gensym name
2501 and return an RTX to refer to its address. */
2504 assemble_static_space (unsigned HOST_WIDE_INT size)
2506 char name[12];
2507 const char *namestring;
2508 rtx x;
2510 ASM_GENERATE_INTERNAL_LABEL (name, "LF", const_labelno);
2511 ++const_labelno;
2512 namestring = ggc_strdup (name);
2514 x = gen_rtx_SYMBOL_REF (Pmode, namestring);
2515 SYMBOL_REF_FLAGS (x) = SYMBOL_FLAG_LOCAL;
2517 #ifdef ASM_OUTPUT_ALIGNED_DECL_LOCAL
2518 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, NULL_TREE, name, size,
2519 BIGGEST_ALIGNMENT);
2520 #else
2521 #ifdef ASM_OUTPUT_ALIGNED_LOCAL
2522 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, BIGGEST_ALIGNMENT);
2523 #else
2525 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
2526 so that each uninitialized object starts on such a boundary. */
2527 /* Variable `rounded' might or might not be used in ASM_OUTPUT_LOCAL. */
2528 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED
2529 = ((size + (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1)
2530 / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
2531 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
2532 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
2534 #endif
2535 #endif
2536 return x;
2539 /* Assemble the static constant template for function entry trampolines.
2540 This is done at most once per compilation.
2541 Returns an RTX for the address of the template. */
2543 static GTY(()) rtx initial_trampoline;
2546 assemble_trampoline_template (void)
2548 char label[256];
2549 const char *name;
2550 int align;
2551 rtx symbol;
2553 gcc_assert (targetm.asm_out.trampoline_template != NULL);
2555 if (initial_trampoline)
2556 return initial_trampoline;
2558 /* By default, put trampoline templates in read-only data section. */
2560 #ifdef TRAMPOLINE_SECTION
2561 switch_to_section (TRAMPOLINE_SECTION);
2562 #else
2563 switch_to_section (readonly_data_section);
2564 #endif
2566 /* Write the assembler code to define one. */
2567 align = floor_log2 (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
2568 if (align > 0)
2569 ASM_OUTPUT_ALIGN (asm_out_file, align);
2571 targetm.asm_out.internal_label (asm_out_file, "LTRAMP", 0);
2572 targetm.asm_out.trampoline_template (asm_out_file);
2574 /* Record the rtl to refer to it. */
2575 ASM_GENERATE_INTERNAL_LABEL (label, "LTRAMP", 0);
2576 name = ggc_strdup (label);
2577 symbol = gen_rtx_SYMBOL_REF (Pmode, name);
2578 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_LOCAL;
2580 initial_trampoline = gen_const_mem (BLKmode, symbol);
2581 set_mem_align (initial_trampoline, TRAMPOLINE_ALIGNMENT);
2582 set_mem_size (initial_trampoline, TRAMPOLINE_SIZE);
2584 return initial_trampoline;
2587 /* A and B are either alignments or offsets. Return the minimum alignment
2588 that may be assumed after adding the two together. */
2590 static inline unsigned
2591 min_align (unsigned int a, unsigned int b)
2593 return (a | b) & -(a | b);
2596 /* Return the assembler directive for creating a given kind of integer
2597 object. SIZE is the number of bytes in the object and ALIGNED_P
2598 indicates whether it is known to be aligned. Return NULL if the
2599 assembly dialect has no such directive.
2601 The returned string should be printed at the start of a new line and
2602 be followed immediately by the object's initial value. */
2604 const char *
2605 integer_asm_op (int size, int aligned_p)
2607 struct asm_int_op *ops;
2609 if (aligned_p)
2610 ops = &targetm.asm_out.aligned_op;
2611 else
2612 ops = &targetm.asm_out.unaligned_op;
2614 switch (size)
2616 case 1:
2617 return targetm.asm_out.byte_op;
2618 case 2:
2619 return ops->hi;
2620 case 4:
2621 return ops->si;
2622 case 8:
2623 return ops->di;
2624 case 16:
2625 return ops->ti;
2626 default:
2627 return NULL;
2631 /* Use directive OP to assemble an integer object X. Print OP at the
2632 start of the line, followed immediately by the value of X. */
2634 void
2635 assemble_integer_with_op (const char *op, rtx x)
2637 fputs (op, asm_out_file);
2638 output_addr_const (asm_out_file, x);
2639 fputc ('\n', asm_out_file);
2642 /* The default implementation of the asm_out.integer target hook. */
2644 bool
2645 default_assemble_integer (rtx x ATTRIBUTE_UNUSED,
2646 unsigned int size ATTRIBUTE_UNUSED,
2647 int aligned_p ATTRIBUTE_UNUSED)
2649 const char *op = integer_asm_op (size, aligned_p);
2650 /* Avoid GAS bugs for large values. Specifically negative values whose
2651 absolute value fits in a bfd_vma, but not in a bfd_signed_vma. */
2652 if (size > UNITS_PER_WORD && size > POINTER_SIZE_UNITS)
2653 return false;
2654 return op && (assemble_integer_with_op (op, x), true);
2657 /* Assemble the integer constant X into an object of SIZE bytes. ALIGN is
2658 the alignment of the integer in bits. Return 1 if we were able to output
2659 the constant, otherwise 0. We must be able to output the constant,
2660 if FORCE is nonzero. */
2662 bool
2663 assemble_integer (rtx x, unsigned int size, unsigned int align, int force)
2665 int aligned_p;
2667 aligned_p = (align >= MIN (size * BITS_PER_UNIT, BIGGEST_ALIGNMENT));
2669 /* See if the target hook can handle this kind of object. */
2670 if (targetm.asm_out.integer (x, size, aligned_p))
2671 return true;
2673 /* If the object is a multi-byte one, try splitting it up. Split
2674 it into words it if is multi-word, otherwise split it into bytes. */
2675 if (size > 1)
2677 enum machine_mode omode, imode;
2678 unsigned int subalign;
2679 unsigned int subsize, i;
2680 enum mode_class mclass;
2682 subsize = size > UNITS_PER_WORD? UNITS_PER_WORD : 1;
2683 subalign = MIN (align, subsize * BITS_PER_UNIT);
2684 if (GET_CODE (x) == CONST_FIXED)
2685 mclass = GET_MODE_CLASS (GET_MODE (x));
2686 else
2687 mclass = MODE_INT;
2689 omode = mode_for_size (subsize * BITS_PER_UNIT, mclass, 0);
2690 imode = mode_for_size (size * BITS_PER_UNIT, mclass, 0);
2692 for (i = 0; i < size; i += subsize)
2694 rtx partial = simplify_subreg (omode, x, imode, i);
2695 if (!partial || !assemble_integer (partial, subsize, subalign, 0))
2696 break;
2698 if (i == size)
2699 return true;
2701 /* If we've printed some of it, but not all of it, there's no going
2702 back now. */
2703 gcc_assert (!i);
2706 gcc_assert (!force);
2708 return false;
2711 void
2712 assemble_real (REAL_VALUE_TYPE d, enum machine_mode mode, unsigned int align)
2714 long data[4] = {0, 0, 0, 0};
2715 int i;
2716 int bitsize, nelts, nunits, units_per;
2718 /* This is hairy. We have a quantity of known size. real_to_target
2719 will put it into an array of *host* longs, 32 bits per element
2720 (even if long is more than 32 bits). We need to determine the
2721 number of array elements that are occupied (nelts) and the number
2722 of *target* min-addressable units that will be occupied in the
2723 object file (nunits). We cannot assume that 32 divides the
2724 mode's bitsize (size * BITS_PER_UNIT) evenly.
2726 size * BITS_PER_UNIT is used here to make sure that padding bits
2727 (which might appear at either end of the value; real_to_target
2728 will include the padding bits in its output array) are included. */
2730 nunits = GET_MODE_SIZE (mode);
2731 bitsize = nunits * BITS_PER_UNIT;
2732 nelts = CEIL (bitsize, 32);
2733 units_per = 32 / BITS_PER_UNIT;
2735 real_to_target (data, &d, mode);
2737 /* Put out the first word with the specified alignment. */
2738 assemble_integer (GEN_INT (data[0]), MIN (nunits, units_per), align, 1);
2739 nunits -= units_per;
2741 /* Subsequent words need only 32-bit alignment. */
2742 align = min_align (align, 32);
2744 for (i = 1; i < nelts; i++)
2746 assemble_integer (GEN_INT (data[i]), MIN (nunits, units_per), align, 1);
2747 nunits -= units_per;
2751 /* Given an expression EXP with a constant value,
2752 reduce it to the sum of an assembler symbol and an integer.
2753 Store them both in the structure *VALUE.
2754 EXP must be reducible. */
2756 struct addr_const {
2757 rtx base;
2758 HOST_WIDE_INT offset;
2761 static void
2762 decode_addr_const (tree exp, struct addr_const *value)
2764 tree target = TREE_OPERAND (exp, 0);
2765 int offset = 0;
2766 rtx x;
2768 while (1)
2770 if (TREE_CODE (target) == COMPONENT_REF
2771 && tree_fits_shwi_p (byte_position (TREE_OPERAND (target, 1))))
2773 offset += int_byte_position (TREE_OPERAND (target, 1));
2774 target = TREE_OPERAND (target, 0);
2776 else if (TREE_CODE (target) == ARRAY_REF
2777 || TREE_CODE (target) == ARRAY_RANGE_REF)
2779 offset += (tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (target)))
2780 * tree_to_shwi (TREE_OPERAND (target, 1)));
2781 target = TREE_OPERAND (target, 0);
2783 else if (TREE_CODE (target) == MEM_REF
2784 && TREE_CODE (TREE_OPERAND (target, 0)) == ADDR_EXPR)
2786 offset += mem_ref_offset (target).to_short_addr ();
2787 target = TREE_OPERAND (TREE_OPERAND (target, 0), 0);
2789 else if (TREE_CODE (target) == INDIRECT_REF
2790 && TREE_CODE (TREE_OPERAND (target, 0)) == NOP_EXPR
2791 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (target, 0), 0))
2792 == ADDR_EXPR)
2793 target = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (target, 0), 0), 0);
2794 else
2795 break;
2798 switch (TREE_CODE (target))
2800 case VAR_DECL:
2801 case FUNCTION_DECL:
2802 x = DECL_RTL (target);
2803 break;
2805 case LABEL_DECL:
2806 x = gen_rtx_MEM (FUNCTION_MODE,
2807 gen_rtx_LABEL_REF (Pmode, force_label_rtx (target)));
2808 break;
2810 case REAL_CST:
2811 case FIXED_CST:
2812 case STRING_CST:
2813 case COMPLEX_CST:
2814 case CONSTRUCTOR:
2815 case INTEGER_CST:
2816 x = output_constant_def (target, 1);
2817 break;
2819 default:
2820 gcc_unreachable ();
2823 gcc_assert (MEM_P (x));
2824 x = XEXP (x, 0);
2826 value->base = x;
2827 value->offset = offset;
2830 static GTY(()) hash_table<tree_descriptor_hasher> *const_desc_htab;
2832 static void maybe_output_constant_def_contents (struct constant_descriptor_tree *, int);
2834 /* Constant pool accessor function. */
2836 hash_table<tree_descriptor_hasher> *
2837 constant_pool_htab (void)
2839 return const_desc_htab;
2842 /* Compute a hash code for a constant expression. */
2844 hashval_t
2845 tree_descriptor_hasher::hash (constant_descriptor_tree *ptr)
2847 return ptr->hash;
2850 static hashval_t
2851 const_hash_1 (const tree exp)
2853 const char *p;
2854 hashval_t hi;
2855 int len, i;
2856 enum tree_code code = TREE_CODE (exp);
2858 /* Either set P and LEN to the address and len of something to hash and
2859 exit the switch or return a value. */
2861 switch (code)
2863 case INTEGER_CST:
2864 p = (char *) &TREE_INT_CST_ELT (exp, 0);
2865 len = TREE_INT_CST_NUNITS (exp) * sizeof (HOST_WIDE_INT);
2866 break;
2868 case REAL_CST:
2869 return real_hash (TREE_REAL_CST_PTR (exp));
2871 case FIXED_CST:
2872 return fixed_hash (TREE_FIXED_CST_PTR (exp));
2874 case STRING_CST:
2875 p = TREE_STRING_POINTER (exp);
2876 len = TREE_STRING_LENGTH (exp);
2877 break;
2879 case COMPLEX_CST:
2880 return (const_hash_1 (TREE_REALPART (exp)) * 5
2881 + const_hash_1 (TREE_IMAGPART (exp)));
2883 case VECTOR_CST:
2885 unsigned i;
2887 hi = 7 + VECTOR_CST_NELTS (exp);
2889 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
2890 hi = hi * 563 + const_hash_1 (VECTOR_CST_ELT (exp, i));
2892 return hi;
2895 case CONSTRUCTOR:
2897 unsigned HOST_WIDE_INT idx;
2898 tree value;
2900 hi = 5 + int_size_in_bytes (TREE_TYPE (exp));
2902 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
2903 if (value)
2904 hi = hi * 603 + const_hash_1 (value);
2906 return hi;
2909 case ADDR_EXPR:
2910 case FDESC_EXPR:
2912 struct addr_const value;
2914 decode_addr_const (exp, &value);
2915 switch (GET_CODE (value.base))
2917 case SYMBOL_REF:
2918 /* Don't hash the address of the SYMBOL_REF;
2919 only use the offset and the symbol name. */
2920 hi = value.offset;
2921 p = XSTR (value.base, 0);
2922 for (i = 0; p[i] != 0; i++)
2923 hi = ((hi * 613) + (unsigned) (p[i]));
2924 break;
2926 case LABEL_REF:
2927 hi = (value.offset
2928 + CODE_LABEL_NUMBER (LABEL_REF_LABEL (value.base)) * 13);
2929 break;
2931 default:
2932 gcc_unreachable ();
2935 return hi;
2937 case PLUS_EXPR:
2938 case POINTER_PLUS_EXPR:
2939 case MINUS_EXPR:
2940 return (const_hash_1 (TREE_OPERAND (exp, 0)) * 9
2941 + const_hash_1 (TREE_OPERAND (exp, 1)));
2943 CASE_CONVERT:
2944 return const_hash_1 (TREE_OPERAND (exp, 0)) * 7 + 2;
2946 default:
2947 /* A language specific constant. Just hash the code. */
2948 return code;
2951 /* Compute hashing function. */
2952 hi = len;
2953 for (i = 0; i < len; i++)
2954 hi = ((hi * 613) + (unsigned) (p[i]));
2956 return hi;
2959 /* Wrapper of compare_constant, for the htab interface. */
2960 bool
2961 tree_descriptor_hasher::equal (constant_descriptor_tree *c1,
2962 constant_descriptor_tree *c2)
2964 if (c1->hash != c2->hash)
2965 return 0;
2966 return compare_constant (c1->value, c2->value);
2969 /* Compare t1 and t2, and return 1 only if they are known to result in
2970 the same bit pattern on output. */
2972 static int
2973 compare_constant (const tree t1, const tree t2)
2975 enum tree_code typecode;
2977 if (t1 == NULL_TREE)
2978 return t2 == NULL_TREE;
2979 if (t2 == NULL_TREE)
2980 return 0;
2982 if (TREE_CODE (t1) != TREE_CODE (t2))
2983 return 0;
2985 switch (TREE_CODE (t1))
2987 case INTEGER_CST:
2988 /* Integer constants are the same only if the same width of type. */
2989 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
2990 return 0;
2991 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
2992 return 0;
2993 return tree_int_cst_equal (t1, t2);
2995 case REAL_CST:
2996 /* Real constants are the same only if the same width of type. */
2997 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
2998 return 0;
3000 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
3002 case FIXED_CST:
3003 /* Fixed constants are the same only if the same width of type. */
3004 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3005 return 0;
3007 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
3009 case STRING_CST:
3010 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
3011 return 0;
3013 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
3014 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
3015 TREE_STRING_LENGTH (t1)));
3017 case COMPLEX_CST:
3018 return (compare_constant (TREE_REALPART (t1), TREE_REALPART (t2))
3019 && compare_constant (TREE_IMAGPART (t1), TREE_IMAGPART (t2)));
3021 case VECTOR_CST:
3023 unsigned i;
3025 if (VECTOR_CST_NELTS (t1) != VECTOR_CST_NELTS (t2))
3026 return 0;
3028 for (i = 0; i < VECTOR_CST_NELTS (t1); ++i)
3029 if (!compare_constant (VECTOR_CST_ELT (t1, i),
3030 VECTOR_CST_ELT (t2, i)))
3031 return 0;
3033 return 1;
3036 case CONSTRUCTOR:
3038 vec<constructor_elt, va_gc> *v1, *v2;
3039 unsigned HOST_WIDE_INT idx;
3041 typecode = TREE_CODE (TREE_TYPE (t1));
3042 if (typecode != TREE_CODE (TREE_TYPE (t2)))
3043 return 0;
3045 if (typecode == ARRAY_TYPE)
3047 HOST_WIDE_INT size_1 = int_size_in_bytes (TREE_TYPE (t1));
3048 /* For arrays, check that the sizes all match. */
3049 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))
3050 || size_1 == -1
3051 || size_1 != int_size_in_bytes (TREE_TYPE (t2)))
3052 return 0;
3054 else
3056 /* For record and union constructors, require exact type
3057 equality. */
3058 if (TREE_TYPE (t1) != TREE_TYPE (t2))
3059 return 0;
3062 v1 = CONSTRUCTOR_ELTS (t1);
3063 v2 = CONSTRUCTOR_ELTS (t2);
3064 if (vec_safe_length (v1) != vec_safe_length (v2))
3065 return 0;
3067 for (idx = 0; idx < vec_safe_length (v1); ++idx)
3069 constructor_elt *c1 = &(*v1)[idx];
3070 constructor_elt *c2 = &(*v2)[idx];
3072 /* Check that each value is the same... */
3073 if (!compare_constant (c1->value, c2->value))
3074 return 0;
3075 /* ... and that they apply to the same fields! */
3076 if (typecode == ARRAY_TYPE)
3078 if (!compare_constant (c1->index, c2->index))
3079 return 0;
3081 else
3083 if (c1->index != c2->index)
3084 return 0;
3088 return 1;
3091 case ADDR_EXPR:
3092 case FDESC_EXPR:
3094 struct addr_const value1, value2;
3095 enum rtx_code code;
3096 int ret;
3098 decode_addr_const (t1, &value1);
3099 decode_addr_const (t2, &value2);
3101 if (value1.offset != value2.offset)
3102 return 0;
3104 code = GET_CODE (value1.base);
3105 if (code != GET_CODE (value2.base))
3106 return 0;
3108 switch (code)
3110 case SYMBOL_REF:
3111 ret = (strcmp (XSTR (value1.base, 0), XSTR (value2.base, 0)) == 0);
3112 break;
3114 case LABEL_REF:
3115 ret = (CODE_LABEL_NUMBER (LABEL_REF_LABEL (value1.base))
3116 == CODE_LABEL_NUMBER (LABEL_REF_LABEL (value2.base)));
3117 break;
3119 default:
3120 gcc_unreachable ();
3122 return ret;
3125 case PLUS_EXPR:
3126 case POINTER_PLUS_EXPR:
3127 case MINUS_EXPR:
3128 case RANGE_EXPR:
3129 return (compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
3130 && compare_constant (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
3132 CASE_CONVERT:
3133 case VIEW_CONVERT_EXPR:
3134 return compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3136 default:
3137 return 0;
3140 gcc_unreachable ();
3143 /* Return the section into which constant EXP should be placed. */
3145 static section *
3146 get_constant_section (tree exp, unsigned int align)
3148 return targetm.asm_out.select_section (exp,
3149 compute_reloc_for_constant (exp),
3150 align);
3153 /* Return the size of constant EXP in bytes. */
3155 static HOST_WIDE_INT
3156 get_constant_size (tree exp)
3158 HOST_WIDE_INT size;
3160 size = int_size_in_bytes (TREE_TYPE (exp));
3161 if (TREE_CODE (exp) == STRING_CST)
3162 size = MAX (TREE_STRING_LENGTH (exp), size);
3163 return size;
3166 /* Subroutine of output_constant_def:
3167 No constant equal to EXP is known to have been output.
3168 Make a constant descriptor to enter EXP in the hash table.
3169 Assign the label number and construct RTL to refer to the
3170 constant's location in memory.
3171 Caller is responsible for updating the hash table. */
3173 static struct constant_descriptor_tree *
3174 build_constant_desc (tree exp)
3176 struct constant_descriptor_tree *desc;
3177 rtx symbol, rtl;
3178 char label[256];
3179 int labelno;
3180 tree decl;
3182 desc = ggc_alloc<constant_descriptor_tree> ();
3183 desc->value = exp;
3185 /* Create a string containing the label name, in LABEL. */
3186 labelno = const_labelno++;
3187 ASM_GENERATE_INTERNAL_LABEL (label, "LC", labelno);
3189 /* Construct the VAR_DECL associated with the constant. */
3190 decl = build_decl (UNKNOWN_LOCATION, VAR_DECL, get_identifier (label),
3191 TREE_TYPE (exp));
3192 DECL_ARTIFICIAL (decl) = 1;
3193 DECL_IGNORED_P (decl) = 1;
3194 TREE_READONLY (decl) = 1;
3195 TREE_STATIC (decl) = 1;
3196 TREE_ADDRESSABLE (decl) = 1;
3197 /* We don't set the RTL yet as this would cause varpool to assume that the
3198 variable is referenced. Moreover, it would just be dropped in LTO mode.
3199 Instead we set the flag that will be recognized in make_decl_rtl. */
3200 DECL_IN_CONSTANT_POOL (decl) = 1;
3201 DECL_INITIAL (decl) = desc->value;
3202 /* ??? CONSTANT_ALIGNMENT hasn't been updated for vector types on most
3203 architectures so use DATA_ALIGNMENT as well, except for strings. */
3204 if (TREE_CODE (exp) == STRING_CST)
3206 #ifdef CONSTANT_ALIGNMENT
3207 DECL_ALIGN (decl) = CONSTANT_ALIGNMENT (exp, DECL_ALIGN (decl));
3208 #endif
3210 else
3211 align_variable (decl, 0);
3213 /* Now construct the SYMBOL_REF and the MEM. */
3214 if (use_object_blocks_p ())
3216 section *sect = get_constant_section (exp, DECL_ALIGN (decl));
3217 symbol = create_block_symbol (ggc_strdup (label),
3218 get_block_for_section (sect), -1);
3220 else
3221 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3222 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3223 SET_SYMBOL_REF_DECL (symbol, decl);
3224 TREE_CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3226 rtl = gen_const_mem (TYPE_MODE (TREE_TYPE (exp)), symbol);
3227 set_mem_attributes (rtl, exp, 1);
3228 set_mem_alias_set (rtl, 0);
3230 /* We cannot share RTX'es in pool entries.
3231 Mark this piece of RTL as required for unsharing. */
3232 RTX_FLAG (rtl, used) = 1;
3234 /* Set flags or add text to the name to record information, such as
3235 that it is a local symbol. If the name is changed, the macro
3236 ASM_OUTPUT_LABELREF will have to know how to strip this
3237 information. This call might invalidate our local variable
3238 SYMBOL; we can't use it afterward. */
3239 targetm.encode_section_info (exp, rtl, true);
3241 desc->rtl = rtl;
3243 return desc;
3246 /* Return an rtx representing a reference to constant data in memory
3247 for the constant expression EXP.
3249 If assembler code for such a constant has already been output,
3250 return an rtx to refer to it.
3251 Otherwise, output such a constant in memory
3252 and generate an rtx for it.
3254 If DEFER is nonzero, this constant can be deferred and output only
3255 if referenced in the function after all optimizations.
3257 `const_desc_table' records which constants already have label strings. */
3260 output_constant_def (tree exp, int defer)
3262 struct constant_descriptor_tree *desc;
3263 struct constant_descriptor_tree key;
3265 /* Look up EXP in the table of constant descriptors. If we didn't find
3266 it, create a new one. */
3267 key.value = exp;
3268 key.hash = const_hash_1 (exp);
3269 constant_descriptor_tree **loc
3270 = const_desc_htab->find_slot_with_hash (&key, key.hash, INSERT);
3272 desc = *loc;
3273 if (desc == 0)
3275 desc = build_constant_desc (exp);
3276 desc->hash = key.hash;
3277 *loc = desc;
3280 maybe_output_constant_def_contents (desc, defer);
3281 return desc->rtl;
3284 /* Subroutine of output_constant_def: Decide whether or not we need to
3285 output the constant DESC now, and if so, do it. */
3286 static void
3287 maybe_output_constant_def_contents (struct constant_descriptor_tree *desc,
3288 int defer)
3290 rtx symbol = XEXP (desc->rtl, 0);
3291 tree exp = desc->value;
3293 if (flag_syntax_only)
3294 return;
3296 if (TREE_ASM_WRITTEN (exp))
3297 /* Already output; don't do it again. */
3298 return;
3300 /* We can always defer constants as long as the context allows
3301 doing so. */
3302 if (defer)
3304 /* Increment n_deferred_constants if it exists. It needs to be at
3305 least as large as the number of constants actually referred to
3306 by the function. If it's too small we'll stop looking too early
3307 and fail to emit constants; if it's too large we'll only look
3308 through the entire function when we could have stopped earlier. */
3309 if (cfun)
3310 n_deferred_constants++;
3311 return;
3314 output_constant_def_contents (symbol);
3317 /* Subroutine of output_constant_def_contents. Output the definition
3318 of constant EXP, which is pointed to by label LABEL. ALIGN is the
3319 constant's alignment in bits. */
3321 static void
3322 assemble_constant_contents (tree exp, const char *label, unsigned int align)
3324 HOST_WIDE_INT size;
3326 size = get_constant_size (exp);
3328 /* Do any machine/system dependent processing of the constant. */
3329 targetm.asm_out.declare_constant_name (asm_out_file, label, exp, size);
3331 /* Output the value of EXP. */
3332 output_constant (exp, size, align);
3335 /* We must output the constant data referred to by SYMBOL; do so. */
3337 static void
3338 output_constant_def_contents (rtx symbol)
3340 tree decl = SYMBOL_REF_DECL (symbol);
3341 tree exp = DECL_INITIAL (decl);
3342 unsigned int align;
3343 bool asan_protected = false;
3345 /* Make sure any other constants whose addresses appear in EXP
3346 are assigned label numbers. */
3347 output_addressed_constants (exp);
3349 /* We are no longer deferring this constant. */
3350 TREE_ASM_WRITTEN (decl) = TREE_ASM_WRITTEN (exp) = 1;
3352 if ((flag_sanitize & SANITIZE_ADDRESS)
3353 && TREE_CODE (exp) == STRING_CST
3354 && asan_protect_global (exp))
3356 asan_protected = true;
3357 DECL_ALIGN (decl) = MAX (DECL_ALIGN (decl),
3358 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
3361 /* If the constant is part of an object block, make sure that the
3362 decl has been positioned within its block, but do not write out
3363 its definition yet. output_object_blocks will do that later. */
3364 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
3365 place_block_symbol (symbol);
3366 else
3368 align = DECL_ALIGN (decl);
3369 switch_to_section (get_constant_section (exp, align));
3370 if (align > BITS_PER_UNIT)
3371 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
3372 assemble_constant_contents (exp, XSTR (symbol, 0), align);
3373 if (asan_protected)
3375 HOST_WIDE_INT size = get_constant_size (exp);
3376 assemble_zeros (asan_red_zone_size (size));
3381 /* Look up EXP in the table of constant descriptors. Return the rtl
3382 if it has been emitted, else null. */
3385 lookup_constant_def (tree exp)
3387 struct constant_descriptor_tree key;
3389 key.value = exp;
3390 key.hash = const_hash_1 (exp);
3391 constant_descriptor_tree *desc
3392 = const_desc_htab->find_with_hash (&key, key.hash);
3394 return (desc ? desc->rtl : NULL_RTX);
3397 /* Return a tree representing a reference to constant data in memory
3398 for the constant expression EXP.
3400 This is the counterpart of output_constant_def at the Tree level. */
3402 tree
3403 tree_output_constant_def (tree exp)
3405 struct constant_descriptor_tree *desc, key;
3406 tree decl;
3408 /* Look up EXP in the table of constant descriptors. If we didn't find
3409 it, create a new one. */
3410 key.value = exp;
3411 key.hash = const_hash_1 (exp);
3412 constant_descriptor_tree **loc
3413 = const_desc_htab->find_slot_with_hash (&key, key.hash, INSERT);
3415 desc = *loc;
3416 if (desc == 0)
3418 desc = build_constant_desc (exp);
3419 desc->hash = key.hash;
3420 *loc = desc;
3423 decl = SYMBOL_REF_DECL (XEXP (desc->rtl, 0));
3424 varpool_node::finalize_decl (decl);
3425 return decl;
3428 struct GTY((chain_next ("%h.next"), for_user)) constant_descriptor_rtx {
3429 struct constant_descriptor_rtx *next;
3430 rtx mem;
3431 rtx sym;
3432 rtx constant;
3433 HOST_WIDE_INT offset;
3434 hashval_t hash;
3435 enum machine_mode mode;
3436 unsigned int align;
3437 int labelno;
3438 int mark;
3441 struct const_rtx_desc_hasher : ggc_hasher<constant_descriptor_rtx *>
3443 static hashval_t hash (constant_descriptor_rtx *);
3444 static bool equal (constant_descriptor_rtx *, constant_descriptor_rtx *);
3447 /* Used in the hash tables to avoid outputting the same constant
3448 twice. Unlike 'struct constant_descriptor_tree', RTX constants
3449 are output once per function, not once per file. */
3450 /* ??? Only a few targets need per-function constant pools. Most
3451 can use one per-file pool. Should add a targetm bit to tell the
3452 difference. */
3454 struct GTY(()) rtx_constant_pool {
3455 /* Pointers to first and last constant in pool, as ordered by offset. */
3456 struct constant_descriptor_rtx *first;
3457 struct constant_descriptor_rtx *last;
3459 /* Hash facility for making memory-constants from constant rtl-expressions.
3460 It is used on RISC machines where immediate integer arguments and
3461 constant addresses are restricted so that such constants must be stored
3462 in memory. */
3463 hash_table<const_rtx_desc_hasher> *const_rtx_htab;
3465 /* Current offset in constant pool (does not include any
3466 machine-specific header). */
3467 HOST_WIDE_INT offset;
3470 /* Hash and compare functions for const_rtx_htab. */
3472 hashval_t
3473 const_rtx_desc_hasher::hash (constant_descriptor_rtx *desc)
3475 return desc->hash;
3478 bool
3479 const_rtx_desc_hasher::equal (constant_descriptor_rtx *x,
3480 constant_descriptor_rtx *y)
3482 if (x->mode != y->mode)
3483 return 0;
3484 return rtx_equal_p (x->constant, y->constant);
3487 /* Hash one component of a constant. */
3489 static hashval_t
3490 const_rtx_hash_1 (const_rtx x)
3492 unsigned HOST_WIDE_INT hwi;
3493 enum machine_mode mode;
3494 enum rtx_code code;
3495 hashval_t h;
3496 int i;
3498 code = GET_CODE (x);
3499 mode = GET_MODE (x);
3500 h = (hashval_t) code * 1048573 + mode;
3502 switch (code)
3504 case CONST_INT:
3505 hwi = INTVAL (x);
3507 fold_hwi:
3509 int shift = sizeof (hashval_t) * CHAR_BIT;
3510 const int n = sizeof (HOST_WIDE_INT) / sizeof (hashval_t);
3512 h ^= (hashval_t) hwi;
3513 for (i = 1; i < n; ++i)
3515 hwi >>= shift;
3516 h ^= (hashval_t) hwi;
3519 break;
3521 case CONST_WIDE_INT:
3522 hwi = GET_MODE_PRECISION (mode);
3524 for (i = 0; i < CONST_WIDE_INT_NUNITS (x); i++)
3525 hwi ^= CONST_WIDE_INT_ELT (x, i);
3526 goto fold_hwi;
3529 case CONST_DOUBLE:
3530 if (TARGET_SUPPORTS_WIDE_INT == 0 && mode == VOIDmode)
3532 hwi = CONST_DOUBLE_LOW (x) ^ CONST_DOUBLE_HIGH (x);
3533 goto fold_hwi;
3535 else
3536 h ^= real_hash (CONST_DOUBLE_REAL_VALUE (x));
3537 break;
3539 case CONST_FIXED:
3540 h ^= fixed_hash (CONST_FIXED_VALUE (x));
3541 break;
3543 case SYMBOL_REF:
3544 h ^= htab_hash_string (XSTR (x, 0));
3545 break;
3547 case LABEL_REF:
3548 h = h * 251 + CODE_LABEL_NUMBER (LABEL_REF_LABEL (x));
3549 break;
3551 case UNSPEC:
3552 case UNSPEC_VOLATILE:
3553 h = h * 251 + XINT (x, 1);
3554 break;
3556 default:
3557 break;
3560 return h;
3563 /* Compute a hash value for X, which should be a constant. */
3565 static hashval_t
3566 const_rtx_hash (rtx x)
3568 hashval_t h = 0;
3569 subrtx_iterator::array_type array;
3570 FOR_EACH_SUBRTX (iter, array, x, ALL)
3571 h = h * 509 + const_rtx_hash_1 (*iter);
3572 return h;
3576 /* Create and return a new rtx constant pool. */
3578 static struct rtx_constant_pool *
3579 create_constant_pool (void)
3581 struct rtx_constant_pool *pool;
3583 pool = ggc_alloc<rtx_constant_pool> ();
3584 pool->const_rtx_htab = hash_table<const_rtx_desc_hasher>::create_ggc (31);
3585 pool->first = NULL;
3586 pool->last = NULL;
3587 pool->offset = 0;
3588 return pool;
3591 /* Initialize constant pool hashing for a new function. */
3593 void
3594 init_varasm_status (void)
3596 crtl->varasm.pool = create_constant_pool ();
3597 crtl->varasm.deferred_constants = 0;
3600 /* Given a MINUS expression, simplify it if both sides
3601 include the same symbol. */
3604 simplify_subtraction (rtx x)
3606 rtx r = simplify_rtx (x);
3607 return r ? r : x;
3610 /* Given a constant rtx X, make (or find) a memory constant for its value
3611 and return a MEM rtx to refer to it in memory. */
3614 force_const_mem (enum machine_mode mode, rtx x)
3616 struct constant_descriptor_rtx *desc, tmp;
3617 struct rtx_constant_pool *pool;
3618 char label[256];
3619 rtx def, symbol;
3620 hashval_t hash;
3621 unsigned int align;
3622 constant_descriptor_rtx **slot;
3624 /* If we're not allowed to drop X into the constant pool, don't. */
3625 if (targetm.cannot_force_const_mem (mode, x))
3626 return NULL_RTX;
3628 /* Record that this function has used a constant pool entry. */
3629 crtl->uses_const_pool = 1;
3631 /* Decide which pool to use. */
3632 pool = (targetm.use_blocks_for_constant_p (mode, x)
3633 ? shared_constant_pool
3634 : crtl->varasm.pool);
3636 /* Lookup the value in the hashtable. */
3637 tmp.constant = x;
3638 tmp.mode = mode;
3639 hash = const_rtx_hash (x);
3640 slot = pool->const_rtx_htab->find_slot_with_hash (&tmp, hash, INSERT);
3641 desc = *slot;
3643 /* If the constant was already present, return its memory. */
3644 if (desc)
3645 return copy_rtx (desc->mem);
3647 /* Otherwise, create a new descriptor. */
3648 desc = ggc_alloc<constant_descriptor_rtx> ();
3649 *slot = desc;
3651 /* Align the location counter as required by EXP's data type. */
3652 align = GET_MODE_ALIGNMENT (mode == VOIDmode ? word_mode : mode);
3653 #ifdef CONSTANT_ALIGNMENT
3655 tree type = lang_hooks.types.type_for_mode (mode, 0);
3656 if (type != NULL_TREE)
3657 align = CONSTANT_ALIGNMENT (make_tree (type, x), align);
3659 #endif
3661 pool->offset += (align / BITS_PER_UNIT) - 1;
3662 pool->offset &= ~ ((align / BITS_PER_UNIT) - 1);
3664 desc->next = NULL;
3665 desc->constant = copy_rtx (tmp.constant);
3666 desc->offset = pool->offset;
3667 desc->hash = hash;
3668 desc->mode = mode;
3669 desc->align = align;
3670 desc->labelno = const_labelno;
3671 desc->mark = 0;
3673 pool->offset += GET_MODE_SIZE (mode);
3674 if (pool->last)
3675 pool->last->next = desc;
3676 else
3677 pool->first = pool->last = desc;
3678 pool->last = desc;
3680 /* Create a string containing the label name, in LABEL. */
3681 ASM_GENERATE_INTERNAL_LABEL (label, "LC", const_labelno);
3682 ++const_labelno;
3684 /* Construct the SYMBOL_REF. Make sure to mark it as belonging to
3685 the constants pool. */
3686 if (use_object_blocks_p () && targetm.use_blocks_for_constant_p (mode, x))
3688 section *sect = targetm.asm_out.select_rtx_section (mode, x, align);
3689 symbol = create_block_symbol (ggc_strdup (label),
3690 get_block_for_section (sect), -1);
3692 else
3693 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3694 desc->sym = symbol;
3695 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3696 CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3697 SET_SYMBOL_REF_CONSTANT (symbol, desc);
3699 /* Construct the MEM. */
3700 desc->mem = def = gen_const_mem (mode, symbol);
3701 set_mem_attributes (def, lang_hooks.types.type_for_mode (mode, 0), 1);
3702 set_mem_align (def, align);
3704 /* If we're dropping a label to the constant pool, make sure we
3705 don't delete it. */
3706 if (GET_CODE (x) == LABEL_REF)
3707 LABEL_PRESERVE_P (XEXP (x, 0)) = 1;
3709 return copy_rtx (def);
3712 /* Given a constant pool SYMBOL_REF, return the corresponding constant. */
3715 get_pool_constant (const_rtx addr)
3717 return SYMBOL_REF_CONSTANT (addr)->constant;
3720 /* Given a constant pool SYMBOL_REF, return the corresponding constant
3721 and whether it has been output or not. */
3724 get_pool_constant_mark (rtx addr, bool *pmarked)
3726 struct constant_descriptor_rtx *desc;
3728 desc = SYMBOL_REF_CONSTANT (addr);
3729 *pmarked = (desc->mark != 0);
3730 return desc->constant;
3733 /* Similar, return the mode. */
3735 enum machine_mode
3736 get_pool_mode (const_rtx addr)
3738 return SYMBOL_REF_CONSTANT (addr)->mode;
3741 /* Return the size of the constant pool. */
3744 get_pool_size (void)
3746 return crtl->varasm.pool->offset;
3749 /* Worker function for output_constant_pool_1. Emit assembly for X
3750 in MODE with known alignment ALIGN. */
3752 static void
3753 output_constant_pool_2 (enum machine_mode mode, rtx x, unsigned int align)
3755 switch (GET_MODE_CLASS (mode))
3757 case MODE_FLOAT:
3758 case MODE_DECIMAL_FLOAT:
3760 REAL_VALUE_TYPE r;
3762 gcc_assert (CONST_DOUBLE_AS_FLOAT_P (x));
3763 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
3764 assemble_real (r, mode, align);
3765 break;
3768 case MODE_INT:
3769 case MODE_PARTIAL_INT:
3770 case MODE_FRACT:
3771 case MODE_UFRACT:
3772 case MODE_ACCUM:
3773 case MODE_UACCUM:
3774 assemble_integer (x, GET_MODE_SIZE (mode), align, 1);
3775 break;
3777 case MODE_VECTOR_FLOAT:
3778 case MODE_VECTOR_INT:
3779 case MODE_VECTOR_FRACT:
3780 case MODE_VECTOR_UFRACT:
3781 case MODE_VECTOR_ACCUM:
3782 case MODE_VECTOR_UACCUM:
3784 int i, units;
3785 enum machine_mode submode = GET_MODE_INNER (mode);
3786 unsigned int subalign = MIN (align, GET_MODE_BITSIZE (submode));
3788 gcc_assert (GET_CODE (x) == CONST_VECTOR);
3789 units = CONST_VECTOR_NUNITS (x);
3791 for (i = 0; i < units; i++)
3793 rtx elt = CONST_VECTOR_ELT (x, i);
3794 output_constant_pool_2 (submode, elt, i ? subalign : align);
3797 break;
3799 default:
3800 gcc_unreachable ();
3804 /* Worker function for output_constant_pool. Emit constant DESC,
3805 giving it ALIGN bits of alignment. */
3807 static void
3808 output_constant_pool_1 (struct constant_descriptor_rtx *desc,
3809 unsigned int align)
3811 rtx x, tmp;
3813 x = desc->constant;
3815 /* See if X is a LABEL_REF (or a CONST referring to a LABEL_REF)
3816 whose CODE_LABEL has been deleted. This can occur if a jump table
3817 is eliminated by optimization. If so, write a constant of zero
3818 instead. Note that this can also happen by turning the
3819 CODE_LABEL into a NOTE. */
3820 /* ??? This seems completely and utterly wrong. Certainly it's
3821 not true for NOTE_INSN_DELETED_LABEL, but I disbelieve proper
3822 functioning even with rtx_insn::deleted and friends. */
3824 tmp = x;
3825 switch (GET_CODE (tmp))
3827 case CONST:
3828 if (GET_CODE (XEXP (tmp, 0)) != PLUS
3829 || GET_CODE (XEXP (XEXP (tmp, 0), 0)) != LABEL_REF)
3830 break;
3831 tmp = XEXP (XEXP (tmp, 0), 0);
3832 /* FALLTHRU */
3834 case LABEL_REF:
3835 tmp = LABEL_REF_LABEL (tmp);
3836 gcc_assert (!as_a<rtx_insn *> (tmp)->deleted ());
3837 gcc_assert (!NOTE_P (tmp)
3838 || NOTE_KIND (tmp) != NOTE_INSN_DELETED);
3839 break;
3841 default:
3842 break;
3845 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3846 ASM_OUTPUT_SPECIAL_POOL_ENTRY (asm_out_file, x, desc->mode,
3847 align, desc->labelno, done);
3848 #endif
3850 assemble_align (align);
3852 /* Output the label. */
3853 targetm.asm_out.internal_label (asm_out_file, "LC", desc->labelno);
3855 /* Output the data. */
3856 output_constant_pool_2 (desc->mode, x, align);
3858 /* Make sure all constants in SECTION_MERGE and not SECTION_STRINGS
3859 sections have proper size. */
3860 if (align > GET_MODE_BITSIZE (desc->mode)
3861 && in_section
3862 && (in_section->common.flags & SECTION_MERGE))
3863 assemble_align (align);
3865 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3866 done:
3867 #endif
3868 return;
3871 /* Mark all constants that are referenced by SYMBOL_REFs in X.
3872 Emit referenced deferred strings. */
3874 static void
3875 mark_constants_in_pattern (rtx insn)
3877 subrtx_iterator::array_type array;
3878 FOR_EACH_SUBRTX (iter, array, PATTERN (insn), ALL)
3880 const_rtx x = *iter;
3881 if (GET_CODE (x) == SYMBOL_REF)
3883 if (CONSTANT_POOL_ADDRESS_P (x))
3885 struct constant_descriptor_rtx *desc = SYMBOL_REF_CONSTANT (x);
3886 if (desc->mark == 0)
3888 desc->mark = 1;
3889 iter.substitute (desc->constant);
3892 else if (TREE_CONSTANT_POOL_ADDRESS_P (x))
3894 tree decl = SYMBOL_REF_DECL (x);
3895 if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
3897 n_deferred_constants--;
3898 output_constant_def_contents (CONST_CAST_RTX (x));
3905 /* Look through appropriate parts of INSN, marking all entries in the
3906 constant pool which are actually being used. Entries that are only
3907 referenced by other constants are also marked as used. Emit
3908 deferred strings that are used. */
3910 static void
3911 mark_constants (rtx_insn *insn)
3913 if (!INSN_P (insn))
3914 return;
3916 /* Insns may appear inside a SEQUENCE. Only check the patterns of
3917 insns, not any notes that may be attached. We don't want to mark
3918 a constant just because it happens to appear in a REG_EQUIV note. */
3919 if (rtx_sequence *seq = dyn_cast <rtx_sequence *> (PATTERN (insn)))
3921 int i, n = seq->len ();
3922 for (i = 0; i < n; ++i)
3924 rtx subinsn = seq->element (i);
3925 if (INSN_P (subinsn))
3926 mark_constants_in_pattern (subinsn);
3929 else
3930 mark_constants_in_pattern (insn);
3933 /* Look through the instructions for this function, and mark all the
3934 entries in POOL which are actually being used. Emit deferred constants
3935 which have indeed been used. */
3937 static void
3938 mark_constant_pool (void)
3940 rtx_insn *insn;
3942 if (!crtl->uses_const_pool && n_deferred_constants == 0)
3943 return;
3945 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3946 mark_constants (insn);
3949 /* Write all the constants in POOL. */
3951 static void
3952 output_constant_pool_contents (struct rtx_constant_pool *pool)
3954 struct constant_descriptor_rtx *desc;
3956 for (desc = pool->first; desc ; desc = desc->next)
3957 if (desc->mark)
3959 /* If the constant is part of an object_block, make sure that
3960 the constant has been positioned within its block, but do not
3961 write out its definition yet. output_object_blocks will do
3962 that later. */
3963 if (SYMBOL_REF_HAS_BLOCK_INFO_P (desc->sym)
3964 && SYMBOL_REF_BLOCK (desc->sym))
3965 place_block_symbol (desc->sym);
3966 else
3968 switch_to_section (targetm.asm_out.select_rtx_section
3969 (desc->mode, desc->constant, desc->align));
3970 output_constant_pool_1 (desc, desc->align);
3975 /* Mark all constants that are used in the current function, then write
3976 out the function's private constant pool. */
3978 static void
3979 output_constant_pool (const char *fnname ATTRIBUTE_UNUSED,
3980 tree fndecl ATTRIBUTE_UNUSED)
3982 struct rtx_constant_pool *pool = crtl->varasm.pool;
3984 /* It is possible for gcc to call force_const_mem and then to later
3985 discard the instructions which refer to the constant. In such a
3986 case we do not need to output the constant. */
3987 mark_constant_pool ();
3989 #ifdef ASM_OUTPUT_POOL_PROLOGUE
3990 ASM_OUTPUT_POOL_PROLOGUE (asm_out_file, fnname, fndecl, pool->offset);
3991 #endif
3993 output_constant_pool_contents (pool);
3995 #ifdef ASM_OUTPUT_POOL_EPILOGUE
3996 ASM_OUTPUT_POOL_EPILOGUE (asm_out_file, fnname, fndecl, pool->offset);
3997 #endif
4000 /* Write the contents of the shared constant pool. */
4002 void
4003 output_shared_constant_pool (void)
4005 output_constant_pool_contents (shared_constant_pool);
4008 /* Determine what kind of relocations EXP may need. */
4011 compute_reloc_for_constant (tree exp)
4013 int reloc = 0, reloc2;
4014 tree tem;
4016 switch (TREE_CODE (exp))
4018 case ADDR_EXPR:
4019 case FDESC_EXPR:
4020 /* Go inside any operations that get_inner_reference can handle and see
4021 if what's inside is a constant: no need to do anything here for
4022 addresses of variables or functions. */
4023 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
4024 tem = TREE_OPERAND (tem, 0))
4027 if (TREE_CODE (tem) == MEM_REF
4028 && TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR)
4030 reloc = compute_reloc_for_constant (TREE_OPERAND (tem, 0));
4031 break;
4034 if (!targetm.binds_local_p (tem))
4035 reloc |= 2;
4036 else
4037 reloc |= 1;
4038 break;
4040 case PLUS_EXPR:
4041 case POINTER_PLUS_EXPR:
4042 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4043 reloc |= compute_reloc_for_constant (TREE_OPERAND (exp, 1));
4044 break;
4046 case MINUS_EXPR:
4047 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4048 reloc2 = compute_reloc_for_constant (TREE_OPERAND (exp, 1));
4049 /* The difference of two local labels is computable at link time. */
4050 if (reloc == 1 && reloc2 == 1)
4051 reloc = 0;
4052 else
4053 reloc |= reloc2;
4054 break;
4056 CASE_CONVERT:
4057 case VIEW_CONVERT_EXPR:
4058 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4059 break;
4061 case CONSTRUCTOR:
4063 unsigned HOST_WIDE_INT idx;
4064 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
4065 if (tem != 0)
4066 reloc |= compute_reloc_for_constant (tem);
4068 break;
4070 default:
4071 break;
4073 return reloc;
4076 /* Find all the constants whose addresses are referenced inside of EXP,
4077 and make sure assembler code with a label has been output for each one.
4078 Indicate whether an ADDR_EXPR has been encountered. */
4080 static void
4081 output_addressed_constants (tree exp)
4083 tree tem;
4085 switch (TREE_CODE (exp))
4087 case ADDR_EXPR:
4088 case FDESC_EXPR:
4089 /* Go inside any operations that get_inner_reference can handle and see
4090 if what's inside is a constant: no need to do anything here for
4091 addresses of variables or functions. */
4092 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
4093 tem = TREE_OPERAND (tem, 0))
4096 /* If we have an initialized CONST_DECL, retrieve the initializer. */
4097 if (TREE_CODE (tem) == CONST_DECL && DECL_INITIAL (tem))
4098 tem = DECL_INITIAL (tem);
4100 if (CONSTANT_CLASS_P (tem) || TREE_CODE (tem) == CONSTRUCTOR)
4101 output_constant_def (tem, 0);
4103 if (TREE_CODE (tem) == MEM_REF)
4104 output_addressed_constants (TREE_OPERAND (tem, 0));
4105 break;
4107 case PLUS_EXPR:
4108 case POINTER_PLUS_EXPR:
4109 case MINUS_EXPR:
4110 output_addressed_constants (TREE_OPERAND (exp, 1));
4111 /* Fall through. */
4113 CASE_CONVERT:
4114 case VIEW_CONVERT_EXPR:
4115 output_addressed_constants (TREE_OPERAND (exp, 0));
4116 break;
4118 case CONSTRUCTOR:
4120 unsigned HOST_WIDE_INT idx;
4121 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
4122 if (tem != 0)
4123 output_addressed_constants (tem);
4125 break;
4127 default:
4128 break;
4132 /* Whether a constructor CTOR is a valid static constant initializer if all
4133 its elements are. This used to be internal to initializer_constant_valid_p
4134 and has been exposed to let other functions like categorize_ctor_elements
4135 evaluate the property while walking a constructor for other purposes. */
4137 bool
4138 constructor_static_from_elts_p (const_tree ctor)
4140 return (TREE_CONSTANT (ctor)
4141 && (TREE_CODE (TREE_TYPE (ctor)) == UNION_TYPE
4142 || TREE_CODE (TREE_TYPE (ctor)) == RECORD_TYPE
4143 || TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE));
4146 static tree initializer_constant_valid_p_1 (tree value, tree endtype,
4147 tree *cache);
4149 /* A subroutine of initializer_constant_valid_p. VALUE is a MINUS_EXPR,
4150 PLUS_EXPR or POINTER_PLUS_EXPR. This looks for cases of VALUE
4151 which are valid when ENDTYPE is an integer of any size; in
4152 particular, this does not accept a pointer minus a constant. This
4153 returns null_pointer_node if the VALUE is an absolute constant
4154 which can be used to initialize a static variable. Otherwise it
4155 returns NULL. */
4157 static tree
4158 narrowing_initializer_constant_valid_p (tree value, tree endtype, tree *cache)
4160 tree op0, op1;
4162 if (!INTEGRAL_TYPE_P (endtype))
4163 return NULL_TREE;
4165 op0 = TREE_OPERAND (value, 0);
4166 op1 = TREE_OPERAND (value, 1);
4168 /* Like STRIP_NOPS except allow the operand mode to widen. This
4169 works around a feature of fold that simplifies (int)(p1 - p2) to
4170 ((int)p1 - (int)p2) under the theory that the narrower operation
4171 is cheaper. */
4173 while (CONVERT_EXPR_P (op0)
4174 || TREE_CODE (op0) == NON_LVALUE_EXPR)
4176 tree inner = TREE_OPERAND (op0, 0);
4177 if (inner == error_mark_node
4178 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4179 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))
4180 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
4181 break;
4182 op0 = inner;
4185 while (CONVERT_EXPR_P (op1)
4186 || TREE_CODE (op1) == NON_LVALUE_EXPR)
4188 tree inner = TREE_OPERAND (op1, 0);
4189 if (inner == error_mark_node
4190 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4191 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op1)))
4192 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
4193 break;
4194 op1 = inner;
4197 op0 = initializer_constant_valid_p_1 (op0, endtype, cache);
4198 if (!op0)
4199 return NULL_TREE;
4201 op1 = initializer_constant_valid_p_1 (op1, endtype,
4202 cache ? cache + 2 : NULL);
4203 /* Both initializers must be known. */
4204 if (op1)
4206 if (op0 == op1
4207 && (op0 == null_pointer_node
4208 || TREE_CODE (value) == MINUS_EXPR))
4209 return null_pointer_node;
4211 /* Support differences between labels. */
4212 if (TREE_CODE (op0) == LABEL_DECL
4213 && TREE_CODE (op1) == LABEL_DECL)
4214 return null_pointer_node;
4216 if (TREE_CODE (op0) == STRING_CST
4217 && TREE_CODE (op1) == STRING_CST
4218 && operand_equal_p (op0, op1, 1))
4219 return null_pointer_node;
4222 return NULL_TREE;
4225 /* Helper function of initializer_constant_valid_p.
4226 Return nonzero if VALUE is a valid constant-valued expression
4227 for use in initializing a static variable; one that can be an
4228 element of a "constant" initializer.
4230 Return null_pointer_node if the value is absolute;
4231 if it is relocatable, return the variable that determines the relocation.
4232 We assume that VALUE has been folded as much as possible;
4233 therefore, we do not need to check for such things as
4234 arithmetic-combinations of integers.
4236 Use CACHE (pointer to 2 tree values) for caching if non-NULL. */
4238 static tree
4239 initializer_constant_valid_p_1 (tree value, tree endtype, tree *cache)
4241 tree ret;
4243 switch (TREE_CODE (value))
4245 case CONSTRUCTOR:
4246 if (constructor_static_from_elts_p (value))
4248 unsigned HOST_WIDE_INT idx;
4249 tree elt;
4250 bool absolute = true;
4252 if (cache && cache[0] == value)
4253 return cache[1];
4254 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
4256 tree reloc;
4257 reloc = initializer_constant_valid_p_1 (elt, TREE_TYPE (elt),
4258 NULL);
4259 if (!reloc)
4261 if (cache)
4263 cache[0] = value;
4264 cache[1] = NULL_TREE;
4266 return NULL_TREE;
4268 if (reloc != null_pointer_node)
4269 absolute = false;
4271 /* For a non-absolute relocation, there is no single
4272 variable that can be "the variable that determines the
4273 relocation." */
4274 if (cache)
4276 cache[0] = value;
4277 cache[1] = absolute ? null_pointer_node : error_mark_node;
4279 return absolute ? null_pointer_node : error_mark_node;
4282 return TREE_STATIC (value) ? null_pointer_node : NULL_TREE;
4284 case INTEGER_CST:
4285 case VECTOR_CST:
4286 case REAL_CST:
4287 case FIXED_CST:
4288 case STRING_CST:
4289 case COMPLEX_CST:
4290 return null_pointer_node;
4292 case ADDR_EXPR:
4293 case FDESC_EXPR:
4295 tree op0 = staticp (TREE_OPERAND (value, 0));
4296 if (op0)
4298 /* "&(*a).f" is like unto pointer arithmetic. If "a" turns out
4299 to be a constant, this is old-skool offsetof-like nonsense. */
4300 if (TREE_CODE (op0) == INDIRECT_REF
4301 && TREE_CONSTANT (TREE_OPERAND (op0, 0)))
4302 return null_pointer_node;
4303 /* Taking the address of a nested function involves a trampoline,
4304 unless we don't need or want one. */
4305 if (TREE_CODE (op0) == FUNCTION_DECL
4306 && DECL_STATIC_CHAIN (op0)
4307 && !TREE_NO_TRAMPOLINE (value))
4308 return NULL_TREE;
4309 /* "&{...}" requires a temporary to hold the constructed
4310 object. */
4311 if (TREE_CODE (op0) == CONSTRUCTOR)
4312 return NULL_TREE;
4314 return op0;
4317 case NON_LVALUE_EXPR:
4318 return initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4319 endtype, cache);
4321 case VIEW_CONVERT_EXPR:
4323 tree src = TREE_OPERAND (value, 0);
4324 tree src_type = TREE_TYPE (src);
4325 tree dest_type = TREE_TYPE (value);
4327 /* Allow view-conversions from aggregate to non-aggregate type only
4328 if the bit pattern is fully preserved afterwards; otherwise, the
4329 RTL expander won't be able to apply a subsequent transformation
4330 to the underlying constructor. */
4331 if (AGGREGATE_TYPE_P (src_type) && !AGGREGATE_TYPE_P (dest_type))
4333 if (TYPE_MODE (endtype) == TYPE_MODE (dest_type))
4334 return initializer_constant_valid_p_1 (src, endtype, cache);
4335 else
4336 return NULL_TREE;
4339 /* Allow all other kinds of view-conversion. */
4340 return initializer_constant_valid_p_1 (src, endtype, cache);
4343 CASE_CONVERT:
4345 tree src = TREE_OPERAND (value, 0);
4346 tree src_type = TREE_TYPE (src);
4347 tree dest_type = TREE_TYPE (value);
4349 /* Allow conversions between pointer types, floating-point
4350 types, and offset types. */
4351 if ((POINTER_TYPE_P (dest_type) && POINTER_TYPE_P (src_type))
4352 || (FLOAT_TYPE_P (dest_type) && FLOAT_TYPE_P (src_type))
4353 || (TREE_CODE (dest_type) == OFFSET_TYPE
4354 && TREE_CODE (src_type) == OFFSET_TYPE))
4355 return initializer_constant_valid_p_1 (src, endtype, cache);
4357 /* Allow length-preserving conversions between integer types. */
4358 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type)
4359 && (TYPE_PRECISION (dest_type) == TYPE_PRECISION (src_type)))
4360 return initializer_constant_valid_p_1 (src, endtype, cache);
4362 /* Allow conversions between other integer types only if
4363 explicit value. */
4364 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type))
4366 tree inner = initializer_constant_valid_p_1 (src, endtype, cache);
4367 if (inner == null_pointer_node)
4368 return null_pointer_node;
4369 break;
4372 /* Allow (int) &foo provided int is as wide as a pointer. */
4373 if (INTEGRAL_TYPE_P (dest_type) && POINTER_TYPE_P (src_type)
4374 && (TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type)))
4375 return initializer_constant_valid_p_1 (src, endtype, cache);
4377 /* Likewise conversions from int to pointers, but also allow
4378 conversions from 0. */
4379 if ((POINTER_TYPE_P (dest_type)
4380 || TREE_CODE (dest_type) == OFFSET_TYPE)
4381 && INTEGRAL_TYPE_P (src_type))
4383 if (TREE_CODE (src) == INTEGER_CST
4384 && TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type))
4385 return null_pointer_node;
4386 if (integer_zerop (src))
4387 return null_pointer_node;
4388 else if (TYPE_PRECISION (dest_type) <= TYPE_PRECISION (src_type))
4389 return initializer_constant_valid_p_1 (src, endtype, cache);
4392 /* Allow conversions to struct or union types if the value
4393 inside is okay. */
4394 if (TREE_CODE (dest_type) == RECORD_TYPE
4395 || TREE_CODE (dest_type) == UNION_TYPE)
4396 return initializer_constant_valid_p_1 (src, endtype, cache);
4398 break;
4400 case POINTER_PLUS_EXPR:
4401 case PLUS_EXPR:
4402 /* Any valid floating-point constants will have been folded by now;
4403 with -frounding-math we hit this with addition of two constants. */
4404 if (TREE_CODE (endtype) == REAL_TYPE)
4405 return NULL_TREE;
4406 if (cache && cache[0] == value)
4407 return cache[1];
4408 if (! INTEGRAL_TYPE_P (endtype)
4409 || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
4411 tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
4412 tree valid0
4413 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4414 endtype, ncache);
4415 tree valid1
4416 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
4417 endtype, ncache + 2);
4418 /* If either term is absolute, use the other term's relocation. */
4419 if (valid0 == null_pointer_node)
4420 ret = valid1;
4421 else if (valid1 == null_pointer_node)
4422 ret = valid0;
4423 /* Support narrowing pointer differences. */
4424 else
4425 ret = narrowing_initializer_constant_valid_p (value, endtype,
4426 ncache);
4428 else
4429 /* Support narrowing pointer differences. */
4430 ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
4431 if (cache)
4433 cache[0] = value;
4434 cache[1] = ret;
4436 return ret;
4438 case MINUS_EXPR:
4439 if (TREE_CODE (endtype) == REAL_TYPE)
4440 return NULL_TREE;
4441 if (cache && cache[0] == value)
4442 return cache[1];
4443 if (! INTEGRAL_TYPE_P (endtype)
4444 || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
4446 tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
4447 tree valid0
4448 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4449 endtype, ncache);
4450 tree valid1
4451 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
4452 endtype, ncache + 2);
4453 /* Win if second argument is absolute. */
4454 if (valid1 == null_pointer_node)
4455 ret = valid0;
4456 /* Win if both arguments have the same relocation.
4457 Then the value is absolute. */
4458 else if (valid0 == valid1 && valid0 != 0)
4459 ret = null_pointer_node;
4460 /* Since GCC guarantees that string constants are unique in the
4461 generated code, a subtraction between two copies of the same
4462 constant string is absolute. */
4463 else if (valid0 && TREE_CODE (valid0) == STRING_CST
4464 && valid1 && TREE_CODE (valid1) == STRING_CST
4465 && operand_equal_p (valid0, valid1, 1))
4466 ret = null_pointer_node;
4467 /* Support narrowing differences. */
4468 else
4469 ret = narrowing_initializer_constant_valid_p (value, endtype,
4470 ncache);
4472 else
4473 /* Support narrowing differences. */
4474 ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
4475 if (cache)
4477 cache[0] = value;
4478 cache[1] = ret;
4480 return ret;
4482 default:
4483 break;
4486 return NULL_TREE;
4489 /* Return nonzero if VALUE is a valid constant-valued expression
4490 for use in initializing a static variable; one that can be an
4491 element of a "constant" initializer.
4493 Return null_pointer_node if the value is absolute;
4494 if it is relocatable, return the variable that determines the relocation.
4495 We assume that VALUE has been folded as much as possible;
4496 therefore, we do not need to check for such things as
4497 arithmetic-combinations of integers. */
4498 tree
4499 initializer_constant_valid_p (tree value, tree endtype)
4501 return initializer_constant_valid_p_1 (value, endtype, NULL);
4504 /* Return true if VALUE is a valid constant-valued expression
4505 for use in initializing a static bit-field; one that can be
4506 an element of a "constant" initializer. */
4508 bool
4509 initializer_constant_valid_for_bitfield_p (tree value)
4511 /* For bitfields we support integer constants or possibly nested aggregates
4512 of such. */
4513 switch (TREE_CODE (value))
4515 case CONSTRUCTOR:
4517 unsigned HOST_WIDE_INT idx;
4518 tree elt;
4520 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
4521 if (!initializer_constant_valid_for_bitfield_p (elt))
4522 return false;
4523 return true;
4526 case INTEGER_CST:
4527 case REAL_CST:
4528 return true;
4530 case VIEW_CONVERT_EXPR:
4531 case NON_LVALUE_EXPR:
4532 return
4533 initializer_constant_valid_for_bitfield_p (TREE_OPERAND (value, 0));
4535 default:
4536 break;
4539 return false;
4542 /* output_constructor outer state of relevance in recursive calls, typically
4543 for nested aggregate bitfields. */
4545 typedef struct {
4546 unsigned int bit_offset; /* current position in ... */
4547 int byte; /* ... the outer byte buffer. */
4548 } oc_outer_state;
4550 static unsigned HOST_WIDE_INT
4551 output_constructor (tree, unsigned HOST_WIDE_INT, unsigned int,
4552 oc_outer_state *);
4554 /* Output assembler code for constant EXP, with no label.
4555 This includes the pseudo-op such as ".int" or ".byte", and a newline.
4556 Assumes output_addressed_constants has been done on EXP already.
4558 Generate at least SIZE bytes of assembler data, padding at the end
4559 with zeros if necessary. SIZE must always be specified. The returned
4560 value is the actual number of bytes of assembler data generated, which
4561 may be bigger than SIZE if the object contains a variable length field.
4563 SIZE is important for structure constructors,
4564 since trailing members may have been omitted from the constructor.
4565 It is also important for initialization of arrays from string constants
4566 since the full length of the string constant might not be wanted.
4567 It is also needed for initialization of unions, where the initializer's
4568 type is just one member, and that may not be as long as the union.
4570 There a case in which we would fail to output exactly SIZE bytes:
4571 for a structure constructor that wants to produce more than SIZE bytes.
4572 But such constructors will never be generated for any possible input.
4574 ALIGN is the alignment of the data in bits. */
4576 static unsigned HOST_WIDE_INT
4577 output_constant (tree exp, unsigned HOST_WIDE_INT size, unsigned int align)
4579 enum tree_code code;
4580 unsigned HOST_WIDE_INT thissize;
4582 if (size == 0 || flag_syntax_only)
4583 return size;
4585 /* See if we're trying to initialize a pointer in a non-default mode
4586 to the address of some declaration somewhere. If the target says
4587 the mode is valid for pointers, assume the target has a way of
4588 resolving it. */
4589 if (TREE_CODE (exp) == NOP_EXPR
4590 && POINTER_TYPE_P (TREE_TYPE (exp))
4591 && targetm.addr_space.valid_pointer_mode
4592 (TYPE_MODE (TREE_TYPE (exp)),
4593 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
4595 tree saved_type = TREE_TYPE (exp);
4597 /* Peel off any intermediate conversions-to-pointer for valid
4598 pointer modes. */
4599 while (TREE_CODE (exp) == NOP_EXPR
4600 && POINTER_TYPE_P (TREE_TYPE (exp))
4601 && targetm.addr_space.valid_pointer_mode
4602 (TYPE_MODE (TREE_TYPE (exp)),
4603 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
4604 exp = TREE_OPERAND (exp, 0);
4606 /* If what we're left with is the address of something, we can
4607 convert the address to the final type and output it that
4608 way. */
4609 if (TREE_CODE (exp) == ADDR_EXPR)
4610 exp = build1 (ADDR_EXPR, saved_type, TREE_OPERAND (exp, 0));
4611 /* Likewise for constant ints. */
4612 else if (TREE_CODE (exp) == INTEGER_CST)
4613 exp = wide_int_to_tree (saved_type, exp);
4617 /* Eliminate any conversions since we'll be outputting the underlying
4618 constant. */
4619 while (CONVERT_EXPR_P (exp)
4620 || TREE_CODE (exp) == NON_LVALUE_EXPR
4621 || TREE_CODE (exp) == VIEW_CONVERT_EXPR)
4623 HOST_WIDE_INT type_size = int_size_in_bytes (TREE_TYPE (exp));
4624 HOST_WIDE_INT op_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (exp, 0)));
4626 /* Make sure eliminating the conversion is really a no-op, except with
4627 VIEW_CONVERT_EXPRs to allow for wild Ada unchecked conversions and
4628 union types to allow for Ada unchecked unions. */
4629 if (type_size > op_size
4630 && TREE_CODE (exp) != VIEW_CONVERT_EXPR
4631 && TREE_CODE (TREE_TYPE (exp)) != UNION_TYPE)
4632 /* Keep the conversion. */
4633 break;
4634 else
4635 exp = TREE_OPERAND (exp, 0);
4638 code = TREE_CODE (TREE_TYPE (exp));
4639 thissize = int_size_in_bytes (TREE_TYPE (exp));
4641 /* Allow a constructor with no elements for any data type.
4642 This means to fill the space with zeros. */
4643 if (TREE_CODE (exp) == CONSTRUCTOR
4644 && vec_safe_is_empty (CONSTRUCTOR_ELTS (exp)))
4646 assemble_zeros (size);
4647 return size;
4650 if (TREE_CODE (exp) == FDESC_EXPR)
4652 #ifdef ASM_OUTPUT_FDESC
4653 HOST_WIDE_INT part = tree_to_shwi (TREE_OPERAND (exp, 1));
4654 tree decl = TREE_OPERAND (exp, 0);
4655 ASM_OUTPUT_FDESC (asm_out_file, decl, part);
4656 #else
4657 gcc_unreachable ();
4658 #endif
4659 return size;
4662 /* Now output the underlying data. If we've handling the padding, return.
4663 Otherwise, break and ensure SIZE is the size written. */
4664 switch (code)
4666 case BOOLEAN_TYPE:
4667 case INTEGER_TYPE:
4668 case ENUMERAL_TYPE:
4669 case POINTER_TYPE:
4670 case REFERENCE_TYPE:
4671 case OFFSET_TYPE:
4672 case FIXED_POINT_TYPE:
4673 case NULLPTR_TYPE:
4674 if (! assemble_integer (expand_expr (exp, NULL_RTX, VOIDmode,
4675 EXPAND_INITIALIZER),
4676 MIN (size, thissize), align, 0))
4677 error ("initializer for integer/fixed-point value is too complicated");
4678 break;
4680 case REAL_TYPE:
4681 if (TREE_CODE (exp) != REAL_CST)
4682 error ("initializer for floating value is not a floating constant");
4683 else
4684 assemble_real (TREE_REAL_CST (exp), TYPE_MODE (TREE_TYPE (exp)), align);
4685 break;
4687 case COMPLEX_TYPE:
4688 output_constant (TREE_REALPART (exp), thissize / 2, align);
4689 output_constant (TREE_IMAGPART (exp), thissize / 2,
4690 min_align (align, BITS_PER_UNIT * (thissize / 2)));
4691 break;
4693 case ARRAY_TYPE:
4694 case VECTOR_TYPE:
4695 switch (TREE_CODE (exp))
4697 case CONSTRUCTOR:
4698 return output_constructor (exp, size, align, NULL);
4699 case STRING_CST:
4700 thissize
4701 = MIN ((unsigned HOST_WIDE_INT)TREE_STRING_LENGTH (exp), size);
4702 assemble_string (TREE_STRING_POINTER (exp), thissize);
4703 break;
4704 case VECTOR_CST:
4706 enum machine_mode inner = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
4707 unsigned int nalign = MIN (align, GET_MODE_ALIGNMENT (inner));
4708 int elt_size = GET_MODE_SIZE (inner);
4709 output_constant (VECTOR_CST_ELT (exp, 0), elt_size, align);
4710 thissize = elt_size;
4711 for (unsigned int i = 1; i < VECTOR_CST_NELTS (exp); i++)
4713 output_constant (VECTOR_CST_ELT (exp, i), elt_size, nalign);
4714 thissize += elt_size;
4716 break;
4718 default:
4719 gcc_unreachable ();
4721 break;
4723 case RECORD_TYPE:
4724 case UNION_TYPE:
4725 gcc_assert (TREE_CODE (exp) == CONSTRUCTOR);
4726 return output_constructor (exp, size, align, NULL);
4728 case ERROR_MARK:
4729 return 0;
4731 default:
4732 gcc_unreachable ();
4735 if (size > thissize)
4736 assemble_zeros (size - thissize);
4738 return size;
4742 /* Subroutine of output_constructor, used for computing the size of
4743 arrays of unspecified length. VAL must be a CONSTRUCTOR of an array
4744 type with an unspecified upper bound. */
4746 static unsigned HOST_WIDE_INT
4747 array_size_for_constructor (tree val)
4749 tree max_index;
4750 unsigned HOST_WIDE_INT cnt;
4751 tree index, value, tmp;
4752 offset_int i;
4754 /* This code used to attempt to handle string constants that are not
4755 arrays of single-bytes, but nothing else does, so there's no point in
4756 doing it here. */
4757 if (TREE_CODE (val) == STRING_CST)
4758 return TREE_STRING_LENGTH (val);
4760 max_index = NULL_TREE;
4761 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (val), cnt, index, value)
4763 if (TREE_CODE (index) == RANGE_EXPR)
4764 index = TREE_OPERAND (index, 1);
4765 if (max_index == NULL_TREE || tree_int_cst_lt (max_index, index))
4766 max_index = index;
4769 if (max_index == NULL_TREE)
4770 return 0;
4772 /* Compute the total number of array elements. */
4773 tmp = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (val)));
4774 i = wi::to_offset (max_index) - wi::to_offset (tmp) + 1;
4776 /* Multiply by the array element unit size to find number of bytes. */
4777 i *= wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (val))));
4779 gcc_assert (wi::fits_uhwi_p (i));
4780 return i.to_uhwi ();
4783 /* Other datastructures + helpers for output_constructor. */
4785 /* output_constructor local state to support interaction with helpers. */
4787 typedef struct {
4789 /* Received arguments. */
4790 tree exp; /* Constructor expression. */
4791 tree type; /* Type of constructor expression. */
4792 unsigned HOST_WIDE_INT size; /* # bytes to output - pad if necessary. */
4793 unsigned int align; /* Known initial alignment. */
4794 tree min_index; /* Lower bound if specified for an array. */
4796 /* Output processing state. */
4797 HOST_WIDE_INT total_bytes; /* # bytes output so far / current position. */
4798 int byte; /* Part of a bitfield byte yet to be output. */
4799 int last_relative_index; /* Implicit or explicit index of the last
4800 array element output within a bitfield. */
4801 bool byte_buffer_in_use; /* Whether BYTE is in use. */
4803 /* Current element. */
4804 tree field; /* Current field decl in a record. */
4805 tree val; /* Current element value. */
4806 tree index; /* Current element index. */
4808 } oc_local_state;
4810 /* Helper for output_constructor. From the current LOCAL state, output a
4811 RANGE_EXPR element. */
4813 static void
4814 output_constructor_array_range (oc_local_state *local)
4816 unsigned HOST_WIDE_INT fieldsize
4817 = int_size_in_bytes (TREE_TYPE (local->type));
4819 HOST_WIDE_INT lo_index
4820 = tree_to_shwi (TREE_OPERAND (local->index, 0));
4821 HOST_WIDE_INT hi_index
4822 = tree_to_shwi (TREE_OPERAND (local->index, 1));
4823 HOST_WIDE_INT index;
4825 unsigned int align2
4826 = min_align (local->align, fieldsize * BITS_PER_UNIT);
4828 for (index = lo_index; index <= hi_index; index++)
4830 /* Output the element's initial value. */
4831 if (local->val == NULL_TREE)
4832 assemble_zeros (fieldsize);
4833 else
4834 fieldsize = output_constant (local->val, fieldsize, align2);
4836 /* Count its size. */
4837 local->total_bytes += fieldsize;
4841 /* Helper for output_constructor. From the current LOCAL state, output a
4842 field element that is not true bitfield or part of an outer one. */
4844 static void
4845 output_constructor_regular_field (oc_local_state *local)
4847 /* Field size and position. Since this structure is static, we know the
4848 positions are constant. */
4849 unsigned HOST_WIDE_INT fieldsize;
4850 HOST_WIDE_INT fieldpos;
4852 unsigned int align2;
4854 if (local->index != NULL_TREE)
4856 /* Perform the index calculation in modulo arithmetic but
4857 sign-extend the result because Ada has negative DECL_FIELD_OFFSETs
4858 but we are using an unsigned sizetype. */
4859 unsigned prec = TYPE_PRECISION (sizetype);
4860 offset_int idx = wi::sext (wi::to_offset (local->index)
4861 - wi::to_offset (local->min_index), prec);
4862 fieldpos = (idx * wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (local->val))))
4863 .to_short_addr ();
4865 else if (local->field != NULL_TREE)
4866 fieldpos = int_byte_position (local->field);
4867 else
4868 fieldpos = 0;
4870 /* Output any buffered-up bit-fields preceding this element. */
4871 if (local->byte_buffer_in_use)
4873 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
4874 local->total_bytes++;
4875 local->byte_buffer_in_use = false;
4878 /* Advance to offset of this element.
4879 Note no alignment needed in an array, since that is guaranteed
4880 if each element has the proper size. */
4881 if ((local->field != NULL_TREE || local->index != NULL_TREE)
4882 && fieldpos > local->total_bytes)
4884 assemble_zeros (fieldpos - local->total_bytes);
4885 local->total_bytes = fieldpos;
4888 /* Find the alignment of this element. */
4889 align2 = min_align (local->align, BITS_PER_UNIT * fieldpos);
4891 /* Determine size this element should occupy. */
4892 if (local->field)
4894 fieldsize = 0;
4896 /* If this is an array with an unspecified upper bound,
4897 the initializer determines the size. */
4898 /* ??? This ought to only checked if DECL_SIZE_UNIT is NULL,
4899 but we cannot do this until the deprecated support for
4900 initializing zero-length array members is removed. */
4901 if (TREE_CODE (TREE_TYPE (local->field)) == ARRAY_TYPE
4902 && TYPE_DOMAIN (TREE_TYPE (local->field))
4903 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (local->field))))
4905 fieldsize = array_size_for_constructor (local->val);
4906 /* Given a non-empty initialization, this field had
4907 better be last. */
4908 gcc_assert (!fieldsize || !DECL_CHAIN (local->field));
4910 else
4911 fieldsize = tree_to_uhwi (DECL_SIZE_UNIT (local->field));
4913 else
4914 fieldsize = int_size_in_bytes (TREE_TYPE (local->type));
4916 /* Output the element's initial value. */
4917 if (local->val == NULL_TREE)
4918 assemble_zeros (fieldsize);
4919 else
4920 fieldsize = output_constant (local->val, fieldsize, align2);
4922 /* Count its size. */
4923 local->total_bytes += fieldsize;
4926 /* Helper for output_constructor. From the LOCAL state, output an element
4927 that is a true bitfield or part of an outer one. BIT_OFFSET is the offset
4928 from the start of a possibly ongoing outer byte buffer. */
4930 static void
4931 output_constructor_bitfield (oc_local_state *local, unsigned int bit_offset)
4933 /* Bit size of this element. */
4934 HOST_WIDE_INT ebitsize
4935 = (local->field
4936 ? tree_to_uhwi (DECL_SIZE (local->field))
4937 : tree_to_uhwi (TYPE_SIZE (TREE_TYPE (local->type))));
4939 /* Relative index of this element if this is an array component. */
4940 HOST_WIDE_INT relative_index
4941 = (!local->field
4942 ? (local->index
4943 ? (tree_to_shwi (local->index)
4944 - tree_to_shwi (local->min_index))
4945 : local->last_relative_index + 1)
4946 : 0);
4948 /* Bit position of this element from the start of the containing
4949 constructor. */
4950 HOST_WIDE_INT constructor_relative_ebitpos
4951 = (local->field
4952 ? int_bit_position (local->field)
4953 : ebitsize * relative_index);
4955 /* Bit position of this element from the start of a possibly ongoing
4956 outer byte buffer. */
4957 HOST_WIDE_INT byte_relative_ebitpos
4958 = bit_offset + constructor_relative_ebitpos;
4960 /* From the start of a possibly ongoing outer byte buffer, offsets to
4961 the first bit of this element and to the first bit past the end of
4962 this element. */
4963 HOST_WIDE_INT next_offset = byte_relative_ebitpos;
4964 HOST_WIDE_INT end_offset = byte_relative_ebitpos + ebitsize;
4966 local->last_relative_index = relative_index;
4968 if (local->val == NULL_TREE)
4969 local->val = integer_zero_node;
4971 while (TREE_CODE (local->val) == VIEW_CONVERT_EXPR
4972 || TREE_CODE (local->val) == NON_LVALUE_EXPR)
4973 local->val = TREE_OPERAND (local->val, 0);
4975 if (TREE_CODE (local->val) != INTEGER_CST
4976 && TREE_CODE (local->val) != CONSTRUCTOR)
4978 error ("invalid initial value for member %qE", DECL_NAME (local->field));
4979 return;
4982 /* If this field does not start in this (or next) byte, skip some bytes. */
4983 if (next_offset / BITS_PER_UNIT != local->total_bytes)
4985 /* Output remnant of any bit field in previous bytes. */
4986 if (local->byte_buffer_in_use)
4988 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
4989 local->total_bytes++;
4990 local->byte_buffer_in_use = false;
4993 /* If still not at proper byte, advance to there. */
4994 if (next_offset / BITS_PER_UNIT != local->total_bytes)
4996 gcc_assert (next_offset / BITS_PER_UNIT >= local->total_bytes);
4997 assemble_zeros (next_offset / BITS_PER_UNIT - local->total_bytes);
4998 local->total_bytes = next_offset / BITS_PER_UNIT;
5002 /* Set up the buffer if necessary. */
5003 if (!local->byte_buffer_in_use)
5005 local->byte = 0;
5006 if (ebitsize > 0)
5007 local->byte_buffer_in_use = true;
5010 /* If this is nested constructor, recurse passing the bit offset and the
5011 pending data, then retrieve the new pending data afterwards. */
5012 if (TREE_CODE (local->val) == CONSTRUCTOR)
5014 oc_outer_state temp_state;
5015 temp_state.bit_offset = next_offset % BITS_PER_UNIT;
5016 temp_state.byte = local->byte;
5017 local->total_bytes
5018 += output_constructor (local->val, 0, 0, &temp_state);
5019 local->byte = temp_state.byte;
5020 return;
5023 /* Otherwise, we must split the element into pieces that fall within
5024 separate bytes, and combine each byte with previous or following
5025 bit-fields. */
5026 while (next_offset < end_offset)
5028 int this_time;
5029 int shift;
5030 HOST_WIDE_INT value;
5031 HOST_WIDE_INT next_byte = next_offset / BITS_PER_UNIT;
5032 HOST_WIDE_INT next_bit = next_offset % BITS_PER_UNIT;
5034 /* Advance from byte to byte within this element when necessary. */
5035 while (next_byte != local->total_bytes)
5037 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5038 local->total_bytes++;
5039 local->byte = 0;
5042 /* Number of bits we can process at once (all part of the same byte). */
5043 this_time = MIN (end_offset - next_offset, BITS_PER_UNIT - next_bit);
5044 if (BYTES_BIG_ENDIAN)
5046 /* On big-endian machine, take the most significant bits (of the
5047 bits that are significant) first and put them into bytes from
5048 the most significant end. */
5049 shift = end_offset - next_offset - this_time;
5051 /* Don't try to take a bunch of bits that cross
5052 the word boundary in the INTEGER_CST. We can
5053 only select bits from one element. */
5054 if ((shift / HOST_BITS_PER_WIDE_INT)
5055 != ((shift + this_time - 1) / HOST_BITS_PER_WIDE_INT))
5057 const int end = shift + this_time - 1;
5058 shift = end & -HOST_BITS_PER_WIDE_INT;
5059 this_time = end - shift + 1;
5062 /* Now get the bits from the appropriate constant word. */
5063 value = TREE_INT_CST_ELT (local->val, shift / HOST_BITS_PER_WIDE_INT);
5064 shift = shift & (HOST_BITS_PER_WIDE_INT - 1);
5066 /* Get the result. This works only when:
5067 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
5068 local->byte |= (((value >> shift)
5069 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
5070 << (BITS_PER_UNIT - this_time - next_bit));
5072 else
5074 /* On little-endian machines, take the least significant bits of
5075 the value first and pack them starting at the least significant
5076 bits of the bytes. */
5077 shift = next_offset - byte_relative_ebitpos;
5079 /* Don't try to take a bunch of bits that cross
5080 the word boundary in the INTEGER_CST. We can
5081 only select bits from one element. */
5082 if ((shift / HOST_BITS_PER_WIDE_INT)
5083 != ((shift + this_time - 1) / HOST_BITS_PER_WIDE_INT))
5084 this_time
5085 = HOST_BITS_PER_WIDE_INT - (shift & (HOST_BITS_PER_WIDE_INT - 1));
5087 /* Now get the bits from the appropriate constant word. */
5088 value = TREE_INT_CST_ELT (local->val, shift / HOST_BITS_PER_WIDE_INT);
5089 shift = shift & (HOST_BITS_PER_WIDE_INT - 1);
5091 /* Get the result. This works only when:
5092 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
5093 local->byte |= (((value >> shift)
5094 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
5095 << next_bit);
5098 next_offset += this_time;
5099 local->byte_buffer_in_use = true;
5103 /* Subroutine of output_constant, used for CONSTRUCTORs (aggregate constants).
5104 Generate at least SIZE bytes, padding if necessary. OUTER designates the
5105 caller output state of relevance in recursive invocations. */
5107 static unsigned HOST_WIDE_INT
5108 output_constructor (tree exp, unsigned HOST_WIDE_INT size,
5109 unsigned int align, oc_outer_state *outer)
5111 unsigned HOST_WIDE_INT cnt;
5112 constructor_elt *ce;
5114 oc_local_state local;
5116 /* Setup our local state to communicate with helpers. */
5117 local.exp = exp;
5118 local.type = TREE_TYPE (exp);
5119 local.size = size;
5120 local.align = align;
5121 if (TREE_CODE (local.type) == ARRAY_TYPE && TYPE_DOMAIN (local.type))
5122 local.min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (local.type));
5123 else
5124 local.min_index = NULL_TREE;
5126 local.total_bytes = 0;
5127 local.byte_buffer_in_use = outer != NULL;
5128 local.byte = outer ? outer->byte : 0;
5129 local.last_relative_index = -1;
5131 gcc_assert (HOST_BITS_PER_WIDE_INT >= BITS_PER_UNIT);
5133 /* As CE goes through the elements of the constant, FIELD goes through the
5134 structure fields if the constant is a structure. If the constant is a
5135 union, we override this by getting the field from the TREE_LIST element.
5136 But the constant could also be an array. Then FIELD is zero.
5138 There is always a maximum of one element in the chain LINK for unions
5139 (even if the initializer in a source program incorrectly contains
5140 more one). */
5142 if (TREE_CODE (local.type) == RECORD_TYPE)
5143 local.field = TYPE_FIELDS (local.type);
5144 else
5145 local.field = NULL_TREE;
5147 for (cnt = 0;
5148 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), cnt, &ce);
5149 cnt++, local.field = local.field ? DECL_CHAIN (local.field) : 0)
5151 local.val = ce->value;
5152 local.index = NULL_TREE;
5154 /* The element in a union constructor specifies the proper field
5155 or index. */
5156 if (RECORD_OR_UNION_TYPE_P (local.type) && ce->index != NULL_TREE)
5157 local.field = ce->index;
5159 else if (TREE_CODE (local.type) == ARRAY_TYPE)
5160 local.index = ce->index;
5162 if (local.field && flag_verbose_asm)
5163 fprintf (asm_out_file, "%s %s:\n",
5164 ASM_COMMENT_START,
5165 DECL_NAME (local.field)
5166 ? IDENTIFIER_POINTER (DECL_NAME (local.field))
5167 : "<anonymous>");
5169 /* Eliminate the marker that makes a cast not be an lvalue. */
5170 if (local.val != NULL_TREE)
5171 STRIP_NOPS (local.val);
5173 /* Output the current element, using the appropriate helper ... */
5175 /* For an array slice not part of an outer bitfield. */
5176 if (!outer
5177 && local.index != NULL_TREE
5178 && TREE_CODE (local.index) == RANGE_EXPR)
5179 output_constructor_array_range (&local);
5181 /* For a field that is neither a true bitfield nor part of an outer one,
5182 known to be at least byte aligned and multiple-of-bytes long. */
5183 else if (!outer
5184 && (local.field == NULL_TREE
5185 || !CONSTRUCTOR_BITFIELD_P (local.field)))
5186 output_constructor_regular_field (&local);
5188 /* For a true bitfield or part of an outer one. Only INTEGER_CSTs are
5189 supported for scalar fields, so we may need to convert first. */
5190 else
5192 if (TREE_CODE (local.val) == REAL_CST)
5193 local.val
5194 = fold_unary (VIEW_CONVERT_EXPR,
5195 build_nonstandard_integer_type
5196 (TYPE_PRECISION (TREE_TYPE (local.val)), 0),
5197 local.val);
5198 output_constructor_bitfield (&local, outer ? outer->bit_offset : 0);
5202 /* If we are not at toplevel, save the pending data for our caller.
5203 Otherwise output the pending data and padding zeros as needed. */
5204 if (outer)
5205 outer->byte = local.byte;
5206 else
5208 if (local.byte_buffer_in_use)
5210 assemble_integer (GEN_INT (local.byte), 1, BITS_PER_UNIT, 1);
5211 local.total_bytes++;
5214 if ((unsigned HOST_WIDE_INT)local.total_bytes < local.size)
5216 assemble_zeros (local.size - local.total_bytes);
5217 local.total_bytes = local.size;
5221 return local.total_bytes;
5224 /* Mark DECL as weak. */
5226 static void
5227 mark_weak (tree decl)
5229 if (DECL_WEAK (decl))
5230 return;
5232 struct symtab_node *n = symtab_node::get (decl);
5233 if (n && n->refuse_visibility_changes)
5234 error ("%+D declared weak after being used", decl);
5235 DECL_WEAK (decl) = 1;
5237 if (DECL_RTL_SET_P (decl)
5238 && MEM_P (DECL_RTL (decl))
5239 && XEXP (DECL_RTL (decl), 0)
5240 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == SYMBOL_REF)
5241 SYMBOL_REF_WEAK (XEXP (DECL_RTL (decl), 0)) = 1;
5244 /* Merge weak status between NEWDECL and OLDDECL. */
5246 void
5247 merge_weak (tree newdecl, tree olddecl)
5249 if (DECL_WEAK (newdecl) == DECL_WEAK (olddecl))
5251 if (DECL_WEAK (newdecl) && TARGET_SUPPORTS_WEAK)
5253 tree *pwd;
5254 /* We put the NEWDECL on the weak_decls list at some point
5255 and OLDDECL as well. Keep just OLDDECL on the list. */
5256 for (pwd = &weak_decls; *pwd; pwd = &TREE_CHAIN (*pwd))
5257 if (TREE_VALUE (*pwd) == newdecl)
5259 *pwd = TREE_CHAIN (*pwd);
5260 break;
5263 return;
5266 if (DECL_WEAK (newdecl))
5268 tree wd;
5270 /* NEWDECL is weak, but OLDDECL is not. */
5272 /* If we already output the OLDDECL, we're in trouble; we can't
5273 go back and make it weak. This should never happen in
5274 unit-at-a-time compilation. */
5275 gcc_assert (!TREE_ASM_WRITTEN (olddecl));
5277 /* If we've already generated rtl referencing OLDDECL, we may
5278 have done so in a way that will not function properly with
5279 a weak symbol. Again in unit-at-a-time this should be
5280 impossible. */
5281 gcc_assert (!TREE_USED (olddecl)
5282 || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (olddecl)));
5284 if (TARGET_SUPPORTS_WEAK)
5286 /* We put the NEWDECL on the weak_decls list at some point.
5287 Replace it with the OLDDECL. */
5288 for (wd = weak_decls; wd; wd = TREE_CHAIN (wd))
5289 if (TREE_VALUE (wd) == newdecl)
5291 TREE_VALUE (wd) = olddecl;
5292 break;
5294 /* We may not find the entry on the list. If NEWDECL is a
5295 weak alias, then we will have already called
5296 globalize_decl to remove the entry; in that case, we do
5297 not need to do anything. */
5300 /* Make the OLDDECL weak; it's OLDDECL that we'll be keeping. */
5301 mark_weak (olddecl);
5303 else
5304 /* OLDDECL was weak, but NEWDECL was not explicitly marked as
5305 weak. Just update NEWDECL to indicate that it's weak too. */
5306 mark_weak (newdecl);
5309 /* Declare DECL to be a weak symbol. */
5311 void
5312 declare_weak (tree decl)
5314 gcc_assert (TREE_CODE (decl) != FUNCTION_DECL || !TREE_ASM_WRITTEN (decl));
5315 if (! TREE_PUBLIC (decl))
5316 error ("weak declaration of %q+D must be public", decl);
5317 else if (!TARGET_SUPPORTS_WEAK)
5318 warning (0, "weak declaration of %q+D not supported", decl);
5320 mark_weak (decl);
5321 if (!lookup_attribute ("weak", DECL_ATTRIBUTES (decl)))
5322 DECL_ATTRIBUTES (decl)
5323 = tree_cons (get_identifier ("weak"), NULL, DECL_ATTRIBUTES (decl));
5326 static void
5327 weak_finish_1 (tree decl)
5329 #if defined (ASM_WEAKEN_DECL) || defined (ASM_WEAKEN_LABEL)
5330 const char *const name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5331 #endif
5333 if (! TREE_USED (decl))
5334 return;
5336 #ifdef ASM_WEAKEN_DECL
5337 ASM_WEAKEN_DECL (asm_out_file, decl, name, NULL);
5338 #else
5339 #ifdef ASM_WEAKEN_LABEL
5340 ASM_WEAKEN_LABEL (asm_out_file, name);
5341 #else
5342 #ifdef ASM_OUTPUT_WEAK_ALIAS
5344 static bool warn_once = 0;
5345 if (! warn_once)
5347 warning (0, "only weak aliases are supported in this configuration");
5348 warn_once = 1;
5350 return;
5352 #endif
5353 #endif
5354 #endif
5357 /* Fiven an assembly name, find the decl it is associated with. */
5358 static tree
5359 find_decl (tree target)
5361 symtab_node *node = symtab_node::get_for_asmname (target);
5362 if (node)
5363 return node->decl;
5364 return NULL_TREE;
5367 /* This TREE_LIST contains weakref targets. */
5369 static GTY(()) tree weakref_targets;
5371 /* Emit any pending weak declarations. */
5373 void
5374 weak_finish (void)
5376 tree t;
5378 for (t = weakref_targets; t; t = TREE_CHAIN (t))
5380 tree alias_decl = TREE_PURPOSE (t);
5381 tree target = ultimate_transparent_alias_target (&TREE_VALUE (t));
5383 if (! TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (alias_decl)))
5384 /* Remove alias_decl from the weak list, but leave entries for
5385 the target alone. */
5386 target = NULL_TREE;
5387 #ifndef ASM_OUTPUT_WEAKREF
5388 else if (! TREE_SYMBOL_REFERENCED (target))
5390 /* Use ASM_WEAKEN_LABEL only if ASM_WEAKEN_DECL is not
5391 defined, otherwise we and weak_finish_1 would use
5392 different macros. */
5393 # if defined ASM_WEAKEN_LABEL && ! defined ASM_WEAKEN_DECL
5394 ASM_WEAKEN_LABEL (asm_out_file, IDENTIFIER_POINTER (target));
5395 # else
5396 tree decl = find_decl (target);
5398 if (! decl)
5400 decl = build_decl (DECL_SOURCE_LOCATION (alias_decl),
5401 TREE_CODE (alias_decl), target,
5402 TREE_TYPE (alias_decl));
5404 DECL_EXTERNAL (decl) = 1;
5405 TREE_PUBLIC (decl) = 1;
5406 DECL_ARTIFICIAL (decl) = 1;
5407 TREE_NOTHROW (decl) = TREE_NOTHROW (alias_decl);
5408 TREE_USED (decl) = 1;
5411 weak_finish_1 (decl);
5412 # endif
5414 #endif
5417 tree *p;
5418 tree t2;
5420 /* Remove the alias and the target from the pending weak list
5421 so that we do not emit any .weak directives for the former,
5422 nor multiple .weak directives for the latter. */
5423 for (p = &weak_decls; (t2 = *p) ; )
5425 if (TREE_VALUE (t2) == alias_decl
5426 || target == DECL_ASSEMBLER_NAME (TREE_VALUE (t2)))
5427 *p = TREE_CHAIN (t2);
5428 else
5429 p = &TREE_CHAIN (t2);
5432 /* Remove other weakrefs to the same target, to speed things up. */
5433 for (p = &TREE_CHAIN (t); (t2 = *p) ; )
5435 if (target == ultimate_transparent_alias_target (&TREE_VALUE (t2)))
5436 *p = TREE_CHAIN (t2);
5437 else
5438 p = &TREE_CHAIN (t2);
5443 for (t = weak_decls; t; t = TREE_CHAIN (t))
5445 tree decl = TREE_VALUE (t);
5447 weak_finish_1 (decl);
5451 /* Emit the assembly bits to indicate that DECL is globally visible. */
5453 static void
5454 globalize_decl (tree decl)
5457 #if defined (ASM_WEAKEN_LABEL) || defined (ASM_WEAKEN_DECL)
5458 if (DECL_WEAK (decl))
5460 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
5461 tree *p, t;
5463 #ifdef ASM_WEAKEN_DECL
5464 ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
5465 #else
5466 ASM_WEAKEN_LABEL (asm_out_file, name);
5467 #endif
5469 /* Remove this function from the pending weak list so that
5470 we do not emit multiple .weak directives for it. */
5471 for (p = &weak_decls; (t = *p) ; )
5473 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
5474 *p = TREE_CHAIN (t);
5475 else
5476 p = &TREE_CHAIN (t);
5479 /* Remove weakrefs to the same target from the pending weakref
5480 list, for the same reason. */
5481 for (p = &weakref_targets; (t = *p) ; )
5483 if (DECL_ASSEMBLER_NAME (decl)
5484 == ultimate_transparent_alias_target (&TREE_VALUE (t)))
5485 *p = TREE_CHAIN (t);
5486 else
5487 p = &TREE_CHAIN (t);
5490 return;
5492 #endif
5494 targetm.asm_out.globalize_decl_name (asm_out_file, decl);
5497 vec<alias_pair, va_gc> *alias_pairs;
5499 /* Output the assembler code for a define (equate) using ASM_OUTPUT_DEF
5500 or ASM_OUTPUT_DEF_FROM_DECLS. The function defines the symbol whose
5501 tree node is DECL to have the value of the tree node TARGET. */
5503 void
5504 do_assemble_alias (tree decl, tree target)
5506 /* Emulated TLS had better not get this var. */
5507 gcc_assert (!(!targetm.have_tls
5508 && TREE_CODE (decl) == VAR_DECL
5509 && DECL_THREAD_LOCAL_P (decl)));
5511 if (TREE_ASM_WRITTEN (decl))
5512 return;
5514 /* We must force creation of DECL_RTL for debug info generation, even though
5515 we don't use it here. */
5516 make_decl_rtl (decl);
5518 TREE_ASM_WRITTEN (decl) = 1;
5519 TREE_ASM_WRITTEN (DECL_ASSEMBLER_NAME (decl)) = 1;
5521 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
5523 ultimate_transparent_alias_target (&target);
5525 if (!TREE_SYMBOL_REFERENCED (target))
5526 weakref_targets = tree_cons (decl, target, weakref_targets);
5528 #ifdef ASM_OUTPUT_WEAKREF
5529 ASM_OUTPUT_WEAKREF (asm_out_file, decl,
5530 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
5531 IDENTIFIER_POINTER (target));
5532 #else
5533 if (!TARGET_SUPPORTS_WEAK)
5535 error_at (DECL_SOURCE_LOCATION (decl),
5536 "weakref is not supported in this configuration");
5537 return;
5539 #endif
5540 return;
5543 #ifdef ASM_OUTPUT_DEF
5544 /* Make name accessible from other files, if appropriate. */
5546 if (TREE_PUBLIC (decl))
5548 globalize_decl (decl);
5549 maybe_assemble_visibility (decl);
5551 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
5553 #if defined (ASM_OUTPUT_TYPE_DIRECTIVE)
5554 if (targetm.has_ifunc_p ())
5555 ASM_OUTPUT_TYPE_DIRECTIVE
5556 (asm_out_file, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
5557 IFUNC_ASM_TYPE);
5558 else
5559 #endif
5560 error_at (DECL_SOURCE_LOCATION (decl),
5561 "ifunc is not supported on this target");
5564 # ifdef ASM_OUTPUT_DEF_FROM_DECLS
5565 ASM_OUTPUT_DEF_FROM_DECLS (asm_out_file, decl, target);
5566 # else
5567 ASM_OUTPUT_DEF (asm_out_file,
5568 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
5569 IDENTIFIER_POINTER (target));
5570 # endif
5571 #elif defined (ASM_OUTPUT_WEAK_ALIAS) || defined (ASM_WEAKEN_DECL)
5573 const char *name;
5574 tree *p, t;
5576 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5577 # ifdef ASM_WEAKEN_DECL
5578 ASM_WEAKEN_DECL (asm_out_file, decl, name, IDENTIFIER_POINTER (target));
5579 # else
5580 ASM_OUTPUT_WEAK_ALIAS (asm_out_file, name, IDENTIFIER_POINTER (target));
5581 # endif
5582 /* Remove this function from the pending weak list so that
5583 we do not emit multiple .weak directives for it. */
5584 for (p = &weak_decls; (t = *p) ; )
5585 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
5586 *p = TREE_CHAIN (t);
5587 else
5588 p = &TREE_CHAIN (t);
5590 /* Remove weakrefs to the same target from the pending weakref
5591 list, for the same reason. */
5592 for (p = &weakref_targets; (t = *p) ; )
5594 if (DECL_ASSEMBLER_NAME (decl)
5595 == ultimate_transparent_alias_target (&TREE_VALUE (t)))
5596 *p = TREE_CHAIN (t);
5597 else
5598 p = &TREE_CHAIN (t);
5601 #endif
5604 /* Emit an assembler directive to make the symbol for DECL an alias to
5605 the symbol for TARGET. */
5607 void
5608 assemble_alias (tree decl, tree target)
5610 tree target_decl;
5612 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
5614 tree alias = DECL_ASSEMBLER_NAME (decl);
5616 ultimate_transparent_alias_target (&target);
5618 if (alias == target)
5619 error ("weakref %q+D ultimately targets itself", decl);
5620 if (TREE_PUBLIC (decl))
5621 error ("weakref %q+D must have static linkage", decl);
5623 else
5625 #if !defined (ASM_OUTPUT_DEF)
5626 # if !defined(ASM_OUTPUT_WEAK_ALIAS) && !defined (ASM_WEAKEN_DECL)
5627 error_at (DECL_SOURCE_LOCATION (decl),
5628 "alias definitions not supported in this configuration");
5629 TREE_ASM_WRITTEN (decl) = 1;
5630 return;
5631 # else
5632 if (!DECL_WEAK (decl))
5634 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
5635 error_at (DECL_SOURCE_LOCATION (decl),
5636 "ifunc is not supported in this configuration");
5637 else
5638 error_at (DECL_SOURCE_LOCATION (decl),
5639 "only weak aliases are supported in this configuration");
5640 TREE_ASM_WRITTEN (decl) = 1;
5641 return;
5643 # endif
5644 #endif
5646 TREE_USED (decl) = 1;
5648 /* Allow aliases to aliases. */
5649 if (TREE_CODE (decl) == FUNCTION_DECL)
5650 cgraph_node::get_create (decl)->alias = true;
5651 else
5652 varpool_node::get_create (decl)->alias = true;
5654 /* If the target has already been emitted, we don't have to queue the
5655 alias. This saves a tad of memory. */
5656 if (symtab->global_info_ready)
5657 target_decl = find_decl (target);
5658 else
5659 target_decl= NULL;
5660 if ((target_decl && TREE_ASM_WRITTEN (target_decl))
5661 || symtab->state >= EXPANSION)
5662 do_assemble_alias (decl, target);
5663 else
5665 alias_pair p = {decl, target};
5666 vec_safe_push (alias_pairs, p);
5670 /* Record and output a table of translations from original function
5671 to its transaction aware clone. Note that tm_pure functions are
5672 considered to be their own clone. */
5674 static GTY((if_marked ("tree_map_marked_p"), param_is (struct tree_map)))
5675 htab_t tm_clone_hash;
5677 void
5678 record_tm_clone_pair (tree o, tree n)
5680 struct tree_map **slot, *h;
5682 if (tm_clone_hash == NULL)
5683 tm_clone_hash = htab_create_ggc (32, tree_map_hash, tree_map_eq, 0);
5685 h = ggc_alloc<tree_map> ();
5686 h->hash = htab_hash_pointer (o);
5687 h->base.from = o;
5688 h->to = n;
5690 slot = (struct tree_map **)
5691 htab_find_slot_with_hash (tm_clone_hash, h, h->hash, INSERT);
5692 *slot = h;
5695 tree
5696 get_tm_clone_pair (tree o)
5698 if (tm_clone_hash)
5700 struct tree_map *h, in;
5702 in.base.from = o;
5703 in.hash = htab_hash_pointer (o);
5704 h = (struct tree_map *) htab_find_with_hash (tm_clone_hash,
5705 &in, in.hash);
5706 if (h)
5707 return h->to;
5709 return NULL_TREE;
5712 typedef struct tm_alias_pair
5714 unsigned int uid;
5715 tree from;
5716 tree to;
5717 } tm_alias_pair;
5720 /* Helper function for finish_tm_clone_pairs. Dump a hash table entry
5721 into a VEC in INFO. */
5723 static int
5724 dump_tm_clone_to_vec (void **slot, void *info)
5726 struct tree_map *map = (struct tree_map *) *slot;
5727 vec<tm_alias_pair> *tm_alias_pairs = (vec<tm_alias_pair> *) info;
5728 tm_alias_pair p = {DECL_UID (map->base.from), map->base.from, map->to};
5729 tm_alias_pairs->safe_push (p);
5730 return 1;
5733 /* Dump the actual pairs to the .tm_clone_table section. */
5735 static void
5736 dump_tm_clone_pairs (vec<tm_alias_pair> tm_alias_pairs)
5738 unsigned i;
5739 tm_alias_pair *p;
5740 bool switched = false;
5742 FOR_EACH_VEC_ELT (tm_alias_pairs, i, p)
5744 tree src = p->from;
5745 tree dst = p->to;
5746 struct cgraph_node *src_n = cgraph_node::get (src);
5747 struct cgraph_node *dst_n = cgraph_node::get (dst);
5749 /* The function ipa_tm_create_version() marks the clone as needed if
5750 the original function was needed. But we also mark the clone as
5751 needed if we ever called the clone indirectly through
5752 TM_GETTMCLONE. If neither of these are true, we didn't generate
5753 a clone, and we didn't call it indirectly... no sense keeping it
5754 in the clone table. */
5755 if (!dst_n || !dst_n->definition)
5756 continue;
5758 /* This covers the case where we have optimized the original
5759 function away, and only access the transactional clone. */
5760 if (!src_n || !src_n->definition)
5761 continue;
5763 if (!switched)
5765 switch_to_section (targetm.asm_out.tm_clone_table_section ());
5766 assemble_align (POINTER_SIZE);
5767 switched = true;
5770 assemble_integer (XEXP (DECL_RTL (src), 0),
5771 POINTER_SIZE_UNITS, POINTER_SIZE, 1);
5772 assemble_integer (XEXP (DECL_RTL (dst), 0),
5773 POINTER_SIZE_UNITS, POINTER_SIZE, 1);
5777 /* Provide a default for the tm_clone_table section. */
5779 section *
5780 default_clone_table_section (void)
5782 return get_named_section (NULL, ".tm_clone_table", 3);
5785 /* Helper comparison function for qsorting by the DECL_UID stored in
5786 alias_pair->emitted_diags. */
5788 static int
5789 tm_alias_pair_cmp (const void *x, const void *y)
5791 const tm_alias_pair *p1 = (const tm_alias_pair *) x;
5792 const tm_alias_pair *p2 = (const tm_alias_pair *) y;
5793 if (p1->uid < p2->uid)
5794 return -1;
5795 if (p1->uid > p2->uid)
5796 return 1;
5797 return 0;
5800 void
5801 finish_tm_clone_pairs (void)
5803 vec<tm_alias_pair> tm_alias_pairs = vNULL;
5805 if (tm_clone_hash == NULL)
5806 return;
5808 /* We need a determenistic order for the .tm_clone_table, otherwise
5809 we will get bootstrap comparison failures, so dump the hash table
5810 to a vector, sort it, and dump the vector. */
5812 /* Dump the hashtable to a vector. */
5813 htab_traverse_noresize (tm_clone_hash, dump_tm_clone_to_vec,
5814 (void *) &tm_alias_pairs);
5815 /* Sort it. */
5816 tm_alias_pairs.qsort (tm_alias_pair_cmp);
5818 /* Dump it. */
5819 dump_tm_clone_pairs (tm_alias_pairs);
5821 htab_delete (tm_clone_hash);
5822 tm_clone_hash = NULL;
5823 tm_alias_pairs.release ();
5827 /* Emit an assembler directive to set symbol for DECL visibility to
5828 the visibility type VIS, which must not be VISIBILITY_DEFAULT. */
5830 void
5831 default_assemble_visibility (tree decl ATTRIBUTE_UNUSED,
5832 int vis ATTRIBUTE_UNUSED)
5834 #ifdef HAVE_GAS_HIDDEN
5835 static const char * const visibility_types[] = {
5836 NULL, "protected", "hidden", "internal"
5839 const char *name, *type;
5841 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5842 type = visibility_types[vis];
5844 fprintf (asm_out_file, "\t.%s\t", type);
5845 assemble_name (asm_out_file, name);
5846 fprintf (asm_out_file, "\n");
5847 #else
5848 if (!DECL_ARTIFICIAL (decl))
5849 warning (OPT_Wattributes, "visibility attribute not supported "
5850 "in this configuration; ignored");
5851 #endif
5854 /* A helper function to call assemble_visibility when needed for a decl. */
5857 maybe_assemble_visibility (tree decl)
5859 enum symbol_visibility vis = DECL_VISIBILITY (decl);
5861 if (vis != VISIBILITY_DEFAULT)
5863 targetm.asm_out.assemble_visibility (decl, vis);
5864 return 1;
5866 else
5867 return 0;
5870 /* Returns 1 if the target configuration supports defining public symbols
5871 so that one of them will be chosen at link time instead of generating a
5872 multiply-defined symbol error, whether through the use of weak symbols or
5873 a target-specific mechanism for having duplicates discarded. */
5876 supports_one_only (void)
5878 if (SUPPORTS_ONE_ONLY)
5879 return 1;
5880 return TARGET_SUPPORTS_WEAK;
5883 /* Set up DECL as a public symbol that can be defined in multiple
5884 translation units without generating a linker error. */
5886 void
5887 make_decl_one_only (tree decl, tree comdat_group)
5889 struct symtab_node *symbol;
5890 gcc_assert (TREE_CODE (decl) == VAR_DECL
5891 || TREE_CODE (decl) == FUNCTION_DECL);
5893 TREE_PUBLIC (decl) = 1;
5895 if (TREE_CODE (decl) == VAR_DECL)
5896 symbol = varpool_node::get_create (decl);
5897 else
5898 symbol = cgraph_node::get_create (decl);
5900 if (SUPPORTS_ONE_ONLY)
5902 #ifdef MAKE_DECL_ONE_ONLY
5903 MAKE_DECL_ONE_ONLY (decl);
5904 #endif
5905 symbol->set_comdat_group (comdat_group);
5907 else if (TREE_CODE (decl) == VAR_DECL
5908 && (DECL_INITIAL (decl) == 0
5909 || (!in_lto_p && DECL_INITIAL (decl) == error_mark_node)))
5910 DECL_COMMON (decl) = 1;
5911 else
5913 gcc_assert (TARGET_SUPPORTS_WEAK);
5914 DECL_WEAK (decl) = 1;
5918 void
5919 init_varasm_once (void)
5921 section_htab = hash_table<section_hasher>::create_ggc (31);
5922 object_block_htab = hash_table<object_block_hasher>::create_ggc (31);
5923 const_desc_htab = hash_table<tree_descriptor_hasher>::create_ggc (1009);
5925 shared_constant_pool = create_constant_pool ();
5927 #ifdef TEXT_SECTION_ASM_OP
5928 text_section = get_unnamed_section (SECTION_CODE, output_section_asm_op,
5929 TEXT_SECTION_ASM_OP);
5930 #endif
5932 #ifdef DATA_SECTION_ASM_OP
5933 data_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
5934 DATA_SECTION_ASM_OP);
5935 #endif
5937 #ifdef SDATA_SECTION_ASM_OP
5938 sdata_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
5939 SDATA_SECTION_ASM_OP);
5940 #endif
5942 #ifdef READONLY_DATA_SECTION_ASM_OP
5943 readonly_data_section = get_unnamed_section (0, output_section_asm_op,
5944 READONLY_DATA_SECTION_ASM_OP);
5945 #endif
5947 #ifdef CTORS_SECTION_ASM_OP
5948 ctors_section = get_unnamed_section (0, output_section_asm_op,
5949 CTORS_SECTION_ASM_OP);
5950 #endif
5952 #ifdef DTORS_SECTION_ASM_OP
5953 dtors_section = get_unnamed_section (0, output_section_asm_op,
5954 DTORS_SECTION_ASM_OP);
5955 #endif
5957 #ifdef BSS_SECTION_ASM_OP
5958 bss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
5959 output_section_asm_op,
5960 BSS_SECTION_ASM_OP);
5961 #endif
5963 #ifdef SBSS_SECTION_ASM_OP
5964 sbss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
5965 output_section_asm_op,
5966 SBSS_SECTION_ASM_OP);
5967 #endif
5969 tls_comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
5970 | SECTION_COMMON, emit_tls_common);
5971 lcomm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
5972 | SECTION_COMMON, emit_local);
5973 comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
5974 | SECTION_COMMON, emit_common);
5976 #if defined ASM_OUTPUT_ALIGNED_BSS
5977 bss_noswitch_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS,
5978 emit_bss);
5979 #endif
5981 targetm.asm_out.init_sections ();
5983 if (readonly_data_section == NULL)
5984 readonly_data_section = text_section;
5986 #ifdef ASM_OUTPUT_EXTERNAL
5987 pending_assemble_externals_set = new hash_set<tree>;
5988 #endif
5991 enum tls_model
5992 decl_default_tls_model (const_tree decl)
5994 enum tls_model kind;
5995 bool is_local;
5997 is_local = targetm.binds_local_p (decl);
5998 if (!flag_shlib)
6000 if (is_local)
6001 kind = TLS_MODEL_LOCAL_EXEC;
6002 else
6003 kind = TLS_MODEL_INITIAL_EXEC;
6006 /* Local dynamic is inefficient when we're not combining the
6007 parts of the address. */
6008 else if (optimize && is_local)
6009 kind = TLS_MODEL_LOCAL_DYNAMIC;
6010 else
6011 kind = TLS_MODEL_GLOBAL_DYNAMIC;
6012 if (kind < flag_tls_default)
6013 kind = flag_tls_default;
6015 return kind;
6018 /* Select a set of attributes for section NAME based on the properties
6019 of DECL and whether or not RELOC indicates that DECL's initializer
6020 might contain runtime relocations.
6022 We make the section read-only and executable for a function decl,
6023 read-only for a const data decl, and writable for a non-const data decl. */
6025 unsigned int
6026 default_section_type_flags (tree decl, const char *name, int reloc)
6028 unsigned int flags;
6030 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
6031 flags = SECTION_CODE;
6032 else if (decl)
6034 enum section_category category
6035 = categorize_decl_for_section (decl, reloc);
6036 if (decl_readonly_section_1 (category))
6037 flags = 0;
6038 else if (category == SECCAT_DATA_REL_RO
6039 || category == SECCAT_DATA_REL_RO_LOCAL)
6040 flags = SECTION_WRITE | SECTION_RELRO;
6041 else
6042 flags = SECTION_WRITE;
6044 else
6046 flags = SECTION_WRITE;
6047 if (strcmp (name, ".data.rel.ro") == 0
6048 || strcmp (name, ".data.rel.ro.local") == 0)
6049 flags |= SECTION_RELRO;
6052 if (decl && DECL_P (decl) && DECL_COMDAT_GROUP (decl))
6053 flags |= SECTION_LINKONCE;
6055 if (strcmp (name, ".vtable_map_vars") == 0)
6056 flags |= SECTION_LINKONCE;
6058 if (decl && TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
6059 flags |= SECTION_TLS | SECTION_WRITE;
6061 if (strcmp (name, ".bss") == 0
6062 || strncmp (name, ".bss.", 5) == 0
6063 || strncmp (name, ".gnu.linkonce.b.", 16) == 0
6064 || strcmp (name, ".persistent.bss") == 0
6065 || strcmp (name, ".sbss") == 0
6066 || strncmp (name, ".sbss.", 6) == 0
6067 || strncmp (name, ".gnu.linkonce.sb.", 17) == 0)
6068 flags |= SECTION_BSS;
6070 if (strcmp (name, ".tdata") == 0
6071 || strncmp (name, ".tdata.", 7) == 0
6072 || strncmp (name, ".gnu.linkonce.td.", 17) == 0)
6073 flags |= SECTION_TLS;
6075 if (strcmp (name, ".tbss") == 0
6076 || strncmp (name, ".tbss.", 6) == 0
6077 || strncmp (name, ".gnu.linkonce.tb.", 17) == 0)
6078 flags |= SECTION_TLS | SECTION_BSS;
6080 /* These three sections have special ELF types. They are neither
6081 SHT_PROGBITS nor SHT_NOBITS, so when changing sections we don't
6082 want to print a section type (@progbits or @nobits). If someone
6083 is silly enough to emit code or TLS variables to one of these
6084 sections, then don't handle them specially. */
6085 if (!(flags & (SECTION_CODE | SECTION_BSS | SECTION_TLS))
6086 && (strcmp (name, ".init_array") == 0
6087 || strcmp (name, ".fini_array") == 0
6088 || strcmp (name, ".preinit_array") == 0))
6089 flags |= SECTION_NOTYPE;
6091 return flags;
6094 /* Return true if the target supports some form of global BSS,
6095 either through bss_noswitch_section, or by selecting a BSS
6096 section in TARGET_ASM_SELECT_SECTION. */
6098 bool
6099 have_global_bss_p (void)
6101 return bss_noswitch_section || targetm.have_switchable_bss_sections;
6104 /* Output assembly to switch to section NAME with attribute FLAGS.
6105 Four variants for common object file formats. */
6107 void
6108 default_no_named_section (const char *name ATTRIBUTE_UNUSED,
6109 unsigned int flags ATTRIBUTE_UNUSED,
6110 tree decl ATTRIBUTE_UNUSED)
6112 /* Some object formats don't support named sections at all. The
6113 front-end should already have flagged this as an error. */
6114 gcc_unreachable ();
6117 #ifndef TLS_SECTION_ASM_FLAG
6118 #define TLS_SECTION_ASM_FLAG 'T'
6119 #endif
6121 void
6122 default_elf_asm_named_section (const char *name, unsigned int flags,
6123 tree decl ATTRIBUTE_UNUSED)
6125 char flagchars[10], *f = flagchars;
6127 /* If we have already declared this section, we can use an
6128 abbreviated form to switch back to it -- unless this section is
6129 part of a COMDAT groups, in which case GAS requires the full
6130 declaration every time. */
6131 if (!(HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6132 && (flags & SECTION_DECLARED))
6134 fprintf (asm_out_file, "\t.section\t%s\n", name);
6135 return;
6138 if (!(flags & SECTION_DEBUG))
6139 *f++ = 'a';
6140 #if defined (HAVE_GAS_SECTION_EXCLUDE) && HAVE_GAS_SECTION_EXCLUDE == 1
6141 if (flags & SECTION_EXCLUDE)
6142 *f++ = 'e';
6143 #endif
6144 if (flags & SECTION_WRITE)
6145 *f++ = 'w';
6146 if (flags & SECTION_CODE)
6147 *f++ = 'x';
6148 if (flags & SECTION_SMALL)
6149 *f++ = 's';
6150 if (flags & SECTION_MERGE)
6151 *f++ = 'M';
6152 if (flags & SECTION_STRINGS)
6153 *f++ = 'S';
6154 if (flags & SECTION_TLS)
6155 *f++ = TLS_SECTION_ASM_FLAG;
6156 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6157 *f++ = 'G';
6158 *f = '\0';
6160 fprintf (asm_out_file, "\t.section\t%s,\"%s\"", name, flagchars);
6162 if (!(flags & SECTION_NOTYPE))
6164 const char *type;
6165 const char *format;
6167 if (flags & SECTION_BSS)
6168 type = "nobits";
6169 else
6170 type = "progbits";
6172 format = ",@%s";
6173 /* On platforms that use "@" as the assembly comment character,
6174 use "%" instead. */
6175 if (strcmp (ASM_COMMENT_START, "@") == 0)
6176 format = ",%%%s";
6177 fprintf (asm_out_file, format, type);
6179 if (flags & SECTION_ENTSIZE)
6180 fprintf (asm_out_file, ",%d", flags & SECTION_ENTSIZE);
6181 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6183 if (TREE_CODE (decl) == IDENTIFIER_NODE)
6184 fprintf (asm_out_file, ",%s,comdat", IDENTIFIER_POINTER (decl));
6185 else
6186 fprintf (asm_out_file, ",%s,comdat",
6187 IDENTIFIER_POINTER (DECL_COMDAT_GROUP (decl)));
6191 putc ('\n', asm_out_file);
6194 void
6195 default_coff_asm_named_section (const char *name, unsigned int flags,
6196 tree decl ATTRIBUTE_UNUSED)
6198 char flagchars[8], *f = flagchars;
6200 if (flags & SECTION_WRITE)
6201 *f++ = 'w';
6202 if (flags & SECTION_CODE)
6203 *f++ = 'x';
6204 *f = '\0';
6206 fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
6209 void
6210 default_pe_asm_named_section (const char *name, unsigned int flags,
6211 tree decl)
6213 default_coff_asm_named_section (name, flags, decl);
6215 if (flags & SECTION_LINKONCE)
6217 /* Functions may have been compiled at various levels of
6218 optimization so we can't use `same_size' here.
6219 Instead, have the linker pick one. */
6220 fprintf (asm_out_file, "\t.linkonce %s\n",
6221 (flags & SECTION_CODE ? "discard" : "same_size"));
6225 /* The lame default section selector. */
6227 section *
6228 default_select_section (tree decl, int reloc,
6229 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
6231 if (DECL_P (decl))
6233 if (decl_readonly_section (decl, reloc))
6234 return readonly_data_section;
6236 else if (TREE_CODE (decl) == CONSTRUCTOR)
6238 if (! ((flag_pic && reloc)
6239 || !TREE_READONLY (decl)
6240 || TREE_SIDE_EFFECTS (decl)
6241 || !TREE_CONSTANT (decl)))
6242 return readonly_data_section;
6244 else if (TREE_CODE (decl) == STRING_CST)
6245 return readonly_data_section;
6246 else if (! (flag_pic && reloc))
6247 return readonly_data_section;
6249 return data_section;
6252 enum section_category
6253 categorize_decl_for_section (const_tree decl, int reloc)
6255 enum section_category ret;
6257 if (TREE_CODE (decl) == FUNCTION_DECL)
6258 return SECCAT_TEXT;
6259 else if (TREE_CODE (decl) == STRING_CST)
6261 if ((flag_sanitize & SANITIZE_ADDRESS)
6262 && asan_protect_global (CONST_CAST_TREE (decl)))
6263 /* or !flag_merge_constants */
6264 return SECCAT_RODATA;
6265 else
6266 return SECCAT_RODATA_MERGE_STR;
6268 else if (TREE_CODE (decl) == VAR_DECL)
6270 if (bss_initializer_p (decl))
6271 ret = SECCAT_BSS;
6272 else if (! TREE_READONLY (decl)
6273 || TREE_SIDE_EFFECTS (decl)
6274 || ! TREE_CONSTANT (DECL_INITIAL (decl)))
6276 /* Here the reloc_rw_mask is not testing whether the section should
6277 be read-only or not, but whether the dynamic link will have to
6278 do something. If so, we wish to segregate the data in order to
6279 minimize cache misses inside the dynamic linker. */
6280 if (reloc & targetm.asm_out.reloc_rw_mask ())
6281 ret = reloc == 1 ? SECCAT_DATA_REL_LOCAL : SECCAT_DATA_REL;
6282 else
6283 ret = SECCAT_DATA;
6285 else if (reloc & targetm.asm_out.reloc_rw_mask ())
6286 ret = reloc == 1 ? SECCAT_DATA_REL_RO_LOCAL : SECCAT_DATA_REL_RO;
6287 else if (reloc || flag_merge_constants < 2
6288 || ((flag_sanitize & SANITIZE_ADDRESS)
6289 && asan_protect_global (CONST_CAST_TREE (decl))))
6290 /* C and C++ don't allow different variables to share the same
6291 location. -fmerge-all-constants allows even that (at the
6292 expense of not conforming). */
6293 ret = SECCAT_RODATA;
6294 else if (TREE_CODE (DECL_INITIAL (decl)) == STRING_CST)
6295 ret = SECCAT_RODATA_MERGE_STR_INIT;
6296 else
6297 ret = SECCAT_RODATA_MERGE_CONST;
6299 else if (TREE_CODE (decl) == CONSTRUCTOR)
6301 if ((reloc & targetm.asm_out.reloc_rw_mask ())
6302 || TREE_SIDE_EFFECTS (decl)
6303 || ! TREE_CONSTANT (decl))
6304 ret = SECCAT_DATA;
6305 else
6306 ret = SECCAT_RODATA;
6308 else
6309 ret = SECCAT_RODATA;
6311 /* There are no read-only thread-local sections. */
6312 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
6314 /* Note that this would be *just* SECCAT_BSS, except that there's
6315 no concept of a read-only thread-local-data section. */
6316 if (ret == SECCAT_BSS
6317 || (flag_zero_initialized_in_bss
6318 && initializer_zerop (DECL_INITIAL (decl))))
6319 ret = SECCAT_TBSS;
6320 else
6321 ret = SECCAT_TDATA;
6324 /* If the target uses small data sections, select it. */
6325 else if (targetm.in_small_data_p (decl))
6327 if (ret == SECCAT_BSS)
6328 ret = SECCAT_SBSS;
6329 else if (targetm.have_srodata_section && ret == SECCAT_RODATA)
6330 ret = SECCAT_SRODATA;
6331 else
6332 ret = SECCAT_SDATA;
6335 return ret;
6338 static bool
6339 decl_readonly_section_1 (enum section_category category)
6341 switch (category)
6343 case SECCAT_RODATA:
6344 case SECCAT_RODATA_MERGE_STR:
6345 case SECCAT_RODATA_MERGE_STR_INIT:
6346 case SECCAT_RODATA_MERGE_CONST:
6347 case SECCAT_SRODATA:
6348 return true;
6349 default:
6350 return false;
6354 bool
6355 decl_readonly_section (const_tree decl, int reloc)
6357 return decl_readonly_section_1 (categorize_decl_for_section (decl, reloc));
6360 /* Select a section based on the above categorization. */
6362 section *
6363 default_elf_select_section (tree decl, int reloc,
6364 unsigned HOST_WIDE_INT align)
6366 const char *sname;
6367 switch (categorize_decl_for_section (decl, reloc))
6369 case SECCAT_TEXT:
6370 /* We're not supposed to be called on FUNCTION_DECLs. */
6371 gcc_unreachable ();
6372 case SECCAT_RODATA:
6373 return readonly_data_section;
6374 case SECCAT_RODATA_MERGE_STR:
6375 return mergeable_string_section (decl, align, 0);
6376 case SECCAT_RODATA_MERGE_STR_INIT:
6377 return mergeable_string_section (DECL_INITIAL (decl), align, 0);
6378 case SECCAT_RODATA_MERGE_CONST:
6379 return mergeable_constant_section (DECL_MODE (decl), align, 0);
6380 case SECCAT_SRODATA:
6381 sname = ".sdata2";
6382 break;
6383 case SECCAT_DATA:
6384 return data_section;
6385 case SECCAT_DATA_REL:
6386 sname = ".data.rel";
6387 break;
6388 case SECCAT_DATA_REL_LOCAL:
6389 sname = ".data.rel.local";
6390 break;
6391 case SECCAT_DATA_REL_RO:
6392 sname = ".data.rel.ro";
6393 break;
6394 case SECCAT_DATA_REL_RO_LOCAL:
6395 sname = ".data.rel.ro.local";
6396 break;
6397 case SECCAT_SDATA:
6398 sname = ".sdata";
6399 break;
6400 case SECCAT_TDATA:
6401 sname = ".tdata";
6402 break;
6403 case SECCAT_BSS:
6404 if (bss_section)
6405 return bss_section;
6406 sname = ".bss";
6407 break;
6408 case SECCAT_SBSS:
6409 sname = ".sbss";
6410 break;
6411 case SECCAT_TBSS:
6412 sname = ".tbss";
6413 break;
6414 default:
6415 gcc_unreachable ();
6418 return get_named_section (decl, sname, reloc);
6421 /* Construct a unique section name based on the decl name and the
6422 categorization performed above. */
6424 void
6425 default_unique_section (tree decl, int reloc)
6427 /* We only need to use .gnu.linkonce if we don't have COMDAT groups. */
6428 bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
6429 const char *prefix, *name, *linkonce;
6430 char *string;
6432 switch (categorize_decl_for_section (decl, reloc))
6434 case SECCAT_TEXT:
6435 prefix = one_only ? ".t" : ".text";
6436 break;
6437 case SECCAT_RODATA:
6438 case SECCAT_RODATA_MERGE_STR:
6439 case SECCAT_RODATA_MERGE_STR_INIT:
6440 case SECCAT_RODATA_MERGE_CONST:
6441 prefix = one_only ? ".r" : ".rodata";
6442 break;
6443 case SECCAT_SRODATA:
6444 prefix = one_only ? ".s2" : ".sdata2";
6445 break;
6446 case SECCAT_DATA:
6447 prefix = one_only ? ".d" : ".data";
6448 break;
6449 case SECCAT_DATA_REL:
6450 prefix = one_only ? ".d.rel" : ".data.rel";
6451 break;
6452 case SECCAT_DATA_REL_LOCAL:
6453 prefix = one_only ? ".d.rel.local" : ".data.rel.local";
6454 break;
6455 case SECCAT_DATA_REL_RO:
6456 prefix = one_only ? ".d.rel.ro" : ".data.rel.ro";
6457 break;
6458 case SECCAT_DATA_REL_RO_LOCAL:
6459 prefix = one_only ? ".d.rel.ro.local" : ".data.rel.ro.local";
6460 break;
6461 case SECCAT_SDATA:
6462 prefix = one_only ? ".s" : ".sdata";
6463 break;
6464 case SECCAT_BSS:
6465 prefix = one_only ? ".b" : ".bss";
6466 break;
6467 case SECCAT_SBSS:
6468 prefix = one_only ? ".sb" : ".sbss";
6469 break;
6470 case SECCAT_TDATA:
6471 prefix = one_only ? ".td" : ".tdata";
6472 break;
6473 case SECCAT_TBSS:
6474 prefix = one_only ? ".tb" : ".tbss";
6475 break;
6476 default:
6477 gcc_unreachable ();
6480 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
6481 name = targetm.strip_name_encoding (name);
6483 /* If we're using one_only, then there needs to be a .gnu.linkonce
6484 prefix to the section name. */
6485 linkonce = one_only ? ".gnu.linkonce" : "";
6487 string = ACONCAT ((linkonce, prefix, ".", name, NULL));
6489 set_decl_section_name (decl, string);
6492 /* Subroutine of compute_reloc_for_rtx for leaf rtxes. */
6494 static int
6495 compute_reloc_for_rtx_1 (const_rtx x)
6497 switch (GET_CODE (x))
6499 case SYMBOL_REF:
6500 return SYMBOL_REF_LOCAL_P (x) ? 1 : 2;
6501 case LABEL_REF:
6502 return 1;
6503 default:
6504 return 0;
6508 /* Like compute_reloc_for_constant, except for an RTX. The return value
6509 is a mask for which bit 1 indicates a global relocation, and bit 0
6510 indicates a local relocation. */
6512 static int
6513 compute_reloc_for_rtx (const_rtx x)
6515 switch (GET_CODE (x))
6517 case SYMBOL_REF:
6518 case LABEL_REF:
6519 return compute_reloc_for_rtx_1 (x);
6521 case CONST:
6523 int reloc = 0;
6524 subrtx_iterator::array_type array;
6525 FOR_EACH_SUBRTX (iter, array, x, ALL)
6526 reloc |= compute_reloc_for_rtx_1 (*iter);
6527 return reloc;
6530 default:
6531 return 0;
6535 section *
6536 default_select_rtx_section (enum machine_mode mode ATTRIBUTE_UNUSED,
6537 rtx x,
6538 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
6540 if (compute_reloc_for_rtx (x) & targetm.asm_out.reloc_rw_mask ())
6541 return data_section;
6542 else
6543 return readonly_data_section;
6546 section *
6547 default_elf_select_rtx_section (enum machine_mode mode, rtx x,
6548 unsigned HOST_WIDE_INT align)
6550 int reloc = compute_reloc_for_rtx (x);
6552 /* ??? Handle small data here somehow. */
6554 if (reloc & targetm.asm_out.reloc_rw_mask ())
6556 if (reloc == 1)
6557 return get_named_section (NULL, ".data.rel.ro.local", 1);
6558 else
6559 return get_named_section (NULL, ".data.rel.ro", 3);
6562 return mergeable_constant_section (mode, align, 0);
6565 /* Set the generally applicable flags on the SYMBOL_REF for EXP. */
6567 void
6568 default_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
6570 rtx symbol;
6571 int flags;
6573 /* Careful not to prod global register variables. */
6574 if (!MEM_P (rtl))
6575 return;
6576 symbol = XEXP (rtl, 0);
6577 if (GET_CODE (symbol) != SYMBOL_REF)
6578 return;
6580 flags = SYMBOL_REF_FLAGS (symbol) & SYMBOL_FLAG_HAS_BLOCK_INFO;
6581 if (TREE_CODE (decl) == FUNCTION_DECL)
6582 flags |= SYMBOL_FLAG_FUNCTION;
6583 if (targetm.binds_local_p (decl))
6584 flags |= SYMBOL_FLAG_LOCAL;
6585 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
6586 flags |= DECL_TLS_MODEL (decl) << SYMBOL_FLAG_TLS_SHIFT;
6587 else if (targetm.in_small_data_p (decl))
6588 flags |= SYMBOL_FLAG_SMALL;
6589 /* ??? Why is DECL_EXTERNAL ever set for non-PUBLIC names? Without
6590 being PUBLIC, the thing *must* be defined in this translation unit.
6591 Prevent this buglet from being propagated into rtl code as well. */
6592 if (DECL_P (decl) && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl))
6593 flags |= SYMBOL_FLAG_EXTERNAL;
6595 SYMBOL_REF_FLAGS (symbol) = flags;
6598 /* By default, we do nothing for encode_section_info, so we need not
6599 do anything but discard the '*' marker. */
6601 const char *
6602 default_strip_name_encoding (const char *str)
6604 return str + (*str == '*');
6607 #ifdef ASM_OUTPUT_DEF
6608 /* The default implementation of TARGET_ASM_OUTPUT_ANCHOR. Define the
6609 anchor relative to ".", the current section position. */
6611 void
6612 default_asm_output_anchor (rtx symbol)
6614 char buffer[100];
6616 sprintf (buffer, "*. + " HOST_WIDE_INT_PRINT_DEC,
6617 SYMBOL_REF_BLOCK_OFFSET (symbol));
6618 ASM_OUTPUT_DEF (asm_out_file, XSTR (symbol, 0), buffer);
6620 #endif
6622 /* The default implementation of TARGET_USE_ANCHORS_FOR_SYMBOL_P. */
6624 bool
6625 default_use_anchors_for_symbol_p (const_rtx symbol)
6627 section *sect;
6628 tree decl;
6630 /* Don't use anchors for mergeable sections. The linker might move
6631 the objects around. */
6632 sect = SYMBOL_REF_BLOCK (symbol)->sect;
6633 if (sect->common.flags & SECTION_MERGE)
6634 return false;
6636 /* Don't use anchors for small data sections. The small data register
6637 acts as an anchor for such sections. */
6638 if (sect->common.flags & SECTION_SMALL)
6639 return false;
6641 decl = SYMBOL_REF_DECL (symbol);
6642 if (decl && DECL_P (decl))
6644 /* Don't use section anchors for decls that might be defined or
6645 usurped by other modules. */
6646 if (TREE_PUBLIC (decl) && !decl_binds_to_current_def_p (decl))
6647 return false;
6649 /* Don't use section anchors for decls that will be placed in a
6650 small data section. */
6651 /* ??? Ideally, this check would be redundant with the SECTION_SMALL
6652 one above. The problem is that we only use SECTION_SMALL for
6653 sections that should be marked as small in the section directive. */
6654 if (targetm.in_small_data_p (decl))
6655 return false;
6657 return true;
6660 /* Return true when RESOLUTION indicate that symbol will be bound to the
6661 definition provided by current .o file. */
6663 static bool
6664 resolution_to_local_definition_p (enum ld_plugin_symbol_resolution resolution)
6666 return (resolution == LDPR_PREVAILING_DEF
6667 || resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
6668 || resolution == LDPR_PREVAILING_DEF_IRONLY);
6671 /* Return true when RESOLUTION indicate that symbol will be bound locally
6672 within current executable or DSO. */
6674 static bool
6675 resolution_local_p (enum ld_plugin_symbol_resolution resolution)
6677 return (resolution == LDPR_PREVAILING_DEF
6678 || resolution == LDPR_PREVAILING_DEF_IRONLY
6679 || resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
6680 || resolution == LDPR_PREEMPTED_REG
6681 || resolution == LDPR_PREEMPTED_IR
6682 || resolution == LDPR_RESOLVED_IR
6683 || resolution == LDPR_RESOLVED_EXEC);
6686 /* Assume ELF-ish defaults, since that's pretty much the most liberal
6687 wrt cross-module name binding. */
6689 bool
6690 default_binds_local_p (const_tree exp)
6692 return default_binds_local_p_1 (exp, flag_shlib);
6695 bool
6696 default_binds_local_p_1 (const_tree exp, int shlib)
6698 bool local_p;
6699 bool resolved_locally = false;
6700 bool resolved_to_local_def = false;
6702 /* With resolution file in hands, take look into resolutions.
6703 We can't just return true for resolved_locally symbols,
6704 because dynamic linking might overwrite symbols
6705 in shared libraries. */
6706 if (TREE_CODE (exp) == VAR_DECL && TREE_PUBLIC (exp)
6707 && (TREE_STATIC (exp) || DECL_EXTERNAL (exp)))
6709 varpool_node *vnode = varpool_node::get (exp);
6710 if (vnode && (resolution_local_p (vnode->resolution) || vnode->in_other_partition))
6711 resolved_locally = true;
6712 if (vnode
6713 && resolution_to_local_definition_p (vnode->resolution))
6714 resolved_to_local_def = true;
6716 else if (TREE_CODE (exp) == FUNCTION_DECL && TREE_PUBLIC (exp))
6718 struct cgraph_node *node = cgraph_node::get (exp);
6719 if (node
6720 && (resolution_local_p (node->resolution) || node->in_other_partition))
6721 resolved_locally = true;
6722 if (node
6723 && resolution_to_local_definition_p (node->resolution))
6724 resolved_to_local_def = true;
6727 /* A non-decl is an entry in the constant pool. */
6728 if (!DECL_P (exp))
6729 local_p = true;
6730 /* Weakrefs may not bind locally, even though the weakref itself is always
6731 static and therefore local. Similarly, the resolver for ifunc functions
6732 might resolve to a non-local function.
6733 FIXME: We can resolve the weakref case more curefuly by looking at the
6734 weakref alias. */
6735 else if (lookup_attribute ("weakref", DECL_ATTRIBUTES (exp))
6736 || (TREE_CODE (exp) == FUNCTION_DECL
6737 && lookup_attribute ("ifunc", DECL_ATTRIBUTES (exp))))
6738 local_p = false;
6739 /* Static variables are always local. */
6740 else if (! TREE_PUBLIC (exp))
6741 local_p = true;
6742 /* A variable is local if the user has said explicitly that it will
6743 be. */
6744 else if ((DECL_VISIBILITY_SPECIFIED (exp)
6745 || resolved_to_local_def)
6746 && DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
6747 local_p = true;
6748 /* Variables defined outside this object might not be local. */
6749 else if (DECL_EXTERNAL (exp) && !resolved_locally)
6750 local_p = false;
6751 /* If defined in this object and visibility is not default, must be
6752 local. */
6753 else if (DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
6754 local_p = true;
6755 /* Default visibility weak data can be overridden by a strong symbol
6756 in another module and so are not local. */
6757 else if (DECL_WEAK (exp)
6758 && !resolved_locally)
6759 local_p = false;
6760 /* If PIC, then assume that any global name can be overridden by
6761 symbols resolved from other modules. */
6762 else if (shlib)
6763 local_p = false;
6764 /* Uninitialized COMMON variable may be unified with symbols
6765 resolved from other modules. */
6766 else if (DECL_COMMON (exp)
6767 && !resolved_locally
6768 && (DECL_INITIAL (exp) == NULL
6769 || (!in_lto_p && DECL_INITIAL (exp) == error_mark_node)))
6770 local_p = false;
6771 /* Otherwise we're left with initialized (or non-common) global data
6772 which is of necessity defined locally. */
6773 else
6774 local_p = true;
6776 return local_p;
6779 /* Return true when references to DECL must bind to current definition in
6780 final executable.
6782 The condition is usually equivalent to whether the function binds to the
6783 current module (shared library or executable), that is to binds_local_p.
6784 We use this fact to avoid need for another target hook and implement
6785 the logic using binds_local_p and just special cases where
6786 decl_binds_to_current_def_p is stronger than binds_local_p. In particular
6787 the weak definitions (that can be overwritten at linktime by other
6788 definition from different object file) and when resolution info is available
6789 we simply use the knowledge passed to us by linker plugin. */
6790 bool
6791 decl_binds_to_current_def_p (const_tree decl)
6793 gcc_assert (DECL_P (decl));
6794 if (!targetm.binds_local_p (decl))
6795 return false;
6796 if (!TREE_PUBLIC (decl))
6797 return true;
6798 /* When resolution is available, just use it. */
6799 if (TREE_CODE (decl) == VAR_DECL
6800 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
6802 varpool_node *vnode = varpool_node::get (decl);
6803 if (vnode
6804 && vnode->resolution != LDPR_UNKNOWN)
6805 return resolution_to_local_definition_p (vnode->resolution);
6807 else if (TREE_CODE (decl) == FUNCTION_DECL)
6809 struct cgraph_node *node = cgraph_node::get (decl);
6810 if (node
6811 && node->resolution != LDPR_UNKNOWN)
6812 return resolution_to_local_definition_p (node->resolution);
6814 /* Otherwise we have to assume the worst for DECL_WEAK (hidden weaks
6815 binds locally but still can be overwritten), DECL_COMMON (can be merged
6816 with a non-common definition somewhere in the same module) or
6817 DECL_EXTERNAL.
6818 This rely on fact that binds_local_p behave as decl_replaceable_p
6819 for all other declaration types. */
6820 if (DECL_WEAK (decl))
6821 return false;
6822 if (DECL_COMMON (decl)
6823 && (DECL_INITIAL (decl) == NULL
6824 || (!in_lto_p && DECL_INITIAL (decl) == error_mark_node)))
6825 return false;
6826 if (DECL_EXTERNAL (decl))
6827 return false;
6828 return true;
6831 /* A replaceable function or variable is one which may be replaced
6832 at link-time with an entirely different definition, provided that the
6833 replacement has the same type. For example, functions declared
6834 with __attribute__((weak)) on most systems are replaceable.
6836 COMDAT functions are not replaceable, since all definitions of the
6837 function must be equivalent. It is important that COMDAT functions
6838 not be treated as replaceable so that use of C++ template
6839 instantiations is not penalized. */
6841 bool
6842 decl_replaceable_p (tree decl)
6844 gcc_assert (DECL_P (decl));
6845 if (!TREE_PUBLIC (decl) || DECL_COMDAT (decl))
6846 return false;
6847 if (!flag_semantic_interposition
6848 && !DECL_WEAK (decl))
6849 return false;
6850 return !decl_binds_to_current_def_p (decl);
6853 /* Default function to output code that will globalize a label. A
6854 target must define GLOBAL_ASM_OP or provide its own function to
6855 globalize a label. */
6856 #ifdef GLOBAL_ASM_OP
6857 void
6858 default_globalize_label (FILE * stream, const char *name)
6860 fputs (GLOBAL_ASM_OP, stream);
6861 assemble_name (stream, name);
6862 putc ('\n', stream);
6864 #endif /* GLOBAL_ASM_OP */
6866 /* Default function to output code that will globalize a declaration. */
6867 void
6868 default_globalize_decl_name (FILE * stream, tree decl)
6870 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
6871 targetm.asm_out.globalize_label (stream, name);
6874 /* Default function to output a label for unwind information. The
6875 default is to do nothing. A target that needs nonlocal labels for
6876 unwind information must provide its own function to do this. */
6877 void
6878 default_emit_unwind_label (FILE * stream ATTRIBUTE_UNUSED,
6879 tree decl ATTRIBUTE_UNUSED,
6880 int for_eh ATTRIBUTE_UNUSED,
6881 int empty ATTRIBUTE_UNUSED)
6885 /* Default function to output a label to divide up the exception table.
6886 The default is to do nothing. A target that needs/wants to divide
6887 up the table must provide it's own function to do this. */
6888 void
6889 default_emit_except_table_label (FILE * stream ATTRIBUTE_UNUSED)
6893 /* This is how to output an internal numbered label where PREFIX is
6894 the class of label and LABELNO is the number within the class. */
6896 void
6897 default_generate_internal_label (char *buf, const char *prefix,
6898 unsigned long labelno)
6900 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
6903 /* This is how to output an internal numbered label where PREFIX is
6904 the class of label and LABELNO is the number within the class. */
6906 void
6907 default_internal_label (FILE *stream, const char *prefix,
6908 unsigned long labelno)
6910 char *const buf = (char *) alloca (40 + strlen (prefix));
6911 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
6912 ASM_OUTPUT_INTERNAL_LABEL (stream, buf);
6916 /* The default implementation of ASM_DECLARE_CONSTANT_NAME. */
6918 void
6919 default_asm_declare_constant_name (FILE *file, const char *name,
6920 const_tree exp ATTRIBUTE_UNUSED,
6921 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
6923 assemble_label (file, name);
6926 /* This is the default behavior at the beginning of a file. It's
6927 controlled by two other target-hook toggles. */
6928 void
6929 default_file_start (void)
6931 if (targetm.asm_file_start_app_off
6932 && !(flag_verbose_asm || flag_debug_asm || flag_dump_rtl_in_asm))
6933 fputs (ASM_APP_OFF, asm_out_file);
6935 if (targetm.asm_file_start_file_directive)
6936 output_file_directive (asm_out_file, main_input_filename);
6939 /* This is a generic routine suitable for use as TARGET_ASM_FILE_END
6940 which emits a special section directive used to indicate whether or
6941 not this object file needs an executable stack. This is primarily
6942 a GNU extension to ELF but could be used on other targets. */
6944 int trampolines_created;
6946 void
6947 file_end_indicate_exec_stack (void)
6949 unsigned int flags = SECTION_DEBUG;
6950 if (trampolines_created)
6951 flags |= SECTION_CODE;
6953 switch_to_section (get_section (".note.GNU-stack", flags, NULL));
6956 /* Emit a special section directive to indicate that this object file
6957 was compiled with -fsplit-stack. This is used to let the linker
6958 detect calls between split-stack code and non-split-stack code, so
6959 that it can modify the split-stack code to allocate a sufficiently
6960 large stack. We emit another special section if there are any
6961 functions in this file which have the no_split_stack attribute, to
6962 prevent the linker from warning about being unable to convert the
6963 functions if they call non-split-stack code. */
6965 void
6966 file_end_indicate_split_stack (void)
6968 if (flag_split_stack)
6970 switch_to_section (get_section (".note.GNU-split-stack", SECTION_DEBUG,
6971 NULL));
6972 if (saw_no_split_stack)
6973 switch_to_section (get_section (".note.GNU-no-split-stack",
6974 SECTION_DEBUG, NULL));
6978 /* Output DIRECTIVE (a C string) followed by a newline. This is used as
6979 a get_unnamed_section callback. */
6981 void
6982 output_section_asm_op (const void *directive)
6984 fprintf (asm_out_file, "%s\n", (const char *) directive);
6987 /* Emit assembly code to switch to section NEW_SECTION. Do nothing if
6988 the current section is NEW_SECTION. */
6990 void
6991 switch_to_section (section *new_section)
6993 if (in_section == new_section)
6994 return;
6996 if (new_section->common.flags & SECTION_FORGET)
6997 in_section = NULL;
6998 else
6999 in_section = new_section;
7001 switch (SECTION_STYLE (new_section))
7003 case SECTION_NAMED:
7004 targetm.asm_out.named_section (new_section->named.name,
7005 new_section->named.common.flags,
7006 new_section->named.decl);
7007 break;
7009 case SECTION_UNNAMED:
7010 new_section->unnamed.callback (new_section->unnamed.data);
7011 break;
7013 case SECTION_NOSWITCH:
7014 gcc_unreachable ();
7015 break;
7018 new_section->common.flags |= SECTION_DECLARED;
7021 /* If block symbol SYMBOL has not yet been assigned an offset, place
7022 it at the end of its block. */
7024 void
7025 place_block_symbol (rtx symbol)
7027 unsigned HOST_WIDE_INT size, mask, offset;
7028 struct constant_descriptor_rtx *desc;
7029 unsigned int alignment;
7030 struct object_block *block;
7031 tree decl;
7033 gcc_assert (SYMBOL_REF_BLOCK (symbol));
7034 if (SYMBOL_REF_BLOCK_OFFSET (symbol) >= 0)
7035 return;
7037 /* Work out the symbol's size and alignment. */
7038 if (CONSTANT_POOL_ADDRESS_P (symbol))
7040 desc = SYMBOL_REF_CONSTANT (symbol);
7041 alignment = desc->align;
7042 size = GET_MODE_SIZE (desc->mode);
7044 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
7046 decl = SYMBOL_REF_DECL (symbol);
7047 alignment = DECL_ALIGN (decl);
7048 size = get_constant_size (DECL_INITIAL (decl));
7049 if ((flag_sanitize & SANITIZE_ADDRESS)
7050 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
7051 && asan_protect_global (DECL_INITIAL (decl)))
7052 size += asan_red_zone_size (size);
7054 else
7056 struct symtab_node *snode;
7057 decl = SYMBOL_REF_DECL (symbol);
7059 snode = symtab_node::get (decl);
7060 if (snode->alias)
7062 rtx target = DECL_RTL (snode->ultimate_alias_target ()->decl);
7064 place_block_symbol (target);
7065 SYMBOL_REF_BLOCK_OFFSET (symbol) = SYMBOL_REF_BLOCK_OFFSET (target);
7066 return;
7068 alignment = get_variable_align (decl);
7069 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
7070 if ((flag_sanitize & SANITIZE_ADDRESS)
7071 && asan_protect_global (decl))
7073 size += asan_red_zone_size (size);
7074 alignment = MAX (alignment,
7075 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
7079 /* Calculate the object's offset from the start of the block. */
7080 block = SYMBOL_REF_BLOCK (symbol);
7081 mask = alignment / BITS_PER_UNIT - 1;
7082 offset = (block->size + mask) & ~mask;
7083 SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
7085 /* Record the block's new alignment and size. */
7086 block->alignment = MAX (block->alignment, alignment);
7087 block->size = offset + size;
7089 vec_safe_push (block->objects, symbol);
7092 /* Return the anchor that should be used to address byte offset OFFSET
7093 from the first object in BLOCK. MODEL is the TLS model used
7094 to access it. */
7097 get_section_anchor (struct object_block *block, HOST_WIDE_INT offset,
7098 enum tls_model model)
7100 char label[100];
7101 unsigned int begin, middle, end;
7102 unsigned HOST_WIDE_INT min_offset, max_offset, range, bias, delta;
7103 rtx anchor;
7105 /* Work out the anchor's offset. Use an offset of 0 for the first
7106 anchor so that we don't pessimize the case where we take the address
7107 of a variable at the beginning of the block. This is particularly
7108 useful when a block has only one variable assigned to it.
7110 We try to place anchors RANGE bytes apart, so there can then be
7111 anchors at +/-RANGE, +/-2 * RANGE, and so on, up to the limits of
7112 a ptr_mode offset. With some target settings, the lowest such
7113 anchor might be out of range for the lowest ptr_mode offset;
7114 likewise the highest anchor for the highest offset. Use anchors
7115 at the extreme ends of the ptr_mode range in such cases.
7117 All arithmetic uses unsigned integers in order to avoid
7118 signed overflow. */
7119 max_offset = (unsigned HOST_WIDE_INT) targetm.max_anchor_offset;
7120 min_offset = (unsigned HOST_WIDE_INT) targetm.min_anchor_offset;
7121 range = max_offset - min_offset + 1;
7122 if (range == 0)
7123 offset = 0;
7124 else
7126 bias = 1 << (GET_MODE_BITSIZE (ptr_mode) - 1);
7127 if (offset < 0)
7129 delta = -(unsigned HOST_WIDE_INT) offset + max_offset;
7130 delta -= delta % range;
7131 if (delta > bias)
7132 delta = bias;
7133 offset = (HOST_WIDE_INT) (-delta);
7135 else
7137 delta = (unsigned HOST_WIDE_INT) offset - min_offset;
7138 delta -= delta % range;
7139 if (delta > bias - 1)
7140 delta = bias - 1;
7141 offset = (HOST_WIDE_INT) delta;
7145 /* Do a binary search to see if there's already an anchor we can use.
7146 Set BEGIN to the new anchor's index if not. */
7147 begin = 0;
7148 end = vec_safe_length (block->anchors);
7149 while (begin != end)
7151 middle = (end + begin) / 2;
7152 anchor = (*block->anchors)[middle];
7153 if (SYMBOL_REF_BLOCK_OFFSET (anchor) > offset)
7154 end = middle;
7155 else if (SYMBOL_REF_BLOCK_OFFSET (anchor) < offset)
7156 begin = middle + 1;
7157 else if (SYMBOL_REF_TLS_MODEL (anchor) > model)
7158 end = middle;
7159 else if (SYMBOL_REF_TLS_MODEL (anchor) < model)
7160 begin = middle + 1;
7161 else
7162 return anchor;
7165 /* Create a new anchor with a unique label. */
7166 ASM_GENERATE_INTERNAL_LABEL (label, "LANCHOR", anchor_labelno++);
7167 anchor = create_block_symbol (ggc_strdup (label), block, offset);
7168 SYMBOL_REF_FLAGS (anchor) |= SYMBOL_FLAG_LOCAL | SYMBOL_FLAG_ANCHOR;
7169 SYMBOL_REF_FLAGS (anchor) |= model << SYMBOL_FLAG_TLS_SHIFT;
7171 /* Insert it at index BEGIN. */
7172 vec_safe_insert (block->anchors, begin, anchor);
7173 return anchor;
7176 /* Output the objects in BLOCK. */
7178 static void
7179 output_object_block (struct object_block *block)
7181 struct constant_descriptor_rtx *desc;
7182 unsigned int i;
7183 HOST_WIDE_INT offset;
7184 tree decl;
7185 rtx symbol;
7187 if (!block->objects)
7188 return;
7190 /* Switch to the section and make sure that the first byte is
7191 suitably aligned. */
7192 switch_to_section (block->sect);
7193 assemble_align (block->alignment);
7195 /* Define the values of all anchors relative to the current section
7196 position. */
7197 FOR_EACH_VEC_SAFE_ELT (block->anchors, i, symbol)
7198 targetm.asm_out.output_anchor (symbol);
7200 /* Output the objects themselves. */
7201 offset = 0;
7202 FOR_EACH_VEC_ELT (*block->objects, i, symbol)
7204 /* Move to the object's offset, padding with zeros if necessary. */
7205 assemble_zeros (SYMBOL_REF_BLOCK_OFFSET (symbol) - offset);
7206 offset = SYMBOL_REF_BLOCK_OFFSET (symbol);
7207 if (CONSTANT_POOL_ADDRESS_P (symbol))
7209 desc = SYMBOL_REF_CONSTANT (symbol);
7210 output_constant_pool_1 (desc, 1);
7211 offset += GET_MODE_SIZE (desc->mode);
7213 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
7215 HOST_WIDE_INT size;
7216 decl = SYMBOL_REF_DECL (symbol);
7217 assemble_constant_contents (DECL_INITIAL (decl), XSTR (symbol, 0),
7218 DECL_ALIGN (decl));
7219 size = get_constant_size (DECL_INITIAL (decl));
7220 offset += size;
7221 if ((flag_sanitize & SANITIZE_ADDRESS)
7222 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
7223 && asan_protect_global (DECL_INITIAL (decl)))
7225 size = asan_red_zone_size (size);
7226 assemble_zeros (size);
7227 offset += size;
7230 else
7232 HOST_WIDE_INT size;
7233 decl = SYMBOL_REF_DECL (symbol);
7234 assemble_variable_contents (decl, XSTR (symbol, 0), false);
7235 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
7236 offset += size;
7237 if ((flag_sanitize & SANITIZE_ADDRESS)
7238 && asan_protect_global (decl))
7240 size = asan_red_zone_size (size);
7241 assemble_zeros (size);
7242 offset += size;
7248 /* A htab_traverse callback used to call output_object_block for
7249 each member of object_block_htab. */
7252 output_object_block_htab (object_block **slot, void *)
7254 output_object_block (*slot);
7255 return 1;
7258 /* Output the definitions of all object_blocks. */
7260 void
7261 output_object_blocks (void)
7263 object_block_htab->traverse<void *, output_object_block_htab> (NULL);
7266 /* This function provides a possible implementation of the
7267 TARGET_ASM_RECORD_GCC_SWITCHES target hook for ELF targets. When triggered
7268 by -frecord-gcc-switches it creates a new mergeable, string section in the
7269 assembler output file called TARGET_ASM_RECORD_GCC_SWITCHES_SECTION which
7270 contains the switches in ASCII format.
7272 FIXME: This code does not correctly handle double quote characters
7273 that appear inside strings, (it strips them rather than preserving them).
7274 FIXME: ASM_OUTPUT_ASCII, as defined in config/elfos.h will not emit NUL
7275 characters - instead it treats them as sub-string separators. Since
7276 we want to emit NUL strings terminators into the object file we have to use
7277 ASM_OUTPUT_SKIP. */
7280 elf_record_gcc_switches (print_switch_type type, const char * name)
7282 switch (type)
7284 case SWITCH_TYPE_PASSED:
7285 ASM_OUTPUT_ASCII (asm_out_file, name, strlen (name));
7286 ASM_OUTPUT_SKIP (asm_out_file, (unsigned HOST_WIDE_INT) 1);
7287 break;
7289 case SWITCH_TYPE_DESCRIPTIVE:
7290 if (name == NULL)
7292 /* Distinguish between invocations where name is NULL. */
7293 static bool started = false;
7295 if (!started)
7297 section * sec;
7299 sec = get_section (targetm.asm_out.record_gcc_switches_section,
7300 SECTION_DEBUG
7301 | SECTION_MERGE
7302 | SECTION_STRINGS
7303 | (SECTION_ENTSIZE & 1),
7304 NULL);
7305 switch_to_section (sec);
7306 started = true;
7310 default:
7311 break;
7314 /* The return value is currently ignored by the caller, but must be 0.
7315 For -fverbose-asm the return value would be the number of characters
7316 emitted into the assembler file. */
7317 return 0;
7320 /* Emit text to declare externally defined symbols. It is needed to
7321 properly support non-default visibility. */
7322 void
7323 default_elf_asm_output_external (FILE *file ATTRIBUTE_UNUSED,
7324 tree decl,
7325 const char *name ATTRIBUTE_UNUSED)
7327 /* We output the name if and only if TREE_SYMBOL_REFERENCED is
7328 set in order to avoid putting out names that are never really
7329 used. */
7330 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
7331 && targetm.binds_local_p (decl))
7332 maybe_assemble_visibility (decl);
7335 /* The default hook for TARGET_ASM_OUTPUT_SOURCE_FILENAME. */
7337 void
7338 default_asm_output_source_filename (FILE *file, const char *name)
7340 #ifdef ASM_OUTPUT_SOURCE_FILENAME
7341 ASM_OUTPUT_SOURCE_FILENAME (file, name);
7342 #else
7343 fprintf (file, "\t.file\t");
7344 output_quoted_string (file, name);
7345 putc ('\n', file);
7346 #endif
7349 /* Output a file name in the form wanted by System V. */
7351 void
7352 output_file_directive (FILE *asm_file, const char *input_name)
7354 int len;
7355 const char *na;
7357 if (input_name == NULL)
7358 input_name = "<stdin>";
7359 else
7360 input_name = remap_debug_filename (input_name);
7362 len = strlen (input_name);
7363 na = input_name + len;
7365 /* NA gets INPUT_NAME sans directory names. */
7366 while (na > input_name)
7368 if (IS_DIR_SEPARATOR (na[-1]))
7369 break;
7370 na--;
7373 targetm.asm_out.output_source_filename (asm_file, na);
7376 /* Create a DEBUG_EXPR_DECL / DEBUG_EXPR pair from RTL expression
7377 EXP. */
7379 make_debug_expr_from_rtl (const_rtx exp)
7381 tree ddecl = make_node (DEBUG_EXPR_DECL), type;
7382 enum machine_mode mode = GET_MODE (exp);
7383 rtx dval;
7385 DECL_ARTIFICIAL (ddecl) = 1;
7386 if (REG_P (exp) && REG_EXPR (exp))
7387 type = TREE_TYPE (REG_EXPR (exp));
7388 else if (MEM_P (exp) && MEM_EXPR (exp))
7389 type = TREE_TYPE (MEM_EXPR (exp));
7390 else
7391 type = NULL_TREE;
7392 if (type && TYPE_MODE (type) == mode)
7393 TREE_TYPE (ddecl) = type;
7394 else
7395 TREE_TYPE (ddecl) = lang_hooks.types.type_for_mode (mode, 1);
7396 DECL_MODE (ddecl) = mode;
7397 dval = gen_rtx_DEBUG_EXPR (mode);
7398 DEBUG_EXPR_TREE_DECL (dval) = ddecl;
7399 SET_DECL_RTL (ddecl, dval);
7400 return dval;
7403 #ifdef ELF_ASCII_ESCAPES
7404 /* Default ASM_OUTPUT_LIMITED_STRING for ELF targets. */
7406 void
7407 default_elf_asm_output_limited_string (FILE *f, const char *s)
7409 int escape;
7410 unsigned char c;
7412 fputs (STRING_ASM_OP, f);
7413 putc ('"', f);
7414 while (*s != '\0')
7416 c = *s;
7417 escape = ELF_ASCII_ESCAPES[c];
7418 switch (escape)
7420 case 0:
7421 putc (c, f);
7422 break;
7423 case 1:
7424 /* TODO: Print in hex with fast function, important for -flto. */
7425 fprintf (f, "\\%03o", c);
7426 break;
7427 default:
7428 putc ('\\', f);
7429 putc (escape, f);
7430 break;
7432 s++;
7434 putc ('\"', f);
7435 putc ('\n', f);
7438 /* Default ASM_OUTPUT_ASCII for ELF targets. */
7440 void
7441 default_elf_asm_output_ascii (FILE *f, const char *s, unsigned int len)
7443 const char *limit = s + len;
7444 const char *last_null = NULL;
7445 unsigned bytes_in_chunk = 0;
7446 unsigned char c;
7447 int escape;
7449 for (; s < limit; s++)
7451 const char *p;
7453 if (bytes_in_chunk >= 60)
7455 putc ('\"', f);
7456 putc ('\n', f);
7457 bytes_in_chunk = 0;
7460 if (s > last_null)
7462 for (p = s; p < limit && *p != '\0'; p++)
7463 continue;
7464 last_null = p;
7466 else
7467 p = last_null;
7469 if (p < limit && (p - s) <= (long) ELF_STRING_LIMIT)
7471 if (bytes_in_chunk > 0)
7473 putc ('\"', f);
7474 putc ('\n', f);
7475 bytes_in_chunk = 0;
7478 default_elf_asm_output_limited_string (f, s);
7479 s = p;
7481 else
7483 if (bytes_in_chunk == 0)
7484 fputs (ASCII_DATA_ASM_OP "\"", f);
7486 c = *s;
7487 escape = ELF_ASCII_ESCAPES[c];
7488 switch (escape)
7490 case 0:
7491 putc (c, f);
7492 bytes_in_chunk++;
7493 break;
7494 case 1:
7495 /* TODO: Print in hex with fast function, important for -flto. */
7496 fprintf (f, "\\%03o", c);
7497 bytes_in_chunk += 4;
7498 break;
7499 default:
7500 putc ('\\', f);
7501 putc (escape, f);
7502 bytes_in_chunk += 2;
7503 break;
7509 if (bytes_in_chunk > 0)
7511 putc ('\"', f);
7512 putc ('\n', f);
7515 #endif
7517 static GTY(()) section *elf_init_array_section;
7518 static GTY(()) section *elf_fini_array_section;
7520 static section *
7521 get_elf_initfini_array_priority_section (int priority,
7522 bool constructor_p)
7524 section *sec;
7525 if (priority != DEFAULT_INIT_PRIORITY)
7527 char buf[18];
7528 sprintf (buf, "%s.%.5u",
7529 constructor_p ? ".init_array" : ".fini_array",
7530 priority);
7531 sec = get_section (buf, SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
7533 else
7535 if (constructor_p)
7537 if (elf_init_array_section == NULL)
7538 elf_init_array_section
7539 = get_section (".init_array",
7540 SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
7541 sec = elf_init_array_section;
7543 else
7545 if (elf_fini_array_section == NULL)
7546 elf_fini_array_section
7547 = get_section (".fini_array",
7548 SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
7549 sec = elf_fini_array_section;
7552 return sec;
7555 /* Use .init_array section for constructors. */
7557 void
7558 default_elf_init_array_asm_out_constructor (rtx symbol, int priority)
7560 section *sec = get_elf_initfini_array_priority_section (priority,
7561 true);
7562 assemble_addr_to_section (symbol, sec);
7565 /* Use .fini_array section for destructors. */
7567 void
7568 default_elf_fini_array_asm_out_destructor (rtx symbol, int priority)
7570 section *sec = get_elf_initfini_array_priority_section (priority,
7571 false);
7572 assemble_addr_to_section (symbol, sec);
7575 /* Default TARGET_ASM_OUTPUT_IDENT hook.
7577 This is a bit of a cheat. The real default is a no-op, but this
7578 hook is the default for all targets with a .ident directive. */
7580 void
7581 default_asm_output_ident_directive (const char *ident_str)
7583 const char *ident_asm_op = "\t.ident\t";
7585 /* If we are still in the front end, do not write out the string
7586 to asm_out_file. Instead, add a fake top-level asm statement.
7587 This allows the front ends to use this hook without actually
7588 writing to asm_out_file, to handle #ident or Pragma Ident. */
7589 if (symtab->state == PARSING)
7591 char *buf = ACONCAT ((ident_asm_op, "\"", ident_str, "\"\n", NULL));
7592 symtab->finalize_toplevel_asm (build_string (strlen (buf), buf));
7594 else
7595 fprintf (asm_out_file, "%s\"%s\"\n", ident_asm_op, ident_str);
7598 #include "gt-varasm.h"