* include/parallel/numeric.h: Do not use default arguments in function
[official-gcc.git] / gcc / varasm.c
blobf83fc5174ce6d828849d7cc2cd633d469188c1de
1 /* Output variables, constants and external declarations, for GNU compiler.
2 Copyright (C) 1987-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
21 /* This file handles generation of all the assembler code
22 *except* the instructions of a function.
23 This includes declarations of variables and their initial values.
25 We also output the assembler code for constants stored in memory
26 and are responsible for combining constants with the same value. */
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tm.h"
32 #include "rtl.h"
33 #include "tree.h"
34 #include "stor-layout.h"
35 #include "stringpool.h"
36 #include "varasm.h"
37 #include "flags.h"
38 #include "hashtab.h"
39 #include "hash-set.h"
40 #include "vec.h"
41 #include "machmode.h"
42 #include "hard-reg-set.h"
43 #include "input.h"
44 #include "function.h"
45 #include "expr.h"
46 #include "regs.h"
47 #include "output.h"
48 #include "diagnostic-core.h"
49 #include "ggc.h"
50 #include "langhooks.h"
51 #include "tm_p.h"
52 #include "debug.h"
53 #include "target.h"
54 #include "common/common-target.h"
55 #include "targhooks.h"
56 #include "predict.h"
57 #include "dominance.h"
58 #include "cfg.h"
59 #include "basic-block.h"
60 #include "hash-map.h"
61 #include "is-a.h"
62 #include "plugin-api.h"
63 #include "ipa-ref.h"
64 #include "cgraph.h"
65 #include "asan.h"
66 #include "rtl-iter.h"
68 #ifdef XCOFF_DEBUGGING_INFO
69 #include "xcoffout.h" /* Needed for external data
70 declarations for e.g. AIX 4.x. */
71 #endif
73 /* The (assembler) name of the first globally-visible object output. */
74 extern GTY(()) const char *first_global_object_name;
75 extern GTY(()) const char *weak_global_object_name;
77 const char *first_global_object_name;
78 const char *weak_global_object_name;
80 struct addr_const;
81 struct constant_descriptor_rtx;
82 struct rtx_constant_pool;
84 #define n_deferred_constants (crtl->varasm.deferred_constants)
86 /* Number for making the label on the next
87 constant that is stored in memory. */
89 static GTY(()) int const_labelno;
91 /* Carry information from ASM_DECLARE_OBJECT_NAME
92 to ASM_FINISH_DECLARE_OBJECT. */
94 int size_directive_output;
96 /* The last decl for which assemble_variable was called,
97 if it did ASM_DECLARE_OBJECT_NAME.
98 If the last call to assemble_variable didn't do that,
99 this holds 0. */
101 tree last_assemble_variable_decl;
103 /* The following global variable indicates if the first basic block
104 in a function belongs to the cold partition or not. */
106 bool first_function_block_is_cold;
108 /* Whether we saw any functions with no_split_stack. */
110 static bool saw_no_split_stack;
112 static const char *strip_reg_name (const char *);
113 static int contains_pointers_p (tree);
114 #ifdef ASM_OUTPUT_EXTERNAL
115 static bool incorporeal_function_p (tree);
116 #endif
117 static void decode_addr_const (tree, struct addr_const *);
118 static hashval_t const_hash_1 (const tree);
119 static int compare_constant (const tree, const tree);
120 static void output_constant_def_contents (rtx);
121 static void output_addressed_constants (tree);
122 static unsigned HOST_WIDE_INT output_constant (tree, unsigned HOST_WIDE_INT,
123 unsigned int);
124 static void globalize_decl (tree);
125 static bool decl_readonly_section_1 (enum section_category);
126 #ifdef BSS_SECTION_ASM_OP
127 #ifdef ASM_OUTPUT_ALIGNED_BSS
128 static void asm_output_aligned_bss (FILE *, tree, const char *,
129 unsigned HOST_WIDE_INT, int)
130 ATTRIBUTE_UNUSED;
131 #endif
132 #endif /* BSS_SECTION_ASM_OP */
133 static void mark_weak (tree);
134 static void output_constant_pool (const char *, tree);
136 /* Well-known sections, each one associated with some sort of *_ASM_OP. */
137 section *text_section;
138 section *data_section;
139 section *readonly_data_section;
140 section *sdata_section;
141 section *ctors_section;
142 section *dtors_section;
143 section *bss_section;
144 section *sbss_section;
146 /* Various forms of common section. All are guaranteed to be nonnull. */
147 section *tls_comm_section;
148 section *comm_section;
149 section *lcomm_section;
151 /* A SECTION_NOSWITCH section used for declaring global BSS variables.
152 May be null. */
153 section *bss_noswitch_section;
155 /* The section that holds the main exception table, when known. The section
156 is set either by the target's init_sections hook or by the first call to
157 switch_to_exception_section. */
158 section *exception_section;
160 /* The section that holds the DWARF2 frame unwind information, when known.
161 The section is set either by the target's init_sections hook or by the
162 first call to switch_to_eh_frame_section. */
163 section *eh_frame_section;
165 /* asm_out_file's current section. This is NULL if no section has yet
166 been selected or if we lose track of what the current section is. */
167 section *in_section;
169 /* True if code for the current function is currently being directed
170 at the cold section. */
171 bool in_cold_section_p;
173 /* A linked list of all the unnamed sections. */
174 static GTY(()) section *unnamed_sections;
176 /* Return a nonzero value if DECL has a section attribute. */
177 #define IN_NAMED_SECTION(DECL) \
178 ((TREE_CODE (DECL) == FUNCTION_DECL || TREE_CODE (DECL) == VAR_DECL) \
179 && DECL_SECTION_NAME (DECL) != NULL)
181 struct section_hasher : ggc_hasher<section *>
183 typedef const char *compare_type;
185 static hashval_t hash (section *);
186 static bool equal (section *, const char *);
189 /* Hash table of named sections. */
190 static GTY(()) hash_table<section_hasher> *section_htab;
192 struct object_block_hasher : ggc_hasher<object_block *>
194 typedef const section *compare_type;
196 static hashval_t hash (object_block *);
197 static bool equal (object_block *, const section *);
200 /* A table of object_blocks, indexed by section. */
201 static GTY(()) hash_table<object_block_hasher> *object_block_htab;
203 /* The next number to use for internal anchor labels. */
204 static GTY(()) int anchor_labelno;
206 /* A pool of constants that can be shared between functions. */
207 static GTY(()) struct rtx_constant_pool *shared_constant_pool;
209 /* Helper routines for maintaining section_htab. */
211 bool
212 section_hasher::equal (section *old, const char *new_name)
214 return strcmp (old->named.name, new_name) == 0;
217 hashval_t
218 section_hasher::hash (section *old)
220 return htab_hash_string (old->named.name);
223 /* Return a hash value for section SECT. */
225 static hashval_t
226 hash_section (section *sect)
228 if (sect->common.flags & SECTION_NAMED)
229 return htab_hash_string (sect->named.name);
230 return sect->common.flags;
233 /* Helper routines for maintaining object_block_htab. */
235 inline bool
236 object_block_hasher::equal (object_block *old, const section *new_section)
238 return old->sect == new_section;
241 hashval_t
242 object_block_hasher::hash (object_block *old)
244 return hash_section (old->sect);
247 /* Return a new unnamed section with the given fields. */
249 section *
250 get_unnamed_section (unsigned int flags, void (*callback) (const void *),
251 const void *data)
253 section *sect;
255 sect = ggc_alloc<section> ();
256 sect->unnamed.common.flags = flags | SECTION_UNNAMED;
257 sect->unnamed.callback = callback;
258 sect->unnamed.data = data;
259 sect->unnamed.next = unnamed_sections;
261 unnamed_sections = sect;
262 return sect;
265 /* Return a SECTION_NOSWITCH section with the given fields. */
267 static section *
268 get_noswitch_section (unsigned int flags, noswitch_section_callback callback)
270 section *sect;
272 sect = ggc_alloc<section> ();
273 sect->noswitch.common.flags = flags | SECTION_NOSWITCH;
274 sect->noswitch.callback = callback;
276 return sect;
279 /* Return the named section structure associated with NAME. Create
280 a new section with the given fields if no such structure exists. */
282 section *
283 get_section (const char *name, unsigned int flags, tree decl)
285 section *sect, **slot;
287 slot = section_htab->find_slot_with_hash (name, htab_hash_string (name),
288 INSERT);
289 flags |= SECTION_NAMED;
290 if (*slot == NULL)
292 sect = ggc_alloc<section> ();
293 sect->named.common.flags = flags;
294 sect->named.name = ggc_strdup (name);
295 sect->named.decl = decl;
296 *slot = sect;
298 else
300 sect = *slot;
301 if ((sect->common.flags & ~SECTION_DECLARED) != flags
302 && ((sect->common.flags | flags) & SECTION_OVERRIDE) == 0)
304 /* It is fine if one of the section flags is
305 SECTION_WRITE | SECTION_RELRO and the other has none of these
306 flags (i.e. read-only) in named sections and either the
307 section hasn't been declared yet or has been declared as writable.
308 In that case just make sure the resulting flags are
309 SECTION_WRITE | SECTION_RELRO, ie. writable only because of
310 relocations. */
311 if (((sect->common.flags ^ flags) & (SECTION_WRITE | SECTION_RELRO))
312 == (SECTION_WRITE | SECTION_RELRO)
313 && (sect->common.flags
314 & ~(SECTION_DECLARED | SECTION_WRITE | SECTION_RELRO))
315 == (flags & ~(SECTION_WRITE | SECTION_RELRO))
316 && ((sect->common.flags & SECTION_DECLARED) == 0
317 || (sect->common.flags & SECTION_WRITE)))
319 sect->common.flags |= (SECTION_WRITE | SECTION_RELRO);
320 return sect;
322 /* Sanity check user variables for flag changes. */
323 if (sect->named.decl != NULL
324 && DECL_P (sect->named.decl)
325 && decl != sect->named.decl)
327 if (decl != NULL && DECL_P (decl))
328 error ("%+D causes a section type conflict with %D",
329 decl, sect->named.decl);
330 else
331 error ("section type conflict with %D", sect->named.decl);
332 inform (DECL_SOURCE_LOCATION (sect->named.decl),
333 "%qD was declared here", sect->named.decl);
335 else if (decl != NULL && DECL_P (decl))
336 error ("%+D causes a section type conflict", decl);
337 else
338 error ("section type conflict");
339 /* Make sure we don't error about one section multiple times. */
340 sect->common.flags |= SECTION_OVERRIDE;
343 return sect;
346 /* Return true if the current compilation mode benefits from having
347 objects grouped into blocks. */
349 static bool
350 use_object_blocks_p (void)
352 return flag_section_anchors;
355 /* Return the object_block structure for section SECT. Create a new
356 structure if we haven't created one already. Return null if SECT
357 itself is null. */
359 static struct object_block *
360 get_block_for_section (section *sect)
362 struct object_block *block;
364 if (sect == NULL)
365 return NULL;
367 object_block **slot
368 = object_block_htab->find_slot_with_hash (sect, hash_section (sect),
369 INSERT);
370 block = *slot;
371 if (block == NULL)
373 block = ggc_cleared_alloc<object_block> ();
374 block->sect = sect;
375 *slot = block;
377 return block;
380 /* Create a symbol with label LABEL and place it at byte offset
381 OFFSET in BLOCK. OFFSET can be negative if the symbol's offset
382 is not yet known. LABEL must be a garbage-collected string. */
384 static rtx
385 create_block_symbol (const char *label, struct object_block *block,
386 HOST_WIDE_INT offset)
388 rtx symbol;
389 unsigned int size;
391 /* Create the extended SYMBOL_REF. */
392 size = RTX_HDR_SIZE + sizeof (struct block_symbol);
393 symbol = (rtx) ggc_internal_alloc (size);
395 /* Initialize the normal SYMBOL_REF fields. */
396 memset (symbol, 0, size);
397 PUT_CODE (symbol, SYMBOL_REF);
398 PUT_MODE (symbol, Pmode);
399 XSTR (symbol, 0) = label;
400 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_HAS_BLOCK_INFO;
402 /* Initialize the block_symbol stuff. */
403 SYMBOL_REF_BLOCK (symbol) = block;
404 SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
406 return symbol;
409 /* Return a section with a particular name and with whatever SECTION_*
410 flags section_type_flags deems appropriate. The name of the section
411 is taken from NAME if nonnull, otherwise it is taken from DECL's
412 DECL_SECTION_NAME. DECL is the decl associated with the section
413 (see the section comment for details) and RELOC is as for
414 section_type_flags. */
416 section *
417 get_named_section (tree decl, const char *name, int reloc)
419 unsigned int flags;
421 if (name == NULL)
423 gcc_assert (decl && DECL_P (decl) && DECL_SECTION_NAME (decl));
424 name = DECL_SECTION_NAME (decl);
427 flags = targetm.section_type_flags (decl, name, reloc);
428 return get_section (name, flags, decl);
431 /* Worker for resolve_unique_section. */
433 static bool
434 set_implicit_section (struct symtab_node *n, void *data ATTRIBUTE_UNUSED)
436 n->implicit_section = true;
437 return false;
440 /* If required, set DECL_SECTION_NAME to a unique name. */
442 void
443 resolve_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED,
444 int flag_function_or_data_sections)
446 if (DECL_SECTION_NAME (decl) == NULL
447 && targetm_common.have_named_sections
448 && (flag_function_or_data_sections
449 || DECL_COMDAT_GROUP (decl)))
451 targetm.asm_out.unique_section (decl, reloc);
452 if (DECL_SECTION_NAME (decl))
453 symtab_node::get (decl)->call_for_symbol_and_aliases
454 (set_implicit_section, NULL, true);
458 #ifdef BSS_SECTION_ASM_OP
460 #ifdef ASM_OUTPUT_ALIGNED_BSS
462 /* Utility function for targets to use in implementing
463 ASM_OUTPUT_ALIGNED_BSS.
464 ??? It is believed that this function will work in most cases so such
465 support is localized here. */
467 static void
468 asm_output_aligned_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
469 const char *name, unsigned HOST_WIDE_INT size,
470 int align)
472 switch_to_section (bss_section);
473 ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
474 #ifdef ASM_DECLARE_OBJECT_NAME
475 last_assemble_variable_decl = decl;
476 ASM_DECLARE_OBJECT_NAME (file, name, decl);
477 #else
478 /* Standard thing is just output label for the object. */
479 ASM_OUTPUT_LABEL (file, name);
480 #endif /* ASM_DECLARE_OBJECT_NAME */
481 ASM_OUTPUT_SKIP (file, size ? size : 1);
484 #endif
486 #endif /* BSS_SECTION_ASM_OP */
488 #ifndef USE_SELECT_SECTION_FOR_FUNCTIONS
489 /* Return the hot section for function DECL. Return text_section for
490 null DECLs. */
492 static section *
493 hot_function_section (tree decl)
495 if (decl != NULL_TREE
496 && DECL_SECTION_NAME (decl) != NULL
497 && targetm_common.have_named_sections)
498 return get_named_section (decl, NULL, 0);
499 else
500 return text_section;
502 #endif
504 /* Return section for TEXT_SECTION_NAME if DECL or DECL_SECTION_NAME (DECL)
505 is NULL.
507 When DECL_SECTION_NAME is non-NULL and it is implicit section and
508 NAMED_SECTION_SUFFIX is non-NULL, then produce section called
509 concatenate the name with NAMED_SECTION_SUFFIX.
510 Otherwise produce "TEXT_SECTION_NAME.IMPLICIT_NAME". */
512 section *
513 get_named_text_section (tree decl,
514 const char *text_section_name,
515 const char *named_section_suffix)
517 if (decl && DECL_SECTION_NAME (decl))
519 if (named_section_suffix)
521 const char *dsn = DECL_SECTION_NAME (decl);
522 const char *stripped_name;
523 char *name, *buffer;
525 name = (char *) alloca (strlen (dsn) + 1);
526 memcpy (name, dsn,
527 strlen (dsn) + 1);
529 stripped_name = targetm.strip_name_encoding (name);
531 buffer = ACONCAT ((stripped_name, named_section_suffix, NULL));
532 return get_named_section (decl, buffer, 0);
534 else if (symtab_node::get (decl)->implicit_section)
536 const char *name;
538 /* Do not try to split gnu_linkonce functions. This gets somewhat
539 slipperly. */
540 if (DECL_COMDAT_GROUP (decl) && !HAVE_COMDAT_GROUP)
541 return NULL;
542 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
543 name = targetm.strip_name_encoding (name);
544 return get_named_section (decl, ACONCAT ((text_section_name, ".",
545 name, NULL)), 0);
547 else
548 return NULL;
550 return get_named_section (decl, text_section_name, 0);
553 /* Choose named function section based on its frequency. */
555 section *
556 default_function_section (tree decl, enum node_frequency freq,
557 bool startup, bool exit)
559 #if defined HAVE_LD_EH_GC_SECTIONS && defined HAVE_LD_EH_GC_SECTIONS_BUG
560 /* Old GNU linkers have buggy --gc-section support, which sometimes
561 results in .gcc_except_table* sections being garbage collected. */
562 if (decl
563 && symtab_node::get (decl)->implicit_section)
564 return NULL;
565 #endif
567 if (!flag_reorder_functions
568 || !targetm_common.have_named_sections)
569 return NULL;
570 /* Startup code should go to startup subsection unless it is
571 unlikely executed (this happens especially with function splitting
572 where we can split away unnecessary parts of static constructors. */
573 if (startup && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
575 /* If we do have a profile or(and) LTO phase is executed, we do not need
576 these ELF section. */
577 if (!in_lto_p || !flag_profile_values)
578 return get_named_text_section (decl, ".text.startup", NULL);
579 else
580 return NULL;
583 /* Similarly for exit. */
584 if (exit && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
585 return get_named_text_section (decl, ".text.exit", NULL);
587 /* Group cold functions together, similarly for hot code. */
588 switch (freq)
590 case NODE_FREQUENCY_UNLIKELY_EXECUTED:
591 return get_named_text_section (decl, ".text.unlikely", NULL);
592 case NODE_FREQUENCY_HOT:
593 /* If we do have a profile or(and) LTO phase is executed, we do not need
594 these ELF section. */
595 if (!in_lto_p || !flag_profile_values)
596 return get_named_text_section (decl, ".text.hot", NULL);
597 default:
598 return NULL;
602 /* Return the section for function DECL.
604 If DECL is NULL_TREE, return the text section. We can be passed
605 NULL_TREE under some circumstances by dbxout.c at least.
607 If FORCE_COLD is true, return cold function section ignoring
608 the frequency info of cgraph_node. */
610 static section *
611 function_section_1 (tree decl, bool force_cold)
613 section *section = NULL;
614 enum node_frequency freq = NODE_FREQUENCY_NORMAL;
615 bool startup = false, exit = false;
617 if (decl)
619 struct cgraph_node *node = cgraph_node::get (decl);
621 if (node)
623 freq = node->frequency;
624 startup = node->only_called_at_startup;
625 exit = node->only_called_at_exit;
628 if (force_cold)
629 freq = NODE_FREQUENCY_UNLIKELY_EXECUTED;
631 #ifdef USE_SELECT_SECTION_FOR_FUNCTIONS
632 if (decl != NULL_TREE
633 && DECL_SECTION_NAME (decl) != NULL)
635 if (targetm.asm_out.function_section)
636 section = targetm.asm_out.function_section (decl, freq,
637 startup, exit);
638 if (section)
639 return section;
640 return get_named_section (decl, NULL, 0);
642 else
643 return targetm.asm_out.select_section
644 (decl, freq == NODE_FREQUENCY_UNLIKELY_EXECUTED,
645 DECL_ALIGN (decl));
646 #else
647 if (targetm.asm_out.function_section)
648 section = targetm.asm_out.function_section (decl, freq, startup, exit);
649 if (section)
650 return section;
651 return hot_function_section (decl);
652 #endif
655 /* Return the section for function DECL.
657 If DECL is NULL_TREE, return the text section. We can be passed
658 NULL_TREE under some circumstances by dbxout.c at least. */
660 section *
661 function_section (tree decl)
663 /* Handle cases where function splitting code decides
664 to put function entry point into unlikely executed section
665 despite the fact that the function itself is not cold
666 (i.e. it is called rarely but contains a hot loop that is
667 better to live in hot subsection for the code locality). */
668 return function_section_1 (decl,
669 first_function_block_is_cold);
672 /* Return the section for the current function, take IN_COLD_SECTION_P
673 into account. */
675 section *
676 current_function_section (void)
678 return function_section_1 (current_function_decl, in_cold_section_p);
681 /* Tell assembler to switch to unlikely-to-be-executed text section. */
683 section *
684 unlikely_text_section (void)
686 return function_section_1 (current_function_decl, true);
689 /* When called within a function context, return true if the function
690 has been assigned a cold text section and if SECT is that section.
691 When called outside a function context, return true if SECT is the
692 default cold section. */
694 bool
695 unlikely_text_section_p (section *sect)
697 return sect == function_section_1 (current_function_decl, true);
700 /* Return the read-only data section associated with function DECL. */
702 section *
703 default_function_rodata_section (tree decl)
705 if (decl != NULL_TREE && DECL_SECTION_NAME (decl))
707 const char *name = DECL_SECTION_NAME (decl);
709 if (DECL_COMDAT_GROUP (decl) && HAVE_COMDAT_GROUP)
711 const char *dot;
712 size_t len;
713 char* rname;
715 dot = strchr (name + 1, '.');
716 if (!dot)
717 dot = name;
718 len = strlen (dot) + 8;
719 rname = (char *) alloca (len);
721 strcpy (rname, ".rodata");
722 strcat (rname, dot);
723 return get_section (rname, SECTION_LINKONCE, decl);
725 /* For .gnu.linkonce.t.foo we want to use .gnu.linkonce.r.foo. */
726 else if (DECL_COMDAT_GROUP (decl)
727 && strncmp (name, ".gnu.linkonce.t.", 16) == 0)
729 size_t len = strlen (name) + 1;
730 char *rname = (char *) alloca (len);
732 memcpy (rname, name, len);
733 rname[14] = 'r';
734 return get_section (rname, SECTION_LINKONCE, decl);
736 /* For .text.foo we want to use .rodata.foo. */
737 else if (flag_function_sections && flag_data_sections
738 && strncmp (name, ".text.", 6) == 0)
740 size_t len = strlen (name) + 1;
741 char *rname = (char *) alloca (len + 2);
743 memcpy (rname, ".rodata", 7);
744 memcpy (rname + 7, name + 5, len - 5);
745 return get_section (rname, 0, decl);
749 return readonly_data_section;
752 /* Return the read-only data section associated with function DECL
753 for targets where that section should be always the single
754 readonly data section. */
756 section *
757 default_no_function_rodata_section (tree decl ATTRIBUTE_UNUSED)
759 return readonly_data_section;
762 /* Return the section to use for string merging. */
764 static section *
765 mergeable_string_section (tree decl ATTRIBUTE_UNUSED,
766 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
767 unsigned int flags ATTRIBUTE_UNUSED)
769 HOST_WIDE_INT len;
771 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
772 && TREE_CODE (decl) == STRING_CST
773 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
774 && align <= 256
775 && (len = int_size_in_bytes (TREE_TYPE (decl))) > 0
776 && TREE_STRING_LENGTH (decl) >= len)
778 machine_mode mode;
779 unsigned int modesize;
780 const char *str;
781 HOST_WIDE_INT i;
782 int j, unit;
783 const char *prefix = targetm.asm_out.mergeable_rodata_prefix;
784 char *name = (char *) alloca (strlen (prefix) + 30);
786 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (decl)));
787 modesize = GET_MODE_BITSIZE (mode);
788 if (modesize >= 8 && modesize <= 256
789 && (modesize & (modesize - 1)) == 0)
791 if (align < modesize)
792 align = modesize;
794 str = TREE_STRING_POINTER (decl);
795 unit = GET_MODE_SIZE (mode);
797 /* Check for embedded NUL characters. */
798 for (i = 0; i < len; i += unit)
800 for (j = 0; j < unit; j++)
801 if (str[i + j] != '\0')
802 break;
803 if (j == unit)
804 break;
806 if (i == len - unit)
808 sprintf (name, "%s.str%d.%d", prefix,
809 modesize / 8, (int) (align / 8));
810 flags |= (modesize / 8) | SECTION_MERGE | SECTION_STRINGS;
811 return get_section (name, flags, NULL);
816 return readonly_data_section;
819 /* Return the section to use for constant merging. */
821 section *
822 mergeable_constant_section (machine_mode mode ATTRIBUTE_UNUSED,
823 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
824 unsigned int flags ATTRIBUTE_UNUSED)
826 unsigned int modesize = GET_MODE_BITSIZE (mode);
828 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
829 && mode != VOIDmode
830 && mode != BLKmode
831 && modesize <= align
832 && align >= 8
833 && align <= 256
834 && (align & (align - 1)) == 0)
836 const char *prefix = targetm.asm_out.mergeable_rodata_prefix;
837 char *name = (char *) alloca (strlen (prefix) + 30);
839 sprintf (name, "%s.cst%d", prefix, (int) (align / 8));
840 flags |= (align / 8) | SECTION_MERGE;
841 return get_section (name, flags, NULL);
843 return readonly_data_section;
846 /* Given NAME, a putative register name, discard any customary prefixes. */
848 static const char *
849 strip_reg_name (const char *name)
851 #ifdef REGISTER_PREFIX
852 if (!strncmp (name, REGISTER_PREFIX, strlen (REGISTER_PREFIX)))
853 name += strlen (REGISTER_PREFIX);
854 #endif
855 if (name[0] == '%' || name[0] == '#')
856 name++;
857 return name;
860 /* The user has asked for a DECL to have a particular name. Set (or
861 change) it in such a way that we don't prefix an underscore to
862 it. */
863 void
864 set_user_assembler_name (tree decl, const char *name)
866 char *starred = (char *) alloca (strlen (name) + 2);
867 starred[0] = '*';
868 strcpy (starred + 1, name);
869 symtab->change_decl_assembler_name (decl, get_identifier (starred));
870 SET_DECL_RTL (decl, NULL_RTX);
873 /* Decode an `asm' spec for a declaration as a register name.
874 Return the register number, or -1 if nothing specified,
875 or -2 if the ASMSPEC is not `cc' or `memory' and is not recognized,
876 or -3 if ASMSPEC is `cc' and is not recognized,
877 or -4 if ASMSPEC is `memory' and is not recognized.
878 Accept an exact spelling or a decimal number.
879 Prefixes such as % are optional. */
882 decode_reg_name_and_count (const char *asmspec, int *pnregs)
884 /* Presume just one register is clobbered. */
885 *pnregs = 1;
887 if (asmspec != 0)
889 int i;
891 /* Get rid of confusing prefixes. */
892 asmspec = strip_reg_name (asmspec);
894 /* Allow a decimal number as a "register name". */
895 for (i = strlen (asmspec) - 1; i >= 0; i--)
896 if (! ISDIGIT (asmspec[i]))
897 break;
898 if (asmspec[0] != 0 && i < 0)
900 i = atoi (asmspec);
901 if (i < FIRST_PSEUDO_REGISTER && i >= 0 && reg_names[i][0])
902 return i;
903 else
904 return -2;
907 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
908 if (reg_names[i][0]
909 && ! strcmp (asmspec, strip_reg_name (reg_names[i])))
910 return i;
912 #ifdef OVERLAPPING_REGISTER_NAMES
914 static const struct
916 const char *const name;
917 const int number;
918 const int nregs;
919 } table[] = OVERLAPPING_REGISTER_NAMES;
921 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
922 if (table[i].name[0]
923 && ! strcmp (asmspec, table[i].name))
925 *pnregs = table[i].nregs;
926 return table[i].number;
929 #endif /* OVERLAPPING_REGISTER_NAMES */
931 #ifdef ADDITIONAL_REGISTER_NAMES
933 static const struct { const char *const name; const int number; } table[]
934 = ADDITIONAL_REGISTER_NAMES;
936 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
937 if (table[i].name[0]
938 && ! strcmp (asmspec, table[i].name)
939 && reg_names[table[i].number][0])
940 return table[i].number;
942 #endif /* ADDITIONAL_REGISTER_NAMES */
944 if (!strcmp (asmspec, "memory"))
945 return -4;
947 if (!strcmp (asmspec, "cc"))
948 return -3;
950 return -2;
953 return -1;
957 decode_reg_name (const char *name)
959 int count;
960 return decode_reg_name_and_count (name, &count);
964 /* Return true if DECL's initializer is suitable for a BSS section. */
966 bool
967 bss_initializer_p (const_tree decl)
969 return (DECL_INITIAL (decl) == NULL
970 /* In LTO we have no errors in program; error_mark_node is used
971 to mark offlined constructors. */
972 || (DECL_INITIAL (decl) == error_mark_node
973 && !in_lto_p)
974 || (flag_zero_initialized_in_bss
975 /* Leave constant zeroes in .rodata so they
976 can be shared. */
977 && !TREE_READONLY (decl)
978 && initializer_zerop (DECL_INITIAL (decl))));
981 /* Compute the alignment of variable specified by DECL.
982 DONT_OUTPUT_DATA is from assemble_variable. */
984 void
985 align_variable (tree decl, bool dont_output_data)
987 unsigned int align = DECL_ALIGN (decl);
989 /* In the case for initialing an array whose length isn't specified,
990 where we have not yet been able to do the layout,
991 figure out the proper alignment now. */
992 if (dont_output_data && DECL_SIZE (decl) == 0
993 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
994 align = MAX (align, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (decl))));
996 /* Some object file formats have a maximum alignment which they support.
997 In particular, a.out format supports a maximum alignment of 4. */
998 if (align > MAX_OFILE_ALIGNMENT)
1000 error ("alignment of %q+D is greater than maximum object "
1001 "file alignment %d", decl,
1002 MAX_OFILE_ALIGNMENT/BITS_PER_UNIT);
1003 align = MAX_OFILE_ALIGNMENT;
1006 if (! DECL_USER_ALIGN (decl))
1008 #ifdef DATA_ABI_ALIGNMENT
1009 unsigned int data_abi_align
1010 = DATA_ABI_ALIGNMENT (TREE_TYPE (decl), align);
1011 /* For backwards compatibility, don't assume the ABI alignment for
1012 TLS variables. */
1013 if (! DECL_THREAD_LOCAL_P (decl) || data_abi_align <= BITS_PER_WORD)
1014 align = data_abi_align;
1015 #endif
1017 /* On some machines, it is good to increase alignment sometimes.
1018 But as DECL_ALIGN is used both for actually emitting the variable
1019 and for code accessing the variable as guaranteed alignment, we
1020 can only increase the alignment if it is a performance optimization
1021 if the references to it must bind to the current definition. */
1022 if (decl_binds_to_current_def_p (decl)
1023 && !DECL_VIRTUAL_P (decl))
1025 #ifdef DATA_ALIGNMENT
1026 unsigned int data_align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
1027 /* Don't increase alignment too much for TLS variables - TLS space
1028 is too precious. */
1029 if (! DECL_THREAD_LOCAL_P (decl) || data_align <= BITS_PER_WORD)
1030 align = data_align;
1031 #endif
1032 #ifdef CONSTANT_ALIGNMENT
1033 if (DECL_INITIAL (decl) != 0
1034 /* In LTO we have no errors in program; error_mark_node is used
1035 to mark offlined constructors. */
1036 && (in_lto_p || DECL_INITIAL (decl) != error_mark_node))
1038 unsigned int const_align
1039 = CONSTANT_ALIGNMENT (DECL_INITIAL (decl), align);
1040 /* Don't increase alignment too much for TLS variables - TLS
1041 space is too precious. */
1042 if (! DECL_THREAD_LOCAL_P (decl) || const_align <= BITS_PER_WORD)
1043 align = const_align;
1045 #endif
1049 /* Reset the alignment in case we have made it tighter, so we can benefit
1050 from it in get_pointer_alignment. */
1051 DECL_ALIGN (decl) = align;
1054 /* Return DECL_ALIGN (decl), possibly increased for optimization purposes
1055 beyond what align_variable returned. */
1057 static unsigned int
1058 get_variable_align (tree decl)
1060 unsigned int align = DECL_ALIGN (decl);
1062 /* For user aligned vars or static vars align_variable already did
1063 everything. */
1064 if (DECL_USER_ALIGN (decl) || !TREE_PUBLIC (decl))
1065 return align;
1067 #ifdef DATA_ABI_ALIGNMENT
1068 if (DECL_THREAD_LOCAL_P (decl))
1069 align = DATA_ABI_ALIGNMENT (TREE_TYPE (decl), align);
1070 #endif
1072 /* For decls that bind to the current definition, align_variable
1073 did also everything, except for not assuming ABI required alignment
1074 of TLS variables. For other vars, increase the alignment here
1075 as an optimization. */
1076 if (!decl_binds_to_current_def_p (decl))
1078 /* On some machines, it is good to increase alignment sometimes. */
1079 #ifdef DATA_ALIGNMENT
1080 unsigned int data_align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
1081 /* Don't increase alignment too much for TLS variables - TLS space
1082 is too precious. */
1083 if (! DECL_THREAD_LOCAL_P (decl) || data_align <= BITS_PER_WORD)
1084 align = data_align;
1085 #endif
1086 #ifdef CONSTANT_ALIGNMENT
1087 if (DECL_INITIAL (decl) != 0
1088 /* In LTO we have no errors in program; error_mark_node is used
1089 to mark offlined constructors. */
1090 && (in_lto_p || DECL_INITIAL (decl) != error_mark_node))
1092 unsigned int const_align = CONSTANT_ALIGNMENT (DECL_INITIAL (decl),
1093 align);
1094 /* Don't increase alignment too much for TLS variables - TLS space
1095 is too precious. */
1096 if (! DECL_THREAD_LOCAL_P (decl) || const_align <= BITS_PER_WORD)
1097 align = const_align;
1099 #endif
1102 return align;
1105 /* Return the section into which the given VAR_DECL or CONST_DECL
1106 should be placed. PREFER_NOSWITCH_P is true if a noswitch
1107 section should be used wherever possible. */
1109 section *
1110 get_variable_section (tree decl, bool prefer_noswitch_p)
1112 addr_space_t as = ADDR_SPACE_GENERIC;
1113 int reloc;
1114 varpool_node *vnode = varpool_node::get (decl);
1115 if (vnode)
1117 vnode = vnode->ultimate_alias_target ();
1118 decl = vnode->decl;
1121 if (TREE_TYPE (decl) != error_mark_node)
1122 as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
1124 /* We need the constructor to figure out reloc flag. */
1125 if (vnode)
1126 vnode->get_constructor ();
1128 if (DECL_COMMON (decl))
1130 /* If the decl has been given an explicit section name, or it resides
1131 in a non-generic address space, then it isn't common, and shouldn't
1132 be handled as such. */
1133 gcc_assert (DECL_SECTION_NAME (decl) == NULL
1134 && ADDR_SPACE_GENERIC_P (as));
1135 if (DECL_THREAD_LOCAL_P (decl))
1136 return tls_comm_section;
1137 else if (TREE_PUBLIC (decl) && bss_initializer_p (decl))
1138 return comm_section;
1141 if (DECL_INITIAL (decl) == error_mark_node)
1142 reloc = contains_pointers_p (TREE_TYPE (decl)) ? 3 : 0;
1143 else if (DECL_INITIAL (decl))
1144 reloc = compute_reloc_for_constant (DECL_INITIAL (decl));
1145 else
1146 reloc = 0;
1148 resolve_unique_section (decl, reloc, flag_data_sections);
1149 if (IN_NAMED_SECTION (decl))
1150 return get_named_section (decl, NULL, reloc);
1152 if (ADDR_SPACE_GENERIC_P (as)
1153 && !DECL_THREAD_LOCAL_P (decl)
1154 && !(prefer_noswitch_p && targetm.have_switchable_bss_sections)
1155 && bss_initializer_p (decl))
1157 if (!TREE_PUBLIC (decl)
1158 && !((flag_sanitize & SANITIZE_ADDRESS)
1159 && asan_protect_global (decl)))
1160 return lcomm_section;
1161 if (bss_noswitch_section)
1162 return bss_noswitch_section;
1165 return targetm.asm_out.select_section (decl, reloc,
1166 get_variable_align (decl));
1169 /* Return the block into which object_block DECL should be placed. */
1171 static struct object_block *
1172 get_block_for_decl (tree decl)
1174 section *sect;
1176 if (TREE_CODE (decl) == VAR_DECL)
1178 /* The object must be defined in this translation unit. */
1179 if (DECL_EXTERNAL (decl))
1180 return NULL;
1182 /* There's no point using object blocks for something that is
1183 isolated by definition. */
1184 if (DECL_COMDAT_GROUP (decl))
1185 return NULL;
1188 /* We can only calculate block offsets if the decl has a known
1189 constant size. */
1190 if (DECL_SIZE_UNIT (decl) == NULL)
1191 return NULL;
1192 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (decl)))
1193 return NULL;
1195 /* Find out which section should contain DECL. We cannot put it into
1196 an object block if it requires a standalone definition. */
1197 if (TREE_CODE (decl) == VAR_DECL)
1198 align_variable (decl, 0);
1199 sect = get_variable_section (decl, true);
1200 if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
1201 return NULL;
1203 return get_block_for_section (sect);
1206 /* Make sure block symbol SYMBOL is in block BLOCK. */
1208 static void
1209 change_symbol_block (rtx symbol, struct object_block *block)
1211 if (block != SYMBOL_REF_BLOCK (symbol))
1213 gcc_assert (SYMBOL_REF_BLOCK_OFFSET (symbol) < 0);
1214 SYMBOL_REF_BLOCK (symbol) = block;
1218 /* Return true if it is possible to put DECL in an object_block. */
1220 static bool
1221 use_blocks_for_decl_p (tree decl)
1223 struct symtab_node *snode;
1225 /* Only data DECLs can be placed into object blocks. */
1226 if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != CONST_DECL)
1227 return false;
1229 /* Detect decls created by dw2_force_const_mem. Such decls are
1230 special because DECL_INITIAL doesn't specify the decl's true value.
1231 dw2_output_indirect_constants will instead call assemble_variable
1232 with dont_output_data set to 1 and then print the contents itself. */
1233 if (DECL_INITIAL (decl) == decl)
1234 return false;
1236 /* If this decl is an alias, then we don't want to emit a
1237 definition. */
1238 if (TREE_CODE (decl) == VAR_DECL
1239 && (snode = symtab_node::get (decl)) != NULL
1240 && snode->alias)
1241 return false;
1243 return targetm.use_blocks_for_decl_p (decl);
1246 /* Create the DECL_RTL for a VAR_DECL or FUNCTION_DECL. DECL should
1247 have static storage duration. In other words, it should not be an
1248 automatic variable, including PARM_DECLs.
1250 There is, however, one exception: this function handles variables
1251 explicitly placed in a particular register by the user.
1253 This is never called for PARM_DECL nodes. */
1255 void
1256 make_decl_rtl (tree decl)
1258 const char *name = 0;
1259 int reg_number;
1260 rtx x;
1262 /* Check that we are not being given an automatic variable. */
1263 gcc_assert (TREE_CODE (decl) != PARM_DECL
1264 && TREE_CODE (decl) != RESULT_DECL);
1266 /* A weak alias has TREE_PUBLIC set but not the other bits. */
1267 gcc_assert (TREE_CODE (decl) != VAR_DECL
1268 || TREE_STATIC (decl)
1269 || TREE_PUBLIC (decl)
1270 || DECL_EXTERNAL (decl)
1271 || DECL_REGISTER (decl));
1273 /* And that we were not given a type or a label. */
1274 gcc_assert (TREE_CODE (decl) != TYPE_DECL
1275 && TREE_CODE (decl) != LABEL_DECL);
1277 /* For a duplicate declaration, we can be called twice on the
1278 same DECL node. Don't discard the RTL already made. */
1279 if (DECL_RTL_SET_P (decl))
1281 /* If the old RTL had the wrong mode, fix the mode. */
1282 x = DECL_RTL (decl);
1283 if (GET_MODE (x) != DECL_MODE (decl))
1284 SET_DECL_RTL (decl, adjust_address_nv (x, DECL_MODE (decl), 0));
1286 if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1287 return;
1289 /* ??? Another way to do this would be to maintain a hashed
1290 table of such critters. Instead of adding stuff to a DECL
1291 to give certain attributes to it, we could use an external
1292 hash map from DECL to set of attributes. */
1294 /* Let the target reassign the RTL if it wants.
1295 This is necessary, for example, when one machine specific
1296 decl attribute overrides another. */
1297 targetm.encode_section_info (decl, DECL_RTL (decl), false);
1299 /* If the symbol has a SYMBOL_REF_BLOCK field, update it based
1300 on the new decl information. */
1301 if (MEM_P (x)
1302 && GET_CODE (XEXP (x, 0)) == SYMBOL_REF
1303 && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (x, 0)))
1304 change_symbol_block (XEXP (x, 0), get_block_for_decl (decl));
1306 return;
1309 /* If this variable belongs to the global constant pool, retrieve the
1310 pre-computed RTL or recompute it in LTO mode. */
1311 if (TREE_CODE (decl) == VAR_DECL && DECL_IN_CONSTANT_POOL (decl))
1313 SET_DECL_RTL (decl, output_constant_def (DECL_INITIAL (decl), 1));
1314 return;
1317 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
1319 if (name[0] != '*' && TREE_CODE (decl) != FUNCTION_DECL
1320 && DECL_REGISTER (decl))
1322 error ("register name not specified for %q+D", decl);
1324 else if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1326 const char *asmspec = name+1;
1327 machine_mode mode = DECL_MODE (decl);
1328 reg_number = decode_reg_name (asmspec);
1329 /* First detect errors in declaring global registers. */
1330 if (reg_number == -1)
1331 error ("register name not specified for %q+D", decl);
1332 else if (reg_number < 0)
1333 error ("invalid register name for %q+D", decl);
1334 else if (mode == BLKmode)
1335 error ("data type of %q+D isn%'t suitable for a register",
1336 decl);
1337 else if (!in_hard_reg_set_p (accessible_reg_set, mode, reg_number))
1338 error ("the register specified for %q+D cannot be accessed"
1339 " by the current target", decl);
1340 else if (!in_hard_reg_set_p (operand_reg_set, mode, reg_number))
1341 error ("the register specified for %q+D is not general enough"
1342 " to be used as a register variable", decl);
1343 else if (!HARD_REGNO_MODE_OK (reg_number, mode))
1344 error ("register specified for %q+D isn%'t suitable for data type",
1345 decl);
1346 /* Now handle properly declared static register variables. */
1347 else
1349 int nregs;
1351 if (DECL_INITIAL (decl) != 0 && TREE_STATIC (decl))
1353 DECL_INITIAL (decl) = 0;
1354 error ("global register variable has initial value");
1356 if (TREE_THIS_VOLATILE (decl))
1357 warning (OPT_Wvolatile_register_var,
1358 "optimization may eliminate reads and/or "
1359 "writes to register variables");
1361 /* If the user specified one of the eliminables registers here,
1362 e.g., FRAME_POINTER_REGNUM, we don't want to get this variable
1363 confused with that register and be eliminated. This usage is
1364 somewhat suspect... */
1366 SET_DECL_RTL (decl, gen_rtx_raw_REG (mode, reg_number));
1367 ORIGINAL_REGNO (DECL_RTL (decl)) = reg_number;
1368 REG_USERVAR_P (DECL_RTL (decl)) = 1;
1370 if (TREE_STATIC (decl))
1372 /* Make this register global, so not usable for anything
1373 else. */
1374 #ifdef ASM_DECLARE_REGISTER_GLOBAL
1375 name = IDENTIFIER_POINTER (DECL_NAME (decl));
1376 ASM_DECLARE_REGISTER_GLOBAL (asm_out_file, decl, reg_number, name);
1377 #endif
1378 nregs = hard_regno_nregs[reg_number][mode];
1379 while (nregs > 0)
1380 globalize_reg (decl, reg_number + --nregs);
1383 /* As a register variable, it has no section. */
1384 return;
1386 /* Avoid internal errors from invalid register
1387 specifications. */
1388 SET_DECL_ASSEMBLER_NAME (decl, NULL_TREE);
1389 DECL_HARD_REGISTER (decl) = 0;
1390 return;
1392 /* Now handle ordinary static variables and functions (in memory).
1393 Also handle vars declared register invalidly. */
1394 else if (name[0] == '*')
1396 #ifdef REGISTER_PREFIX
1397 if (strlen (REGISTER_PREFIX) != 0)
1399 reg_number = decode_reg_name (name);
1400 if (reg_number >= 0 || reg_number == -3)
1401 error ("register name given for non-register variable %q+D", decl);
1403 #endif
1406 /* Specifying a section attribute on a variable forces it into a
1407 non-.bss section, and thus it cannot be common. */
1408 /* FIXME: In general this code should not be necessary because
1409 visibility pass is doing the same work. But notice_global_symbol
1410 is called early and it needs to make DECL_RTL to get the name.
1411 we take care of recomputing the DECL_RTL after visibility is changed. */
1412 if (TREE_CODE (decl) == VAR_DECL
1413 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
1414 && DECL_SECTION_NAME (decl) != NULL
1415 && DECL_INITIAL (decl) == NULL_TREE
1416 && DECL_COMMON (decl))
1417 DECL_COMMON (decl) = 0;
1419 /* Variables can't be both common and weak. */
1420 if (TREE_CODE (decl) == VAR_DECL && DECL_WEAK (decl))
1421 DECL_COMMON (decl) = 0;
1423 if (use_object_blocks_p () && use_blocks_for_decl_p (decl))
1424 x = create_block_symbol (name, get_block_for_decl (decl), -1);
1425 else
1427 machine_mode address_mode = Pmode;
1428 if (TREE_TYPE (decl) != error_mark_node)
1430 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
1431 address_mode = targetm.addr_space.address_mode (as);
1433 x = gen_rtx_SYMBOL_REF (address_mode, name);
1435 SYMBOL_REF_WEAK (x) = DECL_WEAK (decl);
1436 SET_SYMBOL_REF_DECL (x, decl);
1438 x = gen_rtx_MEM (DECL_MODE (decl), x);
1439 if (TREE_CODE (decl) != FUNCTION_DECL)
1440 set_mem_attributes (x, decl, 1);
1441 SET_DECL_RTL (decl, x);
1443 /* Optionally set flags or add text to the name to record information
1444 such as that it is a function name.
1445 If the name is changed, the macro ASM_OUTPUT_LABELREF
1446 will have to know how to strip this information. */
1447 targetm.encode_section_info (decl, DECL_RTL (decl), true);
1450 /* Like make_decl_rtl, but inhibit creation of new alias sets when
1451 calling make_decl_rtl. Also, reset DECL_RTL before returning the
1452 rtl. */
1455 make_decl_rtl_for_debug (tree decl)
1457 unsigned int save_aliasing_flag;
1458 rtx rtl;
1460 if (DECL_RTL_SET_P (decl))
1461 return DECL_RTL (decl);
1463 /* Kludge alert! Somewhere down the call chain, make_decl_rtl will
1464 call new_alias_set. If running with -fcompare-debug, sometimes
1465 we do not want to create alias sets that will throw the alias
1466 numbers off in the comparison dumps. So... clearing
1467 flag_strict_aliasing will keep new_alias_set() from creating a
1468 new set. */
1469 save_aliasing_flag = flag_strict_aliasing;
1470 flag_strict_aliasing = 0;
1472 rtl = DECL_RTL (decl);
1473 /* Reset DECL_RTL back, as various parts of the compiler expects
1474 DECL_RTL set meaning it is actually going to be output. */
1475 SET_DECL_RTL (decl, NULL);
1477 flag_strict_aliasing = save_aliasing_flag;
1478 return rtl;
1481 /* Output a string of literal assembler code
1482 for an `asm' keyword used between functions. */
1484 void
1485 assemble_asm (tree string)
1487 const char *p;
1488 app_enable ();
1490 if (TREE_CODE (string) == ADDR_EXPR)
1491 string = TREE_OPERAND (string, 0);
1493 p = TREE_STRING_POINTER (string);
1494 fprintf (asm_out_file, "%s%s\n", p[0] == '\t' ? "" : "\t", p);
1497 /* Write the address of the entity given by SYMBOL to SEC. */
1498 void
1499 assemble_addr_to_section (rtx symbol, section *sec)
1501 switch_to_section (sec);
1502 assemble_align (POINTER_SIZE);
1503 assemble_integer (symbol, POINTER_SIZE_UNITS, POINTER_SIZE, 1);
1506 /* Return the numbered .ctors.N (if CONSTRUCTOR_P) or .dtors.N (if
1507 not) section for PRIORITY. */
1508 section *
1509 get_cdtor_priority_section (int priority, bool constructor_p)
1511 char buf[16];
1513 /* ??? This only works reliably with the GNU linker. */
1514 sprintf (buf, "%s.%.5u",
1515 constructor_p ? ".ctors" : ".dtors",
1516 /* Invert the numbering so the linker puts us in the proper
1517 order; constructors are run from right to left, and the
1518 linker sorts in increasing order. */
1519 MAX_INIT_PRIORITY - priority);
1520 return get_section (buf, SECTION_WRITE, NULL);
1523 void
1524 default_named_section_asm_out_destructor (rtx symbol, int priority)
1526 section *sec;
1528 if (priority != DEFAULT_INIT_PRIORITY)
1529 sec = get_cdtor_priority_section (priority,
1530 /*constructor_p=*/false);
1531 else
1532 sec = get_section (".dtors", SECTION_WRITE, NULL);
1534 assemble_addr_to_section (symbol, sec);
1537 #ifdef DTORS_SECTION_ASM_OP
1538 void
1539 default_dtor_section_asm_out_destructor (rtx symbol,
1540 int priority ATTRIBUTE_UNUSED)
1542 assemble_addr_to_section (symbol, dtors_section);
1544 #endif
1546 void
1547 default_named_section_asm_out_constructor (rtx symbol, int priority)
1549 section *sec;
1551 if (priority != DEFAULT_INIT_PRIORITY)
1552 sec = get_cdtor_priority_section (priority,
1553 /*constructor_p=*/true);
1554 else
1555 sec = get_section (".ctors", SECTION_WRITE, NULL);
1557 assemble_addr_to_section (symbol, sec);
1560 #ifdef CTORS_SECTION_ASM_OP
1561 void
1562 default_ctor_section_asm_out_constructor (rtx symbol,
1563 int priority ATTRIBUTE_UNUSED)
1565 assemble_addr_to_section (symbol, ctors_section);
1567 #endif
1569 /* CONSTANT_POOL_BEFORE_FUNCTION may be defined as an expression with
1570 a nonzero value if the constant pool should be output before the
1571 start of the function, or a zero value if the pool should output
1572 after the end of the function. The default is to put it before the
1573 start. */
1575 #ifndef CONSTANT_POOL_BEFORE_FUNCTION
1576 #define CONSTANT_POOL_BEFORE_FUNCTION 1
1577 #endif
1579 /* DECL is an object (either VAR_DECL or FUNCTION_DECL) which is going
1580 to be output to assembler.
1581 Set first_global_object_name and weak_global_object_name as appropriate. */
1583 void
1584 notice_global_symbol (tree decl)
1586 const char **type = &first_global_object_name;
1588 if (first_global_object_name
1589 || !TREE_PUBLIC (decl)
1590 || DECL_EXTERNAL (decl)
1591 || !DECL_NAME (decl)
1592 || (TREE_CODE (decl) != FUNCTION_DECL
1593 && (TREE_CODE (decl) != VAR_DECL
1594 || (DECL_COMMON (decl)
1595 && (DECL_INITIAL (decl) == 0
1596 || DECL_INITIAL (decl) == error_mark_node))))
1597 || !MEM_P (DECL_RTL (decl)))
1598 return;
1600 /* We win when global object is found, but it is useful to know about weak
1601 symbol as well so we can produce nicer unique names. */
1602 if (DECL_WEAK (decl) || DECL_ONE_ONLY (decl) || flag_shlib)
1603 type = &weak_global_object_name;
1605 if (!*type)
1607 const char *p;
1608 const char *name;
1609 rtx decl_rtl = DECL_RTL (decl);
1611 p = targetm.strip_name_encoding (XSTR (XEXP (decl_rtl, 0), 0));
1612 name = ggc_strdup (p);
1614 *type = name;
1618 /* If not using flag_reorder_blocks_and_partition, decide early whether the
1619 current function goes into the cold section, so that targets can use
1620 current_function_section during RTL expansion. DECL describes the
1621 function. */
1623 void
1624 decide_function_section (tree decl)
1626 first_function_block_is_cold = false;
1628 if (flag_reorder_blocks_and_partition)
1629 /* We will decide in assemble_start_function. */
1630 return;
1632 if (DECL_SECTION_NAME (decl))
1634 struct cgraph_node *node = cgraph_node::get (current_function_decl);
1635 /* Calls to function_section rely on first_function_block_is_cold
1636 being accurate. */
1637 first_function_block_is_cold = (node
1638 && node->frequency
1639 == NODE_FREQUENCY_UNLIKELY_EXECUTED);
1642 in_cold_section_p = first_function_block_is_cold;
1645 /* Output assembler code for the constant pool of a function and associated
1646 with defining the name of the function. DECL describes the function.
1647 NAME is the function's name. For the constant pool, we use the current
1648 constant pool data. */
1650 void
1651 assemble_start_function (tree decl, const char *fnname)
1653 int align;
1654 char tmp_label[100];
1655 bool hot_label_written = false;
1657 if (flag_reorder_blocks_and_partition)
1659 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTB", const_labelno);
1660 crtl->subsections.hot_section_label = ggc_strdup (tmp_label);
1661 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDB", const_labelno);
1662 crtl->subsections.cold_section_label = ggc_strdup (tmp_label);
1663 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTE", const_labelno);
1664 crtl->subsections.hot_section_end_label = ggc_strdup (tmp_label);
1665 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDE", const_labelno);
1666 crtl->subsections.cold_section_end_label = ggc_strdup (tmp_label);
1667 const_labelno++;
1669 else
1671 crtl->subsections.hot_section_label = NULL;
1672 crtl->subsections.cold_section_label = NULL;
1673 crtl->subsections.hot_section_end_label = NULL;
1674 crtl->subsections.cold_section_end_label = NULL;
1677 /* The following code does not need preprocessing in the assembler. */
1679 app_disable ();
1681 if (CONSTANT_POOL_BEFORE_FUNCTION)
1682 output_constant_pool (fnname, decl);
1684 /* Make sure the not and cold text (code) sections are properly
1685 aligned. This is necessary here in the case where the function
1686 has both hot and cold sections, because we don't want to re-set
1687 the alignment when the section switch happens mid-function. */
1689 if (flag_reorder_blocks_and_partition)
1691 first_function_block_is_cold = false;
1693 switch_to_section (unlikely_text_section ());
1694 assemble_align (DECL_ALIGN (decl));
1695 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_label);
1697 /* When the function starts with a cold section, we need to explicitly
1698 align the hot section and write out the hot section label.
1699 But if the current function is a thunk, we do not have a CFG. */
1700 if (!cfun->is_thunk
1701 && BB_PARTITION (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb) == BB_COLD_PARTITION)
1703 switch_to_section (text_section);
1704 assemble_align (DECL_ALIGN (decl));
1705 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
1706 hot_label_written = true;
1707 first_function_block_is_cold = true;
1709 in_cold_section_p = first_function_block_is_cold;
1713 /* Switch to the correct text section for the start of the function. */
1715 switch_to_section (function_section (decl));
1716 if (flag_reorder_blocks_and_partition
1717 && !hot_label_written)
1718 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
1720 /* Tell assembler to move to target machine's alignment for functions. */
1721 align = floor_log2 (DECL_ALIGN (decl) / BITS_PER_UNIT);
1722 if (align > 0)
1724 ASM_OUTPUT_ALIGN (asm_out_file, align);
1727 /* Handle a user-specified function alignment.
1728 Note that we still need to align to DECL_ALIGN, as above,
1729 because ASM_OUTPUT_MAX_SKIP_ALIGN might not do any alignment at all. */
1730 if (! DECL_USER_ALIGN (decl)
1731 && align_functions_log > align
1732 && optimize_function_for_speed_p (cfun))
1734 #ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
1735 ASM_OUTPUT_MAX_SKIP_ALIGN (asm_out_file,
1736 align_functions_log, align_functions - 1);
1737 #else
1738 ASM_OUTPUT_ALIGN (asm_out_file, align_functions_log);
1739 #endif
1742 #ifdef ASM_OUTPUT_FUNCTION_PREFIX
1743 ASM_OUTPUT_FUNCTION_PREFIX (asm_out_file, fnname);
1744 #endif
1746 if (!DECL_IGNORED_P (decl))
1747 (*debug_hooks->begin_function) (decl);
1749 /* Make function name accessible from other files, if appropriate. */
1751 if (TREE_PUBLIC (decl))
1753 notice_global_symbol (decl);
1755 globalize_decl (decl);
1757 maybe_assemble_visibility (decl);
1760 if (DECL_PRESERVE_P (decl))
1761 targetm.asm_out.mark_decl_preserved (fnname);
1763 /* Do any machine/system dependent processing of the function name. */
1764 #ifdef ASM_DECLARE_FUNCTION_NAME
1765 ASM_DECLARE_FUNCTION_NAME (asm_out_file, fnname, current_function_decl);
1766 #else
1767 /* Standard thing is just output label for the function. */
1768 ASM_OUTPUT_FUNCTION_LABEL (asm_out_file, fnname, current_function_decl);
1769 #endif /* ASM_DECLARE_FUNCTION_NAME */
1771 if (lookup_attribute ("no_split_stack", DECL_ATTRIBUTES (decl)))
1772 saw_no_split_stack = true;
1775 /* Output assembler code associated with defining the size of the
1776 function. DECL describes the function. NAME is the function's name. */
1778 void
1779 assemble_end_function (tree decl, const char *fnname ATTRIBUTE_UNUSED)
1781 #ifdef ASM_DECLARE_FUNCTION_SIZE
1782 /* We could have switched section in the middle of the function. */
1783 if (flag_reorder_blocks_and_partition)
1784 switch_to_section (function_section (decl));
1785 ASM_DECLARE_FUNCTION_SIZE (asm_out_file, fnname, decl);
1786 #endif
1787 if (! CONSTANT_POOL_BEFORE_FUNCTION)
1789 output_constant_pool (fnname, decl);
1790 switch_to_section (function_section (decl)); /* need to switch back */
1792 /* Output labels for end of hot/cold text sections (to be used by
1793 debug info.) */
1794 if (flag_reorder_blocks_and_partition)
1796 section *save_text_section;
1798 save_text_section = in_section;
1799 switch_to_section (unlikely_text_section ());
1800 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_end_label);
1801 if (first_function_block_is_cold)
1802 switch_to_section (text_section);
1803 else
1804 switch_to_section (function_section (decl));
1805 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_end_label);
1806 switch_to_section (save_text_section);
1810 /* Assemble code to leave SIZE bytes of zeros. */
1812 void
1813 assemble_zeros (unsigned HOST_WIDE_INT size)
1815 /* Do no output if -fsyntax-only. */
1816 if (flag_syntax_only)
1817 return;
1819 #ifdef ASM_NO_SKIP_IN_TEXT
1820 /* The `space' pseudo in the text section outputs nop insns rather than 0s,
1821 so we must output 0s explicitly in the text section. */
1822 if (ASM_NO_SKIP_IN_TEXT && (in_section->common.flags & SECTION_CODE) != 0)
1824 unsigned HOST_WIDE_INT i;
1825 for (i = 0; i < size; i++)
1826 assemble_integer (const0_rtx, 1, BITS_PER_UNIT, 1);
1828 else
1829 #endif
1830 if (size > 0)
1831 ASM_OUTPUT_SKIP (asm_out_file, size);
1834 /* Assemble an alignment pseudo op for an ALIGN-bit boundary. */
1836 void
1837 assemble_align (int align)
1839 if (align > BITS_PER_UNIT)
1841 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
1845 /* Assemble a string constant with the specified C string as contents. */
1847 void
1848 assemble_string (const char *p, int size)
1850 int pos = 0;
1851 int maximum = 2000;
1853 /* If the string is very long, split it up. */
1855 while (pos < size)
1857 int thissize = size - pos;
1858 if (thissize > maximum)
1859 thissize = maximum;
1861 ASM_OUTPUT_ASCII (asm_out_file, p, thissize);
1863 pos += thissize;
1864 p += thissize;
1869 /* A noswitch_section_callback for lcomm_section. */
1871 static bool
1872 emit_local (tree decl ATTRIBUTE_UNUSED,
1873 const char *name ATTRIBUTE_UNUSED,
1874 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1875 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1877 #if defined ASM_OUTPUT_ALIGNED_DECL_LOCAL
1878 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, decl, name,
1879 size, DECL_ALIGN (decl));
1880 return true;
1881 #elif defined ASM_OUTPUT_ALIGNED_LOCAL
1882 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, DECL_ALIGN (decl));
1883 return true;
1884 #else
1885 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
1886 return false;
1887 #endif
1890 /* A noswitch_section_callback for bss_noswitch_section. */
1892 #if defined ASM_OUTPUT_ALIGNED_BSS
1893 static bool
1894 emit_bss (tree decl ATTRIBUTE_UNUSED,
1895 const char *name ATTRIBUTE_UNUSED,
1896 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1897 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1899 #if defined ASM_OUTPUT_ALIGNED_BSS
1900 ASM_OUTPUT_ALIGNED_BSS (asm_out_file, decl, name, size,
1901 get_variable_align (decl));
1902 return true;
1903 #endif
1905 #endif
1907 /* A noswitch_section_callback for comm_section. */
1909 static bool
1910 emit_common (tree decl ATTRIBUTE_UNUSED,
1911 const char *name ATTRIBUTE_UNUSED,
1912 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1913 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1915 #if defined ASM_OUTPUT_ALIGNED_DECL_COMMON
1916 ASM_OUTPUT_ALIGNED_DECL_COMMON (asm_out_file, decl, name,
1917 size, get_variable_align (decl));
1918 return true;
1919 #elif defined ASM_OUTPUT_ALIGNED_COMMON
1920 ASM_OUTPUT_ALIGNED_COMMON (asm_out_file, name, size,
1921 get_variable_align (decl));
1922 return true;
1923 #else
1924 ASM_OUTPUT_COMMON (asm_out_file, name, size, rounded);
1925 return false;
1926 #endif
1929 /* A noswitch_section_callback for tls_comm_section. */
1931 static bool
1932 emit_tls_common (tree decl ATTRIBUTE_UNUSED,
1933 const char *name ATTRIBUTE_UNUSED,
1934 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1935 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1937 #ifdef ASM_OUTPUT_TLS_COMMON
1938 ASM_OUTPUT_TLS_COMMON (asm_out_file, decl, name, size);
1939 return true;
1940 #else
1941 sorry ("thread-local COMMON data not implemented");
1942 return true;
1943 #endif
1946 /* Assemble DECL given that it belongs in SECTION_NOSWITCH section SECT.
1947 NAME is the name of DECL's SYMBOL_REF. */
1949 static void
1950 assemble_noswitch_variable (tree decl, const char *name, section *sect,
1951 unsigned int align)
1953 unsigned HOST_WIDE_INT size, rounded;
1955 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
1956 rounded = size;
1958 if ((flag_sanitize & SANITIZE_ADDRESS) && asan_protect_global (decl))
1959 size += asan_red_zone_size (size);
1961 /* Don't allocate zero bytes of common,
1962 since that means "undefined external" in the linker. */
1963 if (size == 0)
1964 rounded = 1;
1966 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
1967 so that each uninitialized object starts on such a boundary. */
1968 rounded += (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1;
1969 rounded = (rounded / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
1970 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
1972 if (!sect->noswitch.callback (decl, name, size, rounded)
1973 && (unsigned HOST_WIDE_INT) (align / BITS_PER_UNIT) > rounded)
1974 error ("requested alignment for %q+D is greater than "
1975 "implemented alignment of %wu", decl, rounded);
1978 /* A subroutine of assemble_variable. Output the label and contents of
1979 DECL, whose address is a SYMBOL_REF with name NAME. DONT_OUTPUT_DATA
1980 is as for assemble_variable. */
1982 static void
1983 assemble_variable_contents (tree decl, const char *name,
1984 bool dont_output_data)
1986 /* Do any machine/system dependent processing of the object. */
1987 #ifdef ASM_DECLARE_OBJECT_NAME
1988 last_assemble_variable_decl = decl;
1989 ASM_DECLARE_OBJECT_NAME (asm_out_file, name, decl);
1990 #else
1991 /* Standard thing is just output label for the object. */
1992 ASM_OUTPUT_LABEL (asm_out_file, name);
1993 #endif /* ASM_DECLARE_OBJECT_NAME */
1995 if (!dont_output_data)
1997 /* Caller is supposed to use varpool_get_constructor when it wants
1998 to output the body. */
1999 gcc_assert (!in_lto_p || DECL_INITIAL (decl) != error_mark_node);
2000 if (DECL_INITIAL (decl)
2001 && DECL_INITIAL (decl) != error_mark_node
2002 && !initializer_zerop (DECL_INITIAL (decl)))
2003 /* Output the actual data. */
2004 output_constant (DECL_INITIAL (decl),
2005 tree_to_uhwi (DECL_SIZE_UNIT (decl)),
2006 get_variable_align (decl));
2007 else
2008 /* Leave space for it. */
2009 assemble_zeros (tree_to_uhwi (DECL_SIZE_UNIT (decl)));
2013 /* Assemble everything that is needed for a variable or function declaration.
2014 Not used for automatic variables, and not used for function definitions.
2015 Should not be called for variables of incomplete structure type.
2017 TOP_LEVEL is nonzero if this variable has file scope.
2018 AT_END is nonzero if this is the special handling, at end of compilation,
2019 to define things that have had only tentative definitions.
2020 DONT_OUTPUT_DATA if nonzero means don't actually output the
2021 initial value (that will be done by the caller). */
2023 void
2024 assemble_variable (tree decl, int top_level ATTRIBUTE_UNUSED,
2025 int at_end ATTRIBUTE_UNUSED, int dont_output_data)
2027 const char *name;
2028 rtx decl_rtl, symbol;
2029 section *sect;
2030 unsigned int align;
2031 bool asan_protected = false;
2033 /* This function is supposed to handle VARIABLES. Ensure we have one. */
2034 gcc_assert (TREE_CODE (decl) == VAR_DECL);
2036 /* Emulated TLS had better not get this far. */
2037 gcc_checking_assert (targetm.have_tls || !DECL_THREAD_LOCAL_P (decl));
2039 last_assemble_variable_decl = 0;
2041 /* Normally no need to say anything here for external references,
2042 since assemble_external is called by the language-specific code
2043 when a declaration is first seen. */
2045 if (DECL_EXTERNAL (decl))
2046 return;
2048 /* Do nothing for global register variables. */
2049 if (DECL_RTL_SET_P (decl) && REG_P (DECL_RTL (decl)))
2051 TREE_ASM_WRITTEN (decl) = 1;
2052 return;
2055 /* If type was incomplete when the variable was declared,
2056 see if it is complete now. */
2058 if (DECL_SIZE (decl) == 0)
2059 layout_decl (decl, 0);
2061 /* Still incomplete => don't allocate it; treat the tentative defn
2062 (which is what it must have been) as an `extern' reference. */
2064 if (!dont_output_data && DECL_SIZE (decl) == 0)
2066 error ("storage size of %q+D isn%'t known", decl);
2067 TREE_ASM_WRITTEN (decl) = 1;
2068 return;
2071 /* The first declaration of a variable that comes through this function
2072 decides whether it is global (in C, has external linkage)
2073 or local (in C, has internal linkage). So do nothing more
2074 if this function has already run. */
2076 if (TREE_ASM_WRITTEN (decl))
2077 return;
2079 /* Make sure targetm.encode_section_info is invoked before we set
2080 ASM_WRITTEN. */
2081 decl_rtl = DECL_RTL (decl);
2083 TREE_ASM_WRITTEN (decl) = 1;
2085 /* Do no output if -fsyntax-only. */
2086 if (flag_syntax_only)
2087 return;
2089 if (! dont_output_data
2090 && ! valid_constant_size_p (DECL_SIZE_UNIT (decl)))
2092 error ("size of variable %q+D is too large", decl);
2093 return;
2096 gcc_assert (MEM_P (decl_rtl));
2097 gcc_assert (GET_CODE (XEXP (decl_rtl, 0)) == SYMBOL_REF);
2098 symbol = XEXP (decl_rtl, 0);
2100 /* If this symbol belongs to the tree constant pool, output the constant
2101 if it hasn't already been written. */
2102 if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
2104 tree decl = SYMBOL_REF_DECL (symbol);
2105 if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
2106 output_constant_def_contents (symbol);
2107 return;
2110 app_disable ();
2112 name = XSTR (symbol, 0);
2113 if (TREE_PUBLIC (decl) && DECL_NAME (decl))
2114 notice_global_symbol (decl);
2116 /* Compute the alignment of this data. */
2118 align_variable (decl, dont_output_data);
2120 if ((flag_sanitize & SANITIZE_ADDRESS)
2121 && asan_protect_global (decl))
2123 asan_protected = true;
2124 DECL_ALIGN (decl) = MAX (DECL_ALIGN (decl),
2125 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
2128 set_mem_align (decl_rtl, DECL_ALIGN (decl));
2130 align = get_variable_align (decl);
2132 if (TREE_PUBLIC (decl))
2133 maybe_assemble_visibility (decl);
2135 if (DECL_PRESERVE_P (decl))
2136 targetm.asm_out.mark_decl_preserved (name);
2138 /* First make the assembler name(s) global if appropriate. */
2139 sect = get_variable_section (decl, false);
2140 if (TREE_PUBLIC (decl)
2141 && (sect->common.flags & SECTION_COMMON) == 0)
2142 globalize_decl (decl);
2144 /* Output any data that we will need to use the address of. */
2145 if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node)
2146 output_addressed_constants (DECL_INITIAL (decl));
2148 /* dbxout.c needs to know this. */
2149 if (sect && (sect->common.flags & SECTION_CODE) != 0)
2150 DECL_IN_TEXT_SECTION (decl) = 1;
2152 /* If the decl is part of an object_block, make sure that the decl
2153 has been positioned within its block, but do not write out its
2154 definition yet. output_object_blocks will do that later. */
2155 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
2157 gcc_assert (!dont_output_data);
2158 place_block_symbol (symbol);
2160 else if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
2161 assemble_noswitch_variable (decl, name, sect, align);
2162 else
2164 /* The following bit of code ensures that vtable_map
2165 variables are not only in the comdat section, but that
2166 each variable has its own unique comdat name. If this
2167 code is removed, the variables end up in the same section
2168 with a single comdat name.
2170 FIXME: resolve_unique_section needs to deal better with
2171 decls with both DECL_SECTION_NAME and DECL_ONE_ONLY. Once
2172 that is fixed, this if-else statement can be replaced with
2173 a single call to "switch_to_section (sect)". */
2174 if (sect->named.name
2175 && (strcmp (sect->named.name, ".vtable_map_vars") == 0))
2177 #if defined (OBJECT_FORMAT_ELF)
2178 targetm.asm_out.named_section (sect->named.name,
2179 sect->named.common.flags
2180 | SECTION_LINKONCE,
2181 DECL_NAME (decl));
2182 in_section = sect;
2183 #else
2184 switch_to_section (sect);
2185 #endif
2187 else
2188 switch_to_section (sect);
2189 if (align > BITS_PER_UNIT)
2190 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
2191 assemble_variable_contents (decl, name, dont_output_data);
2192 if (asan_protected)
2194 unsigned HOST_WIDE_INT int size
2195 = tree_to_uhwi (DECL_SIZE_UNIT (decl));
2196 assemble_zeros (asan_red_zone_size (size));
2202 /* Given a function declaration (FN_DECL), this function assembles the
2203 function into the .preinit_array section. */
2205 void
2206 assemble_vtv_preinit_initializer (tree fn_decl)
2208 section *sect;
2209 unsigned flags = SECTION_WRITE;
2210 rtx symbol = XEXP (DECL_RTL (fn_decl), 0);
2212 flags |= SECTION_NOTYPE;
2213 sect = get_section (".preinit_array", flags, fn_decl);
2214 switch_to_section (sect);
2215 assemble_addr_to_section (symbol, sect);
2218 /* Return 1 if type TYPE contains any pointers. */
2220 static int
2221 contains_pointers_p (tree type)
2223 switch (TREE_CODE (type))
2225 case POINTER_TYPE:
2226 case REFERENCE_TYPE:
2227 /* I'm not sure whether OFFSET_TYPE needs this treatment,
2228 so I'll play safe and return 1. */
2229 case OFFSET_TYPE:
2230 return 1;
2232 case RECORD_TYPE:
2233 case UNION_TYPE:
2234 case QUAL_UNION_TYPE:
2236 tree fields;
2237 /* For a type that has fields, see if the fields have pointers. */
2238 for (fields = TYPE_FIELDS (type); fields; fields = DECL_CHAIN (fields))
2239 if (TREE_CODE (fields) == FIELD_DECL
2240 && contains_pointers_p (TREE_TYPE (fields)))
2241 return 1;
2242 return 0;
2245 case ARRAY_TYPE:
2246 /* An array type contains pointers if its element type does. */
2247 return contains_pointers_p (TREE_TYPE (type));
2249 default:
2250 return 0;
2254 /* We delay assemble_external processing until
2255 the compilation unit is finalized. This is the best we can do for
2256 right now (i.e. stage 3 of GCC 4.0) - the right thing is to delay
2257 it all the way to final. See PR 17982 for further discussion. */
2258 static GTY(()) tree pending_assemble_externals;
2260 #ifdef ASM_OUTPUT_EXTERNAL
2261 /* Some targets delay some output to final using TARGET_ASM_FILE_END.
2262 As a result, assemble_external can be called after the list of externals
2263 is processed and the pointer set destroyed. */
2264 static bool pending_assemble_externals_processed;
2266 /* Avoid O(external_decls**2) lookups in the pending_assemble_externals
2267 TREE_LIST in assemble_external. */
2268 static hash_set<tree> *pending_assemble_externals_set;
2270 /* True if DECL is a function decl for which no out-of-line copy exists.
2271 It is assumed that DECL's assembler name has been set. */
2273 static bool
2274 incorporeal_function_p (tree decl)
2276 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
2278 const char *name;
2280 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
2281 && (DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA
2282 || DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA_WITH_ALIGN))
2283 return true;
2285 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2286 /* Atomic or sync builtins which have survived this far will be
2287 resolved externally and therefore are not incorporeal. */
2288 if (strncmp (name, "__builtin_", 10) == 0)
2289 return true;
2291 return false;
2294 /* Actually do the tests to determine if this is necessary, and invoke
2295 ASM_OUTPUT_EXTERNAL. */
2296 static void
2297 assemble_external_real (tree decl)
2299 rtx rtl = DECL_RTL (decl);
2301 if (MEM_P (rtl) && GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF
2302 && !SYMBOL_REF_USED (XEXP (rtl, 0))
2303 && !incorporeal_function_p (decl))
2305 /* Some systems do require some output. */
2306 SYMBOL_REF_USED (XEXP (rtl, 0)) = 1;
2307 ASM_OUTPUT_EXTERNAL (asm_out_file, decl, XSTR (XEXP (rtl, 0), 0));
2310 #endif
2312 void
2313 process_pending_assemble_externals (void)
2315 #ifdef ASM_OUTPUT_EXTERNAL
2316 tree list;
2317 for (list = pending_assemble_externals; list; list = TREE_CHAIN (list))
2318 assemble_external_real (TREE_VALUE (list));
2320 pending_assemble_externals = 0;
2321 pending_assemble_externals_processed = true;
2322 delete pending_assemble_externals_set;
2323 #endif
2326 /* This TREE_LIST contains any weak symbol declarations waiting
2327 to be emitted. */
2328 static GTY(()) tree weak_decls;
2330 /* Output something to declare an external symbol to the assembler,
2331 and qualifiers such as weakness. (Most assemblers don't need
2332 extern declaration, so we normally output nothing.) Do nothing if
2333 DECL is not external. */
2335 void
2336 assemble_external (tree decl ATTRIBUTE_UNUSED)
2338 /* Make sure that the ASM_OUT_FILE is open.
2339 If it's not, we should not be calling this function. */
2340 gcc_assert (asm_out_file);
2342 /* In a perfect world, the following condition would be true.
2343 Sadly, the Java and Go front ends emit assembly *from the front end*,
2344 bypassing the call graph. See PR52739. Fix before GCC 4.8. */
2345 #if 0
2346 /* This function should only be called if we are expanding, or have
2347 expanded, to RTL.
2348 Ideally, only final.c would be calling this function, but it is
2349 not clear whether that would break things somehow. See PR 17982
2350 for further discussion. */
2351 gcc_assert (state == EXPANSION
2352 || state == FINISHED);
2353 #endif
2355 if (!DECL_P (decl) || !DECL_EXTERNAL (decl) || !TREE_PUBLIC (decl))
2356 return;
2358 /* We want to output annotation for weak and external symbols at
2359 very last to check if they are references or not. */
2361 if (TARGET_SUPPORTS_WEAK
2362 && DECL_WEAK (decl)
2363 /* TREE_STATIC is a weird and abused creature which is not
2364 generally the right test for whether an entity has been
2365 locally emitted, inlined or otherwise not-really-extern, but
2366 for declarations that can be weak, it happens to be
2367 match. */
2368 && !TREE_STATIC (decl)
2369 && lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
2370 && value_member (decl, weak_decls) == NULL_TREE)
2371 weak_decls = tree_cons (NULL, decl, weak_decls);
2373 #ifdef ASM_OUTPUT_EXTERNAL
2374 if (pending_assemble_externals_processed)
2376 assemble_external_real (decl);
2377 return;
2380 if (! pending_assemble_externals_set->add (decl))
2381 pending_assemble_externals = tree_cons (NULL, decl,
2382 pending_assemble_externals);
2383 #endif
2386 /* Similar, for calling a library function FUN. */
2388 void
2389 assemble_external_libcall (rtx fun)
2391 /* Declare library function name external when first used, if nec. */
2392 if (! SYMBOL_REF_USED (fun))
2394 SYMBOL_REF_USED (fun) = 1;
2395 targetm.asm_out.external_libcall (fun);
2399 /* Assemble a label named NAME. */
2401 void
2402 assemble_label (FILE *file, const char *name)
2404 ASM_OUTPUT_LABEL (file, name);
2407 /* Set the symbol_referenced flag for ID. */
2408 void
2409 mark_referenced (tree id)
2411 TREE_SYMBOL_REFERENCED (id) = 1;
2414 /* Set the symbol_referenced flag for DECL and notify callgraph. */
2415 void
2416 mark_decl_referenced (tree decl)
2418 if (TREE_CODE (decl) == FUNCTION_DECL)
2420 /* Extern inline functions don't become needed when referenced.
2421 If we know a method will be emitted in other TU and no new
2422 functions can be marked reachable, just use the external
2423 definition. */
2424 struct cgraph_node *node = cgraph_node::get_create (decl);
2425 if (!DECL_EXTERNAL (decl)
2426 && !node->definition)
2427 node->mark_force_output ();
2429 else if (TREE_CODE (decl) == VAR_DECL)
2431 varpool_node *node = varpool_node::get_create (decl);
2432 /* C++ frontend use mark_decl_references to force COMDAT variables
2433 to be output that might appear dead otherwise. */
2434 node->force_output = true;
2436 /* else do nothing - we can get various sorts of CST nodes here,
2437 which do not need to be marked. */
2441 /* Follow the IDENTIFIER_TRANSPARENT_ALIAS chain starting at *ALIAS
2442 until we find an identifier that is not itself a transparent alias.
2443 Modify the alias passed to it by reference (and all aliases on the
2444 way to the ultimate target), such that they do not have to be
2445 followed again, and return the ultimate target of the alias
2446 chain. */
2448 static inline tree
2449 ultimate_transparent_alias_target (tree *alias)
2451 tree target = *alias;
2453 if (IDENTIFIER_TRANSPARENT_ALIAS (target))
2455 gcc_assert (TREE_CHAIN (target));
2456 target = ultimate_transparent_alias_target (&TREE_CHAIN (target));
2457 gcc_assert (! IDENTIFIER_TRANSPARENT_ALIAS (target)
2458 && ! TREE_CHAIN (target));
2459 *alias = target;
2462 return target;
2465 /* Output to FILE (an assembly file) a reference to NAME. If NAME
2466 starts with a *, the rest of NAME is output verbatim. Otherwise
2467 NAME is transformed in a target-specific way (usually by the
2468 addition of an underscore). */
2470 void
2471 assemble_name_raw (FILE *file, const char *name)
2473 if (name[0] == '*')
2474 fputs (&name[1], file);
2475 else
2476 ASM_OUTPUT_LABELREF (file, name);
2479 /* Like assemble_name_raw, but should be used when NAME might refer to
2480 an entity that is also represented as a tree (like a function or
2481 variable). If NAME does refer to such an entity, that entity will
2482 be marked as referenced. */
2484 void
2485 assemble_name (FILE *file, const char *name)
2487 const char *real_name;
2488 tree id;
2490 real_name = targetm.strip_name_encoding (name);
2492 id = maybe_get_identifier (real_name);
2493 if (id)
2495 tree id_orig = id;
2497 mark_referenced (id);
2498 ultimate_transparent_alias_target (&id);
2499 if (id != id_orig)
2500 name = IDENTIFIER_POINTER (id);
2501 gcc_assert (! TREE_CHAIN (id));
2504 assemble_name_raw (file, name);
2507 /* Allocate SIZE bytes writable static space with a gensym name
2508 and return an RTX to refer to its address. */
2511 assemble_static_space (unsigned HOST_WIDE_INT size)
2513 char name[12];
2514 const char *namestring;
2515 rtx x;
2517 ASM_GENERATE_INTERNAL_LABEL (name, "LF", const_labelno);
2518 ++const_labelno;
2519 namestring = ggc_strdup (name);
2521 x = gen_rtx_SYMBOL_REF (Pmode, namestring);
2522 SYMBOL_REF_FLAGS (x) = SYMBOL_FLAG_LOCAL;
2524 #ifdef ASM_OUTPUT_ALIGNED_DECL_LOCAL
2525 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, NULL_TREE, name, size,
2526 BIGGEST_ALIGNMENT);
2527 #else
2528 #ifdef ASM_OUTPUT_ALIGNED_LOCAL
2529 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, BIGGEST_ALIGNMENT);
2530 #else
2532 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
2533 so that each uninitialized object starts on such a boundary. */
2534 /* Variable `rounded' might or might not be used in ASM_OUTPUT_LOCAL. */
2535 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED
2536 = ((size + (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1)
2537 / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
2538 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
2539 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
2541 #endif
2542 #endif
2543 return x;
2546 /* Assemble the static constant template for function entry trampolines.
2547 This is done at most once per compilation.
2548 Returns an RTX for the address of the template. */
2550 static GTY(()) rtx initial_trampoline;
2553 assemble_trampoline_template (void)
2555 char label[256];
2556 const char *name;
2557 int align;
2558 rtx symbol;
2560 gcc_assert (targetm.asm_out.trampoline_template != NULL);
2562 if (initial_trampoline)
2563 return initial_trampoline;
2565 /* By default, put trampoline templates in read-only data section. */
2567 #ifdef TRAMPOLINE_SECTION
2568 switch_to_section (TRAMPOLINE_SECTION);
2569 #else
2570 switch_to_section (readonly_data_section);
2571 #endif
2573 /* Write the assembler code to define one. */
2574 align = floor_log2 (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
2575 if (align > 0)
2576 ASM_OUTPUT_ALIGN (asm_out_file, align);
2578 targetm.asm_out.internal_label (asm_out_file, "LTRAMP", 0);
2579 targetm.asm_out.trampoline_template (asm_out_file);
2581 /* Record the rtl to refer to it. */
2582 ASM_GENERATE_INTERNAL_LABEL (label, "LTRAMP", 0);
2583 name = ggc_strdup (label);
2584 symbol = gen_rtx_SYMBOL_REF (Pmode, name);
2585 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_LOCAL;
2587 initial_trampoline = gen_const_mem (BLKmode, symbol);
2588 set_mem_align (initial_trampoline, TRAMPOLINE_ALIGNMENT);
2589 set_mem_size (initial_trampoline, TRAMPOLINE_SIZE);
2591 return initial_trampoline;
2594 /* A and B are either alignments or offsets. Return the minimum alignment
2595 that may be assumed after adding the two together. */
2597 static inline unsigned
2598 min_align (unsigned int a, unsigned int b)
2600 return (a | b) & -(a | b);
2603 /* Return the assembler directive for creating a given kind of integer
2604 object. SIZE is the number of bytes in the object and ALIGNED_P
2605 indicates whether it is known to be aligned. Return NULL if the
2606 assembly dialect has no such directive.
2608 The returned string should be printed at the start of a new line and
2609 be followed immediately by the object's initial value. */
2611 const char *
2612 integer_asm_op (int size, int aligned_p)
2614 struct asm_int_op *ops;
2616 if (aligned_p)
2617 ops = &targetm.asm_out.aligned_op;
2618 else
2619 ops = &targetm.asm_out.unaligned_op;
2621 switch (size)
2623 case 1:
2624 return targetm.asm_out.byte_op;
2625 case 2:
2626 return ops->hi;
2627 case 4:
2628 return ops->si;
2629 case 8:
2630 return ops->di;
2631 case 16:
2632 return ops->ti;
2633 default:
2634 return NULL;
2638 /* Use directive OP to assemble an integer object X. Print OP at the
2639 start of the line, followed immediately by the value of X. */
2641 void
2642 assemble_integer_with_op (const char *op, rtx x)
2644 fputs (op, asm_out_file);
2645 output_addr_const (asm_out_file, x);
2646 fputc ('\n', asm_out_file);
2649 /* The default implementation of the asm_out.integer target hook. */
2651 bool
2652 default_assemble_integer (rtx x ATTRIBUTE_UNUSED,
2653 unsigned int size ATTRIBUTE_UNUSED,
2654 int aligned_p ATTRIBUTE_UNUSED)
2656 const char *op = integer_asm_op (size, aligned_p);
2657 /* Avoid GAS bugs for large values. Specifically negative values whose
2658 absolute value fits in a bfd_vma, but not in a bfd_signed_vma. */
2659 if (size > UNITS_PER_WORD && size > POINTER_SIZE_UNITS)
2660 return false;
2661 return op && (assemble_integer_with_op (op, x), true);
2664 /* Assemble the integer constant X into an object of SIZE bytes. ALIGN is
2665 the alignment of the integer in bits. Return 1 if we were able to output
2666 the constant, otherwise 0. We must be able to output the constant,
2667 if FORCE is nonzero. */
2669 bool
2670 assemble_integer (rtx x, unsigned int size, unsigned int align, int force)
2672 int aligned_p;
2674 aligned_p = (align >= MIN (size * BITS_PER_UNIT, BIGGEST_ALIGNMENT));
2676 /* See if the target hook can handle this kind of object. */
2677 if (targetm.asm_out.integer (x, size, aligned_p))
2678 return true;
2680 /* If the object is a multi-byte one, try splitting it up. Split
2681 it into words it if is multi-word, otherwise split it into bytes. */
2682 if (size > 1)
2684 machine_mode omode, imode;
2685 unsigned int subalign;
2686 unsigned int subsize, i;
2687 enum mode_class mclass;
2689 subsize = size > UNITS_PER_WORD? UNITS_PER_WORD : 1;
2690 subalign = MIN (align, subsize * BITS_PER_UNIT);
2691 if (GET_CODE (x) == CONST_FIXED)
2692 mclass = GET_MODE_CLASS (GET_MODE (x));
2693 else
2694 mclass = MODE_INT;
2696 omode = mode_for_size (subsize * BITS_PER_UNIT, mclass, 0);
2697 imode = mode_for_size (size * BITS_PER_UNIT, mclass, 0);
2699 for (i = 0; i < size; i += subsize)
2701 rtx partial = simplify_subreg (omode, x, imode, i);
2702 if (!partial || !assemble_integer (partial, subsize, subalign, 0))
2703 break;
2705 if (i == size)
2706 return true;
2708 /* If we've printed some of it, but not all of it, there's no going
2709 back now. */
2710 gcc_assert (!i);
2713 gcc_assert (!force);
2715 return false;
2718 void
2719 assemble_real (REAL_VALUE_TYPE d, machine_mode mode, unsigned int align)
2721 long data[4] = {0, 0, 0, 0};
2722 int i;
2723 int bitsize, nelts, nunits, units_per;
2725 /* This is hairy. We have a quantity of known size. real_to_target
2726 will put it into an array of *host* longs, 32 bits per element
2727 (even if long is more than 32 bits). We need to determine the
2728 number of array elements that are occupied (nelts) and the number
2729 of *target* min-addressable units that will be occupied in the
2730 object file (nunits). We cannot assume that 32 divides the
2731 mode's bitsize (size * BITS_PER_UNIT) evenly.
2733 size * BITS_PER_UNIT is used here to make sure that padding bits
2734 (which might appear at either end of the value; real_to_target
2735 will include the padding bits in its output array) are included. */
2737 nunits = GET_MODE_SIZE (mode);
2738 bitsize = nunits * BITS_PER_UNIT;
2739 nelts = CEIL (bitsize, 32);
2740 units_per = 32 / BITS_PER_UNIT;
2742 real_to_target (data, &d, mode);
2744 /* Put out the first word with the specified alignment. */
2745 assemble_integer (GEN_INT (data[0]), MIN (nunits, units_per), align, 1);
2746 nunits -= units_per;
2748 /* Subsequent words need only 32-bit alignment. */
2749 align = min_align (align, 32);
2751 for (i = 1; i < nelts; i++)
2753 assemble_integer (GEN_INT (data[i]), MIN (nunits, units_per), align, 1);
2754 nunits -= units_per;
2758 /* Given an expression EXP with a constant value,
2759 reduce it to the sum of an assembler symbol and an integer.
2760 Store them both in the structure *VALUE.
2761 EXP must be reducible. */
2763 struct addr_const {
2764 rtx base;
2765 HOST_WIDE_INT offset;
2768 static void
2769 decode_addr_const (tree exp, struct addr_const *value)
2771 tree target = TREE_OPERAND (exp, 0);
2772 int offset = 0;
2773 rtx x;
2775 while (1)
2777 if (TREE_CODE (target) == COMPONENT_REF
2778 && tree_fits_shwi_p (byte_position (TREE_OPERAND (target, 1))))
2780 offset += int_byte_position (TREE_OPERAND (target, 1));
2781 target = TREE_OPERAND (target, 0);
2783 else if (TREE_CODE (target) == ARRAY_REF
2784 || TREE_CODE (target) == ARRAY_RANGE_REF)
2786 offset += (tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (target)))
2787 * tree_to_shwi (TREE_OPERAND (target, 1)));
2788 target = TREE_OPERAND (target, 0);
2790 else if (TREE_CODE (target) == MEM_REF
2791 && TREE_CODE (TREE_OPERAND (target, 0)) == ADDR_EXPR)
2793 offset += mem_ref_offset (target).to_short_addr ();
2794 target = TREE_OPERAND (TREE_OPERAND (target, 0), 0);
2796 else if (TREE_CODE (target) == INDIRECT_REF
2797 && TREE_CODE (TREE_OPERAND (target, 0)) == NOP_EXPR
2798 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (target, 0), 0))
2799 == ADDR_EXPR)
2800 target = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (target, 0), 0), 0);
2801 else
2802 break;
2805 switch (TREE_CODE (target))
2807 case VAR_DECL:
2808 case FUNCTION_DECL:
2809 x = DECL_RTL (target);
2810 break;
2812 case LABEL_DECL:
2813 x = gen_rtx_MEM (FUNCTION_MODE,
2814 gen_rtx_LABEL_REF (Pmode, force_label_rtx (target)));
2815 break;
2817 case REAL_CST:
2818 case FIXED_CST:
2819 case STRING_CST:
2820 case COMPLEX_CST:
2821 case CONSTRUCTOR:
2822 case INTEGER_CST:
2823 x = output_constant_def (target, 1);
2824 break;
2826 default:
2827 gcc_unreachable ();
2830 gcc_assert (MEM_P (x));
2831 x = XEXP (x, 0);
2833 value->base = x;
2834 value->offset = offset;
2837 static GTY(()) hash_table<tree_descriptor_hasher> *const_desc_htab;
2839 static void maybe_output_constant_def_contents (struct constant_descriptor_tree *, int);
2841 /* Constant pool accessor function. */
2843 hash_table<tree_descriptor_hasher> *
2844 constant_pool_htab (void)
2846 return const_desc_htab;
2849 /* Compute a hash code for a constant expression. */
2851 hashval_t
2852 tree_descriptor_hasher::hash (constant_descriptor_tree *ptr)
2854 return ptr->hash;
2857 static hashval_t
2858 const_hash_1 (const tree exp)
2860 const char *p;
2861 hashval_t hi;
2862 int len, i;
2863 enum tree_code code = TREE_CODE (exp);
2865 /* Either set P and LEN to the address and len of something to hash and
2866 exit the switch or return a value. */
2868 switch (code)
2870 case INTEGER_CST:
2871 p = (char *) &TREE_INT_CST_ELT (exp, 0);
2872 len = TREE_INT_CST_NUNITS (exp) * sizeof (HOST_WIDE_INT);
2873 break;
2875 case REAL_CST:
2876 return real_hash (TREE_REAL_CST_PTR (exp));
2878 case FIXED_CST:
2879 return fixed_hash (TREE_FIXED_CST_PTR (exp));
2881 case STRING_CST:
2882 p = TREE_STRING_POINTER (exp);
2883 len = TREE_STRING_LENGTH (exp);
2884 break;
2886 case COMPLEX_CST:
2887 return (const_hash_1 (TREE_REALPART (exp)) * 5
2888 + const_hash_1 (TREE_IMAGPART (exp)));
2890 case VECTOR_CST:
2892 unsigned i;
2894 hi = 7 + VECTOR_CST_NELTS (exp);
2896 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
2897 hi = hi * 563 + const_hash_1 (VECTOR_CST_ELT (exp, i));
2899 return hi;
2902 case CONSTRUCTOR:
2904 unsigned HOST_WIDE_INT idx;
2905 tree value;
2907 hi = 5 + int_size_in_bytes (TREE_TYPE (exp));
2909 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
2910 if (value)
2911 hi = hi * 603 + const_hash_1 (value);
2913 return hi;
2916 case ADDR_EXPR:
2917 case FDESC_EXPR:
2919 struct addr_const value;
2921 decode_addr_const (exp, &value);
2922 switch (GET_CODE (value.base))
2924 case SYMBOL_REF:
2925 /* Don't hash the address of the SYMBOL_REF;
2926 only use the offset and the symbol name. */
2927 hi = value.offset;
2928 p = XSTR (value.base, 0);
2929 for (i = 0; p[i] != 0; i++)
2930 hi = ((hi * 613) + (unsigned) (p[i]));
2931 break;
2933 case LABEL_REF:
2934 hi = (value.offset
2935 + CODE_LABEL_NUMBER (LABEL_REF_LABEL (value.base)) * 13);
2936 break;
2938 default:
2939 gcc_unreachable ();
2942 return hi;
2944 case PLUS_EXPR:
2945 case POINTER_PLUS_EXPR:
2946 case MINUS_EXPR:
2947 return (const_hash_1 (TREE_OPERAND (exp, 0)) * 9
2948 + const_hash_1 (TREE_OPERAND (exp, 1)));
2950 CASE_CONVERT:
2951 return const_hash_1 (TREE_OPERAND (exp, 0)) * 7 + 2;
2953 default:
2954 /* A language specific constant. Just hash the code. */
2955 return code;
2958 /* Compute hashing function. */
2959 hi = len;
2960 for (i = 0; i < len; i++)
2961 hi = ((hi * 613) + (unsigned) (p[i]));
2963 return hi;
2966 /* Wrapper of compare_constant, for the htab interface. */
2967 bool
2968 tree_descriptor_hasher::equal (constant_descriptor_tree *c1,
2969 constant_descriptor_tree *c2)
2971 if (c1->hash != c2->hash)
2972 return 0;
2973 return compare_constant (c1->value, c2->value);
2976 /* Compare t1 and t2, and return 1 only if they are known to result in
2977 the same bit pattern on output. */
2979 static int
2980 compare_constant (const tree t1, const tree t2)
2982 enum tree_code typecode;
2984 if (t1 == NULL_TREE)
2985 return t2 == NULL_TREE;
2986 if (t2 == NULL_TREE)
2987 return 0;
2989 if (TREE_CODE (t1) != TREE_CODE (t2))
2990 return 0;
2992 switch (TREE_CODE (t1))
2994 case INTEGER_CST:
2995 /* Integer constants are the same only if the same width of type. */
2996 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
2997 return 0;
2998 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
2999 return 0;
3000 return tree_int_cst_equal (t1, t2);
3002 case REAL_CST:
3003 /* Real constants are the same only if the same width of type. */
3004 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3005 return 0;
3007 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
3009 case FIXED_CST:
3010 /* Fixed constants are the same only if the same width of type. */
3011 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3012 return 0;
3014 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
3016 case STRING_CST:
3017 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
3018 return 0;
3020 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
3021 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
3022 TREE_STRING_LENGTH (t1)));
3024 case COMPLEX_CST:
3025 return (compare_constant (TREE_REALPART (t1), TREE_REALPART (t2))
3026 && compare_constant (TREE_IMAGPART (t1), TREE_IMAGPART (t2)));
3028 case VECTOR_CST:
3030 unsigned i;
3032 if (VECTOR_CST_NELTS (t1) != VECTOR_CST_NELTS (t2))
3033 return 0;
3035 for (i = 0; i < VECTOR_CST_NELTS (t1); ++i)
3036 if (!compare_constant (VECTOR_CST_ELT (t1, i),
3037 VECTOR_CST_ELT (t2, i)))
3038 return 0;
3040 return 1;
3043 case CONSTRUCTOR:
3045 vec<constructor_elt, va_gc> *v1, *v2;
3046 unsigned HOST_WIDE_INT idx;
3048 typecode = TREE_CODE (TREE_TYPE (t1));
3049 if (typecode != TREE_CODE (TREE_TYPE (t2)))
3050 return 0;
3052 if (typecode == ARRAY_TYPE)
3054 HOST_WIDE_INT size_1 = int_size_in_bytes (TREE_TYPE (t1));
3055 /* For arrays, check that the sizes all match. */
3056 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))
3057 || size_1 == -1
3058 || size_1 != int_size_in_bytes (TREE_TYPE (t2)))
3059 return 0;
3061 else
3063 /* For record and union constructors, require exact type
3064 equality. */
3065 if (TREE_TYPE (t1) != TREE_TYPE (t2))
3066 return 0;
3069 v1 = CONSTRUCTOR_ELTS (t1);
3070 v2 = CONSTRUCTOR_ELTS (t2);
3071 if (vec_safe_length (v1) != vec_safe_length (v2))
3072 return 0;
3074 for (idx = 0; idx < vec_safe_length (v1); ++idx)
3076 constructor_elt *c1 = &(*v1)[idx];
3077 constructor_elt *c2 = &(*v2)[idx];
3079 /* Check that each value is the same... */
3080 if (!compare_constant (c1->value, c2->value))
3081 return 0;
3082 /* ... and that they apply to the same fields! */
3083 if (typecode == ARRAY_TYPE)
3085 if (!compare_constant (c1->index, c2->index))
3086 return 0;
3088 else
3090 if (c1->index != c2->index)
3091 return 0;
3095 return 1;
3098 case ADDR_EXPR:
3099 case FDESC_EXPR:
3101 struct addr_const value1, value2;
3102 enum rtx_code code;
3103 int ret;
3105 decode_addr_const (t1, &value1);
3106 decode_addr_const (t2, &value2);
3108 if (value1.offset != value2.offset)
3109 return 0;
3111 code = GET_CODE (value1.base);
3112 if (code != GET_CODE (value2.base))
3113 return 0;
3115 switch (code)
3117 case SYMBOL_REF:
3118 ret = (strcmp (XSTR (value1.base, 0), XSTR (value2.base, 0)) == 0);
3119 break;
3121 case LABEL_REF:
3122 ret = (CODE_LABEL_NUMBER (LABEL_REF_LABEL (value1.base))
3123 == CODE_LABEL_NUMBER (LABEL_REF_LABEL (value2.base)));
3124 break;
3126 default:
3127 gcc_unreachable ();
3129 return ret;
3132 case PLUS_EXPR:
3133 case POINTER_PLUS_EXPR:
3134 case MINUS_EXPR:
3135 case RANGE_EXPR:
3136 return (compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
3137 && compare_constant (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
3139 CASE_CONVERT:
3140 case VIEW_CONVERT_EXPR:
3141 return compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3143 default:
3144 return 0;
3147 gcc_unreachable ();
3150 /* Return the section into which constant EXP should be placed. */
3152 static section *
3153 get_constant_section (tree exp, unsigned int align)
3155 return targetm.asm_out.select_section (exp,
3156 compute_reloc_for_constant (exp),
3157 align);
3160 /* Return the size of constant EXP in bytes. */
3162 static HOST_WIDE_INT
3163 get_constant_size (tree exp)
3165 HOST_WIDE_INT size;
3167 size = int_size_in_bytes (TREE_TYPE (exp));
3168 if (TREE_CODE (exp) == STRING_CST)
3169 size = MAX (TREE_STRING_LENGTH (exp), size);
3170 return size;
3173 /* Subroutine of output_constant_def:
3174 No constant equal to EXP is known to have been output.
3175 Make a constant descriptor to enter EXP in the hash table.
3176 Assign the label number and construct RTL to refer to the
3177 constant's location in memory.
3178 Caller is responsible for updating the hash table. */
3180 static struct constant_descriptor_tree *
3181 build_constant_desc (tree exp)
3183 struct constant_descriptor_tree *desc;
3184 rtx symbol, rtl;
3185 char label[256];
3186 int labelno;
3187 tree decl;
3189 desc = ggc_alloc<constant_descriptor_tree> ();
3190 desc->value = exp;
3192 /* Create a string containing the label name, in LABEL. */
3193 labelno = const_labelno++;
3194 ASM_GENERATE_INTERNAL_LABEL (label, "LC", labelno);
3196 /* Construct the VAR_DECL associated with the constant. */
3197 decl = build_decl (UNKNOWN_LOCATION, VAR_DECL, get_identifier (label),
3198 TREE_TYPE (exp));
3199 DECL_ARTIFICIAL (decl) = 1;
3200 DECL_IGNORED_P (decl) = 1;
3201 TREE_READONLY (decl) = 1;
3202 TREE_STATIC (decl) = 1;
3203 TREE_ADDRESSABLE (decl) = 1;
3204 /* We don't set the RTL yet as this would cause varpool to assume that the
3205 variable is referenced. Moreover, it would just be dropped in LTO mode.
3206 Instead we set the flag that will be recognized in make_decl_rtl. */
3207 DECL_IN_CONSTANT_POOL (decl) = 1;
3208 DECL_INITIAL (decl) = desc->value;
3209 /* ??? CONSTANT_ALIGNMENT hasn't been updated for vector types on most
3210 architectures so use DATA_ALIGNMENT as well, except for strings. */
3211 if (TREE_CODE (exp) == STRING_CST)
3213 #ifdef CONSTANT_ALIGNMENT
3214 DECL_ALIGN (decl) = CONSTANT_ALIGNMENT (exp, DECL_ALIGN (decl));
3215 #endif
3217 else
3218 align_variable (decl, 0);
3220 /* Now construct the SYMBOL_REF and the MEM. */
3221 if (use_object_blocks_p ())
3223 section *sect = get_constant_section (exp, DECL_ALIGN (decl));
3224 symbol = create_block_symbol (ggc_strdup (label),
3225 get_block_for_section (sect), -1);
3227 else
3228 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3229 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3230 SET_SYMBOL_REF_DECL (symbol, decl);
3231 TREE_CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3233 rtl = gen_const_mem (TYPE_MODE (TREE_TYPE (exp)), symbol);
3234 set_mem_attributes (rtl, exp, 1);
3235 set_mem_alias_set (rtl, 0);
3237 /* We cannot share RTX'es in pool entries.
3238 Mark this piece of RTL as required for unsharing. */
3239 RTX_FLAG (rtl, used) = 1;
3241 /* Set flags or add text to the name to record information, such as
3242 that it is a local symbol. If the name is changed, the macro
3243 ASM_OUTPUT_LABELREF will have to know how to strip this
3244 information. This call might invalidate our local variable
3245 SYMBOL; we can't use it afterward. */
3246 targetm.encode_section_info (exp, rtl, true);
3248 desc->rtl = rtl;
3250 return desc;
3253 /* Return an rtx representing a reference to constant data in memory
3254 for the constant expression EXP.
3256 If assembler code for such a constant has already been output,
3257 return an rtx to refer to it.
3258 Otherwise, output such a constant in memory
3259 and generate an rtx for it.
3261 If DEFER is nonzero, this constant can be deferred and output only
3262 if referenced in the function after all optimizations.
3264 `const_desc_table' records which constants already have label strings. */
3267 output_constant_def (tree exp, int defer)
3269 struct constant_descriptor_tree *desc;
3270 struct constant_descriptor_tree key;
3272 /* Look up EXP in the table of constant descriptors. If we didn't find
3273 it, create a new one. */
3274 key.value = exp;
3275 key.hash = const_hash_1 (exp);
3276 constant_descriptor_tree **loc
3277 = const_desc_htab->find_slot_with_hash (&key, key.hash, INSERT);
3279 desc = *loc;
3280 if (desc == 0)
3282 desc = build_constant_desc (exp);
3283 desc->hash = key.hash;
3284 *loc = desc;
3287 maybe_output_constant_def_contents (desc, defer);
3288 return desc->rtl;
3291 /* Subroutine of output_constant_def: Decide whether or not we need to
3292 output the constant DESC now, and if so, do it. */
3293 static void
3294 maybe_output_constant_def_contents (struct constant_descriptor_tree *desc,
3295 int defer)
3297 rtx symbol = XEXP (desc->rtl, 0);
3298 tree exp = desc->value;
3300 if (flag_syntax_only)
3301 return;
3303 if (TREE_ASM_WRITTEN (exp))
3304 /* Already output; don't do it again. */
3305 return;
3307 /* We can always defer constants as long as the context allows
3308 doing so. */
3309 if (defer)
3311 /* Increment n_deferred_constants if it exists. It needs to be at
3312 least as large as the number of constants actually referred to
3313 by the function. If it's too small we'll stop looking too early
3314 and fail to emit constants; if it's too large we'll only look
3315 through the entire function when we could have stopped earlier. */
3316 if (cfun)
3317 n_deferred_constants++;
3318 return;
3321 output_constant_def_contents (symbol);
3324 /* Subroutine of output_constant_def_contents. Output the definition
3325 of constant EXP, which is pointed to by label LABEL. ALIGN is the
3326 constant's alignment in bits. */
3328 static void
3329 assemble_constant_contents (tree exp, const char *label, unsigned int align)
3331 HOST_WIDE_INT size;
3333 size = get_constant_size (exp);
3335 /* Do any machine/system dependent processing of the constant. */
3336 targetm.asm_out.declare_constant_name (asm_out_file, label, exp, size);
3338 /* Output the value of EXP. */
3339 output_constant (exp, size, align);
3342 /* We must output the constant data referred to by SYMBOL; do so. */
3344 static void
3345 output_constant_def_contents (rtx symbol)
3347 tree decl = SYMBOL_REF_DECL (symbol);
3348 tree exp = DECL_INITIAL (decl);
3349 unsigned int align;
3350 bool asan_protected = false;
3352 /* Make sure any other constants whose addresses appear in EXP
3353 are assigned label numbers. */
3354 output_addressed_constants (exp);
3356 /* We are no longer deferring this constant. */
3357 TREE_ASM_WRITTEN (decl) = TREE_ASM_WRITTEN (exp) = 1;
3359 if ((flag_sanitize & SANITIZE_ADDRESS)
3360 && TREE_CODE (exp) == STRING_CST
3361 && asan_protect_global (exp))
3363 asan_protected = true;
3364 DECL_ALIGN (decl) = MAX (DECL_ALIGN (decl),
3365 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
3368 /* If the constant is part of an object block, make sure that the
3369 decl has been positioned within its block, but do not write out
3370 its definition yet. output_object_blocks will do that later. */
3371 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
3372 place_block_symbol (symbol);
3373 else
3375 align = DECL_ALIGN (decl);
3376 switch_to_section (get_constant_section (exp, align));
3377 if (align > BITS_PER_UNIT)
3378 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
3379 assemble_constant_contents (exp, XSTR (symbol, 0), align);
3380 if (asan_protected)
3382 HOST_WIDE_INT size = get_constant_size (exp);
3383 assemble_zeros (asan_red_zone_size (size));
3388 /* Look up EXP in the table of constant descriptors. Return the rtl
3389 if it has been emitted, else null. */
3392 lookup_constant_def (tree exp)
3394 struct constant_descriptor_tree key;
3396 key.value = exp;
3397 key.hash = const_hash_1 (exp);
3398 constant_descriptor_tree *desc
3399 = const_desc_htab->find_with_hash (&key, key.hash);
3401 return (desc ? desc->rtl : NULL_RTX);
3404 /* Return a tree representing a reference to constant data in memory
3405 for the constant expression EXP.
3407 This is the counterpart of output_constant_def at the Tree level. */
3409 tree
3410 tree_output_constant_def (tree exp)
3412 struct constant_descriptor_tree *desc, key;
3413 tree decl;
3415 /* Look up EXP in the table of constant descriptors. If we didn't find
3416 it, create a new one. */
3417 key.value = exp;
3418 key.hash = const_hash_1 (exp);
3419 constant_descriptor_tree **loc
3420 = const_desc_htab->find_slot_with_hash (&key, key.hash, INSERT);
3422 desc = *loc;
3423 if (desc == 0)
3425 desc = build_constant_desc (exp);
3426 desc->hash = key.hash;
3427 *loc = desc;
3430 decl = SYMBOL_REF_DECL (XEXP (desc->rtl, 0));
3431 varpool_node::finalize_decl (decl);
3432 return decl;
3435 struct GTY((chain_next ("%h.next"), for_user)) constant_descriptor_rtx {
3436 struct constant_descriptor_rtx *next;
3437 rtx mem;
3438 rtx sym;
3439 rtx constant;
3440 HOST_WIDE_INT offset;
3441 hashval_t hash;
3442 machine_mode mode;
3443 unsigned int align;
3444 int labelno;
3445 int mark;
3448 struct const_rtx_desc_hasher : ggc_hasher<constant_descriptor_rtx *>
3450 static hashval_t hash (constant_descriptor_rtx *);
3451 static bool equal (constant_descriptor_rtx *, constant_descriptor_rtx *);
3454 /* Used in the hash tables to avoid outputting the same constant
3455 twice. Unlike 'struct constant_descriptor_tree', RTX constants
3456 are output once per function, not once per file. */
3457 /* ??? Only a few targets need per-function constant pools. Most
3458 can use one per-file pool. Should add a targetm bit to tell the
3459 difference. */
3461 struct GTY(()) rtx_constant_pool {
3462 /* Pointers to first and last constant in pool, as ordered by offset. */
3463 struct constant_descriptor_rtx *first;
3464 struct constant_descriptor_rtx *last;
3466 /* Hash facility for making memory-constants from constant rtl-expressions.
3467 It is used on RISC machines where immediate integer arguments and
3468 constant addresses are restricted so that such constants must be stored
3469 in memory. */
3470 hash_table<const_rtx_desc_hasher> *const_rtx_htab;
3472 /* Current offset in constant pool (does not include any
3473 machine-specific header). */
3474 HOST_WIDE_INT offset;
3477 /* Hash and compare functions for const_rtx_htab. */
3479 hashval_t
3480 const_rtx_desc_hasher::hash (constant_descriptor_rtx *desc)
3482 return desc->hash;
3485 bool
3486 const_rtx_desc_hasher::equal (constant_descriptor_rtx *x,
3487 constant_descriptor_rtx *y)
3489 if (x->mode != y->mode)
3490 return 0;
3491 return rtx_equal_p (x->constant, y->constant);
3494 /* Hash one component of a constant. */
3496 static hashval_t
3497 const_rtx_hash_1 (const_rtx x)
3499 unsigned HOST_WIDE_INT hwi;
3500 machine_mode mode;
3501 enum rtx_code code;
3502 hashval_t h;
3503 int i;
3505 code = GET_CODE (x);
3506 mode = GET_MODE (x);
3507 h = (hashval_t) code * 1048573 + mode;
3509 switch (code)
3511 case CONST_INT:
3512 hwi = INTVAL (x);
3514 fold_hwi:
3516 int shift = sizeof (hashval_t) * CHAR_BIT;
3517 const int n = sizeof (HOST_WIDE_INT) / sizeof (hashval_t);
3519 h ^= (hashval_t) hwi;
3520 for (i = 1; i < n; ++i)
3522 hwi >>= shift;
3523 h ^= (hashval_t) hwi;
3526 break;
3528 case CONST_WIDE_INT:
3529 hwi = GET_MODE_PRECISION (mode);
3531 for (i = 0; i < CONST_WIDE_INT_NUNITS (x); i++)
3532 hwi ^= CONST_WIDE_INT_ELT (x, i);
3533 goto fold_hwi;
3536 case CONST_DOUBLE:
3537 if (TARGET_SUPPORTS_WIDE_INT == 0 && mode == VOIDmode)
3539 hwi = CONST_DOUBLE_LOW (x) ^ CONST_DOUBLE_HIGH (x);
3540 goto fold_hwi;
3542 else
3543 h ^= real_hash (CONST_DOUBLE_REAL_VALUE (x));
3544 break;
3546 case CONST_FIXED:
3547 h ^= fixed_hash (CONST_FIXED_VALUE (x));
3548 break;
3550 case SYMBOL_REF:
3551 h ^= htab_hash_string (XSTR (x, 0));
3552 break;
3554 case LABEL_REF:
3555 h = h * 251 + CODE_LABEL_NUMBER (LABEL_REF_LABEL (x));
3556 break;
3558 case UNSPEC:
3559 case UNSPEC_VOLATILE:
3560 h = h * 251 + XINT (x, 1);
3561 break;
3563 default:
3564 break;
3567 return h;
3570 /* Compute a hash value for X, which should be a constant. */
3572 static hashval_t
3573 const_rtx_hash (rtx x)
3575 hashval_t h = 0;
3576 subrtx_iterator::array_type array;
3577 FOR_EACH_SUBRTX (iter, array, x, ALL)
3578 h = h * 509 + const_rtx_hash_1 (*iter);
3579 return h;
3583 /* Create and return a new rtx constant pool. */
3585 static struct rtx_constant_pool *
3586 create_constant_pool (void)
3588 struct rtx_constant_pool *pool;
3590 pool = ggc_alloc<rtx_constant_pool> ();
3591 pool->const_rtx_htab = hash_table<const_rtx_desc_hasher>::create_ggc (31);
3592 pool->first = NULL;
3593 pool->last = NULL;
3594 pool->offset = 0;
3595 return pool;
3598 /* Initialize constant pool hashing for a new function. */
3600 void
3601 init_varasm_status (void)
3603 crtl->varasm.pool = create_constant_pool ();
3604 crtl->varasm.deferred_constants = 0;
3607 /* Given a MINUS expression, simplify it if both sides
3608 include the same symbol. */
3611 simplify_subtraction (rtx x)
3613 rtx r = simplify_rtx (x);
3614 return r ? r : x;
3617 /* Given a constant rtx X, make (or find) a memory constant for its value
3618 and return a MEM rtx to refer to it in memory. */
3621 force_const_mem (machine_mode mode, rtx x)
3623 struct constant_descriptor_rtx *desc, tmp;
3624 struct rtx_constant_pool *pool;
3625 char label[256];
3626 rtx def, symbol;
3627 hashval_t hash;
3628 unsigned int align;
3629 constant_descriptor_rtx **slot;
3631 /* If we're not allowed to drop X into the constant pool, don't. */
3632 if (targetm.cannot_force_const_mem (mode, x))
3633 return NULL_RTX;
3635 /* Record that this function has used a constant pool entry. */
3636 crtl->uses_const_pool = 1;
3638 /* Decide which pool to use. */
3639 pool = (targetm.use_blocks_for_constant_p (mode, x)
3640 ? shared_constant_pool
3641 : crtl->varasm.pool);
3643 /* Lookup the value in the hashtable. */
3644 tmp.constant = x;
3645 tmp.mode = mode;
3646 hash = const_rtx_hash (x);
3647 slot = pool->const_rtx_htab->find_slot_with_hash (&tmp, hash, INSERT);
3648 desc = *slot;
3650 /* If the constant was already present, return its memory. */
3651 if (desc)
3652 return copy_rtx (desc->mem);
3654 /* Otherwise, create a new descriptor. */
3655 desc = ggc_alloc<constant_descriptor_rtx> ();
3656 *slot = desc;
3658 /* Align the location counter as required by EXP's data type. */
3659 align = GET_MODE_ALIGNMENT (mode == VOIDmode ? word_mode : mode);
3660 #ifdef CONSTANT_ALIGNMENT
3662 tree type = lang_hooks.types.type_for_mode (mode, 0);
3663 if (type != NULL_TREE)
3664 align = CONSTANT_ALIGNMENT (make_tree (type, x), align);
3666 #endif
3668 pool->offset += (align / BITS_PER_UNIT) - 1;
3669 pool->offset &= ~ ((align / BITS_PER_UNIT) - 1);
3671 desc->next = NULL;
3672 desc->constant = copy_rtx (tmp.constant);
3673 desc->offset = pool->offset;
3674 desc->hash = hash;
3675 desc->mode = mode;
3676 desc->align = align;
3677 desc->labelno = const_labelno;
3678 desc->mark = 0;
3680 pool->offset += GET_MODE_SIZE (mode);
3681 if (pool->last)
3682 pool->last->next = desc;
3683 else
3684 pool->first = pool->last = desc;
3685 pool->last = desc;
3687 /* Create a string containing the label name, in LABEL. */
3688 ASM_GENERATE_INTERNAL_LABEL (label, "LC", const_labelno);
3689 ++const_labelno;
3691 /* Construct the SYMBOL_REF. Make sure to mark it as belonging to
3692 the constants pool. */
3693 if (use_object_blocks_p () && targetm.use_blocks_for_constant_p (mode, x))
3695 section *sect = targetm.asm_out.select_rtx_section (mode, x, align);
3696 symbol = create_block_symbol (ggc_strdup (label),
3697 get_block_for_section (sect), -1);
3699 else
3700 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3701 desc->sym = symbol;
3702 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3703 CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3704 SET_SYMBOL_REF_CONSTANT (symbol, desc);
3706 /* Construct the MEM. */
3707 desc->mem = def = gen_const_mem (mode, symbol);
3708 set_mem_attributes (def, lang_hooks.types.type_for_mode (mode, 0), 1);
3709 set_mem_align (def, align);
3711 /* If we're dropping a label to the constant pool, make sure we
3712 don't delete it. */
3713 if (GET_CODE (x) == LABEL_REF)
3714 LABEL_PRESERVE_P (XEXP (x, 0)) = 1;
3716 return copy_rtx (def);
3719 /* Given a constant pool SYMBOL_REF, return the corresponding constant. */
3722 get_pool_constant (const_rtx addr)
3724 return SYMBOL_REF_CONSTANT (addr)->constant;
3727 /* Given a constant pool SYMBOL_REF, return the corresponding constant
3728 and whether it has been output or not. */
3731 get_pool_constant_mark (rtx addr, bool *pmarked)
3733 struct constant_descriptor_rtx *desc;
3735 desc = SYMBOL_REF_CONSTANT (addr);
3736 *pmarked = (desc->mark != 0);
3737 return desc->constant;
3740 /* Similar, return the mode. */
3742 machine_mode
3743 get_pool_mode (const_rtx addr)
3745 return SYMBOL_REF_CONSTANT (addr)->mode;
3748 /* Return the size of the constant pool. */
3751 get_pool_size (void)
3753 return crtl->varasm.pool->offset;
3756 /* Worker function for output_constant_pool_1. Emit assembly for X
3757 in MODE with known alignment ALIGN. */
3759 static void
3760 output_constant_pool_2 (machine_mode mode, rtx x, unsigned int align)
3762 switch (GET_MODE_CLASS (mode))
3764 case MODE_FLOAT:
3765 case MODE_DECIMAL_FLOAT:
3767 REAL_VALUE_TYPE r;
3769 gcc_assert (CONST_DOUBLE_AS_FLOAT_P (x));
3770 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
3771 assemble_real (r, mode, align);
3772 break;
3775 case MODE_INT:
3776 case MODE_PARTIAL_INT:
3777 case MODE_FRACT:
3778 case MODE_UFRACT:
3779 case MODE_ACCUM:
3780 case MODE_UACCUM:
3781 assemble_integer (x, GET_MODE_SIZE (mode), align, 1);
3782 break;
3784 case MODE_VECTOR_FLOAT:
3785 case MODE_VECTOR_INT:
3786 case MODE_VECTOR_FRACT:
3787 case MODE_VECTOR_UFRACT:
3788 case MODE_VECTOR_ACCUM:
3789 case MODE_VECTOR_UACCUM:
3791 int i, units;
3792 machine_mode submode = GET_MODE_INNER (mode);
3793 unsigned int subalign = MIN (align, GET_MODE_BITSIZE (submode));
3795 gcc_assert (GET_CODE (x) == CONST_VECTOR);
3796 units = CONST_VECTOR_NUNITS (x);
3798 for (i = 0; i < units; i++)
3800 rtx elt = CONST_VECTOR_ELT (x, i);
3801 output_constant_pool_2 (submode, elt, i ? subalign : align);
3804 break;
3806 default:
3807 gcc_unreachable ();
3811 /* Worker function for output_constant_pool. Emit constant DESC,
3812 giving it ALIGN bits of alignment. */
3814 static void
3815 output_constant_pool_1 (struct constant_descriptor_rtx *desc,
3816 unsigned int align)
3818 rtx x, tmp;
3820 x = desc->constant;
3822 /* See if X is a LABEL_REF (or a CONST referring to a LABEL_REF)
3823 whose CODE_LABEL has been deleted. This can occur if a jump table
3824 is eliminated by optimization. If so, write a constant of zero
3825 instead. Note that this can also happen by turning the
3826 CODE_LABEL into a NOTE. */
3827 /* ??? This seems completely and utterly wrong. Certainly it's
3828 not true for NOTE_INSN_DELETED_LABEL, but I disbelieve proper
3829 functioning even with rtx_insn::deleted and friends. */
3831 tmp = x;
3832 switch (GET_CODE (tmp))
3834 case CONST:
3835 if (GET_CODE (XEXP (tmp, 0)) != PLUS
3836 || GET_CODE (XEXP (XEXP (tmp, 0), 0)) != LABEL_REF)
3837 break;
3838 tmp = XEXP (XEXP (tmp, 0), 0);
3839 /* FALLTHRU */
3841 case LABEL_REF:
3842 tmp = LABEL_REF_LABEL (tmp);
3843 gcc_assert (!as_a<rtx_insn *> (tmp)->deleted ());
3844 gcc_assert (!NOTE_P (tmp)
3845 || NOTE_KIND (tmp) != NOTE_INSN_DELETED);
3846 break;
3848 default:
3849 break;
3852 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3853 ASM_OUTPUT_SPECIAL_POOL_ENTRY (asm_out_file, x, desc->mode,
3854 align, desc->labelno, done);
3855 #endif
3857 assemble_align (align);
3859 /* Output the label. */
3860 targetm.asm_out.internal_label (asm_out_file, "LC", desc->labelno);
3862 /* Output the data. */
3863 output_constant_pool_2 (desc->mode, x, align);
3865 /* Make sure all constants in SECTION_MERGE and not SECTION_STRINGS
3866 sections have proper size. */
3867 if (align > GET_MODE_BITSIZE (desc->mode)
3868 && in_section
3869 && (in_section->common.flags & SECTION_MERGE))
3870 assemble_align (align);
3872 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3873 done:
3874 #endif
3875 return;
3878 /* Mark all constants that are referenced by SYMBOL_REFs in X.
3879 Emit referenced deferred strings. */
3881 static void
3882 mark_constants_in_pattern (rtx insn)
3884 subrtx_iterator::array_type array;
3885 FOR_EACH_SUBRTX (iter, array, PATTERN (insn), ALL)
3887 const_rtx x = *iter;
3888 if (GET_CODE (x) == SYMBOL_REF)
3890 if (CONSTANT_POOL_ADDRESS_P (x))
3892 struct constant_descriptor_rtx *desc = SYMBOL_REF_CONSTANT (x);
3893 if (desc->mark == 0)
3895 desc->mark = 1;
3896 iter.substitute (desc->constant);
3899 else if (TREE_CONSTANT_POOL_ADDRESS_P (x))
3901 tree decl = SYMBOL_REF_DECL (x);
3902 if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
3904 n_deferred_constants--;
3905 output_constant_def_contents (CONST_CAST_RTX (x));
3912 /* Look through appropriate parts of INSN, marking all entries in the
3913 constant pool which are actually being used. Entries that are only
3914 referenced by other constants are also marked as used. Emit
3915 deferred strings that are used. */
3917 static void
3918 mark_constants (rtx_insn *insn)
3920 if (!INSN_P (insn))
3921 return;
3923 /* Insns may appear inside a SEQUENCE. Only check the patterns of
3924 insns, not any notes that may be attached. We don't want to mark
3925 a constant just because it happens to appear in a REG_EQUIV note. */
3926 if (rtx_sequence *seq = dyn_cast <rtx_sequence *> (PATTERN (insn)))
3928 int i, n = seq->len ();
3929 for (i = 0; i < n; ++i)
3931 rtx subinsn = seq->element (i);
3932 if (INSN_P (subinsn))
3933 mark_constants_in_pattern (subinsn);
3936 else
3937 mark_constants_in_pattern (insn);
3940 /* Look through the instructions for this function, and mark all the
3941 entries in POOL which are actually being used. Emit deferred constants
3942 which have indeed been used. */
3944 static void
3945 mark_constant_pool (void)
3947 rtx_insn *insn;
3949 if (!crtl->uses_const_pool && n_deferred_constants == 0)
3950 return;
3952 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3953 mark_constants (insn);
3956 /* Write all the constants in POOL. */
3958 static void
3959 output_constant_pool_contents (struct rtx_constant_pool *pool)
3961 struct constant_descriptor_rtx *desc;
3963 for (desc = pool->first; desc ; desc = desc->next)
3964 if (desc->mark)
3966 /* If the constant is part of an object_block, make sure that
3967 the constant has been positioned within its block, but do not
3968 write out its definition yet. output_object_blocks will do
3969 that later. */
3970 if (SYMBOL_REF_HAS_BLOCK_INFO_P (desc->sym)
3971 && SYMBOL_REF_BLOCK (desc->sym))
3972 place_block_symbol (desc->sym);
3973 else
3975 switch_to_section (targetm.asm_out.select_rtx_section
3976 (desc->mode, desc->constant, desc->align));
3977 output_constant_pool_1 (desc, desc->align);
3982 /* Mark all constants that are used in the current function, then write
3983 out the function's private constant pool. */
3985 static void
3986 output_constant_pool (const char *fnname ATTRIBUTE_UNUSED,
3987 tree fndecl ATTRIBUTE_UNUSED)
3989 struct rtx_constant_pool *pool = crtl->varasm.pool;
3991 /* It is possible for gcc to call force_const_mem and then to later
3992 discard the instructions which refer to the constant. In such a
3993 case we do not need to output the constant. */
3994 mark_constant_pool ();
3996 #ifdef ASM_OUTPUT_POOL_PROLOGUE
3997 ASM_OUTPUT_POOL_PROLOGUE (asm_out_file, fnname, fndecl, pool->offset);
3998 #endif
4000 output_constant_pool_contents (pool);
4002 #ifdef ASM_OUTPUT_POOL_EPILOGUE
4003 ASM_OUTPUT_POOL_EPILOGUE (asm_out_file, fnname, fndecl, pool->offset);
4004 #endif
4007 /* Write the contents of the shared constant pool. */
4009 void
4010 output_shared_constant_pool (void)
4012 output_constant_pool_contents (shared_constant_pool);
4015 /* Determine what kind of relocations EXP may need. */
4018 compute_reloc_for_constant (tree exp)
4020 int reloc = 0, reloc2;
4021 tree tem;
4023 switch (TREE_CODE (exp))
4025 case ADDR_EXPR:
4026 case FDESC_EXPR:
4027 /* Go inside any operations that get_inner_reference can handle and see
4028 if what's inside is a constant: no need to do anything here for
4029 addresses of variables or functions. */
4030 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
4031 tem = TREE_OPERAND (tem, 0))
4034 if (TREE_CODE (tem) == MEM_REF
4035 && TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR)
4037 reloc = compute_reloc_for_constant (TREE_OPERAND (tem, 0));
4038 break;
4041 if (!targetm.binds_local_p (tem))
4042 reloc |= 2;
4043 else
4044 reloc |= 1;
4045 break;
4047 case PLUS_EXPR:
4048 case POINTER_PLUS_EXPR:
4049 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4050 reloc |= compute_reloc_for_constant (TREE_OPERAND (exp, 1));
4051 break;
4053 case MINUS_EXPR:
4054 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4055 reloc2 = compute_reloc_for_constant (TREE_OPERAND (exp, 1));
4056 /* The difference of two local labels is computable at link time. */
4057 if (reloc == 1 && reloc2 == 1)
4058 reloc = 0;
4059 else
4060 reloc |= reloc2;
4061 break;
4063 CASE_CONVERT:
4064 case VIEW_CONVERT_EXPR:
4065 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4066 break;
4068 case CONSTRUCTOR:
4070 unsigned HOST_WIDE_INT idx;
4071 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
4072 if (tem != 0)
4073 reloc |= compute_reloc_for_constant (tem);
4075 break;
4077 default:
4078 break;
4080 return reloc;
4083 /* Find all the constants whose addresses are referenced inside of EXP,
4084 and make sure assembler code with a label has been output for each one.
4085 Indicate whether an ADDR_EXPR has been encountered. */
4087 static void
4088 output_addressed_constants (tree exp)
4090 tree tem;
4092 switch (TREE_CODE (exp))
4094 case ADDR_EXPR:
4095 case FDESC_EXPR:
4096 /* Go inside any operations that get_inner_reference can handle and see
4097 if what's inside is a constant: no need to do anything here for
4098 addresses of variables or functions. */
4099 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
4100 tem = TREE_OPERAND (tem, 0))
4103 /* If we have an initialized CONST_DECL, retrieve the initializer. */
4104 if (TREE_CODE (tem) == CONST_DECL && DECL_INITIAL (tem))
4105 tem = DECL_INITIAL (tem);
4107 if (CONSTANT_CLASS_P (tem) || TREE_CODE (tem) == CONSTRUCTOR)
4108 output_constant_def (tem, 0);
4110 if (TREE_CODE (tem) == MEM_REF)
4111 output_addressed_constants (TREE_OPERAND (tem, 0));
4112 break;
4114 case PLUS_EXPR:
4115 case POINTER_PLUS_EXPR:
4116 case MINUS_EXPR:
4117 output_addressed_constants (TREE_OPERAND (exp, 1));
4118 /* Fall through. */
4120 CASE_CONVERT:
4121 case VIEW_CONVERT_EXPR:
4122 output_addressed_constants (TREE_OPERAND (exp, 0));
4123 break;
4125 case CONSTRUCTOR:
4127 unsigned HOST_WIDE_INT idx;
4128 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
4129 if (tem != 0)
4130 output_addressed_constants (tem);
4132 break;
4134 default:
4135 break;
4139 /* Whether a constructor CTOR is a valid static constant initializer if all
4140 its elements are. This used to be internal to initializer_constant_valid_p
4141 and has been exposed to let other functions like categorize_ctor_elements
4142 evaluate the property while walking a constructor for other purposes. */
4144 bool
4145 constructor_static_from_elts_p (const_tree ctor)
4147 return (TREE_CONSTANT (ctor)
4148 && (TREE_CODE (TREE_TYPE (ctor)) == UNION_TYPE
4149 || TREE_CODE (TREE_TYPE (ctor)) == RECORD_TYPE
4150 || TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE));
4153 static tree initializer_constant_valid_p_1 (tree value, tree endtype,
4154 tree *cache);
4156 /* A subroutine of initializer_constant_valid_p. VALUE is a MINUS_EXPR,
4157 PLUS_EXPR or POINTER_PLUS_EXPR. This looks for cases of VALUE
4158 which are valid when ENDTYPE is an integer of any size; in
4159 particular, this does not accept a pointer minus a constant. This
4160 returns null_pointer_node if the VALUE is an absolute constant
4161 which can be used to initialize a static variable. Otherwise it
4162 returns NULL. */
4164 static tree
4165 narrowing_initializer_constant_valid_p (tree value, tree endtype, tree *cache)
4167 tree op0, op1;
4169 if (!INTEGRAL_TYPE_P (endtype))
4170 return NULL_TREE;
4172 op0 = TREE_OPERAND (value, 0);
4173 op1 = TREE_OPERAND (value, 1);
4175 /* Like STRIP_NOPS except allow the operand mode to widen. This
4176 works around a feature of fold that simplifies (int)(p1 - p2) to
4177 ((int)p1 - (int)p2) under the theory that the narrower operation
4178 is cheaper. */
4180 while (CONVERT_EXPR_P (op0)
4181 || TREE_CODE (op0) == NON_LVALUE_EXPR)
4183 tree inner = TREE_OPERAND (op0, 0);
4184 if (inner == error_mark_node
4185 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4186 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))
4187 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
4188 break;
4189 op0 = inner;
4192 while (CONVERT_EXPR_P (op1)
4193 || TREE_CODE (op1) == NON_LVALUE_EXPR)
4195 tree inner = TREE_OPERAND (op1, 0);
4196 if (inner == error_mark_node
4197 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4198 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op1)))
4199 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
4200 break;
4201 op1 = inner;
4204 op0 = initializer_constant_valid_p_1 (op0, endtype, cache);
4205 if (!op0)
4206 return NULL_TREE;
4208 op1 = initializer_constant_valid_p_1 (op1, endtype,
4209 cache ? cache + 2 : NULL);
4210 /* Both initializers must be known. */
4211 if (op1)
4213 if (op0 == op1
4214 && (op0 == null_pointer_node
4215 || TREE_CODE (value) == MINUS_EXPR))
4216 return null_pointer_node;
4218 /* Support differences between labels. */
4219 if (TREE_CODE (op0) == LABEL_DECL
4220 && TREE_CODE (op1) == LABEL_DECL)
4221 return null_pointer_node;
4223 if (TREE_CODE (op0) == STRING_CST
4224 && TREE_CODE (op1) == STRING_CST
4225 && operand_equal_p (op0, op1, 1))
4226 return null_pointer_node;
4229 return NULL_TREE;
4232 /* Helper function of initializer_constant_valid_p.
4233 Return nonzero if VALUE is a valid constant-valued expression
4234 for use in initializing a static variable; one that can be an
4235 element of a "constant" initializer.
4237 Return null_pointer_node if the value is absolute;
4238 if it is relocatable, return the variable that determines the relocation.
4239 We assume that VALUE has been folded as much as possible;
4240 therefore, we do not need to check for such things as
4241 arithmetic-combinations of integers.
4243 Use CACHE (pointer to 2 tree values) for caching if non-NULL. */
4245 static tree
4246 initializer_constant_valid_p_1 (tree value, tree endtype, tree *cache)
4248 tree ret;
4250 switch (TREE_CODE (value))
4252 case CONSTRUCTOR:
4253 if (constructor_static_from_elts_p (value))
4255 unsigned HOST_WIDE_INT idx;
4256 tree elt;
4257 bool absolute = true;
4259 if (cache && cache[0] == value)
4260 return cache[1];
4261 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
4263 tree reloc;
4264 reloc = initializer_constant_valid_p_1 (elt, TREE_TYPE (elt),
4265 NULL);
4266 if (!reloc)
4268 if (cache)
4270 cache[0] = value;
4271 cache[1] = NULL_TREE;
4273 return NULL_TREE;
4275 if (reloc != null_pointer_node)
4276 absolute = false;
4278 /* For a non-absolute relocation, there is no single
4279 variable that can be "the variable that determines the
4280 relocation." */
4281 if (cache)
4283 cache[0] = value;
4284 cache[1] = absolute ? null_pointer_node : error_mark_node;
4286 return absolute ? null_pointer_node : error_mark_node;
4289 return TREE_STATIC (value) ? null_pointer_node : NULL_TREE;
4291 case INTEGER_CST:
4292 case VECTOR_CST:
4293 case REAL_CST:
4294 case FIXED_CST:
4295 case STRING_CST:
4296 case COMPLEX_CST:
4297 return null_pointer_node;
4299 case ADDR_EXPR:
4300 case FDESC_EXPR:
4302 tree op0 = staticp (TREE_OPERAND (value, 0));
4303 if (op0)
4305 /* "&(*a).f" is like unto pointer arithmetic. If "a" turns out
4306 to be a constant, this is old-skool offsetof-like nonsense. */
4307 if (TREE_CODE (op0) == INDIRECT_REF
4308 && TREE_CONSTANT (TREE_OPERAND (op0, 0)))
4309 return null_pointer_node;
4310 /* Taking the address of a nested function involves a trampoline,
4311 unless we don't need or want one. */
4312 if (TREE_CODE (op0) == FUNCTION_DECL
4313 && DECL_STATIC_CHAIN (op0)
4314 && !TREE_NO_TRAMPOLINE (value))
4315 return NULL_TREE;
4316 /* "&{...}" requires a temporary to hold the constructed
4317 object. */
4318 if (TREE_CODE (op0) == CONSTRUCTOR)
4319 return NULL_TREE;
4321 return op0;
4324 case NON_LVALUE_EXPR:
4325 return initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4326 endtype, cache);
4328 case VIEW_CONVERT_EXPR:
4330 tree src = TREE_OPERAND (value, 0);
4331 tree src_type = TREE_TYPE (src);
4332 tree dest_type = TREE_TYPE (value);
4334 /* Allow view-conversions from aggregate to non-aggregate type only
4335 if the bit pattern is fully preserved afterwards; otherwise, the
4336 RTL expander won't be able to apply a subsequent transformation
4337 to the underlying constructor. */
4338 if (AGGREGATE_TYPE_P (src_type) && !AGGREGATE_TYPE_P (dest_type))
4340 if (TYPE_MODE (endtype) == TYPE_MODE (dest_type))
4341 return initializer_constant_valid_p_1 (src, endtype, cache);
4342 else
4343 return NULL_TREE;
4346 /* Allow all other kinds of view-conversion. */
4347 return initializer_constant_valid_p_1 (src, endtype, cache);
4350 CASE_CONVERT:
4352 tree src = TREE_OPERAND (value, 0);
4353 tree src_type = TREE_TYPE (src);
4354 tree dest_type = TREE_TYPE (value);
4356 /* Allow conversions between pointer types, floating-point
4357 types, and offset types. */
4358 if ((POINTER_TYPE_P (dest_type) && POINTER_TYPE_P (src_type))
4359 || (FLOAT_TYPE_P (dest_type) && FLOAT_TYPE_P (src_type))
4360 || (TREE_CODE (dest_type) == OFFSET_TYPE
4361 && TREE_CODE (src_type) == OFFSET_TYPE))
4362 return initializer_constant_valid_p_1 (src, endtype, cache);
4364 /* Allow length-preserving conversions between integer types. */
4365 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type)
4366 && (TYPE_PRECISION (dest_type) == TYPE_PRECISION (src_type)))
4367 return initializer_constant_valid_p_1 (src, endtype, cache);
4369 /* Allow conversions between other integer types only if
4370 explicit value. */
4371 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type))
4373 tree inner = initializer_constant_valid_p_1 (src, endtype, cache);
4374 if (inner == null_pointer_node)
4375 return null_pointer_node;
4376 break;
4379 /* Allow (int) &foo provided int is as wide as a pointer. */
4380 if (INTEGRAL_TYPE_P (dest_type) && POINTER_TYPE_P (src_type)
4381 && (TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type)))
4382 return initializer_constant_valid_p_1 (src, endtype, cache);
4384 /* Likewise conversions from int to pointers, but also allow
4385 conversions from 0. */
4386 if ((POINTER_TYPE_P (dest_type)
4387 || TREE_CODE (dest_type) == OFFSET_TYPE)
4388 && INTEGRAL_TYPE_P (src_type))
4390 if (TREE_CODE (src) == INTEGER_CST
4391 && TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type))
4392 return null_pointer_node;
4393 if (integer_zerop (src))
4394 return null_pointer_node;
4395 else if (TYPE_PRECISION (dest_type) <= TYPE_PRECISION (src_type))
4396 return initializer_constant_valid_p_1 (src, endtype, cache);
4399 /* Allow conversions to struct or union types if the value
4400 inside is okay. */
4401 if (TREE_CODE (dest_type) == RECORD_TYPE
4402 || TREE_CODE (dest_type) == UNION_TYPE)
4403 return initializer_constant_valid_p_1 (src, endtype, cache);
4405 break;
4407 case POINTER_PLUS_EXPR:
4408 case PLUS_EXPR:
4409 /* Any valid floating-point constants will have been folded by now;
4410 with -frounding-math we hit this with addition of two constants. */
4411 if (TREE_CODE (endtype) == REAL_TYPE)
4412 return NULL_TREE;
4413 if (cache && cache[0] == value)
4414 return cache[1];
4415 if (! INTEGRAL_TYPE_P (endtype)
4416 || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
4418 tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
4419 tree valid0
4420 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4421 endtype, ncache);
4422 tree valid1
4423 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
4424 endtype, ncache + 2);
4425 /* If either term is absolute, use the other term's relocation. */
4426 if (valid0 == null_pointer_node)
4427 ret = valid1;
4428 else if (valid1 == null_pointer_node)
4429 ret = valid0;
4430 /* Support narrowing pointer differences. */
4431 else
4432 ret = narrowing_initializer_constant_valid_p (value, endtype,
4433 ncache);
4435 else
4436 /* Support narrowing pointer differences. */
4437 ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
4438 if (cache)
4440 cache[0] = value;
4441 cache[1] = ret;
4443 return ret;
4445 case MINUS_EXPR:
4446 if (TREE_CODE (endtype) == REAL_TYPE)
4447 return NULL_TREE;
4448 if (cache && cache[0] == value)
4449 return cache[1];
4450 if (! INTEGRAL_TYPE_P (endtype)
4451 || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
4453 tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
4454 tree valid0
4455 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4456 endtype, ncache);
4457 tree valid1
4458 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
4459 endtype, ncache + 2);
4460 /* Win if second argument is absolute. */
4461 if (valid1 == null_pointer_node)
4462 ret = valid0;
4463 /* Win if both arguments have the same relocation.
4464 Then the value is absolute. */
4465 else if (valid0 == valid1 && valid0 != 0)
4466 ret = null_pointer_node;
4467 /* Since GCC guarantees that string constants are unique in the
4468 generated code, a subtraction between two copies of the same
4469 constant string is absolute. */
4470 else if (valid0 && TREE_CODE (valid0) == STRING_CST
4471 && valid1 && TREE_CODE (valid1) == STRING_CST
4472 && operand_equal_p (valid0, valid1, 1))
4473 ret = null_pointer_node;
4474 /* Support narrowing differences. */
4475 else
4476 ret = narrowing_initializer_constant_valid_p (value, endtype,
4477 ncache);
4479 else
4480 /* Support narrowing differences. */
4481 ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
4482 if (cache)
4484 cache[0] = value;
4485 cache[1] = ret;
4487 return ret;
4489 default:
4490 break;
4493 return NULL_TREE;
4496 /* Return nonzero if VALUE is a valid constant-valued expression
4497 for use in initializing a static variable; one that can be an
4498 element of a "constant" initializer.
4500 Return null_pointer_node if the value is absolute;
4501 if it is relocatable, return the variable that determines the relocation.
4502 We assume that VALUE has been folded as much as possible;
4503 therefore, we do not need to check for such things as
4504 arithmetic-combinations of integers. */
4505 tree
4506 initializer_constant_valid_p (tree value, tree endtype)
4508 return initializer_constant_valid_p_1 (value, endtype, NULL);
4511 /* Return true if VALUE is a valid constant-valued expression
4512 for use in initializing a static bit-field; one that can be
4513 an element of a "constant" initializer. */
4515 bool
4516 initializer_constant_valid_for_bitfield_p (tree value)
4518 /* For bitfields we support integer constants or possibly nested aggregates
4519 of such. */
4520 switch (TREE_CODE (value))
4522 case CONSTRUCTOR:
4524 unsigned HOST_WIDE_INT idx;
4525 tree elt;
4527 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
4528 if (!initializer_constant_valid_for_bitfield_p (elt))
4529 return false;
4530 return true;
4533 case INTEGER_CST:
4534 case REAL_CST:
4535 return true;
4537 case VIEW_CONVERT_EXPR:
4538 case NON_LVALUE_EXPR:
4539 return
4540 initializer_constant_valid_for_bitfield_p (TREE_OPERAND (value, 0));
4542 default:
4543 break;
4546 return false;
4549 /* output_constructor outer state of relevance in recursive calls, typically
4550 for nested aggregate bitfields. */
4552 typedef struct {
4553 unsigned int bit_offset; /* current position in ... */
4554 int byte; /* ... the outer byte buffer. */
4555 } oc_outer_state;
4557 static unsigned HOST_WIDE_INT
4558 output_constructor (tree, unsigned HOST_WIDE_INT, unsigned int,
4559 oc_outer_state *);
4561 /* Output assembler code for constant EXP, with no label.
4562 This includes the pseudo-op such as ".int" or ".byte", and a newline.
4563 Assumes output_addressed_constants has been done on EXP already.
4565 Generate at least SIZE bytes of assembler data, padding at the end
4566 with zeros if necessary. SIZE must always be specified. The returned
4567 value is the actual number of bytes of assembler data generated, which
4568 may be bigger than SIZE if the object contains a variable length field.
4570 SIZE is important for structure constructors,
4571 since trailing members may have been omitted from the constructor.
4572 It is also important for initialization of arrays from string constants
4573 since the full length of the string constant might not be wanted.
4574 It is also needed for initialization of unions, where the initializer's
4575 type is just one member, and that may not be as long as the union.
4577 There a case in which we would fail to output exactly SIZE bytes:
4578 for a structure constructor that wants to produce more than SIZE bytes.
4579 But such constructors will never be generated for any possible input.
4581 ALIGN is the alignment of the data in bits. */
4583 static unsigned HOST_WIDE_INT
4584 output_constant (tree exp, unsigned HOST_WIDE_INT size, unsigned int align)
4586 enum tree_code code;
4587 unsigned HOST_WIDE_INT thissize;
4589 if (size == 0 || flag_syntax_only)
4590 return size;
4592 /* See if we're trying to initialize a pointer in a non-default mode
4593 to the address of some declaration somewhere. If the target says
4594 the mode is valid for pointers, assume the target has a way of
4595 resolving it. */
4596 if (TREE_CODE (exp) == NOP_EXPR
4597 && POINTER_TYPE_P (TREE_TYPE (exp))
4598 && targetm.addr_space.valid_pointer_mode
4599 (TYPE_MODE (TREE_TYPE (exp)),
4600 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
4602 tree saved_type = TREE_TYPE (exp);
4604 /* Peel off any intermediate conversions-to-pointer for valid
4605 pointer modes. */
4606 while (TREE_CODE (exp) == NOP_EXPR
4607 && POINTER_TYPE_P (TREE_TYPE (exp))
4608 && targetm.addr_space.valid_pointer_mode
4609 (TYPE_MODE (TREE_TYPE (exp)),
4610 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
4611 exp = TREE_OPERAND (exp, 0);
4613 /* If what we're left with is the address of something, we can
4614 convert the address to the final type and output it that
4615 way. */
4616 if (TREE_CODE (exp) == ADDR_EXPR)
4617 exp = build1 (ADDR_EXPR, saved_type, TREE_OPERAND (exp, 0));
4618 /* Likewise for constant ints. */
4619 else if (TREE_CODE (exp) == INTEGER_CST)
4620 exp = wide_int_to_tree (saved_type, exp);
4624 /* Eliminate any conversions since we'll be outputting the underlying
4625 constant. */
4626 while (CONVERT_EXPR_P (exp)
4627 || TREE_CODE (exp) == NON_LVALUE_EXPR
4628 || TREE_CODE (exp) == VIEW_CONVERT_EXPR)
4630 HOST_WIDE_INT type_size = int_size_in_bytes (TREE_TYPE (exp));
4631 HOST_WIDE_INT op_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (exp, 0)));
4633 /* Make sure eliminating the conversion is really a no-op, except with
4634 VIEW_CONVERT_EXPRs to allow for wild Ada unchecked conversions and
4635 union types to allow for Ada unchecked unions. */
4636 if (type_size > op_size
4637 && TREE_CODE (exp) != VIEW_CONVERT_EXPR
4638 && TREE_CODE (TREE_TYPE (exp)) != UNION_TYPE)
4639 /* Keep the conversion. */
4640 break;
4641 else
4642 exp = TREE_OPERAND (exp, 0);
4645 code = TREE_CODE (TREE_TYPE (exp));
4646 thissize = int_size_in_bytes (TREE_TYPE (exp));
4648 /* Allow a constructor with no elements for any data type.
4649 This means to fill the space with zeros. */
4650 if (TREE_CODE (exp) == CONSTRUCTOR
4651 && vec_safe_is_empty (CONSTRUCTOR_ELTS (exp)))
4653 assemble_zeros (size);
4654 return size;
4657 if (TREE_CODE (exp) == FDESC_EXPR)
4659 #ifdef ASM_OUTPUT_FDESC
4660 HOST_WIDE_INT part = tree_to_shwi (TREE_OPERAND (exp, 1));
4661 tree decl = TREE_OPERAND (exp, 0);
4662 ASM_OUTPUT_FDESC (asm_out_file, decl, part);
4663 #else
4664 gcc_unreachable ();
4665 #endif
4666 return size;
4669 /* Now output the underlying data. If we've handling the padding, return.
4670 Otherwise, break and ensure SIZE is the size written. */
4671 switch (code)
4673 case BOOLEAN_TYPE:
4674 case INTEGER_TYPE:
4675 case ENUMERAL_TYPE:
4676 case POINTER_TYPE:
4677 case REFERENCE_TYPE:
4678 case OFFSET_TYPE:
4679 case FIXED_POINT_TYPE:
4680 case NULLPTR_TYPE:
4681 if (! assemble_integer (expand_expr (exp, NULL_RTX, VOIDmode,
4682 EXPAND_INITIALIZER),
4683 MIN (size, thissize), align, 0))
4684 error ("initializer for integer/fixed-point value is too complicated");
4685 break;
4687 case REAL_TYPE:
4688 if (TREE_CODE (exp) != REAL_CST)
4689 error ("initializer for floating value is not a floating constant");
4690 else
4691 assemble_real (TREE_REAL_CST (exp), TYPE_MODE (TREE_TYPE (exp)), align);
4692 break;
4694 case COMPLEX_TYPE:
4695 output_constant (TREE_REALPART (exp), thissize / 2, align);
4696 output_constant (TREE_IMAGPART (exp), thissize / 2,
4697 min_align (align, BITS_PER_UNIT * (thissize / 2)));
4698 break;
4700 case ARRAY_TYPE:
4701 case VECTOR_TYPE:
4702 switch (TREE_CODE (exp))
4704 case CONSTRUCTOR:
4705 return output_constructor (exp, size, align, NULL);
4706 case STRING_CST:
4707 thissize
4708 = MIN ((unsigned HOST_WIDE_INT)TREE_STRING_LENGTH (exp), size);
4709 assemble_string (TREE_STRING_POINTER (exp), thissize);
4710 break;
4711 case VECTOR_CST:
4713 machine_mode inner = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
4714 unsigned int nalign = MIN (align, GET_MODE_ALIGNMENT (inner));
4715 int elt_size = GET_MODE_SIZE (inner);
4716 output_constant (VECTOR_CST_ELT (exp, 0), elt_size, align);
4717 thissize = elt_size;
4718 for (unsigned int i = 1; i < VECTOR_CST_NELTS (exp); i++)
4720 output_constant (VECTOR_CST_ELT (exp, i), elt_size, nalign);
4721 thissize += elt_size;
4723 break;
4725 default:
4726 gcc_unreachable ();
4728 break;
4730 case RECORD_TYPE:
4731 case UNION_TYPE:
4732 gcc_assert (TREE_CODE (exp) == CONSTRUCTOR);
4733 return output_constructor (exp, size, align, NULL);
4735 case ERROR_MARK:
4736 return 0;
4738 default:
4739 gcc_unreachable ();
4742 if (size > thissize)
4743 assemble_zeros (size - thissize);
4745 return size;
4749 /* Subroutine of output_constructor, used for computing the size of
4750 arrays of unspecified length. VAL must be a CONSTRUCTOR of an array
4751 type with an unspecified upper bound. */
4753 static unsigned HOST_WIDE_INT
4754 array_size_for_constructor (tree val)
4756 tree max_index;
4757 unsigned HOST_WIDE_INT cnt;
4758 tree index, value, tmp;
4759 offset_int i;
4761 /* This code used to attempt to handle string constants that are not
4762 arrays of single-bytes, but nothing else does, so there's no point in
4763 doing it here. */
4764 if (TREE_CODE (val) == STRING_CST)
4765 return TREE_STRING_LENGTH (val);
4767 max_index = NULL_TREE;
4768 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (val), cnt, index, value)
4770 if (TREE_CODE (index) == RANGE_EXPR)
4771 index = TREE_OPERAND (index, 1);
4772 if (max_index == NULL_TREE || tree_int_cst_lt (max_index, index))
4773 max_index = index;
4776 if (max_index == NULL_TREE)
4777 return 0;
4779 /* Compute the total number of array elements. */
4780 tmp = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (val)));
4781 i = wi::to_offset (max_index) - wi::to_offset (tmp) + 1;
4783 /* Multiply by the array element unit size to find number of bytes. */
4784 i *= wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (val))));
4786 gcc_assert (wi::fits_uhwi_p (i));
4787 return i.to_uhwi ();
4790 /* Other datastructures + helpers for output_constructor. */
4792 /* output_constructor local state to support interaction with helpers. */
4794 typedef struct {
4796 /* Received arguments. */
4797 tree exp; /* Constructor expression. */
4798 tree type; /* Type of constructor expression. */
4799 unsigned HOST_WIDE_INT size; /* # bytes to output - pad if necessary. */
4800 unsigned int align; /* Known initial alignment. */
4801 tree min_index; /* Lower bound if specified for an array. */
4803 /* Output processing state. */
4804 HOST_WIDE_INT total_bytes; /* # bytes output so far / current position. */
4805 int byte; /* Part of a bitfield byte yet to be output. */
4806 int last_relative_index; /* Implicit or explicit index of the last
4807 array element output within a bitfield. */
4808 bool byte_buffer_in_use; /* Whether BYTE is in use. */
4810 /* Current element. */
4811 tree field; /* Current field decl in a record. */
4812 tree val; /* Current element value. */
4813 tree index; /* Current element index. */
4815 } oc_local_state;
4817 /* Helper for output_constructor. From the current LOCAL state, output a
4818 RANGE_EXPR element. */
4820 static void
4821 output_constructor_array_range (oc_local_state *local)
4823 unsigned HOST_WIDE_INT fieldsize
4824 = int_size_in_bytes (TREE_TYPE (local->type));
4826 HOST_WIDE_INT lo_index
4827 = tree_to_shwi (TREE_OPERAND (local->index, 0));
4828 HOST_WIDE_INT hi_index
4829 = tree_to_shwi (TREE_OPERAND (local->index, 1));
4830 HOST_WIDE_INT index;
4832 unsigned int align2
4833 = min_align (local->align, fieldsize * BITS_PER_UNIT);
4835 for (index = lo_index; index <= hi_index; index++)
4837 /* Output the element's initial value. */
4838 if (local->val == NULL_TREE)
4839 assemble_zeros (fieldsize);
4840 else
4841 fieldsize = output_constant (local->val, fieldsize, align2);
4843 /* Count its size. */
4844 local->total_bytes += fieldsize;
4848 /* Helper for output_constructor. From the current LOCAL state, output a
4849 field element that is not true bitfield or part of an outer one. */
4851 static void
4852 output_constructor_regular_field (oc_local_state *local)
4854 /* Field size and position. Since this structure is static, we know the
4855 positions are constant. */
4856 unsigned HOST_WIDE_INT fieldsize;
4857 HOST_WIDE_INT fieldpos;
4859 unsigned int align2;
4861 if (local->index != NULL_TREE)
4863 /* Perform the index calculation in modulo arithmetic but
4864 sign-extend the result because Ada has negative DECL_FIELD_OFFSETs
4865 but we are using an unsigned sizetype. */
4866 unsigned prec = TYPE_PRECISION (sizetype);
4867 offset_int idx = wi::sext (wi::to_offset (local->index)
4868 - wi::to_offset (local->min_index), prec);
4869 fieldpos = (idx * wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (local->val))))
4870 .to_short_addr ();
4872 else if (local->field != NULL_TREE)
4873 fieldpos = int_byte_position (local->field);
4874 else
4875 fieldpos = 0;
4877 /* Output any buffered-up bit-fields preceding this element. */
4878 if (local->byte_buffer_in_use)
4880 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
4881 local->total_bytes++;
4882 local->byte_buffer_in_use = false;
4885 /* Advance to offset of this element.
4886 Note no alignment needed in an array, since that is guaranteed
4887 if each element has the proper size. */
4888 if ((local->field != NULL_TREE || local->index != NULL_TREE)
4889 && fieldpos > local->total_bytes)
4891 assemble_zeros (fieldpos - local->total_bytes);
4892 local->total_bytes = fieldpos;
4895 /* Find the alignment of this element. */
4896 align2 = min_align (local->align, BITS_PER_UNIT * fieldpos);
4898 /* Determine size this element should occupy. */
4899 if (local->field)
4901 fieldsize = 0;
4903 /* If this is an array with an unspecified upper bound,
4904 the initializer determines the size. */
4905 /* ??? This ought to only checked if DECL_SIZE_UNIT is NULL,
4906 but we cannot do this until the deprecated support for
4907 initializing zero-length array members is removed. */
4908 if (TREE_CODE (TREE_TYPE (local->field)) == ARRAY_TYPE
4909 && TYPE_DOMAIN (TREE_TYPE (local->field))
4910 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (local->field))))
4912 fieldsize = array_size_for_constructor (local->val);
4913 /* Given a non-empty initialization, this field had
4914 better be last. */
4915 gcc_assert (!fieldsize || !DECL_CHAIN (local->field));
4917 else
4918 fieldsize = tree_to_uhwi (DECL_SIZE_UNIT (local->field));
4920 else
4921 fieldsize = int_size_in_bytes (TREE_TYPE (local->type));
4923 /* Output the element's initial value. */
4924 if (local->val == NULL_TREE)
4925 assemble_zeros (fieldsize);
4926 else
4927 fieldsize = output_constant (local->val, fieldsize, align2);
4929 /* Count its size. */
4930 local->total_bytes += fieldsize;
4933 /* Helper for output_constructor. From the LOCAL state, output an element
4934 that is a true bitfield or part of an outer one. BIT_OFFSET is the offset
4935 from the start of a possibly ongoing outer byte buffer. */
4937 static void
4938 output_constructor_bitfield (oc_local_state *local, unsigned int bit_offset)
4940 /* Bit size of this element. */
4941 HOST_WIDE_INT ebitsize
4942 = (local->field
4943 ? tree_to_uhwi (DECL_SIZE (local->field))
4944 : tree_to_uhwi (TYPE_SIZE (TREE_TYPE (local->type))));
4946 /* Relative index of this element if this is an array component. */
4947 HOST_WIDE_INT relative_index
4948 = (!local->field
4949 ? (local->index
4950 ? (tree_to_shwi (local->index)
4951 - tree_to_shwi (local->min_index))
4952 : local->last_relative_index + 1)
4953 : 0);
4955 /* Bit position of this element from the start of the containing
4956 constructor. */
4957 HOST_WIDE_INT constructor_relative_ebitpos
4958 = (local->field
4959 ? int_bit_position (local->field)
4960 : ebitsize * relative_index);
4962 /* Bit position of this element from the start of a possibly ongoing
4963 outer byte buffer. */
4964 HOST_WIDE_INT byte_relative_ebitpos
4965 = bit_offset + constructor_relative_ebitpos;
4967 /* From the start of a possibly ongoing outer byte buffer, offsets to
4968 the first bit of this element and to the first bit past the end of
4969 this element. */
4970 HOST_WIDE_INT next_offset = byte_relative_ebitpos;
4971 HOST_WIDE_INT end_offset = byte_relative_ebitpos + ebitsize;
4973 local->last_relative_index = relative_index;
4975 if (local->val == NULL_TREE)
4976 local->val = integer_zero_node;
4978 while (TREE_CODE (local->val) == VIEW_CONVERT_EXPR
4979 || TREE_CODE (local->val) == NON_LVALUE_EXPR)
4980 local->val = TREE_OPERAND (local->val, 0);
4982 if (TREE_CODE (local->val) != INTEGER_CST
4983 && TREE_CODE (local->val) != CONSTRUCTOR)
4985 error ("invalid initial value for member %qE", DECL_NAME (local->field));
4986 return;
4989 /* If this field does not start in this (or next) byte, skip some bytes. */
4990 if (next_offset / BITS_PER_UNIT != local->total_bytes)
4992 /* Output remnant of any bit field in previous bytes. */
4993 if (local->byte_buffer_in_use)
4995 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
4996 local->total_bytes++;
4997 local->byte_buffer_in_use = false;
5000 /* If still not at proper byte, advance to there. */
5001 if (next_offset / BITS_PER_UNIT != local->total_bytes)
5003 gcc_assert (next_offset / BITS_PER_UNIT >= local->total_bytes);
5004 assemble_zeros (next_offset / BITS_PER_UNIT - local->total_bytes);
5005 local->total_bytes = next_offset / BITS_PER_UNIT;
5009 /* Set up the buffer if necessary. */
5010 if (!local->byte_buffer_in_use)
5012 local->byte = 0;
5013 if (ebitsize > 0)
5014 local->byte_buffer_in_use = true;
5017 /* If this is nested constructor, recurse passing the bit offset and the
5018 pending data, then retrieve the new pending data afterwards. */
5019 if (TREE_CODE (local->val) == CONSTRUCTOR)
5021 oc_outer_state temp_state;
5022 temp_state.bit_offset = next_offset % BITS_PER_UNIT;
5023 temp_state.byte = local->byte;
5024 local->total_bytes
5025 += output_constructor (local->val, 0, 0, &temp_state);
5026 local->byte = temp_state.byte;
5027 return;
5030 /* Otherwise, we must split the element into pieces that fall within
5031 separate bytes, and combine each byte with previous or following
5032 bit-fields. */
5033 while (next_offset < end_offset)
5035 int this_time;
5036 int shift;
5037 HOST_WIDE_INT value;
5038 HOST_WIDE_INT next_byte = next_offset / BITS_PER_UNIT;
5039 HOST_WIDE_INT next_bit = next_offset % BITS_PER_UNIT;
5041 /* Advance from byte to byte within this element when necessary. */
5042 while (next_byte != local->total_bytes)
5044 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5045 local->total_bytes++;
5046 local->byte = 0;
5049 /* Number of bits we can process at once (all part of the same byte). */
5050 this_time = MIN (end_offset - next_offset, BITS_PER_UNIT - next_bit);
5051 if (BYTES_BIG_ENDIAN)
5053 /* On big-endian machine, take the most significant bits (of the
5054 bits that are significant) first and put them into bytes from
5055 the most significant end. */
5056 shift = end_offset - next_offset - this_time;
5058 /* Don't try to take a bunch of bits that cross
5059 the word boundary in the INTEGER_CST. We can
5060 only select bits from one element. */
5061 if ((shift / HOST_BITS_PER_WIDE_INT)
5062 != ((shift + this_time - 1) / HOST_BITS_PER_WIDE_INT))
5064 const int end = shift + this_time - 1;
5065 shift = end & -HOST_BITS_PER_WIDE_INT;
5066 this_time = end - shift + 1;
5069 /* Now get the bits from the appropriate constant word. */
5070 value = TREE_INT_CST_ELT (local->val, shift / HOST_BITS_PER_WIDE_INT);
5071 shift = shift & (HOST_BITS_PER_WIDE_INT - 1);
5073 /* Get the result. This works only when:
5074 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
5075 local->byte |= (((value >> shift)
5076 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
5077 << (BITS_PER_UNIT - this_time - next_bit));
5079 else
5081 /* On little-endian machines, take the least significant bits of
5082 the value first and pack them starting at the least significant
5083 bits of the bytes. */
5084 shift = next_offset - byte_relative_ebitpos;
5086 /* Don't try to take a bunch of bits that cross
5087 the word boundary in the INTEGER_CST. We can
5088 only select bits from one element. */
5089 if ((shift / HOST_BITS_PER_WIDE_INT)
5090 != ((shift + this_time - 1) / HOST_BITS_PER_WIDE_INT))
5091 this_time
5092 = HOST_BITS_PER_WIDE_INT - (shift & (HOST_BITS_PER_WIDE_INT - 1));
5094 /* Now get the bits from the appropriate constant word. */
5095 value = TREE_INT_CST_ELT (local->val, shift / HOST_BITS_PER_WIDE_INT);
5096 shift = shift & (HOST_BITS_PER_WIDE_INT - 1);
5098 /* Get the result. This works only when:
5099 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
5100 local->byte |= (((value >> shift)
5101 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
5102 << next_bit);
5105 next_offset += this_time;
5106 local->byte_buffer_in_use = true;
5110 /* Subroutine of output_constant, used for CONSTRUCTORs (aggregate constants).
5111 Generate at least SIZE bytes, padding if necessary. OUTER designates the
5112 caller output state of relevance in recursive invocations. */
5114 static unsigned HOST_WIDE_INT
5115 output_constructor (tree exp, unsigned HOST_WIDE_INT size,
5116 unsigned int align, oc_outer_state *outer)
5118 unsigned HOST_WIDE_INT cnt;
5119 constructor_elt *ce;
5121 oc_local_state local;
5123 /* Setup our local state to communicate with helpers. */
5124 local.exp = exp;
5125 local.type = TREE_TYPE (exp);
5126 local.size = size;
5127 local.align = align;
5128 if (TREE_CODE (local.type) == ARRAY_TYPE && TYPE_DOMAIN (local.type))
5129 local.min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (local.type));
5130 else
5131 local.min_index = NULL_TREE;
5133 local.total_bytes = 0;
5134 local.byte_buffer_in_use = outer != NULL;
5135 local.byte = outer ? outer->byte : 0;
5136 local.last_relative_index = -1;
5138 gcc_assert (HOST_BITS_PER_WIDE_INT >= BITS_PER_UNIT);
5140 /* As CE goes through the elements of the constant, FIELD goes through the
5141 structure fields if the constant is a structure. If the constant is a
5142 union, we override this by getting the field from the TREE_LIST element.
5143 But the constant could also be an array. Then FIELD is zero.
5145 There is always a maximum of one element in the chain LINK for unions
5146 (even if the initializer in a source program incorrectly contains
5147 more one). */
5149 if (TREE_CODE (local.type) == RECORD_TYPE)
5150 local.field = TYPE_FIELDS (local.type);
5151 else
5152 local.field = NULL_TREE;
5154 for (cnt = 0;
5155 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), cnt, &ce);
5156 cnt++, local.field = local.field ? DECL_CHAIN (local.field) : 0)
5158 local.val = ce->value;
5159 local.index = NULL_TREE;
5161 /* The element in a union constructor specifies the proper field
5162 or index. */
5163 if (RECORD_OR_UNION_TYPE_P (local.type) && ce->index != NULL_TREE)
5164 local.field = ce->index;
5166 else if (TREE_CODE (local.type) == ARRAY_TYPE)
5167 local.index = ce->index;
5169 if (local.field && flag_verbose_asm)
5170 fprintf (asm_out_file, "%s %s:\n",
5171 ASM_COMMENT_START,
5172 DECL_NAME (local.field)
5173 ? IDENTIFIER_POINTER (DECL_NAME (local.field))
5174 : "<anonymous>");
5176 /* Eliminate the marker that makes a cast not be an lvalue. */
5177 if (local.val != NULL_TREE)
5178 STRIP_NOPS (local.val);
5180 /* Output the current element, using the appropriate helper ... */
5182 /* For an array slice not part of an outer bitfield. */
5183 if (!outer
5184 && local.index != NULL_TREE
5185 && TREE_CODE (local.index) == RANGE_EXPR)
5186 output_constructor_array_range (&local);
5188 /* For a field that is neither a true bitfield nor part of an outer one,
5189 known to be at least byte aligned and multiple-of-bytes long. */
5190 else if (!outer
5191 && (local.field == NULL_TREE
5192 || !CONSTRUCTOR_BITFIELD_P (local.field)))
5193 output_constructor_regular_field (&local);
5195 /* For a true bitfield or part of an outer one. Only INTEGER_CSTs are
5196 supported for scalar fields, so we may need to convert first. */
5197 else
5199 if (TREE_CODE (local.val) == REAL_CST)
5200 local.val
5201 = fold_unary (VIEW_CONVERT_EXPR,
5202 build_nonstandard_integer_type
5203 (TYPE_PRECISION (TREE_TYPE (local.val)), 0),
5204 local.val);
5205 output_constructor_bitfield (&local, outer ? outer->bit_offset : 0);
5209 /* If we are not at toplevel, save the pending data for our caller.
5210 Otherwise output the pending data and padding zeros as needed. */
5211 if (outer)
5212 outer->byte = local.byte;
5213 else
5215 if (local.byte_buffer_in_use)
5217 assemble_integer (GEN_INT (local.byte), 1, BITS_PER_UNIT, 1);
5218 local.total_bytes++;
5221 if ((unsigned HOST_WIDE_INT)local.total_bytes < local.size)
5223 assemble_zeros (local.size - local.total_bytes);
5224 local.total_bytes = local.size;
5228 return local.total_bytes;
5231 /* Mark DECL as weak. */
5233 static void
5234 mark_weak (tree decl)
5236 if (DECL_WEAK (decl))
5237 return;
5239 struct symtab_node *n = symtab_node::get (decl);
5240 if (n && n->refuse_visibility_changes)
5241 error ("%+D declared weak after being used", decl);
5242 DECL_WEAK (decl) = 1;
5244 if (DECL_RTL_SET_P (decl)
5245 && MEM_P (DECL_RTL (decl))
5246 && XEXP (DECL_RTL (decl), 0)
5247 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == SYMBOL_REF)
5248 SYMBOL_REF_WEAK (XEXP (DECL_RTL (decl), 0)) = 1;
5251 /* Merge weak status between NEWDECL and OLDDECL. */
5253 void
5254 merge_weak (tree newdecl, tree olddecl)
5256 if (DECL_WEAK (newdecl) == DECL_WEAK (olddecl))
5258 if (DECL_WEAK (newdecl) && TARGET_SUPPORTS_WEAK)
5260 tree *pwd;
5261 /* We put the NEWDECL on the weak_decls list at some point
5262 and OLDDECL as well. Keep just OLDDECL on the list. */
5263 for (pwd = &weak_decls; *pwd; pwd = &TREE_CHAIN (*pwd))
5264 if (TREE_VALUE (*pwd) == newdecl)
5266 *pwd = TREE_CHAIN (*pwd);
5267 break;
5270 return;
5273 if (DECL_WEAK (newdecl))
5275 tree wd;
5277 /* NEWDECL is weak, but OLDDECL is not. */
5279 /* If we already output the OLDDECL, we're in trouble; we can't
5280 go back and make it weak. This should never happen in
5281 unit-at-a-time compilation. */
5282 gcc_assert (!TREE_ASM_WRITTEN (olddecl));
5284 /* If we've already generated rtl referencing OLDDECL, we may
5285 have done so in a way that will not function properly with
5286 a weak symbol. Again in unit-at-a-time this should be
5287 impossible. */
5288 gcc_assert (!TREE_USED (olddecl)
5289 || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (olddecl)));
5291 if (TARGET_SUPPORTS_WEAK)
5293 /* We put the NEWDECL on the weak_decls list at some point.
5294 Replace it with the OLDDECL. */
5295 for (wd = weak_decls; wd; wd = TREE_CHAIN (wd))
5296 if (TREE_VALUE (wd) == newdecl)
5298 TREE_VALUE (wd) = olddecl;
5299 break;
5301 /* We may not find the entry on the list. If NEWDECL is a
5302 weak alias, then we will have already called
5303 globalize_decl to remove the entry; in that case, we do
5304 not need to do anything. */
5307 /* Make the OLDDECL weak; it's OLDDECL that we'll be keeping. */
5308 mark_weak (olddecl);
5310 else
5311 /* OLDDECL was weak, but NEWDECL was not explicitly marked as
5312 weak. Just update NEWDECL to indicate that it's weak too. */
5313 mark_weak (newdecl);
5316 /* Declare DECL to be a weak symbol. */
5318 void
5319 declare_weak (tree decl)
5321 gcc_assert (TREE_CODE (decl) != FUNCTION_DECL || !TREE_ASM_WRITTEN (decl));
5322 if (! TREE_PUBLIC (decl))
5323 error ("weak declaration of %q+D must be public", decl);
5324 else if (!TARGET_SUPPORTS_WEAK)
5325 warning (0, "weak declaration of %q+D not supported", decl);
5327 mark_weak (decl);
5328 if (!lookup_attribute ("weak", DECL_ATTRIBUTES (decl)))
5329 DECL_ATTRIBUTES (decl)
5330 = tree_cons (get_identifier ("weak"), NULL, DECL_ATTRIBUTES (decl));
5333 static void
5334 weak_finish_1 (tree decl)
5336 #if defined (ASM_WEAKEN_DECL) || defined (ASM_WEAKEN_LABEL)
5337 const char *const name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5338 #endif
5340 if (! TREE_USED (decl))
5341 return;
5343 #ifdef ASM_WEAKEN_DECL
5344 ASM_WEAKEN_DECL (asm_out_file, decl, name, NULL);
5345 #else
5346 #ifdef ASM_WEAKEN_LABEL
5347 ASM_WEAKEN_LABEL (asm_out_file, name);
5348 #else
5349 #ifdef ASM_OUTPUT_WEAK_ALIAS
5351 static bool warn_once = 0;
5352 if (! warn_once)
5354 warning (0, "only weak aliases are supported in this configuration");
5355 warn_once = 1;
5357 return;
5359 #endif
5360 #endif
5361 #endif
5364 /* Fiven an assembly name, find the decl it is associated with. */
5365 static tree
5366 find_decl (tree target)
5368 symtab_node *node = symtab_node::get_for_asmname (target);
5369 if (node)
5370 return node->decl;
5371 return NULL_TREE;
5374 /* This TREE_LIST contains weakref targets. */
5376 static GTY(()) tree weakref_targets;
5378 /* Emit any pending weak declarations. */
5380 void
5381 weak_finish (void)
5383 tree t;
5385 for (t = weakref_targets; t; t = TREE_CHAIN (t))
5387 tree alias_decl = TREE_PURPOSE (t);
5388 tree target = ultimate_transparent_alias_target (&TREE_VALUE (t));
5390 if (! TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (alias_decl)))
5391 /* Remove alias_decl from the weak list, but leave entries for
5392 the target alone. */
5393 target = NULL_TREE;
5394 #ifndef ASM_OUTPUT_WEAKREF
5395 else if (! TREE_SYMBOL_REFERENCED (target))
5397 /* Use ASM_WEAKEN_LABEL only if ASM_WEAKEN_DECL is not
5398 defined, otherwise we and weak_finish_1 would use
5399 different macros. */
5400 # if defined ASM_WEAKEN_LABEL && ! defined ASM_WEAKEN_DECL
5401 ASM_WEAKEN_LABEL (asm_out_file, IDENTIFIER_POINTER (target));
5402 # else
5403 tree decl = find_decl (target);
5405 if (! decl)
5407 decl = build_decl (DECL_SOURCE_LOCATION (alias_decl),
5408 TREE_CODE (alias_decl), target,
5409 TREE_TYPE (alias_decl));
5411 DECL_EXTERNAL (decl) = 1;
5412 TREE_PUBLIC (decl) = 1;
5413 DECL_ARTIFICIAL (decl) = 1;
5414 TREE_NOTHROW (decl) = TREE_NOTHROW (alias_decl);
5415 TREE_USED (decl) = 1;
5418 weak_finish_1 (decl);
5419 # endif
5421 #endif
5424 tree *p;
5425 tree t2;
5427 /* Remove the alias and the target from the pending weak list
5428 so that we do not emit any .weak directives for the former,
5429 nor multiple .weak directives for the latter. */
5430 for (p = &weak_decls; (t2 = *p) ; )
5432 if (TREE_VALUE (t2) == alias_decl
5433 || target == DECL_ASSEMBLER_NAME (TREE_VALUE (t2)))
5434 *p = TREE_CHAIN (t2);
5435 else
5436 p = &TREE_CHAIN (t2);
5439 /* Remove other weakrefs to the same target, to speed things up. */
5440 for (p = &TREE_CHAIN (t); (t2 = *p) ; )
5442 if (target == ultimate_transparent_alias_target (&TREE_VALUE (t2)))
5443 *p = TREE_CHAIN (t2);
5444 else
5445 p = &TREE_CHAIN (t2);
5450 for (t = weak_decls; t; t = TREE_CHAIN (t))
5452 tree decl = TREE_VALUE (t);
5454 weak_finish_1 (decl);
5458 /* Emit the assembly bits to indicate that DECL is globally visible. */
5460 static void
5461 globalize_decl (tree decl)
5464 #if defined (ASM_WEAKEN_LABEL) || defined (ASM_WEAKEN_DECL)
5465 if (DECL_WEAK (decl))
5467 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
5468 tree *p, t;
5470 #ifdef ASM_WEAKEN_DECL
5471 ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
5472 #else
5473 ASM_WEAKEN_LABEL (asm_out_file, name);
5474 #endif
5476 /* Remove this function from the pending weak list so that
5477 we do not emit multiple .weak directives for it. */
5478 for (p = &weak_decls; (t = *p) ; )
5480 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
5481 *p = TREE_CHAIN (t);
5482 else
5483 p = &TREE_CHAIN (t);
5486 /* Remove weakrefs to the same target from the pending weakref
5487 list, for the same reason. */
5488 for (p = &weakref_targets; (t = *p) ; )
5490 if (DECL_ASSEMBLER_NAME (decl)
5491 == ultimate_transparent_alias_target (&TREE_VALUE (t)))
5492 *p = TREE_CHAIN (t);
5493 else
5494 p = &TREE_CHAIN (t);
5497 return;
5499 #endif
5501 targetm.asm_out.globalize_decl_name (asm_out_file, decl);
5504 vec<alias_pair, va_gc> *alias_pairs;
5506 /* Output the assembler code for a define (equate) using ASM_OUTPUT_DEF
5507 or ASM_OUTPUT_DEF_FROM_DECLS. The function defines the symbol whose
5508 tree node is DECL to have the value of the tree node TARGET. */
5510 void
5511 do_assemble_alias (tree decl, tree target)
5513 /* Emulated TLS had better not get this var. */
5514 gcc_assert (!(!targetm.have_tls
5515 && TREE_CODE (decl) == VAR_DECL
5516 && DECL_THREAD_LOCAL_P (decl)));
5518 if (TREE_ASM_WRITTEN (decl))
5519 return;
5521 /* We must force creation of DECL_RTL for debug info generation, even though
5522 we don't use it here. */
5523 make_decl_rtl (decl);
5525 TREE_ASM_WRITTEN (decl) = 1;
5526 TREE_ASM_WRITTEN (DECL_ASSEMBLER_NAME (decl)) = 1;
5528 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
5530 ultimate_transparent_alias_target (&target);
5532 if (!TREE_SYMBOL_REFERENCED (target))
5533 weakref_targets = tree_cons (decl, target, weakref_targets);
5535 #ifdef ASM_OUTPUT_WEAKREF
5536 ASM_OUTPUT_WEAKREF (asm_out_file, decl,
5537 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
5538 IDENTIFIER_POINTER (target));
5539 #else
5540 if (!TARGET_SUPPORTS_WEAK)
5542 error_at (DECL_SOURCE_LOCATION (decl),
5543 "weakref is not supported in this configuration");
5544 return;
5546 #endif
5547 return;
5550 #ifdef ASM_OUTPUT_DEF
5551 /* Make name accessible from other files, if appropriate. */
5553 if (TREE_PUBLIC (decl))
5555 globalize_decl (decl);
5556 maybe_assemble_visibility (decl);
5558 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
5560 #if defined (ASM_OUTPUT_TYPE_DIRECTIVE)
5561 if (targetm.has_ifunc_p ())
5562 ASM_OUTPUT_TYPE_DIRECTIVE
5563 (asm_out_file, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
5564 IFUNC_ASM_TYPE);
5565 else
5566 #endif
5567 error_at (DECL_SOURCE_LOCATION (decl),
5568 "ifunc is not supported on this target");
5571 # ifdef ASM_OUTPUT_DEF_FROM_DECLS
5572 ASM_OUTPUT_DEF_FROM_DECLS (asm_out_file, decl, target);
5573 # else
5574 ASM_OUTPUT_DEF (asm_out_file,
5575 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
5576 IDENTIFIER_POINTER (target));
5577 # endif
5578 #elif defined (ASM_OUTPUT_WEAK_ALIAS) || defined (ASM_WEAKEN_DECL)
5580 const char *name;
5581 tree *p, t;
5583 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5584 # ifdef ASM_WEAKEN_DECL
5585 ASM_WEAKEN_DECL (asm_out_file, decl, name, IDENTIFIER_POINTER (target));
5586 # else
5587 ASM_OUTPUT_WEAK_ALIAS (asm_out_file, name, IDENTIFIER_POINTER (target));
5588 # endif
5589 /* Remove this function from the pending weak list so that
5590 we do not emit multiple .weak directives for it. */
5591 for (p = &weak_decls; (t = *p) ; )
5592 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
5593 *p = TREE_CHAIN (t);
5594 else
5595 p = &TREE_CHAIN (t);
5597 /* Remove weakrefs to the same target from the pending weakref
5598 list, for the same reason. */
5599 for (p = &weakref_targets; (t = *p) ; )
5601 if (DECL_ASSEMBLER_NAME (decl)
5602 == ultimate_transparent_alias_target (&TREE_VALUE (t)))
5603 *p = TREE_CHAIN (t);
5604 else
5605 p = &TREE_CHAIN (t);
5608 #endif
5611 /* Emit an assembler directive to make the symbol for DECL an alias to
5612 the symbol for TARGET. */
5614 void
5615 assemble_alias (tree decl, tree target)
5617 tree target_decl;
5619 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
5621 tree alias = DECL_ASSEMBLER_NAME (decl);
5623 ultimate_transparent_alias_target (&target);
5625 if (alias == target)
5626 error ("weakref %q+D ultimately targets itself", decl);
5627 if (TREE_PUBLIC (decl))
5628 error ("weakref %q+D must have static linkage", decl);
5630 else
5632 #if !defined (ASM_OUTPUT_DEF)
5633 # if !defined(ASM_OUTPUT_WEAK_ALIAS) && !defined (ASM_WEAKEN_DECL)
5634 error_at (DECL_SOURCE_LOCATION (decl),
5635 "alias definitions not supported in this configuration");
5636 TREE_ASM_WRITTEN (decl) = 1;
5637 return;
5638 # else
5639 if (!DECL_WEAK (decl))
5641 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
5642 error_at (DECL_SOURCE_LOCATION (decl),
5643 "ifunc is not supported in this configuration");
5644 else
5645 error_at (DECL_SOURCE_LOCATION (decl),
5646 "only weak aliases are supported in this configuration");
5647 TREE_ASM_WRITTEN (decl) = 1;
5648 return;
5650 # endif
5651 #endif
5653 TREE_USED (decl) = 1;
5655 /* Allow aliases to aliases. */
5656 if (TREE_CODE (decl) == FUNCTION_DECL)
5657 cgraph_node::get_create (decl)->alias = true;
5658 else
5659 varpool_node::get_create (decl)->alias = true;
5661 /* If the target has already been emitted, we don't have to queue the
5662 alias. This saves a tad of memory. */
5663 if (symtab->global_info_ready)
5664 target_decl = find_decl (target);
5665 else
5666 target_decl= NULL;
5667 if ((target_decl && TREE_ASM_WRITTEN (target_decl))
5668 || symtab->state >= EXPANSION)
5669 do_assemble_alias (decl, target);
5670 else
5672 alias_pair p = {decl, target};
5673 vec_safe_push (alias_pairs, p);
5677 /* Record and output a table of translations from original function
5678 to its transaction aware clone. Note that tm_pure functions are
5679 considered to be their own clone. */
5681 static GTY((if_marked ("tree_map_marked_p"), param_is (struct tree_map)))
5682 htab_t tm_clone_hash;
5684 void
5685 record_tm_clone_pair (tree o, tree n)
5687 struct tree_map **slot, *h;
5689 if (tm_clone_hash == NULL)
5690 tm_clone_hash = htab_create_ggc (32, tree_map_hash, tree_map_eq, 0);
5692 h = ggc_alloc<tree_map> ();
5693 h->hash = htab_hash_pointer (o);
5694 h->base.from = o;
5695 h->to = n;
5697 slot = (struct tree_map **)
5698 htab_find_slot_with_hash (tm_clone_hash, h, h->hash, INSERT);
5699 *slot = h;
5702 tree
5703 get_tm_clone_pair (tree o)
5705 if (tm_clone_hash)
5707 struct tree_map *h, in;
5709 in.base.from = o;
5710 in.hash = htab_hash_pointer (o);
5711 h = (struct tree_map *) htab_find_with_hash (tm_clone_hash,
5712 &in, in.hash);
5713 if (h)
5714 return h->to;
5716 return NULL_TREE;
5719 typedef struct tm_alias_pair
5721 unsigned int uid;
5722 tree from;
5723 tree to;
5724 } tm_alias_pair;
5727 /* Helper function for finish_tm_clone_pairs. Dump a hash table entry
5728 into a VEC in INFO. */
5730 static int
5731 dump_tm_clone_to_vec (void **slot, void *info)
5733 struct tree_map *map = (struct tree_map *) *slot;
5734 vec<tm_alias_pair> *tm_alias_pairs = (vec<tm_alias_pair> *) info;
5735 tm_alias_pair p = {DECL_UID (map->base.from), map->base.from, map->to};
5736 tm_alias_pairs->safe_push (p);
5737 return 1;
5740 /* Dump the actual pairs to the .tm_clone_table section. */
5742 static void
5743 dump_tm_clone_pairs (vec<tm_alias_pair> tm_alias_pairs)
5745 unsigned i;
5746 tm_alias_pair *p;
5747 bool switched = false;
5749 FOR_EACH_VEC_ELT (tm_alias_pairs, i, p)
5751 tree src = p->from;
5752 tree dst = p->to;
5753 struct cgraph_node *src_n = cgraph_node::get (src);
5754 struct cgraph_node *dst_n = cgraph_node::get (dst);
5756 /* The function ipa_tm_create_version() marks the clone as needed if
5757 the original function was needed. But we also mark the clone as
5758 needed if we ever called the clone indirectly through
5759 TM_GETTMCLONE. If neither of these are true, we didn't generate
5760 a clone, and we didn't call it indirectly... no sense keeping it
5761 in the clone table. */
5762 if (!dst_n || !dst_n->definition)
5763 continue;
5765 /* This covers the case where we have optimized the original
5766 function away, and only access the transactional clone. */
5767 if (!src_n || !src_n->definition)
5768 continue;
5770 if (!switched)
5772 switch_to_section (targetm.asm_out.tm_clone_table_section ());
5773 assemble_align (POINTER_SIZE);
5774 switched = true;
5777 assemble_integer (XEXP (DECL_RTL (src), 0),
5778 POINTER_SIZE_UNITS, POINTER_SIZE, 1);
5779 assemble_integer (XEXP (DECL_RTL (dst), 0),
5780 POINTER_SIZE_UNITS, POINTER_SIZE, 1);
5784 /* Provide a default for the tm_clone_table section. */
5786 section *
5787 default_clone_table_section (void)
5789 return get_named_section (NULL, ".tm_clone_table", 3);
5792 /* Helper comparison function for qsorting by the DECL_UID stored in
5793 alias_pair->emitted_diags. */
5795 static int
5796 tm_alias_pair_cmp (const void *x, const void *y)
5798 const tm_alias_pair *p1 = (const tm_alias_pair *) x;
5799 const tm_alias_pair *p2 = (const tm_alias_pair *) y;
5800 if (p1->uid < p2->uid)
5801 return -1;
5802 if (p1->uid > p2->uid)
5803 return 1;
5804 return 0;
5807 void
5808 finish_tm_clone_pairs (void)
5810 vec<tm_alias_pair> tm_alias_pairs = vNULL;
5812 if (tm_clone_hash == NULL)
5813 return;
5815 /* We need a determenistic order for the .tm_clone_table, otherwise
5816 we will get bootstrap comparison failures, so dump the hash table
5817 to a vector, sort it, and dump the vector. */
5819 /* Dump the hashtable to a vector. */
5820 htab_traverse_noresize (tm_clone_hash, dump_tm_clone_to_vec,
5821 (void *) &tm_alias_pairs);
5822 /* Sort it. */
5823 tm_alias_pairs.qsort (tm_alias_pair_cmp);
5825 /* Dump it. */
5826 dump_tm_clone_pairs (tm_alias_pairs);
5828 htab_delete (tm_clone_hash);
5829 tm_clone_hash = NULL;
5830 tm_alias_pairs.release ();
5834 /* Emit an assembler directive to set symbol for DECL visibility to
5835 the visibility type VIS, which must not be VISIBILITY_DEFAULT. */
5837 void
5838 default_assemble_visibility (tree decl ATTRIBUTE_UNUSED,
5839 int vis ATTRIBUTE_UNUSED)
5841 #ifdef HAVE_GAS_HIDDEN
5842 static const char * const visibility_types[] = {
5843 NULL, "protected", "hidden", "internal"
5846 const char *name, *type;
5848 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5849 type = visibility_types[vis];
5851 fprintf (asm_out_file, "\t.%s\t", type);
5852 assemble_name (asm_out_file, name);
5853 fprintf (asm_out_file, "\n");
5854 #else
5855 if (!DECL_ARTIFICIAL (decl))
5856 warning (OPT_Wattributes, "visibility attribute not supported "
5857 "in this configuration; ignored");
5858 #endif
5861 /* A helper function to call assemble_visibility when needed for a decl. */
5864 maybe_assemble_visibility (tree decl)
5866 enum symbol_visibility vis = DECL_VISIBILITY (decl);
5868 if (vis != VISIBILITY_DEFAULT)
5870 targetm.asm_out.assemble_visibility (decl, vis);
5871 return 1;
5873 else
5874 return 0;
5877 /* Returns 1 if the target configuration supports defining public symbols
5878 so that one of them will be chosen at link time instead of generating a
5879 multiply-defined symbol error, whether through the use of weak symbols or
5880 a target-specific mechanism for having duplicates discarded. */
5883 supports_one_only (void)
5885 if (SUPPORTS_ONE_ONLY)
5886 return 1;
5887 return TARGET_SUPPORTS_WEAK;
5890 /* Set up DECL as a public symbol that can be defined in multiple
5891 translation units without generating a linker error. */
5893 void
5894 make_decl_one_only (tree decl, tree comdat_group)
5896 struct symtab_node *symbol;
5897 gcc_assert (TREE_CODE (decl) == VAR_DECL
5898 || TREE_CODE (decl) == FUNCTION_DECL);
5900 TREE_PUBLIC (decl) = 1;
5902 if (TREE_CODE (decl) == VAR_DECL)
5903 symbol = varpool_node::get_create (decl);
5904 else
5905 symbol = cgraph_node::get_create (decl);
5907 if (SUPPORTS_ONE_ONLY)
5909 #ifdef MAKE_DECL_ONE_ONLY
5910 MAKE_DECL_ONE_ONLY (decl);
5911 #endif
5912 symbol->set_comdat_group (comdat_group);
5914 else if (TREE_CODE (decl) == VAR_DECL
5915 && (DECL_INITIAL (decl) == 0
5916 || (!in_lto_p && DECL_INITIAL (decl) == error_mark_node)))
5917 DECL_COMMON (decl) = 1;
5918 else
5920 gcc_assert (TARGET_SUPPORTS_WEAK);
5921 DECL_WEAK (decl) = 1;
5925 void
5926 init_varasm_once (void)
5928 section_htab = hash_table<section_hasher>::create_ggc (31);
5929 object_block_htab = hash_table<object_block_hasher>::create_ggc (31);
5930 const_desc_htab = hash_table<tree_descriptor_hasher>::create_ggc (1009);
5932 shared_constant_pool = create_constant_pool ();
5934 #ifdef TEXT_SECTION_ASM_OP
5935 text_section = get_unnamed_section (SECTION_CODE, output_section_asm_op,
5936 TEXT_SECTION_ASM_OP);
5937 #endif
5939 #ifdef DATA_SECTION_ASM_OP
5940 data_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
5941 DATA_SECTION_ASM_OP);
5942 #endif
5944 #ifdef SDATA_SECTION_ASM_OP
5945 sdata_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
5946 SDATA_SECTION_ASM_OP);
5947 #endif
5949 #ifdef READONLY_DATA_SECTION_ASM_OP
5950 readonly_data_section = get_unnamed_section (0, output_section_asm_op,
5951 READONLY_DATA_SECTION_ASM_OP);
5952 #endif
5954 #ifdef CTORS_SECTION_ASM_OP
5955 ctors_section = get_unnamed_section (0, output_section_asm_op,
5956 CTORS_SECTION_ASM_OP);
5957 #endif
5959 #ifdef DTORS_SECTION_ASM_OP
5960 dtors_section = get_unnamed_section (0, output_section_asm_op,
5961 DTORS_SECTION_ASM_OP);
5962 #endif
5964 #ifdef BSS_SECTION_ASM_OP
5965 bss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
5966 output_section_asm_op,
5967 BSS_SECTION_ASM_OP);
5968 #endif
5970 #ifdef SBSS_SECTION_ASM_OP
5971 sbss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
5972 output_section_asm_op,
5973 SBSS_SECTION_ASM_OP);
5974 #endif
5976 tls_comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
5977 | SECTION_COMMON, emit_tls_common);
5978 lcomm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
5979 | SECTION_COMMON, emit_local);
5980 comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
5981 | SECTION_COMMON, emit_common);
5983 #if defined ASM_OUTPUT_ALIGNED_BSS
5984 bss_noswitch_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS,
5985 emit_bss);
5986 #endif
5988 targetm.asm_out.init_sections ();
5990 if (readonly_data_section == NULL)
5991 readonly_data_section = text_section;
5993 #ifdef ASM_OUTPUT_EXTERNAL
5994 pending_assemble_externals_set = new hash_set<tree>;
5995 #endif
5998 enum tls_model
5999 decl_default_tls_model (const_tree decl)
6001 enum tls_model kind;
6002 bool is_local;
6004 is_local = targetm.binds_local_p (decl);
6005 if (!flag_shlib)
6007 if (is_local)
6008 kind = TLS_MODEL_LOCAL_EXEC;
6009 else
6010 kind = TLS_MODEL_INITIAL_EXEC;
6013 /* Local dynamic is inefficient when we're not combining the
6014 parts of the address. */
6015 else if (optimize && is_local)
6016 kind = TLS_MODEL_LOCAL_DYNAMIC;
6017 else
6018 kind = TLS_MODEL_GLOBAL_DYNAMIC;
6019 if (kind < flag_tls_default)
6020 kind = flag_tls_default;
6022 return kind;
6025 /* Select a set of attributes for section NAME based on the properties
6026 of DECL and whether or not RELOC indicates that DECL's initializer
6027 might contain runtime relocations.
6029 We make the section read-only and executable for a function decl,
6030 read-only for a const data decl, and writable for a non-const data decl. */
6032 unsigned int
6033 default_section_type_flags (tree decl, const char *name, int reloc)
6035 unsigned int flags;
6037 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
6038 flags = SECTION_CODE;
6039 else if (decl)
6041 enum section_category category
6042 = categorize_decl_for_section (decl, reloc);
6043 if (decl_readonly_section_1 (category))
6044 flags = 0;
6045 else if (category == SECCAT_DATA_REL_RO
6046 || category == SECCAT_DATA_REL_RO_LOCAL)
6047 flags = SECTION_WRITE | SECTION_RELRO;
6048 else
6049 flags = SECTION_WRITE;
6051 else
6053 flags = SECTION_WRITE;
6054 if (strcmp (name, ".data.rel.ro") == 0
6055 || strcmp (name, ".data.rel.ro.local") == 0)
6056 flags |= SECTION_RELRO;
6059 if (decl && DECL_P (decl) && DECL_COMDAT_GROUP (decl))
6060 flags |= SECTION_LINKONCE;
6062 if (strcmp (name, ".vtable_map_vars") == 0)
6063 flags |= SECTION_LINKONCE;
6065 if (decl && TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
6066 flags |= SECTION_TLS | SECTION_WRITE;
6068 if (strcmp (name, ".bss") == 0
6069 || strncmp (name, ".bss.", 5) == 0
6070 || strncmp (name, ".gnu.linkonce.b.", 16) == 0
6071 || strcmp (name, ".persistent.bss") == 0
6072 || strcmp (name, ".sbss") == 0
6073 || strncmp (name, ".sbss.", 6) == 0
6074 || strncmp (name, ".gnu.linkonce.sb.", 17) == 0)
6075 flags |= SECTION_BSS;
6077 if (strcmp (name, ".tdata") == 0
6078 || strncmp (name, ".tdata.", 7) == 0
6079 || strncmp (name, ".gnu.linkonce.td.", 17) == 0)
6080 flags |= SECTION_TLS;
6082 if (strcmp (name, ".tbss") == 0
6083 || strncmp (name, ".tbss.", 6) == 0
6084 || strncmp (name, ".gnu.linkonce.tb.", 17) == 0)
6085 flags |= SECTION_TLS | SECTION_BSS;
6087 /* These three sections have special ELF types. They are neither
6088 SHT_PROGBITS nor SHT_NOBITS, so when changing sections we don't
6089 want to print a section type (@progbits or @nobits). If someone
6090 is silly enough to emit code or TLS variables to one of these
6091 sections, then don't handle them specially. */
6092 if (!(flags & (SECTION_CODE | SECTION_BSS | SECTION_TLS))
6093 && (strcmp (name, ".init_array") == 0
6094 || strcmp (name, ".fini_array") == 0
6095 || strcmp (name, ".preinit_array") == 0))
6096 flags |= SECTION_NOTYPE;
6098 return flags;
6101 /* Return true if the target supports some form of global BSS,
6102 either through bss_noswitch_section, or by selecting a BSS
6103 section in TARGET_ASM_SELECT_SECTION. */
6105 bool
6106 have_global_bss_p (void)
6108 return bss_noswitch_section || targetm.have_switchable_bss_sections;
6111 /* Output assembly to switch to section NAME with attribute FLAGS.
6112 Four variants for common object file formats. */
6114 void
6115 default_no_named_section (const char *name ATTRIBUTE_UNUSED,
6116 unsigned int flags ATTRIBUTE_UNUSED,
6117 tree decl ATTRIBUTE_UNUSED)
6119 /* Some object formats don't support named sections at all. The
6120 front-end should already have flagged this as an error. */
6121 gcc_unreachable ();
6124 #ifndef TLS_SECTION_ASM_FLAG
6125 #define TLS_SECTION_ASM_FLAG 'T'
6126 #endif
6128 void
6129 default_elf_asm_named_section (const char *name, unsigned int flags,
6130 tree decl ATTRIBUTE_UNUSED)
6132 char flagchars[10], *f = flagchars;
6134 /* If we have already declared this section, we can use an
6135 abbreviated form to switch back to it -- unless this section is
6136 part of a COMDAT groups, in which case GAS requires the full
6137 declaration every time. */
6138 if (!(HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6139 && (flags & SECTION_DECLARED))
6141 fprintf (asm_out_file, "\t.section\t%s\n", name);
6142 return;
6145 if (!(flags & SECTION_DEBUG))
6146 *f++ = 'a';
6147 #if defined (HAVE_GAS_SECTION_EXCLUDE) && HAVE_GAS_SECTION_EXCLUDE == 1
6148 if (flags & SECTION_EXCLUDE)
6149 *f++ = 'e';
6150 #endif
6151 if (flags & SECTION_WRITE)
6152 *f++ = 'w';
6153 if (flags & SECTION_CODE)
6154 *f++ = 'x';
6155 if (flags & SECTION_SMALL)
6156 *f++ = 's';
6157 if (flags & SECTION_MERGE)
6158 *f++ = 'M';
6159 if (flags & SECTION_STRINGS)
6160 *f++ = 'S';
6161 if (flags & SECTION_TLS)
6162 *f++ = TLS_SECTION_ASM_FLAG;
6163 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6164 *f++ = 'G';
6165 *f = '\0';
6167 fprintf (asm_out_file, "\t.section\t%s,\"%s\"", name, flagchars);
6169 if (!(flags & SECTION_NOTYPE))
6171 const char *type;
6172 const char *format;
6174 if (flags & SECTION_BSS)
6175 type = "nobits";
6176 else
6177 type = "progbits";
6179 format = ",@%s";
6180 /* On platforms that use "@" as the assembly comment character,
6181 use "%" instead. */
6182 if (strcmp (ASM_COMMENT_START, "@") == 0)
6183 format = ",%%%s";
6184 fprintf (asm_out_file, format, type);
6186 if (flags & SECTION_ENTSIZE)
6187 fprintf (asm_out_file, ",%d", flags & SECTION_ENTSIZE);
6188 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6190 if (TREE_CODE (decl) == IDENTIFIER_NODE)
6191 fprintf (asm_out_file, ",%s,comdat", IDENTIFIER_POINTER (decl));
6192 else
6193 fprintf (asm_out_file, ",%s,comdat",
6194 IDENTIFIER_POINTER (DECL_COMDAT_GROUP (decl)));
6198 putc ('\n', asm_out_file);
6201 void
6202 default_coff_asm_named_section (const char *name, unsigned int flags,
6203 tree decl ATTRIBUTE_UNUSED)
6205 char flagchars[8], *f = flagchars;
6207 if (flags & SECTION_WRITE)
6208 *f++ = 'w';
6209 if (flags & SECTION_CODE)
6210 *f++ = 'x';
6211 *f = '\0';
6213 fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
6216 void
6217 default_pe_asm_named_section (const char *name, unsigned int flags,
6218 tree decl)
6220 default_coff_asm_named_section (name, flags, decl);
6222 if (flags & SECTION_LINKONCE)
6224 /* Functions may have been compiled at various levels of
6225 optimization so we can't use `same_size' here.
6226 Instead, have the linker pick one. */
6227 fprintf (asm_out_file, "\t.linkonce %s\n",
6228 (flags & SECTION_CODE ? "discard" : "same_size"));
6232 /* The lame default section selector. */
6234 section *
6235 default_select_section (tree decl, int reloc,
6236 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
6238 if (DECL_P (decl))
6240 if (decl_readonly_section (decl, reloc))
6241 return readonly_data_section;
6243 else if (TREE_CODE (decl) == CONSTRUCTOR)
6245 if (! ((flag_pic && reloc)
6246 || !TREE_READONLY (decl)
6247 || TREE_SIDE_EFFECTS (decl)
6248 || !TREE_CONSTANT (decl)))
6249 return readonly_data_section;
6251 else if (TREE_CODE (decl) == STRING_CST)
6252 return readonly_data_section;
6253 else if (! (flag_pic && reloc))
6254 return readonly_data_section;
6256 return data_section;
6259 enum section_category
6260 categorize_decl_for_section (const_tree decl, int reloc)
6262 enum section_category ret;
6264 if (TREE_CODE (decl) == FUNCTION_DECL)
6265 return SECCAT_TEXT;
6266 else if (TREE_CODE (decl) == STRING_CST)
6268 if ((flag_sanitize & SANITIZE_ADDRESS)
6269 && asan_protect_global (CONST_CAST_TREE (decl)))
6270 /* or !flag_merge_constants */
6271 return SECCAT_RODATA;
6272 else
6273 return SECCAT_RODATA_MERGE_STR;
6275 else if (TREE_CODE (decl) == VAR_DECL)
6277 if (bss_initializer_p (decl))
6278 ret = SECCAT_BSS;
6279 else if (! TREE_READONLY (decl)
6280 || TREE_SIDE_EFFECTS (decl)
6281 || ! TREE_CONSTANT (DECL_INITIAL (decl)))
6283 /* Here the reloc_rw_mask is not testing whether the section should
6284 be read-only or not, but whether the dynamic link will have to
6285 do something. If so, we wish to segregate the data in order to
6286 minimize cache misses inside the dynamic linker. */
6287 if (reloc & targetm.asm_out.reloc_rw_mask ())
6288 ret = reloc == 1 ? SECCAT_DATA_REL_LOCAL : SECCAT_DATA_REL;
6289 else
6290 ret = SECCAT_DATA;
6292 else if (reloc & targetm.asm_out.reloc_rw_mask ())
6293 ret = reloc == 1 ? SECCAT_DATA_REL_RO_LOCAL : SECCAT_DATA_REL_RO;
6294 else if (reloc || flag_merge_constants < 2
6295 || ((flag_sanitize & SANITIZE_ADDRESS)
6296 && asan_protect_global (CONST_CAST_TREE (decl))))
6297 /* C and C++ don't allow different variables to share the same
6298 location. -fmerge-all-constants allows even that (at the
6299 expense of not conforming). */
6300 ret = SECCAT_RODATA;
6301 else if (TREE_CODE (DECL_INITIAL (decl)) == STRING_CST)
6302 ret = SECCAT_RODATA_MERGE_STR_INIT;
6303 else
6304 ret = SECCAT_RODATA_MERGE_CONST;
6306 else if (TREE_CODE (decl) == CONSTRUCTOR)
6308 if ((reloc & targetm.asm_out.reloc_rw_mask ())
6309 || TREE_SIDE_EFFECTS (decl)
6310 || ! TREE_CONSTANT (decl))
6311 ret = SECCAT_DATA;
6312 else
6313 ret = SECCAT_RODATA;
6315 else
6316 ret = SECCAT_RODATA;
6318 /* There are no read-only thread-local sections. */
6319 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
6321 /* Note that this would be *just* SECCAT_BSS, except that there's
6322 no concept of a read-only thread-local-data section. */
6323 if (ret == SECCAT_BSS
6324 || (flag_zero_initialized_in_bss
6325 && initializer_zerop (DECL_INITIAL (decl))))
6326 ret = SECCAT_TBSS;
6327 else
6328 ret = SECCAT_TDATA;
6331 /* If the target uses small data sections, select it. */
6332 else if (targetm.in_small_data_p (decl))
6334 if (ret == SECCAT_BSS)
6335 ret = SECCAT_SBSS;
6336 else if (targetm.have_srodata_section && ret == SECCAT_RODATA)
6337 ret = SECCAT_SRODATA;
6338 else
6339 ret = SECCAT_SDATA;
6342 return ret;
6345 static bool
6346 decl_readonly_section_1 (enum section_category category)
6348 switch (category)
6350 case SECCAT_RODATA:
6351 case SECCAT_RODATA_MERGE_STR:
6352 case SECCAT_RODATA_MERGE_STR_INIT:
6353 case SECCAT_RODATA_MERGE_CONST:
6354 case SECCAT_SRODATA:
6355 return true;
6356 default:
6357 return false;
6361 bool
6362 decl_readonly_section (const_tree decl, int reloc)
6364 return decl_readonly_section_1 (categorize_decl_for_section (decl, reloc));
6367 /* Select a section based on the above categorization. */
6369 section *
6370 default_elf_select_section (tree decl, int reloc,
6371 unsigned HOST_WIDE_INT align)
6373 const char *sname;
6374 switch (categorize_decl_for_section (decl, reloc))
6376 case SECCAT_TEXT:
6377 /* We're not supposed to be called on FUNCTION_DECLs. */
6378 gcc_unreachable ();
6379 case SECCAT_RODATA:
6380 return readonly_data_section;
6381 case SECCAT_RODATA_MERGE_STR:
6382 return mergeable_string_section (decl, align, 0);
6383 case SECCAT_RODATA_MERGE_STR_INIT:
6384 return mergeable_string_section (DECL_INITIAL (decl), align, 0);
6385 case SECCAT_RODATA_MERGE_CONST:
6386 return mergeable_constant_section (DECL_MODE (decl), align, 0);
6387 case SECCAT_SRODATA:
6388 sname = ".sdata2";
6389 break;
6390 case SECCAT_DATA:
6391 return data_section;
6392 case SECCAT_DATA_REL:
6393 sname = ".data.rel";
6394 break;
6395 case SECCAT_DATA_REL_LOCAL:
6396 sname = ".data.rel.local";
6397 break;
6398 case SECCAT_DATA_REL_RO:
6399 sname = ".data.rel.ro";
6400 break;
6401 case SECCAT_DATA_REL_RO_LOCAL:
6402 sname = ".data.rel.ro.local";
6403 break;
6404 case SECCAT_SDATA:
6405 sname = ".sdata";
6406 break;
6407 case SECCAT_TDATA:
6408 sname = ".tdata";
6409 break;
6410 case SECCAT_BSS:
6411 if (bss_section)
6412 return bss_section;
6413 sname = ".bss";
6414 break;
6415 case SECCAT_SBSS:
6416 sname = ".sbss";
6417 break;
6418 case SECCAT_TBSS:
6419 sname = ".tbss";
6420 break;
6421 default:
6422 gcc_unreachable ();
6425 return get_named_section (decl, sname, reloc);
6428 /* Construct a unique section name based on the decl name and the
6429 categorization performed above. */
6431 void
6432 default_unique_section (tree decl, int reloc)
6434 /* We only need to use .gnu.linkonce if we don't have COMDAT groups. */
6435 bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
6436 const char *prefix, *name, *linkonce;
6437 char *string;
6439 switch (categorize_decl_for_section (decl, reloc))
6441 case SECCAT_TEXT:
6442 prefix = one_only ? ".t" : ".text";
6443 break;
6444 case SECCAT_RODATA:
6445 case SECCAT_RODATA_MERGE_STR:
6446 case SECCAT_RODATA_MERGE_STR_INIT:
6447 case SECCAT_RODATA_MERGE_CONST:
6448 prefix = one_only ? ".r" : ".rodata";
6449 break;
6450 case SECCAT_SRODATA:
6451 prefix = one_only ? ".s2" : ".sdata2";
6452 break;
6453 case SECCAT_DATA:
6454 prefix = one_only ? ".d" : ".data";
6455 break;
6456 case SECCAT_DATA_REL:
6457 prefix = one_only ? ".d.rel" : ".data.rel";
6458 break;
6459 case SECCAT_DATA_REL_LOCAL:
6460 prefix = one_only ? ".d.rel.local" : ".data.rel.local";
6461 break;
6462 case SECCAT_DATA_REL_RO:
6463 prefix = one_only ? ".d.rel.ro" : ".data.rel.ro";
6464 break;
6465 case SECCAT_DATA_REL_RO_LOCAL:
6466 prefix = one_only ? ".d.rel.ro.local" : ".data.rel.ro.local";
6467 break;
6468 case SECCAT_SDATA:
6469 prefix = one_only ? ".s" : ".sdata";
6470 break;
6471 case SECCAT_BSS:
6472 prefix = one_only ? ".b" : ".bss";
6473 break;
6474 case SECCAT_SBSS:
6475 prefix = one_only ? ".sb" : ".sbss";
6476 break;
6477 case SECCAT_TDATA:
6478 prefix = one_only ? ".td" : ".tdata";
6479 break;
6480 case SECCAT_TBSS:
6481 prefix = one_only ? ".tb" : ".tbss";
6482 break;
6483 default:
6484 gcc_unreachable ();
6487 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
6488 name = targetm.strip_name_encoding (name);
6490 /* If we're using one_only, then there needs to be a .gnu.linkonce
6491 prefix to the section name. */
6492 linkonce = one_only ? ".gnu.linkonce" : "";
6494 string = ACONCAT ((linkonce, prefix, ".", name, NULL));
6496 set_decl_section_name (decl, string);
6499 /* Subroutine of compute_reloc_for_rtx for leaf rtxes. */
6501 static int
6502 compute_reloc_for_rtx_1 (const_rtx x)
6504 switch (GET_CODE (x))
6506 case SYMBOL_REF:
6507 return SYMBOL_REF_LOCAL_P (x) ? 1 : 2;
6508 case LABEL_REF:
6509 return 1;
6510 default:
6511 return 0;
6515 /* Like compute_reloc_for_constant, except for an RTX. The return value
6516 is a mask for which bit 1 indicates a global relocation, and bit 0
6517 indicates a local relocation. */
6519 static int
6520 compute_reloc_for_rtx (const_rtx x)
6522 switch (GET_CODE (x))
6524 case SYMBOL_REF:
6525 case LABEL_REF:
6526 return compute_reloc_for_rtx_1 (x);
6528 case CONST:
6530 int reloc = 0;
6531 subrtx_iterator::array_type array;
6532 FOR_EACH_SUBRTX (iter, array, x, ALL)
6533 reloc |= compute_reloc_for_rtx_1 (*iter);
6534 return reloc;
6537 default:
6538 return 0;
6542 section *
6543 default_select_rtx_section (machine_mode mode ATTRIBUTE_UNUSED,
6544 rtx x,
6545 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
6547 if (compute_reloc_for_rtx (x) & targetm.asm_out.reloc_rw_mask ())
6548 return data_section;
6549 else
6550 return readonly_data_section;
6553 section *
6554 default_elf_select_rtx_section (machine_mode mode, rtx x,
6555 unsigned HOST_WIDE_INT align)
6557 int reloc = compute_reloc_for_rtx (x);
6559 /* ??? Handle small data here somehow. */
6561 if (reloc & targetm.asm_out.reloc_rw_mask ())
6563 if (reloc == 1)
6564 return get_named_section (NULL, ".data.rel.ro.local", 1);
6565 else
6566 return get_named_section (NULL, ".data.rel.ro", 3);
6569 return mergeable_constant_section (mode, align, 0);
6572 /* Set the generally applicable flags on the SYMBOL_REF for EXP. */
6574 void
6575 default_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
6577 rtx symbol;
6578 int flags;
6580 /* Careful not to prod global register variables. */
6581 if (!MEM_P (rtl))
6582 return;
6583 symbol = XEXP (rtl, 0);
6584 if (GET_CODE (symbol) != SYMBOL_REF)
6585 return;
6587 flags = SYMBOL_REF_FLAGS (symbol) & SYMBOL_FLAG_HAS_BLOCK_INFO;
6588 if (TREE_CODE (decl) == FUNCTION_DECL)
6589 flags |= SYMBOL_FLAG_FUNCTION;
6590 if (targetm.binds_local_p (decl))
6591 flags |= SYMBOL_FLAG_LOCAL;
6592 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
6593 flags |= DECL_TLS_MODEL (decl) << SYMBOL_FLAG_TLS_SHIFT;
6594 else if (targetm.in_small_data_p (decl))
6595 flags |= SYMBOL_FLAG_SMALL;
6596 /* ??? Why is DECL_EXTERNAL ever set for non-PUBLIC names? Without
6597 being PUBLIC, the thing *must* be defined in this translation unit.
6598 Prevent this buglet from being propagated into rtl code as well. */
6599 if (DECL_P (decl) && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl))
6600 flags |= SYMBOL_FLAG_EXTERNAL;
6602 SYMBOL_REF_FLAGS (symbol) = flags;
6605 /* By default, we do nothing for encode_section_info, so we need not
6606 do anything but discard the '*' marker. */
6608 const char *
6609 default_strip_name_encoding (const char *str)
6611 return str + (*str == '*');
6614 #ifdef ASM_OUTPUT_DEF
6615 /* The default implementation of TARGET_ASM_OUTPUT_ANCHOR. Define the
6616 anchor relative to ".", the current section position. */
6618 void
6619 default_asm_output_anchor (rtx symbol)
6621 char buffer[100];
6623 sprintf (buffer, "*. + " HOST_WIDE_INT_PRINT_DEC,
6624 SYMBOL_REF_BLOCK_OFFSET (symbol));
6625 ASM_OUTPUT_DEF (asm_out_file, XSTR (symbol, 0), buffer);
6627 #endif
6629 /* The default implementation of TARGET_USE_ANCHORS_FOR_SYMBOL_P. */
6631 bool
6632 default_use_anchors_for_symbol_p (const_rtx symbol)
6634 section *sect;
6635 tree decl;
6637 /* Don't use anchors for mergeable sections. The linker might move
6638 the objects around. */
6639 sect = SYMBOL_REF_BLOCK (symbol)->sect;
6640 if (sect->common.flags & SECTION_MERGE)
6641 return false;
6643 /* Don't use anchors for small data sections. The small data register
6644 acts as an anchor for such sections. */
6645 if (sect->common.flags & SECTION_SMALL)
6646 return false;
6648 decl = SYMBOL_REF_DECL (symbol);
6649 if (decl && DECL_P (decl))
6651 /* Don't use section anchors for decls that might be defined or
6652 usurped by other modules. */
6653 if (TREE_PUBLIC (decl) && !decl_binds_to_current_def_p (decl))
6654 return false;
6656 /* Don't use section anchors for decls that will be placed in a
6657 small data section. */
6658 /* ??? Ideally, this check would be redundant with the SECTION_SMALL
6659 one above. The problem is that we only use SECTION_SMALL for
6660 sections that should be marked as small in the section directive. */
6661 if (targetm.in_small_data_p (decl))
6662 return false;
6664 return true;
6667 /* Return true when RESOLUTION indicate that symbol will be bound to the
6668 definition provided by current .o file. */
6670 static bool
6671 resolution_to_local_definition_p (enum ld_plugin_symbol_resolution resolution)
6673 return (resolution == LDPR_PREVAILING_DEF
6674 || resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
6675 || resolution == LDPR_PREVAILING_DEF_IRONLY);
6678 /* Return true when RESOLUTION indicate that symbol will be bound locally
6679 within current executable or DSO. */
6681 static bool
6682 resolution_local_p (enum ld_plugin_symbol_resolution resolution)
6684 return (resolution == LDPR_PREVAILING_DEF
6685 || resolution == LDPR_PREVAILING_DEF_IRONLY
6686 || resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
6687 || resolution == LDPR_PREEMPTED_REG
6688 || resolution == LDPR_PREEMPTED_IR
6689 || resolution == LDPR_RESOLVED_IR
6690 || resolution == LDPR_RESOLVED_EXEC);
6693 /* Assume ELF-ish defaults, since that's pretty much the most liberal
6694 wrt cross-module name binding. */
6696 bool
6697 default_binds_local_p (const_tree exp)
6699 return default_binds_local_p_1 (exp, flag_shlib);
6702 bool
6703 default_binds_local_p_1 (const_tree exp, int shlib)
6705 bool local_p;
6706 bool resolved_locally = false;
6707 bool resolved_to_local_def = false;
6709 /* With resolution file in hands, take look into resolutions.
6710 We can't just return true for resolved_locally symbols,
6711 because dynamic linking might overwrite symbols
6712 in shared libraries. */
6713 if (TREE_CODE (exp) == VAR_DECL && TREE_PUBLIC (exp)
6714 && (TREE_STATIC (exp) || DECL_EXTERNAL (exp)))
6716 varpool_node *vnode = varpool_node::get (exp);
6717 if (vnode && (resolution_local_p (vnode->resolution) || vnode->in_other_partition))
6718 resolved_locally = true;
6719 if (vnode
6720 && resolution_to_local_definition_p (vnode->resolution))
6721 resolved_to_local_def = true;
6723 else if (TREE_CODE (exp) == FUNCTION_DECL && TREE_PUBLIC (exp))
6725 struct cgraph_node *node = cgraph_node::get (exp);
6726 if (node
6727 && (resolution_local_p (node->resolution) || node->in_other_partition))
6728 resolved_locally = true;
6729 if (node
6730 && resolution_to_local_definition_p (node->resolution))
6731 resolved_to_local_def = true;
6734 /* A non-decl is an entry in the constant pool. */
6735 if (!DECL_P (exp))
6736 local_p = true;
6737 /* Weakrefs may not bind locally, even though the weakref itself is always
6738 static and therefore local. Similarly, the resolver for ifunc functions
6739 might resolve to a non-local function.
6740 FIXME: We can resolve the weakref case more curefuly by looking at the
6741 weakref alias. */
6742 else if (lookup_attribute ("weakref", DECL_ATTRIBUTES (exp))
6743 || (TREE_CODE (exp) == FUNCTION_DECL
6744 && lookup_attribute ("ifunc", DECL_ATTRIBUTES (exp))))
6745 local_p = false;
6746 /* Static variables are always local. */
6747 else if (! TREE_PUBLIC (exp))
6748 local_p = true;
6749 /* A variable is local if the user has said explicitly that it will
6750 be. */
6751 else if ((DECL_VISIBILITY_SPECIFIED (exp)
6752 || resolved_to_local_def)
6753 && DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
6754 local_p = true;
6755 /* Variables defined outside this object might not be local. */
6756 else if (DECL_EXTERNAL (exp) && !resolved_locally)
6757 local_p = false;
6758 /* If defined in this object and visibility is not default, must be
6759 local. */
6760 else if (DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
6761 local_p = true;
6762 /* Default visibility weak data can be overridden by a strong symbol
6763 in another module and so are not local. */
6764 else if (DECL_WEAK (exp)
6765 && !resolved_locally)
6766 local_p = false;
6767 /* If PIC, then assume that any global name can be overridden by
6768 symbols resolved from other modules. */
6769 else if (shlib)
6770 local_p = false;
6771 /* Uninitialized COMMON variable may be unified with symbols
6772 resolved from other modules. */
6773 else if (DECL_COMMON (exp)
6774 && !resolved_locally
6775 && (DECL_INITIAL (exp) == NULL
6776 || (!in_lto_p && DECL_INITIAL (exp) == error_mark_node)))
6777 local_p = false;
6778 /* Otherwise we're left with initialized (or non-common) global data
6779 which is of necessity defined locally. */
6780 else
6781 local_p = true;
6783 return local_p;
6786 /* Return true when references to DECL must bind to current definition in
6787 final executable.
6789 The condition is usually equivalent to whether the function binds to the
6790 current module (shared library or executable), that is to binds_local_p.
6791 We use this fact to avoid need for another target hook and implement
6792 the logic using binds_local_p and just special cases where
6793 decl_binds_to_current_def_p is stronger than binds_local_p. In particular
6794 the weak definitions (that can be overwritten at linktime by other
6795 definition from different object file) and when resolution info is available
6796 we simply use the knowledge passed to us by linker plugin. */
6797 bool
6798 decl_binds_to_current_def_p (const_tree decl)
6800 gcc_assert (DECL_P (decl));
6801 if (!targetm.binds_local_p (decl))
6802 return false;
6803 if (!TREE_PUBLIC (decl))
6804 return true;
6805 /* When resolution is available, just use it. */
6806 if (TREE_CODE (decl) == VAR_DECL
6807 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
6809 varpool_node *vnode = varpool_node::get (decl);
6810 if (vnode
6811 && vnode->resolution != LDPR_UNKNOWN)
6812 return resolution_to_local_definition_p (vnode->resolution);
6814 else if (TREE_CODE (decl) == FUNCTION_DECL)
6816 struct cgraph_node *node = cgraph_node::get (decl);
6817 if (node
6818 && node->resolution != LDPR_UNKNOWN)
6819 return resolution_to_local_definition_p (node->resolution);
6821 /* Otherwise we have to assume the worst for DECL_WEAK (hidden weaks
6822 binds locally but still can be overwritten), DECL_COMMON (can be merged
6823 with a non-common definition somewhere in the same module) or
6824 DECL_EXTERNAL.
6825 This rely on fact that binds_local_p behave as decl_replaceable_p
6826 for all other declaration types. */
6827 if (DECL_WEAK (decl))
6828 return false;
6829 if (DECL_COMMON (decl)
6830 && (DECL_INITIAL (decl) == NULL
6831 || (!in_lto_p && DECL_INITIAL (decl) == error_mark_node)))
6832 return false;
6833 if (DECL_EXTERNAL (decl))
6834 return false;
6835 return true;
6838 /* A replaceable function or variable is one which may be replaced
6839 at link-time with an entirely different definition, provided that the
6840 replacement has the same type. For example, functions declared
6841 with __attribute__((weak)) on most systems are replaceable.
6843 COMDAT functions are not replaceable, since all definitions of the
6844 function must be equivalent. It is important that COMDAT functions
6845 not be treated as replaceable so that use of C++ template
6846 instantiations is not penalized. */
6848 bool
6849 decl_replaceable_p (tree decl)
6851 gcc_assert (DECL_P (decl));
6852 if (!TREE_PUBLIC (decl) || DECL_COMDAT (decl))
6853 return false;
6854 if (!flag_semantic_interposition
6855 && !DECL_WEAK (decl))
6856 return false;
6857 return !decl_binds_to_current_def_p (decl);
6860 /* Default function to output code that will globalize a label. A
6861 target must define GLOBAL_ASM_OP or provide its own function to
6862 globalize a label. */
6863 #ifdef GLOBAL_ASM_OP
6864 void
6865 default_globalize_label (FILE * stream, const char *name)
6867 fputs (GLOBAL_ASM_OP, stream);
6868 assemble_name (stream, name);
6869 putc ('\n', stream);
6871 #endif /* GLOBAL_ASM_OP */
6873 /* Default function to output code that will globalize a declaration. */
6874 void
6875 default_globalize_decl_name (FILE * stream, tree decl)
6877 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
6878 targetm.asm_out.globalize_label (stream, name);
6881 /* Default function to output a label for unwind information. The
6882 default is to do nothing. A target that needs nonlocal labels for
6883 unwind information must provide its own function to do this. */
6884 void
6885 default_emit_unwind_label (FILE * stream ATTRIBUTE_UNUSED,
6886 tree decl ATTRIBUTE_UNUSED,
6887 int for_eh ATTRIBUTE_UNUSED,
6888 int empty ATTRIBUTE_UNUSED)
6892 /* Default function to output a label to divide up the exception table.
6893 The default is to do nothing. A target that needs/wants to divide
6894 up the table must provide it's own function to do this. */
6895 void
6896 default_emit_except_table_label (FILE * stream ATTRIBUTE_UNUSED)
6900 /* This is how to output an internal numbered label where PREFIX is
6901 the class of label and LABELNO is the number within the class. */
6903 void
6904 default_generate_internal_label (char *buf, const char *prefix,
6905 unsigned long labelno)
6907 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
6910 /* This is how to output an internal numbered label where PREFIX is
6911 the class of label and LABELNO is the number within the class. */
6913 void
6914 default_internal_label (FILE *stream, const char *prefix,
6915 unsigned long labelno)
6917 char *const buf = (char *) alloca (40 + strlen (prefix));
6918 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
6919 ASM_OUTPUT_INTERNAL_LABEL (stream, buf);
6923 /* The default implementation of ASM_DECLARE_CONSTANT_NAME. */
6925 void
6926 default_asm_declare_constant_name (FILE *file, const char *name,
6927 const_tree exp ATTRIBUTE_UNUSED,
6928 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
6930 assemble_label (file, name);
6933 /* This is the default behavior at the beginning of a file. It's
6934 controlled by two other target-hook toggles. */
6935 void
6936 default_file_start (void)
6938 if (targetm.asm_file_start_app_off
6939 && !(flag_verbose_asm || flag_debug_asm || flag_dump_rtl_in_asm))
6940 fputs (ASM_APP_OFF, asm_out_file);
6942 if (targetm.asm_file_start_file_directive)
6943 output_file_directive (asm_out_file, main_input_filename);
6946 /* This is a generic routine suitable for use as TARGET_ASM_FILE_END
6947 which emits a special section directive used to indicate whether or
6948 not this object file needs an executable stack. This is primarily
6949 a GNU extension to ELF but could be used on other targets. */
6951 int trampolines_created;
6953 void
6954 file_end_indicate_exec_stack (void)
6956 unsigned int flags = SECTION_DEBUG;
6957 if (trampolines_created)
6958 flags |= SECTION_CODE;
6960 switch_to_section (get_section (".note.GNU-stack", flags, NULL));
6963 /* Emit a special section directive to indicate that this object file
6964 was compiled with -fsplit-stack. This is used to let the linker
6965 detect calls between split-stack code and non-split-stack code, so
6966 that it can modify the split-stack code to allocate a sufficiently
6967 large stack. We emit another special section if there are any
6968 functions in this file which have the no_split_stack attribute, to
6969 prevent the linker from warning about being unable to convert the
6970 functions if they call non-split-stack code. */
6972 void
6973 file_end_indicate_split_stack (void)
6975 if (flag_split_stack)
6977 switch_to_section (get_section (".note.GNU-split-stack", SECTION_DEBUG,
6978 NULL));
6979 if (saw_no_split_stack)
6980 switch_to_section (get_section (".note.GNU-no-split-stack",
6981 SECTION_DEBUG, NULL));
6985 /* Output DIRECTIVE (a C string) followed by a newline. This is used as
6986 a get_unnamed_section callback. */
6988 void
6989 output_section_asm_op (const void *directive)
6991 fprintf (asm_out_file, "%s\n", (const char *) directive);
6994 /* Emit assembly code to switch to section NEW_SECTION. Do nothing if
6995 the current section is NEW_SECTION. */
6997 void
6998 switch_to_section (section *new_section)
7000 if (in_section == new_section)
7001 return;
7003 if (new_section->common.flags & SECTION_FORGET)
7004 in_section = NULL;
7005 else
7006 in_section = new_section;
7008 switch (SECTION_STYLE (new_section))
7010 case SECTION_NAMED:
7011 targetm.asm_out.named_section (new_section->named.name,
7012 new_section->named.common.flags,
7013 new_section->named.decl);
7014 break;
7016 case SECTION_UNNAMED:
7017 new_section->unnamed.callback (new_section->unnamed.data);
7018 break;
7020 case SECTION_NOSWITCH:
7021 gcc_unreachable ();
7022 break;
7025 new_section->common.flags |= SECTION_DECLARED;
7028 /* If block symbol SYMBOL has not yet been assigned an offset, place
7029 it at the end of its block. */
7031 void
7032 place_block_symbol (rtx symbol)
7034 unsigned HOST_WIDE_INT size, mask, offset;
7035 struct constant_descriptor_rtx *desc;
7036 unsigned int alignment;
7037 struct object_block *block;
7038 tree decl;
7040 gcc_assert (SYMBOL_REF_BLOCK (symbol));
7041 if (SYMBOL_REF_BLOCK_OFFSET (symbol) >= 0)
7042 return;
7044 /* Work out the symbol's size and alignment. */
7045 if (CONSTANT_POOL_ADDRESS_P (symbol))
7047 desc = SYMBOL_REF_CONSTANT (symbol);
7048 alignment = desc->align;
7049 size = GET_MODE_SIZE (desc->mode);
7051 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
7053 decl = SYMBOL_REF_DECL (symbol);
7054 alignment = DECL_ALIGN (decl);
7055 size = get_constant_size (DECL_INITIAL (decl));
7056 if ((flag_sanitize & SANITIZE_ADDRESS)
7057 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
7058 && asan_protect_global (DECL_INITIAL (decl)))
7059 size += asan_red_zone_size (size);
7061 else
7063 struct symtab_node *snode;
7064 decl = SYMBOL_REF_DECL (symbol);
7066 snode = symtab_node::get (decl);
7067 if (snode->alias)
7069 rtx target = DECL_RTL (snode->ultimate_alias_target ()->decl);
7071 place_block_symbol (target);
7072 SYMBOL_REF_BLOCK_OFFSET (symbol) = SYMBOL_REF_BLOCK_OFFSET (target);
7073 return;
7075 alignment = get_variable_align (decl);
7076 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
7077 if ((flag_sanitize & SANITIZE_ADDRESS)
7078 && asan_protect_global (decl))
7080 size += asan_red_zone_size (size);
7081 alignment = MAX (alignment,
7082 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
7086 /* Calculate the object's offset from the start of the block. */
7087 block = SYMBOL_REF_BLOCK (symbol);
7088 mask = alignment / BITS_PER_UNIT - 1;
7089 offset = (block->size + mask) & ~mask;
7090 SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
7092 /* Record the block's new alignment and size. */
7093 block->alignment = MAX (block->alignment, alignment);
7094 block->size = offset + size;
7096 vec_safe_push (block->objects, symbol);
7099 /* Return the anchor that should be used to address byte offset OFFSET
7100 from the first object in BLOCK. MODEL is the TLS model used
7101 to access it. */
7104 get_section_anchor (struct object_block *block, HOST_WIDE_INT offset,
7105 enum tls_model model)
7107 char label[100];
7108 unsigned int begin, middle, end;
7109 unsigned HOST_WIDE_INT min_offset, max_offset, range, bias, delta;
7110 rtx anchor;
7112 /* Work out the anchor's offset. Use an offset of 0 for the first
7113 anchor so that we don't pessimize the case where we take the address
7114 of a variable at the beginning of the block. This is particularly
7115 useful when a block has only one variable assigned to it.
7117 We try to place anchors RANGE bytes apart, so there can then be
7118 anchors at +/-RANGE, +/-2 * RANGE, and so on, up to the limits of
7119 a ptr_mode offset. With some target settings, the lowest such
7120 anchor might be out of range for the lowest ptr_mode offset;
7121 likewise the highest anchor for the highest offset. Use anchors
7122 at the extreme ends of the ptr_mode range in such cases.
7124 All arithmetic uses unsigned integers in order to avoid
7125 signed overflow. */
7126 max_offset = (unsigned HOST_WIDE_INT) targetm.max_anchor_offset;
7127 min_offset = (unsigned HOST_WIDE_INT) targetm.min_anchor_offset;
7128 range = max_offset - min_offset + 1;
7129 if (range == 0)
7130 offset = 0;
7131 else
7133 bias = 1 << (GET_MODE_BITSIZE (ptr_mode) - 1);
7134 if (offset < 0)
7136 delta = -(unsigned HOST_WIDE_INT) offset + max_offset;
7137 delta -= delta % range;
7138 if (delta > bias)
7139 delta = bias;
7140 offset = (HOST_WIDE_INT) (-delta);
7142 else
7144 delta = (unsigned HOST_WIDE_INT) offset - min_offset;
7145 delta -= delta % range;
7146 if (delta > bias - 1)
7147 delta = bias - 1;
7148 offset = (HOST_WIDE_INT) delta;
7152 /* Do a binary search to see if there's already an anchor we can use.
7153 Set BEGIN to the new anchor's index if not. */
7154 begin = 0;
7155 end = vec_safe_length (block->anchors);
7156 while (begin != end)
7158 middle = (end + begin) / 2;
7159 anchor = (*block->anchors)[middle];
7160 if (SYMBOL_REF_BLOCK_OFFSET (anchor) > offset)
7161 end = middle;
7162 else if (SYMBOL_REF_BLOCK_OFFSET (anchor) < offset)
7163 begin = middle + 1;
7164 else if (SYMBOL_REF_TLS_MODEL (anchor) > model)
7165 end = middle;
7166 else if (SYMBOL_REF_TLS_MODEL (anchor) < model)
7167 begin = middle + 1;
7168 else
7169 return anchor;
7172 /* Create a new anchor with a unique label. */
7173 ASM_GENERATE_INTERNAL_LABEL (label, "LANCHOR", anchor_labelno++);
7174 anchor = create_block_symbol (ggc_strdup (label), block, offset);
7175 SYMBOL_REF_FLAGS (anchor) |= SYMBOL_FLAG_LOCAL | SYMBOL_FLAG_ANCHOR;
7176 SYMBOL_REF_FLAGS (anchor) |= model << SYMBOL_FLAG_TLS_SHIFT;
7178 /* Insert it at index BEGIN. */
7179 vec_safe_insert (block->anchors, begin, anchor);
7180 return anchor;
7183 /* Output the objects in BLOCK. */
7185 static void
7186 output_object_block (struct object_block *block)
7188 struct constant_descriptor_rtx *desc;
7189 unsigned int i;
7190 HOST_WIDE_INT offset;
7191 tree decl;
7192 rtx symbol;
7194 if (!block->objects)
7195 return;
7197 /* Switch to the section and make sure that the first byte is
7198 suitably aligned. */
7199 switch_to_section (block->sect);
7200 assemble_align (block->alignment);
7202 /* Define the values of all anchors relative to the current section
7203 position. */
7204 FOR_EACH_VEC_SAFE_ELT (block->anchors, i, symbol)
7205 targetm.asm_out.output_anchor (symbol);
7207 /* Output the objects themselves. */
7208 offset = 0;
7209 FOR_EACH_VEC_ELT (*block->objects, i, symbol)
7211 /* Move to the object's offset, padding with zeros if necessary. */
7212 assemble_zeros (SYMBOL_REF_BLOCK_OFFSET (symbol) - offset);
7213 offset = SYMBOL_REF_BLOCK_OFFSET (symbol);
7214 if (CONSTANT_POOL_ADDRESS_P (symbol))
7216 desc = SYMBOL_REF_CONSTANT (symbol);
7217 output_constant_pool_1 (desc, 1);
7218 offset += GET_MODE_SIZE (desc->mode);
7220 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
7222 HOST_WIDE_INT size;
7223 decl = SYMBOL_REF_DECL (symbol);
7224 assemble_constant_contents (DECL_INITIAL (decl), XSTR (symbol, 0),
7225 DECL_ALIGN (decl));
7226 size = get_constant_size (DECL_INITIAL (decl));
7227 offset += size;
7228 if ((flag_sanitize & SANITIZE_ADDRESS)
7229 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
7230 && asan_protect_global (DECL_INITIAL (decl)))
7232 size = asan_red_zone_size (size);
7233 assemble_zeros (size);
7234 offset += size;
7237 else
7239 HOST_WIDE_INT size;
7240 decl = SYMBOL_REF_DECL (symbol);
7241 assemble_variable_contents (decl, XSTR (symbol, 0), false);
7242 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
7243 offset += size;
7244 if ((flag_sanitize & SANITIZE_ADDRESS)
7245 && asan_protect_global (decl))
7247 size = asan_red_zone_size (size);
7248 assemble_zeros (size);
7249 offset += size;
7255 /* A htab_traverse callback used to call output_object_block for
7256 each member of object_block_htab. */
7259 output_object_block_htab (object_block **slot, void *)
7261 output_object_block (*slot);
7262 return 1;
7265 /* Output the definitions of all object_blocks. */
7267 void
7268 output_object_blocks (void)
7270 object_block_htab->traverse<void *, output_object_block_htab> (NULL);
7273 /* This function provides a possible implementation of the
7274 TARGET_ASM_RECORD_GCC_SWITCHES target hook for ELF targets. When triggered
7275 by -frecord-gcc-switches it creates a new mergeable, string section in the
7276 assembler output file called TARGET_ASM_RECORD_GCC_SWITCHES_SECTION which
7277 contains the switches in ASCII format.
7279 FIXME: This code does not correctly handle double quote characters
7280 that appear inside strings, (it strips them rather than preserving them).
7281 FIXME: ASM_OUTPUT_ASCII, as defined in config/elfos.h will not emit NUL
7282 characters - instead it treats them as sub-string separators. Since
7283 we want to emit NUL strings terminators into the object file we have to use
7284 ASM_OUTPUT_SKIP. */
7287 elf_record_gcc_switches (print_switch_type type, const char * name)
7289 switch (type)
7291 case SWITCH_TYPE_PASSED:
7292 ASM_OUTPUT_ASCII (asm_out_file, name, strlen (name));
7293 ASM_OUTPUT_SKIP (asm_out_file, (unsigned HOST_WIDE_INT) 1);
7294 break;
7296 case SWITCH_TYPE_DESCRIPTIVE:
7297 if (name == NULL)
7299 /* Distinguish between invocations where name is NULL. */
7300 static bool started = false;
7302 if (!started)
7304 section * sec;
7306 sec = get_section (targetm.asm_out.record_gcc_switches_section,
7307 SECTION_DEBUG
7308 | SECTION_MERGE
7309 | SECTION_STRINGS
7310 | (SECTION_ENTSIZE & 1),
7311 NULL);
7312 switch_to_section (sec);
7313 started = true;
7317 default:
7318 break;
7321 /* The return value is currently ignored by the caller, but must be 0.
7322 For -fverbose-asm the return value would be the number of characters
7323 emitted into the assembler file. */
7324 return 0;
7327 /* Emit text to declare externally defined symbols. It is needed to
7328 properly support non-default visibility. */
7329 void
7330 default_elf_asm_output_external (FILE *file ATTRIBUTE_UNUSED,
7331 tree decl,
7332 const char *name ATTRIBUTE_UNUSED)
7334 /* We output the name if and only if TREE_SYMBOL_REFERENCED is
7335 set in order to avoid putting out names that are never really
7336 used. */
7337 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
7338 && targetm.binds_local_p (decl))
7339 maybe_assemble_visibility (decl);
7342 /* The default hook for TARGET_ASM_OUTPUT_SOURCE_FILENAME. */
7344 void
7345 default_asm_output_source_filename (FILE *file, const char *name)
7347 #ifdef ASM_OUTPUT_SOURCE_FILENAME
7348 ASM_OUTPUT_SOURCE_FILENAME (file, name);
7349 #else
7350 fprintf (file, "\t.file\t");
7351 output_quoted_string (file, name);
7352 putc ('\n', file);
7353 #endif
7356 /* Output a file name in the form wanted by System V. */
7358 void
7359 output_file_directive (FILE *asm_file, const char *input_name)
7361 int len;
7362 const char *na;
7364 if (input_name == NULL)
7365 input_name = "<stdin>";
7366 else
7367 input_name = remap_debug_filename (input_name);
7369 len = strlen (input_name);
7370 na = input_name + len;
7372 /* NA gets INPUT_NAME sans directory names. */
7373 while (na > input_name)
7375 if (IS_DIR_SEPARATOR (na[-1]))
7376 break;
7377 na--;
7380 targetm.asm_out.output_source_filename (asm_file, na);
7383 /* Create a DEBUG_EXPR_DECL / DEBUG_EXPR pair from RTL expression
7384 EXP. */
7386 make_debug_expr_from_rtl (const_rtx exp)
7388 tree ddecl = make_node (DEBUG_EXPR_DECL), type;
7389 machine_mode mode = GET_MODE (exp);
7390 rtx dval;
7392 DECL_ARTIFICIAL (ddecl) = 1;
7393 if (REG_P (exp) && REG_EXPR (exp))
7394 type = TREE_TYPE (REG_EXPR (exp));
7395 else if (MEM_P (exp) && MEM_EXPR (exp))
7396 type = TREE_TYPE (MEM_EXPR (exp));
7397 else
7398 type = NULL_TREE;
7399 if (type && TYPE_MODE (type) == mode)
7400 TREE_TYPE (ddecl) = type;
7401 else
7402 TREE_TYPE (ddecl) = lang_hooks.types.type_for_mode (mode, 1);
7403 DECL_MODE (ddecl) = mode;
7404 dval = gen_rtx_DEBUG_EXPR (mode);
7405 DEBUG_EXPR_TREE_DECL (dval) = ddecl;
7406 SET_DECL_RTL (ddecl, dval);
7407 return dval;
7410 #ifdef ELF_ASCII_ESCAPES
7411 /* Default ASM_OUTPUT_LIMITED_STRING for ELF targets. */
7413 void
7414 default_elf_asm_output_limited_string (FILE *f, const char *s)
7416 int escape;
7417 unsigned char c;
7419 fputs (STRING_ASM_OP, f);
7420 putc ('"', f);
7421 while (*s != '\0')
7423 c = *s;
7424 escape = ELF_ASCII_ESCAPES[c];
7425 switch (escape)
7427 case 0:
7428 putc (c, f);
7429 break;
7430 case 1:
7431 /* TODO: Print in hex with fast function, important for -flto. */
7432 fprintf (f, "\\%03o", c);
7433 break;
7434 default:
7435 putc ('\\', f);
7436 putc (escape, f);
7437 break;
7439 s++;
7441 putc ('\"', f);
7442 putc ('\n', f);
7445 /* Default ASM_OUTPUT_ASCII for ELF targets. */
7447 void
7448 default_elf_asm_output_ascii (FILE *f, const char *s, unsigned int len)
7450 const char *limit = s + len;
7451 const char *last_null = NULL;
7452 unsigned bytes_in_chunk = 0;
7453 unsigned char c;
7454 int escape;
7456 for (; s < limit; s++)
7458 const char *p;
7460 if (bytes_in_chunk >= 60)
7462 putc ('\"', f);
7463 putc ('\n', f);
7464 bytes_in_chunk = 0;
7467 if (s > last_null)
7469 for (p = s; p < limit && *p != '\0'; p++)
7470 continue;
7471 last_null = p;
7473 else
7474 p = last_null;
7476 if (p < limit && (p - s) <= (long) ELF_STRING_LIMIT)
7478 if (bytes_in_chunk > 0)
7480 putc ('\"', f);
7481 putc ('\n', f);
7482 bytes_in_chunk = 0;
7485 default_elf_asm_output_limited_string (f, s);
7486 s = p;
7488 else
7490 if (bytes_in_chunk == 0)
7491 fputs (ASCII_DATA_ASM_OP "\"", f);
7493 c = *s;
7494 escape = ELF_ASCII_ESCAPES[c];
7495 switch (escape)
7497 case 0:
7498 putc (c, f);
7499 bytes_in_chunk++;
7500 break;
7501 case 1:
7502 /* TODO: Print in hex with fast function, important for -flto. */
7503 fprintf (f, "\\%03o", c);
7504 bytes_in_chunk += 4;
7505 break;
7506 default:
7507 putc ('\\', f);
7508 putc (escape, f);
7509 bytes_in_chunk += 2;
7510 break;
7516 if (bytes_in_chunk > 0)
7518 putc ('\"', f);
7519 putc ('\n', f);
7522 #endif
7524 static GTY(()) section *elf_init_array_section;
7525 static GTY(()) section *elf_fini_array_section;
7527 static section *
7528 get_elf_initfini_array_priority_section (int priority,
7529 bool constructor_p)
7531 section *sec;
7532 if (priority != DEFAULT_INIT_PRIORITY)
7534 char buf[18];
7535 sprintf (buf, "%s.%.5u",
7536 constructor_p ? ".init_array" : ".fini_array",
7537 priority);
7538 sec = get_section (buf, SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
7540 else
7542 if (constructor_p)
7544 if (elf_init_array_section == NULL)
7545 elf_init_array_section
7546 = get_section (".init_array",
7547 SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
7548 sec = elf_init_array_section;
7550 else
7552 if (elf_fini_array_section == NULL)
7553 elf_fini_array_section
7554 = get_section (".fini_array",
7555 SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
7556 sec = elf_fini_array_section;
7559 return sec;
7562 /* Use .init_array section for constructors. */
7564 void
7565 default_elf_init_array_asm_out_constructor (rtx symbol, int priority)
7567 section *sec = get_elf_initfini_array_priority_section (priority,
7568 true);
7569 assemble_addr_to_section (symbol, sec);
7572 /* Use .fini_array section for destructors. */
7574 void
7575 default_elf_fini_array_asm_out_destructor (rtx symbol, int priority)
7577 section *sec = get_elf_initfini_array_priority_section (priority,
7578 false);
7579 assemble_addr_to_section (symbol, sec);
7582 /* Default TARGET_ASM_OUTPUT_IDENT hook.
7584 This is a bit of a cheat. The real default is a no-op, but this
7585 hook is the default for all targets with a .ident directive. */
7587 void
7588 default_asm_output_ident_directive (const char *ident_str)
7590 const char *ident_asm_op = "\t.ident\t";
7592 /* If we are still in the front end, do not write out the string
7593 to asm_out_file. Instead, add a fake top-level asm statement.
7594 This allows the front ends to use this hook without actually
7595 writing to asm_out_file, to handle #ident or Pragma Ident. */
7596 if (symtab->state == PARSING)
7598 char *buf = ACONCAT ((ident_asm_op, "\"", ident_str, "\"\n", NULL));
7599 symtab->finalize_toplevel_asm (build_string (strlen (buf), buf));
7601 else
7602 fprintf (asm_out_file, "%s\"%s\"\n", ident_asm_op, ident_str);
7605 #include "gt-varasm.h"