Merge from trunk @217148.
[official-gcc.git] / gcc / varasm.c
blobdd39c90ebc77420216ab955cba56e0a779f9453e
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 "predict.h"
57 #include "dominance.h"
58 #include "cfg.h"
59 #include "basic-block.h"
60 #include "hash-map.h"
61 #include "is-a.h"
62 #include "plugin-api.h"
63 #include "ipa-ref.h"
64 #include "cgraph.h"
65 #include "asan.h"
66 #include "rtl-iter.h"
67 #include "tree-chkp.h"
69 #ifdef XCOFF_DEBUGGING_INFO
70 #include "xcoffout.h" /* Needed for external data
71 declarations for e.g. AIX 4.x. */
72 #endif
74 /* The (assembler) name of the first globally-visible object output. */
75 extern GTY(()) const char *first_global_object_name;
76 extern GTY(()) const char *weak_global_object_name;
78 const char *first_global_object_name;
79 const char *weak_global_object_name;
81 struct addr_const;
82 struct constant_descriptor_rtx;
83 struct rtx_constant_pool;
85 #define n_deferred_constants (crtl->varasm.deferred_constants)
87 /* Number for making the label on the next
88 constant that is stored in memory. */
90 static GTY(()) int const_labelno;
92 /* Carry information from ASM_DECLARE_OBJECT_NAME
93 to ASM_FINISH_DECLARE_OBJECT. */
95 int size_directive_output;
97 /* The last decl for which assemble_variable was called,
98 if it did ASM_DECLARE_OBJECT_NAME.
99 If the last call to assemble_variable didn't do that,
100 this holds 0. */
102 tree last_assemble_variable_decl;
104 /* The following global variable indicates if the first basic block
105 in a function belongs to the cold partition or not. */
107 bool first_function_block_is_cold;
109 /* Whether we saw any functions with no_split_stack. */
111 static bool saw_no_split_stack;
113 static const char *strip_reg_name (const char *);
114 static int contains_pointers_p (tree);
115 #ifdef ASM_OUTPUT_EXTERNAL
116 static bool incorporeal_function_p (tree);
117 #endif
118 static void decode_addr_const (tree, struct addr_const *);
119 static hashval_t const_hash_1 (const tree);
120 static int compare_constant (const tree, const tree);
121 static void output_constant_def_contents (rtx);
122 static void output_addressed_constants (tree);
123 static unsigned HOST_WIDE_INT output_constant (tree, unsigned HOST_WIDE_INT,
124 unsigned int, bool);
125 static void globalize_decl (tree);
126 static bool decl_readonly_section_1 (enum section_category);
127 #ifdef BSS_SECTION_ASM_OP
128 #ifdef ASM_OUTPUT_ALIGNED_BSS
129 static void asm_output_aligned_bss (FILE *, tree, const char *,
130 unsigned HOST_WIDE_INT, int)
131 ATTRIBUTE_UNUSED;
132 #endif
133 #endif /* BSS_SECTION_ASM_OP */
134 static void mark_weak (tree);
135 static void output_constant_pool (const char *, tree);
137 /* Well-known sections, each one associated with some sort of *_ASM_OP. */
138 section *text_section;
139 section *data_section;
140 section *readonly_data_section;
141 section *sdata_section;
142 section *ctors_section;
143 section *dtors_section;
144 section *bss_section;
145 section *sbss_section;
147 /* Various forms of common section. All are guaranteed to be nonnull. */
148 section *tls_comm_section;
149 section *comm_section;
150 section *lcomm_section;
152 /* A SECTION_NOSWITCH section used for declaring global BSS variables.
153 May be null. */
154 section *bss_noswitch_section;
156 /* The section that holds the main exception table, when known. The section
157 is set either by the target's init_sections hook or by the first call to
158 switch_to_exception_section. */
159 section *exception_section;
161 /* The section that holds the DWARF2 frame unwind information, when known.
162 The section is set either by the target's init_sections hook or by the
163 first call to switch_to_eh_frame_section. */
164 section *eh_frame_section;
166 /* asm_out_file's current section. This is NULL if no section has yet
167 been selected or if we lose track of what the current section is. */
168 section *in_section;
170 /* True if code for the current function is currently being directed
171 at the cold section. */
172 bool in_cold_section_p;
174 /* A linked list of all the unnamed sections. */
175 static GTY(()) section *unnamed_sections;
177 /* Return a nonzero value if DECL has a section attribute. */
178 #define IN_NAMED_SECTION(DECL) \
179 ((TREE_CODE (DECL) == FUNCTION_DECL || TREE_CODE (DECL) == VAR_DECL) \
180 && DECL_SECTION_NAME (DECL) != NULL)
182 struct section_hasher : ggc_hasher<section *>
184 typedef const char *compare_type;
186 static hashval_t hash (section *);
187 static bool equal (section *, const char *);
190 /* Hash table of named sections. */
191 static GTY(()) hash_table<section_hasher> *section_htab;
193 struct object_block_hasher : ggc_hasher<object_block *>
195 typedef const section *compare_type;
197 static hashval_t hash (object_block *);
198 static bool equal (object_block *, const section *);
201 /* A table of object_blocks, indexed by section. */
202 static GTY(()) hash_table<object_block_hasher> *object_block_htab;
204 /* The next number to use for internal anchor labels. */
205 static GTY(()) int anchor_labelno;
207 /* A pool of constants that can be shared between functions. */
208 static GTY(()) struct rtx_constant_pool *shared_constant_pool;
210 /* Helper routines for maintaining section_htab. */
212 bool
213 section_hasher::equal (section *old, const char *new_name)
215 return strcmp (old->named.name, new_name) == 0;
218 hashval_t
219 section_hasher::hash (section *old)
221 return htab_hash_string (old->named.name);
224 /* Return a hash value for section SECT. */
226 static hashval_t
227 hash_section (section *sect)
229 if (sect->common.flags & SECTION_NAMED)
230 return htab_hash_string (sect->named.name);
231 return sect->common.flags;
234 /* Helper routines for maintaining object_block_htab. */
236 inline bool
237 object_block_hasher::equal (object_block *old, const section *new_section)
239 return old->sect == new_section;
242 hashval_t
243 object_block_hasher::hash (object_block *old)
245 return hash_section (old->sect);
248 /* Return a new unnamed section with the given fields. */
250 section *
251 get_unnamed_section (unsigned int flags, void (*callback) (const void *),
252 const void *data)
254 section *sect;
256 sect = ggc_alloc<section> ();
257 sect->unnamed.common.flags = flags | SECTION_UNNAMED;
258 sect->unnamed.callback = callback;
259 sect->unnamed.data = data;
260 sect->unnamed.next = unnamed_sections;
262 unnamed_sections = sect;
263 return sect;
266 /* Return a SECTION_NOSWITCH section with the given fields. */
268 static section *
269 get_noswitch_section (unsigned int flags, noswitch_section_callback callback)
271 section *sect;
273 sect = ggc_alloc<section> ();
274 sect->noswitch.common.flags = flags | SECTION_NOSWITCH;
275 sect->noswitch.callback = callback;
277 return sect;
280 /* Return the named section structure associated with NAME. Create
281 a new section with the given fields if no such structure exists. */
283 section *
284 get_section (const char *name, unsigned int flags, tree decl)
286 section *sect, **slot;
288 slot = section_htab->find_slot_with_hash (name, htab_hash_string (name),
289 INSERT);
290 flags |= SECTION_NAMED;
291 if (*slot == NULL)
293 sect = ggc_alloc<section> ();
294 sect->named.common.flags = flags;
295 sect->named.name = ggc_strdup (name);
296 sect->named.decl = decl;
297 *slot = sect;
299 else
301 sect = *slot;
302 if ((sect->common.flags & ~SECTION_DECLARED) != flags
303 && ((sect->common.flags | flags) & SECTION_OVERRIDE) == 0)
305 /* It is fine if one of the section flags is
306 SECTION_WRITE | SECTION_RELRO and the other has none of these
307 flags (i.e. read-only) in named sections and either the
308 section hasn't been declared yet or has been declared as writable.
309 In that case just make sure the resulting flags are
310 SECTION_WRITE | SECTION_RELRO, ie. writable only because of
311 relocations. */
312 if (((sect->common.flags ^ flags) & (SECTION_WRITE | SECTION_RELRO))
313 == (SECTION_WRITE | SECTION_RELRO)
314 && (sect->common.flags
315 & ~(SECTION_DECLARED | SECTION_WRITE | SECTION_RELRO))
316 == (flags & ~(SECTION_WRITE | SECTION_RELRO))
317 && ((sect->common.flags & SECTION_DECLARED) == 0
318 || (sect->common.flags & SECTION_WRITE)))
320 sect->common.flags |= (SECTION_WRITE | SECTION_RELRO);
321 return sect;
323 /* Sanity check user variables for flag changes. */
324 if (sect->named.decl != NULL
325 && DECL_P (sect->named.decl)
326 && decl != sect->named.decl)
328 if (decl != NULL && DECL_P (decl))
329 error ("%+D causes a section type conflict with %D",
330 decl, sect->named.decl);
331 else
332 error ("section type conflict with %D", sect->named.decl);
333 inform (DECL_SOURCE_LOCATION (sect->named.decl),
334 "%qD was declared here", sect->named.decl);
336 else if (decl != NULL && DECL_P (decl))
337 error ("%+D causes a section type conflict", decl);
338 else
339 error ("section type conflict");
340 /* Make sure we don't error about one section multiple times. */
341 sect->common.flags |= SECTION_OVERRIDE;
344 return sect;
347 /* Return true if the current compilation mode benefits from having
348 objects grouped into blocks. */
350 static bool
351 use_object_blocks_p (void)
353 return flag_section_anchors;
356 /* Return the object_block structure for section SECT. Create a new
357 structure if we haven't created one already. Return null if SECT
358 itself is null. */
360 static struct object_block *
361 get_block_for_section (section *sect)
363 struct object_block *block;
365 if (sect == NULL)
366 return NULL;
368 object_block **slot
369 = object_block_htab->find_slot_with_hash (sect, hash_section (sect),
370 INSERT);
371 block = *slot;
372 if (block == NULL)
374 block = ggc_cleared_alloc<object_block> ();
375 block->sect = sect;
376 *slot = block;
378 return block;
381 /* Create a symbol with label LABEL and place it at byte offset
382 OFFSET in BLOCK. OFFSET can be negative if the symbol's offset
383 is not yet known. LABEL must be a garbage-collected string. */
385 static rtx
386 create_block_symbol (const char *label, struct object_block *block,
387 HOST_WIDE_INT offset)
389 rtx symbol;
390 unsigned int size;
392 /* Create the extended SYMBOL_REF. */
393 size = RTX_HDR_SIZE + sizeof (struct block_symbol);
394 symbol = (rtx) ggc_internal_alloc (size);
396 /* Initialize the normal SYMBOL_REF fields. */
397 memset (symbol, 0, size);
398 PUT_CODE (symbol, SYMBOL_REF);
399 PUT_MODE (symbol, Pmode);
400 XSTR (symbol, 0) = label;
401 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_HAS_BLOCK_INFO;
403 /* Initialize the block_symbol stuff. */
404 SYMBOL_REF_BLOCK (symbol) = block;
405 SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
407 return symbol;
410 /* Return a section with a particular name and with whatever SECTION_*
411 flags section_type_flags deems appropriate. The name of the section
412 is taken from NAME if nonnull, otherwise it is taken from DECL's
413 DECL_SECTION_NAME. DECL is the decl associated with the section
414 (see the section comment for details) and RELOC is as for
415 section_type_flags. */
417 section *
418 get_named_section (tree decl, const char *name, int reloc)
420 unsigned int flags;
422 if (name == NULL)
424 gcc_assert (decl && DECL_P (decl) && DECL_SECTION_NAME (decl));
425 name = DECL_SECTION_NAME (decl);
428 flags = targetm.section_type_flags (decl, name, reloc);
429 return get_section (name, flags, decl);
432 /* Worker for resolve_unique_section. */
434 static bool
435 set_implicit_section (struct symtab_node *n, void *data ATTRIBUTE_UNUSED)
437 n->implicit_section = true;
438 return false;
441 /* If required, set DECL_SECTION_NAME to a unique name. */
443 void
444 resolve_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED,
445 int flag_function_or_data_sections)
447 if (DECL_SECTION_NAME (decl) == NULL
448 && targetm_common.have_named_sections
449 && (flag_function_or_data_sections
450 || DECL_COMDAT_GROUP (decl)))
452 targetm.asm_out.unique_section (decl, reloc);
453 if (DECL_SECTION_NAME (decl))
454 symtab_node::get (decl)->call_for_symbol_and_aliases
455 (set_implicit_section, NULL, true);
459 #ifdef BSS_SECTION_ASM_OP
461 #ifdef ASM_OUTPUT_ALIGNED_BSS
463 /* Utility function for targets to use in implementing
464 ASM_OUTPUT_ALIGNED_BSS.
465 ??? It is believed that this function will work in most cases so such
466 support is localized here. */
468 static void
469 asm_output_aligned_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
470 const char *name, unsigned HOST_WIDE_INT size,
471 int align)
473 switch_to_section (bss_section);
474 ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
475 #ifdef ASM_DECLARE_OBJECT_NAME
476 last_assemble_variable_decl = decl;
477 ASM_DECLARE_OBJECT_NAME (file, name, decl);
478 #else
479 /* Standard thing is just output label for the object. */
480 ASM_OUTPUT_LABEL (file, name);
481 #endif /* ASM_DECLARE_OBJECT_NAME */
482 ASM_OUTPUT_SKIP (file, size ? size : 1);
485 #endif
487 #endif /* BSS_SECTION_ASM_OP */
489 #ifndef USE_SELECT_SECTION_FOR_FUNCTIONS
490 /* Return the hot section for function DECL. Return text_section for
491 null DECLs. */
493 static section *
494 hot_function_section (tree decl)
496 if (decl != NULL_TREE
497 && DECL_SECTION_NAME (decl) != NULL
498 && targetm_common.have_named_sections)
499 return get_named_section (decl, NULL, 0);
500 else
501 return text_section;
503 #endif
505 /* Return section for TEXT_SECTION_NAME if DECL or DECL_SECTION_NAME (DECL)
506 is NULL.
508 When DECL_SECTION_NAME is non-NULL and it is implicit section and
509 NAMED_SECTION_SUFFIX is non-NULL, then produce section called
510 concatenate the name with NAMED_SECTION_SUFFIX.
511 Otherwise produce "TEXT_SECTION_NAME.IMPLICIT_NAME". */
513 section *
514 get_named_text_section (tree decl,
515 const char *text_section_name,
516 const char *named_section_suffix)
518 if (decl && DECL_SECTION_NAME (decl))
520 if (named_section_suffix)
522 const char *dsn = DECL_SECTION_NAME (decl);
523 const char *stripped_name;
524 char *name, *buffer;
526 name = (char *) alloca (strlen (dsn) + 1);
527 memcpy (name, dsn,
528 strlen (dsn) + 1);
530 stripped_name = targetm.strip_name_encoding (name);
532 buffer = ACONCAT ((stripped_name, named_section_suffix, NULL));
533 return get_named_section (decl, buffer, 0);
535 else if (symtab_node::get (decl)->implicit_section)
537 const char *name;
539 /* Do not try to split gnu_linkonce functions. This gets somewhat
540 slipperly. */
541 if (DECL_COMDAT_GROUP (decl) && !HAVE_COMDAT_GROUP)
542 return NULL;
543 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
544 name = targetm.strip_name_encoding (name);
545 return get_named_section (decl, ACONCAT ((text_section_name, ".",
546 name, NULL)), 0);
548 else
549 return NULL;
551 return get_named_section (decl, text_section_name, 0);
554 /* Choose named function section based on its frequency. */
556 section *
557 default_function_section (tree decl, enum node_frequency freq,
558 bool startup, bool exit)
560 #if defined HAVE_LD_EH_GC_SECTIONS && defined HAVE_LD_EH_GC_SECTIONS_BUG
561 /* Old GNU linkers have buggy --gc-section support, which sometimes
562 results in .gcc_except_table* sections being garbage collected. */
563 if (decl
564 && symtab_node::get (decl)->implicit_section)
565 return NULL;
566 #endif
568 if (!flag_reorder_functions
569 || !targetm_common.have_named_sections)
570 return NULL;
571 /* Startup code should go to startup subsection unless it is
572 unlikely executed (this happens especially with function splitting
573 where we can split away unnecessary parts of static constructors. */
574 if (startup && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
576 /* If we do have a profile or(and) LTO phase is executed, we do not need
577 these ELF section. */
578 if (!in_lto_p || !flag_profile_values)
579 return get_named_text_section (decl, ".text.startup", NULL);
580 else
581 return NULL;
584 /* Similarly for exit. */
585 if (exit && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
586 return get_named_text_section (decl, ".text.exit", NULL);
588 /* Group cold functions together, similarly for hot code. */
589 switch (freq)
591 case NODE_FREQUENCY_UNLIKELY_EXECUTED:
592 return get_named_text_section (decl, ".text.unlikely", NULL);
593 case NODE_FREQUENCY_HOT:
594 /* If we do have a profile or(and) LTO phase is executed, we do not need
595 these ELF section. */
596 if (!in_lto_p || !flag_profile_values)
597 return get_named_text_section (decl, ".text.hot", NULL);
598 default:
599 return NULL;
603 /* Return the section for function DECL.
605 If DECL is NULL_TREE, return the text section. We can be passed
606 NULL_TREE under some circumstances by dbxout.c at least.
608 If FORCE_COLD is true, return cold function section ignoring
609 the frequency info of cgraph_node. */
611 static section *
612 function_section_1 (tree decl, bool force_cold)
614 section *section = NULL;
615 enum node_frequency freq = NODE_FREQUENCY_NORMAL;
616 bool startup = false, exit = false;
618 if (decl)
620 struct cgraph_node *node = cgraph_node::get (decl);
622 if (node)
624 freq = node->frequency;
625 startup = node->only_called_at_startup;
626 exit = node->only_called_at_exit;
629 if (force_cold)
630 freq = NODE_FREQUENCY_UNLIKELY_EXECUTED;
632 #ifdef USE_SELECT_SECTION_FOR_FUNCTIONS
633 if (decl != NULL_TREE
634 && DECL_SECTION_NAME (decl) != NULL)
636 if (targetm.asm_out.function_section)
637 section = targetm.asm_out.function_section (decl, freq,
638 startup, exit);
639 if (section)
640 return section;
641 return get_named_section (decl, NULL, 0);
643 else
644 return targetm.asm_out.select_section
645 (decl, freq == NODE_FREQUENCY_UNLIKELY_EXECUTED,
646 DECL_ALIGN (decl));
647 #else
648 if (targetm.asm_out.function_section)
649 section = targetm.asm_out.function_section (decl, freq, startup, exit);
650 if (section)
651 return section;
652 return hot_function_section (decl);
653 #endif
656 /* Return the section for function DECL.
658 If DECL is NULL_TREE, return the text section. We can be passed
659 NULL_TREE under some circumstances by dbxout.c at least. */
661 section *
662 function_section (tree decl)
664 /* Handle cases where function splitting code decides
665 to put function entry point into unlikely executed section
666 despite the fact that the function itself is not cold
667 (i.e. it is called rarely but contains a hot loop that is
668 better to live in hot subsection for the code locality). */
669 return function_section_1 (decl,
670 first_function_block_is_cold);
673 /* Return the section for the current function, take IN_COLD_SECTION_P
674 into account. */
676 section *
677 current_function_section (void)
679 return function_section_1 (current_function_decl, in_cold_section_p);
682 /* Tell assembler to switch to unlikely-to-be-executed text section. */
684 section *
685 unlikely_text_section (void)
687 return function_section_1 (current_function_decl, true);
690 /* When called within a function context, return true if the function
691 has been assigned a cold text section and if SECT is that section.
692 When called outside a function context, return true if SECT is the
693 default cold section. */
695 bool
696 unlikely_text_section_p (section *sect)
698 return sect == function_section_1 (current_function_decl, true);
701 /* Return the read-only data section associated with function DECL. */
703 section *
704 default_function_rodata_section (tree decl)
706 if (decl != NULL_TREE && DECL_SECTION_NAME (decl))
708 const char *name = DECL_SECTION_NAME (decl);
710 if (DECL_COMDAT_GROUP (decl) && HAVE_COMDAT_GROUP)
712 const char *dot;
713 size_t len;
714 char* rname;
716 dot = strchr (name + 1, '.');
717 if (!dot)
718 dot = name;
719 len = strlen (dot) + 8;
720 rname = (char *) alloca (len);
722 strcpy (rname, ".rodata");
723 strcat (rname, dot);
724 return get_section (rname, SECTION_LINKONCE, decl);
726 /* For .gnu.linkonce.t.foo we want to use .gnu.linkonce.r.foo. */
727 else if (DECL_COMDAT_GROUP (decl)
728 && strncmp (name, ".gnu.linkonce.t.", 16) == 0)
730 size_t len = strlen (name) + 1;
731 char *rname = (char *) alloca (len);
733 memcpy (rname, name, len);
734 rname[14] = 'r';
735 return get_section (rname, SECTION_LINKONCE, decl);
737 /* For .text.foo we want to use .rodata.foo. */
738 else if (flag_function_sections && flag_data_sections
739 && strncmp (name, ".text.", 6) == 0)
741 size_t len = strlen (name) + 1;
742 char *rname = (char *) alloca (len + 2);
744 memcpy (rname, ".rodata", 7);
745 memcpy (rname + 7, name + 5, len - 5);
746 return get_section (rname, 0, decl);
750 return readonly_data_section;
753 /* Return the read-only data section associated with function DECL
754 for targets where that section should be always the single
755 readonly data section. */
757 section *
758 default_no_function_rodata_section (tree decl ATTRIBUTE_UNUSED)
760 return readonly_data_section;
763 /* Return the section to use for string merging. */
765 static section *
766 mergeable_string_section (tree decl ATTRIBUTE_UNUSED,
767 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
768 unsigned int flags ATTRIBUTE_UNUSED)
770 HOST_WIDE_INT len;
772 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
773 && TREE_CODE (decl) == STRING_CST
774 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
775 && align <= 256
776 && (len = int_size_in_bytes (TREE_TYPE (decl))) > 0
777 && TREE_STRING_LENGTH (decl) >= len)
779 machine_mode mode;
780 unsigned int modesize;
781 const char *str;
782 HOST_WIDE_INT i;
783 int j, unit;
784 const char *prefix = targetm.asm_out.mergeable_rodata_prefix;
785 char *name = (char *) alloca (strlen (prefix) + 30);
787 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (decl)));
788 modesize = GET_MODE_BITSIZE (mode);
789 if (modesize >= 8 && modesize <= 256
790 && (modesize & (modesize - 1)) == 0)
792 if (align < modesize)
793 align = modesize;
795 str = TREE_STRING_POINTER (decl);
796 unit = GET_MODE_SIZE (mode);
798 /* Check for embedded NUL characters. */
799 for (i = 0; i < len; i += unit)
801 for (j = 0; j < unit; j++)
802 if (str[i + j] != '\0')
803 break;
804 if (j == unit)
805 break;
807 if (i == len - unit)
809 sprintf (name, "%s.str%d.%d", prefix,
810 modesize / 8, (int) (align / 8));
811 flags |= (modesize / 8) | SECTION_MERGE | SECTION_STRINGS;
812 return get_section (name, flags, NULL);
817 return readonly_data_section;
820 /* Return the section to use for constant merging. */
822 section *
823 mergeable_constant_section (machine_mode mode ATTRIBUTE_UNUSED,
824 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
825 unsigned int flags ATTRIBUTE_UNUSED)
827 unsigned int modesize = GET_MODE_BITSIZE (mode);
829 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
830 && mode != VOIDmode
831 && mode != BLKmode
832 && modesize <= align
833 && align >= 8
834 && align <= 256
835 && (align & (align - 1)) == 0)
837 const char *prefix = targetm.asm_out.mergeable_rodata_prefix;
838 char *name = (char *) alloca (strlen (prefix) + 30);
840 sprintf (name, "%s.cst%d", prefix, (int) (align / 8));
841 flags |= (align / 8) | SECTION_MERGE;
842 return get_section (name, flags, NULL);
844 return readonly_data_section;
847 /* Given NAME, a putative register name, discard any customary prefixes. */
849 static const char *
850 strip_reg_name (const char *name)
852 #ifdef REGISTER_PREFIX
853 if (!strncmp (name, REGISTER_PREFIX, strlen (REGISTER_PREFIX)))
854 name += strlen (REGISTER_PREFIX);
855 #endif
856 if (name[0] == '%' || name[0] == '#')
857 name++;
858 return name;
861 /* The user has asked for a DECL to have a particular name. Set (or
862 change) it in such a way that we don't prefix an underscore to
863 it. */
864 void
865 set_user_assembler_name (tree decl, const char *name)
867 char *starred = (char *) alloca (strlen (name) + 2);
868 starred[0] = '*';
869 strcpy (starred + 1, name);
870 symtab->change_decl_assembler_name (decl, get_identifier (starred));
871 SET_DECL_RTL (decl, NULL_RTX);
874 /* Decode an `asm' spec for a declaration as a register name.
875 Return the register number, or -1 if nothing specified,
876 or -2 if the ASMSPEC is not `cc' or `memory' and is not recognized,
877 or -3 if ASMSPEC is `cc' and is not recognized,
878 or -4 if ASMSPEC is `memory' and is not recognized.
879 Accept an exact spelling or a decimal number.
880 Prefixes such as % are optional. */
883 decode_reg_name_and_count (const char *asmspec, int *pnregs)
885 /* Presume just one register is clobbered. */
886 *pnregs = 1;
888 if (asmspec != 0)
890 int i;
892 /* Get rid of confusing prefixes. */
893 asmspec = strip_reg_name (asmspec);
895 /* Allow a decimal number as a "register name". */
896 for (i = strlen (asmspec) - 1; i >= 0; i--)
897 if (! ISDIGIT (asmspec[i]))
898 break;
899 if (asmspec[0] != 0 && i < 0)
901 i = atoi (asmspec);
902 if (i < FIRST_PSEUDO_REGISTER && i >= 0 && reg_names[i][0])
903 return i;
904 else
905 return -2;
908 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
909 if (reg_names[i][0]
910 && ! strcmp (asmspec, strip_reg_name (reg_names[i])))
911 return i;
913 #ifdef OVERLAPPING_REGISTER_NAMES
915 static const struct
917 const char *const name;
918 const int number;
919 const int nregs;
920 } table[] = OVERLAPPING_REGISTER_NAMES;
922 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
923 if (table[i].name[0]
924 && ! strcmp (asmspec, table[i].name))
926 *pnregs = table[i].nregs;
927 return table[i].number;
930 #endif /* OVERLAPPING_REGISTER_NAMES */
932 #ifdef ADDITIONAL_REGISTER_NAMES
934 static const struct { const char *const name; const int number; } table[]
935 = ADDITIONAL_REGISTER_NAMES;
937 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
938 if (table[i].name[0]
939 && ! strcmp (asmspec, table[i].name)
940 && reg_names[table[i].number][0])
941 return table[i].number;
943 #endif /* ADDITIONAL_REGISTER_NAMES */
945 if (!strcmp (asmspec, "memory"))
946 return -4;
948 if (!strcmp (asmspec, "cc"))
949 return -3;
951 return -2;
954 return -1;
958 decode_reg_name (const char *name)
960 int count;
961 return decode_reg_name_and_count (name, &count);
965 /* Return true if DECL's initializer is suitable for a BSS section. */
967 bool
968 bss_initializer_p (const_tree decl)
970 return (DECL_INITIAL (decl) == NULL
971 /* In LTO we have no errors in program; error_mark_node is used
972 to mark offlined constructors. */
973 || (DECL_INITIAL (decl) == error_mark_node
974 && !in_lto_p)
975 || (flag_zero_initialized_in_bss
976 /* Leave constant zeroes in .rodata so they
977 can be shared. */
978 && !TREE_READONLY (decl)
979 && initializer_zerop (DECL_INITIAL (decl))));
982 /* Compute the alignment of variable specified by DECL.
983 DONT_OUTPUT_DATA is from assemble_variable. */
985 void
986 align_variable (tree decl, bool dont_output_data)
988 unsigned int align = DECL_ALIGN (decl);
990 /* In the case for initialing an array whose length isn't specified,
991 where we have not yet been able to do the layout,
992 figure out the proper alignment now. */
993 if (dont_output_data && DECL_SIZE (decl) == 0
994 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
995 align = MAX (align, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (decl))));
997 /* Some object file formats have a maximum alignment which they support.
998 In particular, a.out format supports a maximum alignment of 4. */
999 if (align > MAX_OFILE_ALIGNMENT)
1001 error ("alignment of %q+D is greater than maximum object "
1002 "file alignment %d", decl,
1003 MAX_OFILE_ALIGNMENT/BITS_PER_UNIT);
1004 align = MAX_OFILE_ALIGNMENT;
1007 if (! DECL_USER_ALIGN (decl))
1009 #ifdef DATA_ABI_ALIGNMENT
1010 unsigned int data_abi_align
1011 = DATA_ABI_ALIGNMENT (TREE_TYPE (decl), align);
1012 /* For backwards compatibility, don't assume the ABI alignment for
1013 TLS variables. */
1014 if (! DECL_THREAD_LOCAL_P (decl) || data_abi_align <= BITS_PER_WORD)
1015 align = data_abi_align;
1016 #endif
1018 /* On some machines, it is good to increase alignment sometimes.
1019 But as DECL_ALIGN is used both for actually emitting the variable
1020 and for code accessing the variable as guaranteed alignment, we
1021 can only increase the alignment if it is a performance optimization
1022 if the references to it must bind to the current definition. */
1023 if (decl_binds_to_current_def_p (decl)
1024 && !DECL_VIRTUAL_P (decl))
1026 #ifdef DATA_ALIGNMENT
1027 unsigned int data_align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
1028 /* Don't increase alignment too much for TLS variables - TLS space
1029 is too precious. */
1030 if (! DECL_THREAD_LOCAL_P (decl) || data_align <= BITS_PER_WORD)
1031 align = data_align;
1032 #endif
1033 #ifdef CONSTANT_ALIGNMENT
1034 if (DECL_INITIAL (decl) != 0
1035 /* In LTO we have no errors in program; error_mark_node is used
1036 to mark offlined constructors. */
1037 && (in_lto_p || DECL_INITIAL (decl) != error_mark_node))
1039 unsigned int const_align
1040 = CONSTANT_ALIGNMENT (DECL_INITIAL (decl), align);
1041 /* Don't increase alignment too much for TLS variables - TLS
1042 space is too precious. */
1043 if (! DECL_THREAD_LOCAL_P (decl) || const_align <= BITS_PER_WORD)
1044 align = const_align;
1046 #endif
1050 /* Reset the alignment in case we have made it tighter, so we can benefit
1051 from it in get_pointer_alignment. */
1052 DECL_ALIGN (decl) = align;
1055 /* Return DECL_ALIGN (decl), possibly increased for optimization purposes
1056 beyond what align_variable returned. */
1058 static unsigned int
1059 get_variable_align (tree decl)
1061 unsigned int align = DECL_ALIGN (decl);
1063 /* For user aligned vars or static vars align_variable already did
1064 everything. */
1065 if (DECL_USER_ALIGN (decl) || !TREE_PUBLIC (decl))
1066 return align;
1068 #ifdef DATA_ABI_ALIGNMENT
1069 if (DECL_THREAD_LOCAL_P (decl))
1070 align = DATA_ABI_ALIGNMENT (TREE_TYPE (decl), align);
1071 #endif
1073 /* For decls that bind to the current definition, align_variable
1074 did also everything, except for not assuming ABI required alignment
1075 of TLS variables. For other vars, increase the alignment here
1076 as an optimization. */
1077 if (!decl_binds_to_current_def_p (decl))
1079 /* On some machines, it is good to increase alignment sometimes. */
1080 #ifdef DATA_ALIGNMENT
1081 unsigned int data_align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
1082 /* Don't increase alignment too much for TLS variables - TLS space
1083 is too precious. */
1084 if (! DECL_THREAD_LOCAL_P (decl) || data_align <= BITS_PER_WORD)
1085 align = data_align;
1086 #endif
1087 #ifdef CONSTANT_ALIGNMENT
1088 if (DECL_INITIAL (decl) != 0
1089 /* In LTO we have no errors in program; error_mark_node is used
1090 to mark offlined constructors. */
1091 && (in_lto_p || DECL_INITIAL (decl) != error_mark_node))
1093 unsigned int const_align = CONSTANT_ALIGNMENT (DECL_INITIAL (decl),
1094 align);
1095 /* Don't increase alignment too much for TLS variables - TLS space
1096 is too precious. */
1097 if (! DECL_THREAD_LOCAL_P (decl) || const_align <= BITS_PER_WORD)
1098 align = const_align;
1100 #endif
1103 return align;
1106 /* Return the section into which the given VAR_DECL or CONST_DECL
1107 should be placed. PREFER_NOSWITCH_P is true if a noswitch
1108 section should be used wherever possible. */
1110 section *
1111 get_variable_section (tree decl, bool prefer_noswitch_p)
1113 addr_space_t as = ADDR_SPACE_GENERIC;
1114 int reloc;
1115 varpool_node *vnode = varpool_node::get (decl);
1116 if (vnode)
1118 vnode = vnode->ultimate_alias_target ();
1119 decl = vnode->decl;
1122 if (TREE_TYPE (decl) != error_mark_node)
1123 as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
1125 /* We need the constructor to figure out reloc flag. */
1126 if (vnode)
1127 vnode->get_constructor ();
1129 if (DECL_COMMON (decl))
1131 /* If the decl has been given an explicit section name, or it resides
1132 in a non-generic address space, then it isn't common, and shouldn't
1133 be handled as such. */
1134 gcc_assert (DECL_SECTION_NAME (decl) == NULL
1135 && ADDR_SPACE_GENERIC_P (as));
1136 if (DECL_THREAD_LOCAL_P (decl))
1137 return tls_comm_section;
1138 else if (TREE_PUBLIC (decl) && bss_initializer_p (decl))
1139 return comm_section;
1142 if (DECL_INITIAL (decl) == error_mark_node)
1143 reloc = contains_pointers_p (TREE_TYPE (decl)) ? 3 : 0;
1144 else if (DECL_INITIAL (decl))
1145 reloc = compute_reloc_for_constant (DECL_INITIAL (decl));
1146 else
1147 reloc = 0;
1149 resolve_unique_section (decl, reloc, flag_data_sections);
1150 if (IN_NAMED_SECTION (decl))
1151 return get_named_section (decl, NULL, reloc);
1153 if (ADDR_SPACE_GENERIC_P (as)
1154 && !DECL_THREAD_LOCAL_P (decl)
1155 && !(prefer_noswitch_p && targetm.have_switchable_bss_sections)
1156 && bss_initializer_p (decl))
1158 if (!TREE_PUBLIC (decl)
1159 && !((flag_sanitize & SANITIZE_ADDRESS)
1160 && asan_protect_global (decl)))
1161 return lcomm_section;
1162 if (bss_noswitch_section)
1163 return bss_noswitch_section;
1166 return targetm.asm_out.select_section (decl, reloc,
1167 get_variable_align (decl));
1170 /* Return the block into which object_block DECL should be placed. */
1172 static struct object_block *
1173 get_block_for_decl (tree decl)
1175 section *sect;
1177 if (TREE_CODE (decl) == VAR_DECL)
1179 /* The object must be defined in this translation unit. */
1180 if (DECL_EXTERNAL (decl))
1181 return NULL;
1183 /* There's no point using object blocks for something that is
1184 isolated by definition. */
1185 if (DECL_COMDAT_GROUP (decl))
1186 return NULL;
1189 /* We can only calculate block offsets if the decl has a known
1190 constant size. */
1191 if (DECL_SIZE_UNIT (decl) == NULL)
1192 return NULL;
1193 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (decl)))
1194 return NULL;
1196 /* Find out which section should contain DECL. We cannot put it into
1197 an object block if it requires a standalone definition. */
1198 if (TREE_CODE (decl) == VAR_DECL)
1199 align_variable (decl, 0);
1200 sect = get_variable_section (decl, true);
1201 if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
1202 return NULL;
1204 return get_block_for_section (sect);
1207 /* Make sure block symbol SYMBOL is in block BLOCK. */
1209 static void
1210 change_symbol_block (rtx symbol, struct object_block *block)
1212 if (block != SYMBOL_REF_BLOCK (symbol))
1214 gcc_assert (SYMBOL_REF_BLOCK_OFFSET (symbol) < 0);
1215 SYMBOL_REF_BLOCK (symbol) = block;
1219 /* Return true if it is possible to put DECL in an object_block. */
1221 static bool
1222 use_blocks_for_decl_p (tree decl)
1224 struct symtab_node *snode;
1226 /* Only data DECLs can be placed into object blocks. */
1227 if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != CONST_DECL)
1228 return false;
1230 /* Detect decls created by dw2_force_const_mem. Such decls are
1231 special because DECL_INITIAL doesn't specify the decl's true value.
1232 dw2_output_indirect_constants will instead call assemble_variable
1233 with dont_output_data set to 1 and then print the contents itself. */
1234 if (DECL_INITIAL (decl) == decl)
1235 return false;
1237 /* If this decl is an alias, then we don't want to emit a
1238 definition. */
1239 if (TREE_CODE (decl) == VAR_DECL
1240 && (snode = symtab_node::get (decl)) != NULL
1241 && snode->alias)
1242 return false;
1244 return targetm.use_blocks_for_decl_p (decl);
1247 /* Follow the IDENTIFIER_TRANSPARENT_ALIAS chain starting at *ALIAS
1248 until we find an identifier that is not itself a transparent alias.
1249 Modify the alias passed to it by reference (and all aliases on the
1250 way to the ultimate target), such that they do not have to be
1251 followed again, and return the ultimate target of the alias
1252 chain. */
1254 static inline tree
1255 ultimate_transparent_alias_target (tree *alias)
1257 tree target = *alias;
1259 if (IDENTIFIER_TRANSPARENT_ALIAS (target))
1261 gcc_assert (TREE_CHAIN (target));
1262 target = ultimate_transparent_alias_target (&TREE_CHAIN (target));
1263 gcc_assert (! IDENTIFIER_TRANSPARENT_ALIAS (target)
1264 && ! TREE_CHAIN (target));
1265 *alias = target;
1268 return target;
1271 /* Create the DECL_RTL for a VAR_DECL or FUNCTION_DECL. DECL should
1272 have static storage duration. In other words, it should not be an
1273 automatic variable, including PARM_DECLs.
1275 There is, however, one exception: this function handles variables
1276 explicitly placed in a particular register by the user.
1278 This is never called for PARM_DECL nodes. */
1280 void
1281 make_decl_rtl (tree decl)
1283 const char *name = 0;
1284 int reg_number;
1285 tree id;
1286 rtx x;
1288 /* Check that we are not being given an automatic variable. */
1289 gcc_assert (TREE_CODE (decl) != PARM_DECL
1290 && TREE_CODE (decl) != RESULT_DECL);
1292 /* A weak alias has TREE_PUBLIC set but not the other bits. */
1293 gcc_assert (TREE_CODE (decl) != VAR_DECL
1294 || TREE_STATIC (decl)
1295 || TREE_PUBLIC (decl)
1296 || DECL_EXTERNAL (decl)
1297 || DECL_REGISTER (decl));
1299 /* And that we were not given a type or a label. */
1300 gcc_assert (TREE_CODE (decl) != TYPE_DECL
1301 && TREE_CODE (decl) != LABEL_DECL);
1303 /* For a duplicate declaration, we can be called twice on the
1304 same DECL node. Don't discard the RTL already made. */
1305 if (DECL_RTL_SET_P (decl))
1307 /* If the old RTL had the wrong mode, fix the mode. */
1308 x = DECL_RTL (decl);
1309 if (GET_MODE (x) != DECL_MODE (decl))
1310 SET_DECL_RTL (decl, adjust_address_nv (x, DECL_MODE (decl), 0));
1312 if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1313 return;
1315 /* ??? Another way to do this would be to maintain a hashed
1316 table of such critters. Instead of adding stuff to a DECL
1317 to give certain attributes to it, we could use an external
1318 hash map from DECL to set of attributes. */
1320 /* Let the target reassign the RTL if it wants.
1321 This is necessary, for example, when one machine specific
1322 decl attribute overrides another. */
1323 targetm.encode_section_info (decl, DECL_RTL (decl), false);
1325 /* If the symbol has a SYMBOL_REF_BLOCK field, update it based
1326 on the new decl information. */
1327 if (MEM_P (x)
1328 && GET_CODE (XEXP (x, 0)) == SYMBOL_REF
1329 && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (x, 0)))
1330 change_symbol_block (XEXP (x, 0), get_block_for_decl (decl));
1332 return;
1335 /* If this variable belongs to the global constant pool, retrieve the
1336 pre-computed RTL or recompute it in LTO mode. */
1337 if (TREE_CODE (decl) == VAR_DECL && DECL_IN_CONSTANT_POOL (decl))
1339 SET_DECL_RTL (decl, output_constant_def (DECL_INITIAL (decl), 1));
1340 return;
1343 id = DECL_ASSEMBLER_NAME (decl);
1344 if (TREE_CODE (decl) == FUNCTION_DECL
1345 && cgraph_node::get (decl)
1346 && cgraph_node::get (decl)->instrumentation_clone)
1347 ultimate_transparent_alias_target (&id);
1348 name = IDENTIFIER_POINTER (id);
1350 if (name[0] != '*' && TREE_CODE (decl) != FUNCTION_DECL
1351 && DECL_REGISTER (decl))
1353 error ("register name not specified for %q+D", decl);
1355 else if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1357 const char *asmspec = name+1;
1358 machine_mode mode = DECL_MODE (decl);
1359 reg_number = decode_reg_name (asmspec);
1360 /* First detect errors in declaring global registers. */
1361 if (reg_number == -1)
1362 error ("register name not specified for %q+D", decl);
1363 else if (reg_number < 0)
1364 error ("invalid register name for %q+D", decl);
1365 else if (mode == BLKmode)
1366 error ("data type of %q+D isn%'t suitable for a register",
1367 decl);
1368 else if (!in_hard_reg_set_p (accessible_reg_set, mode, reg_number))
1369 error ("the register specified for %q+D cannot be accessed"
1370 " by the current target", decl);
1371 else if (!in_hard_reg_set_p (operand_reg_set, mode, reg_number))
1372 error ("the register specified for %q+D is not general enough"
1373 " to be used as a register variable", decl);
1374 else if (!HARD_REGNO_MODE_OK (reg_number, mode))
1375 error ("register specified for %q+D isn%'t suitable for data type",
1376 decl);
1377 /* Now handle properly declared static register variables. */
1378 else
1380 int nregs;
1382 if (DECL_INITIAL (decl) != 0 && TREE_STATIC (decl))
1384 DECL_INITIAL (decl) = 0;
1385 error ("global register variable has initial value");
1387 if (TREE_THIS_VOLATILE (decl))
1388 warning (OPT_Wvolatile_register_var,
1389 "optimization may eliminate reads and/or "
1390 "writes to register variables");
1392 /* If the user specified one of the eliminables registers here,
1393 e.g., FRAME_POINTER_REGNUM, we don't want to get this variable
1394 confused with that register and be eliminated. This usage is
1395 somewhat suspect... */
1397 SET_DECL_RTL (decl, gen_rtx_raw_REG (mode, reg_number));
1398 ORIGINAL_REGNO (DECL_RTL (decl)) = reg_number;
1399 REG_USERVAR_P (DECL_RTL (decl)) = 1;
1401 if (TREE_STATIC (decl))
1403 /* Make this register global, so not usable for anything
1404 else. */
1405 #ifdef ASM_DECLARE_REGISTER_GLOBAL
1406 name = IDENTIFIER_POINTER (DECL_NAME (decl));
1407 ASM_DECLARE_REGISTER_GLOBAL (asm_out_file, decl, reg_number, name);
1408 #endif
1409 nregs = hard_regno_nregs[reg_number][mode];
1410 while (nregs > 0)
1411 globalize_reg (decl, reg_number + --nregs);
1414 /* As a register variable, it has no section. */
1415 return;
1417 /* Avoid internal errors from invalid register
1418 specifications. */
1419 SET_DECL_ASSEMBLER_NAME (decl, NULL_TREE);
1420 DECL_HARD_REGISTER (decl) = 0;
1421 return;
1423 /* Now handle ordinary static variables and functions (in memory).
1424 Also handle vars declared register invalidly. */
1425 else if (name[0] == '*')
1427 #ifdef REGISTER_PREFIX
1428 if (strlen (REGISTER_PREFIX) != 0)
1430 reg_number = decode_reg_name (name);
1431 if (reg_number >= 0 || reg_number == -3)
1432 error ("register name given for non-register variable %q+D", decl);
1434 #endif
1437 /* Specifying a section attribute on a variable forces it into a
1438 non-.bss section, and thus it cannot be common. */
1439 /* FIXME: In general this code should not be necessary because
1440 visibility pass is doing the same work. But notice_global_symbol
1441 is called early and it needs to make DECL_RTL to get the name.
1442 we take care of recomputing the DECL_RTL after visibility is changed. */
1443 if (TREE_CODE (decl) == VAR_DECL
1444 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
1445 && DECL_SECTION_NAME (decl) != NULL
1446 && DECL_INITIAL (decl) == NULL_TREE
1447 && DECL_COMMON (decl))
1448 DECL_COMMON (decl) = 0;
1450 /* Variables can't be both common and weak. */
1451 if (TREE_CODE (decl) == VAR_DECL && DECL_WEAK (decl))
1452 DECL_COMMON (decl) = 0;
1454 if (use_object_blocks_p () && use_blocks_for_decl_p (decl))
1455 x = create_block_symbol (name, get_block_for_decl (decl), -1);
1456 else
1458 machine_mode address_mode = Pmode;
1459 if (TREE_TYPE (decl) != error_mark_node)
1461 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
1462 address_mode = targetm.addr_space.address_mode (as);
1464 x = gen_rtx_SYMBOL_REF (address_mode, name);
1466 SYMBOL_REF_WEAK (x) = DECL_WEAK (decl);
1467 SET_SYMBOL_REF_DECL (x, decl);
1469 x = gen_rtx_MEM (DECL_MODE (decl), x);
1470 if (TREE_CODE (decl) != FUNCTION_DECL)
1471 set_mem_attributes (x, decl, 1);
1472 SET_DECL_RTL (decl, x);
1474 /* Optionally set flags or add text to the name to record information
1475 such as that it is a function name.
1476 If the name is changed, the macro ASM_OUTPUT_LABELREF
1477 will have to know how to strip this information. */
1478 targetm.encode_section_info (decl, DECL_RTL (decl), true);
1481 /* Like make_decl_rtl, but inhibit creation of new alias sets when
1482 calling make_decl_rtl. Also, reset DECL_RTL before returning the
1483 rtl. */
1486 make_decl_rtl_for_debug (tree decl)
1488 unsigned int save_aliasing_flag;
1489 rtx rtl;
1491 if (DECL_RTL_SET_P (decl))
1492 return DECL_RTL (decl);
1494 /* Kludge alert! Somewhere down the call chain, make_decl_rtl will
1495 call new_alias_set. If running with -fcompare-debug, sometimes
1496 we do not want to create alias sets that will throw the alias
1497 numbers off in the comparison dumps. So... clearing
1498 flag_strict_aliasing will keep new_alias_set() from creating a
1499 new set. */
1500 save_aliasing_flag = flag_strict_aliasing;
1501 flag_strict_aliasing = 0;
1503 rtl = DECL_RTL (decl);
1504 /* Reset DECL_RTL back, as various parts of the compiler expects
1505 DECL_RTL set meaning it is actually going to be output. */
1506 SET_DECL_RTL (decl, NULL);
1508 flag_strict_aliasing = save_aliasing_flag;
1509 return rtl;
1512 /* Output a string of literal assembler code
1513 for an `asm' keyword used between functions. */
1515 void
1516 assemble_asm (tree string)
1518 const char *p;
1519 app_enable ();
1521 if (TREE_CODE (string) == ADDR_EXPR)
1522 string = TREE_OPERAND (string, 0);
1524 p = TREE_STRING_POINTER (string);
1525 fprintf (asm_out_file, "%s%s\n", p[0] == '\t' ? "" : "\t", p);
1528 /* Write the address of the entity given by SYMBOL to SEC. */
1529 void
1530 assemble_addr_to_section (rtx symbol, section *sec)
1532 switch_to_section (sec);
1533 assemble_align (POINTER_SIZE);
1534 assemble_integer (symbol, POINTER_SIZE_UNITS, POINTER_SIZE, 1);
1537 /* Return the numbered .ctors.N (if CONSTRUCTOR_P) or .dtors.N (if
1538 not) section for PRIORITY. */
1539 section *
1540 get_cdtor_priority_section (int priority, bool constructor_p)
1542 char buf[16];
1544 /* ??? This only works reliably with the GNU linker. */
1545 sprintf (buf, "%s.%.5u",
1546 constructor_p ? ".ctors" : ".dtors",
1547 /* Invert the numbering so the linker puts us in the proper
1548 order; constructors are run from right to left, and the
1549 linker sorts in increasing order. */
1550 MAX_INIT_PRIORITY - priority);
1551 return get_section (buf, SECTION_WRITE, NULL);
1554 void
1555 default_named_section_asm_out_destructor (rtx symbol, int priority)
1557 section *sec;
1559 if (priority != DEFAULT_INIT_PRIORITY)
1560 sec = get_cdtor_priority_section (priority,
1561 /*constructor_p=*/false);
1562 else
1563 sec = get_section (".dtors", SECTION_WRITE, NULL);
1565 assemble_addr_to_section (symbol, sec);
1568 #ifdef DTORS_SECTION_ASM_OP
1569 void
1570 default_dtor_section_asm_out_destructor (rtx symbol,
1571 int priority ATTRIBUTE_UNUSED)
1573 assemble_addr_to_section (symbol, dtors_section);
1575 #endif
1577 void
1578 default_named_section_asm_out_constructor (rtx symbol, int priority)
1580 section *sec;
1582 if (priority != DEFAULT_INIT_PRIORITY)
1583 sec = get_cdtor_priority_section (priority,
1584 /*constructor_p=*/true);
1585 else
1586 sec = get_section (".ctors", SECTION_WRITE, NULL);
1588 assemble_addr_to_section (symbol, sec);
1591 #ifdef CTORS_SECTION_ASM_OP
1592 void
1593 default_ctor_section_asm_out_constructor (rtx symbol,
1594 int priority ATTRIBUTE_UNUSED)
1596 assemble_addr_to_section (symbol, ctors_section);
1598 #endif
1600 /* CONSTANT_POOL_BEFORE_FUNCTION may be defined as an expression with
1601 a nonzero value if the constant pool should be output before the
1602 start of the function, or a zero value if the pool should output
1603 after the end of the function. The default is to put it before the
1604 start. */
1606 #ifndef CONSTANT_POOL_BEFORE_FUNCTION
1607 #define CONSTANT_POOL_BEFORE_FUNCTION 1
1608 #endif
1610 /* DECL is an object (either VAR_DECL or FUNCTION_DECL) which is going
1611 to be output to assembler.
1612 Set first_global_object_name and weak_global_object_name as appropriate. */
1614 void
1615 notice_global_symbol (tree decl)
1617 const char **type = &first_global_object_name;
1619 if (first_global_object_name
1620 || !TREE_PUBLIC (decl)
1621 || DECL_EXTERNAL (decl)
1622 || !DECL_NAME (decl)
1623 || (TREE_CODE (decl) != FUNCTION_DECL
1624 && (TREE_CODE (decl) != VAR_DECL
1625 || (DECL_COMMON (decl)
1626 && (DECL_INITIAL (decl) == 0
1627 || DECL_INITIAL (decl) == error_mark_node))))
1628 || !MEM_P (DECL_RTL (decl)))
1629 return;
1631 /* We win when global object is found, but it is useful to know about weak
1632 symbol as well so we can produce nicer unique names. */
1633 if (DECL_WEAK (decl) || DECL_ONE_ONLY (decl) || flag_shlib)
1634 type = &weak_global_object_name;
1636 if (!*type)
1638 const char *p;
1639 const char *name;
1640 rtx decl_rtl = DECL_RTL (decl);
1642 p = targetm.strip_name_encoding (XSTR (XEXP (decl_rtl, 0), 0));
1643 name = ggc_strdup (p);
1645 *type = name;
1649 /* If not using flag_reorder_blocks_and_partition, decide early whether the
1650 current function goes into the cold section, so that targets can use
1651 current_function_section during RTL expansion. DECL describes the
1652 function. */
1654 void
1655 decide_function_section (tree decl)
1657 first_function_block_is_cold = false;
1659 if (flag_reorder_blocks_and_partition)
1660 /* We will decide in assemble_start_function. */
1661 return;
1663 if (DECL_SECTION_NAME (decl))
1665 struct cgraph_node *node = cgraph_node::get (current_function_decl);
1666 /* Calls to function_section rely on first_function_block_is_cold
1667 being accurate. */
1668 first_function_block_is_cold = (node
1669 && node->frequency
1670 == NODE_FREQUENCY_UNLIKELY_EXECUTED);
1673 in_cold_section_p = first_function_block_is_cold;
1676 /* Output assembler code for the constant pool of a function and associated
1677 with defining the name of the function. DECL describes the function.
1678 NAME is the function's name. For the constant pool, we use the current
1679 constant pool data. */
1681 void
1682 assemble_start_function (tree decl, const char *fnname)
1684 int align;
1685 char tmp_label[100];
1686 bool hot_label_written = false;
1688 if (flag_reorder_blocks_and_partition)
1690 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTB", const_labelno);
1691 crtl->subsections.hot_section_label = ggc_strdup (tmp_label);
1692 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDB", const_labelno);
1693 crtl->subsections.cold_section_label = ggc_strdup (tmp_label);
1694 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTE", const_labelno);
1695 crtl->subsections.hot_section_end_label = ggc_strdup (tmp_label);
1696 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDE", const_labelno);
1697 crtl->subsections.cold_section_end_label = ggc_strdup (tmp_label);
1698 const_labelno++;
1700 else
1702 crtl->subsections.hot_section_label = NULL;
1703 crtl->subsections.cold_section_label = NULL;
1704 crtl->subsections.hot_section_end_label = NULL;
1705 crtl->subsections.cold_section_end_label = NULL;
1708 /* The following code does not need preprocessing in the assembler. */
1710 app_disable ();
1712 if (CONSTANT_POOL_BEFORE_FUNCTION)
1713 output_constant_pool (fnname, decl);
1715 /* Make sure the not and cold text (code) sections are properly
1716 aligned. This is necessary here in the case where the function
1717 has both hot and cold sections, because we don't want to re-set
1718 the alignment when the section switch happens mid-function. */
1720 if (flag_reorder_blocks_and_partition)
1722 first_function_block_is_cold = false;
1724 switch_to_section (unlikely_text_section ());
1725 assemble_align (DECL_ALIGN (decl));
1726 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_label);
1728 /* When the function starts with a cold section, we need to explicitly
1729 align the hot section and write out the hot section label.
1730 But if the current function is a thunk, we do not have a CFG. */
1731 if (!cfun->is_thunk
1732 && BB_PARTITION (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb) == BB_COLD_PARTITION)
1734 switch_to_section (text_section);
1735 assemble_align (DECL_ALIGN (decl));
1736 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
1737 hot_label_written = true;
1738 first_function_block_is_cold = true;
1740 in_cold_section_p = first_function_block_is_cold;
1744 /* Switch to the correct text section for the start of the function. */
1746 switch_to_section (function_section (decl));
1747 if (flag_reorder_blocks_and_partition
1748 && !hot_label_written)
1749 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
1751 /* Tell assembler to move to target machine's alignment for functions. */
1752 align = floor_log2 (DECL_ALIGN (decl) / BITS_PER_UNIT);
1753 if (align > 0)
1755 ASM_OUTPUT_ALIGN (asm_out_file, align);
1758 /* Handle a user-specified function alignment.
1759 Note that we still need to align to DECL_ALIGN, as above,
1760 because ASM_OUTPUT_MAX_SKIP_ALIGN might not do any alignment at all. */
1761 if (! DECL_USER_ALIGN (decl)
1762 && align_functions_log > align
1763 && optimize_function_for_speed_p (cfun))
1765 #ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
1766 ASM_OUTPUT_MAX_SKIP_ALIGN (asm_out_file,
1767 align_functions_log, align_functions - 1);
1768 #else
1769 ASM_OUTPUT_ALIGN (asm_out_file, align_functions_log);
1770 #endif
1773 #ifdef ASM_OUTPUT_FUNCTION_PREFIX
1774 ASM_OUTPUT_FUNCTION_PREFIX (asm_out_file, fnname);
1775 #endif
1777 if (!DECL_IGNORED_P (decl))
1778 (*debug_hooks->begin_function) (decl);
1780 /* Make function name accessible from other files, if appropriate. */
1782 if (TREE_PUBLIC (decl)
1783 || (cgraph_node::get (decl)->instrumentation_clone
1784 && cgraph_node::get (decl)->instrumented_version
1785 && TREE_PUBLIC (cgraph_node::get (decl)->instrumented_version->decl)))
1787 notice_global_symbol (decl);
1789 globalize_decl (decl);
1791 maybe_assemble_visibility (decl);
1794 if (DECL_PRESERVE_P (decl))
1795 targetm.asm_out.mark_decl_preserved (fnname);
1797 /* Do any machine/system dependent processing of the function name. */
1798 #ifdef ASM_DECLARE_FUNCTION_NAME
1799 ASM_DECLARE_FUNCTION_NAME (asm_out_file, fnname, current_function_decl);
1800 #else
1801 /* Standard thing is just output label for the function. */
1802 ASM_OUTPUT_FUNCTION_LABEL (asm_out_file, fnname, current_function_decl);
1803 #endif /* ASM_DECLARE_FUNCTION_NAME */
1805 if (lookup_attribute ("no_split_stack", DECL_ATTRIBUTES (decl)))
1806 saw_no_split_stack = true;
1809 /* Output assembler code associated with defining the size of the
1810 function. DECL describes the function. NAME is the function's name. */
1812 void
1813 assemble_end_function (tree decl, const char *fnname ATTRIBUTE_UNUSED)
1815 #ifdef ASM_DECLARE_FUNCTION_SIZE
1816 /* We could have switched section in the middle of the function. */
1817 if (flag_reorder_blocks_and_partition)
1818 switch_to_section (function_section (decl));
1819 ASM_DECLARE_FUNCTION_SIZE (asm_out_file, fnname, decl);
1820 #endif
1821 if (! CONSTANT_POOL_BEFORE_FUNCTION)
1823 output_constant_pool (fnname, decl);
1824 switch_to_section (function_section (decl)); /* need to switch back */
1826 /* Output labels for end of hot/cold text sections (to be used by
1827 debug info.) */
1828 if (flag_reorder_blocks_and_partition)
1830 section *save_text_section;
1832 save_text_section = in_section;
1833 switch_to_section (unlikely_text_section ());
1834 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_end_label);
1835 if (first_function_block_is_cold)
1836 switch_to_section (text_section);
1837 else
1838 switch_to_section (function_section (decl));
1839 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_end_label);
1840 switch_to_section (save_text_section);
1844 /* Assemble code to leave SIZE bytes of zeros. */
1846 void
1847 assemble_zeros (unsigned HOST_WIDE_INT size)
1849 /* Do no output if -fsyntax-only. */
1850 if (flag_syntax_only)
1851 return;
1853 #ifdef ASM_NO_SKIP_IN_TEXT
1854 /* The `space' pseudo in the text section outputs nop insns rather than 0s,
1855 so we must output 0s explicitly in the text section. */
1856 if (ASM_NO_SKIP_IN_TEXT && (in_section->common.flags & SECTION_CODE) != 0)
1858 unsigned HOST_WIDE_INT i;
1859 for (i = 0; i < size; i++)
1860 assemble_integer (const0_rtx, 1, BITS_PER_UNIT, 1);
1862 else
1863 #endif
1864 if (size > 0)
1865 ASM_OUTPUT_SKIP (asm_out_file, size);
1868 /* Assemble an alignment pseudo op for an ALIGN-bit boundary. */
1870 void
1871 assemble_align (int align)
1873 if (align > BITS_PER_UNIT)
1875 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
1879 /* Assemble a string constant with the specified C string as contents. */
1881 void
1882 assemble_string (const char *p, int size)
1884 int pos = 0;
1885 int maximum = 2000;
1887 /* If the string is very long, split it up. */
1889 while (pos < size)
1891 int thissize = size - pos;
1892 if (thissize > maximum)
1893 thissize = maximum;
1895 ASM_OUTPUT_ASCII (asm_out_file, p, thissize);
1897 pos += thissize;
1898 p += thissize;
1903 /* A noswitch_section_callback for lcomm_section. */
1905 static bool
1906 emit_local (tree decl ATTRIBUTE_UNUSED,
1907 const char *name ATTRIBUTE_UNUSED,
1908 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1909 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1911 #if defined ASM_OUTPUT_ALIGNED_DECL_LOCAL
1912 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, decl, name,
1913 size, DECL_ALIGN (decl));
1914 return true;
1915 #elif defined ASM_OUTPUT_ALIGNED_LOCAL
1916 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, DECL_ALIGN (decl));
1917 return true;
1918 #else
1919 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
1920 return false;
1921 #endif
1924 /* A noswitch_section_callback for bss_noswitch_section. */
1926 #if defined ASM_OUTPUT_ALIGNED_BSS
1927 static bool
1928 emit_bss (tree decl ATTRIBUTE_UNUSED,
1929 const char *name ATTRIBUTE_UNUSED,
1930 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1931 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1933 #if defined ASM_OUTPUT_ALIGNED_BSS
1934 ASM_OUTPUT_ALIGNED_BSS (asm_out_file, decl, name, size,
1935 get_variable_align (decl));
1936 return true;
1937 #endif
1939 #endif
1941 /* A noswitch_section_callback for comm_section. */
1943 static bool
1944 emit_common (tree decl ATTRIBUTE_UNUSED,
1945 const char *name ATTRIBUTE_UNUSED,
1946 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1947 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1949 #if defined ASM_OUTPUT_ALIGNED_DECL_COMMON
1950 ASM_OUTPUT_ALIGNED_DECL_COMMON (asm_out_file, decl, name,
1951 size, get_variable_align (decl));
1952 return true;
1953 #elif defined ASM_OUTPUT_ALIGNED_COMMON
1954 ASM_OUTPUT_ALIGNED_COMMON (asm_out_file, name, size,
1955 get_variable_align (decl));
1956 return true;
1957 #else
1958 ASM_OUTPUT_COMMON (asm_out_file, name, size, rounded);
1959 return false;
1960 #endif
1963 /* A noswitch_section_callback for tls_comm_section. */
1965 static bool
1966 emit_tls_common (tree decl ATTRIBUTE_UNUSED,
1967 const char *name ATTRIBUTE_UNUSED,
1968 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1969 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1971 #ifdef ASM_OUTPUT_TLS_COMMON
1972 ASM_OUTPUT_TLS_COMMON (asm_out_file, decl, name, size);
1973 return true;
1974 #else
1975 sorry ("thread-local COMMON data not implemented");
1976 return true;
1977 #endif
1980 /* Assemble DECL given that it belongs in SECTION_NOSWITCH section SECT.
1981 NAME is the name of DECL's SYMBOL_REF. */
1983 static void
1984 assemble_noswitch_variable (tree decl, const char *name, section *sect,
1985 unsigned int align)
1987 unsigned HOST_WIDE_INT size, rounded;
1989 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
1990 rounded = size;
1992 if ((flag_sanitize & SANITIZE_ADDRESS) && asan_protect_global (decl))
1993 size += asan_red_zone_size (size);
1995 /* Don't allocate zero bytes of common,
1996 since that means "undefined external" in the linker. */
1997 if (size == 0)
1998 rounded = 1;
2000 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
2001 so that each uninitialized object starts on such a boundary. */
2002 rounded += (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1;
2003 rounded = (rounded / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
2004 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
2006 if (!sect->noswitch.callback (decl, name, size, rounded)
2007 && (unsigned HOST_WIDE_INT) (align / BITS_PER_UNIT) > rounded)
2008 error ("requested alignment for %q+D is greater than "
2009 "implemented alignment of %wu", decl, rounded);
2012 /* A subroutine of assemble_variable. Output the label and contents of
2013 DECL, whose address is a SYMBOL_REF with name NAME. DONT_OUTPUT_DATA
2014 is as for assemble_variable. */
2016 static void
2017 assemble_variable_contents (tree decl, const char *name,
2018 bool dont_output_data)
2020 /* Do any machine/system dependent processing of the object. */
2021 #ifdef ASM_DECLARE_OBJECT_NAME
2022 last_assemble_variable_decl = decl;
2023 ASM_DECLARE_OBJECT_NAME (asm_out_file, name, decl);
2024 #else
2025 /* Standard thing is just output label for the object. */
2026 ASM_OUTPUT_LABEL (asm_out_file, name);
2027 #endif /* ASM_DECLARE_OBJECT_NAME */
2029 if (!dont_output_data)
2031 /* Caller is supposed to use varpool_get_constructor when it wants
2032 to output the body. */
2033 gcc_assert (!in_lto_p || DECL_INITIAL (decl) != error_mark_node);
2034 if (DECL_INITIAL (decl)
2035 && DECL_INITIAL (decl) != error_mark_node
2036 && !initializer_zerop (DECL_INITIAL (decl)))
2037 /* Output the actual data. */
2038 output_constant (DECL_INITIAL (decl),
2039 tree_to_uhwi (DECL_SIZE_UNIT (decl)),
2040 get_variable_align (decl),
2041 false);
2042 else
2043 /* Leave space for it. */
2044 assemble_zeros (tree_to_uhwi (DECL_SIZE_UNIT (decl)));
2048 /* Assemble everything that is needed for a variable or function declaration.
2049 Not used for automatic variables, and not used for function definitions.
2050 Should not be called for variables of incomplete structure type.
2052 TOP_LEVEL is nonzero if this variable has file scope.
2053 AT_END is nonzero if this is the special handling, at end of compilation,
2054 to define things that have had only tentative definitions.
2055 DONT_OUTPUT_DATA if nonzero means don't actually output the
2056 initial value (that will be done by the caller). */
2058 void
2059 assemble_variable (tree decl, int top_level ATTRIBUTE_UNUSED,
2060 int at_end ATTRIBUTE_UNUSED, int dont_output_data)
2062 const char *name;
2063 rtx decl_rtl, symbol;
2064 section *sect;
2065 unsigned int align;
2066 bool asan_protected = false;
2068 /* This function is supposed to handle VARIABLES. Ensure we have one. */
2069 gcc_assert (TREE_CODE (decl) == VAR_DECL);
2071 /* Emulated TLS had better not get this far. */
2072 gcc_checking_assert (targetm.have_tls || !DECL_THREAD_LOCAL_P (decl));
2074 last_assemble_variable_decl = 0;
2076 /* Normally no need to say anything here for external references,
2077 since assemble_external is called by the language-specific code
2078 when a declaration is first seen. */
2080 if (DECL_EXTERNAL (decl))
2081 return;
2083 /* Do nothing for global register variables. */
2084 if (DECL_RTL_SET_P (decl) && REG_P (DECL_RTL (decl)))
2086 TREE_ASM_WRITTEN (decl) = 1;
2087 return;
2090 /* If type was incomplete when the variable was declared,
2091 see if it is complete now. */
2093 if (DECL_SIZE (decl) == 0)
2094 layout_decl (decl, 0);
2096 /* Still incomplete => don't allocate it; treat the tentative defn
2097 (which is what it must have been) as an `extern' reference. */
2099 if (!dont_output_data && DECL_SIZE (decl) == 0)
2101 error ("storage size of %q+D isn%'t known", decl);
2102 TREE_ASM_WRITTEN (decl) = 1;
2103 return;
2106 /* The first declaration of a variable that comes through this function
2107 decides whether it is global (in C, has external linkage)
2108 or local (in C, has internal linkage). So do nothing more
2109 if this function has already run. */
2111 if (TREE_ASM_WRITTEN (decl))
2112 return;
2114 /* Make sure targetm.encode_section_info is invoked before we set
2115 ASM_WRITTEN. */
2116 decl_rtl = DECL_RTL (decl);
2118 TREE_ASM_WRITTEN (decl) = 1;
2120 /* Do no output if -fsyntax-only. */
2121 if (flag_syntax_only)
2122 return;
2124 if (! dont_output_data
2125 && ! valid_constant_size_p (DECL_SIZE_UNIT (decl)))
2127 error ("size of variable %q+D is too large", decl);
2128 return;
2131 gcc_assert (MEM_P (decl_rtl));
2132 gcc_assert (GET_CODE (XEXP (decl_rtl, 0)) == SYMBOL_REF);
2133 symbol = XEXP (decl_rtl, 0);
2135 /* If this symbol belongs to the tree constant pool, output the constant
2136 if it hasn't already been written. */
2137 if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
2139 tree decl = SYMBOL_REF_DECL (symbol);
2140 if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
2141 output_constant_def_contents (symbol);
2142 return;
2145 app_disable ();
2147 name = XSTR (symbol, 0);
2148 if (TREE_PUBLIC (decl) && DECL_NAME (decl))
2149 notice_global_symbol (decl);
2151 /* Compute the alignment of this data. */
2153 align_variable (decl, dont_output_data);
2155 if ((flag_sanitize & SANITIZE_ADDRESS)
2156 && asan_protect_global (decl))
2158 asan_protected = true;
2159 DECL_ALIGN (decl) = MAX (DECL_ALIGN (decl),
2160 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
2163 set_mem_align (decl_rtl, DECL_ALIGN (decl));
2165 align = get_variable_align (decl);
2167 if (TREE_PUBLIC (decl))
2168 maybe_assemble_visibility (decl);
2170 if (DECL_PRESERVE_P (decl))
2171 targetm.asm_out.mark_decl_preserved (name);
2173 /* First make the assembler name(s) global if appropriate. */
2174 sect = get_variable_section (decl, false);
2175 if (TREE_PUBLIC (decl)
2176 && (sect->common.flags & SECTION_COMMON) == 0)
2177 globalize_decl (decl);
2179 /* Output any data that we will need to use the address of. */
2180 if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node)
2181 output_addressed_constants (DECL_INITIAL (decl));
2183 /* dbxout.c needs to know this. */
2184 if (sect && (sect->common.flags & SECTION_CODE) != 0)
2185 DECL_IN_TEXT_SECTION (decl) = 1;
2187 /* If the decl is part of an object_block, make sure that the decl
2188 has been positioned within its block, but do not write out its
2189 definition yet. output_object_blocks will do that later. */
2190 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
2192 gcc_assert (!dont_output_data);
2193 place_block_symbol (symbol);
2195 else if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
2196 assemble_noswitch_variable (decl, name, sect, align);
2197 else
2199 /* The following bit of code ensures that vtable_map
2200 variables are not only in the comdat section, but that
2201 each variable has its own unique comdat name. If this
2202 code is removed, the variables end up in the same section
2203 with a single comdat name.
2205 FIXME: resolve_unique_section needs to deal better with
2206 decls with both DECL_SECTION_NAME and DECL_ONE_ONLY. Once
2207 that is fixed, this if-else statement can be replaced with
2208 a single call to "switch_to_section (sect)". */
2209 if (sect->named.name
2210 && (strcmp (sect->named.name, ".vtable_map_vars") == 0))
2212 #if defined (OBJECT_FORMAT_ELF)
2213 targetm.asm_out.named_section (sect->named.name,
2214 sect->named.common.flags
2215 | SECTION_LINKONCE,
2216 DECL_NAME (decl));
2217 in_section = sect;
2218 #else
2219 switch_to_section (sect);
2220 #endif
2222 else
2223 switch_to_section (sect);
2224 if (align > BITS_PER_UNIT)
2225 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
2226 assemble_variable_contents (decl, name, dont_output_data);
2227 if (asan_protected)
2229 unsigned HOST_WIDE_INT int size
2230 = tree_to_uhwi (DECL_SIZE_UNIT (decl));
2231 assemble_zeros (asan_red_zone_size (size));
2237 /* Given a function declaration (FN_DECL), this function assembles the
2238 function into the .preinit_array section. */
2240 void
2241 assemble_vtv_preinit_initializer (tree fn_decl)
2243 section *sect;
2244 unsigned flags = SECTION_WRITE;
2245 rtx symbol = XEXP (DECL_RTL (fn_decl), 0);
2247 flags |= SECTION_NOTYPE;
2248 sect = get_section (".preinit_array", flags, fn_decl);
2249 switch_to_section (sect);
2250 assemble_addr_to_section (symbol, sect);
2253 /* Return 1 if type TYPE contains any pointers. */
2255 static int
2256 contains_pointers_p (tree type)
2258 switch (TREE_CODE (type))
2260 case POINTER_TYPE:
2261 case REFERENCE_TYPE:
2262 /* I'm not sure whether OFFSET_TYPE needs this treatment,
2263 so I'll play safe and return 1. */
2264 case OFFSET_TYPE:
2265 return 1;
2267 case RECORD_TYPE:
2268 case UNION_TYPE:
2269 case QUAL_UNION_TYPE:
2271 tree fields;
2272 /* For a type that has fields, see if the fields have pointers. */
2273 for (fields = TYPE_FIELDS (type); fields; fields = DECL_CHAIN (fields))
2274 if (TREE_CODE (fields) == FIELD_DECL
2275 && contains_pointers_p (TREE_TYPE (fields)))
2276 return 1;
2277 return 0;
2280 case ARRAY_TYPE:
2281 /* An array type contains pointers if its element type does. */
2282 return contains_pointers_p (TREE_TYPE (type));
2284 default:
2285 return 0;
2289 /* We delay assemble_external processing until
2290 the compilation unit is finalized. This is the best we can do for
2291 right now (i.e. stage 3 of GCC 4.0) - the right thing is to delay
2292 it all the way to final. See PR 17982 for further discussion. */
2293 static GTY(()) tree pending_assemble_externals;
2295 #ifdef ASM_OUTPUT_EXTERNAL
2296 /* Some targets delay some output to final using TARGET_ASM_FILE_END.
2297 As a result, assemble_external can be called after the list of externals
2298 is processed and the pointer set destroyed. */
2299 static bool pending_assemble_externals_processed;
2301 /* Avoid O(external_decls**2) lookups in the pending_assemble_externals
2302 TREE_LIST in assemble_external. */
2303 static hash_set<tree> *pending_assemble_externals_set;
2305 /* True if DECL is a function decl for which no out-of-line copy exists.
2306 It is assumed that DECL's assembler name has been set. */
2308 static bool
2309 incorporeal_function_p (tree decl)
2311 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
2313 const char *name;
2315 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
2316 && (DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA
2317 || DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA_WITH_ALIGN))
2318 return true;
2320 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2321 /* Atomic or sync builtins which have survived this far will be
2322 resolved externally and therefore are not incorporeal. */
2323 if (strncmp (name, "__builtin_", 10) == 0)
2324 return true;
2326 return false;
2329 /* Actually do the tests to determine if this is necessary, and invoke
2330 ASM_OUTPUT_EXTERNAL. */
2331 static void
2332 assemble_external_real (tree decl)
2334 rtx rtl = DECL_RTL (decl);
2336 if (MEM_P (rtl) && GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF
2337 && !SYMBOL_REF_USED (XEXP (rtl, 0))
2338 && !incorporeal_function_p (decl))
2340 /* Some systems do require some output. */
2341 SYMBOL_REF_USED (XEXP (rtl, 0)) = 1;
2342 ASM_OUTPUT_EXTERNAL (asm_out_file, decl, XSTR (XEXP (rtl, 0), 0));
2345 #endif
2347 void
2348 process_pending_assemble_externals (void)
2350 #ifdef ASM_OUTPUT_EXTERNAL
2351 tree list;
2352 for (list = pending_assemble_externals; list; list = TREE_CHAIN (list))
2353 assemble_external_real (TREE_VALUE (list));
2355 pending_assemble_externals = 0;
2356 pending_assemble_externals_processed = true;
2357 delete pending_assemble_externals_set;
2358 #endif
2361 /* This TREE_LIST contains any weak symbol declarations waiting
2362 to be emitted. */
2363 static GTY(()) tree weak_decls;
2365 /* Output something to declare an external symbol to the assembler,
2366 and qualifiers such as weakness. (Most assemblers don't need
2367 extern declaration, so we normally output nothing.) Do nothing if
2368 DECL is not external. */
2370 void
2371 assemble_external (tree decl ATTRIBUTE_UNUSED)
2373 /* Make sure that the ASM_OUT_FILE is open.
2374 If it's not, we should not be calling this function. */
2375 gcc_assert (asm_out_file);
2377 /* In a perfect world, the following condition would be true.
2378 Sadly, the Java and Go front ends emit assembly *from the front end*,
2379 bypassing the call graph. See PR52739. Fix before GCC 4.8. */
2380 #if 0
2381 /* This function should only be called if we are expanding, or have
2382 expanded, to RTL.
2383 Ideally, only final.c would be calling this function, but it is
2384 not clear whether that would break things somehow. See PR 17982
2385 for further discussion. */
2386 gcc_assert (state == EXPANSION
2387 || state == FINISHED);
2388 #endif
2390 if (!DECL_P (decl) || !DECL_EXTERNAL (decl) || !TREE_PUBLIC (decl))
2391 return;
2393 /* We want to output annotation for weak and external symbols at
2394 very last to check if they are references or not. */
2396 if (TARGET_SUPPORTS_WEAK
2397 && DECL_WEAK (decl)
2398 /* TREE_STATIC is a weird and abused creature which is not
2399 generally the right test for whether an entity has been
2400 locally emitted, inlined or otherwise not-really-extern, but
2401 for declarations that can be weak, it happens to be
2402 match. */
2403 && !TREE_STATIC (decl)
2404 && lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
2405 && value_member (decl, weak_decls) == NULL_TREE)
2406 weak_decls = tree_cons (NULL, decl, weak_decls);
2408 #ifdef ASM_OUTPUT_EXTERNAL
2409 if (pending_assemble_externals_processed)
2411 assemble_external_real (decl);
2412 return;
2415 if (! pending_assemble_externals_set->add (decl))
2416 pending_assemble_externals = tree_cons (NULL, decl,
2417 pending_assemble_externals);
2418 #endif
2421 /* Similar, for calling a library function FUN. */
2423 void
2424 assemble_external_libcall (rtx fun)
2426 /* Declare library function name external when first used, if nec. */
2427 if (! SYMBOL_REF_USED (fun))
2429 SYMBOL_REF_USED (fun) = 1;
2430 targetm.asm_out.external_libcall (fun);
2434 /* Assemble a label named NAME. */
2436 void
2437 assemble_label (FILE *file, const char *name)
2439 ASM_OUTPUT_LABEL (file, name);
2442 /* Set the symbol_referenced flag for ID. */
2443 void
2444 mark_referenced (tree id)
2446 TREE_SYMBOL_REFERENCED (id) = 1;
2449 /* Set the symbol_referenced flag for DECL and notify callgraph. */
2450 void
2451 mark_decl_referenced (tree decl)
2453 if (TREE_CODE (decl) == FUNCTION_DECL)
2455 /* Extern inline functions don't become needed when referenced.
2456 If we know a method will be emitted in other TU and no new
2457 functions can be marked reachable, just use the external
2458 definition. */
2459 struct cgraph_node *node = cgraph_node::get_create (decl);
2460 if (!DECL_EXTERNAL (decl)
2461 && !node->definition)
2462 node->mark_force_output ();
2464 else if (TREE_CODE (decl) == VAR_DECL)
2466 varpool_node *node = varpool_node::get_create (decl);
2467 /* C++ frontend use mark_decl_references to force COMDAT variables
2468 to be output that might appear dead otherwise. */
2469 node->force_output = true;
2471 /* else do nothing - we can get various sorts of CST nodes here,
2472 which do not need to be marked. */
2476 /* Output to FILE (an assembly file) a reference to NAME. If NAME
2477 starts with a *, the rest of NAME is output verbatim. Otherwise
2478 NAME is transformed in a target-specific way (usually by the
2479 addition of an underscore). */
2481 void
2482 assemble_name_raw (FILE *file, const char *name)
2484 if (name[0] == '*')
2485 fputs (&name[1], file);
2486 else
2487 ASM_OUTPUT_LABELREF (file, name);
2490 /* Like assemble_name_raw, but should be used when NAME might refer to
2491 an entity that is also represented as a tree (like a function or
2492 variable). If NAME does refer to such an entity, that entity will
2493 be marked as referenced. */
2495 void
2496 assemble_name (FILE *file, const char *name)
2498 const char *real_name;
2499 tree id;
2501 real_name = targetm.strip_name_encoding (name);
2503 id = maybe_get_identifier (real_name);
2504 if (id)
2506 tree id_orig = id;
2508 mark_referenced (id);
2509 ultimate_transparent_alias_target (&id);
2510 if (id != id_orig)
2511 name = IDENTIFIER_POINTER (id);
2512 gcc_assert (! TREE_CHAIN (id));
2515 assemble_name_raw (file, name);
2518 /* Allocate SIZE bytes writable static space with a gensym name
2519 and return an RTX to refer to its address. */
2522 assemble_static_space (unsigned HOST_WIDE_INT size)
2524 char name[12];
2525 const char *namestring;
2526 rtx x;
2528 ASM_GENERATE_INTERNAL_LABEL (name, "LF", const_labelno);
2529 ++const_labelno;
2530 namestring = ggc_strdup (name);
2532 x = gen_rtx_SYMBOL_REF (Pmode, namestring);
2533 SYMBOL_REF_FLAGS (x) = SYMBOL_FLAG_LOCAL;
2535 #ifdef ASM_OUTPUT_ALIGNED_DECL_LOCAL
2536 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, NULL_TREE, name, size,
2537 BIGGEST_ALIGNMENT);
2538 #else
2539 #ifdef ASM_OUTPUT_ALIGNED_LOCAL
2540 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, BIGGEST_ALIGNMENT);
2541 #else
2543 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
2544 so that each uninitialized object starts on such a boundary. */
2545 /* Variable `rounded' might or might not be used in ASM_OUTPUT_LOCAL. */
2546 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED
2547 = ((size + (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1)
2548 / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
2549 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
2550 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
2552 #endif
2553 #endif
2554 return x;
2557 /* Assemble the static constant template for function entry trampolines.
2558 This is done at most once per compilation.
2559 Returns an RTX for the address of the template. */
2561 static GTY(()) rtx initial_trampoline;
2564 assemble_trampoline_template (void)
2566 char label[256];
2567 const char *name;
2568 int align;
2569 rtx symbol;
2571 gcc_assert (targetm.asm_out.trampoline_template != NULL);
2573 if (initial_trampoline)
2574 return initial_trampoline;
2576 /* By default, put trampoline templates in read-only data section. */
2578 #ifdef TRAMPOLINE_SECTION
2579 switch_to_section (TRAMPOLINE_SECTION);
2580 #else
2581 switch_to_section (readonly_data_section);
2582 #endif
2584 /* Write the assembler code to define one. */
2585 align = floor_log2 (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
2586 if (align > 0)
2587 ASM_OUTPUT_ALIGN (asm_out_file, align);
2589 targetm.asm_out.internal_label (asm_out_file, "LTRAMP", 0);
2590 targetm.asm_out.trampoline_template (asm_out_file);
2592 /* Record the rtl to refer to it. */
2593 ASM_GENERATE_INTERNAL_LABEL (label, "LTRAMP", 0);
2594 name = ggc_strdup (label);
2595 symbol = gen_rtx_SYMBOL_REF (Pmode, name);
2596 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_LOCAL;
2598 initial_trampoline = gen_const_mem (BLKmode, symbol);
2599 set_mem_align (initial_trampoline, TRAMPOLINE_ALIGNMENT);
2600 set_mem_size (initial_trampoline, TRAMPOLINE_SIZE);
2602 return initial_trampoline;
2605 /* A and B are either alignments or offsets. Return the minimum alignment
2606 that may be assumed after adding the two together. */
2608 static inline unsigned
2609 min_align (unsigned int a, unsigned int b)
2611 return (a | b) & -(a | b);
2614 /* Return the assembler directive for creating a given kind of integer
2615 object. SIZE is the number of bytes in the object and ALIGNED_P
2616 indicates whether it is known to be aligned. Return NULL if the
2617 assembly dialect has no such directive.
2619 The returned string should be printed at the start of a new line and
2620 be followed immediately by the object's initial value. */
2622 const char *
2623 integer_asm_op (int size, int aligned_p)
2625 struct asm_int_op *ops;
2627 if (aligned_p)
2628 ops = &targetm.asm_out.aligned_op;
2629 else
2630 ops = &targetm.asm_out.unaligned_op;
2632 switch (size)
2634 case 1:
2635 return targetm.asm_out.byte_op;
2636 case 2:
2637 return ops->hi;
2638 case 4:
2639 return ops->si;
2640 case 8:
2641 return ops->di;
2642 case 16:
2643 return ops->ti;
2644 default:
2645 return NULL;
2649 /* Use directive OP to assemble an integer object X. Print OP at the
2650 start of the line, followed immediately by the value of X. */
2652 void
2653 assemble_integer_with_op (const char *op, rtx x)
2655 fputs (op, asm_out_file);
2656 output_addr_const (asm_out_file, x);
2657 fputc ('\n', asm_out_file);
2660 /* The default implementation of the asm_out.integer target hook. */
2662 bool
2663 default_assemble_integer (rtx x ATTRIBUTE_UNUSED,
2664 unsigned int size ATTRIBUTE_UNUSED,
2665 int aligned_p ATTRIBUTE_UNUSED)
2667 const char *op = integer_asm_op (size, aligned_p);
2668 /* Avoid GAS bugs for large values. Specifically negative values whose
2669 absolute value fits in a bfd_vma, but not in a bfd_signed_vma. */
2670 if (size > UNITS_PER_WORD && size > POINTER_SIZE_UNITS)
2671 return false;
2672 return op && (assemble_integer_with_op (op, x), true);
2675 /* Assemble the integer constant X into an object of SIZE bytes. ALIGN is
2676 the alignment of the integer in bits. Return 1 if we were able to output
2677 the constant, otherwise 0. We must be able to output the constant,
2678 if FORCE is nonzero. */
2680 bool
2681 assemble_integer (rtx x, unsigned int size, unsigned int align, int force)
2683 int aligned_p;
2685 aligned_p = (align >= MIN (size * BITS_PER_UNIT, BIGGEST_ALIGNMENT));
2687 /* See if the target hook can handle this kind of object. */
2688 if (targetm.asm_out.integer (x, size, aligned_p))
2689 return true;
2691 /* If the object is a multi-byte one, try splitting it up. Split
2692 it into words it if is multi-word, otherwise split it into bytes. */
2693 if (size > 1)
2695 machine_mode omode, imode;
2696 unsigned int subalign;
2697 unsigned int subsize, i;
2698 enum mode_class mclass;
2700 subsize = size > UNITS_PER_WORD? UNITS_PER_WORD : 1;
2701 subalign = MIN (align, subsize * BITS_PER_UNIT);
2702 if (GET_CODE (x) == CONST_FIXED)
2703 mclass = GET_MODE_CLASS (GET_MODE (x));
2704 else
2705 mclass = MODE_INT;
2707 omode = mode_for_size (subsize * BITS_PER_UNIT, mclass, 0);
2708 imode = mode_for_size (size * BITS_PER_UNIT, mclass, 0);
2710 for (i = 0; i < size; i += subsize)
2712 rtx partial = simplify_subreg (omode, x, imode, i);
2713 if (!partial || !assemble_integer (partial, subsize, subalign, 0))
2714 break;
2716 if (i == size)
2717 return true;
2719 /* If we've printed some of it, but not all of it, there's no going
2720 back now. */
2721 gcc_assert (!i);
2724 gcc_assert (!force);
2726 return false;
2729 void
2730 assemble_real (REAL_VALUE_TYPE d, machine_mode mode, unsigned int align)
2732 long data[4] = {0, 0, 0, 0};
2733 int i;
2734 int bitsize, nelts, nunits, units_per;
2736 /* This is hairy. We have a quantity of known size. real_to_target
2737 will put it into an array of *host* longs, 32 bits per element
2738 (even if long is more than 32 bits). We need to determine the
2739 number of array elements that are occupied (nelts) and the number
2740 of *target* min-addressable units that will be occupied in the
2741 object file (nunits). We cannot assume that 32 divides the
2742 mode's bitsize (size * BITS_PER_UNIT) evenly.
2744 size * BITS_PER_UNIT is used here to make sure that padding bits
2745 (which might appear at either end of the value; real_to_target
2746 will include the padding bits in its output array) are included. */
2748 nunits = GET_MODE_SIZE (mode);
2749 bitsize = nunits * BITS_PER_UNIT;
2750 nelts = CEIL (bitsize, 32);
2751 units_per = 32 / BITS_PER_UNIT;
2753 real_to_target (data, &d, mode);
2755 /* Put out the first word with the specified alignment. */
2756 assemble_integer (GEN_INT (data[0]), MIN (nunits, units_per), align, 1);
2757 nunits -= units_per;
2759 /* Subsequent words need only 32-bit alignment. */
2760 align = min_align (align, 32);
2762 for (i = 1; i < nelts; i++)
2764 assemble_integer (GEN_INT (data[i]), MIN (nunits, units_per), align, 1);
2765 nunits -= units_per;
2769 /* Given an expression EXP with a constant value,
2770 reduce it to the sum of an assembler symbol and an integer.
2771 Store them both in the structure *VALUE.
2772 EXP must be reducible. */
2774 struct addr_const {
2775 rtx base;
2776 HOST_WIDE_INT offset;
2779 static void
2780 decode_addr_const (tree exp, struct addr_const *value)
2782 tree target = TREE_OPERAND (exp, 0);
2783 int offset = 0;
2784 rtx x;
2786 while (1)
2788 if (TREE_CODE (target) == COMPONENT_REF
2789 && tree_fits_shwi_p (byte_position (TREE_OPERAND (target, 1))))
2791 offset += int_byte_position (TREE_OPERAND (target, 1));
2792 target = TREE_OPERAND (target, 0);
2794 else if (TREE_CODE (target) == ARRAY_REF
2795 || TREE_CODE (target) == ARRAY_RANGE_REF)
2797 offset += (tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (target)))
2798 * tree_to_shwi (TREE_OPERAND (target, 1)));
2799 target = TREE_OPERAND (target, 0);
2801 else if (TREE_CODE (target) == MEM_REF
2802 && TREE_CODE (TREE_OPERAND (target, 0)) == ADDR_EXPR)
2804 offset += mem_ref_offset (target).to_short_addr ();
2805 target = TREE_OPERAND (TREE_OPERAND (target, 0), 0);
2807 else if (TREE_CODE (target) == INDIRECT_REF
2808 && TREE_CODE (TREE_OPERAND (target, 0)) == NOP_EXPR
2809 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (target, 0), 0))
2810 == ADDR_EXPR)
2811 target = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (target, 0), 0), 0);
2812 else
2813 break;
2816 switch (TREE_CODE (target))
2818 case VAR_DECL:
2819 case FUNCTION_DECL:
2820 x = DECL_RTL (target);
2821 break;
2823 case LABEL_DECL:
2824 x = gen_rtx_MEM (FUNCTION_MODE,
2825 gen_rtx_LABEL_REF (Pmode, force_label_rtx (target)));
2826 break;
2828 case REAL_CST:
2829 case FIXED_CST:
2830 case STRING_CST:
2831 case COMPLEX_CST:
2832 case CONSTRUCTOR:
2833 case INTEGER_CST:
2834 x = output_constant_def (target, 1);
2835 break;
2837 default:
2838 gcc_unreachable ();
2841 gcc_assert (MEM_P (x));
2842 x = XEXP (x, 0);
2844 value->base = x;
2845 value->offset = offset;
2848 static GTY(()) hash_table<tree_descriptor_hasher> *const_desc_htab;
2850 static void maybe_output_constant_def_contents (struct constant_descriptor_tree *, int);
2852 /* Constant pool accessor function. */
2854 hash_table<tree_descriptor_hasher> *
2855 constant_pool_htab (void)
2857 return const_desc_htab;
2860 /* Compute a hash code for a constant expression. */
2862 hashval_t
2863 tree_descriptor_hasher::hash (constant_descriptor_tree *ptr)
2865 return ptr->hash;
2868 static hashval_t
2869 const_hash_1 (const tree exp)
2871 const char *p;
2872 hashval_t hi;
2873 int len, i;
2874 enum tree_code code = TREE_CODE (exp);
2876 /* Either set P and LEN to the address and len of something to hash and
2877 exit the switch or return a value. */
2879 switch (code)
2881 case INTEGER_CST:
2882 p = (char *) &TREE_INT_CST_ELT (exp, 0);
2883 len = TREE_INT_CST_NUNITS (exp) * sizeof (HOST_WIDE_INT);
2884 break;
2886 case REAL_CST:
2887 return real_hash (TREE_REAL_CST_PTR (exp));
2889 case FIXED_CST:
2890 return fixed_hash (TREE_FIXED_CST_PTR (exp));
2892 case STRING_CST:
2893 p = TREE_STRING_POINTER (exp);
2894 len = TREE_STRING_LENGTH (exp);
2895 break;
2897 case COMPLEX_CST:
2898 return (const_hash_1 (TREE_REALPART (exp)) * 5
2899 + const_hash_1 (TREE_IMAGPART (exp)));
2901 case VECTOR_CST:
2903 unsigned i;
2905 hi = 7 + VECTOR_CST_NELTS (exp);
2907 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
2908 hi = hi * 563 + const_hash_1 (VECTOR_CST_ELT (exp, i));
2910 return hi;
2913 case CONSTRUCTOR:
2915 unsigned HOST_WIDE_INT idx;
2916 tree value;
2918 hi = 5 + int_size_in_bytes (TREE_TYPE (exp));
2920 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
2921 if (value)
2922 hi = hi * 603 + const_hash_1 (value);
2924 return hi;
2927 case ADDR_EXPR:
2928 case FDESC_EXPR:
2930 struct addr_const value;
2932 decode_addr_const (exp, &value);
2933 switch (GET_CODE (value.base))
2935 case SYMBOL_REF:
2936 /* Don't hash the address of the SYMBOL_REF;
2937 only use the offset and the symbol name. */
2938 hi = value.offset;
2939 p = XSTR (value.base, 0);
2940 for (i = 0; p[i] != 0; i++)
2941 hi = ((hi * 613) + (unsigned) (p[i]));
2942 break;
2944 case LABEL_REF:
2945 hi = (value.offset
2946 + CODE_LABEL_NUMBER (LABEL_REF_LABEL (value.base)) * 13);
2947 break;
2949 default:
2950 gcc_unreachable ();
2953 return hi;
2955 case PLUS_EXPR:
2956 case POINTER_PLUS_EXPR:
2957 case MINUS_EXPR:
2958 return (const_hash_1 (TREE_OPERAND (exp, 0)) * 9
2959 + const_hash_1 (TREE_OPERAND (exp, 1)));
2961 CASE_CONVERT:
2962 return const_hash_1 (TREE_OPERAND (exp, 0)) * 7 + 2;
2964 default:
2965 /* A language specific constant. Just hash the code. */
2966 return code;
2969 /* Compute hashing function. */
2970 hi = len;
2971 for (i = 0; i < len; i++)
2972 hi = ((hi * 613) + (unsigned) (p[i]));
2974 return hi;
2977 /* Wrapper of compare_constant, for the htab interface. */
2978 bool
2979 tree_descriptor_hasher::equal (constant_descriptor_tree *c1,
2980 constant_descriptor_tree *c2)
2982 if (c1->hash != c2->hash)
2983 return 0;
2984 return compare_constant (c1->value, c2->value);
2987 /* Compare t1 and t2, and return 1 only if they are known to result in
2988 the same bit pattern on output. */
2990 static int
2991 compare_constant (const tree t1, const tree t2)
2993 enum tree_code typecode;
2995 if (t1 == NULL_TREE)
2996 return t2 == NULL_TREE;
2997 if (t2 == NULL_TREE)
2998 return 0;
3000 if (TREE_CODE (t1) != TREE_CODE (t2))
3001 return 0;
3003 switch (TREE_CODE (t1))
3005 case INTEGER_CST:
3006 /* Integer constants are the same only if the same width of type. */
3007 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3008 return 0;
3009 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
3010 return 0;
3011 return tree_int_cst_equal (t1, t2);
3013 case REAL_CST:
3014 /* Real constants are the same only if the same width of type. */
3015 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3016 return 0;
3018 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
3020 case FIXED_CST:
3021 /* Fixed constants are the same only if the same width of type. */
3022 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3023 return 0;
3025 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
3027 case STRING_CST:
3028 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
3029 return 0;
3031 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
3032 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
3033 TREE_STRING_LENGTH (t1)));
3035 case COMPLEX_CST:
3036 return (compare_constant (TREE_REALPART (t1), TREE_REALPART (t2))
3037 && compare_constant (TREE_IMAGPART (t1), TREE_IMAGPART (t2)));
3039 case VECTOR_CST:
3041 unsigned i;
3043 if (VECTOR_CST_NELTS (t1) != VECTOR_CST_NELTS (t2))
3044 return 0;
3046 for (i = 0; i < VECTOR_CST_NELTS (t1); ++i)
3047 if (!compare_constant (VECTOR_CST_ELT (t1, i),
3048 VECTOR_CST_ELT (t2, i)))
3049 return 0;
3051 return 1;
3054 case CONSTRUCTOR:
3056 vec<constructor_elt, va_gc> *v1, *v2;
3057 unsigned HOST_WIDE_INT idx;
3059 typecode = TREE_CODE (TREE_TYPE (t1));
3060 if (typecode != TREE_CODE (TREE_TYPE (t2)))
3061 return 0;
3063 if (typecode == ARRAY_TYPE)
3065 HOST_WIDE_INT size_1 = int_size_in_bytes (TREE_TYPE (t1));
3066 /* For arrays, check that the sizes all match. */
3067 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))
3068 || size_1 == -1
3069 || size_1 != int_size_in_bytes (TREE_TYPE (t2)))
3070 return 0;
3072 else
3074 /* For record and union constructors, require exact type
3075 equality. */
3076 if (TREE_TYPE (t1) != TREE_TYPE (t2))
3077 return 0;
3080 v1 = CONSTRUCTOR_ELTS (t1);
3081 v2 = CONSTRUCTOR_ELTS (t2);
3082 if (vec_safe_length (v1) != vec_safe_length (v2))
3083 return 0;
3085 for (idx = 0; idx < vec_safe_length (v1); ++idx)
3087 constructor_elt *c1 = &(*v1)[idx];
3088 constructor_elt *c2 = &(*v2)[idx];
3090 /* Check that each value is the same... */
3091 if (!compare_constant (c1->value, c2->value))
3092 return 0;
3093 /* ... and that they apply to the same fields! */
3094 if (typecode == ARRAY_TYPE)
3096 if (!compare_constant (c1->index, c2->index))
3097 return 0;
3099 else
3101 if (c1->index != c2->index)
3102 return 0;
3106 return 1;
3109 case ADDR_EXPR:
3110 case FDESC_EXPR:
3112 struct addr_const value1, value2;
3113 enum rtx_code code;
3114 int ret;
3116 decode_addr_const (t1, &value1);
3117 decode_addr_const (t2, &value2);
3119 if (value1.offset != value2.offset)
3120 return 0;
3122 code = GET_CODE (value1.base);
3123 if (code != GET_CODE (value2.base))
3124 return 0;
3126 switch (code)
3128 case SYMBOL_REF:
3129 ret = (strcmp (XSTR (value1.base, 0), XSTR (value2.base, 0)) == 0);
3130 break;
3132 case LABEL_REF:
3133 ret = (CODE_LABEL_NUMBER (LABEL_REF_LABEL (value1.base))
3134 == CODE_LABEL_NUMBER (LABEL_REF_LABEL (value2.base)));
3135 break;
3137 default:
3138 gcc_unreachable ();
3140 return ret;
3143 case PLUS_EXPR:
3144 case POINTER_PLUS_EXPR:
3145 case MINUS_EXPR:
3146 case RANGE_EXPR:
3147 return (compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
3148 && compare_constant (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
3150 CASE_CONVERT:
3151 case VIEW_CONVERT_EXPR:
3152 return compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3154 default:
3155 return 0;
3158 gcc_unreachable ();
3161 /* Return the section into which constant EXP should be placed. */
3163 static section *
3164 get_constant_section (tree exp, unsigned int align)
3166 return targetm.asm_out.select_section (exp,
3167 compute_reloc_for_constant (exp),
3168 align);
3171 /* Return the size of constant EXP in bytes. */
3173 static HOST_WIDE_INT
3174 get_constant_size (tree exp)
3176 HOST_WIDE_INT size;
3178 size = int_size_in_bytes (TREE_TYPE (exp));
3179 if (TREE_CODE (exp) == STRING_CST)
3180 size = MAX (TREE_STRING_LENGTH (exp), size);
3181 return size;
3184 /* Subroutine of output_constant_def:
3185 No constant equal to EXP is known to have been output.
3186 Make a constant descriptor to enter EXP in the hash table.
3187 Assign the label number and construct RTL to refer to the
3188 constant's location in memory.
3189 Caller is responsible for updating the hash table. */
3191 static struct constant_descriptor_tree *
3192 build_constant_desc (tree exp)
3194 struct constant_descriptor_tree *desc;
3195 rtx symbol, rtl;
3196 char label[256];
3197 int labelno;
3198 tree decl;
3200 desc = ggc_alloc<constant_descriptor_tree> ();
3201 desc->value = exp;
3203 /* Create a string containing the label name, in LABEL. */
3204 labelno = const_labelno++;
3205 ASM_GENERATE_INTERNAL_LABEL (label, "LC", labelno);
3207 /* Construct the VAR_DECL associated with the constant. */
3208 decl = build_decl (UNKNOWN_LOCATION, VAR_DECL, get_identifier (label),
3209 TREE_TYPE (exp));
3210 DECL_ARTIFICIAL (decl) = 1;
3211 DECL_IGNORED_P (decl) = 1;
3212 TREE_READONLY (decl) = 1;
3213 TREE_STATIC (decl) = 1;
3214 TREE_ADDRESSABLE (decl) = 1;
3215 /* We don't set the RTL yet as this would cause varpool to assume that the
3216 variable is referenced. Moreover, it would just be dropped in LTO mode.
3217 Instead we set the flag that will be recognized in make_decl_rtl. */
3218 DECL_IN_CONSTANT_POOL (decl) = 1;
3219 DECL_INITIAL (decl) = desc->value;
3220 /* ??? CONSTANT_ALIGNMENT hasn't been updated for vector types on most
3221 architectures so use DATA_ALIGNMENT as well, except for strings. */
3222 if (TREE_CODE (exp) == STRING_CST)
3224 #ifdef CONSTANT_ALIGNMENT
3225 DECL_ALIGN (decl) = CONSTANT_ALIGNMENT (exp, DECL_ALIGN (decl));
3226 #endif
3228 else
3229 align_variable (decl, 0);
3231 /* Now construct the SYMBOL_REF and the MEM. */
3232 if (use_object_blocks_p ())
3234 section *sect = get_constant_section (exp, DECL_ALIGN (decl));
3235 symbol = create_block_symbol (ggc_strdup (label),
3236 get_block_for_section (sect), -1);
3238 else
3239 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3240 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3241 SET_SYMBOL_REF_DECL (symbol, decl);
3242 TREE_CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3244 rtl = gen_const_mem (TYPE_MODE (TREE_TYPE (exp)), symbol);
3245 set_mem_attributes (rtl, exp, 1);
3246 set_mem_alias_set (rtl, 0);
3248 /* We cannot share RTX'es in pool entries.
3249 Mark this piece of RTL as required for unsharing. */
3250 RTX_FLAG (rtl, used) = 1;
3252 /* Set flags or add text to the name to record information, such as
3253 that it is a local symbol. If the name is changed, the macro
3254 ASM_OUTPUT_LABELREF will have to know how to strip this
3255 information. This call might invalidate our local variable
3256 SYMBOL; we can't use it afterward. */
3257 targetm.encode_section_info (exp, rtl, true);
3259 desc->rtl = rtl;
3261 return desc;
3264 /* Return an rtx representing a reference to constant data in memory
3265 for the constant expression EXP.
3267 If assembler code for such a constant has already been output,
3268 return an rtx to refer to it.
3269 Otherwise, output such a constant in memory
3270 and generate an rtx for it.
3272 If DEFER is nonzero, this constant can be deferred and output only
3273 if referenced in the function after all optimizations.
3275 `const_desc_table' records which constants already have label strings. */
3278 output_constant_def (tree exp, int defer)
3280 struct constant_descriptor_tree *desc;
3281 struct constant_descriptor_tree key;
3283 /* Look up EXP in the table of constant descriptors. If we didn't find
3284 it, create a new one. */
3285 key.value = exp;
3286 key.hash = const_hash_1 (exp);
3287 constant_descriptor_tree **loc
3288 = const_desc_htab->find_slot_with_hash (&key, key.hash, INSERT);
3290 desc = *loc;
3291 if (desc == 0)
3293 desc = build_constant_desc (exp);
3294 desc->hash = key.hash;
3295 *loc = desc;
3298 maybe_output_constant_def_contents (desc, defer);
3299 return desc->rtl;
3302 /* Subroutine of output_constant_def: Decide whether or not we need to
3303 output the constant DESC now, and if so, do it. */
3304 static void
3305 maybe_output_constant_def_contents (struct constant_descriptor_tree *desc,
3306 int defer)
3308 rtx symbol = XEXP (desc->rtl, 0);
3309 tree exp = desc->value;
3311 if (flag_syntax_only)
3312 return;
3314 if (TREE_ASM_WRITTEN (exp))
3315 /* Already output; don't do it again. */
3316 return;
3318 /* We can always defer constants as long as the context allows
3319 doing so. */
3320 if (defer)
3322 /* Increment n_deferred_constants if it exists. It needs to be at
3323 least as large as the number of constants actually referred to
3324 by the function. If it's too small we'll stop looking too early
3325 and fail to emit constants; if it's too large we'll only look
3326 through the entire function when we could have stopped earlier. */
3327 if (cfun)
3328 n_deferred_constants++;
3329 return;
3332 output_constant_def_contents (symbol);
3335 /* Subroutine of output_constant_def_contents. Output the definition
3336 of constant EXP, which is pointed to by label LABEL. ALIGN is the
3337 constant's alignment in bits. */
3339 static void
3340 assemble_constant_contents (tree exp, const char *label, unsigned int align)
3342 HOST_WIDE_INT size;
3344 size = get_constant_size (exp);
3346 /* Do any machine/system dependent processing of the constant. */
3347 targetm.asm_out.declare_constant_name (asm_out_file, label, exp, size);
3349 /* Output the value of EXP. */
3350 output_constant (exp, size, align, false);
3353 /* We must output the constant data referred to by SYMBOL; do so. */
3355 static void
3356 output_constant_def_contents (rtx symbol)
3358 tree decl = SYMBOL_REF_DECL (symbol);
3359 tree exp = DECL_INITIAL (decl);
3360 unsigned int align;
3361 bool asan_protected = false;
3363 /* Make sure any other constants whose addresses appear in EXP
3364 are assigned label numbers. */
3365 output_addressed_constants (exp);
3367 /* We are no longer deferring this constant. */
3368 TREE_ASM_WRITTEN (decl) = TREE_ASM_WRITTEN (exp) = 1;
3370 if ((flag_sanitize & SANITIZE_ADDRESS)
3371 && TREE_CODE (exp) == STRING_CST
3372 && asan_protect_global (exp))
3374 asan_protected = true;
3375 DECL_ALIGN (decl) = MAX (DECL_ALIGN (decl),
3376 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
3379 /* If the constant is part of an object block, make sure that the
3380 decl has been positioned within its block, but do not write out
3381 its definition yet. output_object_blocks will do that later. */
3382 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
3383 place_block_symbol (symbol);
3384 else
3386 align = DECL_ALIGN (decl);
3387 switch_to_section (get_constant_section (exp, align));
3388 if (align > BITS_PER_UNIT)
3389 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
3390 assemble_constant_contents (exp, XSTR (symbol, 0), align);
3391 if (asan_protected)
3393 HOST_WIDE_INT size = get_constant_size (exp);
3394 assemble_zeros (asan_red_zone_size (size));
3399 /* Look up EXP in the table of constant descriptors. Return the rtl
3400 if it has been emitted, else null. */
3403 lookup_constant_def (tree exp)
3405 struct constant_descriptor_tree key;
3407 key.value = exp;
3408 key.hash = const_hash_1 (exp);
3409 constant_descriptor_tree *desc
3410 = const_desc_htab->find_with_hash (&key, key.hash);
3412 return (desc ? desc->rtl : NULL_RTX);
3415 /* Return a tree representing a reference to constant data in memory
3416 for the constant expression EXP.
3418 This is the counterpart of output_constant_def at the Tree level. */
3420 tree
3421 tree_output_constant_def (tree exp)
3423 struct constant_descriptor_tree *desc, key;
3424 tree decl;
3426 /* Look up EXP in the table of constant descriptors. If we didn't find
3427 it, create a new one. */
3428 key.value = exp;
3429 key.hash = const_hash_1 (exp);
3430 constant_descriptor_tree **loc
3431 = const_desc_htab->find_slot_with_hash (&key, key.hash, INSERT);
3433 desc = *loc;
3434 if (desc == 0)
3436 desc = build_constant_desc (exp);
3437 desc->hash = key.hash;
3438 *loc = desc;
3441 decl = SYMBOL_REF_DECL (XEXP (desc->rtl, 0));
3442 varpool_node::finalize_decl (decl);
3443 return decl;
3446 struct GTY((chain_next ("%h.next"), for_user)) constant_descriptor_rtx {
3447 struct constant_descriptor_rtx *next;
3448 rtx mem;
3449 rtx sym;
3450 rtx constant;
3451 HOST_WIDE_INT offset;
3452 hashval_t hash;
3453 machine_mode mode;
3454 unsigned int align;
3455 int labelno;
3456 int mark;
3459 struct const_rtx_desc_hasher : ggc_hasher<constant_descriptor_rtx *>
3461 static hashval_t hash (constant_descriptor_rtx *);
3462 static bool equal (constant_descriptor_rtx *, constant_descriptor_rtx *);
3465 /* Used in the hash tables to avoid outputting the same constant
3466 twice. Unlike 'struct constant_descriptor_tree', RTX constants
3467 are output once per function, not once per file. */
3468 /* ??? Only a few targets need per-function constant pools. Most
3469 can use one per-file pool. Should add a targetm bit to tell the
3470 difference. */
3472 struct GTY(()) rtx_constant_pool {
3473 /* Pointers to first and last constant in pool, as ordered by offset. */
3474 struct constant_descriptor_rtx *first;
3475 struct constant_descriptor_rtx *last;
3477 /* Hash facility for making memory-constants from constant rtl-expressions.
3478 It is used on RISC machines where immediate integer arguments and
3479 constant addresses are restricted so that such constants must be stored
3480 in memory. */
3481 hash_table<const_rtx_desc_hasher> *const_rtx_htab;
3483 /* Current offset in constant pool (does not include any
3484 machine-specific header). */
3485 HOST_WIDE_INT offset;
3488 /* Hash and compare functions for const_rtx_htab. */
3490 hashval_t
3491 const_rtx_desc_hasher::hash (constant_descriptor_rtx *desc)
3493 return desc->hash;
3496 bool
3497 const_rtx_desc_hasher::equal (constant_descriptor_rtx *x,
3498 constant_descriptor_rtx *y)
3500 if (x->mode != y->mode)
3501 return 0;
3502 return rtx_equal_p (x->constant, y->constant);
3505 /* Hash one component of a constant. */
3507 static hashval_t
3508 const_rtx_hash_1 (const_rtx x)
3510 unsigned HOST_WIDE_INT hwi;
3511 machine_mode mode;
3512 enum rtx_code code;
3513 hashval_t h;
3514 int i;
3516 code = GET_CODE (x);
3517 mode = GET_MODE (x);
3518 h = (hashval_t) code * 1048573 + mode;
3520 switch (code)
3522 case CONST_INT:
3523 hwi = INTVAL (x);
3525 fold_hwi:
3527 int shift = sizeof (hashval_t) * CHAR_BIT;
3528 const int n = sizeof (HOST_WIDE_INT) / sizeof (hashval_t);
3530 h ^= (hashval_t) hwi;
3531 for (i = 1; i < n; ++i)
3533 hwi >>= shift;
3534 h ^= (hashval_t) hwi;
3537 break;
3539 case CONST_WIDE_INT:
3540 hwi = GET_MODE_PRECISION (mode);
3542 for (i = 0; i < CONST_WIDE_INT_NUNITS (x); i++)
3543 hwi ^= CONST_WIDE_INT_ELT (x, i);
3544 goto fold_hwi;
3547 case CONST_DOUBLE:
3548 if (TARGET_SUPPORTS_WIDE_INT == 0 && mode == VOIDmode)
3550 hwi = CONST_DOUBLE_LOW (x) ^ CONST_DOUBLE_HIGH (x);
3551 goto fold_hwi;
3553 else
3554 h ^= real_hash (CONST_DOUBLE_REAL_VALUE (x));
3555 break;
3557 case CONST_FIXED:
3558 h ^= fixed_hash (CONST_FIXED_VALUE (x));
3559 break;
3561 case SYMBOL_REF:
3562 h ^= htab_hash_string (XSTR (x, 0));
3563 break;
3565 case LABEL_REF:
3566 h = h * 251 + CODE_LABEL_NUMBER (LABEL_REF_LABEL (x));
3567 break;
3569 case UNSPEC:
3570 case UNSPEC_VOLATILE:
3571 h = h * 251 + XINT (x, 1);
3572 break;
3574 default:
3575 break;
3578 return h;
3581 /* Compute a hash value for X, which should be a constant. */
3583 static hashval_t
3584 const_rtx_hash (rtx x)
3586 hashval_t h = 0;
3587 subrtx_iterator::array_type array;
3588 FOR_EACH_SUBRTX (iter, array, x, ALL)
3589 h = h * 509 + const_rtx_hash_1 (*iter);
3590 return h;
3594 /* Create and return a new rtx constant pool. */
3596 static struct rtx_constant_pool *
3597 create_constant_pool (void)
3599 struct rtx_constant_pool *pool;
3601 pool = ggc_alloc<rtx_constant_pool> ();
3602 pool->const_rtx_htab = hash_table<const_rtx_desc_hasher>::create_ggc (31);
3603 pool->first = NULL;
3604 pool->last = NULL;
3605 pool->offset = 0;
3606 return pool;
3609 /* Initialize constant pool hashing for a new function. */
3611 void
3612 init_varasm_status (void)
3614 crtl->varasm.pool = create_constant_pool ();
3615 crtl->varasm.deferred_constants = 0;
3618 /* Given a MINUS expression, simplify it if both sides
3619 include the same symbol. */
3622 simplify_subtraction (rtx x)
3624 rtx r = simplify_rtx (x);
3625 return r ? r : x;
3628 /* Given a constant rtx X, make (or find) a memory constant for its value
3629 and return a MEM rtx to refer to it in memory. */
3632 force_const_mem (machine_mode mode, rtx x)
3634 struct constant_descriptor_rtx *desc, tmp;
3635 struct rtx_constant_pool *pool;
3636 char label[256];
3637 rtx def, symbol;
3638 hashval_t hash;
3639 unsigned int align;
3640 constant_descriptor_rtx **slot;
3642 /* If we're not allowed to drop X into the constant pool, don't. */
3643 if (targetm.cannot_force_const_mem (mode, x))
3644 return NULL_RTX;
3646 /* Record that this function has used a constant pool entry. */
3647 crtl->uses_const_pool = 1;
3649 /* Decide which pool to use. */
3650 pool = (targetm.use_blocks_for_constant_p (mode, x)
3651 ? shared_constant_pool
3652 : crtl->varasm.pool);
3654 /* Lookup the value in the hashtable. */
3655 tmp.constant = x;
3656 tmp.mode = mode;
3657 hash = const_rtx_hash (x);
3658 slot = pool->const_rtx_htab->find_slot_with_hash (&tmp, hash, INSERT);
3659 desc = *slot;
3661 /* If the constant was already present, return its memory. */
3662 if (desc)
3663 return copy_rtx (desc->mem);
3665 /* Otherwise, create a new descriptor. */
3666 desc = ggc_alloc<constant_descriptor_rtx> ();
3667 *slot = desc;
3669 /* Align the location counter as required by EXP's data type. */
3670 align = GET_MODE_ALIGNMENT (mode == VOIDmode ? word_mode : mode);
3671 #ifdef CONSTANT_ALIGNMENT
3673 tree type = lang_hooks.types.type_for_mode (mode, 0);
3674 if (type != NULL_TREE)
3675 align = CONSTANT_ALIGNMENT (make_tree (type, x), align);
3677 #endif
3679 pool->offset += (align / BITS_PER_UNIT) - 1;
3680 pool->offset &= ~ ((align / BITS_PER_UNIT) - 1);
3682 desc->next = NULL;
3683 desc->constant = copy_rtx (tmp.constant);
3684 desc->offset = pool->offset;
3685 desc->hash = hash;
3686 desc->mode = mode;
3687 desc->align = align;
3688 desc->labelno = const_labelno;
3689 desc->mark = 0;
3691 pool->offset += GET_MODE_SIZE (mode);
3692 if (pool->last)
3693 pool->last->next = desc;
3694 else
3695 pool->first = pool->last = desc;
3696 pool->last = desc;
3698 /* Create a string containing the label name, in LABEL. */
3699 ASM_GENERATE_INTERNAL_LABEL (label, "LC", const_labelno);
3700 ++const_labelno;
3702 /* Construct the SYMBOL_REF. Make sure to mark it as belonging to
3703 the constants pool. */
3704 if (use_object_blocks_p () && targetm.use_blocks_for_constant_p (mode, x))
3706 section *sect = targetm.asm_out.select_rtx_section (mode, x, align);
3707 symbol = create_block_symbol (ggc_strdup (label),
3708 get_block_for_section (sect), -1);
3710 else
3711 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3712 desc->sym = symbol;
3713 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3714 CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3715 SET_SYMBOL_REF_CONSTANT (symbol, desc);
3717 /* Construct the MEM. */
3718 desc->mem = def = gen_const_mem (mode, symbol);
3719 set_mem_attributes (def, lang_hooks.types.type_for_mode (mode, 0), 1);
3720 set_mem_align (def, align);
3722 /* If we're dropping a label to the constant pool, make sure we
3723 don't delete it. */
3724 if (GET_CODE (x) == LABEL_REF)
3725 LABEL_PRESERVE_P (XEXP (x, 0)) = 1;
3727 return copy_rtx (def);
3730 /* Given a constant pool SYMBOL_REF, return the corresponding constant. */
3733 get_pool_constant (const_rtx addr)
3735 return SYMBOL_REF_CONSTANT (addr)->constant;
3738 /* Given a constant pool SYMBOL_REF, return the corresponding constant
3739 and whether it has been output or not. */
3742 get_pool_constant_mark (rtx addr, bool *pmarked)
3744 struct constant_descriptor_rtx *desc;
3746 desc = SYMBOL_REF_CONSTANT (addr);
3747 *pmarked = (desc->mark != 0);
3748 return desc->constant;
3751 /* Similar, return the mode. */
3753 machine_mode
3754 get_pool_mode (const_rtx addr)
3756 return SYMBOL_REF_CONSTANT (addr)->mode;
3759 /* Return the size of the constant pool. */
3762 get_pool_size (void)
3764 return crtl->varasm.pool->offset;
3767 /* Worker function for output_constant_pool_1. Emit assembly for X
3768 in MODE with known alignment ALIGN. */
3770 static void
3771 output_constant_pool_2 (machine_mode mode, rtx x, unsigned int align)
3773 switch (GET_MODE_CLASS (mode))
3775 case MODE_FLOAT:
3776 case MODE_DECIMAL_FLOAT:
3778 REAL_VALUE_TYPE r;
3780 gcc_assert (CONST_DOUBLE_AS_FLOAT_P (x));
3781 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
3782 assemble_real (r, mode, align);
3783 break;
3786 case MODE_INT:
3787 case MODE_PARTIAL_INT:
3788 case MODE_FRACT:
3789 case MODE_UFRACT:
3790 case MODE_ACCUM:
3791 case MODE_UACCUM:
3792 case MODE_POINTER_BOUNDS:
3793 assemble_integer (x, GET_MODE_SIZE (mode), align, 1);
3794 break;
3796 case MODE_VECTOR_FLOAT:
3797 case MODE_VECTOR_INT:
3798 case MODE_VECTOR_FRACT:
3799 case MODE_VECTOR_UFRACT:
3800 case MODE_VECTOR_ACCUM:
3801 case MODE_VECTOR_UACCUM:
3803 int i, units;
3804 machine_mode submode = GET_MODE_INNER (mode);
3805 unsigned int subalign = MIN (align, GET_MODE_BITSIZE (submode));
3807 gcc_assert (GET_CODE (x) == CONST_VECTOR);
3808 units = CONST_VECTOR_NUNITS (x);
3810 for (i = 0; i < units; i++)
3812 rtx elt = CONST_VECTOR_ELT (x, i);
3813 output_constant_pool_2 (submode, elt, i ? subalign : align);
3816 break;
3818 default:
3819 gcc_unreachable ();
3823 /* Worker function for output_constant_pool. Emit constant DESC,
3824 giving it ALIGN bits of alignment. */
3826 static void
3827 output_constant_pool_1 (struct constant_descriptor_rtx *desc,
3828 unsigned int align)
3830 rtx x, tmp;
3832 x = desc->constant;
3834 /* See if X is a LABEL_REF (or a CONST referring to a LABEL_REF)
3835 whose CODE_LABEL has been deleted. This can occur if a jump table
3836 is eliminated by optimization. If so, write a constant of zero
3837 instead. Note that this can also happen by turning the
3838 CODE_LABEL into a NOTE. */
3839 /* ??? This seems completely and utterly wrong. Certainly it's
3840 not true for NOTE_INSN_DELETED_LABEL, but I disbelieve proper
3841 functioning even with rtx_insn::deleted and friends. */
3843 tmp = x;
3844 switch (GET_CODE (tmp))
3846 case CONST:
3847 if (GET_CODE (XEXP (tmp, 0)) != PLUS
3848 || GET_CODE (XEXP (XEXP (tmp, 0), 0)) != LABEL_REF)
3849 break;
3850 tmp = XEXP (XEXP (tmp, 0), 0);
3851 /* FALLTHRU */
3853 case LABEL_REF:
3854 tmp = LABEL_REF_LABEL (tmp);
3855 gcc_assert (!as_a<rtx_insn *> (tmp)->deleted ());
3856 gcc_assert (!NOTE_P (tmp)
3857 || NOTE_KIND (tmp) != NOTE_INSN_DELETED);
3858 break;
3860 default:
3861 break;
3864 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3865 ASM_OUTPUT_SPECIAL_POOL_ENTRY (asm_out_file, x, desc->mode,
3866 align, desc->labelno, done);
3867 #endif
3869 assemble_align (align);
3871 /* Output the label. */
3872 targetm.asm_out.internal_label (asm_out_file, "LC", desc->labelno);
3874 /* Output the data. */
3875 output_constant_pool_2 (desc->mode, x, align);
3877 /* Make sure all constants in SECTION_MERGE and not SECTION_STRINGS
3878 sections have proper size. */
3879 if (align > GET_MODE_BITSIZE (desc->mode)
3880 && in_section
3881 && (in_section->common.flags & SECTION_MERGE))
3882 assemble_align (align);
3884 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3885 done:
3886 #endif
3887 return;
3890 /* Mark all constants that are referenced by SYMBOL_REFs in X.
3891 Emit referenced deferred strings. */
3893 static void
3894 mark_constants_in_pattern (rtx insn)
3896 subrtx_iterator::array_type array;
3897 FOR_EACH_SUBRTX (iter, array, PATTERN (insn), ALL)
3899 const_rtx x = *iter;
3900 if (GET_CODE (x) == SYMBOL_REF)
3902 if (CONSTANT_POOL_ADDRESS_P (x))
3904 struct constant_descriptor_rtx *desc = SYMBOL_REF_CONSTANT (x);
3905 if (desc->mark == 0)
3907 desc->mark = 1;
3908 iter.substitute (desc->constant);
3911 else if (TREE_CONSTANT_POOL_ADDRESS_P (x))
3913 tree decl = SYMBOL_REF_DECL (x);
3914 if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
3916 n_deferred_constants--;
3917 output_constant_def_contents (CONST_CAST_RTX (x));
3924 /* Look through appropriate parts of INSN, marking all entries in the
3925 constant pool which are actually being used. Entries that are only
3926 referenced by other constants are also marked as used. Emit
3927 deferred strings that are used. */
3929 static void
3930 mark_constants (rtx_insn *insn)
3932 if (!INSN_P (insn))
3933 return;
3935 /* Insns may appear inside a SEQUENCE. Only check the patterns of
3936 insns, not any notes that may be attached. We don't want to mark
3937 a constant just because it happens to appear in a REG_EQUIV note. */
3938 if (rtx_sequence *seq = dyn_cast <rtx_sequence *> (PATTERN (insn)))
3940 int i, n = seq->len ();
3941 for (i = 0; i < n; ++i)
3943 rtx subinsn = seq->element (i);
3944 if (INSN_P (subinsn))
3945 mark_constants_in_pattern (subinsn);
3948 else
3949 mark_constants_in_pattern (insn);
3952 /* Look through the instructions for this function, and mark all the
3953 entries in POOL which are actually being used. Emit deferred constants
3954 which have indeed been used. */
3956 static void
3957 mark_constant_pool (void)
3959 rtx_insn *insn;
3961 if (!crtl->uses_const_pool && n_deferred_constants == 0)
3962 return;
3964 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3965 mark_constants (insn);
3968 /* Write all the constants in POOL. */
3970 static void
3971 output_constant_pool_contents (struct rtx_constant_pool *pool)
3973 struct constant_descriptor_rtx *desc;
3975 for (desc = pool->first; desc ; desc = desc->next)
3976 if (desc->mark)
3978 /* If the constant is part of an object_block, make sure that
3979 the constant has been positioned within its block, but do not
3980 write out its definition yet. output_object_blocks will do
3981 that later. */
3982 if (SYMBOL_REF_HAS_BLOCK_INFO_P (desc->sym)
3983 && SYMBOL_REF_BLOCK (desc->sym))
3984 place_block_symbol (desc->sym);
3985 else
3987 switch_to_section (targetm.asm_out.select_rtx_section
3988 (desc->mode, desc->constant, desc->align));
3989 output_constant_pool_1 (desc, desc->align);
3994 /* Mark all constants that are used in the current function, then write
3995 out the function's private constant pool. */
3997 static void
3998 output_constant_pool (const char *fnname ATTRIBUTE_UNUSED,
3999 tree fndecl ATTRIBUTE_UNUSED)
4001 struct rtx_constant_pool *pool = crtl->varasm.pool;
4003 /* It is possible for gcc to call force_const_mem and then to later
4004 discard the instructions which refer to the constant. In such a
4005 case we do not need to output the constant. */
4006 mark_constant_pool ();
4008 #ifdef ASM_OUTPUT_POOL_PROLOGUE
4009 ASM_OUTPUT_POOL_PROLOGUE (asm_out_file, fnname, fndecl, pool->offset);
4010 #endif
4012 output_constant_pool_contents (pool);
4014 #ifdef ASM_OUTPUT_POOL_EPILOGUE
4015 ASM_OUTPUT_POOL_EPILOGUE (asm_out_file, fnname, fndecl, pool->offset);
4016 #endif
4019 /* Write the contents of the shared constant pool. */
4021 void
4022 output_shared_constant_pool (void)
4024 output_constant_pool_contents (shared_constant_pool);
4027 /* Determine what kind of relocations EXP may need. */
4030 compute_reloc_for_constant (tree exp)
4032 int reloc = 0, reloc2;
4033 tree tem;
4035 switch (TREE_CODE (exp))
4037 case ADDR_EXPR:
4038 case FDESC_EXPR:
4039 /* Go inside any operations that get_inner_reference can handle and see
4040 if what's inside is a constant: no need to do anything here for
4041 addresses of variables or functions. */
4042 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
4043 tem = TREE_OPERAND (tem, 0))
4046 if (TREE_CODE (tem) == MEM_REF
4047 && TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR)
4049 reloc = compute_reloc_for_constant (TREE_OPERAND (tem, 0));
4050 break;
4053 if (!targetm.binds_local_p (tem))
4054 reloc |= 2;
4055 else
4056 reloc |= 1;
4057 break;
4059 case PLUS_EXPR:
4060 case POINTER_PLUS_EXPR:
4061 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4062 reloc |= compute_reloc_for_constant (TREE_OPERAND (exp, 1));
4063 break;
4065 case MINUS_EXPR:
4066 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4067 reloc2 = compute_reloc_for_constant (TREE_OPERAND (exp, 1));
4068 /* The difference of two local labels is computable at link time. */
4069 if (reloc == 1 && reloc2 == 1)
4070 reloc = 0;
4071 else
4072 reloc |= reloc2;
4073 break;
4075 CASE_CONVERT:
4076 case VIEW_CONVERT_EXPR:
4077 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4078 break;
4080 case CONSTRUCTOR:
4082 unsigned HOST_WIDE_INT idx;
4083 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
4084 if (tem != 0)
4085 reloc |= compute_reloc_for_constant (tem);
4087 break;
4089 default:
4090 break;
4092 return reloc;
4095 /* Find all the constants whose addresses are referenced inside of EXP,
4096 and make sure assembler code with a label has been output for each one.
4097 Indicate whether an ADDR_EXPR has been encountered. */
4099 static void
4100 output_addressed_constants (tree exp)
4102 tree tem;
4104 switch (TREE_CODE (exp))
4106 case ADDR_EXPR:
4107 case FDESC_EXPR:
4108 /* Go inside any operations that get_inner_reference can handle and see
4109 if what's inside is a constant: no need to do anything here for
4110 addresses of variables or functions. */
4111 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
4112 tem = TREE_OPERAND (tem, 0))
4115 /* If we have an initialized CONST_DECL, retrieve the initializer. */
4116 if (TREE_CODE (tem) == CONST_DECL && DECL_INITIAL (tem))
4117 tem = DECL_INITIAL (tem);
4119 if (CONSTANT_CLASS_P (tem) || TREE_CODE (tem) == CONSTRUCTOR)
4120 output_constant_def (tem, 0);
4122 if (TREE_CODE (tem) == MEM_REF)
4123 output_addressed_constants (TREE_OPERAND (tem, 0));
4124 break;
4126 case PLUS_EXPR:
4127 case POINTER_PLUS_EXPR:
4128 case MINUS_EXPR:
4129 output_addressed_constants (TREE_OPERAND (exp, 1));
4130 /* Fall through. */
4132 CASE_CONVERT:
4133 case VIEW_CONVERT_EXPR:
4134 output_addressed_constants (TREE_OPERAND (exp, 0));
4135 break;
4137 case CONSTRUCTOR:
4139 unsigned HOST_WIDE_INT idx;
4140 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
4141 if (tem != 0)
4142 output_addressed_constants (tem);
4144 break;
4146 default:
4147 break;
4151 /* Whether a constructor CTOR is a valid static constant initializer if all
4152 its elements are. This used to be internal to initializer_constant_valid_p
4153 and has been exposed to let other functions like categorize_ctor_elements
4154 evaluate the property while walking a constructor for other purposes. */
4156 bool
4157 constructor_static_from_elts_p (const_tree ctor)
4159 return (TREE_CONSTANT (ctor)
4160 && (TREE_CODE (TREE_TYPE (ctor)) == UNION_TYPE
4161 || TREE_CODE (TREE_TYPE (ctor)) == RECORD_TYPE
4162 || TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE));
4165 static tree initializer_constant_valid_p_1 (tree value, tree endtype,
4166 tree *cache);
4168 /* A subroutine of initializer_constant_valid_p. VALUE is a MINUS_EXPR,
4169 PLUS_EXPR or POINTER_PLUS_EXPR. This looks for cases of VALUE
4170 which are valid when ENDTYPE is an integer of any size; in
4171 particular, this does not accept a pointer minus a constant. This
4172 returns null_pointer_node if the VALUE is an absolute constant
4173 which can be used to initialize a static variable. Otherwise it
4174 returns NULL. */
4176 static tree
4177 narrowing_initializer_constant_valid_p (tree value, tree endtype, tree *cache)
4179 tree op0, op1;
4181 if (!INTEGRAL_TYPE_P (endtype))
4182 return NULL_TREE;
4184 op0 = TREE_OPERAND (value, 0);
4185 op1 = TREE_OPERAND (value, 1);
4187 /* Like STRIP_NOPS except allow the operand mode to widen. This
4188 works around a feature of fold that simplifies (int)(p1 - p2) to
4189 ((int)p1 - (int)p2) under the theory that the narrower operation
4190 is cheaper. */
4192 while (CONVERT_EXPR_P (op0)
4193 || TREE_CODE (op0) == NON_LVALUE_EXPR)
4195 tree inner = TREE_OPERAND (op0, 0);
4196 if (inner == error_mark_node
4197 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4198 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))
4199 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
4200 break;
4201 op0 = inner;
4204 while (CONVERT_EXPR_P (op1)
4205 || TREE_CODE (op1) == NON_LVALUE_EXPR)
4207 tree inner = TREE_OPERAND (op1, 0);
4208 if (inner == error_mark_node
4209 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4210 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op1)))
4211 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
4212 break;
4213 op1 = inner;
4216 op0 = initializer_constant_valid_p_1 (op0, endtype, cache);
4217 if (!op0)
4218 return NULL_TREE;
4220 op1 = initializer_constant_valid_p_1 (op1, endtype,
4221 cache ? cache + 2 : NULL);
4222 /* Both initializers must be known. */
4223 if (op1)
4225 if (op0 == op1
4226 && (op0 == null_pointer_node
4227 || TREE_CODE (value) == MINUS_EXPR))
4228 return null_pointer_node;
4230 /* Support differences between labels. */
4231 if (TREE_CODE (op0) == LABEL_DECL
4232 && TREE_CODE (op1) == LABEL_DECL)
4233 return null_pointer_node;
4235 if (TREE_CODE (op0) == STRING_CST
4236 && TREE_CODE (op1) == STRING_CST
4237 && operand_equal_p (op0, op1, 1))
4238 return null_pointer_node;
4241 return NULL_TREE;
4244 /* Helper function of initializer_constant_valid_p.
4245 Return nonzero if VALUE is a valid constant-valued expression
4246 for use in initializing a static variable; one that can be an
4247 element of a "constant" initializer.
4249 Return null_pointer_node if the value is absolute;
4250 if it is relocatable, return the variable that determines the relocation.
4251 We assume that VALUE has been folded as much as possible;
4252 therefore, we do not need to check for such things as
4253 arithmetic-combinations of integers.
4255 Use CACHE (pointer to 2 tree values) for caching if non-NULL. */
4257 static tree
4258 initializer_constant_valid_p_1 (tree value, tree endtype, tree *cache)
4260 tree ret;
4262 switch (TREE_CODE (value))
4264 case CONSTRUCTOR:
4265 if (constructor_static_from_elts_p (value))
4267 unsigned HOST_WIDE_INT idx;
4268 tree elt;
4269 bool absolute = true;
4271 if (cache && cache[0] == value)
4272 return cache[1];
4273 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
4275 tree reloc;
4276 reloc = initializer_constant_valid_p_1 (elt, TREE_TYPE (elt),
4277 NULL);
4278 if (!reloc)
4280 if (cache)
4282 cache[0] = value;
4283 cache[1] = NULL_TREE;
4285 return NULL_TREE;
4287 if (reloc != null_pointer_node)
4288 absolute = false;
4290 /* For a non-absolute relocation, there is no single
4291 variable that can be "the variable that determines the
4292 relocation." */
4293 if (cache)
4295 cache[0] = value;
4296 cache[1] = absolute ? null_pointer_node : error_mark_node;
4298 return absolute ? null_pointer_node : error_mark_node;
4301 return TREE_STATIC (value) ? null_pointer_node : NULL_TREE;
4303 case INTEGER_CST:
4304 case VECTOR_CST:
4305 case REAL_CST:
4306 case FIXED_CST:
4307 case STRING_CST:
4308 case COMPLEX_CST:
4309 return null_pointer_node;
4311 case ADDR_EXPR:
4312 case FDESC_EXPR:
4314 tree op0 = staticp (TREE_OPERAND (value, 0));
4315 if (op0)
4317 /* "&(*a).f" is like unto pointer arithmetic. If "a" turns out
4318 to be a constant, this is old-skool offsetof-like nonsense. */
4319 if (TREE_CODE (op0) == INDIRECT_REF
4320 && TREE_CONSTANT (TREE_OPERAND (op0, 0)))
4321 return null_pointer_node;
4322 /* Taking the address of a nested function involves a trampoline,
4323 unless we don't need or want one. */
4324 if (TREE_CODE (op0) == FUNCTION_DECL
4325 && DECL_STATIC_CHAIN (op0)
4326 && !TREE_NO_TRAMPOLINE (value))
4327 return NULL_TREE;
4328 /* "&{...}" requires a temporary to hold the constructed
4329 object. */
4330 if (TREE_CODE (op0) == CONSTRUCTOR)
4331 return NULL_TREE;
4333 return op0;
4336 case NON_LVALUE_EXPR:
4337 return initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4338 endtype, cache);
4340 case VIEW_CONVERT_EXPR:
4342 tree src = TREE_OPERAND (value, 0);
4343 tree src_type = TREE_TYPE (src);
4344 tree dest_type = TREE_TYPE (value);
4346 /* Allow view-conversions from aggregate to non-aggregate type only
4347 if the bit pattern is fully preserved afterwards; otherwise, the
4348 RTL expander won't be able to apply a subsequent transformation
4349 to the underlying constructor. */
4350 if (AGGREGATE_TYPE_P (src_type) && !AGGREGATE_TYPE_P (dest_type))
4352 if (TYPE_MODE (endtype) == TYPE_MODE (dest_type))
4353 return initializer_constant_valid_p_1 (src, endtype, cache);
4354 else
4355 return NULL_TREE;
4358 /* Allow all other kinds of view-conversion. */
4359 return initializer_constant_valid_p_1 (src, endtype, cache);
4362 CASE_CONVERT:
4364 tree src = TREE_OPERAND (value, 0);
4365 tree src_type = TREE_TYPE (src);
4366 tree dest_type = TREE_TYPE (value);
4368 /* Allow conversions between pointer types, floating-point
4369 types, and offset types. */
4370 if ((POINTER_TYPE_P (dest_type) && POINTER_TYPE_P (src_type))
4371 || (FLOAT_TYPE_P (dest_type) && FLOAT_TYPE_P (src_type))
4372 || (TREE_CODE (dest_type) == OFFSET_TYPE
4373 && TREE_CODE (src_type) == OFFSET_TYPE))
4374 return initializer_constant_valid_p_1 (src, endtype, cache);
4376 /* Allow length-preserving conversions between integer types. */
4377 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type)
4378 && (TYPE_PRECISION (dest_type) == TYPE_PRECISION (src_type)))
4379 return initializer_constant_valid_p_1 (src, endtype, cache);
4381 /* Allow conversions between other integer types only if
4382 explicit value. */
4383 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type))
4385 tree inner = initializer_constant_valid_p_1 (src, endtype, cache);
4386 if (inner == null_pointer_node)
4387 return null_pointer_node;
4388 break;
4391 /* Allow (int) &foo provided int is as wide as a pointer. */
4392 if (INTEGRAL_TYPE_P (dest_type) && POINTER_TYPE_P (src_type)
4393 && (TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type)))
4394 return initializer_constant_valid_p_1 (src, endtype, cache);
4396 /* Likewise conversions from int to pointers, but also allow
4397 conversions from 0. */
4398 if ((POINTER_TYPE_P (dest_type)
4399 || TREE_CODE (dest_type) == OFFSET_TYPE)
4400 && INTEGRAL_TYPE_P (src_type))
4402 if (TREE_CODE (src) == INTEGER_CST
4403 && TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type))
4404 return null_pointer_node;
4405 if (integer_zerop (src))
4406 return null_pointer_node;
4407 else if (TYPE_PRECISION (dest_type) <= TYPE_PRECISION (src_type))
4408 return initializer_constant_valid_p_1 (src, endtype, cache);
4411 /* Allow conversions to struct or union types if the value
4412 inside is okay. */
4413 if (TREE_CODE (dest_type) == RECORD_TYPE
4414 || TREE_CODE (dest_type) == UNION_TYPE)
4415 return initializer_constant_valid_p_1 (src, endtype, cache);
4417 break;
4419 case POINTER_PLUS_EXPR:
4420 case PLUS_EXPR:
4421 /* Any valid floating-point constants will have been folded by now;
4422 with -frounding-math we hit this with addition of two constants. */
4423 if (TREE_CODE (endtype) == REAL_TYPE)
4424 return NULL_TREE;
4425 if (cache && cache[0] == value)
4426 return cache[1];
4427 if (! INTEGRAL_TYPE_P (endtype)
4428 || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
4430 tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
4431 tree valid0
4432 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4433 endtype, ncache);
4434 tree valid1
4435 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
4436 endtype, ncache + 2);
4437 /* If either term is absolute, use the other term's relocation. */
4438 if (valid0 == null_pointer_node)
4439 ret = valid1;
4440 else if (valid1 == null_pointer_node)
4441 ret = valid0;
4442 /* Support narrowing pointer differences. */
4443 else
4444 ret = narrowing_initializer_constant_valid_p (value, endtype,
4445 ncache);
4447 else
4448 /* Support narrowing pointer differences. */
4449 ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
4450 if (cache)
4452 cache[0] = value;
4453 cache[1] = ret;
4455 return ret;
4457 case MINUS_EXPR:
4458 if (TREE_CODE (endtype) == REAL_TYPE)
4459 return NULL_TREE;
4460 if (cache && cache[0] == value)
4461 return cache[1];
4462 if (! INTEGRAL_TYPE_P (endtype)
4463 || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
4465 tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
4466 tree valid0
4467 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4468 endtype, ncache);
4469 tree valid1
4470 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
4471 endtype, ncache + 2);
4472 /* Win if second argument is absolute. */
4473 if (valid1 == null_pointer_node)
4474 ret = valid0;
4475 /* Win if both arguments have the same relocation.
4476 Then the value is absolute. */
4477 else if (valid0 == valid1 && valid0 != 0)
4478 ret = null_pointer_node;
4479 /* Since GCC guarantees that string constants are unique in the
4480 generated code, a subtraction between two copies of the same
4481 constant string is absolute. */
4482 else if (valid0 && TREE_CODE (valid0) == STRING_CST
4483 && valid1 && TREE_CODE (valid1) == STRING_CST
4484 && operand_equal_p (valid0, valid1, 1))
4485 ret = null_pointer_node;
4486 /* Support narrowing differences. */
4487 else
4488 ret = narrowing_initializer_constant_valid_p (value, endtype,
4489 ncache);
4491 else
4492 /* Support narrowing differences. */
4493 ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
4494 if (cache)
4496 cache[0] = value;
4497 cache[1] = ret;
4499 return ret;
4501 default:
4502 break;
4505 return NULL_TREE;
4508 /* Return nonzero if VALUE is a valid constant-valued expression
4509 for use in initializing a static variable; one that can be an
4510 element of a "constant" initializer.
4512 Return null_pointer_node if the value is absolute;
4513 if it is relocatable, return the variable that determines the relocation.
4514 We assume that VALUE has been folded as much as possible;
4515 therefore, we do not need to check for such things as
4516 arithmetic-combinations of integers. */
4517 tree
4518 initializer_constant_valid_p (tree value, tree endtype)
4520 return initializer_constant_valid_p_1 (value, endtype, NULL);
4523 /* Return true if VALUE is a valid constant-valued expression
4524 for use in initializing a static bit-field; one that can be
4525 an element of a "constant" initializer. */
4527 bool
4528 initializer_constant_valid_for_bitfield_p (tree value)
4530 /* For bitfields we support integer constants or possibly nested aggregates
4531 of such. */
4532 switch (TREE_CODE (value))
4534 case CONSTRUCTOR:
4536 unsigned HOST_WIDE_INT idx;
4537 tree elt;
4539 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
4540 if (!initializer_constant_valid_for_bitfield_p (elt))
4541 return false;
4542 return true;
4545 case INTEGER_CST:
4546 case REAL_CST:
4547 return true;
4549 case VIEW_CONVERT_EXPR:
4550 case NON_LVALUE_EXPR:
4551 return
4552 initializer_constant_valid_for_bitfield_p (TREE_OPERAND (value, 0));
4554 default:
4555 break;
4558 return false;
4561 /* output_constructor outer state of relevance in recursive calls, typically
4562 for nested aggregate bitfields. */
4564 typedef struct {
4565 unsigned int bit_offset; /* current position in ... */
4566 int byte; /* ... the outer byte buffer. */
4567 } oc_outer_state;
4569 static unsigned HOST_WIDE_INT
4570 output_constructor (tree, unsigned HOST_WIDE_INT, unsigned int, bool,
4571 oc_outer_state *);
4573 /* Output assembler code for constant EXP, with no label.
4574 This includes the pseudo-op such as ".int" or ".byte", and a newline.
4575 Assumes output_addressed_constants has been done on EXP already.
4577 Generate at least SIZE bytes of assembler data, padding at the end
4578 with zeros if necessary. SIZE must always be specified. The returned
4579 value is the actual number of bytes of assembler data generated, which
4580 may be bigger than SIZE if the object contains a variable length field.
4582 SIZE is important for structure constructors,
4583 since trailing members may have been omitted from the constructor.
4584 It is also important for initialization of arrays from string constants
4585 since the full length of the string constant might not be wanted.
4586 It is also needed for initialization of unions, where the initializer's
4587 type is just one member, and that may not be as long as the union.
4589 There a case in which we would fail to output exactly SIZE bytes:
4590 for a structure constructor that wants to produce more than SIZE bytes.
4591 But such constructors will never be generated for any possible input.
4593 ALIGN is the alignment of the data in bits.
4595 If REVERSE is true, EXP is interpreted in reverse storage order wrt the
4596 target order. */
4598 static unsigned HOST_WIDE_INT
4599 output_constant (tree exp, unsigned HOST_WIDE_INT size, unsigned int align,
4600 bool reverse)
4602 enum tree_code code;
4603 unsigned HOST_WIDE_INT thissize;
4605 if (size == 0 || flag_syntax_only)
4606 return size;
4608 /* See if we're trying to initialize a pointer in a non-default mode
4609 to the address of some declaration somewhere. If the target says
4610 the mode is valid for pointers, assume the target has a way of
4611 resolving it. */
4612 if (TREE_CODE (exp) == NOP_EXPR
4613 && POINTER_TYPE_P (TREE_TYPE (exp))
4614 && targetm.addr_space.valid_pointer_mode
4615 (TYPE_MODE (TREE_TYPE (exp)),
4616 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
4618 tree saved_type = TREE_TYPE (exp);
4620 /* Peel off any intermediate conversions-to-pointer for valid
4621 pointer modes. */
4622 while (TREE_CODE (exp) == NOP_EXPR
4623 && POINTER_TYPE_P (TREE_TYPE (exp))
4624 && targetm.addr_space.valid_pointer_mode
4625 (TYPE_MODE (TREE_TYPE (exp)),
4626 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
4627 exp = TREE_OPERAND (exp, 0);
4629 /* If what we're left with is the address of something, we can
4630 convert the address to the final type and output it that
4631 way. */
4632 if (TREE_CODE (exp) == ADDR_EXPR)
4633 exp = build1 (ADDR_EXPR, saved_type, TREE_OPERAND (exp, 0));
4634 /* Likewise for constant ints. */
4635 else if (TREE_CODE (exp) == INTEGER_CST)
4636 exp = wide_int_to_tree (saved_type, exp);
4640 /* Eliminate any conversions since we'll be outputting the underlying
4641 constant. */
4642 while (CONVERT_EXPR_P (exp)
4643 || TREE_CODE (exp) == NON_LVALUE_EXPR
4644 || TREE_CODE (exp) == VIEW_CONVERT_EXPR)
4646 HOST_WIDE_INT type_size = int_size_in_bytes (TREE_TYPE (exp));
4647 HOST_WIDE_INT op_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (exp, 0)));
4649 /* Make sure eliminating the conversion is really a no-op, except with
4650 VIEW_CONVERT_EXPRs to allow for wild Ada unchecked conversions and
4651 union types to allow for Ada unchecked unions. */
4652 if (type_size > op_size
4653 && TREE_CODE (exp) != VIEW_CONVERT_EXPR
4654 && TREE_CODE (TREE_TYPE (exp)) != UNION_TYPE)
4655 /* Keep the conversion. */
4656 break;
4657 else
4658 exp = TREE_OPERAND (exp, 0);
4661 code = TREE_CODE (TREE_TYPE (exp));
4662 thissize = int_size_in_bytes (TREE_TYPE (exp));
4664 /* Allow a constructor with no elements for any data type.
4665 This means to fill the space with zeros. */
4666 if (TREE_CODE (exp) == CONSTRUCTOR
4667 && vec_safe_is_empty (CONSTRUCTOR_ELTS (exp)))
4669 assemble_zeros (size);
4670 return size;
4673 if (TREE_CODE (exp) == FDESC_EXPR)
4675 #ifdef ASM_OUTPUT_FDESC
4676 HOST_WIDE_INT part = tree_to_shwi (TREE_OPERAND (exp, 1));
4677 tree decl = TREE_OPERAND (exp, 0);
4678 ASM_OUTPUT_FDESC (asm_out_file, decl, part);
4679 #else
4680 gcc_unreachable ();
4681 #endif
4682 return size;
4685 /* Now output the underlying data. If we've handling the padding, return.
4686 Otherwise, break and ensure SIZE is the size written. */
4687 switch (code)
4689 case BOOLEAN_TYPE:
4690 case INTEGER_TYPE:
4691 case ENUMERAL_TYPE:
4692 case POINTER_TYPE:
4693 case REFERENCE_TYPE:
4694 case OFFSET_TYPE:
4695 case FIXED_POINT_TYPE:
4696 case POINTER_BOUNDS_TYPE:
4697 case NULLPTR_TYPE:
4698 if (reverse)
4699 exp = fold_flip_storage_order (exp);
4700 if (! assemble_integer (expand_expr (exp, NULL_RTX, VOIDmode,
4701 EXPAND_INITIALIZER),
4702 MIN (size, thissize), align, 0))
4703 error ("initializer for integer/fixed-point value is too complicated");
4704 break;
4706 case REAL_TYPE:
4707 if (TREE_CODE (exp) != REAL_CST)
4708 error ("initializer for floating value is not a floating constant");
4709 else
4711 if (reverse)
4712 exp = fold_flip_storage_order (exp);
4713 assemble_real (TREE_REAL_CST (exp), TYPE_MODE (TREE_TYPE (exp)),
4714 align);
4716 break;
4718 case COMPLEX_TYPE:
4719 output_constant (TREE_REALPART (exp), thissize / 2, align, reverse);
4720 output_constant (TREE_IMAGPART (exp), thissize / 2,
4721 min_align (align, BITS_PER_UNIT * (thissize / 2)),
4722 reverse);
4723 break;
4725 case ARRAY_TYPE:
4726 case VECTOR_TYPE:
4727 switch (TREE_CODE (exp))
4729 case CONSTRUCTOR:
4730 return output_constructor (exp, size, align, reverse, NULL);
4731 case STRING_CST:
4732 thissize
4733 = MIN ((unsigned HOST_WIDE_INT)TREE_STRING_LENGTH (exp), size);
4734 assemble_string (TREE_STRING_POINTER (exp), thissize);
4735 break;
4736 case VECTOR_CST:
4738 machine_mode inner = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
4739 unsigned int nalign = MIN (align, GET_MODE_ALIGNMENT (inner));
4740 int elt_size = GET_MODE_SIZE (inner);
4741 output_constant (VECTOR_CST_ELT (exp, 0), elt_size, align,
4742 reverse);
4743 thissize = elt_size;
4744 for (unsigned int i = 1; i < VECTOR_CST_NELTS (exp); i++)
4746 output_constant (VECTOR_CST_ELT (exp, i), elt_size, nalign,
4747 reverse);
4748 thissize += elt_size;
4750 break;
4752 default:
4753 gcc_unreachable ();
4755 break;
4757 case RECORD_TYPE:
4758 case UNION_TYPE:
4759 gcc_assert (TREE_CODE (exp) == CONSTRUCTOR);
4760 return output_constructor (exp, size, align, reverse, NULL);
4762 case ERROR_MARK:
4763 return 0;
4765 default:
4766 gcc_unreachable ();
4769 if (size > thissize)
4770 assemble_zeros (size - thissize);
4772 return size;
4775 /* Subroutine of output_constructor, used for computing the size of
4776 arrays of unspecified length. VAL must be a CONSTRUCTOR of an array
4777 type with an unspecified upper bound. */
4779 static unsigned HOST_WIDE_INT
4780 array_size_for_constructor (tree val)
4782 tree max_index;
4783 unsigned HOST_WIDE_INT cnt;
4784 tree index, value, tmp;
4785 offset_int i;
4787 /* This code used to attempt to handle string constants that are not
4788 arrays of single-bytes, but nothing else does, so there's no point in
4789 doing it here. */
4790 if (TREE_CODE (val) == STRING_CST)
4791 return TREE_STRING_LENGTH (val);
4793 max_index = NULL_TREE;
4794 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (val), cnt, index, value)
4796 if (TREE_CODE (index) == RANGE_EXPR)
4797 index = TREE_OPERAND (index, 1);
4798 if (max_index == NULL_TREE || tree_int_cst_lt (max_index, index))
4799 max_index = index;
4802 if (max_index == NULL_TREE)
4803 return 0;
4805 /* Compute the total number of array elements. */
4806 tmp = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (val)));
4807 i = wi::to_offset (max_index) - wi::to_offset (tmp) + 1;
4809 /* Multiply by the array element unit size to find number of bytes. */
4810 i *= wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (val))));
4812 gcc_assert (wi::fits_uhwi_p (i));
4813 return i.to_uhwi ();
4816 /* Other datastructures + helpers for output_constructor. */
4818 /* output_constructor local state to support interaction with helpers. */
4820 typedef struct {
4822 /* Received arguments. */
4823 tree exp; /* Constructor expression. */
4824 tree type; /* Type of constructor expression. */
4825 unsigned HOST_WIDE_INT size; /* # bytes to output - pad if necessary. */
4826 unsigned int align; /* Known initial alignment. */
4827 tree min_index; /* Lower bound if specified for an array. */
4829 /* Output processing state. */
4830 HOST_WIDE_INT total_bytes; /* # bytes output so far / current position. */
4831 int byte; /* Part of a bitfield byte yet to be output. */
4832 int last_relative_index; /* Implicit or explicit index of the last
4833 array element output within a bitfield. */
4834 bool byte_buffer_in_use; /* Whether BYTE is in use. */
4835 bool reverse; /* Whether reverse storage order is in use. */
4837 /* Current element. */
4838 tree field; /* Current field decl in a record. */
4839 tree val; /* Current element value. */
4840 tree index; /* Current element index. */
4842 } oc_local_state;
4844 /* Helper for output_constructor. From the current LOCAL state, output a
4845 RANGE_EXPR element. */
4847 static void
4848 output_constructor_array_range (oc_local_state *local)
4850 unsigned HOST_WIDE_INT fieldsize
4851 = int_size_in_bytes (TREE_TYPE (local->type));
4853 HOST_WIDE_INT lo_index
4854 = tree_to_shwi (TREE_OPERAND (local->index, 0));
4855 HOST_WIDE_INT hi_index
4856 = tree_to_shwi (TREE_OPERAND (local->index, 1));
4857 HOST_WIDE_INT index;
4859 unsigned int align2
4860 = min_align (local->align, fieldsize * BITS_PER_UNIT);
4862 for (index = lo_index; index <= hi_index; index++)
4864 /* Output the element's initial value. */
4865 if (local->val == NULL_TREE)
4866 assemble_zeros (fieldsize);
4867 else
4868 fieldsize
4869 = output_constant (local->val, fieldsize, align2, local->reverse);
4871 /* Count its size. */
4872 local->total_bytes += fieldsize;
4876 /* Helper for output_constructor. From the current LOCAL state, output a
4877 field element that is not true bitfield or part of an outer one. */
4879 static void
4880 output_constructor_regular_field (oc_local_state *local)
4882 /* Field size and position. Since this structure is static, we know the
4883 positions are constant. */
4884 unsigned HOST_WIDE_INT fieldsize;
4885 HOST_WIDE_INT fieldpos;
4887 unsigned int align2;
4889 if (local->index != NULL_TREE)
4891 /* Perform the index calculation in modulo arithmetic but
4892 sign-extend the result because Ada has negative DECL_FIELD_OFFSETs
4893 but we are using an unsigned sizetype. */
4894 unsigned prec = TYPE_PRECISION (sizetype);
4895 offset_int idx = wi::sext (wi::to_offset (local->index)
4896 - wi::to_offset (local->min_index), prec);
4897 fieldpos = (idx * wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (local->val))))
4898 .to_short_addr ();
4900 else if (local->field != NULL_TREE)
4901 fieldpos = int_byte_position (local->field);
4902 else
4903 fieldpos = 0;
4905 /* Output any buffered-up bit-fields preceding this element. */
4906 if (local->byte_buffer_in_use)
4908 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
4909 local->total_bytes++;
4910 local->byte_buffer_in_use = false;
4913 /* Advance to offset of this element.
4914 Note no alignment needed in an array, since that is guaranteed
4915 if each element has the proper size. */
4916 if ((local->field != NULL_TREE || local->index != NULL_TREE)
4917 && fieldpos > local->total_bytes)
4919 assemble_zeros (fieldpos - local->total_bytes);
4920 local->total_bytes = fieldpos;
4923 /* Find the alignment of this element. */
4924 align2 = min_align (local->align, BITS_PER_UNIT * fieldpos);
4926 /* Determine size this element should occupy. */
4927 if (local->field)
4929 fieldsize = 0;
4931 /* If this is an array with an unspecified upper bound,
4932 the initializer determines the size. */
4933 /* ??? This ought to only checked if DECL_SIZE_UNIT is NULL,
4934 but we cannot do this until the deprecated support for
4935 initializing zero-length array members is removed. */
4936 if (TREE_CODE (TREE_TYPE (local->field)) == ARRAY_TYPE
4937 && TYPE_DOMAIN (TREE_TYPE (local->field))
4938 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (local->field))))
4940 fieldsize = array_size_for_constructor (local->val);
4941 /* Given a non-empty initialization, this field had
4942 better be last. */
4943 gcc_assert (!fieldsize || !DECL_CHAIN (local->field));
4945 else
4946 fieldsize = tree_to_uhwi (DECL_SIZE_UNIT (local->field));
4948 else
4949 fieldsize = int_size_in_bytes (TREE_TYPE (local->type));
4951 /* Output the element's initial value. */
4952 if (local->val == NULL_TREE)
4953 assemble_zeros (fieldsize);
4954 else
4955 fieldsize
4956 = output_constant (local->val, fieldsize, align2, local->reverse);
4958 /* Count its size. */
4959 local->total_bytes += fieldsize;
4962 /* Helper for output_constructor. From the LOCAL state, output an element
4963 that is a true bitfield or part of an outer one. BIT_OFFSET is the offset
4964 from the start of a possibly ongoing outer byte buffer. */
4966 static void
4967 output_constructor_bitfield (oc_local_state *local, unsigned int bit_offset)
4969 /* Bit size of this element. */
4970 HOST_WIDE_INT ebitsize
4971 = (local->field
4972 ? tree_to_uhwi (DECL_SIZE (local->field))
4973 : tree_to_uhwi (TYPE_SIZE (TREE_TYPE (local->type))));
4975 /* Relative index of this element if this is an array component. */
4976 HOST_WIDE_INT relative_index
4977 = (!local->field
4978 ? (local->index
4979 ? (tree_to_shwi (local->index)
4980 - tree_to_shwi (local->min_index))
4981 : local->last_relative_index + 1)
4982 : 0);
4984 /* Bit position of this element from the start of the containing
4985 constructor. */
4986 HOST_WIDE_INT constructor_relative_ebitpos
4987 = (local->field
4988 ? int_bit_position (local->field)
4989 : ebitsize * relative_index);
4991 /* Bit position of this element from the start of a possibly ongoing
4992 outer byte buffer. */
4993 HOST_WIDE_INT byte_relative_ebitpos
4994 = bit_offset + constructor_relative_ebitpos;
4996 /* From the start of a possibly ongoing outer byte buffer, offsets to
4997 the first bit of this element and to the first bit past the end of
4998 this element. */
4999 HOST_WIDE_INT next_offset = byte_relative_ebitpos;
5000 HOST_WIDE_INT end_offset = byte_relative_ebitpos + ebitsize;
5002 local->last_relative_index = relative_index;
5004 if (local->val == NULL_TREE)
5005 local->val = integer_zero_node;
5007 while (TREE_CODE (local->val) == VIEW_CONVERT_EXPR
5008 || TREE_CODE (local->val) == NON_LVALUE_EXPR)
5009 local->val = TREE_OPERAND (local->val, 0);
5011 if (TREE_CODE (local->val) != INTEGER_CST
5012 && TREE_CODE (local->val) != CONSTRUCTOR)
5014 error ("invalid initial value for member %qE", DECL_NAME (local->field));
5015 return;
5018 /* If this field does not start in this (or next) byte, skip some bytes. */
5019 if (next_offset / BITS_PER_UNIT != local->total_bytes)
5021 /* Output remnant of any bit field in previous bytes. */
5022 if (local->byte_buffer_in_use)
5024 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5025 local->total_bytes++;
5026 local->byte_buffer_in_use = false;
5029 /* If still not at proper byte, advance to there. */
5030 if (next_offset / BITS_PER_UNIT != local->total_bytes)
5032 gcc_assert (next_offset / BITS_PER_UNIT >= local->total_bytes);
5033 assemble_zeros (next_offset / BITS_PER_UNIT - local->total_bytes);
5034 local->total_bytes = next_offset / BITS_PER_UNIT;
5038 /* Set up the buffer if necessary. */
5039 if (!local->byte_buffer_in_use)
5041 local->byte = 0;
5042 if (ebitsize > 0)
5043 local->byte_buffer_in_use = true;
5046 /* If this is nested constructor, recurse passing the bit offset and the
5047 pending data, then retrieve the new pending data afterwards. */
5048 if (TREE_CODE (local->val) == CONSTRUCTOR)
5050 oc_outer_state temp_state;
5051 temp_state.bit_offset = next_offset % BITS_PER_UNIT;
5052 temp_state.byte = local->byte;
5053 local->total_bytes
5054 += output_constructor (local->val, 0, 0, local->reverse, &temp_state);
5055 local->byte = temp_state.byte;
5056 return;
5059 /* Otherwise, we must split the element into pieces that fall within
5060 separate bytes, and combine each byte with previous or following
5061 bit-fields. */
5062 while (next_offset < end_offset)
5064 int this_time;
5065 int shift;
5066 HOST_WIDE_INT value;
5067 HOST_WIDE_INT next_byte = next_offset / BITS_PER_UNIT;
5068 HOST_WIDE_INT next_bit = next_offset % BITS_PER_UNIT;
5070 /* Advance from byte to byte within this element when necessary. */
5071 while (next_byte != local->total_bytes)
5073 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5074 local->total_bytes++;
5075 local->byte = 0;
5078 /* Number of bits we can process at once (all part of the same byte). */
5079 this_time = MIN (end_offset - next_offset, BITS_PER_UNIT - next_bit);
5080 if (local->reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5082 /* On big-endian machine, take the most significant bits (of the
5083 bits that are significant) first and put them into bytes from
5084 the most significant end. */
5085 shift = end_offset - next_offset - this_time;
5087 /* Don't try to take a bunch of bits that cross
5088 the word boundary in the INTEGER_CST. We can
5089 only select bits from one element. */
5090 if ((shift / HOST_BITS_PER_WIDE_INT)
5091 != ((shift + this_time - 1) / HOST_BITS_PER_WIDE_INT))
5093 const int end = shift + this_time - 1;
5094 shift = end & -HOST_BITS_PER_WIDE_INT;
5095 this_time = end - shift + 1;
5098 /* Now get the bits from the appropriate constant word. */
5099 value = TREE_INT_CST_ELT (local->val, shift / HOST_BITS_PER_WIDE_INT);
5100 shift = shift & (HOST_BITS_PER_WIDE_INT - 1);
5102 /* Get the result. This works only when:
5103 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
5104 local->byte |= (((value >> shift)
5105 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
5106 << (BITS_PER_UNIT - this_time - next_bit));
5108 else
5110 /* On little-endian machines, take the least significant bits of
5111 the value first and pack them starting at the least significant
5112 bits of the bytes. */
5113 shift = next_offset - byte_relative_ebitpos;
5115 /* Don't try to take a bunch of bits that cross
5116 the word boundary in the INTEGER_CST. We can
5117 only select bits from one element. */
5118 if ((shift / HOST_BITS_PER_WIDE_INT)
5119 != ((shift + this_time - 1) / HOST_BITS_PER_WIDE_INT))
5120 this_time
5121 = HOST_BITS_PER_WIDE_INT - (shift & (HOST_BITS_PER_WIDE_INT - 1));
5123 /* Now get the bits from the appropriate constant word. */
5124 value = TREE_INT_CST_ELT (local->val, shift / HOST_BITS_PER_WIDE_INT);
5125 shift = shift & (HOST_BITS_PER_WIDE_INT - 1);
5127 /* Get the result. This works only when:
5128 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
5129 local->byte |= (((value >> shift)
5130 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
5131 << next_bit);
5134 next_offset += this_time;
5135 local->byte_buffer_in_use = true;
5139 /* Subroutine of output_constant, used for CONSTRUCTORs (aggregate constants).
5140 Generate at least SIZE bytes, padding if necessary. OUTER designates the
5141 caller output state of relevance in recursive invocations. */
5143 static unsigned HOST_WIDE_INT
5144 output_constructor (tree exp, unsigned HOST_WIDE_INT size, unsigned int align,
5145 bool reverse, oc_outer_state *outer)
5147 unsigned HOST_WIDE_INT cnt;
5148 constructor_elt *ce;
5149 oc_local_state local;
5151 /* Setup our local state to communicate with helpers. */
5152 local.exp = exp;
5153 local.type = TREE_TYPE (exp);
5154 local.size = size;
5155 local.align = align;
5156 if (TREE_CODE (local.type) == ARRAY_TYPE && TYPE_DOMAIN (local.type))
5157 local.min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (local.type));
5158 else
5159 local.min_index = NULL_TREE;
5161 local.total_bytes = 0;
5162 local.byte_buffer_in_use = outer != NULL;
5163 local.byte = outer ? outer->byte : 0;
5164 local.last_relative_index = -1;
5165 /* The storage order is specified for every aggregate type. */
5166 if (AGGREGATE_TYPE_P (local.type))
5167 local.reverse = TYPE_REVERSE_STORAGE_ORDER (local.type);
5168 else
5169 local.reverse = reverse;
5171 gcc_assert (HOST_BITS_PER_WIDE_INT >= BITS_PER_UNIT);
5173 /* As CE goes through the elements of the constant, FIELD goes through the
5174 structure fields if the constant is a structure. If the constant is a
5175 union, we override this by getting the field from the TREE_LIST element.
5176 But the constant could also be an array. Then FIELD is zero.
5178 There is always a maximum of one element in the chain LINK for unions
5179 (even if the initializer in a source program incorrectly contains
5180 more one). */
5182 if (TREE_CODE (local.type) == RECORD_TYPE)
5183 local.field = TYPE_FIELDS (local.type);
5184 else
5185 local.field = NULL_TREE;
5187 for (cnt = 0;
5188 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), cnt, &ce);
5189 cnt++, local.field = local.field ? DECL_CHAIN (local.field) : 0)
5191 local.val = ce->value;
5192 local.index = NULL_TREE;
5194 /* The element in a union constructor specifies the proper field
5195 or index. */
5196 if (RECORD_OR_UNION_TYPE_P (local.type) && ce->index != NULL_TREE)
5197 local.field = ce->index;
5199 else if (TREE_CODE (local.type) == ARRAY_TYPE)
5200 local.index = ce->index;
5202 if (local.field && flag_verbose_asm)
5203 fprintf (asm_out_file, "%s %s:\n",
5204 ASM_COMMENT_START,
5205 DECL_NAME (local.field)
5206 ? IDENTIFIER_POINTER (DECL_NAME (local.field))
5207 : "<anonymous>");
5209 /* Eliminate the marker that makes a cast not be an lvalue. */
5210 if (local.val != NULL_TREE)
5211 STRIP_NOPS (local.val);
5213 /* Output the current element, using the appropriate helper ... */
5215 /* For an array slice not part of an outer bitfield. */
5216 if (!outer
5217 && local.index != NULL_TREE
5218 && TREE_CODE (local.index) == RANGE_EXPR)
5219 output_constructor_array_range (&local);
5221 /* For a field that is neither a true bitfield nor part of an outer one,
5222 known to be at least byte aligned and multiple-of-bytes long. */
5223 else if (!outer
5224 && (local.field == NULL_TREE
5225 || !CONSTRUCTOR_BITFIELD_P (local.field)))
5226 output_constructor_regular_field (&local);
5228 /* For a true bitfield or part of an outer one. Only INTEGER_CSTs are
5229 supported for scalar fields, so we may need to convert first. */
5230 else
5232 if (TREE_CODE (local.val) == REAL_CST)
5233 local.val
5234 = fold_unary (VIEW_CONVERT_EXPR,
5235 build_nonstandard_integer_type
5236 (TYPE_PRECISION (TREE_TYPE (local.val)), 0),
5237 local.val);
5238 output_constructor_bitfield (&local, outer ? outer->bit_offset : 0);
5242 /* If we are not at toplevel, save the pending data for our caller.
5243 Otherwise output the pending data and padding zeros as needed. */
5244 if (outer)
5245 outer->byte = local.byte;
5246 else
5248 if (local.byte_buffer_in_use)
5250 assemble_integer (GEN_INT (local.byte), 1, BITS_PER_UNIT, 1);
5251 local.total_bytes++;
5254 if ((unsigned HOST_WIDE_INT)local.total_bytes < local.size)
5256 assemble_zeros (local.size - local.total_bytes);
5257 local.total_bytes = local.size;
5261 return local.total_bytes;
5264 /* Mark DECL as weak. */
5266 static void
5267 mark_weak (tree decl)
5269 if (DECL_WEAK (decl))
5270 return;
5272 struct symtab_node *n = symtab_node::get (decl);
5273 if (n && n->refuse_visibility_changes)
5274 error ("%+D declared weak after being used", decl);
5275 DECL_WEAK (decl) = 1;
5277 if (DECL_RTL_SET_P (decl)
5278 && MEM_P (DECL_RTL (decl))
5279 && XEXP (DECL_RTL (decl), 0)
5280 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == SYMBOL_REF)
5281 SYMBOL_REF_WEAK (XEXP (DECL_RTL (decl), 0)) = 1;
5284 /* Merge weak status between NEWDECL and OLDDECL. */
5286 void
5287 merge_weak (tree newdecl, tree olddecl)
5289 if (DECL_WEAK (newdecl) == DECL_WEAK (olddecl))
5291 if (DECL_WEAK (newdecl) && TARGET_SUPPORTS_WEAK)
5293 tree *pwd;
5294 /* We put the NEWDECL on the weak_decls list at some point
5295 and OLDDECL as well. Keep just OLDDECL on the list. */
5296 for (pwd = &weak_decls; *pwd; pwd = &TREE_CHAIN (*pwd))
5297 if (TREE_VALUE (*pwd) == newdecl)
5299 *pwd = TREE_CHAIN (*pwd);
5300 break;
5303 return;
5306 if (DECL_WEAK (newdecl))
5308 tree wd;
5310 /* NEWDECL is weak, but OLDDECL is not. */
5312 /* If we already output the OLDDECL, we're in trouble; we can't
5313 go back and make it weak. This should never happen in
5314 unit-at-a-time compilation. */
5315 gcc_assert (!TREE_ASM_WRITTEN (olddecl));
5317 /* If we've already generated rtl referencing OLDDECL, we may
5318 have done so in a way that will not function properly with
5319 a weak symbol. Again in unit-at-a-time this should be
5320 impossible. */
5321 gcc_assert (!TREE_USED (olddecl)
5322 || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (olddecl)));
5324 if (TARGET_SUPPORTS_WEAK)
5326 /* We put the NEWDECL on the weak_decls list at some point.
5327 Replace it with the OLDDECL. */
5328 for (wd = weak_decls; wd; wd = TREE_CHAIN (wd))
5329 if (TREE_VALUE (wd) == newdecl)
5331 TREE_VALUE (wd) = olddecl;
5332 break;
5334 /* We may not find the entry on the list. If NEWDECL is a
5335 weak alias, then we will have already called
5336 globalize_decl to remove the entry; in that case, we do
5337 not need to do anything. */
5340 /* Make the OLDDECL weak; it's OLDDECL that we'll be keeping. */
5341 mark_weak (olddecl);
5343 else
5344 /* OLDDECL was weak, but NEWDECL was not explicitly marked as
5345 weak. Just update NEWDECL to indicate that it's weak too. */
5346 mark_weak (newdecl);
5349 /* Declare DECL to be a weak symbol. */
5351 void
5352 declare_weak (tree decl)
5354 gcc_assert (TREE_CODE (decl) != FUNCTION_DECL || !TREE_ASM_WRITTEN (decl));
5355 if (! TREE_PUBLIC (decl))
5356 error ("weak declaration of %q+D must be public", decl);
5357 else if (!TARGET_SUPPORTS_WEAK)
5358 warning (0, "weak declaration of %q+D not supported", decl);
5360 mark_weak (decl);
5361 if (!lookup_attribute ("weak", DECL_ATTRIBUTES (decl)))
5362 DECL_ATTRIBUTES (decl)
5363 = tree_cons (get_identifier ("weak"), NULL, DECL_ATTRIBUTES (decl));
5366 static void
5367 weak_finish_1 (tree decl)
5369 #if defined (ASM_WEAKEN_DECL) || defined (ASM_WEAKEN_LABEL)
5370 const char *const name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5371 #endif
5373 if (! TREE_USED (decl))
5374 return;
5376 #ifdef ASM_WEAKEN_DECL
5377 ASM_WEAKEN_DECL (asm_out_file, decl, name, NULL);
5378 #else
5379 #ifdef ASM_WEAKEN_LABEL
5380 ASM_WEAKEN_LABEL (asm_out_file, name);
5381 #else
5382 #ifdef ASM_OUTPUT_WEAK_ALIAS
5384 static bool warn_once = 0;
5385 if (! warn_once)
5387 warning (0, "only weak aliases are supported in this configuration");
5388 warn_once = 1;
5390 return;
5392 #endif
5393 #endif
5394 #endif
5397 /* Fiven an assembly name, find the decl it is associated with. */
5398 static tree
5399 find_decl (tree target)
5401 symtab_node *node = symtab_node::get_for_asmname (target);
5402 if (node)
5403 return node->decl;
5404 return NULL_TREE;
5407 /* This TREE_LIST contains weakref targets. */
5409 static GTY(()) tree weakref_targets;
5411 /* Emit any pending weak declarations. */
5413 void
5414 weak_finish (void)
5416 tree t;
5418 for (t = weakref_targets; t; t = TREE_CHAIN (t))
5420 tree alias_decl = TREE_PURPOSE (t);
5421 tree target = ultimate_transparent_alias_target (&TREE_VALUE (t));
5423 if (! TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (alias_decl)))
5424 /* Remove alias_decl from the weak list, but leave entries for
5425 the target alone. */
5426 target = NULL_TREE;
5427 #ifndef ASM_OUTPUT_WEAKREF
5428 else if (! TREE_SYMBOL_REFERENCED (target))
5430 /* Use ASM_WEAKEN_LABEL only if ASM_WEAKEN_DECL is not
5431 defined, otherwise we and weak_finish_1 would use
5432 different macros. */
5433 # if defined ASM_WEAKEN_LABEL && ! defined ASM_WEAKEN_DECL
5434 ASM_WEAKEN_LABEL (asm_out_file, IDENTIFIER_POINTER (target));
5435 # else
5436 tree decl = find_decl (target);
5438 if (! decl)
5440 decl = build_decl (DECL_SOURCE_LOCATION (alias_decl),
5441 TREE_CODE (alias_decl), target,
5442 TREE_TYPE (alias_decl));
5444 DECL_EXTERNAL (decl) = 1;
5445 TREE_PUBLIC (decl) = 1;
5446 DECL_ARTIFICIAL (decl) = 1;
5447 TREE_NOTHROW (decl) = TREE_NOTHROW (alias_decl);
5448 TREE_USED (decl) = 1;
5451 weak_finish_1 (decl);
5452 # endif
5454 #endif
5457 tree *p;
5458 tree t2;
5460 /* Remove the alias and the target from the pending weak list
5461 so that we do not emit any .weak directives for the former,
5462 nor multiple .weak directives for the latter. */
5463 for (p = &weak_decls; (t2 = *p) ; )
5465 if (TREE_VALUE (t2) == alias_decl
5466 || target == DECL_ASSEMBLER_NAME (TREE_VALUE (t2)))
5467 *p = TREE_CHAIN (t2);
5468 else
5469 p = &TREE_CHAIN (t2);
5472 /* Remove other weakrefs to the same target, to speed things up. */
5473 for (p = &TREE_CHAIN (t); (t2 = *p) ; )
5475 if (target == ultimate_transparent_alias_target (&TREE_VALUE (t2)))
5476 *p = TREE_CHAIN (t2);
5477 else
5478 p = &TREE_CHAIN (t2);
5483 for (t = weak_decls; t; t = TREE_CHAIN (t))
5485 tree decl = TREE_VALUE (t);
5487 weak_finish_1 (decl);
5491 /* Emit the assembly bits to indicate that DECL is globally visible. */
5493 static void
5494 globalize_decl (tree decl)
5497 #if defined (ASM_WEAKEN_LABEL) || defined (ASM_WEAKEN_DECL)
5498 if (DECL_WEAK (decl))
5500 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
5501 tree *p, t;
5503 #ifdef ASM_WEAKEN_DECL
5504 ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
5505 #else
5506 ASM_WEAKEN_LABEL (asm_out_file, name);
5507 #endif
5509 /* Remove this function from the pending weak list so that
5510 we do not emit multiple .weak directives for it. */
5511 for (p = &weak_decls; (t = *p) ; )
5513 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
5514 *p = TREE_CHAIN (t);
5515 else
5516 p = &TREE_CHAIN (t);
5519 /* Remove weakrefs to the same target from the pending weakref
5520 list, for the same reason. */
5521 for (p = &weakref_targets; (t = *p) ; )
5523 if (DECL_ASSEMBLER_NAME (decl)
5524 == ultimate_transparent_alias_target (&TREE_VALUE (t)))
5525 *p = TREE_CHAIN (t);
5526 else
5527 p = &TREE_CHAIN (t);
5530 return;
5532 #endif
5534 targetm.asm_out.globalize_decl_name (asm_out_file, decl);
5537 vec<alias_pair, va_gc> *alias_pairs;
5539 /* Output the assembler code for a define (equate) using ASM_OUTPUT_DEF
5540 or ASM_OUTPUT_DEF_FROM_DECLS. The function defines the symbol whose
5541 tree node is DECL to have the value of the tree node TARGET. */
5543 void
5544 do_assemble_alias (tree decl, tree target)
5546 tree id;
5548 /* Emulated TLS had better not get this var. */
5549 gcc_assert (!(!targetm.have_tls
5550 && TREE_CODE (decl) == VAR_DECL
5551 && DECL_THREAD_LOCAL_P (decl)));
5553 if (TREE_ASM_WRITTEN (decl))
5554 return;
5556 id = DECL_ASSEMBLER_NAME (decl);
5557 ultimate_transparent_alias_target (&id);
5559 /* We must force creation of DECL_RTL for debug info generation, even though
5560 we don't use it here. */
5561 make_decl_rtl (decl);
5563 TREE_ASM_WRITTEN (decl) = 1;
5564 TREE_ASM_WRITTEN (DECL_ASSEMBLER_NAME (decl)) = 1;
5565 TREE_ASM_WRITTEN (id) = 1;
5567 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
5569 ultimate_transparent_alias_target (&target);
5571 if (!TREE_SYMBOL_REFERENCED (target))
5572 weakref_targets = tree_cons (decl, target, weakref_targets);
5574 #ifdef ASM_OUTPUT_WEAKREF
5575 ASM_OUTPUT_WEAKREF (asm_out_file, decl,
5576 IDENTIFIER_POINTER (id),
5577 IDENTIFIER_POINTER (target));
5578 #else
5579 if (!TARGET_SUPPORTS_WEAK)
5581 error_at (DECL_SOURCE_LOCATION (decl),
5582 "weakref is not supported in this configuration");
5583 return;
5585 #endif
5586 return;
5589 #ifdef ASM_OUTPUT_DEF
5590 tree orig_decl = decl;
5592 if (TREE_CODE (decl) == FUNCTION_DECL
5593 && cgraph_node::get (decl)->instrumentation_clone
5594 && cgraph_node::get (decl)->instrumented_version)
5595 orig_decl = cgraph_node::get (decl)->instrumented_version->decl;
5597 /* Make name accessible from other files, if appropriate. */
5599 if (TREE_PUBLIC (decl) || TREE_PUBLIC (orig_decl))
5601 globalize_decl (decl);
5602 maybe_assemble_visibility (decl);
5604 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
5606 #if defined (ASM_OUTPUT_TYPE_DIRECTIVE)
5607 if (targetm.has_ifunc_p ())
5608 ASM_OUTPUT_TYPE_DIRECTIVE
5609 (asm_out_file, IDENTIFIER_POINTER (id),
5610 IFUNC_ASM_TYPE);
5611 else
5612 #endif
5613 error_at (DECL_SOURCE_LOCATION (decl),
5614 "ifunc is not supported on this target");
5617 # ifdef ASM_OUTPUT_DEF_FROM_DECLS
5618 ASM_OUTPUT_DEF_FROM_DECLS (asm_out_file, decl, target);
5619 # else
5620 ASM_OUTPUT_DEF (asm_out_file,
5621 IDENTIFIER_POINTER (id),
5622 IDENTIFIER_POINTER (target));
5623 # endif
5624 #elif defined (ASM_OUTPUT_WEAK_ALIAS) || defined (ASM_WEAKEN_DECL)
5626 const char *name;
5627 tree *p, t;
5629 name = IDENTIFIER_POINTER (id);
5630 # ifdef ASM_WEAKEN_DECL
5631 ASM_WEAKEN_DECL (asm_out_file, decl, name, IDENTIFIER_POINTER (target));
5632 # else
5633 ASM_OUTPUT_WEAK_ALIAS (asm_out_file, name, IDENTIFIER_POINTER (target));
5634 # endif
5635 /* Remove this function from the pending weak list so that
5636 we do not emit multiple .weak directives for it. */
5637 for (p = &weak_decls; (t = *p) ; )
5638 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t))
5639 || id == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
5640 *p = TREE_CHAIN (t);
5641 else
5642 p = &TREE_CHAIN (t);
5644 /* Remove weakrefs to the same target from the pending weakref
5645 list, for the same reason. */
5646 for (p = &weakref_targets; (t = *p) ; )
5648 if (id == ultimate_transparent_alias_target (&TREE_VALUE (t)))
5649 *p = TREE_CHAIN (t);
5650 else
5651 p = &TREE_CHAIN (t);
5654 #endif
5657 /* Emit an assembler directive to make the symbol for DECL an alias to
5658 the symbol for TARGET. */
5660 void
5661 assemble_alias (tree decl, tree target)
5663 tree target_decl;
5665 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
5667 tree alias = DECL_ASSEMBLER_NAME (decl);
5669 ultimate_transparent_alias_target (&target);
5671 if (alias == target)
5672 error ("weakref %q+D ultimately targets itself", decl);
5673 if (TREE_PUBLIC (decl))
5674 error ("weakref %q+D must have static linkage", decl);
5676 else
5678 #if !defined (ASM_OUTPUT_DEF)
5679 # if !defined(ASM_OUTPUT_WEAK_ALIAS) && !defined (ASM_WEAKEN_DECL)
5680 error_at (DECL_SOURCE_LOCATION (decl),
5681 "alias definitions not supported in this configuration");
5682 TREE_ASM_WRITTEN (decl) = 1;
5683 return;
5684 # else
5685 if (!DECL_WEAK (decl))
5687 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
5688 error_at (DECL_SOURCE_LOCATION (decl),
5689 "ifunc is not supported in this configuration");
5690 else
5691 error_at (DECL_SOURCE_LOCATION (decl),
5692 "only weak aliases are supported in this configuration");
5693 TREE_ASM_WRITTEN (decl) = 1;
5694 return;
5696 # endif
5697 #endif
5699 TREE_USED (decl) = 1;
5701 /* Allow aliases to aliases. */
5702 if (TREE_CODE (decl) == FUNCTION_DECL)
5703 cgraph_node::get_create (decl)->alias = true;
5704 else
5705 varpool_node::get_create (decl)->alias = true;
5707 /* If the target has already been emitted, we don't have to queue the
5708 alias. This saves a tad of memory. */
5709 if (symtab->global_info_ready)
5710 target_decl = find_decl (target);
5711 else
5712 target_decl= NULL;
5713 if ((target_decl && TREE_ASM_WRITTEN (target_decl))
5714 || symtab->state >= EXPANSION)
5715 do_assemble_alias (decl, target);
5716 else
5718 alias_pair p = {decl, target};
5719 vec_safe_push (alias_pairs, p);
5723 /* Record and output a table of translations from original function
5724 to its transaction aware clone. Note that tm_pure functions are
5725 considered to be their own clone. */
5727 static GTY((if_marked ("tree_map_marked_p"), param_is (struct tree_map)))
5728 htab_t tm_clone_hash;
5730 void
5731 record_tm_clone_pair (tree o, tree n)
5733 struct tree_map **slot, *h;
5735 if (tm_clone_hash == NULL)
5736 tm_clone_hash = htab_create_ggc (32, tree_map_hash, tree_map_eq, 0);
5738 h = ggc_alloc<tree_map> ();
5739 h->hash = htab_hash_pointer (o);
5740 h->base.from = o;
5741 h->to = n;
5743 slot = (struct tree_map **)
5744 htab_find_slot_with_hash (tm_clone_hash, h, h->hash, INSERT);
5745 *slot = h;
5748 tree
5749 get_tm_clone_pair (tree o)
5751 if (tm_clone_hash)
5753 struct tree_map *h, in;
5755 in.base.from = o;
5756 in.hash = htab_hash_pointer (o);
5757 h = (struct tree_map *) htab_find_with_hash (tm_clone_hash,
5758 &in, in.hash);
5759 if (h)
5760 return h->to;
5762 return NULL_TREE;
5765 typedef struct tm_alias_pair
5767 unsigned int uid;
5768 tree from;
5769 tree to;
5770 } tm_alias_pair;
5773 /* Helper function for finish_tm_clone_pairs. Dump a hash table entry
5774 into a VEC in INFO. */
5776 static int
5777 dump_tm_clone_to_vec (void **slot, void *info)
5779 struct tree_map *map = (struct tree_map *) *slot;
5780 vec<tm_alias_pair> *tm_alias_pairs = (vec<tm_alias_pair> *) info;
5781 tm_alias_pair p = {DECL_UID (map->base.from), map->base.from, map->to};
5782 tm_alias_pairs->safe_push (p);
5783 return 1;
5786 /* Dump the actual pairs to the .tm_clone_table section. */
5788 static void
5789 dump_tm_clone_pairs (vec<tm_alias_pair> tm_alias_pairs)
5791 unsigned i;
5792 tm_alias_pair *p;
5793 bool switched = false;
5795 FOR_EACH_VEC_ELT (tm_alias_pairs, i, p)
5797 tree src = p->from;
5798 tree dst = p->to;
5799 struct cgraph_node *src_n = cgraph_node::get (src);
5800 struct cgraph_node *dst_n = cgraph_node::get (dst);
5802 /* The function ipa_tm_create_version() marks the clone as needed if
5803 the original function was needed. But we also mark the clone as
5804 needed if we ever called the clone indirectly through
5805 TM_GETTMCLONE. If neither of these are true, we didn't generate
5806 a clone, and we didn't call it indirectly... no sense keeping it
5807 in the clone table. */
5808 if (!dst_n || !dst_n->definition)
5809 continue;
5811 /* This covers the case where we have optimized the original
5812 function away, and only access the transactional clone. */
5813 if (!src_n || !src_n->definition)
5814 continue;
5816 if (!switched)
5818 switch_to_section (targetm.asm_out.tm_clone_table_section ());
5819 assemble_align (POINTER_SIZE);
5820 switched = true;
5823 assemble_integer (XEXP (DECL_RTL (src), 0),
5824 POINTER_SIZE_UNITS, POINTER_SIZE, 1);
5825 assemble_integer (XEXP (DECL_RTL (dst), 0),
5826 POINTER_SIZE_UNITS, POINTER_SIZE, 1);
5830 /* Provide a default for the tm_clone_table section. */
5832 section *
5833 default_clone_table_section (void)
5835 return get_named_section (NULL, ".tm_clone_table", 3);
5838 /* Helper comparison function for qsorting by the DECL_UID stored in
5839 alias_pair->emitted_diags. */
5841 static int
5842 tm_alias_pair_cmp (const void *x, const void *y)
5844 const tm_alias_pair *p1 = (const tm_alias_pair *) x;
5845 const tm_alias_pair *p2 = (const tm_alias_pair *) y;
5846 if (p1->uid < p2->uid)
5847 return -1;
5848 if (p1->uid > p2->uid)
5849 return 1;
5850 return 0;
5853 void
5854 finish_tm_clone_pairs (void)
5856 vec<tm_alias_pair> tm_alias_pairs = vNULL;
5858 if (tm_clone_hash == NULL)
5859 return;
5861 /* We need a determenistic order for the .tm_clone_table, otherwise
5862 we will get bootstrap comparison failures, so dump the hash table
5863 to a vector, sort it, and dump the vector. */
5865 /* Dump the hashtable to a vector. */
5866 htab_traverse_noresize (tm_clone_hash, dump_tm_clone_to_vec,
5867 (void *) &tm_alias_pairs);
5868 /* Sort it. */
5869 tm_alias_pairs.qsort (tm_alias_pair_cmp);
5871 /* Dump it. */
5872 dump_tm_clone_pairs (tm_alias_pairs);
5874 htab_delete (tm_clone_hash);
5875 tm_clone_hash = NULL;
5876 tm_alias_pairs.release ();
5880 /* Emit an assembler directive to set symbol for DECL visibility to
5881 the visibility type VIS, which must not be VISIBILITY_DEFAULT. */
5883 void
5884 default_assemble_visibility (tree decl ATTRIBUTE_UNUSED,
5885 int vis ATTRIBUTE_UNUSED)
5887 #ifdef HAVE_GAS_HIDDEN
5888 static const char * const visibility_types[] = {
5889 NULL, "protected", "hidden", "internal"
5892 const char *name, *type;
5894 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5895 type = visibility_types[vis];
5897 fprintf (asm_out_file, "\t.%s\t", type);
5898 assemble_name (asm_out_file, name);
5899 fprintf (asm_out_file, "\n");
5900 #else
5901 if (!DECL_ARTIFICIAL (decl))
5902 warning (OPT_Wattributes, "visibility attribute not supported "
5903 "in this configuration; ignored");
5904 #endif
5907 /* A helper function to call assemble_visibility when needed for a decl. */
5910 maybe_assemble_visibility (tree decl)
5912 enum symbol_visibility vis = DECL_VISIBILITY (decl);
5914 if (TREE_CODE (decl) == FUNCTION_DECL
5915 && cgraph_node::get (decl)
5916 && cgraph_node::get (decl)->instrumentation_clone
5917 && cgraph_node::get (decl)->instrumented_version)
5918 vis = DECL_VISIBILITY (cgraph_node::get (decl)->instrumented_version->decl);
5920 if (vis != VISIBILITY_DEFAULT)
5922 targetm.asm_out.assemble_visibility (decl, vis);
5923 return 1;
5925 else
5926 return 0;
5929 /* Returns 1 if the target configuration supports defining public symbols
5930 so that one of them will be chosen at link time instead of generating a
5931 multiply-defined symbol error, whether through the use of weak symbols or
5932 a target-specific mechanism for having duplicates discarded. */
5935 supports_one_only (void)
5937 if (SUPPORTS_ONE_ONLY)
5938 return 1;
5939 return TARGET_SUPPORTS_WEAK;
5942 /* Set up DECL as a public symbol that can be defined in multiple
5943 translation units without generating a linker error. */
5945 void
5946 make_decl_one_only (tree decl, tree comdat_group)
5948 struct symtab_node *symbol;
5949 gcc_assert (TREE_CODE (decl) == VAR_DECL
5950 || TREE_CODE (decl) == FUNCTION_DECL);
5952 TREE_PUBLIC (decl) = 1;
5954 if (TREE_CODE (decl) == VAR_DECL)
5955 symbol = varpool_node::get_create (decl);
5956 else
5957 symbol = cgraph_node::get_create (decl);
5959 if (SUPPORTS_ONE_ONLY)
5961 #ifdef MAKE_DECL_ONE_ONLY
5962 MAKE_DECL_ONE_ONLY (decl);
5963 #endif
5964 symbol->set_comdat_group (comdat_group);
5966 else if (TREE_CODE (decl) == VAR_DECL
5967 && (DECL_INITIAL (decl) == 0
5968 || (!in_lto_p && DECL_INITIAL (decl) == error_mark_node)))
5969 DECL_COMMON (decl) = 1;
5970 else
5972 gcc_assert (TARGET_SUPPORTS_WEAK);
5973 DECL_WEAK (decl) = 1;
5977 void
5978 init_varasm_once (void)
5980 section_htab = hash_table<section_hasher>::create_ggc (31);
5981 object_block_htab = hash_table<object_block_hasher>::create_ggc (31);
5982 const_desc_htab = hash_table<tree_descriptor_hasher>::create_ggc (1009);
5984 shared_constant_pool = create_constant_pool ();
5986 #ifdef TEXT_SECTION_ASM_OP
5987 text_section = get_unnamed_section (SECTION_CODE, output_section_asm_op,
5988 TEXT_SECTION_ASM_OP);
5989 #endif
5991 #ifdef DATA_SECTION_ASM_OP
5992 data_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
5993 DATA_SECTION_ASM_OP);
5994 #endif
5996 #ifdef SDATA_SECTION_ASM_OP
5997 sdata_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
5998 SDATA_SECTION_ASM_OP);
5999 #endif
6001 #ifdef READONLY_DATA_SECTION_ASM_OP
6002 readonly_data_section = get_unnamed_section (0, output_section_asm_op,
6003 READONLY_DATA_SECTION_ASM_OP);
6004 #endif
6006 #ifdef CTORS_SECTION_ASM_OP
6007 ctors_section = get_unnamed_section (0, output_section_asm_op,
6008 CTORS_SECTION_ASM_OP);
6009 #endif
6011 #ifdef DTORS_SECTION_ASM_OP
6012 dtors_section = get_unnamed_section (0, output_section_asm_op,
6013 DTORS_SECTION_ASM_OP);
6014 #endif
6016 #ifdef BSS_SECTION_ASM_OP
6017 bss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
6018 output_section_asm_op,
6019 BSS_SECTION_ASM_OP);
6020 #endif
6022 #ifdef SBSS_SECTION_ASM_OP
6023 sbss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
6024 output_section_asm_op,
6025 SBSS_SECTION_ASM_OP);
6026 #endif
6028 tls_comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6029 | SECTION_COMMON, emit_tls_common);
6030 lcomm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6031 | SECTION_COMMON, emit_local);
6032 comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6033 | SECTION_COMMON, emit_common);
6035 #if defined ASM_OUTPUT_ALIGNED_BSS
6036 bss_noswitch_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS,
6037 emit_bss);
6038 #endif
6040 targetm.asm_out.init_sections ();
6042 if (readonly_data_section == NULL)
6043 readonly_data_section = text_section;
6045 #ifdef ASM_OUTPUT_EXTERNAL
6046 pending_assemble_externals_set = new hash_set<tree>;
6047 #endif
6050 enum tls_model
6051 decl_default_tls_model (const_tree decl)
6053 enum tls_model kind;
6054 bool is_local;
6056 is_local = targetm.binds_local_p (decl);
6057 if (!flag_shlib)
6059 if (is_local)
6060 kind = TLS_MODEL_LOCAL_EXEC;
6061 else
6062 kind = TLS_MODEL_INITIAL_EXEC;
6065 /* Local dynamic is inefficient when we're not combining the
6066 parts of the address. */
6067 else if (optimize && is_local)
6068 kind = TLS_MODEL_LOCAL_DYNAMIC;
6069 else
6070 kind = TLS_MODEL_GLOBAL_DYNAMIC;
6071 if (kind < flag_tls_default)
6072 kind = flag_tls_default;
6074 return kind;
6077 /* Select a set of attributes for section NAME based on the properties
6078 of DECL and whether or not RELOC indicates that DECL's initializer
6079 might contain runtime relocations.
6081 We make the section read-only and executable for a function decl,
6082 read-only for a const data decl, and writable for a non-const data decl. */
6084 unsigned int
6085 default_section_type_flags (tree decl, const char *name, int reloc)
6087 unsigned int flags;
6089 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
6090 flags = SECTION_CODE;
6091 else if (decl)
6093 enum section_category category
6094 = categorize_decl_for_section (decl, reloc);
6095 if (decl_readonly_section_1 (category))
6096 flags = 0;
6097 else if (category == SECCAT_DATA_REL_RO
6098 || category == SECCAT_DATA_REL_RO_LOCAL)
6099 flags = SECTION_WRITE | SECTION_RELRO;
6100 else
6101 flags = SECTION_WRITE;
6103 else
6105 flags = SECTION_WRITE;
6106 if (strcmp (name, ".data.rel.ro") == 0
6107 || strcmp (name, ".data.rel.ro.local") == 0)
6108 flags |= SECTION_RELRO;
6111 if (decl && DECL_P (decl) && DECL_COMDAT_GROUP (decl))
6112 flags |= SECTION_LINKONCE;
6114 if (strcmp (name, ".vtable_map_vars") == 0)
6115 flags |= SECTION_LINKONCE;
6117 if (decl && TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
6118 flags |= SECTION_TLS | SECTION_WRITE;
6120 if (strcmp (name, ".bss") == 0
6121 || strncmp (name, ".bss.", 5) == 0
6122 || strncmp (name, ".gnu.linkonce.b.", 16) == 0
6123 || strcmp (name, ".persistent.bss") == 0
6124 || strcmp (name, ".sbss") == 0
6125 || strncmp (name, ".sbss.", 6) == 0
6126 || strncmp (name, ".gnu.linkonce.sb.", 17) == 0)
6127 flags |= SECTION_BSS;
6129 if (strcmp (name, ".tdata") == 0
6130 || strncmp (name, ".tdata.", 7) == 0
6131 || strncmp (name, ".gnu.linkonce.td.", 17) == 0)
6132 flags |= SECTION_TLS;
6134 if (strcmp (name, ".tbss") == 0
6135 || strncmp (name, ".tbss.", 6) == 0
6136 || strncmp (name, ".gnu.linkonce.tb.", 17) == 0)
6137 flags |= SECTION_TLS | SECTION_BSS;
6139 /* These three sections have special ELF types. They are neither
6140 SHT_PROGBITS nor SHT_NOBITS, so when changing sections we don't
6141 want to print a section type (@progbits or @nobits). If someone
6142 is silly enough to emit code or TLS variables to one of these
6143 sections, then don't handle them specially. */
6144 if (!(flags & (SECTION_CODE | SECTION_BSS | SECTION_TLS))
6145 && (strcmp (name, ".init_array") == 0
6146 || strcmp (name, ".fini_array") == 0
6147 || strcmp (name, ".preinit_array") == 0))
6148 flags |= SECTION_NOTYPE;
6150 return flags;
6153 /* Return true if the target supports some form of global BSS,
6154 either through bss_noswitch_section, or by selecting a BSS
6155 section in TARGET_ASM_SELECT_SECTION. */
6157 bool
6158 have_global_bss_p (void)
6160 return bss_noswitch_section || targetm.have_switchable_bss_sections;
6163 /* Output assembly to switch to section NAME with attribute FLAGS.
6164 Four variants for common object file formats. */
6166 void
6167 default_no_named_section (const char *name ATTRIBUTE_UNUSED,
6168 unsigned int flags ATTRIBUTE_UNUSED,
6169 tree decl ATTRIBUTE_UNUSED)
6171 /* Some object formats don't support named sections at all. The
6172 front-end should already have flagged this as an error. */
6173 gcc_unreachable ();
6176 #ifndef TLS_SECTION_ASM_FLAG
6177 #define TLS_SECTION_ASM_FLAG 'T'
6178 #endif
6180 void
6181 default_elf_asm_named_section (const char *name, unsigned int flags,
6182 tree decl ATTRIBUTE_UNUSED)
6184 char flagchars[10], *f = flagchars;
6186 /* If we have already declared this section, we can use an
6187 abbreviated form to switch back to it -- unless this section is
6188 part of a COMDAT groups, in which case GAS requires the full
6189 declaration every time. */
6190 if (!(HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6191 && (flags & SECTION_DECLARED))
6193 fprintf (asm_out_file, "\t.section\t%s\n", name);
6194 return;
6197 if (!(flags & SECTION_DEBUG))
6198 *f++ = 'a';
6199 #if defined (HAVE_GAS_SECTION_EXCLUDE) && HAVE_GAS_SECTION_EXCLUDE == 1
6200 if (flags & SECTION_EXCLUDE)
6201 *f++ = 'e';
6202 #endif
6203 if (flags & SECTION_WRITE)
6204 *f++ = 'w';
6205 if (flags & SECTION_CODE)
6206 *f++ = 'x';
6207 if (flags & SECTION_SMALL)
6208 *f++ = 's';
6209 if (flags & SECTION_MERGE)
6210 *f++ = 'M';
6211 if (flags & SECTION_STRINGS)
6212 *f++ = 'S';
6213 if (flags & SECTION_TLS)
6214 *f++ = TLS_SECTION_ASM_FLAG;
6215 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6216 *f++ = 'G';
6217 *f = '\0';
6219 fprintf (asm_out_file, "\t.section\t%s,\"%s\"", name, flagchars);
6221 if (!(flags & SECTION_NOTYPE))
6223 const char *type;
6224 const char *format;
6226 if (flags & SECTION_BSS)
6227 type = "nobits";
6228 else
6229 type = "progbits";
6231 format = ",@%s";
6232 /* On platforms that use "@" as the assembly comment character,
6233 use "%" instead. */
6234 if (strcmp (ASM_COMMENT_START, "@") == 0)
6235 format = ",%%%s";
6236 fprintf (asm_out_file, format, type);
6238 if (flags & SECTION_ENTSIZE)
6239 fprintf (asm_out_file, ",%d", flags & SECTION_ENTSIZE);
6240 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6242 if (TREE_CODE (decl) == IDENTIFIER_NODE)
6243 fprintf (asm_out_file, ",%s,comdat", IDENTIFIER_POINTER (decl));
6244 else
6245 fprintf (asm_out_file, ",%s,comdat",
6246 IDENTIFIER_POINTER (DECL_COMDAT_GROUP (decl)));
6250 putc ('\n', asm_out_file);
6253 void
6254 default_coff_asm_named_section (const char *name, unsigned int flags,
6255 tree decl ATTRIBUTE_UNUSED)
6257 char flagchars[8], *f = flagchars;
6259 if (flags & SECTION_WRITE)
6260 *f++ = 'w';
6261 if (flags & SECTION_CODE)
6262 *f++ = 'x';
6263 *f = '\0';
6265 fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
6268 void
6269 default_pe_asm_named_section (const char *name, unsigned int flags,
6270 tree decl)
6272 default_coff_asm_named_section (name, flags, decl);
6274 if (flags & SECTION_LINKONCE)
6276 /* Functions may have been compiled at various levels of
6277 optimization so we can't use `same_size' here.
6278 Instead, have the linker pick one. */
6279 fprintf (asm_out_file, "\t.linkonce %s\n",
6280 (flags & SECTION_CODE ? "discard" : "same_size"));
6284 /* The lame default section selector. */
6286 section *
6287 default_select_section (tree decl, int reloc,
6288 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
6290 if (DECL_P (decl))
6292 if (decl_readonly_section (decl, reloc))
6293 return readonly_data_section;
6295 else if (TREE_CODE (decl) == CONSTRUCTOR)
6297 if (! ((flag_pic && reloc)
6298 || !TREE_READONLY (decl)
6299 || TREE_SIDE_EFFECTS (decl)
6300 || !TREE_CONSTANT (decl)))
6301 return readonly_data_section;
6303 else if (TREE_CODE (decl) == STRING_CST)
6304 return readonly_data_section;
6305 else if (! (flag_pic && reloc))
6306 return readonly_data_section;
6308 return data_section;
6311 enum section_category
6312 categorize_decl_for_section (const_tree decl, int reloc)
6314 enum section_category ret;
6316 if (TREE_CODE (decl) == FUNCTION_DECL)
6317 return SECCAT_TEXT;
6318 else if (TREE_CODE (decl) == STRING_CST)
6320 if ((flag_sanitize & SANITIZE_ADDRESS)
6321 && asan_protect_global (CONST_CAST_TREE (decl)))
6322 /* or !flag_merge_constants */
6323 return SECCAT_RODATA;
6324 else
6325 return SECCAT_RODATA_MERGE_STR;
6327 else if (TREE_CODE (decl) == VAR_DECL)
6329 if (bss_initializer_p (decl))
6330 ret = SECCAT_BSS;
6331 else if (! TREE_READONLY (decl)
6332 || TREE_SIDE_EFFECTS (decl)
6333 || ! TREE_CONSTANT (DECL_INITIAL (decl)))
6335 /* Here the reloc_rw_mask is not testing whether the section should
6336 be read-only or not, but whether the dynamic link will have to
6337 do something. If so, we wish to segregate the data in order to
6338 minimize cache misses inside the dynamic linker. */
6339 if (reloc & targetm.asm_out.reloc_rw_mask ())
6340 ret = reloc == 1 ? SECCAT_DATA_REL_LOCAL : SECCAT_DATA_REL;
6341 else
6342 ret = SECCAT_DATA;
6344 else if (reloc & targetm.asm_out.reloc_rw_mask ())
6345 ret = reloc == 1 ? SECCAT_DATA_REL_RO_LOCAL : SECCAT_DATA_REL_RO;
6346 else if (reloc || flag_merge_constants < 2
6347 || ((flag_sanitize & SANITIZE_ADDRESS)
6348 && asan_protect_global (CONST_CAST_TREE (decl))))
6349 /* C and C++ don't allow different variables to share the same
6350 location. -fmerge-all-constants allows even that (at the
6351 expense of not conforming). */
6352 ret = SECCAT_RODATA;
6353 else if (TREE_CODE (DECL_INITIAL (decl)) == STRING_CST)
6354 ret = SECCAT_RODATA_MERGE_STR_INIT;
6355 else
6356 ret = SECCAT_RODATA_MERGE_CONST;
6358 else if (TREE_CODE (decl) == CONSTRUCTOR)
6360 if ((reloc & targetm.asm_out.reloc_rw_mask ())
6361 || TREE_SIDE_EFFECTS (decl)
6362 || ! TREE_CONSTANT (decl))
6363 ret = SECCAT_DATA;
6364 else
6365 ret = SECCAT_RODATA;
6367 else
6368 ret = SECCAT_RODATA;
6370 /* There are no read-only thread-local sections. */
6371 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
6373 /* Note that this would be *just* SECCAT_BSS, except that there's
6374 no concept of a read-only thread-local-data section. */
6375 if (ret == SECCAT_BSS
6376 || (flag_zero_initialized_in_bss
6377 && initializer_zerop (DECL_INITIAL (decl))))
6378 ret = SECCAT_TBSS;
6379 else
6380 ret = SECCAT_TDATA;
6383 /* If the target uses small data sections, select it. */
6384 else if (targetm.in_small_data_p (decl))
6386 if (ret == SECCAT_BSS)
6387 ret = SECCAT_SBSS;
6388 else if (targetm.have_srodata_section && ret == SECCAT_RODATA)
6389 ret = SECCAT_SRODATA;
6390 else
6391 ret = SECCAT_SDATA;
6394 return ret;
6397 static bool
6398 decl_readonly_section_1 (enum section_category category)
6400 switch (category)
6402 case SECCAT_RODATA:
6403 case SECCAT_RODATA_MERGE_STR:
6404 case SECCAT_RODATA_MERGE_STR_INIT:
6405 case SECCAT_RODATA_MERGE_CONST:
6406 case SECCAT_SRODATA:
6407 return true;
6408 default:
6409 return false;
6413 bool
6414 decl_readonly_section (const_tree decl, int reloc)
6416 return decl_readonly_section_1 (categorize_decl_for_section (decl, reloc));
6419 /* Select a section based on the above categorization. */
6421 section *
6422 default_elf_select_section (tree decl, int reloc,
6423 unsigned HOST_WIDE_INT align)
6425 const char *sname;
6426 switch (categorize_decl_for_section (decl, reloc))
6428 case SECCAT_TEXT:
6429 /* We're not supposed to be called on FUNCTION_DECLs. */
6430 gcc_unreachable ();
6431 case SECCAT_RODATA:
6432 return readonly_data_section;
6433 case SECCAT_RODATA_MERGE_STR:
6434 return mergeable_string_section (decl, align, 0);
6435 case SECCAT_RODATA_MERGE_STR_INIT:
6436 return mergeable_string_section (DECL_INITIAL (decl), align, 0);
6437 case SECCAT_RODATA_MERGE_CONST:
6438 return mergeable_constant_section (DECL_MODE (decl), align, 0);
6439 case SECCAT_SRODATA:
6440 sname = ".sdata2";
6441 break;
6442 case SECCAT_DATA:
6443 return data_section;
6444 case SECCAT_DATA_REL:
6445 sname = ".data.rel";
6446 break;
6447 case SECCAT_DATA_REL_LOCAL:
6448 sname = ".data.rel.local";
6449 break;
6450 case SECCAT_DATA_REL_RO:
6451 sname = ".data.rel.ro";
6452 break;
6453 case SECCAT_DATA_REL_RO_LOCAL:
6454 sname = ".data.rel.ro.local";
6455 break;
6456 case SECCAT_SDATA:
6457 sname = ".sdata";
6458 break;
6459 case SECCAT_TDATA:
6460 sname = ".tdata";
6461 break;
6462 case SECCAT_BSS:
6463 if (bss_section)
6464 return bss_section;
6465 sname = ".bss";
6466 break;
6467 case SECCAT_SBSS:
6468 sname = ".sbss";
6469 break;
6470 case SECCAT_TBSS:
6471 sname = ".tbss";
6472 break;
6473 default:
6474 gcc_unreachable ();
6477 return get_named_section (decl, sname, reloc);
6480 /* Construct a unique section name based on the decl name and the
6481 categorization performed above. */
6483 void
6484 default_unique_section (tree decl, int reloc)
6486 /* We only need to use .gnu.linkonce if we don't have COMDAT groups. */
6487 bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
6488 const char *prefix, *name, *linkonce;
6489 char *string;
6490 tree id;
6492 switch (categorize_decl_for_section (decl, reloc))
6494 case SECCAT_TEXT:
6495 prefix = one_only ? ".t" : ".text";
6496 break;
6497 case SECCAT_RODATA:
6498 case SECCAT_RODATA_MERGE_STR:
6499 case SECCAT_RODATA_MERGE_STR_INIT:
6500 case SECCAT_RODATA_MERGE_CONST:
6501 prefix = one_only ? ".r" : ".rodata";
6502 break;
6503 case SECCAT_SRODATA:
6504 prefix = one_only ? ".s2" : ".sdata2";
6505 break;
6506 case SECCAT_DATA:
6507 prefix = one_only ? ".d" : ".data";
6508 break;
6509 case SECCAT_DATA_REL:
6510 prefix = one_only ? ".d.rel" : ".data.rel";
6511 break;
6512 case SECCAT_DATA_REL_LOCAL:
6513 prefix = one_only ? ".d.rel.local" : ".data.rel.local";
6514 break;
6515 case SECCAT_DATA_REL_RO:
6516 prefix = one_only ? ".d.rel.ro" : ".data.rel.ro";
6517 break;
6518 case SECCAT_DATA_REL_RO_LOCAL:
6519 prefix = one_only ? ".d.rel.ro.local" : ".data.rel.ro.local";
6520 break;
6521 case SECCAT_SDATA:
6522 prefix = one_only ? ".s" : ".sdata";
6523 break;
6524 case SECCAT_BSS:
6525 prefix = one_only ? ".b" : ".bss";
6526 break;
6527 case SECCAT_SBSS:
6528 prefix = one_only ? ".sb" : ".sbss";
6529 break;
6530 case SECCAT_TDATA:
6531 prefix = one_only ? ".td" : ".tdata";
6532 break;
6533 case SECCAT_TBSS:
6534 prefix = one_only ? ".tb" : ".tbss";
6535 break;
6536 default:
6537 gcc_unreachable ();
6540 id = DECL_ASSEMBLER_NAME (decl);
6541 ultimate_transparent_alias_target (&id);
6542 name = IDENTIFIER_POINTER (id);
6543 name = targetm.strip_name_encoding (name);
6545 /* If we're using one_only, then there needs to be a .gnu.linkonce
6546 prefix to the section name. */
6547 linkonce = one_only ? ".gnu.linkonce" : "";
6549 string = ACONCAT ((linkonce, prefix, ".", name, NULL));
6551 set_decl_section_name (decl, string);
6554 /* Subroutine of compute_reloc_for_rtx for leaf rtxes. */
6556 static int
6557 compute_reloc_for_rtx_1 (const_rtx x)
6559 switch (GET_CODE (x))
6561 case SYMBOL_REF:
6562 return SYMBOL_REF_LOCAL_P (x) ? 1 : 2;
6563 case LABEL_REF:
6564 return 1;
6565 default:
6566 return 0;
6570 /* Like compute_reloc_for_constant, except for an RTX. The return value
6571 is a mask for which bit 1 indicates a global relocation, and bit 0
6572 indicates a local relocation. */
6574 static int
6575 compute_reloc_for_rtx (const_rtx x)
6577 switch (GET_CODE (x))
6579 case SYMBOL_REF:
6580 case LABEL_REF:
6581 return compute_reloc_for_rtx_1 (x);
6583 case CONST:
6585 int reloc = 0;
6586 subrtx_iterator::array_type array;
6587 FOR_EACH_SUBRTX (iter, array, x, ALL)
6588 reloc |= compute_reloc_for_rtx_1 (*iter);
6589 return reloc;
6592 default:
6593 return 0;
6597 section *
6598 default_select_rtx_section (machine_mode mode ATTRIBUTE_UNUSED,
6599 rtx x,
6600 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
6602 if (compute_reloc_for_rtx (x) & targetm.asm_out.reloc_rw_mask ())
6603 return data_section;
6604 else
6605 return readonly_data_section;
6608 section *
6609 default_elf_select_rtx_section (machine_mode mode, rtx x,
6610 unsigned HOST_WIDE_INT align)
6612 int reloc = compute_reloc_for_rtx (x);
6614 /* ??? Handle small data here somehow. */
6616 if (reloc & targetm.asm_out.reloc_rw_mask ())
6618 if (reloc == 1)
6619 return get_named_section (NULL, ".data.rel.ro.local", 1);
6620 else
6621 return get_named_section (NULL, ".data.rel.ro", 3);
6624 return mergeable_constant_section (mode, align, 0);
6627 /* Set the generally applicable flags on the SYMBOL_REF for EXP. */
6629 void
6630 default_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
6632 rtx symbol;
6633 int flags;
6635 /* Careful not to prod global register variables. */
6636 if (!MEM_P (rtl))
6637 return;
6638 symbol = XEXP (rtl, 0);
6639 if (GET_CODE (symbol) != SYMBOL_REF)
6640 return;
6642 flags = SYMBOL_REF_FLAGS (symbol) & SYMBOL_FLAG_HAS_BLOCK_INFO;
6643 if (TREE_CODE (decl) == FUNCTION_DECL)
6644 flags |= SYMBOL_FLAG_FUNCTION;
6645 if (targetm.binds_local_p (decl))
6646 flags |= SYMBOL_FLAG_LOCAL;
6647 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
6648 flags |= DECL_TLS_MODEL (decl) << SYMBOL_FLAG_TLS_SHIFT;
6649 else if (targetm.in_small_data_p (decl))
6650 flags |= SYMBOL_FLAG_SMALL;
6651 /* ??? Why is DECL_EXTERNAL ever set for non-PUBLIC names? Without
6652 being PUBLIC, the thing *must* be defined in this translation unit.
6653 Prevent this buglet from being propagated into rtl code as well. */
6654 if (DECL_P (decl) && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl))
6655 flags |= SYMBOL_FLAG_EXTERNAL;
6657 SYMBOL_REF_FLAGS (symbol) = flags;
6660 /* By default, we do nothing for encode_section_info, so we need not
6661 do anything but discard the '*' marker. */
6663 const char *
6664 default_strip_name_encoding (const char *str)
6666 return str + (*str == '*');
6669 #ifdef ASM_OUTPUT_DEF
6670 /* The default implementation of TARGET_ASM_OUTPUT_ANCHOR. Define the
6671 anchor relative to ".", the current section position. */
6673 void
6674 default_asm_output_anchor (rtx symbol)
6676 char buffer[100];
6678 sprintf (buffer, "*. + " HOST_WIDE_INT_PRINT_DEC,
6679 SYMBOL_REF_BLOCK_OFFSET (symbol));
6680 ASM_OUTPUT_DEF (asm_out_file, XSTR (symbol, 0), buffer);
6682 #endif
6684 /* The default implementation of TARGET_USE_ANCHORS_FOR_SYMBOL_P. */
6686 bool
6687 default_use_anchors_for_symbol_p (const_rtx symbol)
6689 section *sect;
6690 tree decl;
6692 /* Don't use anchors for mergeable sections. The linker might move
6693 the objects around. */
6694 sect = SYMBOL_REF_BLOCK (symbol)->sect;
6695 if (sect->common.flags & SECTION_MERGE)
6696 return false;
6698 /* Don't use anchors for small data sections. The small data register
6699 acts as an anchor for such sections. */
6700 if (sect->common.flags & SECTION_SMALL)
6701 return false;
6703 decl = SYMBOL_REF_DECL (symbol);
6704 if (decl && DECL_P (decl))
6706 /* Don't use section anchors for decls that might be defined or
6707 usurped by other modules. */
6708 if (TREE_PUBLIC (decl) && !decl_binds_to_current_def_p (decl))
6709 return false;
6711 /* Don't use section anchors for decls that will be placed in a
6712 small data section. */
6713 /* ??? Ideally, this check would be redundant with the SECTION_SMALL
6714 one above. The problem is that we only use SECTION_SMALL for
6715 sections that should be marked as small in the section directive. */
6716 if (targetm.in_small_data_p (decl))
6717 return false;
6719 return true;
6722 /* Return true when RESOLUTION indicate that symbol will be bound to the
6723 definition provided by current .o file. */
6725 static bool
6726 resolution_to_local_definition_p (enum ld_plugin_symbol_resolution resolution)
6728 return (resolution == LDPR_PREVAILING_DEF
6729 || resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
6730 || resolution == LDPR_PREVAILING_DEF_IRONLY);
6733 /* Return true when RESOLUTION indicate that symbol will be bound locally
6734 within current executable or DSO. */
6736 static bool
6737 resolution_local_p (enum ld_plugin_symbol_resolution resolution)
6739 return (resolution == LDPR_PREVAILING_DEF
6740 || resolution == LDPR_PREVAILING_DEF_IRONLY
6741 || resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
6742 || resolution == LDPR_PREEMPTED_REG
6743 || resolution == LDPR_PREEMPTED_IR
6744 || resolution == LDPR_RESOLVED_IR
6745 || resolution == LDPR_RESOLVED_EXEC);
6748 /* Assume ELF-ish defaults, since that's pretty much the most liberal
6749 wrt cross-module name binding. */
6751 bool
6752 default_binds_local_p (const_tree exp)
6754 return default_binds_local_p_1 (exp, flag_shlib);
6757 bool
6758 default_binds_local_p_1 (const_tree exp, int shlib)
6760 bool local_p;
6761 bool resolved_locally = false;
6762 bool resolved_to_local_def = false;
6764 /* With resolution file in hands, take look into resolutions.
6765 We can't just return true for resolved_locally symbols,
6766 because dynamic linking might overwrite symbols
6767 in shared libraries. */
6768 if (TREE_CODE (exp) == VAR_DECL && TREE_PUBLIC (exp)
6769 && (TREE_STATIC (exp) || DECL_EXTERNAL (exp)))
6771 varpool_node *vnode = varpool_node::get (exp);
6772 if (vnode && (resolution_local_p (vnode->resolution) || vnode->in_other_partition))
6773 resolved_locally = true;
6774 if (vnode
6775 && resolution_to_local_definition_p (vnode->resolution))
6776 resolved_to_local_def = true;
6778 else if (TREE_CODE (exp) == FUNCTION_DECL && TREE_PUBLIC (exp))
6780 struct cgraph_node *node = cgraph_node::get (exp);
6781 if (node
6782 && (resolution_local_p (node->resolution) || node->in_other_partition))
6783 resolved_locally = true;
6784 if (node
6785 && resolution_to_local_definition_p (node->resolution))
6786 resolved_to_local_def = true;
6789 /* A non-decl is an entry in the constant pool. */
6790 if (!DECL_P (exp))
6791 local_p = true;
6792 /* Weakrefs may not bind locally, even though the weakref itself is always
6793 static and therefore local. Similarly, the resolver for ifunc functions
6794 might resolve to a non-local function.
6795 FIXME: We can resolve the weakref case more curefuly by looking at the
6796 weakref alias. */
6797 else if (lookup_attribute ("weakref", DECL_ATTRIBUTES (exp))
6798 || (TREE_CODE (exp) == FUNCTION_DECL
6799 && lookup_attribute ("ifunc", DECL_ATTRIBUTES (exp))))
6800 local_p = false;
6801 /* Static variables are always local. */
6802 else if (! TREE_PUBLIC (exp))
6803 local_p = true;
6804 /* A variable is local if the user has said explicitly that it will
6805 be. */
6806 else if ((DECL_VISIBILITY_SPECIFIED (exp)
6807 || resolved_to_local_def)
6808 && DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
6809 local_p = true;
6810 /* Variables defined outside this object might not be local. */
6811 else if (DECL_EXTERNAL (exp) && !resolved_locally)
6812 local_p = false;
6813 /* If defined in this object and visibility is not default, must be
6814 local. */
6815 else if (DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
6816 local_p = true;
6817 /* Default visibility weak data can be overridden by a strong symbol
6818 in another module and so are not local. */
6819 else if (DECL_WEAK (exp)
6820 && !resolved_locally)
6821 local_p = false;
6822 /* If PIC, then assume that any global name can be overridden by
6823 symbols resolved from other modules. */
6824 else if (shlib)
6825 local_p = false;
6826 /* Uninitialized COMMON variable may be unified with symbols
6827 resolved from other modules. */
6828 else if (DECL_COMMON (exp)
6829 && !resolved_locally
6830 && (DECL_INITIAL (exp) == NULL
6831 || (!in_lto_p && DECL_INITIAL (exp) == error_mark_node)))
6832 local_p = false;
6833 /* Otherwise we're left with initialized (or non-common) global data
6834 which is of necessity defined locally. */
6835 else
6836 local_p = true;
6838 return local_p;
6841 /* Return true when references to DECL must bind to current definition in
6842 final executable.
6844 The condition is usually equivalent to whether the function binds to the
6845 current module (shared library or executable), that is to binds_local_p.
6846 We use this fact to avoid need for another target hook and implement
6847 the logic using binds_local_p and just special cases where
6848 decl_binds_to_current_def_p is stronger than binds_local_p. In particular
6849 the weak definitions (that can be overwritten at linktime by other
6850 definition from different object file) and when resolution info is available
6851 we simply use the knowledge passed to us by linker plugin. */
6852 bool
6853 decl_binds_to_current_def_p (const_tree decl)
6855 gcc_assert (DECL_P (decl));
6856 if (!targetm.binds_local_p (decl))
6857 return false;
6858 if (!TREE_PUBLIC (decl))
6859 return true;
6860 /* When resolution is available, just use it. */
6861 if (TREE_CODE (decl) == VAR_DECL
6862 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
6864 varpool_node *vnode = varpool_node::get (decl);
6865 if (vnode
6866 && vnode->resolution != LDPR_UNKNOWN)
6867 return resolution_to_local_definition_p (vnode->resolution);
6869 else if (TREE_CODE (decl) == FUNCTION_DECL)
6871 struct cgraph_node *node = cgraph_node::get (decl);
6872 if (node
6873 && node->resolution != LDPR_UNKNOWN)
6874 return resolution_to_local_definition_p (node->resolution);
6876 /* Otherwise we have to assume the worst for DECL_WEAK (hidden weaks
6877 binds locally but still can be overwritten), DECL_COMMON (can be merged
6878 with a non-common definition somewhere in the same module) or
6879 DECL_EXTERNAL.
6880 This rely on fact that binds_local_p behave as decl_replaceable_p
6881 for all other declaration types. */
6882 if (DECL_WEAK (decl))
6883 return false;
6884 if (DECL_COMMON (decl)
6885 && (DECL_INITIAL (decl) == NULL
6886 || (!in_lto_p && DECL_INITIAL (decl) == error_mark_node)))
6887 return false;
6888 if (DECL_EXTERNAL (decl))
6889 return false;
6890 return true;
6893 /* A replaceable function or variable is one which may be replaced
6894 at link-time with an entirely different definition, provided that the
6895 replacement has the same type. For example, functions declared
6896 with __attribute__((weak)) on most systems are replaceable.
6898 COMDAT functions are not replaceable, since all definitions of the
6899 function must be equivalent. It is important that COMDAT functions
6900 not be treated as replaceable so that use of C++ template
6901 instantiations is not penalized. */
6903 bool
6904 decl_replaceable_p (tree decl)
6906 gcc_assert (DECL_P (decl));
6907 if (!TREE_PUBLIC (decl) || DECL_COMDAT (decl))
6908 return false;
6909 if (!flag_semantic_interposition
6910 && !DECL_WEAK (decl))
6911 return false;
6912 return !decl_binds_to_current_def_p (decl);
6915 /* Default function to output code that will globalize a label. A
6916 target must define GLOBAL_ASM_OP or provide its own function to
6917 globalize a label. */
6918 #ifdef GLOBAL_ASM_OP
6919 void
6920 default_globalize_label (FILE * stream, const char *name)
6922 fputs (GLOBAL_ASM_OP, stream);
6923 assemble_name (stream, name);
6924 putc ('\n', stream);
6926 #endif /* GLOBAL_ASM_OP */
6928 /* Default function to output code that will globalize a declaration. */
6929 void
6930 default_globalize_decl_name (FILE * stream, tree decl)
6932 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
6933 targetm.asm_out.globalize_label (stream, name);
6936 /* Default function to output a label for unwind information. The
6937 default is to do nothing. A target that needs nonlocal labels for
6938 unwind information must provide its own function to do this. */
6939 void
6940 default_emit_unwind_label (FILE * stream ATTRIBUTE_UNUSED,
6941 tree decl ATTRIBUTE_UNUSED,
6942 int for_eh ATTRIBUTE_UNUSED,
6943 int empty ATTRIBUTE_UNUSED)
6947 /* Default function to output a label to divide up the exception table.
6948 The default is to do nothing. A target that needs/wants to divide
6949 up the table must provide it's own function to do this. */
6950 void
6951 default_emit_except_table_label (FILE * stream ATTRIBUTE_UNUSED)
6955 /* This is how to output an internal numbered label where PREFIX is
6956 the class of label and LABELNO is the number within the class. */
6958 void
6959 default_generate_internal_label (char *buf, const char *prefix,
6960 unsigned long labelno)
6962 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
6965 /* This is how to output an internal numbered label where PREFIX is
6966 the class of label and LABELNO is the number within the class. */
6968 void
6969 default_internal_label (FILE *stream, const char *prefix,
6970 unsigned long labelno)
6972 char *const buf = (char *) alloca (40 + strlen (prefix));
6973 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
6974 ASM_OUTPUT_INTERNAL_LABEL (stream, buf);
6978 /* The default implementation of ASM_DECLARE_CONSTANT_NAME. */
6980 void
6981 default_asm_declare_constant_name (FILE *file, const char *name,
6982 const_tree exp ATTRIBUTE_UNUSED,
6983 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
6985 assemble_label (file, name);
6988 /* This is the default behavior at the beginning of a file. It's
6989 controlled by two other target-hook toggles. */
6990 void
6991 default_file_start (void)
6993 if (targetm.asm_file_start_app_off
6994 && !(flag_verbose_asm || flag_debug_asm || flag_dump_rtl_in_asm))
6995 fputs (ASM_APP_OFF, asm_out_file);
6997 if (targetm.asm_file_start_file_directive)
6998 output_file_directive (asm_out_file, main_input_filename);
7001 /* This is a generic routine suitable for use as TARGET_ASM_FILE_END
7002 which emits a special section directive used to indicate whether or
7003 not this object file needs an executable stack. This is primarily
7004 a GNU extension to ELF but could be used on other targets. */
7006 int trampolines_created;
7008 void
7009 file_end_indicate_exec_stack (void)
7011 unsigned int flags = SECTION_DEBUG;
7012 if (trampolines_created)
7013 flags |= SECTION_CODE;
7015 switch_to_section (get_section (".note.GNU-stack", flags, NULL));
7018 /* Emit a special section directive to indicate that this object file
7019 was compiled with -fsplit-stack. This is used to let the linker
7020 detect calls between split-stack code and non-split-stack code, so
7021 that it can modify the split-stack code to allocate a sufficiently
7022 large stack. We emit another special section if there are any
7023 functions in this file which have the no_split_stack attribute, to
7024 prevent the linker from warning about being unable to convert the
7025 functions if they call non-split-stack code. */
7027 void
7028 file_end_indicate_split_stack (void)
7030 if (flag_split_stack)
7032 switch_to_section (get_section (".note.GNU-split-stack", SECTION_DEBUG,
7033 NULL));
7034 if (saw_no_split_stack)
7035 switch_to_section (get_section (".note.GNU-no-split-stack",
7036 SECTION_DEBUG, NULL));
7040 /* Output DIRECTIVE (a C string) followed by a newline. This is used as
7041 a get_unnamed_section callback. */
7043 void
7044 output_section_asm_op (const void *directive)
7046 fprintf (asm_out_file, "%s\n", (const char *) directive);
7049 /* Emit assembly code to switch to section NEW_SECTION. Do nothing if
7050 the current section is NEW_SECTION. */
7052 void
7053 switch_to_section (section *new_section)
7055 if (in_section == new_section)
7056 return;
7058 if (new_section->common.flags & SECTION_FORGET)
7059 in_section = NULL;
7060 else
7061 in_section = new_section;
7063 switch (SECTION_STYLE (new_section))
7065 case SECTION_NAMED:
7066 targetm.asm_out.named_section (new_section->named.name,
7067 new_section->named.common.flags,
7068 new_section->named.decl);
7069 break;
7071 case SECTION_UNNAMED:
7072 new_section->unnamed.callback (new_section->unnamed.data);
7073 break;
7075 case SECTION_NOSWITCH:
7076 gcc_unreachable ();
7077 break;
7080 new_section->common.flags |= SECTION_DECLARED;
7083 /* If block symbol SYMBOL has not yet been assigned an offset, place
7084 it at the end of its block. */
7086 void
7087 place_block_symbol (rtx symbol)
7089 unsigned HOST_WIDE_INT size, mask, offset;
7090 struct constant_descriptor_rtx *desc;
7091 unsigned int alignment;
7092 struct object_block *block;
7093 tree decl;
7095 gcc_assert (SYMBOL_REF_BLOCK (symbol));
7096 if (SYMBOL_REF_BLOCK_OFFSET (symbol) >= 0)
7097 return;
7099 /* Work out the symbol's size and alignment. */
7100 if (CONSTANT_POOL_ADDRESS_P (symbol))
7102 desc = SYMBOL_REF_CONSTANT (symbol);
7103 alignment = desc->align;
7104 size = GET_MODE_SIZE (desc->mode);
7106 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
7108 decl = SYMBOL_REF_DECL (symbol);
7109 alignment = DECL_ALIGN (decl);
7110 size = get_constant_size (DECL_INITIAL (decl));
7111 if ((flag_sanitize & SANITIZE_ADDRESS)
7112 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
7113 && asan_protect_global (DECL_INITIAL (decl)))
7114 size += asan_red_zone_size (size);
7116 else
7118 struct symtab_node *snode;
7119 decl = SYMBOL_REF_DECL (symbol);
7121 snode = symtab_node::get (decl);
7122 if (snode->alias)
7124 rtx target = DECL_RTL (snode->ultimate_alias_target ()->decl);
7126 place_block_symbol (target);
7127 SYMBOL_REF_BLOCK_OFFSET (symbol) = SYMBOL_REF_BLOCK_OFFSET (target);
7128 return;
7130 alignment = get_variable_align (decl);
7131 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
7132 if ((flag_sanitize & SANITIZE_ADDRESS)
7133 && asan_protect_global (decl))
7135 size += asan_red_zone_size (size);
7136 alignment = MAX (alignment,
7137 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
7141 /* Calculate the object's offset from the start of the block. */
7142 block = SYMBOL_REF_BLOCK (symbol);
7143 mask = alignment / BITS_PER_UNIT - 1;
7144 offset = (block->size + mask) & ~mask;
7145 SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
7147 /* Record the block's new alignment and size. */
7148 block->alignment = MAX (block->alignment, alignment);
7149 block->size = offset + size;
7151 vec_safe_push (block->objects, symbol);
7154 /* Return the anchor that should be used to address byte offset OFFSET
7155 from the first object in BLOCK. MODEL is the TLS model used
7156 to access it. */
7159 get_section_anchor (struct object_block *block, HOST_WIDE_INT offset,
7160 enum tls_model model)
7162 char label[100];
7163 unsigned int begin, middle, end;
7164 unsigned HOST_WIDE_INT min_offset, max_offset, range, bias, delta;
7165 rtx anchor;
7167 /* Work out the anchor's offset. Use an offset of 0 for the first
7168 anchor so that we don't pessimize the case where we take the address
7169 of a variable at the beginning of the block. This is particularly
7170 useful when a block has only one variable assigned to it.
7172 We try to place anchors RANGE bytes apart, so there can then be
7173 anchors at +/-RANGE, +/-2 * RANGE, and so on, up to the limits of
7174 a ptr_mode offset. With some target settings, the lowest such
7175 anchor might be out of range for the lowest ptr_mode offset;
7176 likewise the highest anchor for the highest offset. Use anchors
7177 at the extreme ends of the ptr_mode range in such cases.
7179 All arithmetic uses unsigned integers in order to avoid
7180 signed overflow. */
7181 max_offset = (unsigned HOST_WIDE_INT) targetm.max_anchor_offset;
7182 min_offset = (unsigned HOST_WIDE_INT) targetm.min_anchor_offset;
7183 range = max_offset - min_offset + 1;
7184 if (range == 0)
7185 offset = 0;
7186 else
7188 bias = 1 << (GET_MODE_BITSIZE (ptr_mode) - 1);
7189 if (offset < 0)
7191 delta = -(unsigned HOST_WIDE_INT) offset + max_offset;
7192 delta -= delta % range;
7193 if (delta > bias)
7194 delta = bias;
7195 offset = (HOST_WIDE_INT) (-delta);
7197 else
7199 delta = (unsigned HOST_WIDE_INT) offset - min_offset;
7200 delta -= delta % range;
7201 if (delta > bias - 1)
7202 delta = bias - 1;
7203 offset = (HOST_WIDE_INT) delta;
7207 /* Do a binary search to see if there's already an anchor we can use.
7208 Set BEGIN to the new anchor's index if not. */
7209 begin = 0;
7210 end = vec_safe_length (block->anchors);
7211 while (begin != end)
7213 middle = (end + begin) / 2;
7214 anchor = (*block->anchors)[middle];
7215 if (SYMBOL_REF_BLOCK_OFFSET (anchor) > offset)
7216 end = middle;
7217 else if (SYMBOL_REF_BLOCK_OFFSET (anchor) < offset)
7218 begin = middle + 1;
7219 else if (SYMBOL_REF_TLS_MODEL (anchor) > model)
7220 end = middle;
7221 else if (SYMBOL_REF_TLS_MODEL (anchor) < model)
7222 begin = middle + 1;
7223 else
7224 return anchor;
7227 /* Create a new anchor with a unique label. */
7228 ASM_GENERATE_INTERNAL_LABEL (label, "LANCHOR", anchor_labelno++);
7229 anchor = create_block_symbol (ggc_strdup (label), block, offset);
7230 SYMBOL_REF_FLAGS (anchor) |= SYMBOL_FLAG_LOCAL | SYMBOL_FLAG_ANCHOR;
7231 SYMBOL_REF_FLAGS (anchor) |= model << SYMBOL_FLAG_TLS_SHIFT;
7233 /* Insert it at index BEGIN. */
7234 vec_safe_insert (block->anchors, begin, anchor);
7235 return anchor;
7238 /* Output the objects in BLOCK. */
7240 static void
7241 output_object_block (struct object_block *block)
7243 struct constant_descriptor_rtx *desc;
7244 unsigned int i;
7245 HOST_WIDE_INT offset;
7246 tree decl;
7247 rtx symbol;
7249 if (!block->objects)
7250 return;
7252 /* Switch to the section and make sure that the first byte is
7253 suitably aligned. */
7254 switch_to_section (block->sect);
7255 assemble_align (block->alignment);
7257 /* Define the values of all anchors relative to the current section
7258 position. */
7259 FOR_EACH_VEC_SAFE_ELT (block->anchors, i, symbol)
7260 targetm.asm_out.output_anchor (symbol);
7262 /* Output the objects themselves. */
7263 offset = 0;
7264 FOR_EACH_VEC_ELT (*block->objects, i, symbol)
7266 /* Move to the object's offset, padding with zeros if necessary. */
7267 assemble_zeros (SYMBOL_REF_BLOCK_OFFSET (symbol) - offset);
7268 offset = SYMBOL_REF_BLOCK_OFFSET (symbol);
7269 if (CONSTANT_POOL_ADDRESS_P (symbol))
7271 desc = SYMBOL_REF_CONSTANT (symbol);
7272 output_constant_pool_1 (desc, 1);
7273 offset += GET_MODE_SIZE (desc->mode);
7275 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
7277 HOST_WIDE_INT size;
7278 decl = SYMBOL_REF_DECL (symbol);
7279 assemble_constant_contents (DECL_INITIAL (decl), XSTR (symbol, 0),
7280 DECL_ALIGN (decl));
7281 size = get_constant_size (DECL_INITIAL (decl));
7282 offset += size;
7283 if ((flag_sanitize & SANITIZE_ADDRESS)
7284 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
7285 && asan_protect_global (DECL_INITIAL (decl)))
7287 size = asan_red_zone_size (size);
7288 assemble_zeros (size);
7289 offset += size;
7292 else
7294 HOST_WIDE_INT size;
7295 decl = SYMBOL_REF_DECL (symbol);
7296 assemble_variable_contents (decl, XSTR (symbol, 0), false);
7297 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
7298 offset += size;
7299 if ((flag_sanitize & SANITIZE_ADDRESS)
7300 && asan_protect_global (decl))
7302 size = asan_red_zone_size (size);
7303 assemble_zeros (size);
7304 offset += size;
7310 /* A htab_traverse callback used to call output_object_block for
7311 each member of object_block_htab. */
7314 output_object_block_htab (object_block **slot, void *)
7316 output_object_block (*slot);
7317 return 1;
7320 /* Output the definitions of all object_blocks. */
7322 void
7323 output_object_blocks (void)
7325 object_block_htab->traverse<void *, output_object_block_htab> (NULL);
7328 /* This function provides a possible implementation of the
7329 TARGET_ASM_RECORD_GCC_SWITCHES target hook for ELF targets. When triggered
7330 by -frecord-gcc-switches it creates a new mergeable, string section in the
7331 assembler output file called TARGET_ASM_RECORD_GCC_SWITCHES_SECTION which
7332 contains the switches in ASCII format.
7334 FIXME: This code does not correctly handle double quote characters
7335 that appear inside strings, (it strips them rather than preserving them).
7336 FIXME: ASM_OUTPUT_ASCII, as defined in config/elfos.h will not emit NUL
7337 characters - instead it treats them as sub-string separators. Since
7338 we want to emit NUL strings terminators into the object file we have to use
7339 ASM_OUTPUT_SKIP. */
7342 elf_record_gcc_switches (print_switch_type type, const char * name)
7344 switch (type)
7346 case SWITCH_TYPE_PASSED:
7347 ASM_OUTPUT_ASCII (asm_out_file, name, strlen (name));
7348 ASM_OUTPUT_SKIP (asm_out_file, (unsigned HOST_WIDE_INT) 1);
7349 break;
7351 case SWITCH_TYPE_DESCRIPTIVE:
7352 if (name == NULL)
7354 /* Distinguish between invocations where name is NULL. */
7355 static bool started = false;
7357 if (!started)
7359 section * sec;
7361 sec = get_section (targetm.asm_out.record_gcc_switches_section,
7362 SECTION_DEBUG
7363 | SECTION_MERGE
7364 | SECTION_STRINGS
7365 | (SECTION_ENTSIZE & 1),
7366 NULL);
7367 switch_to_section (sec);
7368 started = true;
7372 default:
7373 break;
7376 /* The return value is currently ignored by the caller, but must be 0.
7377 For -fverbose-asm the return value would be the number of characters
7378 emitted into the assembler file. */
7379 return 0;
7382 /* Emit text to declare externally defined symbols. It is needed to
7383 properly support non-default visibility. */
7384 void
7385 default_elf_asm_output_external (FILE *file ATTRIBUTE_UNUSED,
7386 tree decl,
7387 const char *name ATTRIBUTE_UNUSED)
7389 /* We output the name if and only if TREE_SYMBOL_REFERENCED is
7390 set in order to avoid putting out names that are never really
7391 used. */
7392 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
7393 && targetm.binds_local_p (decl))
7394 maybe_assemble_visibility (decl);
7397 /* The default hook for TARGET_ASM_OUTPUT_SOURCE_FILENAME. */
7399 void
7400 default_asm_output_source_filename (FILE *file, const char *name)
7402 #ifdef ASM_OUTPUT_SOURCE_FILENAME
7403 ASM_OUTPUT_SOURCE_FILENAME (file, name);
7404 #else
7405 fprintf (file, "\t.file\t");
7406 output_quoted_string (file, name);
7407 putc ('\n', file);
7408 #endif
7411 /* Output a file name in the form wanted by System V. */
7413 void
7414 output_file_directive (FILE *asm_file, const char *input_name)
7416 int len;
7417 const char *na;
7419 if (input_name == NULL)
7420 input_name = "<stdin>";
7421 else
7422 input_name = remap_debug_filename (input_name);
7424 len = strlen (input_name);
7425 na = input_name + len;
7427 /* NA gets INPUT_NAME sans directory names. */
7428 while (na > input_name)
7430 if (IS_DIR_SEPARATOR (na[-1]))
7431 break;
7432 na--;
7435 targetm.asm_out.output_source_filename (asm_file, na);
7438 /* Create a DEBUG_EXPR_DECL / DEBUG_EXPR pair from RTL expression
7439 EXP. */
7441 make_debug_expr_from_rtl (const_rtx exp)
7443 tree ddecl = make_node (DEBUG_EXPR_DECL), type;
7444 machine_mode mode = GET_MODE (exp);
7445 rtx dval;
7447 DECL_ARTIFICIAL (ddecl) = 1;
7448 if (REG_P (exp) && REG_EXPR (exp))
7449 type = TREE_TYPE (REG_EXPR (exp));
7450 else if (MEM_P (exp) && MEM_EXPR (exp))
7451 type = TREE_TYPE (MEM_EXPR (exp));
7452 else
7453 type = NULL_TREE;
7454 if (type && TYPE_MODE (type) == mode)
7455 TREE_TYPE (ddecl) = type;
7456 else
7457 TREE_TYPE (ddecl) = lang_hooks.types.type_for_mode (mode, 1);
7458 DECL_MODE (ddecl) = mode;
7459 dval = gen_rtx_DEBUG_EXPR (mode);
7460 DEBUG_EXPR_TREE_DECL (dval) = ddecl;
7461 SET_DECL_RTL (ddecl, dval);
7462 return dval;
7465 #ifdef ELF_ASCII_ESCAPES
7466 /* Default ASM_OUTPUT_LIMITED_STRING for ELF targets. */
7468 void
7469 default_elf_asm_output_limited_string (FILE *f, const char *s)
7471 int escape;
7472 unsigned char c;
7474 fputs (STRING_ASM_OP, f);
7475 putc ('"', f);
7476 while (*s != '\0')
7478 c = *s;
7479 escape = ELF_ASCII_ESCAPES[c];
7480 switch (escape)
7482 case 0:
7483 putc (c, f);
7484 break;
7485 case 1:
7486 /* TODO: Print in hex with fast function, important for -flto. */
7487 fprintf (f, "\\%03o", c);
7488 break;
7489 default:
7490 putc ('\\', f);
7491 putc (escape, f);
7492 break;
7494 s++;
7496 putc ('\"', f);
7497 putc ('\n', f);
7500 /* Default ASM_OUTPUT_ASCII for ELF targets. */
7502 void
7503 default_elf_asm_output_ascii (FILE *f, const char *s, unsigned int len)
7505 const char *limit = s + len;
7506 const char *last_null = NULL;
7507 unsigned bytes_in_chunk = 0;
7508 unsigned char c;
7509 int escape;
7511 for (; s < limit; s++)
7513 const char *p;
7515 if (bytes_in_chunk >= 60)
7517 putc ('\"', f);
7518 putc ('\n', f);
7519 bytes_in_chunk = 0;
7522 if (s > last_null)
7524 for (p = s; p < limit && *p != '\0'; p++)
7525 continue;
7526 last_null = p;
7528 else
7529 p = last_null;
7531 if (p < limit && (p - s) <= (long) ELF_STRING_LIMIT)
7533 if (bytes_in_chunk > 0)
7535 putc ('\"', f);
7536 putc ('\n', f);
7537 bytes_in_chunk = 0;
7540 default_elf_asm_output_limited_string (f, s);
7541 s = p;
7543 else
7545 if (bytes_in_chunk == 0)
7546 fputs (ASCII_DATA_ASM_OP "\"", f);
7548 c = *s;
7549 escape = ELF_ASCII_ESCAPES[c];
7550 switch (escape)
7552 case 0:
7553 putc (c, f);
7554 bytes_in_chunk++;
7555 break;
7556 case 1:
7557 /* TODO: Print in hex with fast function, important for -flto. */
7558 fprintf (f, "\\%03o", c);
7559 bytes_in_chunk += 4;
7560 break;
7561 default:
7562 putc ('\\', f);
7563 putc (escape, f);
7564 bytes_in_chunk += 2;
7565 break;
7571 if (bytes_in_chunk > 0)
7573 putc ('\"', f);
7574 putc ('\n', f);
7577 #endif
7579 static GTY(()) section *elf_init_array_section;
7580 static GTY(()) section *elf_fini_array_section;
7582 static section *
7583 get_elf_initfini_array_priority_section (int priority,
7584 bool constructor_p)
7586 section *sec;
7587 if (priority != DEFAULT_INIT_PRIORITY)
7589 char buf[18];
7590 sprintf (buf, "%s.%.5u",
7591 constructor_p ? ".init_array" : ".fini_array",
7592 priority);
7593 sec = get_section (buf, SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
7595 else
7597 if (constructor_p)
7599 if (elf_init_array_section == NULL)
7600 elf_init_array_section
7601 = get_section (".init_array",
7602 SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
7603 sec = elf_init_array_section;
7605 else
7607 if (elf_fini_array_section == NULL)
7608 elf_fini_array_section
7609 = get_section (".fini_array",
7610 SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
7611 sec = elf_fini_array_section;
7614 return sec;
7617 /* Use .init_array section for constructors. */
7619 void
7620 default_elf_init_array_asm_out_constructor (rtx symbol, int priority)
7622 section *sec = get_elf_initfini_array_priority_section (priority,
7623 true);
7624 assemble_addr_to_section (symbol, sec);
7627 /* Use .fini_array section for destructors. */
7629 void
7630 default_elf_fini_array_asm_out_destructor (rtx symbol, int priority)
7632 section *sec = get_elf_initfini_array_priority_section (priority,
7633 false);
7634 assemble_addr_to_section (symbol, sec);
7637 /* Default TARGET_ASM_OUTPUT_IDENT hook.
7639 This is a bit of a cheat. The real default is a no-op, but this
7640 hook is the default for all targets with a .ident directive. */
7642 void
7643 default_asm_output_ident_directive (const char *ident_str)
7645 const char *ident_asm_op = "\t.ident\t";
7647 /* If we are still in the front end, do not write out the string
7648 to asm_out_file. Instead, add a fake top-level asm statement.
7649 This allows the front ends to use this hook without actually
7650 writing to asm_out_file, to handle #ident or Pragma Ident. */
7651 if (symtab->state == PARSING)
7653 char *buf = ACONCAT ((ident_asm_op, "\"", ident_str, "\"\n", NULL));
7654 symtab->finalize_toplevel_asm (build_string (strlen (buf), buf));
7656 else
7657 fprintf (asm_out_file, "%s\"%s\"\n", ident_asm_op, ident_str);
7660 #include "gt-varasm.h"