Fixups after merge
[official-gcc.git] / gcc / varasm.c
blob54611f8fd3f17a035eb3befbc606a6ce311fc7de
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);
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 /* Get the function's name, as described by its RTL. This may be
1677 different from the DECL_NAME name used in the source file. */
1678 const char *
1679 get_fnname_from_decl (tree decl)
1681 rtx x = DECL_RTL (decl);
1682 gcc_assert (MEM_P (x));
1683 x = XEXP (x, 0);
1684 gcc_assert (GET_CODE (x) == SYMBOL_REF);
1685 return XSTR (x, 0);
1688 /* Output assembler code for the constant pool of a function and associated
1689 with defining the name of the function. DECL describes the function.
1690 NAME is the function's name. For the constant pool, we use the current
1691 constant pool data. */
1693 void
1694 assemble_start_function (tree decl, const char *fnname)
1696 int align;
1697 char tmp_label[100];
1698 bool hot_label_written = false;
1700 if (flag_reorder_blocks_and_partition)
1702 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTB", const_labelno);
1703 crtl->subsections.hot_section_label = ggc_strdup (tmp_label);
1704 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDB", const_labelno);
1705 crtl->subsections.cold_section_label = ggc_strdup (tmp_label);
1706 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTE", const_labelno);
1707 crtl->subsections.hot_section_end_label = ggc_strdup (tmp_label);
1708 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDE", const_labelno);
1709 crtl->subsections.cold_section_end_label = ggc_strdup (tmp_label);
1710 const_labelno++;
1712 else
1714 crtl->subsections.hot_section_label = NULL;
1715 crtl->subsections.cold_section_label = NULL;
1716 crtl->subsections.hot_section_end_label = NULL;
1717 crtl->subsections.cold_section_end_label = NULL;
1720 /* The following code does not need preprocessing in the assembler. */
1722 app_disable ();
1724 if (CONSTANT_POOL_BEFORE_FUNCTION)
1725 output_constant_pool (fnname, decl);
1727 /* Make sure the not and cold text (code) sections are properly
1728 aligned. This is necessary here in the case where the function
1729 has both hot and cold sections, because we don't want to re-set
1730 the alignment when the section switch happens mid-function. */
1732 if (flag_reorder_blocks_and_partition)
1734 first_function_block_is_cold = false;
1736 switch_to_section (unlikely_text_section ());
1737 assemble_align (DECL_ALIGN (decl));
1738 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_label);
1740 /* When the function starts with a cold section, we need to explicitly
1741 align the hot section and write out the hot section label.
1742 But if the current function is a thunk, we do not have a CFG. */
1743 if (!cfun->is_thunk
1744 && BB_PARTITION (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb) == BB_COLD_PARTITION)
1746 switch_to_section (text_section);
1747 assemble_align (DECL_ALIGN (decl));
1748 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
1749 hot_label_written = true;
1750 first_function_block_is_cold = true;
1752 in_cold_section_p = first_function_block_is_cold;
1756 /* Switch to the correct text section for the start of the function. */
1758 switch_to_section (function_section (decl));
1759 if (flag_reorder_blocks_and_partition
1760 && !hot_label_written)
1761 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
1763 /* Tell assembler to move to target machine's alignment for functions. */
1764 align = floor_log2 (DECL_ALIGN (decl) / BITS_PER_UNIT);
1765 if (align > 0)
1767 ASM_OUTPUT_ALIGN (asm_out_file, align);
1770 /* Handle a user-specified function alignment.
1771 Note that we still need to align to DECL_ALIGN, as above,
1772 because ASM_OUTPUT_MAX_SKIP_ALIGN might not do any alignment at all. */
1773 if (! DECL_USER_ALIGN (decl)
1774 && align_functions_log > align
1775 && optimize_function_for_speed_p (cfun))
1777 #ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
1778 ASM_OUTPUT_MAX_SKIP_ALIGN (asm_out_file,
1779 align_functions_log, align_functions - 1);
1780 #else
1781 ASM_OUTPUT_ALIGN (asm_out_file, align_functions_log);
1782 #endif
1785 #ifdef ASM_OUTPUT_FUNCTION_PREFIX
1786 ASM_OUTPUT_FUNCTION_PREFIX (asm_out_file, fnname);
1787 #endif
1789 if (!DECL_IGNORED_P (decl))
1790 (*debug_hooks->begin_function) (decl);
1792 /* Make function name accessible from other files, if appropriate. */
1794 if (TREE_PUBLIC (decl)
1795 || (cgraph_node::get (decl)->instrumentation_clone
1796 && cgraph_node::get (decl)->instrumented_version
1797 && TREE_PUBLIC (cgraph_node::get (decl)->instrumented_version->decl)))
1799 notice_global_symbol (decl);
1801 globalize_decl (decl);
1803 maybe_assemble_visibility (decl);
1806 if (DECL_PRESERVE_P (decl))
1807 targetm.asm_out.mark_decl_preserved (fnname);
1809 /* Do any machine/system dependent processing of the function name. */
1810 #ifdef ASM_DECLARE_FUNCTION_NAME
1811 ASM_DECLARE_FUNCTION_NAME (asm_out_file, fnname, current_function_decl);
1812 #else
1813 /* Standard thing is just output label for the function. */
1814 ASM_OUTPUT_FUNCTION_LABEL (asm_out_file, fnname, current_function_decl);
1815 #endif /* ASM_DECLARE_FUNCTION_NAME */
1817 if (lookup_attribute ("no_split_stack", DECL_ATTRIBUTES (decl)))
1818 saw_no_split_stack = true;
1821 /* Output assembler code associated with defining the size of the
1822 function. DECL describes the function. NAME is the function's name. */
1824 void
1825 assemble_end_function (tree decl, const char *fnname ATTRIBUTE_UNUSED)
1827 #ifdef ASM_DECLARE_FUNCTION_SIZE
1828 /* We could have switched section in the middle of the function. */
1829 if (flag_reorder_blocks_and_partition)
1830 switch_to_section (function_section (decl));
1831 ASM_DECLARE_FUNCTION_SIZE (asm_out_file, fnname, decl);
1832 #endif
1833 if (! CONSTANT_POOL_BEFORE_FUNCTION)
1835 output_constant_pool (fnname, decl);
1836 switch_to_section (function_section (decl)); /* need to switch back */
1838 /* Output labels for end of hot/cold text sections (to be used by
1839 debug info.) */
1840 if (flag_reorder_blocks_and_partition)
1842 section *save_text_section;
1844 save_text_section = in_section;
1845 switch_to_section (unlikely_text_section ());
1846 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_end_label);
1847 if (first_function_block_is_cold)
1848 switch_to_section (text_section);
1849 else
1850 switch_to_section (function_section (decl));
1851 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_end_label);
1852 switch_to_section (save_text_section);
1856 /* Assemble code to leave SIZE bytes of zeros. */
1858 void
1859 assemble_zeros (unsigned HOST_WIDE_INT size)
1861 /* Do no output if -fsyntax-only. */
1862 if (flag_syntax_only)
1863 return;
1865 #ifdef ASM_NO_SKIP_IN_TEXT
1866 /* The `space' pseudo in the text section outputs nop insns rather than 0s,
1867 so we must output 0s explicitly in the text section. */
1868 if (ASM_NO_SKIP_IN_TEXT && (in_section->common.flags & SECTION_CODE) != 0)
1870 unsigned HOST_WIDE_INT i;
1871 for (i = 0; i < size; i++)
1872 assemble_integer (const0_rtx, 1, BITS_PER_UNIT, 1);
1874 else
1875 #endif
1876 if (size > 0)
1877 ASM_OUTPUT_SKIP (asm_out_file, size);
1880 /* Assemble an alignment pseudo op for an ALIGN-bit boundary. */
1882 void
1883 assemble_align (int align)
1885 if (align > BITS_PER_UNIT)
1887 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
1891 /* Assemble a string constant with the specified C string as contents. */
1893 void
1894 assemble_string (const char *p, int size)
1896 int pos = 0;
1897 int maximum = 2000;
1899 /* If the string is very long, split it up. */
1901 while (pos < size)
1903 int thissize = size - pos;
1904 if (thissize > maximum)
1905 thissize = maximum;
1907 ASM_OUTPUT_ASCII (asm_out_file, p, thissize);
1909 pos += thissize;
1910 p += thissize;
1915 /* A noswitch_section_callback for lcomm_section. */
1917 static bool
1918 emit_local (tree decl ATTRIBUTE_UNUSED,
1919 const char *name ATTRIBUTE_UNUSED,
1920 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1921 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1923 #if defined ASM_OUTPUT_ALIGNED_DECL_LOCAL
1924 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, decl, name,
1925 size, DECL_ALIGN (decl));
1926 return true;
1927 #elif defined ASM_OUTPUT_ALIGNED_LOCAL
1928 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, DECL_ALIGN (decl));
1929 return true;
1930 #else
1931 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
1932 return false;
1933 #endif
1936 /* A noswitch_section_callback for bss_noswitch_section. */
1938 #if defined ASM_OUTPUT_ALIGNED_BSS
1939 static bool
1940 emit_bss (tree decl ATTRIBUTE_UNUSED,
1941 const char *name ATTRIBUTE_UNUSED,
1942 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1943 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1945 #if defined ASM_OUTPUT_ALIGNED_BSS
1946 ASM_OUTPUT_ALIGNED_BSS (asm_out_file, decl, name, size,
1947 get_variable_align (decl));
1948 return true;
1949 #endif
1951 #endif
1953 /* A noswitch_section_callback for comm_section. */
1955 static bool
1956 emit_common (tree decl ATTRIBUTE_UNUSED,
1957 const char *name ATTRIBUTE_UNUSED,
1958 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1959 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1961 #if defined ASM_OUTPUT_ALIGNED_DECL_COMMON
1962 ASM_OUTPUT_ALIGNED_DECL_COMMON (asm_out_file, decl, name,
1963 size, get_variable_align (decl));
1964 return true;
1965 #elif defined ASM_OUTPUT_ALIGNED_COMMON
1966 ASM_OUTPUT_ALIGNED_COMMON (asm_out_file, name, size,
1967 get_variable_align (decl));
1968 return true;
1969 #else
1970 ASM_OUTPUT_COMMON (asm_out_file, name, size, rounded);
1971 return false;
1972 #endif
1975 /* A noswitch_section_callback for tls_comm_section. */
1977 static bool
1978 emit_tls_common (tree decl ATTRIBUTE_UNUSED,
1979 const char *name ATTRIBUTE_UNUSED,
1980 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1981 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1983 #ifdef ASM_OUTPUT_TLS_COMMON
1984 ASM_OUTPUT_TLS_COMMON (asm_out_file, decl, name, size);
1985 return true;
1986 #else
1987 sorry ("thread-local COMMON data not implemented");
1988 return true;
1989 #endif
1992 /* Assemble DECL given that it belongs in SECTION_NOSWITCH section SECT.
1993 NAME is the name of DECL's SYMBOL_REF. */
1995 static void
1996 assemble_noswitch_variable (tree decl, const char *name, section *sect,
1997 unsigned int align)
1999 unsigned HOST_WIDE_INT size, rounded;
2001 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
2002 rounded = size;
2004 if ((flag_sanitize & SANITIZE_ADDRESS) && asan_protect_global (decl))
2005 size += asan_red_zone_size (size);
2007 /* Don't allocate zero bytes of common,
2008 since that means "undefined external" in the linker. */
2009 if (size == 0)
2010 rounded = 1;
2012 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
2013 so that each uninitialized object starts on such a boundary. */
2014 rounded += (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1;
2015 rounded = (rounded / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
2016 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
2018 if (!sect->noswitch.callback (decl, name, size, rounded)
2019 && (unsigned HOST_WIDE_INT) (align / BITS_PER_UNIT) > rounded)
2020 error ("requested alignment for %q+D is greater than "
2021 "implemented alignment of %wu", decl, rounded);
2024 /* A subroutine of assemble_variable. Output the label and contents of
2025 DECL, whose address is a SYMBOL_REF with name NAME. DONT_OUTPUT_DATA
2026 is as for assemble_variable. */
2028 static void
2029 assemble_variable_contents (tree decl, const char *name,
2030 bool dont_output_data)
2032 /* Do any machine/system dependent processing of the object. */
2033 #ifdef ASM_DECLARE_OBJECT_NAME
2034 last_assemble_variable_decl = decl;
2035 ASM_DECLARE_OBJECT_NAME (asm_out_file, name, decl);
2036 #else
2037 /* Standard thing is just output label for the object. */
2038 ASM_OUTPUT_LABEL (asm_out_file, name);
2039 #endif /* ASM_DECLARE_OBJECT_NAME */
2041 if (!dont_output_data)
2043 /* Caller is supposed to use varpool_get_constructor when it wants
2044 to output the body. */
2045 gcc_assert (!in_lto_p || DECL_INITIAL (decl) != error_mark_node);
2046 if (DECL_INITIAL (decl)
2047 && DECL_INITIAL (decl) != error_mark_node
2048 && !initializer_zerop (DECL_INITIAL (decl)))
2049 /* Output the actual data. */
2050 output_constant (DECL_INITIAL (decl),
2051 tree_to_uhwi (DECL_SIZE_UNIT (decl)),
2052 get_variable_align (decl));
2053 else
2054 /* Leave space for it. */
2055 assemble_zeros (tree_to_uhwi (DECL_SIZE_UNIT (decl)));
2056 targetm.asm_out.decl_end ();
2060 /* Write out assembly for the variable DECL, which is not defined in
2061 the current translation unit. */
2062 void
2063 assemble_undefined_decl (tree decl)
2065 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
2066 targetm.asm_out.assemble_undefined_decl (asm_out_file, name, decl);
2069 /* Assemble everything that is needed for a variable or function declaration.
2070 Not used for automatic variables, and not used for function definitions.
2071 Should not be called for variables of incomplete structure type.
2073 TOP_LEVEL is nonzero if this variable has file scope.
2074 AT_END is nonzero if this is the special handling, at end of compilation,
2075 to define things that have had only tentative definitions.
2076 DONT_OUTPUT_DATA if nonzero means don't actually output the
2077 initial value (that will be done by the caller). */
2079 void
2080 assemble_variable (tree decl, int top_level ATTRIBUTE_UNUSED,
2081 int at_end ATTRIBUTE_UNUSED, int dont_output_data)
2083 const char *name;
2084 rtx decl_rtl, symbol;
2085 section *sect;
2086 unsigned int align;
2087 bool asan_protected = false;
2089 /* This function is supposed to handle VARIABLES. Ensure we have one. */
2090 gcc_assert (TREE_CODE (decl) == VAR_DECL);
2092 /* Emulated TLS had better not get this far. */
2093 gcc_checking_assert (targetm.have_tls || !DECL_THREAD_LOCAL_P (decl));
2095 last_assemble_variable_decl = 0;
2097 /* Normally no need to say anything here for external references,
2098 since assemble_external is called by the language-specific code
2099 when a declaration is first seen. */
2101 if (DECL_EXTERNAL (decl))
2102 return;
2104 /* Do nothing for global register variables. */
2105 if (DECL_RTL_SET_P (decl) && REG_P (DECL_RTL (decl)))
2107 TREE_ASM_WRITTEN (decl) = 1;
2108 return;
2111 /* If type was incomplete when the variable was declared,
2112 see if it is complete now. */
2114 if (DECL_SIZE (decl) == 0)
2115 layout_decl (decl, 0);
2117 /* Still incomplete => don't allocate it; treat the tentative defn
2118 (which is what it must have been) as an `extern' reference. */
2120 if (!dont_output_data && DECL_SIZE (decl) == 0)
2122 error ("storage size of %q+D isn%'t known", decl);
2123 TREE_ASM_WRITTEN (decl) = 1;
2124 return;
2127 /* The first declaration of a variable that comes through this function
2128 decides whether it is global (in C, has external linkage)
2129 or local (in C, has internal linkage). So do nothing more
2130 if this function has already run. */
2132 if (TREE_ASM_WRITTEN (decl))
2133 return;
2135 /* Make sure targetm.encode_section_info is invoked before we set
2136 ASM_WRITTEN. */
2137 decl_rtl = DECL_RTL (decl);
2139 TREE_ASM_WRITTEN (decl) = 1;
2141 /* Do no output if -fsyntax-only. */
2142 if (flag_syntax_only)
2143 return;
2145 if (! dont_output_data
2146 && ! valid_constant_size_p (DECL_SIZE_UNIT (decl)))
2148 error ("size of variable %q+D is too large", decl);
2149 return;
2152 gcc_assert (MEM_P (decl_rtl));
2153 gcc_assert (GET_CODE (XEXP (decl_rtl, 0)) == SYMBOL_REF);
2154 symbol = XEXP (decl_rtl, 0);
2156 /* If this symbol belongs to the tree constant pool, output the constant
2157 if it hasn't already been written. */
2158 if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
2160 tree decl = SYMBOL_REF_DECL (symbol);
2161 if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
2162 output_constant_def_contents (symbol);
2163 return;
2166 app_disable ();
2168 name = XSTR (symbol, 0);
2169 if (TREE_PUBLIC (decl) && DECL_NAME (decl))
2170 notice_global_symbol (decl);
2172 /* Compute the alignment of this data. */
2174 align_variable (decl, dont_output_data);
2176 if ((flag_sanitize & SANITIZE_ADDRESS)
2177 && asan_protect_global (decl))
2179 asan_protected = true;
2180 DECL_ALIGN (decl) = MAX (DECL_ALIGN (decl),
2181 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
2184 set_mem_align (decl_rtl, DECL_ALIGN (decl));
2186 align = get_variable_align (decl);
2188 if (TREE_PUBLIC (decl))
2189 maybe_assemble_visibility (decl);
2191 if (DECL_PRESERVE_P (decl))
2192 targetm.asm_out.mark_decl_preserved (name);
2194 /* First make the assembler name(s) global if appropriate. */
2195 sect = get_variable_section (decl, false);
2196 if (TREE_PUBLIC (decl)
2197 && (sect->common.flags & SECTION_COMMON) == 0)
2198 globalize_decl (decl);
2200 /* Output any data that we will need to use the address of. */
2201 if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node)
2202 output_addressed_constants (DECL_INITIAL (decl));
2204 /* dbxout.c needs to know this. */
2205 if (sect && (sect->common.flags & SECTION_CODE) != 0)
2206 DECL_IN_TEXT_SECTION (decl) = 1;
2208 /* If the decl is part of an object_block, make sure that the decl
2209 has been positioned within its block, but do not write out its
2210 definition yet. output_object_blocks will do that later. */
2211 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
2213 gcc_assert (!dont_output_data);
2214 place_block_symbol (symbol);
2216 else if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
2217 assemble_noswitch_variable (decl, name, sect, align);
2218 else
2220 /* The following bit of code ensures that vtable_map
2221 variables are not only in the comdat section, but that
2222 each variable has its own unique comdat name. If this
2223 code is removed, the variables end up in the same section
2224 with a single comdat name.
2226 FIXME: resolve_unique_section needs to deal better with
2227 decls with both DECL_SECTION_NAME and DECL_ONE_ONLY. Once
2228 that is fixed, this if-else statement can be replaced with
2229 a single call to "switch_to_section (sect)". */
2230 if (sect->named.name
2231 && (strcmp (sect->named.name, ".vtable_map_vars") == 0))
2233 #if defined (OBJECT_FORMAT_ELF)
2234 targetm.asm_out.named_section (sect->named.name,
2235 sect->named.common.flags
2236 | SECTION_LINKONCE,
2237 DECL_NAME (decl));
2238 in_section = sect;
2239 #else
2240 switch_to_section (sect);
2241 #endif
2243 else
2244 switch_to_section (sect);
2245 if (align > BITS_PER_UNIT)
2246 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
2247 assemble_variable_contents (decl, name, dont_output_data);
2248 if (asan_protected)
2250 unsigned HOST_WIDE_INT int size
2251 = tree_to_uhwi (DECL_SIZE_UNIT (decl));
2252 assemble_zeros (asan_red_zone_size (size));
2258 /* Given a function declaration (FN_DECL), this function assembles the
2259 function into the .preinit_array section. */
2261 void
2262 assemble_vtv_preinit_initializer (tree fn_decl)
2264 section *sect;
2265 unsigned flags = SECTION_WRITE;
2266 rtx symbol = XEXP (DECL_RTL (fn_decl), 0);
2268 flags |= SECTION_NOTYPE;
2269 sect = get_section (".preinit_array", flags, fn_decl);
2270 switch_to_section (sect);
2271 assemble_addr_to_section (symbol, sect);
2274 /* Return 1 if type TYPE contains any pointers. */
2276 static int
2277 contains_pointers_p (tree type)
2279 switch (TREE_CODE (type))
2281 case POINTER_TYPE:
2282 case REFERENCE_TYPE:
2283 /* I'm not sure whether OFFSET_TYPE needs this treatment,
2284 so I'll play safe and return 1. */
2285 case OFFSET_TYPE:
2286 return 1;
2288 case RECORD_TYPE:
2289 case UNION_TYPE:
2290 case QUAL_UNION_TYPE:
2292 tree fields;
2293 /* For a type that has fields, see if the fields have pointers. */
2294 for (fields = TYPE_FIELDS (type); fields; fields = DECL_CHAIN (fields))
2295 if (TREE_CODE (fields) == FIELD_DECL
2296 && contains_pointers_p (TREE_TYPE (fields)))
2297 return 1;
2298 return 0;
2301 case ARRAY_TYPE:
2302 /* An array type contains pointers if its element type does. */
2303 return contains_pointers_p (TREE_TYPE (type));
2305 default:
2306 return 0;
2310 /* We delay assemble_external processing until
2311 the compilation unit is finalized. This is the best we can do for
2312 right now (i.e. stage 3 of GCC 4.0) - the right thing is to delay
2313 it all the way to final. See PR 17982 for further discussion. */
2314 static GTY(()) tree pending_assemble_externals;
2316 #ifdef ASM_OUTPUT_EXTERNAL
2317 /* Some targets delay some output to final using TARGET_ASM_FILE_END.
2318 As a result, assemble_external can be called after the list of externals
2319 is processed and the pointer set destroyed. */
2320 static bool pending_assemble_externals_processed;
2322 /* Avoid O(external_decls**2) lookups in the pending_assemble_externals
2323 TREE_LIST in assemble_external. */
2324 static hash_set<tree> *pending_assemble_externals_set;
2326 /* True if DECL is a function decl for which no out-of-line copy exists.
2327 It is assumed that DECL's assembler name has been set. */
2329 static bool
2330 incorporeal_function_p (tree decl)
2332 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
2334 const char *name;
2336 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
2337 && (DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA
2338 || DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA_WITH_ALIGN))
2339 return true;
2341 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2342 /* Atomic or sync builtins which have survived this far will be
2343 resolved externally and therefore are not incorporeal. */
2344 if (strncmp (name, "__builtin_", 10) == 0)
2345 return true;
2347 return false;
2350 /* Actually do the tests to determine if this is necessary, and invoke
2351 ASM_OUTPUT_EXTERNAL. */
2352 static void
2353 assemble_external_real (tree decl)
2355 rtx rtl = DECL_RTL (decl);
2357 if (MEM_P (rtl) && GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF
2358 && !SYMBOL_REF_USED (XEXP (rtl, 0))
2359 && !incorporeal_function_p (decl))
2361 /* Some systems do require some output. */
2362 SYMBOL_REF_USED (XEXP (rtl, 0)) = 1;
2363 ASM_OUTPUT_EXTERNAL (asm_out_file, decl, XSTR (XEXP (rtl, 0), 0));
2366 #endif
2368 void
2369 process_pending_assemble_externals (void)
2371 #ifdef ASM_OUTPUT_EXTERNAL
2372 tree list;
2373 for (list = pending_assemble_externals; list; list = TREE_CHAIN (list))
2374 assemble_external_real (TREE_VALUE (list));
2376 pending_assemble_externals = 0;
2377 pending_assemble_externals_processed = true;
2378 delete pending_assemble_externals_set;
2379 #endif
2382 /* This TREE_LIST contains any weak symbol declarations waiting
2383 to be emitted. */
2384 static GTY(()) tree weak_decls;
2386 /* Output something to declare an external symbol to the assembler,
2387 and qualifiers such as weakness. (Most assemblers don't need
2388 extern declaration, so we normally output nothing.) Do nothing if
2389 DECL is not external. */
2391 void
2392 assemble_external (tree decl ATTRIBUTE_UNUSED)
2394 /* Make sure that the ASM_OUT_FILE is open.
2395 If it's not, we should not be calling this function. */
2396 gcc_assert (asm_out_file);
2398 /* In a perfect world, the following condition would be true.
2399 Sadly, the Java and Go front ends emit assembly *from the front end*,
2400 bypassing the call graph. See PR52739. Fix before GCC 4.8. */
2401 #if 0
2402 /* This function should only be called if we are expanding, or have
2403 expanded, to RTL.
2404 Ideally, only final.c would be calling this function, but it is
2405 not clear whether that would break things somehow. See PR 17982
2406 for further discussion. */
2407 gcc_assert (state == EXPANSION
2408 || state == FINISHED);
2409 #endif
2411 if (!DECL_P (decl) || !DECL_EXTERNAL (decl) || !TREE_PUBLIC (decl))
2412 return;
2414 /* We want to output annotation for weak and external symbols at
2415 very last to check if they are references or not. */
2417 if (TARGET_SUPPORTS_WEAK
2418 && DECL_WEAK (decl)
2419 /* TREE_STATIC is a weird and abused creature which is not
2420 generally the right test for whether an entity has been
2421 locally emitted, inlined or otherwise not-really-extern, but
2422 for declarations that can be weak, it happens to be
2423 match. */
2424 && !TREE_STATIC (decl)
2425 && lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
2426 && value_member (decl, weak_decls) == NULL_TREE)
2427 weak_decls = tree_cons (NULL, decl, weak_decls);
2429 #ifdef ASM_OUTPUT_EXTERNAL
2430 if (pending_assemble_externals_processed)
2432 assemble_external_real (decl);
2433 return;
2436 if (! pending_assemble_externals_set->add (decl))
2437 pending_assemble_externals = tree_cons (NULL, decl,
2438 pending_assemble_externals);
2439 #endif
2442 /* Similar, for calling a library function FUN. */
2444 void
2445 assemble_external_libcall (rtx fun)
2447 /* Declare library function name external when first used, if nec. */
2448 if (! SYMBOL_REF_USED (fun))
2450 SYMBOL_REF_USED (fun) = 1;
2451 targetm.asm_out.external_libcall (fun);
2455 /* Assemble a label named NAME. */
2457 void
2458 assemble_label (FILE *file, const char *name)
2460 ASM_OUTPUT_LABEL (file, name);
2463 /* Set the symbol_referenced flag for ID. */
2464 void
2465 mark_referenced (tree id)
2467 TREE_SYMBOL_REFERENCED (id) = 1;
2470 /* Set the symbol_referenced flag for DECL and notify callgraph. */
2471 void
2472 mark_decl_referenced (tree decl)
2474 if (TREE_CODE (decl) == FUNCTION_DECL)
2476 /* Extern inline functions don't become needed when referenced.
2477 If we know a method will be emitted in other TU and no new
2478 functions can be marked reachable, just use the external
2479 definition. */
2480 struct cgraph_node *node = cgraph_node::get_create (decl);
2481 if (!DECL_EXTERNAL (decl)
2482 && !node->definition)
2483 node->mark_force_output ();
2485 else if (TREE_CODE (decl) == VAR_DECL)
2487 varpool_node *node = varpool_node::get_create (decl);
2488 /* C++ frontend use mark_decl_references to force COMDAT variables
2489 to be output that might appear dead otherwise. */
2490 node->force_output = true;
2492 /* else do nothing - we can get various sorts of CST nodes here,
2493 which do not need to be marked. */
2497 /* Output to FILE (an assembly file) a reference to NAME. If NAME
2498 starts with a *, the rest of NAME is output verbatim. Otherwise
2499 NAME is transformed in a target-specific way (usually by the
2500 addition of an underscore). */
2502 void
2503 assemble_name_raw (FILE *file, const char *name)
2505 if (name[0] == '*')
2506 fputs (&name[1], file);
2507 else
2508 ASM_OUTPUT_LABELREF (file, name);
2511 /* Like assemble_name_raw, but should be used when NAME might refer to
2512 an entity that is also represented as a tree (like a function or
2513 variable). If NAME does refer to such an entity, that entity will
2514 be marked as referenced. */
2516 void
2517 assemble_name (FILE *file, const char *name)
2519 const char *real_name;
2520 tree id;
2522 real_name = targetm.strip_name_encoding (name);
2524 id = maybe_get_identifier (real_name);
2525 if (id)
2527 tree id_orig = id;
2529 mark_referenced (id);
2530 ultimate_transparent_alias_target (&id);
2531 if (id != id_orig)
2532 name = IDENTIFIER_POINTER (id);
2533 gcc_assert (! TREE_CHAIN (id));
2536 assemble_name_raw (file, name);
2539 /* Allocate SIZE bytes writable static space with a gensym name
2540 and return an RTX to refer to its address. */
2543 assemble_static_space (unsigned HOST_WIDE_INT size)
2545 char name[12];
2546 const char *namestring;
2547 rtx x;
2549 ASM_GENERATE_INTERNAL_LABEL (name, "LF", const_labelno);
2550 ++const_labelno;
2551 namestring = ggc_strdup (name);
2553 x = gen_rtx_SYMBOL_REF (Pmode, namestring);
2554 SYMBOL_REF_FLAGS (x) = SYMBOL_FLAG_LOCAL;
2556 #ifdef ASM_OUTPUT_ALIGNED_DECL_LOCAL
2557 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, NULL_TREE, name, size,
2558 BIGGEST_ALIGNMENT);
2559 #else
2560 #ifdef ASM_OUTPUT_ALIGNED_LOCAL
2561 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, BIGGEST_ALIGNMENT);
2562 #else
2564 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
2565 so that each uninitialized object starts on such a boundary. */
2566 /* Variable `rounded' might or might not be used in ASM_OUTPUT_LOCAL. */
2567 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED
2568 = ((size + (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1)
2569 / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
2570 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
2571 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
2573 #endif
2574 #endif
2575 return x;
2578 /* Assemble the static constant template for function entry trampolines.
2579 This is done at most once per compilation.
2580 Returns an RTX for the address of the template. */
2582 static GTY(()) rtx initial_trampoline;
2585 assemble_trampoline_template (void)
2587 char label[256];
2588 const char *name;
2589 int align;
2590 rtx symbol;
2592 gcc_assert (targetm.asm_out.trampoline_template != NULL);
2594 if (initial_trampoline)
2595 return initial_trampoline;
2597 /* By default, put trampoline templates in read-only data section. */
2599 #ifdef TRAMPOLINE_SECTION
2600 switch_to_section (TRAMPOLINE_SECTION);
2601 #else
2602 switch_to_section (readonly_data_section);
2603 #endif
2605 /* Write the assembler code to define one. */
2606 align = floor_log2 (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
2607 if (align > 0)
2608 ASM_OUTPUT_ALIGN (asm_out_file, align);
2610 targetm.asm_out.internal_label (asm_out_file, "LTRAMP", 0);
2611 targetm.asm_out.trampoline_template (asm_out_file);
2613 /* Record the rtl to refer to it. */
2614 ASM_GENERATE_INTERNAL_LABEL (label, "LTRAMP", 0);
2615 name = ggc_strdup (label);
2616 symbol = gen_rtx_SYMBOL_REF (Pmode, name);
2617 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_LOCAL;
2619 initial_trampoline = gen_const_mem (BLKmode, symbol);
2620 set_mem_align (initial_trampoline, TRAMPOLINE_ALIGNMENT);
2621 set_mem_size (initial_trampoline, TRAMPOLINE_SIZE);
2623 return initial_trampoline;
2626 /* A and B are either alignments or offsets. Return the minimum alignment
2627 that may be assumed after adding the two together. */
2629 static inline unsigned
2630 min_align (unsigned int a, unsigned int b)
2632 return (a | b) & -(a | b);
2635 /* Return the assembler directive for creating a given kind of integer
2636 object. SIZE is the number of bytes in the object and ALIGNED_P
2637 indicates whether it is known to be aligned. Return NULL if the
2638 assembly dialect has no such directive.
2640 The returned string should be printed at the start of a new line and
2641 be followed immediately by the object's initial value. */
2643 const char *
2644 integer_asm_op (int size, int aligned_p)
2646 struct asm_int_op *ops;
2648 if (aligned_p)
2649 ops = &targetm.asm_out.aligned_op;
2650 else
2651 ops = &targetm.asm_out.unaligned_op;
2653 switch (size)
2655 case 1:
2656 return targetm.asm_out.byte_op;
2657 case 2:
2658 return ops->hi;
2659 case 4:
2660 return ops->si;
2661 case 8:
2662 return ops->di;
2663 case 16:
2664 return ops->ti;
2665 default:
2666 return NULL;
2670 /* Use directive OP to assemble an integer object X. Print OP at the
2671 start of the line, followed immediately by the value of X. */
2673 void
2674 assemble_integer_with_op (const char *op, rtx x)
2676 fputs (op, asm_out_file);
2677 output_addr_const (asm_out_file, x);
2678 fputc ('\n', asm_out_file);
2681 /* The default implementation of the asm_out.integer target hook. */
2683 bool
2684 default_assemble_integer (rtx x ATTRIBUTE_UNUSED,
2685 unsigned int size ATTRIBUTE_UNUSED,
2686 int aligned_p ATTRIBUTE_UNUSED)
2688 const char *op = integer_asm_op (size, aligned_p);
2689 /* Avoid GAS bugs for large values. Specifically negative values whose
2690 absolute value fits in a bfd_vma, but not in a bfd_signed_vma. */
2691 if (size > UNITS_PER_WORD && size > POINTER_SIZE_UNITS)
2692 return false;
2693 return op && (assemble_integer_with_op (op, x), true);
2696 /* Assemble the integer constant X into an object of SIZE bytes. ALIGN is
2697 the alignment of the integer in bits. Return 1 if we were able to output
2698 the constant, otherwise 0. We must be able to output the constant,
2699 if FORCE is nonzero. */
2701 bool
2702 assemble_integer (rtx x, unsigned int size, unsigned int align, int force)
2704 int aligned_p;
2706 aligned_p = (align >= MIN (size * BITS_PER_UNIT, BIGGEST_ALIGNMENT));
2708 /* See if the target hook can handle this kind of object. */
2709 if (targetm.asm_out.integer (x, size, aligned_p))
2710 return true;
2712 /* If the object is a multi-byte one, try splitting it up. Split
2713 it into words it if is multi-word, otherwise split it into bytes. */
2714 if (size > 1)
2716 machine_mode omode, imode;
2717 unsigned int subalign;
2718 unsigned int subsize, i;
2719 enum mode_class mclass;
2721 subsize = size > UNITS_PER_WORD? UNITS_PER_WORD : 1;
2722 subalign = MIN (align, subsize * BITS_PER_UNIT);
2723 if (GET_CODE (x) == CONST_FIXED)
2724 mclass = GET_MODE_CLASS (GET_MODE (x));
2725 else
2726 mclass = MODE_INT;
2728 omode = mode_for_size (subsize * BITS_PER_UNIT, mclass, 0);
2729 imode = mode_for_size (size * BITS_PER_UNIT, mclass, 0);
2731 for (i = 0; i < size; i += subsize)
2733 rtx partial = simplify_subreg (omode, x, imode, i);
2734 if (!partial || !assemble_integer (partial, subsize, subalign, 0))
2735 break;
2737 if (i == size)
2738 return true;
2740 /* If we've printed some of it, but not all of it, there's no going
2741 back now. */
2742 gcc_assert (!i);
2745 gcc_assert (!force);
2747 return false;
2750 void
2751 assemble_real (REAL_VALUE_TYPE d, machine_mode mode, unsigned int align)
2753 long data[4] = {0, 0, 0, 0};
2754 int i;
2755 int bitsize, nelts, nunits, units_per;
2757 /* This is hairy. We have a quantity of known size. real_to_target
2758 will put it into an array of *host* longs, 32 bits per element
2759 (even if long is more than 32 bits). We need to determine the
2760 number of array elements that are occupied (nelts) and the number
2761 of *target* min-addressable units that will be occupied in the
2762 object file (nunits). We cannot assume that 32 divides the
2763 mode's bitsize (size * BITS_PER_UNIT) evenly.
2765 size * BITS_PER_UNIT is used here to make sure that padding bits
2766 (which might appear at either end of the value; real_to_target
2767 will include the padding bits in its output array) are included. */
2769 nunits = GET_MODE_SIZE (mode);
2770 bitsize = nunits * BITS_PER_UNIT;
2771 nelts = CEIL (bitsize, 32);
2772 units_per = 32 / BITS_PER_UNIT;
2774 real_to_target (data, &d, mode);
2776 /* Put out the first word with the specified alignment. */
2777 assemble_integer (GEN_INT (data[0]), MIN (nunits, units_per), align, 1);
2778 nunits -= units_per;
2780 /* Subsequent words need only 32-bit alignment. */
2781 align = min_align (align, 32);
2783 for (i = 1; i < nelts; i++)
2785 assemble_integer (GEN_INT (data[i]), MIN (nunits, units_per), align, 1);
2786 nunits -= units_per;
2790 /* Given an expression EXP with a constant value,
2791 reduce it to the sum of an assembler symbol and an integer.
2792 Store them both in the structure *VALUE.
2793 EXP must be reducible. */
2795 struct addr_const {
2796 rtx base;
2797 HOST_WIDE_INT offset;
2800 static void
2801 decode_addr_const (tree exp, struct addr_const *value)
2803 tree target = TREE_OPERAND (exp, 0);
2804 int offset = 0;
2805 rtx x;
2807 while (1)
2809 if (TREE_CODE (target) == COMPONENT_REF
2810 && tree_fits_shwi_p (byte_position (TREE_OPERAND (target, 1))))
2812 offset += int_byte_position (TREE_OPERAND (target, 1));
2813 target = TREE_OPERAND (target, 0);
2815 else if (TREE_CODE (target) == ARRAY_REF
2816 || TREE_CODE (target) == ARRAY_RANGE_REF)
2818 offset += (tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (target)))
2819 * tree_to_shwi (TREE_OPERAND (target, 1)));
2820 target = TREE_OPERAND (target, 0);
2822 else if (TREE_CODE (target) == MEM_REF
2823 && TREE_CODE (TREE_OPERAND (target, 0)) == ADDR_EXPR)
2825 offset += mem_ref_offset (target).to_short_addr ();
2826 target = TREE_OPERAND (TREE_OPERAND (target, 0), 0);
2828 else if (TREE_CODE (target) == INDIRECT_REF
2829 && TREE_CODE (TREE_OPERAND (target, 0)) == NOP_EXPR
2830 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (target, 0), 0))
2831 == ADDR_EXPR)
2832 target = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (target, 0), 0), 0);
2833 else
2834 break;
2837 switch (TREE_CODE (target))
2839 case VAR_DECL:
2840 case FUNCTION_DECL:
2841 x = DECL_RTL (target);
2842 break;
2844 case LABEL_DECL:
2845 x = gen_rtx_MEM (FUNCTION_MODE,
2846 gen_rtx_LABEL_REF (Pmode, force_label_rtx (target)));
2847 break;
2849 case REAL_CST:
2850 case FIXED_CST:
2851 case STRING_CST:
2852 case COMPLEX_CST:
2853 case CONSTRUCTOR:
2854 case INTEGER_CST:
2855 x = output_constant_def (target, 1);
2856 break;
2858 default:
2859 gcc_unreachable ();
2862 gcc_assert (MEM_P (x));
2863 x = XEXP (x, 0);
2865 value->base = x;
2866 value->offset = offset;
2869 static GTY(()) hash_table<tree_descriptor_hasher> *const_desc_htab;
2871 static void maybe_output_constant_def_contents (struct constant_descriptor_tree *, int);
2873 /* Constant pool accessor function. */
2875 hash_table<tree_descriptor_hasher> *
2876 constant_pool_htab (void)
2878 return const_desc_htab;
2881 /* Compute a hash code for a constant expression. */
2883 hashval_t
2884 tree_descriptor_hasher::hash (constant_descriptor_tree *ptr)
2886 return ptr->hash;
2889 static hashval_t
2890 const_hash_1 (const tree exp)
2892 const char *p;
2893 hashval_t hi;
2894 int len, i;
2895 enum tree_code code = TREE_CODE (exp);
2897 /* Either set P and LEN to the address and len of something to hash and
2898 exit the switch or return a value. */
2900 switch (code)
2902 case INTEGER_CST:
2903 p = (char *) &TREE_INT_CST_ELT (exp, 0);
2904 len = TREE_INT_CST_NUNITS (exp) * sizeof (HOST_WIDE_INT);
2905 break;
2907 case REAL_CST:
2908 return real_hash (TREE_REAL_CST_PTR (exp));
2910 case FIXED_CST:
2911 return fixed_hash (TREE_FIXED_CST_PTR (exp));
2913 case STRING_CST:
2914 p = TREE_STRING_POINTER (exp);
2915 len = TREE_STRING_LENGTH (exp);
2916 break;
2918 case COMPLEX_CST:
2919 return (const_hash_1 (TREE_REALPART (exp)) * 5
2920 + const_hash_1 (TREE_IMAGPART (exp)));
2922 case VECTOR_CST:
2924 unsigned i;
2926 hi = 7 + VECTOR_CST_NELTS (exp);
2928 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
2929 hi = hi * 563 + const_hash_1 (VECTOR_CST_ELT (exp, i));
2931 return hi;
2934 case CONSTRUCTOR:
2936 unsigned HOST_WIDE_INT idx;
2937 tree value;
2939 hi = 5 + int_size_in_bytes (TREE_TYPE (exp));
2941 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
2942 if (value)
2943 hi = hi * 603 + const_hash_1 (value);
2945 return hi;
2948 case ADDR_EXPR:
2949 case FDESC_EXPR:
2951 struct addr_const value;
2953 decode_addr_const (exp, &value);
2954 switch (GET_CODE (value.base))
2956 case SYMBOL_REF:
2957 /* Don't hash the address of the SYMBOL_REF;
2958 only use the offset and the symbol name. */
2959 hi = value.offset;
2960 p = XSTR (value.base, 0);
2961 for (i = 0; p[i] != 0; i++)
2962 hi = ((hi * 613) + (unsigned) (p[i]));
2963 break;
2965 case LABEL_REF:
2966 hi = (value.offset
2967 + CODE_LABEL_NUMBER (LABEL_REF_LABEL (value.base)) * 13);
2968 break;
2970 default:
2971 gcc_unreachable ();
2974 return hi;
2976 case PLUS_EXPR:
2977 case POINTER_PLUS_EXPR:
2978 case MINUS_EXPR:
2979 return (const_hash_1 (TREE_OPERAND (exp, 0)) * 9
2980 + const_hash_1 (TREE_OPERAND (exp, 1)));
2982 CASE_CONVERT:
2983 return const_hash_1 (TREE_OPERAND (exp, 0)) * 7 + 2;
2985 default:
2986 /* A language specific constant. Just hash the code. */
2987 return code;
2990 /* Compute hashing function. */
2991 hi = len;
2992 for (i = 0; i < len; i++)
2993 hi = ((hi * 613) + (unsigned) (p[i]));
2995 return hi;
2998 /* Wrapper of compare_constant, for the htab interface. */
2999 bool
3000 tree_descriptor_hasher::equal (constant_descriptor_tree *c1,
3001 constant_descriptor_tree *c2)
3003 if (c1->hash != c2->hash)
3004 return 0;
3005 return compare_constant (c1->value, c2->value);
3008 /* Compare t1 and t2, and return 1 only if they are known to result in
3009 the same bit pattern on output. */
3011 static int
3012 compare_constant (const tree t1, const tree t2)
3014 enum tree_code typecode;
3016 if (t1 == NULL_TREE)
3017 return t2 == NULL_TREE;
3018 if (t2 == NULL_TREE)
3019 return 0;
3021 if (TREE_CODE (t1) != TREE_CODE (t2))
3022 return 0;
3024 switch (TREE_CODE (t1))
3026 case INTEGER_CST:
3027 /* Integer constants are the same only if the same width of type. */
3028 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3029 return 0;
3030 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
3031 return 0;
3032 return tree_int_cst_equal (t1, t2);
3034 case REAL_CST:
3035 /* Real constants are the same only if the same width of type. */
3036 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3037 return 0;
3039 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
3041 case FIXED_CST:
3042 /* Fixed constants are the same only if the same width of type. */
3043 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3044 return 0;
3046 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
3048 case STRING_CST:
3049 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
3050 return 0;
3052 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
3053 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
3054 TREE_STRING_LENGTH (t1)));
3056 case COMPLEX_CST:
3057 return (compare_constant (TREE_REALPART (t1), TREE_REALPART (t2))
3058 && compare_constant (TREE_IMAGPART (t1), TREE_IMAGPART (t2)));
3060 case VECTOR_CST:
3062 unsigned i;
3064 if (VECTOR_CST_NELTS (t1) != VECTOR_CST_NELTS (t2))
3065 return 0;
3067 for (i = 0; i < VECTOR_CST_NELTS (t1); ++i)
3068 if (!compare_constant (VECTOR_CST_ELT (t1, i),
3069 VECTOR_CST_ELT (t2, i)))
3070 return 0;
3072 return 1;
3075 case CONSTRUCTOR:
3077 vec<constructor_elt, va_gc> *v1, *v2;
3078 unsigned HOST_WIDE_INT idx;
3080 typecode = TREE_CODE (TREE_TYPE (t1));
3081 if (typecode != TREE_CODE (TREE_TYPE (t2)))
3082 return 0;
3084 if (typecode == ARRAY_TYPE)
3086 HOST_WIDE_INT size_1 = int_size_in_bytes (TREE_TYPE (t1));
3087 /* For arrays, check that the sizes all match. */
3088 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))
3089 || size_1 == -1
3090 || size_1 != int_size_in_bytes (TREE_TYPE (t2)))
3091 return 0;
3093 else
3095 /* For record and union constructors, require exact type
3096 equality. */
3097 if (TREE_TYPE (t1) != TREE_TYPE (t2))
3098 return 0;
3101 v1 = CONSTRUCTOR_ELTS (t1);
3102 v2 = CONSTRUCTOR_ELTS (t2);
3103 if (vec_safe_length (v1) != vec_safe_length (v2))
3104 return 0;
3106 for (idx = 0; idx < vec_safe_length (v1); ++idx)
3108 constructor_elt *c1 = &(*v1)[idx];
3109 constructor_elt *c2 = &(*v2)[idx];
3111 /* Check that each value is the same... */
3112 if (!compare_constant (c1->value, c2->value))
3113 return 0;
3114 /* ... and that they apply to the same fields! */
3115 if (typecode == ARRAY_TYPE)
3117 if (!compare_constant (c1->index, c2->index))
3118 return 0;
3120 else
3122 if (c1->index != c2->index)
3123 return 0;
3127 return 1;
3130 case ADDR_EXPR:
3131 case FDESC_EXPR:
3133 struct addr_const value1, value2;
3134 enum rtx_code code;
3135 int ret;
3137 decode_addr_const (t1, &value1);
3138 decode_addr_const (t2, &value2);
3140 if (value1.offset != value2.offset)
3141 return 0;
3143 code = GET_CODE (value1.base);
3144 if (code != GET_CODE (value2.base))
3145 return 0;
3147 switch (code)
3149 case SYMBOL_REF:
3150 ret = (strcmp (XSTR (value1.base, 0), XSTR (value2.base, 0)) == 0);
3151 break;
3153 case LABEL_REF:
3154 ret = (CODE_LABEL_NUMBER (LABEL_REF_LABEL (value1.base))
3155 == CODE_LABEL_NUMBER (LABEL_REF_LABEL (value2.base)));
3156 break;
3158 default:
3159 gcc_unreachable ();
3161 return ret;
3164 case PLUS_EXPR:
3165 case POINTER_PLUS_EXPR:
3166 case MINUS_EXPR:
3167 case RANGE_EXPR:
3168 return (compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
3169 && compare_constant (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
3171 CASE_CONVERT:
3172 case VIEW_CONVERT_EXPR:
3173 return compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3175 default:
3176 return 0;
3179 gcc_unreachable ();
3182 /* Return the section into which constant EXP should be placed. */
3184 static section *
3185 get_constant_section (tree exp, unsigned int align)
3187 return targetm.asm_out.select_section (exp,
3188 compute_reloc_for_constant (exp),
3189 align);
3192 /* Return the size of constant EXP in bytes. */
3194 static HOST_WIDE_INT
3195 get_constant_size (tree exp)
3197 HOST_WIDE_INT size;
3199 size = int_size_in_bytes (TREE_TYPE (exp));
3200 if (TREE_CODE (exp) == STRING_CST)
3201 size = MAX (TREE_STRING_LENGTH (exp), size);
3202 return size;
3205 /* Subroutine of output_constant_def:
3206 No constant equal to EXP is known to have been output.
3207 Make a constant descriptor to enter EXP in the hash table.
3208 Assign the label number and construct RTL to refer to the
3209 constant's location in memory.
3210 Caller is responsible for updating the hash table. */
3212 static struct constant_descriptor_tree *
3213 build_constant_desc (tree exp)
3215 struct constant_descriptor_tree *desc;
3216 rtx symbol, rtl;
3217 char label[256];
3218 int labelno;
3219 tree decl;
3221 desc = ggc_alloc<constant_descriptor_tree> ();
3222 desc->value = exp;
3224 /* Create a string containing the label name, in LABEL. */
3225 labelno = const_labelno++;
3226 ASM_GENERATE_INTERNAL_LABEL (label, "LC", labelno);
3228 /* Construct the VAR_DECL associated with the constant. */
3229 decl = build_decl (UNKNOWN_LOCATION, VAR_DECL, get_identifier (label),
3230 TREE_TYPE (exp));
3231 DECL_ARTIFICIAL (decl) = 1;
3232 DECL_IGNORED_P (decl) = 1;
3233 TREE_READONLY (decl) = 1;
3234 TREE_STATIC (decl) = 1;
3235 TREE_ADDRESSABLE (decl) = 1;
3236 /* We don't set the RTL yet as this would cause varpool to assume that the
3237 variable is referenced. Moreover, it would just be dropped in LTO mode.
3238 Instead we set the flag that will be recognized in make_decl_rtl. */
3239 DECL_IN_CONSTANT_POOL (decl) = 1;
3240 DECL_INITIAL (decl) = desc->value;
3241 /* ??? CONSTANT_ALIGNMENT hasn't been updated for vector types on most
3242 architectures so use DATA_ALIGNMENT as well, except for strings. */
3243 if (TREE_CODE (exp) == STRING_CST)
3245 #ifdef CONSTANT_ALIGNMENT
3246 DECL_ALIGN (decl) = CONSTANT_ALIGNMENT (exp, DECL_ALIGN (decl));
3247 #endif
3249 else
3250 align_variable (decl, 0);
3252 /* Now construct the SYMBOL_REF and the MEM. */
3253 if (use_object_blocks_p ())
3255 section *sect = get_constant_section (exp, DECL_ALIGN (decl));
3256 symbol = create_block_symbol (ggc_strdup (label),
3257 get_block_for_section (sect), -1);
3259 else
3260 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3261 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3262 SET_SYMBOL_REF_DECL (symbol, decl);
3263 TREE_CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3265 rtl = gen_const_mem (TYPE_MODE (TREE_TYPE (exp)), symbol);
3266 set_mem_attributes (rtl, exp, 1);
3267 set_mem_alias_set (rtl, 0);
3269 /* We cannot share RTX'es in pool entries.
3270 Mark this piece of RTL as required for unsharing. */
3271 RTX_FLAG (rtl, used) = 1;
3273 /* Set flags or add text to the name to record information, such as
3274 that it is a local symbol. If the name is changed, the macro
3275 ASM_OUTPUT_LABELREF will have to know how to strip this
3276 information. This call might invalidate our local variable
3277 SYMBOL; we can't use it afterward. */
3278 targetm.encode_section_info (exp, rtl, true);
3280 desc->rtl = rtl;
3282 return desc;
3285 /* Return an rtx representing a reference to constant data in memory
3286 for the constant expression EXP.
3288 If assembler code for such a constant has already been output,
3289 return an rtx to refer to it.
3290 Otherwise, output such a constant in memory
3291 and generate an rtx for it.
3293 If DEFER is nonzero, this constant can be deferred and output only
3294 if referenced in the function after all optimizations.
3296 `const_desc_table' records which constants already have label strings. */
3299 output_constant_def (tree exp, int defer)
3301 struct constant_descriptor_tree *desc;
3302 struct constant_descriptor_tree key;
3304 /* Look up EXP in the table of constant descriptors. If we didn't find
3305 it, create a new one. */
3306 key.value = exp;
3307 key.hash = const_hash_1 (exp);
3308 constant_descriptor_tree **loc
3309 = const_desc_htab->find_slot_with_hash (&key, key.hash, INSERT);
3311 desc = *loc;
3312 if (desc == 0)
3314 desc = build_constant_desc (exp);
3315 desc->hash = key.hash;
3316 *loc = desc;
3319 maybe_output_constant_def_contents (desc, defer);
3320 return desc->rtl;
3323 /* Subroutine of output_constant_def: Decide whether or not we need to
3324 output the constant DESC now, and if so, do it. */
3325 static void
3326 maybe_output_constant_def_contents (struct constant_descriptor_tree *desc,
3327 int defer)
3329 rtx symbol = XEXP (desc->rtl, 0);
3330 tree exp = desc->value;
3332 if (flag_syntax_only)
3333 return;
3335 if (TREE_ASM_WRITTEN (exp))
3336 /* Already output; don't do it again. */
3337 return;
3339 /* We can always defer constants as long as the context allows
3340 doing so. */
3341 if (defer)
3343 /* Increment n_deferred_constants if it exists. It needs to be at
3344 least as large as the number of constants actually referred to
3345 by the function. If it's too small we'll stop looking too early
3346 and fail to emit constants; if it's too large we'll only look
3347 through the entire function when we could have stopped earlier. */
3348 if (cfun)
3349 n_deferred_constants++;
3350 return;
3353 output_constant_def_contents (symbol);
3356 /* Subroutine of output_constant_def_contents. Output the definition
3357 of constant EXP, which is pointed to by label LABEL. ALIGN is the
3358 constant's alignment in bits. */
3360 static void
3361 assemble_constant_contents (tree exp, const char *label, unsigned int align)
3363 HOST_WIDE_INT size;
3365 size = get_constant_size (exp);
3367 /* Do any machine/system dependent processing of the constant. */
3368 targetm.asm_out.declare_constant_name (asm_out_file, label, exp, size);
3370 /* Output the value of EXP. */
3371 output_constant (exp, size, align);
3373 targetm.asm_out.decl_end ();
3376 /* We must output the constant data referred to by SYMBOL; do so. */
3378 static void
3379 output_constant_def_contents (rtx symbol)
3381 tree decl = SYMBOL_REF_DECL (symbol);
3382 tree exp = DECL_INITIAL (decl);
3383 unsigned int align;
3384 bool asan_protected = false;
3386 /* Make sure any other constants whose addresses appear in EXP
3387 are assigned label numbers. */
3388 output_addressed_constants (exp);
3390 /* We are no longer deferring this constant. */
3391 TREE_ASM_WRITTEN (decl) = TREE_ASM_WRITTEN (exp) = 1;
3393 if ((flag_sanitize & SANITIZE_ADDRESS)
3394 && TREE_CODE (exp) == STRING_CST
3395 && asan_protect_global (exp))
3397 asan_protected = true;
3398 DECL_ALIGN (decl) = MAX (DECL_ALIGN (decl),
3399 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
3402 /* If the constant is part of an object block, make sure that the
3403 decl has been positioned within its block, but do not write out
3404 its definition yet. output_object_blocks will do that later. */
3405 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
3406 place_block_symbol (symbol);
3407 else
3409 align = DECL_ALIGN (decl);
3410 switch_to_section (get_constant_section (exp, align));
3411 if (align > BITS_PER_UNIT)
3412 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
3413 assemble_constant_contents (exp, XSTR (symbol, 0), align);
3414 if (asan_protected)
3416 HOST_WIDE_INT size = get_constant_size (exp);
3417 assemble_zeros (asan_red_zone_size (size));
3422 /* Look up EXP in the table of constant descriptors. Return the rtl
3423 if it has been emitted, else null. */
3426 lookup_constant_def (tree exp)
3428 struct constant_descriptor_tree key;
3430 key.value = exp;
3431 key.hash = const_hash_1 (exp);
3432 constant_descriptor_tree *desc
3433 = const_desc_htab->find_with_hash (&key, key.hash);
3435 return (desc ? desc->rtl : NULL_RTX);
3438 /* Return a tree representing a reference to constant data in memory
3439 for the constant expression EXP.
3441 This is the counterpart of output_constant_def at the Tree level. */
3443 tree
3444 tree_output_constant_def (tree exp)
3446 struct constant_descriptor_tree *desc, key;
3447 tree decl;
3449 /* Look up EXP in the table of constant descriptors. If we didn't find
3450 it, create a new one. */
3451 key.value = exp;
3452 key.hash = const_hash_1 (exp);
3453 constant_descriptor_tree **loc
3454 = const_desc_htab->find_slot_with_hash (&key, key.hash, INSERT);
3456 desc = *loc;
3457 if (desc == 0)
3459 desc = build_constant_desc (exp);
3460 desc->hash = key.hash;
3461 *loc = desc;
3464 decl = SYMBOL_REF_DECL (XEXP (desc->rtl, 0));
3465 varpool_node::finalize_decl (decl);
3466 return decl;
3469 struct GTY((chain_next ("%h.next"), for_user)) constant_descriptor_rtx {
3470 struct constant_descriptor_rtx *next;
3471 rtx mem;
3472 rtx sym;
3473 rtx constant;
3474 HOST_WIDE_INT offset;
3475 hashval_t hash;
3476 machine_mode mode;
3477 unsigned int align;
3478 int labelno;
3479 int mark;
3482 struct const_rtx_desc_hasher : ggc_hasher<constant_descriptor_rtx *>
3484 static hashval_t hash (constant_descriptor_rtx *);
3485 static bool equal (constant_descriptor_rtx *, constant_descriptor_rtx *);
3488 /* Used in the hash tables to avoid outputting the same constant
3489 twice. Unlike 'struct constant_descriptor_tree', RTX constants
3490 are output once per function, not once per file. */
3491 /* ??? Only a few targets need per-function constant pools. Most
3492 can use one per-file pool. Should add a targetm bit to tell the
3493 difference. */
3495 struct GTY(()) rtx_constant_pool {
3496 /* Pointers to first and last constant in pool, as ordered by offset. */
3497 struct constant_descriptor_rtx *first;
3498 struct constant_descriptor_rtx *last;
3500 /* Hash facility for making memory-constants from constant rtl-expressions.
3501 It is used on RISC machines where immediate integer arguments and
3502 constant addresses are restricted so that such constants must be stored
3503 in memory. */
3504 hash_table<const_rtx_desc_hasher> *const_rtx_htab;
3506 /* Current offset in constant pool (does not include any
3507 machine-specific header). */
3508 HOST_WIDE_INT offset;
3511 /* Hash and compare functions for const_rtx_htab. */
3513 hashval_t
3514 const_rtx_desc_hasher::hash (constant_descriptor_rtx *desc)
3516 return desc->hash;
3519 bool
3520 const_rtx_desc_hasher::equal (constant_descriptor_rtx *x,
3521 constant_descriptor_rtx *y)
3523 if (x->mode != y->mode)
3524 return 0;
3525 return rtx_equal_p (x->constant, y->constant);
3528 /* Hash one component of a constant. */
3530 static hashval_t
3531 const_rtx_hash_1 (const_rtx x)
3533 unsigned HOST_WIDE_INT hwi;
3534 machine_mode mode;
3535 enum rtx_code code;
3536 hashval_t h;
3537 int i;
3539 code = GET_CODE (x);
3540 mode = GET_MODE (x);
3541 h = (hashval_t) code * 1048573 + mode;
3543 switch (code)
3545 case CONST_INT:
3546 hwi = INTVAL (x);
3548 fold_hwi:
3550 int shift = sizeof (hashval_t) * CHAR_BIT;
3551 const int n = sizeof (HOST_WIDE_INT) / sizeof (hashval_t);
3553 h ^= (hashval_t) hwi;
3554 for (i = 1; i < n; ++i)
3556 hwi >>= shift;
3557 h ^= (hashval_t) hwi;
3560 break;
3562 case CONST_WIDE_INT:
3563 hwi = GET_MODE_PRECISION (mode);
3565 for (i = 0; i < CONST_WIDE_INT_NUNITS (x); i++)
3566 hwi ^= CONST_WIDE_INT_ELT (x, i);
3567 goto fold_hwi;
3570 case CONST_DOUBLE:
3571 if (TARGET_SUPPORTS_WIDE_INT == 0 && mode == VOIDmode)
3573 hwi = CONST_DOUBLE_LOW (x) ^ CONST_DOUBLE_HIGH (x);
3574 goto fold_hwi;
3576 else
3577 h ^= real_hash (CONST_DOUBLE_REAL_VALUE (x));
3578 break;
3580 case CONST_FIXED:
3581 h ^= fixed_hash (CONST_FIXED_VALUE (x));
3582 break;
3584 case SYMBOL_REF:
3585 h ^= htab_hash_string (XSTR (x, 0));
3586 break;
3588 case LABEL_REF:
3589 h = h * 251 + CODE_LABEL_NUMBER (LABEL_REF_LABEL (x));
3590 break;
3592 case UNSPEC:
3593 case UNSPEC_VOLATILE:
3594 h = h * 251 + XINT (x, 1);
3595 break;
3597 default:
3598 break;
3601 return h;
3604 /* Compute a hash value for X, which should be a constant. */
3606 static hashval_t
3607 const_rtx_hash (rtx x)
3609 hashval_t h = 0;
3610 subrtx_iterator::array_type array;
3611 FOR_EACH_SUBRTX (iter, array, x, ALL)
3612 h = h * 509 + const_rtx_hash_1 (*iter);
3613 return h;
3617 /* Create and return a new rtx constant pool. */
3619 static struct rtx_constant_pool *
3620 create_constant_pool (void)
3622 struct rtx_constant_pool *pool;
3624 pool = ggc_alloc<rtx_constant_pool> ();
3625 pool->const_rtx_htab = hash_table<const_rtx_desc_hasher>::create_ggc (31);
3626 pool->first = NULL;
3627 pool->last = NULL;
3628 pool->offset = 0;
3629 return pool;
3632 /* Initialize constant pool hashing for a new function. */
3634 void
3635 init_varasm_status (void)
3637 crtl->varasm.pool = create_constant_pool ();
3638 crtl->varasm.deferred_constants = 0;
3641 /* Given a MINUS expression, simplify it if both sides
3642 include the same symbol. */
3645 simplify_subtraction (rtx x)
3647 rtx r = simplify_rtx (x);
3648 return r ? r : x;
3651 /* Given a constant rtx X, make (or find) a memory constant for its value
3652 and return a MEM rtx to refer to it in memory. */
3655 force_const_mem (machine_mode mode, rtx x)
3657 struct constant_descriptor_rtx *desc, tmp;
3658 struct rtx_constant_pool *pool;
3659 char label[256];
3660 rtx def, symbol;
3661 hashval_t hash;
3662 unsigned int align;
3663 constant_descriptor_rtx **slot;
3665 /* If we're not allowed to drop X into the constant pool, don't. */
3666 if (targetm.cannot_force_const_mem (mode, x))
3667 return NULL_RTX;
3669 /* Record that this function has used a constant pool entry. */
3670 crtl->uses_const_pool = 1;
3672 /* Decide which pool to use. */
3673 pool = (targetm.use_blocks_for_constant_p (mode, x)
3674 ? shared_constant_pool
3675 : crtl->varasm.pool);
3677 /* Lookup the value in the hashtable. */
3678 tmp.constant = x;
3679 tmp.mode = mode;
3680 hash = const_rtx_hash (x);
3681 slot = pool->const_rtx_htab->find_slot_with_hash (&tmp, hash, INSERT);
3682 desc = *slot;
3684 /* If the constant was already present, return its memory. */
3685 if (desc)
3686 return copy_rtx (desc->mem);
3688 /* Otherwise, create a new descriptor. */
3689 desc = ggc_alloc<constant_descriptor_rtx> ();
3690 *slot = desc;
3692 /* Align the location counter as required by EXP's data type. */
3693 align = GET_MODE_ALIGNMENT (mode == VOIDmode ? word_mode : mode);
3694 #ifdef CONSTANT_ALIGNMENT
3696 tree type = lang_hooks.types.type_for_mode (mode, 0);
3697 if (type != NULL_TREE)
3698 align = CONSTANT_ALIGNMENT (make_tree (type, x), align);
3700 #endif
3702 pool->offset += (align / BITS_PER_UNIT) - 1;
3703 pool->offset &= ~ ((align / BITS_PER_UNIT) - 1);
3705 desc->next = NULL;
3706 desc->constant = copy_rtx (tmp.constant);
3707 desc->offset = pool->offset;
3708 desc->hash = hash;
3709 desc->mode = mode;
3710 desc->align = align;
3711 desc->labelno = const_labelno;
3712 desc->mark = 0;
3714 pool->offset += GET_MODE_SIZE (mode);
3715 if (pool->last)
3716 pool->last->next = desc;
3717 else
3718 pool->first = pool->last = desc;
3719 pool->last = desc;
3721 /* Create a string containing the label name, in LABEL. */
3722 ASM_GENERATE_INTERNAL_LABEL (label, "LC", const_labelno);
3723 ++const_labelno;
3725 /* Construct the SYMBOL_REF. Make sure to mark it as belonging to
3726 the constants pool. */
3727 if (use_object_blocks_p () && targetm.use_blocks_for_constant_p (mode, x))
3729 section *sect = targetm.asm_out.select_rtx_section (mode, x, align);
3730 symbol = create_block_symbol (ggc_strdup (label),
3731 get_block_for_section (sect), -1);
3733 else
3734 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3735 desc->sym = symbol;
3736 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3737 CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3738 SET_SYMBOL_REF_CONSTANT (symbol, desc);
3740 /* Construct the MEM. */
3741 desc->mem = def = gen_const_mem (mode, symbol);
3742 set_mem_attributes (def, lang_hooks.types.type_for_mode (mode, 0), 1);
3743 set_mem_align (def, align);
3745 /* If we're dropping a label to the constant pool, make sure we
3746 don't delete it. */
3747 if (GET_CODE (x) == LABEL_REF)
3748 LABEL_PRESERVE_P (XEXP (x, 0)) = 1;
3750 return copy_rtx (def);
3753 /* Given a constant pool SYMBOL_REF, return the corresponding constant. */
3756 get_pool_constant (const_rtx addr)
3758 return SYMBOL_REF_CONSTANT (addr)->constant;
3761 /* Given a constant pool SYMBOL_REF, return the corresponding constant
3762 and whether it has been output or not. */
3765 get_pool_constant_mark (rtx addr, bool *pmarked)
3767 struct constant_descriptor_rtx *desc;
3769 desc = SYMBOL_REF_CONSTANT (addr);
3770 *pmarked = (desc->mark != 0);
3771 return desc->constant;
3774 /* Similar, return the mode. */
3776 machine_mode
3777 get_pool_mode (const_rtx addr)
3779 return SYMBOL_REF_CONSTANT (addr)->mode;
3782 /* Return the size of the constant pool. */
3785 get_pool_size (void)
3787 return crtl->varasm.pool->offset;
3790 /* Worker function for output_constant_pool_1. Emit assembly for X
3791 in MODE with known alignment ALIGN. */
3793 static void
3794 output_constant_pool_2 (machine_mode mode, rtx x, unsigned int align)
3796 switch (GET_MODE_CLASS (mode))
3798 case MODE_FLOAT:
3799 case MODE_DECIMAL_FLOAT:
3801 REAL_VALUE_TYPE r;
3803 gcc_assert (CONST_DOUBLE_AS_FLOAT_P (x));
3804 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
3805 assemble_real (r, mode, align);
3806 break;
3809 case MODE_INT:
3810 case MODE_PARTIAL_INT:
3811 case MODE_FRACT:
3812 case MODE_UFRACT:
3813 case MODE_ACCUM:
3814 case MODE_UACCUM:
3815 case MODE_POINTER_BOUNDS:
3816 assemble_integer (x, GET_MODE_SIZE (mode), align, 1);
3817 break;
3819 case MODE_VECTOR_FLOAT:
3820 case MODE_VECTOR_INT:
3821 case MODE_VECTOR_FRACT:
3822 case MODE_VECTOR_UFRACT:
3823 case MODE_VECTOR_ACCUM:
3824 case MODE_VECTOR_UACCUM:
3826 int i, units;
3827 machine_mode submode = GET_MODE_INNER (mode);
3828 unsigned int subalign = MIN (align, GET_MODE_BITSIZE (submode));
3830 gcc_assert (GET_CODE (x) == CONST_VECTOR);
3831 units = CONST_VECTOR_NUNITS (x);
3833 for (i = 0; i < units; i++)
3835 rtx elt = CONST_VECTOR_ELT (x, i);
3836 output_constant_pool_2 (submode, elt, i ? subalign : align);
3839 break;
3841 default:
3842 gcc_unreachable ();
3846 /* Worker function for output_constant_pool. Emit constant DESC,
3847 giving it ALIGN bits of alignment. */
3849 static void
3850 output_constant_pool_1 (struct constant_descriptor_rtx *desc,
3851 unsigned int align)
3853 rtx x, tmp;
3855 x = desc->constant;
3857 /* See if X is a LABEL_REF (or a CONST referring to a LABEL_REF)
3858 whose CODE_LABEL has been deleted. This can occur if a jump table
3859 is eliminated by optimization. If so, write a constant of zero
3860 instead. Note that this can also happen by turning the
3861 CODE_LABEL into a NOTE. */
3862 /* ??? This seems completely and utterly wrong. Certainly it's
3863 not true for NOTE_INSN_DELETED_LABEL, but I disbelieve proper
3864 functioning even with rtx_insn::deleted and friends. */
3866 tmp = x;
3867 switch (GET_CODE (tmp))
3869 case CONST:
3870 if (GET_CODE (XEXP (tmp, 0)) != PLUS
3871 || GET_CODE (XEXP (XEXP (tmp, 0), 0)) != LABEL_REF)
3872 break;
3873 tmp = XEXP (XEXP (tmp, 0), 0);
3874 /* FALLTHRU */
3876 case LABEL_REF:
3877 tmp = LABEL_REF_LABEL (tmp);
3878 gcc_assert (!as_a<rtx_insn *> (tmp)->deleted ());
3879 gcc_assert (!NOTE_P (tmp)
3880 || NOTE_KIND (tmp) != NOTE_INSN_DELETED);
3881 break;
3883 default:
3884 break;
3887 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3888 ASM_OUTPUT_SPECIAL_POOL_ENTRY (asm_out_file, x, desc->mode,
3889 align, desc->labelno, done);
3890 #endif
3892 assemble_align (align);
3894 /* Output the label. */
3895 targetm.asm_out.internal_label (asm_out_file, "LC", desc->labelno);
3897 /* Output the data. */
3898 output_constant_pool_2 (desc->mode, x, align);
3900 /* Make sure all constants in SECTION_MERGE and not SECTION_STRINGS
3901 sections have proper size. */
3902 if (align > GET_MODE_BITSIZE (desc->mode)
3903 && in_section
3904 && (in_section->common.flags & SECTION_MERGE))
3905 assemble_align (align);
3907 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3908 done:
3909 #endif
3910 return;
3913 /* Mark all constants that are referenced by SYMBOL_REFs in X.
3914 Emit referenced deferred strings. */
3916 static void
3917 mark_constants_in_pattern (rtx insn)
3919 subrtx_iterator::array_type array;
3920 FOR_EACH_SUBRTX (iter, array, PATTERN (insn), ALL)
3922 const_rtx x = *iter;
3923 if (GET_CODE (x) == SYMBOL_REF)
3925 if (CONSTANT_POOL_ADDRESS_P (x))
3927 struct constant_descriptor_rtx *desc = SYMBOL_REF_CONSTANT (x);
3928 if (desc->mark == 0)
3930 desc->mark = 1;
3931 iter.substitute (desc->constant);
3934 else if (TREE_CONSTANT_POOL_ADDRESS_P (x))
3936 tree decl = SYMBOL_REF_DECL (x);
3937 if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
3939 n_deferred_constants--;
3940 output_constant_def_contents (CONST_CAST_RTX (x));
3947 /* Look through appropriate parts of INSN, marking all entries in the
3948 constant pool which are actually being used. Entries that are only
3949 referenced by other constants are also marked as used. Emit
3950 deferred strings that are used. */
3952 static void
3953 mark_constants (rtx_insn *insn)
3955 if (!INSN_P (insn))
3956 return;
3958 /* Insns may appear inside a SEQUENCE. Only check the patterns of
3959 insns, not any notes that may be attached. We don't want to mark
3960 a constant just because it happens to appear in a REG_EQUIV note. */
3961 if (rtx_sequence *seq = dyn_cast <rtx_sequence *> (PATTERN (insn)))
3963 int i, n = seq->len ();
3964 for (i = 0; i < n; ++i)
3966 rtx subinsn = seq->element (i);
3967 if (INSN_P (subinsn))
3968 mark_constants_in_pattern (subinsn);
3971 else
3972 mark_constants_in_pattern (insn);
3975 /* Look through the instructions for this function, and mark all the
3976 entries in POOL which are actually being used. Emit deferred constants
3977 which have indeed been used. */
3979 static void
3980 mark_constant_pool (void)
3982 rtx_insn *insn;
3984 if (!crtl->uses_const_pool && n_deferred_constants == 0)
3985 return;
3987 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3988 mark_constants (insn);
3991 /* Write all the constants in POOL. */
3993 static void
3994 output_constant_pool_contents (struct rtx_constant_pool *pool)
3996 struct constant_descriptor_rtx *desc;
3998 for (desc = pool->first; desc ; desc = desc->next)
3999 if (desc->mark)
4001 /* If the constant is part of an object_block, make sure that
4002 the constant has been positioned within its block, but do not
4003 write out its definition yet. output_object_blocks will do
4004 that later. */
4005 if (SYMBOL_REF_HAS_BLOCK_INFO_P (desc->sym)
4006 && SYMBOL_REF_BLOCK (desc->sym))
4007 place_block_symbol (desc->sym);
4008 else
4010 switch_to_section (targetm.asm_out.select_rtx_section
4011 (desc->mode, desc->constant, desc->align));
4012 output_constant_pool_1 (desc, desc->align);
4017 /* Mark all constants that are used in the current function, then write
4018 out the function's private constant pool. */
4020 static void
4021 output_constant_pool (const char *fnname ATTRIBUTE_UNUSED,
4022 tree fndecl ATTRIBUTE_UNUSED)
4024 struct rtx_constant_pool *pool = crtl->varasm.pool;
4026 /* It is possible for gcc to call force_const_mem and then to later
4027 discard the instructions which refer to the constant. In such a
4028 case we do not need to output the constant. */
4029 mark_constant_pool ();
4031 #ifdef ASM_OUTPUT_POOL_PROLOGUE
4032 ASM_OUTPUT_POOL_PROLOGUE (asm_out_file, fnname, fndecl, pool->offset);
4033 #endif
4035 output_constant_pool_contents (pool);
4037 #ifdef ASM_OUTPUT_POOL_EPILOGUE
4038 ASM_OUTPUT_POOL_EPILOGUE (asm_out_file, fnname, fndecl, pool->offset);
4039 #endif
4042 /* Write the contents of the shared constant pool. */
4044 void
4045 output_shared_constant_pool (void)
4047 output_constant_pool_contents (shared_constant_pool);
4050 /* Determine what kind of relocations EXP may need. */
4053 compute_reloc_for_constant (tree exp)
4055 int reloc = 0, reloc2;
4056 tree tem;
4058 switch (TREE_CODE (exp))
4060 case ADDR_EXPR:
4061 case FDESC_EXPR:
4062 /* Go inside any operations that get_inner_reference can handle and see
4063 if what's inside is a constant: no need to do anything here for
4064 addresses of variables or functions. */
4065 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
4066 tem = TREE_OPERAND (tem, 0))
4069 if (TREE_CODE (tem) == MEM_REF
4070 && TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR)
4072 reloc = compute_reloc_for_constant (TREE_OPERAND (tem, 0));
4073 break;
4076 if (!targetm.binds_local_p (tem))
4077 reloc |= 2;
4078 else
4079 reloc |= 1;
4080 break;
4082 case PLUS_EXPR:
4083 case POINTER_PLUS_EXPR:
4084 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4085 reloc |= compute_reloc_for_constant (TREE_OPERAND (exp, 1));
4086 break;
4088 case MINUS_EXPR:
4089 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4090 reloc2 = compute_reloc_for_constant (TREE_OPERAND (exp, 1));
4091 /* The difference of two local labels is computable at link time. */
4092 if (reloc == 1 && reloc2 == 1)
4093 reloc = 0;
4094 else
4095 reloc |= reloc2;
4096 break;
4098 CASE_CONVERT:
4099 case VIEW_CONVERT_EXPR:
4100 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4101 break;
4103 case CONSTRUCTOR:
4105 unsigned HOST_WIDE_INT idx;
4106 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
4107 if (tem != 0)
4108 reloc |= compute_reloc_for_constant (tem);
4110 break;
4112 default:
4113 break;
4115 return reloc;
4118 /* Find all the constants whose addresses are referenced inside of EXP,
4119 and make sure assembler code with a label has been output for each one.
4120 Indicate whether an ADDR_EXPR has been encountered. */
4122 static void
4123 output_addressed_constants (tree exp)
4125 tree tem;
4127 switch (TREE_CODE (exp))
4129 case ADDR_EXPR:
4130 case FDESC_EXPR:
4131 /* Go inside any operations that get_inner_reference can handle and see
4132 if what's inside is a constant: no need to do anything here for
4133 addresses of variables or functions. */
4134 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
4135 tem = TREE_OPERAND (tem, 0))
4138 /* If we have an initialized CONST_DECL, retrieve the initializer. */
4139 if (TREE_CODE (tem) == CONST_DECL && DECL_INITIAL (tem))
4140 tem = DECL_INITIAL (tem);
4142 if (CONSTANT_CLASS_P (tem) || TREE_CODE (tem) == CONSTRUCTOR)
4143 output_constant_def (tem, 0);
4145 if (TREE_CODE (tem) == MEM_REF)
4146 output_addressed_constants (TREE_OPERAND (tem, 0));
4147 break;
4149 case PLUS_EXPR:
4150 case POINTER_PLUS_EXPR:
4151 case MINUS_EXPR:
4152 output_addressed_constants (TREE_OPERAND (exp, 1));
4153 /* Fall through. */
4155 CASE_CONVERT:
4156 case VIEW_CONVERT_EXPR:
4157 output_addressed_constants (TREE_OPERAND (exp, 0));
4158 break;
4160 case CONSTRUCTOR:
4162 unsigned HOST_WIDE_INT idx;
4163 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
4164 if (tem != 0)
4165 output_addressed_constants (tem);
4167 break;
4169 default:
4170 break;
4174 /* Whether a constructor CTOR is a valid static constant initializer if all
4175 its elements are. This used to be internal to initializer_constant_valid_p
4176 and has been exposed to let other functions like categorize_ctor_elements
4177 evaluate the property while walking a constructor for other purposes. */
4179 bool
4180 constructor_static_from_elts_p (const_tree ctor)
4182 return (TREE_CONSTANT (ctor)
4183 && (TREE_CODE (TREE_TYPE (ctor)) == UNION_TYPE
4184 || TREE_CODE (TREE_TYPE (ctor)) == RECORD_TYPE
4185 || TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE));
4188 static tree initializer_constant_valid_p_1 (tree value, tree endtype,
4189 tree *cache);
4191 /* A subroutine of initializer_constant_valid_p. VALUE is a MINUS_EXPR,
4192 PLUS_EXPR or POINTER_PLUS_EXPR. This looks for cases of VALUE
4193 which are valid when ENDTYPE is an integer of any size; in
4194 particular, this does not accept a pointer minus a constant. This
4195 returns null_pointer_node if the VALUE is an absolute constant
4196 which can be used to initialize a static variable. Otherwise it
4197 returns NULL. */
4199 static tree
4200 narrowing_initializer_constant_valid_p (tree value, tree endtype, tree *cache)
4202 tree op0, op1;
4204 if (!INTEGRAL_TYPE_P (endtype))
4205 return NULL_TREE;
4207 op0 = TREE_OPERAND (value, 0);
4208 op1 = TREE_OPERAND (value, 1);
4210 /* Like STRIP_NOPS except allow the operand mode to widen. This
4211 works around a feature of fold that simplifies (int)(p1 - p2) to
4212 ((int)p1 - (int)p2) under the theory that the narrower operation
4213 is cheaper. */
4215 while (CONVERT_EXPR_P (op0)
4216 || TREE_CODE (op0) == NON_LVALUE_EXPR)
4218 tree inner = TREE_OPERAND (op0, 0);
4219 if (inner == error_mark_node
4220 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4221 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))
4222 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
4223 break;
4224 op0 = inner;
4227 while (CONVERT_EXPR_P (op1)
4228 || TREE_CODE (op1) == NON_LVALUE_EXPR)
4230 tree inner = TREE_OPERAND (op1, 0);
4231 if (inner == error_mark_node
4232 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4233 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op1)))
4234 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
4235 break;
4236 op1 = inner;
4239 op0 = initializer_constant_valid_p_1 (op0, endtype, cache);
4240 if (!op0)
4241 return NULL_TREE;
4243 op1 = initializer_constant_valid_p_1 (op1, endtype,
4244 cache ? cache + 2 : NULL);
4245 /* Both initializers must be known. */
4246 if (op1)
4248 if (op0 == op1
4249 && (op0 == null_pointer_node
4250 || TREE_CODE (value) == MINUS_EXPR))
4251 return null_pointer_node;
4253 /* Support differences between labels. */
4254 if (TREE_CODE (op0) == LABEL_DECL
4255 && TREE_CODE (op1) == LABEL_DECL)
4256 return null_pointer_node;
4258 if (TREE_CODE (op0) == STRING_CST
4259 && TREE_CODE (op1) == STRING_CST
4260 && operand_equal_p (op0, op1, 1))
4261 return null_pointer_node;
4264 return NULL_TREE;
4267 /* Helper function of initializer_constant_valid_p.
4268 Return nonzero if VALUE is a valid constant-valued expression
4269 for use in initializing a static variable; one that can be an
4270 element of a "constant" initializer.
4272 Return null_pointer_node if the value is absolute;
4273 if it is relocatable, return the variable that determines the relocation.
4274 We assume that VALUE has been folded as much as possible;
4275 therefore, we do not need to check for such things as
4276 arithmetic-combinations of integers.
4278 Use CACHE (pointer to 2 tree values) for caching if non-NULL. */
4280 static tree
4281 initializer_constant_valid_p_1 (tree value, tree endtype, tree *cache)
4283 tree ret;
4285 switch (TREE_CODE (value))
4287 case CONSTRUCTOR:
4288 if (constructor_static_from_elts_p (value))
4290 unsigned HOST_WIDE_INT idx;
4291 tree elt;
4292 bool absolute = true;
4294 if (cache && cache[0] == value)
4295 return cache[1];
4296 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
4298 tree reloc;
4299 reloc = initializer_constant_valid_p_1 (elt, TREE_TYPE (elt),
4300 NULL);
4301 if (!reloc)
4303 if (cache)
4305 cache[0] = value;
4306 cache[1] = NULL_TREE;
4308 return NULL_TREE;
4310 if (reloc != null_pointer_node)
4311 absolute = false;
4313 /* For a non-absolute relocation, there is no single
4314 variable that can be "the variable that determines the
4315 relocation." */
4316 if (cache)
4318 cache[0] = value;
4319 cache[1] = absolute ? null_pointer_node : error_mark_node;
4321 return absolute ? null_pointer_node : error_mark_node;
4324 return TREE_STATIC (value) ? null_pointer_node : NULL_TREE;
4326 case INTEGER_CST:
4327 case VECTOR_CST:
4328 case REAL_CST:
4329 case FIXED_CST:
4330 case STRING_CST:
4331 case COMPLEX_CST:
4332 return null_pointer_node;
4334 case ADDR_EXPR:
4335 case FDESC_EXPR:
4337 tree op0 = staticp (TREE_OPERAND (value, 0));
4338 if (op0)
4340 /* "&(*a).f" is like unto pointer arithmetic. If "a" turns out
4341 to be a constant, this is old-skool offsetof-like nonsense. */
4342 if (TREE_CODE (op0) == INDIRECT_REF
4343 && TREE_CONSTANT (TREE_OPERAND (op0, 0)))
4344 return null_pointer_node;
4345 /* Taking the address of a nested function involves a trampoline,
4346 unless we don't need or want one. */
4347 if (TREE_CODE (op0) == FUNCTION_DECL
4348 && DECL_STATIC_CHAIN (op0)
4349 && !TREE_NO_TRAMPOLINE (value))
4350 return NULL_TREE;
4351 /* "&{...}" requires a temporary to hold the constructed
4352 object. */
4353 if (TREE_CODE (op0) == CONSTRUCTOR)
4354 return NULL_TREE;
4356 return op0;
4359 case NON_LVALUE_EXPR:
4360 return initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4361 endtype, cache);
4363 case VIEW_CONVERT_EXPR:
4365 tree src = TREE_OPERAND (value, 0);
4366 tree src_type = TREE_TYPE (src);
4367 tree dest_type = TREE_TYPE (value);
4369 /* Allow view-conversions from aggregate to non-aggregate type only
4370 if the bit pattern is fully preserved afterwards; otherwise, the
4371 RTL expander won't be able to apply a subsequent transformation
4372 to the underlying constructor. */
4373 if (AGGREGATE_TYPE_P (src_type) && !AGGREGATE_TYPE_P (dest_type))
4375 if (TYPE_MODE (endtype) == TYPE_MODE (dest_type))
4376 return initializer_constant_valid_p_1 (src, endtype, cache);
4377 else
4378 return NULL_TREE;
4381 /* Allow all other kinds of view-conversion. */
4382 return initializer_constant_valid_p_1 (src, endtype, cache);
4385 CASE_CONVERT:
4387 tree src = TREE_OPERAND (value, 0);
4388 tree src_type = TREE_TYPE (src);
4389 tree dest_type = TREE_TYPE (value);
4391 /* Allow conversions between pointer types, floating-point
4392 types, and offset types. */
4393 if ((POINTER_TYPE_P (dest_type) && POINTER_TYPE_P (src_type))
4394 || (FLOAT_TYPE_P (dest_type) && FLOAT_TYPE_P (src_type))
4395 || (TREE_CODE (dest_type) == OFFSET_TYPE
4396 && TREE_CODE (src_type) == OFFSET_TYPE))
4397 return initializer_constant_valid_p_1 (src, endtype, cache);
4399 /* Allow length-preserving conversions between integer types. */
4400 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type)
4401 && (TYPE_PRECISION (dest_type) == TYPE_PRECISION (src_type)))
4402 return initializer_constant_valid_p_1 (src, endtype, cache);
4404 /* Allow conversions between other integer types only if
4405 explicit value. */
4406 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type))
4408 tree inner = initializer_constant_valid_p_1 (src, endtype, cache);
4409 if (inner == null_pointer_node)
4410 return null_pointer_node;
4411 break;
4414 /* Allow (int) &foo provided int is as wide as a pointer. */
4415 if (INTEGRAL_TYPE_P (dest_type) && POINTER_TYPE_P (src_type)
4416 && (TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type)))
4417 return initializer_constant_valid_p_1 (src, endtype, cache);
4419 /* Likewise conversions from int to pointers, but also allow
4420 conversions from 0. */
4421 if ((POINTER_TYPE_P (dest_type)
4422 || TREE_CODE (dest_type) == OFFSET_TYPE)
4423 && INTEGRAL_TYPE_P (src_type))
4425 if (TREE_CODE (src) == INTEGER_CST
4426 && TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type))
4427 return null_pointer_node;
4428 if (integer_zerop (src))
4429 return null_pointer_node;
4430 else if (TYPE_PRECISION (dest_type) <= TYPE_PRECISION (src_type))
4431 return initializer_constant_valid_p_1 (src, endtype, cache);
4434 /* Allow conversions to struct or union types if the value
4435 inside is okay. */
4436 if (TREE_CODE (dest_type) == RECORD_TYPE
4437 || TREE_CODE (dest_type) == UNION_TYPE)
4438 return initializer_constant_valid_p_1 (src, endtype, cache);
4440 break;
4442 case POINTER_PLUS_EXPR:
4443 case PLUS_EXPR:
4444 /* Any valid floating-point constants will have been folded by now;
4445 with -frounding-math we hit this with addition of two constants. */
4446 if (TREE_CODE (endtype) == REAL_TYPE)
4447 return NULL_TREE;
4448 if (cache && cache[0] == value)
4449 return cache[1];
4450 if (! INTEGRAL_TYPE_P (endtype)
4451 || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
4453 tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
4454 tree valid0
4455 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4456 endtype, ncache);
4457 tree valid1
4458 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
4459 endtype, ncache + 2);
4460 /* If either term is absolute, use the other term's relocation. */
4461 if (valid0 == null_pointer_node)
4462 ret = valid1;
4463 else if (valid1 == null_pointer_node)
4464 ret = valid0;
4465 /* Support narrowing pointer differences. */
4466 else
4467 ret = narrowing_initializer_constant_valid_p (value, endtype,
4468 ncache);
4470 else
4471 /* Support narrowing pointer differences. */
4472 ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
4473 if (cache)
4475 cache[0] = value;
4476 cache[1] = ret;
4478 return ret;
4480 case MINUS_EXPR:
4481 if (TREE_CODE (endtype) == REAL_TYPE)
4482 return NULL_TREE;
4483 if (cache && cache[0] == value)
4484 return cache[1];
4485 if (! INTEGRAL_TYPE_P (endtype)
4486 || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
4488 tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
4489 tree valid0
4490 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4491 endtype, ncache);
4492 tree valid1
4493 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
4494 endtype, ncache + 2);
4495 /* Win if second argument is absolute. */
4496 if (valid1 == null_pointer_node)
4497 ret = valid0;
4498 /* Win if both arguments have the same relocation.
4499 Then the value is absolute. */
4500 else if (valid0 == valid1 && valid0 != 0)
4501 ret = null_pointer_node;
4502 /* Since GCC guarantees that string constants are unique in the
4503 generated code, a subtraction between two copies of the same
4504 constant string is absolute. */
4505 else if (valid0 && TREE_CODE (valid0) == STRING_CST
4506 && valid1 && TREE_CODE (valid1) == STRING_CST
4507 && operand_equal_p (valid0, valid1, 1))
4508 ret = null_pointer_node;
4509 /* Support narrowing differences. */
4510 else
4511 ret = narrowing_initializer_constant_valid_p (value, endtype,
4512 ncache);
4514 else
4515 /* Support narrowing differences. */
4516 ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
4517 if (cache)
4519 cache[0] = value;
4520 cache[1] = ret;
4522 return ret;
4524 default:
4525 break;
4528 return NULL_TREE;
4531 /* Return nonzero if VALUE is a valid constant-valued expression
4532 for use in initializing a static variable; one that can be an
4533 element of a "constant" initializer.
4535 Return null_pointer_node if the value is absolute;
4536 if it is relocatable, return the variable that determines the relocation.
4537 We assume that VALUE has been folded as much as possible;
4538 therefore, we do not need to check for such things as
4539 arithmetic-combinations of integers. */
4540 tree
4541 initializer_constant_valid_p (tree value, tree endtype)
4543 return initializer_constant_valid_p_1 (value, endtype, NULL);
4546 /* Return true if VALUE is a valid constant-valued expression
4547 for use in initializing a static bit-field; one that can be
4548 an element of a "constant" initializer. */
4550 bool
4551 initializer_constant_valid_for_bitfield_p (tree value)
4553 /* For bitfields we support integer constants or possibly nested aggregates
4554 of such. */
4555 switch (TREE_CODE (value))
4557 case CONSTRUCTOR:
4559 unsigned HOST_WIDE_INT idx;
4560 tree elt;
4562 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
4563 if (!initializer_constant_valid_for_bitfield_p (elt))
4564 return false;
4565 return true;
4568 case INTEGER_CST:
4569 case REAL_CST:
4570 return true;
4572 case VIEW_CONVERT_EXPR:
4573 case NON_LVALUE_EXPR:
4574 return
4575 initializer_constant_valid_for_bitfield_p (TREE_OPERAND (value, 0));
4577 default:
4578 break;
4581 return false;
4584 /* output_constructor outer state of relevance in recursive calls, typically
4585 for nested aggregate bitfields. */
4587 typedef struct {
4588 unsigned int bit_offset; /* current position in ... */
4589 int byte; /* ... the outer byte buffer. */
4590 } oc_outer_state;
4592 static unsigned HOST_WIDE_INT
4593 output_constructor (tree, unsigned HOST_WIDE_INT, unsigned int,
4594 oc_outer_state *);
4596 /* Output assembler code for constant EXP, with no label.
4597 This includes the pseudo-op such as ".int" or ".byte", and a newline.
4598 Assumes output_addressed_constants has been done on EXP already.
4600 Generate at least SIZE bytes of assembler data, padding at the end
4601 with zeros if necessary. SIZE must always be specified. The returned
4602 value is the actual number of bytes of assembler data generated, which
4603 may be bigger than SIZE if the object contains a variable length field.
4605 SIZE is important for structure constructors,
4606 since trailing members may have been omitted from the constructor.
4607 It is also important for initialization of arrays from string constants
4608 since the full length of the string constant might not be wanted.
4609 It is also needed for initialization of unions, where the initializer's
4610 type is just one member, and that may not be as long as the union.
4612 There a case in which we would fail to output exactly SIZE bytes:
4613 for a structure constructor that wants to produce more than SIZE bytes.
4614 But such constructors will never be generated for any possible input.
4616 ALIGN is the alignment of the data in bits. */
4618 static unsigned HOST_WIDE_INT
4619 output_constant (tree exp, unsigned HOST_WIDE_INT size, unsigned int align)
4621 enum tree_code code;
4622 unsigned HOST_WIDE_INT thissize;
4624 if (size == 0 || flag_syntax_only)
4625 return size;
4627 /* See if we're trying to initialize a pointer in a non-default mode
4628 to the address of some declaration somewhere. If the target says
4629 the mode is valid for pointers, assume the target has a way of
4630 resolving it. */
4631 if (TREE_CODE (exp) == NOP_EXPR
4632 && POINTER_TYPE_P (TREE_TYPE (exp))
4633 && targetm.addr_space.valid_pointer_mode
4634 (TYPE_MODE (TREE_TYPE (exp)),
4635 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
4637 tree saved_type = TREE_TYPE (exp);
4639 /* Peel off any intermediate conversions-to-pointer for valid
4640 pointer modes. */
4641 while (TREE_CODE (exp) == NOP_EXPR
4642 && POINTER_TYPE_P (TREE_TYPE (exp))
4643 && targetm.addr_space.valid_pointer_mode
4644 (TYPE_MODE (TREE_TYPE (exp)),
4645 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
4646 exp = TREE_OPERAND (exp, 0);
4648 /* If what we're left with is the address of something, we can
4649 convert the address to the final type and output it that
4650 way. */
4651 if (TREE_CODE (exp) == ADDR_EXPR)
4652 exp = build1 (ADDR_EXPR, saved_type, TREE_OPERAND (exp, 0));
4653 /* Likewise for constant ints. */
4654 else if (TREE_CODE (exp) == INTEGER_CST)
4655 exp = wide_int_to_tree (saved_type, exp);
4659 /* Eliminate any conversions since we'll be outputting the underlying
4660 constant. */
4661 while (CONVERT_EXPR_P (exp)
4662 || TREE_CODE (exp) == NON_LVALUE_EXPR
4663 || TREE_CODE (exp) == VIEW_CONVERT_EXPR)
4665 HOST_WIDE_INT type_size = int_size_in_bytes (TREE_TYPE (exp));
4666 HOST_WIDE_INT op_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (exp, 0)));
4668 /* Make sure eliminating the conversion is really a no-op, except with
4669 VIEW_CONVERT_EXPRs to allow for wild Ada unchecked conversions and
4670 union types to allow for Ada unchecked unions. */
4671 if (type_size > op_size
4672 && TREE_CODE (exp) != VIEW_CONVERT_EXPR
4673 && TREE_CODE (TREE_TYPE (exp)) != UNION_TYPE)
4674 /* Keep the conversion. */
4675 break;
4676 else
4677 exp = TREE_OPERAND (exp, 0);
4680 code = TREE_CODE (TREE_TYPE (exp));
4681 thissize = int_size_in_bytes (TREE_TYPE (exp));
4683 /* Allow a constructor with no elements for any data type.
4684 This means to fill the space with zeros. */
4685 if (TREE_CODE (exp) == CONSTRUCTOR
4686 && vec_safe_is_empty (CONSTRUCTOR_ELTS (exp)))
4688 assemble_zeros (size);
4689 return size;
4692 if (TREE_CODE (exp) == FDESC_EXPR)
4694 #ifdef ASM_OUTPUT_FDESC
4695 HOST_WIDE_INT part = tree_to_shwi (TREE_OPERAND (exp, 1));
4696 tree decl = TREE_OPERAND (exp, 0);
4697 ASM_OUTPUT_FDESC (asm_out_file, decl, part);
4698 #else
4699 gcc_unreachable ();
4700 #endif
4701 return size;
4704 /* Now output the underlying data. If we've handling the padding, return.
4705 Otherwise, break and ensure SIZE is the size written. */
4706 switch (code)
4708 case BOOLEAN_TYPE:
4709 case INTEGER_TYPE:
4710 case ENUMERAL_TYPE:
4711 case POINTER_TYPE:
4712 case REFERENCE_TYPE:
4713 case OFFSET_TYPE:
4714 case FIXED_POINT_TYPE:
4715 case POINTER_BOUNDS_TYPE:
4716 case NULLPTR_TYPE:
4717 if (! assemble_integer (expand_expr (exp, NULL_RTX, VOIDmode,
4718 EXPAND_INITIALIZER),
4719 MIN (size, thissize), align, 0))
4720 error ("initializer for integer/fixed-point value is too complicated");
4721 break;
4723 case REAL_TYPE:
4724 if (TREE_CODE (exp) != REAL_CST)
4725 error ("initializer for floating value is not a floating constant");
4726 else
4727 assemble_real (TREE_REAL_CST (exp), TYPE_MODE (TREE_TYPE (exp)), align);
4728 break;
4730 case COMPLEX_TYPE:
4731 output_constant (TREE_REALPART (exp), thissize / 2, align);
4732 output_constant (TREE_IMAGPART (exp), thissize / 2,
4733 min_align (align, BITS_PER_UNIT * (thissize / 2)));
4734 break;
4736 case ARRAY_TYPE:
4737 case VECTOR_TYPE:
4738 switch (TREE_CODE (exp))
4740 case CONSTRUCTOR:
4741 return output_constructor (exp, size, align, NULL);
4742 case STRING_CST:
4743 thissize
4744 = MIN ((unsigned HOST_WIDE_INT)TREE_STRING_LENGTH (exp), size);
4745 assemble_string (TREE_STRING_POINTER (exp), thissize);
4746 break;
4747 case VECTOR_CST:
4749 machine_mode inner = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
4750 unsigned int nalign = MIN (align, GET_MODE_ALIGNMENT (inner));
4751 int elt_size = GET_MODE_SIZE (inner);
4752 output_constant (VECTOR_CST_ELT (exp, 0), elt_size, align);
4753 thissize = elt_size;
4754 for (unsigned int i = 1; i < VECTOR_CST_NELTS (exp); i++)
4756 output_constant (VECTOR_CST_ELT (exp, i), elt_size, nalign);
4757 thissize += elt_size;
4759 break;
4761 default:
4762 gcc_unreachable ();
4764 break;
4766 case RECORD_TYPE:
4767 case UNION_TYPE:
4768 gcc_assert (TREE_CODE (exp) == CONSTRUCTOR);
4769 return output_constructor (exp, size, align, NULL);
4771 case ERROR_MARK:
4772 return 0;
4774 default:
4775 gcc_unreachable ();
4778 if (size > thissize)
4779 assemble_zeros (size - thissize);
4781 return size;
4785 /* Subroutine of output_constructor, used for computing the size of
4786 arrays of unspecified length. VAL must be a CONSTRUCTOR of an array
4787 type with an unspecified upper bound. */
4789 static unsigned HOST_WIDE_INT
4790 array_size_for_constructor (tree val)
4792 tree max_index;
4793 unsigned HOST_WIDE_INT cnt;
4794 tree index, value, tmp;
4795 offset_int i;
4797 /* This code used to attempt to handle string constants that are not
4798 arrays of single-bytes, but nothing else does, so there's no point in
4799 doing it here. */
4800 if (TREE_CODE (val) == STRING_CST)
4801 return TREE_STRING_LENGTH (val);
4803 max_index = NULL_TREE;
4804 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (val), cnt, index, value)
4806 if (TREE_CODE (index) == RANGE_EXPR)
4807 index = TREE_OPERAND (index, 1);
4808 if (max_index == NULL_TREE || tree_int_cst_lt (max_index, index))
4809 max_index = index;
4812 if (max_index == NULL_TREE)
4813 return 0;
4815 /* Compute the total number of array elements. */
4816 tmp = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (val)));
4817 i = wi::to_offset (max_index) - wi::to_offset (tmp) + 1;
4819 /* Multiply by the array element unit size to find number of bytes. */
4820 i *= wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (val))));
4822 gcc_assert (wi::fits_uhwi_p (i));
4823 return i.to_uhwi ();
4826 /* Other datastructures + helpers for output_constructor. */
4828 /* output_constructor local state to support interaction with helpers. */
4830 typedef struct {
4832 /* Received arguments. */
4833 tree exp; /* Constructor expression. */
4834 tree type; /* Type of constructor expression. */
4835 unsigned HOST_WIDE_INT size; /* # bytes to output - pad if necessary. */
4836 unsigned int align; /* Known initial alignment. */
4837 tree min_index; /* Lower bound if specified for an array. */
4839 /* Output processing state. */
4840 HOST_WIDE_INT total_bytes; /* # bytes output so far / current position. */
4841 int byte; /* Part of a bitfield byte yet to be output. */
4842 int last_relative_index; /* Implicit or explicit index of the last
4843 array element output within a bitfield. */
4844 bool byte_buffer_in_use; /* Whether BYTE is in use. */
4846 /* Current element. */
4847 tree field; /* Current field decl in a record. */
4848 tree val; /* Current element value. */
4849 tree index; /* Current element index. */
4851 } oc_local_state;
4853 /* Helper for output_constructor. From the current LOCAL state, output a
4854 RANGE_EXPR element. */
4856 static void
4857 output_constructor_array_range (oc_local_state *local)
4859 unsigned HOST_WIDE_INT fieldsize
4860 = int_size_in_bytes (TREE_TYPE (local->type));
4862 HOST_WIDE_INT lo_index
4863 = tree_to_shwi (TREE_OPERAND (local->index, 0));
4864 HOST_WIDE_INT hi_index
4865 = tree_to_shwi (TREE_OPERAND (local->index, 1));
4866 HOST_WIDE_INT index;
4868 unsigned int align2
4869 = min_align (local->align, fieldsize * BITS_PER_UNIT);
4871 for (index = lo_index; index <= hi_index; index++)
4873 /* Output the element's initial value. */
4874 if (local->val == NULL_TREE)
4875 assemble_zeros (fieldsize);
4876 else
4877 fieldsize = output_constant (local->val, fieldsize, align2);
4879 /* Count its size. */
4880 local->total_bytes += fieldsize;
4884 /* Helper for output_constructor. From the current LOCAL state, output a
4885 field element that is not true bitfield or part of an outer one. */
4887 static void
4888 output_constructor_regular_field (oc_local_state *local)
4890 /* Field size and position. Since this structure is static, we know the
4891 positions are constant. */
4892 unsigned HOST_WIDE_INT fieldsize;
4893 HOST_WIDE_INT fieldpos;
4895 unsigned int align2;
4897 if (local->index != NULL_TREE)
4899 /* Perform the index calculation in modulo arithmetic but
4900 sign-extend the result because Ada has negative DECL_FIELD_OFFSETs
4901 but we are using an unsigned sizetype. */
4902 unsigned prec = TYPE_PRECISION (sizetype);
4903 offset_int idx = wi::sext (wi::to_offset (local->index)
4904 - wi::to_offset (local->min_index), prec);
4905 fieldpos = (idx * wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (local->val))))
4906 .to_short_addr ();
4908 else if (local->field != NULL_TREE)
4909 fieldpos = int_byte_position (local->field);
4910 else
4911 fieldpos = 0;
4913 /* Output any buffered-up bit-fields preceding this element. */
4914 if (local->byte_buffer_in_use)
4916 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
4917 local->total_bytes++;
4918 local->byte_buffer_in_use = false;
4921 /* Advance to offset of this element.
4922 Note no alignment needed in an array, since that is guaranteed
4923 if each element has the proper size. */
4924 if ((local->field != NULL_TREE || local->index != NULL_TREE)
4925 && fieldpos > local->total_bytes)
4927 assemble_zeros (fieldpos - local->total_bytes);
4928 local->total_bytes = fieldpos;
4931 /* Find the alignment of this element. */
4932 align2 = min_align (local->align, BITS_PER_UNIT * fieldpos);
4934 /* Determine size this element should occupy. */
4935 if (local->field)
4937 fieldsize = 0;
4939 /* If this is an array with an unspecified upper bound,
4940 the initializer determines the size. */
4941 /* ??? This ought to only checked if DECL_SIZE_UNIT is NULL,
4942 but we cannot do this until the deprecated support for
4943 initializing zero-length array members is removed. */
4944 if (TREE_CODE (TREE_TYPE (local->field)) == ARRAY_TYPE
4945 && TYPE_DOMAIN (TREE_TYPE (local->field))
4946 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (local->field))))
4948 fieldsize = array_size_for_constructor (local->val);
4949 /* Given a non-empty initialization, this field had
4950 better be last. */
4951 gcc_assert (!fieldsize || !DECL_CHAIN (local->field));
4953 else
4954 fieldsize = tree_to_uhwi (DECL_SIZE_UNIT (local->field));
4956 else
4957 fieldsize = int_size_in_bytes (TREE_TYPE (local->type));
4959 /* Output the element's initial value. */
4960 if (local->val == NULL_TREE)
4961 assemble_zeros (fieldsize);
4962 else
4963 fieldsize = output_constant (local->val, fieldsize, align2);
4965 /* Count its size. */
4966 local->total_bytes += fieldsize;
4969 /* Helper for output_constructor. From the LOCAL state, output an element
4970 that is a true bitfield or part of an outer one. BIT_OFFSET is the offset
4971 from the start of a possibly ongoing outer byte buffer. */
4973 static void
4974 output_constructor_bitfield (oc_local_state *local, unsigned int bit_offset)
4976 /* Bit size of this element. */
4977 HOST_WIDE_INT ebitsize
4978 = (local->field
4979 ? tree_to_uhwi (DECL_SIZE (local->field))
4980 : tree_to_uhwi (TYPE_SIZE (TREE_TYPE (local->type))));
4982 /* Relative index of this element if this is an array component. */
4983 HOST_WIDE_INT relative_index
4984 = (!local->field
4985 ? (local->index
4986 ? (tree_to_shwi (local->index)
4987 - tree_to_shwi (local->min_index))
4988 : local->last_relative_index + 1)
4989 : 0);
4991 /* Bit position of this element from the start of the containing
4992 constructor. */
4993 HOST_WIDE_INT constructor_relative_ebitpos
4994 = (local->field
4995 ? int_bit_position (local->field)
4996 : ebitsize * relative_index);
4998 /* Bit position of this element from the start of a possibly ongoing
4999 outer byte buffer. */
5000 HOST_WIDE_INT byte_relative_ebitpos
5001 = bit_offset + constructor_relative_ebitpos;
5003 /* From the start of a possibly ongoing outer byte buffer, offsets to
5004 the first bit of this element and to the first bit past the end of
5005 this element. */
5006 HOST_WIDE_INT next_offset = byte_relative_ebitpos;
5007 HOST_WIDE_INT end_offset = byte_relative_ebitpos + ebitsize;
5009 local->last_relative_index = relative_index;
5011 if (local->val == NULL_TREE)
5012 local->val = integer_zero_node;
5014 while (TREE_CODE (local->val) == VIEW_CONVERT_EXPR
5015 || TREE_CODE (local->val) == NON_LVALUE_EXPR)
5016 local->val = TREE_OPERAND (local->val, 0);
5018 if (TREE_CODE (local->val) != INTEGER_CST
5019 && TREE_CODE (local->val) != CONSTRUCTOR)
5021 error ("invalid initial value for member %qE", DECL_NAME (local->field));
5022 return;
5025 /* If this field does not start in this (or next) byte, skip some bytes. */
5026 if (next_offset / BITS_PER_UNIT != local->total_bytes)
5028 /* Output remnant of any bit field in previous bytes. */
5029 if (local->byte_buffer_in_use)
5031 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5032 local->total_bytes++;
5033 local->byte_buffer_in_use = false;
5036 /* If still not at proper byte, advance to there. */
5037 if (next_offset / BITS_PER_UNIT != local->total_bytes)
5039 gcc_assert (next_offset / BITS_PER_UNIT >= local->total_bytes);
5040 assemble_zeros (next_offset / BITS_PER_UNIT - local->total_bytes);
5041 local->total_bytes = next_offset / BITS_PER_UNIT;
5045 /* Set up the buffer if necessary. */
5046 if (!local->byte_buffer_in_use)
5048 local->byte = 0;
5049 if (ebitsize > 0)
5050 local->byte_buffer_in_use = true;
5053 /* If this is nested constructor, recurse passing the bit offset and the
5054 pending data, then retrieve the new pending data afterwards. */
5055 if (TREE_CODE (local->val) == CONSTRUCTOR)
5057 oc_outer_state temp_state;
5058 temp_state.bit_offset = next_offset % BITS_PER_UNIT;
5059 temp_state.byte = local->byte;
5060 local->total_bytes
5061 += output_constructor (local->val, 0, 0, &temp_state);
5062 local->byte = temp_state.byte;
5063 return;
5066 /* Otherwise, we must split the element into pieces that fall within
5067 separate bytes, and combine each byte with previous or following
5068 bit-fields. */
5069 while (next_offset < end_offset)
5071 int this_time;
5072 int shift;
5073 HOST_WIDE_INT value;
5074 HOST_WIDE_INT next_byte = next_offset / BITS_PER_UNIT;
5075 HOST_WIDE_INT next_bit = next_offset % BITS_PER_UNIT;
5077 /* Advance from byte to byte within this element when necessary. */
5078 while (next_byte != local->total_bytes)
5080 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5081 local->total_bytes++;
5082 local->byte = 0;
5085 /* Number of bits we can process at once (all part of the same byte). */
5086 this_time = MIN (end_offset - next_offset, BITS_PER_UNIT - next_bit);
5087 if (BYTES_BIG_ENDIAN)
5089 /* On big-endian machine, take the most significant bits (of the
5090 bits that are significant) first and put them into bytes from
5091 the most significant end. */
5092 shift = end_offset - next_offset - this_time;
5094 /* Don't try to take a bunch of bits that cross
5095 the word boundary in the INTEGER_CST. We can
5096 only select bits from one element. */
5097 if ((shift / HOST_BITS_PER_WIDE_INT)
5098 != ((shift + this_time - 1) / HOST_BITS_PER_WIDE_INT))
5100 const int end = shift + this_time - 1;
5101 shift = end & -HOST_BITS_PER_WIDE_INT;
5102 this_time = end - shift + 1;
5105 /* Now get the bits from the appropriate constant word. */
5106 value = TREE_INT_CST_ELT (local->val, shift / HOST_BITS_PER_WIDE_INT);
5107 shift = shift & (HOST_BITS_PER_WIDE_INT - 1);
5109 /* Get the result. This works only when:
5110 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
5111 local->byte |= (((value >> shift)
5112 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
5113 << (BITS_PER_UNIT - this_time - next_bit));
5115 else
5117 /* On little-endian machines, take the least significant bits of
5118 the value first and pack them starting at the least significant
5119 bits of the bytes. */
5120 shift = next_offset - byte_relative_ebitpos;
5122 /* Don't try to take a bunch of bits that cross
5123 the word boundary in the INTEGER_CST. We can
5124 only select bits from one element. */
5125 if ((shift / HOST_BITS_PER_WIDE_INT)
5126 != ((shift + this_time - 1) / HOST_BITS_PER_WIDE_INT))
5127 this_time
5128 = HOST_BITS_PER_WIDE_INT - (shift & (HOST_BITS_PER_WIDE_INT - 1));
5130 /* Now get the bits from the appropriate constant word. */
5131 value = TREE_INT_CST_ELT (local->val, shift / HOST_BITS_PER_WIDE_INT);
5132 shift = shift & (HOST_BITS_PER_WIDE_INT - 1);
5134 /* Get the result. This works only when:
5135 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
5136 local->byte |= (((value >> shift)
5137 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
5138 << next_bit);
5141 next_offset += this_time;
5142 local->byte_buffer_in_use = true;
5146 /* Subroutine of output_constant, used for CONSTRUCTORs (aggregate constants).
5147 Generate at least SIZE bytes, padding if necessary. OUTER designates the
5148 caller output state of relevance in recursive invocations. */
5150 static unsigned HOST_WIDE_INT
5151 output_constructor (tree exp, unsigned HOST_WIDE_INT size,
5152 unsigned int align, oc_outer_state *outer)
5154 unsigned HOST_WIDE_INT cnt;
5155 constructor_elt *ce;
5157 oc_local_state local;
5159 /* Setup our local state to communicate with helpers. */
5160 local.exp = exp;
5161 local.type = TREE_TYPE (exp);
5162 local.size = size;
5163 local.align = align;
5164 if (TREE_CODE (local.type) == ARRAY_TYPE && TYPE_DOMAIN (local.type))
5165 local.min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (local.type));
5166 else
5167 local.min_index = NULL_TREE;
5169 local.total_bytes = 0;
5170 local.byte_buffer_in_use = outer != NULL;
5171 local.byte = outer ? outer->byte : 0;
5172 local.last_relative_index = -1;
5174 gcc_assert (HOST_BITS_PER_WIDE_INT >= BITS_PER_UNIT);
5176 /* As CE goes through the elements of the constant, FIELD goes through the
5177 structure fields if the constant is a structure. If the constant is a
5178 union, we override this by getting the field from the TREE_LIST element.
5179 But the constant could also be an array. Then FIELD is zero.
5181 There is always a maximum of one element in the chain LINK for unions
5182 (even if the initializer in a source program incorrectly contains
5183 more one). */
5185 if (TREE_CODE (local.type) == RECORD_TYPE)
5186 local.field = TYPE_FIELDS (local.type);
5187 else
5188 local.field = NULL_TREE;
5190 for (cnt = 0;
5191 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), cnt, &ce);
5192 cnt++, local.field = local.field ? DECL_CHAIN (local.field) : 0)
5194 local.val = ce->value;
5195 local.index = NULL_TREE;
5197 /* The element in a union constructor specifies the proper field
5198 or index. */
5199 if (RECORD_OR_UNION_TYPE_P (local.type) && ce->index != NULL_TREE)
5200 local.field = ce->index;
5202 else if (TREE_CODE (local.type) == ARRAY_TYPE)
5203 local.index = ce->index;
5205 if (local.field && flag_verbose_asm)
5206 fprintf (asm_out_file, "%s %s:\n",
5207 ASM_COMMENT_START,
5208 DECL_NAME (local.field)
5209 ? IDENTIFIER_POINTER (DECL_NAME (local.field))
5210 : "<anonymous>");
5212 /* Eliminate the marker that makes a cast not be an lvalue. */
5213 if (local.val != NULL_TREE)
5214 STRIP_NOPS (local.val);
5216 /* Output the current element, using the appropriate helper ... */
5218 /* For an array slice not part of an outer bitfield. */
5219 if (!outer
5220 && local.index != NULL_TREE
5221 && TREE_CODE (local.index) == RANGE_EXPR)
5222 output_constructor_array_range (&local);
5224 /* For a field that is neither a true bitfield nor part of an outer one,
5225 known to be at least byte aligned and multiple-of-bytes long. */
5226 else if (!outer
5227 && (local.field == NULL_TREE
5228 || !CONSTRUCTOR_BITFIELD_P (local.field)))
5229 output_constructor_regular_field (&local);
5231 /* For a true bitfield or part of an outer one. Only INTEGER_CSTs are
5232 supported for scalar fields, so we may need to convert first. */
5233 else
5235 if (TREE_CODE (local.val) == REAL_CST)
5236 local.val
5237 = fold_unary (VIEW_CONVERT_EXPR,
5238 build_nonstandard_integer_type
5239 (TYPE_PRECISION (TREE_TYPE (local.val)), 0),
5240 local.val);
5241 output_constructor_bitfield (&local, outer ? outer->bit_offset : 0);
5245 /* If we are not at toplevel, save the pending data for our caller.
5246 Otherwise output the pending data and padding zeros as needed. */
5247 if (outer)
5248 outer->byte = local.byte;
5249 else
5251 if (local.byte_buffer_in_use)
5253 assemble_integer (GEN_INT (local.byte), 1, BITS_PER_UNIT, 1);
5254 local.total_bytes++;
5257 if ((unsigned HOST_WIDE_INT)local.total_bytes < local.size)
5259 assemble_zeros (local.size - local.total_bytes);
5260 local.total_bytes = local.size;
5264 return local.total_bytes;
5267 /* Mark DECL as weak. */
5269 static void
5270 mark_weak (tree decl)
5272 if (DECL_WEAK (decl))
5273 return;
5275 struct symtab_node *n = symtab_node::get (decl);
5276 if (n && n->refuse_visibility_changes)
5277 error ("%+D declared weak after being used", decl);
5278 DECL_WEAK (decl) = 1;
5280 if (DECL_RTL_SET_P (decl)
5281 && MEM_P (DECL_RTL (decl))
5282 && XEXP (DECL_RTL (decl), 0)
5283 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == SYMBOL_REF)
5284 SYMBOL_REF_WEAK (XEXP (DECL_RTL (decl), 0)) = 1;
5287 /* Merge weak status between NEWDECL and OLDDECL. */
5289 void
5290 merge_weak (tree newdecl, tree olddecl)
5292 if (DECL_WEAK (newdecl) == DECL_WEAK (olddecl))
5294 if (DECL_WEAK (newdecl) && TARGET_SUPPORTS_WEAK)
5296 tree *pwd;
5297 /* We put the NEWDECL on the weak_decls list at some point
5298 and OLDDECL as well. Keep just OLDDECL on the list. */
5299 for (pwd = &weak_decls; *pwd; pwd = &TREE_CHAIN (*pwd))
5300 if (TREE_VALUE (*pwd) == newdecl)
5302 *pwd = TREE_CHAIN (*pwd);
5303 break;
5306 return;
5309 if (DECL_WEAK (newdecl))
5311 tree wd;
5313 /* NEWDECL is weak, but OLDDECL is not. */
5315 /* If we already output the OLDDECL, we're in trouble; we can't
5316 go back and make it weak. This should never happen in
5317 unit-at-a-time compilation. */
5318 gcc_assert (!TREE_ASM_WRITTEN (olddecl));
5320 /* If we've already generated rtl referencing OLDDECL, we may
5321 have done so in a way that will not function properly with
5322 a weak symbol. Again in unit-at-a-time this should be
5323 impossible. */
5324 gcc_assert (!TREE_USED (olddecl)
5325 || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (olddecl)));
5327 if (TARGET_SUPPORTS_WEAK)
5329 /* We put the NEWDECL on the weak_decls list at some point.
5330 Replace it with the OLDDECL. */
5331 for (wd = weak_decls; wd; wd = TREE_CHAIN (wd))
5332 if (TREE_VALUE (wd) == newdecl)
5334 TREE_VALUE (wd) = olddecl;
5335 break;
5337 /* We may not find the entry on the list. If NEWDECL is a
5338 weak alias, then we will have already called
5339 globalize_decl to remove the entry; in that case, we do
5340 not need to do anything. */
5343 /* Make the OLDDECL weak; it's OLDDECL that we'll be keeping. */
5344 mark_weak (olddecl);
5346 else
5347 /* OLDDECL was weak, but NEWDECL was not explicitly marked as
5348 weak. Just update NEWDECL to indicate that it's weak too. */
5349 mark_weak (newdecl);
5352 /* Declare DECL to be a weak symbol. */
5354 void
5355 declare_weak (tree decl)
5357 gcc_assert (TREE_CODE (decl) != FUNCTION_DECL || !TREE_ASM_WRITTEN (decl));
5358 if (! TREE_PUBLIC (decl))
5359 error ("weak declaration of %q+D must be public", decl);
5360 else if (!TARGET_SUPPORTS_WEAK)
5361 warning (0, "weak declaration of %q+D not supported", decl);
5363 mark_weak (decl);
5364 if (!lookup_attribute ("weak", DECL_ATTRIBUTES (decl)))
5365 DECL_ATTRIBUTES (decl)
5366 = tree_cons (get_identifier ("weak"), NULL, DECL_ATTRIBUTES (decl));
5369 static void
5370 weak_finish_1 (tree decl)
5372 #if defined (ASM_WEAKEN_DECL) || defined (ASM_WEAKEN_LABEL)
5373 const char *const name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5374 #endif
5376 if (! TREE_USED (decl))
5377 return;
5379 #ifdef ASM_WEAKEN_DECL
5380 ASM_WEAKEN_DECL (asm_out_file, decl, name, NULL);
5381 #else
5382 #ifdef ASM_WEAKEN_LABEL
5383 ASM_WEAKEN_LABEL (asm_out_file, name);
5384 #else
5385 #ifdef ASM_OUTPUT_WEAK_ALIAS
5387 static bool warn_once = 0;
5388 if (! warn_once)
5390 warning (0, "only weak aliases are supported in this configuration");
5391 warn_once = 1;
5393 return;
5395 #endif
5396 #endif
5397 #endif
5400 /* Fiven an assembly name, find the decl it is associated with. */
5401 static tree
5402 find_decl (tree target)
5404 symtab_node *node = symtab_node::get_for_asmname (target);
5405 if (node)
5406 return node->decl;
5407 return NULL_TREE;
5410 /* This TREE_LIST contains weakref targets. */
5412 static GTY(()) tree weakref_targets;
5414 /* Emit any pending weak declarations. */
5416 void
5417 weak_finish (void)
5419 tree t;
5421 for (t = weakref_targets; t; t = TREE_CHAIN (t))
5423 tree alias_decl = TREE_PURPOSE (t);
5424 tree target = ultimate_transparent_alias_target (&TREE_VALUE (t));
5426 if (! TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (alias_decl)))
5427 /* Remove alias_decl from the weak list, but leave entries for
5428 the target alone. */
5429 target = NULL_TREE;
5430 #ifndef ASM_OUTPUT_WEAKREF
5431 else if (! TREE_SYMBOL_REFERENCED (target))
5433 /* Use ASM_WEAKEN_LABEL only if ASM_WEAKEN_DECL is not
5434 defined, otherwise we and weak_finish_1 would use
5435 different macros. */
5436 # if defined ASM_WEAKEN_LABEL && ! defined ASM_WEAKEN_DECL
5437 ASM_WEAKEN_LABEL (asm_out_file, IDENTIFIER_POINTER (target));
5438 # else
5439 tree decl = find_decl (target);
5441 if (! decl)
5443 decl = build_decl (DECL_SOURCE_LOCATION (alias_decl),
5444 TREE_CODE (alias_decl), target,
5445 TREE_TYPE (alias_decl));
5447 DECL_EXTERNAL (decl) = 1;
5448 TREE_PUBLIC (decl) = 1;
5449 DECL_ARTIFICIAL (decl) = 1;
5450 TREE_NOTHROW (decl) = TREE_NOTHROW (alias_decl);
5451 TREE_USED (decl) = 1;
5454 weak_finish_1 (decl);
5455 # endif
5457 #endif
5460 tree *p;
5461 tree t2;
5463 /* Remove the alias and the target from the pending weak list
5464 so that we do not emit any .weak directives for the former,
5465 nor multiple .weak directives for the latter. */
5466 for (p = &weak_decls; (t2 = *p) ; )
5468 if (TREE_VALUE (t2) == alias_decl
5469 || target == DECL_ASSEMBLER_NAME (TREE_VALUE (t2)))
5470 *p = TREE_CHAIN (t2);
5471 else
5472 p = &TREE_CHAIN (t2);
5475 /* Remove other weakrefs to the same target, to speed things up. */
5476 for (p = &TREE_CHAIN (t); (t2 = *p) ; )
5478 if (target == ultimate_transparent_alias_target (&TREE_VALUE (t2)))
5479 *p = TREE_CHAIN (t2);
5480 else
5481 p = &TREE_CHAIN (t2);
5486 for (t = weak_decls; t; t = TREE_CHAIN (t))
5488 tree decl = TREE_VALUE (t);
5490 weak_finish_1 (decl);
5494 /* Emit the assembly bits to indicate that DECL is globally visible. */
5496 static void
5497 globalize_decl (tree decl)
5500 #if defined (ASM_WEAKEN_LABEL) || defined (ASM_WEAKEN_DECL)
5501 if (DECL_WEAK (decl))
5503 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
5504 tree *p, t;
5506 #ifdef ASM_WEAKEN_DECL
5507 ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
5508 #else
5509 ASM_WEAKEN_LABEL (asm_out_file, name);
5510 #endif
5512 /* Remove this function from the pending weak list so that
5513 we do not emit multiple .weak directives for it. */
5514 for (p = &weak_decls; (t = *p) ; )
5516 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
5517 *p = TREE_CHAIN (t);
5518 else
5519 p = &TREE_CHAIN (t);
5522 /* Remove weakrefs to the same target from the pending weakref
5523 list, for the same reason. */
5524 for (p = &weakref_targets; (t = *p) ; )
5526 if (DECL_ASSEMBLER_NAME (decl)
5527 == ultimate_transparent_alias_target (&TREE_VALUE (t)))
5528 *p = TREE_CHAIN (t);
5529 else
5530 p = &TREE_CHAIN (t);
5533 return;
5535 #endif
5537 targetm.asm_out.globalize_decl_name (asm_out_file, decl);
5540 vec<alias_pair, va_gc> *alias_pairs;
5542 /* Output the assembler code for a define (equate) using ASM_OUTPUT_DEF
5543 or ASM_OUTPUT_DEF_FROM_DECLS. The function defines the symbol whose
5544 tree node is DECL to have the value of the tree node TARGET. */
5546 void
5547 do_assemble_alias (tree decl, tree target)
5549 tree id;
5551 /* Emulated TLS had better not get this var. */
5552 gcc_assert (!(!targetm.have_tls
5553 && TREE_CODE (decl) == VAR_DECL
5554 && DECL_THREAD_LOCAL_P (decl)));
5556 if (TREE_ASM_WRITTEN (decl))
5557 return;
5559 id = DECL_ASSEMBLER_NAME (decl);
5560 ultimate_transparent_alias_target (&id);
5562 /* We must force creation of DECL_RTL for debug info generation, even though
5563 we don't use it here. */
5564 make_decl_rtl (decl);
5566 TREE_ASM_WRITTEN (decl) = 1;
5567 TREE_ASM_WRITTEN (DECL_ASSEMBLER_NAME (decl)) = 1;
5568 TREE_ASM_WRITTEN (id) = 1;
5570 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
5572 ultimate_transparent_alias_target (&target);
5574 if (!TREE_SYMBOL_REFERENCED (target))
5575 weakref_targets = tree_cons (decl, target, weakref_targets);
5577 #ifdef ASM_OUTPUT_WEAKREF
5578 ASM_OUTPUT_WEAKREF (asm_out_file, decl,
5579 IDENTIFIER_POINTER (id),
5580 IDENTIFIER_POINTER (target));
5581 #else
5582 if (!TARGET_SUPPORTS_WEAK)
5584 error_at (DECL_SOURCE_LOCATION (decl),
5585 "weakref is not supported in this configuration");
5586 return;
5588 #endif
5589 return;
5592 #ifdef ASM_OUTPUT_DEF
5593 tree orig_decl = decl;
5595 if (TREE_CODE (decl) == FUNCTION_DECL
5596 && cgraph_node::get (decl)->instrumentation_clone
5597 && cgraph_node::get (decl)->instrumented_version)
5598 orig_decl = cgraph_node::get (decl)->instrumented_version->decl;
5600 /* Make name accessible from other files, if appropriate. */
5602 if (TREE_PUBLIC (decl) || TREE_PUBLIC (orig_decl))
5604 globalize_decl (decl);
5605 maybe_assemble_visibility (decl);
5607 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
5609 #if defined (ASM_OUTPUT_TYPE_DIRECTIVE)
5610 if (targetm.has_ifunc_p ())
5611 ASM_OUTPUT_TYPE_DIRECTIVE
5612 (asm_out_file, IDENTIFIER_POINTER (id),
5613 IFUNC_ASM_TYPE);
5614 else
5615 #endif
5616 error_at (DECL_SOURCE_LOCATION (decl),
5617 "ifunc is not supported on this target");
5620 # ifdef ASM_OUTPUT_DEF_FROM_DECLS
5621 ASM_OUTPUT_DEF_FROM_DECLS (asm_out_file, decl, target);
5622 # else
5623 ASM_OUTPUT_DEF (asm_out_file,
5624 IDENTIFIER_POINTER (id),
5625 IDENTIFIER_POINTER (target));
5626 # endif
5627 #elif defined (ASM_OUTPUT_WEAK_ALIAS) || defined (ASM_WEAKEN_DECL)
5629 const char *name;
5630 tree *p, t;
5632 name = IDENTIFIER_POINTER (id);
5633 # ifdef ASM_WEAKEN_DECL
5634 ASM_WEAKEN_DECL (asm_out_file, decl, name, IDENTIFIER_POINTER (target));
5635 # else
5636 ASM_OUTPUT_WEAK_ALIAS (asm_out_file, name, IDENTIFIER_POINTER (target));
5637 # endif
5638 /* Remove this function from the pending weak list so that
5639 we do not emit multiple .weak directives for it. */
5640 for (p = &weak_decls; (t = *p) ; )
5641 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t))
5642 || id == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
5643 *p = TREE_CHAIN (t);
5644 else
5645 p = &TREE_CHAIN (t);
5647 /* Remove weakrefs to the same target from the pending weakref
5648 list, for the same reason. */
5649 for (p = &weakref_targets; (t = *p) ; )
5651 if (id == ultimate_transparent_alias_target (&TREE_VALUE (t)))
5652 *p = TREE_CHAIN (t);
5653 else
5654 p = &TREE_CHAIN (t);
5657 #endif
5660 /* Emit an assembler directive to make the symbol for DECL an alias to
5661 the symbol for TARGET. */
5663 void
5664 assemble_alias (tree decl, tree target)
5666 tree target_decl;
5668 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
5670 tree alias = DECL_ASSEMBLER_NAME (decl);
5672 ultimate_transparent_alias_target (&target);
5674 if (alias == target)
5675 error ("weakref %q+D ultimately targets itself", decl);
5676 if (TREE_PUBLIC (decl))
5677 error ("weakref %q+D must have static linkage", decl);
5679 else
5681 #if !defined (ASM_OUTPUT_DEF)
5682 # if !defined(ASM_OUTPUT_WEAK_ALIAS) && !defined (ASM_WEAKEN_DECL)
5683 error_at (DECL_SOURCE_LOCATION (decl),
5684 "alias definitions not supported in this configuration");
5685 TREE_ASM_WRITTEN (decl) = 1;
5686 return;
5687 # else
5688 if (!DECL_WEAK (decl))
5690 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
5691 error_at (DECL_SOURCE_LOCATION (decl),
5692 "ifunc is not supported in this configuration");
5693 else
5694 error_at (DECL_SOURCE_LOCATION (decl),
5695 "only weak aliases are supported in this configuration");
5696 TREE_ASM_WRITTEN (decl) = 1;
5697 return;
5699 # endif
5700 #endif
5702 TREE_USED (decl) = 1;
5704 /* Allow aliases to aliases. */
5705 if (TREE_CODE (decl) == FUNCTION_DECL)
5706 cgraph_node::get_create (decl)->alias = true;
5707 else
5708 varpool_node::get_create (decl)->alias = true;
5710 /* If the target has already been emitted, we don't have to queue the
5711 alias. This saves a tad of memory. */
5712 if (symtab->global_info_ready)
5713 target_decl = find_decl (target);
5714 else
5715 target_decl= NULL;
5716 if ((target_decl && TREE_ASM_WRITTEN (target_decl))
5717 || symtab->state >= EXPANSION)
5718 do_assemble_alias (decl, target);
5719 else
5721 alias_pair p = {decl, target};
5722 vec_safe_push (alias_pairs, p);
5726 /* Record and output a table of translations from original function
5727 to its transaction aware clone. Note that tm_pure functions are
5728 considered to be their own clone. */
5730 static GTY((if_marked ("tree_map_marked_p"), param_is (struct tree_map)))
5731 htab_t tm_clone_hash;
5733 void
5734 record_tm_clone_pair (tree o, tree n)
5736 struct tree_map **slot, *h;
5738 if (tm_clone_hash == NULL)
5739 tm_clone_hash = htab_create_ggc (32, tree_map_hash, tree_map_eq, 0);
5741 h = ggc_alloc<tree_map> ();
5742 h->hash = htab_hash_pointer (o);
5743 h->base.from = o;
5744 h->to = n;
5746 slot = (struct tree_map **)
5747 htab_find_slot_with_hash (tm_clone_hash, h, h->hash, INSERT);
5748 *slot = h;
5751 tree
5752 get_tm_clone_pair (tree o)
5754 if (tm_clone_hash)
5756 struct tree_map *h, in;
5758 in.base.from = o;
5759 in.hash = htab_hash_pointer (o);
5760 h = (struct tree_map *) htab_find_with_hash (tm_clone_hash,
5761 &in, in.hash);
5762 if (h)
5763 return h->to;
5765 return NULL_TREE;
5768 typedef struct tm_alias_pair
5770 unsigned int uid;
5771 tree from;
5772 tree to;
5773 } tm_alias_pair;
5776 /* Helper function for finish_tm_clone_pairs. Dump a hash table entry
5777 into a VEC in INFO. */
5779 static int
5780 dump_tm_clone_to_vec (void **slot, void *info)
5782 struct tree_map *map = (struct tree_map *) *slot;
5783 vec<tm_alias_pair> *tm_alias_pairs = (vec<tm_alias_pair> *) info;
5784 tm_alias_pair p = {DECL_UID (map->base.from), map->base.from, map->to};
5785 tm_alias_pairs->safe_push (p);
5786 return 1;
5789 /* Dump the actual pairs to the .tm_clone_table section. */
5791 static void
5792 dump_tm_clone_pairs (vec<tm_alias_pair> tm_alias_pairs)
5794 unsigned i;
5795 tm_alias_pair *p;
5796 bool switched = false;
5798 FOR_EACH_VEC_ELT (tm_alias_pairs, i, p)
5800 tree src = p->from;
5801 tree dst = p->to;
5802 struct cgraph_node *src_n = cgraph_node::get (src);
5803 struct cgraph_node *dst_n = cgraph_node::get (dst);
5805 /* The function ipa_tm_create_version() marks the clone as needed if
5806 the original function was needed. But we also mark the clone as
5807 needed if we ever called the clone indirectly through
5808 TM_GETTMCLONE. If neither of these are true, we didn't generate
5809 a clone, and we didn't call it indirectly... no sense keeping it
5810 in the clone table. */
5811 if (!dst_n || !dst_n->definition)
5812 continue;
5814 /* This covers the case where we have optimized the original
5815 function away, and only access the transactional clone. */
5816 if (!src_n || !src_n->definition)
5817 continue;
5819 if (!switched)
5821 switch_to_section (targetm.asm_out.tm_clone_table_section ());
5822 assemble_align (POINTER_SIZE);
5823 switched = true;
5826 assemble_integer (XEXP (DECL_RTL (src), 0),
5827 POINTER_SIZE_UNITS, POINTER_SIZE, 1);
5828 assemble_integer (XEXP (DECL_RTL (dst), 0),
5829 POINTER_SIZE_UNITS, POINTER_SIZE, 1);
5833 /* Provide a default for the tm_clone_table section. */
5835 section *
5836 default_clone_table_section (void)
5838 return get_named_section (NULL, ".tm_clone_table", 3);
5841 /* Helper comparison function for qsorting by the DECL_UID stored in
5842 alias_pair->emitted_diags. */
5844 static int
5845 tm_alias_pair_cmp (const void *x, const void *y)
5847 const tm_alias_pair *p1 = (const tm_alias_pair *) x;
5848 const tm_alias_pair *p2 = (const tm_alias_pair *) y;
5849 if (p1->uid < p2->uid)
5850 return -1;
5851 if (p1->uid > p2->uid)
5852 return 1;
5853 return 0;
5856 void
5857 finish_tm_clone_pairs (void)
5859 vec<tm_alias_pair> tm_alias_pairs = vNULL;
5861 if (tm_clone_hash == NULL)
5862 return;
5864 /* We need a determenistic order for the .tm_clone_table, otherwise
5865 we will get bootstrap comparison failures, so dump the hash table
5866 to a vector, sort it, and dump the vector. */
5868 /* Dump the hashtable to a vector. */
5869 htab_traverse_noresize (tm_clone_hash, dump_tm_clone_to_vec,
5870 (void *) &tm_alias_pairs);
5871 /* Sort it. */
5872 tm_alias_pairs.qsort (tm_alias_pair_cmp);
5874 /* Dump it. */
5875 dump_tm_clone_pairs (tm_alias_pairs);
5877 htab_delete (tm_clone_hash);
5878 tm_clone_hash = NULL;
5879 tm_alias_pairs.release ();
5883 /* Emit an assembler directive to set symbol for DECL visibility to
5884 the visibility type VIS, which must not be VISIBILITY_DEFAULT. */
5886 void
5887 default_assemble_visibility (tree decl ATTRIBUTE_UNUSED,
5888 int vis ATTRIBUTE_UNUSED)
5890 #ifdef HAVE_GAS_HIDDEN
5891 static const char * const visibility_types[] = {
5892 NULL, "protected", "hidden", "internal"
5895 const char *name, *type;
5897 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5898 type = visibility_types[vis];
5900 fprintf (asm_out_file, "\t.%s\t", type);
5901 assemble_name (asm_out_file, name);
5902 fprintf (asm_out_file, "\n");
5903 #else
5904 if (!DECL_ARTIFICIAL (decl))
5905 warning (OPT_Wattributes, "visibility attribute not supported "
5906 "in this configuration; ignored");
5907 #endif
5910 /* A helper function to call assemble_visibility when needed for a decl. */
5913 maybe_assemble_visibility (tree decl)
5915 enum symbol_visibility vis = DECL_VISIBILITY (decl);
5917 if (TREE_CODE (decl) == FUNCTION_DECL
5918 && cgraph_node::get (decl)
5919 && cgraph_node::get (decl)->instrumentation_clone
5920 && cgraph_node::get (decl)->instrumented_version)
5921 vis = DECL_VISIBILITY (cgraph_node::get (decl)->instrumented_version->decl);
5923 if (vis != VISIBILITY_DEFAULT)
5925 targetm.asm_out.assemble_visibility (decl, vis);
5926 return 1;
5928 else
5929 return 0;
5932 /* Returns 1 if the target configuration supports defining public symbols
5933 so that one of them will be chosen at link time instead of generating a
5934 multiply-defined symbol error, whether through the use of weak symbols or
5935 a target-specific mechanism for having duplicates discarded. */
5938 supports_one_only (void)
5940 if (SUPPORTS_ONE_ONLY)
5941 return 1;
5942 return TARGET_SUPPORTS_WEAK;
5945 /* Set up DECL as a public symbol that can be defined in multiple
5946 translation units without generating a linker error. */
5948 void
5949 make_decl_one_only (tree decl, tree comdat_group)
5951 struct symtab_node *symbol;
5952 gcc_assert (TREE_CODE (decl) == VAR_DECL
5953 || TREE_CODE (decl) == FUNCTION_DECL);
5955 TREE_PUBLIC (decl) = 1;
5957 if (TREE_CODE (decl) == VAR_DECL)
5958 symbol = varpool_node::get_create (decl);
5959 else
5960 symbol = cgraph_node::get_create (decl);
5962 if (SUPPORTS_ONE_ONLY)
5964 #ifdef MAKE_DECL_ONE_ONLY
5965 MAKE_DECL_ONE_ONLY (decl);
5966 #endif
5967 symbol->set_comdat_group (comdat_group);
5969 else if (TREE_CODE (decl) == VAR_DECL
5970 && (DECL_INITIAL (decl) == 0
5971 || (!in_lto_p && DECL_INITIAL (decl) == error_mark_node)))
5972 DECL_COMMON (decl) = 1;
5973 else
5975 gcc_assert (TARGET_SUPPORTS_WEAK);
5976 DECL_WEAK (decl) = 1;
5980 void
5981 init_varasm_once (void)
5983 section_htab = hash_table<section_hasher>::create_ggc (31);
5984 object_block_htab = hash_table<object_block_hasher>::create_ggc (31);
5985 const_desc_htab = hash_table<tree_descriptor_hasher>::create_ggc (1009);
5987 shared_constant_pool = create_constant_pool ();
5989 #ifdef TEXT_SECTION_ASM_OP
5990 text_section = get_unnamed_section (SECTION_CODE, output_section_asm_op,
5991 TEXT_SECTION_ASM_OP);
5992 #endif
5994 #ifdef DATA_SECTION_ASM_OP
5995 data_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
5996 DATA_SECTION_ASM_OP);
5997 #endif
5999 #ifdef SDATA_SECTION_ASM_OP
6000 sdata_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
6001 SDATA_SECTION_ASM_OP);
6002 #endif
6004 #ifdef READONLY_DATA_SECTION_ASM_OP
6005 readonly_data_section = get_unnamed_section (0, output_section_asm_op,
6006 READONLY_DATA_SECTION_ASM_OP);
6007 #endif
6009 #ifdef CTORS_SECTION_ASM_OP
6010 ctors_section = get_unnamed_section (0, output_section_asm_op,
6011 CTORS_SECTION_ASM_OP);
6012 #endif
6014 #ifdef DTORS_SECTION_ASM_OP
6015 dtors_section = get_unnamed_section (0, output_section_asm_op,
6016 DTORS_SECTION_ASM_OP);
6017 #endif
6019 #ifdef BSS_SECTION_ASM_OP
6020 bss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
6021 output_section_asm_op,
6022 BSS_SECTION_ASM_OP);
6023 #endif
6025 #ifdef SBSS_SECTION_ASM_OP
6026 sbss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
6027 output_section_asm_op,
6028 SBSS_SECTION_ASM_OP);
6029 #endif
6031 tls_comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6032 | SECTION_COMMON, emit_tls_common);
6033 lcomm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6034 | SECTION_COMMON, emit_local);
6035 comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6036 | SECTION_COMMON, emit_common);
6038 #if defined ASM_OUTPUT_ALIGNED_BSS
6039 bss_noswitch_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS,
6040 emit_bss);
6041 #endif
6043 targetm.asm_out.init_sections ();
6045 if (readonly_data_section == NULL)
6046 readonly_data_section = text_section;
6048 #ifdef ASM_OUTPUT_EXTERNAL
6049 pending_assemble_externals_set = new hash_set<tree>;
6050 #endif
6053 enum tls_model
6054 decl_default_tls_model (const_tree decl)
6056 enum tls_model kind;
6057 bool is_local;
6059 is_local = targetm.binds_local_p (decl);
6060 if (!flag_shlib)
6062 if (is_local)
6063 kind = TLS_MODEL_LOCAL_EXEC;
6064 else
6065 kind = TLS_MODEL_INITIAL_EXEC;
6068 /* Local dynamic is inefficient when we're not combining the
6069 parts of the address. */
6070 else if (optimize && is_local)
6071 kind = TLS_MODEL_LOCAL_DYNAMIC;
6072 else
6073 kind = TLS_MODEL_GLOBAL_DYNAMIC;
6074 if (kind < flag_tls_default)
6075 kind = flag_tls_default;
6077 return kind;
6080 /* Select a set of attributes for section NAME based on the properties
6081 of DECL and whether or not RELOC indicates that DECL's initializer
6082 might contain runtime relocations.
6084 We make the section read-only and executable for a function decl,
6085 read-only for a const data decl, and writable for a non-const data decl. */
6087 unsigned int
6088 default_section_type_flags (tree decl, const char *name, int reloc)
6090 unsigned int flags;
6092 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
6093 flags = SECTION_CODE;
6094 else if (decl)
6096 enum section_category category
6097 = categorize_decl_for_section (decl, reloc);
6098 if (decl_readonly_section_1 (category))
6099 flags = 0;
6100 else if (category == SECCAT_DATA_REL_RO
6101 || category == SECCAT_DATA_REL_RO_LOCAL)
6102 flags = SECTION_WRITE | SECTION_RELRO;
6103 else
6104 flags = SECTION_WRITE;
6106 else
6108 flags = SECTION_WRITE;
6109 if (strcmp (name, ".data.rel.ro") == 0
6110 || strcmp (name, ".data.rel.ro.local") == 0)
6111 flags |= SECTION_RELRO;
6114 if (decl && DECL_P (decl) && DECL_COMDAT_GROUP (decl))
6115 flags |= SECTION_LINKONCE;
6117 if (strcmp (name, ".vtable_map_vars") == 0)
6118 flags |= SECTION_LINKONCE;
6120 if (decl && TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
6121 flags |= SECTION_TLS | SECTION_WRITE;
6123 if (strcmp (name, ".bss") == 0
6124 || strncmp (name, ".bss.", 5) == 0
6125 || strncmp (name, ".gnu.linkonce.b.", 16) == 0
6126 || strcmp (name, ".persistent.bss") == 0
6127 || strcmp (name, ".sbss") == 0
6128 || strncmp (name, ".sbss.", 6) == 0
6129 || strncmp (name, ".gnu.linkonce.sb.", 17) == 0)
6130 flags |= SECTION_BSS;
6132 if (strcmp (name, ".tdata") == 0
6133 || strncmp (name, ".tdata.", 7) == 0
6134 || strncmp (name, ".gnu.linkonce.td.", 17) == 0)
6135 flags |= SECTION_TLS;
6137 if (strcmp (name, ".tbss") == 0
6138 || strncmp (name, ".tbss.", 6) == 0
6139 || strncmp (name, ".gnu.linkonce.tb.", 17) == 0)
6140 flags |= SECTION_TLS | SECTION_BSS;
6142 /* These three sections have special ELF types. They are neither
6143 SHT_PROGBITS nor SHT_NOBITS, so when changing sections we don't
6144 want to print a section type (@progbits or @nobits). If someone
6145 is silly enough to emit code or TLS variables to one of these
6146 sections, then don't handle them specially. */
6147 if (!(flags & (SECTION_CODE | SECTION_BSS | SECTION_TLS))
6148 && (strcmp (name, ".init_array") == 0
6149 || strcmp (name, ".fini_array") == 0
6150 || strcmp (name, ".preinit_array") == 0))
6151 flags |= SECTION_NOTYPE;
6153 return flags;
6156 /* Return true if the target supports some form of global BSS,
6157 either through bss_noswitch_section, or by selecting a BSS
6158 section in TARGET_ASM_SELECT_SECTION. */
6160 bool
6161 have_global_bss_p (void)
6163 return bss_noswitch_section || targetm.have_switchable_bss_sections;
6166 /* Output assembly to switch to section NAME with attribute FLAGS.
6167 Four variants for common object file formats. */
6169 void
6170 default_no_named_section (const char *name ATTRIBUTE_UNUSED,
6171 unsigned int flags ATTRIBUTE_UNUSED,
6172 tree decl ATTRIBUTE_UNUSED)
6174 /* Some object formats don't support named sections at all. The
6175 front-end should already have flagged this as an error. */
6176 gcc_unreachable ();
6179 #ifndef TLS_SECTION_ASM_FLAG
6180 #define TLS_SECTION_ASM_FLAG 'T'
6181 #endif
6183 void
6184 default_elf_asm_named_section (const char *name, unsigned int flags,
6185 tree decl ATTRIBUTE_UNUSED)
6187 char flagchars[10], *f = flagchars;
6189 /* If we have already declared this section, we can use an
6190 abbreviated form to switch back to it -- unless this section is
6191 part of a COMDAT groups, in which case GAS requires the full
6192 declaration every time. */
6193 if (!(HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6194 && (flags & SECTION_DECLARED))
6196 fprintf (asm_out_file, "\t.section\t%s\n", name);
6197 return;
6200 if (!(flags & SECTION_DEBUG))
6201 *f++ = 'a';
6202 #if defined (HAVE_GAS_SECTION_EXCLUDE) && HAVE_GAS_SECTION_EXCLUDE == 1
6203 if (flags & SECTION_EXCLUDE)
6204 *f++ = 'e';
6205 #endif
6206 if (flags & SECTION_WRITE)
6207 *f++ = 'w';
6208 if (flags & SECTION_CODE)
6209 *f++ = 'x';
6210 if (flags & SECTION_SMALL)
6211 *f++ = 's';
6212 if (flags & SECTION_MERGE)
6213 *f++ = 'M';
6214 if (flags & SECTION_STRINGS)
6215 *f++ = 'S';
6216 if (flags & SECTION_TLS)
6217 *f++ = TLS_SECTION_ASM_FLAG;
6218 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6219 *f++ = 'G';
6220 *f = '\0';
6222 fprintf (asm_out_file, "\t.section\t%s,\"%s\"", name, flagchars);
6224 if (!(flags & SECTION_NOTYPE))
6226 const char *type;
6227 const char *format;
6229 if (flags & SECTION_BSS)
6230 type = "nobits";
6231 else
6232 type = "progbits";
6234 format = ",@%s";
6235 /* On platforms that use "@" as the assembly comment character,
6236 use "%" instead. */
6237 if (strcmp (ASM_COMMENT_START, "@") == 0)
6238 format = ",%%%s";
6239 fprintf (asm_out_file, format, type);
6241 if (flags & SECTION_ENTSIZE)
6242 fprintf (asm_out_file, ",%d", flags & SECTION_ENTSIZE);
6243 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6245 if (TREE_CODE (decl) == IDENTIFIER_NODE)
6246 fprintf (asm_out_file, ",%s,comdat", IDENTIFIER_POINTER (decl));
6247 else
6248 fprintf (asm_out_file, ",%s,comdat",
6249 IDENTIFIER_POINTER (DECL_COMDAT_GROUP (decl)));
6253 putc ('\n', asm_out_file);
6256 void
6257 default_coff_asm_named_section (const char *name, unsigned int flags,
6258 tree decl ATTRIBUTE_UNUSED)
6260 char flagchars[8], *f = flagchars;
6262 if (flags & SECTION_WRITE)
6263 *f++ = 'w';
6264 if (flags & SECTION_CODE)
6265 *f++ = 'x';
6266 *f = '\0';
6268 fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
6271 void
6272 default_pe_asm_named_section (const char *name, unsigned int flags,
6273 tree decl)
6275 default_coff_asm_named_section (name, flags, decl);
6277 if (flags & SECTION_LINKONCE)
6279 /* Functions may have been compiled at various levels of
6280 optimization so we can't use `same_size' here.
6281 Instead, have the linker pick one. */
6282 fprintf (asm_out_file, "\t.linkonce %s\n",
6283 (flags & SECTION_CODE ? "discard" : "same_size"));
6287 /* The lame default section selector. */
6289 section *
6290 default_select_section (tree decl, int reloc,
6291 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
6293 if (DECL_P (decl))
6295 if (decl_readonly_section (decl, reloc))
6296 return readonly_data_section;
6298 else if (TREE_CODE (decl) == CONSTRUCTOR)
6300 if (! ((flag_pic && reloc)
6301 || !TREE_READONLY (decl)
6302 || TREE_SIDE_EFFECTS (decl)
6303 || !TREE_CONSTANT (decl)))
6304 return readonly_data_section;
6306 else if (TREE_CODE (decl) == STRING_CST)
6307 return readonly_data_section;
6308 else if (! (flag_pic && reloc))
6309 return readonly_data_section;
6311 return data_section;
6314 enum section_category
6315 categorize_decl_for_section (const_tree decl, int reloc)
6317 enum section_category ret;
6319 if (TREE_CODE (decl) == FUNCTION_DECL)
6320 return SECCAT_TEXT;
6321 else if (TREE_CODE (decl) == STRING_CST)
6323 if ((flag_sanitize & SANITIZE_ADDRESS)
6324 && asan_protect_global (CONST_CAST_TREE (decl)))
6325 /* or !flag_merge_constants */
6326 return SECCAT_RODATA;
6327 else
6328 return SECCAT_RODATA_MERGE_STR;
6330 else if (TREE_CODE (decl) == VAR_DECL)
6332 if (bss_initializer_p (decl))
6333 ret = SECCAT_BSS;
6334 else if (! TREE_READONLY (decl)
6335 || TREE_SIDE_EFFECTS (decl)
6336 || ! TREE_CONSTANT (DECL_INITIAL (decl)))
6338 /* Here the reloc_rw_mask is not testing whether the section should
6339 be read-only or not, but whether the dynamic link will have to
6340 do something. If so, we wish to segregate the data in order to
6341 minimize cache misses inside the dynamic linker. */
6342 if (reloc & targetm.asm_out.reloc_rw_mask ())
6343 ret = reloc == 1 ? SECCAT_DATA_REL_LOCAL : SECCAT_DATA_REL;
6344 else
6345 ret = SECCAT_DATA;
6347 else if (reloc & targetm.asm_out.reloc_rw_mask ())
6348 ret = reloc == 1 ? SECCAT_DATA_REL_RO_LOCAL : SECCAT_DATA_REL_RO;
6349 else if (reloc || flag_merge_constants < 2
6350 || ((flag_sanitize & SANITIZE_ADDRESS)
6351 && asan_protect_global (CONST_CAST_TREE (decl))))
6352 /* C and C++ don't allow different variables to share the same
6353 location. -fmerge-all-constants allows even that (at the
6354 expense of not conforming). */
6355 ret = SECCAT_RODATA;
6356 else if (TREE_CODE (DECL_INITIAL (decl)) == STRING_CST)
6357 ret = SECCAT_RODATA_MERGE_STR_INIT;
6358 else
6359 ret = SECCAT_RODATA_MERGE_CONST;
6361 else if (TREE_CODE (decl) == CONSTRUCTOR)
6363 if ((reloc & targetm.asm_out.reloc_rw_mask ())
6364 || TREE_SIDE_EFFECTS (decl)
6365 || ! TREE_CONSTANT (decl))
6366 ret = SECCAT_DATA;
6367 else
6368 ret = SECCAT_RODATA;
6370 else
6371 ret = SECCAT_RODATA;
6373 /* There are no read-only thread-local sections. */
6374 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
6376 /* Note that this would be *just* SECCAT_BSS, except that there's
6377 no concept of a read-only thread-local-data section. */
6378 if (ret == SECCAT_BSS
6379 || (flag_zero_initialized_in_bss
6380 && initializer_zerop (DECL_INITIAL (decl))))
6381 ret = SECCAT_TBSS;
6382 else
6383 ret = SECCAT_TDATA;
6386 /* If the target uses small data sections, select it. */
6387 else if (targetm.in_small_data_p (decl))
6389 if (ret == SECCAT_BSS)
6390 ret = SECCAT_SBSS;
6391 else if (targetm.have_srodata_section && ret == SECCAT_RODATA)
6392 ret = SECCAT_SRODATA;
6393 else
6394 ret = SECCAT_SDATA;
6397 return ret;
6400 static bool
6401 decl_readonly_section_1 (enum section_category category)
6403 switch (category)
6405 case SECCAT_RODATA:
6406 case SECCAT_RODATA_MERGE_STR:
6407 case SECCAT_RODATA_MERGE_STR_INIT:
6408 case SECCAT_RODATA_MERGE_CONST:
6409 case SECCAT_SRODATA:
6410 return true;
6411 default:
6412 return false;
6416 bool
6417 decl_readonly_section (const_tree decl, int reloc)
6419 return decl_readonly_section_1 (categorize_decl_for_section (decl, reloc));
6422 /* Select a section based on the above categorization. */
6424 section *
6425 default_elf_select_section (tree decl, int reloc,
6426 unsigned HOST_WIDE_INT align)
6428 const char *sname;
6429 switch (categorize_decl_for_section (decl, reloc))
6431 case SECCAT_TEXT:
6432 /* We're not supposed to be called on FUNCTION_DECLs. */
6433 gcc_unreachable ();
6434 case SECCAT_RODATA:
6435 return readonly_data_section;
6436 case SECCAT_RODATA_MERGE_STR:
6437 return mergeable_string_section (decl, align, 0);
6438 case SECCAT_RODATA_MERGE_STR_INIT:
6439 return mergeable_string_section (DECL_INITIAL (decl), align, 0);
6440 case SECCAT_RODATA_MERGE_CONST:
6441 return mergeable_constant_section (DECL_MODE (decl), align, 0);
6442 case SECCAT_SRODATA:
6443 sname = ".sdata2";
6444 break;
6445 case SECCAT_DATA:
6446 return data_section;
6447 case SECCAT_DATA_REL:
6448 sname = ".data.rel";
6449 break;
6450 case SECCAT_DATA_REL_LOCAL:
6451 sname = ".data.rel.local";
6452 break;
6453 case SECCAT_DATA_REL_RO:
6454 sname = ".data.rel.ro";
6455 break;
6456 case SECCAT_DATA_REL_RO_LOCAL:
6457 sname = ".data.rel.ro.local";
6458 break;
6459 case SECCAT_SDATA:
6460 sname = ".sdata";
6461 break;
6462 case SECCAT_TDATA:
6463 sname = ".tdata";
6464 break;
6465 case SECCAT_BSS:
6466 if (bss_section)
6467 return bss_section;
6468 sname = ".bss";
6469 break;
6470 case SECCAT_SBSS:
6471 sname = ".sbss";
6472 break;
6473 case SECCAT_TBSS:
6474 sname = ".tbss";
6475 break;
6476 default:
6477 gcc_unreachable ();
6480 return get_named_section (decl, sname, reloc);
6483 /* Construct a unique section name based on the decl name and the
6484 categorization performed above. */
6486 void
6487 default_unique_section (tree decl, int reloc)
6489 /* We only need to use .gnu.linkonce if we don't have COMDAT groups. */
6490 bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
6491 const char *prefix, *name, *linkonce;
6492 char *string;
6493 tree id;
6495 switch (categorize_decl_for_section (decl, reloc))
6497 case SECCAT_TEXT:
6498 prefix = one_only ? ".t" : ".text";
6499 break;
6500 case SECCAT_RODATA:
6501 case SECCAT_RODATA_MERGE_STR:
6502 case SECCAT_RODATA_MERGE_STR_INIT:
6503 case SECCAT_RODATA_MERGE_CONST:
6504 prefix = one_only ? ".r" : ".rodata";
6505 break;
6506 case SECCAT_SRODATA:
6507 prefix = one_only ? ".s2" : ".sdata2";
6508 break;
6509 case SECCAT_DATA:
6510 prefix = one_only ? ".d" : ".data";
6511 break;
6512 case SECCAT_DATA_REL:
6513 prefix = one_only ? ".d.rel" : ".data.rel";
6514 break;
6515 case SECCAT_DATA_REL_LOCAL:
6516 prefix = one_only ? ".d.rel.local" : ".data.rel.local";
6517 break;
6518 case SECCAT_DATA_REL_RO:
6519 prefix = one_only ? ".d.rel.ro" : ".data.rel.ro";
6520 break;
6521 case SECCAT_DATA_REL_RO_LOCAL:
6522 prefix = one_only ? ".d.rel.ro.local" : ".data.rel.ro.local";
6523 break;
6524 case SECCAT_SDATA:
6525 prefix = one_only ? ".s" : ".sdata";
6526 break;
6527 case SECCAT_BSS:
6528 prefix = one_only ? ".b" : ".bss";
6529 break;
6530 case SECCAT_SBSS:
6531 prefix = one_only ? ".sb" : ".sbss";
6532 break;
6533 case SECCAT_TDATA:
6534 prefix = one_only ? ".td" : ".tdata";
6535 break;
6536 case SECCAT_TBSS:
6537 prefix = one_only ? ".tb" : ".tbss";
6538 break;
6539 default:
6540 gcc_unreachable ();
6543 id = DECL_ASSEMBLER_NAME (decl);
6544 ultimate_transparent_alias_target (&id);
6545 name = IDENTIFIER_POINTER (id);
6546 name = targetm.strip_name_encoding (name);
6548 /* If we're using one_only, then there needs to be a .gnu.linkonce
6549 prefix to the section name. */
6550 linkonce = one_only ? ".gnu.linkonce" : "";
6552 string = ACONCAT ((linkonce, prefix, ".", name, NULL));
6554 set_decl_section_name (decl, string);
6557 /* Subroutine of compute_reloc_for_rtx for leaf rtxes. */
6559 static int
6560 compute_reloc_for_rtx_1 (const_rtx x)
6562 switch (GET_CODE (x))
6564 case SYMBOL_REF:
6565 return SYMBOL_REF_LOCAL_P (x) ? 1 : 2;
6566 case LABEL_REF:
6567 return 1;
6568 default:
6569 return 0;
6573 /* Like compute_reloc_for_constant, except for an RTX. The return value
6574 is a mask for which bit 1 indicates a global relocation, and bit 0
6575 indicates a local relocation. */
6577 static int
6578 compute_reloc_for_rtx (const_rtx x)
6580 switch (GET_CODE (x))
6582 case SYMBOL_REF:
6583 case LABEL_REF:
6584 return compute_reloc_for_rtx_1 (x);
6586 case CONST:
6588 int reloc = 0;
6589 subrtx_iterator::array_type array;
6590 FOR_EACH_SUBRTX (iter, array, x, ALL)
6591 reloc |= compute_reloc_for_rtx_1 (*iter);
6592 return reloc;
6595 default:
6596 return 0;
6600 section *
6601 default_select_rtx_section (machine_mode mode ATTRIBUTE_UNUSED,
6602 rtx x,
6603 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
6605 if (compute_reloc_for_rtx (x) & targetm.asm_out.reloc_rw_mask ())
6606 return data_section;
6607 else
6608 return readonly_data_section;
6611 section *
6612 default_elf_select_rtx_section (machine_mode mode, rtx x,
6613 unsigned HOST_WIDE_INT align)
6615 int reloc = compute_reloc_for_rtx (x);
6617 /* ??? Handle small data here somehow. */
6619 if (reloc & targetm.asm_out.reloc_rw_mask ())
6621 if (reloc == 1)
6622 return get_named_section (NULL, ".data.rel.ro.local", 1);
6623 else
6624 return get_named_section (NULL, ".data.rel.ro", 3);
6627 return mergeable_constant_section (mode, align, 0);
6630 /* Set the generally applicable flags on the SYMBOL_REF for EXP. */
6632 void
6633 default_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
6635 rtx symbol;
6636 int flags;
6638 /* Careful not to prod global register variables. */
6639 if (!MEM_P (rtl))
6640 return;
6641 symbol = XEXP (rtl, 0);
6642 if (GET_CODE (symbol) != SYMBOL_REF)
6643 return;
6645 flags = SYMBOL_REF_FLAGS (symbol) & SYMBOL_FLAG_HAS_BLOCK_INFO;
6646 if (TREE_CODE (decl) == FUNCTION_DECL)
6647 flags |= SYMBOL_FLAG_FUNCTION;
6648 if (targetm.binds_local_p (decl))
6649 flags |= SYMBOL_FLAG_LOCAL;
6650 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
6651 flags |= DECL_TLS_MODEL (decl) << SYMBOL_FLAG_TLS_SHIFT;
6652 else if (targetm.in_small_data_p (decl))
6653 flags |= SYMBOL_FLAG_SMALL;
6654 /* ??? Why is DECL_EXTERNAL ever set for non-PUBLIC names? Without
6655 being PUBLIC, the thing *must* be defined in this translation unit.
6656 Prevent this buglet from being propagated into rtl code as well. */
6657 if (DECL_P (decl) && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl))
6658 flags |= SYMBOL_FLAG_EXTERNAL;
6660 SYMBOL_REF_FLAGS (symbol) = flags;
6663 /* By default, we do nothing for encode_section_info, so we need not
6664 do anything but discard the '*' marker. */
6666 const char *
6667 default_strip_name_encoding (const char *str)
6669 return str + (*str == '*');
6672 #ifdef ASM_OUTPUT_DEF
6673 /* The default implementation of TARGET_ASM_OUTPUT_ANCHOR. Define the
6674 anchor relative to ".", the current section position. */
6676 void
6677 default_asm_output_anchor (rtx symbol)
6679 char buffer[100];
6681 sprintf (buffer, "*. + " HOST_WIDE_INT_PRINT_DEC,
6682 SYMBOL_REF_BLOCK_OFFSET (symbol));
6683 ASM_OUTPUT_DEF (asm_out_file, XSTR (symbol, 0), buffer);
6685 #endif
6687 /* The default implementation of TARGET_USE_ANCHORS_FOR_SYMBOL_P. */
6689 bool
6690 default_use_anchors_for_symbol_p (const_rtx symbol)
6692 section *sect;
6693 tree decl;
6695 /* Don't use anchors for mergeable sections. The linker might move
6696 the objects around. */
6697 sect = SYMBOL_REF_BLOCK (symbol)->sect;
6698 if (sect->common.flags & SECTION_MERGE)
6699 return false;
6701 /* Don't use anchors for small data sections. The small data register
6702 acts as an anchor for such sections. */
6703 if (sect->common.flags & SECTION_SMALL)
6704 return false;
6706 decl = SYMBOL_REF_DECL (symbol);
6707 if (decl && DECL_P (decl))
6709 /* Don't use section anchors for decls that might be defined or
6710 usurped by other modules. */
6711 if (TREE_PUBLIC (decl) && !decl_binds_to_current_def_p (decl))
6712 return false;
6714 /* Don't use section anchors for decls that will be placed in a
6715 small data section. */
6716 /* ??? Ideally, this check would be redundant with the SECTION_SMALL
6717 one above. The problem is that we only use SECTION_SMALL for
6718 sections that should be marked as small in the section directive. */
6719 if (targetm.in_small_data_p (decl))
6720 return false;
6722 return true;
6725 /* Return true when RESOLUTION indicate that symbol will be bound to the
6726 definition provided by current .o file. */
6728 static bool
6729 resolution_to_local_definition_p (enum ld_plugin_symbol_resolution resolution)
6731 return (resolution == LDPR_PREVAILING_DEF
6732 || resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
6733 || resolution == LDPR_PREVAILING_DEF_IRONLY);
6736 /* Return true when RESOLUTION indicate that symbol will be bound locally
6737 within current executable or DSO. */
6739 static bool
6740 resolution_local_p (enum ld_plugin_symbol_resolution resolution)
6742 return (resolution == LDPR_PREVAILING_DEF
6743 || resolution == LDPR_PREVAILING_DEF_IRONLY
6744 || resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
6745 || resolution == LDPR_PREEMPTED_REG
6746 || resolution == LDPR_PREEMPTED_IR
6747 || resolution == LDPR_RESOLVED_IR
6748 || resolution == LDPR_RESOLVED_EXEC);
6751 /* Assume ELF-ish defaults, since that's pretty much the most liberal
6752 wrt cross-module name binding. */
6754 bool
6755 default_binds_local_p (const_tree exp)
6757 return default_binds_local_p_1 (exp, flag_shlib);
6760 bool
6761 default_binds_local_p_1 (const_tree exp, int shlib)
6763 bool local_p;
6764 bool resolved_locally = false;
6765 bool resolved_to_local_def = false;
6767 /* With resolution file in hands, take look into resolutions.
6768 We can't just return true for resolved_locally symbols,
6769 because dynamic linking might overwrite symbols
6770 in shared libraries. */
6771 if (TREE_CODE (exp) == VAR_DECL && TREE_PUBLIC (exp)
6772 && (TREE_STATIC (exp) || DECL_EXTERNAL (exp)))
6774 varpool_node *vnode = varpool_node::get (exp);
6775 if (vnode && (resolution_local_p (vnode->resolution) || vnode->in_other_partition))
6776 resolved_locally = true;
6777 if (vnode
6778 && resolution_to_local_definition_p (vnode->resolution))
6779 resolved_to_local_def = true;
6781 else if (TREE_CODE (exp) == FUNCTION_DECL && TREE_PUBLIC (exp))
6783 struct cgraph_node *node = cgraph_node::get (exp);
6784 if (node
6785 && (resolution_local_p (node->resolution) || node->in_other_partition))
6786 resolved_locally = true;
6787 if (node
6788 && resolution_to_local_definition_p (node->resolution))
6789 resolved_to_local_def = true;
6792 /* A non-decl is an entry in the constant pool. */
6793 if (!DECL_P (exp))
6794 local_p = true;
6795 /* Weakrefs may not bind locally, even though the weakref itself is always
6796 static and therefore local. Similarly, the resolver for ifunc functions
6797 might resolve to a non-local function.
6798 FIXME: We can resolve the weakref case more curefuly by looking at the
6799 weakref alias. */
6800 else if (lookup_attribute ("weakref", DECL_ATTRIBUTES (exp))
6801 || (TREE_CODE (exp) == FUNCTION_DECL
6802 && lookup_attribute ("ifunc", DECL_ATTRIBUTES (exp))))
6803 local_p = false;
6804 /* Static variables are always local. */
6805 else if (! TREE_PUBLIC (exp))
6806 local_p = true;
6807 /* A variable is local if the user has said explicitly that it will
6808 be. */
6809 else if ((DECL_VISIBILITY_SPECIFIED (exp)
6810 || resolved_to_local_def)
6811 && DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
6812 local_p = true;
6813 /* Variables defined outside this object might not be local. */
6814 else if (DECL_EXTERNAL (exp) && !resolved_locally)
6815 local_p = false;
6816 /* If defined in this object and visibility is not default, must be
6817 local. */
6818 else if (DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
6819 local_p = true;
6820 /* Default visibility weak data can be overridden by a strong symbol
6821 in another module and so are not local. */
6822 else if (DECL_WEAK (exp)
6823 && !resolved_locally)
6824 local_p = false;
6825 /* If PIC, then assume that any global name can be overridden by
6826 symbols resolved from other modules. */
6827 else if (shlib)
6828 local_p = false;
6829 /* Uninitialized COMMON variable may be unified with symbols
6830 resolved from other modules. */
6831 else if (DECL_COMMON (exp)
6832 && !resolved_locally
6833 && (DECL_INITIAL (exp) == NULL
6834 || (!in_lto_p && DECL_INITIAL (exp) == error_mark_node)))
6835 local_p = false;
6836 /* Otherwise we're left with initialized (or non-common) global data
6837 which is of necessity defined locally. */
6838 else
6839 local_p = true;
6841 return local_p;
6844 /* Return true when references to DECL must bind to current definition in
6845 final executable.
6847 The condition is usually equivalent to whether the function binds to the
6848 current module (shared library or executable), that is to binds_local_p.
6849 We use this fact to avoid need for another target hook and implement
6850 the logic using binds_local_p and just special cases where
6851 decl_binds_to_current_def_p is stronger than binds_local_p. In particular
6852 the weak definitions (that can be overwritten at linktime by other
6853 definition from different object file) and when resolution info is available
6854 we simply use the knowledge passed to us by linker plugin. */
6855 bool
6856 decl_binds_to_current_def_p (const_tree decl)
6858 gcc_assert (DECL_P (decl));
6859 if (!targetm.binds_local_p (decl))
6860 return false;
6861 if (!TREE_PUBLIC (decl))
6862 return true;
6863 /* When resolution is available, just use it. */
6864 if (TREE_CODE (decl) == VAR_DECL
6865 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
6867 varpool_node *vnode = varpool_node::get (decl);
6868 if (vnode
6869 && vnode->resolution != LDPR_UNKNOWN)
6870 return resolution_to_local_definition_p (vnode->resolution);
6872 else if (TREE_CODE (decl) == FUNCTION_DECL)
6874 struct cgraph_node *node = cgraph_node::get (decl);
6875 if (node
6876 && node->resolution != LDPR_UNKNOWN)
6877 return resolution_to_local_definition_p (node->resolution);
6879 /* Otherwise we have to assume the worst for DECL_WEAK (hidden weaks
6880 binds locally but still can be overwritten), DECL_COMMON (can be merged
6881 with a non-common definition somewhere in the same module) or
6882 DECL_EXTERNAL.
6883 This rely on fact that binds_local_p behave as decl_replaceable_p
6884 for all other declaration types. */
6885 if (DECL_WEAK (decl))
6886 return false;
6887 if (DECL_COMMON (decl)
6888 && (DECL_INITIAL (decl) == NULL
6889 || (!in_lto_p && DECL_INITIAL (decl) == error_mark_node)))
6890 return false;
6891 if (DECL_EXTERNAL (decl))
6892 return false;
6893 return true;
6896 /* A replaceable function or variable is one which may be replaced
6897 at link-time with an entirely different definition, provided that the
6898 replacement has the same type. For example, functions declared
6899 with __attribute__((weak)) on most systems are replaceable.
6901 COMDAT functions are not replaceable, since all definitions of the
6902 function must be equivalent. It is important that COMDAT functions
6903 not be treated as replaceable so that use of C++ template
6904 instantiations is not penalized. */
6906 bool
6907 decl_replaceable_p (tree decl)
6909 gcc_assert (DECL_P (decl));
6910 if (!TREE_PUBLIC (decl) || DECL_COMDAT (decl))
6911 return false;
6912 if (!flag_semantic_interposition
6913 && !DECL_WEAK (decl))
6914 return false;
6915 return !decl_binds_to_current_def_p (decl);
6918 /* Default function to output code that will globalize a label. A
6919 target must define GLOBAL_ASM_OP or provide its own function to
6920 globalize a label. */
6921 #ifdef GLOBAL_ASM_OP
6922 void
6923 default_globalize_label (FILE * stream, const char *name)
6925 fputs (GLOBAL_ASM_OP, stream);
6926 assemble_name (stream, name);
6927 putc ('\n', stream);
6929 #endif /* GLOBAL_ASM_OP */
6931 /* Default function to output code that will globalize a declaration. */
6932 void
6933 default_globalize_decl_name (FILE * stream, tree decl)
6935 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
6936 targetm.asm_out.globalize_label (stream, name);
6939 /* Default function to output a label for unwind information. The
6940 default is to do nothing. A target that needs nonlocal labels for
6941 unwind information must provide its own function to do this. */
6942 void
6943 default_emit_unwind_label (FILE * stream ATTRIBUTE_UNUSED,
6944 tree decl ATTRIBUTE_UNUSED,
6945 int for_eh ATTRIBUTE_UNUSED,
6946 int empty ATTRIBUTE_UNUSED)
6950 /* Default function to output a label to divide up the exception table.
6951 The default is to do nothing. A target that needs/wants to divide
6952 up the table must provide it's own function to do this. */
6953 void
6954 default_emit_except_table_label (FILE * stream ATTRIBUTE_UNUSED)
6958 /* This is how to output an internal numbered label where PREFIX is
6959 the class of label and LABELNO is the number within the class. */
6961 void
6962 default_generate_internal_label (char *buf, const char *prefix,
6963 unsigned long labelno)
6965 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
6968 /* This is how to output an internal numbered label where PREFIX is
6969 the class of label and LABELNO is the number within the class. */
6971 void
6972 default_internal_label (FILE *stream, const char *prefix,
6973 unsigned long labelno)
6975 char *const buf = (char *) alloca (40 + strlen (prefix));
6976 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
6977 ASM_OUTPUT_INTERNAL_LABEL (stream, buf);
6981 /* The default implementation of ASM_DECLARE_CONSTANT_NAME. */
6983 void
6984 default_asm_declare_constant_name (FILE *file, const char *name,
6985 const_tree exp ATTRIBUTE_UNUSED,
6986 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
6988 assemble_label (file, name);
6991 /* This is the default behavior at the beginning of a file. It's
6992 controlled by two other target-hook toggles. */
6993 void
6994 default_file_start (void)
6996 if (targetm.asm_file_start_app_off
6997 && !(flag_verbose_asm || flag_debug_asm || flag_dump_rtl_in_asm))
6998 fputs (ASM_APP_OFF, asm_out_file);
7000 if (targetm.asm_file_start_file_directive)
7001 output_file_directive (asm_out_file, main_input_filename);
7004 /* This is a generic routine suitable for use as TARGET_ASM_FILE_END
7005 which emits a special section directive used to indicate whether or
7006 not this object file needs an executable stack. This is primarily
7007 a GNU extension to ELF but could be used on other targets. */
7009 int trampolines_created;
7011 void
7012 file_end_indicate_exec_stack (void)
7014 unsigned int flags = SECTION_DEBUG;
7015 if (trampolines_created)
7016 flags |= SECTION_CODE;
7018 switch_to_section (get_section (".note.GNU-stack", flags, NULL));
7021 /* Emit a special section directive to indicate that this object file
7022 was compiled with -fsplit-stack. This is used to let the linker
7023 detect calls between split-stack code and non-split-stack code, so
7024 that it can modify the split-stack code to allocate a sufficiently
7025 large stack. We emit another special section if there are any
7026 functions in this file which have the no_split_stack attribute, to
7027 prevent the linker from warning about being unable to convert the
7028 functions if they call non-split-stack code. */
7030 void
7031 file_end_indicate_split_stack (void)
7033 if (flag_split_stack)
7035 switch_to_section (get_section (".note.GNU-split-stack", SECTION_DEBUG,
7036 NULL));
7037 if (saw_no_split_stack)
7038 switch_to_section (get_section (".note.GNU-no-split-stack",
7039 SECTION_DEBUG, NULL));
7043 /* Output DIRECTIVE (a C string) followed by a newline. This is used as
7044 a get_unnamed_section callback. */
7046 void
7047 output_section_asm_op (const void *directive)
7049 fprintf (asm_out_file, "%s\n", (const char *) directive);
7052 /* Emit assembly code to switch to section NEW_SECTION. Do nothing if
7053 the current section is NEW_SECTION. */
7055 void
7056 switch_to_section (section *new_section)
7058 if (in_section == new_section)
7059 return;
7061 if (new_section->common.flags & SECTION_FORGET)
7062 in_section = NULL;
7063 else
7064 in_section = new_section;
7066 switch (SECTION_STYLE (new_section))
7068 case SECTION_NAMED:
7069 targetm.asm_out.named_section (new_section->named.name,
7070 new_section->named.common.flags,
7071 new_section->named.decl);
7072 break;
7074 case SECTION_UNNAMED:
7075 new_section->unnamed.callback (new_section->unnamed.data);
7076 break;
7078 case SECTION_NOSWITCH:
7079 gcc_unreachable ();
7080 break;
7083 new_section->common.flags |= SECTION_DECLARED;
7086 /* If block symbol SYMBOL has not yet been assigned an offset, place
7087 it at the end of its block. */
7089 void
7090 place_block_symbol (rtx symbol)
7092 unsigned HOST_WIDE_INT size, mask, offset;
7093 struct constant_descriptor_rtx *desc;
7094 unsigned int alignment;
7095 struct object_block *block;
7096 tree decl;
7098 gcc_assert (SYMBOL_REF_BLOCK (symbol));
7099 if (SYMBOL_REF_BLOCK_OFFSET (symbol) >= 0)
7100 return;
7102 /* Work out the symbol's size and alignment. */
7103 if (CONSTANT_POOL_ADDRESS_P (symbol))
7105 desc = SYMBOL_REF_CONSTANT (symbol);
7106 alignment = desc->align;
7107 size = GET_MODE_SIZE (desc->mode);
7109 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
7111 decl = SYMBOL_REF_DECL (symbol);
7112 alignment = DECL_ALIGN (decl);
7113 size = get_constant_size (DECL_INITIAL (decl));
7114 if ((flag_sanitize & SANITIZE_ADDRESS)
7115 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
7116 && asan_protect_global (DECL_INITIAL (decl)))
7117 size += asan_red_zone_size (size);
7119 else
7121 struct symtab_node *snode;
7122 decl = SYMBOL_REF_DECL (symbol);
7124 snode = symtab_node::get (decl);
7125 if (snode->alias)
7127 rtx target = DECL_RTL (snode->ultimate_alias_target ()->decl);
7129 place_block_symbol (target);
7130 SYMBOL_REF_BLOCK_OFFSET (symbol) = SYMBOL_REF_BLOCK_OFFSET (target);
7131 return;
7133 alignment = get_variable_align (decl);
7134 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
7135 if ((flag_sanitize & SANITIZE_ADDRESS)
7136 && asan_protect_global (decl))
7138 size += asan_red_zone_size (size);
7139 alignment = MAX (alignment,
7140 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
7144 /* Calculate the object's offset from the start of the block. */
7145 block = SYMBOL_REF_BLOCK (symbol);
7146 mask = alignment / BITS_PER_UNIT - 1;
7147 offset = (block->size + mask) & ~mask;
7148 SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
7150 /* Record the block's new alignment and size. */
7151 block->alignment = MAX (block->alignment, alignment);
7152 block->size = offset + size;
7154 vec_safe_push (block->objects, symbol);
7157 /* Return the anchor that should be used to address byte offset OFFSET
7158 from the first object in BLOCK. MODEL is the TLS model used
7159 to access it. */
7162 get_section_anchor (struct object_block *block, HOST_WIDE_INT offset,
7163 enum tls_model model)
7165 char label[100];
7166 unsigned int begin, middle, end;
7167 unsigned HOST_WIDE_INT min_offset, max_offset, range, bias, delta;
7168 rtx anchor;
7170 /* Work out the anchor's offset. Use an offset of 0 for the first
7171 anchor so that we don't pessimize the case where we take the address
7172 of a variable at the beginning of the block. This is particularly
7173 useful when a block has only one variable assigned to it.
7175 We try to place anchors RANGE bytes apart, so there can then be
7176 anchors at +/-RANGE, +/-2 * RANGE, and so on, up to the limits of
7177 a ptr_mode offset. With some target settings, the lowest such
7178 anchor might be out of range for the lowest ptr_mode offset;
7179 likewise the highest anchor for the highest offset. Use anchors
7180 at the extreme ends of the ptr_mode range in such cases.
7182 All arithmetic uses unsigned integers in order to avoid
7183 signed overflow. */
7184 max_offset = (unsigned HOST_WIDE_INT) targetm.max_anchor_offset;
7185 min_offset = (unsigned HOST_WIDE_INT) targetm.min_anchor_offset;
7186 range = max_offset - min_offset + 1;
7187 if (range == 0)
7188 offset = 0;
7189 else
7191 bias = 1 << (GET_MODE_BITSIZE (ptr_mode) - 1);
7192 if (offset < 0)
7194 delta = -(unsigned HOST_WIDE_INT) offset + max_offset;
7195 delta -= delta % range;
7196 if (delta > bias)
7197 delta = bias;
7198 offset = (HOST_WIDE_INT) (-delta);
7200 else
7202 delta = (unsigned HOST_WIDE_INT) offset - min_offset;
7203 delta -= delta % range;
7204 if (delta > bias - 1)
7205 delta = bias - 1;
7206 offset = (HOST_WIDE_INT) delta;
7210 /* Do a binary search to see if there's already an anchor we can use.
7211 Set BEGIN to the new anchor's index if not. */
7212 begin = 0;
7213 end = vec_safe_length (block->anchors);
7214 while (begin != end)
7216 middle = (end + begin) / 2;
7217 anchor = (*block->anchors)[middle];
7218 if (SYMBOL_REF_BLOCK_OFFSET (anchor) > offset)
7219 end = middle;
7220 else if (SYMBOL_REF_BLOCK_OFFSET (anchor) < offset)
7221 begin = middle + 1;
7222 else if (SYMBOL_REF_TLS_MODEL (anchor) > model)
7223 end = middle;
7224 else if (SYMBOL_REF_TLS_MODEL (anchor) < model)
7225 begin = middle + 1;
7226 else
7227 return anchor;
7230 /* Create a new anchor with a unique label. */
7231 ASM_GENERATE_INTERNAL_LABEL (label, "LANCHOR", anchor_labelno++);
7232 anchor = create_block_symbol (ggc_strdup (label), block, offset);
7233 SYMBOL_REF_FLAGS (anchor) |= SYMBOL_FLAG_LOCAL | SYMBOL_FLAG_ANCHOR;
7234 SYMBOL_REF_FLAGS (anchor) |= model << SYMBOL_FLAG_TLS_SHIFT;
7236 /* Insert it at index BEGIN. */
7237 vec_safe_insert (block->anchors, begin, anchor);
7238 return anchor;
7241 /* Output the objects in BLOCK. */
7243 static void
7244 output_object_block (struct object_block *block)
7246 struct constant_descriptor_rtx *desc;
7247 unsigned int i;
7248 HOST_WIDE_INT offset;
7249 tree decl;
7250 rtx symbol;
7252 if (!block->objects)
7253 return;
7255 /* Switch to the section and make sure that the first byte is
7256 suitably aligned. */
7257 switch_to_section (block->sect);
7258 assemble_align (block->alignment);
7260 /* Define the values of all anchors relative to the current section
7261 position. */
7262 FOR_EACH_VEC_SAFE_ELT (block->anchors, i, symbol)
7263 targetm.asm_out.output_anchor (symbol);
7265 /* Output the objects themselves. */
7266 offset = 0;
7267 FOR_EACH_VEC_ELT (*block->objects, i, symbol)
7269 /* Move to the object's offset, padding with zeros if necessary. */
7270 assemble_zeros (SYMBOL_REF_BLOCK_OFFSET (symbol) - offset);
7271 offset = SYMBOL_REF_BLOCK_OFFSET (symbol);
7272 if (CONSTANT_POOL_ADDRESS_P (symbol))
7274 desc = SYMBOL_REF_CONSTANT (symbol);
7275 output_constant_pool_1 (desc, 1);
7276 offset += GET_MODE_SIZE (desc->mode);
7278 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
7280 HOST_WIDE_INT size;
7281 decl = SYMBOL_REF_DECL (symbol);
7282 assemble_constant_contents (DECL_INITIAL (decl), XSTR (symbol, 0),
7283 DECL_ALIGN (decl));
7284 size = get_constant_size (DECL_INITIAL (decl));
7285 offset += size;
7286 if ((flag_sanitize & SANITIZE_ADDRESS)
7287 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
7288 && asan_protect_global (DECL_INITIAL (decl)))
7290 size = asan_red_zone_size (size);
7291 assemble_zeros (size);
7292 offset += size;
7295 else
7297 HOST_WIDE_INT size;
7298 decl = SYMBOL_REF_DECL (symbol);
7299 assemble_variable_contents (decl, XSTR (symbol, 0), false);
7300 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
7301 offset += size;
7302 if ((flag_sanitize & SANITIZE_ADDRESS)
7303 && asan_protect_global (decl))
7305 size = asan_red_zone_size (size);
7306 assemble_zeros (size);
7307 offset += size;
7313 /* A htab_traverse callback used to call output_object_block for
7314 each member of object_block_htab. */
7317 output_object_block_htab (object_block **slot, void *)
7319 output_object_block (*slot);
7320 return 1;
7323 /* Output the definitions of all object_blocks. */
7325 void
7326 output_object_blocks (void)
7328 object_block_htab->traverse<void *, output_object_block_htab> (NULL);
7331 /* This function provides a possible implementation of the
7332 TARGET_ASM_RECORD_GCC_SWITCHES target hook for ELF targets. When triggered
7333 by -frecord-gcc-switches it creates a new mergeable, string section in the
7334 assembler output file called TARGET_ASM_RECORD_GCC_SWITCHES_SECTION which
7335 contains the switches in ASCII format.
7337 FIXME: This code does not correctly handle double quote characters
7338 that appear inside strings, (it strips them rather than preserving them).
7339 FIXME: ASM_OUTPUT_ASCII, as defined in config/elfos.h will not emit NUL
7340 characters - instead it treats them as sub-string separators. Since
7341 we want to emit NUL strings terminators into the object file we have to use
7342 ASM_OUTPUT_SKIP. */
7345 elf_record_gcc_switches (print_switch_type type, const char * name)
7347 switch (type)
7349 case SWITCH_TYPE_PASSED:
7350 ASM_OUTPUT_ASCII (asm_out_file, name, strlen (name));
7351 ASM_OUTPUT_SKIP (asm_out_file, (unsigned HOST_WIDE_INT) 1);
7352 break;
7354 case SWITCH_TYPE_DESCRIPTIVE:
7355 if (name == NULL)
7357 /* Distinguish between invocations where name is NULL. */
7358 static bool started = false;
7360 if (!started)
7362 section * sec;
7364 sec = get_section (targetm.asm_out.record_gcc_switches_section,
7365 SECTION_DEBUG
7366 | SECTION_MERGE
7367 | SECTION_STRINGS
7368 | (SECTION_ENTSIZE & 1),
7369 NULL);
7370 switch_to_section (sec);
7371 started = true;
7375 default:
7376 break;
7379 /* The return value is currently ignored by the caller, but must be 0.
7380 For -fverbose-asm the return value would be the number of characters
7381 emitted into the assembler file. */
7382 return 0;
7385 /* Emit text to declare externally defined symbols. It is needed to
7386 properly support non-default visibility. */
7387 void
7388 default_elf_asm_output_external (FILE *file ATTRIBUTE_UNUSED,
7389 tree decl,
7390 const char *name ATTRIBUTE_UNUSED)
7392 /* We output the name if and only if TREE_SYMBOL_REFERENCED is
7393 set in order to avoid putting out names that are never really
7394 used. */
7395 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
7396 && targetm.binds_local_p (decl))
7397 maybe_assemble_visibility (decl);
7400 /* The default hook for TARGET_ASM_OUTPUT_SOURCE_FILENAME. */
7402 void
7403 default_asm_output_source_filename (FILE *file, const char *name)
7405 #ifdef ASM_OUTPUT_SOURCE_FILENAME
7406 ASM_OUTPUT_SOURCE_FILENAME (file, name);
7407 #else
7408 fprintf (file, "\t.file\t");
7409 output_quoted_string (file, name);
7410 putc ('\n', file);
7411 #endif
7414 /* Output a file name in the form wanted by System V. */
7416 void
7417 output_file_directive (FILE *asm_file, const char *input_name)
7419 int len;
7420 const char *na;
7422 if (input_name == NULL)
7423 input_name = "<stdin>";
7424 else
7425 input_name = remap_debug_filename (input_name);
7427 len = strlen (input_name);
7428 na = input_name + len;
7430 /* NA gets INPUT_NAME sans directory names. */
7431 while (na > input_name)
7433 if (IS_DIR_SEPARATOR (na[-1]))
7434 break;
7435 na--;
7438 targetm.asm_out.output_source_filename (asm_file, na);
7441 /* Create a DEBUG_EXPR_DECL / DEBUG_EXPR pair from RTL expression
7442 EXP. */
7444 make_debug_expr_from_rtl (const_rtx exp)
7446 tree ddecl = make_node (DEBUG_EXPR_DECL), type;
7447 machine_mode mode = GET_MODE (exp);
7448 rtx dval;
7450 DECL_ARTIFICIAL (ddecl) = 1;
7451 if (REG_P (exp) && REG_EXPR (exp))
7452 type = TREE_TYPE (REG_EXPR (exp));
7453 else if (MEM_P (exp) && MEM_EXPR (exp))
7454 type = TREE_TYPE (MEM_EXPR (exp));
7455 else
7456 type = NULL_TREE;
7457 if (type && TYPE_MODE (type) == mode)
7458 TREE_TYPE (ddecl) = type;
7459 else
7460 TREE_TYPE (ddecl) = lang_hooks.types.type_for_mode (mode, 1);
7461 DECL_MODE (ddecl) = mode;
7462 dval = gen_rtx_DEBUG_EXPR (mode);
7463 DEBUG_EXPR_TREE_DECL (dval) = ddecl;
7464 SET_DECL_RTL (ddecl, dval);
7465 return dval;
7468 #ifdef ELF_ASCII_ESCAPES
7469 /* Default ASM_OUTPUT_LIMITED_STRING for ELF targets. */
7471 void
7472 default_elf_asm_output_limited_string (FILE *f, const char *s)
7474 int escape;
7475 unsigned char c;
7477 fputs (STRING_ASM_OP, f);
7478 putc ('"', f);
7479 while (*s != '\0')
7481 c = *s;
7482 escape = ELF_ASCII_ESCAPES[c];
7483 switch (escape)
7485 case 0:
7486 putc (c, f);
7487 break;
7488 case 1:
7489 /* TODO: Print in hex with fast function, important for -flto. */
7490 fprintf (f, "\\%03o", c);
7491 break;
7492 default:
7493 putc ('\\', f);
7494 putc (escape, f);
7495 break;
7497 s++;
7499 putc ('\"', f);
7500 putc ('\n', f);
7503 /* Default ASM_OUTPUT_ASCII for ELF targets. */
7505 void
7506 default_elf_asm_output_ascii (FILE *f, const char *s, unsigned int len)
7508 const char *limit = s + len;
7509 const char *last_null = NULL;
7510 unsigned bytes_in_chunk = 0;
7511 unsigned char c;
7512 int escape;
7514 for (; s < limit; s++)
7516 const char *p;
7518 if (bytes_in_chunk >= 60)
7520 putc ('\"', f);
7521 putc ('\n', f);
7522 bytes_in_chunk = 0;
7525 if (s > last_null)
7527 for (p = s; p < limit && *p != '\0'; p++)
7528 continue;
7529 last_null = p;
7531 else
7532 p = last_null;
7534 if (p < limit && (p - s) <= (long) ELF_STRING_LIMIT)
7536 if (bytes_in_chunk > 0)
7538 putc ('\"', f);
7539 putc ('\n', f);
7540 bytes_in_chunk = 0;
7543 default_elf_asm_output_limited_string (f, s);
7544 s = p;
7546 else
7548 if (bytes_in_chunk == 0)
7549 fputs (ASCII_DATA_ASM_OP "\"", f);
7551 c = *s;
7552 escape = ELF_ASCII_ESCAPES[c];
7553 switch (escape)
7555 case 0:
7556 putc (c, f);
7557 bytes_in_chunk++;
7558 break;
7559 case 1:
7560 /* TODO: Print in hex with fast function, important for -flto. */
7561 fprintf (f, "\\%03o", c);
7562 bytes_in_chunk += 4;
7563 break;
7564 default:
7565 putc ('\\', f);
7566 putc (escape, f);
7567 bytes_in_chunk += 2;
7568 break;
7574 if (bytes_in_chunk > 0)
7576 putc ('\"', f);
7577 putc ('\n', f);
7580 #endif
7582 static GTY(()) section *elf_init_array_section;
7583 static GTY(()) section *elf_fini_array_section;
7585 static section *
7586 get_elf_initfini_array_priority_section (int priority,
7587 bool constructor_p)
7589 section *sec;
7590 if (priority != DEFAULT_INIT_PRIORITY)
7592 char buf[18];
7593 sprintf (buf, "%s.%.5u",
7594 constructor_p ? ".init_array" : ".fini_array",
7595 priority);
7596 sec = get_section (buf, SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
7598 else
7600 if (constructor_p)
7602 if (elf_init_array_section == NULL)
7603 elf_init_array_section
7604 = get_section (".init_array",
7605 SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
7606 sec = elf_init_array_section;
7608 else
7610 if (elf_fini_array_section == NULL)
7611 elf_fini_array_section
7612 = get_section (".fini_array",
7613 SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
7614 sec = elf_fini_array_section;
7617 return sec;
7620 /* Use .init_array section for constructors. */
7622 void
7623 default_elf_init_array_asm_out_constructor (rtx symbol, int priority)
7625 section *sec = get_elf_initfini_array_priority_section (priority,
7626 true);
7627 assemble_addr_to_section (symbol, sec);
7630 /* Use .fini_array section for destructors. */
7632 void
7633 default_elf_fini_array_asm_out_destructor (rtx symbol, int priority)
7635 section *sec = get_elf_initfini_array_priority_section (priority,
7636 false);
7637 assemble_addr_to_section (symbol, sec);
7640 /* Default TARGET_ASM_OUTPUT_IDENT hook.
7642 This is a bit of a cheat. The real default is a no-op, but this
7643 hook is the default for all targets with a .ident directive. */
7645 void
7646 default_asm_output_ident_directive (const char *ident_str)
7648 const char *ident_asm_op = "\t.ident\t";
7650 /* If we are still in the front end, do not write out the string
7651 to asm_out_file. Instead, add a fake top-level asm statement.
7652 This allows the front ends to use this hook without actually
7653 writing to asm_out_file, to handle #ident or Pragma Ident. */
7654 if (symtab->state == PARSING)
7656 char *buf = ACONCAT ((ident_asm_op, "\"", ident_str, "\"\n", NULL));
7657 symtab->finalize_toplevel_asm (build_string (strlen (buf), buf));
7659 else
7660 fprintf (asm_out_file, "%s\"%s\"\n", ident_asm_op, ident_str);
7663 #include "gt-varasm.h"