PR preprocessor/60723 - missing system-ness marks for macro tokens
[official-gcc.git] / gcc / varasm.c
blobaea5a255060202c6d4799df273be83e553dc95fd
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 "gcc-symtab.h"
37 #include "varasm.h"
38 #include "flags.h"
39 #include "function.h"
40 #include "expr.h"
41 #include "hard-reg-set.h"
42 #include "regs.h"
43 #include "output.h"
44 #include "diagnostic-core.h"
45 #include "hashtab.h"
46 #include "ggc.h"
47 #include "langhooks.h"
48 #include "tm_p.h"
49 #include "debug.h"
50 #include "target.h"
51 #include "common/common-target.h"
52 #include "targhooks.h"
53 #include "cgraph.h"
54 #include "pointer-set.h"
55 #include "asan.h"
56 #include "basic-block.h"
58 #ifdef XCOFF_DEBUGGING_INFO
59 #include "xcoffout.h" /* Needed for external data
60 declarations for e.g. AIX 4.x. */
61 #endif
63 /* The (assembler) name of the first globally-visible object output. */
64 extern GTY(()) const char *first_global_object_name;
65 extern GTY(()) const char *weak_global_object_name;
67 const char *first_global_object_name;
68 const char *weak_global_object_name;
70 struct addr_const;
71 struct constant_descriptor_rtx;
72 struct rtx_constant_pool;
74 #define n_deferred_constants (crtl->varasm.deferred_constants)
76 /* Number for making the label on the next
77 constant that is stored in memory. */
79 static GTY(()) int const_labelno;
81 /* Carry information from ASM_DECLARE_OBJECT_NAME
82 to ASM_FINISH_DECLARE_OBJECT. */
84 int size_directive_output;
86 /* The last decl for which assemble_variable was called,
87 if it did ASM_DECLARE_OBJECT_NAME.
88 If the last call to assemble_variable didn't do that,
89 this holds 0. */
91 tree last_assemble_variable_decl;
93 /* The following global variable indicates if the first basic block
94 in a function belongs to the cold partition or not. */
96 bool first_function_block_is_cold;
98 /* We give all constants their own alias set. Perhaps redundant with
99 MEM_READONLY_P, but pre-dates it. */
101 static alias_set_type const_alias_set;
103 /* Whether we saw any functions with no_split_stack. */
105 static bool saw_no_split_stack;
107 static const char *strip_reg_name (const char *);
108 static int contains_pointers_p (tree);
109 #ifdef ASM_OUTPUT_EXTERNAL
110 static bool incorporeal_function_p (tree);
111 #endif
112 static void decode_addr_const (tree, struct addr_const *);
113 static hashval_t const_desc_hash (const void *);
114 static int const_desc_eq (const void *, const void *);
115 static hashval_t const_hash_1 (const tree);
116 static int compare_constant (const tree, const tree);
117 static void output_constant_def_contents (rtx);
118 static void output_addressed_constants (tree);
119 static unsigned HOST_WIDE_INT output_constant (tree, unsigned HOST_WIDE_INT,
120 unsigned int);
121 static void globalize_decl (tree);
122 static bool decl_readonly_section_1 (enum section_category);
123 #ifdef BSS_SECTION_ASM_OP
124 #ifdef ASM_OUTPUT_ALIGNED_BSS
125 static void asm_output_aligned_bss (FILE *, tree, const char *,
126 unsigned HOST_WIDE_INT, int)
127 ATTRIBUTE_UNUSED;
128 #endif
129 #endif /* BSS_SECTION_ASM_OP */
130 static void mark_weak (tree);
131 static void output_constant_pool (const char *, tree);
133 /* Well-known sections, each one associated with some sort of *_ASM_OP. */
134 section *text_section;
135 section *data_section;
136 section *readonly_data_section;
137 section *sdata_section;
138 section *ctors_section;
139 section *dtors_section;
140 section *bss_section;
141 section *sbss_section;
143 /* Various forms of common section. All are guaranteed to be nonnull. */
144 section *tls_comm_section;
145 section *comm_section;
146 section *lcomm_section;
148 /* A SECTION_NOSWITCH section used for declaring global BSS variables.
149 May be null. */
150 section *bss_noswitch_section;
152 /* The section that holds the main exception table, when known. The section
153 is set either by the target's init_sections hook or by the first call to
154 switch_to_exception_section. */
155 section *exception_section;
157 /* The section that holds the DWARF2 frame unwind information, when known.
158 The section is set either by the target's init_sections hook or by the
159 first call to switch_to_eh_frame_section. */
160 section *eh_frame_section;
162 /* asm_out_file's current section. This is NULL if no section has yet
163 been selected or if we lose track of what the current section is. */
164 section *in_section;
166 /* True if code for the current function is currently being directed
167 at the cold section. */
168 bool in_cold_section_p;
170 /* A linked list of all the unnamed sections. */
171 static GTY(()) section *unnamed_sections;
173 /* Return a nonzero value if DECL has a section attribute. */
174 #define IN_NAMED_SECTION(DECL) \
175 ((TREE_CODE (DECL) == FUNCTION_DECL || TREE_CODE (DECL) == VAR_DECL) \
176 && DECL_SECTION_NAME (DECL) != NULL)
178 /* Hash table of named sections. */
179 static GTY((param_is (section))) htab_t section_htab;
181 /* A table of object_blocks, indexed by section. */
182 static GTY((param_is (struct object_block))) htab_t object_block_htab;
184 /* The next number to use for internal anchor labels. */
185 static GTY(()) int anchor_labelno;
187 /* A pool of constants that can be shared between functions. */
188 static GTY(()) struct rtx_constant_pool *shared_constant_pool;
190 /* Helper routines for maintaining section_htab. */
192 static int
193 section_entry_eq (const void *p1, const void *p2)
195 const section *old = (const section *) p1;
196 const char *new_name = (const char *) p2;
198 return strcmp (old->named.name, new_name) == 0;
201 static hashval_t
202 section_entry_hash (const void *p)
204 const section *old = (const section *) p;
205 return htab_hash_string (old->named.name);
208 /* Return a hash value for section SECT. */
210 static hashval_t
211 hash_section (section *sect)
213 if (sect->common.flags & SECTION_NAMED)
214 return htab_hash_string (sect->named.name);
215 return sect->common.flags;
218 /* Helper routines for maintaining object_block_htab. */
220 static int
221 object_block_entry_eq (const void *p1, const void *p2)
223 const struct object_block *old = (const struct object_block *) p1;
224 const section *new_section = (const section *) p2;
226 return old->sect == new_section;
229 static hashval_t
230 object_block_entry_hash (const void *p)
232 const struct object_block *old = (const struct object_block *) p;
233 return hash_section (old->sect);
236 /* Return a new unnamed section with the given fields. */
238 section *
239 get_unnamed_section (unsigned int flags, void (*callback) (const void *),
240 const void *data)
242 section *sect;
244 sect = ggc_alloc<section> ();
245 sect->unnamed.common.flags = flags | SECTION_UNNAMED;
246 sect->unnamed.callback = callback;
247 sect->unnamed.data = data;
248 sect->unnamed.next = unnamed_sections;
250 unnamed_sections = sect;
251 return sect;
254 /* Return a SECTION_NOSWITCH section with the given fields. */
256 static section *
257 get_noswitch_section (unsigned int flags, noswitch_section_callback callback)
259 section *sect;
261 sect = ggc_alloc<section> ();
262 sect->noswitch.common.flags = flags | SECTION_NOSWITCH;
263 sect->noswitch.callback = callback;
265 return sect;
268 /* Return the named section structure associated with NAME. Create
269 a new section with the given fields if no such structure exists. */
271 section *
272 get_section (const char *name, unsigned int flags, tree decl)
274 section *sect, **slot;
276 slot = (section **)
277 htab_find_slot_with_hash (section_htab, name,
278 htab_hash_string (name), INSERT);
279 flags |= SECTION_NAMED;
280 if (*slot == NULL)
282 sect = ggc_alloc<section> ();
283 sect->named.common.flags = flags;
284 sect->named.name = ggc_strdup (name);
285 sect->named.decl = decl;
286 *slot = sect;
288 else
290 sect = *slot;
291 if ((sect->common.flags & ~SECTION_DECLARED) != flags
292 && ((sect->common.flags | flags) & SECTION_OVERRIDE) == 0)
294 /* It is fine if one of the section flags is
295 SECTION_WRITE | SECTION_RELRO and the other has none of these
296 flags (i.e. read-only) in named sections and either the
297 section hasn't been declared yet or has been declared as writable.
298 In that case just make sure the resulting flags are
299 SECTION_WRITE | SECTION_RELRO, ie. writable only because of
300 relocations. */
301 if (((sect->common.flags ^ flags) & (SECTION_WRITE | SECTION_RELRO))
302 == (SECTION_WRITE | SECTION_RELRO)
303 && (sect->common.flags
304 & ~(SECTION_DECLARED | SECTION_WRITE | SECTION_RELRO))
305 == (flags & ~(SECTION_WRITE | SECTION_RELRO))
306 && ((sect->common.flags & SECTION_DECLARED) == 0
307 || (sect->common.flags & SECTION_WRITE)))
309 sect->common.flags |= (SECTION_WRITE | SECTION_RELRO);
310 return sect;
312 /* Sanity check user variables for flag changes. */
313 if (sect->named.decl != NULL
314 && DECL_P (sect->named.decl)
315 && decl != sect->named.decl)
317 if (decl != NULL && DECL_P (decl))
318 error ("%+D causes a section type conflict with %D",
319 decl, sect->named.decl);
320 else
321 error ("section type conflict with %D", sect->named.decl);
322 inform (DECL_SOURCE_LOCATION (sect->named.decl),
323 "%qD was declared here", sect->named.decl);
325 else if (decl != NULL && DECL_P (decl))
326 error ("%+D causes a section type conflict", decl);
327 else
328 error ("section type conflict");
329 /* Make sure we don't error about one section multiple times. */
330 sect->common.flags |= SECTION_OVERRIDE;
333 return sect;
336 /* Return true if the current compilation mode benefits from having
337 objects grouped into blocks. */
339 static bool
340 use_object_blocks_p (void)
342 return flag_section_anchors;
345 /* Return the object_block structure for section SECT. Create a new
346 structure if we haven't created one already. Return null if SECT
347 itself is null. */
349 static struct object_block *
350 get_block_for_section (section *sect)
352 struct object_block *block;
353 void **slot;
355 if (sect == NULL)
356 return NULL;
358 slot = htab_find_slot_with_hash (object_block_htab, sect,
359 hash_section (sect), INSERT);
360 block = (struct object_block *) *slot;
361 if (block == NULL)
363 block = ggc_cleared_alloc<object_block> ();
364 block->sect = sect;
365 *slot = block;
367 return block;
370 /* Create a symbol with label LABEL and place it at byte offset
371 OFFSET in BLOCK. OFFSET can be negative if the symbol's offset
372 is not yet known. LABEL must be a garbage-collected string. */
374 static rtx
375 create_block_symbol (const char *label, struct object_block *block,
376 HOST_WIDE_INT offset)
378 rtx symbol;
379 unsigned int size;
381 /* Create the extended SYMBOL_REF. */
382 size = RTX_HDR_SIZE + sizeof (struct block_symbol);
383 symbol = (rtx) ggc_internal_alloc (size);
385 /* Initialize the normal SYMBOL_REF fields. */
386 memset (symbol, 0, size);
387 PUT_CODE (symbol, SYMBOL_REF);
388 PUT_MODE (symbol, Pmode);
389 XSTR (symbol, 0) = label;
390 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_HAS_BLOCK_INFO;
392 /* Initialize the block_symbol stuff. */
393 SYMBOL_REF_BLOCK (symbol) = block;
394 SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
396 return symbol;
399 /* Return a section with a particular name and with whatever SECTION_*
400 flags section_type_flags deems appropriate. The name of the section
401 is taken from NAME if nonnull, otherwise it is taken from DECL's
402 DECL_SECTION_NAME. DECL is the decl associated with the section
403 (see the section comment for details) and RELOC is as for
404 section_type_flags. */
406 section *
407 get_named_section (tree decl, const char *name, int reloc)
409 unsigned int flags;
411 if (name == NULL)
413 gcc_assert (decl && DECL_P (decl) && DECL_SECTION_NAME (decl));
414 name = DECL_SECTION_NAME (decl);
417 flags = targetm.section_type_flags (decl, name, reloc);
418 return get_section (name, flags, decl);
421 /* Worker for resolve_unique_section. */
423 static bool
424 set_implicit_section (struct symtab_node *n, void *data ATTRIBUTE_UNUSED)
426 n->implicit_section = true;
427 return false;
430 /* If required, set DECL_SECTION_NAME to a unique name. */
432 void
433 resolve_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED,
434 int flag_function_or_data_sections)
436 if (DECL_SECTION_NAME (decl) == NULL
437 && targetm_common.have_named_sections
438 && (flag_function_or_data_sections
439 || DECL_COMDAT_GROUP (decl)))
441 targetm.asm_out.unique_section (decl, reloc);
442 if (DECL_SECTION_NAME (decl))
443 symtab_for_node_and_aliases (symtab_get_node (decl),
444 set_implicit_section, NULL, true);
448 #ifdef BSS_SECTION_ASM_OP
450 #ifdef ASM_OUTPUT_ALIGNED_BSS
452 /* Utility function for targets to use in implementing
453 ASM_OUTPUT_ALIGNED_BSS.
454 ??? It is believed that this function will work in most cases so such
455 support is localized here. */
457 static void
458 asm_output_aligned_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
459 const char *name, unsigned HOST_WIDE_INT size,
460 int align)
462 switch_to_section (bss_section);
463 ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
464 #ifdef ASM_DECLARE_OBJECT_NAME
465 last_assemble_variable_decl = decl;
466 ASM_DECLARE_OBJECT_NAME (file, name, decl);
467 #else
468 /* Standard thing is just output label for the object. */
469 ASM_OUTPUT_LABEL (file, name);
470 #endif /* ASM_DECLARE_OBJECT_NAME */
471 ASM_OUTPUT_SKIP (file, size ? size : 1);
474 #endif
476 #endif /* BSS_SECTION_ASM_OP */
478 #ifndef USE_SELECT_SECTION_FOR_FUNCTIONS
479 /* Return the hot section for function DECL. Return text_section for
480 null DECLs. */
482 static section *
483 hot_function_section (tree decl)
485 if (decl != NULL_TREE
486 && DECL_SECTION_NAME (decl) != NULL
487 && targetm_common.have_named_sections)
488 return get_named_section (decl, NULL, 0);
489 else
490 return text_section;
492 #endif
494 /* Return section for TEXT_SECTION_NAME if DECL or DECL_SECTION_NAME (DECL)
495 is NULL.
497 When DECL_SECTION_NAME is non-NULL and it is implicit section and
498 NAMED_SECTION_SUFFIX is non-NULL, then produce section called
499 concatenate the name with NAMED_SECTION_SUFFIX.
500 Otherwise produce "TEXT_SECTION_NAME.IMPLICIT_NAME". */
502 section *
503 get_named_text_section (tree decl,
504 const char *text_section_name,
505 const char *named_section_suffix)
507 if (decl && DECL_SECTION_NAME (decl))
509 if (named_section_suffix)
511 const char *dsn = DECL_SECTION_NAME (decl);
512 const char *stripped_name;
513 char *name, *buffer;
515 name = (char *) alloca (strlen (dsn) + 1);
516 memcpy (name, dsn,
517 strlen (dsn) + 1);
519 stripped_name = targetm.strip_name_encoding (name);
521 buffer = ACONCAT ((stripped_name, named_section_suffix, NULL));
522 return get_named_section (decl, buffer, 0);
524 else if (symtab_get_node (decl)->implicit_section)
526 const char *name;
528 /* Do not try to split gnu_linkonce functions. This gets somewhat
529 slipperly. */
530 if (DECL_COMDAT_GROUP (decl) && !HAVE_COMDAT_GROUP)
531 return NULL;
532 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
533 name = targetm.strip_name_encoding (name);
534 return get_named_section (decl, ACONCAT ((text_section_name, ".",
535 name, NULL)), 0);
537 else
538 return NULL;
540 return get_named_section (decl, text_section_name, 0);
543 /* Choose named function section based on its frequency. */
545 section *
546 default_function_section (tree decl, enum node_frequency freq,
547 bool startup, bool exit)
549 #if defined HAVE_LD_EH_GC_SECTIONS && defined HAVE_LD_EH_GC_SECTIONS_BUG
550 /* Old GNU linkers have buggy --gc-section support, which sometimes
551 results in .gcc_except_table* sections being garbage collected. */
552 if (decl
553 && symtab_get_node (decl)->implicit_section)
554 return NULL;
555 #endif
557 if (!flag_reorder_functions
558 || !targetm_common.have_named_sections)
559 return NULL;
560 /* Startup code should go to startup subsection unless it is
561 unlikely executed (this happens especially with function splitting
562 where we can split away unnecessary parts of static constructors. */
563 if (startup && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
565 /* If we do have a profile or(and) LTO phase is executed, we do not need
566 these ELF section. */
567 if (!in_lto_p || !flag_profile_values)
568 return get_named_text_section (decl, ".text.startup", NULL);
569 else
570 return NULL;
573 /* Similarly for exit. */
574 if (exit && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
575 return get_named_text_section (decl, ".text.exit", NULL);
577 /* Group cold functions together, similarly for hot code. */
578 switch (freq)
580 case NODE_FREQUENCY_UNLIKELY_EXECUTED:
581 return get_named_text_section (decl, ".text.unlikely", NULL);
582 case NODE_FREQUENCY_HOT:
583 /* If we do have a profile or(and) LTO phase is executed, we do not need
584 these ELF section. */
585 if (!in_lto_p || !flag_profile_values)
586 return get_named_text_section (decl, ".text.hot", NULL);
587 default:
588 return NULL;
592 /* Return the section for function DECL.
594 If DECL is NULL_TREE, return the text section. We can be passed
595 NULL_TREE under some circumstances by dbxout.c at least.
597 If FORCE_COLD is true, return cold function section ignoring
598 the frequency info of cgraph_node. */
600 static section *
601 function_section_1 (tree decl, bool force_cold)
603 section *section = NULL;
604 enum node_frequency freq = NODE_FREQUENCY_NORMAL;
605 bool startup = false, exit = false;
607 if (decl)
609 struct cgraph_node *node = cgraph_get_node (decl);
611 if (node)
613 freq = node->frequency;
614 startup = node->only_called_at_startup;
615 exit = node->only_called_at_exit;
618 if (force_cold)
619 freq = NODE_FREQUENCY_UNLIKELY_EXECUTED;
621 #ifdef USE_SELECT_SECTION_FOR_FUNCTIONS
622 if (decl != NULL_TREE
623 && DECL_SECTION_NAME (decl) != NULL)
625 if (targetm.asm_out.function_section)
626 section = targetm.asm_out.function_section (decl, freq,
627 startup, exit);
628 if (section)
629 return section;
630 return get_named_section (decl, NULL, 0);
632 else
633 return targetm.asm_out.select_section
634 (decl, freq == NODE_FREQUENCY_UNLIKELY_EXECUTED,
635 DECL_ALIGN (decl));
636 #else
637 if (targetm.asm_out.function_section)
638 section = targetm.asm_out.function_section (decl, freq, startup, exit);
639 if (section)
640 return section;
641 return hot_function_section (decl);
642 #endif
645 /* Return the section for function DECL.
647 If DECL is NULL_TREE, return the text section. We can be passed
648 NULL_TREE under some circumstances by dbxout.c at least. */
650 section *
651 function_section (tree decl)
653 /* Handle cases where function splitting code decides
654 to put function entry point into unlikely executed section
655 despite the fact that the function itself is not cold
656 (i.e. it is called rarely but contains a hot loop that is
657 better to live in hot subsection for the code locality). */
658 return function_section_1 (decl,
659 first_function_block_is_cold);
662 /* Return the section for the current function, take IN_COLD_SECTION_P
663 into account. */
665 section *
666 current_function_section (void)
668 return function_section_1 (current_function_decl, in_cold_section_p);
671 /* Tell assembler to switch to unlikely-to-be-executed text section. */
673 section *
674 unlikely_text_section (void)
676 return function_section_1 (current_function_decl, true);
679 /* When called within a function context, return true if the function
680 has been assigned a cold text section and if SECT is that section.
681 When called outside a function context, return true if SECT is the
682 default cold section. */
684 bool
685 unlikely_text_section_p (section *sect)
687 return sect == function_section_1 (current_function_decl, true);
690 /* Return the read-only data section associated with function DECL. */
692 section *
693 default_function_rodata_section (tree decl)
695 if (decl != NULL_TREE && DECL_SECTION_NAME (decl))
697 const char *name = DECL_SECTION_NAME (decl);
699 if (DECL_COMDAT_GROUP (decl) && HAVE_COMDAT_GROUP)
701 const char *dot;
702 size_t len;
703 char* rname;
705 dot = strchr (name + 1, '.');
706 if (!dot)
707 dot = name;
708 len = strlen (dot) + 8;
709 rname = (char *) alloca (len);
711 strcpy (rname, ".rodata");
712 strcat (rname, dot);
713 return get_section (rname, SECTION_LINKONCE, decl);
715 /* For .gnu.linkonce.t.foo we want to use .gnu.linkonce.r.foo. */
716 else if (DECL_COMDAT_GROUP (decl)
717 && strncmp (name, ".gnu.linkonce.t.", 16) == 0)
719 size_t len = strlen (name) + 1;
720 char *rname = (char *) alloca (len);
722 memcpy (rname, name, len);
723 rname[14] = 'r';
724 return get_section (rname, SECTION_LINKONCE, decl);
726 /* For .text.foo we want to use .rodata.foo. */
727 else if (flag_function_sections && flag_data_sections
728 && strncmp (name, ".text.", 6) == 0)
730 size_t len = strlen (name) + 1;
731 char *rname = (char *) alloca (len + 2);
733 memcpy (rname, ".rodata", 7);
734 memcpy (rname + 7, name + 5, len - 5);
735 return get_section (rname, 0, decl);
739 return readonly_data_section;
742 /* Return the read-only data section associated with function DECL
743 for targets where that section should be always the single
744 readonly data section. */
746 section *
747 default_no_function_rodata_section (tree decl ATTRIBUTE_UNUSED)
749 return readonly_data_section;
752 /* Return the section to use for string merging. */
754 static section *
755 mergeable_string_section (tree decl ATTRIBUTE_UNUSED,
756 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
757 unsigned int flags ATTRIBUTE_UNUSED)
759 HOST_WIDE_INT len;
761 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
762 && TREE_CODE (decl) == STRING_CST
763 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
764 && align <= 256
765 && (len = int_size_in_bytes (TREE_TYPE (decl))) > 0
766 && TREE_STRING_LENGTH (decl) >= len)
768 enum machine_mode mode;
769 unsigned int modesize;
770 const char *str;
771 HOST_WIDE_INT i;
772 int j, unit;
773 const char *prefix = targetm.asm_out.mergeable_rodata_prefix;
774 char *name = (char *) alloca (strlen (prefix) + 30);
776 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (decl)));
777 modesize = GET_MODE_BITSIZE (mode);
778 if (modesize >= 8 && modesize <= 256
779 && (modesize & (modesize - 1)) == 0)
781 if (align < modesize)
782 align = modesize;
784 str = TREE_STRING_POINTER (decl);
785 unit = GET_MODE_SIZE (mode);
787 /* Check for embedded NUL characters. */
788 for (i = 0; i < len; i += unit)
790 for (j = 0; j < unit; j++)
791 if (str[i + j] != '\0')
792 break;
793 if (j == unit)
794 break;
796 if (i == len - unit)
798 sprintf (name, "%s.str%d.%d", prefix,
799 modesize / 8, (int) (align / 8));
800 flags |= (modesize / 8) | SECTION_MERGE | SECTION_STRINGS;
801 return get_section (name, flags, NULL);
806 return readonly_data_section;
809 /* Return the section to use for constant merging. */
811 section *
812 mergeable_constant_section (enum machine_mode mode ATTRIBUTE_UNUSED,
813 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
814 unsigned int flags ATTRIBUTE_UNUSED)
816 unsigned int modesize = GET_MODE_BITSIZE (mode);
818 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
819 && mode != VOIDmode
820 && mode != BLKmode
821 && modesize <= align
822 && align >= 8
823 && align <= 256
824 && (align & (align - 1)) == 0)
826 const char *prefix = targetm.asm_out.mergeable_rodata_prefix;
827 char *name = (char *) alloca (strlen (prefix) + 30);
829 sprintf (name, "%s.cst%d", prefix, (int) (align / 8));
830 flags |= (align / 8) | SECTION_MERGE;
831 return get_section (name, flags, NULL);
833 return readonly_data_section;
836 /* Given NAME, a putative register name, discard any customary prefixes. */
838 static const char *
839 strip_reg_name (const char *name)
841 #ifdef REGISTER_PREFIX
842 if (!strncmp (name, REGISTER_PREFIX, strlen (REGISTER_PREFIX)))
843 name += strlen (REGISTER_PREFIX);
844 #endif
845 if (name[0] == '%' || name[0] == '#')
846 name++;
847 return name;
850 /* The user has asked for a DECL to have a particular name. Set (or
851 change) it in such a way that we don't prefix an underscore to
852 it. */
853 void
854 set_user_assembler_name (tree decl, const char *name)
856 char *starred = (char *) alloca (strlen (name) + 2);
857 starred[0] = '*';
858 strcpy (starred + 1, name);
859 change_decl_assembler_name (decl, get_identifier (starred));
860 SET_DECL_RTL (decl, NULL_RTX);
863 /* Decode an `asm' spec for a declaration as a register name.
864 Return the register number, or -1 if nothing specified,
865 or -2 if the ASMSPEC is not `cc' or `memory' and is not recognized,
866 or -3 if ASMSPEC is `cc' and is not recognized,
867 or -4 if ASMSPEC is `memory' and is not recognized.
868 Accept an exact spelling or a decimal number.
869 Prefixes such as % are optional. */
872 decode_reg_name_and_count (const char *asmspec, int *pnregs)
874 /* Presume just one register is clobbered. */
875 *pnregs = 1;
877 if (asmspec != 0)
879 int i;
881 /* Get rid of confusing prefixes. */
882 asmspec = strip_reg_name (asmspec);
884 /* Allow a decimal number as a "register name". */
885 for (i = strlen (asmspec) - 1; i >= 0; i--)
886 if (! ISDIGIT (asmspec[i]))
887 break;
888 if (asmspec[0] != 0 && i < 0)
890 i = atoi (asmspec);
891 if (i < FIRST_PSEUDO_REGISTER && i >= 0)
892 return i;
893 else
894 return -2;
897 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
898 if (reg_names[i][0]
899 && ! strcmp (asmspec, strip_reg_name (reg_names[i])))
900 return i;
902 #ifdef OVERLAPPING_REGISTER_NAMES
904 static const struct
906 const char *const name;
907 const int number;
908 const int nregs;
909 } table[] = OVERLAPPING_REGISTER_NAMES;
911 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
912 if (table[i].name[0]
913 && ! strcmp (asmspec, table[i].name))
915 *pnregs = table[i].nregs;
916 return table[i].number;
919 #endif /* OVERLAPPING_REGISTER_NAMES */
921 #ifdef ADDITIONAL_REGISTER_NAMES
923 static const struct { const char *const name; const int number; } table[]
924 = ADDITIONAL_REGISTER_NAMES;
926 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
927 if (table[i].name[0]
928 && ! strcmp (asmspec, table[i].name))
929 return table[i].number;
931 #endif /* ADDITIONAL_REGISTER_NAMES */
933 if (!strcmp (asmspec, "memory"))
934 return -4;
936 if (!strcmp (asmspec, "cc"))
937 return -3;
939 return -2;
942 return -1;
946 decode_reg_name (const char *name)
948 int count;
949 return decode_reg_name_and_count (name, &count);
953 /* Return true if DECL's initializer is suitable for a BSS section. */
955 bool
956 bss_initializer_p (const_tree decl)
958 return (DECL_INITIAL (decl) == NULL
959 || DECL_INITIAL (decl) == error_mark_node
960 || (flag_zero_initialized_in_bss
961 /* Leave constant zeroes in .rodata so they
962 can be shared. */
963 && !TREE_READONLY (decl)
964 && initializer_zerop (DECL_INITIAL (decl))));
967 /* Compute the alignment of variable specified by DECL.
968 DONT_OUTPUT_DATA is from assemble_variable. */
970 void
971 align_variable (tree decl, bool dont_output_data)
973 unsigned int align = DECL_ALIGN (decl);
975 /* In the case for initialing an array whose length isn't specified,
976 where we have not yet been able to do the layout,
977 figure out the proper alignment now. */
978 if (dont_output_data && DECL_SIZE (decl) == 0
979 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
980 align = MAX (align, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (decl))));
982 /* Some object file formats have a maximum alignment which they support.
983 In particular, a.out format supports a maximum alignment of 4. */
984 if (align > MAX_OFILE_ALIGNMENT)
986 error ("alignment of %q+D is greater than maximum object "
987 "file alignment %d", decl,
988 MAX_OFILE_ALIGNMENT/BITS_PER_UNIT);
989 align = MAX_OFILE_ALIGNMENT;
992 if (! DECL_USER_ALIGN (decl))
994 #ifdef DATA_ABI_ALIGNMENT
995 unsigned int data_abi_align
996 = DATA_ABI_ALIGNMENT (TREE_TYPE (decl), align);
997 /* For backwards compatibility, don't assume the ABI alignment for
998 TLS variables. */
999 if (! DECL_THREAD_LOCAL_P (decl) || data_abi_align <= BITS_PER_WORD)
1000 align = data_abi_align;
1001 #endif
1003 /* On some machines, it is good to increase alignment sometimes.
1004 But as DECL_ALIGN is used both for actually emitting the variable
1005 and for code accessing the variable as guaranteed alignment, we
1006 can only increase the alignment if it is a performance optimization
1007 if the references to it must bind to the current definition. */
1008 if (decl_binds_to_current_def_p (decl)
1009 && !DECL_VIRTUAL_P (decl))
1011 #ifdef DATA_ALIGNMENT
1012 unsigned int data_align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
1013 /* Don't increase alignment too much for TLS variables - TLS space
1014 is too precious. */
1015 if (! DECL_THREAD_LOCAL_P (decl) || data_align <= BITS_PER_WORD)
1016 align = data_align;
1017 #endif
1018 #ifdef CONSTANT_ALIGNMENT
1019 if (DECL_INITIAL (decl) != 0
1020 && DECL_INITIAL (decl) != error_mark_node)
1022 unsigned int const_align
1023 = CONSTANT_ALIGNMENT (DECL_INITIAL (decl), align);
1024 /* Don't increase alignment too much for TLS variables - TLS
1025 space is too precious. */
1026 if (! DECL_THREAD_LOCAL_P (decl) || const_align <= BITS_PER_WORD)
1027 align = const_align;
1029 #endif
1033 /* Reset the alignment in case we have made it tighter, so we can benefit
1034 from it in get_pointer_alignment. */
1035 DECL_ALIGN (decl) = align;
1038 /* Return DECL_ALIGN (decl), possibly increased for optimization purposes
1039 beyond what align_variable returned. */
1041 static unsigned int
1042 get_variable_align (tree decl)
1044 unsigned int align = DECL_ALIGN (decl);
1046 /* For user aligned vars or static vars align_variable already did
1047 everything. */
1048 if (DECL_USER_ALIGN (decl) || !TREE_PUBLIC (decl))
1049 return align;
1051 #ifdef DATA_ABI_ALIGNMENT
1052 if (DECL_THREAD_LOCAL_P (decl))
1053 align = DATA_ABI_ALIGNMENT (TREE_TYPE (decl), align);
1054 #endif
1056 /* For decls that bind to the current definition, align_variable
1057 did also everything, except for not assuming ABI required alignment
1058 of TLS variables. For other vars, increase the alignment here
1059 as an optimization. */
1060 if (!decl_binds_to_current_def_p (decl))
1062 /* On some machines, it is good to increase alignment sometimes. */
1063 #ifdef DATA_ALIGNMENT
1064 unsigned int data_align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
1065 /* Don't increase alignment too much for TLS variables - TLS space
1066 is too precious. */
1067 if (! DECL_THREAD_LOCAL_P (decl) || data_align <= BITS_PER_WORD)
1068 align = data_align;
1069 #endif
1070 #ifdef CONSTANT_ALIGNMENT
1071 if (DECL_INITIAL (decl) != 0 && DECL_INITIAL (decl) != error_mark_node)
1073 unsigned int const_align = CONSTANT_ALIGNMENT (DECL_INITIAL (decl),
1074 align);
1075 /* Don't increase alignment too much for TLS variables - TLS space
1076 is too precious. */
1077 if (! DECL_THREAD_LOCAL_P (decl) || const_align <= BITS_PER_WORD)
1078 align = const_align;
1080 #endif
1083 return align;
1086 /* Return the section into which the given VAR_DECL or CONST_DECL
1087 should be placed. PREFER_NOSWITCH_P is true if a noswitch
1088 section should be used wherever possible. */
1090 section *
1091 get_variable_section (tree decl, bool prefer_noswitch_p)
1093 addr_space_t as = ADDR_SPACE_GENERIC;
1094 int reloc;
1095 symtab_node *snode = symtab_get_node (decl);
1096 if (snode)
1097 decl = symtab_alias_ultimate_target (snode)->decl;
1099 if (TREE_TYPE (decl) != error_mark_node)
1100 as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
1102 if (DECL_COMMON (decl))
1104 /* If the decl has been given an explicit section name, or it resides
1105 in a non-generic address space, then it isn't common, and shouldn't
1106 be handled as such. */
1107 gcc_assert (DECL_SECTION_NAME (decl) == NULL
1108 && ADDR_SPACE_GENERIC_P (as));
1109 if (DECL_THREAD_LOCAL_P (decl))
1110 return tls_comm_section;
1111 else if (TREE_PUBLIC (decl) && bss_initializer_p (decl))
1112 return comm_section;
1115 if (DECL_INITIAL (decl) == error_mark_node)
1116 reloc = contains_pointers_p (TREE_TYPE (decl)) ? 3 : 0;
1117 else if (DECL_INITIAL (decl))
1118 reloc = compute_reloc_for_constant (DECL_INITIAL (decl));
1119 else
1120 reloc = 0;
1122 resolve_unique_section (decl, reloc, flag_data_sections);
1123 if (IN_NAMED_SECTION (decl))
1124 return get_named_section (decl, NULL, reloc);
1126 if (ADDR_SPACE_GENERIC_P (as)
1127 && !DECL_THREAD_LOCAL_P (decl)
1128 && !(prefer_noswitch_p && targetm.have_switchable_bss_sections)
1129 && bss_initializer_p (decl))
1131 if (!TREE_PUBLIC (decl)
1132 && !((flag_sanitize & SANITIZE_ADDRESS)
1133 && asan_protect_global (decl)))
1134 return lcomm_section;
1135 if (bss_noswitch_section)
1136 return bss_noswitch_section;
1139 return targetm.asm_out.select_section (decl, reloc,
1140 get_variable_align (decl));
1143 /* Return the block into which object_block DECL should be placed. */
1145 static struct object_block *
1146 get_block_for_decl (tree decl)
1148 section *sect;
1150 if (TREE_CODE (decl) == VAR_DECL)
1152 /* The object must be defined in this translation unit. */
1153 if (DECL_EXTERNAL (decl))
1154 return NULL;
1156 /* There's no point using object blocks for something that is
1157 isolated by definition. */
1158 if (DECL_COMDAT_GROUP (decl))
1159 return NULL;
1162 /* We can only calculate block offsets if the decl has a known
1163 constant size. */
1164 if (DECL_SIZE_UNIT (decl) == NULL)
1165 return NULL;
1166 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (decl)))
1167 return NULL;
1169 /* Find out which section should contain DECL. We cannot put it into
1170 an object block if it requires a standalone definition. */
1171 if (TREE_CODE (decl) == VAR_DECL)
1172 align_variable (decl, 0);
1173 sect = get_variable_section (decl, true);
1174 if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
1175 return NULL;
1177 return get_block_for_section (sect);
1180 /* Make sure block symbol SYMBOL is in block BLOCK. */
1182 static void
1183 change_symbol_block (rtx symbol, struct object_block *block)
1185 if (block != SYMBOL_REF_BLOCK (symbol))
1187 gcc_assert (SYMBOL_REF_BLOCK_OFFSET (symbol) < 0);
1188 SYMBOL_REF_BLOCK (symbol) = block;
1192 /* Return true if it is possible to put DECL in an object_block. */
1194 static bool
1195 use_blocks_for_decl_p (tree decl)
1197 struct symtab_node *snode;
1199 /* Only data DECLs can be placed into object blocks. */
1200 if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != CONST_DECL)
1201 return false;
1203 /* Detect decls created by dw2_force_const_mem. Such decls are
1204 special because DECL_INITIAL doesn't specify the decl's true value.
1205 dw2_output_indirect_constants will instead call assemble_variable
1206 with dont_output_data set to 1 and then print the contents itself. */
1207 if (DECL_INITIAL (decl) == decl)
1208 return false;
1210 /* If this decl is an alias, then we don't want to emit a
1211 definition. */
1212 if (TREE_CODE (decl) == VAR_DECL
1213 && (snode = symtab_get_node (decl)) != NULL
1214 && snode->alias)
1215 return false;
1217 return targetm.use_blocks_for_decl_p (decl);
1220 /* Create the DECL_RTL for a VAR_DECL or FUNCTION_DECL. DECL should
1221 have static storage duration. In other words, it should not be an
1222 automatic variable, including PARM_DECLs.
1224 There is, however, one exception: this function handles variables
1225 explicitly placed in a particular register by the user.
1227 This is never called for PARM_DECL nodes. */
1229 void
1230 make_decl_rtl (tree decl)
1232 const char *name = 0;
1233 int reg_number;
1234 rtx x;
1236 /* Check that we are not being given an automatic variable. */
1237 gcc_assert (TREE_CODE (decl) != PARM_DECL
1238 && TREE_CODE (decl) != RESULT_DECL);
1240 /* A weak alias has TREE_PUBLIC set but not the other bits. */
1241 gcc_assert (TREE_CODE (decl) != VAR_DECL
1242 || TREE_STATIC (decl)
1243 || TREE_PUBLIC (decl)
1244 || DECL_EXTERNAL (decl)
1245 || DECL_REGISTER (decl));
1247 /* And that we were not given a type or a label. */
1248 gcc_assert (TREE_CODE (decl) != TYPE_DECL
1249 && TREE_CODE (decl) != LABEL_DECL);
1251 /* For a duplicate declaration, we can be called twice on the
1252 same DECL node. Don't discard the RTL already made. */
1253 if (DECL_RTL_SET_P (decl))
1255 /* If the old RTL had the wrong mode, fix the mode. */
1256 x = DECL_RTL (decl);
1257 if (GET_MODE (x) != DECL_MODE (decl))
1258 SET_DECL_RTL (decl, adjust_address_nv (x, DECL_MODE (decl), 0));
1260 if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1261 return;
1263 /* ??? Another way to do this would be to maintain a hashed
1264 table of such critters. Instead of adding stuff to a DECL
1265 to give certain attributes to it, we could use an external
1266 hash map from DECL to set of attributes. */
1268 /* Let the target reassign the RTL if it wants.
1269 This is necessary, for example, when one machine specific
1270 decl attribute overrides another. */
1271 targetm.encode_section_info (decl, DECL_RTL (decl), false);
1273 /* If the symbol has a SYMBOL_REF_BLOCK field, update it based
1274 on the new decl information. */
1275 if (MEM_P (x)
1276 && GET_CODE (XEXP (x, 0)) == SYMBOL_REF
1277 && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (x, 0)))
1278 change_symbol_block (XEXP (x, 0), get_block_for_decl (decl));
1280 return;
1283 /* If this variable belongs to the global constant pool, retrieve the
1284 pre-computed RTL or recompute it in LTO mode. */
1285 if (TREE_CODE (decl) == VAR_DECL && DECL_IN_CONSTANT_POOL (decl))
1287 SET_DECL_RTL (decl, output_constant_def (DECL_INITIAL (decl), 1));
1288 return;
1291 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
1293 if (name[0] != '*' && TREE_CODE (decl) != FUNCTION_DECL
1294 && DECL_REGISTER (decl))
1296 error ("register name not specified for %q+D", decl);
1298 else if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1300 const char *asmspec = name+1;
1301 enum machine_mode mode = DECL_MODE (decl);
1302 reg_number = decode_reg_name (asmspec);
1303 /* First detect errors in declaring global registers. */
1304 if (reg_number == -1)
1305 error ("register name not specified for %q+D", decl);
1306 else if (reg_number < 0)
1307 error ("invalid register name for %q+D", decl);
1308 else if (mode == BLKmode)
1309 error ("data type of %q+D isn%'t suitable for a register",
1310 decl);
1311 else if (!in_hard_reg_set_p (accessible_reg_set, mode, reg_number))
1312 error ("the register specified for %q+D cannot be accessed"
1313 " by the current target", decl);
1314 else if (!in_hard_reg_set_p (operand_reg_set, mode, reg_number))
1315 error ("the register specified for %q+D is not general enough"
1316 " to be used as a register variable", decl);
1317 else if (!HARD_REGNO_MODE_OK (reg_number, mode))
1318 error ("register specified for %q+D isn%'t suitable for data type",
1319 decl);
1320 /* Now handle properly declared static register variables. */
1321 else
1323 int nregs;
1325 if (DECL_INITIAL (decl) != 0 && TREE_STATIC (decl))
1327 DECL_INITIAL (decl) = 0;
1328 error ("global register variable has initial value");
1330 if (TREE_THIS_VOLATILE (decl))
1331 warning (OPT_Wvolatile_register_var,
1332 "optimization may eliminate reads and/or "
1333 "writes to register variables");
1335 /* If the user specified one of the eliminables registers here,
1336 e.g., FRAME_POINTER_REGNUM, we don't want to get this variable
1337 confused with that register and be eliminated. This usage is
1338 somewhat suspect... */
1340 SET_DECL_RTL (decl, gen_rtx_raw_REG (mode, reg_number));
1341 ORIGINAL_REGNO (DECL_RTL (decl)) = reg_number;
1342 REG_USERVAR_P (DECL_RTL (decl)) = 1;
1344 if (TREE_STATIC (decl))
1346 /* Make this register global, so not usable for anything
1347 else. */
1348 #ifdef ASM_DECLARE_REGISTER_GLOBAL
1349 name = IDENTIFIER_POINTER (DECL_NAME (decl));
1350 ASM_DECLARE_REGISTER_GLOBAL (asm_out_file, decl, reg_number, name);
1351 #endif
1352 nregs = hard_regno_nregs[reg_number][mode];
1353 while (nregs > 0)
1354 globalize_reg (decl, reg_number + --nregs);
1357 /* As a register variable, it has no section. */
1358 return;
1361 /* Now handle ordinary static variables and functions (in memory).
1362 Also handle vars declared register invalidly. */
1363 else if (name[0] == '*')
1365 #ifdef REGISTER_PREFIX
1366 if (strlen (REGISTER_PREFIX) != 0)
1368 reg_number = decode_reg_name (name);
1369 if (reg_number >= 0 || reg_number == -3)
1370 error ("register name given for non-register variable %q+D", decl);
1372 #endif
1375 /* Specifying a section attribute on a variable forces it into a
1376 non-.bss section, and thus it cannot be common. */
1377 /* FIXME: In general this code should not be necessary because
1378 visibility pass is doing the same work. But notice_global_symbol
1379 is called early and it needs to make DECL_RTL to get the name.
1380 we take care of recomputing the DECL_RTL after visibility is changed. */
1381 if (TREE_CODE (decl) == VAR_DECL
1382 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
1383 && DECL_SECTION_NAME (decl) != NULL
1384 && DECL_INITIAL (decl) == NULL_TREE
1385 && DECL_COMMON (decl))
1386 DECL_COMMON (decl) = 0;
1388 /* Variables can't be both common and weak. */
1389 if (TREE_CODE (decl) == VAR_DECL && DECL_WEAK (decl))
1390 DECL_COMMON (decl) = 0;
1392 if (use_object_blocks_p () && use_blocks_for_decl_p (decl))
1393 x = create_block_symbol (name, get_block_for_decl (decl), -1);
1394 else
1396 enum machine_mode address_mode = Pmode;
1397 if (TREE_TYPE (decl) != error_mark_node)
1399 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
1400 address_mode = targetm.addr_space.address_mode (as);
1402 x = gen_rtx_SYMBOL_REF (address_mode, name);
1404 SYMBOL_REF_WEAK (x) = DECL_WEAK (decl);
1405 SET_SYMBOL_REF_DECL (x, decl);
1407 x = gen_rtx_MEM (DECL_MODE (decl), x);
1408 if (TREE_CODE (decl) != FUNCTION_DECL)
1409 set_mem_attributes (x, decl, 1);
1410 SET_DECL_RTL (decl, x);
1412 /* Optionally set flags or add text to the name to record information
1413 such as that it is a function name.
1414 If the name is changed, the macro ASM_OUTPUT_LABELREF
1415 will have to know how to strip this information. */
1416 targetm.encode_section_info (decl, DECL_RTL (decl), true);
1419 /* Like make_decl_rtl, but inhibit creation of new alias sets when
1420 calling make_decl_rtl. Also, reset DECL_RTL before returning the
1421 rtl. */
1424 make_decl_rtl_for_debug (tree decl)
1426 unsigned int save_aliasing_flag;
1427 rtx rtl;
1429 if (DECL_RTL_SET_P (decl))
1430 return DECL_RTL (decl);
1432 /* Kludge alert! Somewhere down the call chain, make_decl_rtl will
1433 call new_alias_set. If running with -fcompare-debug, sometimes
1434 we do not want to create alias sets that will throw the alias
1435 numbers off in the comparison dumps. So... clearing
1436 flag_strict_aliasing will keep new_alias_set() from creating a
1437 new set. */
1438 save_aliasing_flag = flag_strict_aliasing;
1439 flag_strict_aliasing = 0;
1441 rtl = DECL_RTL (decl);
1442 /* Reset DECL_RTL back, as various parts of the compiler expects
1443 DECL_RTL set meaning it is actually going to be output. */
1444 SET_DECL_RTL (decl, NULL);
1446 flag_strict_aliasing = save_aliasing_flag;
1447 return rtl;
1450 /* Output a string of literal assembler code
1451 for an `asm' keyword used between functions. */
1453 void
1454 assemble_asm (tree string)
1456 const char *p;
1457 app_enable ();
1459 if (TREE_CODE (string) == ADDR_EXPR)
1460 string = TREE_OPERAND (string, 0);
1462 p = TREE_STRING_POINTER (string);
1463 fprintf (asm_out_file, "%s%s\n", p[0] == '\t' ? "" : "\t", p);
1466 /* Write the address of the entity given by SYMBOL to SEC. */
1467 void
1468 assemble_addr_to_section (rtx symbol, section *sec)
1470 switch_to_section (sec);
1471 assemble_align (POINTER_SIZE);
1472 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1475 /* Return the numbered .ctors.N (if CONSTRUCTOR_P) or .dtors.N (if
1476 not) section for PRIORITY. */
1477 section *
1478 get_cdtor_priority_section (int priority, bool constructor_p)
1480 char buf[16];
1482 /* ??? This only works reliably with the GNU linker. */
1483 sprintf (buf, "%s.%.5u",
1484 constructor_p ? ".ctors" : ".dtors",
1485 /* Invert the numbering so the linker puts us in the proper
1486 order; constructors are run from right to left, and the
1487 linker sorts in increasing order. */
1488 MAX_INIT_PRIORITY - priority);
1489 return get_section (buf, SECTION_WRITE, NULL);
1492 void
1493 default_named_section_asm_out_destructor (rtx symbol, int priority)
1495 section *sec;
1497 if (priority != DEFAULT_INIT_PRIORITY)
1498 sec = get_cdtor_priority_section (priority,
1499 /*constructor_p=*/false);
1500 else
1501 sec = get_section (".dtors", SECTION_WRITE, NULL);
1503 assemble_addr_to_section (symbol, sec);
1506 #ifdef DTORS_SECTION_ASM_OP
1507 void
1508 default_dtor_section_asm_out_destructor (rtx symbol,
1509 int priority ATTRIBUTE_UNUSED)
1511 assemble_addr_to_section (symbol, dtors_section);
1513 #endif
1515 void
1516 default_named_section_asm_out_constructor (rtx symbol, int priority)
1518 section *sec;
1520 if (priority != DEFAULT_INIT_PRIORITY)
1521 sec = get_cdtor_priority_section (priority,
1522 /*constructor_p=*/true);
1523 else
1524 sec = get_section (".ctors", SECTION_WRITE, NULL);
1526 assemble_addr_to_section (symbol, sec);
1529 #ifdef CTORS_SECTION_ASM_OP
1530 void
1531 default_ctor_section_asm_out_constructor (rtx symbol,
1532 int priority ATTRIBUTE_UNUSED)
1534 assemble_addr_to_section (symbol, ctors_section);
1536 #endif
1538 /* CONSTANT_POOL_BEFORE_FUNCTION may be defined as an expression with
1539 a nonzero value if the constant pool should be output before the
1540 start of the function, or a zero value if the pool should output
1541 after the end of the function. The default is to put it before the
1542 start. */
1544 #ifndef CONSTANT_POOL_BEFORE_FUNCTION
1545 #define CONSTANT_POOL_BEFORE_FUNCTION 1
1546 #endif
1548 /* DECL is an object (either VAR_DECL or FUNCTION_DECL) which is going
1549 to be output to assembler.
1550 Set first_global_object_name and weak_global_object_name as appropriate. */
1552 void
1553 notice_global_symbol (tree decl)
1555 const char **type = &first_global_object_name;
1557 if (first_global_object_name
1558 || !TREE_PUBLIC (decl)
1559 || DECL_EXTERNAL (decl)
1560 || !DECL_NAME (decl)
1561 || (TREE_CODE (decl) != FUNCTION_DECL
1562 && (TREE_CODE (decl) != VAR_DECL
1563 || (DECL_COMMON (decl)
1564 && (DECL_INITIAL (decl) == 0
1565 || DECL_INITIAL (decl) == error_mark_node))))
1566 || !MEM_P (DECL_RTL (decl)))
1567 return;
1569 /* We win when global object is found, but it is useful to know about weak
1570 symbol as well so we can produce nicer unique names. */
1571 if (DECL_WEAK (decl) || DECL_ONE_ONLY (decl) || flag_shlib)
1572 type = &weak_global_object_name;
1574 if (!*type)
1576 const char *p;
1577 const char *name;
1578 rtx decl_rtl = DECL_RTL (decl);
1580 p = targetm.strip_name_encoding (XSTR (XEXP (decl_rtl, 0), 0));
1581 name = ggc_strdup (p);
1583 *type = name;
1587 /* If not using flag_reorder_blocks_and_partition, decide early whether the
1588 current function goes into the cold section, so that targets can use
1589 current_function_section during RTL expansion. DECL describes the
1590 function. */
1592 void
1593 decide_function_section (tree decl)
1595 first_function_block_is_cold = false;
1597 if (flag_reorder_blocks_and_partition)
1598 /* We will decide in assemble_start_function. */
1599 return;
1601 if (DECL_SECTION_NAME (decl))
1603 struct cgraph_node *node = cgraph_get_node (current_function_decl);
1604 /* Calls to function_section rely on first_function_block_is_cold
1605 being accurate. */
1606 first_function_block_is_cold = (node
1607 && node->frequency
1608 == NODE_FREQUENCY_UNLIKELY_EXECUTED);
1611 in_cold_section_p = first_function_block_is_cold;
1614 /* Output assembler code for the constant pool of a function and associated
1615 with defining the name of the function. DECL describes the function.
1616 NAME is the function's name. For the constant pool, we use the current
1617 constant pool data. */
1619 void
1620 assemble_start_function (tree decl, const char *fnname)
1622 int align;
1623 char tmp_label[100];
1624 bool hot_label_written = false;
1626 if (flag_reorder_blocks_and_partition)
1628 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTB", const_labelno);
1629 crtl->subsections.hot_section_label = ggc_strdup (tmp_label);
1630 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDB", const_labelno);
1631 crtl->subsections.cold_section_label = ggc_strdup (tmp_label);
1632 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTE", const_labelno);
1633 crtl->subsections.hot_section_end_label = ggc_strdup (tmp_label);
1634 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDE", const_labelno);
1635 crtl->subsections.cold_section_end_label = ggc_strdup (tmp_label);
1636 const_labelno++;
1638 else
1640 crtl->subsections.hot_section_label = NULL;
1641 crtl->subsections.cold_section_label = NULL;
1642 crtl->subsections.hot_section_end_label = NULL;
1643 crtl->subsections.cold_section_end_label = NULL;
1646 /* The following code does not need preprocessing in the assembler. */
1648 app_disable ();
1650 if (CONSTANT_POOL_BEFORE_FUNCTION)
1651 output_constant_pool (fnname, decl);
1653 /* Make sure the not and cold text (code) sections are properly
1654 aligned. This is necessary here in the case where the function
1655 has both hot and cold sections, because we don't want to re-set
1656 the alignment when the section switch happens mid-function. */
1658 if (flag_reorder_blocks_and_partition)
1660 first_function_block_is_cold = false;
1662 switch_to_section (unlikely_text_section ());
1663 assemble_align (DECL_ALIGN (decl));
1664 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_label);
1666 /* When the function starts with a cold section, we need to explicitly
1667 align the hot section and write out the hot section label.
1668 But if the current function is a thunk, we do not have a CFG. */
1669 if (!cfun->is_thunk
1670 && BB_PARTITION (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb) == BB_COLD_PARTITION)
1672 switch_to_section (text_section);
1673 assemble_align (DECL_ALIGN (decl));
1674 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
1675 hot_label_written = true;
1676 first_function_block_is_cold = true;
1678 in_cold_section_p = first_function_block_is_cold;
1682 /* Switch to the correct text section for the start of the function. */
1684 switch_to_section (function_section (decl));
1685 if (flag_reorder_blocks_and_partition
1686 && !hot_label_written)
1687 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
1689 /* Tell assembler to move to target machine's alignment for functions. */
1690 align = floor_log2 (DECL_ALIGN (decl) / BITS_PER_UNIT);
1691 if (align > 0)
1693 ASM_OUTPUT_ALIGN (asm_out_file, align);
1696 /* Handle a user-specified function alignment.
1697 Note that we still need to align to DECL_ALIGN, as above,
1698 because ASM_OUTPUT_MAX_SKIP_ALIGN might not do any alignment at all. */
1699 if (! DECL_USER_ALIGN (decl)
1700 && align_functions_log > align
1701 && optimize_function_for_speed_p (cfun))
1703 #ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
1704 ASM_OUTPUT_MAX_SKIP_ALIGN (asm_out_file,
1705 align_functions_log, align_functions - 1);
1706 #else
1707 ASM_OUTPUT_ALIGN (asm_out_file, align_functions_log);
1708 #endif
1711 #ifdef ASM_OUTPUT_FUNCTION_PREFIX
1712 ASM_OUTPUT_FUNCTION_PREFIX (asm_out_file, fnname);
1713 #endif
1715 if (!DECL_IGNORED_P (decl))
1716 (*debug_hooks->begin_function) (decl);
1718 /* Make function name accessible from other files, if appropriate. */
1720 if (TREE_PUBLIC (decl))
1722 notice_global_symbol (decl);
1724 globalize_decl (decl);
1726 maybe_assemble_visibility (decl);
1729 if (DECL_PRESERVE_P (decl))
1730 targetm.asm_out.mark_decl_preserved (fnname);
1732 /* Do any machine/system dependent processing of the function name. */
1733 #ifdef ASM_DECLARE_FUNCTION_NAME
1734 ASM_DECLARE_FUNCTION_NAME (asm_out_file, fnname, current_function_decl);
1735 #else
1736 /* Standard thing is just output label for the function. */
1737 ASM_OUTPUT_FUNCTION_LABEL (asm_out_file, fnname, current_function_decl);
1738 #endif /* ASM_DECLARE_FUNCTION_NAME */
1740 if (lookup_attribute ("no_split_stack", DECL_ATTRIBUTES (decl)))
1741 saw_no_split_stack = true;
1744 /* Output assembler code associated with defining the size of the
1745 function. DECL describes the function. NAME is the function's name. */
1747 void
1748 assemble_end_function (tree decl, const char *fnname ATTRIBUTE_UNUSED)
1750 #ifdef ASM_DECLARE_FUNCTION_SIZE
1751 /* We could have switched section in the middle of the function. */
1752 if (flag_reorder_blocks_and_partition)
1753 switch_to_section (function_section (decl));
1754 ASM_DECLARE_FUNCTION_SIZE (asm_out_file, fnname, decl);
1755 #endif
1756 if (! CONSTANT_POOL_BEFORE_FUNCTION)
1758 output_constant_pool (fnname, decl);
1759 switch_to_section (function_section (decl)); /* need to switch back */
1761 /* Output labels for end of hot/cold text sections (to be used by
1762 debug info.) */
1763 if (flag_reorder_blocks_and_partition)
1765 section *save_text_section;
1767 save_text_section = in_section;
1768 switch_to_section (unlikely_text_section ());
1769 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_end_label);
1770 if (first_function_block_is_cold)
1771 switch_to_section (text_section);
1772 else
1773 switch_to_section (function_section (decl));
1774 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_end_label);
1775 switch_to_section (save_text_section);
1779 /* Assemble code to leave SIZE bytes of zeros. */
1781 void
1782 assemble_zeros (unsigned HOST_WIDE_INT size)
1784 /* Do no output if -fsyntax-only. */
1785 if (flag_syntax_only)
1786 return;
1788 #ifdef ASM_NO_SKIP_IN_TEXT
1789 /* The `space' pseudo in the text section outputs nop insns rather than 0s,
1790 so we must output 0s explicitly in the text section. */
1791 if (ASM_NO_SKIP_IN_TEXT && (in_section->common.flags & SECTION_CODE) != 0)
1793 unsigned HOST_WIDE_INT i;
1794 for (i = 0; i < size; i++)
1795 assemble_integer (const0_rtx, 1, BITS_PER_UNIT, 1);
1797 else
1798 #endif
1799 if (size > 0)
1800 ASM_OUTPUT_SKIP (asm_out_file, size);
1803 /* Assemble an alignment pseudo op for an ALIGN-bit boundary. */
1805 void
1806 assemble_align (int align)
1808 if (align > BITS_PER_UNIT)
1810 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
1814 /* Assemble a string constant with the specified C string as contents. */
1816 void
1817 assemble_string (const char *p, int size)
1819 int pos = 0;
1820 int maximum = 2000;
1822 /* If the string is very long, split it up. */
1824 while (pos < size)
1826 int thissize = size - pos;
1827 if (thissize > maximum)
1828 thissize = maximum;
1830 ASM_OUTPUT_ASCII (asm_out_file, p, thissize);
1832 pos += thissize;
1833 p += thissize;
1838 /* A noswitch_section_callback for lcomm_section. */
1840 static bool
1841 emit_local (tree decl ATTRIBUTE_UNUSED,
1842 const char *name ATTRIBUTE_UNUSED,
1843 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1844 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1846 #if defined ASM_OUTPUT_ALIGNED_DECL_LOCAL
1847 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, decl, name,
1848 size, DECL_ALIGN (decl));
1849 return true;
1850 #elif defined ASM_OUTPUT_ALIGNED_LOCAL
1851 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, DECL_ALIGN (decl));
1852 return true;
1853 #else
1854 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
1855 return false;
1856 #endif
1859 /* A noswitch_section_callback for bss_noswitch_section. */
1861 #if defined ASM_OUTPUT_ALIGNED_BSS
1862 static bool
1863 emit_bss (tree decl ATTRIBUTE_UNUSED,
1864 const char *name ATTRIBUTE_UNUSED,
1865 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1866 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1868 #if defined ASM_OUTPUT_ALIGNED_BSS
1869 ASM_OUTPUT_ALIGNED_BSS (asm_out_file, decl, name, size,
1870 get_variable_align (decl));
1871 return true;
1872 #endif
1874 #endif
1876 /* A noswitch_section_callback for comm_section. */
1878 static bool
1879 emit_common (tree decl ATTRIBUTE_UNUSED,
1880 const char *name ATTRIBUTE_UNUSED,
1881 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1882 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1884 #if defined ASM_OUTPUT_ALIGNED_DECL_COMMON
1885 ASM_OUTPUT_ALIGNED_DECL_COMMON (asm_out_file, decl, name,
1886 size, get_variable_align (decl));
1887 return true;
1888 #elif defined ASM_OUTPUT_ALIGNED_COMMON
1889 ASM_OUTPUT_ALIGNED_COMMON (asm_out_file, name, size,
1890 get_variable_align (decl));
1891 return true;
1892 #else
1893 ASM_OUTPUT_COMMON (asm_out_file, name, size, rounded);
1894 return false;
1895 #endif
1898 /* A noswitch_section_callback for tls_comm_section. */
1900 static bool
1901 emit_tls_common (tree decl ATTRIBUTE_UNUSED,
1902 const char *name ATTRIBUTE_UNUSED,
1903 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1904 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1906 #ifdef ASM_OUTPUT_TLS_COMMON
1907 ASM_OUTPUT_TLS_COMMON (asm_out_file, decl, name, size);
1908 return true;
1909 #else
1910 sorry ("thread-local COMMON data not implemented");
1911 return true;
1912 #endif
1915 /* Assemble DECL given that it belongs in SECTION_NOSWITCH section SECT.
1916 NAME is the name of DECL's SYMBOL_REF. */
1918 static void
1919 assemble_noswitch_variable (tree decl, const char *name, section *sect,
1920 unsigned int align)
1922 unsigned HOST_WIDE_INT size, rounded;
1924 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
1925 rounded = size;
1927 if ((flag_sanitize & SANITIZE_ADDRESS) && asan_protect_global (decl))
1928 size += asan_red_zone_size (size);
1930 /* Don't allocate zero bytes of common,
1931 since that means "undefined external" in the linker. */
1932 if (size == 0)
1933 rounded = 1;
1935 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
1936 so that each uninitialized object starts on such a boundary. */
1937 rounded += (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1;
1938 rounded = (rounded / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
1939 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
1941 if (!sect->noswitch.callback (decl, name, size, rounded)
1942 && (unsigned HOST_WIDE_INT) (align / BITS_PER_UNIT) > rounded)
1943 error ("requested alignment for %q+D is greater than "
1944 "implemented alignment of %wu", decl, rounded);
1947 /* A subroutine of assemble_variable. Output the label and contents of
1948 DECL, whose address is a SYMBOL_REF with name NAME. DONT_OUTPUT_DATA
1949 is as for assemble_variable. */
1951 static void
1952 assemble_variable_contents (tree decl, const char *name,
1953 bool dont_output_data)
1955 /* Do any machine/system dependent processing of the object. */
1956 #ifdef ASM_DECLARE_OBJECT_NAME
1957 last_assemble_variable_decl = decl;
1958 ASM_DECLARE_OBJECT_NAME (asm_out_file, name, decl);
1959 #else
1960 /* Standard thing is just output label for the object. */
1961 ASM_OUTPUT_LABEL (asm_out_file, name);
1962 #endif /* ASM_DECLARE_OBJECT_NAME */
1964 if (!dont_output_data)
1966 if (DECL_INITIAL (decl)
1967 && DECL_INITIAL (decl) != error_mark_node
1968 && !initializer_zerop (DECL_INITIAL (decl)))
1969 /* Output the actual data. */
1970 output_constant (DECL_INITIAL (decl),
1971 tree_to_uhwi (DECL_SIZE_UNIT (decl)),
1972 get_variable_align (decl));
1973 else
1974 /* Leave space for it. */
1975 assemble_zeros (tree_to_uhwi (DECL_SIZE_UNIT (decl)));
1979 /* Assemble everything that is needed for a variable or function declaration.
1980 Not used for automatic variables, and not used for function definitions.
1981 Should not be called for variables of incomplete structure type.
1983 TOP_LEVEL is nonzero if this variable has file scope.
1984 AT_END is nonzero if this is the special handling, at end of compilation,
1985 to define things that have had only tentative definitions.
1986 DONT_OUTPUT_DATA if nonzero means don't actually output the
1987 initial value (that will be done by the caller). */
1989 void
1990 assemble_variable (tree decl, int top_level ATTRIBUTE_UNUSED,
1991 int at_end ATTRIBUTE_UNUSED, int dont_output_data)
1993 const char *name;
1994 rtx decl_rtl, symbol;
1995 section *sect;
1996 unsigned int align;
1997 bool asan_protected = false;
1999 /* This function is supposed to handle VARIABLES. Ensure we have one. */
2000 gcc_assert (TREE_CODE (decl) == VAR_DECL);
2002 /* Emulated TLS had better not get this far. */
2003 gcc_checking_assert (targetm.have_tls || !DECL_THREAD_LOCAL_P (decl));
2005 last_assemble_variable_decl = 0;
2007 /* Normally no need to say anything here for external references,
2008 since assemble_external is called by the language-specific code
2009 when a declaration is first seen. */
2011 if (DECL_EXTERNAL (decl))
2012 return;
2014 /* Do nothing for global register variables. */
2015 if (DECL_RTL_SET_P (decl) && REG_P (DECL_RTL (decl)))
2017 TREE_ASM_WRITTEN (decl) = 1;
2018 return;
2021 /* If type was incomplete when the variable was declared,
2022 see if it is complete now. */
2024 if (DECL_SIZE (decl) == 0)
2025 layout_decl (decl, 0);
2027 /* Still incomplete => don't allocate it; treat the tentative defn
2028 (which is what it must have been) as an `extern' reference. */
2030 if (!dont_output_data && DECL_SIZE (decl) == 0)
2032 error ("storage size of %q+D isn%'t known", decl);
2033 TREE_ASM_WRITTEN (decl) = 1;
2034 return;
2037 /* The first declaration of a variable that comes through this function
2038 decides whether it is global (in C, has external linkage)
2039 or local (in C, has internal linkage). So do nothing more
2040 if this function has already run. */
2042 if (TREE_ASM_WRITTEN (decl))
2043 return;
2045 /* Make sure targetm.encode_section_info is invoked before we set
2046 ASM_WRITTEN. */
2047 decl_rtl = DECL_RTL (decl);
2049 TREE_ASM_WRITTEN (decl) = 1;
2051 /* Do no output if -fsyntax-only. */
2052 if (flag_syntax_only)
2053 return;
2055 if (! dont_output_data
2056 && ! valid_constant_size_p (DECL_SIZE_UNIT (decl)))
2058 error ("size of variable %q+D is too large", decl);
2059 return;
2062 gcc_assert (MEM_P (decl_rtl));
2063 gcc_assert (GET_CODE (XEXP (decl_rtl, 0)) == SYMBOL_REF);
2064 symbol = XEXP (decl_rtl, 0);
2066 /* If this symbol belongs to the tree constant pool, output the constant
2067 if it hasn't already been written. */
2068 if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
2070 tree decl = SYMBOL_REF_DECL (symbol);
2071 if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
2072 output_constant_def_contents (symbol);
2073 return;
2076 app_disable ();
2078 name = XSTR (symbol, 0);
2079 if (TREE_PUBLIC (decl) && DECL_NAME (decl))
2080 notice_global_symbol (decl);
2082 /* Compute the alignment of this data. */
2084 align_variable (decl, dont_output_data);
2086 if ((flag_sanitize & SANITIZE_ADDRESS)
2087 && asan_protect_global (decl))
2089 asan_protected = true;
2090 DECL_ALIGN (decl) = MAX (DECL_ALIGN (decl),
2091 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
2094 set_mem_align (decl_rtl, DECL_ALIGN (decl));
2096 align = get_variable_align (decl);
2098 if (TREE_PUBLIC (decl))
2099 maybe_assemble_visibility (decl);
2101 if (DECL_PRESERVE_P (decl))
2102 targetm.asm_out.mark_decl_preserved (name);
2104 /* First make the assembler name(s) global if appropriate. */
2105 sect = get_variable_section (decl, false);
2106 if (TREE_PUBLIC (decl)
2107 && (sect->common.flags & SECTION_COMMON) == 0)
2108 globalize_decl (decl);
2110 /* Output any data that we will need to use the address of. */
2111 if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node)
2112 output_addressed_constants (DECL_INITIAL (decl));
2114 /* dbxout.c needs to know this. */
2115 if (sect && (sect->common.flags & SECTION_CODE) != 0)
2116 DECL_IN_TEXT_SECTION (decl) = 1;
2118 /* If the decl is part of an object_block, make sure that the decl
2119 has been positioned within its block, but do not write out its
2120 definition yet. output_object_blocks will do that later. */
2121 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
2123 gcc_assert (!dont_output_data);
2124 place_block_symbol (symbol);
2126 else if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
2127 assemble_noswitch_variable (decl, name, sect, align);
2128 else
2130 /* The following bit of code ensures that vtable_map
2131 variables are not only in the comdat section, but that
2132 each variable has its own unique comdat name. If this
2133 code is removed, the variables end up in the same section
2134 with a single comdat name.
2136 FIXME: resolve_unique_section needs to deal better with
2137 decls with both DECL_SECTION_NAME and DECL_ONE_ONLY. Once
2138 that is fixed, this if-else statement can be replaced with
2139 a single call to "switch_to_section (sect)". */
2140 if (sect->named.name
2141 && (strcmp (sect->named.name, ".vtable_map_vars") == 0))
2143 #if defined (OBJECT_FORMAT_ELF)
2144 targetm.asm_out.named_section (sect->named.name,
2145 sect->named.common.flags
2146 | SECTION_LINKONCE,
2147 DECL_NAME (decl));
2148 in_section = sect;
2149 #else
2150 switch_to_section (sect);
2151 #endif
2153 else
2154 switch_to_section (sect);
2155 if (align > BITS_PER_UNIT)
2156 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
2157 assemble_variable_contents (decl, name, dont_output_data);
2158 if (asan_protected)
2160 unsigned HOST_WIDE_INT int size
2161 = tree_to_uhwi (DECL_SIZE_UNIT (decl));
2162 assemble_zeros (asan_red_zone_size (size));
2168 /* Given a function declaration (FN_DECL), this function assembles the
2169 function into the .preinit_array section. */
2171 void
2172 assemble_vtv_preinit_initializer (tree fn_decl)
2174 section *sect;
2175 unsigned flags = SECTION_WRITE;
2176 rtx symbol = XEXP (DECL_RTL (fn_decl), 0);
2178 flags |= SECTION_NOTYPE;
2179 sect = get_section (".preinit_array", flags, fn_decl);
2180 switch_to_section (sect);
2181 assemble_addr_to_section (symbol, sect);
2184 /* Return 1 if type TYPE contains any pointers. */
2186 static int
2187 contains_pointers_p (tree type)
2189 switch (TREE_CODE (type))
2191 case POINTER_TYPE:
2192 case REFERENCE_TYPE:
2193 /* I'm not sure whether OFFSET_TYPE needs this treatment,
2194 so I'll play safe and return 1. */
2195 case OFFSET_TYPE:
2196 return 1;
2198 case RECORD_TYPE:
2199 case UNION_TYPE:
2200 case QUAL_UNION_TYPE:
2202 tree fields;
2203 /* For a type that has fields, see if the fields have pointers. */
2204 for (fields = TYPE_FIELDS (type); fields; fields = DECL_CHAIN (fields))
2205 if (TREE_CODE (fields) == FIELD_DECL
2206 && contains_pointers_p (TREE_TYPE (fields)))
2207 return 1;
2208 return 0;
2211 case ARRAY_TYPE:
2212 /* An array type contains pointers if its element type does. */
2213 return contains_pointers_p (TREE_TYPE (type));
2215 default:
2216 return 0;
2220 /* We delay assemble_external processing until
2221 the compilation unit is finalized. This is the best we can do for
2222 right now (i.e. stage 3 of GCC 4.0) - the right thing is to delay
2223 it all the way to final. See PR 17982 for further discussion. */
2224 static GTY(()) tree pending_assemble_externals;
2226 #ifdef ASM_OUTPUT_EXTERNAL
2227 /* Some targets delay some output to final using TARGET_ASM_FILE_END.
2228 As a result, assemble_external can be called after the list of externals
2229 is processed and the pointer set destroyed. */
2230 static bool pending_assemble_externals_processed;
2232 /* Avoid O(external_decls**2) lookups in the pending_assemble_externals
2233 TREE_LIST in assemble_external. */
2234 static struct pointer_set_t *pending_assemble_externals_set;
2236 /* True if DECL is a function decl for which no out-of-line copy exists.
2237 It is assumed that DECL's assembler name has been set. */
2239 static bool
2240 incorporeal_function_p (tree decl)
2242 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
2244 const char *name;
2246 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
2247 && (DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA
2248 || DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA_WITH_ALIGN))
2249 return true;
2251 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2252 /* Atomic or sync builtins which have survived this far will be
2253 resolved externally and therefore are not incorporeal. */
2254 if (strncmp (name, "__builtin_", 10) == 0)
2255 return true;
2257 return false;
2260 /* Actually do the tests to determine if this is necessary, and invoke
2261 ASM_OUTPUT_EXTERNAL. */
2262 static void
2263 assemble_external_real (tree decl)
2265 rtx rtl = DECL_RTL (decl);
2267 if (MEM_P (rtl) && GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF
2268 && !SYMBOL_REF_USED (XEXP (rtl, 0))
2269 && !incorporeal_function_p (decl))
2271 /* Some systems do require some output. */
2272 SYMBOL_REF_USED (XEXP (rtl, 0)) = 1;
2273 ASM_OUTPUT_EXTERNAL (asm_out_file, decl, XSTR (XEXP (rtl, 0), 0));
2276 #endif
2278 void
2279 process_pending_assemble_externals (void)
2281 #ifdef ASM_OUTPUT_EXTERNAL
2282 tree list;
2283 for (list = pending_assemble_externals; list; list = TREE_CHAIN (list))
2284 assemble_external_real (TREE_VALUE (list));
2286 pending_assemble_externals = 0;
2287 pending_assemble_externals_processed = true;
2288 pointer_set_destroy (pending_assemble_externals_set);
2289 #endif
2292 /* This TREE_LIST contains any weak symbol declarations waiting
2293 to be emitted. */
2294 static GTY(()) tree weak_decls;
2296 /* Output something to declare an external symbol to the assembler,
2297 and qualifiers such as weakness. (Most assemblers don't need
2298 extern declaration, so we normally output nothing.) Do nothing if
2299 DECL is not external. */
2301 void
2302 assemble_external (tree decl ATTRIBUTE_UNUSED)
2304 /* Make sure that the ASM_OUT_FILE is open.
2305 If it's not, we should not be calling this function. */
2306 gcc_assert (asm_out_file);
2308 /* In a perfect world, the following condition would be true.
2309 Sadly, the Java and Go front ends emit assembly *from the front end*,
2310 bypassing the call graph. See PR52739. Fix before GCC 4.8. */
2311 #if 0
2312 /* This function should only be called if we are expanding, or have
2313 expanded, to RTL.
2314 Ideally, only final.c would be calling this function, but it is
2315 not clear whether that would break things somehow. See PR 17982
2316 for further discussion. */
2317 gcc_assert (cgraph_state == CGRAPH_STATE_EXPANSION
2318 || cgraph_state == CGRAPH_STATE_FINISHED);
2319 #endif
2321 if (!DECL_P (decl) || !DECL_EXTERNAL (decl) || !TREE_PUBLIC (decl))
2322 return;
2324 /* We want to output annotation for weak and external symbols at
2325 very last to check if they are references or not. */
2327 if (TARGET_SUPPORTS_WEAK
2328 && DECL_WEAK (decl)
2329 /* TREE_STATIC is a weird and abused creature which is not
2330 generally the right test for whether an entity has been
2331 locally emitted, inlined or otherwise not-really-extern, but
2332 for declarations that can be weak, it happens to be
2333 match. */
2334 && !TREE_STATIC (decl)
2335 && lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
2336 && value_member (decl, weak_decls) == NULL_TREE)
2337 weak_decls = tree_cons (NULL, decl, weak_decls);
2339 #ifdef ASM_OUTPUT_EXTERNAL
2340 if (pending_assemble_externals_processed)
2342 assemble_external_real (decl);
2343 return;
2346 if (! pointer_set_insert (pending_assemble_externals_set, decl))
2347 pending_assemble_externals = tree_cons (NULL, decl,
2348 pending_assemble_externals);
2349 #endif
2352 /* Similar, for calling a library function FUN. */
2354 void
2355 assemble_external_libcall (rtx fun)
2357 /* Declare library function name external when first used, if nec. */
2358 if (! SYMBOL_REF_USED (fun))
2360 SYMBOL_REF_USED (fun) = 1;
2361 targetm.asm_out.external_libcall (fun);
2365 /* Assemble a label named NAME. */
2367 void
2368 assemble_label (FILE *file, const char *name)
2370 ASM_OUTPUT_LABEL (file, name);
2373 /* Set the symbol_referenced flag for ID. */
2374 void
2375 mark_referenced (tree id)
2377 TREE_SYMBOL_REFERENCED (id) = 1;
2380 /* Set the symbol_referenced flag for DECL and notify callgraph. */
2381 void
2382 mark_decl_referenced (tree decl)
2384 if (TREE_CODE (decl) == FUNCTION_DECL)
2386 /* Extern inline functions don't become needed when referenced.
2387 If we know a method will be emitted in other TU and no new
2388 functions can be marked reachable, just use the external
2389 definition. */
2390 struct cgraph_node *node = cgraph_get_create_node (decl);
2391 if (!DECL_EXTERNAL (decl)
2392 && !node->definition)
2393 cgraph_mark_force_output_node (node);
2395 else if (TREE_CODE (decl) == VAR_DECL)
2397 varpool_node *node = varpool_node_for_decl (decl);
2398 /* C++ frontend use mark_decl_references to force COMDAT variables
2399 to be output that might appear dead otherwise. */
2400 node->force_output = true;
2402 /* else do nothing - we can get various sorts of CST nodes here,
2403 which do not need to be marked. */
2407 /* Follow the IDENTIFIER_TRANSPARENT_ALIAS chain starting at *ALIAS
2408 until we find an identifier that is not itself a transparent alias.
2409 Modify the alias passed to it by reference (and all aliases on the
2410 way to the ultimate target), such that they do not have to be
2411 followed again, and return the ultimate target of the alias
2412 chain. */
2414 static inline tree
2415 ultimate_transparent_alias_target (tree *alias)
2417 tree target = *alias;
2419 if (IDENTIFIER_TRANSPARENT_ALIAS (target))
2421 gcc_assert (TREE_CHAIN (target));
2422 target = ultimate_transparent_alias_target (&TREE_CHAIN (target));
2423 gcc_assert (! IDENTIFIER_TRANSPARENT_ALIAS (target)
2424 && ! TREE_CHAIN (target));
2425 *alias = target;
2428 return target;
2431 /* Output to FILE (an assembly file) a reference to NAME. If NAME
2432 starts with a *, the rest of NAME is output verbatim. Otherwise
2433 NAME is transformed in a target-specific way (usually by the
2434 addition of an underscore). */
2436 void
2437 assemble_name_raw (FILE *file, const char *name)
2439 if (name[0] == '*')
2440 fputs (&name[1], file);
2441 else
2442 ASM_OUTPUT_LABELREF (file, name);
2445 /* Like assemble_name_raw, but should be used when NAME might refer to
2446 an entity that is also represented as a tree (like a function or
2447 variable). If NAME does refer to such an entity, that entity will
2448 be marked as referenced. */
2450 void
2451 assemble_name (FILE *file, const char *name)
2453 const char *real_name;
2454 tree id;
2456 real_name = targetm.strip_name_encoding (name);
2458 id = maybe_get_identifier (real_name);
2459 if (id)
2461 tree id_orig = id;
2463 mark_referenced (id);
2464 ultimate_transparent_alias_target (&id);
2465 if (id != id_orig)
2466 name = IDENTIFIER_POINTER (id);
2467 gcc_assert (! TREE_CHAIN (id));
2470 assemble_name_raw (file, name);
2473 /* Allocate SIZE bytes writable static space with a gensym name
2474 and return an RTX to refer to its address. */
2477 assemble_static_space (unsigned HOST_WIDE_INT size)
2479 char name[12];
2480 const char *namestring;
2481 rtx x;
2483 ASM_GENERATE_INTERNAL_LABEL (name, "LF", const_labelno);
2484 ++const_labelno;
2485 namestring = ggc_strdup (name);
2487 x = gen_rtx_SYMBOL_REF (Pmode, namestring);
2488 SYMBOL_REF_FLAGS (x) = SYMBOL_FLAG_LOCAL;
2490 #ifdef ASM_OUTPUT_ALIGNED_DECL_LOCAL
2491 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, NULL_TREE, name, size,
2492 BIGGEST_ALIGNMENT);
2493 #else
2494 #ifdef ASM_OUTPUT_ALIGNED_LOCAL
2495 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, BIGGEST_ALIGNMENT);
2496 #else
2498 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
2499 so that each uninitialized object starts on such a boundary. */
2500 /* Variable `rounded' might or might not be used in ASM_OUTPUT_LOCAL. */
2501 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED
2502 = ((size + (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1)
2503 / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
2504 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
2505 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
2507 #endif
2508 #endif
2509 return x;
2512 /* Assemble the static constant template for function entry trampolines.
2513 This is done at most once per compilation.
2514 Returns an RTX for the address of the template. */
2516 static GTY(()) rtx initial_trampoline;
2519 assemble_trampoline_template (void)
2521 char label[256];
2522 const char *name;
2523 int align;
2524 rtx symbol;
2526 gcc_assert (targetm.asm_out.trampoline_template != NULL);
2528 if (initial_trampoline)
2529 return initial_trampoline;
2531 /* By default, put trampoline templates in read-only data section. */
2533 #ifdef TRAMPOLINE_SECTION
2534 switch_to_section (TRAMPOLINE_SECTION);
2535 #else
2536 switch_to_section (readonly_data_section);
2537 #endif
2539 /* Write the assembler code to define one. */
2540 align = floor_log2 (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
2541 if (align > 0)
2542 ASM_OUTPUT_ALIGN (asm_out_file, align);
2544 targetm.asm_out.internal_label (asm_out_file, "LTRAMP", 0);
2545 targetm.asm_out.trampoline_template (asm_out_file);
2547 /* Record the rtl to refer to it. */
2548 ASM_GENERATE_INTERNAL_LABEL (label, "LTRAMP", 0);
2549 name = ggc_strdup (label);
2550 symbol = gen_rtx_SYMBOL_REF (Pmode, name);
2551 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_LOCAL;
2553 initial_trampoline = gen_const_mem (BLKmode, symbol);
2554 set_mem_align (initial_trampoline, TRAMPOLINE_ALIGNMENT);
2555 set_mem_size (initial_trampoline, TRAMPOLINE_SIZE);
2557 return initial_trampoline;
2560 /* A and B are either alignments or offsets. Return the minimum alignment
2561 that may be assumed after adding the two together. */
2563 static inline unsigned
2564 min_align (unsigned int a, unsigned int b)
2566 return (a | b) & -(a | b);
2569 /* Return the assembler directive for creating a given kind of integer
2570 object. SIZE is the number of bytes in the object and ALIGNED_P
2571 indicates whether it is known to be aligned. Return NULL if the
2572 assembly dialect has no such directive.
2574 The returned string should be printed at the start of a new line and
2575 be followed immediately by the object's initial value. */
2577 const char *
2578 integer_asm_op (int size, int aligned_p)
2580 struct asm_int_op *ops;
2582 if (aligned_p)
2583 ops = &targetm.asm_out.aligned_op;
2584 else
2585 ops = &targetm.asm_out.unaligned_op;
2587 switch (size)
2589 case 1:
2590 return targetm.asm_out.byte_op;
2591 case 2:
2592 return ops->hi;
2593 case 4:
2594 return ops->si;
2595 case 8:
2596 return ops->di;
2597 case 16:
2598 return ops->ti;
2599 default:
2600 return NULL;
2604 /* Use directive OP to assemble an integer object X. Print OP at the
2605 start of the line, followed immediately by the value of X. */
2607 void
2608 assemble_integer_with_op (const char *op, rtx x)
2610 fputs (op, asm_out_file);
2611 output_addr_const (asm_out_file, x);
2612 fputc ('\n', asm_out_file);
2615 /* The default implementation of the asm_out.integer target hook. */
2617 bool
2618 default_assemble_integer (rtx x ATTRIBUTE_UNUSED,
2619 unsigned int size ATTRIBUTE_UNUSED,
2620 int aligned_p ATTRIBUTE_UNUSED)
2622 const char *op = integer_asm_op (size, aligned_p);
2623 /* Avoid GAS bugs for large values. Specifically negative values whose
2624 absolute value fits in a bfd_vma, but not in a bfd_signed_vma. */
2625 if (size > UNITS_PER_WORD && size > POINTER_SIZE / BITS_PER_UNIT)
2626 return false;
2627 return op && (assemble_integer_with_op (op, x), true);
2630 /* Assemble the integer constant X into an object of SIZE bytes. ALIGN is
2631 the alignment of the integer in bits. Return 1 if we were able to output
2632 the constant, otherwise 0. We must be able to output the constant,
2633 if FORCE is nonzero. */
2635 bool
2636 assemble_integer (rtx x, unsigned int size, unsigned int align, int force)
2638 int aligned_p;
2640 aligned_p = (align >= MIN (size * BITS_PER_UNIT, BIGGEST_ALIGNMENT));
2642 /* See if the target hook can handle this kind of object. */
2643 if (targetm.asm_out.integer (x, size, aligned_p))
2644 return true;
2646 /* If the object is a multi-byte one, try splitting it up. Split
2647 it into words it if is multi-word, otherwise split it into bytes. */
2648 if (size > 1)
2650 enum machine_mode omode, imode;
2651 unsigned int subalign;
2652 unsigned int subsize, i;
2653 enum mode_class mclass;
2655 subsize = size > UNITS_PER_WORD? UNITS_PER_WORD : 1;
2656 subalign = MIN (align, subsize * BITS_PER_UNIT);
2657 if (GET_CODE (x) == CONST_FIXED)
2658 mclass = GET_MODE_CLASS (GET_MODE (x));
2659 else
2660 mclass = MODE_INT;
2662 omode = mode_for_size (subsize * BITS_PER_UNIT, mclass, 0);
2663 imode = mode_for_size (size * BITS_PER_UNIT, mclass, 0);
2665 for (i = 0; i < size; i += subsize)
2667 rtx partial = simplify_subreg (omode, x, imode, i);
2668 if (!partial || !assemble_integer (partial, subsize, subalign, 0))
2669 break;
2671 if (i == size)
2672 return true;
2674 /* If we've printed some of it, but not all of it, there's no going
2675 back now. */
2676 gcc_assert (!i);
2679 gcc_assert (!force);
2681 return false;
2684 void
2685 assemble_real (REAL_VALUE_TYPE d, enum machine_mode mode, unsigned int align)
2687 long data[4] = {0, 0, 0, 0};
2688 int i;
2689 int bitsize, nelts, nunits, units_per;
2691 /* This is hairy. We have a quantity of known size. real_to_target
2692 will put it into an array of *host* longs, 32 bits per element
2693 (even if long is more than 32 bits). We need to determine the
2694 number of array elements that are occupied (nelts) and the number
2695 of *target* min-addressable units that will be occupied in the
2696 object file (nunits). We cannot assume that 32 divides the
2697 mode's bitsize (size * BITS_PER_UNIT) evenly.
2699 size * BITS_PER_UNIT is used here to make sure that padding bits
2700 (which might appear at either end of the value; real_to_target
2701 will include the padding bits in its output array) are included. */
2703 nunits = GET_MODE_SIZE (mode);
2704 bitsize = nunits * BITS_PER_UNIT;
2705 nelts = CEIL (bitsize, 32);
2706 units_per = 32 / BITS_PER_UNIT;
2708 real_to_target (data, &d, mode);
2710 /* Put out the first word with the specified alignment. */
2711 assemble_integer (GEN_INT (data[0]), MIN (nunits, units_per), align, 1);
2712 nunits -= units_per;
2714 /* Subsequent words need only 32-bit alignment. */
2715 align = min_align (align, 32);
2717 for (i = 1; i < nelts; i++)
2719 assemble_integer (GEN_INT (data[i]), MIN (nunits, units_per), align, 1);
2720 nunits -= units_per;
2724 /* Given an expression EXP with a constant value,
2725 reduce it to the sum of an assembler symbol and an integer.
2726 Store them both in the structure *VALUE.
2727 EXP must be reducible. */
2729 struct addr_const {
2730 rtx base;
2731 HOST_WIDE_INT offset;
2734 static void
2735 decode_addr_const (tree exp, struct addr_const *value)
2737 tree target = TREE_OPERAND (exp, 0);
2738 int offset = 0;
2739 rtx x;
2741 while (1)
2743 if (TREE_CODE (target) == COMPONENT_REF
2744 && tree_fits_shwi_p (byte_position (TREE_OPERAND (target, 1))))
2746 offset += int_byte_position (TREE_OPERAND (target, 1));
2747 target = TREE_OPERAND (target, 0);
2749 else if (TREE_CODE (target) == ARRAY_REF
2750 || TREE_CODE (target) == ARRAY_RANGE_REF)
2752 offset += (tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (target)))
2753 * tree_to_shwi (TREE_OPERAND (target, 1)));
2754 target = TREE_OPERAND (target, 0);
2756 else if (TREE_CODE (target) == MEM_REF
2757 && TREE_CODE (TREE_OPERAND (target, 0)) == ADDR_EXPR)
2759 offset += mem_ref_offset (target).to_short_addr ();
2760 target = TREE_OPERAND (TREE_OPERAND (target, 0), 0);
2762 else if (TREE_CODE (target) == INDIRECT_REF
2763 && TREE_CODE (TREE_OPERAND (target, 0)) == NOP_EXPR
2764 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (target, 0), 0))
2765 == ADDR_EXPR)
2766 target = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (target, 0), 0), 0);
2767 else
2768 break;
2771 switch (TREE_CODE (target))
2773 case VAR_DECL:
2774 case FUNCTION_DECL:
2775 x = DECL_RTL (target);
2776 break;
2778 case LABEL_DECL:
2779 x = gen_rtx_MEM (FUNCTION_MODE,
2780 gen_rtx_LABEL_REF (Pmode, force_label_rtx (target)));
2781 break;
2783 case REAL_CST:
2784 case FIXED_CST:
2785 case STRING_CST:
2786 case COMPLEX_CST:
2787 case CONSTRUCTOR:
2788 case INTEGER_CST:
2789 x = output_constant_def (target, 1);
2790 break;
2792 default:
2793 gcc_unreachable ();
2796 gcc_assert (MEM_P (x));
2797 x = XEXP (x, 0);
2799 value->base = x;
2800 value->offset = offset;
2804 static GTY((param_is (struct constant_descriptor_tree)))
2805 htab_t const_desc_htab;
2807 static void maybe_output_constant_def_contents (struct constant_descriptor_tree *, int);
2809 /* Constant pool accessor function. */
2811 htab_t
2812 constant_pool_htab (void)
2814 return const_desc_htab;
2817 /* Compute a hash code for a constant expression. */
2819 static hashval_t
2820 const_desc_hash (const void *ptr)
2822 return ((const struct constant_descriptor_tree *)ptr)->hash;
2825 static hashval_t
2826 const_hash_1 (const tree exp)
2828 const char *p;
2829 hashval_t hi;
2830 int len, i;
2831 enum tree_code code = TREE_CODE (exp);
2833 /* Either set P and LEN to the address and len of something to hash and
2834 exit the switch or return a value. */
2836 switch (code)
2838 case INTEGER_CST:
2839 p = (char *) &TREE_INT_CST_ELT (exp, 0);
2840 len = TREE_INT_CST_NUNITS (exp) * sizeof (HOST_WIDE_INT);
2841 break;
2843 case REAL_CST:
2844 return real_hash (TREE_REAL_CST_PTR (exp));
2846 case FIXED_CST:
2847 return fixed_hash (TREE_FIXED_CST_PTR (exp));
2849 case STRING_CST:
2850 p = TREE_STRING_POINTER (exp);
2851 len = TREE_STRING_LENGTH (exp);
2852 break;
2854 case COMPLEX_CST:
2855 return (const_hash_1 (TREE_REALPART (exp)) * 5
2856 + const_hash_1 (TREE_IMAGPART (exp)));
2858 case VECTOR_CST:
2860 unsigned i;
2862 hi = 7 + VECTOR_CST_NELTS (exp);
2864 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
2865 hi = hi * 563 + const_hash_1 (VECTOR_CST_ELT (exp, i));
2867 return hi;
2870 case CONSTRUCTOR:
2872 unsigned HOST_WIDE_INT idx;
2873 tree value;
2875 hi = 5 + int_size_in_bytes (TREE_TYPE (exp));
2877 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
2878 if (value)
2879 hi = hi * 603 + const_hash_1 (value);
2881 return hi;
2884 case ADDR_EXPR:
2885 case FDESC_EXPR:
2887 struct addr_const value;
2889 decode_addr_const (exp, &value);
2890 switch (GET_CODE (value.base))
2892 case SYMBOL_REF:
2893 /* Don't hash the address of the SYMBOL_REF;
2894 only use the offset and the symbol name. */
2895 hi = value.offset;
2896 p = XSTR (value.base, 0);
2897 for (i = 0; p[i] != 0; i++)
2898 hi = ((hi * 613) + (unsigned) (p[i]));
2899 break;
2901 case LABEL_REF:
2902 hi = value.offset + CODE_LABEL_NUMBER (XEXP (value.base, 0)) * 13;
2903 break;
2905 default:
2906 gcc_unreachable ();
2909 return hi;
2911 case PLUS_EXPR:
2912 case POINTER_PLUS_EXPR:
2913 case MINUS_EXPR:
2914 return (const_hash_1 (TREE_OPERAND (exp, 0)) * 9
2915 + const_hash_1 (TREE_OPERAND (exp, 1)));
2917 CASE_CONVERT:
2918 return const_hash_1 (TREE_OPERAND (exp, 0)) * 7 + 2;
2920 default:
2921 /* A language specific constant. Just hash the code. */
2922 return code;
2925 /* Compute hashing function. */
2926 hi = len;
2927 for (i = 0; i < len; i++)
2928 hi = ((hi * 613) + (unsigned) (p[i]));
2930 return hi;
2933 /* Wrapper of compare_constant, for the htab interface. */
2934 static int
2935 const_desc_eq (const void *p1, const void *p2)
2937 const struct constant_descriptor_tree *const c1
2938 = (const struct constant_descriptor_tree *) p1;
2939 const struct constant_descriptor_tree *const c2
2940 = (const struct constant_descriptor_tree *) p2;
2941 if (c1->hash != c2->hash)
2942 return 0;
2943 return compare_constant (c1->value, c2->value);
2946 /* Compare t1 and t2, and return 1 only if they are known to result in
2947 the same bit pattern on output. */
2949 static int
2950 compare_constant (const tree t1, const tree t2)
2952 enum tree_code typecode;
2954 if (t1 == NULL_TREE)
2955 return t2 == NULL_TREE;
2956 if (t2 == NULL_TREE)
2957 return 0;
2959 if (TREE_CODE (t1) != TREE_CODE (t2))
2960 return 0;
2962 switch (TREE_CODE (t1))
2964 case INTEGER_CST:
2965 /* Integer constants are the same only if the same width of type. */
2966 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
2967 return 0;
2968 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
2969 return 0;
2970 return tree_int_cst_equal (t1, t2);
2972 case REAL_CST:
2973 /* Real constants are the same only if the same width of type. */
2974 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
2975 return 0;
2977 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
2979 case FIXED_CST:
2980 /* Fixed constants are the same only if the same width of type. */
2981 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
2982 return 0;
2984 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
2986 case STRING_CST:
2987 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
2988 return 0;
2990 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
2991 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
2992 TREE_STRING_LENGTH (t1)));
2994 case COMPLEX_CST:
2995 return (compare_constant (TREE_REALPART (t1), TREE_REALPART (t2))
2996 && compare_constant (TREE_IMAGPART (t1), TREE_IMAGPART (t2)));
2998 case VECTOR_CST:
3000 unsigned i;
3002 if (VECTOR_CST_NELTS (t1) != VECTOR_CST_NELTS (t2))
3003 return 0;
3005 for (i = 0; i < VECTOR_CST_NELTS (t1); ++i)
3006 if (!compare_constant (VECTOR_CST_ELT (t1, i),
3007 VECTOR_CST_ELT (t2, i)))
3008 return 0;
3010 return 1;
3013 case CONSTRUCTOR:
3015 vec<constructor_elt, va_gc> *v1, *v2;
3016 unsigned HOST_WIDE_INT idx;
3018 typecode = TREE_CODE (TREE_TYPE (t1));
3019 if (typecode != TREE_CODE (TREE_TYPE (t2)))
3020 return 0;
3022 if (typecode == ARRAY_TYPE)
3024 HOST_WIDE_INT size_1 = int_size_in_bytes (TREE_TYPE (t1));
3025 /* For arrays, check that the sizes all match. */
3026 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))
3027 || size_1 == -1
3028 || size_1 != int_size_in_bytes (TREE_TYPE (t2)))
3029 return 0;
3031 else
3033 /* For record and union constructors, require exact type
3034 equality. */
3035 if (TREE_TYPE (t1) != TREE_TYPE (t2))
3036 return 0;
3039 v1 = CONSTRUCTOR_ELTS (t1);
3040 v2 = CONSTRUCTOR_ELTS (t2);
3041 if (vec_safe_length (v1) != vec_safe_length (v2))
3042 return 0;
3044 for (idx = 0; idx < vec_safe_length (v1); ++idx)
3046 constructor_elt *c1 = &(*v1)[idx];
3047 constructor_elt *c2 = &(*v2)[idx];
3049 /* Check that each value is the same... */
3050 if (!compare_constant (c1->value, c2->value))
3051 return 0;
3052 /* ... and that they apply to the same fields! */
3053 if (typecode == ARRAY_TYPE)
3055 if (!compare_constant (c1->index, c2->index))
3056 return 0;
3058 else
3060 if (c1->index != c2->index)
3061 return 0;
3065 return 1;
3068 case ADDR_EXPR:
3069 case FDESC_EXPR:
3071 struct addr_const value1, value2;
3072 enum rtx_code code;
3073 int ret;
3075 decode_addr_const (t1, &value1);
3076 decode_addr_const (t2, &value2);
3078 if (value1.offset != value2.offset)
3079 return 0;
3081 code = GET_CODE (value1.base);
3082 if (code != GET_CODE (value2.base))
3083 return 0;
3085 switch (code)
3087 case SYMBOL_REF:
3088 ret = (strcmp (XSTR (value1.base, 0), XSTR (value2.base, 0)) == 0);
3089 break;
3091 case LABEL_REF:
3092 ret = (CODE_LABEL_NUMBER (XEXP (value1.base, 0))
3093 == CODE_LABEL_NUMBER (XEXP (value2.base, 0)));
3094 break;
3096 default:
3097 gcc_unreachable ();
3099 return ret;
3102 case PLUS_EXPR:
3103 case POINTER_PLUS_EXPR:
3104 case MINUS_EXPR:
3105 case RANGE_EXPR:
3106 return (compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
3107 && compare_constant (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
3109 CASE_CONVERT:
3110 case VIEW_CONVERT_EXPR:
3111 return compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3113 default:
3114 return 0;
3117 gcc_unreachable ();
3120 /* Return the section into which constant EXP should be placed. */
3122 static section *
3123 get_constant_section (tree exp, unsigned int align)
3125 return targetm.asm_out.select_section (exp,
3126 compute_reloc_for_constant (exp),
3127 align);
3130 /* Return the size of constant EXP in bytes. */
3132 static HOST_WIDE_INT
3133 get_constant_size (tree exp)
3135 HOST_WIDE_INT size;
3137 size = int_size_in_bytes (TREE_TYPE (exp));
3138 if (TREE_CODE (exp) == STRING_CST)
3139 size = MAX (TREE_STRING_LENGTH (exp), size);
3140 return size;
3143 /* Subroutine of output_constant_def:
3144 No constant equal to EXP is known to have been output.
3145 Make a constant descriptor to enter EXP in the hash table.
3146 Assign the label number and construct RTL to refer to the
3147 constant's location in memory.
3148 Caller is responsible for updating the hash table. */
3150 static struct constant_descriptor_tree *
3151 build_constant_desc (tree exp)
3153 struct constant_descriptor_tree *desc;
3154 rtx symbol, rtl;
3155 char label[256];
3156 int labelno;
3157 tree decl;
3159 desc = ggc_alloc<constant_descriptor_tree> ();
3160 desc->value = exp;
3162 /* Create a string containing the label name, in LABEL. */
3163 labelno = const_labelno++;
3164 ASM_GENERATE_INTERNAL_LABEL (label, "LC", labelno);
3166 /* Construct the VAR_DECL associated with the constant. */
3167 decl = build_decl (UNKNOWN_LOCATION, VAR_DECL, get_identifier (label),
3168 TREE_TYPE (exp));
3169 DECL_ARTIFICIAL (decl) = 1;
3170 DECL_IGNORED_P (decl) = 1;
3171 TREE_READONLY (decl) = 1;
3172 TREE_STATIC (decl) = 1;
3173 TREE_ADDRESSABLE (decl) = 1;
3174 /* We don't set the RTL yet as this would cause varpool to assume that the
3175 variable is referenced. Moreover, it would just be dropped in LTO mode.
3176 Instead we set the flag that will be recognized in make_decl_rtl. */
3177 DECL_IN_CONSTANT_POOL (decl) = 1;
3178 DECL_INITIAL (decl) = desc->value;
3179 /* ??? CONSTANT_ALIGNMENT hasn't been updated for vector types on most
3180 architectures so use DATA_ALIGNMENT as well, except for strings. */
3181 if (TREE_CODE (exp) == STRING_CST)
3183 #ifdef CONSTANT_ALIGNMENT
3184 DECL_ALIGN (decl) = CONSTANT_ALIGNMENT (exp, DECL_ALIGN (decl));
3185 #endif
3187 else
3188 align_variable (decl, 0);
3190 /* Now construct the SYMBOL_REF and the MEM. */
3191 if (use_object_blocks_p ())
3193 section *sect = get_constant_section (exp, DECL_ALIGN (decl));
3194 symbol = create_block_symbol (ggc_strdup (label),
3195 get_block_for_section (sect), -1);
3197 else
3198 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3199 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3200 SET_SYMBOL_REF_DECL (symbol, decl);
3201 TREE_CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3203 rtl = gen_const_mem (TYPE_MODE (TREE_TYPE (exp)), symbol);
3204 set_mem_attributes (rtl, exp, 1);
3205 set_mem_alias_set (rtl, 0);
3206 set_mem_alias_set (rtl, const_alias_set);
3208 /* We cannot share RTX'es in pool entries.
3209 Mark this piece of RTL as required for unsharing. */
3210 RTX_FLAG (rtl, used) = 1;
3212 /* Set flags or add text to the name to record information, such as
3213 that it is a local symbol. If the name is changed, the macro
3214 ASM_OUTPUT_LABELREF will have to know how to strip this
3215 information. This call might invalidate our local variable
3216 SYMBOL; we can't use it afterward. */
3217 targetm.encode_section_info (exp, rtl, true);
3219 desc->rtl = rtl;
3221 return desc;
3224 /* Return an rtx representing a reference to constant data in memory
3225 for the constant expression EXP.
3227 If assembler code for such a constant has already been output,
3228 return an rtx to refer to it.
3229 Otherwise, output such a constant in memory
3230 and generate an rtx for it.
3232 If DEFER is nonzero, this constant can be deferred and output only
3233 if referenced in the function after all optimizations.
3235 `const_desc_table' records which constants already have label strings. */
3238 output_constant_def (tree exp, int defer)
3240 struct constant_descriptor_tree *desc;
3241 struct constant_descriptor_tree key;
3242 void **loc;
3244 /* Look up EXP in the table of constant descriptors. If we didn't find
3245 it, create a new one. */
3246 key.value = exp;
3247 key.hash = const_hash_1 (exp);
3248 loc = htab_find_slot_with_hash (const_desc_htab, &key, key.hash, INSERT);
3250 desc = (struct constant_descriptor_tree *) *loc;
3251 if (desc == 0)
3253 desc = build_constant_desc (exp);
3254 desc->hash = key.hash;
3255 *loc = desc;
3258 maybe_output_constant_def_contents (desc, defer);
3259 return desc->rtl;
3262 /* Subroutine of output_constant_def: Decide whether or not we need to
3263 output the constant DESC now, and if so, do it. */
3264 static void
3265 maybe_output_constant_def_contents (struct constant_descriptor_tree *desc,
3266 int defer)
3268 rtx symbol = XEXP (desc->rtl, 0);
3269 tree exp = desc->value;
3271 if (flag_syntax_only)
3272 return;
3274 if (TREE_ASM_WRITTEN (exp))
3275 /* Already output; don't do it again. */
3276 return;
3278 /* We can always defer constants as long as the context allows
3279 doing so. */
3280 if (defer)
3282 /* Increment n_deferred_constants if it exists. It needs to be at
3283 least as large as the number of constants actually referred to
3284 by the function. If it's too small we'll stop looking too early
3285 and fail to emit constants; if it's too large we'll only look
3286 through the entire function when we could have stopped earlier. */
3287 if (cfun)
3288 n_deferred_constants++;
3289 return;
3292 output_constant_def_contents (symbol);
3295 /* Subroutine of output_constant_def_contents. Output the definition
3296 of constant EXP, which is pointed to by label LABEL. ALIGN is the
3297 constant's alignment in bits. */
3299 static void
3300 assemble_constant_contents (tree exp, const char *label, unsigned int align)
3302 HOST_WIDE_INT size;
3304 size = get_constant_size (exp);
3306 /* Do any machine/system dependent processing of the constant. */
3307 targetm.asm_out.declare_constant_name (asm_out_file, label, exp, size);
3309 /* Output the value of EXP. */
3310 output_constant (exp, size, align);
3313 /* We must output the constant data referred to by SYMBOL; do so. */
3315 static void
3316 output_constant_def_contents (rtx symbol)
3318 tree decl = SYMBOL_REF_DECL (symbol);
3319 tree exp = DECL_INITIAL (decl);
3320 unsigned int align;
3321 bool asan_protected = false;
3323 /* Make sure any other constants whose addresses appear in EXP
3324 are assigned label numbers. */
3325 output_addressed_constants (exp);
3327 /* We are no longer deferring this constant. */
3328 TREE_ASM_WRITTEN (decl) = TREE_ASM_WRITTEN (exp) = 1;
3330 if ((flag_sanitize & SANITIZE_ADDRESS)
3331 && TREE_CODE (exp) == STRING_CST
3332 && asan_protect_global (exp))
3334 asan_protected = true;
3335 DECL_ALIGN (decl) = MAX (DECL_ALIGN (decl),
3336 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
3339 /* If the constant is part of an object block, make sure that the
3340 decl has been positioned within its block, but do not write out
3341 its definition yet. output_object_blocks will do that later. */
3342 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
3343 place_block_symbol (symbol);
3344 else
3346 align = DECL_ALIGN (decl);
3347 switch_to_section (get_constant_section (exp, align));
3348 if (align > BITS_PER_UNIT)
3349 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
3350 assemble_constant_contents (exp, XSTR (symbol, 0), align);
3351 if (asan_protected)
3353 HOST_WIDE_INT size = get_constant_size (exp);
3354 assemble_zeros (asan_red_zone_size (size));
3359 /* Look up EXP in the table of constant descriptors. Return the rtl
3360 if it has been emitted, else null. */
3363 lookup_constant_def (tree exp)
3365 struct constant_descriptor_tree *desc;
3366 struct constant_descriptor_tree key;
3368 key.value = exp;
3369 key.hash = const_hash_1 (exp);
3370 desc = (struct constant_descriptor_tree *)
3371 htab_find_with_hash (const_desc_htab, &key, key.hash);
3373 return (desc ? desc->rtl : NULL_RTX);
3376 /* Return a tree representing a reference to constant data in memory
3377 for the constant expression EXP.
3379 This is the counterpart of output_constant_def at the Tree level. */
3381 tree
3382 tree_output_constant_def (tree exp)
3384 struct constant_descriptor_tree *desc, key;
3385 void **loc;
3386 tree decl;
3388 /* Look up EXP in the table of constant descriptors. If we didn't find
3389 it, create a new one. */
3390 key.value = exp;
3391 key.hash = const_hash_1 (exp);
3392 loc = htab_find_slot_with_hash (const_desc_htab, &key, key.hash, INSERT);
3394 desc = (struct constant_descriptor_tree *) *loc;
3395 if (desc == 0)
3397 desc = build_constant_desc (exp);
3398 desc->hash = key.hash;
3399 *loc = desc;
3402 decl = SYMBOL_REF_DECL (XEXP (desc->rtl, 0));
3403 varpool_finalize_decl (decl);
3404 return decl;
3407 /* Used in the hash tables to avoid outputting the same constant
3408 twice. Unlike 'struct constant_descriptor_tree', RTX constants
3409 are output once per function, not once per file. */
3410 /* ??? Only a few targets need per-function constant pools. Most
3411 can use one per-file pool. Should add a targetm bit to tell the
3412 difference. */
3414 struct GTY(()) rtx_constant_pool {
3415 /* Pointers to first and last constant in pool, as ordered by offset. */
3416 struct constant_descriptor_rtx *first;
3417 struct constant_descriptor_rtx *last;
3419 /* Hash facility for making memory-constants from constant rtl-expressions.
3420 It is used on RISC machines where immediate integer arguments and
3421 constant addresses are restricted so that such constants must be stored
3422 in memory. */
3423 htab_t GTY((param_is (struct constant_descriptor_rtx))) const_rtx_htab;
3425 /* Current offset in constant pool (does not include any
3426 machine-specific header). */
3427 HOST_WIDE_INT offset;
3430 struct GTY((chain_next ("%h.next"))) constant_descriptor_rtx {
3431 struct constant_descriptor_rtx *next;
3432 rtx mem;
3433 rtx sym;
3434 rtx constant;
3435 HOST_WIDE_INT offset;
3436 hashval_t hash;
3437 enum machine_mode mode;
3438 unsigned int align;
3439 int labelno;
3440 int mark;
3443 /* Hash and compare functions for const_rtx_htab. */
3445 static hashval_t
3446 const_desc_rtx_hash (const void *ptr)
3448 const struct constant_descriptor_rtx *const desc
3449 = (const struct constant_descriptor_rtx *) ptr;
3450 return desc->hash;
3453 static int
3454 const_desc_rtx_eq (const void *a, const void *b)
3456 const struct constant_descriptor_rtx *const x
3457 = (const struct constant_descriptor_rtx *) a;
3458 const struct constant_descriptor_rtx *const y
3459 = (const struct constant_descriptor_rtx *) b;
3461 if (x->mode != y->mode)
3462 return 0;
3463 return rtx_equal_p (x->constant, y->constant);
3466 /* This is the worker function for const_rtx_hash, called via for_each_rtx. */
3468 static int
3469 const_rtx_hash_1 (rtx *xp, void *data)
3471 unsigned HOST_WIDE_INT hwi;
3472 enum machine_mode mode;
3473 enum rtx_code code;
3474 hashval_t h, *hp;
3475 rtx x;
3476 int i;
3478 x = *xp;
3479 code = GET_CODE (x);
3480 mode = GET_MODE (x);
3481 h = (hashval_t) code * 1048573 + mode;
3483 switch (code)
3485 case CONST_INT:
3486 hwi = INTVAL (x);
3488 fold_hwi:
3490 int shift = sizeof (hashval_t) * CHAR_BIT;
3491 const int n = sizeof (HOST_WIDE_INT) / sizeof (hashval_t);
3493 h ^= (hashval_t) hwi;
3494 for (i = 1; i < n; ++i)
3496 hwi >>= shift;
3497 h ^= (hashval_t) hwi;
3500 break;
3502 case CONST_WIDE_INT:
3503 hwi = GET_MODE_PRECISION (mode);
3505 for (i = 0; i < CONST_WIDE_INT_NUNITS (x); i++)
3506 hwi ^= CONST_WIDE_INT_ELT (x, i);
3507 goto fold_hwi;
3510 case CONST_DOUBLE:
3511 if (TARGET_SUPPORTS_WIDE_INT == 0 && mode == VOIDmode)
3513 hwi = CONST_DOUBLE_LOW (x) ^ CONST_DOUBLE_HIGH (x);
3514 goto fold_hwi;
3516 else
3517 h ^= real_hash (CONST_DOUBLE_REAL_VALUE (x));
3518 break;
3520 case CONST_FIXED:
3521 h ^= fixed_hash (CONST_FIXED_VALUE (x));
3522 break;
3524 case CONST_VECTOR:
3526 int i;
3527 for (i = XVECLEN (x, 0); i-- > 0; )
3528 h = h * 251 + const_rtx_hash_1 (&XVECEXP (x, 0, i), data);
3530 break;
3532 case SYMBOL_REF:
3533 h ^= htab_hash_string (XSTR (x, 0));
3534 break;
3536 case LABEL_REF:
3537 h = h * 251 + CODE_LABEL_NUMBER (XEXP (x, 0));
3538 break;
3540 case UNSPEC:
3541 case UNSPEC_VOLATILE:
3542 h = h * 251 + XINT (x, 1);
3543 break;
3545 default:
3546 break;
3549 hp = (hashval_t *) data;
3550 *hp = *hp * 509 + h;
3551 return 0;
3554 /* Compute a hash value for X, which should be a constant. */
3556 static hashval_t
3557 const_rtx_hash (rtx x)
3559 hashval_t h = 0;
3560 for_each_rtx (&x, const_rtx_hash_1, &h);
3561 return h;
3565 /* Create and return a new rtx constant pool. */
3567 static struct rtx_constant_pool *
3568 create_constant_pool (void)
3570 struct rtx_constant_pool *pool;
3572 pool = ggc_alloc<rtx_constant_pool> ();
3573 pool->const_rtx_htab = htab_create_ggc (31, const_desc_rtx_hash,
3574 const_desc_rtx_eq, NULL);
3575 pool->first = NULL;
3576 pool->last = NULL;
3577 pool->offset = 0;
3578 return pool;
3581 /* Initialize constant pool hashing for a new function. */
3583 void
3584 init_varasm_status (void)
3586 crtl->varasm.pool = create_constant_pool ();
3587 crtl->varasm.deferred_constants = 0;
3590 /* Given a MINUS expression, simplify it if both sides
3591 include the same symbol. */
3594 simplify_subtraction (rtx x)
3596 rtx r = simplify_rtx (x);
3597 return r ? r : x;
3600 /* Given a constant rtx X, make (or find) a memory constant for its value
3601 and return a MEM rtx to refer to it in memory. */
3604 force_const_mem (enum machine_mode mode, rtx x)
3606 struct constant_descriptor_rtx *desc, tmp;
3607 struct rtx_constant_pool *pool;
3608 char label[256];
3609 rtx def, symbol;
3610 hashval_t hash;
3611 unsigned int align;
3612 void **slot;
3614 /* If we're not allowed to drop X into the constant pool, don't. */
3615 if (targetm.cannot_force_const_mem (mode, x))
3616 return NULL_RTX;
3618 /* Record that this function has used a constant pool entry. */
3619 crtl->uses_const_pool = 1;
3621 /* Decide which pool to use. */
3622 pool = (targetm.use_blocks_for_constant_p (mode, x)
3623 ? shared_constant_pool
3624 : crtl->varasm.pool);
3626 /* Lookup the value in the hashtable. */
3627 tmp.constant = x;
3628 tmp.mode = mode;
3629 hash = const_rtx_hash (x);
3630 slot = htab_find_slot_with_hash (pool->const_rtx_htab, &tmp, hash, INSERT);
3631 desc = (struct constant_descriptor_rtx *) *slot;
3633 /* If the constant was already present, return its memory. */
3634 if (desc)
3635 return copy_rtx (desc->mem);
3637 /* Otherwise, create a new descriptor. */
3638 desc = ggc_alloc<constant_descriptor_rtx> ();
3639 *slot = desc;
3641 /* Align the location counter as required by EXP's data type. */
3642 align = GET_MODE_ALIGNMENT (mode == VOIDmode ? word_mode : mode);
3643 #ifdef CONSTANT_ALIGNMENT
3645 tree type = lang_hooks.types.type_for_mode (mode, 0);
3646 if (type != NULL_TREE)
3647 align = CONSTANT_ALIGNMENT (make_tree (type, x), align);
3649 #endif
3651 pool->offset += (align / BITS_PER_UNIT) - 1;
3652 pool->offset &= ~ ((align / BITS_PER_UNIT) - 1);
3654 desc->next = NULL;
3655 desc->constant = copy_rtx (tmp.constant);
3656 desc->offset = pool->offset;
3657 desc->hash = hash;
3658 desc->mode = mode;
3659 desc->align = align;
3660 desc->labelno = const_labelno;
3661 desc->mark = 0;
3663 pool->offset += GET_MODE_SIZE (mode);
3664 if (pool->last)
3665 pool->last->next = desc;
3666 else
3667 pool->first = pool->last = desc;
3668 pool->last = desc;
3670 /* Create a string containing the label name, in LABEL. */
3671 ASM_GENERATE_INTERNAL_LABEL (label, "LC", const_labelno);
3672 ++const_labelno;
3674 /* Construct the SYMBOL_REF. Make sure to mark it as belonging to
3675 the constants pool. */
3676 if (use_object_blocks_p () && targetm.use_blocks_for_constant_p (mode, x))
3678 section *sect = targetm.asm_out.select_rtx_section (mode, x, align);
3679 symbol = create_block_symbol (ggc_strdup (label),
3680 get_block_for_section (sect), -1);
3682 else
3683 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3684 desc->sym = symbol;
3685 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3686 CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3687 SET_SYMBOL_REF_CONSTANT (symbol, desc);
3689 /* Construct the MEM. */
3690 desc->mem = def = gen_const_mem (mode, symbol);
3691 set_mem_attributes (def, lang_hooks.types.type_for_mode (mode, 0), 1);
3692 set_mem_align (def, align);
3694 /* If we're dropping a label to the constant pool, make sure we
3695 don't delete it. */
3696 if (GET_CODE (x) == LABEL_REF)
3697 LABEL_PRESERVE_P (XEXP (x, 0)) = 1;
3699 return copy_rtx (def);
3702 /* Given a constant pool SYMBOL_REF, return the corresponding constant. */
3705 get_pool_constant (rtx addr)
3707 return SYMBOL_REF_CONSTANT (addr)->constant;
3710 /* Given a constant pool SYMBOL_REF, return the corresponding constant
3711 and whether it has been output or not. */
3714 get_pool_constant_mark (rtx addr, bool *pmarked)
3716 struct constant_descriptor_rtx *desc;
3718 desc = SYMBOL_REF_CONSTANT (addr);
3719 *pmarked = (desc->mark != 0);
3720 return desc->constant;
3723 /* Similar, return the mode. */
3725 enum machine_mode
3726 get_pool_mode (const_rtx addr)
3728 return SYMBOL_REF_CONSTANT (addr)->mode;
3731 /* Return the size of the constant pool. */
3734 get_pool_size (void)
3736 return crtl->varasm.pool->offset;
3739 /* Worker function for output_constant_pool_1. Emit assembly for X
3740 in MODE with known alignment ALIGN. */
3742 static void
3743 output_constant_pool_2 (enum machine_mode mode, rtx x, unsigned int align)
3745 switch (GET_MODE_CLASS (mode))
3747 case MODE_FLOAT:
3748 case MODE_DECIMAL_FLOAT:
3750 REAL_VALUE_TYPE r;
3752 gcc_assert (CONST_DOUBLE_AS_FLOAT_P (x));
3753 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
3754 assemble_real (r, mode, align);
3755 break;
3758 case MODE_INT:
3759 case MODE_PARTIAL_INT:
3760 case MODE_FRACT:
3761 case MODE_UFRACT:
3762 case MODE_ACCUM:
3763 case MODE_UACCUM:
3764 assemble_integer (x, GET_MODE_SIZE (mode), align, 1);
3765 break;
3767 case MODE_VECTOR_FLOAT:
3768 case MODE_VECTOR_INT:
3769 case MODE_VECTOR_FRACT:
3770 case MODE_VECTOR_UFRACT:
3771 case MODE_VECTOR_ACCUM:
3772 case MODE_VECTOR_UACCUM:
3774 int i, units;
3775 enum machine_mode submode = GET_MODE_INNER (mode);
3776 unsigned int subalign = MIN (align, GET_MODE_BITSIZE (submode));
3778 gcc_assert (GET_CODE (x) == CONST_VECTOR);
3779 units = CONST_VECTOR_NUNITS (x);
3781 for (i = 0; i < units; i++)
3783 rtx elt = CONST_VECTOR_ELT (x, i);
3784 output_constant_pool_2 (submode, elt, i ? subalign : align);
3787 break;
3789 default:
3790 gcc_unreachable ();
3794 /* Worker function for output_constant_pool. Emit constant DESC,
3795 giving it ALIGN bits of alignment. */
3797 static void
3798 output_constant_pool_1 (struct constant_descriptor_rtx *desc,
3799 unsigned int align)
3801 rtx x, tmp;
3803 x = desc->constant;
3805 /* See if X is a LABEL_REF (or a CONST referring to a LABEL_REF)
3806 whose CODE_LABEL has been deleted. This can occur if a jump table
3807 is eliminated by optimization. If so, write a constant of zero
3808 instead. Note that this can also happen by turning the
3809 CODE_LABEL into a NOTE. */
3810 /* ??? This seems completely and utterly wrong. Certainly it's
3811 not true for NOTE_INSN_DELETED_LABEL, but I disbelieve proper
3812 functioning even with INSN_DELETED_P and friends. */
3814 tmp = x;
3815 switch (GET_CODE (tmp))
3817 case CONST:
3818 if (GET_CODE (XEXP (tmp, 0)) != PLUS
3819 || GET_CODE (XEXP (XEXP (tmp, 0), 0)) != LABEL_REF)
3820 break;
3821 tmp = XEXP (XEXP (tmp, 0), 0);
3822 /* FALLTHRU */
3824 case LABEL_REF:
3825 tmp = XEXP (tmp, 0);
3826 gcc_assert (!INSN_DELETED_P (tmp));
3827 gcc_assert (!NOTE_P (tmp)
3828 || NOTE_KIND (tmp) != NOTE_INSN_DELETED);
3829 break;
3831 default:
3832 break;
3835 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3836 ASM_OUTPUT_SPECIAL_POOL_ENTRY (asm_out_file, x, desc->mode,
3837 align, desc->labelno, done);
3838 #endif
3840 assemble_align (align);
3842 /* Output the label. */
3843 targetm.asm_out.internal_label (asm_out_file, "LC", desc->labelno);
3845 /* Output the data. */
3846 output_constant_pool_2 (desc->mode, x, align);
3848 /* Make sure all constants in SECTION_MERGE and not SECTION_STRINGS
3849 sections have proper size. */
3850 if (align > GET_MODE_BITSIZE (desc->mode)
3851 && in_section
3852 && (in_section->common.flags & SECTION_MERGE))
3853 assemble_align (align);
3855 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3856 done:
3857 #endif
3858 return;
3861 /* Given a SYMBOL_REF CURRENT_RTX, mark it and all constants it refers
3862 to as used. Emit referenced deferred strings. This function can
3863 be used with for_each_rtx to mark all SYMBOL_REFs in an rtx. */
3865 static int
3866 mark_constant (rtx *current_rtx, void *data ATTRIBUTE_UNUSED)
3868 rtx x = *current_rtx;
3870 if (x == NULL_RTX || GET_CODE (x) != SYMBOL_REF)
3871 return 0;
3873 if (CONSTANT_POOL_ADDRESS_P (x))
3875 struct constant_descriptor_rtx *desc = SYMBOL_REF_CONSTANT (x);
3876 if (desc->mark == 0)
3878 desc->mark = 1;
3879 for_each_rtx (&desc->constant, mark_constant, NULL);
3882 else if (TREE_CONSTANT_POOL_ADDRESS_P (x))
3884 tree decl = SYMBOL_REF_DECL (x);
3885 if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
3887 n_deferred_constants--;
3888 output_constant_def_contents (x);
3892 return -1;
3895 /* Look through appropriate parts of INSN, marking all entries in the
3896 constant pool which are actually being used. Entries that are only
3897 referenced by other constants are also marked as used. Emit
3898 deferred strings that are used. */
3900 static void
3901 mark_constants (rtx insn)
3903 if (!INSN_P (insn))
3904 return;
3906 /* Insns may appear inside a SEQUENCE. Only check the patterns of
3907 insns, not any notes that may be attached. We don't want to mark
3908 a constant just because it happens to appear in a REG_EQUIV note. */
3909 if (GET_CODE (PATTERN (insn)) == SEQUENCE)
3911 rtx seq = PATTERN (insn);
3912 int i, n = XVECLEN (seq, 0);
3913 for (i = 0; i < n; ++i)
3915 rtx subinsn = XVECEXP (seq, 0, i);
3916 if (INSN_P (subinsn))
3917 for_each_rtx (&PATTERN (subinsn), mark_constant, NULL);
3920 else
3921 for_each_rtx (&PATTERN (insn), mark_constant, NULL);
3924 /* Look through the instructions for this function, and mark all the
3925 entries in POOL which are actually being used. Emit deferred constants
3926 which have indeed been used. */
3928 static void
3929 mark_constant_pool (void)
3931 rtx insn;
3933 if (!crtl->uses_const_pool && n_deferred_constants == 0)
3934 return;
3936 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3937 mark_constants (insn);
3940 /* Write all the constants in POOL. */
3942 static void
3943 output_constant_pool_contents (struct rtx_constant_pool *pool)
3945 struct constant_descriptor_rtx *desc;
3947 for (desc = pool->first; desc ; desc = desc->next)
3948 if (desc->mark)
3950 /* If the constant is part of an object_block, make sure that
3951 the constant has been positioned within its block, but do not
3952 write out its definition yet. output_object_blocks will do
3953 that later. */
3954 if (SYMBOL_REF_HAS_BLOCK_INFO_P (desc->sym)
3955 && SYMBOL_REF_BLOCK (desc->sym))
3956 place_block_symbol (desc->sym);
3957 else
3959 switch_to_section (targetm.asm_out.select_rtx_section
3960 (desc->mode, desc->constant, desc->align));
3961 output_constant_pool_1 (desc, desc->align);
3966 /* Mark all constants that are used in the current function, then write
3967 out the function's private constant pool. */
3969 static void
3970 output_constant_pool (const char *fnname ATTRIBUTE_UNUSED,
3971 tree fndecl ATTRIBUTE_UNUSED)
3973 struct rtx_constant_pool *pool = crtl->varasm.pool;
3975 /* It is possible for gcc to call force_const_mem and then to later
3976 discard the instructions which refer to the constant. In such a
3977 case we do not need to output the constant. */
3978 mark_constant_pool ();
3980 #ifdef ASM_OUTPUT_POOL_PROLOGUE
3981 ASM_OUTPUT_POOL_PROLOGUE (asm_out_file, fnname, fndecl, pool->offset);
3982 #endif
3984 output_constant_pool_contents (pool);
3986 #ifdef ASM_OUTPUT_POOL_EPILOGUE
3987 ASM_OUTPUT_POOL_EPILOGUE (asm_out_file, fnname, fndecl, pool->offset);
3988 #endif
3991 /* Write the contents of the shared constant pool. */
3993 void
3994 output_shared_constant_pool (void)
3996 output_constant_pool_contents (shared_constant_pool);
3999 /* Determine what kind of relocations EXP may need. */
4002 compute_reloc_for_constant (tree exp)
4004 int reloc = 0, reloc2;
4005 tree tem;
4007 switch (TREE_CODE (exp))
4009 case ADDR_EXPR:
4010 case FDESC_EXPR:
4011 /* Go inside any operations that get_inner_reference can handle and see
4012 if what's inside is a constant: no need to do anything here for
4013 addresses of variables or functions. */
4014 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
4015 tem = TREE_OPERAND (tem, 0))
4018 if (TREE_CODE (tem) == MEM_REF
4019 && TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR)
4021 reloc = compute_reloc_for_constant (TREE_OPERAND (tem, 0));
4022 break;
4025 if (!targetm.binds_local_p (tem))
4026 reloc |= 2;
4027 else
4028 reloc |= 1;
4029 break;
4031 case PLUS_EXPR:
4032 case POINTER_PLUS_EXPR:
4033 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4034 reloc |= compute_reloc_for_constant (TREE_OPERAND (exp, 1));
4035 break;
4037 case MINUS_EXPR:
4038 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4039 reloc2 = compute_reloc_for_constant (TREE_OPERAND (exp, 1));
4040 /* The difference of two local labels is computable at link time. */
4041 if (reloc == 1 && reloc2 == 1)
4042 reloc = 0;
4043 else
4044 reloc |= reloc2;
4045 break;
4047 CASE_CONVERT:
4048 case VIEW_CONVERT_EXPR:
4049 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4050 break;
4052 case CONSTRUCTOR:
4054 unsigned HOST_WIDE_INT idx;
4055 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
4056 if (tem != 0)
4057 reloc |= compute_reloc_for_constant (tem);
4059 break;
4061 default:
4062 break;
4064 return reloc;
4067 /* Find all the constants whose addresses are referenced inside of EXP,
4068 and make sure assembler code with a label has been output for each one.
4069 Indicate whether an ADDR_EXPR has been encountered. */
4071 static void
4072 output_addressed_constants (tree exp)
4074 tree tem;
4076 switch (TREE_CODE (exp))
4078 case ADDR_EXPR:
4079 case FDESC_EXPR:
4080 /* Go inside any operations that get_inner_reference can handle and see
4081 if what's inside is a constant: no need to do anything here for
4082 addresses of variables or functions. */
4083 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
4084 tem = TREE_OPERAND (tem, 0))
4087 /* If we have an initialized CONST_DECL, retrieve the initializer. */
4088 if (TREE_CODE (tem) == CONST_DECL && DECL_INITIAL (tem))
4089 tem = DECL_INITIAL (tem);
4091 if (CONSTANT_CLASS_P (tem) || TREE_CODE (tem) == CONSTRUCTOR)
4092 output_constant_def (tem, 0);
4094 if (TREE_CODE (tem) == MEM_REF)
4095 output_addressed_constants (TREE_OPERAND (tem, 0));
4096 break;
4098 case PLUS_EXPR:
4099 case POINTER_PLUS_EXPR:
4100 case MINUS_EXPR:
4101 output_addressed_constants (TREE_OPERAND (exp, 1));
4102 /* Fall through. */
4104 CASE_CONVERT:
4105 case VIEW_CONVERT_EXPR:
4106 output_addressed_constants (TREE_OPERAND (exp, 0));
4107 break;
4109 case CONSTRUCTOR:
4111 unsigned HOST_WIDE_INT idx;
4112 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
4113 if (tem != 0)
4114 output_addressed_constants (tem);
4116 break;
4118 default:
4119 break;
4123 /* Whether a constructor CTOR is a valid static constant initializer if all
4124 its elements are. This used to be internal to initializer_constant_valid_p
4125 and has been exposed to let other functions like categorize_ctor_elements
4126 evaluate the property while walking a constructor for other purposes. */
4128 bool
4129 constructor_static_from_elts_p (const_tree ctor)
4131 return (TREE_CONSTANT (ctor)
4132 && (TREE_CODE (TREE_TYPE (ctor)) == UNION_TYPE
4133 || TREE_CODE (TREE_TYPE (ctor)) == RECORD_TYPE
4134 || TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE));
4137 static tree initializer_constant_valid_p_1 (tree value, tree endtype,
4138 tree *cache);
4140 /* A subroutine of initializer_constant_valid_p. VALUE is a MINUS_EXPR,
4141 PLUS_EXPR or POINTER_PLUS_EXPR. This looks for cases of VALUE
4142 which are valid when ENDTYPE is an integer of any size; in
4143 particular, this does not accept a pointer minus a constant. This
4144 returns null_pointer_node if the VALUE is an absolute constant
4145 which can be used to initialize a static variable. Otherwise it
4146 returns NULL. */
4148 static tree
4149 narrowing_initializer_constant_valid_p (tree value, tree endtype, tree *cache)
4151 tree op0, op1;
4153 if (!INTEGRAL_TYPE_P (endtype))
4154 return NULL_TREE;
4156 op0 = TREE_OPERAND (value, 0);
4157 op1 = TREE_OPERAND (value, 1);
4159 /* Like STRIP_NOPS except allow the operand mode to widen. This
4160 works around a feature of fold that simplifies (int)(p1 - p2) to
4161 ((int)p1 - (int)p2) under the theory that the narrower operation
4162 is cheaper. */
4164 while (CONVERT_EXPR_P (op0)
4165 || TREE_CODE (op0) == NON_LVALUE_EXPR)
4167 tree inner = TREE_OPERAND (op0, 0);
4168 if (inner == error_mark_node
4169 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4170 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))
4171 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
4172 break;
4173 op0 = inner;
4176 while (CONVERT_EXPR_P (op1)
4177 || TREE_CODE (op1) == NON_LVALUE_EXPR)
4179 tree inner = TREE_OPERAND (op1, 0);
4180 if (inner == error_mark_node
4181 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4182 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op1)))
4183 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
4184 break;
4185 op1 = inner;
4188 op0 = initializer_constant_valid_p_1 (op0, endtype, cache);
4189 if (!op0)
4190 return NULL_TREE;
4192 op1 = initializer_constant_valid_p_1 (op1, endtype,
4193 cache ? cache + 2 : NULL);
4194 /* Both initializers must be known. */
4195 if (op1)
4197 if (op0 == op1
4198 && (op0 == null_pointer_node
4199 || TREE_CODE (value) == MINUS_EXPR))
4200 return null_pointer_node;
4202 /* Support differences between labels. */
4203 if (TREE_CODE (op0) == LABEL_DECL
4204 && TREE_CODE (op1) == LABEL_DECL)
4205 return null_pointer_node;
4207 if (TREE_CODE (op0) == STRING_CST
4208 && TREE_CODE (op1) == STRING_CST
4209 && operand_equal_p (op0, op1, 1))
4210 return null_pointer_node;
4213 return NULL_TREE;
4216 /* Helper function of initializer_constant_valid_p.
4217 Return nonzero if VALUE is a valid constant-valued expression
4218 for use in initializing a static variable; one that can be an
4219 element of a "constant" initializer.
4221 Return null_pointer_node if the value is absolute;
4222 if it is relocatable, return the variable that determines the relocation.
4223 We assume that VALUE has been folded as much as possible;
4224 therefore, we do not need to check for such things as
4225 arithmetic-combinations of integers.
4227 Use CACHE (pointer to 2 tree values) for caching if non-NULL. */
4229 static tree
4230 initializer_constant_valid_p_1 (tree value, tree endtype, tree *cache)
4232 tree ret;
4234 switch (TREE_CODE (value))
4236 case CONSTRUCTOR:
4237 if (constructor_static_from_elts_p (value))
4239 unsigned HOST_WIDE_INT idx;
4240 tree elt;
4241 bool absolute = true;
4243 if (cache && cache[0] == value)
4244 return cache[1];
4245 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
4247 tree reloc;
4248 reloc = initializer_constant_valid_p_1 (elt, TREE_TYPE (elt),
4249 NULL);
4250 if (!reloc)
4252 if (cache)
4254 cache[0] = value;
4255 cache[1] = NULL_TREE;
4257 return NULL_TREE;
4259 if (reloc != null_pointer_node)
4260 absolute = false;
4262 /* For a non-absolute relocation, there is no single
4263 variable that can be "the variable that determines the
4264 relocation." */
4265 if (cache)
4267 cache[0] = value;
4268 cache[1] = absolute ? null_pointer_node : error_mark_node;
4270 return absolute ? null_pointer_node : error_mark_node;
4273 return TREE_STATIC (value) ? null_pointer_node : NULL_TREE;
4275 case INTEGER_CST:
4276 case VECTOR_CST:
4277 case REAL_CST:
4278 case FIXED_CST:
4279 case STRING_CST:
4280 case COMPLEX_CST:
4281 return null_pointer_node;
4283 case ADDR_EXPR:
4284 case FDESC_EXPR:
4286 tree op0 = staticp (TREE_OPERAND (value, 0));
4287 if (op0)
4289 /* "&(*a).f" is like unto pointer arithmetic. If "a" turns out
4290 to be a constant, this is old-skool offsetof-like nonsense. */
4291 if (TREE_CODE (op0) == INDIRECT_REF
4292 && TREE_CONSTANT (TREE_OPERAND (op0, 0)))
4293 return null_pointer_node;
4294 /* Taking the address of a nested function involves a trampoline,
4295 unless we don't need or want one. */
4296 if (TREE_CODE (op0) == FUNCTION_DECL
4297 && DECL_STATIC_CHAIN (op0)
4298 && !TREE_NO_TRAMPOLINE (value))
4299 return NULL_TREE;
4300 /* "&{...}" requires a temporary to hold the constructed
4301 object. */
4302 if (TREE_CODE (op0) == CONSTRUCTOR)
4303 return NULL_TREE;
4305 return op0;
4308 case NON_LVALUE_EXPR:
4309 return initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4310 endtype, cache);
4312 case VIEW_CONVERT_EXPR:
4314 tree src = TREE_OPERAND (value, 0);
4315 tree src_type = TREE_TYPE (src);
4316 tree dest_type = TREE_TYPE (value);
4318 /* Allow view-conversions from aggregate to non-aggregate type only
4319 if the bit pattern is fully preserved afterwards; otherwise, the
4320 RTL expander won't be able to apply a subsequent transformation
4321 to the underlying constructor. */
4322 if (AGGREGATE_TYPE_P (src_type) && !AGGREGATE_TYPE_P (dest_type))
4324 if (TYPE_MODE (endtype) == TYPE_MODE (dest_type))
4325 return initializer_constant_valid_p_1 (src, endtype, cache);
4326 else
4327 return NULL_TREE;
4330 /* Allow all other kinds of view-conversion. */
4331 return initializer_constant_valid_p_1 (src, endtype, cache);
4334 CASE_CONVERT:
4336 tree src = TREE_OPERAND (value, 0);
4337 tree src_type = TREE_TYPE (src);
4338 tree dest_type = TREE_TYPE (value);
4340 /* Allow conversions between pointer types, floating-point
4341 types, and offset types. */
4342 if ((POINTER_TYPE_P (dest_type) && POINTER_TYPE_P (src_type))
4343 || (FLOAT_TYPE_P (dest_type) && FLOAT_TYPE_P (src_type))
4344 || (TREE_CODE (dest_type) == OFFSET_TYPE
4345 && TREE_CODE (src_type) == OFFSET_TYPE))
4346 return initializer_constant_valid_p_1 (src, endtype, cache);
4348 /* Allow length-preserving conversions between integer types. */
4349 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type)
4350 && (TYPE_PRECISION (dest_type) == TYPE_PRECISION (src_type)))
4351 return initializer_constant_valid_p_1 (src, endtype, cache);
4353 /* Allow conversions between other integer types only if
4354 explicit value. */
4355 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type))
4357 tree inner = initializer_constant_valid_p_1 (src, endtype, cache);
4358 if (inner == null_pointer_node)
4359 return null_pointer_node;
4360 break;
4363 /* Allow (int) &foo provided int is as wide as a pointer. */
4364 if (INTEGRAL_TYPE_P (dest_type) && POINTER_TYPE_P (src_type)
4365 && (TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type)))
4366 return initializer_constant_valid_p_1 (src, endtype, cache);
4368 /* Likewise conversions from int to pointers, but also allow
4369 conversions from 0. */
4370 if ((POINTER_TYPE_P (dest_type)
4371 || TREE_CODE (dest_type) == OFFSET_TYPE)
4372 && INTEGRAL_TYPE_P (src_type))
4374 if (TREE_CODE (src) == INTEGER_CST
4375 && TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type))
4376 return null_pointer_node;
4377 if (integer_zerop (src))
4378 return null_pointer_node;
4379 else if (TYPE_PRECISION (dest_type) <= TYPE_PRECISION (src_type))
4380 return initializer_constant_valid_p_1 (src, endtype, cache);
4383 /* Allow conversions to struct or union types if the value
4384 inside is okay. */
4385 if (TREE_CODE (dest_type) == RECORD_TYPE
4386 || TREE_CODE (dest_type) == UNION_TYPE)
4387 return initializer_constant_valid_p_1 (src, endtype, cache);
4389 break;
4391 case POINTER_PLUS_EXPR:
4392 case PLUS_EXPR:
4393 /* Any valid floating-point constants will have been folded by now;
4394 with -frounding-math we hit this with addition of two constants. */
4395 if (TREE_CODE (endtype) == REAL_TYPE)
4396 return NULL_TREE;
4397 if (cache && cache[0] == value)
4398 return cache[1];
4399 if (! INTEGRAL_TYPE_P (endtype)
4400 || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
4402 tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
4403 tree valid0
4404 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4405 endtype, ncache);
4406 tree valid1
4407 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
4408 endtype, ncache + 2);
4409 /* If either term is absolute, use the other term's relocation. */
4410 if (valid0 == null_pointer_node)
4411 ret = valid1;
4412 else if (valid1 == null_pointer_node)
4413 ret = valid0;
4414 /* Support narrowing pointer differences. */
4415 else
4416 ret = narrowing_initializer_constant_valid_p (value, endtype,
4417 ncache);
4419 else
4420 /* Support narrowing pointer differences. */
4421 ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
4422 if (cache)
4424 cache[0] = value;
4425 cache[1] = ret;
4427 return ret;
4429 case MINUS_EXPR:
4430 if (TREE_CODE (endtype) == REAL_TYPE)
4431 return NULL_TREE;
4432 if (cache && cache[0] == value)
4433 return cache[1];
4434 if (! INTEGRAL_TYPE_P (endtype)
4435 || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
4437 tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
4438 tree valid0
4439 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4440 endtype, ncache);
4441 tree valid1
4442 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
4443 endtype, ncache + 2);
4444 /* Win if second argument is absolute. */
4445 if (valid1 == null_pointer_node)
4446 ret = valid0;
4447 /* Win if both arguments have the same relocation.
4448 Then the value is absolute. */
4449 else if (valid0 == valid1 && valid0 != 0)
4450 ret = null_pointer_node;
4451 /* Since GCC guarantees that string constants are unique in the
4452 generated code, a subtraction between two copies of the same
4453 constant string is absolute. */
4454 else if (valid0 && TREE_CODE (valid0) == STRING_CST
4455 && valid1 && TREE_CODE (valid1) == STRING_CST
4456 && operand_equal_p (valid0, valid1, 1))
4457 ret = null_pointer_node;
4458 /* Support narrowing differences. */
4459 else
4460 ret = narrowing_initializer_constant_valid_p (value, endtype,
4461 ncache);
4463 else
4464 /* Support narrowing differences. */
4465 ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
4466 if (cache)
4468 cache[0] = value;
4469 cache[1] = ret;
4471 return ret;
4473 default:
4474 break;
4477 return NULL_TREE;
4480 /* Return nonzero if VALUE is a valid constant-valued expression
4481 for use in initializing a static variable; one that can be an
4482 element of a "constant" initializer.
4484 Return null_pointer_node if the value is absolute;
4485 if it is relocatable, return the variable that determines the relocation.
4486 We assume that VALUE has been folded as much as possible;
4487 therefore, we do not need to check for such things as
4488 arithmetic-combinations of integers. */
4489 tree
4490 initializer_constant_valid_p (tree value, tree endtype)
4492 return initializer_constant_valid_p_1 (value, endtype, NULL);
4495 /* Return true if VALUE is a valid constant-valued expression
4496 for use in initializing a static bit-field; one that can be
4497 an element of a "constant" initializer. */
4499 bool
4500 initializer_constant_valid_for_bitfield_p (tree value)
4502 /* For bitfields we support integer constants or possibly nested aggregates
4503 of such. */
4504 switch (TREE_CODE (value))
4506 case CONSTRUCTOR:
4508 unsigned HOST_WIDE_INT idx;
4509 tree elt;
4511 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
4512 if (!initializer_constant_valid_for_bitfield_p (elt))
4513 return false;
4514 return true;
4517 case INTEGER_CST:
4518 case REAL_CST:
4519 return true;
4521 case VIEW_CONVERT_EXPR:
4522 case NON_LVALUE_EXPR:
4523 return
4524 initializer_constant_valid_for_bitfield_p (TREE_OPERAND (value, 0));
4526 default:
4527 break;
4530 return false;
4533 /* output_constructor outer state of relevance in recursive calls, typically
4534 for nested aggregate bitfields. */
4536 typedef struct {
4537 unsigned int bit_offset; /* current position in ... */
4538 int byte; /* ... the outer byte buffer. */
4539 } oc_outer_state;
4541 static unsigned HOST_WIDE_INT
4542 output_constructor (tree, unsigned HOST_WIDE_INT, unsigned int,
4543 oc_outer_state *);
4545 /* Output assembler code for constant EXP, with no label.
4546 This includes the pseudo-op such as ".int" or ".byte", and a newline.
4547 Assumes output_addressed_constants has been done on EXP already.
4549 Generate at least SIZE bytes of assembler data, padding at the end
4550 with zeros if necessary. SIZE must always be specified. The returned
4551 value is the actual number of bytes of assembler data generated, which
4552 may be bigger than SIZE if the object contains a variable length field.
4554 SIZE is important for structure constructors,
4555 since trailing members may have been omitted from the constructor.
4556 It is also important for initialization of arrays from string constants
4557 since the full length of the string constant might not be wanted.
4558 It is also needed for initialization of unions, where the initializer's
4559 type is just one member, and that may not be as long as the union.
4561 There a case in which we would fail to output exactly SIZE bytes:
4562 for a structure constructor that wants to produce more than SIZE bytes.
4563 But such constructors will never be generated for any possible input.
4565 ALIGN is the alignment of the data in bits. */
4567 static unsigned HOST_WIDE_INT
4568 output_constant (tree exp, unsigned HOST_WIDE_INT size, unsigned int align)
4570 enum tree_code code;
4571 unsigned HOST_WIDE_INT thissize;
4573 if (size == 0 || flag_syntax_only)
4574 return size;
4576 /* See if we're trying to initialize a pointer in a non-default mode
4577 to the address of some declaration somewhere. If the target says
4578 the mode is valid for pointers, assume the target has a way of
4579 resolving it. */
4580 if (TREE_CODE (exp) == NOP_EXPR
4581 && POINTER_TYPE_P (TREE_TYPE (exp))
4582 && targetm.addr_space.valid_pointer_mode
4583 (TYPE_MODE (TREE_TYPE (exp)),
4584 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
4586 tree saved_type = TREE_TYPE (exp);
4588 /* Peel off any intermediate conversions-to-pointer for valid
4589 pointer modes. */
4590 while (TREE_CODE (exp) == NOP_EXPR
4591 && POINTER_TYPE_P (TREE_TYPE (exp))
4592 && targetm.addr_space.valid_pointer_mode
4593 (TYPE_MODE (TREE_TYPE (exp)),
4594 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
4595 exp = TREE_OPERAND (exp, 0);
4597 /* If what we're left with is the address of something, we can
4598 convert the address to the final type and output it that
4599 way. */
4600 if (TREE_CODE (exp) == ADDR_EXPR)
4601 exp = build1 (ADDR_EXPR, saved_type, TREE_OPERAND (exp, 0));
4602 /* Likewise for constant ints. */
4603 else if (TREE_CODE (exp) == INTEGER_CST)
4604 exp = wide_int_to_tree (saved_type, exp);
4608 /* Eliminate any conversions since we'll be outputting the underlying
4609 constant. */
4610 while (CONVERT_EXPR_P (exp)
4611 || TREE_CODE (exp) == NON_LVALUE_EXPR
4612 || TREE_CODE (exp) == VIEW_CONVERT_EXPR)
4614 HOST_WIDE_INT type_size = int_size_in_bytes (TREE_TYPE (exp));
4615 HOST_WIDE_INT op_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (exp, 0)));
4617 /* Make sure eliminating the conversion is really a no-op, except with
4618 VIEW_CONVERT_EXPRs to allow for wild Ada unchecked conversions and
4619 union types to allow for Ada unchecked unions. */
4620 if (type_size > op_size
4621 && TREE_CODE (exp) != VIEW_CONVERT_EXPR
4622 && TREE_CODE (TREE_TYPE (exp)) != UNION_TYPE)
4623 /* Keep the conversion. */
4624 break;
4625 else
4626 exp = TREE_OPERAND (exp, 0);
4629 code = TREE_CODE (TREE_TYPE (exp));
4630 thissize = int_size_in_bytes (TREE_TYPE (exp));
4632 /* Allow a constructor with no elements for any data type.
4633 This means to fill the space with zeros. */
4634 if (TREE_CODE (exp) == CONSTRUCTOR
4635 && vec_safe_is_empty (CONSTRUCTOR_ELTS (exp)))
4637 assemble_zeros (size);
4638 return size;
4641 if (TREE_CODE (exp) == FDESC_EXPR)
4643 #ifdef ASM_OUTPUT_FDESC
4644 HOST_WIDE_INT part = tree_to_shwi (TREE_OPERAND (exp, 1));
4645 tree decl = TREE_OPERAND (exp, 0);
4646 ASM_OUTPUT_FDESC (asm_out_file, decl, part);
4647 #else
4648 gcc_unreachable ();
4649 #endif
4650 return size;
4653 /* Now output the underlying data. If we've handling the padding, return.
4654 Otherwise, break and ensure SIZE is the size written. */
4655 switch (code)
4657 case BOOLEAN_TYPE:
4658 case INTEGER_TYPE:
4659 case ENUMERAL_TYPE:
4660 case POINTER_TYPE:
4661 case REFERENCE_TYPE:
4662 case OFFSET_TYPE:
4663 case FIXED_POINT_TYPE:
4664 case NULLPTR_TYPE:
4665 if (! assemble_integer (expand_expr (exp, NULL_RTX, VOIDmode,
4666 EXPAND_INITIALIZER),
4667 MIN (size, thissize), align, 0))
4668 error ("initializer for integer/fixed-point value is too complicated");
4669 break;
4671 case REAL_TYPE:
4672 if (TREE_CODE (exp) != REAL_CST)
4673 error ("initializer for floating value is not a floating constant");
4674 else
4675 assemble_real (TREE_REAL_CST (exp), TYPE_MODE (TREE_TYPE (exp)), align);
4676 break;
4678 case COMPLEX_TYPE:
4679 output_constant (TREE_REALPART (exp), thissize / 2, align);
4680 output_constant (TREE_IMAGPART (exp), thissize / 2,
4681 min_align (align, BITS_PER_UNIT * (thissize / 2)));
4682 break;
4684 case ARRAY_TYPE:
4685 case VECTOR_TYPE:
4686 switch (TREE_CODE (exp))
4688 case CONSTRUCTOR:
4689 return output_constructor (exp, size, align, NULL);
4690 case STRING_CST:
4691 thissize
4692 = MIN ((unsigned HOST_WIDE_INT)TREE_STRING_LENGTH (exp), size);
4693 assemble_string (TREE_STRING_POINTER (exp), thissize);
4694 break;
4695 case VECTOR_CST:
4697 enum machine_mode inner = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
4698 unsigned int nalign = MIN (align, GET_MODE_ALIGNMENT (inner));
4699 int elt_size = GET_MODE_SIZE (inner);
4700 output_constant (VECTOR_CST_ELT (exp, 0), elt_size, align);
4701 thissize = elt_size;
4702 for (unsigned int i = 1; i < VECTOR_CST_NELTS (exp); i++)
4704 output_constant (VECTOR_CST_ELT (exp, i), elt_size, nalign);
4705 thissize += elt_size;
4707 break;
4709 default:
4710 gcc_unreachable ();
4712 break;
4714 case RECORD_TYPE:
4715 case UNION_TYPE:
4716 gcc_assert (TREE_CODE (exp) == CONSTRUCTOR);
4717 return output_constructor (exp, size, align, NULL);
4719 case ERROR_MARK:
4720 return 0;
4722 default:
4723 gcc_unreachable ();
4726 if (size > thissize)
4727 assemble_zeros (size - thissize);
4729 return size;
4733 /* Subroutine of output_constructor, used for computing the size of
4734 arrays of unspecified length. VAL must be a CONSTRUCTOR of an array
4735 type with an unspecified upper bound. */
4737 static unsigned HOST_WIDE_INT
4738 array_size_for_constructor (tree val)
4740 tree max_index;
4741 unsigned HOST_WIDE_INT cnt;
4742 tree index, value, tmp;
4743 offset_int i;
4745 /* This code used to attempt to handle string constants that are not
4746 arrays of single-bytes, but nothing else does, so there's no point in
4747 doing it here. */
4748 if (TREE_CODE (val) == STRING_CST)
4749 return TREE_STRING_LENGTH (val);
4751 max_index = NULL_TREE;
4752 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (val), cnt, index, value)
4754 if (TREE_CODE (index) == RANGE_EXPR)
4755 index = TREE_OPERAND (index, 1);
4756 if (max_index == NULL_TREE || tree_int_cst_lt (max_index, index))
4757 max_index = index;
4760 if (max_index == NULL_TREE)
4761 return 0;
4763 /* Compute the total number of array elements. */
4764 tmp = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (val)));
4765 i = wi::to_offset (max_index) - wi::to_offset (tmp) + 1;
4767 /* Multiply by the array element unit size to find number of bytes. */
4768 i *= wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (val))));
4770 gcc_assert (wi::fits_uhwi_p (i));
4771 return i.to_uhwi ();
4774 /* Other datastructures + helpers for output_constructor. */
4776 /* output_constructor local state to support interaction with helpers. */
4778 typedef struct {
4780 /* Received arguments. */
4781 tree exp; /* Constructor expression. */
4782 tree type; /* Type of constructor expression. */
4783 unsigned HOST_WIDE_INT size; /* # bytes to output - pad if necessary. */
4784 unsigned int align; /* Known initial alignment. */
4785 tree min_index; /* Lower bound if specified for an array. */
4787 /* Output processing state. */
4788 HOST_WIDE_INT total_bytes; /* # bytes output so far / current position. */
4789 int byte; /* Part of a bitfield byte yet to be output. */
4790 int last_relative_index; /* Implicit or explicit index of the last
4791 array element output within a bitfield. */
4792 bool byte_buffer_in_use; /* Whether BYTE is in use. */
4794 /* Current element. */
4795 tree field; /* Current field decl in a record. */
4796 tree val; /* Current element value. */
4797 tree index; /* Current element index. */
4799 } oc_local_state;
4801 /* Helper for output_constructor. From the current LOCAL state, output a
4802 RANGE_EXPR element. */
4804 static void
4805 output_constructor_array_range (oc_local_state *local)
4807 unsigned HOST_WIDE_INT fieldsize
4808 = int_size_in_bytes (TREE_TYPE (local->type));
4810 HOST_WIDE_INT lo_index
4811 = tree_to_shwi (TREE_OPERAND (local->index, 0));
4812 HOST_WIDE_INT hi_index
4813 = tree_to_shwi (TREE_OPERAND (local->index, 1));
4814 HOST_WIDE_INT index;
4816 unsigned int align2
4817 = min_align (local->align, fieldsize * BITS_PER_UNIT);
4819 for (index = lo_index; index <= hi_index; index++)
4821 /* Output the element's initial value. */
4822 if (local->val == NULL_TREE)
4823 assemble_zeros (fieldsize);
4824 else
4825 fieldsize = output_constant (local->val, fieldsize, align2);
4827 /* Count its size. */
4828 local->total_bytes += fieldsize;
4832 /* Helper for output_constructor. From the current LOCAL state, output a
4833 field element that is not true bitfield or part of an outer one. */
4835 static void
4836 output_constructor_regular_field (oc_local_state *local)
4838 /* Field size and position. Since this structure is static, we know the
4839 positions are constant. */
4840 unsigned HOST_WIDE_INT fieldsize;
4841 HOST_WIDE_INT fieldpos;
4843 unsigned int align2;
4845 if (local->index != NULL_TREE)
4847 /* Perform the index calculation in modulo arithmetic but
4848 sign-extend the result because Ada has negative DECL_FIELD_OFFSETs
4849 but we are using an unsigned sizetype. */
4850 unsigned prec = TYPE_PRECISION (sizetype);
4851 offset_int idx = wi::sext (wi::to_offset (local->index)
4852 - wi::to_offset (local->min_index), prec);
4853 fieldpos = (idx * wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (local->val))))
4854 .to_short_addr ();
4856 else if (local->field != NULL_TREE)
4857 fieldpos = int_byte_position (local->field);
4858 else
4859 fieldpos = 0;
4861 /* Output any buffered-up bit-fields preceding this element. */
4862 if (local->byte_buffer_in_use)
4864 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
4865 local->total_bytes++;
4866 local->byte_buffer_in_use = false;
4869 /* Advance to offset of this element.
4870 Note no alignment needed in an array, since that is guaranteed
4871 if each element has the proper size. */
4872 if ((local->field != NULL_TREE || local->index != NULL_TREE)
4873 && fieldpos > local->total_bytes)
4875 assemble_zeros (fieldpos - local->total_bytes);
4876 local->total_bytes = fieldpos;
4879 /* Find the alignment of this element. */
4880 align2 = min_align (local->align, BITS_PER_UNIT * fieldpos);
4882 /* Determine size this element should occupy. */
4883 if (local->field)
4885 fieldsize = 0;
4887 /* If this is an array with an unspecified upper bound,
4888 the initializer determines the size. */
4889 /* ??? This ought to only checked if DECL_SIZE_UNIT is NULL,
4890 but we cannot do this until the deprecated support for
4891 initializing zero-length array members is removed. */
4892 if (TREE_CODE (TREE_TYPE (local->field)) == ARRAY_TYPE
4893 && TYPE_DOMAIN (TREE_TYPE (local->field))
4894 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (local->field))))
4896 fieldsize = array_size_for_constructor (local->val);
4897 /* Given a non-empty initialization, this field had
4898 better be last. */
4899 gcc_assert (!fieldsize || !DECL_CHAIN (local->field));
4901 else
4902 fieldsize = tree_to_uhwi (DECL_SIZE_UNIT (local->field));
4904 else
4905 fieldsize = int_size_in_bytes (TREE_TYPE (local->type));
4907 /* Output the element's initial value. */
4908 if (local->val == NULL_TREE)
4909 assemble_zeros (fieldsize);
4910 else
4911 fieldsize = output_constant (local->val, fieldsize, align2);
4913 /* Count its size. */
4914 local->total_bytes += fieldsize;
4917 /* Helper for output_constructor. From the LOCAL state, output an element
4918 that is a true bitfield or part of an outer one. BIT_OFFSET is the offset
4919 from the start of a possibly ongoing outer byte buffer. */
4921 static void
4922 output_constructor_bitfield (oc_local_state *local, unsigned int bit_offset)
4924 /* Bit size of this element. */
4925 HOST_WIDE_INT ebitsize
4926 = (local->field
4927 ? tree_to_uhwi (DECL_SIZE (local->field))
4928 : tree_to_uhwi (TYPE_SIZE (TREE_TYPE (local->type))));
4930 /* Relative index of this element if this is an array component. */
4931 HOST_WIDE_INT relative_index
4932 = (!local->field
4933 ? (local->index
4934 ? (tree_to_shwi (local->index)
4935 - tree_to_shwi (local->min_index))
4936 : local->last_relative_index + 1)
4937 : 0);
4939 /* Bit position of this element from the start of the containing
4940 constructor. */
4941 HOST_WIDE_INT constructor_relative_ebitpos
4942 = (local->field
4943 ? int_bit_position (local->field)
4944 : ebitsize * relative_index);
4946 /* Bit position of this element from the start of a possibly ongoing
4947 outer byte buffer. */
4948 HOST_WIDE_INT byte_relative_ebitpos
4949 = bit_offset + constructor_relative_ebitpos;
4951 /* From the start of a possibly ongoing outer byte buffer, offsets to
4952 the first bit of this element and to the first bit past the end of
4953 this element. */
4954 HOST_WIDE_INT next_offset = byte_relative_ebitpos;
4955 HOST_WIDE_INT end_offset = byte_relative_ebitpos + ebitsize;
4957 local->last_relative_index = relative_index;
4959 if (local->val == NULL_TREE)
4960 local->val = integer_zero_node;
4962 while (TREE_CODE (local->val) == VIEW_CONVERT_EXPR
4963 || TREE_CODE (local->val) == NON_LVALUE_EXPR)
4964 local->val = TREE_OPERAND (local->val, 0);
4966 if (TREE_CODE (local->val) != INTEGER_CST
4967 && TREE_CODE (local->val) != CONSTRUCTOR)
4969 error ("invalid initial value for member %qE", DECL_NAME (local->field));
4970 return;
4973 /* If this field does not start in this (or next) byte, skip some bytes. */
4974 if (next_offset / BITS_PER_UNIT != local->total_bytes)
4976 /* Output remnant of any bit field in previous bytes. */
4977 if (local->byte_buffer_in_use)
4979 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
4980 local->total_bytes++;
4981 local->byte_buffer_in_use = false;
4984 /* If still not at proper byte, advance to there. */
4985 if (next_offset / BITS_PER_UNIT != local->total_bytes)
4987 gcc_assert (next_offset / BITS_PER_UNIT >= local->total_bytes);
4988 assemble_zeros (next_offset / BITS_PER_UNIT - local->total_bytes);
4989 local->total_bytes = next_offset / BITS_PER_UNIT;
4993 /* Set up the buffer if necessary. */
4994 if (!local->byte_buffer_in_use)
4996 local->byte = 0;
4997 if (ebitsize > 0)
4998 local->byte_buffer_in_use = true;
5001 /* If this is nested constructor, recurse passing the bit offset and the
5002 pending data, then retrieve the new pending data afterwards. */
5003 if (TREE_CODE (local->val) == CONSTRUCTOR)
5005 oc_outer_state temp_state;
5006 temp_state.bit_offset = next_offset % BITS_PER_UNIT;
5007 temp_state.byte = local->byte;
5008 local->total_bytes
5009 += output_constructor (local->val, 0, 0, &temp_state);
5010 local->byte = temp_state.byte;
5011 return;
5014 /* Otherwise, we must split the element into pieces that fall within
5015 separate bytes, and combine each byte with previous or following
5016 bit-fields. */
5017 while (next_offset < end_offset)
5019 int this_time;
5020 int shift;
5021 HOST_WIDE_INT value;
5022 HOST_WIDE_INT next_byte = next_offset / BITS_PER_UNIT;
5023 HOST_WIDE_INT next_bit = next_offset % BITS_PER_UNIT;
5025 /* Advance from byte to byte within this element when necessary. */
5026 while (next_byte != local->total_bytes)
5028 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5029 local->total_bytes++;
5030 local->byte = 0;
5033 /* Number of bits we can process at once (all part of the same byte). */
5034 this_time = MIN (end_offset - next_offset, BITS_PER_UNIT - next_bit);
5035 if (BYTES_BIG_ENDIAN)
5037 /* On big-endian machine, take the most significant bits (of the
5038 bits that are significant) first and put them into bytes from
5039 the most significant end. */
5040 shift = end_offset - next_offset - this_time;
5042 /* Don't try to take a bunch of bits that cross
5043 the word boundary in the INTEGER_CST. We can
5044 only select bits from one element. */
5045 if ((shift / HOST_BITS_PER_WIDE_INT)
5046 != ((shift + this_time - 1) / HOST_BITS_PER_WIDE_INT))
5048 const int end = shift + this_time - 1;
5049 shift = end & -HOST_BITS_PER_WIDE_INT;
5050 this_time = end - shift + 1;
5053 /* Now get the bits from the appropriate constant word. */
5054 value = TREE_INT_CST_ELT (local->val, shift / HOST_BITS_PER_WIDE_INT);
5055 shift = shift & (HOST_BITS_PER_WIDE_INT - 1);
5057 /* Get the result. This works only when:
5058 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
5059 local->byte |= (((value >> shift)
5060 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
5061 << (BITS_PER_UNIT - this_time - next_bit));
5063 else
5065 /* On little-endian machines, take the least significant bits of
5066 the value first and pack them starting at the least significant
5067 bits of the bytes. */
5068 shift = next_offset - byte_relative_ebitpos;
5070 /* Don't try to take a bunch of bits that cross
5071 the word boundary in the INTEGER_CST. We can
5072 only select bits from one element. */
5073 if ((shift / HOST_BITS_PER_WIDE_INT)
5074 != ((shift + this_time - 1) / HOST_BITS_PER_WIDE_INT))
5075 this_time
5076 = HOST_BITS_PER_WIDE_INT - (shift & (HOST_BITS_PER_WIDE_INT - 1));
5078 /* Now get the bits from the appropriate constant word. */
5079 value = TREE_INT_CST_ELT (local->val, shift / HOST_BITS_PER_WIDE_INT);
5080 shift = shift & (HOST_BITS_PER_WIDE_INT - 1);
5082 /* Get the result. This works only when:
5083 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
5084 local->byte |= (((value >> shift)
5085 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
5086 << next_bit);
5089 next_offset += this_time;
5090 local->byte_buffer_in_use = true;
5094 /* Subroutine of output_constant, used for CONSTRUCTORs (aggregate constants).
5095 Generate at least SIZE bytes, padding if necessary. OUTER designates the
5096 caller output state of relevance in recursive invocations. */
5098 static unsigned HOST_WIDE_INT
5099 output_constructor (tree exp, unsigned HOST_WIDE_INT size,
5100 unsigned int align, oc_outer_state *outer)
5102 unsigned HOST_WIDE_INT cnt;
5103 constructor_elt *ce;
5105 oc_local_state local;
5107 /* Setup our local state to communicate with helpers. */
5108 local.exp = exp;
5109 local.type = TREE_TYPE (exp);
5110 local.size = size;
5111 local.align = align;
5112 if (TREE_CODE (local.type) == ARRAY_TYPE && TYPE_DOMAIN (local.type))
5113 local.min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (local.type));
5114 else
5115 local.min_index = NULL_TREE;
5117 local.total_bytes = 0;
5118 local.byte_buffer_in_use = outer != NULL;
5119 local.byte = outer ? outer->byte : 0;
5120 local.last_relative_index = -1;
5122 gcc_assert (HOST_BITS_PER_WIDE_INT >= BITS_PER_UNIT);
5124 /* As CE goes through the elements of the constant, FIELD goes through the
5125 structure fields if the constant is a structure. If the constant is a
5126 union, we override this by getting the field from the TREE_LIST element.
5127 But the constant could also be an array. Then FIELD is zero.
5129 There is always a maximum of one element in the chain LINK for unions
5130 (even if the initializer in a source program incorrectly contains
5131 more one). */
5133 if (TREE_CODE (local.type) == RECORD_TYPE)
5134 local.field = TYPE_FIELDS (local.type);
5135 else
5136 local.field = NULL_TREE;
5138 for (cnt = 0;
5139 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), cnt, &ce);
5140 cnt++, local.field = local.field ? DECL_CHAIN (local.field) : 0)
5142 local.val = ce->value;
5143 local.index = NULL_TREE;
5145 /* The element in a union constructor specifies the proper field
5146 or index. */
5147 if (RECORD_OR_UNION_TYPE_P (local.type) && ce->index != NULL_TREE)
5148 local.field = ce->index;
5150 else if (TREE_CODE (local.type) == ARRAY_TYPE)
5151 local.index = ce->index;
5153 if (local.field && flag_verbose_asm)
5154 fprintf (asm_out_file, "%s %s:\n",
5155 ASM_COMMENT_START,
5156 DECL_NAME (local.field)
5157 ? IDENTIFIER_POINTER (DECL_NAME (local.field))
5158 : "<anonymous>");
5160 /* Eliminate the marker that makes a cast not be an lvalue. */
5161 if (local.val != NULL_TREE)
5162 STRIP_NOPS (local.val);
5164 /* Output the current element, using the appropriate helper ... */
5166 /* For an array slice not part of an outer bitfield. */
5167 if (!outer
5168 && local.index != NULL_TREE
5169 && TREE_CODE (local.index) == RANGE_EXPR)
5170 output_constructor_array_range (&local);
5172 /* For a field that is neither a true bitfield nor part of an outer one,
5173 known to be at least byte aligned and multiple-of-bytes long. */
5174 else if (!outer
5175 && (local.field == NULL_TREE
5176 || !CONSTRUCTOR_BITFIELD_P (local.field)))
5177 output_constructor_regular_field (&local);
5179 /* For a true bitfield or part of an outer one. Only INTEGER_CSTs are
5180 supported for scalar fields, so we may need to convert first. */
5181 else
5183 if (TREE_CODE (local.val) == REAL_CST)
5184 local.val
5185 = fold_unary (VIEW_CONVERT_EXPR,
5186 build_nonstandard_integer_type
5187 (TYPE_PRECISION (TREE_TYPE (local.val)), 0),
5188 local.val);
5189 output_constructor_bitfield (&local, outer ? outer->bit_offset : 0);
5193 /* If we are not at toplevel, save the pending data for our caller.
5194 Otherwise output the pending data and padding zeros as needed. */
5195 if (outer)
5196 outer->byte = local.byte;
5197 else
5199 if (local.byte_buffer_in_use)
5201 assemble_integer (GEN_INT (local.byte), 1, BITS_PER_UNIT, 1);
5202 local.total_bytes++;
5205 if ((unsigned HOST_WIDE_INT)local.total_bytes < local.size)
5207 assemble_zeros (local.size - local.total_bytes);
5208 local.total_bytes = local.size;
5212 return local.total_bytes;
5215 /* Mark DECL as weak. */
5217 static void
5218 mark_weak (tree decl)
5220 DECL_WEAK (decl) = 1;
5222 if (DECL_RTL_SET_P (decl)
5223 && MEM_P (DECL_RTL (decl))
5224 && XEXP (DECL_RTL (decl), 0)
5225 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == SYMBOL_REF)
5226 SYMBOL_REF_WEAK (XEXP (DECL_RTL (decl), 0)) = 1;
5229 /* Merge weak status between NEWDECL and OLDDECL. */
5231 void
5232 merge_weak (tree newdecl, tree olddecl)
5234 if (DECL_WEAK (newdecl) == DECL_WEAK (olddecl))
5236 if (DECL_WEAK (newdecl) && TARGET_SUPPORTS_WEAK)
5238 tree *pwd;
5239 /* We put the NEWDECL on the weak_decls list at some point
5240 and OLDDECL as well. Keep just OLDDECL on the list. */
5241 for (pwd = &weak_decls; *pwd; pwd = &TREE_CHAIN (*pwd))
5242 if (TREE_VALUE (*pwd) == newdecl)
5244 *pwd = TREE_CHAIN (*pwd);
5245 break;
5248 return;
5251 if (DECL_WEAK (newdecl))
5253 tree wd;
5255 /* NEWDECL is weak, but OLDDECL is not. */
5257 /* If we already output the OLDDECL, we're in trouble; we can't
5258 go back and make it weak. This should never happen in
5259 unit-at-a-time compilation. */
5260 gcc_assert (!TREE_ASM_WRITTEN (olddecl));
5262 /* If we've already generated rtl referencing OLDDECL, we may
5263 have done so in a way that will not function properly with
5264 a weak symbol. Again in unit-at-a-time this should be
5265 impossible. */
5266 gcc_assert (!TREE_USED (olddecl)
5267 || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (olddecl)));
5269 if (TARGET_SUPPORTS_WEAK)
5271 /* We put the NEWDECL on the weak_decls list at some point.
5272 Replace it with the OLDDECL. */
5273 for (wd = weak_decls; wd; wd = TREE_CHAIN (wd))
5274 if (TREE_VALUE (wd) == newdecl)
5276 TREE_VALUE (wd) = olddecl;
5277 break;
5279 /* We may not find the entry on the list. If NEWDECL is a
5280 weak alias, then we will have already called
5281 globalize_decl to remove the entry; in that case, we do
5282 not need to do anything. */
5285 /* Make the OLDDECL weak; it's OLDDECL that we'll be keeping. */
5286 mark_weak (olddecl);
5288 else
5289 /* OLDDECL was weak, but NEWDECL was not explicitly marked as
5290 weak. Just update NEWDECL to indicate that it's weak too. */
5291 mark_weak (newdecl);
5294 /* Declare DECL to be a weak symbol. */
5296 void
5297 declare_weak (tree decl)
5299 gcc_assert (TREE_CODE (decl) != FUNCTION_DECL || !TREE_ASM_WRITTEN (decl));
5300 if (! TREE_PUBLIC (decl))
5301 error ("weak declaration of %q+D must be public", decl);
5302 else if (!TARGET_SUPPORTS_WEAK)
5303 warning (0, "weak declaration of %q+D not supported", decl);
5305 mark_weak (decl);
5306 if (!lookup_attribute ("weak", DECL_ATTRIBUTES (decl)))
5307 DECL_ATTRIBUTES (decl)
5308 = tree_cons (get_identifier ("weak"), NULL, DECL_ATTRIBUTES (decl));
5311 static void
5312 weak_finish_1 (tree decl)
5314 #if defined (ASM_WEAKEN_DECL) || defined (ASM_WEAKEN_LABEL)
5315 const char *const name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5316 #endif
5318 if (! TREE_USED (decl))
5319 return;
5321 #ifdef ASM_WEAKEN_DECL
5322 ASM_WEAKEN_DECL (asm_out_file, decl, name, NULL);
5323 #else
5324 #ifdef ASM_WEAKEN_LABEL
5325 ASM_WEAKEN_LABEL (asm_out_file, name);
5326 #else
5327 #ifdef ASM_OUTPUT_WEAK_ALIAS
5329 static bool warn_once = 0;
5330 if (! warn_once)
5332 warning (0, "only weak aliases are supported in this configuration");
5333 warn_once = 1;
5335 return;
5337 #endif
5338 #endif
5339 #endif
5342 /* Fiven an assembly name, find the decl it is associated with. */
5343 static tree
5344 find_decl (tree target)
5346 symtab_node *node = symtab_node_for_asm (target);
5347 if (node)
5348 return node->decl;
5349 return NULL_TREE;
5352 /* This TREE_LIST contains weakref targets. */
5354 static GTY(()) tree weakref_targets;
5356 /* Emit any pending weak declarations. */
5358 void
5359 weak_finish (void)
5361 tree t;
5363 for (t = weakref_targets; t; t = TREE_CHAIN (t))
5365 tree alias_decl = TREE_PURPOSE (t);
5366 tree target = ultimate_transparent_alias_target (&TREE_VALUE (t));
5368 if (! TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (alias_decl)))
5369 /* Remove alias_decl from the weak list, but leave entries for
5370 the target alone. */
5371 target = NULL_TREE;
5372 #ifndef ASM_OUTPUT_WEAKREF
5373 else if (! TREE_SYMBOL_REFERENCED (target))
5375 /* Use ASM_WEAKEN_LABEL only if ASM_WEAKEN_DECL is not
5376 defined, otherwise we and weak_finish_1 would use
5377 different macros. */
5378 # if defined ASM_WEAKEN_LABEL && ! defined ASM_WEAKEN_DECL
5379 ASM_WEAKEN_LABEL (asm_out_file, IDENTIFIER_POINTER (target));
5380 # else
5381 tree decl = find_decl (target);
5383 if (! decl)
5385 decl = build_decl (DECL_SOURCE_LOCATION (alias_decl),
5386 TREE_CODE (alias_decl), target,
5387 TREE_TYPE (alias_decl));
5389 DECL_EXTERNAL (decl) = 1;
5390 TREE_PUBLIC (decl) = 1;
5391 DECL_ARTIFICIAL (decl) = 1;
5392 TREE_NOTHROW (decl) = TREE_NOTHROW (alias_decl);
5393 TREE_USED (decl) = 1;
5396 weak_finish_1 (decl);
5397 # endif
5399 #endif
5402 tree *p;
5403 tree t2;
5405 /* Remove the alias and the target from the pending weak list
5406 so that we do not emit any .weak directives for the former,
5407 nor multiple .weak directives for the latter. */
5408 for (p = &weak_decls; (t2 = *p) ; )
5410 if (TREE_VALUE (t2) == alias_decl
5411 || target == DECL_ASSEMBLER_NAME (TREE_VALUE (t2)))
5412 *p = TREE_CHAIN (t2);
5413 else
5414 p = &TREE_CHAIN (t2);
5417 /* Remove other weakrefs to the same target, to speed things up. */
5418 for (p = &TREE_CHAIN (t); (t2 = *p) ; )
5420 if (target == ultimate_transparent_alias_target (&TREE_VALUE (t2)))
5421 *p = TREE_CHAIN (t2);
5422 else
5423 p = &TREE_CHAIN (t2);
5428 for (t = weak_decls; t; t = TREE_CHAIN (t))
5430 tree decl = TREE_VALUE (t);
5432 weak_finish_1 (decl);
5436 /* Emit the assembly bits to indicate that DECL is globally visible. */
5438 static void
5439 globalize_decl (tree decl)
5442 #if defined (ASM_WEAKEN_LABEL) || defined (ASM_WEAKEN_DECL)
5443 if (DECL_WEAK (decl))
5445 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
5446 tree *p, t;
5448 #ifdef ASM_WEAKEN_DECL
5449 ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
5450 #else
5451 ASM_WEAKEN_LABEL (asm_out_file, name);
5452 #endif
5454 /* Remove this function from the pending weak list so that
5455 we do not emit multiple .weak directives for it. */
5456 for (p = &weak_decls; (t = *p) ; )
5458 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
5459 *p = TREE_CHAIN (t);
5460 else
5461 p = &TREE_CHAIN (t);
5464 /* Remove weakrefs to the same target from the pending weakref
5465 list, for the same reason. */
5466 for (p = &weakref_targets; (t = *p) ; )
5468 if (DECL_ASSEMBLER_NAME (decl)
5469 == ultimate_transparent_alias_target (&TREE_VALUE (t)))
5470 *p = TREE_CHAIN (t);
5471 else
5472 p = &TREE_CHAIN (t);
5475 return;
5477 #endif
5479 targetm.asm_out.globalize_decl_name (asm_out_file, decl);
5482 vec<alias_pair, va_gc> *alias_pairs;
5484 /* Output the assembler code for a define (equate) using ASM_OUTPUT_DEF
5485 or ASM_OUTPUT_DEF_FROM_DECLS. The function defines the symbol whose
5486 tree node is DECL to have the value of the tree node TARGET. */
5488 void
5489 do_assemble_alias (tree decl, tree target)
5491 /* Emulated TLS had better not get this var. */
5492 gcc_assert (!(!targetm.have_tls
5493 && TREE_CODE (decl) == VAR_DECL
5494 && DECL_THREAD_LOCAL_P (decl)));
5496 if (TREE_ASM_WRITTEN (decl))
5497 return;
5499 /* We must force creation of DECL_RTL for debug info generation, even though
5500 we don't use it here. */
5501 make_decl_rtl (decl);
5503 TREE_ASM_WRITTEN (decl) = 1;
5504 TREE_ASM_WRITTEN (DECL_ASSEMBLER_NAME (decl)) = 1;
5506 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
5508 ultimate_transparent_alias_target (&target);
5510 if (!TREE_SYMBOL_REFERENCED (target))
5511 weakref_targets = tree_cons (decl, target, weakref_targets);
5513 #ifdef ASM_OUTPUT_WEAKREF
5514 ASM_OUTPUT_WEAKREF (asm_out_file, decl,
5515 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
5516 IDENTIFIER_POINTER (target));
5517 #else
5518 if (!TARGET_SUPPORTS_WEAK)
5520 error_at (DECL_SOURCE_LOCATION (decl),
5521 "weakref is not supported in this configuration");
5522 return;
5524 #endif
5525 return;
5528 #ifdef ASM_OUTPUT_DEF
5529 /* Make name accessible from other files, if appropriate. */
5531 if (TREE_PUBLIC (decl))
5533 globalize_decl (decl);
5534 maybe_assemble_visibility (decl);
5536 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
5538 #if defined (ASM_OUTPUT_TYPE_DIRECTIVE)
5539 if (targetm.has_ifunc_p ())
5540 ASM_OUTPUT_TYPE_DIRECTIVE
5541 (asm_out_file, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
5542 IFUNC_ASM_TYPE);
5543 else
5544 #endif
5545 error_at (DECL_SOURCE_LOCATION (decl),
5546 "ifunc is not supported on this target");
5549 # ifdef ASM_OUTPUT_DEF_FROM_DECLS
5550 ASM_OUTPUT_DEF_FROM_DECLS (asm_out_file, decl, target);
5551 # else
5552 ASM_OUTPUT_DEF (asm_out_file,
5553 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
5554 IDENTIFIER_POINTER (target));
5555 # endif
5556 #elif defined (ASM_OUTPUT_WEAK_ALIAS) || defined (ASM_WEAKEN_DECL)
5558 const char *name;
5559 tree *p, t;
5561 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5562 # ifdef ASM_WEAKEN_DECL
5563 ASM_WEAKEN_DECL (asm_out_file, decl, name, IDENTIFIER_POINTER (target));
5564 # else
5565 ASM_OUTPUT_WEAK_ALIAS (asm_out_file, name, IDENTIFIER_POINTER (target));
5566 # endif
5567 /* Remove this function from the pending weak list so that
5568 we do not emit multiple .weak directives for it. */
5569 for (p = &weak_decls; (t = *p) ; )
5570 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
5571 *p = TREE_CHAIN (t);
5572 else
5573 p = &TREE_CHAIN (t);
5575 /* Remove weakrefs to the same target from the pending weakref
5576 list, for the same reason. */
5577 for (p = &weakref_targets; (t = *p) ; )
5579 if (DECL_ASSEMBLER_NAME (decl)
5580 == ultimate_transparent_alias_target (&TREE_VALUE (t)))
5581 *p = TREE_CHAIN (t);
5582 else
5583 p = &TREE_CHAIN (t);
5586 #endif
5589 /* Emit an assembler directive to make the symbol for DECL an alias to
5590 the symbol for TARGET. */
5592 void
5593 assemble_alias (tree decl, tree target)
5595 tree target_decl;
5597 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
5599 tree alias = DECL_ASSEMBLER_NAME (decl);
5601 ultimate_transparent_alias_target (&target);
5603 if (alias == target)
5604 error ("weakref %q+D ultimately targets itself", decl);
5605 if (TREE_PUBLIC (decl))
5606 error ("weakref %q+D must have static linkage", decl);
5608 else
5610 #if !defined (ASM_OUTPUT_DEF)
5611 # if !defined(ASM_OUTPUT_WEAK_ALIAS) && !defined (ASM_WEAKEN_DECL)
5612 error_at (DECL_SOURCE_LOCATION (decl),
5613 "alias definitions not supported in this configuration");
5614 TREE_ASM_WRITTEN (decl) = 1;
5615 return;
5616 # else
5617 if (!DECL_WEAK (decl))
5619 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
5620 error_at (DECL_SOURCE_LOCATION (decl),
5621 "ifunc is not supported in this configuration");
5622 else
5623 error_at (DECL_SOURCE_LOCATION (decl),
5624 "only weak aliases are supported in this configuration");
5625 TREE_ASM_WRITTEN (decl) = 1;
5626 return;
5628 # endif
5629 #endif
5631 TREE_USED (decl) = 1;
5633 /* Allow aliases to aliases. */
5634 if (TREE_CODE (decl) == FUNCTION_DECL)
5635 cgraph_get_create_node (decl)->alias = true;
5636 else
5637 varpool_node_for_decl (decl)->alias = true;
5639 /* If the target has already been emitted, we don't have to queue the
5640 alias. This saves a tad of memory. */
5641 if (cgraph_global_info_ready)
5642 target_decl = find_decl (target);
5643 else
5644 target_decl= NULL;
5645 if ((target_decl && TREE_ASM_WRITTEN (target_decl))
5646 || cgraph_state >= CGRAPH_STATE_EXPANSION)
5647 do_assemble_alias (decl, target);
5648 else
5650 alias_pair p = {decl, target};
5651 vec_safe_push (alias_pairs, p);
5655 /* Record and output a table of translations from original function
5656 to its transaction aware clone. Note that tm_pure functions are
5657 considered to be their own clone. */
5659 static GTY((if_marked ("tree_map_marked_p"), param_is (struct tree_map)))
5660 htab_t tm_clone_hash;
5662 void
5663 record_tm_clone_pair (tree o, tree n)
5665 struct tree_map **slot, *h;
5667 if (tm_clone_hash == NULL)
5668 tm_clone_hash = htab_create_ggc (32, tree_map_hash, tree_map_eq, 0);
5670 h = ggc_alloc<tree_map> ();
5671 h->hash = htab_hash_pointer (o);
5672 h->base.from = o;
5673 h->to = n;
5675 slot = (struct tree_map **)
5676 htab_find_slot_with_hash (tm_clone_hash, h, h->hash, INSERT);
5677 *slot = h;
5680 tree
5681 get_tm_clone_pair (tree o)
5683 if (tm_clone_hash)
5685 struct tree_map *h, in;
5687 in.base.from = o;
5688 in.hash = htab_hash_pointer (o);
5689 h = (struct tree_map *) htab_find_with_hash (tm_clone_hash,
5690 &in, in.hash);
5691 if (h)
5692 return h->to;
5694 return NULL_TREE;
5697 typedef struct tm_alias_pair
5699 unsigned int uid;
5700 tree from;
5701 tree to;
5702 } tm_alias_pair;
5705 /* Helper function for finish_tm_clone_pairs. Dump a hash table entry
5706 into a VEC in INFO. */
5708 static int
5709 dump_tm_clone_to_vec (void **slot, void *info)
5711 struct tree_map *map = (struct tree_map *) *slot;
5712 vec<tm_alias_pair> *tm_alias_pairs = (vec<tm_alias_pair> *) info;
5713 tm_alias_pair p = {DECL_UID (map->base.from), map->base.from, map->to};
5714 tm_alias_pairs->safe_push (p);
5715 return 1;
5718 /* Dump the actual pairs to the .tm_clone_table section. */
5720 static void
5721 dump_tm_clone_pairs (vec<tm_alias_pair> tm_alias_pairs)
5723 unsigned i;
5724 tm_alias_pair *p;
5725 bool switched = false;
5727 FOR_EACH_VEC_ELT (tm_alias_pairs, i, p)
5729 tree src = p->from;
5730 tree dst = p->to;
5731 struct cgraph_node *src_n = cgraph_get_node (src);
5732 struct cgraph_node *dst_n = cgraph_get_node (dst);
5734 /* The function ipa_tm_create_version() marks the clone as needed if
5735 the original function was needed. But we also mark the clone as
5736 needed if we ever called the clone indirectly through
5737 TM_GETTMCLONE. If neither of these are true, we didn't generate
5738 a clone, and we didn't call it indirectly... no sense keeping it
5739 in the clone table. */
5740 if (!dst_n || !dst_n->definition)
5741 continue;
5743 /* This covers the case where we have optimized the original
5744 function away, and only access the transactional clone. */
5745 if (!src_n || !src_n->definition)
5746 continue;
5748 if (!switched)
5750 switch_to_section (targetm.asm_out.tm_clone_table_section ());
5751 assemble_align (POINTER_SIZE);
5752 switched = true;
5755 assemble_integer (XEXP (DECL_RTL (src), 0),
5756 POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
5757 assemble_integer (XEXP (DECL_RTL (dst), 0),
5758 POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
5762 /* Provide a default for the tm_clone_table section. */
5764 section *
5765 default_clone_table_section (void)
5767 return get_named_section (NULL, ".tm_clone_table", 3);
5770 /* Helper comparison function for qsorting by the DECL_UID stored in
5771 alias_pair->emitted_diags. */
5773 static int
5774 tm_alias_pair_cmp (const void *x, const void *y)
5776 const tm_alias_pair *p1 = (const tm_alias_pair *) x;
5777 const tm_alias_pair *p2 = (const tm_alias_pair *) y;
5778 if (p1->uid < p2->uid)
5779 return -1;
5780 if (p1->uid > p2->uid)
5781 return 1;
5782 return 0;
5785 void
5786 finish_tm_clone_pairs (void)
5788 vec<tm_alias_pair> tm_alias_pairs = vNULL;
5790 if (tm_clone_hash == NULL)
5791 return;
5793 /* We need a determenistic order for the .tm_clone_table, otherwise
5794 we will get bootstrap comparison failures, so dump the hash table
5795 to a vector, sort it, and dump the vector. */
5797 /* Dump the hashtable to a vector. */
5798 htab_traverse_noresize (tm_clone_hash, dump_tm_clone_to_vec,
5799 (void *) &tm_alias_pairs);
5800 /* Sort it. */
5801 tm_alias_pairs.qsort (tm_alias_pair_cmp);
5803 /* Dump it. */
5804 dump_tm_clone_pairs (tm_alias_pairs);
5806 htab_delete (tm_clone_hash);
5807 tm_clone_hash = NULL;
5808 tm_alias_pairs.release ();
5812 /* Emit an assembler directive to set symbol for DECL visibility to
5813 the visibility type VIS, which must not be VISIBILITY_DEFAULT. */
5815 void
5816 default_assemble_visibility (tree decl ATTRIBUTE_UNUSED,
5817 int vis ATTRIBUTE_UNUSED)
5819 #ifdef HAVE_GAS_HIDDEN
5820 static const char * const visibility_types[] = {
5821 NULL, "protected", "hidden", "internal"
5824 const char *name, *type;
5826 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5827 type = visibility_types[vis];
5829 fprintf (asm_out_file, "\t.%s\t", type);
5830 assemble_name (asm_out_file, name);
5831 fprintf (asm_out_file, "\n");
5832 #else
5833 if (!DECL_ARTIFICIAL (decl))
5834 warning (OPT_Wattributes, "visibility attribute not supported "
5835 "in this configuration; ignored");
5836 #endif
5839 /* A helper function to call assemble_visibility when needed for a decl. */
5842 maybe_assemble_visibility (tree decl)
5844 enum symbol_visibility vis = DECL_VISIBILITY (decl);
5846 if (vis != VISIBILITY_DEFAULT)
5848 targetm.asm_out.assemble_visibility (decl, vis);
5849 return 1;
5851 else
5852 return 0;
5855 /* Returns 1 if the target configuration supports defining public symbols
5856 so that one of them will be chosen at link time instead of generating a
5857 multiply-defined symbol error, whether through the use of weak symbols or
5858 a target-specific mechanism for having duplicates discarded. */
5861 supports_one_only (void)
5863 if (SUPPORTS_ONE_ONLY)
5864 return 1;
5865 return TARGET_SUPPORTS_WEAK;
5868 /* Set up DECL as a public symbol that can be defined in multiple
5869 translation units without generating a linker error. */
5871 void
5872 make_decl_one_only (tree decl, tree comdat_group)
5874 struct symtab_node *symbol;
5875 gcc_assert (TREE_CODE (decl) == VAR_DECL
5876 || TREE_CODE (decl) == FUNCTION_DECL);
5878 TREE_PUBLIC (decl) = 1;
5880 if (TREE_CODE (decl) == VAR_DECL)
5881 symbol = varpool_node_for_decl (decl);
5882 else
5883 symbol = cgraph_get_create_node (decl);
5885 if (SUPPORTS_ONE_ONLY)
5887 #ifdef MAKE_DECL_ONE_ONLY
5888 MAKE_DECL_ONE_ONLY (decl);
5889 #endif
5890 symbol->set_comdat_group (comdat_group);
5892 else if (TREE_CODE (decl) == VAR_DECL
5893 && (DECL_INITIAL (decl) == 0 || DECL_INITIAL (decl) == error_mark_node))
5894 DECL_COMMON (decl) = 1;
5895 else
5897 gcc_assert (TARGET_SUPPORTS_WEAK);
5898 DECL_WEAK (decl) = 1;
5902 void
5903 init_varasm_once (void)
5905 section_htab = htab_create_ggc (31, section_entry_hash,
5906 section_entry_eq, NULL);
5907 object_block_htab = htab_create_ggc (31, object_block_entry_hash,
5908 object_block_entry_eq, NULL);
5909 const_desc_htab = htab_create_ggc (1009, const_desc_hash,
5910 const_desc_eq, NULL);
5912 const_alias_set = new_alias_set ();
5913 shared_constant_pool = create_constant_pool ();
5915 #ifdef TEXT_SECTION_ASM_OP
5916 text_section = get_unnamed_section (SECTION_CODE, output_section_asm_op,
5917 TEXT_SECTION_ASM_OP);
5918 #endif
5920 #ifdef DATA_SECTION_ASM_OP
5921 data_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
5922 DATA_SECTION_ASM_OP);
5923 #endif
5925 #ifdef SDATA_SECTION_ASM_OP
5926 sdata_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
5927 SDATA_SECTION_ASM_OP);
5928 #endif
5930 #ifdef READONLY_DATA_SECTION_ASM_OP
5931 readonly_data_section = get_unnamed_section (0, output_section_asm_op,
5932 READONLY_DATA_SECTION_ASM_OP);
5933 #endif
5935 #ifdef CTORS_SECTION_ASM_OP
5936 ctors_section = get_unnamed_section (0, output_section_asm_op,
5937 CTORS_SECTION_ASM_OP);
5938 #endif
5940 #ifdef DTORS_SECTION_ASM_OP
5941 dtors_section = get_unnamed_section (0, output_section_asm_op,
5942 DTORS_SECTION_ASM_OP);
5943 #endif
5945 #ifdef BSS_SECTION_ASM_OP
5946 bss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
5947 output_section_asm_op,
5948 BSS_SECTION_ASM_OP);
5949 #endif
5951 #ifdef SBSS_SECTION_ASM_OP
5952 sbss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
5953 output_section_asm_op,
5954 SBSS_SECTION_ASM_OP);
5955 #endif
5957 tls_comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
5958 | SECTION_COMMON, emit_tls_common);
5959 lcomm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
5960 | SECTION_COMMON, emit_local);
5961 comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
5962 | SECTION_COMMON, emit_common);
5964 #if defined ASM_OUTPUT_ALIGNED_BSS
5965 bss_noswitch_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS,
5966 emit_bss);
5967 #endif
5969 targetm.asm_out.init_sections ();
5971 if (readonly_data_section == NULL)
5972 readonly_data_section = text_section;
5974 #ifdef ASM_OUTPUT_EXTERNAL
5975 pending_assemble_externals_set = pointer_set_create ();
5976 #endif
5979 enum tls_model
5980 decl_default_tls_model (const_tree decl)
5982 enum tls_model kind;
5983 bool is_local;
5985 is_local = targetm.binds_local_p (decl);
5986 if (!flag_shlib)
5988 if (is_local)
5989 kind = TLS_MODEL_LOCAL_EXEC;
5990 else
5991 kind = TLS_MODEL_INITIAL_EXEC;
5994 /* Local dynamic is inefficient when we're not combining the
5995 parts of the address. */
5996 else if (optimize && is_local)
5997 kind = TLS_MODEL_LOCAL_DYNAMIC;
5998 else
5999 kind = TLS_MODEL_GLOBAL_DYNAMIC;
6000 if (kind < flag_tls_default)
6001 kind = flag_tls_default;
6003 return kind;
6006 /* Select a set of attributes for section NAME based on the properties
6007 of DECL and whether or not RELOC indicates that DECL's initializer
6008 might contain runtime relocations.
6010 We make the section read-only and executable for a function decl,
6011 read-only for a const data decl, and writable for a non-const data decl. */
6013 unsigned int
6014 default_section_type_flags (tree decl, const char *name, int reloc)
6016 unsigned int flags;
6018 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
6019 flags = SECTION_CODE;
6020 else if (decl)
6022 enum section_category category
6023 = categorize_decl_for_section (decl, reloc);
6024 if (decl_readonly_section_1 (category))
6025 flags = 0;
6026 else if (category == SECCAT_DATA_REL_RO
6027 || category == SECCAT_DATA_REL_RO_LOCAL)
6028 flags = SECTION_WRITE | SECTION_RELRO;
6029 else
6030 flags = SECTION_WRITE;
6032 else
6034 flags = SECTION_WRITE;
6035 if (strcmp (name, ".data.rel.ro") == 0
6036 || strcmp (name, ".data.rel.ro.local") == 0)
6037 flags |= SECTION_RELRO;
6040 if (decl && DECL_P (decl) && DECL_COMDAT_GROUP (decl))
6041 flags |= SECTION_LINKONCE;
6043 if (strcmp (name, ".vtable_map_vars") == 0)
6044 flags |= SECTION_LINKONCE;
6046 if (decl && TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
6047 flags |= SECTION_TLS | SECTION_WRITE;
6049 if (strcmp (name, ".bss") == 0
6050 || strncmp (name, ".bss.", 5) == 0
6051 || strncmp (name, ".gnu.linkonce.b.", 16) == 0
6052 || strcmp (name, ".sbss") == 0
6053 || strncmp (name, ".sbss.", 6) == 0
6054 || strncmp (name, ".gnu.linkonce.sb.", 17) == 0)
6055 flags |= SECTION_BSS;
6057 if (strcmp (name, ".tdata") == 0
6058 || strncmp (name, ".tdata.", 7) == 0
6059 || strncmp (name, ".gnu.linkonce.td.", 17) == 0)
6060 flags |= SECTION_TLS;
6062 if (strcmp (name, ".tbss") == 0
6063 || strncmp (name, ".tbss.", 6) == 0
6064 || strncmp (name, ".gnu.linkonce.tb.", 17) == 0)
6065 flags |= SECTION_TLS | SECTION_BSS;
6067 /* These three sections have special ELF types. They are neither
6068 SHT_PROGBITS nor SHT_NOBITS, so when changing sections we don't
6069 want to print a section type (@progbits or @nobits). If someone
6070 is silly enough to emit code or TLS variables to one of these
6071 sections, then don't handle them specially. */
6072 if (!(flags & (SECTION_CODE | SECTION_BSS | SECTION_TLS))
6073 && (strcmp (name, ".init_array") == 0
6074 || strcmp (name, ".fini_array") == 0
6075 || strcmp (name, ".preinit_array") == 0))
6076 flags |= SECTION_NOTYPE;
6078 return flags;
6081 /* Return true if the target supports some form of global BSS,
6082 either through bss_noswitch_section, or by selecting a BSS
6083 section in TARGET_ASM_SELECT_SECTION. */
6085 bool
6086 have_global_bss_p (void)
6088 return bss_noswitch_section || targetm.have_switchable_bss_sections;
6091 /* Output assembly to switch to section NAME with attribute FLAGS.
6092 Four variants for common object file formats. */
6094 void
6095 default_no_named_section (const char *name ATTRIBUTE_UNUSED,
6096 unsigned int flags ATTRIBUTE_UNUSED,
6097 tree decl ATTRIBUTE_UNUSED)
6099 /* Some object formats don't support named sections at all. The
6100 front-end should already have flagged this as an error. */
6101 gcc_unreachable ();
6104 #ifndef TLS_SECTION_ASM_FLAG
6105 #define TLS_SECTION_ASM_FLAG 'T'
6106 #endif
6108 void
6109 default_elf_asm_named_section (const char *name, unsigned int flags,
6110 tree decl ATTRIBUTE_UNUSED)
6112 char flagchars[10], *f = flagchars;
6114 /* If we have already declared this section, we can use an
6115 abbreviated form to switch back to it -- unless this section is
6116 part of a COMDAT groups, in which case GAS requires the full
6117 declaration every time. */
6118 if (!(HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6119 && (flags & SECTION_DECLARED))
6121 fprintf (asm_out_file, "\t.section\t%s\n", name);
6122 return;
6125 if (!(flags & SECTION_DEBUG))
6126 *f++ = 'a';
6127 if (flags & SECTION_EXCLUDE)
6128 *f++ = 'e';
6129 if (flags & SECTION_WRITE)
6130 *f++ = 'w';
6131 if (flags & SECTION_CODE)
6132 *f++ = 'x';
6133 if (flags & SECTION_SMALL)
6134 *f++ = 's';
6135 if (flags & SECTION_MERGE)
6136 *f++ = 'M';
6137 if (flags & SECTION_STRINGS)
6138 *f++ = 'S';
6139 if (flags & SECTION_TLS)
6140 *f++ = TLS_SECTION_ASM_FLAG;
6141 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6142 *f++ = 'G';
6143 *f = '\0';
6145 fprintf (asm_out_file, "\t.section\t%s,\"%s\"", name, flagchars);
6147 if (!(flags & SECTION_NOTYPE))
6149 const char *type;
6150 const char *format;
6152 if (flags & SECTION_BSS)
6153 type = "nobits";
6154 else
6155 type = "progbits";
6157 format = ",@%s";
6158 /* On platforms that use "@" as the assembly comment character,
6159 use "%" instead. */
6160 if (strcmp (ASM_COMMENT_START, "@") == 0)
6161 format = ",%%%s";
6162 fprintf (asm_out_file, format, type);
6164 if (flags & SECTION_ENTSIZE)
6165 fprintf (asm_out_file, ",%d", flags & SECTION_ENTSIZE);
6166 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6168 if (TREE_CODE (decl) == IDENTIFIER_NODE)
6169 fprintf (asm_out_file, ",%s,comdat", IDENTIFIER_POINTER (decl));
6170 else
6171 fprintf (asm_out_file, ",%s,comdat",
6172 IDENTIFIER_POINTER (DECL_COMDAT_GROUP (decl)));
6176 putc ('\n', asm_out_file);
6179 void
6180 default_coff_asm_named_section (const char *name, unsigned int flags,
6181 tree decl ATTRIBUTE_UNUSED)
6183 char flagchars[8], *f = flagchars;
6185 if (flags & SECTION_WRITE)
6186 *f++ = 'w';
6187 if (flags & SECTION_CODE)
6188 *f++ = 'x';
6189 *f = '\0';
6191 fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
6194 void
6195 default_pe_asm_named_section (const char *name, unsigned int flags,
6196 tree decl)
6198 default_coff_asm_named_section (name, flags, decl);
6200 if (flags & SECTION_LINKONCE)
6202 /* Functions may have been compiled at various levels of
6203 optimization so we can't use `same_size' here.
6204 Instead, have the linker pick one. */
6205 fprintf (asm_out_file, "\t.linkonce %s\n",
6206 (flags & SECTION_CODE ? "discard" : "same_size"));
6210 /* The lame default section selector. */
6212 section *
6213 default_select_section (tree decl, int reloc,
6214 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
6216 if (DECL_P (decl))
6218 if (decl_readonly_section (decl, reloc))
6219 return readonly_data_section;
6221 else if (TREE_CODE (decl) == CONSTRUCTOR)
6223 if (! ((flag_pic && reloc)
6224 || !TREE_READONLY (decl)
6225 || TREE_SIDE_EFFECTS (decl)
6226 || !TREE_CONSTANT (decl)))
6227 return readonly_data_section;
6229 else if (TREE_CODE (decl) == STRING_CST)
6230 return readonly_data_section;
6231 else if (! (flag_pic && reloc))
6232 return readonly_data_section;
6234 return data_section;
6237 enum section_category
6238 categorize_decl_for_section (const_tree decl, int reloc)
6240 enum section_category ret;
6242 if (TREE_CODE (decl) == FUNCTION_DECL)
6243 return SECCAT_TEXT;
6244 else if (TREE_CODE (decl) == STRING_CST)
6246 if ((flag_sanitize & SANITIZE_ADDRESS)
6247 && asan_protect_global (CONST_CAST_TREE (decl)))
6248 /* or !flag_merge_constants */
6249 return SECCAT_RODATA;
6250 else
6251 return SECCAT_RODATA_MERGE_STR;
6253 else if (TREE_CODE (decl) == VAR_DECL)
6255 if (bss_initializer_p (decl))
6256 ret = SECCAT_BSS;
6257 else if (! TREE_READONLY (decl)
6258 || TREE_SIDE_EFFECTS (decl)
6259 || ! TREE_CONSTANT (DECL_INITIAL (decl)))
6261 /* Here the reloc_rw_mask is not testing whether the section should
6262 be read-only or not, but whether the dynamic link will have to
6263 do something. If so, we wish to segregate the data in order to
6264 minimize cache misses inside the dynamic linker. */
6265 if (reloc & targetm.asm_out.reloc_rw_mask ())
6266 ret = reloc == 1 ? SECCAT_DATA_REL_LOCAL : SECCAT_DATA_REL;
6267 else
6268 ret = SECCAT_DATA;
6270 else if (reloc & targetm.asm_out.reloc_rw_mask ())
6271 ret = reloc == 1 ? SECCAT_DATA_REL_RO_LOCAL : SECCAT_DATA_REL_RO;
6272 else if (reloc || flag_merge_constants < 2
6273 || ((flag_sanitize & SANITIZE_ADDRESS)
6274 && asan_protect_global (CONST_CAST_TREE (decl))))
6275 /* C and C++ don't allow different variables to share the same
6276 location. -fmerge-all-constants allows even that (at the
6277 expense of not conforming). */
6278 ret = SECCAT_RODATA;
6279 else if (TREE_CODE (DECL_INITIAL (decl)) == STRING_CST)
6280 ret = SECCAT_RODATA_MERGE_STR_INIT;
6281 else
6282 ret = SECCAT_RODATA_MERGE_CONST;
6284 else if (TREE_CODE (decl) == CONSTRUCTOR)
6286 if ((reloc & targetm.asm_out.reloc_rw_mask ())
6287 || TREE_SIDE_EFFECTS (decl)
6288 || ! TREE_CONSTANT (decl))
6289 ret = SECCAT_DATA;
6290 else
6291 ret = SECCAT_RODATA;
6293 else
6294 ret = SECCAT_RODATA;
6296 /* There are no read-only thread-local sections. */
6297 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
6299 /* Note that this would be *just* SECCAT_BSS, except that there's
6300 no concept of a read-only thread-local-data section. */
6301 if (ret == SECCAT_BSS
6302 || (flag_zero_initialized_in_bss
6303 && initializer_zerop (DECL_INITIAL (decl))))
6304 ret = SECCAT_TBSS;
6305 else
6306 ret = SECCAT_TDATA;
6309 /* If the target uses small data sections, select it. */
6310 else if (targetm.in_small_data_p (decl))
6312 if (ret == SECCAT_BSS)
6313 ret = SECCAT_SBSS;
6314 else if (targetm.have_srodata_section && ret == SECCAT_RODATA)
6315 ret = SECCAT_SRODATA;
6316 else
6317 ret = SECCAT_SDATA;
6320 return ret;
6323 static bool
6324 decl_readonly_section_1 (enum section_category category)
6326 switch (category)
6328 case SECCAT_RODATA:
6329 case SECCAT_RODATA_MERGE_STR:
6330 case SECCAT_RODATA_MERGE_STR_INIT:
6331 case SECCAT_RODATA_MERGE_CONST:
6332 case SECCAT_SRODATA:
6333 return true;
6334 default:
6335 return false;
6339 bool
6340 decl_readonly_section (const_tree decl, int reloc)
6342 return decl_readonly_section_1 (categorize_decl_for_section (decl, reloc));
6345 /* Select a section based on the above categorization. */
6347 section *
6348 default_elf_select_section (tree decl, int reloc,
6349 unsigned HOST_WIDE_INT align)
6351 const char *sname;
6352 switch (categorize_decl_for_section (decl, reloc))
6354 case SECCAT_TEXT:
6355 /* We're not supposed to be called on FUNCTION_DECLs. */
6356 gcc_unreachable ();
6357 case SECCAT_RODATA:
6358 return readonly_data_section;
6359 case SECCAT_RODATA_MERGE_STR:
6360 return mergeable_string_section (decl, align, 0);
6361 case SECCAT_RODATA_MERGE_STR_INIT:
6362 return mergeable_string_section (DECL_INITIAL (decl), align, 0);
6363 case SECCAT_RODATA_MERGE_CONST:
6364 return mergeable_constant_section (DECL_MODE (decl), align, 0);
6365 case SECCAT_SRODATA:
6366 sname = ".sdata2";
6367 break;
6368 case SECCAT_DATA:
6369 return data_section;
6370 case SECCAT_DATA_REL:
6371 sname = ".data.rel";
6372 break;
6373 case SECCAT_DATA_REL_LOCAL:
6374 sname = ".data.rel.local";
6375 break;
6376 case SECCAT_DATA_REL_RO:
6377 sname = ".data.rel.ro";
6378 break;
6379 case SECCAT_DATA_REL_RO_LOCAL:
6380 sname = ".data.rel.ro.local";
6381 break;
6382 case SECCAT_SDATA:
6383 sname = ".sdata";
6384 break;
6385 case SECCAT_TDATA:
6386 sname = ".tdata";
6387 break;
6388 case SECCAT_BSS:
6389 if (bss_section)
6390 return bss_section;
6391 sname = ".bss";
6392 break;
6393 case SECCAT_SBSS:
6394 sname = ".sbss";
6395 break;
6396 case SECCAT_TBSS:
6397 sname = ".tbss";
6398 break;
6399 default:
6400 gcc_unreachable ();
6403 return get_named_section (decl, sname, reloc);
6406 /* Construct a unique section name based on the decl name and the
6407 categorization performed above. */
6409 void
6410 default_unique_section (tree decl, int reloc)
6412 /* We only need to use .gnu.linkonce if we don't have COMDAT groups. */
6413 bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
6414 const char *prefix, *name, *linkonce;
6415 char *string;
6417 switch (categorize_decl_for_section (decl, reloc))
6419 case SECCAT_TEXT:
6420 prefix = one_only ? ".t" : ".text";
6421 break;
6422 case SECCAT_RODATA:
6423 case SECCAT_RODATA_MERGE_STR:
6424 case SECCAT_RODATA_MERGE_STR_INIT:
6425 case SECCAT_RODATA_MERGE_CONST:
6426 prefix = one_only ? ".r" : ".rodata";
6427 break;
6428 case SECCAT_SRODATA:
6429 prefix = one_only ? ".s2" : ".sdata2";
6430 break;
6431 case SECCAT_DATA:
6432 prefix = one_only ? ".d" : ".data";
6433 break;
6434 case SECCAT_DATA_REL:
6435 prefix = one_only ? ".d.rel" : ".data.rel";
6436 break;
6437 case SECCAT_DATA_REL_LOCAL:
6438 prefix = one_only ? ".d.rel.local" : ".data.rel.local";
6439 break;
6440 case SECCAT_DATA_REL_RO:
6441 prefix = one_only ? ".d.rel.ro" : ".data.rel.ro";
6442 break;
6443 case SECCAT_DATA_REL_RO_LOCAL:
6444 prefix = one_only ? ".d.rel.ro.local" : ".data.rel.ro.local";
6445 break;
6446 case SECCAT_SDATA:
6447 prefix = one_only ? ".s" : ".sdata";
6448 break;
6449 case SECCAT_BSS:
6450 prefix = one_only ? ".b" : ".bss";
6451 break;
6452 case SECCAT_SBSS:
6453 prefix = one_only ? ".sb" : ".sbss";
6454 break;
6455 case SECCAT_TDATA:
6456 prefix = one_only ? ".td" : ".tdata";
6457 break;
6458 case SECCAT_TBSS:
6459 prefix = one_only ? ".tb" : ".tbss";
6460 break;
6461 default:
6462 gcc_unreachable ();
6465 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
6466 name = targetm.strip_name_encoding (name);
6468 /* If we're using one_only, then there needs to be a .gnu.linkonce
6469 prefix to the section name. */
6470 linkonce = one_only ? ".gnu.linkonce" : "";
6472 string = ACONCAT ((linkonce, prefix, ".", name, NULL));
6474 set_decl_section_name (decl, string);
6477 /* Like compute_reloc_for_constant, except for an RTX. The return value
6478 is a mask for which bit 1 indicates a global relocation, and bit 0
6479 indicates a local relocation. */
6481 static int
6482 compute_reloc_for_rtx_1 (rtx *xp, void *data)
6484 int *preloc = (int *) data;
6485 rtx x = *xp;
6487 switch (GET_CODE (x))
6489 case SYMBOL_REF:
6490 *preloc |= SYMBOL_REF_LOCAL_P (x) ? 1 : 2;
6491 break;
6492 case LABEL_REF:
6493 *preloc |= 1;
6494 break;
6495 default:
6496 break;
6499 return 0;
6502 static int
6503 compute_reloc_for_rtx (rtx x)
6505 int reloc;
6507 switch (GET_CODE (x))
6509 case CONST:
6510 case SYMBOL_REF:
6511 case LABEL_REF:
6512 reloc = 0;
6513 for_each_rtx (&x, compute_reloc_for_rtx_1, &reloc);
6514 return reloc;
6516 default:
6517 return 0;
6521 section *
6522 default_select_rtx_section (enum machine_mode mode ATTRIBUTE_UNUSED,
6523 rtx x,
6524 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
6526 if (compute_reloc_for_rtx (x) & targetm.asm_out.reloc_rw_mask ())
6527 return data_section;
6528 else
6529 return readonly_data_section;
6532 section *
6533 default_elf_select_rtx_section (enum machine_mode mode, rtx x,
6534 unsigned HOST_WIDE_INT align)
6536 int reloc = compute_reloc_for_rtx (x);
6538 /* ??? Handle small data here somehow. */
6540 if (reloc & targetm.asm_out.reloc_rw_mask ())
6542 if (reloc == 1)
6543 return get_named_section (NULL, ".data.rel.ro.local", 1);
6544 else
6545 return get_named_section (NULL, ".data.rel.ro", 3);
6548 return mergeable_constant_section (mode, align, 0);
6551 /* Set the generally applicable flags on the SYMBOL_REF for EXP. */
6553 void
6554 default_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
6556 rtx symbol;
6557 int flags;
6559 /* Careful not to prod global register variables. */
6560 if (!MEM_P (rtl))
6561 return;
6562 symbol = XEXP (rtl, 0);
6563 if (GET_CODE (symbol) != SYMBOL_REF)
6564 return;
6566 flags = SYMBOL_REF_FLAGS (symbol) & SYMBOL_FLAG_HAS_BLOCK_INFO;
6567 if (TREE_CODE (decl) == FUNCTION_DECL)
6568 flags |= SYMBOL_FLAG_FUNCTION;
6569 if (targetm.binds_local_p (decl))
6570 flags |= SYMBOL_FLAG_LOCAL;
6571 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
6572 flags |= DECL_TLS_MODEL (decl) << SYMBOL_FLAG_TLS_SHIFT;
6573 else if (targetm.in_small_data_p (decl))
6574 flags |= SYMBOL_FLAG_SMALL;
6575 /* ??? Why is DECL_EXTERNAL ever set for non-PUBLIC names? Without
6576 being PUBLIC, the thing *must* be defined in this translation unit.
6577 Prevent this buglet from being propagated into rtl code as well. */
6578 if (DECL_P (decl) && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl))
6579 flags |= SYMBOL_FLAG_EXTERNAL;
6581 SYMBOL_REF_FLAGS (symbol) = flags;
6584 /* By default, we do nothing for encode_section_info, so we need not
6585 do anything but discard the '*' marker. */
6587 const char *
6588 default_strip_name_encoding (const char *str)
6590 return str + (*str == '*');
6593 #ifdef ASM_OUTPUT_DEF
6594 /* The default implementation of TARGET_ASM_OUTPUT_ANCHOR. Define the
6595 anchor relative to ".", the current section position. */
6597 void
6598 default_asm_output_anchor (rtx symbol)
6600 char buffer[100];
6602 sprintf (buffer, "*. + " HOST_WIDE_INT_PRINT_DEC,
6603 SYMBOL_REF_BLOCK_OFFSET (symbol));
6604 ASM_OUTPUT_DEF (asm_out_file, XSTR (symbol, 0), buffer);
6606 #endif
6608 /* The default implementation of TARGET_USE_ANCHORS_FOR_SYMBOL_P. */
6610 bool
6611 default_use_anchors_for_symbol_p (const_rtx symbol)
6613 section *sect;
6614 tree decl;
6616 /* Don't use anchors for mergeable sections. The linker might move
6617 the objects around. */
6618 sect = SYMBOL_REF_BLOCK (symbol)->sect;
6619 if (sect->common.flags & SECTION_MERGE)
6620 return false;
6622 /* Don't use anchors for small data sections. The small data register
6623 acts as an anchor for such sections. */
6624 if (sect->common.flags & SECTION_SMALL)
6625 return false;
6627 decl = SYMBOL_REF_DECL (symbol);
6628 if (decl && DECL_P (decl))
6630 /* Don't use section anchors for decls that might be defined or
6631 usurped by other modules. */
6632 if (TREE_PUBLIC (decl) && !decl_binds_to_current_def_p (decl))
6633 return false;
6635 /* Don't use section anchors for decls that will be placed in a
6636 small data section. */
6637 /* ??? Ideally, this check would be redundant with the SECTION_SMALL
6638 one above. The problem is that we only use SECTION_SMALL for
6639 sections that should be marked as small in the section directive. */
6640 if (targetm.in_small_data_p (decl))
6641 return false;
6643 return true;
6646 /* Return true when RESOLUTION indicate that symbol will be bound to the
6647 definition provided by current .o file. */
6649 static bool
6650 resolution_to_local_definition_p (enum ld_plugin_symbol_resolution resolution)
6652 return (resolution == LDPR_PREVAILING_DEF
6653 || resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
6654 || resolution == LDPR_PREVAILING_DEF_IRONLY);
6657 /* Return true when RESOLUTION indicate that symbol will be bound locally
6658 within current executable or DSO. */
6660 static bool
6661 resolution_local_p (enum ld_plugin_symbol_resolution resolution)
6663 return (resolution == LDPR_PREVAILING_DEF
6664 || resolution == LDPR_PREVAILING_DEF_IRONLY
6665 || resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
6666 || resolution == LDPR_PREEMPTED_REG
6667 || resolution == LDPR_PREEMPTED_IR
6668 || resolution == LDPR_RESOLVED_IR
6669 || resolution == LDPR_RESOLVED_EXEC);
6672 /* Assume ELF-ish defaults, since that's pretty much the most liberal
6673 wrt cross-module name binding. */
6675 bool
6676 default_binds_local_p (const_tree exp)
6678 return default_binds_local_p_1 (exp, flag_shlib);
6681 bool
6682 default_binds_local_p_1 (const_tree exp, int shlib)
6684 bool local_p;
6685 bool resolved_locally = false;
6686 bool resolved_to_local_def = false;
6688 /* With resolution file in hands, take look into resolutions.
6689 We can't just return true for resolved_locally symbols,
6690 because dynamic linking might overwrite symbols
6691 in shared libraries. */
6692 if (TREE_CODE (exp) == VAR_DECL && TREE_PUBLIC (exp)
6693 && (TREE_STATIC (exp) || DECL_EXTERNAL (exp)))
6695 varpool_node *vnode = varpool_get_node (exp);
6696 if (vnode && (resolution_local_p (vnode->resolution) || vnode->in_other_partition))
6697 resolved_locally = true;
6698 if (vnode
6699 && resolution_to_local_definition_p (vnode->resolution))
6700 resolved_to_local_def = true;
6702 else if (TREE_CODE (exp) == FUNCTION_DECL && TREE_PUBLIC (exp))
6704 struct cgraph_node *node = cgraph_get_node (exp);
6705 if (node
6706 && (resolution_local_p (node->resolution) || node->in_other_partition))
6707 resolved_locally = true;
6708 if (node
6709 && resolution_to_local_definition_p (node->resolution))
6710 resolved_to_local_def = true;
6713 /* A non-decl is an entry in the constant pool. */
6714 if (!DECL_P (exp))
6715 local_p = true;
6716 /* Weakrefs may not bind locally, even though the weakref itself is always
6717 static and therefore local. Similarly, the resolver for ifunc functions
6718 might resolve to a non-local function.
6719 FIXME: We can resolve the weakref case more curefuly by looking at the
6720 weakref alias. */
6721 else if (lookup_attribute ("weakref", DECL_ATTRIBUTES (exp))
6722 || (TREE_CODE (exp) == FUNCTION_DECL
6723 && lookup_attribute ("ifunc", DECL_ATTRIBUTES (exp))))
6724 local_p = false;
6725 /* Static variables are always local. */
6726 else if (! TREE_PUBLIC (exp))
6727 local_p = true;
6728 /* A variable is local if the user has said explicitly that it will
6729 be. */
6730 else if ((DECL_VISIBILITY_SPECIFIED (exp)
6731 || resolved_to_local_def)
6732 && DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
6733 local_p = true;
6734 /* Variables defined outside this object might not be local. */
6735 else if (DECL_EXTERNAL (exp) && !resolved_locally)
6736 local_p = false;
6737 /* If defined in this object and visibility is not default, must be
6738 local. */
6739 else if (DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
6740 local_p = true;
6741 /* Default visibility weak data can be overridden by a strong symbol
6742 in another module and so are not local. */
6743 else if (DECL_WEAK (exp)
6744 && !resolved_locally)
6745 local_p = false;
6746 /* If PIC, then assume that any global name can be overridden by
6747 symbols resolved from other modules. */
6748 else if (shlib)
6749 local_p = false;
6750 /* Uninitialized COMMON variable may be unified with symbols
6751 resolved from other modules. */
6752 else if (DECL_COMMON (exp)
6753 && !resolved_locally
6754 && (DECL_INITIAL (exp) == NULL
6755 || DECL_INITIAL (exp) == error_mark_node))
6756 local_p = false;
6757 /* Otherwise we're left with initialized (or non-common) global data
6758 which is of necessity defined locally. */
6759 else
6760 local_p = true;
6762 return local_p;
6765 /* Return true when references to DECL must bind to current definition in
6766 final executable.
6768 The condition is usually equivalent to whether the function binds to the
6769 current module (shared library or executable), that is to binds_local_p.
6770 We use this fact to avoid need for another target hook and implement
6771 the logic using binds_local_p and just special cases where
6772 decl_binds_to_current_def_p is stronger than binds_local_p. In particular
6773 the weak definitions (that can be overwritten at linktime by other
6774 definition from different object file) and when resolution info is available
6775 we simply use the knowledge passed to us by linker plugin. */
6776 bool
6777 decl_binds_to_current_def_p (const_tree decl)
6779 gcc_assert (DECL_P (decl));
6780 if (!targetm.binds_local_p (decl))
6781 return false;
6782 if (!TREE_PUBLIC (decl))
6783 return true;
6784 /* When resolution is available, just use it. */
6785 if (TREE_CODE (decl) == VAR_DECL
6786 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
6788 varpool_node *vnode = varpool_get_node (decl);
6789 if (vnode
6790 && vnode->resolution != LDPR_UNKNOWN)
6791 return resolution_to_local_definition_p (vnode->resolution);
6793 else if (TREE_CODE (decl) == FUNCTION_DECL)
6795 struct cgraph_node *node = cgraph_get_node (decl);
6796 if (node
6797 && node->resolution != LDPR_UNKNOWN)
6798 return resolution_to_local_definition_p (node->resolution);
6800 /* Otherwise we have to assume the worst for DECL_WEAK (hidden weaks
6801 binds locally but still can be overwritten), DECL_COMMON (can be merged
6802 with a non-common definition somewhere in the same module) or
6803 DECL_EXTERNAL.
6804 This rely on fact that binds_local_p behave as decl_replaceable_p
6805 for all other declaration types. */
6806 if (DECL_WEAK (decl))
6807 return false;
6808 if (DECL_COMMON (decl)
6809 && (DECL_INITIAL (decl) == NULL
6810 || DECL_INITIAL (decl) == error_mark_node))
6811 return false;
6812 if (DECL_EXTERNAL (decl))
6813 return false;
6814 return true;
6817 /* A replaceable function or variable is one which may be replaced
6818 at link-time with an entirely different definition, provided that the
6819 replacement has the same type. For example, functions declared
6820 with __attribute__((weak)) on most systems are replaceable.
6822 COMDAT functions are not replaceable, since all definitions of the
6823 function must be equivalent. It is important that COMDAT functions
6824 not be treated as replaceable so that use of C++ template
6825 instantiations is not penalized. */
6827 bool
6828 decl_replaceable_p (tree decl)
6830 gcc_assert (DECL_P (decl));
6831 if (!TREE_PUBLIC (decl) || DECL_COMDAT (decl))
6832 return false;
6833 if (!flag_semantic_interposition
6834 && !DECL_WEAK (decl))
6835 return false;
6836 return !decl_binds_to_current_def_p (decl);
6839 /* Default function to output code that will globalize a label. A
6840 target must define GLOBAL_ASM_OP or provide its own function to
6841 globalize a label. */
6842 #ifdef GLOBAL_ASM_OP
6843 void
6844 default_globalize_label (FILE * stream, const char *name)
6846 fputs (GLOBAL_ASM_OP, stream);
6847 assemble_name (stream, name);
6848 putc ('\n', stream);
6850 #endif /* GLOBAL_ASM_OP */
6852 /* Default function to output code that will globalize a declaration. */
6853 void
6854 default_globalize_decl_name (FILE * stream, tree decl)
6856 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
6857 targetm.asm_out.globalize_label (stream, name);
6860 /* Default function to output a label for unwind information. The
6861 default is to do nothing. A target that needs nonlocal labels for
6862 unwind information must provide its own function to do this. */
6863 void
6864 default_emit_unwind_label (FILE * stream ATTRIBUTE_UNUSED,
6865 tree decl ATTRIBUTE_UNUSED,
6866 int for_eh ATTRIBUTE_UNUSED,
6867 int empty ATTRIBUTE_UNUSED)
6871 /* Default function to output a label to divide up the exception table.
6872 The default is to do nothing. A target that needs/wants to divide
6873 up the table must provide it's own function to do this. */
6874 void
6875 default_emit_except_table_label (FILE * stream ATTRIBUTE_UNUSED)
6879 /* This is how to output an internal numbered label where PREFIX is
6880 the class of label and LABELNO is the number within the class. */
6882 void
6883 default_generate_internal_label (char *buf, const char *prefix,
6884 unsigned long labelno)
6886 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
6889 /* This is how to output an internal numbered label where PREFIX is
6890 the class of label and LABELNO is the number within the class. */
6892 void
6893 default_internal_label (FILE *stream, const char *prefix,
6894 unsigned long labelno)
6896 char *const buf = (char *) alloca (40 + strlen (prefix));
6897 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
6898 ASM_OUTPUT_INTERNAL_LABEL (stream, buf);
6902 /* The default implementation of ASM_DECLARE_CONSTANT_NAME. */
6904 void
6905 default_asm_declare_constant_name (FILE *file, const char *name,
6906 const_tree exp ATTRIBUTE_UNUSED,
6907 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
6909 assemble_label (file, name);
6912 /* This is the default behavior at the beginning of a file. It's
6913 controlled by two other target-hook toggles. */
6914 void
6915 default_file_start (void)
6917 if (targetm.asm_file_start_app_off
6918 && !(flag_verbose_asm || flag_debug_asm || flag_dump_rtl_in_asm))
6919 fputs (ASM_APP_OFF, asm_out_file);
6921 if (targetm.asm_file_start_file_directive)
6922 output_file_directive (asm_out_file, main_input_filename);
6925 /* This is a generic routine suitable for use as TARGET_ASM_FILE_END
6926 which emits a special section directive used to indicate whether or
6927 not this object file needs an executable stack. This is primarily
6928 a GNU extension to ELF but could be used on other targets. */
6930 int trampolines_created;
6932 void
6933 file_end_indicate_exec_stack (void)
6935 unsigned int flags = SECTION_DEBUG;
6936 if (trampolines_created)
6937 flags |= SECTION_CODE;
6939 switch_to_section (get_section (".note.GNU-stack", flags, NULL));
6942 /* Emit a special section directive to indicate that this object file
6943 was compiled with -fsplit-stack. This is used to let the linker
6944 detect calls between split-stack code and non-split-stack code, so
6945 that it can modify the split-stack code to allocate a sufficiently
6946 large stack. We emit another special section if there are any
6947 functions in this file which have the no_split_stack attribute, to
6948 prevent the linker from warning about being unable to convert the
6949 functions if they call non-split-stack code. */
6951 void
6952 file_end_indicate_split_stack (void)
6954 if (flag_split_stack)
6956 switch_to_section (get_section (".note.GNU-split-stack", SECTION_DEBUG,
6957 NULL));
6958 if (saw_no_split_stack)
6959 switch_to_section (get_section (".note.GNU-no-split-stack",
6960 SECTION_DEBUG, NULL));
6964 /* Output DIRECTIVE (a C string) followed by a newline. This is used as
6965 a get_unnamed_section callback. */
6967 void
6968 output_section_asm_op (const void *directive)
6970 fprintf (asm_out_file, "%s\n", (const char *) directive);
6973 /* Emit assembly code to switch to section NEW_SECTION. Do nothing if
6974 the current section is NEW_SECTION. */
6976 void
6977 switch_to_section (section *new_section)
6979 if (in_section == new_section)
6980 return;
6982 if (new_section->common.flags & SECTION_FORGET)
6983 in_section = NULL;
6984 else
6985 in_section = new_section;
6987 switch (SECTION_STYLE (new_section))
6989 case SECTION_NAMED:
6990 targetm.asm_out.named_section (new_section->named.name,
6991 new_section->named.common.flags,
6992 new_section->named.decl);
6993 break;
6995 case SECTION_UNNAMED:
6996 new_section->unnamed.callback (new_section->unnamed.data);
6997 break;
6999 case SECTION_NOSWITCH:
7000 gcc_unreachable ();
7001 break;
7004 new_section->common.flags |= SECTION_DECLARED;
7007 /* If block symbol SYMBOL has not yet been assigned an offset, place
7008 it at the end of its block. */
7010 void
7011 place_block_symbol (rtx symbol)
7013 unsigned HOST_WIDE_INT size, mask, offset;
7014 struct constant_descriptor_rtx *desc;
7015 unsigned int alignment;
7016 struct object_block *block;
7017 tree decl;
7019 gcc_assert (SYMBOL_REF_BLOCK (symbol));
7020 if (SYMBOL_REF_BLOCK_OFFSET (symbol) >= 0)
7021 return;
7023 /* Work out the symbol's size and alignment. */
7024 if (CONSTANT_POOL_ADDRESS_P (symbol))
7026 desc = SYMBOL_REF_CONSTANT (symbol);
7027 alignment = desc->align;
7028 size = GET_MODE_SIZE (desc->mode);
7030 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
7032 decl = SYMBOL_REF_DECL (symbol);
7033 alignment = DECL_ALIGN (decl);
7034 size = get_constant_size (DECL_INITIAL (decl));
7035 if ((flag_sanitize & SANITIZE_ADDRESS)
7036 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
7037 && asan_protect_global (DECL_INITIAL (decl)))
7038 size += asan_red_zone_size (size);
7040 else
7042 struct symtab_node *snode;
7043 decl = SYMBOL_REF_DECL (symbol);
7045 snode = symtab_get_node (decl);
7046 if (snode->alias)
7048 rtx target = DECL_RTL (symtab_alias_ultimate_target (snode)->decl);
7050 place_block_symbol (target);
7051 SYMBOL_REF_BLOCK_OFFSET (symbol) = SYMBOL_REF_BLOCK_OFFSET (target);
7052 return;
7054 alignment = get_variable_align (decl);
7055 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
7056 if ((flag_sanitize & SANITIZE_ADDRESS)
7057 && asan_protect_global (decl))
7059 size += asan_red_zone_size (size);
7060 alignment = MAX (alignment,
7061 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
7065 /* Calculate the object's offset from the start of the block. */
7066 block = SYMBOL_REF_BLOCK (symbol);
7067 mask = alignment / BITS_PER_UNIT - 1;
7068 offset = (block->size + mask) & ~mask;
7069 SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
7071 /* Record the block's new alignment and size. */
7072 block->alignment = MAX (block->alignment, alignment);
7073 block->size = offset + size;
7075 vec_safe_push (block->objects, symbol);
7078 /* Return the anchor that should be used to address byte offset OFFSET
7079 from the first object in BLOCK. MODEL is the TLS model used
7080 to access it. */
7083 get_section_anchor (struct object_block *block, HOST_WIDE_INT offset,
7084 enum tls_model model)
7086 char label[100];
7087 unsigned int begin, middle, end;
7088 unsigned HOST_WIDE_INT min_offset, max_offset, range, bias, delta;
7089 rtx anchor;
7091 /* Work out the anchor's offset. Use an offset of 0 for the first
7092 anchor so that we don't pessimize the case where we take the address
7093 of a variable at the beginning of the block. This is particularly
7094 useful when a block has only one variable assigned to it.
7096 We try to place anchors RANGE bytes apart, so there can then be
7097 anchors at +/-RANGE, +/-2 * RANGE, and so on, up to the limits of
7098 a ptr_mode offset. With some target settings, the lowest such
7099 anchor might be out of range for the lowest ptr_mode offset;
7100 likewise the highest anchor for the highest offset. Use anchors
7101 at the extreme ends of the ptr_mode range in such cases.
7103 All arithmetic uses unsigned integers in order to avoid
7104 signed overflow. */
7105 max_offset = (unsigned HOST_WIDE_INT) targetm.max_anchor_offset;
7106 min_offset = (unsigned HOST_WIDE_INT) targetm.min_anchor_offset;
7107 range = max_offset - min_offset + 1;
7108 if (range == 0)
7109 offset = 0;
7110 else
7112 bias = 1 << (GET_MODE_BITSIZE (ptr_mode) - 1);
7113 if (offset < 0)
7115 delta = -(unsigned HOST_WIDE_INT) offset + max_offset;
7116 delta -= delta % range;
7117 if (delta > bias)
7118 delta = bias;
7119 offset = (HOST_WIDE_INT) (-delta);
7121 else
7123 delta = (unsigned HOST_WIDE_INT) offset - min_offset;
7124 delta -= delta % range;
7125 if (delta > bias - 1)
7126 delta = bias - 1;
7127 offset = (HOST_WIDE_INT) delta;
7131 /* Do a binary search to see if there's already an anchor we can use.
7132 Set BEGIN to the new anchor's index if not. */
7133 begin = 0;
7134 end = vec_safe_length (block->anchors);
7135 while (begin != end)
7137 middle = (end + begin) / 2;
7138 anchor = (*block->anchors)[middle];
7139 if (SYMBOL_REF_BLOCK_OFFSET (anchor) > offset)
7140 end = middle;
7141 else if (SYMBOL_REF_BLOCK_OFFSET (anchor) < offset)
7142 begin = middle + 1;
7143 else if (SYMBOL_REF_TLS_MODEL (anchor) > model)
7144 end = middle;
7145 else if (SYMBOL_REF_TLS_MODEL (anchor) < model)
7146 begin = middle + 1;
7147 else
7148 return anchor;
7151 /* Create a new anchor with a unique label. */
7152 ASM_GENERATE_INTERNAL_LABEL (label, "LANCHOR", anchor_labelno++);
7153 anchor = create_block_symbol (ggc_strdup (label), block, offset);
7154 SYMBOL_REF_FLAGS (anchor) |= SYMBOL_FLAG_LOCAL | SYMBOL_FLAG_ANCHOR;
7155 SYMBOL_REF_FLAGS (anchor) |= model << SYMBOL_FLAG_TLS_SHIFT;
7157 /* Insert it at index BEGIN. */
7158 vec_safe_insert (block->anchors, begin, anchor);
7159 return anchor;
7162 /* Output the objects in BLOCK. */
7164 static void
7165 output_object_block (struct object_block *block)
7167 struct constant_descriptor_rtx *desc;
7168 unsigned int i;
7169 HOST_WIDE_INT offset;
7170 tree decl;
7171 rtx symbol;
7173 if (!block->objects)
7174 return;
7176 /* Switch to the section and make sure that the first byte is
7177 suitably aligned. */
7178 switch_to_section (block->sect);
7179 assemble_align (block->alignment);
7181 /* Define the values of all anchors relative to the current section
7182 position. */
7183 FOR_EACH_VEC_SAFE_ELT (block->anchors, i, symbol)
7184 targetm.asm_out.output_anchor (symbol);
7186 /* Output the objects themselves. */
7187 offset = 0;
7188 FOR_EACH_VEC_ELT (*block->objects, i, symbol)
7190 /* Move to the object's offset, padding with zeros if necessary. */
7191 assemble_zeros (SYMBOL_REF_BLOCK_OFFSET (symbol) - offset);
7192 offset = SYMBOL_REF_BLOCK_OFFSET (symbol);
7193 if (CONSTANT_POOL_ADDRESS_P (symbol))
7195 desc = SYMBOL_REF_CONSTANT (symbol);
7196 output_constant_pool_1 (desc, 1);
7197 offset += GET_MODE_SIZE (desc->mode);
7199 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
7201 HOST_WIDE_INT size;
7202 decl = SYMBOL_REF_DECL (symbol);
7203 assemble_constant_contents (DECL_INITIAL (decl), XSTR (symbol, 0),
7204 DECL_ALIGN (decl));
7205 size = get_constant_size (DECL_INITIAL (decl));
7206 offset += size;
7207 if ((flag_sanitize & SANITIZE_ADDRESS)
7208 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
7209 && asan_protect_global (DECL_INITIAL (decl)))
7211 size = asan_red_zone_size (size);
7212 assemble_zeros (size);
7213 offset += size;
7216 else
7218 HOST_WIDE_INT size;
7219 decl = SYMBOL_REF_DECL (symbol);
7220 assemble_variable_contents (decl, XSTR (symbol, 0), false);
7221 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
7222 offset += size;
7223 if ((flag_sanitize & SANITIZE_ADDRESS)
7224 && asan_protect_global (decl))
7226 size = asan_red_zone_size (size);
7227 assemble_zeros (size);
7228 offset += size;
7234 /* A htab_traverse callback used to call output_object_block for
7235 each member of object_block_htab. */
7237 static int
7238 output_object_block_htab (void **slot, void *data ATTRIBUTE_UNUSED)
7240 output_object_block ((struct object_block *) (*slot));
7241 return 1;
7244 /* Output the definitions of all object_blocks. */
7246 void
7247 output_object_blocks (void)
7249 htab_traverse (object_block_htab, output_object_block_htab, NULL);
7252 /* This function provides a possible implementation of the
7253 TARGET_ASM_RECORD_GCC_SWITCHES target hook for ELF targets. When triggered
7254 by -frecord-gcc-switches it creates a new mergeable, string section in the
7255 assembler output file called TARGET_ASM_RECORD_GCC_SWITCHES_SECTION which
7256 contains the switches in ASCII format.
7258 FIXME: This code does not correctly handle double quote characters
7259 that appear inside strings, (it strips them rather than preserving them).
7260 FIXME: ASM_OUTPUT_ASCII, as defined in config/elfos.h will not emit NUL
7261 characters - instead it treats them as sub-string separators. Since
7262 we want to emit NUL strings terminators into the object file we have to use
7263 ASM_OUTPUT_SKIP. */
7266 elf_record_gcc_switches (print_switch_type type, const char * name)
7268 switch (type)
7270 case SWITCH_TYPE_PASSED:
7271 ASM_OUTPUT_ASCII (asm_out_file, name, strlen (name));
7272 ASM_OUTPUT_SKIP (asm_out_file, (unsigned HOST_WIDE_INT) 1);
7273 break;
7275 case SWITCH_TYPE_DESCRIPTIVE:
7276 if (name == NULL)
7278 /* Distinguish between invocations where name is NULL. */
7279 static bool started = false;
7281 if (!started)
7283 section * sec;
7285 sec = get_section (targetm.asm_out.record_gcc_switches_section,
7286 SECTION_DEBUG
7287 | SECTION_MERGE
7288 | SECTION_STRINGS
7289 | (SECTION_ENTSIZE & 1),
7290 NULL);
7291 switch_to_section (sec);
7292 started = true;
7296 default:
7297 break;
7300 /* The return value is currently ignored by the caller, but must be 0.
7301 For -fverbose-asm the return value would be the number of characters
7302 emitted into the assembler file. */
7303 return 0;
7306 /* Emit text to declare externally defined symbols. It is needed to
7307 properly support non-default visibility. */
7308 void
7309 default_elf_asm_output_external (FILE *file ATTRIBUTE_UNUSED,
7310 tree decl,
7311 const char *name ATTRIBUTE_UNUSED)
7313 /* We output the name if and only if TREE_SYMBOL_REFERENCED is
7314 set in order to avoid putting out names that are never really
7315 used. */
7316 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
7317 && targetm.binds_local_p (decl))
7318 maybe_assemble_visibility (decl);
7321 /* The default hook for TARGET_ASM_OUTPUT_SOURCE_FILENAME. */
7323 void
7324 default_asm_output_source_filename (FILE *file, const char *name)
7326 #ifdef ASM_OUTPUT_SOURCE_FILENAME
7327 ASM_OUTPUT_SOURCE_FILENAME (file, name);
7328 #else
7329 fprintf (file, "\t.file\t");
7330 output_quoted_string (file, name);
7331 putc ('\n', file);
7332 #endif
7335 /* Output a file name in the form wanted by System V. */
7337 void
7338 output_file_directive (FILE *asm_file, const char *input_name)
7340 int len;
7341 const char *na;
7343 if (input_name == NULL)
7344 input_name = "<stdin>";
7345 else
7346 input_name = remap_debug_filename (input_name);
7348 len = strlen (input_name);
7349 na = input_name + len;
7351 /* NA gets INPUT_NAME sans directory names. */
7352 while (na > input_name)
7354 if (IS_DIR_SEPARATOR (na[-1]))
7355 break;
7356 na--;
7359 targetm.asm_out.output_source_filename (asm_file, na);
7362 /* Create a DEBUG_EXPR_DECL / DEBUG_EXPR pair from RTL expression
7363 EXP. */
7365 make_debug_expr_from_rtl (const_rtx exp)
7367 tree ddecl = make_node (DEBUG_EXPR_DECL), type;
7368 enum machine_mode mode = GET_MODE (exp);
7369 rtx dval;
7371 DECL_ARTIFICIAL (ddecl) = 1;
7372 if (REG_P (exp) && REG_EXPR (exp))
7373 type = TREE_TYPE (REG_EXPR (exp));
7374 else if (MEM_P (exp) && MEM_EXPR (exp))
7375 type = TREE_TYPE (MEM_EXPR (exp));
7376 else
7377 type = NULL_TREE;
7378 if (type && TYPE_MODE (type) == mode)
7379 TREE_TYPE (ddecl) = type;
7380 else
7381 TREE_TYPE (ddecl) = lang_hooks.types.type_for_mode (mode, 1);
7382 DECL_MODE (ddecl) = mode;
7383 dval = gen_rtx_DEBUG_EXPR (mode);
7384 DEBUG_EXPR_TREE_DECL (dval) = ddecl;
7385 SET_DECL_RTL (ddecl, dval);
7386 return dval;
7389 #ifdef ELF_ASCII_ESCAPES
7390 /* Default ASM_OUTPUT_LIMITED_STRING for ELF targets. */
7392 void
7393 default_elf_asm_output_limited_string (FILE *f, const char *s)
7395 int escape;
7396 unsigned char c;
7398 fputs (STRING_ASM_OP, f);
7399 putc ('"', f);
7400 while (*s != '\0')
7402 c = *s;
7403 escape = ELF_ASCII_ESCAPES[c];
7404 switch (escape)
7406 case 0:
7407 putc (c, f);
7408 break;
7409 case 1:
7410 /* TODO: Print in hex with fast function, important for -flto. */
7411 fprintf (f, "\\%03o", c);
7412 break;
7413 default:
7414 putc ('\\', f);
7415 putc (escape, f);
7416 break;
7418 s++;
7420 putc ('\"', f);
7421 putc ('\n', f);
7424 /* Default ASM_OUTPUT_ASCII for ELF targets. */
7426 void
7427 default_elf_asm_output_ascii (FILE *f, const char *s, unsigned int len)
7429 const char *limit = s + len;
7430 const char *last_null = NULL;
7431 unsigned bytes_in_chunk = 0;
7432 unsigned char c;
7433 int escape;
7435 for (; s < limit; s++)
7437 const char *p;
7439 if (bytes_in_chunk >= 60)
7441 putc ('\"', f);
7442 putc ('\n', f);
7443 bytes_in_chunk = 0;
7446 if (s > last_null)
7448 for (p = s; p < limit && *p != '\0'; p++)
7449 continue;
7450 last_null = p;
7452 else
7453 p = last_null;
7455 if (p < limit && (p - s) <= (long) ELF_STRING_LIMIT)
7457 if (bytes_in_chunk > 0)
7459 putc ('\"', f);
7460 putc ('\n', f);
7461 bytes_in_chunk = 0;
7464 default_elf_asm_output_limited_string (f, s);
7465 s = p;
7467 else
7469 if (bytes_in_chunk == 0)
7470 fputs (ASCII_DATA_ASM_OP "\"", f);
7472 c = *s;
7473 escape = ELF_ASCII_ESCAPES[c];
7474 switch (escape)
7476 case 0:
7477 putc (c, f);
7478 bytes_in_chunk++;
7479 break;
7480 case 1:
7481 /* TODO: Print in hex with fast function, important for -flto. */
7482 fprintf (f, "\\%03o", c);
7483 bytes_in_chunk += 4;
7484 break;
7485 default:
7486 putc ('\\', f);
7487 putc (escape, f);
7488 bytes_in_chunk += 2;
7489 break;
7495 if (bytes_in_chunk > 0)
7497 putc ('\"', f);
7498 putc ('\n', f);
7501 #endif
7503 static GTY(()) section *elf_init_array_section;
7504 static GTY(()) section *elf_fini_array_section;
7506 static section *
7507 get_elf_initfini_array_priority_section (int priority,
7508 bool constructor_p)
7510 section *sec;
7511 if (priority != DEFAULT_INIT_PRIORITY)
7513 char buf[18];
7514 sprintf (buf, "%s.%.5u",
7515 constructor_p ? ".init_array" : ".fini_array",
7516 priority);
7517 sec = get_section (buf, SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
7519 else
7521 if (constructor_p)
7523 if (elf_init_array_section == NULL)
7524 elf_init_array_section
7525 = get_section (".init_array",
7526 SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
7527 sec = elf_init_array_section;
7529 else
7531 if (elf_fini_array_section == NULL)
7532 elf_fini_array_section
7533 = get_section (".fini_array",
7534 SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
7535 sec = elf_fini_array_section;
7538 return sec;
7541 /* Use .init_array section for constructors. */
7543 void
7544 default_elf_init_array_asm_out_constructor (rtx symbol, int priority)
7546 section *sec = get_elf_initfini_array_priority_section (priority,
7547 true);
7548 assemble_addr_to_section (symbol, sec);
7551 /* Use .fini_array section for destructors. */
7553 void
7554 default_elf_fini_array_asm_out_destructor (rtx symbol, int priority)
7556 section *sec = get_elf_initfini_array_priority_section (priority,
7557 false);
7558 assemble_addr_to_section (symbol, sec);
7561 /* Default TARGET_ASM_OUTPUT_IDENT hook.
7563 This is a bit of a cheat. The real default is a no-op, but this
7564 hook is the default for all targets with a .ident directive. */
7566 void
7567 default_asm_output_ident_directive (const char *ident_str)
7569 const char *ident_asm_op = "\t.ident\t";
7571 /* If we are still in the front end, do not write out the string
7572 to asm_out_file. Instead, add a fake top-level asm statement.
7573 This allows the front ends to use this hook without actually
7574 writing to asm_out_file, to handle #ident or Pragma Ident. */
7575 if (cgraph_state == CGRAPH_STATE_PARSING)
7577 char *buf = ACONCAT ((ident_asm_op, "\"", ident_str, "\"\n", NULL));
7578 add_asm_node (build_string (strlen (buf), buf));
7580 else
7581 fprintf (asm_out_file, "%s\"%s\"\n", ident_asm_op, ident_str);
7584 #include "gt-varasm.h"