Merge from trunk @222673.
[official-gcc.git] / gcc / varasm.c
blobe336c891e2b538271c125110c0e865725497354e
1 /* Output variables, constants and external declarations, for GNU compiler.
2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
21 /* This file handles generation of all the assembler code
22 *except* the instructions of a function.
23 This includes declarations of variables and their initial values.
25 We also output the assembler code for constants stored in memory
26 and are responsible for combining constants with the same value. */
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tm.h"
32 #include "rtl.h"
33 #include "hash-set.h"
34 #include "machmode.h"
35 #include "vec.h"
36 #include "double-int.h"
37 #include "input.h"
38 #include "alias.h"
39 #include "symtab.h"
40 #include "wide-int.h"
41 #include "inchash.h"
42 #include "tree.h"
43 #include "fold-const.h"
44 #include "stor-layout.h"
45 #include "stringpool.h"
46 #include "varasm.h"
47 #include "flags.h"
48 #include "hard-reg-set.h"
49 #include "function.h"
50 #include "hashtab.h"
51 #include "statistics.h"
52 #include "real.h"
53 #include "fixed-value.h"
54 #include "insn-config.h"
55 #include "expmed.h"
56 #include "dojump.h"
57 #include "explow.h"
58 #include "calls.h"
59 #include "emit-rtl.h"
60 #include "stmt.h"
61 #include "expr.h"
62 #include "regs.h"
63 #include "output.h"
64 #include "diagnostic-core.h"
65 #include "ggc.h"
66 #include "langhooks.h"
67 #include "tm_p.h"
68 #include "debug.h"
69 #include "target.h"
70 #include "common/common-target.h"
71 #include "targhooks.h"
72 #include "predict.h"
73 #include "dominance.h"
74 #include "cfg.h"
75 #include "basic-block.h"
76 #include "hash-map.h"
77 #include "is-a.h"
78 #include "plugin-api.h"
79 #include "ipa-ref.h"
80 #include "cgraph.h"
81 #include "asan.h"
82 #include "rtl-iter.h"
83 #include "tree-chkp.h"
85 #ifdef XCOFF_DEBUGGING_INFO
86 #include "xcoffout.h" /* Needed for external data
87 declarations for e.g. AIX 4.x. */
88 #endif
90 /* The (assembler) name of the first globally-visible object output. */
91 extern GTY(()) const char *first_global_object_name;
92 extern GTY(()) const char *weak_global_object_name;
94 const char *first_global_object_name;
95 const char *weak_global_object_name;
97 struct addr_const;
98 struct constant_descriptor_rtx;
99 struct rtx_constant_pool;
101 #define n_deferred_constants (crtl->varasm.deferred_constants)
103 /* Number for making the label on the next
104 constant that is stored in memory. */
106 static GTY(()) int const_labelno;
108 /* Carry information from ASM_DECLARE_OBJECT_NAME
109 to ASM_FINISH_DECLARE_OBJECT. */
111 int size_directive_output;
113 /* The last decl for which assemble_variable was called,
114 if it did ASM_DECLARE_OBJECT_NAME.
115 If the last call to assemble_variable didn't do that,
116 this holds 0. */
118 tree last_assemble_variable_decl;
120 /* The following global variable indicates if the first basic block
121 in a function belongs to the cold partition or not. */
123 bool first_function_block_is_cold;
125 /* Whether we saw any functions with no_split_stack. */
127 static bool saw_no_split_stack;
129 static const char *strip_reg_name (const char *);
130 static int contains_pointers_p (tree);
131 #ifdef ASM_OUTPUT_EXTERNAL
132 static bool incorporeal_function_p (tree);
133 #endif
134 static void decode_addr_const (tree, struct addr_const *);
135 static hashval_t const_hash_1 (const tree);
136 static int compare_constant (const tree, const tree);
137 static void output_constant_def_contents (rtx);
138 static void output_addressed_constants (tree);
139 static unsigned HOST_WIDE_INT output_constant (tree, unsigned HOST_WIDE_INT,
140 unsigned int, bool);
141 static void globalize_decl (tree);
142 static bool decl_readonly_section_1 (enum section_category);
143 #ifdef BSS_SECTION_ASM_OP
144 #ifdef ASM_OUTPUT_ALIGNED_BSS
145 static void asm_output_aligned_bss (FILE *, tree, const char *,
146 unsigned HOST_WIDE_INT, int)
147 ATTRIBUTE_UNUSED;
148 #endif
149 #endif /* BSS_SECTION_ASM_OP */
150 static void mark_weak (tree);
151 static void output_constant_pool (const char *, tree);
153 /* Well-known sections, each one associated with some sort of *_ASM_OP. */
154 section *text_section;
155 section *data_section;
156 section *readonly_data_section;
157 section *sdata_section;
158 section *ctors_section;
159 section *dtors_section;
160 section *bss_section;
161 section *sbss_section;
163 /* Various forms of common section. All are guaranteed to be nonnull. */
164 section *tls_comm_section;
165 section *comm_section;
166 section *lcomm_section;
168 /* A SECTION_NOSWITCH section used for declaring global BSS variables.
169 May be null. */
170 section *bss_noswitch_section;
172 /* The section that holds the main exception table, when known. The section
173 is set either by the target's init_sections hook or by the first call to
174 switch_to_exception_section. */
175 section *exception_section;
177 /* The section that holds the DWARF2 frame unwind information, when known.
178 The section is set either by the target's init_sections hook or by the
179 first call to switch_to_eh_frame_section. */
180 section *eh_frame_section;
182 /* asm_out_file's current section. This is NULL if no section has yet
183 been selected or if we lose track of what the current section is. */
184 section *in_section;
186 /* True if code for the current function is currently being directed
187 at the cold section. */
188 bool in_cold_section_p;
190 /* The following global holds the "function name" for the code in the
191 cold section of a function, if hot/cold function splitting is enabled
192 and there was actually code that went into the cold section. A
193 pseudo function name is needed for the cold section of code for some
194 debugging tools that perform symbolization. */
195 tree cold_function_name = NULL_TREE;
197 /* A linked list of all the unnamed sections. */
198 static GTY(()) section *unnamed_sections;
200 /* Return a nonzero value if DECL has a section attribute. */
201 #define IN_NAMED_SECTION(DECL) \
202 ((TREE_CODE (DECL) == FUNCTION_DECL || TREE_CODE (DECL) == VAR_DECL) \
203 && DECL_SECTION_NAME (DECL) != NULL)
205 struct section_hasher : ggc_hasher<section *>
207 typedef const char *compare_type;
209 static hashval_t hash (section *);
210 static bool equal (section *, const char *);
213 /* Hash table of named sections. */
214 static GTY(()) hash_table<section_hasher> *section_htab;
216 struct object_block_hasher : ggc_hasher<object_block *>
218 typedef const section *compare_type;
220 static hashval_t hash (object_block *);
221 static bool equal (object_block *, const section *);
224 /* A table of object_blocks, indexed by section. */
225 static GTY(()) hash_table<object_block_hasher> *object_block_htab;
227 /* The next number to use for internal anchor labels. */
228 static GTY(()) int anchor_labelno;
230 /* A pool of constants that can be shared between functions. */
231 static GTY(()) struct rtx_constant_pool *shared_constant_pool;
233 /* Helper routines for maintaining section_htab. */
235 bool
236 section_hasher::equal (section *old, const char *new_name)
238 return strcmp (old->named.name, new_name) == 0;
241 hashval_t
242 section_hasher::hash (section *old)
244 return htab_hash_string (old->named.name);
247 /* Return a hash value for section SECT. */
249 static hashval_t
250 hash_section (section *sect)
252 if (sect->common.flags & SECTION_NAMED)
253 return htab_hash_string (sect->named.name);
254 return sect->common.flags;
257 /* Helper routines for maintaining object_block_htab. */
259 inline bool
260 object_block_hasher::equal (object_block *old, const section *new_section)
262 return old->sect == new_section;
265 hashval_t
266 object_block_hasher::hash (object_block *old)
268 return hash_section (old->sect);
271 /* Return a new unnamed section with the given fields. */
273 section *
274 get_unnamed_section (unsigned int flags, void (*callback) (const void *),
275 const void *data)
277 section *sect;
279 sect = ggc_alloc<section> ();
280 sect->unnamed.common.flags = flags | SECTION_UNNAMED;
281 sect->unnamed.callback = callback;
282 sect->unnamed.data = data;
283 sect->unnamed.next = unnamed_sections;
285 unnamed_sections = sect;
286 return sect;
289 /* Return a SECTION_NOSWITCH section with the given fields. */
291 static section *
292 get_noswitch_section (unsigned int flags, noswitch_section_callback callback)
294 section *sect;
296 sect = ggc_alloc<section> ();
297 sect->noswitch.common.flags = flags | SECTION_NOSWITCH;
298 sect->noswitch.callback = callback;
300 return sect;
303 /* Return the named section structure associated with NAME. Create
304 a new section with the given fields if no such structure exists. */
306 section *
307 get_section (const char *name, unsigned int flags, tree decl)
309 section *sect, **slot;
311 slot = section_htab->find_slot_with_hash (name, htab_hash_string (name),
312 INSERT);
313 flags |= SECTION_NAMED;
314 if (*slot == NULL)
316 sect = ggc_alloc<section> ();
317 sect->named.common.flags = flags;
318 sect->named.name = ggc_strdup (name);
319 sect->named.decl = decl;
320 *slot = sect;
322 else
324 sect = *slot;
325 if ((sect->common.flags & ~SECTION_DECLARED) != flags
326 && ((sect->common.flags | flags) & SECTION_OVERRIDE) == 0)
328 /* It is fine if one of the section flags is
329 SECTION_WRITE | SECTION_RELRO and the other has none of these
330 flags (i.e. read-only) in named sections and either the
331 section hasn't been declared yet or has been declared as writable.
332 In that case just make sure the resulting flags are
333 SECTION_WRITE | SECTION_RELRO, ie. writable only because of
334 relocations. */
335 if (((sect->common.flags ^ flags) & (SECTION_WRITE | SECTION_RELRO))
336 == (SECTION_WRITE | SECTION_RELRO)
337 && (sect->common.flags
338 & ~(SECTION_DECLARED | SECTION_WRITE | SECTION_RELRO))
339 == (flags & ~(SECTION_WRITE | SECTION_RELRO))
340 && ((sect->common.flags & SECTION_DECLARED) == 0
341 || (sect->common.flags & SECTION_WRITE)))
343 sect->common.flags |= (SECTION_WRITE | SECTION_RELRO);
344 return sect;
346 /* Sanity check user variables for flag changes. */
347 if (sect->named.decl != NULL
348 && DECL_P (sect->named.decl)
349 && decl != sect->named.decl)
351 if (decl != NULL && DECL_P (decl))
352 error ("%+D causes a section type conflict with %D",
353 decl, sect->named.decl);
354 else
355 error ("section type conflict with %D", sect->named.decl);
356 inform (DECL_SOURCE_LOCATION (sect->named.decl),
357 "%qD was declared here", sect->named.decl);
359 else if (decl != NULL && DECL_P (decl))
360 error ("%+D causes a section type conflict", decl);
361 else
362 error ("section type conflict");
363 /* Make sure we don't error about one section multiple times. */
364 sect->common.flags |= SECTION_OVERRIDE;
367 return sect;
370 /* Return true if the current compilation mode benefits from having
371 objects grouped into blocks. */
373 static bool
374 use_object_blocks_p (void)
376 return flag_section_anchors;
379 /* Return the object_block structure for section SECT. Create a new
380 structure if we haven't created one already. Return null if SECT
381 itself is null. */
383 static struct object_block *
384 get_block_for_section (section *sect)
386 struct object_block *block;
388 if (sect == NULL)
389 return NULL;
391 object_block **slot
392 = object_block_htab->find_slot_with_hash (sect, hash_section (sect),
393 INSERT);
394 block = *slot;
395 if (block == NULL)
397 block = ggc_cleared_alloc<object_block> ();
398 block->sect = sect;
399 *slot = block;
401 return block;
404 /* Create a symbol with label LABEL and place it at byte offset
405 OFFSET in BLOCK. OFFSET can be negative if the symbol's offset
406 is not yet known. LABEL must be a garbage-collected string. */
408 static rtx
409 create_block_symbol (const char *label, struct object_block *block,
410 HOST_WIDE_INT offset)
412 rtx symbol;
413 unsigned int size;
415 /* Create the extended SYMBOL_REF. */
416 size = RTX_HDR_SIZE + sizeof (struct block_symbol);
417 symbol = (rtx) ggc_internal_alloc (size);
419 /* Initialize the normal SYMBOL_REF fields. */
420 memset (symbol, 0, size);
421 PUT_CODE (symbol, SYMBOL_REF);
422 PUT_MODE (symbol, Pmode);
423 XSTR (symbol, 0) = label;
424 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_HAS_BLOCK_INFO;
426 /* Initialize the block_symbol stuff. */
427 SYMBOL_REF_BLOCK (symbol) = block;
428 SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
430 return symbol;
433 /* Return a section with a particular name and with whatever SECTION_*
434 flags section_type_flags deems appropriate. The name of the section
435 is taken from NAME if nonnull, otherwise it is taken from DECL's
436 DECL_SECTION_NAME. DECL is the decl associated with the section
437 (see the section comment for details) and RELOC is as for
438 section_type_flags. */
440 section *
441 get_named_section (tree decl, const char *name, int reloc)
443 unsigned int flags;
445 if (name == NULL)
447 gcc_assert (decl && DECL_P (decl) && DECL_SECTION_NAME (decl));
448 name = DECL_SECTION_NAME (decl);
451 flags = targetm.section_type_flags (decl, name, reloc);
452 return get_section (name, flags, decl);
455 /* Worker for resolve_unique_section. */
457 static bool
458 set_implicit_section (struct symtab_node *n, void *data ATTRIBUTE_UNUSED)
460 n->implicit_section = true;
461 return false;
464 /* If required, set DECL_SECTION_NAME to a unique name. */
466 void
467 resolve_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED,
468 int flag_function_or_data_sections)
470 if (DECL_SECTION_NAME (decl) == NULL
471 && targetm_common.have_named_sections
472 && (flag_function_or_data_sections
473 || DECL_COMDAT_GROUP (decl)))
475 targetm.asm_out.unique_section (decl, reloc);
476 if (DECL_SECTION_NAME (decl))
477 symtab_node::get (decl)->call_for_symbol_and_aliases
478 (set_implicit_section, NULL, true);
482 #ifdef BSS_SECTION_ASM_OP
484 #ifdef ASM_OUTPUT_ALIGNED_BSS
486 /* Utility function for targets to use in implementing
487 ASM_OUTPUT_ALIGNED_BSS.
488 ??? It is believed that this function will work in most cases so such
489 support is localized here. */
491 static void
492 asm_output_aligned_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
493 const char *name, unsigned HOST_WIDE_INT size,
494 int align)
496 switch_to_section (bss_section);
497 ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
498 #ifdef ASM_DECLARE_OBJECT_NAME
499 last_assemble_variable_decl = decl;
500 ASM_DECLARE_OBJECT_NAME (file, name, decl);
501 #else
502 /* Standard thing is just output label for the object. */
503 ASM_OUTPUT_LABEL (file, name);
504 #endif /* ASM_DECLARE_OBJECT_NAME */
505 ASM_OUTPUT_SKIP (file, size ? size : 1);
508 #endif
510 #endif /* BSS_SECTION_ASM_OP */
512 #ifndef USE_SELECT_SECTION_FOR_FUNCTIONS
513 /* Return the hot section for function DECL. Return text_section for
514 null DECLs. */
516 static section *
517 hot_function_section (tree decl)
519 if (decl != NULL_TREE
520 && DECL_SECTION_NAME (decl) != NULL
521 && targetm_common.have_named_sections)
522 return get_named_section (decl, NULL, 0);
523 else
524 return text_section;
526 #endif
528 /* Return section for TEXT_SECTION_NAME if DECL or DECL_SECTION_NAME (DECL)
529 is NULL.
531 When DECL_SECTION_NAME is non-NULL and it is implicit section and
532 NAMED_SECTION_SUFFIX is non-NULL, then produce section called
533 concatenate the name with NAMED_SECTION_SUFFIX.
534 Otherwise produce "TEXT_SECTION_NAME.IMPLICIT_NAME". */
536 section *
537 get_named_text_section (tree decl,
538 const char *text_section_name,
539 const char *named_section_suffix)
541 if (decl && DECL_SECTION_NAME (decl))
543 if (named_section_suffix)
545 const char *dsn = DECL_SECTION_NAME (decl);
546 const char *stripped_name;
547 char *name, *buffer;
549 name = (char *) alloca (strlen (dsn) + 1);
550 memcpy (name, dsn,
551 strlen (dsn) + 1);
553 stripped_name = targetm.strip_name_encoding (name);
555 buffer = ACONCAT ((stripped_name, named_section_suffix, NULL));
556 return get_named_section (decl, buffer, 0);
558 else if (symtab_node::get (decl)->implicit_section)
560 const char *name;
562 /* Do not try to split gnu_linkonce functions. This gets somewhat
563 slipperly. */
564 if (DECL_COMDAT_GROUP (decl) && !HAVE_COMDAT_GROUP)
565 return NULL;
566 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
567 name = targetm.strip_name_encoding (name);
568 return get_named_section (decl, ACONCAT ((text_section_name, ".",
569 name, NULL)), 0);
571 else
572 return NULL;
574 return get_named_section (decl, text_section_name, 0);
577 /* Choose named function section based on its frequency. */
579 section *
580 default_function_section (tree decl, enum node_frequency freq,
581 bool startup, bool exit)
583 #if defined HAVE_LD_EH_GC_SECTIONS && defined HAVE_LD_EH_GC_SECTIONS_BUG
584 /* Old GNU linkers have buggy --gc-section support, which sometimes
585 results in .gcc_except_table* sections being garbage collected. */
586 if (decl
587 && symtab_node::get (decl)->implicit_section)
588 return NULL;
589 #endif
591 if (!flag_reorder_functions
592 || !targetm_common.have_named_sections)
593 return NULL;
594 /* Startup code should go to startup subsection unless it is
595 unlikely executed (this happens especially with function splitting
596 where we can split away unnecessary parts of static constructors. */
597 if (startup && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
599 /* If we do have a profile or(and) LTO phase is executed, we do not need
600 these ELF section. */
601 if (!in_lto_p || !flag_profile_values)
602 return get_named_text_section (decl, ".text.startup", NULL);
603 else
604 return NULL;
607 /* Similarly for exit. */
608 if (exit && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
609 return get_named_text_section (decl, ".text.exit", NULL);
611 /* Group cold functions together, similarly for hot code. */
612 switch (freq)
614 case NODE_FREQUENCY_UNLIKELY_EXECUTED:
615 return get_named_text_section (decl, ".text.unlikely", NULL);
616 case NODE_FREQUENCY_HOT:
617 /* If we do have a profile or(and) LTO phase is executed, we do not need
618 these ELF section. */
619 if (!in_lto_p || !flag_profile_values)
620 return get_named_text_section (decl, ".text.hot", NULL);
621 default:
622 return NULL;
626 /* Return the section for function DECL.
628 If DECL is NULL_TREE, return the text section. We can be passed
629 NULL_TREE under some circumstances by dbxout.c at least.
631 If FORCE_COLD is true, return cold function section ignoring
632 the frequency info of cgraph_node. */
634 static section *
635 function_section_1 (tree decl, bool force_cold)
637 section *section = NULL;
638 enum node_frequency freq = NODE_FREQUENCY_NORMAL;
639 bool startup = false, exit = false;
641 if (decl)
643 struct cgraph_node *node = cgraph_node::get (decl);
645 if (node)
647 freq = node->frequency;
648 startup = node->only_called_at_startup;
649 exit = node->only_called_at_exit;
652 if (force_cold)
653 freq = NODE_FREQUENCY_UNLIKELY_EXECUTED;
655 #ifdef USE_SELECT_SECTION_FOR_FUNCTIONS
656 if (decl != NULL_TREE
657 && DECL_SECTION_NAME (decl) != NULL)
659 if (targetm.asm_out.function_section)
660 section = targetm.asm_out.function_section (decl, freq,
661 startup, exit);
662 if (section)
663 return section;
664 return get_named_section (decl, NULL, 0);
666 else
667 return targetm.asm_out.select_section
668 (decl, freq == NODE_FREQUENCY_UNLIKELY_EXECUTED,
669 symtab_node::get (decl)->definition_alignment ());
670 #else
671 if (targetm.asm_out.function_section)
672 section = targetm.asm_out.function_section (decl, freq, startup, exit);
673 if (section)
674 return section;
675 return hot_function_section (decl);
676 #endif
679 /* Return the section for function DECL.
681 If DECL is NULL_TREE, return the text section. We can be passed
682 NULL_TREE under some circumstances by dbxout.c at least. */
684 section *
685 function_section (tree decl)
687 /* Handle cases where function splitting code decides
688 to put function entry point into unlikely executed section
689 despite the fact that the function itself is not cold
690 (i.e. it is called rarely but contains a hot loop that is
691 better to live in hot subsection for the code locality). */
692 return function_section_1 (decl,
693 first_function_block_is_cold);
696 /* Return the section for the current function, take IN_COLD_SECTION_P
697 into account. */
699 section *
700 current_function_section (void)
702 return function_section_1 (current_function_decl, in_cold_section_p);
705 /* Tell assembler to switch to unlikely-to-be-executed text section. */
707 section *
708 unlikely_text_section (void)
710 return function_section_1 (current_function_decl, true);
713 /* When called within a function context, return true if the function
714 has been assigned a cold text section and if SECT is that section.
715 When called outside a function context, return true if SECT is the
716 default cold section. */
718 bool
719 unlikely_text_section_p (section *sect)
721 return sect == function_section_1 (current_function_decl, true);
724 /* Return the read-only data section associated with function DECL. */
726 section *
727 default_function_rodata_section (tree decl)
729 if (decl != NULL_TREE && DECL_SECTION_NAME (decl))
731 const char *name = DECL_SECTION_NAME (decl);
733 if (DECL_COMDAT_GROUP (decl) && HAVE_COMDAT_GROUP)
735 const char *dot;
736 size_t len;
737 char* rname;
739 dot = strchr (name + 1, '.');
740 if (!dot)
741 dot = name;
742 len = strlen (dot) + 8;
743 rname = (char *) alloca (len);
745 strcpy (rname, ".rodata");
746 strcat (rname, dot);
747 return get_section (rname, SECTION_LINKONCE, decl);
749 /* For .gnu.linkonce.t.foo we want to use .gnu.linkonce.r.foo. */
750 else if (DECL_COMDAT_GROUP (decl)
751 && strncmp (name, ".gnu.linkonce.t.", 16) == 0)
753 size_t len = strlen (name) + 1;
754 char *rname = (char *) alloca (len);
756 memcpy (rname, name, len);
757 rname[14] = 'r';
758 return get_section (rname, SECTION_LINKONCE, decl);
760 /* For .text.foo we want to use .rodata.foo. */
761 else if (flag_function_sections && flag_data_sections
762 && strncmp (name, ".text.", 6) == 0)
764 size_t len = strlen (name) + 1;
765 char *rname = (char *) alloca (len + 2);
767 memcpy (rname, ".rodata", 7);
768 memcpy (rname + 7, name + 5, len - 5);
769 return get_section (rname, 0, decl);
773 return readonly_data_section;
776 /* Return the read-only data section associated with function DECL
777 for targets where that section should be always the single
778 readonly data section. */
780 section *
781 default_no_function_rodata_section (tree decl ATTRIBUTE_UNUSED)
783 return readonly_data_section;
786 /* Return the section to use for string merging. */
788 static section *
789 mergeable_string_section (tree decl ATTRIBUTE_UNUSED,
790 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
791 unsigned int flags ATTRIBUTE_UNUSED)
793 HOST_WIDE_INT len;
795 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
796 && TREE_CODE (decl) == STRING_CST
797 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
798 && align <= 256
799 && (len = int_size_in_bytes (TREE_TYPE (decl))) > 0
800 && TREE_STRING_LENGTH (decl) >= len)
802 machine_mode mode;
803 unsigned int modesize;
804 const char *str;
805 HOST_WIDE_INT i;
806 int j, unit;
807 const char *prefix = targetm.asm_out.mergeable_rodata_prefix;
808 char *name = (char *) alloca (strlen (prefix) + 30);
810 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (decl)));
811 modesize = GET_MODE_BITSIZE (mode);
812 if (modesize >= 8 && modesize <= 256
813 && (modesize & (modesize - 1)) == 0)
815 if (align < modesize)
816 align = modesize;
818 str = TREE_STRING_POINTER (decl);
819 unit = GET_MODE_SIZE (mode);
821 /* Check for embedded NUL characters. */
822 for (i = 0; i < len; i += unit)
824 for (j = 0; j < unit; j++)
825 if (str[i + j] != '\0')
826 break;
827 if (j == unit)
828 break;
830 if (i == len - unit)
832 sprintf (name, "%s.str%d.%d", prefix,
833 modesize / 8, (int) (align / 8));
834 flags |= (modesize / 8) | SECTION_MERGE | SECTION_STRINGS;
835 return get_section (name, flags, NULL);
840 return readonly_data_section;
843 /* Return the section to use for constant merging. */
845 section *
846 mergeable_constant_section (machine_mode mode ATTRIBUTE_UNUSED,
847 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
848 unsigned int flags ATTRIBUTE_UNUSED)
850 unsigned int modesize = GET_MODE_BITSIZE (mode);
852 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
853 && mode != VOIDmode
854 && mode != BLKmode
855 && modesize <= align
856 && align >= 8
857 && align <= 256
858 && (align & (align - 1)) == 0)
860 const char *prefix = targetm.asm_out.mergeable_rodata_prefix;
861 char *name = (char *) alloca (strlen (prefix) + 30);
863 sprintf (name, "%s.cst%d", prefix, (int) (align / 8));
864 flags |= (align / 8) | SECTION_MERGE;
865 return get_section (name, flags, NULL);
867 return readonly_data_section;
870 /* Given NAME, a putative register name, discard any customary prefixes. */
872 static const char *
873 strip_reg_name (const char *name)
875 #ifdef REGISTER_PREFIX
876 if (!strncmp (name, REGISTER_PREFIX, strlen (REGISTER_PREFIX)))
877 name += strlen (REGISTER_PREFIX);
878 #endif
879 if (name[0] == '%' || name[0] == '#')
880 name++;
881 return name;
884 /* The user has asked for a DECL to have a particular name. Set (or
885 change) it in such a way that we don't prefix an underscore to
886 it. */
887 void
888 set_user_assembler_name (tree decl, const char *name)
890 char *starred = (char *) alloca (strlen (name) + 2);
891 starred[0] = '*';
892 strcpy (starred + 1, name);
893 symtab->change_decl_assembler_name (decl, get_identifier (starred));
894 SET_DECL_RTL (decl, NULL_RTX);
897 /* Decode an `asm' spec for a declaration as a register name.
898 Return the register number, or -1 if nothing specified,
899 or -2 if the ASMSPEC is not `cc' or `memory' and is not recognized,
900 or -3 if ASMSPEC is `cc' and is not recognized,
901 or -4 if ASMSPEC is `memory' and is not recognized.
902 Accept an exact spelling or a decimal number.
903 Prefixes such as % are optional. */
906 decode_reg_name_and_count (const char *asmspec, int *pnregs)
908 /* Presume just one register is clobbered. */
909 *pnregs = 1;
911 if (asmspec != 0)
913 int i;
915 /* Get rid of confusing prefixes. */
916 asmspec = strip_reg_name (asmspec);
918 /* Allow a decimal number as a "register name". */
919 for (i = strlen (asmspec) - 1; i >= 0; i--)
920 if (! ISDIGIT (asmspec[i]))
921 break;
922 if (asmspec[0] != 0 && i < 0)
924 i = atoi (asmspec);
925 if (i < FIRST_PSEUDO_REGISTER && i >= 0 && reg_names[i][0])
926 return i;
927 else
928 return -2;
931 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
932 if (reg_names[i][0]
933 && ! strcmp (asmspec, strip_reg_name (reg_names[i])))
934 return i;
936 #ifdef OVERLAPPING_REGISTER_NAMES
938 static const struct
940 const char *const name;
941 const int number;
942 const int nregs;
943 } table[] = OVERLAPPING_REGISTER_NAMES;
945 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
946 if (table[i].name[0]
947 && ! strcmp (asmspec, table[i].name))
949 *pnregs = table[i].nregs;
950 return table[i].number;
953 #endif /* OVERLAPPING_REGISTER_NAMES */
955 #ifdef ADDITIONAL_REGISTER_NAMES
957 static const struct { const char *const name; const int number; } table[]
958 = ADDITIONAL_REGISTER_NAMES;
960 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
961 if (table[i].name[0]
962 && ! strcmp (asmspec, table[i].name)
963 && reg_names[table[i].number][0])
964 return table[i].number;
966 #endif /* ADDITIONAL_REGISTER_NAMES */
968 if (!strcmp (asmspec, "memory"))
969 return -4;
971 if (!strcmp (asmspec, "cc"))
972 return -3;
974 return -2;
977 return -1;
981 decode_reg_name (const char *name)
983 int count;
984 return decode_reg_name_and_count (name, &count);
988 /* Return true if DECL's initializer is suitable for a BSS section. */
990 bool
991 bss_initializer_p (const_tree decl)
993 return (DECL_INITIAL (decl) == NULL
994 /* In LTO we have no errors in program; error_mark_node is used
995 to mark offlined constructors. */
996 || (DECL_INITIAL (decl) == error_mark_node
997 && !in_lto_p)
998 || (flag_zero_initialized_in_bss
999 /* Leave constant zeroes in .rodata so they
1000 can be shared. */
1001 && !TREE_READONLY (decl)
1002 && initializer_zerop (DECL_INITIAL (decl))));
1005 /* Compute the alignment of variable specified by DECL.
1006 DONT_OUTPUT_DATA is from assemble_variable. */
1008 void
1009 align_variable (tree decl, bool dont_output_data)
1011 unsigned int align = DECL_ALIGN (decl);
1013 /* In the case for initialing an array whose length isn't specified,
1014 where we have not yet been able to do the layout,
1015 figure out the proper alignment now. */
1016 if (dont_output_data && DECL_SIZE (decl) == 0
1017 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
1018 align = MAX (align, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (decl))));
1020 /* Some object file formats have a maximum alignment which they support.
1021 In particular, a.out format supports a maximum alignment of 4. */
1022 if (align > MAX_OFILE_ALIGNMENT)
1024 error ("alignment of %q+D is greater than maximum object "
1025 "file alignment %d", decl,
1026 MAX_OFILE_ALIGNMENT/BITS_PER_UNIT);
1027 align = MAX_OFILE_ALIGNMENT;
1030 if (! DECL_USER_ALIGN (decl))
1032 #ifdef DATA_ABI_ALIGNMENT
1033 unsigned int data_abi_align
1034 = DATA_ABI_ALIGNMENT (TREE_TYPE (decl), align);
1035 /* For backwards compatibility, don't assume the ABI alignment for
1036 TLS variables. */
1037 if (! DECL_THREAD_LOCAL_P (decl) || data_abi_align <= BITS_PER_WORD)
1038 align = data_abi_align;
1039 #endif
1041 /* On some machines, it is good to increase alignment sometimes.
1042 But as DECL_ALIGN is used both for actually emitting the variable
1043 and for code accessing the variable as guaranteed alignment, we
1044 can only increase the alignment if it is a performance optimization
1045 if the references to it must bind to the current definition. */
1046 if (decl_binds_to_current_def_p (decl)
1047 && !DECL_VIRTUAL_P (decl))
1049 #ifdef DATA_ALIGNMENT
1050 unsigned int data_align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
1051 /* Don't increase alignment too much for TLS variables - TLS space
1052 is too precious. */
1053 if (! DECL_THREAD_LOCAL_P (decl) || data_align <= BITS_PER_WORD)
1054 align = data_align;
1055 #endif
1056 #ifdef CONSTANT_ALIGNMENT
1057 if (DECL_INITIAL (decl) != 0
1058 /* In LTO we have no errors in program; error_mark_node is used
1059 to mark offlined constructors. */
1060 && (in_lto_p || DECL_INITIAL (decl) != error_mark_node))
1062 unsigned int const_align
1063 = CONSTANT_ALIGNMENT (DECL_INITIAL (decl), align);
1064 /* Don't increase alignment too much for TLS variables - TLS
1065 space is too precious. */
1066 if (! DECL_THREAD_LOCAL_P (decl) || const_align <= BITS_PER_WORD)
1067 align = const_align;
1069 #endif
1073 /* Reset the alignment in case we have made it tighter, so we can benefit
1074 from it in get_pointer_alignment. */
1075 DECL_ALIGN (decl) = align;
1078 /* Return DECL_ALIGN (decl), possibly increased for optimization purposes
1079 beyond what align_variable returned. */
1081 static unsigned int
1082 get_variable_align (tree decl)
1084 unsigned int align = DECL_ALIGN (decl);
1086 /* For user aligned vars or static vars align_variable already did
1087 everything. */
1088 if (DECL_USER_ALIGN (decl) || !TREE_PUBLIC (decl))
1089 return align;
1091 #ifdef DATA_ABI_ALIGNMENT
1092 if (DECL_THREAD_LOCAL_P (decl))
1093 align = DATA_ABI_ALIGNMENT (TREE_TYPE (decl), align);
1094 #endif
1096 /* For decls that bind to the current definition, align_variable
1097 did also everything, except for not assuming ABI required alignment
1098 of TLS variables. For other vars, increase the alignment here
1099 as an optimization. */
1100 if (!decl_binds_to_current_def_p (decl))
1102 /* On some machines, it is good to increase alignment sometimes. */
1103 #ifdef DATA_ALIGNMENT
1104 unsigned int data_align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
1105 /* Don't increase alignment too much for TLS variables - TLS space
1106 is too precious. */
1107 if (! DECL_THREAD_LOCAL_P (decl) || data_align <= BITS_PER_WORD)
1108 align = data_align;
1109 #endif
1110 #ifdef CONSTANT_ALIGNMENT
1111 if (DECL_INITIAL (decl) != 0
1112 /* In LTO we have no errors in program; error_mark_node is used
1113 to mark offlined constructors. */
1114 && (in_lto_p || DECL_INITIAL (decl) != error_mark_node))
1116 unsigned int const_align = CONSTANT_ALIGNMENT (DECL_INITIAL (decl),
1117 align);
1118 /* Don't increase alignment too much for TLS variables - TLS space
1119 is too precious. */
1120 if (! DECL_THREAD_LOCAL_P (decl) || const_align <= BITS_PER_WORD)
1121 align = const_align;
1123 #endif
1126 return align;
1129 /* Return the section into which the given VAR_DECL or CONST_DECL
1130 should be placed. PREFER_NOSWITCH_P is true if a noswitch
1131 section should be used wherever possible. */
1133 section *
1134 get_variable_section (tree decl, bool prefer_noswitch_p)
1136 addr_space_t as = ADDR_SPACE_GENERIC;
1137 int reloc;
1138 varpool_node *vnode = varpool_node::get (decl);
1139 if (vnode)
1141 vnode = vnode->ultimate_alias_target ();
1142 decl = vnode->decl;
1145 if (TREE_TYPE (decl) != error_mark_node)
1146 as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
1148 /* We need the constructor to figure out reloc flag. */
1149 if (vnode)
1150 vnode->get_constructor ();
1152 if (DECL_COMMON (decl))
1154 /* If the decl has been given an explicit section name, or it resides
1155 in a non-generic address space, then it isn't common, and shouldn't
1156 be handled as such. */
1157 gcc_assert (DECL_SECTION_NAME (decl) == NULL
1158 && ADDR_SPACE_GENERIC_P (as));
1159 if (DECL_THREAD_LOCAL_P (decl))
1160 return tls_comm_section;
1161 else if (TREE_PUBLIC (decl) && bss_initializer_p (decl))
1162 return comm_section;
1165 if (DECL_INITIAL (decl) == error_mark_node)
1166 reloc = contains_pointers_p (TREE_TYPE (decl)) ? 3 : 0;
1167 else if (DECL_INITIAL (decl))
1168 reloc = compute_reloc_for_constant (DECL_INITIAL (decl));
1169 else
1170 reloc = 0;
1172 resolve_unique_section (decl, reloc, flag_data_sections);
1173 if (IN_NAMED_SECTION (decl))
1174 return get_named_section (decl, NULL, reloc);
1176 if (ADDR_SPACE_GENERIC_P (as)
1177 && !DECL_THREAD_LOCAL_P (decl)
1178 && !(prefer_noswitch_p && targetm.have_switchable_bss_sections)
1179 && bss_initializer_p (decl))
1181 if (!TREE_PUBLIC (decl)
1182 && !((flag_sanitize & SANITIZE_ADDRESS)
1183 && asan_protect_global (decl)))
1184 return lcomm_section;
1185 if (bss_noswitch_section)
1186 return bss_noswitch_section;
1189 return targetm.asm_out.select_section (decl, reloc,
1190 get_variable_align (decl));
1193 /* Return the block into which object_block DECL should be placed. */
1195 static struct object_block *
1196 get_block_for_decl (tree decl)
1198 section *sect;
1200 if (TREE_CODE (decl) == VAR_DECL)
1202 /* The object must be defined in this translation unit. */
1203 if (DECL_EXTERNAL (decl))
1204 return NULL;
1206 /* There's no point using object blocks for something that is
1207 isolated by definition. */
1208 if (DECL_COMDAT_GROUP (decl))
1209 return NULL;
1212 /* We can only calculate block offsets if the decl has a known
1213 constant size. */
1214 if (DECL_SIZE_UNIT (decl) == NULL)
1215 return NULL;
1216 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (decl)))
1217 return NULL;
1219 /* Find out which section should contain DECL. We cannot put it into
1220 an object block if it requires a standalone definition. */
1221 if (TREE_CODE (decl) == VAR_DECL)
1222 align_variable (decl, 0);
1223 sect = get_variable_section (decl, true);
1224 if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
1225 return NULL;
1227 return get_block_for_section (sect);
1230 /* Make sure block symbol SYMBOL is in block BLOCK. */
1232 static void
1233 change_symbol_block (rtx symbol, struct object_block *block)
1235 if (block != SYMBOL_REF_BLOCK (symbol))
1237 gcc_assert (SYMBOL_REF_BLOCK_OFFSET (symbol) < 0);
1238 SYMBOL_REF_BLOCK (symbol) = block;
1242 /* Return true if it is possible to put DECL in an object_block. */
1244 static bool
1245 use_blocks_for_decl_p (tree decl)
1247 struct symtab_node *snode;
1249 /* Only data DECLs can be placed into object blocks. */
1250 if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != CONST_DECL)
1251 return false;
1253 /* Detect decls created by dw2_force_const_mem. Such decls are
1254 special because DECL_INITIAL doesn't specify the decl's true value.
1255 dw2_output_indirect_constants will instead call assemble_variable
1256 with dont_output_data set to 1 and then print the contents itself. */
1257 if (DECL_INITIAL (decl) == decl)
1258 return false;
1260 /* If this decl is an alias, then we don't want to emit a
1261 definition. */
1262 if (TREE_CODE (decl) == VAR_DECL
1263 && (snode = symtab_node::get (decl)) != NULL
1264 && snode->alias)
1265 return false;
1267 return targetm.use_blocks_for_decl_p (decl);
1270 /* Follow the IDENTIFIER_TRANSPARENT_ALIAS chain starting at *ALIAS
1271 until we find an identifier that is not itself a transparent alias.
1272 Modify the alias passed to it by reference (and all aliases on the
1273 way to the ultimate target), such that they do not have to be
1274 followed again, and return the ultimate target of the alias
1275 chain. */
1277 static inline tree
1278 ultimate_transparent_alias_target (tree *alias)
1280 tree target = *alias;
1282 if (IDENTIFIER_TRANSPARENT_ALIAS (target))
1284 gcc_assert (TREE_CHAIN (target));
1285 target = ultimate_transparent_alias_target (&TREE_CHAIN (target));
1286 gcc_assert (! IDENTIFIER_TRANSPARENT_ALIAS (target)
1287 && ! TREE_CHAIN (target));
1288 *alias = target;
1291 return target;
1294 /* Create the DECL_RTL for a VAR_DECL or FUNCTION_DECL. DECL should
1295 have static storage duration. In other words, it should not be an
1296 automatic variable, including PARM_DECLs.
1298 There is, however, one exception: this function handles variables
1299 explicitly placed in a particular register by the user.
1301 This is never called for PARM_DECL nodes. */
1303 void
1304 make_decl_rtl (tree decl)
1306 const char *name = 0;
1307 int reg_number;
1308 tree id;
1309 rtx x;
1311 /* Check that we are not being given an automatic variable. */
1312 gcc_assert (TREE_CODE (decl) != PARM_DECL
1313 && TREE_CODE (decl) != RESULT_DECL);
1315 /* A weak alias has TREE_PUBLIC set but not the other bits. */
1316 gcc_assert (TREE_CODE (decl) != VAR_DECL
1317 || TREE_STATIC (decl)
1318 || TREE_PUBLIC (decl)
1319 || DECL_EXTERNAL (decl)
1320 || DECL_REGISTER (decl));
1322 /* And that we were not given a type or a label. */
1323 gcc_assert (TREE_CODE (decl) != TYPE_DECL
1324 && TREE_CODE (decl) != LABEL_DECL);
1326 /* For a duplicate declaration, we can be called twice on the
1327 same DECL node. Don't discard the RTL already made. */
1328 if (DECL_RTL_SET_P (decl))
1330 /* If the old RTL had the wrong mode, fix the mode. */
1331 x = DECL_RTL (decl);
1332 if (GET_MODE (x) != DECL_MODE (decl))
1333 SET_DECL_RTL (decl, adjust_address_nv (x, DECL_MODE (decl), 0));
1335 if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1336 return;
1338 /* ??? Another way to do this would be to maintain a hashed
1339 table of such critters. Instead of adding stuff to a DECL
1340 to give certain attributes to it, we could use an external
1341 hash map from DECL to set of attributes. */
1343 /* Let the target reassign the RTL if it wants.
1344 This is necessary, for example, when one machine specific
1345 decl attribute overrides another. */
1346 targetm.encode_section_info (decl, DECL_RTL (decl), false);
1348 /* If the symbol has a SYMBOL_REF_BLOCK field, update it based
1349 on the new decl information. */
1350 if (MEM_P (x)
1351 && GET_CODE (XEXP (x, 0)) == SYMBOL_REF
1352 && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (x, 0)))
1353 change_symbol_block (XEXP (x, 0), get_block_for_decl (decl));
1355 return;
1358 /* If this variable belongs to the global constant pool, retrieve the
1359 pre-computed RTL or recompute it in LTO mode. */
1360 if (TREE_CODE (decl) == VAR_DECL && DECL_IN_CONSTANT_POOL (decl))
1362 SET_DECL_RTL (decl, output_constant_def (DECL_INITIAL (decl), 1));
1363 return;
1366 id = DECL_ASSEMBLER_NAME (decl);
1367 if (TREE_CODE (decl) == FUNCTION_DECL
1368 && cgraph_node::get (decl)
1369 && cgraph_node::get (decl)->instrumentation_clone)
1370 ultimate_transparent_alias_target (&id);
1371 name = IDENTIFIER_POINTER (id);
1373 if (name[0] != '*' && TREE_CODE (decl) != FUNCTION_DECL
1374 && DECL_REGISTER (decl))
1376 error ("register name not specified for %q+D", decl);
1378 else if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1380 const char *asmspec = name+1;
1381 machine_mode mode = DECL_MODE (decl);
1382 reg_number = decode_reg_name (asmspec);
1383 /* First detect errors in declaring global registers. */
1384 if (reg_number == -1)
1385 error ("register name not specified for %q+D", decl);
1386 else if (reg_number < 0)
1387 error ("invalid register name for %q+D", decl);
1388 else if (mode == BLKmode)
1389 error ("data type of %q+D isn%'t suitable for a register",
1390 decl);
1391 else if (!in_hard_reg_set_p (accessible_reg_set, mode, reg_number))
1392 error ("the register specified for %q+D cannot be accessed"
1393 " by the current target", decl);
1394 else if (!in_hard_reg_set_p (operand_reg_set, mode, reg_number))
1395 error ("the register specified for %q+D is not general enough"
1396 " to be used as a register variable", decl);
1397 else if (!HARD_REGNO_MODE_OK (reg_number, mode))
1398 error ("register specified for %q+D isn%'t suitable for data type",
1399 decl);
1400 /* Now handle properly declared static register variables. */
1401 else
1403 int nregs;
1405 if (DECL_INITIAL (decl) != 0 && TREE_STATIC (decl))
1407 DECL_INITIAL (decl) = 0;
1408 error ("global register variable has initial value");
1410 if (TREE_THIS_VOLATILE (decl))
1411 warning (OPT_Wvolatile_register_var,
1412 "optimization may eliminate reads and/or "
1413 "writes to register variables");
1415 /* If the user specified one of the eliminables registers here,
1416 e.g., FRAME_POINTER_REGNUM, we don't want to get this variable
1417 confused with that register and be eliminated. This usage is
1418 somewhat suspect... */
1420 SET_DECL_RTL (decl, gen_rtx_raw_REG (mode, reg_number));
1421 ORIGINAL_REGNO (DECL_RTL (decl)) = reg_number;
1422 REG_USERVAR_P (DECL_RTL (decl)) = 1;
1424 if (TREE_STATIC (decl))
1426 /* Make this register global, so not usable for anything
1427 else. */
1428 #ifdef ASM_DECLARE_REGISTER_GLOBAL
1429 name = IDENTIFIER_POINTER (DECL_NAME (decl));
1430 ASM_DECLARE_REGISTER_GLOBAL (asm_out_file, decl, reg_number, name);
1431 #endif
1432 nregs = hard_regno_nregs[reg_number][mode];
1433 while (nregs > 0)
1434 globalize_reg (decl, reg_number + --nregs);
1437 /* As a register variable, it has no section. */
1438 return;
1440 /* Avoid internal errors from invalid register
1441 specifications. */
1442 SET_DECL_ASSEMBLER_NAME (decl, NULL_TREE);
1443 DECL_HARD_REGISTER (decl) = 0;
1444 return;
1446 /* Now handle ordinary static variables and functions (in memory).
1447 Also handle vars declared register invalidly. */
1448 else if (name[0] == '*')
1450 #ifdef REGISTER_PREFIX
1451 if (strlen (REGISTER_PREFIX) != 0)
1453 reg_number = decode_reg_name (name);
1454 if (reg_number >= 0 || reg_number == -3)
1455 error ("register name given for non-register variable %q+D", decl);
1457 #endif
1460 /* Specifying a section attribute on a variable forces it into a
1461 non-.bss section, and thus it cannot be common. */
1462 /* FIXME: In general this code should not be necessary because
1463 visibility pass is doing the same work. But notice_global_symbol
1464 is called early and it needs to make DECL_RTL to get the name.
1465 we take care of recomputing the DECL_RTL after visibility is changed. */
1466 if (TREE_CODE (decl) == VAR_DECL
1467 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
1468 && DECL_SECTION_NAME (decl) != NULL
1469 && DECL_INITIAL (decl) == NULL_TREE
1470 && DECL_COMMON (decl))
1471 DECL_COMMON (decl) = 0;
1473 /* Variables can't be both common and weak. */
1474 if (TREE_CODE (decl) == VAR_DECL && DECL_WEAK (decl))
1475 DECL_COMMON (decl) = 0;
1477 if (use_object_blocks_p () && use_blocks_for_decl_p (decl))
1478 x = create_block_symbol (name, get_block_for_decl (decl), -1);
1479 else
1481 machine_mode address_mode = Pmode;
1482 if (TREE_TYPE (decl) != error_mark_node)
1484 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
1485 address_mode = targetm.addr_space.address_mode (as);
1487 x = gen_rtx_SYMBOL_REF (address_mode, name);
1489 SYMBOL_REF_WEAK (x) = DECL_WEAK (decl);
1490 SET_SYMBOL_REF_DECL (x, decl);
1492 x = gen_rtx_MEM (DECL_MODE (decl), x);
1493 if (TREE_CODE (decl) != FUNCTION_DECL)
1494 set_mem_attributes (x, decl, 1);
1495 SET_DECL_RTL (decl, x);
1497 /* Optionally set flags or add text to the name to record information
1498 such as that it is a function name.
1499 If the name is changed, the macro ASM_OUTPUT_LABELREF
1500 will have to know how to strip this information. */
1501 targetm.encode_section_info (decl, DECL_RTL (decl), true);
1504 /* Like make_decl_rtl, but inhibit creation of new alias sets when
1505 calling make_decl_rtl. Also, reset DECL_RTL before returning the
1506 rtl. */
1509 make_decl_rtl_for_debug (tree decl)
1511 unsigned int save_aliasing_flag;
1512 rtx rtl;
1514 if (DECL_RTL_SET_P (decl))
1515 return DECL_RTL (decl);
1517 /* Kludge alert! Somewhere down the call chain, make_decl_rtl will
1518 call new_alias_set. If running with -fcompare-debug, sometimes
1519 we do not want to create alias sets that will throw the alias
1520 numbers off in the comparison dumps. So... clearing
1521 flag_strict_aliasing will keep new_alias_set() from creating a
1522 new set. */
1523 save_aliasing_flag = flag_strict_aliasing;
1524 flag_strict_aliasing = 0;
1526 rtl = DECL_RTL (decl);
1527 /* Reset DECL_RTL back, as various parts of the compiler expects
1528 DECL_RTL set meaning it is actually going to be output. */
1529 SET_DECL_RTL (decl, NULL);
1531 flag_strict_aliasing = save_aliasing_flag;
1532 return rtl;
1535 /* Output a string of literal assembler code
1536 for an `asm' keyword used between functions. */
1538 void
1539 assemble_asm (tree string)
1541 const char *p;
1542 app_enable ();
1544 if (TREE_CODE (string) == ADDR_EXPR)
1545 string = TREE_OPERAND (string, 0);
1547 p = TREE_STRING_POINTER (string);
1548 fprintf (asm_out_file, "%s%s\n", p[0] == '\t' ? "" : "\t", p);
1551 /* Write the address of the entity given by SYMBOL to SEC. */
1552 void
1553 assemble_addr_to_section (rtx symbol, section *sec)
1555 switch_to_section (sec);
1556 assemble_align (POINTER_SIZE);
1557 assemble_integer (symbol, POINTER_SIZE_UNITS, POINTER_SIZE, 1);
1560 /* Return the numbered .ctors.N (if CONSTRUCTOR_P) or .dtors.N (if
1561 not) section for PRIORITY. */
1562 section *
1563 get_cdtor_priority_section (int priority, bool constructor_p)
1565 char buf[16];
1567 /* ??? This only works reliably with the GNU linker. */
1568 sprintf (buf, "%s.%.5u",
1569 constructor_p ? ".ctors" : ".dtors",
1570 /* Invert the numbering so the linker puts us in the proper
1571 order; constructors are run from right to left, and the
1572 linker sorts in increasing order. */
1573 MAX_INIT_PRIORITY - priority);
1574 return get_section (buf, SECTION_WRITE, NULL);
1577 void
1578 default_named_section_asm_out_destructor (rtx symbol, int priority)
1580 section *sec;
1582 if (priority != DEFAULT_INIT_PRIORITY)
1583 sec = get_cdtor_priority_section (priority,
1584 /*constructor_p=*/false);
1585 else
1586 sec = get_section (".dtors", SECTION_WRITE, NULL);
1588 assemble_addr_to_section (symbol, sec);
1591 #ifdef DTORS_SECTION_ASM_OP
1592 void
1593 default_dtor_section_asm_out_destructor (rtx symbol,
1594 int priority ATTRIBUTE_UNUSED)
1596 assemble_addr_to_section (symbol, dtors_section);
1598 #endif
1600 void
1601 default_named_section_asm_out_constructor (rtx symbol, int priority)
1603 section *sec;
1605 if (priority != DEFAULT_INIT_PRIORITY)
1606 sec = get_cdtor_priority_section (priority,
1607 /*constructor_p=*/true);
1608 else
1609 sec = get_section (".ctors", SECTION_WRITE, NULL);
1611 assemble_addr_to_section (symbol, sec);
1614 #ifdef CTORS_SECTION_ASM_OP
1615 void
1616 default_ctor_section_asm_out_constructor (rtx symbol,
1617 int priority ATTRIBUTE_UNUSED)
1619 assemble_addr_to_section (symbol, ctors_section);
1621 #endif
1623 /* CONSTANT_POOL_BEFORE_FUNCTION may be defined as an expression with
1624 a nonzero value if the constant pool should be output before the
1625 start of the function, or a zero value if the pool should output
1626 after the end of the function. The default is to put it before the
1627 start. */
1629 #ifndef CONSTANT_POOL_BEFORE_FUNCTION
1630 #define CONSTANT_POOL_BEFORE_FUNCTION 1
1631 #endif
1633 /* DECL is an object (either VAR_DECL or FUNCTION_DECL) which is going
1634 to be output to assembler.
1635 Set first_global_object_name and weak_global_object_name as appropriate. */
1637 void
1638 notice_global_symbol (tree decl)
1640 const char **t = &first_global_object_name;
1642 if (first_global_object_name
1643 || !TREE_PUBLIC (decl)
1644 || DECL_EXTERNAL (decl)
1645 || !DECL_NAME (decl)
1646 || (TREE_CODE (decl) == VAR_DECL && DECL_HARD_REGISTER (decl))
1647 || (TREE_CODE (decl) != FUNCTION_DECL
1648 && (TREE_CODE (decl) != VAR_DECL
1649 || (DECL_COMMON (decl)
1650 && (DECL_INITIAL (decl) == 0
1651 || DECL_INITIAL (decl) == error_mark_node)))))
1652 return;
1654 /* We win when global object is found, but it is useful to know about weak
1655 symbol as well so we can produce nicer unique names. */
1656 if (DECL_WEAK (decl) || DECL_ONE_ONLY (decl) || flag_shlib)
1657 t = &weak_global_object_name;
1659 if (!*t)
1661 tree id = DECL_ASSEMBLER_NAME (decl);
1662 ultimate_transparent_alias_target (&id);
1663 *t = ggc_strdup (targetm.strip_name_encoding (IDENTIFIER_POINTER (id)));
1667 /* If not using flag_reorder_blocks_and_partition, decide early whether the
1668 current function goes into the cold section, so that targets can use
1669 current_function_section during RTL expansion. DECL describes the
1670 function. */
1672 void
1673 decide_function_section (tree decl)
1675 first_function_block_is_cold = false;
1677 if (flag_reorder_blocks_and_partition)
1678 /* We will decide in assemble_start_function. */
1679 return;
1681 if (DECL_SECTION_NAME (decl))
1683 struct cgraph_node *node = cgraph_node::get (current_function_decl);
1684 /* Calls to function_section rely on first_function_block_is_cold
1685 being accurate. */
1686 first_function_block_is_cold = (node
1687 && node->frequency
1688 == NODE_FREQUENCY_UNLIKELY_EXECUTED);
1691 in_cold_section_p = first_function_block_is_cold;
1694 /* Get the function's name, as described by its RTL. This may be
1695 different from the DECL_NAME name used in the source file. */
1696 const char *
1697 get_fnname_from_decl (tree decl)
1699 rtx x = DECL_RTL (decl);
1700 gcc_assert (MEM_P (x));
1701 x = XEXP (x, 0);
1702 gcc_assert (GET_CODE (x) == SYMBOL_REF);
1703 return XSTR (x, 0);
1706 /* Output assembler code for the constant pool of a function and associated
1707 with defining the name of the function. DECL describes the function.
1708 NAME is the function's name. For the constant pool, we use the current
1709 constant pool data. */
1711 void
1712 assemble_start_function (tree decl, const char *fnname)
1714 int align;
1715 char tmp_label[100];
1716 bool hot_label_written = false;
1718 if (flag_reorder_blocks_and_partition)
1720 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTB", const_labelno);
1721 crtl->subsections.hot_section_label = ggc_strdup (tmp_label);
1722 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDB", const_labelno);
1723 crtl->subsections.cold_section_label = ggc_strdup (tmp_label);
1724 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTE", const_labelno);
1725 crtl->subsections.hot_section_end_label = ggc_strdup (tmp_label);
1726 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDE", const_labelno);
1727 crtl->subsections.cold_section_end_label = ggc_strdup (tmp_label);
1728 const_labelno++;
1729 cold_function_name = NULL_TREE;
1731 else
1733 crtl->subsections.hot_section_label = NULL;
1734 crtl->subsections.cold_section_label = NULL;
1735 crtl->subsections.hot_section_end_label = NULL;
1736 crtl->subsections.cold_section_end_label = NULL;
1739 /* The following code does not need preprocessing in the assembler. */
1741 app_disable ();
1743 if (CONSTANT_POOL_BEFORE_FUNCTION)
1744 output_constant_pool (fnname, decl);
1746 align = symtab_node::get (decl)->definition_alignment ();
1748 /* Make sure the not and cold text (code) sections are properly
1749 aligned. This is necessary here in the case where the function
1750 has both hot and cold sections, because we don't want to re-set
1751 the alignment when the section switch happens mid-function. */
1753 if (flag_reorder_blocks_and_partition)
1755 first_function_block_is_cold = false;
1757 switch_to_section (unlikely_text_section ());
1758 assemble_align (align);
1759 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_label);
1761 /* When the function starts with a cold section, we need to explicitly
1762 align the hot section and write out the hot section label.
1763 But if the current function is a thunk, we do not have a CFG. */
1764 if (!cfun->is_thunk
1765 && BB_PARTITION (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb) == BB_COLD_PARTITION)
1767 switch_to_section (text_section);
1768 assemble_align (align);
1769 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
1770 hot_label_written = true;
1771 first_function_block_is_cold = true;
1773 in_cold_section_p = first_function_block_is_cold;
1777 /* Switch to the correct text section for the start of the function. */
1779 switch_to_section (function_section (decl));
1780 if (flag_reorder_blocks_and_partition
1781 && !hot_label_written)
1782 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
1784 /* Tell assembler to move to target machine's alignment for functions. */
1785 align = floor_log2 (align / BITS_PER_UNIT);
1786 if (align > 0)
1788 ASM_OUTPUT_ALIGN (asm_out_file, align);
1791 /* Handle a user-specified function alignment.
1792 Note that we still need to align to DECL_ALIGN, as above,
1793 because ASM_OUTPUT_MAX_SKIP_ALIGN might not do any alignment at all. */
1794 if (! DECL_USER_ALIGN (decl)
1795 && align_functions_log > align
1796 && optimize_function_for_speed_p (cfun))
1798 #ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
1799 ASM_OUTPUT_MAX_SKIP_ALIGN (asm_out_file,
1800 align_functions_log, align_functions - 1);
1801 #else
1802 ASM_OUTPUT_ALIGN (asm_out_file, align_functions_log);
1803 #endif
1806 #ifdef ASM_OUTPUT_FUNCTION_PREFIX
1807 ASM_OUTPUT_FUNCTION_PREFIX (asm_out_file, fnname);
1808 #endif
1810 if (!DECL_IGNORED_P (decl))
1811 (*debug_hooks->begin_function) (decl);
1813 /* Make function name accessible from other files, if appropriate. */
1815 if (TREE_PUBLIC (decl)
1816 || (cgraph_node::get (decl)->instrumentation_clone
1817 && cgraph_node::get (decl)->instrumented_version
1818 && TREE_PUBLIC (cgraph_node::get (decl)->instrumented_version->decl)))
1820 notice_global_symbol (decl);
1822 globalize_decl (decl);
1824 maybe_assemble_visibility (decl);
1827 if (DECL_PRESERVE_P (decl))
1828 targetm.asm_out.mark_decl_preserved (fnname);
1830 /* Do any machine/system dependent processing of the function name. */
1831 #ifdef ASM_DECLARE_FUNCTION_NAME
1832 ASM_DECLARE_FUNCTION_NAME (asm_out_file, fnname, current_function_decl);
1833 #else
1834 /* Standard thing is just output label for the function. */
1835 ASM_OUTPUT_FUNCTION_LABEL (asm_out_file, fnname, current_function_decl);
1836 #endif /* ASM_DECLARE_FUNCTION_NAME */
1838 if (lookup_attribute ("no_split_stack", DECL_ATTRIBUTES (decl)))
1839 saw_no_split_stack = true;
1842 /* Output assembler code associated with defining the size of the
1843 function. DECL describes the function. NAME is the function's name. */
1845 void
1846 assemble_end_function (tree decl, const char *fnname ATTRIBUTE_UNUSED)
1848 #ifdef ASM_DECLARE_FUNCTION_SIZE
1849 /* We could have switched section in the middle of the function. */
1850 if (flag_reorder_blocks_and_partition)
1851 switch_to_section (function_section (decl));
1852 ASM_DECLARE_FUNCTION_SIZE (asm_out_file, fnname, decl);
1853 #endif
1854 if (! CONSTANT_POOL_BEFORE_FUNCTION)
1856 output_constant_pool (fnname, decl);
1857 switch_to_section (function_section (decl)); /* need to switch back */
1859 /* Output labels for end of hot/cold text sections (to be used by
1860 debug info.) */
1861 if (flag_reorder_blocks_and_partition)
1863 section *save_text_section;
1865 save_text_section = in_section;
1866 switch_to_section (unlikely_text_section ());
1867 #ifdef ASM_DECLARE_COLD_FUNCTION_SIZE
1868 if (cold_function_name != NULL_TREE)
1869 ASM_DECLARE_COLD_FUNCTION_SIZE (asm_out_file,
1870 IDENTIFIER_POINTER (cold_function_name),
1871 decl);
1872 #endif
1873 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_end_label);
1874 if (first_function_block_is_cold)
1875 switch_to_section (text_section);
1876 else
1877 switch_to_section (function_section (decl));
1878 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_end_label);
1879 switch_to_section (save_text_section);
1883 /* Assemble code to leave SIZE bytes of zeros. */
1885 void
1886 assemble_zeros (unsigned HOST_WIDE_INT size)
1888 /* Do no output if -fsyntax-only. */
1889 if (flag_syntax_only)
1890 return;
1892 #ifdef ASM_NO_SKIP_IN_TEXT
1893 /* The `space' pseudo in the text section outputs nop insns rather than 0s,
1894 so we must output 0s explicitly in the text section. */
1895 if (ASM_NO_SKIP_IN_TEXT && (in_section->common.flags & SECTION_CODE) != 0)
1897 unsigned HOST_WIDE_INT i;
1898 for (i = 0; i < size; i++)
1899 assemble_integer (const0_rtx, 1, BITS_PER_UNIT, 1);
1901 else
1902 #endif
1903 if (size > 0)
1904 ASM_OUTPUT_SKIP (asm_out_file, size);
1907 /* Assemble an alignment pseudo op for an ALIGN-bit boundary. */
1909 void
1910 assemble_align (int align)
1912 if (align > BITS_PER_UNIT)
1914 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
1918 /* Assemble a string constant with the specified C string as contents. */
1920 void
1921 assemble_string (const char *p, int size)
1923 int pos = 0;
1924 int maximum = 2000;
1926 /* If the string is very long, split it up. */
1928 while (pos < size)
1930 int thissize = size - pos;
1931 if (thissize > maximum)
1932 thissize = maximum;
1934 ASM_OUTPUT_ASCII (asm_out_file, p, thissize);
1936 pos += thissize;
1937 p += thissize;
1942 /* A noswitch_section_callback for lcomm_section. */
1944 static bool
1945 emit_local (tree decl ATTRIBUTE_UNUSED,
1946 const char *name ATTRIBUTE_UNUSED,
1947 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1948 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1950 #if defined ASM_OUTPUT_ALIGNED_DECL_LOCAL
1951 int align = symtab_node::get (decl)->definition_alignment ();
1952 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, decl, name,
1953 size, align);
1954 return true;
1955 #elif defined ASM_OUTPUT_ALIGNED_LOCAL
1956 int align = symtab_node::get (decl)->definition_alignment ();
1957 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, align);
1958 return true;
1959 #else
1960 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
1961 return false;
1962 #endif
1965 /* A noswitch_section_callback for bss_noswitch_section. */
1967 #if defined ASM_OUTPUT_ALIGNED_BSS
1968 static bool
1969 emit_bss (tree decl ATTRIBUTE_UNUSED,
1970 const char *name ATTRIBUTE_UNUSED,
1971 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1972 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1974 ASM_OUTPUT_ALIGNED_BSS (asm_out_file, decl, name, size,
1975 get_variable_align (decl));
1976 return true;
1978 #endif
1980 /* A noswitch_section_callback for comm_section. */
1982 static bool
1983 emit_common (tree decl ATTRIBUTE_UNUSED,
1984 const char *name ATTRIBUTE_UNUSED,
1985 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1986 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1988 #if defined ASM_OUTPUT_ALIGNED_DECL_COMMON
1989 ASM_OUTPUT_ALIGNED_DECL_COMMON (asm_out_file, decl, name,
1990 size, get_variable_align (decl));
1991 return true;
1992 #elif defined ASM_OUTPUT_ALIGNED_COMMON
1993 ASM_OUTPUT_ALIGNED_COMMON (asm_out_file, name, size,
1994 get_variable_align (decl));
1995 return true;
1996 #else
1997 ASM_OUTPUT_COMMON (asm_out_file, name, size, rounded);
1998 return false;
1999 #endif
2002 /* A noswitch_section_callback for tls_comm_section. */
2004 static bool
2005 emit_tls_common (tree decl ATTRIBUTE_UNUSED,
2006 const char *name ATTRIBUTE_UNUSED,
2007 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
2008 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
2010 #ifdef ASM_OUTPUT_TLS_COMMON
2011 ASM_OUTPUT_TLS_COMMON (asm_out_file, decl, name, size);
2012 return true;
2013 #else
2014 sorry ("thread-local COMMON data not implemented");
2015 return true;
2016 #endif
2019 /* Assemble DECL given that it belongs in SECTION_NOSWITCH section SECT.
2020 NAME is the name of DECL's SYMBOL_REF. */
2022 static void
2023 assemble_noswitch_variable (tree decl, const char *name, section *sect,
2024 unsigned int align)
2026 unsigned HOST_WIDE_INT size, rounded;
2028 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
2029 rounded = size;
2031 if ((flag_sanitize & SANITIZE_ADDRESS) && asan_protect_global (decl))
2032 size += asan_red_zone_size (size);
2034 /* Don't allocate zero bytes of common,
2035 since that means "undefined external" in the linker. */
2036 if (size == 0)
2037 rounded = 1;
2039 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
2040 so that each uninitialized object starts on such a boundary. */
2041 rounded += (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1;
2042 rounded = (rounded / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
2043 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
2045 if (!sect->noswitch.callback (decl, name, size, rounded)
2046 && (unsigned HOST_WIDE_INT) (align / BITS_PER_UNIT) > rounded)
2047 error ("requested alignment for %q+D is greater than "
2048 "implemented alignment of %wu", decl, rounded);
2051 /* A subroutine of assemble_variable. Output the label and contents of
2052 DECL, whose address is a SYMBOL_REF with name NAME. DONT_OUTPUT_DATA
2053 is as for assemble_variable. */
2055 static void
2056 assemble_variable_contents (tree decl, const char *name,
2057 bool dont_output_data)
2059 /* Do any machine/system dependent processing of the object. */
2060 #ifdef ASM_DECLARE_OBJECT_NAME
2061 last_assemble_variable_decl = decl;
2062 ASM_DECLARE_OBJECT_NAME (asm_out_file, name, decl);
2063 #else
2064 /* Standard thing is just output label for the object. */
2065 ASM_OUTPUT_LABEL (asm_out_file, name);
2066 #endif /* ASM_DECLARE_OBJECT_NAME */
2068 if (!dont_output_data)
2070 /* Caller is supposed to use varpool_get_constructor when it wants
2071 to output the body. */
2072 gcc_assert (!in_lto_p || DECL_INITIAL (decl) != error_mark_node);
2073 if (DECL_INITIAL (decl)
2074 && DECL_INITIAL (decl) != error_mark_node
2075 && !initializer_zerop (DECL_INITIAL (decl)))
2076 /* Output the actual data. */
2077 output_constant (DECL_INITIAL (decl),
2078 tree_to_uhwi (DECL_SIZE_UNIT (decl)),
2079 get_variable_align (decl),
2080 false);
2081 else
2082 /* Leave space for it. */
2083 assemble_zeros (tree_to_uhwi (DECL_SIZE_UNIT (decl)));
2084 targetm.asm_out.decl_end ();
2088 /* Write out assembly for the variable DECL, which is not defined in
2089 the current translation unit. */
2090 void
2091 assemble_undefined_decl (tree decl)
2093 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
2094 targetm.asm_out.assemble_undefined_decl (asm_out_file, name, decl);
2097 /* Assemble everything that is needed for a variable or function declaration.
2098 Not used for automatic variables, and not used for function definitions.
2099 Should not be called for variables of incomplete structure type.
2101 TOP_LEVEL is nonzero if this variable has file scope.
2102 AT_END is nonzero if this is the special handling, at end of compilation,
2103 to define things that have had only tentative definitions.
2104 DONT_OUTPUT_DATA if nonzero means don't actually output the
2105 initial value (that will be done by the caller). */
2107 void
2108 assemble_variable (tree decl, int top_level ATTRIBUTE_UNUSED,
2109 int at_end ATTRIBUTE_UNUSED, int dont_output_data)
2111 const char *name;
2112 rtx decl_rtl, symbol;
2113 section *sect;
2114 unsigned int align;
2115 bool asan_protected = false;
2117 /* This function is supposed to handle VARIABLES. Ensure we have one. */
2118 gcc_assert (TREE_CODE (decl) == VAR_DECL);
2120 /* Emulated TLS had better not get this far. */
2121 gcc_checking_assert (targetm.have_tls || !DECL_THREAD_LOCAL_P (decl));
2123 last_assemble_variable_decl = 0;
2125 /* Normally no need to say anything here for external references,
2126 since assemble_external is called by the language-specific code
2127 when a declaration is first seen. */
2129 if (DECL_EXTERNAL (decl))
2130 return;
2132 /* Do nothing for global register variables. */
2133 if (DECL_RTL_SET_P (decl) && REG_P (DECL_RTL (decl)))
2135 TREE_ASM_WRITTEN (decl) = 1;
2136 return;
2139 /* If type was incomplete when the variable was declared,
2140 see if it is complete now. */
2142 if (DECL_SIZE (decl) == 0)
2143 layout_decl (decl, 0);
2145 /* Still incomplete => don't allocate it; treat the tentative defn
2146 (which is what it must have been) as an `extern' reference. */
2148 if (!dont_output_data && DECL_SIZE (decl) == 0)
2150 error ("storage size of %q+D isn%'t known", decl);
2151 TREE_ASM_WRITTEN (decl) = 1;
2152 return;
2155 /* The first declaration of a variable that comes through this function
2156 decides whether it is global (in C, has external linkage)
2157 or local (in C, has internal linkage). So do nothing more
2158 if this function has already run. */
2160 if (TREE_ASM_WRITTEN (decl))
2161 return;
2163 /* Make sure targetm.encode_section_info is invoked before we set
2164 ASM_WRITTEN. */
2165 decl_rtl = DECL_RTL (decl);
2167 TREE_ASM_WRITTEN (decl) = 1;
2169 /* Do no output if -fsyntax-only. */
2170 if (flag_syntax_only)
2171 return;
2173 if (! dont_output_data
2174 && ! valid_constant_size_p (DECL_SIZE_UNIT (decl)))
2176 error ("size of variable %q+D is too large", decl);
2177 return;
2180 gcc_assert (MEM_P (decl_rtl));
2181 gcc_assert (GET_CODE (XEXP (decl_rtl, 0)) == SYMBOL_REF);
2182 symbol = XEXP (decl_rtl, 0);
2184 /* If this symbol belongs to the tree constant pool, output the constant
2185 if it hasn't already been written. */
2186 if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
2188 tree decl = SYMBOL_REF_DECL (symbol);
2189 if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
2190 output_constant_def_contents (symbol);
2191 return;
2194 app_disable ();
2196 name = XSTR (symbol, 0);
2197 if (TREE_PUBLIC (decl) && DECL_NAME (decl))
2198 notice_global_symbol (decl);
2200 /* Compute the alignment of this data. */
2202 align_variable (decl, dont_output_data);
2204 if ((flag_sanitize & SANITIZE_ADDRESS)
2205 && asan_protect_global (decl))
2207 asan_protected = true;
2208 DECL_ALIGN (decl) = MAX (DECL_ALIGN (decl),
2209 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
2212 set_mem_align (decl_rtl, DECL_ALIGN (decl));
2214 align = get_variable_align (decl);
2216 if (TREE_PUBLIC (decl))
2217 maybe_assemble_visibility (decl);
2219 if (DECL_PRESERVE_P (decl))
2220 targetm.asm_out.mark_decl_preserved (name);
2222 /* First make the assembler name(s) global if appropriate. */
2223 sect = get_variable_section (decl, false);
2224 if (TREE_PUBLIC (decl)
2225 && (sect->common.flags & SECTION_COMMON) == 0)
2226 globalize_decl (decl);
2228 /* Output any data that we will need to use the address of. */
2229 if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node)
2230 output_addressed_constants (DECL_INITIAL (decl));
2232 /* dbxout.c needs to know this. */
2233 if (sect && (sect->common.flags & SECTION_CODE) != 0)
2234 DECL_IN_TEXT_SECTION (decl) = 1;
2236 /* If the decl is part of an object_block, make sure that the decl
2237 has been positioned within its block, but do not write out its
2238 definition yet. output_object_blocks will do that later. */
2239 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
2241 gcc_assert (!dont_output_data);
2242 place_block_symbol (symbol);
2244 else if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
2245 assemble_noswitch_variable (decl, name, sect, align);
2246 else
2248 /* The following bit of code ensures that vtable_map
2249 variables are not only in the comdat section, but that
2250 each variable has its own unique comdat name. If this
2251 code is removed, the variables end up in the same section
2252 with a single comdat name.
2254 FIXME: resolve_unique_section needs to deal better with
2255 decls with both DECL_SECTION_NAME and DECL_ONE_ONLY. Once
2256 that is fixed, this if-else statement can be replaced with
2257 a single call to "switch_to_section (sect)". */
2258 if (sect->named.name
2259 && (strcmp (sect->named.name, ".vtable_map_vars") == 0))
2261 #if defined (OBJECT_FORMAT_ELF)
2262 targetm.asm_out.named_section (sect->named.name,
2263 sect->named.common.flags
2264 | SECTION_LINKONCE,
2265 DECL_NAME (decl));
2266 in_section = sect;
2267 #elif defined (TARGET_PECOFF)
2268 /* Neither OBJECT_FORMAT_PE, nor OBJECT_FORMAT_COFF is set here.
2269 Therefore the following check is used.
2270 In case a the target is PE or COFF a comdat group section
2271 is created, e.g. .vtable_map_vars$foo. The linker places
2272 everything in .vtable_map_vars at the end.
2274 A fix could be made in
2275 gcc/config/i386/winnt.c: i386_pe_unique_section. */
2276 if (TARGET_PECOFF)
2278 char *name;
2280 if (TREE_CODE (DECL_NAME (decl)) == IDENTIFIER_NODE)
2281 name = ACONCAT ((sect->named.name, "$",
2282 IDENTIFIER_POINTER (DECL_NAME (decl)), NULL));
2283 else
2284 name = ACONCAT ((sect->named.name, "$",
2285 IDENTIFIER_POINTER (DECL_COMDAT_GROUP (DECL_NAME (decl))),
2286 NULL));
2288 targetm.asm_out.named_section (name,
2289 sect->named.common.flags
2290 | SECTION_LINKONCE,
2291 DECL_NAME (decl));
2292 in_section = sect;
2294 #else
2295 switch_to_section (sect);
2296 #endif
2298 else
2299 switch_to_section (sect);
2300 if (align > BITS_PER_UNIT)
2301 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
2302 assemble_variable_contents (decl, name, dont_output_data);
2303 if (asan_protected)
2305 unsigned HOST_WIDE_INT int size
2306 = tree_to_uhwi (DECL_SIZE_UNIT (decl));
2307 assemble_zeros (asan_red_zone_size (size));
2313 /* Given a function declaration (FN_DECL), this function assembles the
2314 function into the .preinit_array section. */
2316 void
2317 assemble_vtv_preinit_initializer (tree fn_decl)
2319 section *sect;
2320 unsigned flags = SECTION_WRITE;
2321 rtx symbol = XEXP (DECL_RTL (fn_decl), 0);
2323 flags |= SECTION_NOTYPE;
2324 sect = get_section (".preinit_array", flags, fn_decl);
2325 switch_to_section (sect);
2326 assemble_addr_to_section (symbol, sect);
2329 /* Return 1 if type TYPE contains any pointers. */
2331 static int
2332 contains_pointers_p (tree type)
2334 switch (TREE_CODE (type))
2336 case POINTER_TYPE:
2337 case REFERENCE_TYPE:
2338 /* I'm not sure whether OFFSET_TYPE needs this treatment,
2339 so I'll play safe and return 1. */
2340 case OFFSET_TYPE:
2341 return 1;
2343 case RECORD_TYPE:
2344 case UNION_TYPE:
2345 case QUAL_UNION_TYPE:
2347 tree fields;
2348 /* For a type that has fields, see if the fields have pointers. */
2349 for (fields = TYPE_FIELDS (type); fields; fields = DECL_CHAIN (fields))
2350 if (TREE_CODE (fields) == FIELD_DECL
2351 && contains_pointers_p (TREE_TYPE (fields)))
2352 return 1;
2353 return 0;
2356 case ARRAY_TYPE:
2357 /* An array type contains pointers if its element type does. */
2358 return contains_pointers_p (TREE_TYPE (type));
2360 default:
2361 return 0;
2365 /* We delay assemble_external processing until
2366 the compilation unit is finalized. This is the best we can do for
2367 right now (i.e. stage 3 of GCC 4.0) - the right thing is to delay
2368 it all the way to final. See PR 17982 for further discussion. */
2369 static GTY(()) tree pending_assemble_externals;
2371 #ifdef ASM_OUTPUT_EXTERNAL
2372 /* Some targets delay some output to final using TARGET_ASM_FILE_END.
2373 As a result, assemble_external can be called after the list of externals
2374 is processed and the pointer set destroyed. */
2375 static bool pending_assemble_externals_processed;
2377 /* Avoid O(external_decls**2) lookups in the pending_assemble_externals
2378 TREE_LIST in assemble_external. */
2379 static hash_set<tree> *pending_assemble_externals_set;
2381 /* True if DECL is a function decl for which no out-of-line copy exists.
2382 It is assumed that DECL's assembler name has been set. */
2384 static bool
2385 incorporeal_function_p (tree decl)
2387 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
2389 const char *name;
2391 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
2392 && (DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA
2393 || DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA_WITH_ALIGN))
2394 return true;
2396 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2397 /* Atomic or sync builtins which have survived this far will be
2398 resolved externally and therefore are not incorporeal. */
2399 if (strncmp (name, "__builtin_", 10) == 0)
2400 return true;
2402 return false;
2405 /* Actually do the tests to determine if this is necessary, and invoke
2406 ASM_OUTPUT_EXTERNAL. */
2407 static void
2408 assemble_external_real (tree decl)
2410 rtx rtl = DECL_RTL (decl);
2412 if (MEM_P (rtl) && GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF
2413 && !SYMBOL_REF_USED (XEXP (rtl, 0))
2414 && !incorporeal_function_p (decl))
2416 /* Some systems do require some output. */
2417 SYMBOL_REF_USED (XEXP (rtl, 0)) = 1;
2418 ASM_OUTPUT_EXTERNAL (asm_out_file, decl, XSTR (XEXP (rtl, 0), 0));
2421 #endif
2423 void
2424 process_pending_assemble_externals (void)
2426 #ifdef ASM_OUTPUT_EXTERNAL
2427 tree list;
2428 for (list = pending_assemble_externals; list; list = TREE_CHAIN (list))
2429 assemble_external_real (TREE_VALUE (list));
2431 pending_assemble_externals = 0;
2432 pending_assemble_externals_processed = true;
2433 delete pending_assemble_externals_set;
2434 #endif
2437 /* This TREE_LIST contains any weak symbol declarations waiting
2438 to be emitted. */
2439 static GTY(()) tree weak_decls;
2441 /* Output something to declare an external symbol to the assembler,
2442 and qualifiers such as weakness. (Most assemblers don't need
2443 extern declaration, so we normally output nothing.) Do nothing if
2444 DECL is not external. */
2446 void
2447 assemble_external (tree decl ATTRIBUTE_UNUSED)
2449 /* Make sure that the ASM_OUT_FILE is open.
2450 If it's not, we should not be calling this function. */
2451 gcc_assert (asm_out_file);
2453 /* In a perfect world, the following condition would be true.
2454 Sadly, the Java and Go front ends emit assembly *from the front end*,
2455 bypassing the call graph. See PR52739. Fix before GCC 4.8. */
2456 #if 0
2457 /* This function should only be called if we are expanding, or have
2458 expanded, to RTL.
2459 Ideally, only final.c would be calling this function, but it is
2460 not clear whether that would break things somehow. See PR 17982
2461 for further discussion. */
2462 gcc_assert (state == EXPANSION
2463 || state == FINISHED);
2464 #endif
2466 if (!DECL_P (decl) || !DECL_EXTERNAL (decl) || !TREE_PUBLIC (decl))
2467 return;
2469 /* We want to output annotation for weak and external symbols at
2470 very last to check if they are references or not. */
2472 if (TARGET_SUPPORTS_WEAK
2473 && DECL_WEAK (decl)
2474 /* TREE_STATIC is a weird and abused creature which is not
2475 generally the right test for whether an entity has been
2476 locally emitted, inlined or otherwise not-really-extern, but
2477 for declarations that can be weak, it happens to be
2478 match. */
2479 && !TREE_STATIC (decl)
2480 && lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
2481 && value_member (decl, weak_decls) == NULL_TREE)
2482 weak_decls = tree_cons (NULL, decl, weak_decls);
2484 #ifdef ASM_OUTPUT_EXTERNAL
2485 if (pending_assemble_externals_processed)
2487 assemble_external_real (decl);
2488 return;
2491 if (! pending_assemble_externals_set->add (decl))
2492 pending_assemble_externals = tree_cons (NULL, decl,
2493 pending_assemble_externals);
2494 #endif
2497 /* Similar, for calling a library function FUN. */
2499 void
2500 assemble_external_libcall (rtx fun)
2502 /* Declare library function name external when first used, if nec. */
2503 if (! SYMBOL_REF_USED (fun))
2505 SYMBOL_REF_USED (fun) = 1;
2506 targetm.asm_out.external_libcall (fun);
2510 /* Assemble a label named NAME. */
2512 void
2513 assemble_label (FILE *file, const char *name)
2515 ASM_OUTPUT_LABEL (file, name);
2518 /* Set the symbol_referenced flag for ID. */
2519 void
2520 mark_referenced (tree id)
2522 TREE_SYMBOL_REFERENCED (id) = 1;
2525 /* Set the symbol_referenced flag for DECL and notify callgraph. */
2526 void
2527 mark_decl_referenced (tree decl)
2529 if (TREE_CODE (decl) == FUNCTION_DECL)
2531 /* Extern inline functions don't become needed when referenced.
2532 If we know a method will be emitted in other TU and no new
2533 functions can be marked reachable, just use the external
2534 definition. */
2535 struct cgraph_node *node = cgraph_node::get_create (decl);
2536 if (!DECL_EXTERNAL (decl)
2537 && !node->definition)
2538 node->mark_force_output ();
2540 else if (TREE_CODE (decl) == VAR_DECL)
2542 varpool_node *node = varpool_node::get_create (decl);
2543 /* C++ frontend use mark_decl_references to force COMDAT variables
2544 to be output that might appear dead otherwise. */
2545 node->force_output = true;
2547 /* else do nothing - we can get various sorts of CST nodes here,
2548 which do not need to be marked. */
2552 /* Output to FILE (an assembly file) a reference to NAME. If NAME
2553 starts with a *, the rest of NAME is output verbatim. Otherwise
2554 NAME is transformed in a target-specific way (usually by the
2555 addition of an underscore). */
2557 void
2558 assemble_name_raw (FILE *file, const char *name)
2560 if (name[0] == '*')
2561 fputs (&name[1], file);
2562 else
2563 ASM_OUTPUT_LABELREF (file, name);
2566 /* Like assemble_name_raw, but should be used when NAME might refer to
2567 an entity that is also represented as a tree (like a function or
2568 variable). If NAME does refer to such an entity, that entity will
2569 be marked as referenced. */
2571 void
2572 assemble_name (FILE *file, const char *name)
2574 const char *real_name;
2575 tree id;
2577 real_name = targetm.strip_name_encoding (name);
2579 id = maybe_get_identifier (real_name);
2580 if (id)
2582 tree id_orig = id;
2584 mark_referenced (id);
2585 ultimate_transparent_alias_target (&id);
2586 if (id != id_orig)
2587 name = IDENTIFIER_POINTER (id);
2588 gcc_assert (! TREE_CHAIN (id));
2591 assemble_name_raw (file, name);
2594 /* Allocate SIZE bytes writable static space with a gensym name
2595 and return an RTX to refer to its address. */
2598 assemble_static_space (unsigned HOST_WIDE_INT size)
2600 char name[12];
2601 const char *namestring;
2602 rtx x;
2604 ASM_GENERATE_INTERNAL_LABEL (name, "LF", const_labelno);
2605 ++const_labelno;
2606 namestring = ggc_strdup (name);
2608 x = gen_rtx_SYMBOL_REF (Pmode, namestring);
2609 SYMBOL_REF_FLAGS (x) = SYMBOL_FLAG_LOCAL;
2611 #ifdef ASM_OUTPUT_ALIGNED_DECL_LOCAL
2612 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, NULL_TREE, name, size,
2613 BIGGEST_ALIGNMENT);
2614 #else
2615 #ifdef ASM_OUTPUT_ALIGNED_LOCAL
2616 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, BIGGEST_ALIGNMENT);
2617 #else
2619 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
2620 so that each uninitialized object starts on such a boundary. */
2621 /* Variable `rounded' might or might not be used in ASM_OUTPUT_LOCAL. */
2622 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED
2623 = ((size + (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1)
2624 / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
2625 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
2626 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
2628 #endif
2629 #endif
2630 return x;
2633 /* Assemble the static constant template for function entry trampolines.
2634 This is done at most once per compilation.
2635 Returns an RTX for the address of the template. */
2637 static GTY(()) rtx initial_trampoline;
2640 assemble_trampoline_template (void)
2642 char label[256];
2643 const char *name;
2644 int align;
2645 rtx symbol;
2647 gcc_assert (targetm.asm_out.trampoline_template != NULL);
2649 if (initial_trampoline)
2650 return initial_trampoline;
2652 /* By default, put trampoline templates in read-only data section. */
2654 #ifdef TRAMPOLINE_SECTION
2655 switch_to_section (TRAMPOLINE_SECTION);
2656 #else
2657 switch_to_section (readonly_data_section);
2658 #endif
2660 /* Write the assembler code to define one. */
2661 align = floor_log2 (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
2662 if (align > 0)
2663 ASM_OUTPUT_ALIGN (asm_out_file, align);
2665 targetm.asm_out.internal_label (asm_out_file, "LTRAMP", 0);
2666 targetm.asm_out.trampoline_template (asm_out_file);
2668 /* Record the rtl to refer to it. */
2669 ASM_GENERATE_INTERNAL_LABEL (label, "LTRAMP", 0);
2670 name = ggc_strdup (label);
2671 symbol = gen_rtx_SYMBOL_REF (Pmode, name);
2672 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_LOCAL;
2674 initial_trampoline = gen_const_mem (BLKmode, symbol);
2675 set_mem_align (initial_trampoline, TRAMPOLINE_ALIGNMENT);
2676 set_mem_size (initial_trampoline, TRAMPOLINE_SIZE);
2678 return initial_trampoline;
2681 /* A and B are either alignments or offsets. Return the minimum alignment
2682 that may be assumed after adding the two together. */
2684 static inline unsigned
2685 min_align (unsigned int a, unsigned int b)
2687 return (a | b) & -(a | b);
2690 /* Return the assembler directive for creating a given kind of integer
2691 object. SIZE is the number of bytes in the object and ALIGNED_P
2692 indicates whether it is known to be aligned. Return NULL if the
2693 assembly dialect has no such directive.
2695 The returned string should be printed at the start of a new line and
2696 be followed immediately by the object's initial value. */
2698 const char *
2699 integer_asm_op (int size, int aligned_p)
2701 struct asm_int_op *ops;
2703 if (aligned_p)
2704 ops = &targetm.asm_out.aligned_op;
2705 else
2706 ops = &targetm.asm_out.unaligned_op;
2708 switch (size)
2710 case 1:
2711 return targetm.asm_out.byte_op;
2712 case 2:
2713 return ops->hi;
2714 case 4:
2715 return ops->si;
2716 case 8:
2717 return ops->di;
2718 case 16:
2719 return ops->ti;
2720 default:
2721 return NULL;
2725 /* Use directive OP to assemble an integer object X. Print OP at the
2726 start of the line, followed immediately by the value of X. */
2728 void
2729 assemble_integer_with_op (const char *op, rtx x)
2731 fputs (op, asm_out_file);
2732 output_addr_const (asm_out_file, x);
2733 fputc ('\n', asm_out_file);
2736 /* The default implementation of the asm_out.integer target hook. */
2738 bool
2739 default_assemble_integer (rtx x ATTRIBUTE_UNUSED,
2740 unsigned int size ATTRIBUTE_UNUSED,
2741 int aligned_p ATTRIBUTE_UNUSED)
2743 const char *op = integer_asm_op (size, aligned_p);
2744 /* Avoid GAS bugs for large values. Specifically negative values whose
2745 absolute value fits in a bfd_vma, but not in a bfd_signed_vma. */
2746 if (size > UNITS_PER_WORD && size > POINTER_SIZE_UNITS)
2747 return false;
2748 return op && (assemble_integer_with_op (op, x), true);
2751 /* Assemble the integer constant X into an object of SIZE bytes. ALIGN is
2752 the alignment of the integer in bits. Return 1 if we were able to output
2753 the constant, otherwise 0. We must be able to output the constant,
2754 if FORCE is nonzero. */
2756 bool
2757 assemble_integer (rtx x, unsigned int size, unsigned int align, int force)
2759 int aligned_p;
2761 aligned_p = (align >= MIN (size * BITS_PER_UNIT, BIGGEST_ALIGNMENT));
2763 /* See if the target hook can handle this kind of object. */
2764 if (targetm.asm_out.integer (x, size, aligned_p))
2765 return true;
2767 /* If the object is a multi-byte one, try splitting it up. Split
2768 it into words it if is multi-word, otherwise split it into bytes. */
2769 if (size > 1)
2771 machine_mode omode, imode;
2772 unsigned int subalign;
2773 unsigned int subsize, i;
2774 enum mode_class mclass;
2776 subsize = size > UNITS_PER_WORD? UNITS_PER_WORD : 1;
2777 subalign = MIN (align, subsize * BITS_PER_UNIT);
2778 if (GET_CODE (x) == CONST_FIXED)
2779 mclass = GET_MODE_CLASS (GET_MODE (x));
2780 else
2781 mclass = MODE_INT;
2783 omode = mode_for_size (subsize * BITS_PER_UNIT, mclass, 0);
2784 imode = mode_for_size (size * BITS_PER_UNIT, mclass, 0);
2786 for (i = 0; i < size; i += subsize)
2788 rtx partial = simplify_subreg (omode, x, imode, i);
2789 if (!partial || !assemble_integer (partial, subsize, subalign, 0))
2790 break;
2792 if (i == size)
2793 return true;
2795 /* If we've printed some of it, but not all of it, there's no going
2796 back now. */
2797 gcc_assert (!i);
2800 gcc_assert (!force);
2802 return false;
2805 void
2806 assemble_real (REAL_VALUE_TYPE d, machine_mode mode, unsigned int align,
2807 bool reverse)
2809 long data[4] = {0, 0, 0, 0};
2810 int bitsize, nelts, nunits, units_per;
2811 rtx elt;
2813 /* This is hairy. We have a quantity of known size. real_to_target
2814 will put it into an array of *host* longs, 32 bits per element
2815 (even if long is more than 32 bits). We need to determine the
2816 number of array elements that are occupied (nelts) and the number
2817 of *target* min-addressable units that will be occupied in the
2818 object file (nunits). We cannot assume that 32 divides the
2819 mode's bitsize (size * BITS_PER_UNIT) evenly.
2821 size * BITS_PER_UNIT is used here to make sure that padding bits
2822 (which might appear at either end of the value; real_to_target
2823 will include the padding bits in its output array) are included. */
2825 nunits = GET_MODE_SIZE (mode);
2826 bitsize = nunits * BITS_PER_UNIT;
2827 nelts = CEIL (bitsize, 32);
2828 units_per = 32 / BITS_PER_UNIT;
2830 real_to_target (data, &d, mode);
2832 /* Put out the first word with the specified alignment. */
2833 if (reverse)
2834 elt = flip_storage_order (SImode, GEN_INT (data[nelts - 1]));
2835 else
2836 elt = GEN_INT (data[0]);
2837 assemble_integer (elt, MIN (nunits, units_per), align, 1);
2838 nunits -= units_per;
2840 /* Subsequent words need only 32-bit alignment. */
2841 align = min_align (align, 32);
2843 for (int i = 1; i < nelts; i++)
2845 if (reverse)
2846 elt = flip_storage_order (SImode, GEN_INT (data[nelts - 1 - i]));
2847 else
2848 elt = GEN_INT (data[i]);
2849 assemble_integer (elt, MIN (nunits, units_per), align, 1);
2850 nunits -= units_per;
2854 /* Given an expression EXP with a constant value,
2855 reduce it to the sum of an assembler symbol and an integer.
2856 Store them both in the structure *VALUE.
2857 EXP must be reducible. */
2859 struct addr_const {
2860 rtx base;
2861 HOST_WIDE_INT offset;
2864 static void
2865 decode_addr_const (tree exp, struct addr_const *value)
2867 tree target = TREE_OPERAND (exp, 0);
2868 int offset = 0;
2869 rtx x;
2871 while (1)
2873 if (TREE_CODE (target) == COMPONENT_REF
2874 && tree_fits_shwi_p (byte_position (TREE_OPERAND (target, 1))))
2876 offset += int_byte_position (TREE_OPERAND (target, 1));
2877 target = TREE_OPERAND (target, 0);
2879 else if (TREE_CODE (target) == ARRAY_REF
2880 || TREE_CODE (target) == ARRAY_RANGE_REF)
2882 offset += (tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (target)))
2883 * tree_to_shwi (TREE_OPERAND (target, 1)));
2884 target = TREE_OPERAND (target, 0);
2886 else if (TREE_CODE (target) == MEM_REF
2887 && TREE_CODE (TREE_OPERAND (target, 0)) == ADDR_EXPR)
2889 offset += mem_ref_offset (target).to_short_addr ();
2890 target = TREE_OPERAND (TREE_OPERAND (target, 0), 0);
2892 else if (TREE_CODE (target) == INDIRECT_REF
2893 && TREE_CODE (TREE_OPERAND (target, 0)) == NOP_EXPR
2894 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (target, 0), 0))
2895 == ADDR_EXPR)
2896 target = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (target, 0), 0), 0);
2897 else
2898 break;
2901 switch (TREE_CODE (target))
2903 case VAR_DECL:
2904 case FUNCTION_DECL:
2905 x = DECL_RTL (target);
2906 break;
2908 case LABEL_DECL:
2909 x = gen_rtx_MEM (FUNCTION_MODE,
2910 gen_rtx_LABEL_REF (Pmode, force_label_rtx (target)));
2911 break;
2913 case REAL_CST:
2914 case FIXED_CST:
2915 case STRING_CST:
2916 case COMPLEX_CST:
2917 case CONSTRUCTOR:
2918 case INTEGER_CST:
2919 x = output_constant_def (target, 1);
2920 break;
2922 default:
2923 gcc_unreachable ();
2926 gcc_assert (MEM_P (x));
2927 x = XEXP (x, 0);
2929 value->base = x;
2930 value->offset = offset;
2933 static GTY(()) hash_table<tree_descriptor_hasher> *const_desc_htab;
2935 static void maybe_output_constant_def_contents (struct constant_descriptor_tree *, int);
2937 /* Constant pool accessor function. */
2939 hash_table<tree_descriptor_hasher> *
2940 constant_pool_htab (void)
2942 return const_desc_htab;
2945 /* Compute a hash code for a constant expression. */
2947 hashval_t
2948 tree_descriptor_hasher::hash (constant_descriptor_tree *ptr)
2950 return ptr->hash;
2953 static hashval_t
2954 const_hash_1 (const tree exp)
2956 const char *p;
2957 hashval_t hi;
2958 int len, i;
2959 enum tree_code code = TREE_CODE (exp);
2961 /* Either set P and LEN to the address and len of something to hash and
2962 exit the switch or return a value. */
2964 switch (code)
2966 case INTEGER_CST:
2967 p = (char *) &TREE_INT_CST_ELT (exp, 0);
2968 len = TREE_INT_CST_NUNITS (exp) * sizeof (HOST_WIDE_INT);
2969 break;
2971 case REAL_CST:
2972 return real_hash (TREE_REAL_CST_PTR (exp));
2974 case FIXED_CST:
2975 return fixed_hash (TREE_FIXED_CST_PTR (exp));
2977 case STRING_CST:
2978 p = TREE_STRING_POINTER (exp);
2979 len = TREE_STRING_LENGTH (exp);
2980 break;
2982 case COMPLEX_CST:
2983 return (const_hash_1 (TREE_REALPART (exp)) * 5
2984 + const_hash_1 (TREE_IMAGPART (exp)));
2986 case VECTOR_CST:
2988 unsigned i;
2990 hi = 7 + VECTOR_CST_NELTS (exp);
2992 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
2993 hi = hi * 563 + const_hash_1 (VECTOR_CST_ELT (exp, i));
2995 return hi;
2998 case CONSTRUCTOR:
3000 unsigned HOST_WIDE_INT idx;
3001 tree value;
3003 hi = 5 + int_size_in_bytes (TREE_TYPE (exp));
3005 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
3006 if (value)
3007 hi = hi * 603 + const_hash_1 (value);
3009 return hi;
3012 case ADDR_EXPR:
3013 case FDESC_EXPR:
3015 struct addr_const value;
3017 decode_addr_const (exp, &value);
3018 switch (GET_CODE (value.base))
3020 case SYMBOL_REF:
3021 /* Don't hash the address of the SYMBOL_REF;
3022 only use the offset and the symbol name. */
3023 hi = value.offset;
3024 p = XSTR (value.base, 0);
3025 for (i = 0; p[i] != 0; i++)
3026 hi = ((hi * 613) + (unsigned) (p[i]));
3027 break;
3029 case LABEL_REF:
3030 hi = (value.offset
3031 + CODE_LABEL_NUMBER (LABEL_REF_LABEL (value.base)) * 13);
3032 break;
3034 default:
3035 gcc_unreachable ();
3038 return hi;
3040 case PLUS_EXPR:
3041 case POINTER_PLUS_EXPR:
3042 case MINUS_EXPR:
3043 return (const_hash_1 (TREE_OPERAND (exp, 0)) * 9
3044 + const_hash_1 (TREE_OPERAND (exp, 1)));
3046 CASE_CONVERT:
3047 return const_hash_1 (TREE_OPERAND (exp, 0)) * 7 + 2;
3049 default:
3050 /* A language specific constant. Just hash the code. */
3051 return code;
3054 /* Compute hashing function. */
3055 hi = len;
3056 for (i = 0; i < len; i++)
3057 hi = ((hi * 613) + (unsigned) (p[i]));
3059 return hi;
3062 /* Wrapper of compare_constant, for the htab interface. */
3063 bool
3064 tree_descriptor_hasher::equal (constant_descriptor_tree *c1,
3065 constant_descriptor_tree *c2)
3067 if (c1->hash != c2->hash)
3068 return 0;
3069 return compare_constant (c1->value, c2->value);
3072 /* Compare t1 and t2, and return 1 only if they are known to result in
3073 the same bit pattern on output. */
3075 static int
3076 compare_constant (const tree t1, const tree t2)
3078 enum tree_code typecode;
3080 if (t1 == NULL_TREE)
3081 return t2 == NULL_TREE;
3082 if (t2 == NULL_TREE)
3083 return 0;
3085 if (TREE_CODE (t1) != TREE_CODE (t2))
3086 return 0;
3088 switch (TREE_CODE (t1))
3090 case INTEGER_CST:
3091 /* Integer constants are the same only if the same width of type. */
3092 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3093 return 0;
3094 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
3095 return 0;
3096 return tree_int_cst_equal (t1, t2);
3098 case REAL_CST:
3099 /* Real constants are the same only if the same width of type. */
3100 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3101 return 0;
3103 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
3105 case FIXED_CST:
3106 /* Fixed constants are the same only if the same width of type. */
3107 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3108 return 0;
3110 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
3112 case STRING_CST:
3113 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
3114 return 0;
3116 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
3117 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
3118 TREE_STRING_LENGTH (t1)));
3120 case COMPLEX_CST:
3121 return (compare_constant (TREE_REALPART (t1), TREE_REALPART (t2))
3122 && compare_constant (TREE_IMAGPART (t1), TREE_IMAGPART (t2)));
3124 case VECTOR_CST:
3126 unsigned i;
3128 if (VECTOR_CST_NELTS (t1) != VECTOR_CST_NELTS (t2))
3129 return 0;
3131 for (i = 0; i < VECTOR_CST_NELTS (t1); ++i)
3132 if (!compare_constant (VECTOR_CST_ELT (t1, i),
3133 VECTOR_CST_ELT (t2, i)))
3134 return 0;
3136 return 1;
3139 case CONSTRUCTOR:
3141 vec<constructor_elt, va_gc> *v1, *v2;
3142 unsigned HOST_WIDE_INT idx;
3144 typecode = TREE_CODE (TREE_TYPE (t1));
3145 if (typecode != TREE_CODE (TREE_TYPE (t2)))
3146 return 0;
3148 if (typecode == ARRAY_TYPE)
3150 HOST_WIDE_INT size_1 = int_size_in_bytes (TREE_TYPE (t1));
3151 /* For arrays, check that the sizes all match. */
3152 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))
3153 || size_1 == -1
3154 || size_1 != int_size_in_bytes (TREE_TYPE (t2)))
3155 return 0;
3157 else
3159 /* For record and union constructors, require exact type
3160 equality. */
3161 if (TREE_TYPE (t1) != TREE_TYPE (t2))
3162 return 0;
3165 v1 = CONSTRUCTOR_ELTS (t1);
3166 v2 = CONSTRUCTOR_ELTS (t2);
3167 if (vec_safe_length (v1) != vec_safe_length (v2))
3168 return 0;
3170 for (idx = 0; idx < vec_safe_length (v1); ++idx)
3172 constructor_elt *c1 = &(*v1)[idx];
3173 constructor_elt *c2 = &(*v2)[idx];
3175 /* Check that each value is the same... */
3176 if (!compare_constant (c1->value, c2->value))
3177 return 0;
3178 /* ... and that they apply to the same fields! */
3179 if (typecode == ARRAY_TYPE)
3181 if (!compare_constant (c1->index, c2->index))
3182 return 0;
3184 else
3186 if (c1->index != c2->index)
3187 return 0;
3191 return 1;
3194 case ADDR_EXPR:
3195 case FDESC_EXPR:
3197 struct addr_const value1, value2;
3198 enum rtx_code code;
3199 int ret;
3201 decode_addr_const (t1, &value1);
3202 decode_addr_const (t2, &value2);
3204 if (value1.offset != value2.offset)
3205 return 0;
3207 code = GET_CODE (value1.base);
3208 if (code != GET_CODE (value2.base))
3209 return 0;
3211 switch (code)
3213 case SYMBOL_REF:
3214 ret = (strcmp (XSTR (value1.base, 0), XSTR (value2.base, 0)) == 0);
3215 break;
3217 case LABEL_REF:
3218 ret = (CODE_LABEL_NUMBER (LABEL_REF_LABEL (value1.base))
3219 == CODE_LABEL_NUMBER (LABEL_REF_LABEL (value2.base)));
3220 break;
3222 default:
3223 gcc_unreachable ();
3225 return ret;
3228 case PLUS_EXPR:
3229 case POINTER_PLUS_EXPR:
3230 case MINUS_EXPR:
3231 case RANGE_EXPR:
3232 return (compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
3233 && compare_constant (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
3235 CASE_CONVERT:
3236 case VIEW_CONVERT_EXPR:
3237 return compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3239 default:
3240 return 0;
3243 gcc_unreachable ();
3246 /* Return the section into which constant EXP should be placed. */
3248 static section *
3249 get_constant_section (tree exp, unsigned int align)
3251 return targetm.asm_out.select_section (exp,
3252 compute_reloc_for_constant (exp),
3253 align);
3256 /* Return the size of constant EXP in bytes. */
3258 static HOST_WIDE_INT
3259 get_constant_size (tree exp)
3261 HOST_WIDE_INT size;
3263 size = int_size_in_bytes (TREE_TYPE (exp));
3264 if (TREE_CODE (exp) == STRING_CST)
3265 size = MAX (TREE_STRING_LENGTH (exp), size);
3266 return size;
3269 /* Subroutine of output_constant_def:
3270 No constant equal to EXP is known to have been output.
3271 Make a constant descriptor to enter EXP in the hash table.
3272 Assign the label number and construct RTL to refer to the
3273 constant's location in memory.
3274 Caller is responsible for updating the hash table. */
3276 static struct constant_descriptor_tree *
3277 build_constant_desc (tree exp)
3279 struct constant_descriptor_tree *desc;
3280 rtx symbol, rtl;
3281 char label[256];
3282 int labelno;
3283 tree decl;
3285 desc = ggc_alloc<constant_descriptor_tree> ();
3286 desc->value = exp;
3288 /* Create a string containing the label name, in LABEL. */
3289 labelno = const_labelno++;
3290 ASM_GENERATE_INTERNAL_LABEL (label, "LC", labelno);
3292 /* Construct the VAR_DECL associated with the constant. */
3293 decl = build_decl (UNKNOWN_LOCATION, VAR_DECL, get_identifier (label),
3294 TREE_TYPE (exp));
3295 DECL_ARTIFICIAL (decl) = 1;
3296 DECL_IGNORED_P (decl) = 1;
3297 TREE_READONLY (decl) = 1;
3298 TREE_STATIC (decl) = 1;
3299 TREE_ADDRESSABLE (decl) = 1;
3300 /* We don't set the RTL yet as this would cause varpool to assume that the
3301 variable is referenced. Moreover, it would just be dropped in LTO mode.
3302 Instead we set the flag that will be recognized in make_decl_rtl. */
3303 DECL_IN_CONSTANT_POOL (decl) = 1;
3304 DECL_INITIAL (decl) = desc->value;
3305 /* ??? CONSTANT_ALIGNMENT hasn't been updated for vector types on most
3306 architectures so use DATA_ALIGNMENT as well, except for strings. */
3307 if (TREE_CODE (exp) == STRING_CST)
3309 #ifdef CONSTANT_ALIGNMENT
3310 DECL_ALIGN (decl) = CONSTANT_ALIGNMENT (exp, DECL_ALIGN (decl));
3311 #endif
3313 else
3314 align_variable (decl, 0);
3316 /* Now construct the SYMBOL_REF and the MEM. */
3317 if (use_object_blocks_p ())
3319 int align = (TREE_CODE (decl) == CONST_DECL
3320 || (TREE_CODE (decl) == VAR_DECL
3321 && DECL_IN_CONSTANT_POOL (decl))
3322 ? DECL_ALIGN (decl)
3323 : symtab_node::get (decl)->definition_alignment ());
3324 section *sect = get_constant_section (exp, align);
3325 symbol = create_block_symbol (ggc_strdup (label),
3326 get_block_for_section (sect), -1);
3328 else
3329 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3330 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3331 SET_SYMBOL_REF_DECL (symbol, decl);
3332 TREE_CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3334 rtl = gen_const_mem (TYPE_MODE (TREE_TYPE (exp)), symbol);
3335 set_mem_attributes (rtl, exp, 1);
3336 set_mem_alias_set (rtl, 0);
3338 /* We cannot share RTX'es in pool entries.
3339 Mark this piece of RTL as required for unsharing. */
3340 RTX_FLAG (rtl, used) = 1;
3342 /* Set flags or add text to the name to record information, such as
3343 that it is a local symbol. If the name is changed, the macro
3344 ASM_OUTPUT_LABELREF will have to know how to strip this
3345 information. This call might invalidate our local variable
3346 SYMBOL; we can't use it afterward. */
3347 targetm.encode_section_info (exp, rtl, true);
3349 desc->rtl = rtl;
3351 return desc;
3354 /* Return an rtx representing a reference to constant data in memory
3355 for the constant expression EXP.
3357 If assembler code for such a constant has already been output,
3358 return an rtx to refer to it.
3359 Otherwise, output such a constant in memory
3360 and generate an rtx for it.
3362 If DEFER is nonzero, this constant can be deferred and output only
3363 if referenced in the function after all optimizations.
3365 `const_desc_table' records which constants already have label strings. */
3368 output_constant_def (tree exp, int defer)
3370 struct constant_descriptor_tree *desc;
3371 struct constant_descriptor_tree key;
3373 /* Look up EXP in the table of constant descriptors. If we didn't find
3374 it, create a new one. */
3375 key.value = exp;
3376 key.hash = const_hash_1 (exp);
3377 constant_descriptor_tree **loc
3378 = const_desc_htab->find_slot_with_hash (&key, key.hash, INSERT);
3380 desc = *loc;
3381 if (desc == 0)
3383 desc = build_constant_desc (exp);
3384 desc->hash = key.hash;
3385 *loc = desc;
3388 maybe_output_constant_def_contents (desc, defer);
3389 return desc->rtl;
3392 /* Subroutine of output_constant_def: Decide whether or not we need to
3393 output the constant DESC now, and if so, do it. */
3394 static void
3395 maybe_output_constant_def_contents (struct constant_descriptor_tree *desc,
3396 int defer)
3398 rtx symbol = XEXP (desc->rtl, 0);
3399 tree exp = desc->value;
3401 if (flag_syntax_only)
3402 return;
3404 if (TREE_ASM_WRITTEN (exp))
3405 /* Already output; don't do it again. */
3406 return;
3408 /* We can always defer constants as long as the context allows
3409 doing so. */
3410 if (defer)
3412 /* Increment n_deferred_constants if it exists. It needs to be at
3413 least as large as the number of constants actually referred to
3414 by the function. If it's too small we'll stop looking too early
3415 and fail to emit constants; if it's too large we'll only look
3416 through the entire function when we could have stopped earlier. */
3417 if (cfun)
3418 n_deferred_constants++;
3419 return;
3422 output_constant_def_contents (symbol);
3425 /* Subroutine of output_constant_def_contents. Output the definition
3426 of constant EXP, which is pointed to by label LABEL. ALIGN is the
3427 constant's alignment in bits. */
3429 static void
3430 assemble_constant_contents (tree exp, const char *label, unsigned int align)
3432 HOST_WIDE_INT size;
3434 size = get_constant_size (exp);
3436 /* Do any machine/system dependent processing of the constant. */
3437 targetm.asm_out.declare_constant_name (asm_out_file, label, exp, size);
3439 /* Output the value of EXP. */
3440 output_constant (exp, size, align, false);
3442 targetm.asm_out.decl_end ();
3445 /* We must output the constant data referred to by SYMBOL; do so. */
3447 static void
3448 output_constant_def_contents (rtx symbol)
3450 tree decl = SYMBOL_REF_DECL (symbol);
3451 tree exp = DECL_INITIAL (decl);
3452 bool asan_protected = false;
3454 /* Make sure any other constants whose addresses appear in EXP
3455 are assigned label numbers. */
3456 output_addressed_constants (exp);
3458 /* We are no longer deferring this constant. */
3459 TREE_ASM_WRITTEN (decl) = TREE_ASM_WRITTEN (exp) = 1;
3461 if ((flag_sanitize & SANITIZE_ADDRESS)
3462 && TREE_CODE (exp) == STRING_CST
3463 && asan_protect_global (exp))
3465 asan_protected = true;
3466 DECL_ALIGN (decl) = MAX (DECL_ALIGN (decl),
3467 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
3470 /* If the constant is part of an object block, make sure that the
3471 decl has been positioned within its block, but do not write out
3472 its definition yet. output_object_blocks will do that later. */
3473 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
3474 place_block_symbol (symbol);
3475 else
3477 int align = (TREE_CODE (decl) == CONST_DECL
3478 || (TREE_CODE (decl) == VAR_DECL
3479 && DECL_IN_CONSTANT_POOL (decl))
3480 ? DECL_ALIGN (decl)
3481 : symtab_node::get (decl)->definition_alignment ());
3482 switch_to_section (get_constant_section (exp, align));
3483 if (align > BITS_PER_UNIT)
3484 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
3485 assemble_constant_contents (exp, XSTR (symbol, 0), align);
3486 if (asan_protected)
3488 HOST_WIDE_INT size = get_constant_size (exp);
3489 assemble_zeros (asan_red_zone_size (size));
3494 /* Look up EXP in the table of constant descriptors. Return the rtl
3495 if it has been emitted, else null. */
3498 lookup_constant_def (tree exp)
3500 struct constant_descriptor_tree key;
3502 key.value = exp;
3503 key.hash = const_hash_1 (exp);
3504 constant_descriptor_tree *desc
3505 = const_desc_htab->find_with_hash (&key, key.hash);
3507 return (desc ? desc->rtl : NULL_RTX);
3510 /* Return a tree representing a reference to constant data in memory
3511 for the constant expression EXP.
3513 This is the counterpart of output_constant_def at the Tree level. */
3515 tree
3516 tree_output_constant_def (tree exp)
3518 struct constant_descriptor_tree *desc, key;
3519 tree decl;
3521 /* Look up EXP in the table of constant descriptors. If we didn't find
3522 it, create a new one. */
3523 key.value = exp;
3524 key.hash = const_hash_1 (exp);
3525 constant_descriptor_tree **loc
3526 = const_desc_htab->find_slot_with_hash (&key, key.hash, INSERT);
3528 desc = *loc;
3529 if (desc == 0)
3531 desc = build_constant_desc (exp);
3532 desc->hash = key.hash;
3533 *loc = desc;
3536 decl = SYMBOL_REF_DECL (XEXP (desc->rtl, 0));
3537 varpool_node::finalize_decl (decl);
3538 return decl;
3541 struct GTY((chain_next ("%h.next"), for_user)) constant_descriptor_rtx {
3542 struct constant_descriptor_rtx *next;
3543 rtx mem;
3544 rtx sym;
3545 rtx constant;
3546 HOST_WIDE_INT offset;
3547 hashval_t hash;
3548 machine_mode mode;
3549 unsigned int align;
3550 int labelno;
3551 int mark;
3554 struct const_rtx_desc_hasher : ggc_hasher<constant_descriptor_rtx *>
3556 static hashval_t hash (constant_descriptor_rtx *);
3557 static bool equal (constant_descriptor_rtx *, constant_descriptor_rtx *);
3560 /* Used in the hash tables to avoid outputting the same constant
3561 twice. Unlike 'struct constant_descriptor_tree', RTX constants
3562 are output once per function, not once per file. */
3563 /* ??? Only a few targets need per-function constant pools. Most
3564 can use one per-file pool. Should add a targetm bit to tell the
3565 difference. */
3567 struct GTY(()) rtx_constant_pool {
3568 /* Pointers to first and last constant in pool, as ordered by offset. */
3569 struct constant_descriptor_rtx *first;
3570 struct constant_descriptor_rtx *last;
3572 /* Hash facility for making memory-constants from constant rtl-expressions.
3573 It is used on RISC machines where immediate integer arguments and
3574 constant addresses are restricted so that such constants must be stored
3575 in memory. */
3576 hash_table<const_rtx_desc_hasher> *const_rtx_htab;
3578 /* Current offset in constant pool (does not include any
3579 machine-specific header). */
3580 HOST_WIDE_INT offset;
3583 /* Hash and compare functions for const_rtx_htab. */
3585 hashval_t
3586 const_rtx_desc_hasher::hash (constant_descriptor_rtx *desc)
3588 return desc->hash;
3591 bool
3592 const_rtx_desc_hasher::equal (constant_descriptor_rtx *x,
3593 constant_descriptor_rtx *y)
3595 if (x->mode != y->mode)
3596 return 0;
3597 return rtx_equal_p (x->constant, y->constant);
3600 /* Hash one component of a constant. */
3602 static hashval_t
3603 const_rtx_hash_1 (const_rtx x)
3605 unsigned HOST_WIDE_INT hwi;
3606 machine_mode mode;
3607 enum rtx_code code;
3608 hashval_t h;
3609 int i;
3611 code = GET_CODE (x);
3612 mode = GET_MODE (x);
3613 h = (hashval_t) code * 1048573 + mode;
3615 switch (code)
3617 case CONST_INT:
3618 hwi = INTVAL (x);
3620 fold_hwi:
3622 int shift = sizeof (hashval_t) * CHAR_BIT;
3623 const int n = sizeof (HOST_WIDE_INT) / sizeof (hashval_t);
3625 h ^= (hashval_t) hwi;
3626 for (i = 1; i < n; ++i)
3628 hwi >>= shift;
3629 h ^= (hashval_t) hwi;
3632 break;
3634 case CONST_WIDE_INT:
3635 hwi = GET_MODE_PRECISION (mode);
3637 for (i = 0; i < CONST_WIDE_INT_NUNITS (x); i++)
3638 hwi ^= CONST_WIDE_INT_ELT (x, i);
3639 goto fold_hwi;
3642 case CONST_DOUBLE:
3643 if (TARGET_SUPPORTS_WIDE_INT == 0 && mode == VOIDmode)
3645 hwi = CONST_DOUBLE_LOW (x) ^ CONST_DOUBLE_HIGH (x);
3646 goto fold_hwi;
3648 else
3649 h ^= real_hash (CONST_DOUBLE_REAL_VALUE (x));
3650 break;
3652 case CONST_FIXED:
3653 h ^= fixed_hash (CONST_FIXED_VALUE (x));
3654 break;
3656 case SYMBOL_REF:
3657 h ^= htab_hash_string (XSTR (x, 0));
3658 break;
3660 case LABEL_REF:
3661 h = h * 251 + CODE_LABEL_NUMBER (LABEL_REF_LABEL (x));
3662 break;
3664 case UNSPEC:
3665 case UNSPEC_VOLATILE:
3666 h = h * 251 + XINT (x, 1);
3667 break;
3669 default:
3670 break;
3673 return h;
3676 /* Compute a hash value for X, which should be a constant. */
3678 static hashval_t
3679 const_rtx_hash (rtx x)
3681 hashval_t h = 0;
3682 subrtx_iterator::array_type array;
3683 FOR_EACH_SUBRTX (iter, array, x, ALL)
3684 h = h * 509 + const_rtx_hash_1 (*iter);
3685 return h;
3689 /* Create and return a new rtx constant pool. */
3691 static struct rtx_constant_pool *
3692 create_constant_pool (void)
3694 struct rtx_constant_pool *pool;
3696 pool = ggc_alloc<rtx_constant_pool> ();
3697 pool->const_rtx_htab = hash_table<const_rtx_desc_hasher>::create_ggc (31);
3698 pool->first = NULL;
3699 pool->last = NULL;
3700 pool->offset = 0;
3701 return pool;
3704 /* Initialize constant pool hashing for a new function. */
3706 void
3707 init_varasm_status (void)
3709 crtl->varasm.pool = create_constant_pool ();
3710 crtl->varasm.deferred_constants = 0;
3713 /* Given a MINUS expression, simplify it if both sides
3714 include the same symbol. */
3717 simplify_subtraction (rtx x)
3719 rtx r = simplify_rtx (x);
3720 return r ? r : x;
3723 /* Given a constant rtx X, make (or find) a memory constant for its value
3724 and return a MEM rtx to refer to it in memory. */
3727 force_const_mem (machine_mode mode, rtx x)
3729 struct constant_descriptor_rtx *desc, tmp;
3730 struct rtx_constant_pool *pool;
3731 char label[256];
3732 rtx def, symbol;
3733 hashval_t hash;
3734 unsigned int align;
3735 constant_descriptor_rtx **slot;
3737 /* If we're not allowed to drop X into the constant pool, don't. */
3738 if (targetm.cannot_force_const_mem (mode, x))
3739 return NULL_RTX;
3741 /* Record that this function has used a constant pool entry. */
3742 crtl->uses_const_pool = 1;
3744 /* Decide which pool to use. */
3745 pool = (targetm.use_blocks_for_constant_p (mode, x)
3746 ? shared_constant_pool
3747 : crtl->varasm.pool);
3749 /* Lookup the value in the hashtable. */
3750 tmp.constant = x;
3751 tmp.mode = mode;
3752 hash = const_rtx_hash (x);
3753 slot = pool->const_rtx_htab->find_slot_with_hash (&tmp, hash, INSERT);
3754 desc = *slot;
3756 /* If the constant was already present, return its memory. */
3757 if (desc)
3758 return copy_rtx (desc->mem);
3760 /* Otherwise, create a new descriptor. */
3761 desc = ggc_alloc<constant_descriptor_rtx> ();
3762 *slot = desc;
3764 /* Align the location counter as required by EXP's data type. */
3765 align = GET_MODE_ALIGNMENT (mode == VOIDmode ? word_mode : mode);
3766 #ifdef CONSTANT_ALIGNMENT
3768 tree type = lang_hooks.types.type_for_mode (mode, 0);
3769 if (type != NULL_TREE)
3770 align = CONSTANT_ALIGNMENT (make_tree (type, x), align);
3772 #endif
3774 pool->offset += (align / BITS_PER_UNIT) - 1;
3775 pool->offset &= ~ ((align / BITS_PER_UNIT) - 1);
3777 desc->next = NULL;
3778 desc->constant = copy_rtx (tmp.constant);
3779 desc->offset = pool->offset;
3780 desc->hash = hash;
3781 desc->mode = mode;
3782 desc->align = align;
3783 desc->labelno = const_labelno;
3784 desc->mark = 0;
3786 pool->offset += GET_MODE_SIZE (mode);
3787 if (pool->last)
3788 pool->last->next = desc;
3789 else
3790 pool->first = pool->last = desc;
3791 pool->last = desc;
3793 /* Create a string containing the label name, in LABEL. */
3794 ASM_GENERATE_INTERNAL_LABEL (label, "LC", const_labelno);
3795 ++const_labelno;
3797 /* Construct the SYMBOL_REF. Make sure to mark it as belonging to
3798 the constants pool. */
3799 if (use_object_blocks_p () && targetm.use_blocks_for_constant_p (mode, x))
3801 section *sect = targetm.asm_out.select_rtx_section (mode, x, align);
3802 symbol = create_block_symbol (ggc_strdup (label),
3803 get_block_for_section (sect), -1);
3805 else
3806 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3807 desc->sym = symbol;
3808 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3809 CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3810 SET_SYMBOL_REF_CONSTANT (symbol, desc);
3812 /* Construct the MEM. */
3813 desc->mem = def = gen_const_mem (mode, symbol);
3814 set_mem_attributes (def, lang_hooks.types.type_for_mode (mode, 0), 1);
3815 set_mem_align (def, align);
3817 /* If we're dropping a label to the constant pool, make sure we
3818 don't delete it. */
3819 if (GET_CODE (x) == LABEL_REF)
3820 LABEL_PRESERVE_P (XEXP (x, 0)) = 1;
3822 return copy_rtx (def);
3825 /* Given a constant pool SYMBOL_REF, return the corresponding constant. */
3828 get_pool_constant (const_rtx addr)
3830 return SYMBOL_REF_CONSTANT (addr)->constant;
3833 /* Given a constant pool SYMBOL_REF, return the corresponding constant
3834 and whether it has been output or not. */
3837 get_pool_constant_mark (rtx addr, bool *pmarked)
3839 struct constant_descriptor_rtx *desc;
3841 desc = SYMBOL_REF_CONSTANT (addr);
3842 *pmarked = (desc->mark != 0);
3843 return desc->constant;
3846 /* Similar, return the mode. */
3848 machine_mode
3849 get_pool_mode (const_rtx addr)
3851 return SYMBOL_REF_CONSTANT (addr)->mode;
3854 /* Return the size of the constant pool. */
3857 get_pool_size (void)
3859 return crtl->varasm.pool->offset;
3862 /* Worker function for output_constant_pool_1. Emit assembly for X
3863 in MODE with known alignment ALIGN. */
3865 static void
3866 output_constant_pool_2 (machine_mode mode, rtx x, unsigned int align)
3868 switch (GET_MODE_CLASS (mode))
3870 case MODE_FLOAT:
3871 case MODE_DECIMAL_FLOAT:
3873 REAL_VALUE_TYPE r;
3875 gcc_assert (CONST_DOUBLE_AS_FLOAT_P (x));
3876 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
3877 assemble_real (r, mode, align, false);
3878 break;
3881 case MODE_INT:
3882 case MODE_PARTIAL_INT:
3883 case MODE_FRACT:
3884 case MODE_UFRACT:
3885 case MODE_ACCUM:
3886 case MODE_UACCUM:
3887 case MODE_POINTER_BOUNDS:
3888 assemble_integer (x, GET_MODE_SIZE (mode), align, 1);
3889 break;
3891 case MODE_VECTOR_FLOAT:
3892 case MODE_VECTOR_INT:
3893 case MODE_VECTOR_FRACT:
3894 case MODE_VECTOR_UFRACT:
3895 case MODE_VECTOR_ACCUM:
3896 case MODE_VECTOR_UACCUM:
3898 int i, units;
3899 machine_mode submode = GET_MODE_INNER (mode);
3900 unsigned int subalign = MIN (align, GET_MODE_BITSIZE (submode));
3902 gcc_assert (GET_CODE (x) == CONST_VECTOR);
3903 units = CONST_VECTOR_NUNITS (x);
3905 for (i = 0; i < units; i++)
3907 rtx elt = CONST_VECTOR_ELT (x, i);
3908 output_constant_pool_2 (submode, elt, i ? subalign : align);
3911 break;
3913 default:
3914 gcc_unreachable ();
3918 /* Worker function for output_constant_pool. Emit constant DESC,
3919 giving it ALIGN bits of alignment. */
3921 static void
3922 output_constant_pool_1 (struct constant_descriptor_rtx *desc,
3923 unsigned int align)
3925 rtx x, tmp;
3927 x = desc->constant;
3929 /* See if X is a LABEL_REF (or a CONST referring to a LABEL_REF)
3930 whose CODE_LABEL has been deleted. This can occur if a jump table
3931 is eliminated by optimization. If so, write a constant of zero
3932 instead. Note that this can also happen by turning the
3933 CODE_LABEL into a NOTE. */
3934 /* ??? This seems completely and utterly wrong. Certainly it's
3935 not true for NOTE_INSN_DELETED_LABEL, but I disbelieve proper
3936 functioning even with rtx_insn::deleted and friends. */
3938 tmp = x;
3939 switch (GET_CODE (tmp))
3941 case CONST:
3942 if (GET_CODE (XEXP (tmp, 0)) != PLUS
3943 || GET_CODE (XEXP (XEXP (tmp, 0), 0)) != LABEL_REF)
3944 break;
3945 tmp = XEXP (XEXP (tmp, 0), 0);
3946 /* FALLTHRU */
3948 case LABEL_REF:
3949 tmp = LABEL_REF_LABEL (tmp);
3950 gcc_assert (!as_a<rtx_insn *> (tmp)->deleted ());
3951 gcc_assert (!NOTE_P (tmp)
3952 || NOTE_KIND (tmp) != NOTE_INSN_DELETED);
3953 break;
3955 default:
3956 break;
3959 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3960 ASM_OUTPUT_SPECIAL_POOL_ENTRY (asm_out_file, x, desc->mode,
3961 align, desc->labelno, done);
3962 #endif
3964 assemble_align (align);
3966 /* Output the label. */
3967 targetm.asm_out.internal_label (asm_out_file, "LC", desc->labelno);
3969 /* Output the data. */
3970 output_constant_pool_2 (desc->mode, x, align);
3972 /* Make sure all constants in SECTION_MERGE and not SECTION_STRINGS
3973 sections have proper size. */
3974 if (align > GET_MODE_BITSIZE (desc->mode)
3975 && in_section
3976 && (in_section->common.flags & SECTION_MERGE))
3977 assemble_align (align);
3979 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3980 done:
3981 #endif
3982 return;
3985 /* Mark all constants that are referenced by SYMBOL_REFs in X.
3986 Emit referenced deferred strings. */
3988 static void
3989 mark_constants_in_pattern (rtx insn)
3991 subrtx_iterator::array_type array;
3992 FOR_EACH_SUBRTX (iter, array, PATTERN (insn), ALL)
3994 const_rtx x = *iter;
3995 if (GET_CODE (x) == SYMBOL_REF)
3997 if (CONSTANT_POOL_ADDRESS_P (x))
3999 struct constant_descriptor_rtx *desc = SYMBOL_REF_CONSTANT (x);
4000 if (desc->mark == 0)
4002 desc->mark = 1;
4003 iter.substitute (desc->constant);
4006 else if (TREE_CONSTANT_POOL_ADDRESS_P (x))
4008 tree decl = SYMBOL_REF_DECL (x);
4009 if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
4011 n_deferred_constants--;
4012 output_constant_def_contents (CONST_CAST_RTX (x));
4019 /* Look through appropriate parts of INSN, marking all entries in the
4020 constant pool which are actually being used. Entries that are only
4021 referenced by other constants are also marked as used. Emit
4022 deferred strings that are used. */
4024 static void
4025 mark_constants (rtx_insn *insn)
4027 if (!INSN_P (insn))
4028 return;
4030 /* Insns may appear inside a SEQUENCE. Only check the patterns of
4031 insns, not any notes that may be attached. We don't want to mark
4032 a constant just because it happens to appear in a REG_EQUIV note. */
4033 if (rtx_sequence *seq = dyn_cast <rtx_sequence *> (PATTERN (insn)))
4035 int i, n = seq->len ();
4036 for (i = 0; i < n; ++i)
4038 rtx subinsn = seq->element (i);
4039 if (INSN_P (subinsn))
4040 mark_constants_in_pattern (subinsn);
4043 else
4044 mark_constants_in_pattern (insn);
4047 /* Look through the instructions for this function, and mark all the
4048 entries in POOL which are actually being used. Emit deferred constants
4049 which have indeed been used. */
4051 static void
4052 mark_constant_pool (void)
4054 rtx_insn *insn;
4056 if (!crtl->uses_const_pool && n_deferred_constants == 0)
4057 return;
4059 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4060 mark_constants (insn);
4063 /* Write all the constants in POOL. */
4065 static void
4066 output_constant_pool_contents (struct rtx_constant_pool *pool)
4068 struct constant_descriptor_rtx *desc;
4070 for (desc = pool->first; desc ; desc = desc->next)
4071 if (desc->mark)
4073 /* If the constant is part of an object_block, make sure that
4074 the constant has been positioned within its block, but do not
4075 write out its definition yet. output_object_blocks will do
4076 that later. */
4077 if (SYMBOL_REF_HAS_BLOCK_INFO_P (desc->sym)
4078 && SYMBOL_REF_BLOCK (desc->sym))
4079 place_block_symbol (desc->sym);
4080 else
4082 switch_to_section (targetm.asm_out.select_rtx_section
4083 (desc->mode, desc->constant, desc->align));
4084 output_constant_pool_1 (desc, desc->align);
4089 /* Mark all constants that are used in the current function, then write
4090 out the function's private constant pool. */
4092 static void
4093 output_constant_pool (const char *fnname ATTRIBUTE_UNUSED,
4094 tree fndecl ATTRIBUTE_UNUSED)
4096 struct rtx_constant_pool *pool = crtl->varasm.pool;
4098 /* It is possible for gcc to call force_const_mem and then to later
4099 discard the instructions which refer to the constant. In such a
4100 case we do not need to output the constant. */
4101 mark_constant_pool ();
4103 #ifdef ASM_OUTPUT_POOL_PROLOGUE
4104 ASM_OUTPUT_POOL_PROLOGUE (asm_out_file, fnname, fndecl, pool->offset);
4105 #endif
4107 output_constant_pool_contents (pool);
4109 #ifdef ASM_OUTPUT_POOL_EPILOGUE
4110 ASM_OUTPUT_POOL_EPILOGUE (asm_out_file, fnname, fndecl, pool->offset);
4111 #endif
4114 /* Write the contents of the shared constant pool. */
4116 void
4117 output_shared_constant_pool (void)
4119 output_constant_pool_contents (shared_constant_pool);
4122 /* Determine what kind of relocations EXP may need. */
4125 compute_reloc_for_constant (tree exp)
4127 int reloc = 0, reloc2;
4128 tree tem;
4130 switch (TREE_CODE (exp))
4132 case ADDR_EXPR:
4133 case FDESC_EXPR:
4134 /* Go inside any operations that get_inner_reference can handle and see
4135 if what's inside is a constant: no need to do anything here for
4136 addresses of variables or functions. */
4137 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
4138 tem = TREE_OPERAND (tem, 0))
4141 if (TREE_CODE (tem) == MEM_REF
4142 && TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR)
4144 reloc = compute_reloc_for_constant (TREE_OPERAND (tem, 0));
4145 break;
4148 if (!targetm.binds_local_p (tem))
4149 reloc |= 2;
4150 else
4151 reloc |= 1;
4152 break;
4154 case PLUS_EXPR:
4155 case POINTER_PLUS_EXPR:
4156 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4157 reloc |= compute_reloc_for_constant (TREE_OPERAND (exp, 1));
4158 break;
4160 case MINUS_EXPR:
4161 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4162 reloc2 = compute_reloc_for_constant (TREE_OPERAND (exp, 1));
4163 /* The difference of two local labels is computable at link time. */
4164 if (reloc == 1 && reloc2 == 1)
4165 reloc = 0;
4166 else
4167 reloc |= reloc2;
4168 break;
4170 CASE_CONVERT:
4171 case VIEW_CONVERT_EXPR:
4172 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4173 break;
4175 case CONSTRUCTOR:
4177 unsigned HOST_WIDE_INT idx;
4178 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
4179 if (tem != 0)
4180 reloc |= compute_reloc_for_constant (tem);
4182 break;
4184 default:
4185 break;
4187 return reloc;
4190 /* Find all the constants whose addresses are referenced inside of EXP,
4191 and make sure assembler code with a label has been output for each one.
4192 Indicate whether an ADDR_EXPR has been encountered. */
4194 static void
4195 output_addressed_constants (tree exp)
4197 tree tem;
4199 switch (TREE_CODE (exp))
4201 case ADDR_EXPR:
4202 case FDESC_EXPR:
4203 /* Go inside any operations that get_inner_reference can handle and see
4204 if what's inside is a constant: no need to do anything here for
4205 addresses of variables or functions. */
4206 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
4207 tem = TREE_OPERAND (tem, 0))
4210 /* If we have an initialized CONST_DECL, retrieve the initializer. */
4211 if (TREE_CODE (tem) == CONST_DECL && DECL_INITIAL (tem))
4212 tem = DECL_INITIAL (tem);
4214 if (CONSTANT_CLASS_P (tem) || TREE_CODE (tem) == CONSTRUCTOR)
4215 output_constant_def (tem, 0);
4217 if (TREE_CODE (tem) == MEM_REF)
4218 output_addressed_constants (TREE_OPERAND (tem, 0));
4219 break;
4221 case PLUS_EXPR:
4222 case POINTER_PLUS_EXPR:
4223 case MINUS_EXPR:
4224 output_addressed_constants (TREE_OPERAND (exp, 1));
4225 /* Fall through. */
4227 CASE_CONVERT:
4228 case VIEW_CONVERT_EXPR:
4229 output_addressed_constants (TREE_OPERAND (exp, 0));
4230 break;
4232 case CONSTRUCTOR:
4234 unsigned HOST_WIDE_INT idx;
4235 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
4236 if (tem != 0)
4237 output_addressed_constants (tem);
4239 break;
4241 default:
4242 break;
4246 /* Whether a constructor CTOR is a valid static constant initializer if all
4247 its elements are. This used to be internal to initializer_constant_valid_p
4248 and has been exposed to let other functions like categorize_ctor_elements
4249 evaluate the property while walking a constructor for other purposes. */
4251 bool
4252 constructor_static_from_elts_p (const_tree ctor)
4254 return (TREE_CONSTANT (ctor)
4255 && (TREE_CODE (TREE_TYPE (ctor)) == UNION_TYPE
4256 || TREE_CODE (TREE_TYPE (ctor)) == RECORD_TYPE
4257 || TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE));
4260 static tree initializer_constant_valid_p_1 (tree value, tree endtype,
4261 tree *cache);
4263 /* A subroutine of initializer_constant_valid_p. VALUE is a MINUS_EXPR,
4264 PLUS_EXPR or POINTER_PLUS_EXPR. This looks for cases of VALUE
4265 which are valid when ENDTYPE is an integer of any size; in
4266 particular, this does not accept a pointer minus a constant. This
4267 returns null_pointer_node if the VALUE is an absolute constant
4268 which can be used to initialize a static variable. Otherwise it
4269 returns NULL. */
4271 static tree
4272 narrowing_initializer_constant_valid_p (tree value, tree endtype, tree *cache)
4274 tree op0, op1;
4276 if (!INTEGRAL_TYPE_P (endtype))
4277 return NULL_TREE;
4279 op0 = TREE_OPERAND (value, 0);
4280 op1 = TREE_OPERAND (value, 1);
4282 /* Like STRIP_NOPS except allow the operand mode to widen. This
4283 works around a feature of fold that simplifies (int)(p1 - p2) to
4284 ((int)p1 - (int)p2) under the theory that the narrower operation
4285 is cheaper. */
4287 while (CONVERT_EXPR_P (op0)
4288 || TREE_CODE (op0) == NON_LVALUE_EXPR)
4290 tree inner = TREE_OPERAND (op0, 0);
4291 if (inner == error_mark_node
4292 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4293 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))
4294 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
4295 break;
4296 op0 = inner;
4299 while (CONVERT_EXPR_P (op1)
4300 || TREE_CODE (op1) == NON_LVALUE_EXPR)
4302 tree inner = TREE_OPERAND (op1, 0);
4303 if (inner == error_mark_node
4304 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4305 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op1)))
4306 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
4307 break;
4308 op1 = inner;
4311 op0 = initializer_constant_valid_p_1 (op0, endtype, cache);
4312 if (!op0)
4313 return NULL_TREE;
4315 op1 = initializer_constant_valid_p_1 (op1, endtype,
4316 cache ? cache + 2 : NULL);
4317 /* Both initializers must be known. */
4318 if (op1)
4320 if (op0 == op1
4321 && (op0 == null_pointer_node
4322 || TREE_CODE (value) == MINUS_EXPR))
4323 return null_pointer_node;
4325 /* Support differences between labels. */
4326 if (TREE_CODE (op0) == LABEL_DECL
4327 && TREE_CODE (op1) == LABEL_DECL)
4328 return null_pointer_node;
4330 if (TREE_CODE (op0) == STRING_CST
4331 && TREE_CODE (op1) == STRING_CST
4332 && operand_equal_p (op0, op1, 1))
4333 return null_pointer_node;
4336 return NULL_TREE;
4339 /* Helper function of initializer_constant_valid_p.
4340 Return nonzero if VALUE is a valid constant-valued expression
4341 for use in initializing a static variable; one that can be an
4342 element of a "constant" initializer.
4344 Return null_pointer_node if the value is absolute;
4345 if it is relocatable, return the variable that determines the relocation.
4346 We assume that VALUE has been folded as much as possible;
4347 therefore, we do not need to check for such things as
4348 arithmetic-combinations of integers.
4350 Use CACHE (pointer to 2 tree values) for caching if non-NULL. */
4352 static tree
4353 initializer_constant_valid_p_1 (tree value, tree endtype, tree *cache)
4355 tree ret;
4357 switch (TREE_CODE (value))
4359 case CONSTRUCTOR:
4360 if (constructor_static_from_elts_p (value))
4362 unsigned HOST_WIDE_INT idx;
4363 tree elt;
4364 bool absolute = true;
4366 if (cache && cache[0] == value)
4367 return cache[1];
4368 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
4370 tree reloc;
4371 reloc = initializer_constant_valid_p_1 (elt, TREE_TYPE (elt),
4372 NULL);
4373 if (!reloc
4374 /* An absolute value is required with reverse SSO. */
4375 || (reloc != null_pointer_node
4376 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (value))
4377 && !AGGREGATE_TYPE_P (TREE_TYPE (elt))))
4379 if (cache)
4381 cache[0] = value;
4382 cache[1] = NULL_TREE;
4384 return NULL_TREE;
4386 if (reloc != null_pointer_node)
4387 absolute = false;
4389 /* For a non-absolute relocation, there is no single
4390 variable that can be "the variable that determines the
4391 relocation." */
4392 if (cache)
4394 cache[0] = value;
4395 cache[1] = absolute ? null_pointer_node : error_mark_node;
4397 return absolute ? null_pointer_node : error_mark_node;
4400 return TREE_STATIC (value) ? null_pointer_node : NULL_TREE;
4402 case INTEGER_CST:
4403 case VECTOR_CST:
4404 case REAL_CST:
4405 case FIXED_CST:
4406 case STRING_CST:
4407 case COMPLEX_CST:
4408 return null_pointer_node;
4410 case ADDR_EXPR:
4411 case FDESC_EXPR:
4413 tree op0 = staticp (TREE_OPERAND (value, 0));
4414 if (op0)
4416 /* "&(*a).f" is like unto pointer arithmetic. If "a" turns out
4417 to be a constant, this is old-skool offsetof-like nonsense. */
4418 if (TREE_CODE (op0) == INDIRECT_REF
4419 && TREE_CONSTANT (TREE_OPERAND (op0, 0)))
4420 return null_pointer_node;
4421 /* Taking the address of a nested function involves a trampoline,
4422 unless we don't need or want one. */
4423 if (TREE_CODE (op0) == FUNCTION_DECL
4424 && DECL_STATIC_CHAIN (op0)
4425 && !TREE_NO_TRAMPOLINE (value))
4426 return NULL_TREE;
4427 /* "&{...}" requires a temporary to hold the constructed
4428 object. */
4429 if (TREE_CODE (op0) == CONSTRUCTOR)
4430 return NULL_TREE;
4432 return op0;
4435 case NON_LVALUE_EXPR:
4436 return initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4437 endtype, cache);
4439 case VIEW_CONVERT_EXPR:
4441 tree src = TREE_OPERAND (value, 0);
4442 tree src_type = TREE_TYPE (src);
4443 tree dest_type = TREE_TYPE (value);
4445 /* Allow view-conversions from aggregate to non-aggregate type only
4446 if the bit pattern is fully preserved afterwards; otherwise, the
4447 RTL expander won't be able to apply a subsequent transformation
4448 to the underlying constructor. */
4449 if (AGGREGATE_TYPE_P (src_type) && !AGGREGATE_TYPE_P (dest_type))
4451 if (TYPE_MODE (endtype) == TYPE_MODE (dest_type))
4452 return initializer_constant_valid_p_1 (src, endtype, cache);
4453 else
4454 return NULL_TREE;
4457 /* Allow all other kinds of view-conversion. */
4458 return initializer_constant_valid_p_1 (src, endtype, cache);
4461 CASE_CONVERT:
4463 tree src = TREE_OPERAND (value, 0);
4464 tree src_type = TREE_TYPE (src);
4465 tree dest_type = TREE_TYPE (value);
4467 /* Allow conversions between pointer types, floating-point
4468 types, and offset types. */
4469 if ((POINTER_TYPE_P (dest_type) && POINTER_TYPE_P (src_type))
4470 || (FLOAT_TYPE_P (dest_type) && FLOAT_TYPE_P (src_type))
4471 || (TREE_CODE (dest_type) == OFFSET_TYPE
4472 && TREE_CODE (src_type) == OFFSET_TYPE))
4473 return initializer_constant_valid_p_1 (src, endtype, cache);
4475 /* Allow length-preserving conversions between integer types. */
4476 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type)
4477 && (TYPE_PRECISION (dest_type) == TYPE_PRECISION (src_type)))
4478 return initializer_constant_valid_p_1 (src, endtype, cache);
4480 /* Allow conversions between other integer types only if
4481 explicit value. */
4482 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type))
4484 tree inner = initializer_constant_valid_p_1 (src, endtype, cache);
4485 if (inner == null_pointer_node)
4486 return null_pointer_node;
4487 break;
4490 /* Allow (int) &foo provided int is as wide as a pointer. */
4491 if (INTEGRAL_TYPE_P (dest_type) && POINTER_TYPE_P (src_type)
4492 && (TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type)))
4493 return initializer_constant_valid_p_1 (src, endtype, cache);
4495 /* Likewise conversions from int to pointers, but also allow
4496 conversions from 0. */
4497 if ((POINTER_TYPE_P (dest_type)
4498 || TREE_CODE (dest_type) == OFFSET_TYPE)
4499 && INTEGRAL_TYPE_P (src_type))
4501 if (TREE_CODE (src) == INTEGER_CST
4502 && TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type))
4503 return null_pointer_node;
4504 if (integer_zerop (src))
4505 return null_pointer_node;
4506 else if (TYPE_PRECISION (dest_type) <= TYPE_PRECISION (src_type))
4507 return initializer_constant_valid_p_1 (src, endtype, cache);
4510 /* Allow conversions to struct or union types if the value
4511 inside is okay. */
4512 if (TREE_CODE (dest_type) == RECORD_TYPE
4513 || TREE_CODE (dest_type) == UNION_TYPE)
4514 return initializer_constant_valid_p_1 (src, endtype, cache);
4516 break;
4518 case POINTER_PLUS_EXPR:
4519 case PLUS_EXPR:
4520 /* Any valid floating-point constants will have been folded by now;
4521 with -frounding-math we hit this with addition of two constants. */
4522 if (TREE_CODE (endtype) == REAL_TYPE)
4523 return NULL_TREE;
4524 if (cache && cache[0] == value)
4525 return cache[1];
4526 if (! INTEGRAL_TYPE_P (endtype)
4527 || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
4529 tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
4530 tree valid0
4531 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4532 endtype, ncache);
4533 tree valid1
4534 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
4535 endtype, ncache + 2);
4536 /* If either term is absolute, use the other term's relocation. */
4537 if (valid0 == null_pointer_node)
4538 ret = valid1;
4539 else if (valid1 == null_pointer_node)
4540 ret = valid0;
4541 /* Support narrowing pointer differences. */
4542 else
4543 ret = narrowing_initializer_constant_valid_p (value, endtype,
4544 ncache);
4546 else
4547 /* Support narrowing pointer differences. */
4548 ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
4549 if (cache)
4551 cache[0] = value;
4552 cache[1] = ret;
4554 return ret;
4556 case MINUS_EXPR:
4557 if (TREE_CODE (endtype) == REAL_TYPE)
4558 return NULL_TREE;
4559 if (cache && cache[0] == value)
4560 return cache[1];
4561 if (! INTEGRAL_TYPE_P (endtype)
4562 || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
4564 tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
4565 tree valid0
4566 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4567 endtype, ncache);
4568 tree valid1
4569 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
4570 endtype, ncache + 2);
4571 /* Win if second argument is absolute. */
4572 if (valid1 == null_pointer_node)
4573 ret = valid0;
4574 /* Win if both arguments have the same relocation.
4575 Then the value is absolute. */
4576 else if (valid0 == valid1 && valid0 != 0)
4577 ret = null_pointer_node;
4578 /* Since GCC guarantees that string constants are unique in the
4579 generated code, a subtraction between two copies of the same
4580 constant string is absolute. */
4581 else if (valid0 && TREE_CODE (valid0) == STRING_CST
4582 && valid1 && TREE_CODE (valid1) == STRING_CST
4583 && operand_equal_p (valid0, valid1, 1))
4584 ret = null_pointer_node;
4585 /* Support narrowing differences. */
4586 else
4587 ret = narrowing_initializer_constant_valid_p (value, endtype,
4588 ncache);
4590 else
4591 /* Support narrowing differences. */
4592 ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
4593 if (cache)
4595 cache[0] = value;
4596 cache[1] = ret;
4598 return ret;
4600 default:
4601 break;
4604 return NULL_TREE;
4607 /* Return nonzero if VALUE is a valid constant-valued expression
4608 for use in initializing a static variable; one that can be an
4609 element of a "constant" initializer.
4611 Return null_pointer_node if the value is absolute;
4612 if it is relocatable, return the variable that determines the relocation.
4613 We assume that VALUE has been folded as much as possible;
4614 therefore, we do not need to check for such things as
4615 arithmetic-combinations of integers. */
4616 tree
4617 initializer_constant_valid_p (tree value, tree endtype)
4619 return initializer_constant_valid_p_1 (value, endtype, NULL);
4622 /* Return true if VALUE is a valid constant-valued expression
4623 for use in initializing a static bit-field; one that can be
4624 an element of a "constant" initializer. */
4626 bool
4627 initializer_constant_valid_for_bitfield_p (tree value)
4629 /* For bitfields we support integer constants or possibly nested aggregates
4630 of such. */
4631 switch (TREE_CODE (value))
4633 case CONSTRUCTOR:
4635 unsigned HOST_WIDE_INT idx;
4636 tree elt;
4638 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
4639 if (!initializer_constant_valid_for_bitfield_p (elt))
4640 return false;
4641 return true;
4644 case INTEGER_CST:
4645 case REAL_CST:
4646 return true;
4648 case VIEW_CONVERT_EXPR:
4649 case NON_LVALUE_EXPR:
4650 return
4651 initializer_constant_valid_for_bitfield_p (TREE_OPERAND (value, 0));
4653 default:
4654 break;
4657 return false;
4660 /* output_constructor outer state of relevance in recursive calls, typically
4661 for nested aggregate bitfields. */
4663 typedef struct {
4664 unsigned int bit_offset; /* current position in ... */
4665 int byte; /* ... the outer byte buffer. */
4666 } oc_outer_state;
4668 static unsigned HOST_WIDE_INT
4669 output_constructor (tree, unsigned HOST_WIDE_INT, unsigned int, bool,
4670 oc_outer_state *);
4672 /* Output assembler code for constant EXP, with no label.
4673 This includes the pseudo-op such as ".int" or ".byte", and a newline.
4674 Assumes output_addressed_constants has been done on EXP already.
4676 Generate at least SIZE bytes of assembler data, padding at the end
4677 with zeros if necessary. SIZE must always be specified. The returned
4678 value is the actual number of bytes of assembler data generated, which
4679 may be bigger than SIZE if the object contains a variable length field.
4681 SIZE is important for structure constructors,
4682 since trailing members may have been omitted from the constructor.
4683 It is also important for initialization of arrays from string constants
4684 since the full length of the string constant might not be wanted.
4685 It is also needed for initialization of unions, where the initializer's
4686 type is just one member, and that may not be as long as the union.
4688 There a case in which we would fail to output exactly SIZE bytes:
4689 for a structure constructor that wants to produce more than SIZE bytes.
4690 But such constructors will never be generated for any possible input.
4692 ALIGN is the alignment of the data in bits.
4694 If REVERSE is true, EXP is interpreted in reverse storage order wrt the
4695 target order. */
4697 static unsigned HOST_WIDE_INT
4698 output_constant (tree exp, unsigned HOST_WIDE_INT size, unsigned int align,
4699 bool reverse)
4701 enum tree_code code;
4702 unsigned HOST_WIDE_INT thissize;
4703 rtx cst;
4705 if (size == 0 || flag_syntax_only)
4706 return size;
4708 /* See if we're trying to initialize a pointer in a non-default mode
4709 to the address of some declaration somewhere. If the target says
4710 the mode is valid for pointers, assume the target has a way of
4711 resolving it. */
4712 if (TREE_CODE (exp) == NOP_EXPR
4713 && POINTER_TYPE_P (TREE_TYPE (exp))
4714 && targetm.addr_space.valid_pointer_mode
4715 (TYPE_MODE (TREE_TYPE (exp)),
4716 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
4718 tree saved_type = TREE_TYPE (exp);
4720 /* Peel off any intermediate conversions-to-pointer for valid
4721 pointer modes. */
4722 while (TREE_CODE (exp) == NOP_EXPR
4723 && POINTER_TYPE_P (TREE_TYPE (exp))
4724 && targetm.addr_space.valid_pointer_mode
4725 (TYPE_MODE (TREE_TYPE (exp)),
4726 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
4727 exp = TREE_OPERAND (exp, 0);
4729 /* If what we're left with is the address of something, we can
4730 convert the address to the final type and output it that
4731 way. */
4732 if (TREE_CODE (exp) == ADDR_EXPR)
4733 exp = build1 (ADDR_EXPR, saved_type, TREE_OPERAND (exp, 0));
4734 /* Likewise for constant ints. */
4735 else if (TREE_CODE (exp) == INTEGER_CST)
4736 exp = wide_int_to_tree (saved_type, exp);
4740 /* Eliminate any conversions since we'll be outputting the underlying
4741 constant. */
4742 while (CONVERT_EXPR_P (exp)
4743 || TREE_CODE (exp) == NON_LVALUE_EXPR
4744 || TREE_CODE (exp) == VIEW_CONVERT_EXPR)
4746 HOST_WIDE_INT type_size = int_size_in_bytes (TREE_TYPE (exp));
4747 HOST_WIDE_INT op_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (exp, 0)));
4749 /* Make sure eliminating the conversion is really a no-op, except with
4750 VIEW_CONVERT_EXPRs to allow for wild Ada unchecked conversions and
4751 union types to allow for Ada unchecked unions. */
4752 if (type_size > op_size
4753 && TREE_CODE (exp) != VIEW_CONVERT_EXPR
4754 && TREE_CODE (TREE_TYPE (exp)) != UNION_TYPE)
4755 /* Keep the conversion. */
4756 break;
4757 else
4758 exp = TREE_OPERAND (exp, 0);
4761 code = TREE_CODE (TREE_TYPE (exp));
4762 thissize = int_size_in_bytes (TREE_TYPE (exp));
4764 /* Allow a constructor with no elements for any data type.
4765 This means to fill the space with zeros. */
4766 if (TREE_CODE (exp) == CONSTRUCTOR
4767 && vec_safe_is_empty (CONSTRUCTOR_ELTS (exp)))
4769 assemble_zeros (size);
4770 return size;
4773 if (TREE_CODE (exp) == FDESC_EXPR)
4775 #ifdef ASM_OUTPUT_FDESC
4776 HOST_WIDE_INT part = tree_to_shwi (TREE_OPERAND (exp, 1));
4777 tree decl = TREE_OPERAND (exp, 0);
4778 ASM_OUTPUT_FDESC (asm_out_file, decl, part);
4779 #else
4780 gcc_unreachable ();
4781 #endif
4782 return size;
4785 /* Now output the underlying data. If we've handling the padding, return.
4786 Otherwise, break and ensure SIZE is the size written. */
4787 switch (code)
4789 case BOOLEAN_TYPE:
4790 case INTEGER_TYPE:
4791 case ENUMERAL_TYPE:
4792 case POINTER_TYPE:
4793 case REFERENCE_TYPE:
4794 case OFFSET_TYPE:
4795 case FIXED_POINT_TYPE:
4796 case POINTER_BOUNDS_TYPE:
4797 case NULLPTR_TYPE:
4798 cst = expand_expr (exp, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
4799 if (reverse)
4800 cst = flip_storage_order (TYPE_MODE (TREE_TYPE (exp)), cst);
4801 if (!assemble_integer (cst, MIN (size, thissize), align, 0))
4802 error ("initializer for integer/fixed-point value is too complicated");
4803 break;
4805 case REAL_TYPE:
4806 if (TREE_CODE (exp) != REAL_CST)
4807 error ("initializer for floating value is not a floating constant");
4808 else
4810 assemble_real (TREE_REAL_CST (exp), TYPE_MODE (TREE_TYPE (exp)),
4811 align, reverse);
4813 break;
4815 case COMPLEX_TYPE:
4816 output_constant (TREE_REALPART (exp), thissize / 2, align, reverse);
4817 output_constant (TREE_IMAGPART (exp), thissize / 2,
4818 min_align (align, BITS_PER_UNIT * (thissize / 2)),
4819 reverse);
4820 break;
4822 case ARRAY_TYPE:
4823 case VECTOR_TYPE:
4824 switch (TREE_CODE (exp))
4826 case CONSTRUCTOR:
4827 return output_constructor (exp, size, align, reverse, NULL);
4828 case STRING_CST:
4829 thissize
4830 = MIN ((unsigned HOST_WIDE_INT)TREE_STRING_LENGTH (exp), size);
4831 assemble_string (TREE_STRING_POINTER (exp), thissize);
4832 break;
4833 case VECTOR_CST:
4835 machine_mode inner = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
4836 unsigned int nalign = MIN (align, GET_MODE_ALIGNMENT (inner));
4837 int elt_size = GET_MODE_SIZE (inner);
4838 output_constant (VECTOR_CST_ELT (exp, 0), elt_size, align,
4839 reverse);
4840 thissize = elt_size;
4841 for (unsigned int i = 1; i < VECTOR_CST_NELTS (exp); i++)
4843 output_constant (VECTOR_CST_ELT (exp, i), elt_size, nalign,
4844 reverse);
4845 thissize += elt_size;
4847 break;
4849 default:
4850 gcc_unreachable ();
4852 break;
4854 case RECORD_TYPE:
4855 case UNION_TYPE:
4856 gcc_assert (TREE_CODE (exp) == CONSTRUCTOR);
4857 return output_constructor (exp, size, align, reverse, NULL);
4859 case ERROR_MARK:
4860 return 0;
4862 default:
4863 gcc_unreachable ();
4866 if (size > thissize)
4867 assemble_zeros (size - thissize);
4869 return size;
4872 /* Subroutine of output_constructor, used for computing the size of
4873 arrays of unspecified length. VAL must be a CONSTRUCTOR of an array
4874 type with an unspecified upper bound. */
4876 static unsigned HOST_WIDE_INT
4877 array_size_for_constructor (tree val)
4879 tree max_index;
4880 unsigned HOST_WIDE_INT cnt;
4881 tree index, value, tmp;
4882 offset_int i;
4884 /* This code used to attempt to handle string constants that are not
4885 arrays of single-bytes, but nothing else does, so there's no point in
4886 doing it here. */
4887 if (TREE_CODE (val) == STRING_CST)
4888 return TREE_STRING_LENGTH (val);
4890 max_index = NULL_TREE;
4891 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (val), cnt, index, value)
4893 if (TREE_CODE (index) == RANGE_EXPR)
4894 index = TREE_OPERAND (index, 1);
4895 if (max_index == NULL_TREE || tree_int_cst_lt (max_index, index))
4896 max_index = index;
4899 if (max_index == NULL_TREE)
4900 return 0;
4902 /* Compute the total number of array elements. */
4903 tmp = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (val)));
4904 i = wi::to_offset (max_index) - wi::to_offset (tmp) + 1;
4906 /* Multiply by the array element unit size to find number of bytes. */
4907 i *= wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (val))));
4909 gcc_assert (wi::fits_uhwi_p (i));
4910 return i.to_uhwi ();
4913 /* Other datastructures + helpers for output_constructor. */
4915 /* output_constructor local state to support interaction with helpers. */
4917 typedef struct {
4919 /* Received arguments. */
4920 tree exp; /* Constructor expression. */
4921 tree type; /* Type of constructor expression. */
4922 unsigned HOST_WIDE_INT size; /* # bytes to output - pad if necessary. */
4923 unsigned int align; /* Known initial alignment. */
4924 tree min_index; /* Lower bound if specified for an array. */
4926 /* Output processing state. */
4927 HOST_WIDE_INT total_bytes; /* # bytes output so far / current position. */
4928 int byte; /* Part of a bitfield byte yet to be output. */
4929 int last_relative_index; /* Implicit or explicit index of the last
4930 array element output within a bitfield. */
4931 bool byte_buffer_in_use; /* Whether BYTE is in use. */
4932 bool reverse; /* Whether reverse storage order is in use. */
4934 /* Current element. */
4935 tree field; /* Current field decl in a record. */
4936 tree val; /* Current element value. */
4937 tree index; /* Current element index. */
4939 } oc_local_state;
4941 /* Helper for output_constructor. From the current LOCAL state, output a
4942 RANGE_EXPR element. */
4944 static void
4945 output_constructor_array_range (oc_local_state *local)
4947 unsigned HOST_WIDE_INT fieldsize
4948 = int_size_in_bytes (TREE_TYPE (local->type));
4950 HOST_WIDE_INT lo_index
4951 = tree_to_shwi (TREE_OPERAND (local->index, 0));
4952 HOST_WIDE_INT hi_index
4953 = tree_to_shwi (TREE_OPERAND (local->index, 1));
4954 HOST_WIDE_INT index;
4956 unsigned int align2
4957 = min_align (local->align, fieldsize * BITS_PER_UNIT);
4959 for (index = lo_index; index <= hi_index; index++)
4961 /* Output the element's initial value. */
4962 if (local->val == NULL_TREE)
4963 assemble_zeros (fieldsize);
4964 else
4965 fieldsize
4966 = output_constant (local->val, fieldsize, align2, local->reverse);
4968 /* Count its size. */
4969 local->total_bytes += fieldsize;
4973 /* Helper for output_constructor. From the current LOCAL state, output a
4974 field element that is not true bitfield or part of an outer one. */
4976 static void
4977 output_constructor_regular_field (oc_local_state *local)
4979 /* Field size and position. Since this structure is static, we know the
4980 positions are constant. */
4981 unsigned HOST_WIDE_INT fieldsize;
4982 HOST_WIDE_INT fieldpos;
4984 unsigned int align2;
4986 if (local->index != NULL_TREE)
4988 /* Perform the index calculation in modulo arithmetic but
4989 sign-extend the result because Ada has negative DECL_FIELD_OFFSETs
4990 but we are using an unsigned sizetype. */
4991 unsigned prec = TYPE_PRECISION (sizetype);
4992 offset_int idx = wi::sext (wi::to_offset (local->index)
4993 - wi::to_offset (local->min_index), prec);
4994 fieldpos = (idx * wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (local->val))))
4995 .to_short_addr ();
4997 else if (local->field != NULL_TREE)
4998 fieldpos = int_byte_position (local->field);
4999 else
5000 fieldpos = 0;
5002 /* Output any buffered-up bit-fields preceding this element. */
5003 if (local->byte_buffer_in_use)
5005 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5006 local->total_bytes++;
5007 local->byte_buffer_in_use = false;
5010 /* Advance to offset of this element.
5011 Note no alignment needed in an array, since that is guaranteed
5012 if each element has the proper size. */
5013 if ((local->field != NULL_TREE || local->index != NULL_TREE)
5014 && fieldpos > local->total_bytes)
5016 assemble_zeros (fieldpos - local->total_bytes);
5017 local->total_bytes = fieldpos;
5020 /* Find the alignment of this element. */
5021 align2 = min_align (local->align, BITS_PER_UNIT * fieldpos);
5023 /* Determine size this element should occupy. */
5024 if (local->field)
5026 fieldsize = 0;
5028 /* If this is an array with an unspecified upper bound,
5029 the initializer determines the size. */
5030 /* ??? This ought to only checked if DECL_SIZE_UNIT is NULL,
5031 but we cannot do this until the deprecated support for
5032 initializing zero-length array members is removed. */
5033 if (TREE_CODE (TREE_TYPE (local->field)) == ARRAY_TYPE
5034 && TYPE_DOMAIN (TREE_TYPE (local->field))
5035 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (local->field))))
5037 fieldsize = array_size_for_constructor (local->val);
5038 /* Given a non-empty initialization, this field had
5039 better be last. */
5040 gcc_assert (!fieldsize || !DECL_CHAIN (local->field));
5042 else
5043 fieldsize = tree_to_uhwi (DECL_SIZE_UNIT (local->field));
5045 else
5046 fieldsize = int_size_in_bytes (TREE_TYPE (local->type));
5048 /* Output the element's initial value. */
5049 if (local->val == NULL_TREE)
5050 assemble_zeros (fieldsize);
5051 else
5052 fieldsize
5053 = output_constant (local->val, fieldsize, align2, local->reverse);
5055 /* Count its size. */
5056 local->total_bytes += fieldsize;
5059 /* Helper for output_constructor. From the LOCAL state, output an element
5060 that is a true bitfield or part of an outer one. BIT_OFFSET is the offset
5061 from the start of a possibly ongoing outer byte buffer. */
5063 static void
5064 output_constructor_bitfield (oc_local_state *local, unsigned int bit_offset)
5066 /* Bit size of this element. */
5067 HOST_WIDE_INT ebitsize
5068 = (local->field
5069 ? tree_to_uhwi (DECL_SIZE (local->field))
5070 : tree_to_uhwi (TYPE_SIZE (TREE_TYPE (local->type))));
5072 /* Relative index of this element if this is an array component. */
5073 HOST_WIDE_INT relative_index
5074 = (!local->field
5075 ? (local->index
5076 ? (tree_to_shwi (local->index)
5077 - tree_to_shwi (local->min_index))
5078 : local->last_relative_index + 1)
5079 : 0);
5081 /* Bit position of this element from the start of the containing
5082 constructor. */
5083 HOST_WIDE_INT constructor_relative_ebitpos
5084 = (local->field
5085 ? int_bit_position (local->field)
5086 : ebitsize * relative_index);
5088 /* Bit position of this element from the start of a possibly ongoing
5089 outer byte buffer. */
5090 HOST_WIDE_INT byte_relative_ebitpos
5091 = bit_offset + constructor_relative_ebitpos;
5093 /* From the start of a possibly ongoing outer byte buffer, offsets to
5094 the first bit of this element and to the first bit past the end of
5095 this element. */
5096 HOST_WIDE_INT next_offset = byte_relative_ebitpos;
5097 HOST_WIDE_INT end_offset = byte_relative_ebitpos + ebitsize;
5099 local->last_relative_index = relative_index;
5101 if (local->val == NULL_TREE)
5102 local->val = integer_zero_node;
5104 while (TREE_CODE (local->val) == VIEW_CONVERT_EXPR
5105 || TREE_CODE (local->val) == NON_LVALUE_EXPR)
5106 local->val = TREE_OPERAND (local->val, 0);
5108 if (TREE_CODE (local->val) != INTEGER_CST
5109 && TREE_CODE (local->val) != CONSTRUCTOR)
5111 error ("invalid initial value for member %qE", DECL_NAME (local->field));
5112 return;
5115 /* If this field does not start in this (or next) byte, skip some bytes. */
5116 if (next_offset / BITS_PER_UNIT != local->total_bytes)
5118 /* Output remnant of any bit field in previous bytes. */
5119 if (local->byte_buffer_in_use)
5121 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5122 local->total_bytes++;
5123 local->byte_buffer_in_use = false;
5126 /* If still not at proper byte, advance to there. */
5127 if (next_offset / BITS_PER_UNIT != local->total_bytes)
5129 gcc_assert (next_offset / BITS_PER_UNIT >= local->total_bytes);
5130 assemble_zeros (next_offset / BITS_PER_UNIT - local->total_bytes);
5131 local->total_bytes = next_offset / BITS_PER_UNIT;
5135 /* Set up the buffer if necessary. */
5136 if (!local->byte_buffer_in_use)
5138 local->byte = 0;
5139 if (ebitsize > 0)
5140 local->byte_buffer_in_use = true;
5143 /* If this is nested constructor, recurse passing the bit offset and the
5144 pending data, then retrieve the new pending data afterwards. */
5145 if (TREE_CODE (local->val) == CONSTRUCTOR)
5147 oc_outer_state temp_state;
5148 temp_state.bit_offset = next_offset % BITS_PER_UNIT;
5149 temp_state.byte = local->byte;
5150 local->total_bytes
5151 += output_constructor (local->val, 0, 0, local->reverse, &temp_state);
5152 local->byte = temp_state.byte;
5153 return;
5156 /* Otherwise, we must split the element into pieces that fall within
5157 separate bytes, and combine each byte with previous or following
5158 bit-fields. */
5159 while (next_offset < end_offset)
5161 int this_time;
5162 int shift;
5163 HOST_WIDE_INT value;
5164 HOST_WIDE_INT next_byte = next_offset / BITS_PER_UNIT;
5165 HOST_WIDE_INT next_bit = next_offset % BITS_PER_UNIT;
5167 /* Advance from byte to byte within this element when necessary. */
5168 while (next_byte != local->total_bytes)
5170 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5171 local->total_bytes++;
5172 local->byte = 0;
5175 /* Number of bits we can process at once (all part of the same byte). */
5176 this_time = MIN (end_offset - next_offset, BITS_PER_UNIT - next_bit);
5177 if (local->reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5179 /* On big-endian machine, take the most significant bits (of the
5180 bits that are significant) first and put them into bytes from
5181 the most significant end. */
5182 shift = end_offset - next_offset - this_time;
5184 /* Don't try to take a bunch of bits that cross
5185 the word boundary in the INTEGER_CST. We can
5186 only select bits from one element. */
5187 if ((shift / HOST_BITS_PER_WIDE_INT)
5188 != ((shift + this_time - 1) / HOST_BITS_PER_WIDE_INT))
5190 const int end = shift + this_time - 1;
5191 shift = end & -HOST_BITS_PER_WIDE_INT;
5192 this_time = end - shift + 1;
5195 /* Now get the bits from the appropriate constant word. */
5196 value = TREE_INT_CST_ELT (local->val, shift / HOST_BITS_PER_WIDE_INT);
5197 shift = shift & (HOST_BITS_PER_WIDE_INT - 1);
5199 /* Get the result. This works only when:
5200 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
5201 local->byte |= (((value >> shift)
5202 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
5203 << (BITS_PER_UNIT - this_time - next_bit));
5205 else
5207 /* On little-endian machines, take the least significant bits of
5208 the value first and pack them starting at the least significant
5209 bits of the bytes. */
5210 shift = next_offset - byte_relative_ebitpos;
5212 /* Don't try to take a bunch of bits that cross
5213 the word boundary in the INTEGER_CST. We can
5214 only select bits from one element. */
5215 if ((shift / HOST_BITS_PER_WIDE_INT)
5216 != ((shift + this_time - 1) / HOST_BITS_PER_WIDE_INT))
5217 this_time
5218 = HOST_BITS_PER_WIDE_INT - (shift & (HOST_BITS_PER_WIDE_INT - 1));
5220 /* Now get the bits from the appropriate constant word. */
5221 value = TREE_INT_CST_ELT (local->val, shift / HOST_BITS_PER_WIDE_INT);
5222 shift = shift & (HOST_BITS_PER_WIDE_INT - 1);
5224 /* Get the result. This works only when:
5225 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
5226 local->byte |= (((value >> shift)
5227 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
5228 << next_bit);
5231 next_offset += this_time;
5232 local->byte_buffer_in_use = true;
5236 /* Subroutine of output_constant, used for CONSTRUCTORs (aggregate constants).
5237 Generate at least SIZE bytes, padding if necessary. OUTER designates the
5238 caller output state of relevance in recursive invocations. */
5240 static unsigned HOST_WIDE_INT
5241 output_constructor (tree exp, unsigned HOST_WIDE_INT size, unsigned int align,
5242 bool reverse, oc_outer_state *outer)
5244 unsigned HOST_WIDE_INT cnt;
5245 constructor_elt *ce;
5246 oc_local_state local;
5248 /* Setup our local state to communicate with helpers. */
5249 local.exp = exp;
5250 local.type = TREE_TYPE (exp);
5251 local.size = size;
5252 local.align = align;
5253 if (TREE_CODE (local.type) == ARRAY_TYPE && TYPE_DOMAIN (local.type))
5254 local.min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (local.type));
5255 else
5256 local.min_index = NULL_TREE;
5258 local.total_bytes = 0;
5259 local.byte_buffer_in_use = outer != NULL;
5260 local.byte = outer ? outer->byte : 0;
5261 local.last_relative_index = -1;
5262 /* The storage order is specified for every aggregate type. */
5263 if (AGGREGATE_TYPE_P (local.type))
5264 local.reverse = TYPE_REVERSE_STORAGE_ORDER (local.type);
5265 else
5266 local.reverse = reverse;
5268 gcc_assert (HOST_BITS_PER_WIDE_INT >= BITS_PER_UNIT);
5270 /* As CE goes through the elements of the constant, FIELD goes through the
5271 structure fields if the constant is a structure. If the constant is a
5272 union, we override this by getting the field from the TREE_LIST element.
5273 But the constant could also be an array. Then FIELD is zero.
5275 There is always a maximum of one element in the chain LINK for unions
5276 (even if the initializer in a source program incorrectly contains
5277 more one). */
5279 if (TREE_CODE (local.type) == RECORD_TYPE)
5280 local.field = TYPE_FIELDS (local.type);
5281 else
5282 local.field = NULL_TREE;
5284 for (cnt = 0;
5285 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), cnt, &ce);
5286 cnt++, local.field = local.field ? DECL_CHAIN (local.field) : 0)
5288 local.val = ce->value;
5289 local.index = NULL_TREE;
5291 /* The element in a union constructor specifies the proper field
5292 or index. */
5293 if (RECORD_OR_UNION_TYPE_P (local.type) && ce->index != NULL_TREE)
5294 local.field = ce->index;
5296 else if (TREE_CODE (local.type) == ARRAY_TYPE)
5297 local.index = ce->index;
5299 if (local.field && flag_verbose_asm)
5300 fprintf (asm_out_file, "%s %s:\n",
5301 ASM_COMMENT_START,
5302 DECL_NAME (local.field)
5303 ? IDENTIFIER_POINTER (DECL_NAME (local.field))
5304 : "<anonymous>");
5306 /* Eliminate the marker that makes a cast not be an lvalue. */
5307 if (local.val != NULL_TREE)
5308 STRIP_NOPS (local.val);
5310 /* Output the current element, using the appropriate helper ... */
5312 /* For an array slice not part of an outer bitfield. */
5313 if (!outer
5314 && local.index != NULL_TREE
5315 && TREE_CODE (local.index) == RANGE_EXPR)
5316 output_constructor_array_range (&local);
5318 /* For a field that is neither a true bitfield nor part of an outer one,
5319 known to be at least byte aligned and multiple-of-bytes long. */
5320 else if (!outer
5321 && (local.field == NULL_TREE
5322 || !CONSTRUCTOR_BITFIELD_P (local.field)))
5323 output_constructor_regular_field (&local);
5325 /* For a true bitfield or part of an outer one. Only INTEGER_CSTs are
5326 supported for scalar fields, so we may need to convert first. */
5327 else
5329 if (TREE_CODE (local.val) == REAL_CST)
5330 local.val
5331 = fold_unary (VIEW_CONVERT_EXPR,
5332 build_nonstandard_integer_type
5333 (TYPE_PRECISION (TREE_TYPE (local.val)), 0),
5334 local.val);
5335 output_constructor_bitfield (&local, outer ? outer->bit_offset : 0);
5339 /* If we are not at toplevel, save the pending data for our caller.
5340 Otherwise output the pending data and padding zeros as needed. */
5341 if (outer)
5342 outer->byte = local.byte;
5343 else
5345 if (local.byte_buffer_in_use)
5347 assemble_integer (GEN_INT (local.byte), 1, BITS_PER_UNIT, 1);
5348 local.total_bytes++;
5351 if ((unsigned HOST_WIDE_INT)local.total_bytes < local.size)
5353 assemble_zeros (local.size - local.total_bytes);
5354 local.total_bytes = local.size;
5358 return local.total_bytes;
5361 /* Mark DECL as weak. */
5363 static void
5364 mark_weak (tree decl)
5366 if (DECL_WEAK (decl))
5367 return;
5369 struct symtab_node *n = symtab_node::get (decl);
5370 if (n && n->refuse_visibility_changes)
5371 error ("%+D declared weak after being used", decl);
5372 DECL_WEAK (decl) = 1;
5374 if (DECL_RTL_SET_P (decl)
5375 && MEM_P (DECL_RTL (decl))
5376 && XEXP (DECL_RTL (decl), 0)
5377 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == SYMBOL_REF)
5378 SYMBOL_REF_WEAK (XEXP (DECL_RTL (decl), 0)) = 1;
5381 /* Merge weak status between NEWDECL and OLDDECL. */
5383 void
5384 merge_weak (tree newdecl, tree olddecl)
5386 if (DECL_WEAK (newdecl) == DECL_WEAK (olddecl))
5388 if (DECL_WEAK (newdecl) && TARGET_SUPPORTS_WEAK)
5390 tree *pwd;
5391 /* We put the NEWDECL on the weak_decls list at some point
5392 and OLDDECL as well. Keep just OLDDECL on the list. */
5393 for (pwd = &weak_decls; *pwd; pwd = &TREE_CHAIN (*pwd))
5394 if (TREE_VALUE (*pwd) == newdecl)
5396 *pwd = TREE_CHAIN (*pwd);
5397 break;
5400 return;
5403 if (DECL_WEAK (newdecl))
5405 tree wd;
5407 /* NEWDECL is weak, but OLDDECL is not. */
5409 /* If we already output the OLDDECL, we're in trouble; we can't
5410 go back and make it weak. This should never happen in
5411 unit-at-a-time compilation. */
5412 gcc_assert (!TREE_ASM_WRITTEN (olddecl));
5414 /* If we've already generated rtl referencing OLDDECL, we may
5415 have done so in a way that will not function properly with
5416 a weak symbol. Again in unit-at-a-time this should be
5417 impossible. */
5418 gcc_assert (!TREE_USED (olddecl)
5419 || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (olddecl)));
5421 if (TARGET_SUPPORTS_WEAK)
5423 /* We put the NEWDECL on the weak_decls list at some point.
5424 Replace it with the OLDDECL. */
5425 for (wd = weak_decls; wd; wd = TREE_CHAIN (wd))
5426 if (TREE_VALUE (wd) == newdecl)
5428 TREE_VALUE (wd) = olddecl;
5429 break;
5431 /* We may not find the entry on the list. If NEWDECL is a
5432 weak alias, then we will have already called
5433 globalize_decl to remove the entry; in that case, we do
5434 not need to do anything. */
5437 /* Make the OLDDECL weak; it's OLDDECL that we'll be keeping. */
5438 mark_weak (olddecl);
5440 else
5441 /* OLDDECL was weak, but NEWDECL was not explicitly marked as
5442 weak. Just update NEWDECL to indicate that it's weak too. */
5443 mark_weak (newdecl);
5446 /* Declare DECL to be a weak symbol. */
5448 void
5449 declare_weak (tree decl)
5451 gcc_assert (TREE_CODE (decl) != FUNCTION_DECL || !TREE_ASM_WRITTEN (decl));
5452 if (! TREE_PUBLIC (decl))
5453 error ("weak declaration of %q+D must be public", decl);
5454 else if (!TARGET_SUPPORTS_WEAK)
5455 warning (0, "weak declaration of %q+D not supported", decl);
5457 mark_weak (decl);
5458 if (!lookup_attribute ("weak", DECL_ATTRIBUTES (decl)))
5459 DECL_ATTRIBUTES (decl)
5460 = tree_cons (get_identifier ("weak"), NULL, DECL_ATTRIBUTES (decl));
5463 static void
5464 weak_finish_1 (tree decl)
5466 #if defined (ASM_WEAKEN_DECL) || defined (ASM_WEAKEN_LABEL)
5467 const char *const name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5468 #endif
5470 if (! TREE_USED (decl))
5471 return;
5473 #ifdef ASM_WEAKEN_DECL
5474 ASM_WEAKEN_DECL (asm_out_file, decl, name, NULL);
5475 #else
5476 #ifdef ASM_WEAKEN_LABEL
5477 ASM_WEAKEN_LABEL (asm_out_file, name);
5478 #else
5479 #ifdef ASM_OUTPUT_WEAK_ALIAS
5481 static bool warn_once = 0;
5482 if (! warn_once)
5484 warning (0, "only weak aliases are supported in this configuration");
5485 warn_once = 1;
5487 return;
5489 #endif
5490 #endif
5491 #endif
5494 /* Fiven an assembly name, find the decl it is associated with. */
5495 static tree
5496 find_decl (tree target)
5498 symtab_node *node = symtab_node::get_for_asmname (target);
5499 if (node)
5500 return node->decl;
5501 return NULL_TREE;
5504 /* This TREE_LIST contains weakref targets. */
5506 static GTY(()) tree weakref_targets;
5508 /* Emit any pending weak declarations. */
5510 void
5511 weak_finish (void)
5513 tree t;
5515 for (t = weakref_targets; t; t = TREE_CHAIN (t))
5517 tree alias_decl = TREE_PURPOSE (t);
5518 tree target = ultimate_transparent_alias_target (&TREE_VALUE (t));
5520 if (! TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (alias_decl)))
5521 /* Remove alias_decl from the weak list, but leave entries for
5522 the target alone. */
5523 target = NULL_TREE;
5524 #ifndef ASM_OUTPUT_WEAKREF
5525 else if (! TREE_SYMBOL_REFERENCED (target))
5527 /* Use ASM_WEAKEN_LABEL only if ASM_WEAKEN_DECL is not
5528 defined, otherwise we and weak_finish_1 would use
5529 different macros. */
5530 # if defined ASM_WEAKEN_LABEL && ! defined ASM_WEAKEN_DECL
5531 ASM_WEAKEN_LABEL (asm_out_file, IDENTIFIER_POINTER (target));
5532 # else
5533 tree decl = find_decl (target);
5535 if (! decl)
5537 decl = build_decl (DECL_SOURCE_LOCATION (alias_decl),
5538 TREE_CODE (alias_decl), target,
5539 TREE_TYPE (alias_decl));
5541 DECL_EXTERNAL (decl) = 1;
5542 TREE_PUBLIC (decl) = 1;
5543 DECL_ARTIFICIAL (decl) = 1;
5544 TREE_NOTHROW (decl) = TREE_NOTHROW (alias_decl);
5545 TREE_USED (decl) = 1;
5548 weak_finish_1 (decl);
5549 # endif
5551 #endif
5554 tree *p;
5555 tree t2;
5557 /* Remove the alias and the target from the pending weak list
5558 so that we do not emit any .weak directives for the former,
5559 nor multiple .weak directives for the latter. */
5560 for (p = &weak_decls; (t2 = *p) ; )
5562 if (TREE_VALUE (t2) == alias_decl
5563 || target == DECL_ASSEMBLER_NAME (TREE_VALUE (t2)))
5564 *p = TREE_CHAIN (t2);
5565 else
5566 p = &TREE_CHAIN (t2);
5569 /* Remove other weakrefs to the same target, to speed things up. */
5570 for (p = &TREE_CHAIN (t); (t2 = *p) ; )
5572 if (target == ultimate_transparent_alias_target (&TREE_VALUE (t2)))
5573 *p = TREE_CHAIN (t2);
5574 else
5575 p = &TREE_CHAIN (t2);
5580 for (t = weak_decls; t; t = TREE_CHAIN (t))
5582 tree decl = TREE_VALUE (t);
5584 weak_finish_1 (decl);
5588 /* Emit the assembly bits to indicate that DECL is globally visible. */
5590 static void
5591 globalize_decl (tree decl)
5594 #if defined (ASM_WEAKEN_LABEL) || defined (ASM_WEAKEN_DECL)
5595 if (DECL_WEAK (decl))
5597 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
5598 tree *p, t;
5600 #ifdef ASM_WEAKEN_DECL
5601 ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
5602 #else
5603 ASM_WEAKEN_LABEL (asm_out_file, name);
5604 #endif
5606 /* Remove this function from the pending weak list so that
5607 we do not emit multiple .weak directives for it. */
5608 for (p = &weak_decls; (t = *p) ; )
5610 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
5611 *p = TREE_CHAIN (t);
5612 else
5613 p = &TREE_CHAIN (t);
5616 /* Remove weakrefs to the same target from the pending weakref
5617 list, for the same reason. */
5618 for (p = &weakref_targets; (t = *p) ; )
5620 if (DECL_ASSEMBLER_NAME (decl)
5621 == ultimate_transparent_alias_target (&TREE_VALUE (t)))
5622 *p = TREE_CHAIN (t);
5623 else
5624 p = &TREE_CHAIN (t);
5627 return;
5629 #endif
5631 targetm.asm_out.globalize_decl_name (asm_out_file, decl);
5634 vec<alias_pair, va_gc> *alias_pairs;
5636 /* Output the assembler code for a define (equate) using ASM_OUTPUT_DEF
5637 or ASM_OUTPUT_DEF_FROM_DECLS. The function defines the symbol whose
5638 tree node is DECL to have the value of the tree node TARGET. */
5640 void
5641 do_assemble_alias (tree decl, tree target)
5643 tree id;
5645 /* Emulated TLS had better not get this var. */
5646 gcc_assert (!(!targetm.have_tls
5647 && TREE_CODE (decl) == VAR_DECL
5648 && DECL_THREAD_LOCAL_P (decl)));
5650 if (TREE_ASM_WRITTEN (decl))
5651 return;
5653 id = DECL_ASSEMBLER_NAME (decl);
5654 ultimate_transparent_alias_target (&id);
5655 ultimate_transparent_alias_target (&target);
5657 /* We must force creation of DECL_RTL for debug info generation, even though
5658 we don't use it here. */
5659 make_decl_rtl (decl);
5661 TREE_ASM_WRITTEN (decl) = 1;
5662 TREE_ASM_WRITTEN (DECL_ASSEMBLER_NAME (decl)) = 1;
5663 TREE_ASM_WRITTEN (id) = 1;
5665 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
5667 if (!TREE_SYMBOL_REFERENCED (target))
5668 weakref_targets = tree_cons (decl, target, weakref_targets);
5670 #ifdef ASM_OUTPUT_WEAKREF
5671 ASM_OUTPUT_WEAKREF (asm_out_file, decl,
5672 IDENTIFIER_POINTER (id),
5673 IDENTIFIER_POINTER (target));
5674 #else
5675 if (!TARGET_SUPPORTS_WEAK)
5677 error_at (DECL_SOURCE_LOCATION (decl),
5678 "weakref is not supported in this configuration");
5679 return;
5681 #endif
5682 return;
5685 #ifdef ASM_OUTPUT_DEF
5686 tree orig_decl = decl;
5688 if (TREE_CODE (decl) == FUNCTION_DECL
5689 && cgraph_node::get (decl)->instrumentation_clone
5690 && cgraph_node::get (decl)->instrumented_version)
5691 orig_decl = cgraph_node::get (decl)->instrumented_version->decl;
5693 /* Make name accessible from other files, if appropriate. */
5695 if (TREE_PUBLIC (decl) || TREE_PUBLIC (orig_decl))
5697 globalize_decl (decl);
5698 maybe_assemble_visibility (decl);
5700 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
5702 #if defined (ASM_OUTPUT_TYPE_DIRECTIVE)
5703 if (targetm.has_ifunc_p ())
5704 ASM_OUTPUT_TYPE_DIRECTIVE
5705 (asm_out_file, IDENTIFIER_POINTER (id),
5706 IFUNC_ASM_TYPE);
5707 else
5708 #endif
5709 error_at (DECL_SOURCE_LOCATION (decl),
5710 "ifunc is not supported on this target");
5713 # ifdef ASM_OUTPUT_DEF_FROM_DECLS
5714 ASM_OUTPUT_DEF_FROM_DECLS (asm_out_file, decl, target);
5715 # else
5716 ASM_OUTPUT_DEF (asm_out_file,
5717 IDENTIFIER_POINTER (id),
5718 IDENTIFIER_POINTER (target));
5719 # endif
5720 #elif defined (ASM_OUTPUT_WEAK_ALIAS) || defined (ASM_WEAKEN_DECL)
5722 const char *name;
5723 tree *p, t;
5725 name = IDENTIFIER_POINTER (id);
5726 # ifdef ASM_WEAKEN_DECL
5727 ASM_WEAKEN_DECL (asm_out_file, decl, name, IDENTIFIER_POINTER (target));
5728 # else
5729 ASM_OUTPUT_WEAK_ALIAS (asm_out_file, name, IDENTIFIER_POINTER (target));
5730 # endif
5731 /* Remove this function from the pending weak list so that
5732 we do not emit multiple .weak directives for it. */
5733 for (p = &weak_decls; (t = *p) ; )
5734 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t))
5735 || id == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
5736 *p = TREE_CHAIN (t);
5737 else
5738 p = &TREE_CHAIN (t);
5740 /* Remove weakrefs to the same target from the pending weakref
5741 list, for the same reason. */
5742 for (p = &weakref_targets; (t = *p) ; )
5744 if (id == ultimate_transparent_alias_target (&TREE_VALUE (t)))
5745 *p = TREE_CHAIN (t);
5746 else
5747 p = &TREE_CHAIN (t);
5750 #endif
5753 /* Emit an assembler directive to make the symbol for DECL an alias to
5754 the symbol for TARGET. */
5756 void
5757 assemble_alias (tree decl, tree target)
5759 tree target_decl;
5761 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
5763 tree alias = DECL_ASSEMBLER_NAME (decl);
5765 ultimate_transparent_alias_target (&target);
5767 if (alias == target)
5768 error ("weakref %q+D ultimately targets itself", decl);
5769 if (TREE_PUBLIC (decl))
5770 error ("weakref %q+D must have static linkage", decl);
5772 else
5774 #if !defined (ASM_OUTPUT_DEF)
5775 # if !defined(ASM_OUTPUT_WEAK_ALIAS) && !defined (ASM_WEAKEN_DECL)
5776 error_at (DECL_SOURCE_LOCATION (decl),
5777 "alias definitions not supported in this configuration");
5778 TREE_ASM_WRITTEN (decl) = 1;
5779 return;
5780 # else
5781 if (!DECL_WEAK (decl))
5783 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
5784 error_at (DECL_SOURCE_LOCATION (decl),
5785 "ifunc is not supported in this configuration");
5786 else
5787 error_at (DECL_SOURCE_LOCATION (decl),
5788 "only weak aliases are supported in this configuration");
5789 TREE_ASM_WRITTEN (decl) = 1;
5790 return;
5792 # endif
5793 #endif
5795 TREE_USED (decl) = 1;
5797 /* Allow aliases to aliases. */
5798 if (TREE_CODE (decl) == FUNCTION_DECL)
5799 cgraph_node::get_create (decl)->alias = true;
5800 else
5801 varpool_node::get_create (decl)->alias = true;
5803 /* If the target has already been emitted, we don't have to queue the
5804 alias. This saves a tad of memory. */
5805 if (symtab->global_info_ready)
5806 target_decl = find_decl (target);
5807 else
5808 target_decl= NULL;
5809 if ((target_decl && TREE_ASM_WRITTEN (target_decl))
5810 || symtab->state >= EXPANSION)
5811 do_assemble_alias (decl, target);
5812 else
5814 alias_pair p = {decl, target};
5815 vec_safe_push (alias_pairs, p);
5819 /* Record and output a table of translations from original function
5820 to its transaction aware clone. Note that tm_pure functions are
5821 considered to be their own clone. */
5823 struct tm_clone_hasher : ggc_cache_hasher<tree_map *>
5825 static hashval_t hash (tree_map *m) { return tree_map_hash (m); }
5826 static bool equal (tree_map *a, tree_map *b) { return tree_map_eq (a, b); }
5828 static void
5829 handle_cache_entry (tree_map *&e)
5831 extern void gt_ggc_mx (tree_map *&);
5832 if (e == HTAB_EMPTY_ENTRY || e == HTAB_DELETED_ENTRY)
5833 return;
5834 else if (ggc_marked_p (e->base.from))
5835 gt_ggc_mx (e);
5836 else
5837 e = static_cast<tree_map *> (HTAB_DELETED_ENTRY);
5841 static GTY((cache)) hash_table<tm_clone_hasher> *tm_clone_hash;
5843 void
5844 record_tm_clone_pair (tree o, tree n)
5846 struct tree_map **slot, *h;
5848 if (tm_clone_hash == NULL)
5849 tm_clone_hash = hash_table<tm_clone_hasher>::create_ggc (32);
5851 h = ggc_alloc<tree_map> ();
5852 h->hash = htab_hash_pointer (o);
5853 h->base.from = o;
5854 h->to = n;
5856 slot = tm_clone_hash->find_slot_with_hash (h, h->hash, INSERT);
5857 *slot = h;
5860 tree
5861 get_tm_clone_pair (tree o)
5863 if (tm_clone_hash)
5865 struct tree_map *h, in;
5867 in.base.from = o;
5868 in.hash = htab_hash_pointer (o);
5869 h = tm_clone_hash->find_with_hash (&in, in.hash);
5870 if (h)
5871 return h->to;
5873 return NULL_TREE;
5876 typedef struct tm_alias_pair
5878 unsigned int uid;
5879 tree from;
5880 tree to;
5881 } tm_alias_pair;
5884 /* Dump the actual pairs to the .tm_clone_table section. */
5886 static void
5887 dump_tm_clone_pairs (vec<tm_alias_pair> tm_alias_pairs)
5889 unsigned i;
5890 tm_alias_pair *p;
5891 bool switched = false;
5893 FOR_EACH_VEC_ELT (tm_alias_pairs, i, p)
5895 tree src = p->from;
5896 tree dst = p->to;
5897 struct cgraph_node *src_n = cgraph_node::get (src);
5898 struct cgraph_node *dst_n = cgraph_node::get (dst);
5900 /* The function ipa_tm_create_version() marks the clone as needed if
5901 the original function was needed. But we also mark the clone as
5902 needed if we ever called the clone indirectly through
5903 TM_GETTMCLONE. If neither of these are true, we didn't generate
5904 a clone, and we didn't call it indirectly... no sense keeping it
5905 in the clone table. */
5906 if (!dst_n || !dst_n->definition)
5907 continue;
5909 /* This covers the case where we have optimized the original
5910 function away, and only access the transactional clone. */
5911 if (!src_n || !src_n->definition)
5912 continue;
5914 if (!switched)
5916 switch_to_section (targetm.asm_out.tm_clone_table_section ());
5917 assemble_align (POINTER_SIZE);
5918 switched = true;
5921 assemble_integer (XEXP (DECL_RTL (src), 0),
5922 POINTER_SIZE_UNITS, POINTER_SIZE, 1);
5923 assemble_integer (XEXP (DECL_RTL (dst), 0),
5924 POINTER_SIZE_UNITS, POINTER_SIZE, 1);
5928 /* Provide a default for the tm_clone_table section. */
5930 section *
5931 default_clone_table_section (void)
5933 return get_named_section (NULL, ".tm_clone_table", 3);
5936 /* Helper comparison function for qsorting by the DECL_UID stored in
5937 alias_pair->emitted_diags. */
5939 static int
5940 tm_alias_pair_cmp (const void *x, const void *y)
5942 const tm_alias_pair *p1 = (const tm_alias_pair *) x;
5943 const tm_alias_pair *p2 = (const tm_alias_pair *) y;
5944 if (p1->uid < p2->uid)
5945 return -1;
5946 if (p1->uid > p2->uid)
5947 return 1;
5948 return 0;
5951 void
5952 finish_tm_clone_pairs (void)
5954 vec<tm_alias_pair> tm_alias_pairs = vNULL;
5956 if (tm_clone_hash == NULL)
5957 return;
5959 /* We need a determenistic order for the .tm_clone_table, otherwise
5960 we will get bootstrap comparison failures, so dump the hash table
5961 to a vector, sort it, and dump the vector. */
5963 /* Dump the hashtable to a vector. */
5964 tree_map *map;
5965 hash_table<tm_clone_hasher>::iterator iter;
5966 FOR_EACH_HASH_TABLE_ELEMENT (*tm_clone_hash, map, tree_map *, iter)
5968 tm_alias_pair p = {DECL_UID (map->base.from), map->base.from, map->to};
5969 tm_alias_pairs.safe_push (p);
5971 /* Sort it. */
5972 tm_alias_pairs.qsort (tm_alias_pair_cmp);
5974 /* Dump it. */
5975 dump_tm_clone_pairs (tm_alias_pairs);
5977 tm_clone_hash->empty ();
5978 tm_clone_hash = NULL;
5979 tm_alias_pairs.release ();
5983 /* Emit an assembler directive to set symbol for DECL visibility to
5984 the visibility type VIS, which must not be VISIBILITY_DEFAULT. */
5986 void
5987 default_assemble_visibility (tree decl ATTRIBUTE_UNUSED,
5988 int vis ATTRIBUTE_UNUSED)
5990 #ifdef HAVE_GAS_HIDDEN
5991 static const char * const visibility_types[] = {
5992 NULL, "protected", "hidden", "internal"
5995 const char *name, *type;
5996 tree id;
5998 id = DECL_ASSEMBLER_NAME (decl);
5999 ultimate_transparent_alias_target (&id);
6000 name = IDENTIFIER_POINTER (id);
6002 type = visibility_types[vis];
6004 fprintf (asm_out_file, "\t.%s\t", type);
6005 assemble_name (asm_out_file, name);
6006 fprintf (asm_out_file, "\n");
6007 #else
6008 if (!DECL_ARTIFICIAL (decl))
6009 warning (OPT_Wattributes, "visibility attribute not supported "
6010 "in this configuration; ignored");
6011 #endif
6014 /* A helper function to call assemble_visibility when needed for a decl. */
6017 maybe_assemble_visibility (tree decl)
6019 enum symbol_visibility vis = DECL_VISIBILITY (decl);
6021 if (TREE_CODE (decl) == FUNCTION_DECL
6022 && cgraph_node::get (decl)
6023 && cgraph_node::get (decl)->instrumentation_clone
6024 && cgraph_node::get (decl)->instrumented_version)
6025 vis = DECL_VISIBILITY (cgraph_node::get (decl)->instrumented_version->decl);
6027 if (vis != VISIBILITY_DEFAULT)
6029 targetm.asm_out.assemble_visibility (decl, vis);
6030 return 1;
6032 else
6033 return 0;
6036 /* Returns 1 if the target configuration supports defining public symbols
6037 so that one of them will be chosen at link time instead of generating a
6038 multiply-defined symbol error, whether through the use of weak symbols or
6039 a target-specific mechanism for having duplicates discarded. */
6042 supports_one_only (void)
6044 if (SUPPORTS_ONE_ONLY)
6045 return 1;
6046 return TARGET_SUPPORTS_WEAK;
6049 /* Set up DECL as a public symbol that can be defined in multiple
6050 translation units without generating a linker error. */
6052 void
6053 make_decl_one_only (tree decl, tree comdat_group)
6055 struct symtab_node *symbol;
6056 gcc_assert (TREE_CODE (decl) == VAR_DECL
6057 || TREE_CODE (decl) == FUNCTION_DECL);
6059 TREE_PUBLIC (decl) = 1;
6061 if (TREE_CODE (decl) == VAR_DECL)
6062 symbol = varpool_node::get_create (decl);
6063 else
6064 symbol = cgraph_node::get_create (decl);
6066 if (SUPPORTS_ONE_ONLY)
6068 #ifdef MAKE_DECL_ONE_ONLY
6069 MAKE_DECL_ONE_ONLY (decl);
6070 #endif
6071 symbol->set_comdat_group (comdat_group);
6073 else if (TREE_CODE (decl) == VAR_DECL
6074 && (DECL_INITIAL (decl) == 0
6075 || (!in_lto_p && DECL_INITIAL (decl) == error_mark_node)))
6076 DECL_COMMON (decl) = 1;
6077 else
6079 gcc_assert (TARGET_SUPPORTS_WEAK);
6080 DECL_WEAK (decl) = 1;
6084 void
6085 init_varasm_once (void)
6087 section_htab = hash_table<section_hasher>::create_ggc (31);
6088 object_block_htab = hash_table<object_block_hasher>::create_ggc (31);
6089 const_desc_htab = hash_table<tree_descriptor_hasher>::create_ggc (1009);
6091 shared_constant_pool = create_constant_pool ();
6093 #ifdef TEXT_SECTION_ASM_OP
6094 text_section = get_unnamed_section (SECTION_CODE, output_section_asm_op,
6095 TEXT_SECTION_ASM_OP);
6096 #endif
6098 #ifdef DATA_SECTION_ASM_OP
6099 data_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
6100 DATA_SECTION_ASM_OP);
6101 #endif
6103 #ifdef SDATA_SECTION_ASM_OP
6104 sdata_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
6105 SDATA_SECTION_ASM_OP);
6106 #endif
6108 #ifdef READONLY_DATA_SECTION_ASM_OP
6109 readonly_data_section = get_unnamed_section (0, output_section_asm_op,
6110 READONLY_DATA_SECTION_ASM_OP);
6111 #endif
6113 #ifdef CTORS_SECTION_ASM_OP
6114 ctors_section = get_unnamed_section (0, output_section_asm_op,
6115 CTORS_SECTION_ASM_OP);
6116 #endif
6118 #ifdef DTORS_SECTION_ASM_OP
6119 dtors_section = get_unnamed_section (0, output_section_asm_op,
6120 DTORS_SECTION_ASM_OP);
6121 #endif
6123 #ifdef BSS_SECTION_ASM_OP
6124 bss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
6125 output_section_asm_op,
6126 BSS_SECTION_ASM_OP);
6127 #endif
6129 #ifdef SBSS_SECTION_ASM_OP
6130 sbss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
6131 output_section_asm_op,
6132 SBSS_SECTION_ASM_OP);
6133 #endif
6135 tls_comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6136 | SECTION_COMMON, emit_tls_common);
6137 lcomm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6138 | SECTION_COMMON, emit_local);
6139 comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6140 | SECTION_COMMON, emit_common);
6142 #if defined ASM_OUTPUT_ALIGNED_BSS
6143 bss_noswitch_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS,
6144 emit_bss);
6145 #endif
6147 targetm.asm_out.init_sections ();
6149 if (readonly_data_section == NULL)
6150 readonly_data_section = text_section;
6152 #ifdef ASM_OUTPUT_EXTERNAL
6153 pending_assemble_externals_set = new hash_set<tree>;
6154 #endif
6157 enum tls_model
6158 decl_default_tls_model (const_tree decl)
6160 enum tls_model kind;
6161 bool is_local;
6163 is_local = targetm.binds_local_p (decl);
6164 if (!flag_shlib)
6166 if (is_local)
6167 kind = TLS_MODEL_LOCAL_EXEC;
6168 else
6169 kind = TLS_MODEL_INITIAL_EXEC;
6172 /* Local dynamic is inefficient when we're not combining the
6173 parts of the address. */
6174 else if (optimize && is_local)
6175 kind = TLS_MODEL_LOCAL_DYNAMIC;
6176 else
6177 kind = TLS_MODEL_GLOBAL_DYNAMIC;
6178 if (kind < flag_tls_default)
6179 kind = flag_tls_default;
6181 return kind;
6184 /* Select a set of attributes for section NAME based on the properties
6185 of DECL and whether or not RELOC indicates that DECL's initializer
6186 might contain runtime relocations.
6188 We make the section read-only and executable for a function decl,
6189 read-only for a const data decl, and writable for a non-const data decl. */
6191 unsigned int
6192 default_section_type_flags (tree decl, const char *name, int reloc)
6194 unsigned int flags;
6196 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
6197 flags = SECTION_CODE;
6198 else if (decl)
6200 enum section_category category
6201 = categorize_decl_for_section (decl, reloc);
6202 if (decl_readonly_section_1 (category))
6203 flags = 0;
6204 else if (category == SECCAT_DATA_REL_RO
6205 || category == SECCAT_DATA_REL_RO_LOCAL)
6206 flags = SECTION_WRITE | SECTION_RELRO;
6207 else
6208 flags = SECTION_WRITE;
6210 else
6212 flags = SECTION_WRITE;
6213 if (strcmp (name, ".data.rel.ro") == 0
6214 || strcmp (name, ".data.rel.ro.local") == 0)
6215 flags |= SECTION_RELRO;
6218 if (decl && DECL_P (decl) && DECL_COMDAT_GROUP (decl))
6219 flags |= SECTION_LINKONCE;
6221 if (strcmp (name, ".vtable_map_vars") == 0)
6222 flags |= SECTION_LINKONCE;
6224 if (decl && TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
6225 flags |= SECTION_TLS | SECTION_WRITE;
6227 if (strcmp (name, ".bss") == 0
6228 || strncmp (name, ".bss.", 5) == 0
6229 || strncmp (name, ".gnu.linkonce.b.", 16) == 0
6230 || strcmp (name, ".persistent.bss") == 0
6231 || strcmp (name, ".sbss") == 0
6232 || strncmp (name, ".sbss.", 6) == 0
6233 || strncmp (name, ".gnu.linkonce.sb.", 17) == 0)
6234 flags |= SECTION_BSS;
6236 if (strcmp (name, ".tdata") == 0
6237 || strncmp (name, ".tdata.", 7) == 0
6238 || strncmp (name, ".gnu.linkonce.td.", 17) == 0)
6239 flags |= SECTION_TLS;
6241 if (strcmp (name, ".tbss") == 0
6242 || strncmp (name, ".tbss.", 6) == 0
6243 || strncmp (name, ".gnu.linkonce.tb.", 17) == 0)
6244 flags |= SECTION_TLS | SECTION_BSS;
6246 /* These three sections have special ELF types. They are neither
6247 SHT_PROGBITS nor SHT_NOBITS, so when changing sections we don't
6248 want to print a section type (@progbits or @nobits). If someone
6249 is silly enough to emit code or TLS variables to one of these
6250 sections, then don't handle them specially. */
6251 if (!(flags & (SECTION_CODE | SECTION_BSS | SECTION_TLS))
6252 && (strcmp (name, ".init_array") == 0
6253 || strcmp (name, ".fini_array") == 0
6254 || strcmp (name, ".preinit_array") == 0))
6255 flags |= SECTION_NOTYPE;
6257 return flags;
6260 /* Return true if the target supports some form of global BSS,
6261 either through bss_noswitch_section, or by selecting a BSS
6262 section in TARGET_ASM_SELECT_SECTION. */
6264 bool
6265 have_global_bss_p (void)
6267 return bss_noswitch_section || targetm.have_switchable_bss_sections;
6270 /* Output assembly to switch to section NAME with attribute FLAGS.
6271 Four variants for common object file formats. */
6273 void
6274 default_no_named_section (const char *name ATTRIBUTE_UNUSED,
6275 unsigned int flags ATTRIBUTE_UNUSED,
6276 tree decl ATTRIBUTE_UNUSED)
6278 /* Some object formats don't support named sections at all. The
6279 front-end should already have flagged this as an error. */
6280 gcc_unreachable ();
6283 #ifndef TLS_SECTION_ASM_FLAG
6284 #define TLS_SECTION_ASM_FLAG 'T'
6285 #endif
6287 void
6288 default_elf_asm_named_section (const char *name, unsigned int flags,
6289 tree decl ATTRIBUTE_UNUSED)
6291 char flagchars[10], *f = flagchars;
6293 /* If we have already declared this section, we can use an
6294 abbreviated form to switch back to it -- unless this section is
6295 part of a COMDAT groups, in which case GAS requires the full
6296 declaration every time. */
6297 if (!(HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6298 && (flags & SECTION_DECLARED))
6300 fprintf (asm_out_file, "\t.section\t%s\n", name);
6301 return;
6304 if (!(flags & SECTION_DEBUG))
6305 *f++ = 'a';
6306 #if defined (HAVE_GAS_SECTION_EXCLUDE) && HAVE_GAS_SECTION_EXCLUDE == 1
6307 if (flags & SECTION_EXCLUDE)
6308 *f++ = 'e';
6309 #endif
6310 if (flags & SECTION_WRITE)
6311 *f++ = 'w';
6312 if (flags & SECTION_CODE)
6313 *f++ = 'x';
6314 if (flags & SECTION_SMALL)
6315 *f++ = 's';
6316 if (flags & SECTION_MERGE)
6317 *f++ = 'M';
6318 if (flags & SECTION_STRINGS)
6319 *f++ = 'S';
6320 if (flags & SECTION_TLS)
6321 *f++ = TLS_SECTION_ASM_FLAG;
6322 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6323 *f++ = 'G';
6324 *f = '\0';
6326 fprintf (asm_out_file, "\t.section\t%s,\"%s\"", name, flagchars);
6328 if (!(flags & SECTION_NOTYPE))
6330 const char *type;
6331 const char *format;
6333 if (flags & SECTION_BSS)
6334 type = "nobits";
6335 else
6336 type = "progbits";
6338 format = ",@%s";
6339 /* On platforms that use "@" as the assembly comment character,
6340 use "%" instead. */
6341 if (strcmp (ASM_COMMENT_START, "@") == 0)
6342 format = ",%%%s";
6343 fprintf (asm_out_file, format, type);
6345 if (flags & SECTION_ENTSIZE)
6346 fprintf (asm_out_file, ",%d", flags & SECTION_ENTSIZE);
6347 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6349 if (TREE_CODE (decl) == IDENTIFIER_NODE)
6350 fprintf (asm_out_file, ",%s,comdat", IDENTIFIER_POINTER (decl));
6351 else
6352 fprintf (asm_out_file, ",%s,comdat",
6353 IDENTIFIER_POINTER (DECL_COMDAT_GROUP (decl)));
6357 putc ('\n', asm_out_file);
6360 void
6361 default_coff_asm_named_section (const char *name, unsigned int flags,
6362 tree decl ATTRIBUTE_UNUSED)
6364 char flagchars[8], *f = flagchars;
6366 if (flags & SECTION_WRITE)
6367 *f++ = 'w';
6368 if (flags & SECTION_CODE)
6369 *f++ = 'x';
6370 *f = '\0';
6372 fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
6375 void
6376 default_pe_asm_named_section (const char *name, unsigned int flags,
6377 tree decl)
6379 default_coff_asm_named_section (name, flags, decl);
6381 if (flags & SECTION_LINKONCE)
6383 /* Functions may have been compiled at various levels of
6384 optimization so we can't use `same_size' here.
6385 Instead, have the linker pick one. */
6386 fprintf (asm_out_file, "\t.linkonce %s\n",
6387 (flags & SECTION_CODE ? "discard" : "same_size"));
6391 /* The lame default section selector. */
6393 section *
6394 default_select_section (tree decl, int reloc,
6395 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
6397 if (DECL_P (decl))
6399 if (decl_readonly_section (decl, reloc))
6400 return readonly_data_section;
6402 else if (TREE_CODE (decl) == CONSTRUCTOR)
6404 if (! ((flag_pic && reloc)
6405 || !TREE_READONLY (decl)
6406 || TREE_SIDE_EFFECTS (decl)
6407 || !TREE_CONSTANT (decl)))
6408 return readonly_data_section;
6410 else if (TREE_CODE (decl) == STRING_CST)
6411 return readonly_data_section;
6412 else if (! (flag_pic && reloc))
6413 return readonly_data_section;
6415 return data_section;
6418 enum section_category
6419 categorize_decl_for_section (const_tree decl, int reloc)
6421 enum section_category ret;
6423 if (TREE_CODE (decl) == FUNCTION_DECL)
6424 return SECCAT_TEXT;
6425 else if (TREE_CODE (decl) == STRING_CST)
6427 if ((flag_sanitize & SANITIZE_ADDRESS)
6428 && asan_protect_global (CONST_CAST_TREE (decl)))
6429 /* or !flag_merge_constants */
6430 return SECCAT_RODATA;
6431 else
6432 return SECCAT_RODATA_MERGE_STR;
6434 else if (TREE_CODE (decl) == VAR_DECL)
6436 if (bss_initializer_p (decl))
6437 ret = SECCAT_BSS;
6438 else if (! TREE_READONLY (decl)
6439 || TREE_SIDE_EFFECTS (decl)
6440 || ! TREE_CONSTANT (DECL_INITIAL (decl)))
6442 /* Here the reloc_rw_mask is not testing whether the section should
6443 be read-only or not, but whether the dynamic link will have to
6444 do something. If so, we wish to segregate the data in order to
6445 minimize cache misses inside the dynamic linker. */
6446 if (reloc & targetm.asm_out.reloc_rw_mask ())
6447 ret = reloc == 1 ? SECCAT_DATA_REL_LOCAL : SECCAT_DATA_REL;
6448 else
6449 ret = SECCAT_DATA;
6451 else if (reloc & targetm.asm_out.reloc_rw_mask ())
6452 ret = reloc == 1 ? SECCAT_DATA_REL_RO_LOCAL : SECCAT_DATA_REL_RO;
6453 else if (reloc || flag_merge_constants < 2
6454 || ((flag_sanitize & SANITIZE_ADDRESS)
6455 && asan_protect_global (CONST_CAST_TREE (decl))))
6456 /* C and C++ don't allow different variables to share the same
6457 location. -fmerge-all-constants allows even that (at the
6458 expense of not conforming). */
6459 ret = SECCAT_RODATA;
6460 else if (TREE_CODE (DECL_INITIAL (decl)) == STRING_CST)
6461 ret = SECCAT_RODATA_MERGE_STR_INIT;
6462 else
6463 ret = SECCAT_RODATA_MERGE_CONST;
6465 else if (TREE_CODE (decl) == CONSTRUCTOR)
6467 if ((reloc & targetm.asm_out.reloc_rw_mask ())
6468 || TREE_SIDE_EFFECTS (decl)
6469 || ! TREE_CONSTANT (decl))
6470 ret = SECCAT_DATA;
6471 else
6472 ret = SECCAT_RODATA;
6474 else
6475 ret = SECCAT_RODATA;
6477 /* There are no read-only thread-local sections. */
6478 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
6480 /* Note that this would be *just* SECCAT_BSS, except that there's
6481 no concept of a read-only thread-local-data section. */
6482 if (ret == SECCAT_BSS
6483 || (flag_zero_initialized_in_bss
6484 && initializer_zerop (DECL_INITIAL (decl))))
6485 ret = SECCAT_TBSS;
6486 else
6487 ret = SECCAT_TDATA;
6490 /* If the target uses small data sections, select it. */
6491 else if (targetm.in_small_data_p (decl))
6493 if (ret == SECCAT_BSS)
6494 ret = SECCAT_SBSS;
6495 else if (targetm.have_srodata_section && ret == SECCAT_RODATA)
6496 ret = SECCAT_SRODATA;
6497 else
6498 ret = SECCAT_SDATA;
6501 return ret;
6504 static bool
6505 decl_readonly_section_1 (enum section_category category)
6507 switch (category)
6509 case SECCAT_RODATA:
6510 case SECCAT_RODATA_MERGE_STR:
6511 case SECCAT_RODATA_MERGE_STR_INIT:
6512 case SECCAT_RODATA_MERGE_CONST:
6513 case SECCAT_SRODATA:
6514 return true;
6515 default:
6516 return false;
6520 bool
6521 decl_readonly_section (const_tree decl, int reloc)
6523 return decl_readonly_section_1 (categorize_decl_for_section (decl, reloc));
6526 /* Select a section based on the above categorization. */
6528 section *
6529 default_elf_select_section (tree decl, int reloc,
6530 unsigned HOST_WIDE_INT align)
6532 const char *sname;
6533 switch (categorize_decl_for_section (decl, reloc))
6535 case SECCAT_TEXT:
6536 /* We're not supposed to be called on FUNCTION_DECLs. */
6537 gcc_unreachable ();
6538 case SECCAT_RODATA:
6539 return readonly_data_section;
6540 case SECCAT_RODATA_MERGE_STR:
6541 return mergeable_string_section (decl, align, 0);
6542 case SECCAT_RODATA_MERGE_STR_INIT:
6543 return mergeable_string_section (DECL_INITIAL (decl), align, 0);
6544 case SECCAT_RODATA_MERGE_CONST:
6545 return mergeable_constant_section (DECL_MODE (decl), align, 0);
6546 case SECCAT_SRODATA:
6547 sname = ".sdata2";
6548 break;
6549 case SECCAT_DATA:
6550 return data_section;
6551 case SECCAT_DATA_REL:
6552 sname = ".data.rel";
6553 break;
6554 case SECCAT_DATA_REL_LOCAL:
6555 sname = ".data.rel.local";
6556 break;
6557 case SECCAT_DATA_REL_RO:
6558 sname = ".data.rel.ro";
6559 break;
6560 case SECCAT_DATA_REL_RO_LOCAL:
6561 sname = ".data.rel.ro.local";
6562 break;
6563 case SECCAT_SDATA:
6564 sname = ".sdata";
6565 break;
6566 case SECCAT_TDATA:
6567 sname = ".tdata";
6568 break;
6569 case SECCAT_BSS:
6570 if (bss_section)
6571 return bss_section;
6572 sname = ".bss";
6573 break;
6574 case SECCAT_SBSS:
6575 sname = ".sbss";
6576 break;
6577 case SECCAT_TBSS:
6578 sname = ".tbss";
6579 break;
6580 default:
6581 gcc_unreachable ();
6584 return get_named_section (decl, sname, reloc);
6587 /* Construct a unique section name based on the decl name and the
6588 categorization performed above. */
6590 void
6591 default_unique_section (tree decl, int reloc)
6593 /* We only need to use .gnu.linkonce if we don't have COMDAT groups. */
6594 bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
6595 const char *prefix, *name, *linkonce;
6596 char *string;
6597 tree id;
6599 switch (categorize_decl_for_section (decl, reloc))
6601 case SECCAT_TEXT:
6602 prefix = one_only ? ".t" : ".text";
6603 break;
6604 case SECCAT_RODATA:
6605 case SECCAT_RODATA_MERGE_STR:
6606 case SECCAT_RODATA_MERGE_STR_INIT:
6607 case SECCAT_RODATA_MERGE_CONST:
6608 prefix = one_only ? ".r" : ".rodata";
6609 break;
6610 case SECCAT_SRODATA:
6611 prefix = one_only ? ".s2" : ".sdata2";
6612 break;
6613 case SECCAT_DATA:
6614 prefix = one_only ? ".d" : ".data";
6615 break;
6616 case SECCAT_DATA_REL:
6617 prefix = one_only ? ".d.rel" : ".data.rel";
6618 break;
6619 case SECCAT_DATA_REL_LOCAL:
6620 prefix = one_only ? ".d.rel.local" : ".data.rel.local";
6621 break;
6622 case SECCAT_DATA_REL_RO:
6623 prefix = one_only ? ".d.rel.ro" : ".data.rel.ro";
6624 break;
6625 case SECCAT_DATA_REL_RO_LOCAL:
6626 prefix = one_only ? ".d.rel.ro.local" : ".data.rel.ro.local";
6627 break;
6628 case SECCAT_SDATA:
6629 prefix = one_only ? ".s" : ".sdata";
6630 break;
6631 case SECCAT_BSS:
6632 prefix = one_only ? ".b" : ".bss";
6633 break;
6634 case SECCAT_SBSS:
6635 prefix = one_only ? ".sb" : ".sbss";
6636 break;
6637 case SECCAT_TDATA:
6638 prefix = one_only ? ".td" : ".tdata";
6639 break;
6640 case SECCAT_TBSS:
6641 prefix = one_only ? ".tb" : ".tbss";
6642 break;
6643 default:
6644 gcc_unreachable ();
6647 id = DECL_ASSEMBLER_NAME (decl);
6648 ultimate_transparent_alias_target (&id);
6649 name = IDENTIFIER_POINTER (id);
6650 name = targetm.strip_name_encoding (name);
6652 /* If we're using one_only, then there needs to be a .gnu.linkonce
6653 prefix to the section name. */
6654 linkonce = one_only ? ".gnu.linkonce" : "";
6656 string = ACONCAT ((linkonce, prefix, ".", name, NULL));
6658 set_decl_section_name (decl, string);
6661 /* Subroutine of compute_reloc_for_rtx for leaf rtxes. */
6663 static int
6664 compute_reloc_for_rtx_1 (const_rtx x)
6666 switch (GET_CODE (x))
6668 case SYMBOL_REF:
6669 return SYMBOL_REF_LOCAL_P (x) ? 1 : 2;
6670 case LABEL_REF:
6671 return 1;
6672 default:
6673 return 0;
6677 /* Like compute_reloc_for_constant, except for an RTX. The return value
6678 is a mask for which bit 1 indicates a global relocation, and bit 0
6679 indicates a local relocation. */
6681 static int
6682 compute_reloc_for_rtx (const_rtx x)
6684 switch (GET_CODE (x))
6686 case SYMBOL_REF:
6687 case LABEL_REF:
6688 return compute_reloc_for_rtx_1 (x);
6690 case CONST:
6692 int reloc = 0;
6693 subrtx_iterator::array_type array;
6694 FOR_EACH_SUBRTX (iter, array, x, ALL)
6695 reloc |= compute_reloc_for_rtx_1 (*iter);
6696 return reloc;
6699 default:
6700 return 0;
6704 section *
6705 default_select_rtx_section (machine_mode mode ATTRIBUTE_UNUSED,
6706 rtx x,
6707 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
6709 if (compute_reloc_for_rtx (x) & targetm.asm_out.reloc_rw_mask ())
6710 return data_section;
6711 else
6712 return readonly_data_section;
6715 section *
6716 default_elf_select_rtx_section (machine_mode mode, rtx x,
6717 unsigned HOST_WIDE_INT align)
6719 int reloc = compute_reloc_for_rtx (x);
6721 /* ??? Handle small data here somehow. */
6723 if (reloc & targetm.asm_out.reloc_rw_mask ())
6725 if (reloc == 1)
6726 return get_named_section (NULL, ".data.rel.ro.local", 1);
6727 else
6728 return get_named_section (NULL, ".data.rel.ro", 3);
6731 return mergeable_constant_section (mode, align, 0);
6734 /* Set the generally applicable flags on the SYMBOL_REF for EXP. */
6736 void
6737 default_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
6739 rtx symbol;
6740 int flags;
6742 /* Careful not to prod global register variables. */
6743 if (!MEM_P (rtl))
6744 return;
6745 symbol = XEXP (rtl, 0);
6746 if (GET_CODE (symbol) != SYMBOL_REF)
6747 return;
6749 flags = SYMBOL_REF_FLAGS (symbol) & SYMBOL_FLAG_HAS_BLOCK_INFO;
6750 if (TREE_CODE (decl) == FUNCTION_DECL)
6751 flags |= SYMBOL_FLAG_FUNCTION;
6752 if (targetm.binds_local_p (decl))
6753 flags |= SYMBOL_FLAG_LOCAL;
6754 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
6755 flags |= DECL_TLS_MODEL (decl) << SYMBOL_FLAG_TLS_SHIFT;
6756 else if (targetm.in_small_data_p (decl))
6757 flags |= SYMBOL_FLAG_SMALL;
6758 /* ??? Why is DECL_EXTERNAL ever set for non-PUBLIC names? Without
6759 being PUBLIC, the thing *must* be defined in this translation unit.
6760 Prevent this buglet from being propagated into rtl code as well. */
6761 if (DECL_P (decl) && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl))
6762 flags |= SYMBOL_FLAG_EXTERNAL;
6764 SYMBOL_REF_FLAGS (symbol) = flags;
6767 /* By default, we do nothing for encode_section_info, so we need not
6768 do anything but discard the '*' marker. */
6770 const char *
6771 default_strip_name_encoding (const char *str)
6773 return str + (*str == '*');
6776 #ifdef ASM_OUTPUT_DEF
6777 /* The default implementation of TARGET_ASM_OUTPUT_ANCHOR. Define the
6778 anchor relative to ".", the current section position. */
6780 void
6781 default_asm_output_anchor (rtx symbol)
6783 char buffer[100];
6785 sprintf (buffer, "*. + " HOST_WIDE_INT_PRINT_DEC,
6786 SYMBOL_REF_BLOCK_OFFSET (symbol));
6787 ASM_OUTPUT_DEF (asm_out_file, XSTR (symbol, 0), buffer);
6789 #endif
6791 /* The default implementation of TARGET_USE_ANCHORS_FOR_SYMBOL_P. */
6793 bool
6794 default_use_anchors_for_symbol_p (const_rtx symbol)
6796 section *sect;
6797 tree decl;
6799 /* Don't use anchors for mergeable sections. The linker might move
6800 the objects around. */
6801 sect = SYMBOL_REF_BLOCK (symbol)->sect;
6802 if (sect->common.flags & SECTION_MERGE)
6803 return false;
6805 /* Don't use anchors for small data sections. The small data register
6806 acts as an anchor for such sections. */
6807 if (sect->common.flags & SECTION_SMALL)
6808 return false;
6810 decl = SYMBOL_REF_DECL (symbol);
6811 if (decl && DECL_P (decl))
6813 /* Don't use section anchors for decls that might be defined or
6814 usurped by other modules. */
6815 if (TREE_PUBLIC (decl) && !decl_binds_to_current_def_p (decl))
6816 return false;
6818 /* Don't use section anchors for decls that will be placed in a
6819 small data section. */
6820 /* ??? Ideally, this check would be redundant with the SECTION_SMALL
6821 one above. The problem is that we only use SECTION_SMALL for
6822 sections that should be marked as small in the section directive. */
6823 if (targetm.in_small_data_p (decl))
6824 return false;
6826 return true;
6829 /* Return true when RESOLUTION indicate that symbol will be bound to the
6830 definition provided by current .o file. */
6832 static bool
6833 resolution_to_local_definition_p (enum ld_plugin_symbol_resolution resolution)
6835 return (resolution == LDPR_PREVAILING_DEF
6836 || resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
6837 || resolution == LDPR_PREVAILING_DEF_IRONLY);
6840 /* Return true when RESOLUTION indicate that symbol will be bound locally
6841 within current executable or DSO. */
6843 static bool
6844 resolution_local_p (enum ld_plugin_symbol_resolution resolution)
6846 return (resolution == LDPR_PREVAILING_DEF
6847 || resolution == LDPR_PREVAILING_DEF_IRONLY
6848 || resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
6849 || resolution == LDPR_PREEMPTED_REG
6850 || resolution == LDPR_PREEMPTED_IR
6851 || resolution == LDPR_RESOLVED_IR
6852 || resolution == LDPR_RESOLVED_EXEC);
6855 /* COMMON_LOCAL_P is true means that the linker can guarantee that an
6856 uninitialized common symbol in the executable will still be defined
6857 (through COPY relocation) in the executable. */
6859 bool
6860 default_binds_local_p_3 (const_tree exp, bool shlib, bool weak_dominate,
6861 bool extern_protected_data, bool common_local_p)
6863 /* A non-decl is an entry in the constant pool. */
6864 if (!DECL_P (exp))
6865 return true;
6867 /* Weakrefs may not bind locally, even though the weakref itself is always
6868 static and therefore local. Similarly, the resolver for ifunc functions
6869 might resolve to a non-local function.
6870 FIXME: We can resolve the weakref case more curefuly by looking at the
6871 weakref alias. */
6872 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (exp))
6873 || (TREE_CODE (exp) == FUNCTION_DECL
6874 && lookup_attribute ("ifunc", DECL_ATTRIBUTES (exp))))
6875 return false;
6877 /* Static variables are always local. */
6878 if (! TREE_PUBLIC (exp))
6879 return true;
6881 /* With resolution file in hand, take look into resolutions.
6882 We can't just return true for resolved_locally symbols,
6883 because dynamic linking might overwrite symbols
6884 in shared libraries. */
6885 bool resolved_locally = false;
6887 bool uninited_common = (DECL_COMMON (exp)
6888 && (DECL_INITIAL (exp) == NULL
6889 || (!in_lto_p
6890 && DECL_INITIAL (exp) == error_mark_node)));
6892 /* A non-external variable is defined locally only if it isn't
6893 uninitialized COMMON variable or common_local_p is true. */
6894 bool defined_locally = (!DECL_EXTERNAL (exp)
6895 && (!uninited_common || common_local_p));
6896 if (symtab_node *node = symtab_node::get (exp))
6898 if (node->in_other_partition)
6899 defined_locally = true;
6900 if (resolution_to_local_definition_p (node->resolution))
6901 defined_locally = resolved_locally = true;
6902 else if (resolution_local_p (node->resolution))
6903 resolved_locally = true;
6905 if (defined_locally && weak_dominate && !shlib)
6906 resolved_locally = true;
6908 /* Undefined weak symbols are never defined locally. */
6909 if (DECL_WEAK (exp) && !defined_locally)
6910 return false;
6912 /* A symbol is local if the user has said explicitly that it will be,
6913 or if we have a definition for the symbol. We cannot infer visibility
6914 for undefined symbols. */
6915 if (DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT
6916 && (TREE_CODE (exp) == FUNCTION_DECL
6917 || !extern_protected_data
6918 || DECL_VISIBILITY (exp) != VISIBILITY_PROTECTED)
6919 && (DECL_VISIBILITY_SPECIFIED (exp) || defined_locally))
6920 return true;
6922 /* If PIC, then assume that any global name can be overridden by
6923 symbols resolved from other modules. */
6924 if (shlib)
6925 return false;
6927 /* Variables defined outside this object might not be local. */
6928 if (DECL_EXTERNAL (exp) && !resolved_locally)
6929 return false;
6931 /* Non-dominant weak symbols are not defined locally. */
6932 if (DECL_WEAK (exp) && !resolved_locally)
6933 return false;
6935 /* Uninitialized COMMON variable may be unified with symbols
6936 resolved from other modules. */
6937 if (uninited_common && !resolved_locally)
6938 return false;
6940 /* Otherwise we're left with initialized (or non-common) global data
6941 which is of necessity defined locally. */
6942 return true;
6945 /* Assume ELF-ish defaults, since that's pretty much the most liberal
6946 wrt cross-module name binding. */
6948 bool
6949 default_binds_local_p (const_tree exp)
6951 return default_binds_local_p_3 (exp, flag_shlib != 0, true, false, false);
6954 /* Similar to default_binds_local_p, but common symbol may be local. */
6956 bool
6957 default_binds_local_p_2 (const_tree exp)
6959 return default_binds_local_p_3 (exp, flag_shlib != 0, true, false,
6960 !flag_pic);
6963 bool
6964 default_binds_local_p_1 (const_tree exp, int shlib)
6966 return default_binds_local_p_3 (exp, shlib != 0, false, false, false);
6969 /* Return true when references to DECL must bind to current definition in
6970 final executable.
6972 The condition is usually equivalent to whether the function binds to the
6973 current module (shared library or executable), that is to binds_local_p.
6974 We use this fact to avoid need for another target hook and implement
6975 the logic using binds_local_p and just special cases where
6976 decl_binds_to_current_def_p is stronger than binds_local_p. In particular
6977 the weak definitions (that can be overwritten at linktime by other
6978 definition from different object file) and when resolution info is available
6979 we simply use the knowledge passed to us by linker plugin. */
6980 bool
6981 decl_binds_to_current_def_p (const_tree decl)
6983 gcc_assert (DECL_P (decl));
6984 if (!targetm.binds_local_p (decl))
6985 return false;
6986 if (!TREE_PUBLIC (decl))
6987 return true;
6989 /* When resolution is available, just use it. */
6990 if (symtab_node *node = symtab_node::get (decl))
6992 if (node->resolution != LDPR_UNKNOWN)
6993 return resolution_to_local_definition_p (node->resolution);
6996 /* Otherwise we have to assume the worst for DECL_WEAK (hidden weaks
6997 binds locally but still can be overwritten), DECL_COMMON (can be merged
6998 with a non-common definition somewhere in the same module) or
6999 DECL_EXTERNAL.
7000 This rely on fact that binds_local_p behave as decl_replaceable_p
7001 for all other declaration types. */
7002 if (DECL_WEAK (decl))
7003 return false;
7004 if (DECL_COMMON (decl)
7005 && (DECL_INITIAL (decl) == NULL
7006 || (!in_lto_p && DECL_INITIAL (decl) == error_mark_node)))
7007 return false;
7008 if (DECL_EXTERNAL (decl))
7009 return false;
7010 return true;
7013 /* A replaceable function or variable is one which may be replaced
7014 at link-time with an entirely different definition, provided that the
7015 replacement has the same type. For example, functions declared
7016 with __attribute__((weak)) on most systems are replaceable.
7018 COMDAT functions are not replaceable, since all definitions of the
7019 function must be equivalent. It is important that COMDAT functions
7020 not be treated as replaceable so that use of C++ template
7021 instantiations is not penalized. */
7023 bool
7024 decl_replaceable_p (tree decl)
7026 gcc_assert (DECL_P (decl));
7027 if (!TREE_PUBLIC (decl) || DECL_COMDAT (decl))
7028 return false;
7029 if (!flag_semantic_interposition
7030 && !DECL_WEAK (decl))
7031 return false;
7032 return !decl_binds_to_current_def_p (decl);
7035 /* Default function to output code that will globalize a label. A
7036 target must define GLOBAL_ASM_OP or provide its own function to
7037 globalize a label. */
7038 #ifdef GLOBAL_ASM_OP
7039 void
7040 default_globalize_label (FILE * stream, const char *name)
7042 fputs (GLOBAL_ASM_OP, stream);
7043 assemble_name (stream, name);
7044 putc ('\n', stream);
7046 #endif /* GLOBAL_ASM_OP */
7048 /* Default function to output code that will globalize a declaration. */
7049 void
7050 default_globalize_decl_name (FILE * stream, tree decl)
7052 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
7053 targetm.asm_out.globalize_label (stream, name);
7056 /* Default function to output a label for unwind information. The
7057 default is to do nothing. A target that needs nonlocal labels for
7058 unwind information must provide its own function to do this. */
7059 void
7060 default_emit_unwind_label (FILE * stream ATTRIBUTE_UNUSED,
7061 tree decl ATTRIBUTE_UNUSED,
7062 int for_eh ATTRIBUTE_UNUSED,
7063 int empty ATTRIBUTE_UNUSED)
7067 /* Default function to output a label to divide up the exception table.
7068 The default is to do nothing. A target that needs/wants to divide
7069 up the table must provide it's own function to do this. */
7070 void
7071 default_emit_except_table_label (FILE * stream ATTRIBUTE_UNUSED)
7075 /* This is how to output an internal numbered label where PREFIX is
7076 the class of label and LABELNO is the number within the class. */
7078 void
7079 default_generate_internal_label (char *buf, const char *prefix,
7080 unsigned long labelno)
7082 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
7085 /* This is how to output an internal numbered label where PREFIX is
7086 the class of label and LABELNO is the number within the class. */
7088 void
7089 default_internal_label (FILE *stream, const char *prefix,
7090 unsigned long labelno)
7092 char *const buf = (char *) alloca (40 + strlen (prefix));
7093 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
7094 ASM_OUTPUT_INTERNAL_LABEL (stream, buf);
7098 /* The default implementation of ASM_DECLARE_CONSTANT_NAME. */
7100 void
7101 default_asm_declare_constant_name (FILE *file, const char *name,
7102 const_tree exp ATTRIBUTE_UNUSED,
7103 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
7105 assemble_label (file, name);
7108 /* This is the default behavior at the beginning of a file. It's
7109 controlled by two other target-hook toggles. */
7110 void
7111 default_file_start (void)
7113 if (targetm.asm_file_start_app_off
7114 && !(flag_verbose_asm || flag_debug_asm || flag_dump_rtl_in_asm))
7115 fputs (ASM_APP_OFF, asm_out_file);
7117 if (targetm.asm_file_start_file_directive)
7119 /* LTO produced units have no meaningful main_input_filename. */
7120 if (in_lto_p)
7121 output_file_directive (asm_out_file, "<artificial>");
7122 else
7123 output_file_directive (asm_out_file, main_input_filename);
7127 /* This is a generic routine suitable for use as TARGET_ASM_FILE_END
7128 which emits a special section directive used to indicate whether or
7129 not this object file needs an executable stack. This is primarily
7130 a GNU extension to ELF but could be used on other targets. */
7132 int trampolines_created;
7134 void
7135 file_end_indicate_exec_stack (void)
7137 unsigned int flags = SECTION_DEBUG;
7138 if (trampolines_created)
7139 flags |= SECTION_CODE;
7141 switch_to_section (get_section (".note.GNU-stack", flags, NULL));
7144 /* Emit a special section directive to indicate that this object file
7145 was compiled with -fsplit-stack. This is used to let the linker
7146 detect calls between split-stack code and non-split-stack code, so
7147 that it can modify the split-stack code to allocate a sufficiently
7148 large stack. We emit another special section if there are any
7149 functions in this file which have the no_split_stack attribute, to
7150 prevent the linker from warning about being unable to convert the
7151 functions if they call non-split-stack code. */
7153 void
7154 file_end_indicate_split_stack (void)
7156 if (flag_split_stack)
7158 switch_to_section (get_section (".note.GNU-split-stack", SECTION_DEBUG,
7159 NULL));
7160 if (saw_no_split_stack)
7161 switch_to_section (get_section (".note.GNU-no-split-stack",
7162 SECTION_DEBUG, NULL));
7166 /* Output DIRECTIVE (a C string) followed by a newline. This is used as
7167 a get_unnamed_section callback. */
7169 void
7170 output_section_asm_op (const void *directive)
7172 fprintf (asm_out_file, "%s\n", (const char *) directive);
7175 /* Emit assembly code to switch to section NEW_SECTION. Do nothing if
7176 the current section is NEW_SECTION. */
7178 void
7179 switch_to_section (section *new_section)
7181 if (in_section == new_section)
7182 return;
7184 if (new_section->common.flags & SECTION_FORGET)
7185 in_section = NULL;
7186 else
7187 in_section = new_section;
7189 switch (SECTION_STYLE (new_section))
7191 case SECTION_NAMED:
7192 targetm.asm_out.named_section (new_section->named.name,
7193 new_section->named.common.flags,
7194 new_section->named.decl);
7195 break;
7197 case SECTION_UNNAMED:
7198 new_section->unnamed.callback (new_section->unnamed.data);
7199 break;
7201 case SECTION_NOSWITCH:
7202 gcc_unreachable ();
7203 break;
7206 new_section->common.flags |= SECTION_DECLARED;
7209 /* If block symbol SYMBOL has not yet been assigned an offset, place
7210 it at the end of its block. */
7212 void
7213 place_block_symbol (rtx symbol)
7215 unsigned HOST_WIDE_INT size, mask, offset;
7216 struct constant_descriptor_rtx *desc;
7217 unsigned int alignment;
7218 struct object_block *block;
7219 tree decl;
7221 gcc_assert (SYMBOL_REF_BLOCK (symbol));
7222 if (SYMBOL_REF_BLOCK_OFFSET (symbol) >= 0)
7223 return;
7225 /* Work out the symbol's size and alignment. */
7226 if (CONSTANT_POOL_ADDRESS_P (symbol))
7228 desc = SYMBOL_REF_CONSTANT (symbol);
7229 alignment = desc->align;
7230 size = GET_MODE_SIZE (desc->mode);
7232 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
7234 decl = SYMBOL_REF_DECL (symbol);
7235 gcc_checking_assert (DECL_IN_CONSTANT_POOL (decl));
7236 alignment = DECL_ALIGN (decl);
7237 size = get_constant_size (DECL_INITIAL (decl));
7238 if ((flag_sanitize & SANITIZE_ADDRESS)
7239 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
7240 && asan_protect_global (DECL_INITIAL (decl)))
7241 size += asan_red_zone_size (size);
7243 else
7245 struct symtab_node *snode;
7246 decl = SYMBOL_REF_DECL (symbol);
7248 snode = symtab_node::get (decl);
7249 if (snode->alias)
7251 rtx target = DECL_RTL (snode->ultimate_alias_target ()->decl);
7253 gcc_assert (MEM_P (target)
7254 && GET_CODE (XEXP (target, 0)) == SYMBOL_REF
7255 && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (target, 0)));
7256 target = XEXP (target, 0);
7257 place_block_symbol (target);
7258 SYMBOL_REF_BLOCK_OFFSET (symbol) = SYMBOL_REF_BLOCK_OFFSET (target);
7259 return;
7261 alignment = get_variable_align (decl);
7262 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
7263 if ((flag_sanitize & SANITIZE_ADDRESS)
7264 && asan_protect_global (decl))
7266 size += asan_red_zone_size (size);
7267 alignment = MAX (alignment,
7268 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
7272 /* Calculate the object's offset from the start of the block. */
7273 block = SYMBOL_REF_BLOCK (symbol);
7274 mask = alignment / BITS_PER_UNIT - 1;
7275 offset = (block->size + mask) & ~mask;
7276 SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
7278 /* Record the block's new alignment and size. */
7279 block->alignment = MAX (block->alignment, alignment);
7280 block->size = offset + size;
7282 vec_safe_push (block->objects, symbol);
7285 /* Return the anchor that should be used to address byte offset OFFSET
7286 from the first object in BLOCK. MODEL is the TLS model used
7287 to access it. */
7290 get_section_anchor (struct object_block *block, HOST_WIDE_INT offset,
7291 enum tls_model model)
7293 char label[100];
7294 unsigned int begin, middle, end;
7295 unsigned HOST_WIDE_INT min_offset, max_offset, range, bias, delta;
7296 rtx anchor;
7298 /* Work out the anchor's offset. Use an offset of 0 for the first
7299 anchor so that we don't pessimize the case where we take the address
7300 of a variable at the beginning of the block. This is particularly
7301 useful when a block has only one variable assigned to it.
7303 We try to place anchors RANGE bytes apart, so there can then be
7304 anchors at +/-RANGE, +/-2 * RANGE, and so on, up to the limits of
7305 a ptr_mode offset. With some target settings, the lowest such
7306 anchor might be out of range for the lowest ptr_mode offset;
7307 likewise the highest anchor for the highest offset. Use anchors
7308 at the extreme ends of the ptr_mode range in such cases.
7310 All arithmetic uses unsigned integers in order to avoid
7311 signed overflow. */
7312 max_offset = (unsigned HOST_WIDE_INT) targetm.max_anchor_offset;
7313 min_offset = (unsigned HOST_WIDE_INT) targetm.min_anchor_offset;
7314 range = max_offset - min_offset + 1;
7315 if (range == 0)
7316 offset = 0;
7317 else
7319 bias = HOST_WIDE_INT_1U << (GET_MODE_BITSIZE (ptr_mode) - 1);
7320 if (offset < 0)
7322 delta = -(unsigned HOST_WIDE_INT) offset + max_offset;
7323 delta -= delta % range;
7324 if (delta > bias)
7325 delta = bias;
7326 offset = (HOST_WIDE_INT) (-delta);
7328 else
7330 delta = (unsigned HOST_WIDE_INT) offset - min_offset;
7331 delta -= delta % range;
7332 if (delta > bias - 1)
7333 delta = bias - 1;
7334 offset = (HOST_WIDE_INT) delta;
7338 /* Do a binary search to see if there's already an anchor we can use.
7339 Set BEGIN to the new anchor's index if not. */
7340 begin = 0;
7341 end = vec_safe_length (block->anchors);
7342 while (begin != end)
7344 middle = (end + begin) / 2;
7345 anchor = (*block->anchors)[middle];
7346 if (SYMBOL_REF_BLOCK_OFFSET (anchor) > offset)
7347 end = middle;
7348 else if (SYMBOL_REF_BLOCK_OFFSET (anchor) < offset)
7349 begin = middle + 1;
7350 else if (SYMBOL_REF_TLS_MODEL (anchor) > model)
7351 end = middle;
7352 else if (SYMBOL_REF_TLS_MODEL (anchor) < model)
7353 begin = middle + 1;
7354 else
7355 return anchor;
7358 /* Create a new anchor with a unique label. */
7359 ASM_GENERATE_INTERNAL_LABEL (label, "LANCHOR", anchor_labelno++);
7360 anchor = create_block_symbol (ggc_strdup (label), block, offset);
7361 SYMBOL_REF_FLAGS (anchor) |= SYMBOL_FLAG_LOCAL | SYMBOL_FLAG_ANCHOR;
7362 SYMBOL_REF_FLAGS (anchor) |= model << SYMBOL_FLAG_TLS_SHIFT;
7364 /* Insert it at index BEGIN. */
7365 vec_safe_insert (block->anchors, begin, anchor);
7366 return anchor;
7369 /* Output the objects in BLOCK. */
7371 static void
7372 output_object_block (struct object_block *block)
7374 struct constant_descriptor_rtx *desc;
7375 unsigned int i;
7376 HOST_WIDE_INT offset;
7377 tree decl;
7378 rtx symbol;
7380 if (!block->objects)
7381 return;
7383 /* Switch to the section and make sure that the first byte is
7384 suitably aligned. */
7385 switch_to_section (block->sect);
7386 assemble_align (block->alignment);
7388 /* Define the values of all anchors relative to the current section
7389 position. */
7390 FOR_EACH_VEC_SAFE_ELT (block->anchors, i, symbol)
7391 targetm.asm_out.output_anchor (symbol);
7393 /* Output the objects themselves. */
7394 offset = 0;
7395 FOR_EACH_VEC_ELT (*block->objects, i, symbol)
7397 /* Move to the object's offset, padding with zeros if necessary. */
7398 assemble_zeros (SYMBOL_REF_BLOCK_OFFSET (symbol) - offset);
7399 offset = SYMBOL_REF_BLOCK_OFFSET (symbol);
7400 if (CONSTANT_POOL_ADDRESS_P (symbol))
7402 desc = SYMBOL_REF_CONSTANT (symbol);
7403 output_constant_pool_1 (desc, 1);
7404 offset += GET_MODE_SIZE (desc->mode);
7406 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
7408 HOST_WIDE_INT size;
7409 decl = SYMBOL_REF_DECL (symbol);
7410 assemble_constant_contents
7411 (DECL_INITIAL (decl), XSTR (symbol, 0), DECL_ALIGN (decl));
7413 size = get_constant_size (DECL_INITIAL (decl));
7414 offset += size;
7415 if ((flag_sanitize & SANITIZE_ADDRESS)
7416 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
7417 && asan_protect_global (DECL_INITIAL (decl)))
7419 size = asan_red_zone_size (size);
7420 assemble_zeros (size);
7421 offset += size;
7424 else
7426 HOST_WIDE_INT size;
7427 decl = SYMBOL_REF_DECL (symbol);
7428 assemble_variable_contents (decl, XSTR (symbol, 0), false);
7429 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
7430 offset += size;
7431 if ((flag_sanitize & SANITIZE_ADDRESS)
7432 && asan_protect_global (decl))
7434 size = asan_red_zone_size (size);
7435 assemble_zeros (size);
7436 offset += size;
7442 /* A htab_traverse callback used to call output_object_block for
7443 each member of object_block_htab. */
7446 output_object_block_htab (object_block **slot, void *)
7448 output_object_block (*slot);
7449 return 1;
7452 /* Output the definitions of all object_blocks. */
7454 void
7455 output_object_blocks (void)
7457 object_block_htab->traverse<void *, output_object_block_htab> (NULL);
7460 /* This function provides a possible implementation of the
7461 TARGET_ASM_RECORD_GCC_SWITCHES target hook for ELF targets. When triggered
7462 by -frecord-gcc-switches it creates a new mergeable, string section in the
7463 assembler output file called TARGET_ASM_RECORD_GCC_SWITCHES_SECTION which
7464 contains the switches in ASCII format.
7466 FIXME: This code does not correctly handle double quote characters
7467 that appear inside strings, (it strips them rather than preserving them).
7468 FIXME: ASM_OUTPUT_ASCII, as defined in config/elfos.h will not emit NUL
7469 characters - instead it treats them as sub-string separators. Since
7470 we want to emit NUL strings terminators into the object file we have to use
7471 ASM_OUTPUT_SKIP. */
7474 elf_record_gcc_switches (print_switch_type type, const char * name)
7476 switch (type)
7478 case SWITCH_TYPE_PASSED:
7479 ASM_OUTPUT_ASCII (asm_out_file, name, strlen (name));
7480 ASM_OUTPUT_SKIP (asm_out_file, (unsigned HOST_WIDE_INT) 1);
7481 break;
7483 case SWITCH_TYPE_DESCRIPTIVE:
7484 if (name == NULL)
7486 /* Distinguish between invocations where name is NULL. */
7487 static bool started = false;
7489 if (!started)
7491 section * sec;
7493 sec = get_section (targetm.asm_out.record_gcc_switches_section,
7494 SECTION_DEBUG
7495 | SECTION_MERGE
7496 | SECTION_STRINGS
7497 | (SECTION_ENTSIZE & 1),
7498 NULL);
7499 switch_to_section (sec);
7500 started = true;
7504 default:
7505 break;
7508 /* The return value is currently ignored by the caller, but must be 0.
7509 For -fverbose-asm the return value would be the number of characters
7510 emitted into the assembler file. */
7511 return 0;
7514 /* Emit text to declare externally defined symbols. It is needed to
7515 properly support non-default visibility. */
7516 void
7517 default_elf_asm_output_external (FILE *file ATTRIBUTE_UNUSED,
7518 tree decl,
7519 const char *name ATTRIBUTE_UNUSED)
7521 /* We output the name if and only if TREE_SYMBOL_REFERENCED is
7522 set in order to avoid putting out names that are never really
7523 used. Always output visibility specified in the source. */
7524 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
7525 && (DECL_VISIBILITY_SPECIFIED (decl)
7526 || targetm.binds_local_p (decl)))
7527 maybe_assemble_visibility (decl);
7530 /* The default hook for TARGET_ASM_OUTPUT_SOURCE_FILENAME. */
7532 void
7533 default_asm_output_source_filename (FILE *file, const char *name)
7535 #ifdef ASM_OUTPUT_SOURCE_FILENAME
7536 ASM_OUTPUT_SOURCE_FILENAME (file, name);
7537 #else
7538 fprintf (file, "\t.file\t");
7539 output_quoted_string (file, name);
7540 putc ('\n', file);
7541 #endif
7544 /* Output a file name in the form wanted by System V. */
7546 void
7547 output_file_directive (FILE *asm_file, const char *input_name)
7549 int len;
7550 const char *na;
7552 if (input_name == NULL)
7553 input_name = "<stdin>";
7554 else
7555 input_name = remap_debug_filename (input_name);
7557 len = strlen (input_name);
7558 na = input_name + len;
7560 /* NA gets INPUT_NAME sans directory names. */
7561 while (na > input_name)
7563 if (IS_DIR_SEPARATOR (na[-1]))
7564 break;
7565 na--;
7568 targetm.asm_out.output_source_filename (asm_file, na);
7571 /* Create a DEBUG_EXPR_DECL / DEBUG_EXPR pair from RTL expression
7572 EXP. */
7574 make_debug_expr_from_rtl (const_rtx exp)
7576 tree ddecl = make_node (DEBUG_EXPR_DECL), type;
7577 machine_mode mode = GET_MODE (exp);
7578 rtx dval;
7580 DECL_ARTIFICIAL (ddecl) = 1;
7581 if (REG_P (exp) && REG_EXPR (exp))
7582 type = TREE_TYPE (REG_EXPR (exp));
7583 else if (MEM_P (exp) && MEM_EXPR (exp))
7584 type = TREE_TYPE (MEM_EXPR (exp));
7585 else
7586 type = NULL_TREE;
7587 if (type && TYPE_MODE (type) == mode)
7588 TREE_TYPE (ddecl) = type;
7589 else
7590 TREE_TYPE (ddecl) = lang_hooks.types.type_for_mode (mode, 1);
7591 DECL_MODE (ddecl) = mode;
7592 dval = gen_rtx_DEBUG_EXPR (mode);
7593 DEBUG_EXPR_TREE_DECL (dval) = ddecl;
7594 SET_DECL_RTL (ddecl, dval);
7595 return dval;
7598 #ifdef ELF_ASCII_ESCAPES
7599 /* Default ASM_OUTPUT_LIMITED_STRING for ELF targets. */
7601 void
7602 default_elf_asm_output_limited_string (FILE *f, const char *s)
7604 int escape;
7605 unsigned char c;
7607 fputs (STRING_ASM_OP, f);
7608 putc ('"', f);
7609 while (*s != '\0')
7611 c = *s;
7612 escape = ELF_ASCII_ESCAPES[c];
7613 switch (escape)
7615 case 0:
7616 putc (c, f);
7617 break;
7618 case 1:
7619 /* TODO: Print in hex with fast function, important for -flto. */
7620 fprintf (f, "\\%03o", c);
7621 break;
7622 default:
7623 putc ('\\', f);
7624 putc (escape, f);
7625 break;
7627 s++;
7629 putc ('\"', f);
7630 putc ('\n', f);
7633 /* Default ASM_OUTPUT_ASCII for ELF targets. */
7635 void
7636 default_elf_asm_output_ascii (FILE *f, const char *s, unsigned int len)
7638 const char *limit = s + len;
7639 const char *last_null = NULL;
7640 unsigned bytes_in_chunk = 0;
7641 unsigned char c;
7642 int escape;
7644 for (; s < limit; s++)
7646 const char *p;
7648 if (bytes_in_chunk >= 60)
7650 putc ('\"', f);
7651 putc ('\n', f);
7652 bytes_in_chunk = 0;
7655 if (s > last_null)
7657 for (p = s; p < limit && *p != '\0'; p++)
7658 continue;
7659 last_null = p;
7661 else
7662 p = last_null;
7664 if (p < limit && (p - s) <= (long) ELF_STRING_LIMIT)
7666 if (bytes_in_chunk > 0)
7668 putc ('\"', f);
7669 putc ('\n', f);
7670 bytes_in_chunk = 0;
7673 default_elf_asm_output_limited_string (f, s);
7674 s = p;
7676 else
7678 if (bytes_in_chunk == 0)
7679 fputs (ASCII_DATA_ASM_OP "\"", f);
7681 c = *s;
7682 escape = ELF_ASCII_ESCAPES[c];
7683 switch (escape)
7685 case 0:
7686 putc (c, f);
7687 bytes_in_chunk++;
7688 break;
7689 case 1:
7690 /* TODO: Print in hex with fast function, important for -flto. */
7691 fprintf (f, "\\%03o", c);
7692 bytes_in_chunk += 4;
7693 break;
7694 default:
7695 putc ('\\', f);
7696 putc (escape, f);
7697 bytes_in_chunk += 2;
7698 break;
7704 if (bytes_in_chunk > 0)
7706 putc ('\"', f);
7707 putc ('\n', f);
7710 #endif
7712 static GTY(()) section *elf_init_array_section;
7713 static GTY(()) section *elf_fini_array_section;
7715 static section *
7716 get_elf_initfini_array_priority_section (int priority,
7717 bool constructor_p)
7719 section *sec;
7720 if (priority != DEFAULT_INIT_PRIORITY)
7722 char buf[18];
7723 sprintf (buf, "%s.%.5u",
7724 constructor_p ? ".init_array" : ".fini_array",
7725 priority);
7726 sec = get_section (buf, SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
7728 else
7730 if (constructor_p)
7732 if (elf_init_array_section == NULL)
7733 elf_init_array_section
7734 = get_section (".init_array",
7735 SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
7736 sec = elf_init_array_section;
7738 else
7740 if (elf_fini_array_section == NULL)
7741 elf_fini_array_section
7742 = get_section (".fini_array",
7743 SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
7744 sec = elf_fini_array_section;
7747 return sec;
7750 /* Use .init_array section for constructors. */
7752 void
7753 default_elf_init_array_asm_out_constructor (rtx symbol, int priority)
7755 section *sec = get_elf_initfini_array_priority_section (priority,
7756 true);
7757 assemble_addr_to_section (symbol, sec);
7760 /* Use .fini_array section for destructors. */
7762 void
7763 default_elf_fini_array_asm_out_destructor (rtx symbol, int priority)
7765 section *sec = get_elf_initfini_array_priority_section (priority,
7766 false);
7767 assemble_addr_to_section (symbol, sec);
7770 /* Default TARGET_ASM_OUTPUT_IDENT hook.
7772 This is a bit of a cheat. The real default is a no-op, but this
7773 hook is the default for all targets with a .ident directive. */
7775 void
7776 default_asm_output_ident_directive (const char *ident_str)
7778 const char *ident_asm_op = "\t.ident\t";
7780 /* If we are still in the front end, do not write out the string
7781 to asm_out_file. Instead, add a fake top-level asm statement.
7782 This allows the front ends to use this hook without actually
7783 writing to asm_out_file, to handle #ident or Pragma Ident. */
7784 if (symtab->state == PARSING)
7786 char *buf = ACONCAT ((ident_asm_op, "\"", ident_str, "\"\n", NULL));
7787 symtab->finalize_toplevel_asm (build_string (strlen (buf), buf));
7789 else
7790 fprintf (asm_out_file, "%s\"%s\"\n", ident_asm_op, ident_str);
7793 #include "gt-varasm.h"