2013-04-26 François Dumont <fdumont@gcc.gnu.org>
[official-gcc.git] / gcc / varasm.c
blob2532d804214c332fa43170078186ced103e4bc0f
1 /* Output variables, constants and external declarations, for GNU compiler.
2 Copyright (C) 1987-2013 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 "flags.h"
35 #include "function.h"
36 #include "expr.h"
37 #include "hard-reg-set.h"
38 #include "regs.h"
39 #include "output.h"
40 #include "diagnostic-core.h"
41 #include "hashtab.h"
42 #include "ggc.h"
43 #include "langhooks.h"
44 #include "tm_p.h"
45 #include "debug.h"
46 #include "target.h"
47 #include "common/common-target.h"
48 #include "targhooks.h"
49 #include "tree-mudflap.h"
50 #include "cgraph.h"
51 #include "pointer-set.h"
52 #include "asan.h"
54 #ifdef XCOFF_DEBUGGING_INFO
55 #include "xcoffout.h" /* Needed for external data
56 declarations for e.g. AIX 4.x. */
57 #endif
59 /* The (assembler) name of the first globally-visible object output. */
60 extern GTY(()) const char *first_global_object_name;
61 extern GTY(()) const char *weak_global_object_name;
63 const char *first_global_object_name;
64 const char *weak_global_object_name;
66 struct addr_const;
67 struct constant_descriptor_rtx;
68 struct rtx_constant_pool;
70 #define n_deferred_constants (crtl->varasm.deferred_constants)
72 /* Number for making the label on the next
73 constant that is stored in memory. */
75 static GTY(()) int const_labelno;
77 /* Carry information from ASM_DECLARE_OBJECT_NAME
78 to ASM_FINISH_DECLARE_OBJECT. */
80 int size_directive_output;
82 /* The last decl for which assemble_variable was called,
83 if it did ASM_DECLARE_OBJECT_NAME.
84 If the last call to assemble_variable didn't do that,
85 this holds 0. */
87 tree last_assemble_variable_decl;
89 /* The following global variable indicates if the first basic block
90 in a function belongs to the cold partition or not. */
92 bool first_function_block_is_cold;
94 /* We give all constants their own alias set. Perhaps redundant with
95 MEM_READONLY_P, but pre-dates it. */
97 static alias_set_type const_alias_set;
99 /* Whether we saw any functions with no_split_stack. */
101 static bool saw_no_split_stack;
103 static const char *strip_reg_name (const char *);
104 static int contains_pointers_p (tree);
105 #ifdef ASM_OUTPUT_EXTERNAL
106 static bool incorporeal_function_p (tree);
107 #endif
108 static void decode_addr_const (tree, struct addr_const *);
109 static hashval_t const_desc_hash (const void *);
110 static int const_desc_eq (const void *, const void *);
111 static hashval_t const_hash_1 (const tree);
112 static int compare_constant (const tree, const tree);
113 static tree copy_constant (tree);
114 static void output_constant_def_contents (rtx);
115 static void output_addressed_constants (tree);
116 static unsigned HOST_WIDE_INT array_size_for_constructor (tree);
117 static unsigned min_align (unsigned, unsigned);
118 static void globalize_decl (tree);
119 static bool decl_readonly_section_1 (enum section_category);
120 #ifdef BSS_SECTION_ASM_OP
121 #ifdef ASM_OUTPUT_ALIGNED_BSS
122 static void asm_output_aligned_bss (FILE *, tree, const char *,
123 unsigned HOST_WIDE_INT, int)
124 ATTRIBUTE_UNUSED;
125 #endif
126 #endif /* BSS_SECTION_ASM_OP */
127 static void mark_weak (tree);
128 static void output_constant_pool (const char *, tree);
130 /* Well-known sections, each one associated with some sort of *_ASM_OP. */
131 section *text_section;
132 section *data_section;
133 section *readonly_data_section;
134 section *sdata_section;
135 section *ctors_section;
136 section *dtors_section;
137 section *bss_section;
138 section *sbss_section;
140 /* Various forms of common section. All are guaranteed to be nonnull. */
141 section *tls_comm_section;
142 section *comm_section;
143 section *lcomm_section;
145 /* A SECTION_NOSWITCH section used for declaring global BSS variables.
146 May be null. */
147 section *bss_noswitch_section;
149 /* The section that holds the main exception table, when known. The section
150 is set either by the target's init_sections hook or by the first call to
151 switch_to_exception_section. */
152 section *exception_section;
154 /* The section that holds the DWARF2 frame unwind information, when known.
155 The section is set either by the target's init_sections hook or by the
156 first call to switch_to_eh_frame_section. */
157 section *eh_frame_section;
159 /* asm_out_file's current section. This is NULL if no section has yet
160 been selected or if we lose track of what the current section is. */
161 section *in_section;
163 /* True if code for the current function is currently being directed
164 at the cold section. */
165 bool in_cold_section_p;
167 /* A linked list of all the unnamed sections. */
168 static GTY(()) section *unnamed_sections;
170 /* Return a nonzero value if DECL has a section attribute. */
171 #define IN_NAMED_SECTION(DECL) \
172 ((TREE_CODE (DECL) == FUNCTION_DECL || TREE_CODE (DECL) == VAR_DECL) \
173 && DECL_SECTION_NAME (DECL) != NULL_TREE)
175 /* Hash table of named sections. */
176 static GTY((param_is (section))) htab_t section_htab;
178 /* A table of object_blocks, indexed by section. */
179 static GTY((param_is (struct object_block))) htab_t object_block_htab;
181 /* The next number to use for internal anchor labels. */
182 static GTY(()) int anchor_labelno;
184 /* A pool of constants that can be shared between functions. */
185 static GTY(()) struct rtx_constant_pool *shared_constant_pool;
187 /* Helper routines for maintaining section_htab. */
189 static int
190 section_entry_eq (const void *p1, const void *p2)
192 const section *old = (const section *) p1;
193 const char *new_name = (const char *) p2;
195 return strcmp (old->named.name, new_name) == 0;
198 static hashval_t
199 section_entry_hash (const void *p)
201 const section *old = (const section *) p;
202 return htab_hash_string (old->named.name);
205 /* Return a hash value for section SECT. */
207 static hashval_t
208 hash_section (section *sect)
210 if (sect->common.flags & SECTION_NAMED)
211 return htab_hash_string (sect->named.name);
212 return sect->common.flags;
215 /* Helper routines for maintaining object_block_htab. */
217 static int
218 object_block_entry_eq (const void *p1, const void *p2)
220 const struct object_block *old = (const struct object_block *) p1;
221 const section *new_section = (const section *) p2;
223 return old->sect == new_section;
226 static hashval_t
227 object_block_entry_hash (const void *p)
229 const struct object_block *old = (const struct object_block *) p;
230 return hash_section (old->sect);
233 /* Return a new unnamed section with the given fields. */
235 section *
236 get_unnamed_section (unsigned int flags, void (*callback) (const void *),
237 const void *data)
239 section *sect;
241 sect = ggc_alloc_section ();
242 sect->unnamed.common.flags = flags | SECTION_UNNAMED;
243 sect->unnamed.callback = callback;
244 sect->unnamed.data = data;
245 sect->unnamed.next = unnamed_sections;
247 unnamed_sections = sect;
248 return sect;
251 /* Return a SECTION_NOSWITCH section with the given fields. */
253 static section *
254 get_noswitch_section (unsigned int flags, noswitch_section_callback callback)
256 section *sect;
258 sect = ggc_alloc_section ();
259 sect->noswitch.common.flags = flags | SECTION_NOSWITCH;
260 sect->noswitch.callback = callback;
262 return sect;
265 /* Return the named section structure associated with NAME. Create
266 a new section with the given fields if no such structure exists. */
268 section *
269 get_section (const char *name, unsigned int flags, tree decl)
271 section *sect, **slot;
273 slot = (section **)
274 htab_find_slot_with_hash (section_htab, name,
275 htab_hash_string (name), INSERT);
276 flags |= SECTION_NAMED;
277 if (*slot == NULL)
279 sect = ggc_alloc_section ();
280 sect->named.common.flags = flags;
281 sect->named.name = ggc_strdup (name);
282 sect->named.decl = decl;
283 *slot = sect;
285 else
287 sect = *slot;
288 if ((sect->common.flags & ~SECTION_DECLARED) != flags
289 && ((sect->common.flags | flags) & SECTION_OVERRIDE) == 0)
291 /* It is fine if one of the section flags is
292 SECTION_WRITE | SECTION_RELRO and the other has none of these
293 flags (i.e. read-only) in named sections and either the
294 section hasn't been declared yet or has been declared as writable.
295 In that case just make sure the resulting flags are
296 SECTION_WRITE | SECTION_RELRO, ie. writable only because of
297 relocations. */
298 if (((sect->common.flags ^ flags) & (SECTION_WRITE | SECTION_RELRO))
299 == (SECTION_WRITE | SECTION_RELRO)
300 && (sect->common.flags
301 & ~(SECTION_DECLARED | SECTION_WRITE | SECTION_RELRO))
302 == (flags & ~(SECTION_WRITE | SECTION_RELRO))
303 && ((sect->common.flags & SECTION_DECLARED) == 0
304 || (sect->common.flags & SECTION_WRITE)))
306 sect->common.flags |= (SECTION_WRITE | SECTION_RELRO);
307 return sect;
309 /* Sanity check user variables for flag changes. */
310 if (decl == 0)
311 decl = sect->named.decl;
312 gcc_assert (decl);
313 if (sect->named.decl == NULL)
314 error ("%+D causes a section type conflict", decl);
315 else
317 error ("%+D causes a section type conflict with %D",
318 decl, sect->named.decl);
319 if (decl != sect->named.decl)
320 inform (DECL_SOURCE_LOCATION (sect->named.decl),
321 "%qD was declared here", sect->named.decl);
323 /* Make sure we don't error about one section multiple times. */
324 sect->common.flags |= SECTION_OVERRIDE;
327 return sect;
330 /* Return true if the current compilation mode benefits from having
331 objects grouped into blocks. */
333 static bool
334 use_object_blocks_p (void)
336 return flag_section_anchors;
339 /* Return the object_block structure for section SECT. Create a new
340 structure if we haven't created one already. Return null if SECT
341 itself is null. */
343 static struct object_block *
344 get_block_for_section (section *sect)
346 struct object_block *block;
347 void **slot;
349 if (sect == NULL)
350 return NULL;
352 slot = htab_find_slot_with_hash (object_block_htab, sect,
353 hash_section (sect), INSERT);
354 block = (struct object_block *) *slot;
355 if (block == NULL)
357 block = ggc_alloc_cleared_object_block ();
358 block->sect = sect;
359 *slot = block;
361 return block;
364 /* Create a symbol with label LABEL and place it at byte offset
365 OFFSET in BLOCK. OFFSET can be negative if the symbol's offset
366 is not yet known. LABEL must be a garbage-collected string. */
368 static rtx
369 create_block_symbol (const char *label, struct object_block *block,
370 HOST_WIDE_INT offset)
372 rtx symbol;
373 unsigned int size;
375 /* Create the extended SYMBOL_REF. */
376 size = RTX_HDR_SIZE + sizeof (struct block_symbol);
377 symbol = ggc_alloc_rtx_def (size);
379 /* Initialize the normal SYMBOL_REF fields. */
380 memset (symbol, 0, size);
381 PUT_CODE (symbol, SYMBOL_REF);
382 PUT_MODE (symbol, Pmode);
383 XSTR (symbol, 0) = label;
384 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_HAS_BLOCK_INFO;
386 /* Initialize the block_symbol stuff. */
387 SYMBOL_REF_BLOCK (symbol) = block;
388 SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
390 return symbol;
393 /* Return a section with a particular name and with whatever SECTION_*
394 flags section_type_flags deems appropriate. The name of the section
395 is taken from NAME if nonnull, otherwise it is taken from DECL's
396 DECL_SECTION_NAME. DECL is the decl associated with the section
397 (see the section comment for details) and RELOC is as for
398 section_type_flags. */
400 section *
401 get_named_section (tree decl, const char *name, int reloc)
403 unsigned int flags;
405 gcc_assert (!decl || DECL_P (decl));
406 if (name == NULL)
407 name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
409 flags = targetm.section_type_flags (decl, name, reloc);
411 return get_section (name, flags, decl);
414 /* If required, set DECL_SECTION_NAME to a unique name. */
416 void
417 resolve_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED,
418 int flag_function_or_data_sections)
420 if (DECL_SECTION_NAME (decl) == NULL_TREE
421 && targetm_common.have_named_sections
422 && (flag_function_or_data_sections
423 || DECL_ONE_ONLY (decl)))
425 targetm.asm_out.unique_section (decl, reloc);
426 DECL_HAS_IMPLICIT_SECTION_NAME_P (decl) = true;
430 #ifdef BSS_SECTION_ASM_OP
432 #ifdef ASM_OUTPUT_ALIGNED_BSS
434 /* Utility function for targets to use in implementing
435 ASM_OUTPUT_ALIGNED_BSS.
436 ??? It is believed that this function will work in most cases so such
437 support is localized here. */
439 static void
440 asm_output_aligned_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
441 const char *name, unsigned HOST_WIDE_INT size,
442 int align)
444 switch_to_section (bss_section);
445 ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
446 #ifdef ASM_DECLARE_OBJECT_NAME
447 last_assemble_variable_decl = decl;
448 ASM_DECLARE_OBJECT_NAME (file, name, decl);
449 #else
450 /* Standard thing is just output label for the object. */
451 ASM_OUTPUT_LABEL (file, name);
452 #endif /* ASM_DECLARE_OBJECT_NAME */
453 ASM_OUTPUT_SKIP (file, size ? size : 1);
456 #endif
458 #endif /* BSS_SECTION_ASM_OP */
460 #ifndef USE_SELECT_SECTION_FOR_FUNCTIONS
461 /* Return the hot section for function DECL. Return text_section for
462 null DECLs. */
464 static section *
465 hot_function_section (tree decl)
467 if (decl != NULL_TREE
468 && DECL_SECTION_NAME (decl) != NULL_TREE
469 && targetm_common.have_named_sections)
470 return get_named_section (decl, NULL, 0);
471 else
472 return text_section;
474 #endif
476 /* Return section for TEXT_SECTION_NAME if DECL or DECL_SECTION_NAME (DECL)
477 is NULL.
479 When DECL_SECTION_NAME is non-NULL and it is implicit section and
480 NAMED_SECTION_SUFFIX is non-NULL, then produce section called
481 concatenate the name with NAMED_SECTION_SUFFIX.
482 Otherwise produce "TEXT_SECTION_NAME.IMPLICIT_NAME". */
484 section *
485 get_named_text_section (tree decl,
486 const char *text_section_name,
487 const char *named_section_suffix)
489 if (decl && DECL_SECTION_NAME (decl))
491 if (named_section_suffix)
493 tree dsn = DECL_SECTION_NAME (decl);
494 const char *stripped_name;
495 char *name, *buffer;
497 name = (char *) alloca (TREE_STRING_LENGTH (dsn) + 1);
498 memcpy (name, TREE_STRING_POINTER (dsn),
499 TREE_STRING_LENGTH (dsn) + 1);
501 stripped_name = targetm.strip_name_encoding (name);
503 buffer = ACONCAT ((stripped_name, named_section_suffix, NULL));
504 return get_named_section (decl, buffer, 0);
506 else if (DECL_HAS_IMPLICIT_SECTION_NAME_P (decl))
508 const char *name;
510 /* Do not try to split gnu_linkonce functions. This gets somewhat
511 slipperly. */
512 if (DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP)
513 return NULL;
514 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
515 name = targetm.strip_name_encoding (name);
516 return get_named_section (decl, ACONCAT ((text_section_name, ".",
517 name, NULL)), 0);
519 else
520 return NULL;
522 return get_named_section (decl, text_section_name, 0);
525 /* Choose named function section based on its frequency. */
527 section *
528 default_function_section (tree decl, enum node_frequency freq,
529 bool startup, bool exit)
531 #if defined HAVE_LD_EH_GC_SECTIONS && defined HAVE_LD_EH_GC_SECTIONS_BUG
532 /* Old GNU linkers have buggy --gc-section support, which sometimes
533 results in .gcc_except_table* sections being garbage collected. */
534 if (decl
535 && DECL_SECTION_NAME (decl)
536 && DECL_HAS_IMPLICIT_SECTION_NAME_P (decl))
537 return NULL;
538 #endif
540 if (!flag_reorder_functions
541 || !targetm_common.have_named_sections)
542 return NULL;
543 /* Startup code should go to startup subsection unless it is
544 unlikely executed (this happens especially with function splitting
545 where we can split away unnecessary parts of static constructors. */
546 if (startup && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
547 return get_named_text_section (decl, ".text.startup", NULL);
549 /* Similarly for exit. */
550 if (exit && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
551 return get_named_text_section (decl, ".text.exit", NULL);
553 /* Group cold functions together, similarly for hot code. */
554 switch (freq)
556 case NODE_FREQUENCY_UNLIKELY_EXECUTED:
557 return get_named_text_section (decl, ".text.unlikely", NULL);
558 case NODE_FREQUENCY_HOT:
559 return get_named_text_section (decl, ".text.hot", NULL);
560 default:
561 return NULL;
565 /* Return the section for function DECL.
567 If DECL is NULL_TREE, return the text section. We can be passed
568 NULL_TREE under some circumstances by dbxout.c at least.
570 If FORCE_COLD is true, return cold function section ignoring
571 the frequency info of cgraph_node. */
573 static section *
574 function_section_1 (tree decl, bool force_cold)
576 section *section = NULL;
577 enum node_frequency freq = NODE_FREQUENCY_NORMAL;
578 bool startup = false, exit = false;
580 if (decl)
582 struct cgraph_node *node = cgraph_get_node (decl);
584 if (node)
586 freq = node->frequency;
587 startup = node->only_called_at_startup;
588 exit = node->only_called_at_exit;
591 if (force_cold)
592 freq = NODE_FREQUENCY_UNLIKELY_EXECUTED;
594 #ifdef USE_SELECT_SECTION_FOR_FUNCTIONS
595 if (decl != NULL_TREE
596 && DECL_SECTION_NAME (decl) != NULL_TREE)
598 if (targetm.asm_out.function_section)
599 section = targetm.asm_out.function_section (decl, freq,
600 startup, exit);
601 if (section)
602 return section;
603 return get_named_section (decl, NULL, 0);
605 else
606 return targetm.asm_out.select_section
607 (decl, freq == NODE_FREQUENCY_UNLIKELY_EXECUTED,
608 DECL_ALIGN (decl));
609 #else
610 if (targetm.asm_out.function_section)
611 section = targetm.asm_out.function_section (decl, freq, startup, exit);
612 if (section)
613 return section;
614 return hot_function_section (decl);
615 #endif
618 /* Return the section for function DECL.
620 If DECL is NULL_TREE, return the text section. We can be passed
621 NULL_TREE under some circumstances by dbxout.c at least. */
623 section *
624 function_section (tree decl)
626 /* Handle cases where function splitting code decides
627 to put function entry point into unlikely executed section
628 despite the fact that the function itself is not cold
629 (i.e. it is called rarely but contains a hot loop that is
630 better to live in hot subsection for the code locality). */
631 return function_section_1 (decl,
632 first_function_block_is_cold);
635 /* Return the section for the current function, take IN_COLD_SECTION_P
636 into account. */
638 section *
639 current_function_section (void)
641 return function_section_1 (current_function_decl, in_cold_section_p);
644 /* Tell assembler to switch to unlikely-to-be-executed text section. */
646 section *
647 unlikely_text_section (void)
649 return function_section_1 (current_function_decl, true);
652 /* When called within a function context, return true if the function
653 has been assigned a cold text section and if SECT is that section.
654 When called outside a function context, return true if SECT is the
655 default cold section. */
657 bool
658 unlikely_text_section_p (section *sect)
660 return sect == function_section_1 (current_function_decl, true);
663 /* Return the read-only data section associated with function DECL. */
665 section *
666 default_function_rodata_section (tree decl)
668 if (decl != NULL_TREE && DECL_SECTION_NAME (decl))
670 const char *name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
672 if (DECL_ONE_ONLY (decl) && HAVE_COMDAT_GROUP)
674 const char *dot;
675 size_t len;
676 char* rname;
678 dot = strchr (name + 1, '.');
679 if (!dot)
680 dot = name;
681 len = strlen (dot) + 8;
682 rname = (char *) alloca (len);
684 strcpy (rname, ".rodata");
685 strcat (rname, dot);
686 return get_section (rname, SECTION_LINKONCE, decl);
688 /* For .gnu.linkonce.t.foo we want to use .gnu.linkonce.r.foo. */
689 else if (DECL_ONE_ONLY (decl)
690 && strncmp (name, ".gnu.linkonce.t.", 16) == 0)
692 size_t len = strlen (name) + 1;
693 char *rname = (char *) alloca (len);
695 memcpy (rname, name, len);
696 rname[14] = 'r';
697 return get_section (rname, SECTION_LINKONCE, decl);
699 /* For .text.foo we want to use .rodata.foo. */
700 else if (flag_function_sections && flag_data_sections
701 && strncmp (name, ".text.", 6) == 0)
703 size_t len = strlen (name) + 1;
704 char *rname = (char *) alloca (len + 2);
706 memcpy (rname, ".rodata", 7);
707 memcpy (rname + 7, name + 5, len - 5);
708 return get_section (rname, 0, decl);
712 return readonly_data_section;
715 /* Return the read-only data section associated with function DECL
716 for targets where that section should be always the single
717 readonly data section. */
719 section *
720 default_no_function_rodata_section (tree decl ATTRIBUTE_UNUSED)
722 return readonly_data_section;
725 /* Return the section to use for string merging. */
727 static section *
728 mergeable_string_section (tree decl ATTRIBUTE_UNUSED,
729 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
730 unsigned int flags ATTRIBUTE_UNUSED)
732 HOST_WIDE_INT len;
734 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
735 && TREE_CODE (decl) == STRING_CST
736 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
737 && align <= 256
738 && (len = int_size_in_bytes (TREE_TYPE (decl))) > 0
739 && TREE_STRING_LENGTH (decl) >= len)
741 enum machine_mode mode;
742 unsigned int modesize;
743 const char *str;
744 HOST_WIDE_INT i;
745 int j, unit;
746 const char *prefix = targetm.asm_out.mergeable_rodata_prefix;
747 char *name = (char *) alloca (strlen (prefix) + 30);
749 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (decl)));
750 modesize = GET_MODE_BITSIZE (mode);
751 if (modesize >= 8 && modesize <= 256
752 && (modesize & (modesize - 1)) == 0)
754 if (align < modesize)
755 align = modesize;
757 str = TREE_STRING_POINTER (decl);
758 unit = GET_MODE_SIZE (mode);
760 /* Check for embedded NUL characters. */
761 for (i = 0; i < len; i += unit)
763 for (j = 0; j < unit; j++)
764 if (str[i + j] != '\0')
765 break;
766 if (j == unit)
767 break;
769 if (i == len - unit)
771 sprintf (name, "%s.str%d.%d", prefix,
772 modesize / 8, (int) (align / 8));
773 flags |= (modesize / 8) | SECTION_MERGE | SECTION_STRINGS;
774 return get_section (name, flags, NULL);
779 return readonly_data_section;
782 /* Return the section to use for constant merging. */
784 section *
785 mergeable_constant_section (enum machine_mode mode ATTRIBUTE_UNUSED,
786 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
787 unsigned int flags ATTRIBUTE_UNUSED)
789 unsigned int modesize = GET_MODE_BITSIZE (mode);
791 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
792 && mode != VOIDmode
793 && mode != BLKmode
794 && modesize <= align
795 && align >= 8
796 && align <= 256
797 && (align & (align - 1)) == 0)
799 const char *prefix = targetm.asm_out.mergeable_rodata_prefix;
800 char *name = (char *) alloca (strlen (prefix) + 30);
802 sprintf (name, "%s.cst%d", prefix, (int) (align / 8));
803 flags |= (align / 8) | SECTION_MERGE;
804 return get_section (name, flags, NULL);
806 return readonly_data_section;
809 /* Given NAME, a putative register name, discard any customary prefixes. */
811 static const char *
812 strip_reg_name (const char *name)
814 #ifdef REGISTER_PREFIX
815 if (!strncmp (name, REGISTER_PREFIX, strlen (REGISTER_PREFIX)))
816 name += strlen (REGISTER_PREFIX);
817 #endif
818 if (name[0] == '%' || name[0] == '#')
819 name++;
820 return name;
823 /* The user has asked for a DECL to have a particular name. Set (or
824 change) it in such a way that we don't prefix an underscore to
825 it. */
826 void
827 set_user_assembler_name (tree decl, const char *name)
829 char *starred = (char *) alloca (strlen (name) + 2);
830 starred[0] = '*';
831 strcpy (starred + 1, name);
832 change_decl_assembler_name (decl, get_identifier (starred));
833 SET_DECL_RTL (decl, NULL_RTX);
836 /* Decode an `asm' spec for a declaration as a register name.
837 Return the register number, or -1 if nothing specified,
838 or -2 if the ASMSPEC is not `cc' or `memory' and is not recognized,
839 or -3 if ASMSPEC is `cc' and is not recognized,
840 or -4 if ASMSPEC is `memory' and is not recognized.
841 Accept an exact spelling or a decimal number.
842 Prefixes such as % are optional. */
845 decode_reg_name_and_count (const char *asmspec, int *pnregs)
847 /* Presume just one register is clobbered. */
848 *pnregs = 1;
850 if (asmspec != 0)
852 int i;
854 /* Get rid of confusing prefixes. */
855 asmspec = strip_reg_name (asmspec);
857 /* Allow a decimal number as a "register name". */
858 for (i = strlen (asmspec) - 1; i >= 0; i--)
859 if (! ISDIGIT (asmspec[i]))
860 break;
861 if (asmspec[0] != 0 && i < 0)
863 i = atoi (asmspec);
864 if (i < FIRST_PSEUDO_REGISTER && i >= 0)
865 return i;
866 else
867 return -2;
870 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
871 if (reg_names[i][0]
872 && ! strcmp (asmspec, strip_reg_name (reg_names[i])))
873 return i;
875 #ifdef OVERLAPPING_REGISTER_NAMES
877 static const struct
879 const char *const name;
880 const int number;
881 const int nregs;
882 } table[] = OVERLAPPING_REGISTER_NAMES;
884 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
885 if (table[i].name[0]
886 && ! strcmp (asmspec, table[i].name))
888 *pnregs = table[i].nregs;
889 return table[i].number;
892 #endif /* OVERLAPPING_REGISTER_NAMES */
894 #ifdef ADDITIONAL_REGISTER_NAMES
896 static const struct { const char *const name; const int number; } table[]
897 = ADDITIONAL_REGISTER_NAMES;
899 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
900 if (table[i].name[0]
901 && ! strcmp (asmspec, table[i].name))
902 return table[i].number;
904 #endif /* ADDITIONAL_REGISTER_NAMES */
906 if (!strcmp (asmspec, "memory"))
907 return -4;
909 if (!strcmp (asmspec, "cc"))
910 return -3;
912 return -2;
915 return -1;
919 decode_reg_name (const char *name)
921 int count;
922 return decode_reg_name_and_count (name, &count);
926 /* Return true if DECL's initializer is suitable for a BSS section. */
928 bool
929 bss_initializer_p (const_tree decl)
931 return (DECL_INITIAL (decl) == NULL
932 || DECL_INITIAL (decl) == error_mark_node
933 || (flag_zero_initialized_in_bss
934 /* Leave constant zeroes in .rodata so they
935 can be shared. */
936 && !TREE_READONLY (decl)
937 && initializer_zerop (DECL_INITIAL (decl))));
940 /* Compute the alignment of variable specified by DECL.
941 DONT_OUTPUT_DATA is from assemble_variable. */
943 void
944 align_variable (tree decl, bool dont_output_data)
946 unsigned int align = DECL_ALIGN (decl);
948 /* In the case for initialing an array whose length isn't specified,
949 where we have not yet been able to do the layout,
950 figure out the proper alignment now. */
951 if (dont_output_data && DECL_SIZE (decl) == 0
952 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
953 align = MAX (align, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (decl))));
955 /* Some object file formats have a maximum alignment which they support.
956 In particular, a.out format supports a maximum alignment of 4. */
957 if (align > MAX_OFILE_ALIGNMENT)
959 warning (0, "alignment of %q+D is greater than maximum object "
960 "file alignment. Using %d", decl,
961 MAX_OFILE_ALIGNMENT/BITS_PER_UNIT);
962 align = MAX_OFILE_ALIGNMENT;
965 /* On some machines, it is good to increase alignment sometimes. */
966 if (! DECL_USER_ALIGN (decl))
968 #ifdef DATA_ALIGNMENT
969 unsigned int data_align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
970 /* Don't increase alignment too much for TLS variables - TLS space
971 is too precious. */
972 if (! DECL_THREAD_LOCAL_P (decl) || data_align <= BITS_PER_WORD)
973 align = data_align;
974 #endif
975 #ifdef CONSTANT_ALIGNMENT
976 if (DECL_INITIAL (decl) != 0 && DECL_INITIAL (decl) != error_mark_node)
978 unsigned int const_align = CONSTANT_ALIGNMENT (DECL_INITIAL (decl),
979 align);
980 /* Don't increase alignment too much for TLS variables - TLS space
981 is too precious. */
982 if (! DECL_THREAD_LOCAL_P (decl) || const_align <= BITS_PER_WORD)
983 align = const_align;
985 #endif
988 /* Reset the alignment in case we have made it tighter, so we can benefit
989 from it in get_pointer_alignment. */
990 DECL_ALIGN (decl) = align;
993 /* Return the section into which the given VAR_DECL or CONST_DECL
994 should be placed. PREFER_NOSWITCH_P is true if a noswitch
995 section should be used wherever possible. */
997 section *
998 get_variable_section (tree decl, bool prefer_noswitch_p)
1000 addr_space_t as = ADDR_SPACE_GENERIC;
1001 int reloc;
1003 if (TREE_TYPE (decl) != error_mark_node)
1004 as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
1006 if (DECL_COMMON (decl))
1008 /* If the decl has been given an explicit section name, or it resides
1009 in a non-generic address space, then it isn't common, and shouldn't
1010 be handled as such. */
1011 gcc_assert (DECL_SECTION_NAME (decl) == NULL
1012 && ADDR_SPACE_GENERIC_P (as));
1013 if (DECL_THREAD_LOCAL_P (decl))
1014 return tls_comm_section;
1015 else if (TREE_PUBLIC (decl) && bss_initializer_p (decl))
1016 return comm_section;
1019 if (DECL_INITIAL (decl) == error_mark_node)
1020 reloc = contains_pointers_p (TREE_TYPE (decl)) ? 3 : 0;
1021 else if (DECL_INITIAL (decl))
1022 reloc = compute_reloc_for_constant (DECL_INITIAL (decl));
1023 else
1024 reloc = 0;
1026 resolve_unique_section (decl, reloc, flag_data_sections);
1027 if (IN_NAMED_SECTION (decl))
1028 return get_named_section (decl, NULL, reloc);
1030 if (ADDR_SPACE_GENERIC_P (as)
1031 && !DECL_THREAD_LOCAL_P (decl)
1032 && !(prefer_noswitch_p && targetm.have_switchable_bss_sections)
1033 && bss_initializer_p (decl))
1035 if (!TREE_PUBLIC (decl)
1036 && !(flag_asan && asan_protect_global (decl)))
1037 return lcomm_section;
1038 if (bss_noswitch_section)
1039 return bss_noswitch_section;
1042 return targetm.asm_out.select_section (decl, reloc, DECL_ALIGN (decl));
1045 /* Return the block into which object_block DECL should be placed. */
1047 static struct object_block *
1048 get_block_for_decl (tree decl)
1050 section *sect;
1052 if (TREE_CODE (decl) == VAR_DECL)
1054 /* The object must be defined in this translation unit. */
1055 if (DECL_EXTERNAL (decl))
1056 return NULL;
1058 /* There's no point using object blocks for something that is
1059 isolated by definition. */
1060 if (DECL_ONE_ONLY (decl))
1061 return NULL;
1064 /* We can only calculate block offsets if the decl has a known
1065 constant size. */
1066 if (DECL_SIZE_UNIT (decl) == NULL)
1067 return NULL;
1068 if (!host_integerp (DECL_SIZE_UNIT (decl), 1))
1069 return NULL;
1071 /* Find out which section should contain DECL. We cannot put it into
1072 an object block if it requires a standalone definition. */
1073 if (TREE_CODE (decl) == VAR_DECL)
1074 align_variable (decl, 0);
1075 sect = get_variable_section (decl, true);
1076 if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
1077 return NULL;
1079 return get_block_for_section (sect);
1082 /* Make sure block symbol SYMBOL is in block BLOCK. */
1084 static void
1085 change_symbol_block (rtx symbol, struct object_block *block)
1087 if (block != SYMBOL_REF_BLOCK (symbol))
1089 gcc_assert (SYMBOL_REF_BLOCK_OFFSET (symbol) < 0);
1090 SYMBOL_REF_BLOCK (symbol) = block;
1094 /* Return true if it is possible to put DECL in an object_block. */
1096 static bool
1097 use_blocks_for_decl_p (tree decl)
1099 /* Only data DECLs can be placed into object blocks. */
1100 if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != CONST_DECL)
1101 return false;
1103 /* Detect decls created by dw2_force_const_mem. Such decls are
1104 special because DECL_INITIAL doesn't specify the decl's true value.
1105 dw2_output_indirect_constants will instead call assemble_variable
1106 with dont_output_data set to 1 and then print the contents itself. */
1107 if (DECL_INITIAL (decl) == decl)
1108 return false;
1110 /* If this decl is an alias, then we don't want to emit a
1111 definition. */
1112 if (lookup_attribute ("alias", DECL_ATTRIBUTES (decl)))
1113 return false;
1115 return targetm.use_blocks_for_decl_p (decl);
1118 /* Create the DECL_RTL for a VAR_DECL or FUNCTION_DECL. DECL should
1119 have static storage duration. In other words, it should not be an
1120 automatic variable, including PARM_DECLs.
1122 There is, however, one exception: this function handles variables
1123 explicitly placed in a particular register by the user.
1125 This is never called for PARM_DECL nodes. */
1127 void
1128 make_decl_rtl (tree decl)
1130 const char *name = 0;
1131 int reg_number;
1132 rtx x;
1134 /* Check that we are not being given an automatic variable. */
1135 gcc_assert (TREE_CODE (decl) != PARM_DECL
1136 && TREE_CODE (decl) != RESULT_DECL);
1138 /* A weak alias has TREE_PUBLIC set but not the other bits. */
1139 gcc_assert (TREE_CODE (decl) != VAR_DECL
1140 || TREE_STATIC (decl)
1141 || TREE_PUBLIC (decl)
1142 || DECL_EXTERNAL (decl)
1143 || DECL_REGISTER (decl));
1145 /* And that we were not given a type or a label. */
1146 gcc_assert (TREE_CODE (decl) != TYPE_DECL
1147 && TREE_CODE (decl) != LABEL_DECL);
1149 /* For a duplicate declaration, we can be called twice on the
1150 same DECL node. Don't discard the RTL already made. */
1151 if (DECL_RTL_SET_P (decl))
1153 /* If the old RTL had the wrong mode, fix the mode. */
1154 x = DECL_RTL (decl);
1155 if (GET_MODE (x) != DECL_MODE (decl))
1156 SET_DECL_RTL (decl, adjust_address_nv (x, DECL_MODE (decl), 0));
1158 if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1159 return;
1161 /* ??? Another way to do this would be to maintain a hashed
1162 table of such critters. Instead of adding stuff to a DECL
1163 to give certain attributes to it, we could use an external
1164 hash map from DECL to set of attributes. */
1166 /* Let the target reassign the RTL if it wants.
1167 This is necessary, for example, when one machine specific
1168 decl attribute overrides another. */
1169 targetm.encode_section_info (decl, DECL_RTL (decl), false);
1171 /* If the symbol has a SYMBOL_REF_BLOCK field, update it based
1172 on the new decl information. */
1173 if (MEM_P (x)
1174 && GET_CODE (XEXP (x, 0)) == SYMBOL_REF
1175 && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (x, 0)))
1176 change_symbol_block (XEXP (x, 0), get_block_for_decl (decl));
1178 /* Make this function static known to the mudflap runtime. */
1179 if (flag_mudflap && TREE_CODE (decl) == VAR_DECL)
1180 mudflap_enqueue_decl (decl);
1182 return;
1185 /* If this variable belongs to the global constant pool, retrieve the
1186 pre-computed RTL or recompute it in LTO mode. */
1187 if (TREE_CODE (decl) == VAR_DECL && DECL_IN_CONSTANT_POOL (decl))
1189 SET_DECL_RTL (decl, output_constant_def (DECL_INITIAL (decl), 1));
1190 return;
1193 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
1195 if (name[0] != '*' && TREE_CODE (decl) != FUNCTION_DECL
1196 && DECL_REGISTER (decl))
1198 error ("register name not specified for %q+D", decl);
1200 else if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1202 const char *asmspec = name+1;
1203 enum machine_mode mode = DECL_MODE (decl);
1204 reg_number = decode_reg_name (asmspec);
1205 /* First detect errors in declaring global registers. */
1206 if (reg_number == -1)
1207 error ("register name not specified for %q+D", decl);
1208 else if (reg_number < 0)
1209 error ("invalid register name for %q+D", decl);
1210 else if (mode == BLKmode)
1211 error ("data type of %q+D isn%'t suitable for a register",
1212 decl);
1213 else if (!in_hard_reg_set_p (accessible_reg_set, mode, reg_number))
1214 error ("the register specified for %q+D cannot be accessed"
1215 " by the current target", decl);
1216 else if (!in_hard_reg_set_p (operand_reg_set, mode, reg_number))
1217 error ("the register specified for %q+D is not general enough"
1218 " to be used as a register variable", decl);
1219 else if (!HARD_REGNO_MODE_OK (reg_number, mode))
1220 error ("register specified for %q+D isn%'t suitable for data type",
1221 decl);
1222 /* Now handle properly declared static register variables. */
1223 else
1225 int nregs;
1227 if (DECL_INITIAL (decl) != 0 && TREE_STATIC (decl))
1229 DECL_INITIAL (decl) = 0;
1230 error ("global register variable has initial value");
1232 if (TREE_THIS_VOLATILE (decl))
1233 warning (OPT_Wvolatile_register_var,
1234 "optimization may eliminate reads and/or "
1235 "writes to register variables");
1237 /* If the user specified one of the eliminables registers here,
1238 e.g., FRAME_POINTER_REGNUM, we don't want to get this variable
1239 confused with that register and be eliminated. This usage is
1240 somewhat suspect... */
1242 SET_DECL_RTL (decl, gen_rtx_raw_REG (mode, reg_number));
1243 ORIGINAL_REGNO (DECL_RTL (decl)) = reg_number;
1244 REG_USERVAR_P (DECL_RTL (decl)) = 1;
1246 if (TREE_STATIC (decl))
1248 /* Make this register global, so not usable for anything
1249 else. */
1250 #ifdef ASM_DECLARE_REGISTER_GLOBAL
1251 name = IDENTIFIER_POINTER (DECL_NAME (decl));
1252 ASM_DECLARE_REGISTER_GLOBAL (asm_out_file, decl, reg_number, name);
1253 #endif
1254 nregs = hard_regno_nregs[reg_number][mode];
1255 while (nregs > 0)
1256 globalize_reg (decl, reg_number + --nregs);
1259 /* As a register variable, it has no section. */
1260 return;
1263 /* Now handle ordinary static variables and functions (in memory).
1264 Also handle vars declared register invalidly. */
1265 else if (name[0] == '*')
1267 #ifdef REGISTER_PREFIX
1268 if (strlen (REGISTER_PREFIX) != 0)
1270 reg_number = decode_reg_name (name);
1271 if (reg_number >= 0 || reg_number == -3)
1272 error ("register name given for non-register variable %q+D", decl);
1274 #endif
1277 /* Specifying a section attribute on a variable forces it into a
1278 non-.bss section, and thus it cannot be common. */
1279 /* FIXME: In general this code should not be necessary because
1280 visibility pass is doing the same work. But notice_global_symbol
1281 is called early and it needs to make DECL_RTL to get the name.
1282 we take care of recomputing the DECL_RTL after visibility is changed. */
1283 if (TREE_CODE (decl) == VAR_DECL
1284 && DECL_SECTION_NAME (decl) != NULL_TREE
1285 && DECL_INITIAL (decl) == NULL_TREE
1286 && DECL_COMMON (decl))
1287 DECL_COMMON (decl) = 0;
1289 /* Variables can't be both common and weak. */
1290 if (TREE_CODE (decl) == VAR_DECL && DECL_WEAK (decl))
1291 DECL_COMMON (decl) = 0;
1293 if (use_object_blocks_p () && use_blocks_for_decl_p (decl))
1294 x = create_block_symbol (name, get_block_for_decl (decl), -1);
1295 else
1297 enum machine_mode address_mode = Pmode;
1298 if (TREE_TYPE (decl) != error_mark_node)
1300 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
1301 address_mode = targetm.addr_space.address_mode (as);
1303 x = gen_rtx_SYMBOL_REF (address_mode, name);
1305 SYMBOL_REF_WEAK (x) = DECL_WEAK (decl);
1306 SET_SYMBOL_REF_DECL (x, decl);
1308 x = gen_rtx_MEM (DECL_MODE (decl), x);
1309 if (TREE_CODE (decl) != FUNCTION_DECL)
1310 set_mem_attributes (x, decl, 1);
1311 SET_DECL_RTL (decl, x);
1313 /* Optionally set flags or add text to the name to record information
1314 such as that it is a function name.
1315 If the name is changed, the macro ASM_OUTPUT_LABELREF
1316 will have to know how to strip this information. */
1317 targetm.encode_section_info (decl, DECL_RTL (decl), true);
1319 /* Make this function static known to the mudflap runtime. */
1320 if (flag_mudflap && TREE_CODE (decl) == VAR_DECL)
1321 mudflap_enqueue_decl (decl);
1324 /* Like make_decl_rtl, but inhibit creation of new alias sets when
1325 calling make_decl_rtl. Also, reset DECL_RTL before returning the
1326 rtl. */
1329 make_decl_rtl_for_debug (tree decl)
1331 unsigned int save_aliasing_flag, save_mudflap_flag;
1332 rtx rtl;
1334 if (DECL_RTL_SET_P (decl))
1335 return DECL_RTL (decl);
1337 /* Kludge alert! Somewhere down the call chain, make_decl_rtl will
1338 call new_alias_set. If running with -fcompare-debug, sometimes
1339 we do not want to create alias sets that will throw the alias
1340 numbers off in the comparison dumps. So... clearing
1341 flag_strict_aliasing will keep new_alias_set() from creating a
1342 new set. It is undesirable to register decl with mudflap
1343 in this case as well. */
1344 save_aliasing_flag = flag_strict_aliasing;
1345 flag_strict_aliasing = 0;
1346 save_mudflap_flag = flag_mudflap;
1347 flag_mudflap = 0;
1349 rtl = DECL_RTL (decl);
1350 /* Reset DECL_RTL back, as various parts of the compiler expects
1351 DECL_RTL set meaning it is actually going to be output. */
1352 SET_DECL_RTL (decl, NULL);
1354 flag_strict_aliasing = save_aliasing_flag;
1355 flag_mudflap = save_mudflap_flag;
1357 return rtl;
1360 /* Output a string of literal assembler code
1361 for an `asm' keyword used between functions. */
1363 void
1364 assemble_asm (tree string)
1366 const char *p;
1367 app_enable ();
1369 if (TREE_CODE (string) == ADDR_EXPR)
1370 string = TREE_OPERAND (string, 0);
1372 p = TREE_STRING_POINTER (string);
1373 fprintf (asm_out_file, "%s%s\n", p[0] == '\t' ? "" : "\t", p);
1376 /* Write the address of the entity given by SYMBOL to SEC. */
1377 void
1378 assemble_addr_to_section (rtx symbol, section *sec)
1380 switch_to_section (sec);
1381 assemble_align (POINTER_SIZE);
1382 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1385 /* Return the numbered .ctors.N (if CONSTRUCTOR_P) or .dtors.N (if
1386 not) section for PRIORITY. */
1387 section *
1388 get_cdtor_priority_section (int priority, bool constructor_p)
1390 char buf[16];
1392 /* ??? This only works reliably with the GNU linker. */
1393 sprintf (buf, "%s.%.5u",
1394 constructor_p ? ".ctors" : ".dtors",
1395 /* Invert the numbering so the linker puts us in the proper
1396 order; constructors are run from right to left, and the
1397 linker sorts in increasing order. */
1398 MAX_INIT_PRIORITY - priority);
1399 return get_section (buf, SECTION_WRITE, NULL);
1402 void
1403 default_named_section_asm_out_destructor (rtx symbol, int priority)
1405 section *sec;
1407 if (priority != DEFAULT_INIT_PRIORITY)
1408 sec = get_cdtor_priority_section (priority,
1409 /*constructor_p=*/false);
1410 else
1411 sec = get_section (".dtors", SECTION_WRITE, NULL);
1413 assemble_addr_to_section (symbol, sec);
1416 #ifdef DTORS_SECTION_ASM_OP
1417 void
1418 default_dtor_section_asm_out_destructor (rtx symbol,
1419 int priority ATTRIBUTE_UNUSED)
1421 assemble_addr_to_section (symbol, dtors_section);
1423 #endif
1425 void
1426 default_named_section_asm_out_constructor (rtx symbol, int priority)
1428 section *sec;
1430 if (priority != DEFAULT_INIT_PRIORITY)
1431 sec = get_cdtor_priority_section (priority,
1432 /*constructor_p=*/true);
1433 else
1434 sec = get_section (".ctors", SECTION_WRITE, NULL);
1436 assemble_addr_to_section (symbol, sec);
1439 #ifdef CTORS_SECTION_ASM_OP
1440 void
1441 default_ctor_section_asm_out_constructor (rtx symbol,
1442 int priority ATTRIBUTE_UNUSED)
1444 assemble_addr_to_section (symbol, ctors_section);
1446 #endif
1448 /* CONSTANT_POOL_BEFORE_FUNCTION may be defined as an expression with
1449 a nonzero value if the constant pool should be output before the
1450 start of the function, or a zero value if the pool should output
1451 after the end of the function. The default is to put it before the
1452 start. */
1454 #ifndef CONSTANT_POOL_BEFORE_FUNCTION
1455 #define CONSTANT_POOL_BEFORE_FUNCTION 1
1456 #endif
1458 /* DECL is an object (either VAR_DECL or FUNCTION_DECL) which is going
1459 to be output to assembler.
1460 Set first_global_object_name and weak_global_object_name as appropriate. */
1462 void
1463 notice_global_symbol (tree decl)
1465 const char **type = &first_global_object_name;
1467 if (first_global_object_name
1468 || !TREE_PUBLIC (decl)
1469 || DECL_EXTERNAL (decl)
1470 || !DECL_NAME (decl)
1471 || (TREE_CODE (decl) != FUNCTION_DECL
1472 && (TREE_CODE (decl) != VAR_DECL
1473 || (DECL_COMMON (decl)
1474 && (DECL_INITIAL (decl) == 0
1475 || DECL_INITIAL (decl) == error_mark_node))))
1476 || !MEM_P (DECL_RTL (decl)))
1477 return;
1479 /* We win when global object is found, but it is useful to know about weak
1480 symbol as well so we can produce nicer unique names. */
1481 if (DECL_WEAK (decl) || DECL_ONE_ONLY (decl) || flag_shlib)
1482 type = &weak_global_object_name;
1484 if (!*type)
1486 const char *p;
1487 const char *name;
1488 rtx decl_rtl = DECL_RTL (decl);
1490 p = targetm.strip_name_encoding (XSTR (XEXP (decl_rtl, 0), 0));
1491 name = ggc_strdup (p);
1493 *type = name;
1497 /* If not using flag_reorder_blocks_and_partition, decide early whether the
1498 current function goes into the cold section, so that targets can use
1499 current_function_section during RTL expansion. DECL describes the
1500 function. */
1502 void
1503 decide_function_section (tree decl)
1505 first_function_block_is_cold = false;
1507 if (flag_reorder_blocks_and_partition)
1508 /* We will decide in assemble_start_function. */
1509 return;
1511 if (DECL_SECTION_NAME (decl))
1513 struct cgraph_node *node = cgraph_get_node (current_function_decl);
1514 /* Calls to function_section rely on first_function_block_is_cold
1515 being accurate. */
1516 first_function_block_is_cold = (node
1517 && node->frequency
1518 == NODE_FREQUENCY_UNLIKELY_EXECUTED);
1521 in_cold_section_p = first_function_block_is_cold;
1524 /* Output assembler code for the constant pool of a function and associated
1525 with defining the name of the function. DECL describes the function.
1526 NAME is the function's name. For the constant pool, we use the current
1527 constant pool data. */
1529 void
1530 assemble_start_function (tree decl, const char *fnname)
1532 int align;
1533 char tmp_label[100];
1534 bool hot_label_written = false;
1536 if (flag_reorder_blocks_and_partition)
1538 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTB", const_labelno);
1539 crtl->subsections.hot_section_label = ggc_strdup (tmp_label);
1540 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDB", const_labelno);
1541 crtl->subsections.cold_section_label = ggc_strdup (tmp_label);
1542 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTE", const_labelno);
1543 crtl->subsections.hot_section_end_label = ggc_strdup (tmp_label);
1544 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDE", const_labelno);
1545 crtl->subsections.cold_section_end_label = ggc_strdup (tmp_label);
1546 const_labelno++;
1548 else
1550 crtl->subsections.hot_section_label = NULL;
1551 crtl->subsections.cold_section_label = NULL;
1552 crtl->subsections.hot_section_end_label = NULL;
1553 crtl->subsections.cold_section_end_label = NULL;
1556 /* The following code does not need preprocessing in the assembler. */
1558 app_disable ();
1560 if (CONSTANT_POOL_BEFORE_FUNCTION)
1561 output_constant_pool (fnname, decl);
1563 /* Make sure the not and cold text (code) sections are properly
1564 aligned. This is necessary here in the case where the function
1565 has both hot and cold sections, because we don't want to re-set
1566 the alignment when the section switch happens mid-function. */
1568 if (flag_reorder_blocks_and_partition)
1570 first_function_block_is_cold = false;
1572 switch_to_section (unlikely_text_section ());
1573 assemble_align (DECL_ALIGN (decl));
1574 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_label);
1576 /* When the function starts with a cold section, we need to explicitly
1577 align the hot section and write out the hot section label.
1578 But if the current function is a thunk, we do not have a CFG. */
1579 if (!cfun->is_thunk
1580 && BB_PARTITION (ENTRY_BLOCK_PTR->next_bb) == BB_COLD_PARTITION)
1582 switch_to_section (text_section);
1583 assemble_align (DECL_ALIGN (decl));
1584 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
1585 hot_label_written = true;
1586 first_function_block_is_cold = true;
1588 in_cold_section_p = first_function_block_is_cold;
1592 /* Switch to the correct text section for the start of the function. */
1594 switch_to_section (function_section (decl));
1595 if (flag_reorder_blocks_and_partition
1596 && !hot_label_written)
1597 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
1599 /* Tell assembler to move to target machine's alignment for functions. */
1600 align = floor_log2 (DECL_ALIGN (decl) / BITS_PER_UNIT);
1601 if (align > 0)
1603 ASM_OUTPUT_ALIGN (asm_out_file, align);
1606 /* Handle a user-specified function alignment.
1607 Note that we still need to align to DECL_ALIGN, as above,
1608 because ASM_OUTPUT_MAX_SKIP_ALIGN might not do any alignment at all. */
1609 if (! DECL_USER_ALIGN (decl)
1610 && align_functions_log > align
1611 && optimize_function_for_speed_p (cfun))
1613 #ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
1614 ASM_OUTPUT_MAX_SKIP_ALIGN (asm_out_file,
1615 align_functions_log, align_functions - 1);
1616 #else
1617 ASM_OUTPUT_ALIGN (asm_out_file, align_functions_log);
1618 #endif
1621 #ifdef ASM_OUTPUT_FUNCTION_PREFIX
1622 ASM_OUTPUT_FUNCTION_PREFIX (asm_out_file, fnname);
1623 #endif
1625 if (!DECL_IGNORED_P (decl))
1626 (*debug_hooks->begin_function) (decl);
1628 /* Make function name accessible from other files, if appropriate. */
1630 if (TREE_PUBLIC (decl))
1632 notice_global_symbol (decl);
1634 globalize_decl (decl);
1636 maybe_assemble_visibility (decl);
1639 if (DECL_PRESERVE_P (decl))
1640 targetm.asm_out.mark_decl_preserved (fnname);
1642 /* Do any machine/system dependent processing of the function name. */
1643 #ifdef ASM_DECLARE_FUNCTION_NAME
1644 ASM_DECLARE_FUNCTION_NAME (asm_out_file, fnname, current_function_decl);
1645 #else
1646 /* Standard thing is just output label for the function. */
1647 ASM_OUTPUT_FUNCTION_LABEL (asm_out_file, fnname, current_function_decl);
1648 #endif /* ASM_DECLARE_FUNCTION_NAME */
1650 if (lookup_attribute ("no_split_stack", DECL_ATTRIBUTES (decl)))
1651 saw_no_split_stack = true;
1654 /* Output assembler code associated with defining the size of the
1655 function. DECL describes the function. NAME is the function's name. */
1657 void
1658 assemble_end_function (tree decl, const char *fnname ATTRIBUTE_UNUSED)
1660 #ifdef ASM_DECLARE_FUNCTION_SIZE
1661 /* We could have switched section in the middle of the function. */
1662 if (flag_reorder_blocks_and_partition)
1663 switch_to_section (function_section (decl));
1664 ASM_DECLARE_FUNCTION_SIZE (asm_out_file, fnname, decl);
1665 #endif
1666 if (! CONSTANT_POOL_BEFORE_FUNCTION)
1668 output_constant_pool (fnname, decl);
1669 switch_to_section (function_section (decl)); /* need to switch back */
1671 /* Output labels for end of hot/cold text sections (to be used by
1672 debug info.) */
1673 if (flag_reorder_blocks_and_partition)
1675 section *save_text_section;
1677 save_text_section = in_section;
1678 switch_to_section (unlikely_text_section ());
1679 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_end_label);
1680 if (first_function_block_is_cold)
1681 switch_to_section (text_section);
1682 else
1683 switch_to_section (function_section (decl));
1684 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_end_label);
1685 switch_to_section (save_text_section);
1689 /* Assemble code to leave SIZE bytes of zeros. */
1691 void
1692 assemble_zeros (unsigned HOST_WIDE_INT size)
1694 /* Do no output if -fsyntax-only. */
1695 if (flag_syntax_only)
1696 return;
1698 #ifdef ASM_NO_SKIP_IN_TEXT
1699 /* The `space' pseudo in the text section outputs nop insns rather than 0s,
1700 so we must output 0s explicitly in the text section. */
1701 if (ASM_NO_SKIP_IN_TEXT && (in_section->common.flags & SECTION_CODE) != 0)
1703 unsigned HOST_WIDE_INT i;
1704 for (i = 0; i < size; i++)
1705 assemble_integer (const0_rtx, 1, BITS_PER_UNIT, 1);
1707 else
1708 #endif
1709 if (size > 0)
1710 ASM_OUTPUT_SKIP (asm_out_file, size);
1713 /* Assemble an alignment pseudo op for an ALIGN-bit boundary. */
1715 void
1716 assemble_align (int align)
1718 if (align > BITS_PER_UNIT)
1720 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
1724 /* Assemble a string constant with the specified C string as contents. */
1726 void
1727 assemble_string (const char *p, int size)
1729 int pos = 0;
1730 int maximum = 2000;
1732 /* If the string is very long, split it up. */
1734 while (pos < size)
1736 int thissize = size - pos;
1737 if (thissize > maximum)
1738 thissize = maximum;
1740 ASM_OUTPUT_ASCII (asm_out_file, p, thissize);
1742 pos += thissize;
1743 p += thissize;
1748 /* A noswitch_section_callback for lcomm_section. */
1750 static bool
1751 emit_local (tree decl ATTRIBUTE_UNUSED,
1752 const char *name ATTRIBUTE_UNUSED,
1753 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1754 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1756 #if defined ASM_OUTPUT_ALIGNED_DECL_LOCAL
1757 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, decl, name,
1758 size, DECL_ALIGN (decl));
1759 return true;
1760 #elif defined ASM_OUTPUT_ALIGNED_LOCAL
1761 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, DECL_ALIGN (decl));
1762 return true;
1763 #else
1764 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
1765 return false;
1766 #endif
1769 /* A noswitch_section_callback for bss_noswitch_section. */
1771 #if defined ASM_OUTPUT_ALIGNED_BSS
1772 static bool
1773 emit_bss (tree decl ATTRIBUTE_UNUSED,
1774 const char *name ATTRIBUTE_UNUSED,
1775 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1776 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1778 #if defined ASM_OUTPUT_ALIGNED_BSS
1779 ASM_OUTPUT_ALIGNED_BSS (asm_out_file, decl, name, size, DECL_ALIGN (decl));
1780 return true;
1781 #endif
1783 #endif
1785 /* A noswitch_section_callback for comm_section. */
1787 static bool
1788 emit_common (tree decl ATTRIBUTE_UNUSED,
1789 const char *name ATTRIBUTE_UNUSED,
1790 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1791 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1793 #if defined ASM_OUTPUT_ALIGNED_DECL_COMMON
1794 ASM_OUTPUT_ALIGNED_DECL_COMMON (asm_out_file, decl, name,
1795 size, DECL_ALIGN (decl));
1796 return true;
1797 #elif defined ASM_OUTPUT_ALIGNED_COMMON
1798 ASM_OUTPUT_ALIGNED_COMMON (asm_out_file, name, size, DECL_ALIGN (decl));
1799 return true;
1800 #else
1801 ASM_OUTPUT_COMMON (asm_out_file, name, size, rounded);
1802 return false;
1803 #endif
1806 /* A noswitch_section_callback for tls_comm_section. */
1808 static bool
1809 emit_tls_common (tree decl ATTRIBUTE_UNUSED,
1810 const char *name ATTRIBUTE_UNUSED,
1811 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1812 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1814 #ifdef ASM_OUTPUT_TLS_COMMON
1815 ASM_OUTPUT_TLS_COMMON (asm_out_file, decl, name, size);
1816 return true;
1817 #else
1818 sorry ("thread-local COMMON data not implemented");
1819 return true;
1820 #endif
1823 /* Assemble DECL given that it belongs in SECTION_NOSWITCH section SECT.
1824 NAME is the name of DECL's SYMBOL_REF. */
1826 static void
1827 assemble_noswitch_variable (tree decl, const char *name, section *sect)
1829 unsigned HOST_WIDE_INT size, rounded;
1831 size = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
1832 rounded = size;
1834 if (flag_asan && asan_protect_global (decl))
1835 size += asan_red_zone_size (size);
1837 /* Don't allocate zero bytes of common,
1838 since that means "undefined external" in the linker. */
1839 if (size == 0)
1840 rounded = 1;
1842 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
1843 so that each uninitialized object starts on such a boundary. */
1844 rounded += (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1;
1845 rounded = (rounded / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
1846 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
1848 if (!sect->noswitch.callback (decl, name, size, rounded)
1849 && (unsigned HOST_WIDE_INT) DECL_ALIGN_UNIT (decl) > rounded)
1850 warning (0, "requested alignment for %q+D is greater than "
1851 "implemented alignment of %wu", decl, rounded);
1854 /* A subroutine of assemble_variable. Output the label and contents of
1855 DECL, whose address is a SYMBOL_REF with name NAME. DONT_OUTPUT_DATA
1856 is as for assemble_variable. */
1858 static void
1859 assemble_variable_contents (tree decl, const char *name,
1860 bool dont_output_data)
1862 /* Do any machine/system dependent processing of the object. */
1863 #ifdef ASM_DECLARE_OBJECT_NAME
1864 last_assemble_variable_decl = decl;
1865 ASM_DECLARE_OBJECT_NAME (asm_out_file, name, decl);
1866 #else
1867 /* Standard thing is just output label for the object. */
1868 ASM_OUTPUT_LABEL (asm_out_file, name);
1869 #endif /* ASM_DECLARE_OBJECT_NAME */
1871 if (!dont_output_data)
1873 if (DECL_INITIAL (decl)
1874 && DECL_INITIAL (decl) != error_mark_node
1875 && !initializer_zerop (DECL_INITIAL (decl)))
1876 /* Output the actual data. */
1877 output_constant (DECL_INITIAL (decl),
1878 tree_low_cst (DECL_SIZE_UNIT (decl), 1),
1879 DECL_ALIGN (decl));
1880 else
1881 /* Leave space for it. */
1882 assemble_zeros (tree_low_cst (DECL_SIZE_UNIT (decl), 1));
1886 /* Assemble everything that is needed for a variable or function declaration.
1887 Not used for automatic variables, and not used for function definitions.
1888 Should not be called for variables of incomplete structure type.
1890 TOP_LEVEL is nonzero if this variable has file scope.
1891 AT_END is nonzero if this is the special handling, at end of compilation,
1892 to define things that have had only tentative definitions.
1893 DONT_OUTPUT_DATA if nonzero means don't actually output the
1894 initial value (that will be done by the caller). */
1896 void
1897 assemble_variable (tree decl, int top_level ATTRIBUTE_UNUSED,
1898 int at_end ATTRIBUTE_UNUSED, int dont_output_data)
1900 const char *name;
1901 rtx decl_rtl, symbol;
1902 section *sect;
1903 bool asan_protected = false;
1905 /* This function is supposed to handle VARIABLES. Ensure we have one. */
1906 gcc_assert (TREE_CODE (decl) == VAR_DECL);
1908 /* Emulated TLS had better not get this far. */
1909 gcc_checking_assert (targetm.have_tls || !DECL_THREAD_LOCAL_P (decl));
1911 last_assemble_variable_decl = 0;
1913 /* Normally no need to say anything here for external references,
1914 since assemble_external is called by the language-specific code
1915 when a declaration is first seen. */
1917 if (DECL_EXTERNAL (decl))
1918 return;
1920 /* Do nothing for global register variables. */
1921 if (DECL_RTL_SET_P (decl) && REG_P (DECL_RTL (decl)))
1923 TREE_ASM_WRITTEN (decl) = 1;
1924 return;
1927 /* If type was incomplete when the variable was declared,
1928 see if it is complete now. */
1930 if (DECL_SIZE (decl) == 0)
1931 layout_decl (decl, 0);
1933 /* Still incomplete => don't allocate it; treat the tentative defn
1934 (which is what it must have been) as an `extern' reference. */
1936 if (!dont_output_data && DECL_SIZE (decl) == 0)
1938 error ("storage size of %q+D isn%'t known", decl);
1939 TREE_ASM_WRITTEN (decl) = 1;
1940 return;
1943 /* The first declaration of a variable that comes through this function
1944 decides whether it is global (in C, has external linkage)
1945 or local (in C, has internal linkage). So do nothing more
1946 if this function has already run. */
1948 if (TREE_ASM_WRITTEN (decl))
1949 return;
1951 /* Make sure targetm.encode_section_info is invoked before we set
1952 ASM_WRITTEN. */
1953 decl_rtl = DECL_RTL (decl);
1955 TREE_ASM_WRITTEN (decl) = 1;
1957 /* Do no output if -fsyntax-only. */
1958 if (flag_syntax_only)
1959 return;
1961 if (! dont_output_data
1962 && ! valid_constant_size_p (DECL_SIZE_UNIT (decl)))
1964 error ("size of variable %q+D is too large", decl);
1965 return;
1968 gcc_assert (MEM_P (decl_rtl));
1969 gcc_assert (GET_CODE (XEXP (decl_rtl, 0)) == SYMBOL_REF);
1970 symbol = XEXP (decl_rtl, 0);
1972 /* If this symbol belongs to the tree constant pool, output the constant
1973 if it hasn't already been written. */
1974 if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
1976 tree decl = SYMBOL_REF_DECL (symbol);
1977 if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
1978 output_constant_def_contents (symbol);
1979 return;
1982 app_disable ();
1984 name = XSTR (symbol, 0);
1985 if (TREE_PUBLIC (decl) && DECL_NAME (decl))
1986 notice_global_symbol (decl);
1988 /* Compute the alignment of this data. */
1990 align_variable (decl, dont_output_data);
1992 if (flag_asan
1993 && asan_protect_global (decl))
1995 asan_protected = true;
1996 DECL_ALIGN (decl) = MAX (DECL_ALIGN (decl),
1997 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
2000 set_mem_align (decl_rtl, DECL_ALIGN (decl));
2002 if (TREE_PUBLIC (decl))
2003 maybe_assemble_visibility (decl);
2005 if (DECL_PRESERVE_P (decl))
2006 targetm.asm_out.mark_decl_preserved (name);
2008 /* First make the assembler name(s) global if appropriate. */
2009 sect = get_variable_section (decl, false);
2010 if (TREE_PUBLIC (decl)
2011 && (sect->common.flags & SECTION_COMMON) == 0)
2012 globalize_decl (decl);
2014 /* Output any data that we will need to use the address of. */
2015 if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node)
2016 output_addressed_constants (DECL_INITIAL (decl));
2018 /* dbxout.c needs to know this. */
2019 if (sect && (sect->common.flags & SECTION_CODE) != 0)
2020 DECL_IN_TEXT_SECTION (decl) = 1;
2022 /* If the decl is part of an object_block, make sure that the decl
2023 has been positioned within its block, but do not write out its
2024 definition yet. output_object_blocks will do that later. */
2025 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
2027 gcc_assert (!dont_output_data);
2028 place_block_symbol (symbol);
2030 else if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
2031 assemble_noswitch_variable (decl, name, sect);
2032 else
2034 switch_to_section (sect);
2035 if (DECL_ALIGN (decl) > BITS_PER_UNIT)
2036 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (DECL_ALIGN_UNIT (decl)));
2037 assemble_variable_contents (decl, name, dont_output_data);
2038 if (asan_protected)
2040 unsigned HOST_WIDE_INT int size
2041 = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
2042 assemble_zeros (asan_red_zone_size (size));
2047 /* Return 1 if type TYPE contains any pointers. */
2049 static int
2050 contains_pointers_p (tree type)
2052 switch (TREE_CODE (type))
2054 case POINTER_TYPE:
2055 case REFERENCE_TYPE:
2056 /* I'm not sure whether OFFSET_TYPE needs this treatment,
2057 so I'll play safe and return 1. */
2058 case OFFSET_TYPE:
2059 return 1;
2061 case RECORD_TYPE:
2062 case UNION_TYPE:
2063 case QUAL_UNION_TYPE:
2065 tree fields;
2066 /* For a type that has fields, see if the fields have pointers. */
2067 for (fields = TYPE_FIELDS (type); fields; fields = DECL_CHAIN (fields))
2068 if (TREE_CODE (fields) == FIELD_DECL
2069 && contains_pointers_p (TREE_TYPE (fields)))
2070 return 1;
2071 return 0;
2074 case ARRAY_TYPE:
2075 /* An array type contains pointers if its element type does. */
2076 return contains_pointers_p (TREE_TYPE (type));
2078 default:
2079 return 0;
2083 /* We delay assemble_external processing until
2084 the compilation unit is finalized. This is the best we can do for
2085 right now (i.e. stage 3 of GCC 4.0) - the right thing is to delay
2086 it all the way to final. See PR 17982 for further discussion. */
2087 static GTY(()) tree pending_assemble_externals;
2089 #ifdef ASM_OUTPUT_EXTERNAL
2090 /* Some targets delay some output to final using TARGET_ASM_FILE_END.
2091 As a result, assemble_external can be called after the list of externals
2092 is processed and the pointer set destroyed. */
2093 static bool pending_assemble_externals_processed;
2095 /* Avoid O(external_decls**2) lookups in the pending_assemble_externals
2096 TREE_LIST in assemble_external. */
2097 static struct pointer_set_t *pending_assemble_externals_set;
2099 /* True if DECL is a function decl for which no out-of-line copy exists.
2100 It is assumed that DECL's assembler name has been set. */
2102 static bool
2103 incorporeal_function_p (tree decl)
2105 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
2107 const char *name;
2109 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
2110 && (DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA
2111 || DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA_WITH_ALIGN))
2112 return true;
2114 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2115 /* Atomic or sync builtins which have survived this far will be
2116 resolved externally and therefore are not incorporeal. */
2117 if (strncmp (name, "__builtin_", 10) == 0)
2118 return true;
2120 return false;
2123 /* Actually do the tests to determine if this is necessary, and invoke
2124 ASM_OUTPUT_EXTERNAL. */
2125 static void
2126 assemble_external_real (tree decl)
2128 rtx rtl = DECL_RTL (decl);
2130 if (MEM_P (rtl) && GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF
2131 && !SYMBOL_REF_USED (XEXP (rtl, 0))
2132 && !incorporeal_function_p (decl))
2134 /* Some systems do require some output. */
2135 SYMBOL_REF_USED (XEXP (rtl, 0)) = 1;
2136 ASM_OUTPUT_EXTERNAL (asm_out_file, decl, XSTR (XEXP (rtl, 0), 0));
2139 #endif
2141 void
2142 process_pending_assemble_externals (void)
2144 #ifdef ASM_OUTPUT_EXTERNAL
2145 tree list;
2146 for (list = pending_assemble_externals; list; list = TREE_CHAIN (list))
2147 assemble_external_real (TREE_VALUE (list));
2149 pending_assemble_externals = 0;
2150 pending_assemble_externals_processed = true;
2151 pointer_set_destroy (pending_assemble_externals_set);
2152 #endif
2155 /* This TREE_LIST contains any weak symbol declarations waiting
2156 to be emitted. */
2157 static GTY(()) tree weak_decls;
2159 /* Output something to declare an external symbol to the assembler,
2160 and qualifiers such as weakness. (Most assemblers don't need
2161 extern declaration, so we normally output nothing.) Do nothing if
2162 DECL is not external. */
2164 void
2165 assemble_external (tree decl ATTRIBUTE_UNUSED)
2167 /* Make sure that the ASM_OUT_FILE is open.
2168 If it's not, we should not be calling this function. */
2169 gcc_assert (asm_out_file);
2171 /* In a perfect world, the following condition would be true.
2172 Sadly, the Java and Go front ends emit assembly *from the front end*,
2173 bypassing the call graph. See PR52739. Fix before GCC 4.8. */
2174 #if 0
2175 /* This function should only be called if we are expanding, or have
2176 expanded, to RTL.
2177 Ideally, only final.c would be calling this function, but it is
2178 not clear whether that would break things somehow. See PR 17982
2179 for further discussion. */
2180 gcc_assert (cgraph_state == CGRAPH_STATE_EXPANSION
2181 || cgraph_state == CGRAPH_STATE_FINISHED);
2182 #endif
2184 if (!DECL_P (decl) || !DECL_EXTERNAL (decl) || !TREE_PUBLIC (decl))
2185 return;
2187 /* We want to output annotation for weak and external symbols at
2188 very last to check if they are references or not. */
2190 if (TARGET_SUPPORTS_WEAK
2191 && DECL_WEAK (decl)
2192 /* TREE_STATIC is a weird and abused creature which is not
2193 generally the right test for whether an entity has been
2194 locally emitted, inlined or otherwise not-really-extern, but
2195 for declarations that can be weak, it happens to be
2196 match. */
2197 && !TREE_STATIC (decl)
2198 && lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
2199 && value_member (decl, weak_decls) == NULL_TREE)
2200 weak_decls = tree_cons (NULL, decl, weak_decls);
2202 #ifdef ASM_OUTPUT_EXTERNAL
2203 if (pending_assemble_externals_processed)
2205 assemble_external_real (decl);
2206 return;
2209 if (! pointer_set_insert (pending_assemble_externals_set, decl))
2210 pending_assemble_externals = tree_cons (NULL, decl,
2211 pending_assemble_externals);
2212 #endif
2215 /* Similar, for calling a library function FUN. */
2217 void
2218 assemble_external_libcall (rtx fun)
2220 /* Declare library function name external when first used, if nec. */
2221 if (! SYMBOL_REF_USED (fun))
2223 SYMBOL_REF_USED (fun) = 1;
2224 targetm.asm_out.external_libcall (fun);
2228 /* Assemble a label named NAME. */
2230 void
2231 assemble_label (FILE *file, const char *name)
2233 ASM_OUTPUT_LABEL (file, name);
2236 /* Set the symbol_referenced flag for ID. */
2237 void
2238 mark_referenced (tree id)
2240 TREE_SYMBOL_REFERENCED (id) = 1;
2243 /* Set the symbol_referenced flag for DECL and notify callgraph. */
2244 void
2245 mark_decl_referenced (tree decl)
2247 if (TREE_CODE (decl) == FUNCTION_DECL)
2249 /* Extern inline functions don't become needed when referenced.
2250 If we know a method will be emitted in other TU and no new
2251 functions can be marked reachable, just use the external
2252 definition. */
2253 struct cgraph_node *node = cgraph_get_create_node (decl);
2254 if (!DECL_EXTERNAL (decl)
2255 && !node->local.finalized)
2256 cgraph_mark_force_output_node (node);
2258 else if (TREE_CODE (decl) == VAR_DECL)
2260 struct varpool_node *node = varpool_node_for_decl (decl);
2261 /* C++ frontend use mark_decl_references to force COMDAT variables
2262 to be output that might appear dead otherwise. */
2263 node->symbol.force_output = true;
2265 /* else do nothing - we can get various sorts of CST nodes here,
2266 which do not need to be marked. */
2270 /* Follow the IDENTIFIER_TRANSPARENT_ALIAS chain starting at *ALIAS
2271 until we find an identifier that is not itself a transparent alias.
2272 Modify the alias passed to it by reference (and all aliases on the
2273 way to the ultimate target), such that they do not have to be
2274 followed again, and return the ultimate target of the alias
2275 chain. */
2277 static inline tree
2278 ultimate_transparent_alias_target (tree *alias)
2280 tree target = *alias;
2282 if (IDENTIFIER_TRANSPARENT_ALIAS (target))
2284 gcc_assert (TREE_CHAIN (target));
2285 target = ultimate_transparent_alias_target (&TREE_CHAIN (target));
2286 gcc_assert (! IDENTIFIER_TRANSPARENT_ALIAS (target)
2287 && ! TREE_CHAIN (target));
2288 *alias = target;
2291 return target;
2294 /* Output to FILE (an assembly file) a reference to NAME. If NAME
2295 starts with a *, the rest of NAME is output verbatim. Otherwise
2296 NAME is transformed in a target-specific way (usually by the
2297 addition of an underscore). */
2299 void
2300 assemble_name_raw (FILE *file, const char *name)
2302 if (name[0] == '*')
2303 fputs (&name[1], file);
2304 else
2305 ASM_OUTPUT_LABELREF (file, name);
2308 /* Like assemble_name_raw, but should be used when NAME might refer to
2309 an entity that is also represented as a tree (like a function or
2310 variable). If NAME does refer to such an entity, that entity will
2311 be marked as referenced. */
2313 void
2314 assemble_name (FILE *file, const char *name)
2316 const char *real_name;
2317 tree id;
2319 real_name = targetm.strip_name_encoding (name);
2321 id = maybe_get_identifier (real_name);
2322 if (id)
2324 tree id_orig = id;
2326 mark_referenced (id);
2327 ultimate_transparent_alias_target (&id);
2328 if (id != id_orig)
2329 name = IDENTIFIER_POINTER (id);
2330 gcc_assert (! TREE_CHAIN (id));
2333 assemble_name_raw (file, name);
2336 /* Allocate SIZE bytes writable static space with a gensym name
2337 and return an RTX to refer to its address. */
2340 assemble_static_space (unsigned HOST_WIDE_INT size)
2342 char name[12];
2343 const char *namestring;
2344 rtx x;
2346 ASM_GENERATE_INTERNAL_LABEL (name, "LF", const_labelno);
2347 ++const_labelno;
2348 namestring = ggc_strdup (name);
2350 x = gen_rtx_SYMBOL_REF (Pmode, namestring);
2351 SYMBOL_REF_FLAGS (x) = SYMBOL_FLAG_LOCAL;
2353 #ifdef ASM_OUTPUT_ALIGNED_DECL_LOCAL
2354 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, NULL_TREE, name, size,
2355 BIGGEST_ALIGNMENT);
2356 #else
2357 #ifdef ASM_OUTPUT_ALIGNED_LOCAL
2358 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, BIGGEST_ALIGNMENT);
2359 #else
2361 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
2362 so that each uninitialized object starts on such a boundary. */
2363 /* Variable `rounded' might or might not be used in ASM_OUTPUT_LOCAL. */
2364 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED
2365 = ((size + (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1)
2366 / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
2367 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
2368 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
2370 #endif
2371 #endif
2372 return x;
2375 /* Assemble the static constant template for function entry trampolines.
2376 This is done at most once per compilation.
2377 Returns an RTX for the address of the template. */
2379 static GTY(()) rtx initial_trampoline;
2382 assemble_trampoline_template (void)
2384 char label[256];
2385 const char *name;
2386 int align;
2387 rtx symbol;
2389 gcc_assert (targetm.asm_out.trampoline_template != NULL);
2391 if (initial_trampoline)
2392 return initial_trampoline;
2394 /* By default, put trampoline templates in read-only data section. */
2396 #ifdef TRAMPOLINE_SECTION
2397 switch_to_section (TRAMPOLINE_SECTION);
2398 #else
2399 switch_to_section (readonly_data_section);
2400 #endif
2402 /* Write the assembler code to define one. */
2403 align = floor_log2 (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
2404 if (align > 0)
2405 ASM_OUTPUT_ALIGN (asm_out_file, align);
2407 targetm.asm_out.internal_label (asm_out_file, "LTRAMP", 0);
2408 targetm.asm_out.trampoline_template (asm_out_file);
2410 /* Record the rtl to refer to it. */
2411 ASM_GENERATE_INTERNAL_LABEL (label, "LTRAMP", 0);
2412 name = ggc_strdup (label);
2413 symbol = gen_rtx_SYMBOL_REF (Pmode, name);
2414 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_LOCAL;
2416 initial_trampoline = gen_const_mem (BLKmode, symbol);
2417 set_mem_align (initial_trampoline, TRAMPOLINE_ALIGNMENT);
2418 set_mem_size (initial_trampoline, TRAMPOLINE_SIZE);
2420 return initial_trampoline;
2423 /* A and B are either alignments or offsets. Return the minimum alignment
2424 that may be assumed after adding the two together. */
2426 static inline unsigned
2427 min_align (unsigned int a, unsigned int b)
2429 return (a | b) & -(a | b);
2432 /* Return the assembler directive for creating a given kind of integer
2433 object. SIZE is the number of bytes in the object and ALIGNED_P
2434 indicates whether it is known to be aligned. Return NULL if the
2435 assembly dialect has no such directive.
2437 The returned string should be printed at the start of a new line and
2438 be followed immediately by the object's initial value. */
2440 const char *
2441 integer_asm_op (int size, int aligned_p)
2443 struct asm_int_op *ops;
2445 if (aligned_p)
2446 ops = &targetm.asm_out.aligned_op;
2447 else
2448 ops = &targetm.asm_out.unaligned_op;
2450 switch (size)
2452 case 1:
2453 return targetm.asm_out.byte_op;
2454 case 2:
2455 return ops->hi;
2456 case 4:
2457 return ops->si;
2458 case 8:
2459 return ops->di;
2460 case 16:
2461 return ops->ti;
2462 default:
2463 return NULL;
2467 /* Use directive OP to assemble an integer object X. Print OP at the
2468 start of the line, followed immediately by the value of X. */
2470 void
2471 assemble_integer_with_op (const char *op, rtx x)
2473 fputs (op, asm_out_file);
2474 output_addr_const (asm_out_file, x);
2475 fputc ('\n', asm_out_file);
2478 /* The default implementation of the asm_out.integer target hook. */
2480 bool
2481 default_assemble_integer (rtx x ATTRIBUTE_UNUSED,
2482 unsigned int size ATTRIBUTE_UNUSED,
2483 int aligned_p ATTRIBUTE_UNUSED)
2485 const char *op = integer_asm_op (size, aligned_p);
2486 /* Avoid GAS bugs for large values. Specifically negative values whose
2487 absolute value fits in a bfd_vma, but not in a bfd_signed_vma. */
2488 if (size > UNITS_PER_WORD && size > POINTER_SIZE / BITS_PER_UNIT)
2489 return false;
2490 return op && (assemble_integer_with_op (op, x), true);
2493 /* Assemble the integer constant X into an object of SIZE bytes. ALIGN is
2494 the alignment of the integer in bits. Return 1 if we were able to output
2495 the constant, otherwise 0. We must be able to output the constant,
2496 if FORCE is nonzero. */
2498 bool
2499 assemble_integer (rtx x, unsigned int size, unsigned int align, int force)
2501 int aligned_p;
2503 aligned_p = (align >= MIN (size * BITS_PER_UNIT, BIGGEST_ALIGNMENT));
2505 /* See if the target hook can handle this kind of object. */
2506 if (targetm.asm_out.integer (x, size, aligned_p))
2507 return true;
2509 /* If the object is a multi-byte one, try splitting it up. Split
2510 it into words it if is multi-word, otherwise split it into bytes. */
2511 if (size > 1)
2513 enum machine_mode omode, imode;
2514 unsigned int subalign;
2515 unsigned int subsize, i;
2516 enum mode_class mclass;
2518 subsize = size > UNITS_PER_WORD? UNITS_PER_WORD : 1;
2519 subalign = MIN (align, subsize * BITS_PER_UNIT);
2520 if (GET_CODE (x) == CONST_FIXED)
2521 mclass = GET_MODE_CLASS (GET_MODE (x));
2522 else
2523 mclass = MODE_INT;
2525 omode = mode_for_size (subsize * BITS_PER_UNIT, mclass, 0);
2526 imode = mode_for_size (size * BITS_PER_UNIT, mclass, 0);
2528 for (i = 0; i < size; i += subsize)
2530 rtx partial = simplify_subreg (omode, x, imode, i);
2531 if (!partial || !assemble_integer (partial, subsize, subalign, 0))
2532 break;
2534 if (i == size)
2535 return true;
2537 /* If we've printed some of it, but not all of it, there's no going
2538 back now. */
2539 gcc_assert (!i);
2542 gcc_assert (!force);
2544 return false;
2547 void
2548 assemble_real (REAL_VALUE_TYPE d, enum machine_mode mode, unsigned int align)
2550 long data[4] = {0, 0, 0, 0};
2551 int i;
2552 int bitsize, nelts, nunits, units_per;
2554 /* This is hairy. We have a quantity of known size. real_to_target
2555 will put it into an array of *host* longs, 32 bits per element
2556 (even if long is more than 32 bits). We need to determine the
2557 number of array elements that are occupied (nelts) and the number
2558 of *target* min-addressable units that will be occupied in the
2559 object file (nunits). We cannot assume that 32 divides the
2560 mode's bitsize (size * BITS_PER_UNIT) evenly.
2562 size * BITS_PER_UNIT is used here to make sure that padding bits
2563 (which might appear at either end of the value; real_to_target
2564 will include the padding bits in its output array) are included. */
2566 nunits = GET_MODE_SIZE (mode);
2567 bitsize = nunits * BITS_PER_UNIT;
2568 nelts = CEIL (bitsize, 32);
2569 units_per = 32 / BITS_PER_UNIT;
2571 real_to_target (data, &d, mode);
2573 /* Put out the first word with the specified alignment. */
2574 assemble_integer (GEN_INT (data[0]), MIN (nunits, units_per), align, 1);
2575 nunits -= units_per;
2577 /* Subsequent words need only 32-bit alignment. */
2578 align = min_align (align, 32);
2580 for (i = 1; i < nelts; i++)
2582 assemble_integer (GEN_INT (data[i]), MIN (nunits, units_per), align, 1);
2583 nunits -= units_per;
2587 /* Given an expression EXP with a constant value,
2588 reduce it to the sum of an assembler symbol and an integer.
2589 Store them both in the structure *VALUE.
2590 EXP must be reducible. */
2592 struct addr_const {
2593 rtx base;
2594 HOST_WIDE_INT offset;
2597 static void
2598 decode_addr_const (tree exp, struct addr_const *value)
2600 tree target = TREE_OPERAND (exp, 0);
2601 int offset = 0;
2602 rtx x;
2604 while (1)
2606 if (TREE_CODE (target) == COMPONENT_REF
2607 && host_integerp (byte_position (TREE_OPERAND (target, 1)), 0))
2609 offset += int_byte_position (TREE_OPERAND (target, 1));
2610 target = TREE_OPERAND (target, 0);
2612 else if (TREE_CODE (target) == ARRAY_REF
2613 || TREE_CODE (target) == ARRAY_RANGE_REF)
2615 offset += (tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (target)), 1)
2616 * tree_low_cst (TREE_OPERAND (target, 1), 0));
2617 target = TREE_OPERAND (target, 0);
2619 else if (TREE_CODE (target) == MEM_REF
2620 && TREE_CODE (TREE_OPERAND (target, 0)) == ADDR_EXPR)
2622 offset += mem_ref_offset (target).low;
2623 target = TREE_OPERAND (TREE_OPERAND (target, 0), 0);
2625 else if (TREE_CODE (target) == INDIRECT_REF
2626 && TREE_CODE (TREE_OPERAND (target, 0)) == NOP_EXPR
2627 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (target, 0), 0))
2628 == ADDR_EXPR)
2629 target = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (target, 0), 0), 0);
2630 else
2631 break;
2634 switch (TREE_CODE (target))
2636 case VAR_DECL:
2637 case FUNCTION_DECL:
2638 x = DECL_RTL (target);
2639 break;
2641 case LABEL_DECL:
2642 x = gen_rtx_MEM (FUNCTION_MODE,
2643 gen_rtx_LABEL_REF (Pmode, force_label_rtx (target)));
2644 break;
2646 case REAL_CST:
2647 case FIXED_CST:
2648 case STRING_CST:
2649 case COMPLEX_CST:
2650 case CONSTRUCTOR:
2651 case INTEGER_CST:
2652 x = output_constant_def (target, 1);
2653 break;
2655 default:
2656 gcc_unreachable ();
2659 gcc_assert (MEM_P (x));
2660 x = XEXP (x, 0);
2662 value->base = x;
2663 value->offset = offset;
2667 static GTY((param_is (struct constant_descriptor_tree)))
2668 htab_t const_desc_htab;
2670 static void maybe_output_constant_def_contents (struct constant_descriptor_tree *, int);
2672 /* Constant pool accessor function. */
2674 htab_t
2675 constant_pool_htab (void)
2677 return const_desc_htab;
2680 /* Compute a hash code for a constant expression. */
2682 static hashval_t
2683 const_desc_hash (const void *ptr)
2685 return ((const struct constant_descriptor_tree *)ptr)->hash;
2688 static hashval_t
2689 const_hash_1 (const tree exp)
2691 const char *p;
2692 hashval_t hi;
2693 int len, i;
2694 enum tree_code code = TREE_CODE (exp);
2696 /* Either set P and LEN to the address and len of something to hash and
2697 exit the switch or return a value. */
2699 switch (code)
2701 case INTEGER_CST:
2702 p = (char *) &TREE_INT_CST (exp);
2703 len = sizeof TREE_INT_CST (exp);
2704 break;
2706 case REAL_CST:
2707 return real_hash (TREE_REAL_CST_PTR (exp));
2709 case FIXED_CST:
2710 return fixed_hash (TREE_FIXED_CST_PTR (exp));
2712 case STRING_CST:
2713 p = TREE_STRING_POINTER (exp);
2714 len = TREE_STRING_LENGTH (exp);
2715 break;
2717 case COMPLEX_CST:
2718 return (const_hash_1 (TREE_REALPART (exp)) * 5
2719 + const_hash_1 (TREE_IMAGPART (exp)));
2721 case VECTOR_CST:
2723 unsigned i;
2725 hi = 7 + VECTOR_CST_NELTS (exp);
2727 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
2728 hi = hi * 563 + const_hash_1 (VECTOR_CST_ELT (exp, i));
2730 return hi;
2733 case CONSTRUCTOR:
2735 unsigned HOST_WIDE_INT idx;
2736 tree value;
2738 hi = 5 + int_size_in_bytes (TREE_TYPE (exp));
2740 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
2741 if (value)
2742 hi = hi * 603 + const_hash_1 (value);
2744 return hi;
2747 case ADDR_EXPR:
2748 case FDESC_EXPR:
2750 struct addr_const value;
2752 decode_addr_const (exp, &value);
2753 switch (GET_CODE (value.base))
2755 case SYMBOL_REF:
2756 /* Don't hash the address of the SYMBOL_REF;
2757 only use the offset and the symbol name. */
2758 hi = value.offset;
2759 p = XSTR (value.base, 0);
2760 for (i = 0; p[i] != 0; i++)
2761 hi = ((hi * 613) + (unsigned) (p[i]));
2762 break;
2764 case LABEL_REF:
2765 hi = value.offset + CODE_LABEL_NUMBER (XEXP (value.base, 0)) * 13;
2766 break;
2768 default:
2769 gcc_unreachable ();
2772 return hi;
2774 case PLUS_EXPR:
2775 case POINTER_PLUS_EXPR:
2776 case MINUS_EXPR:
2777 return (const_hash_1 (TREE_OPERAND (exp, 0)) * 9
2778 + const_hash_1 (TREE_OPERAND (exp, 1)));
2780 CASE_CONVERT:
2781 return const_hash_1 (TREE_OPERAND (exp, 0)) * 7 + 2;
2783 default:
2784 /* A language specific constant. Just hash the code. */
2785 return code;
2788 /* Compute hashing function. */
2789 hi = len;
2790 for (i = 0; i < len; i++)
2791 hi = ((hi * 613) + (unsigned) (p[i]));
2793 return hi;
2796 /* Wrapper of compare_constant, for the htab interface. */
2797 static int
2798 const_desc_eq (const void *p1, const void *p2)
2800 const struct constant_descriptor_tree *const c1
2801 = (const struct constant_descriptor_tree *) p1;
2802 const struct constant_descriptor_tree *const c2
2803 = (const struct constant_descriptor_tree *) p2;
2804 if (c1->hash != c2->hash)
2805 return 0;
2806 return compare_constant (c1->value, c2->value);
2809 /* Compare t1 and t2, and return 1 only if they are known to result in
2810 the same bit pattern on output. */
2812 static int
2813 compare_constant (const tree t1, const tree t2)
2815 enum tree_code typecode;
2817 if (t1 == NULL_TREE)
2818 return t2 == NULL_TREE;
2819 if (t2 == NULL_TREE)
2820 return 0;
2822 if (TREE_CODE (t1) != TREE_CODE (t2))
2823 return 0;
2825 switch (TREE_CODE (t1))
2827 case INTEGER_CST:
2828 /* Integer constants are the same only if the same width of type. */
2829 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
2830 return 0;
2831 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
2832 return 0;
2833 return tree_int_cst_equal (t1, t2);
2835 case REAL_CST:
2836 /* Real constants are the same only if the same width of type. */
2837 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
2838 return 0;
2840 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
2842 case FIXED_CST:
2843 /* Fixed constants are the same only if the same width of type. */
2844 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
2845 return 0;
2847 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
2849 case STRING_CST:
2850 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
2851 return 0;
2853 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
2854 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
2855 TREE_STRING_LENGTH (t1)));
2857 case COMPLEX_CST:
2858 return (compare_constant (TREE_REALPART (t1), TREE_REALPART (t2))
2859 && compare_constant (TREE_IMAGPART (t1), TREE_IMAGPART (t2)));
2861 case VECTOR_CST:
2863 unsigned i;
2865 if (VECTOR_CST_NELTS (t1) != VECTOR_CST_NELTS (t2))
2866 return 0;
2868 for (i = 0; i < VECTOR_CST_NELTS (t1); ++i)
2869 if (!compare_constant (VECTOR_CST_ELT (t1, i),
2870 VECTOR_CST_ELT (t2, i)))
2871 return 0;
2873 return 1;
2876 case CONSTRUCTOR:
2878 vec<constructor_elt, va_gc> *v1, *v2;
2879 unsigned HOST_WIDE_INT idx;
2881 typecode = TREE_CODE (TREE_TYPE (t1));
2882 if (typecode != TREE_CODE (TREE_TYPE (t2)))
2883 return 0;
2885 if (typecode == ARRAY_TYPE)
2887 HOST_WIDE_INT size_1 = int_size_in_bytes (TREE_TYPE (t1));
2888 /* For arrays, check that the sizes all match. */
2889 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))
2890 || size_1 == -1
2891 || size_1 != int_size_in_bytes (TREE_TYPE (t2)))
2892 return 0;
2894 else
2896 /* For record and union constructors, require exact type
2897 equality. */
2898 if (TREE_TYPE (t1) != TREE_TYPE (t2))
2899 return 0;
2902 v1 = CONSTRUCTOR_ELTS (t1);
2903 v2 = CONSTRUCTOR_ELTS (t2);
2904 if (vec_safe_length (v1) != vec_safe_length (v2))
2905 return 0;
2907 for (idx = 0; idx < vec_safe_length (v1); ++idx)
2909 constructor_elt *c1 = &(*v1)[idx];
2910 constructor_elt *c2 = &(*v2)[idx];
2912 /* Check that each value is the same... */
2913 if (!compare_constant (c1->value, c2->value))
2914 return 0;
2915 /* ... and that they apply to the same fields! */
2916 if (typecode == ARRAY_TYPE)
2918 if (!compare_constant (c1->index, c2->index))
2919 return 0;
2921 else
2923 if (c1->index != c2->index)
2924 return 0;
2928 return 1;
2931 case ADDR_EXPR:
2932 case FDESC_EXPR:
2934 struct addr_const value1, value2;
2935 enum rtx_code code;
2936 int ret;
2938 decode_addr_const (t1, &value1);
2939 decode_addr_const (t2, &value2);
2941 if (value1.offset != value2.offset)
2942 return 0;
2944 code = GET_CODE (value1.base);
2945 if (code != GET_CODE (value2.base))
2946 return 0;
2948 switch (code)
2950 case SYMBOL_REF:
2951 ret = (strcmp (XSTR (value1.base, 0), XSTR (value2.base, 0)) == 0);
2952 break;
2954 case LABEL_REF:
2955 ret = (CODE_LABEL_NUMBER (XEXP (value1.base, 0))
2956 == CODE_LABEL_NUMBER (XEXP (value2.base, 0)));
2957 break;
2959 default:
2960 gcc_unreachable ();
2962 return ret;
2965 case PLUS_EXPR:
2966 case POINTER_PLUS_EXPR:
2967 case MINUS_EXPR:
2968 case RANGE_EXPR:
2969 return (compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
2970 && compare_constant(TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
2972 CASE_CONVERT:
2973 case VIEW_CONVERT_EXPR:
2974 return compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2976 default:
2977 return 0;
2980 gcc_unreachable ();
2983 /* Make a copy of the whole tree structure for a constant. This
2984 handles the same types of nodes that compare_constant handles. */
2986 static tree
2987 copy_constant (tree exp)
2989 switch (TREE_CODE (exp))
2991 case ADDR_EXPR:
2992 /* For ADDR_EXPR, we do not want to copy the decl whose address
2993 is requested. We do want to copy constants though. */
2994 if (CONSTANT_CLASS_P (TREE_OPERAND (exp, 0)))
2995 return build1 (TREE_CODE (exp), TREE_TYPE (exp),
2996 copy_constant (TREE_OPERAND (exp, 0)));
2997 else
2998 return copy_node (exp);
3000 case INTEGER_CST:
3001 case REAL_CST:
3002 case FIXED_CST:
3003 case STRING_CST:
3004 return copy_node (exp);
3006 case COMPLEX_CST:
3007 return build_complex (TREE_TYPE (exp),
3008 copy_constant (TREE_REALPART (exp)),
3009 copy_constant (TREE_IMAGPART (exp)));
3011 case PLUS_EXPR:
3012 case POINTER_PLUS_EXPR:
3013 case MINUS_EXPR:
3014 return build2 (TREE_CODE (exp), TREE_TYPE (exp),
3015 copy_constant (TREE_OPERAND (exp, 0)),
3016 copy_constant (TREE_OPERAND (exp, 1)));
3018 CASE_CONVERT:
3019 case VIEW_CONVERT_EXPR:
3020 return build1 (TREE_CODE (exp), TREE_TYPE (exp),
3021 copy_constant (TREE_OPERAND (exp, 0)));
3023 case VECTOR_CST:
3024 return build_vector (TREE_TYPE (exp), VECTOR_CST_ELTS (exp));
3026 case CONSTRUCTOR:
3028 tree copy = copy_node (exp);
3029 vec<constructor_elt, va_gc> *v;
3030 unsigned HOST_WIDE_INT idx;
3031 tree purpose, value;
3033 vec_alloc (v, vec_safe_length (CONSTRUCTOR_ELTS (exp)));
3034 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, purpose, value)
3036 constructor_elt ce = {purpose, copy_constant (value)};
3037 v->quick_push (ce);
3039 CONSTRUCTOR_ELTS (copy) = v;
3040 return copy;
3043 default:
3044 gcc_unreachable ();
3048 /* Return the section into which constant EXP should be placed. */
3050 static section *
3051 get_constant_section (tree exp, unsigned int align)
3053 return targetm.asm_out.select_section (exp,
3054 compute_reloc_for_constant (exp),
3055 align);
3058 /* Return the size of constant EXP in bytes. */
3060 static HOST_WIDE_INT
3061 get_constant_size (tree exp)
3063 HOST_WIDE_INT size;
3065 size = int_size_in_bytes (TREE_TYPE (exp));
3066 if (TREE_CODE (exp) == STRING_CST)
3067 size = MAX (TREE_STRING_LENGTH (exp), size);
3068 return size;
3071 /* Subroutine of output_constant_def:
3072 No constant equal to EXP is known to have been output.
3073 Make a constant descriptor to enter EXP in the hash table.
3074 Assign the label number and construct RTL to refer to the
3075 constant's location in memory.
3076 Caller is responsible for updating the hash table. */
3078 static struct constant_descriptor_tree *
3079 build_constant_desc (tree exp)
3081 struct constant_descriptor_tree *desc;
3082 rtx symbol, rtl;
3083 char label[256];
3084 int labelno;
3085 tree decl;
3087 desc = ggc_alloc_constant_descriptor_tree ();
3088 desc->value = copy_constant (exp);
3090 /* Propagate marked-ness to copied constant. */
3091 if (flag_mudflap && mf_marked_p (exp))
3092 mf_mark (desc->value);
3094 /* Create a string containing the label name, in LABEL. */
3095 labelno = const_labelno++;
3096 ASM_GENERATE_INTERNAL_LABEL (label, "LC", labelno);
3098 /* Construct the VAR_DECL associated with the constant. */
3099 decl = build_decl (UNKNOWN_LOCATION, VAR_DECL, get_identifier (label),
3100 TREE_TYPE (exp));
3101 DECL_ARTIFICIAL (decl) = 1;
3102 DECL_IGNORED_P (decl) = 1;
3103 TREE_READONLY (decl) = 1;
3104 TREE_STATIC (decl) = 1;
3105 TREE_ADDRESSABLE (decl) = 1;
3106 /* We don't set the RTL yet as this would cause varpool to assume that the
3107 variable is referenced. Moreover, it would just be dropped in LTO mode.
3108 Instead we set the flag that will be recognized in make_decl_rtl. */
3109 DECL_IN_CONSTANT_POOL (decl) = 1;
3110 DECL_INITIAL (decl) = desc->value;
3111 /* ??? CONSTANT_ALIGNMENT hasn't been updated for vector types on most
3112 architectures so use DATA_ALIGNMENT as well, except for strings. */
3113 if (TREE_CODE (exp) == STRING_CST)
3115 #ifdef CONSTANT_ALIGNMENT
3116 DECL_ALIGN (decl) = CONSTANT_ALIGNMENT (exp, DECL_ALIGN (decl));
3117 #endif
3119 else
3120 align_variable (decl, 0);
3122 /* Now construct the SYMBOL_REF and the MEM. */
3123 if (use_object_blocks_p ())
3125 section *sect = get_constant_section (exp, DECL_ALIGN (decl));
3126 symbol = create_block_symbol (ggc_strdup (label),
3127 get_block_for_section (sect), -1);
3129 else
3130 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3131 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3132 SET_SYMBOL_REF_DECL (symbol, decl);
3133 TREE_CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3135 rtl = gen_const_mem (TYPE_MODE (TREE_TYPE (exp)), symbol);
3136 set_mem_attributes (rtl, exp, 1);
3137 set_mem_alias_set (rtl, 0);
3138 set_mem_alias_set (rtl, const_alias_set);
3140 /* We cannot share RTX'es in pool entries.
3141 Mark this piece of RTL as required for unsharing. */
3142 RTX_FLAG (rtl, used) = 1;
3144 /* Set flags or add text to the name to record information, such as
3145 that it is a local symbol. If the name is changed, the macro
3146 ASM_OUTPUT_LABELREF will have to know how to strip this
3147 information. This call might invalidate our local variable
3148 SYMBOL; we can't use it afterward. */
3149 targetm.encode_section_info (exp, rtl, true);
3151 desc->rtl = rtl;
3153 return desc;
3156 /* Return an rtx representing a reference to constant data in memory
3157 for the constant expression EXP.
3159 If assembler code for such a constant has already been output,
3160 return an rtx to refer to it.
3161 Otherwise, output such a constant in memory
3162 and generate an rtx for it.
3164 If DEFER is nonzero, this constant can be deferred and output only
3165 if referenced in the function after all optimizations.
3167 `const_desc_table' records which constants already have label strings. */
3170 output_constant_def (tree exp, int defer)
3172 struct constant_descriptor_tree *desc;
3173 struct constant_descriptor_tree key;
3174 void **loc;
3176 /* Look up EXP in the table of constant descriptors. If we didn't find
3177 it, create a new one. */
3178 key.value = exp;
3179 key.hash = const_hash_1 (exp);
3180 loc = htab_find_slot_with_hash (const_desc_htab, &key, key.hash, INSERT);
3182 desc = (struct constant_descriptor_tree *) *loc;
3183 if (desc == 0)
3185 desc = build_constant_desc (exp);
3186 desc->hash = key.hash;
3187 *loc = desc;
3190 maybe_output_constant_def_contents (desc, defer);
3191 return desc->rtl;
3194 /* Subroutine of output_constant_def: Decide whether or not we need to
3195 output the constant DESC now, and if so, do it. */
3196 static void
3197 maybe_output_constant_def_contents (struct constant_descriptor_tree *desc,
3198 int defer)
3200 rtx symbol = XEXP (desc->rtl, 0);
3201 tree exp = desc->value;
3203 if (flag_syntax_only)
3204 return;
3206 if (TREE_ASM_WRITTEN (exp))
3207 /* Already output; don't do it again. */
3208 return;
3210 /* We can always defer constants as long as the context allows
3211 doing so. */
3212 if (defer)
3214 /* Increment n_deferred_constants if it exists. It needs to be at
3215 least as large as the number of constants actually referred to
3216 by the function. If it's too small we'll stop looking too early
3217 and fail to emit constants; if it's too large we'll only look
3218 through the entire function when we could have stopped earlier. */
3219 if (cfun)
3220 n_deferred_constants++;
3221 return;
3224 output_constant_def_contents (symbol);
3227 /* Subroutine of output_constant_def_contents. Output the definition
3228 of constant EXP, which is pointed to by label LABEL. ALIGN is the
3229 constant's alignment in bits. */
3231 static void
3232 assemble_constant_contents (tree exp, const char *label, unsigned int align)
3234 HOST_WIDE_INT size;
3236 size = get_constant_size (exp);
3238 /* Do any machine/system dependent processing of the constant. */
3239 targetm.asm_out.declare_constant_name (asm_out_file, label, exp, size);
3241 /* Output the value of EXP. */
3242 output_constant (exp, size, align);
3245 /* We must output the constant data referred to by SYMBOL; do so. */
3247 static void
3248 output_constant_def_contents (rtx symbol)
3250 tree decl = SYMBOL_REF_DECL (symbol);
3251 tree exp = DECL_INITIAL (decl);
3252 unsigned int align;
3253 bool asan_protected = false;
3255 /* Make sure any other constants whose addresses appear in EXP
3256 are assigned label numbers. */
3257 output_addressed_constants (exp);
3259 /* We are no longer deferring this constant. */
3260 TREE_ASM_WRITTEN (decl) = TREE_ASM_WRITTEN (exp) = 1;
3262 if (flag_asan && TREE_CODE (exp) == STRING_CST
3263 && asan_protect_global (exp))
3265 asan_protected = true;
3266 DECL_ALIGN (decl) = MAX (DECL_ALIGN (decl),
3267 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
3270 /* If the constant is part of an object block, make sure that the
3271 decl has been positioned within its block, but do not write out
3272 its definition yet. output_object_blocks will do that later. */
3273 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
3274 place_block_symbol (symbol);
3275 else
3277 align = DECL_ALIGN (decl);
3278 switch_to_section (get_constant_section (exp, align));
3279 if (align > BITS_PER_UNIT)
3280 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
3281 assemble_constant_contents (exp, XSTR (symbol, 0), align);
3282 if (asan_protected)
3284 HOST_WIDE_INT size = get_constant_size (exp);
3285 assemble_zeros (asan_red_zone_size (size));
3288 if (flag_mudflap)
3289 mudflap_enqueue_constant (exp);
3292 /* Look up EXP in the table of constant descriptors. Return the rtl
3293 if it has been emitted, else null. */
3296 lookup_constant_def (tree exp)
3298 struct constant_descriptor_tree *desc;
3299 struct constant_descriptor_tree key;
3301 key.value = exp;
3302 key.hash = const_hash_1 (exp);
3303 desc = (struct constant_descriptor_tree *)
3304 htab_find_with_hash (const_desc_htab, &key, key.hash);
3306 return (desc ? desc->rtl : NULL_RTX);
3309 /* Return a tree representing a reference to constant data in memory
3310 for the constant expression EXP.
3312 This is the counterpart of output_constant_def at the Tree level. */
3314 tree
3315 tree_output_constant_def (tree exp)
3317 struct constant_descriptor_tree *desc, key;
3318 void **loc;
3319 tree decl;
3321 /* Look up EXP in the table of constant descriptors. If we didn't find
3322 it, create a new one. */
3323 key.value = exp;
3324 key.hash = const_hash_1 (exp);
3325 loc = htab_find_slot_with_hash (const_desc_htab, &key, key.hash, INSERT);
3327 desc = (struct constant_descriptor_tree *) *loc;
3328 if (desc == 0)
3330 desc = build_constant_desc (exp);
3331 desc->hash = key.hash;
3332 *loc = desc;
3335 decl = SYMBOL_REF_DECL (XEXP (desc->rtl, 0));
3336 varpool_finalize_decl (decl);
3337 return decl;
3340 /* Used in the hash tables to avoid outputting the same constant
3341 twice. Unlike 'struct constant_descriptor_tree', RTX constants
3342 are output once per function, not once per file. */
3343 /* ??? Only a few targets need per-function constant pools. Most
3344 can use one per-file pool. Should add a targetm bit to tell the
3345 difference. */
3347 struct GTY(()) rtx_constant_pool {
3348 /* Pointers to first and last constant in pool, as ordered by offset. */
3349 struct constant_descriptor_rtx *first;
3350 struct constant_descriptor_rtx *last;
3352 /* Hash facility for making memory-constants from constant rtl-expressions.
3353 It is used on RISC machines where immediate integer arguments and
3354 constant addresses are restricted so that such constants must be stored
3355 in memory. */
3356 htab_t GTY((param_is (struct constant_descriptor_rtx))) const_rtx_htab;
3358 /* Current offset in constant pool (does not include any
3359 machine-specific header). */
3360 HOST_WIDE_INT offset;
3363 struct GTY((chain_next ("%h.next"))) constant_descriptor_rtx {
3364 struct constant_descriptor_rtx *next;
3365 rtx mem;
3366 rtx sym;
3367 rtx constant;
3368 HOST_WIDE_INT offset;
3369 hashval_t hash;
3370 enum machine_mode mode;
3371 unsigned int align;
3372 int labelno;
3373 int mark;
3376 /* Hash and compare functions for const_rtx_htab. */
3378 static hashval_t
3379 const_desc_rtx_hash (const void *ptr)
3381 const struct constant_descriptor_rtx *const desc
3382 = (const struct constant_descriptor_rtx *) ptr;
3383 return desc->hash;
3386 static int
3387 const_desc_rtx_eq (const void *a, const void *b)
3389 const struct constant_descriptor_rtx *const x
3390 = (const struct constant_descriptor_rtx *) a;
3391 const struct constant_descriptor_rtx *const y
3392 = (const struct constant_descriptor_rtx *) b;
3394 if (x->mode != y->mode)
3395 return 0;
3396 return rtx_equal_p (x->constant, y->constant);
3399 /* This is the worker function for const_rtx_hash, called via for_each_rtx. */
3401 static int
3402 const_rtx_hash_1 (rtx *xp, void *data)
3404 unsigned HOST_WIDE_INT hwi;
3405 enum machine_mode mode;
3406 enum rtx_code code;
3407 hashval_t h, *hp;
3408 rtx x;
3410 x = *xp;
3411 code = GET_CODE (x);
3412 mode = GET_MODE (x);
3413 h = (hashval_t) code * 1048573 + mode;
3415 switch (code)
3417 case CONST_INT:
3418 hwi = INTVAL (x);
3419 fold_hwi:
3421 int shift = sizeof (hashval_t) * CHAR_BIT;
3422 const int n = sizeof (HOST_WIDE_INT) / sizeof (hashval_t);
3423 int i;
3425 h ^= (hashval_t) hwi;
3426 for (i = 1; i < n; ++i)
3428 hwi >>= shift;
3429 h ^= (hashval_t) hwi;
3432 break;
3434 case CONST_DOUBLE:
3435 if (mode == VOIDmode)
3437 hwi = CONST_DOUBLE_LOW (x) ^ CONST_DOUBLE_HIGH (x);
3438 goto fold_hwi;
3440 else
3441 h ^= real_hash (CONST_DOUBLE_REAL_VALUE (x));
3442 break;
3444 case CONST_FIXED:
3445 h ^= fixed_hash (CONST_FIXED_VALUE (x));
3446 break;
3448 case CONST_VECTOR:
3450 int i;
3451 for (i = XVECLEN (x, 0); i-- > 0; )
3452 h = h * 251 + const_rtx_hash_1 (&XVECEXP (x, 0, i), data);
3454 break;
3456 case SYMBOL_REF:
3457 h ^= htab_hash_string (XSTR (x, 0));
3458 break;
3460 case LABEL_REF:
3461 h = h * 251 + CODE_LABEL_NUMBER (XEXP (x, 0));
3462 break;
3464 case UNSPEC:
3465 case UNSPEC_VOLATILE:
3466 h = h * 251 + XINT (x, 1);
3467 break;
3469 default:
3470 break;
3473 hp = (hashval_t *) data;
3474 *hp = *hp * 509 + h;
3475 return 0;
3478 /* Compute a hash value for X, which should be a constant. */
3480 static hashval_t
3481 const_rtx_hash (rtx x)
3483 hashval_t h = 0;
3484 for_each_rtx (&x, const_rtx_hash_1, &h);
3485 return h;
3489 /* Create and return a new rtx constant pool. */
3491 static struct rtx_constant_pool *
3492 create_constant_pool (void)
3494 struct rtx_constant_pool *pool;
3496 pool = ggc_alloc_rtx_constant_pool ();
3497 pool->const_rtx_htab = htab_create_ggc (31, const_desc_rtx_hash,
3498 const_desc_rtx_eq, NULL);
3499 pool->first = NULL;
3500 pool->last = NULL;
3501 pool->offset = 0;
3502 return pool;
3505 /* Initialize constant pool hashing for a new function. */
3507 void
3508 init_varasm_status (void)
3510 crtl->varasm.pool = create_constant_pool ();
3511 crtl->varasm.deferred_constants = 0;
3514 /* Given a MINUS expression, simplify it if both sides
3515 include the same symbol. */
3518 simplify_subtraction (rtx x)
3520 rtx r = simplify_rtx (x);
3521 return r ? r : x;
3524 /* Given a constant rtx X, make (or find) a memory constant for its value
3525 and return a MEM rtx to refer to it in memory. */
3528 force_const_mem (enum machine_mode mode, rtx x)
3530 struct constant_descriptor_rtx *desc, tmp;
3531 struct rtx_constant_pool *pool;
3532 char label[256];
3533 rtx def, symbol;
3534 hashval_t hash;
3535 unsigned int align;
3536 void **slot;
3538 /* If we're not allowed to drop X into the constant pool, don't. */
3539 if (targetm.cannot_force_const_mem (mode, x))
3540 return NULL_RTX;
3542 /* Record that this function has used a constant pool entry. */
3543 crtl->uses_const_pool = 1;
3545 /* Decide which pool to use. */
3546 pool = (targetm.use_blocks_for_constant_p (mode, x)
3547 ? shared_constant_pool
3548 : crtl->varasm.pool);
3550 /* Lookup the value in the hashtable. */
3551 tmp.constant = x;
3552 tmp.mode = mode;
3553 hash = const_rtx_hash (x);
3554 slot = htab_find_slot_with_hash (pool->const_rtx_htab, &tmp, hash, INSERT);
3555 desc = (struct constant_descriptor_rtx *) *slot;
3557 /* If the constant was already present, return its memory. */
3558 if (desc)
3559 return copy_rtx (desc->mem);
3561 /* Otherwise, create a new descriptor. */
3562 desc = ggc_alloc_constant_descriptor_rtx ();
3563 *slot = desc;
3565 /* Align the location counter as required by EXP's data type. */
3566 align = GET_MODE_ALIGNMENT (mode == VOIDmode ? word_mode : mode);
3567 #ifdef CONSTANT_ALIGNMENT
3569 tree type = lang_hooks.types.type_for_mode (mode, 0);
3570 if (type != NULL_TREE)
3571 align = CONSTANT_ALIGNMENT (make_tree (type, x), align);
3573 #endif
3575 pool->offset += (align / BITS_PER_UNIT) - 1;
3576 pool->offset &= ~ ((align / BITS_PER_UNIT) - 1);
3578 desc->next = NULL;
3579 desc->constant = copy_rtx (tmp.constant);
3580 desc->offset = pool->offset;
3581 desc->hash = hash;
3582 desc->mode = mode;
3583 desc->align = align;
3584 desc->labelno = const_labelno;
3585 desc->mark = 0;
3587 pool->offset += GET_MODE_SIZE (mode);
3588 if (pool->last)
3589 pool->last->next = desc;
3590 else
3591 pool->first = pool->last = desc;
3592 pool->last = desc;
3594 /* Create a string containing the label name, in LABEL. */
3595 ASM_GENERATE_INTERNAL_LABEL (label, "LC", const_labelno);
3596 ++const_labelno;
3598 /* Construct the SYMBOL_REF. Make sure to mark it as belonging to
3599 the constants pool. */
3600 if (use_object_blocks_p () && targetm.use_blocks_for_constant_p (mode, x))
3602 section *sect = targetm.asm_out.select_rtx_section (mode, x, align);
3603 symbol = create_block_symbol (ggc_strdup (label),
3604 get_block_for_section (sect), -1);
3606 else
3607 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3608 desc->sym = symbol;
3609 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3610 CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3611 SET_SYMBOL_REF_CONSTANT (symbol, desc);
3613 /* Construct the MEM. */
3614 desc->mem = def = gen_const_mem (mode, symbol);
3615 set_mem_attributes (def, lang_hooks.types.type_for_mode (mode, 0), 1);
3616 set_mem_align (def, align);
3618 /* If we're dropping a label to the constant pool, make sure we
3619 don't delete it. */
3620 if (GET_CODE (x) == LABEL_REF)
3621 LABEL_PRESERVE_P (XEXP (x, 0)) = 1;
3623 return copy_rtx (def);
3626 /* Given a constant pool SYMBOL_REF, return the corresponding constant. */
3629 get_pool_constant (rtx addr)
3631 return SYMBOL_REF_CONSTANT (addr)->constant;
3634 /* Given a constant pool SYMBOL_REF, return the corresponding constant
3635 and whether it has been output or not. */
3638 get_pool_constant_mark (rtx addr, bool *pmarked)
3640 struct constant_descriptor_rtx *desc;
3642 desc = SYMBOL_REF_CONSTANT (addr);
3643 *pmarked = (desc->mark != 0);
3644 return desc->constant;
3647 /* Similar, return the mode. */
3649 enum machine_mode
3650 get_pool_mode (const_rtx addr)
3652 return SYMBOL_REF_CONSTANT (addr)->mode;
3655 /* Return the size of the constant pool. */
3658 get_pool_size (void)
3660 return crtl->varasm.pool->offset;
3663 /* Worker function for output_constant_pool_1. Emit assembly for X
3664 in MODE with known alignment ALIGN. */
3666 static void
3667 output_constant_pool_2 (enum machine_mode mode, rtx x, unsigned int align)
3669 switch (GET_MODE_CLASS (mode))
3671 case MODE_FLOAT:
3672 case MODE_DECIMAL_FLOAT:
3674 REAL_VALUE_TYPE r;
3676 gcc_assert (CONST_DOUBLE_AS_FLOAT_P (x));
3677 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
3678 assemble_real (r, mode, align);
3679 break;
3682 case MODE_INT:
3683 case MODE_PARTIAL_INT:
3684 case MODE_FRACT:
3685 case MODE_UFRACT:
3686 case MODE_ACCUM:
3687 case MODE_UACCUM:
3688 assemble_integer (x, GET_MODE_SIZE (mode), align, 1);
3689 break;
3691 case MODE_VECTOR_FLOAT:
3692 case MODE_VECTOR_INT:
3693 case MODE_VECTOR_FRACT:
3694 case MODE_VECTOR_UFRACT:
3695 case MODE_VECTOR_ACCUM:
3696 case MODE_VECTOR_UACCUM:
3698 int i, units;
3699 enum machine_mode submode = GET_MODE_INNER (mode);
3700 unsigned int subalign = MIN (align, GET_MODE_BITSIZE (submode));
3702 gcc_assert (GET_CODE (x) == CONST_VECTOR);
3703 units = CONST_VECTOR_NUNITS (x);
3705 for (i = 0; i < units; i++)
3707 rtx elt = CONST_VECTOR_ELT (x, i);
3708 output_constant_pool_2 (submode, elt, i ? subalign : align);
3711 break;
3713 default:
3714 gcc_unreachable ();
3718 /* Worker function for output_constant_pool. Emit constant DESC,
3719 giving it ALIGN bits of alignment. */
3721 static void
3722 output_constant_pool_1 (struct constant_descriptor_rtx *desc,
3723 unsigned int align)
3725 rtx x, tmp;
3727 x = desc->constant;
3729 /* See if X is a LABEL_REF (or a CONST referring to a LABEL_REF)
3730 whose CODE_LABEL has been deleted. This can occur if a jump table
3731 is eliminated by optimization. If so, write a constant of zero
3732 instead. Note that this can also happen by turning the
3733 CODE_LABEL into a NOTE. */
3734 /* ??? This seems completely and utterly wrong. Certainly it's
3735 not true for NOTE_INSN_DELETED_LABEL, but I disbelieve proper
3736 functioning even with INSN_DELETED_P and friends. */
3738 tmp = x;
3739 switch (GET_CODE (tmp))
3741 case CONST:
3742 if (GET_CODE (XEXP (tmp, 0)) != PLUS
3743 || GET_CODE (XEXP (XEXP (tmp, 0), 0)) != LABEL_REF)
3744 break;
3745 tmp = XEXP (XEXP (tmp, 0), 0);
3746 /* FALLTHRU */
3748 case LABEL_REF:
3749 tmp = XEXP (tmp, 0);
3750 gcc_assert (!INSN_DELETED_P (tmp));
3751 gcc_assert (!NOTE_P (tmp)
3752 || NOTE_KIND (tmp) != NOTE_INSN_DELETED);
3753 break;
3755 default:
3756 break;
3759 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3760 ASM_OUTPUT_SPECIAL_POOL_ENTRY (asm_out_file, x, desc->mode,
3761 align, desc->labelno, done);
3762 #endif
3764 assemble_align (align);
3766 /* Output the label. */
3767 targetm.asm_out.internal_label (asm_out_file, "LC", desc->labelno);
3769 /* Output the data. */
3770 output_constant_pool_2 (desc->mode, x, align);
3772 /* Make sure all constants in SECTION_MERGE and not SECTION_STRINGS
3773 sections have proper size. */
3774 if (align > GET_MODE_BITSIZE (desc->mode)
3775 && in_section
3776 && (in_section->common.flags & SECTION_MERGE))
3777 assemble_align (align);
3779 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3780 done:
3781 #endif
3782 return;
3785 /* Given a SYMBOL_REF CURRENT_RTX, mark it and all constants it refers
3786 to as used. Emit referenced deferred strings. This function can
3787 be used with for_each_rtx to mark all SYMBOL_REFs in an rtx. */
3789 static int
3790 mark_constant (rtx *current_rtx, void *data ATTRIBUTE_UNUSED)
3792 rtx x = *current_rtx;
3794 if (x == NULL_RTX || GET_CODE (x) != SYMBOL_REF)
3795 return 0;
3797 if (CONSTANT_POOL_ADDRESS_P (x))
3799 struct constant_descriptor_rtx *desc = SYMBOL_REF_CONSTANT (x);
3800 if (desc->mark == 0)
3802 desc->mark = 1;
3803 for_each_rtx (&desc->constant, mark_constant, NULL);
3806 else if (TREE_CONSTANT_POOL_ADDRESS_P (x))
3808 tree decl = SYMBOL_REF_DECL (x);
3809 if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
3811 n_deferred_constants--;
3812 output_constant_def_contents (x);
3816 return -1;
3819 /* Look through appropriate parts of INSN, marking all entries in the
3820 constant pool which are actually being used. Entries that are only
3821 referenced by other constants are also marked as used. Emit
3822 deferred strings that are used. */
3824 static void
3825 mark_constants (rtx insn)
3827 if (!INSN_P (insn))
3828 return;
3830 /* Insns may appear inside a SEQUENCE. Only check the patterns of
3831 insns, not any notes that may be attached. We don't want to mark
3832 a constant just because it happens to appear in a REG_EQUIV note. */
3833 if (GET_CODE (PATTERN (insn)) == SEQUENCE)
3835 rtx seq = PATTERN (insn);
3836 int i, n = XVECLEN (seq, 0);
3837 for (i = 0; i < n; ++i)
3839 rtx subinsn = XVECEXP (seq, 0, i);
3840 if (INSN_P (subinsn))
3841 for_each_rtx (&PATTERN (subinsn), mark_constant, NULL);
3844 else
3845 for_each_rtx (&PATTERN (insn), mark_constant, NULL);
3848 /* Look through the instructions for this function, and mark all the
3849 entries in POOL which are actually being used. Emit deferred constants
3850 which have indeed been used. */
3852 static void
3853 mark_constant_pool (void)
3855 rtx insn;
3857 if (!crtl->uses_const_pool && n_deferred_constants == 0)
3858 return;
3860 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3861 mark_constants (insn);
3864 /* Write all the constants in POOL. */
3866 static void
3867 output_constant_pool_contents (struct rtx_constant_pool *pool)
3869 struct constant_descriptor_rtx *desc;
3871 for (desc = pool->first; desc ; desc = desc->next)
3872 if (desc->mark)
3874 /* If the constant is part of an object_block, make sure that
3875 the constant has been positioned within its block, but do not
3876 write out its definition yet. output_object_blocks will do
3877 that later. */
3878 if (SYMBOL_REF_HAS_BLOCK_INFO_P (desc->sym)
3879 && SYMBOL_REF_BLOCK (desc->sym))
3880 place_block_symbol (desc->sym);
3881 else
3883 switch_to_section (targetm.asm_out.select_rtx_section
3884 (desc->mode, desc->constant, desc->align));
3885 output_constant_pool_1 (desc, desc->align);
3890 /* Mark all constants that are used in the current function, then write
3891 out the function's private constant pool. */
3893 static void
3894 output_constant_pool (const char *fnname ATTRIBUTE_UNUSED,
3895 tree fndecl ATTRIBUTE_UNUSED)
3897 struct rtx_constant_pool *pool = crtl->varasm.pool;
3899 /* It is possible for gcc to call force_const_mem and then to later
3900 discard the instructions which refer to the constant. In such a
3901 case we do not need to output the constant. */
3902 mark_constant_pool ();
3904 #ifdef ASM_OUTPUT_POOL_PROLOGUE
3905 ASM_OUTPUT_POOL_PROLOGUE (asm_out_file, fnname, fndecl, pool->offset);
3906 #endif
3908 output_constant_pool_contents (pool);
3910 #ifdef ASM_OUTPUT_POOL_EPILOGUE
3911 ASM_OUTPUT_POOL_EPILOGUE (asm_out_file, fnname, fndecl, pool->offset);
3912 #endif
3915 /* Write the contents of the shared constant pool. */
3917 void
3918 output_shared_constant_pool (void)
3920 output_constant_pool_contents (shared_constant_pool);
3923 /* Determine what kind of relocations EXP may need. */
3926 compute_reloc_for_constant (tree exp)
3928 int reloc = 0, reloc2;
3929 tree tem;
3931 switch (TREE_CODE (exp))
3933 case ADDR_EXPR:
3934 case FDESC_EXPR:
3935 /* Go inside any operations that get_inner_reference can handle and see
3936 if what's inside is a constant: no need to do anything here for
3937 addresses of variables or functions. */
3938 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
3939 tem = TREE_OPERAND (tem, 0))
3942 if (TREE_CODE (tem) == MEM_REF
3943 && TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR)
3945 reloc = compute_reloc_for_constant (TREE_OPERAND (tem, 0));
3946 break;
3949 if (TREE_PUBLIC (tem))
3950 reloc |= 2;
3951 else
3952 reloc |= 1;
3953 break;
3955 case PLUS_EXPR:
3956 case POINTER_PLUS_EXPR:
3957 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3958 reloc |= compute_reloc_for_constant (TREE_OPERAND (exp, 1));
3959 break;
3961 case MINUS_EXPR:
3962 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3963 reloc2 = compute_reloc_for_constant (TREE_OPERAND (exp, 1));
3964 /* The difference of two local labels is computable at link time. */
3965 if (reloc == 1 && reloc2 == 1)
3966 reloc = 0;
3967 else
3968 reloc |= reloc2;
3969 break;
3971 CASE_CONVERT:
3972 case VIEW_CONVERT_EXPR:
3973 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3974 break;
3976 case CONSTRUCTOR:
3978 unsigned HOST_WIDE_INT idx;
3979 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
3980 if (tem != 0)
3981 reloc |= compute_reloc_for_constant (tem);
3983 break;
3985 default:
3986 break;
3988 return reloc;
3991 /* Find all the constants whose addresses are referenced inside of EXP,
3992 and make sure assembler code with a label has been output for each one.
3993 Indicate whether an ADDR_EXPR has been encountered. */
3995 static void
3996 output_addressed_constants (tree exp)
3998 tree tem;
4000 switch (TREE_CODE (exp))
4002 case ADDR_EXPR:
4003 case FDESC_EXPR:
4004 /* Go inside any operations that get_inner_reference can handle and see
4005 if what's inside is a constant: no need to do anything here for
4006 addresses of variables or functions. */
4007 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
4008 tem = TREE_OPERAND (tem, 0))
4011 /* If we have an initialized CONST_DECL, retrieve the initializer. */
4012 if (TREE_CODE (tem) == CONST_DECL && DECL_INITIAL (tem))
4013 tem = DECL_INITIAL (tem);
4015 if (CONSTANT_CLASS_P (tem) || TREE_CODE (tem) == CONSTRUCTOR)
4016 output_constant_def (tem, 0);
4018 if (TREE_CODE (tem) == MEM_REF)
4019 output_addressed_constants (TREE_OPERAND (tem, 0));
4020 break;
4022 case PLUS_EXPR:
4023 case POINTER_PLUS_EXPR:
4024 case MINUS_EXPR:
4025 output_addressed_constants (TREE_OPERAND (exp, 1));
4026 /* Fall through. */
4028 CASE_CONVERT:
4029 case VIEW_CONVERT_EXPR:
4030 output_addressed_constants (TREE_OPERAND (exp, 0));
4031 break;
4033 case CONSTRUCTOR:
4035 unsigned HOST_WIDE_INT idx;
4036 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
4037 if (tem != 0)
4038 output_addressed_constants (tem);
4040 break;
4042 default:
4043 break;
4047 /* Whether a constructor CTOR is a valid static constant initializer if all
4048 its elements are. This used to be internal to initializer_constant_valid_p
4049 and has been exposed to let other functions like categorize_ctor_elements
4050 evaluate the property while walking a constructor for other purposes. */
4052 bool
4053 constructor_static_from_elts_p (const_tree ctor)
4055 return (TREE_CONSTANT (ctor)
4056 && (TREE_CODE (TREE_TYPE (ctor)) == UNION_TYPE
4057 || TREE_CODE (TREE_TYPE (ctor)) == RECORD_TYPE
4058 || TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE));
4061 static tree initializer_constant_valid_p_1 (tree value, tree endtype,
4062 tree *cache);
4064 /* A subroutine of initializer_constant_valid_p. VALUE is a MINUS_EXPR,
4065 PLUS_EXPR or POINTER_PLUS_EXPR. This looks for cases of VALUE
4066 which are valid when ENDTYPE is an integer of any size; in
4067 particular, this does not accept a pointer minus a constant. This
4068 returns null_pointer_node if the VALUE is an absolute constant
4069 which can be used to initialize a static variable. Otherwise it
4070 returns NULL. */
4072 static tree
4073 narrowing_initializer_constant_valid_p (tree value, tree endtype, tree *cache)
4075 tree op0, op1;
4077 if (!INTEGRAL_TYPE_P (endtype))
4078 return NULL_TREE;
4080 op0 = TREE_OPERAND (value, 0);
4081 op1 = TREE_OPERAND (value, 1);
4083 /* Like STRIP_NOPS except allow the operand mode to widen. This
4084 works around a feature of fold that simplifies (int)(p1 - p2) to
4085 ((int)p1 - (int)p2) under the theory that the narrower operation
4086 is cheaper. */
4088 while (CONVERT_EXPR_P (op0)
4089 || TREE_CODE (op0) == NON_LVALUE_EXPR)
4091 tree inner = TREE_OPERAND (op0, 0);
4092 if (inner == error_mark_node
4093 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4094 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))
4095 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
4096 break;
4097 op0 = inner;
4100 while (CONVERT_EXPR_P (op1)
4101 || TREE_CODE (op1) == NON_LVALUE_EXPR)
4103 tree inner = TREE_OPERAND (op1, 0);
4104 if (inner == error_mark_node
4105 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4106 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op1)))
4107 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
4108 break;
4109 op1 = inner;
4112 op0 = initializer_constant_valid_p_1 (op0, endtype, cache);
4113 if (!op0)
4114 return NULL_TREE;
4116 op1 = initializer_constant_valid_p_1 (op1, endtype,
4117 cache ? cache + 2 : NULL);
4118 /* Both initializers must be known. */
4119 if (op1)
4121 if (op0 == op1
4122 && (op0 == null_pointer_node
4123 || TREE_CODE (value) == MINUS_EXPR))
4124 return null_pointer_node;
4126 /* Support differences between labels. */
4127 if (TREE_CODE (op0) == LABEL_DECL
4128 && TREE_CODE (op1) == LABEL_DECL)
4129 return null_pointer_node;
4131 if (TREE_CODE (op0) == STRING_CST
4132 && TREE_CODE (op1) == STRING_CST
4133 && operand_equal_p (op0, op1, 1))
4134 return null_pointer_node;
4137 return NULL_TREE;
4140 /* Helper function of initializer_constant_valid_p.
4141 Return nonzero if VALUE is a valid constant-valued expression
4142 for use in initializing a static variable; one that can be an
4143 element of a "constant" initializer.
4145 Return null_pointer_node if the value is absolute;
4146 if it is relocatable, return the variable that determines the relocation.
4147 We assume that VALUE has been folded as much as possible;
4148 therefore, we do not need to check for such things as
4149 arithmetic-combinations of integers.
4151 Use CACHE (pointer to 2 tree values) for caching if non-NULL. */
4153 static tree
4154 initializer_constant_valid_p_1 (tree value, tree endtype, tree *cache)
4156 tree ret;
4158 switch (TREE_CODE (value))
4160 case CONSTRUCTOR:
4161 if (constructor_static_from_elts_p (value))
4163 unsigned HOST_WIDE_INT idx;
4164 tree elt;
4165 bool absolute = true;
4167 if (cache && cache[0] == value)
4168 return cache[1];
4169 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
4171 tree reloc;
4172 reloc = initializer_constant_valid_p_1 (elt, TREE_TYPE (elt),
4173 NULL);
4174 if (!reloc)
4176 if (cache)
4178 cache[0] = value;
4179 cache[1] = NULL_TREE;
4181 return NULL_TREE;
4183 if (reloc != null_pointer_node)
4184 absolute = false;
4186 /* For a non-absolute relocation, there is no single
4187 variable that can be "the variable that determines the
4188 relocation." */
4189 if (cache)
4191 cache[0] = value;
4192 cache[1] = absolute ? null_pointer_node : error_mark_node;
4194 return absolute ? null_pointer_node : error_mark_node;
4197 return TREE_STATIC (value) ? null_pointer_node : NULL_TREE;
4199 case INTEGER_CST:
4200 case VECTOR_CST:
4201 case REAL_CST:
4202 case FIXED_CST:
4203 case STRING_CST:
4204 case COMPLEX_CST:
4205 return null_pointer_node;
4207 case ADDR_EXPR:
4208 case FDESC_EXPR:
4210 tree op0 = staticp (TREE_OPERAND (value, 0));
4211 if (op0)
4213 /* "&(*a).f" is like unto pointer arithmetic. If "a" turns out
4214 to be a constant, this is old-skool offsetof-like nonsense. */
4215 if (TREE_CODE (op0) == INDIRECT_REF
4216 && TREE_CONSTANT (TREE_OPERAND (op0, 0)))
4217 return null_pointer_node;
4218 /* Taking the address of a nested function involves a trampoline,
4219 unless we don't need or want one. */
4220 if (TREE_CODE (op0) == FUNCTION_DECL
4221 && DECL_STATIC_CHAIN (op0)
4222 && !TREE_NO_TRAMPOLINE (value))
4223 return NULL_TREE;
4224 /* "&{...}" requires a temporary to hold the constructed
4225 object. */
4226 if (TREE_CODE (op0) == CONSTRUCTOR)
4227 return NULL_TREE;
4229 return op0;
4232 case NON_LVALUE_EXPR:
4233 return initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4234 endtype, cache);
4236 case VIEW_CONVERT_EXPR:
4238 tree src = TREE_OPERAND (value, 0);
4239 tree src_type = TREE_TYPE (src);
4240 tree dest_type = TREE_TYPE (value);
4242 /* Allow view-conversions from aggregate to non-aggregate type only
4243 if the bit pattern is fully preserved afterwards; otherwise, the
4244 RTL expander won't be able to apply a subsequent transformation
4245 to the underlying constructor. */
4246 if (AGGREGATE_TYPE_P (src_type) && !AGGREGATE_TYPE_P (dest_type))
4248 if (TYPE_MODE (endtype) == TYPE_MODE (dest_type))
4249 return initializer_constant_valid_p_1 (src, endtype, cache);
4250 else
4251 return NULL_TREE;
4254 /* Allow all other kinds of view-conversion. */
4255 return initializer_constant_valid_p_1 (src, endtype, cache);
4258 CASE_CONVERT:
4260 tree src = TREE_OPERAND (value, 0);
4261 tree src_type = TREE_TYPE (src);
4262 tree dest_type = TREE_TYPE (value);
4264 /* Allow conversions between pointer types, floating-point
4265 types, and offset types. */
4266 if ((POINTER_TYPE_P (dest_type) && POINTER_TYPE_P (src_type))
4267 || (FLOAT_TYPE_P (dest_type) && FLOAT_TYPE_P (src_type))
4268 || (TREE_CODE (dest_type) == OFFSET_TYPE
4269 && TREE_CODE (src_type) == OFFSET_TYPE))
4270 return initializer_constant_valid_p_1 (src, endtype, cache);
4272 /* Allow length-preserving conversions between integer types. */
4273 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type)
4274 && (TYPE_PRECISION (dest_type) == TYPE_PRECISION (src_type)))
4275 return initializer_constant_valid_p_1 (src, endtype, cache);
4277 /* Allow conversions between other integer types only if
4278 explicit value. */
4279 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type))
4281 tree inner = initializer_constant_valid_p_1 (src, endtype, cache);
4282 if (inner == null_pointer_node)
4283 return null_pointer_node;
4284 break;
4287 /* Allow (int) &foo provided int is as wide as a pointer. */
4288 if (INTEGRAL_TYPE_P (dest_type) && POINTER_TYPE_P (src_type)
4289 && (TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type)))
4290 return initializer_constant_valid_p_1 (src, endtype, cache);
4292 /* Likewise conversions from int to pointers, but also allow
4293 conversions from 0. */
4294 if ((POINTER_TYPE_P (dest_type)
4295 || TREE_CODE (dest_type) == OFFSET_TYPE)
4296 && INTEGRAL_TYPE_P (src_type))
4298 if (TREE_CODE (src) == INTEGER_CST
4299 && TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type))
4300 return null_pointer_node;
4301 if (integer_zerop (src))
4302 return null_pointer_node;
4303 else if (TYPE_PRECISION (dest_type) <= TYPE_PRECISION (src_type))
4304 return initializer_constant_valid_p_1 (src, endtype, cache);
4307 /* Allow conversions to struct or union types if the value
4308 inside is okay. */
4309 if (TREE_CODE (dest_type) == RECORD_TYPE
4310 || TREE_CODE (dest_type) == UNION_TYPE)
4311 return initializer_constant_valid_p_1 (src, endtype, cache);
4313 break;
4315 case POINTER_PLUS_EXPR:
4316 case PLUS_EXPR:
4317 /* Any valid floating-point constants will have been folded by now;
4318 with -frounding-math we hit this with addition of two constants. */
4319 if (TREE_CODE (endtype) == REAL_TYPE)
4320 return NULL_TREE;
4321 if (cache && cache[0] == value)
4322 return cache[1];
4323 if (! INTEGRAL_TYPE_P (endtype)
4324 || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
4326 tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
4327 tree valid0
4328 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4329 endtype, ncache);
4330 tree valid1
4331 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
4332 endtype, ncache + 2);
4333 /* If either term is absolute, use the other term's relocation. */
4334 if (valid0 == null_pointer_node)
4335 ret = valid1;
4336 else if (valid1 == null_pointer_node)
4337 ret = valid0;
4338 /* Support narrowing pointer differences. */
4339 else
4340 ret = narrowing_initializer_constant_valid_p (value, endtype,
4341 ncache);
4343 else
4344 /* Support narrowing pointer differences. */
4345 ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
4346 if (cache)
4348 cache[0] = value;
4349 cache[1] = ret;
4351 return ret;
4353 case MINUS_EXPR:
4354 if (TREE_CODE (endtype) == REAL_TYPE)
4355 return NULL_TREE;
4356 if (cache && cache[0] == value)
4357 return cache[1];
4358 if (! INTEGRAL_TYPE_P (endtype)
4359 || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
4361 tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
4362 tree valid0
4363 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4364 endtype, ncache);
4365 tree valid1
4366 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
4367 endtype, ncache + 2);
4368 /* Win if second argument is absolute. */
4369 if (valid1 == null_pointer_node)
4370 ret = valid0;
4371 /* Win if both arguments have the same relocation.
4372 Then the value is absolute. */
4373 else if (valid0 == valid1 && valid0 != 0)
4374 ret = null_pointer_node;
4375 /* Since GCC guarantees that string constants are unique in the
4376 generated code, a subtraction between two copies of the same
4377 constant string is absolute. */
4378 else if (valid0 && TREE_CODE (valid0) == STRING_CST
4379 && valid1 && TREE_CODE (valid1) == STRING_CST
4380 && operand_equal_p (valid0, valid1, 1))
4381 ret = null_pointer_node;
4382 /* Support narrowing differences. */
4383 else
4384 ret = narrowing_initializer_constant_valid_p (value, endtype,
4385 ncache);
4387 else
4388 /* Support narrowing differences. */
4389 ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
4390 if (cache)
4392 cache[0] = value;
4393 cache[1] = ret;
4395 return ret;
4397 default:
4398 break;
4401 return NULL_TREE;
4404 /* Return nonzero if VALUE is a valid constant-valued expression
4405 for use in initializing a static variable; one that can be an
4406 element of a "constant" initializer.
4408 Return null_pointer_node if the value is absolute;
4409 if it is relocatable, return the variable that determines the relocation.
4410 We assume that VALUE has been folded as much as possible;
4411 therefore, we do not need to check for such things as
4412 arithmetic-combinations of integers. */
4413 tree
4414 initializer_constant_valid_p (tree value, tree endtype)
4416 return initializer_constant_valid_p_1 (value, endtype, NULL);
4419 /* Return true if VALUE is a valid constant-valued expression
4420 for use in initializing a static bit-field; one that can be
4421 an element of a "constant" initializer. */
4423 bool
4424 initializer_constant_valid_for_bitfield_p (tree value)
4426 /* For bitfields we support integer constants or possibly nested aggregates
4427 of such. */
4428 switch (TREE_CODE (value))
4430 case CONSTRUCTOR:
4432 unsigned HOST_WIDE_INT idx;
4433 tree elt;
4435 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
4436 if (!initializer_constant_valid_for_bitfield_p (elt))
4437 return false;
4438 return true;
4441 case INTEGER_CST:
4442 case REAL_CST:
4443 return true;
4445 case VIEW_CONVERT_EXPR:
4446 case NON_LVALUE_EXPR:
4447 return
4448 initializer_constant_valid_for_bitfield_p (TREE_OPERAND (value, 0));
4450 default:
4451 break;
4454 return false;
4457 /* output_constructor outer state of relevance in recursive calls, typically
4458 for nested aggregate bitfields. */
4460 typedef struct {
4461 unsigned int bit_offset; /* current position in ... */
4462 int byte; /* ... the outer byte buffer. */
4463 } oc_outer_state;
4465 static unsigned HOST_WIDE_INT
4466 output_constructor (tree, unsigned HOST_WIDE_INT, unsigned int,
4467 oc_outer_state *);
4469 /* Output assembler code for constant EXP to FILE, with no label.
4470 This includes the pseudo-op such as ".int" or ".byte", and a newline.
4471 Assumes output_addressed_constants has been done on EXP already.
4473 Generate exactly SIZE bytes of assembler data, padding at the end
4474 with zeros if necessary. SIZE must always be specified.
4476 SIZE is important for structure constructors,
4477 since trailing members may have been omitted from the constructor.
4478 It is also important for initialization of arrays from string constants
4479 since the full length of the string constant might not be wanted.
4480 It is also needed for initialization of unions, where the initializer's
4481 type is just one member, and that may not be as long as the union.
4483 There a case in which we would fail to output exactly SIZE bytes:
4484 for a structure constructor that wants to produce more than SIZE bytes.
4485 But such constructors will never be generated for any possible input.
4487 ALIGN is the alignment of the data in bits. */
4489 void
4490 output_constant (tree exp, unsigned HOST_WIDE_INT size, unsigned int align)
4492 enum tree_code code;
4493 unsigned HOST_WIDE_INT thissize;
4495 if (size == 0 || flag_syntax_only)
4496 return;
4498 /* See if we're trying to initialize a pointer in a non-default mode
4499 to the address of some declaration somewhere. If the target says
4500 the mode is valid for pointers, assume the target has a way of
4501 resolving it. */
4502 if (TREE_CODE (exp) == NOP_EXPR
4503 && POINTER_TYPE_P (TREE_TYPE (exp))
4504 && targetm.addr_space.valid_pointer_mode
4505 (TYPE_MODE (TREE_TYPE (exp)),
4506 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
4508 tree saved_type = TREE_TYPE (exp);
4510 /* Peel off any intermediate conversions-to-pointer for valid
4511 pointer modes. */
4512 while (TREE_CODE (exp) == NOP_EXPR
4513 && POINTER_TYPE_P (TREE_TYPE (exp))
4514 && targetm.addr_space.valid_pointer_mode
4515 (TYPE_MODE (TREE_TYPE (exp)),
4516 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
4517 exp = TREE_OPERAND (exp, 0);
4519 /* If what we're left with is the address of something, we can
4520 convert the address to the final type and output it that
4521 way. */
4522 if (TREE_CODE (exp) == ADDR_EXPR)
4523 exp = build1 (ADDR_EXPR, saved_type, TREE_OPERAND (exp, 0));
4524 /* Likewise for constant ints. */
4525 else if (TREE_CODE (exp) == INTEGER_CST)
4526 exp = build_int_cst_wide (saved_type, TREE_INT_CST_LOW (exp),
4527 TREE_INT_CST_HIGH (exp));
4531 /* Eliminate any conversions since we'll be outputting the underlying
4532 constant. */
4533 while (CONVERT_EXPR_P (exp)
4534 || TREE_CODE (exp) == NON_LVALUE_EXPR
4535 || TREE_CODE (exp) == VIEW_CONVERT_EXPR)
4537 HOST_WIDE_INT type_size = int_size_in_bytes (TREE_TYPE (exp));
4538 HOST_WIDE_INT op_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (exp, 0)));
4540 /* Make sure eliminating the conversion is really a no-op, except with
4541 VIEW_CONVERT_EXPRs to allow for wild Ada unchecked conversions and
4542 union types to allow for Ada unchecked unions. */
4543 if (type_size > op_size
4544 && TREE_CODE (exp) != VIEW_CONVERT_EXPR
4545 && TREE_CODE (TREE_TYPE (exp)) != UNION_TYPE)
4546 /* Keep the conversion. */
4547 break;
4548 else
4549 exp = TREE_OPERAND (exp, 0);
4552 code = TREE_CODE (TREE_TYPE (exp));
4553 thissize = int_size_in_bytes (TREE_TYPE (exp));
4555 /* Allow a constructor with no elements for any data type.
4556 This means to fill the space with zeros. */
4557 if (TREE_CODE (exp) == CONSTRUCTOR
4558 && vec_safe_is_empty (CONSTRUCTOR_ELTS (exp)))
4560 assemble_zeros (size);
4561 return;
4564 if (TREE_CODE (exp) == FDESC_EXPR)
4566 #ifdef ASM_OUTPUT_FDESC
4567 HOST_WIDE_INT part = tree_low_cst (TREE_OPERAND (exp, 1), 0);
4568 tree decl = TREE_OPERAND (exp, 0);
4569 ASM_OUTPUT_FDESC (asm_out_file, decl, part);
4570 #else
4571 gcc_unreachable ();
4572 #endif
4573 return;
4576 /* Now output the underlying data. If we've handling the padding, return.
4577 Otherwise, break and ensure SIZE is the size written. */
4578 switch (code)
4580 case BOOLEAN_TYPE:
4581 case INTEGER_TYPE:
4582 case ENUMERAL_TYPE:
4583 case POINTER_TYPE:
4584 case REFERENCE_TYPE:
4585 case OFFSET_TYPE:
4586 case FIXED_POINT_TYPE:
4587 if (! assemble_integer (expand_expr (exp, NULL_RTX, VOIDmode,
4588 EXPAND_INITIALIZER),
4589 MIN (size, thissize), align, 0))
4590 error ("initializer for integer/fixed-point value is too complicated");
4591 break;
4593 case REAL_TYPE:
4594 if (TREE_CODE (exp) != REAL_CST)
4595 error ("initializer for floating value is not a floating constant");
4596 else
4597 assemble_real (TREE_REAL_CST (exp), TYPE_MODE (TREE_TYPE (exp)), align);
4598 break;
4600 case COMPLEX_TYPE:
4601 output_constant (TREE_REALPART (exp), thissize / 2, align);
4602 output_constant (TREE_IMAGPART (exp), thissize / 2,
4603 min_align (align, BITS_PER_UNIT * (thissize / 2)));
4604 break;
4606 case ARRAY_TYPE:
4607 case VECTOR_TYPE:
4608 switch (TREE_CODE (exp))
4610 case CONSTRUCTOR:
4611 output_constructor (exp, size, align, NULL);
4612 return;
4613 case STRING_CST:
4614 thissize = MIN ((unsigned HOST_WIDE_INT)TREE_STRING_LENGTH (exp),
4615 size);
4616 assemble_string (TREE_STRING_POINTER (exp), thissize);
4617 break;
4619 case VECTOR_CST:
4621 int elt_size;
4622 unsigned int i, nalign;
4623 enum machine_mode inner;
4625 inner = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
4626 nalign = MIN (align, GET_MODE_ALIGNMENT (inner));
4628 elt_size = GET_MODE_SIZE (inner);
4630 output_constant (VECTOR_CST_ELT (exp, 0), elt_size, align);
4631 thissize = elt_size;
4632 for (i = 1; i < VECTOR_CST_NELTS (exp); ++i)
4634 output_constant (VECTOR_CST_ELT (exp, i), elt_size, nalign);
4635 thissize += elt_size;
4637 break;
4639 default:
4640 gcc_unreachable ();
4642 break;
4644 case RECORD_TYPE:
4645 case UNION_TYPE:
4646 gcc_assert (TREE_CODE (exp) == CONSTRUCTOR);
4647 output_constructor (exp, size, align, NULL);
4648 return;
4650 case ERROR_MARK:
4651 return;
4653 default:
4654 gcc_unreachable ();
4657 if (size > thissize)
4658 assemble_zeros (size - thissize);
4662 /* Subroutine of output_constructor, used for computing the size of
4663 arrays of unspecified length. VAL must be a CONSTRUCTOR of an array
4664 type with an unspecified upper bound. */
4666 static unsigned HOST_WIDE_INT
4667 array_size_for_constructor (tree val)
4669 tree max_index;
4670 unsigned HOST_WIDE_INT cnt;
4671 tree index, value, tmp;
4672 double_int i;
4674 /* This code used to attempt to handle string constants that are not
4675 arrays of single-bytes, but nothing else does, so there's no point in
4676 doing it here. */
4677 if (TREE_CODE (val) == STRING_CST)
4678 return TREE_STRING_LENGTH (val);
4680 max_index = NULL_TREE;
4681 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (val), cnt, index, value)
4683 if (TREE_CODE (index) == RANGE_EXPR)
4684 index = TREE_OPERAND (index, 1);
4685 if (max_index == NULL_TREE || tree_int_cst_lt (max_index, index))
4686 max_index = index;
4689 if (max_index == NULL_TREE)
4690 return 0;
4692 /* Compute the total number of array elements. */
4693 tmp = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (val)));
4694 i = tree_to_double_int (max_index) - tree_to_double_int (tmp);
4695 i += double_int_one;
4697 /* Multiply by the array element unit size to find number of bytes. */
4698 i *= tree_to_double_int (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (val))));
4700 gcc_assert (i.fits_uhwi ());
4701 return i.low;
4704 /* Other datastructures + helpers for output_constructor. */
4706 /* output_constructor local state to support interaction with helpers. */
4708 typedef struct {
4710 /* Received arguments. */
4711 tree exp; /* Constructor expression. */
4712 unsigned HOST_WIDE_INT size; /* # bytes to output - pad if necessary. */
4713 unsigned int align; /* Known initial alignment. */
4715 /* Constructor expression data. */
4716 tree type; /* Expression type. */
4717 tree field; /* Current field decl in a record. */
4718 tree min_index; /* Lower bound if specified for an array. */
4720 /* Output processing state. */
4721 HOST_WIDE_INT total_bytes; /* # bytes output so far / current position. */
4722 bool byte_buffer_in_use; /* Whether byte ... */
4723 int byte; /* ... contains part of a bitfield byte yet to
4724 be output. */
4726 int last_relative_index; /* Implicit or explicit index of the last
4727 array element output within a bitfield. */
4728 /* Current element. */
4729 tree val; /* Current element value. */
4730 tree index; /* Current element index. */
4732 } oc_local_state;
4734 /* Helper for output_constructor. From the current LOCAL state, output a
4735 RANGE_EXPR element. */
4737 static void
4738 output_constructor_array_range (oc_local_state *local)
4740 unsigned HOST_WIDE_INT fieldsize
4741 = int_size_in_bytes (TREE_TYPE (local->type));
4743 HOST_WIDE_INT lo_index
4744 = tree_low_cst (TREE_OPERAND (local->index, 0), 0);
4745 HOST_WIDE_INT hi_index
4746 = tree_low_cst (TREE_OPERAND (local->index, 1), 0);
4747 HOST_WIDE_INT index;
4749 unsigned int align2
4750 = min_align (local->align, fieldsize * BITS_PER_UNIT);
4752 for (index = lo_index; index <= hi_index; index++)
4754 /* Output the element's initial value. */
4755 if (local->val == NULL_TREE)
4756 assemble_zeros (fieldsize);
4757 else
4758 output_constant (local->val, fieldsize, align2);
4760 /* Count its size. */
4761 local->total_bytes += fieldsize;
4765 /* Helper for output_constructor. From the current LOCAL state, output a
4766 field element that is not true bitfield or part of an outer one. */
4768 static void
4769 output_constructor_regular_field (oc_local_state *local)
4771 /* Field size and position. Since this structure is static, we know the
4772 positions are constant. */
4773 unsigned HOST_WIDE_INT fieldsize;
4774 HOST_WIDE_INT fieldpos;
4776 unsigned int align2;
4778 if (local->index != NULL_TREE)
4780 /* Perform the index calculation in modulo arithmetic but
4781 sign-extend the result because Ada has negative DECL_FIELD_OFFSETs
4782 but we are using an unsigned sizetype. */
4783 unsigned prec = TYPE_PRECISION (sizetype);
4784 double_int idx = tree_to_double_int (local->index)
4785 - tree_to_double_int (local->min_index);
4786 idx = idx.sext (prec);
4787 fieldpos = (tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (local->val)), 1)
4788 * idx.low);
4790 else if (local->field != NULL_TREE)
4791 fieldpos = int_byte_position (local->field);
4792 else
4793 fieldpos = 0;
4795 /* Output any buffered-up bit-fields preceding this element. */
4796 if (local->byte_buffer_in_use)
4798 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
4799 local->total_bytes++;
4800 local->byte_buffer_in_use = false;
4803 /* Advance to offset of this element.
4804 Note no alignment needed in an array, since that is guaranteed
4805 if each element has the proper size. */
4806 if ((local->field != NULL_TREE || local->index != NULL_TREE)
4807 && fieldpos != local->total_bytes)
4809 gcc_assert (fieldpos >= local->total_bytes);
4810 assemble_zeros (fieldpos - local->total_bytes);
4811 local->total_bytes = fieldpos;
4814 /* Find the alignment of this element. */
4815 align2 = min_align (local->align, BITS_PER_UNIT * fieldpos);
4817 /* Determine size this element should occupy. */
4818 if (local->field)
4820 fieldsize = 0;
4822 /* If this is an array with an unspecified upper bound,
4823 the initializer determines the size. */
4824 /* ??? This ought to only checked if DECL_SIZE_UNIT is NULL,
4825 but we cannot do this until the deprecated support for
4826 initializing zero-length array members is removed. */
4827 if (TREE_CODE (TREE_TYPE (local->field)) == ARRAY_TYPE
4828 && TYPE_DOMAIN (TREE_TYPE (local->field))
4829 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (local->field))))
4831 fieldsize = array_size_for_constructor (local->val);
4832 /* Given a non-empty initialization, this field had
4833 better be last. */
4834 gcc_assert (!fieldsize || !DECL_CHAIN (local->field));
4836 else
4837 fieldsize = tree_low_cst (DECL_SIZE_UNIT (local->field), 1);
4839 else
4840 fieldsize = int_size_in_bytes (TREE_TYPE (local->type));
4842 /* Output the element's initial value. */
4843 if (local->val == NULL_TREE)
4844 assemble_zeros (fieldsize);
4845 else
4846 output_constant (local->val, fieldsize, align2);
4848 /* Count its size. */
4849 local->total_bytes += fieldsize;
4852 /* Helper for output_constructor. From the current LOCAL and OUTER states,
4853 output an element that is a true bitfield or part of an outer one. */
4855 static void
4856 output_constructor_bitfield (oc_local_state *local, oc_outer_state *outer)
4858 /* Bit size of this element. */
4859 HOST_WIDE_INT ebitsize
4860 = (local->field
4861 ? tree_low_cst (DECL_SIZE (local->field), 1)
4862 : tree_low_cst (TYPE_SIZE (TREE_TYPE (local->type)), 1));
4864 /* Relative index of this element if this is an array component. */
4865 HOST_WIDE_INT relative_index
4866 = (!local->field
4867 ? (local->index
4868 ? (tree_low_cst (local->index, 0)
4869 - tree_low_cst (local->min_index, 0))
4870 : local->last_relative_index + 1)
4871 : 0);
4873 /* Bit position of this element from the start of the containing
4874 constructor. */
4875 HOST_WIDE_INT constructor_relative_ebitpos
4876 = (local->field
4877 ? int_bit_position (local->field)
4878 : ebitsize * relative_index);
4880 /* Bit position of this element from the start of a possibly ongoing
4881 outer byte buffer. */
4882 HOST_WIDE_INT byte_relative_ebitpos
4883 = ((outer ? outer->bit_offset : 0) + constructor_relative_ebitpos);
4885 /* From the start of a possibly ongoing outer byte buffer, offsets to
4886 the first bit of this element and to the first bit past the end of
4887 this element. */
4888 HOST_WIDE_INT next_offset = byte_relative_ebitpos;
4889 HOST_WIDE_INT end_offset = byte_relative_ebitpos + ebitsize;
4891 local->last_relative_index = relative_index;
4893 if (local->val == NULL_TREE)
4894 local->val = integer_zero_node;
4896 while (TREE_CODE (local->val) == VIEW_CONVERT_EXPR
4897 || TREE_CODE (local->val) == NON_LVALUE_EXPR)
4898 local->val = TREE_OPERAND (local->val, 0);
4900 if (TREE_CODE (local->val) != INTEGER_CST
4901 && TREE_CODE (local->val) != CONSTRUCTOR)
4903 error ("invalid initial value for member %qE", DECL_NAME (local->field));
4904 return;
4907 /* If this field does not start in this (or, next) byte,
4908 skip some bytes. */
4909 if (next_offset / BITS_PER_UNIT != local->total_bytes)
4911 /* Output remnant of any bit field in previous bytes. */
4912 if (local->byte_buffer_in_use)
4914 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
4915 local->total_bytes++;
4916 local->byte_buffer_in_use = false;
4919 /* If still not at proper byte, advance to there. */
4920 if (next_offset / BITS_PER_UNIT != local->total_bytes)
4922 gcc_assert (next_offset / BITS_PER_UNIT >= local->total_bytes);
4923 assemble_zeros (next_offset / BITS_PER_UNIT - local->total_bytes);
4924 local->total_bytes = next_offset / BITS_PER_UNIT;
4928 /* Set up the buffer if necessary. */
4929 if (!local->byte_buffer_in_use)
4931 local->byte = 0;
4932 if (ebitsize > 0)
4933 local->byte_buffer_in_use = true;
4936 /* If this is nested constructor, recurse passing the bit offset and the
4937 pending data, then retrieve the new pending data afterwards. */
4938 if (TREE_CODE (local->val) == CONSTRUCTOR)
4940 oc_outer_state output_state;
4942 output_state.bit_offset = next_offset % BITS_PER_UNIT;
4943 output_state.byte = local->byte;
4944 local->total_bytes
4945 += output_constructor (local->val, 0, 0, &output_state);
4946 local->byte = output_state.byte;
4947 return;
4950 /* Otherwise, we must split the element into pieces that fall within
4951 separate bytes, and combine each byte with previous or following
4952 bit-fields. */
4953 while (next_offset < end_offset)
4955 int this_time;
4956 int shift;
4957 HOST_WIDE_INT value;
4958 HOST_WIDE_INT next_byte = next_offset / BITS_PER_UNIT;
4959 HOST_WIDE_INT next_bit = next_offset % BITS_PER_UNIT;
4961 /* Advance from byte to byte
4962 within this element when necessary. */
4963 while (next_byte != local->total_bytes)
4965 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
4966 local->total_bytes++;
4967 local->byte = 0;
4970 /* Number of bits we can process at once
4971 (all part of the same byte). */
4972 this_time = MIN (end_offset - next_offset,
4973 BITS_PER_UNIT - next_bit);
4974 if (BYTES_BIG_ENDIAN)
4976 /* On big-endian machine, take the most significant bits
4977 first (of the bits that are significant)
4978 and put them into bytes from the most significant end. */
4979 shift = end_offset - next_offset - this_time;
4981 /* Don't try to take a bunch of bits that cross
4982 the word boundary in the INTEGER_CST. We can
4983 only select bits from the LOW or HIGH part
4984 not from both. */
4985 if (shift < HOST_BITS_PER_WIDE_INT
4986 && shift + this_time > HOST_BITS_PER_WIDE_INT)
4988 this_time = shift + this_time - HOST_BITS_PER_WIDE_INT;
4989 shift = HOST_BITS_PER_WIDE_INT;
4992 /* Now get the bits from the appropriate constant word. */
4993 if (shift < HOST_BITS_PER_WIDE_INT)
4994 value = TREE_INT_CST_LOW (local->val);
4995 else
4997 gcc_assert (shift < HOST_BITS_PER_DOUBLE_INT);
4998 value = TREE_INT_CST_HIGH (local->val);
4999 shift -= HOST_BITS_PER_WIDE_INT;
5002 /* Get the result. This works only when:
5003 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
5004 local->byte |= (((value >> shift)
5005 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
5006 << (BITS_PER_UNIT - this_time - next_bit));
5008 else
5010 /* On little-endian machines,
5011 take first the least significant bits of the value
5012 and pack them starting at the least significant
5013 bits of the bytes. */
5014 shift = next_offset - byte_relative_ebitpos;
5016 /* Don't try to take a bunch of bits that cross
5017 the word boundary in the INTEGER_CST. We can
5018 only select bits from the LOW or HIGH part
5019 not from both. */
5020 if (shift < HOST_BITS_PER_WIDE_INT
5021 && shift + this_time > HOST_BITS_PER_WIDE_INT)
5022 this_time = (HOST_BITS_PER_WIDE_INT - shift);
5024 /* Now get the bits from the appropriate constant word. */
5025 if (shift < HOST_BITS_PER_WIDE_INT)
5026 value = TREE_INT_CST_LOW (local->val);
5027 else
5029 gcc_assert (shift < HOST_BITS_PER_DOUBLE_INT);
5030 value = TREE_INT_CST_HIGH (local->val);
5031 shift -= HOST_BITS_PER_WIDE_INT;
5034 /* Get the result. This works only when:
5035 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
5036 local->byte |= (((value >> shift)
5037 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
5038 << next_bit);
5041 next_offset += this_time;
5042 local->byte_buffer_in_use = true;
5046 /* Subroutine of output_constant, used for CONSTRUCTORs (aggregate constants).
5047 Generate at least SIZE bytes, padding if necessary. OUTER designates the
5048 caller output state of relevance in recursive invocations. */
5050 static unsigned HOST_WIDE_INT
5051 output_constructor (tree exp, unsigned HOST_WIDE_INT size,
5052 unsigned int align, oc_outer_state * outer)
5054 unsigned HOST_WIDE_INT cnt;
5055 constructor_elt *ce;
5057 oc_local_state local;
5059 /* Setup our local state to communicate with helpers. */
5060 local.exp = exp;
5061 local.size = size;
5062 local.align = align;
5064 local.total_bytes = 0;
5065 local.byte_buffer_in_use = outer != NULL;
5066 local.byte = outer ? outer->byte : 0;
5068 local.type = TREE_TYPE (exp);
5070 local.last_relative_index = -1;
5072 local.min_index = NULL_TREE;
5073 if (TREE_CODE (local.type) == ARRAY_TYPE
5074 && TYPE_DOMAIN (local.type) != NULL_TREE)
5075 local.min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (local.type));
5077 gcc_assert (HOST_BITS_PER_WIDE_INT >= BITS_PER_UNIT);
5079 /* As CE goes through the elements of the constant, FIELD goes through the
5080 structure fields if the constant is a structure. If the constant is a
5081 union, we override this by getting the field from the TREE_LIST element.
5082 But the constant could also be an array. Then FIELD is zero.
5084 There is always a maximum of one element in the chain LINK for unions
5085 (even if the initializer in a source program incorrectly contains
5086 more one). */
5088 local.field = NULL_TREE;
5089 if (TREE_CODE (local.type) == RECORD_TYPE)
5090 local.field = TYPE_FIELDS (local.type);
5092 for (cnt = 0;
5093 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), cnt, &ce);
5094 cnt++, local.field = local.field ? DECL_CHAIN (local.field) : 0)
5096 local.val = ce->value;
5097 local.index = NULL_TREE;
5099 /* The element in a union constructor specifies the proper field
5100 or index. */
5101 if (RECORD_OR_UNION_TYPE_P (local.type) && ce->index != NULL_TREE)
5102 local.field = ce->index;
5104 else if (TREE_CODE (local.type) == ARRAY_TYPE)
5105 local.index = ce->index;
5107 if (local.field && flag_verbose_asm)
5108 fprintf (asm_out_file, "%s %s:\n",
5109 ASM_COMMENT_START,
5110 DECL_NAME (local.field)
5111 ? IDENTIFIER_POINTER (DECL_NAME (local.field))
5112 : "<anonymous>");
5114 /* Eliminate the marker that makes a cast not be an lvalue. */
5115 if (local.val != NULL_TREE)
5116 STRIP_NOPS (local.val);
5118 /* Output the current element, using the appropriate helper ... */
5120 /* For an array slice not part of an outer bitfield. */
5121 if (!outer
5122 && local.index != NULL_TREE
5123 && TREE_CODE (local.index) == RANGE_EXPR)
5124 output_constructor_array_range (&local);
5126 /* For a field that is neither a true bitfield nor part of an outer one,
5127 known to be at least byte aligned and multiple-of-bytes long. */
5128 else if (!outer
5129 && (local.field == NULL_TREE
5130 || !CONSTRUCTOR_BITFIELD_P (local.field)))
5131 output_constructor_regular_field (&local);
5133 /* For a true bitfield or part of an outer one. Only INTEGER_CSTs are
5134 supported for scalar fields, so we may need to convert first. */
5135 else
5137 if (TREE_CODE (local.val) == REAL_CST)
5138 local.val
5139 = fold_unary (VIEW_CONVERT_EXPR,
5140 build_nonstandard_integer_type
5141 (TYPE_PRECISION (TREE_TYPE (local.val)), 0),
5142 local.val);
5143 output_constructor_bitfield (&local, outer);
5147 /* If we are not at toplevel, save the pending data for our caller.
5148 Otherwise output the pending data and padding zeros as needed. */
5149 if (outer)
5150 outer->byte = local.byte;
5151 else
5153 if (local.byte_buffer_in_use)
5155 assemble_integer (GEN_INT (local.byte), 1, BITS_PER_UNIT, 1);
5156 local.total_bytes++;
5159 if ((unsigned HOST_WIDE_INT)local.total_bytes < local.size)
5161 assemble_zeros (local.size - local.total_bytes);
5162 local.total_bytes = local.size;
5166 return local.total_bytes;
5169 /* Mark DECL as weak. */
5171 static void
5172 mark_weak (tree decl)
5174 DECL_WEAK (decl) = 1;
5176 if (DECL_RTL_SET_P (decl)
5177 && MEM_P (DECL_RTL (decl))
5178 && XEXP (DECL_RTL (decl), 0)
5179 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == SYMBOL_REF)
5180 SYMBOL_REF_WEAK (XEXP (DECL_RTL (decl), 0)) = 1;
5183 /* Merge weak status between NEWDECL and OLDDECL. */
5185 void
5186 merge_weak (tree newdecl, tree olddecl)
5188 if (DECL_WEAK (newdecl) == DECL_WEAK (olddecl))
5190 if (DECL_WEAK (newdecl) && TARGET_SUPPORTS_WEAK)
5192 tree *pwd;
5193 /* We put the NEWDECL on the weak_decls list at some point
5194 and OLDDECL as well. Keep just OLDDECL on the list. */
5195 for (pwd = &weak_decls; *pwd; pwd = &TREE_CHAIN (*pwd))
5196 if (TREE_VALUE (*pwd) == newdecl)
5198 *pwd = TREE_CHAIN (*pwd);
5199 break;
5202 return;
5205 if (DECL_WEAK (newdecl))
5207 tree wd;
5209 /* NEWDECL is weak, but OLDDECL is not. */
5211 /* If we already output the OLDDECL, we're in trouble; we can't
5212 go back and make it weak. This should never happen in
5213 unit-at-a-time compilation. */
5214 gcc_assert (!TREE_ASM_WRITTEN (olddecl));
5216 /* If we've already generated rtl referencing OLDDECL, we may
5217 have done so in a way that will not function properly with
5218 a weak symbol. Again in unit-at-a-time this should be
5219 impossible. */
5220 gcc_assert (!TREE_USED (olddecl)
5221 || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (olddecl)));
5223 if (TARGET_SUPPORTS_WEAK)
5225 /* We put the NEWDECL on the weak_decls list at some point.
5226 Replace it with the OLDDECL. */
5227 for (wd = weak_decls; wd; wd = TREE_CHAIN (wd))
5228 if (TREE_VALUE (wd) == newdecl)
5230 TREE_VALUE (wd) = olddecl;
5231 break;
5233 /* We may not find the entry on the list. If NEWDECL is a
5234 weak alias, then we will have already called
5235 globalize_decl to remove the entry; in that case, we do
5236 not need to do anything. */
5239 /* Make the OLDDECL weak; it's OLDDECL that we'll be keeping. */
5240 mark_weak (olddecl);
5242 else
5243 /* OLDDECL was weak, but NEWDECL was not explicitly marked as
5244 weak. Just update NEWDECL to indicate that it's weak too. */
5245 mark_weak (newdecl);
5248 /* Declare DECL to be a weak symbol. */
5250 void
5251 declare_weak (tree decl)
5253 gcc_assert (TREE_CODE (decl) != FUNCTION_DECL || !TREE_ASM_WRITTEN (decl));
5254 if (! TREE_PUBLIC (decl))
5255 error ("weak declaration of %q+D must be public", decl);
5256 else if (!TARGET_SUPPORTS_WEAK)
5257 warning (0, "weak declaration of %q+D not supported", decl);
5259 mark_weak (decl);
5260 if (!lookup_attribute ("weak", DECL_ATTRIBUTES (decl)))
5261 DECL_ATTRIBUTES (decl)
5262 = tree_cons (get_identifier ("weak"), NULL, DECL_ATTRIBUTES (decl));
5265 static void
5266 weak_finish_1 (tree decl)
5268 #if defined (ASM_WEAKEN_DECL) || defined (ASM_WEAKEN_LABEL)
5269 const char *const name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5270 #endif
5272 if (! TREE_USED (decl))
5273 return;
5275 #ifdef ASM_WEAKEN_DECL
5276 ASM_WEAKEN_DECL (asm_out_file, decl, name, NULL);
5277 #else
5278 #ifdef ASM_WEAKEN_LABEL
5279 ASM_WEAKEN_LABEL (asm_out_file, name);
5280 #else
5281 #ifdef ASM_OUTPUT_WEAK_ALIAS
5283 static bool warn_once = 0;
5284 if (! warn_once)
5286 warning (0, "only weak aliases are supported in this configuration");
5287 warn_once = 1;
5289 return;
5291 #endif
5292 #endif
5293 #endif
5296 /* Fiven an assembly name, find the decl it is associated with. */
5297 static tree
5298 find_decl (tree target)
5300 symtab_node node = symtab_node_for_asm (target);
5301 if (node)
5302 return node->symbol.decl;
5303 return NULL_TREE;
5306 /* This TREE_LIST contains weakref targets. */
5308 static GTY(()) tree weakref_targets;
5310 /* Emit any pending weak declarations. */
5312 void
5313 weak_finish (void)
5315 tree t;
5317 for (t = weakref_targets; t; t = TREE_CHAIN (t))
5319 tree alias_decl = TREE_PURPOSE (t);
5320 tree target = ultimate_transparent_alias_target (&TREE_VALUE (t));
5322 if (! TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (alias_decl)))
5323 /* Remove alias_decl from the weak list, but leave entries for
5324 the target alone. */
5325 target = NULL_TREE;
5326 #ifndef ASM_OUTPUT_WEAKREF
5327 else if (! TREE_SYMBOL_REFERENCED (target))
5329 /* Use ASM_WEAKEN_LABEL only if ASM_WEAKEN_DECL is not
5330 defined, otherwise we and weak_finish_1 would use
5331 different macros. */
5332 # if defined ASM_WEAKEN_LABEL && ! defined ASM_WEAKEN_DECL
5333 ASM_WEAKEN_LABEL (asm_out_file, IDENTIFIER_POINTER (target));
5334 # else
5335 tree decl = find_decl (target);
5337 if (! decl)
5339 decl = build_decl (DECL_SOURCE_LOCATION (alias_decl),
5340 TREE_CODE (alias_decl), target,
5341 TREE_TYPE (alias_decl));
5343 DECL_EXTERNAL (decl) = 1;
5344 TREE_PUBLIC (decl) = 1;
5345 DECL_ARTIFICIAL (decl) = 1;
5346 TREE_NOTHROW (decl) = TREE_NOTHROW (alias_decl);
5347 TREE_USED (decl) = 1;
5350 weak_finish_1 (decl);
5351 # endif
5353 #endif
5356 tree *p;
5357 tree t2;
5359 /* Remove the alias and the target from the pending weak list
5360 so that we do not emit any .weak directives for the former,
5361 nor multiple .weak directives for the latter. */
5362 for (p = &weak_decls; (t2 = *p) ; )
5364 if (TREE_VALUE (t2) == alias_decl
5365 || target == DECL_ASSEMBLER_NAME (TREE_VALUE (t2)))
5366 *p = TREE_CHAIN (t2);
5367 else
5368 p = &TREE_CHAIN (t2);
5371 /* Remove other weakrefs to the same target, to speed things up. */
5372 for (p = &TREE_CHAIN (t); (t2 = *p) ; )
5374 if (target == ultimate_transparent_alias_target (&TREE_VALUE (t2)))
5375 *p = TREE_CHAIN (t2);
5376 else
5377 p = &TREE_CHAIN (t2);
5382 for (t = weak_decls; t; t = TREE_CHAIN (t))
5384 tree decl = TREE_VALUE (t);
5386 weak_finish_1 (decl);
5390 /* Emit the assembly bits to indicate that DECL is globally visible. */
5392 static void
5393 globalize_decl (tree decl)
5396 #if defined (ASM_WEAKEN_LABEL) || defined (ASM_WEAKEN_DECL)
5397 if (DECL_WEAK (decl))
5399 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
5400 tree *p, t;
5402 #ifdef ASM_WEAKEN_DECL
5403 ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
5404 #else
5405 ASM_WEAKEN_LABEL (asm_out_file, name);
5406 #endif
5408 /* Remove this function from the pending weak list so that
5409 we do not emit multiple .weak directives for it. */
5410 for (p = &weak_decls; (t = *p) ; )
5412 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
5413 *p = TREE_CHAIN (t);
5414 else
5415 p = &TREE_CHAIN (t);
5418 /* Remove weakrefs to the same target from the pending weakref
5419 list, for the same reason. */
5420 for (p = &weakref_targets; (t = *p) ; )
5422 if (DECL_ASSEMBLER_NAME (decl)
5423 == ultimate_transparent_alias_target (&TREE_VALUE (t)))
5424 *p = TREE_CHAIN (t);
5425 else
5426 p = &TREE_CHAIN (t);
5429 return;
5431 #endif
5433 targetm.asm_out.globalize_decl_name (asm_out_file, decl);
5436 vec<alias_pair, va_gc> *alias_pairs;
5438 /* Output the assembler code for a define (equate) using ASM_OUTPUT_DEF
5439 or ASM_OUTPUT_DEF_FROM_DECLS. The function defines the symbol whose
5440 tree node is DECL to have the value of the tree node TARGET. */
5442 void
5443 do_assemble_alias (tree decl, tree target)
5445 /* Emulated TLS had better not get this var. */
5446 gcc_assert(!(!targetm.have_tls
5447 && TREE_CODE (decl) == VAR_DECL
5448 && DECL_THREAD_LOCAL_P (decl)));
5450 if (TREE_ASM_WRITTEN (decl))
5451 return;
5453 /* We must force creation of DECL_RTL for debug info generation, even though
5454 we don't use it here. */
5455 make_decl_rtl (decl);
5457 TREE_ASM_WRITTEN (decl) = 1;
5458 TREE_ASM_WRITTEN (DECL_ASSEMBLER_NAME (decl)) = 1;
5460 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
5462 ultimate_transparent_alias_target (&target);
5464 if (!TREE_SYMBOL_REFERENCED (target))
5465 weakref_targets = tree_cons (decl, target, weakref_targets);
5467 #ifdef ASM_OUTPUT_WEAKREF
5468 ASM_OUTPUT_WEAKREF (asm_out_file, decl,
5469 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
5470 IDENTIFIER_POINTER (target));
5471 #else
5472 if (!TARGET_SUPPORTS_WEAK)
5474 error_at (DECL_SOURCE_LOCATION (decl),
5475 "weakref is not supported in this configuration");
5476 return;
5478 #endif
5479 return;
5482 #ifdef ASM_OUTPUT_DEF
5483 /* Make name accessible from other files, if appropriate. */
5485 if (TREE_PUBLIC (decl))
5487 globalize_decl (decl);
5488 maybe_assemble_visibility (decl);
5490 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
5492 #if defined (ASM_OUTPUT_TYPE_DIRECTIVE)
5493 if (targetm.has_ifunc_p ())
5494 ASM_OUTPUT_TYPE_DIRECTIVE
5495 (asm_out_file, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
5496 IFUNC_ASM_TYPE);
5497 else
5498 #endif
5499 error_at (DECL_SOURCE_LOCATION (decl),
5500 "ifunc is not supported on this target");
5503 # ifdef ASM_OUTPUT_DEF_FROM_DECLS
5504 ASM_OUTPUT_DEF_FROM_DECLS (asm_out_file, decl, target);
5505 # else
5506 ASM_OUTPUT_DEF (asm_out_file,
5507 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
5508 IDENTIFIER_POINTER (target));
5509 # endif
5510 #elif defined (ASM_OUTPUT_WEAK_ALIAS) || defined (ASM_WEAKEN_DECL)
5512 const char *name;
5513 tree *p, t;
5515 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5516 # ifdef ASM_WEAKEN_DECL
5517 ASM_WEAKEN_DECL (asm_out_file, decl, name, IDENTIFIER_POINTER (target));
5518 # else
5519 ASM_OUTPUT_WEAK_ALIAS (asm_out_file, name, IDENTIFIER_POINTER (target));
5520 # endif
5521 /* Remove this function from the pending weak list so that
5522 we do not emit multiple .weak directives for it. */
5523 for (p = &weak_decls; (t = *p) ; )
5524 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
5525 *p = TREE_CHAIN (t);
5526 else
5527 p = &TREE_CHAIN (t);
5529 /* Remove weakrefs to the same target from the pending weakref
5530 list, for the same reason. */
5531 for (p = &weakref_targets; (t = *p) ; )
5533 if (DECL_ASSEMBLER_NAME (decl)
5534 == ultimate_transparent_alias_target (&TREE_VALUE (t)))
5535 *p = TREE_CHAIN (t);
5536 else
5537 p = &TREE_CHAIN (t);
5540 #endif
5543 /* Emit an assembler directive to make the symbol for DECL an alias to
5544 the symbol for TARGET. */
5546 void
5547 assemble_alias (tree decl, tree target)
5549 tree target_decl;
5551 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
5553 tree alias = DECL_ASSEMBLER_NAME (decl);
5555 ultimate_transparent_alias_target (&target);
5557 if (alias == target)
5558 error ("weakref %q+D ultimately targets itself", decl);
5559 else
5561 #ifndef ASM_OUTPUT_WEAKREF
5562 IDENTIFIER_TRANSPARENT_ALIAS (alias) = 1;
5563 TREE_CHAIN (alias) = target;
5564 #endif
5566 if (TREE_PUBLIC (decl))
5567 error ("weakref %q+D must have static linkage", decl);
5569 else
5571 #if !defined (ASM_OUTPUT_DEF)
5572 # if !defined(ASM_OUTPUT_WEAK_ALIAS) && !defined (ASM_WEAKEN_DECL)
5573 error_at (DECL_SOURCE_LOCATION (decl),
5574 "alias definitions not supported in this configuration");
5575 return;
5576 # else
5577 if (!DECL_WEAK (decl))
5579 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
5580 error_at (DECL_SOURCE_LOCATION (decl),
5581 "ifunc is not supported in this configuration");
5582 else
5583 error_at (DECL_SOURCE_LOCATION (decl),
5584 "only weak aliases are supported in this configuration");
5585 return;
5587 # endif
5588 #endif
5590 TREE_USED (decl) = 1;
5592 /* Allow aliases to aliases. */
5593 if (TREE_CODE (decl) == FUNCTION_DECL)
5594 cgraph_get_create_node (decl)->alias = true;
5595 else
5596 varpool_node_for_decl (decl)->alias = true;
5598 /* If the target has already been emitted, we don't have to queue the
5599 alias. This saves a tad of memory. */
5600 if (cgraph_global_info_ready)
5601 target_decl = find_decl (target);
5602 else
5603 target_decl= NULL;
5604 if ((target_decl && TREE_ASM_WRITTEN (target_decl))
5605 || cgraph_state >= CGRAPH_STATE_EXPANSION)
5606 do_assemble_alias (decl, target);
5607 else
5609 alias_pair p = {decl, target};
5610 vec_safe_push (alias_pairs, p);
5614 /* Record and output a table of translations from original function
5615 to its transaction aware clone. Note that tm_pure functions are
5616 considered to be their own clone. */
5618 static GTY((if_marked ("tree_map_marked_p"), param_is (struct tree_map)))
5619 htab_t tm_clone_hash;
5621 void
5622 record_tm_clone_pair (tree o, tree n)
5624 struct tree_map **slot, *h;
5626 if (tm_clone_hash == NULL)
5627 tm_clone_hash = htab_create_ggc (32, tree_map_hash, tree_map_eq, 0);
5629 h = ggc_alloc_tree_map ();
5630 h->hash = htab_hash_pointer (o);
5631 h->base.from = o;
5632 h->to = n;
5634 slot = (struct tree_map **)
5635 htab_find_slot_with_hash (tm_clone_hash, h, h->hash, INSERT);
5636 *slot = h;
5639 tree
5640 get_tm_clone_pair (tree o)
5642 if (tm_clone_hash)
5644 struct tree_map *h, in;
5646 in.base.from = o;
5647 in.hash = htab_hash_pointer (o);
5648 h = (struct tree_map *) htab_find_with_hash (tm_clone_hash,
5649 &in, in.hash);
5650 if (h)
5651 return h->to;
5653 return NULL_TREE;
5656 typedef struct tm_alias_pair
5658 unsigned int uid;
5659 tree from;
5660 tree to;
5661 } tm_alias_pair;
5664 /* Helper function for finish_tm_clone_pairs. Dump a hash table entry
5665 into a VEC in INFO. */
5667 static int
5668 dump_tm_clone_to_vec (void **slot, void *info)
5670 struct tree_map *map = (struct tree_map *) *slot;
5671 vec<tm_alias_pair> *tm_alias_pairs = (vec<tm_alias_pair> *) info;
5672 tm_alias_pair p = {DECL_UID (map->base.from), map->base.from, map->to};
5673 tm_alias_pairs->safe_push (p);
5674 return 1;
5677 /* Dump the actual pairs to the .tm_clone_table section. */
5679 static void
5680 dump_tm_clone_pairs (vec<tm_alias_pair> tm_alias_pairs)
5682 unsigned i;
5683 tm_alias_pair *p;
5684 bool switched = false;
5686 FOR_EACH_VEC_ELT (tm_alias_pairs, i, p)
5688 tree src = p->from;
5689 tree dst = p->to;
5690 struct cgraph_node *src_n = cgraph_get_node (src);
5691 struct cgraph_node *dst_n = cgraph_get_node (dst);
5693 /* The function ipa_tm_create_version() marks the clone as needed if
5694 the original function was needed. But we also mark the clone as
5695 needed if we ever called the clone indirectly through
5696 TM_GETTMCLONE. If neither of these are true, we didn't generate
5697 a clone, and we didn't call it indirectly... no sense keeping it
5698 in the clone table. */
5699 if (!dst_n || !dst_n->analyzed)
5700 continue;
5702 /* This covers the case where we have optimized the original
5703 function away, and only access the transactional clone. */
5704 if (!src_n || !src_n->analyzed)
5705 continue;
5707 if (!switched)
5709 switch_to_section (targetm.asm_out.tm_clone_table_section ());
5710 assemble_align (POINTER_SIZE);
5711 switched = true;
5714 assemble_integer (XEXP (DECL_RTL (src), 0),
5715 POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
5716 assemble_integer (XEXP (DECL_RTL (dst), 0),
5717 POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
5721 /* Provide a default for the tm_clone_table section. */
5723 section *
5724 default_clone_table_section (void)
5726 return get_named_section (NULL, ".tm_clone_table", 3);
5729 /* Helper comparison function for qsorting by the DECL_UID stored in
5730 alias_pair->emitted_diags. */
5732 static int
5733 tm_alias_pair_cmp (const void *x, const void *y)
5735 const tm_alias_pair *p1 = (const tm_alias_pair *) x;
5736 const tm_alias_pair *p2 = (const tm_alias_pair *) y;
5737 if (p1->uid < p2->uid)
5738 return -1;
5739 if (p1->uid > p2->uid)
5740 return 1;
5741 return 0;
5744 void
5745 finish_tm_clone_pairs (void)
5747 vec<tm_alias_pair> tm_alias_pairs = vNULL;
5749 if (tm_clone_hash == NULL)
5750 return;
5752 /* We need a determenistic order for the .tm_clone_table, otherwise
5753 we will get bootstrap comparison failures, so dump the hash table
5754 to a vector, sort it, and dump the vector. */
5756 /* Dump the hashtable to a vector. */
5757 htab_traverse_noresize (tm_clone_hash, dump_tm_clone_to_vec,
5758 (void *) &tm_alias_pairs);
5759 /* Sort it. */
5760 tm_alias_pairs.qsort (tm_alias_pair_cmp);
5762 /* Dump it. */
5763 dump_tm_clone_pairs (tm_alias_pairs);
5765 htab_delete (tm_clone_hash);
5766 tm_clone_hash = NULL;
5767 tm_alias_pairs.release ();
5771 /* Emit an assembler directive to set symbol for DECL visibility to
5772 the visibility type VIS, which must not be VISIBILITY_DEFAULT. */
5774 void
5775 default_assemble_visibility (tree decl ATTRIBUTE_UNUSED,
5776 int vis ATTRIBUTE_UNUSED)
5778 #ifdef HAVE_GAS_HIDDEN
5779 static const char * const visibility_types[] = {
5780 NULL, "protected", "hidden", "internal"
5783 const char *name, *type;
5785 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5786 type = visibility_types[vis];
5788 fprintf (asm_out_file, "\t.%s\t", type);
5789 assemble_name (asm_out_file, name);
5790 fprintf (asm_out_file, "\n");
5791 #else
5792 if (!DECL_ARTIFICIAL (decl))
5793 warning (OPT_Wattributes, "visibility attribute not supported "
5794 "in this configuration; ignored");
5795 #endif
5798 /* A helper function to call assemble_visibility when needed for a decl. */
5801 maybe_assemble_visibility (tree decl)
5803 enum symbol_visibility vis = DECL_VISIBILITY (decl);
5805 if (vis != VISIBILITY_DEFAULT)
5807 targetm.asm_out.assemble_visibility (decl, vis);
5808 return 1;
5810 else
5811 return 0;
5814 /* Returns 1 if the target configuration supports defining public symbols
5815 so that one of them will be chosen at link time instead of generating a
5816 multiply-defined symbol error, whether through the use of weak symbols or
5817 a target-specific mechanism for having duplicates discarded. */
5820 supports_one_only (void)
5822 if (SUPPORTS_ONE_ONLY)
5823 return 1;
5824 return TARGET_SUPPORTS_WEAK;
5827 /* Set up DECL as a public symbol that can be defined in multiple
5828 translation units without generating a linker error. */
5830 void
5831 make_decl_one_only (tree decl, tree comdat_group)
5833 gcc_assert (TREE_CODE (decl) == VAR_DECL
5834 || TREE_CODE (decl) == FUNCTION_DECL);
5836 TREE_PUBLIC (decl) = 1;
5838 if (SUPPORTS_ONE_ONLY)
5840 #ifdef MAKE_DECL_ONE_ONLY
5841 MAKE_DECL_ONE_ONLY (decl);
5842 #endif
5843 DECL_COMDAT_GROUP (decl) = comdat_group;
5845 else if (TREE_CODE (decl) == VAR_DECL
5846 && (DECL_INITIAL (decl) == 0 || DECL_INITIAL (decl) == error_mark_node))
5847 DECL_COMMON (decl) = 1;
5848 else
5850 gcc_assert (TARGET_SUPPORTS_WEAK);
5851 DECL_WEAK (decl) = 1;
5855 void
5856 init_varasm_once (void)
5858 section_htab = htab_create_ggc (31, section_entry_hash,
5859 section_entry_eq, NULL);
5860 object_block_htab = htab_create_ggc (31, object_block_entry_hash,
5861 object_block_entry_eq, NULL);
5862 const_desc_htab = htab_create_ggc (1009, const_desc_hash,
5863 const_desc_eq, NULL);
5865 const_alias_set = new_alias_set ();
5866 shared_constant_pool = create_constant_pool ();
5868 #ifdef TEXT_SECTION_ASM_OP
5869 text_section = get_unnamed_section (SECTION_CODE, output_section_asm_op,
5870 TEXT_SECTION_ASM_OP);
5871 #endif
5873 #ifdef DATA_SECTION_ASM_OP
5874 data_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
5875 DATA_SECTION_ASM_OP);
5876 #endif
5878 #ifdef SDATA_SECTION_ASM_OP
5879 sdata_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
5880 SDATA_SECTION_ASM_OP);
5881 #endif
5883 #ifdef READONLY_DATA_SECTION_ASM_OP
5884 readonly_data_section = get_unnamed_section (0, output_section_asm_op,
5885 READONLY_DATA_SECTION_ASM_OP);
5886 #endif
5888 #ifdef CTORS_SECTION_ASM_OP
5889 ctors_section = get_unnamed_section (0, output_section_asm_op,
5890 CTORS_SECTION_ASM_OP);
5891 #endif
5893 #ifdef DTORS_SECTION_ASM_OP
5894 dtors_section = get_unnamed_section (0, output_section_asm_op,
5895 DTORS_SECTION_ASM_OP);
5896 #endif
5898 #ifdef BSS_SECTION_ASM_OP
5899 bss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
5900 output_section_asm_op,
5901 BSS_SECTION_ASM_OP);
5902 #endif
5904 #ifdef SBSS_SECTION_ASM_OP
5905 sbss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
5906 output_section_asm_op,
5907 SBSS_SECTION_ASM_OP);
5908 #endif
5910 tls_comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
5911 | SECTION_COMMON, emit_tls_common);
5912 lcomm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
5913 | SECTION_COMMON, emit_local);
5914 comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
5915 | SECTION_COMMON, emit_common);
5917 #if defined ASM_OUTPUT_ALIGNED_BSS
5918 bss_noswitch_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS,
5919 emit_bss);
5920 #endif
5922 targetm.asm_out.init_sections ();
5924 if (readonly_data_section == NULL)
5925 readonly_data_section = text_section;
5927 #ifdef ASM_OUTPUT_EXTERNAL
5928 pending_assemble_externals_set = pointer_set_create ();
5929 #endif
5932 enum tls_model
5933 decl_default_tls_model (const_tree decl)
5935 enum tls_model kind;
5936 bool is_local;
5938 is_local = targetm.binds_local_p (decl);
5939 if (!flag_shlib)
5941 if (is_local)
5942 kind = TLS_MODEL_LOCAL_EXEC;
5943 else
5944 kind = TLS_MODEL_INITIAL_EXEC;
5947 /* Local dynamic is inefficient when we're not combining the
5948 parts of the address. */
5949 else if (optimize && is_local)
5950 kind = TLS_MODEL_LOCAL_DYNAMIC;
5951 else
5952 kind = TLS_MODEL_GLOBAL_DYNAMIC;
5953 if (kind < flag_tls_default)
5954 kind = flag_tls_default;
5956 return kind;
5959 /* Select a set of attributes for section NAME based on the properties
5960 of DECL and whether or not RELOC indicates that DECL's initializer
5961 might contain runtime relocations.
5963 We make the section read-only and executable for a function decl,
5964 read-only for a const data decl, and writable for a non-const data decl. */
5966 unsigned int
5967 default_section_type_flags (tree decl, const char *name, int reloc)
5969 unsigned int flags;
5971 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
5972 flags = SECTION_CODE;
5973 else if (decl)
5975 enum section_category category
5976 = categorize_decl_for_section (decl, reloc);
5977 if (decl_readonly_section_1 (category))
5978 flags = 0;
5979 else if (category == SECCAT_DATA_REL_RO
5980 || category == SECCAT_DATA_REL_RO_LOCAL)
5981 flags = SECTION_WRITE | SECTION_RELRO;
5982 else
5983 flags = SECTION_WRITE;
5985 else
5987 flags = SECTION_WRITE;
5988 if (strcmp (name, ".data.rel.ro") == 0
5989 || strcmp (name, ".data.rel.ro.local") == 0)
5990 flags |= SECTION_RELRO;
5993 if (decl && DECL_ONE_ONLY (decl))
5994 flags |= SECTION_LINKONCE;
5996 if (decl && TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
5997 flags |= SECTION_TLS | SECTION_WRITE;
5999 if (strcmp (name, ".bss") == 0
6000 || strncmp (name, ".bss.", 5) == 0
6001 || strncmp (name, ".gnu.linkonce.b.", 16) == 0
6002 || strcmp (name, ".sbss") == 0
6003 || strncmp (name, ".sbss.", 6) == 0
6004 || strncmp (name, ".gnu.linkonce.sb.", 17) == 0)
6005 flags |= SECTION_BSS;
6007 if (strcmp (name, ".tdata") == 0
6008 || strncmp (name, ".tdata.", 7) == 0
6009 || strncmp (name, ".gnu.linkonce.td.", 17) == 0)
6010 flags |= SECTION_TLS;
6012 if (strcmp (name, ".tbss") == 0
6013 || strncmp (name, ".tbss.", 6) == 0
6014 || strncmp (name, ".gnu.linkonce.tb.", 17) == 0)
6015 flags |= SECTION_TLS | SECTION_BSS;
6017 /* These three sections have special ELF types. They are neither
6018 SHT_PROGBITS nor SHT_NOBITS, so when changing sections we don't
6019 want to print a section type (@progbits or @nobits). If someone
6020 is silly enough to emit code or TLS variables to one of these
6021 sections, then don't handle them specially. */
6022 if (!(flags & (SECTION_CODE | SECTION_BSS | SECTION_TLS))
6023 && (strcmp (name, ".init_array") == 0
6024 || strcmp (name, ".fini_array") == 0
6025 || strcmp (name, ".preinit_array") == 0))
6026 flags |= SECTION_NOTYPE;
6028 return flags;
6031 /* Return true if the target supports some form of global BSS,
6032 either through bss_noswitch_section, or by selecting a BSS
6033 section in TARGET_ASM_SELECT_SECTION. */
6035 bool
6036 have_global_bss_p (void)
6038 return bss_noswitch_section || targetm.have_switchable_bss_sections;
6041 /* Output assembly to switch to section NAME with attribute FLAGS.
6042 Four variants for common object file formats. */
6044 void
6045 default_no_named_section (const char *name ATTRIBUTE_UNUSED,
6046 unsigned int flags ATTRIBUTE_UNUSED,
6047 tree decl ATTRIBUTE_UNUSED)
6049 /* Some object formats don't support named sections at all. The
6050 front-end should already have flagged this as an error. */
6051 gcc_unreachable ();
6054 #ifndef TLS_SECTION_ASM_FLAG
6055 #define TLS_SECTION_ASM_FLAG 'T'
6056 #endif
6058 void
6059 default_elf_asm_named_section (const char *name, unsigned int flags,
6060 tree decl ATTRIBUTE_UNUSED)
6062 char flagchars[10], *f = flagchars;
6064 /* If we have already declared this section, we can use an
6065 abbreviated form to switch back to it -- unless this section is
6066 part of a COMDAT groups, in which case GAS requires the full
6067 declaration every time. */
6068 if (!(HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6069 && (flags & SECTION_DECLARED))
6071 fprintf (asm_out_file, "\t.section\t%s\n", name);
6072 return;
6075 if (!(flags & SECTION_DEBUG))
6076 *f++ = 'a';
6077 if (flags & SECTION_EXCLUDE)
6078 *f++ = 'e';
6079 if (flags & SECTION_WRITE)
6080 *f++ = 'w';
6081 if (flags & SECTION_CODE)
6082 *f++ = 'x';
6083 if (flags & SECTION_SMALL)
6084 *f++ = 's';
6085 if (flags & SECTION_MERGE)
6086 *f++ = 'M';
6087 if (flags & SECTION_STRINGS)
6088 *f++ = 'S';
6089 if (flags & SECTION_TLS)
6090 *f++ = TLS_SECTION_ASM_FLAG;
6091 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6092 *f++ = 'G';
6093 *f = '\0';
6095 fprintf (asm_out_file, "\t.section\t%s,\"%s\"", name, flagchars);
6097 if (!(flags & SECTION_NOTYPE))
6099 const char *type;
6100 const char *format;
6102 if (flags & SECTION_BSS)
6103 type = "nobits";
6104 else
6105 type = "progbits";
6107 format = ",@%s";
6108 /* On platforms that use "@" as the assembly comment character,
6109 use "%" instead. */
6110 if (strcmp (ASM_COMMENT_START, "@") == 0)
6111 format = ",%%%s";
6112 fprintf (asm_out_file, format, type);
6114 if (flags & SECTION_ENTSIZE)
6115 fprintf (asm_out_file, ",%d", flags & SECTION_ENTSIZE);
6116 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6118 if (TREE_CODE (decl) == IDENTIFIER_NODE)
6119 fprintf (asm_out_file, ",%s,comdat", IDENTIFIER_POINTER (decl));
6120 else
6121 fprintf (asm_out_file, ",%s,comdat",
6122 IDENTIFIER_POINTER (DECL_COMDAT_GROUP (decl)));
6126 putc ('\n', asm_out_file);
6129 void
6130 default_coff_asm_named_section (const char *name, unsigned int flags,
6131 tree decl ATTRIBUTE_UNUSED)
6133 char flagchars[8], *f = flagchars;
6135 if (flags & SECTION_WRITE)
6136 *f++ = 'w';
6137 if (flags & SECTION_CODE)
6138 *f++ = 'x';
6139 *f = '\0';
6141 fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
6144 void
6145 default_pe_asm_named_section (const char *name, unsigned int flags,
6146 tree decl)
6148 default_coff_asm_named_section (name, flags, decl);
6150 if (flags & SECTION_LINKONCE)
6152 /* Functions may have been compiled at various levels of
6153 optimization so we can't use `same_size' here.
6154 Instead, have the linker pick one. */
6155 fprintf (asm_out_file, "\t.linkonce %s\n",
6156 (flags & SECTION_CODE ? "discard" : "same_size"));
6160 /* The lame default section selector. */
6162 section *
6163 default_select_section (tree decl, int reloc,
6164 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
6166 if (DECL_P (decl))
6168 if (decl_readonly_section (decl, reloc))
6169 return readonly_data_section;
6171 else if (TREE_CODE (decl) == CONSTRUCTOR)
6173 if (! ((flag_pic && reloc)
6174 || !TREE_READONLY (decl)
6175 || TREE_SIDE_EFFECTS (decl)
6176 || !TREE_CONSTANT (decl)))
6177 return readonly_data_section;
6179 else if (TREE_CODE (decl) == STRING_CST)
6180 return readonly_data_section;
6181 else if (! (flag_pic && reloc))
6182 return readonly_data_section;
6184 return data_section;
6187 enum section_category
6188 categorize_decl_for_section (const_tree decl, int reloc)
6190 enum section_category ret;
6192 if (TREE_CODE (decl) == FUNCTION_DECL)
6193 return SECCAT_TEXT;
6194 else if (TREE_CODE (decl) == STRING_CST)
6196 if (flag_mudflap
6197 || (flag_asan && asan_protect_global (CONST_CAST_TREE (decl))))
6198 /* or !flag_merge_constants */
6199 return SECCAT_RODATA;
6200 else
6201 return SECCAT_RODATA_MERGE_STR;
6203 else if (TREE_CODE (decl) == VAR_DECL)
6205 if (bss_initializer_p (decl))
6206 ret = SECCAT_BSS;
6207 else if (! TREE_READONLY (decl)
6208 || TREE_SIDE_EFFECTS (decl)
6209 || ! TREE_CONSTANT (DECL_INITIAL (decl)))
6211 /* Here the reloc_rw_mask is not testing whether the section should
6212 be read-only or not, but whether the dynamic link will have to
6213 do something. If so, we wish to segregate the data in order to
6214 minimize cache misses inside the dynamic linker. */
6215 if (reloc & targetm.asm_out.reloc_rw_mask ())
6216 ret = reloc == 1 ? SECCAT_DATA_REL_LOCAL : SECCAT_DATA_REL;
6217 else
6218 ret = SECCAT_DATA;
6220 else if (reloc & targetm.asm_out.reloc_rw_mask ())
6221 ret = reloc == 1 ? SECCAT_DATA_REL_RO_LOCAL : SECCAT_DATA_REL_RO;
6222 else if (reloc || flag_merge_constants < 2 || flag_mudflap
6223 || (flag_asan && asan_protect_global (CONST_CAST_TREE (decl))))
6224 /* C and C++ don't allow different variables to share the same
6225 location. -fmerge-all-constants allows even that (at the
6226 expense of not conforming). */
6227 ret = SECCAT_RODATA;
6228 else if (TREE_CODE (DECL_INITIAL (decl)) == STRING_CST)
6229 ret = SECCAT_RODATA_MERGE_STR_INIT;
6230 else
6231 ret = SECCAT_RODATA_MERGE_CONST;
6233 else if (TREE_CODE (decl) == CONSTRUCTOR)
6235 if ((reloc & targetm.asm_out.reloc_rw_mask ())
6236 || TREE_SIDE_EFFECTS (decl)
6237 || ! TREE_CONSTANT (decl))
6238 ret = SECCAT_DATA;
6239 else
6240 ret = SECCAT_RODATA;
6242 else
6243 ret = SECCAT_RODATA;
6245 /* There are no read-only thread-local sections. */
6246 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
6248 /* Note that this would be *just* SECCAT_BSS, except that there's
6249 no concept of a read-only thread-local-data section. */
6250 if (ret == SECCAT_BSS
6251 || (flag_zero_initialized_in_bss
6252 && initializer_zerop (DECL_INITIAL (decl))))
6253 ret = SECCAT_TBSS;
6254 else
6255 ret = SECCAT_TDATA;
6258 /* If the target uses small data sections, select it. */
6259 else if (targetm.in_small_data_p (decl))
6261 if (ret == SECCAT_BSS)
6262 ret = SECCAT_SBSS;
6263 else if (targetm.have_srodata_section && ret == SECCAT_RODATA)
6264 ret = SECCAT_SRODATA;
6265 else
6266 ret = SECCAT_SDATA;
6269 return ret;
6272 static bool
6273 decl_readonly_section_1 (enum section_category category)
6275 switch (category)
6277 case SECCAT_RODATA:
6278 case SECCAT_RODATA_MERGE_STR:
6279 case SECCAT_RODATA_MERGE_STR_INIT:
6280 case SECCAT_RODATA_MERGE_CONST:
6281 case SECCAT_SRODATA:
6282 return true;
6283 default:
6284 return false;
6288 bool
6289 decl_readonly_section (const_tree decl, int reloc)
6291 return decl_readonly_section_1 (categorize_decl_for_section (decl, reloc));
6294 /* Select a section based on the above categorization. */
6296 section *
6297 default_elf_select_section (tree decl, int reloc,
6298 unsigned HOST_WIDE_INT align)
6300 const char *sname;
6301 switch (categorize_decl_for_section (decl, reloc))
6303 case SECCAT_TEXT:
6304 /* We're not supposed to be called on FUNCTION_DECLs. */
6305 gcc_unreachable ();
6306 case SECCAT_RODATA:
6307 return readonly_data_section;
6308 case SECCAT_RODATA_MERGE_STR:
6309 return mergeable_string_section (decl, align, 0);
6310 case SECCAT_RODATA_MERGE_STR_INIT:
6311 return mergeable_string_section (DECL_INITIAL (decl), align, 0);
6312 case SECCAT_RODATA_MERGE_CONST:
6313 return mergeable_constant_section (DECL_MODE (decl), align, 0);
6314 case SECCAT_SRODATA:
6315 sname = ".sdata2";
6316 break;
6317 case SECCAT_DATA:
6318 return data_section;
6319 case SECCAT_DATA_REL:
6320 sname = ".data.rel";
6321 break;
6322 case SECCAT_DATA_REL_LOCAL:
6323 sname = ".data.rel.local";
6324 break;
6325 case SECCAT_DATA_REL_RO:
6326 sname = ".data.rel.ro";
6327 break;
6328 case SECCAT_DATA_REL_RO_LOCAL:
6329 sname = ".data.rel.ro.local";
6330 break;
6331 case SECCAT_SDATA:
6332 sname = ".sdata";
6333 break;
6334 case SECCAT_TDATA:
6335 sname = ".tdata";
6336 break;
6337 case SECCAT_BSS:
6338 if (bss_section)
6339 return bss_section;
6340 sname = ".bss";
6341 break;
6342 case SECCAT_SBSS:
6343 sname = ".sbss";
6344 break;
6345 case SECCAT_TBSS:
6346 sname = ".tbss";
6347 break;
6348 default:
6349 gcc_unreachable ();
6352 if (!DECL_P (decl))
6353 decl = NULL_TREE;
6354 return get_named_section (decl, sname, reloc);
6357 /* Construct a unique section name based on the decl name and the
6358 categorization performed above. */
6360 void
6361 default_unique_section (tree decl, int reloc)
6363 /* We only need to use .gnu.linkonce if we don't have COMDAT groups. */
6364 bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
6365 const char *prefix, *name, *linkonce;
6366 char *string;
6368 switch (categorize_decl_for_section (decl, reloc))
6370 case SECCAT_TEXT:
6371 prefix = one_only ? ".t" : ".text";
6372 break;
6373 case SECCAT_RODATA:
6374 case SECCAT_RODATA_MERGE_STR:
6375 case SECCAT_RODATA_MERGE_STR_INIT:
6376 case SECCAT_RODATA_MERGE_CONST:
6377 prefix = one_only ? ".r" : ".rodata";
6378 break;
6379 case SECCAT_SRODATA:
6380 prefix = one_only ? ".s2" : ".sdata2";
6381 break;
6382 case SECCAT_DATA:
6383 prefix = one_only ? ".d" : ".data";
6384 break;
6385 case SECCAT_DATA_REL:
6386 prefix = one_only ? ".d.rel" : ".data.rel";
6387 break;
6388 case SECCAT_DATA_REL_LOCAL:
6389 prefix = one_only ? ".d.rel.local" : ".data.rel.local";
6390 break;
6391 case SECCAT_DATA_REL_RO:
6392 prefix = one_only ? ".d.rel.ro" : ".data.rel.ro";
6393 break;
6394 case SECCAT_DATA_REL_RO_LOCAL:
6395 prefix = one_only ? ".d.rel.ro.local" : ".data.rel.ro.local";
6396 break;
6397 case SECCAT_SDATA:
6398 prefix = one_only ? ".s" : ".sdata";
6399 break;
6400 case SECCAT_BSS:
6401 prefix = one_only ? ".b" : ".bss";
6402 break;
6403 case SECCAT_SBSS:
6404 prefix = one_only ? ".sb" : ".sbss";
6405 break;
6406 case SECCAT_TDATA:
6407 prefix = one_only ? ".td" : ".tdata";
6408 break;
6409 case SECCAT_TBSS:
6410 prefix = one_only ? ".tb" : ".tbss";
6411 break;
6412 default:
6413 gcc_unreachable ();
6416 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
6417 name = targetm.strip_name_encoding (name);
6419 /* If we're using one_only, then there needs to be a .gnu.linkonce
6420 prefix to the section name. */
6421 linkonce = one_only ? ".gnu.linkonce" : "";
6423 string = ACONCAT ((linkonce, prefix, ".", name, NULL));
6425 DECL_SECTION_NAME (decl) = build_string (strlen (string), string);
6428 /* Like compute_reloc_for_constant, except for an RTX. The return value
6429 is a mask for which bit 1 indicates a global relocation, and bit 0
6430 indicates a local relocation. */
6432 static int
6433 compute_reloc_for_rtx_1 (rtx *xp, void *data)
6435 int *preloc = (int *) data;
6436 rtx x = *xp;
6438 switch (GET_CODE (x))
6440 case SYMBOL_REF:
6441 *preloc |= SYMBOL_REF_LOCAL_P (x) ? 1 : 2;
6442 break;
6443 case LABEL_REF:
6444 *preloc |= 1;
6445 break;
6446 default:
6447 break;
6450 return 0;
6453 static int
6454 compute_reloc_for_rtx (rtx x)
6456 int reloc;
6458 switch (GET_CODE (x))
6460 case CONST:
6461 case SYMBOL_REF:
6462 case LABEL_REF:
6463 reloc = 0;
6464 for_each_rtx (&x, compute_reloc_for_rtx_1, &reloc);
6465 return reloc;
6467 default:
6468 return 0;
6472 section *
6473 default_select_rtx_section (enum machine_mode mode ATTRIBUTE_UNUSED,
6474 rtx x,
6475 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
6477 if (compute_reloc_for_rtx (x) & targetm.asm_out.reloc_rw_mask ())
6478 return data_section;
6479 else
6480 return readonly_data_section;
6483 section *
6484 default_elf_select_rtx_section (enum machine_mode mode, rtx x,
6485 unsigned HOST_WIDE_INT align)
6487 int reloc = compute_reloc_for_rtx (x);
6489 /* ??? Handle small data here somehow. */
6491 if (reloc & targetm.asm_out.reloc_rw_mask ())
6493 if (reloc == 1)
6494 return get_named_section (NULL, ".data.rel.ro.local", 1);
6495 else
6496 return get_named_section (NULL, ".data.rel.ro", 3);
6499 return mergeable_constant_section (mode, align, 0);
6502 /* Set the generally applicable flags on the SYMBOL_REF for EXP. */
6504 void
6505 default_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
6507 rtx symbol;
6508 int flags;
6510 /* Careful not to prod global register variables. */
6511 if (!MEM_P (rtl))
6512 return;
6513 symbol = XEXP (rtl, 0);
6514 if (GET_CODE (symbol) != SYMBOL_REF)
6515 return;
6517 flags = SYMBOL_REF_FLAGS (symbol) & SYMBOL_FLAG_HAS_BLOCK_INFO;
6518 if (TREE_CODE (decl) == FUNCTION_DECL)
6519 flags |= SYMBOL_FLAG_FUNCTION;
6520 if (targetm.binds_local_p (decl))
6521 flags |= SYMBOL_FLAG_LOCAL;
6522 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
6523 flags |= DECL_TLS_MODEL (decl) << SYMBOL_FLAG_TLS_SHIFT;
6524 else if (targetm.in_small_data_p (decl))
6525 flags |= SYMBOL_FLAG_SMALL;
6526 /* ??? Why is DECL_EXTERNAL ever set for non-PUBLIC names? Without
6527 being PUBLIC, the thing *must* be defined in this translation unit.
6528 Prevent this buglet from being propagated into rtl code as well. */
6529 if (DECL_P (decl) && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl))
6530 flags |= SYMBOL_FLAG_EXTERNAL;
6532 SYMBOL_REF_FLAGS (symbol) = flags;
6535 /* By default, we do nothing for encode_section_info, so we need not
6536 do anything but discard the '*' marker. */
6538 const char *
6539 default_strip_name_encoding (const char *str)
6541 return str + (*str == '*');
6544 #ifdef ASM_OUTPUT_DEF
6545 /* The default implementation of TARGET_ASM_OUTPUT_ANCHOR. Define the
6546 anchor relative to ".", the current section position. */
6548 void
6549 default_asm_output_anchor (rtx symbol)
6551 char buffer[100];
6553 sprintf (buffer, "*. + " HOST_WIDE_INT_PRINT_DEC,
6554 SYMBOL_REF_BLOCK_OFFSET (symbol));
6555 ASM_OUTPUT_DEF (asm_out_file, XSTR (symbol, 0), buffer);
6557 #endif
6559 /* The default implementation of TARGET_USE_ANCHORS_FOR_SYMBOL_P. */
6561 bool
6562 default_use_anchors_for_symbol_p (const_rtx symbol)
6564 section *sect;
6565 tree decl;
6567 /* Don't use anchors for mergeable sections. The linker might move
6568 the objects around. */
6569 sect = SYMBOL_REF_BLOCK (symbol)->sect;
6570 if (sect->common.flags & SECTION_MERGE)
6571 return false;
6573 /* Don't use anchors for small data sections. The small data register
6574 acts as an anchor for such sections. */
6575 if (sect->common.flags & SECTION_SMALL)
6576 return false;
6578 decl = SYMBOL_REF_DECL (symbol);
6579 if (decl && DECL_P (decl))
6581 /* Don't use section anchors for decls that might be defined by
6582 other modules. */
6583 if (!targetm.binds_local_p (decl))
6584 return false;
6586 /* Don't use section anchors for decls that will be placed in a
6587 small data section. */
6588 /* ??? Ideally, this check would be redundant with the SECTION_SMALL
6589 one above. The problem is that we only use SECTION_SMALL for
6590 sections that should be marked as small in the section directive. */
6591 if (targetm.in_small_data_p (decl))
6592 return false;
6594 return true;
6597 /* Return true when RESOLUTION indicate that symbol will be bound to the
6598 definition provided by current .o file. */
6600 static bool
6601 resolution_to_local_definition_p (enum ld_plugin_symbol_resolution resolution)
6603 return (resolution == LDPR_PREVAILING_DEF
6604 || resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
6605 || resolution == LDPR_PREVAILING_DEF_IRONLY);
6608 /* Return true when RESOLUTION indicate that symbol will be bound locally
6609 within current executable or DSO. */
6611 static bool
6612 resolution_local_p (enum ld_plugin_symbol_resolution resolution)
6614 return (resolution == LDPR_PREVAILING_DEF
6615 || resolution == LDPR_PREVAILING_DEF_IRONLY
6616 || resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
6617 || resolution == LDPR_PREEMPTED_REG
6618 || resolution == LDPR_PREEMPTED_IR
6619 || resolution == LDPR_RESOLVED_IR
6620 || resolution == LDPR_RESOLVED_EXEC);
6623 /* Assume ELF-ish defaults, since that's pretty much the most liberal
6624 wrt cross-module name binding. */
6626 bool
6627 default_binds_local_p (const_tree exp)
6629 return default_binds_local_p_1 (exp, flag_shlib);
6632 bool
6633 default_binds_local_p_1 (const_tree exp, int shlib)
6635 bool local_p;
6636 bool resolved_locally = false;
6637 bool resolved_to_local_def = false;
6639 /* With resolution file in hands, take look into resolutions.
6640 We can't just return true for resolved_locally symbols,
6641 because dynamic linking might overwrite symbols
6642 in shared libraries. */
6643 if (TREE_CODE (exp) == VAR_DECL && TREE_PUBLIC (exp)
6644 && (TREE_STATIC (exp) || DECL_EXTERNAL (exp)))
6646 struct varpool_node *vnode = varpool_get_node (exp);
6647 if (vnode && resolution_local_p (vnode->symbol.resolution))
6648 resolved_locally = true;
6649 if (vnode
6650 && resolution_to_local_definition_p (vnode->symbol.resolution))
6651 resolved_to_local_def = true;
6653 else if (TREE_CODE (exp) == FUNCTION_DECL && TREE_PUBLIC (exp))
6655 struct cgraph_node *node = cgraph_get_node (exp);
6656 if (node
6657 && resolution_local_p (node->symbol.resolution))
6658 resolved_locally = true;
6659 if (node
6660 && resolution_to_local_definition_p (node->symbol.resolution))
6661 resolved_to_local_def = true;
6664 /* A non-decl is an entry in the constant pool. */
6665 if (!DECL_P (exp))
6666 local_p = true;
6667 /* Weakrefs may not bind locally, even though the weakref itself is always
6668 static and therefore local. Similarly, the resolver for ifunc functions
6669 might resolve to a non-local function.
6670 FIXME: We can resolve the weakref case more curefuly by looking at the
6671 weakref alias. */
6672 else if (lookup_attribute ("weakref", DECL_ATTRIBUTES (exp))
6673 || (TREE_CODE (exp) == FUNCTION_DECL
6674 && lookup_attribute ("ifunc", DECL_ATTRIBUTES (exp))))
6675 local_p = false;
6676 /* Static variables are always local. */
6677 else if (! TREE_PUBLIC (exp))
6678 local_p = true;
6679 /* A variable is local if the user has said explicitly that it will
6680 be. */
6681 else if ((DECL_VISIBILITY_SPECIFIED (exp)
6682 || resolved_to_local_def)
6683 && DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
6684 local_p = true;
6685 /* Variables defined outside this object might not be local. */
6686 else if (DECL_EXTERNAL (exp) && !resolved_locally)
6687 local_p = false;
6688 /* If defined in this object and visibility is not default, must be
6689 local. */
6690 else if (DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
6691 local_p = true;
6692 /* Default visibility weak data can be overridden by a strong symbol
6693 in another module and so are not local. */
6694 else if (DECL_WEAK (exp)
6695 && !resolved_locally)
6696 local_p = false;
6697 /* If PIC, then assume that any global name can be overridden by
6698 symbols resolved from other modules. */
6699 else if (shlib)
6700 local_p = false;
6701 /* Uninitialized COMMON variable may be unified with symbols
6702 resolved from other modules. */
6703 else if (DECL_COMMON (exp)
6704 && !resolved_locally
6705 && (DECL_INITIAL (exp) == NULL
6706 || DECL_INITIAL (exp) == error_mark_node))
6707 local_p = false;
6708 /* Otherwise we're left with initialized (or non-common) global data
6709 which is of necessity defined locally. */
6710 else
6711 local_p = true;
6713 return local_p;
6716 /* Return true when references to DECL must bind to current definition in
6717 final executable.
6719 The condition is usually equivalent to whether the function binds to the
6720 current module (shared library or executable), that is to binds_local_p.
6721 We use this fact to avoid need for another target hook and implement
6722 the logic using binds_local_p and just special cases where
6723 decl_binds_to_current_def_p is stronger than binds_local_p. In particular
6724 the weak definitions (that can be overwritten at linktime by other
6725 definition from different object file) and when resolution info is available
6726 we simply use the knowledge passed to us by linker plugin. */
6727 bool
6728 decl_binds_to_current_def_p (tree decl)
6730 gcc_assert (DECL_P (decl));
6731 if (!TREE_PUBLIC (decl))
6732 return true;
6733 if (!targetm.binds_local_p (decl))
6734 return false;
6735 /* When resolution is available, just use it. */
6736 if (TREE_CODE (decl) == VAR_DECL
6737 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
6739 struct varpool_node *vnode = varpool_get_node (decl);
6740 if (vnode
6741 && vnode->symbol.resolution != LDPR_UNKNOWN)
6742 return resolution_to_local_definition_p (vnode->symbol.resolution);
6744 else if (TREE_CODE (decl) == FUNCTION_DECL)
6746 struct cgraph_node *node = cgraph_get_node (decl);
6747 if (node
6748 && node->symbol.resolution != LDPR_UNKNOWN)
6749 return resolution_to_local_definition_p (node->symbol.resolution);
6751 /* Otherwise we have to assume the worst for DECL_WEAK (hidden weaks
6752 binds locally but still can be overwritten).
6753 This rely on fact that binds_local_p behave as decl_replaceable_p
6754 for all other declaration types. */
6755 return !DECL_WEAK (decl);
6758 /* A replaceable function or variable is one which may be replaced
6759 at link-time with an entirely different definition, provided that the
6760 replacement has the same type. For example, functions declared
6761 with __attribute__((weak)) on most systems are replaceable.
6763 COMDAT functions are not replaceable, since all definitions of the
6764 function must be equivalent. It is important that COMDAT functions
6765 not be treated as replaceable so that use of C++ template
6766 instantiations is not penalized. */
6768 bool
6769 decl_replaceable_p (tree decl)
6771 gcc_assert (DECL_P (decl));
6772 if (!TREE_PUBLIC (decl) || DECL_COMDAT (decl))
6773 return false;
6774 return !decl_binds_to_current_def_p (decl);
6777 /* Default function to output code that will globalize a label. A
6778 target must define GLOBAL_ASM_OP or provide its own function to
6779 globalize a label. */
6780 #ifdef GLOBAL_ASM_OP
6781 void
6782 default_globalize_label (FILE * stream, const char *name)
6784 fputs (GLOBAL_ASM_OP, stream);
6785 assemble_name (stream, name);
6786 putc ('\n', stream);
6788 #endif /* GLOBAL_ASM_OP */
6790 /* Default function to output code that will globalize a declaration. */
6791 void
6792 default_globalize_decl_name (FILE * stream, tree decl)
6794 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
6795 targetm.asm_out.globalize_label (stream, name);
6798 /* Default function to output a label for unwind information. The
6799 default is to do nothing. A target that needs nonlocal labels for
6800 unwind information must provide its own function to do this. */
6801 void
6802 default_emit_unwind_label (FILE * stream ATTRIBUTE_UNUSED,
6803 tree decl ATTRIBUTE_UNUSED,
6804 int for_eh ATTRIBUTE_UNUSED,
6805 int empty ATTRIBUTE_UNUSED)
6809 /* Default function to output a label to divide up the exception table.
6810 The default is to do nothing. A target that needs/wants to divide
6811 up the table must provide it's own function to do this. */
6812 void
6813 default_emit_except_table_label (FILE * stream ATTRIBUTE_UNUSED)
6817 /* This is how to output an internal numbered label where PREFIX is
6818 the class of label and LABELNO is the number within the class. */
6820 void
6821 default_generate_internal_label (char *buf, const char *prefix,
6822 unsigned long labelno)
6824 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
6827 /* This is how to output an internal numbered label where PREFIX is
6828 the class of label and LABELNO is the number within the class. */
6830 void
6831 default_internal_label (FILE *stream, const char *prefix,
6832 unsigned long labelno)
6834 char *const buf = (char *) alloca (40 + strlen (prefix));
6835 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
6836 ASM_OUTPUT_INTERNAL_LABEL (stream, buf);
6840 /* The default implementation of ASM_DECLARE_CONSTANT_NAME. */
6842 void
6843 default_asm_declare_constant_name (FILE *file, const char *name,
6844 const_tree exp ATTRIBUTE_UNUSED,
6845 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
6847 assemble_label (file, name);
6850 /* This is the default behavior at the beginning of a file. It's
6851 controlled by two other target-hook toggles. */
6852 void
6853 default_file_start (void)
6855 if (targetm.asm_file_start_app_off
6856 && !(flag_verbose_asm || flag_debug_asm || flag_dump_rtl_in_asm))
6857 fputs (ASM_APP_OFF, asm_out_file);
6859 if (targetm.asm_file_start_file_directive)
6860 output_file_directive (asm_out_file, main_input_filename);
6863 /* This is a generic routine suitable for use as TARGET_ASM_FILE_END
6864 which emits a special section directive used to indicate whether or
6865 not this object file needs an executable stack. This is primarily
6866 a GNU extension to ELF but could be used on other targets. */
6868 int trampolines_created;
6870 void
6871 file_end_indicate_exec_stack (void)
6873 unsigned int flags = SECTION_DEBUG;
6874 if (trampolines_created)
6875 flags |= SECTION_CODE;
6877 switch_to_section (get_section (".note.GNU-stack", flags, NULL));
6880 /* Emit a special section directive to indicate that this object file
6881 was compiled with -fsplit-stack. This is used to let the linker
6882 detect calls between split-stack code and non-split-stack code, so
6883 that it can modify the split-stack code to allocate a sufficiently
6884 large stack. We emit another special section if there are any
6885 functions in this file which have the no_split_stack attribute, to
6886 prevent the linker from warning about being unable to convert the
6887 functions if they call non-split-stack code. */
6889 void
6890 file_end_indicate_split_stack (void)
6892 if (flag_split_stack)
6894 switch_to_section (get_section (".note.GNU-split-stack", SECTION_DEBUG,
6895 NULL));
6896 if (saw_no_split_stack)
6897 switch_to_section (get_section (".note.GNU-no-split-stack",
6898 SECTION_DEBUG, NULL));
6902 /* Output DIRECTIVE (a C string) followed by a newline. This is used as
6903 a get_unnamed_section callback. */
6905 void
6906 output_section_asm_op (const void *directive)
6908 fprintf (asm_out_file, "%s\n", (const char *) directive);
6911 /* Emit assembly code to switch to section NEW_SECTION. Do nothing if
6912 the current section is NEW_SECTION. */
6914 void
6915 switch_to_section (section *new_section)
6917 if (in_section == new_section)
6918 return;
6920 if (new_section->common.flags & SECTION_FORGET)
6921 in_section = NULL;
6922 else
6923 in_section = new_section;
6925 switch (SECTION_STYLE (new_section))
6927 case SECTION_NAMED:
6928 targetm.asm_out.named_section (new_section->named.name,
6929 new_section->named.common.flags,
6930 new_section->named.decl);
6931 break;
6933 case SECTION_UNNAMED:
6934 new_section->unnamed.callback (new_section->unnamed.data);
6935 break;
6937 case SECTION_NOSWITCH:
6938 gcc_unreachable ();
6939 break;
6942 new_section->common.flags |= SECTION_DECLARED;
6945 /* If block symbol SYMBOL has not yet been assigned an offset, place
6946 it at the end of its block. */
6948 void
6949 place_block_symbol (rtx symbol)
6951 unsigned HOST_WIDE_INT size, mask, offset;
6952 struct constant_descriptor_rtx *desc;
6953 unsigned int alignment;
6954 struct object_block *block;
6955 tree decl;
6957 gcc_assert (SYMBOL_REF_BLOCK (symbol));
6958 if (SYMBOL_REF_BLOCK_OFFSET (symbol) >= 0)
6959 return;
6961 /* Work out the symbol's size and alignment. */
6962 if (CONSTANT_POOL_ADDRESS_P (symbol))
6964 desc = SYMBOL_REF_CONSTANT (symbol);
6965 alignment = desc->align;
6966 size = GET_MODE_SIZE (desc->mode);
6968 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
6970 decl = SYMBOL_REF_DECL (symbol);
6971 alignment = DECL_ALIGN (decl);
6972 size = get_constant_size (DECL_INITIAL (decl));
6973 if (flag_asan
6974 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
6975 && asan_protect_global (DECL_INITIAL (decl)))
6976 size += asan_red_zone_size (size);
6978 else
6980 decl = SYMBOL_REF_DECL (symbol);
6981 alignment = DECL_ALIGN (decl);
6982 size = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
6983 if (flag_asan && asan_protect_global (decl))
6985 size += asan_red_zone_size (size);
6986 alignment = MAX (alignment,
6987 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
6991 /* Calculate the object's offset from the start of the block. */
6992 block = SYMBOL_REF_BLOCK (symbol);
6993 mask = alignment / BITS_PER_UNIT - 1;
6994 offset = (block->size + mask) & ~mask;
6995 SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
6997 /* Record the block's new alignment and size. */
6998 block->alignment = MAX (block->alignment, alignment);
6999 block->size = offset + size;
7001 vec_safe_push (block->objects, symbol);
7004 /* Return the anchor that should be used to address byte offset OFFSET
7005 from the first object in BLOCK. MODEL is the TLS model used
7006 to access it. */
7009 get_section_anchor (struct object_block *block, HOST_WIDE_INT offset,
7010 enum tls_model model)
7012 char label[100];
7013 unsigned int begin, middle, end;
7014 unsigned HOST_WIDE_INT min_offset, max_offset, range, bias, delta;
7015 rtx anchor;
7017 /* Work out the anchor's offset. Use an offset of 0 for the first
7018 anchor so that we don't pessimize the case where we take the address
7019 of a variable at the beginning of the block. This is particularly
7020 useful when a block has only one variable assigned to it.
7022 We try to place anchors RANGE bytes apart, so there can then be
7023 anchors at +/-RANGE, +/-2 * RANGE, and so on, up to the limits of
7024 a ptr_mode offset. With some target settings, the lowest such
7025 anchor might be out of range for the lowest ptr_mode offset;
7026 likewise the highest anchor for the highest offset. Use anchors
7027 at the extreme ends of the ptr_mode range in such cases.
7029 All arithmetic uses unsigned integers in order to avoid
7030 signed overflow. */
7031 max_offset = (unsigned HOST_WIDE_INT) targetm.max_anchor_offset;
7032 min_offset = (unsigned HOST_WIDE_INT) targetm.min_anchor_offset;
7033 range = max_offset - min_offset + 1;
7034 if (range == 0)
7035 offset = 0;
7036 else
7038 bias = 1 << (GET_MODE_BITSIZE (ptr_mode) - 1);
7039 if (offset < 0)
7041 delta = -(unsigned HOST_WIDE_INT) offset + max_offset;
7042 delta -= delta % range;
7043 if (delta > bias)
7044 delta = bias;
7045 offset = (HOST_WIDE_INT) (-delta);
7047 else
7049 delta = (unsigned HOST_WIDE_INT) offset - min_offset;
7050 delta -= delta % range;
7051 if (delta > bias - 1)
7052 delta = bias - 1;
7053 offset = (HOST_WIDE_INT) delta;
7057 /* Do a binary search to see if there's already an anchor we can use.
7058 Set BEGIN to the new anchor's index if not. */
7059 begin = 0;
7060 end = vec_safe_length (block->anchors);
7061 while (begin != end)
7063 middle = (end + begin) / 2;
7064 anchor = (*block->anchors)[middle];
7065 if (SYMBOL_REF_BLOCK_OFFSET (anchor) > offset)
7066 end = middle;
7067 else if (SYMBOL_REF_BLOCK_OFFSET (anchor) < offset)
7068 begin = middle + 1;
7069 else if (SYMBOL_REF_TLS_MODEL (anchor) > model)
7070 end = middle;
7071 else if (SYMBOL_REF_TLS_MODEL (anchor) < model)
7072 begin = middle + 1;
7073 else
7074 return anchor;
7077 /* Create a new anchor with a unique label. */
7078 ASM_GENERATE_INTERNAL_LABEL (label, "LANCHOR", anchor_labelno++);
7079 anchor = create_block_symbol (ggc_strdup (label), block, offset);
7080 SYMBOL_REF_FLAGS (anchor) |= SYMBOL_FLAG_LOCAL | SYMBOL_FLAG_ANCHOR;
7081 SYMBOL_REF_FLAGS (anchor) |= model << SYMBOL_FLAG_TLS_SHIFT;
7083 /* Insert it at index BEGIN. */
7084 vec_safe_insert (block->anchors, begin, anchor);
7085 return anchor;
7088 /* Output the objects in BLOCK. */
7090 static void
7091 output_object_block (struct object_block *block)
7093 struct constant_descriptor_rtx *desc;
7094 unsigned int i;
7095 HOST_WIDE_INT offset;
7096 tree decl;
7097 rtx symbol;
7099 if (!block->objects)
7100 return;
7102 /* Switch to the section and make sure that the first byte is
7103 suitably aligned. */
7104 switch_to_section (block->sect);
7105 assemble_align (block->alignment);
7107 /* Define the values of all anchors relative to the current section
7108 position. */
7109 FOR_EACH_VEC_SAFE_ELT (block->anchors, i, symbol)
7110 targetm.asm_out.output_anchor (symbol);
7112 /* Output the objects themselves. */
7113 offset = 0;
7114 FOR_EACH_VEC_ELT (*block->objects, i, symbol)
7116 /* Move to the object's offset, padding with zeros if necessary. */
7117 assemble_zeros (SYMBOL_REF_BLOCK_OFFSET (symbol) - offset);
7118 offset = SYMBOL_REF_BLOCK_OFFSET (symbol);
7119 if (CONSTANT_POOL_ADDRESS_P (symbol))
7121 desc = SYMBOL_REF_CONSTANT (symbol);
7122 output_constant_pool_1 (desc, 1);
7123 offset += GET_MODE_SIZE (desc->mode);
7125 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
7127 HOST_WIDE_INT size;
7128 decl = SYMBOL_REF_DECL (symbol);
7129 assemble_constant_contents (DECL_INITIAL (decl), XSTR (symbol, 0),
7130 DECL_ALIGN (decl));
7131 size = get_constant_size (DECL_INITIAL (decl));
7132 offset += size;
7133 if (flag_asan
7134 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
7135 && asan_protect_global (DECL_INITIAL (decl)))
7137 size = asan_red_zone_size (size);
7138 assemble_zeros (size);
7139 offset += size;
7142 else
7144 HOST_WIDE_INT size;
7145 decl = SYMBOL_REF_DECL (symbol);
7146 assemble_variable_contents (decl, XSTR (symbol, 0), false);
7147 size = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
7148 offset += size;
7149 if (flag_asan && asan_protect_global (decl))
7151 size = asan_red_zone_size (size);
7152 assemble_zeros (size);
7153 offset += size;
7159 /* A htab_traverse callback used to call output_object_block for
7160 each member of object_block_htab. */
7162 static int
7163 output_object_block_htab (void **slot, void *data ATTRIBUTE_UNUSED)
7165 output_object_block ((struct object_block *) (*slot));
7166 return 1;
7169 /* Output the definitions of all object_blocks. */
7171 void
7172 output_object_blocks (void)
7174 htab_traverse (object_block_htab, output_object_block_htab, NULL);
7177 /* This function provides a possible implementation of the
7178 TARGET_ASM_RECORD_GCC_SWITCHES target hook for ELF targets. When triggered
7179 by -frecord-gcc-switches it creates a new mergeable, string section in the
7180 assembler output file called TARGET_ASM_RECORD_GCC_SWITCHES_SECTION which
7181 contains the switches in ASCII format.
7183 FIXME: This code does not correctly handle double quote characters
7184 that appear inside strings, (it strips them rather than preserving them).
7185 FIXME: ASM_OUTPUT_ASCII, as defined in config/elfos.h will not emit NUL
7186 characters - instead it treats them as sub-string separators. Since
7187 we want to emit NUL strings terminators into the object file we have to use
7188 ASM_OUTPUT_SKIP. */
7191 elf_record_gcc_switches (print_switch_type type, const char * name)
7193 switch (type)
7195 case SWITCH_TYPE_PASSED:
7196 ASM_OUTPUT_ASCII (asm_out_file, name, strlen (name));
7197 ASM_OUTPUT_SKIP (asm_out_file, (unsigned HOST_WIDE_INT) 1);
7198 break;
7200 case SWITCH_TYPE_DESCRIPTIVE:
7201 if (name == NULL)
7203 /* Distinguish between invocations where name is NULL. */
7204 static bool started = false;
7206 if (!started)
7208 section * sec;
7210 sec = get_section (targetm.asm_out.record_gcc_switches_section,
7211 SECTION_DEBUG
7212 | SECTION_MERGE
7213 | SECTION_STRINGS
7214 | (SECTION_ENTSIZE & 1),
7215 NULL);
7216 switch_to_section (sec);
7217 started = true;
7221 default:
7222 break;
7225 /* The return value is currently ignored by the caller, but must be 0.
7226 For -fverbose-asm the return value would be the number of characters
7227 emitted into the assembler file. */
7228 return 0;
7231 /* Emit text to declare externally defined symbols. It is needed to
7232 properly support non-default visibility. */
7233 void
7234 default_elf_asm_output_external (FILE *file ATTRIBUTE_UNUSED,
7235 tree decl,
7236 const char *name ATTRIBUTE_UNUSED)
7238 /* We output the name if and only if TREE_SYMBOL_REFERENCED is
7239 set in order to avoid putting out names that are never really
7240 used. */
7241 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
7242 && targetm.binds_local_p (decl))
7243 maybe_assemble_visibility (decl);
7246 /* The default hook for TARGET_ASM_OUTPUT_SOURCE_FILENAME. */
7248 void
7249 default_asm_output_source_filename (FILE *file, const char *name)
7251 #ifdef ASM_OUTPUT_SOURCE_FILENAME
7252 ASM_OUTPUT_SOURCE_FILENAME (file, name);
7253 #else
7254 fprintf (file, "\t.file\t");
7255 output_quoted_string (file, name);
7256 putc ('\n', file);
7257 #endif
7260 /* Output a file name in the form wanted by System V. */
7262 void
7263 output_file_directive (FILE *asm_file, const char *input_name)
7265 int len;
7266 const char *na;
7268 if (input_name == NULL)
7269 input_name = "<stdin>";
7270 else
7271 input_name = remap_debug_filename (input_name);
7273 len = strlen (input_name);
7274 na = input_name + len;
7276 /* NA gets INPUT_NAME sans directory names. */
7277 while (na > input_name)
7279 if (IS_DIR_SEPARATOR (na[-1]))
7280 break;
7281 na--;
7284 targetm.asm_out.output_source_filename (asm_file, na);
7287 /* Create a DEBUG_EXPR_DECL / DEBUG_EXPR pair from RTL expression
7288 EXP. */
7290 make_debug_expr_from_rtl (const_rtx exp)
7292 tree ddecl = make_node (DEBUG_EXPR_DECL), type;
7293 enum machine_mode mode = GET_MODE (exp);
7294 rtx dval;
7296 DECL_ARTIFICIAL (ddecl) = 1;
7297 if (REG_P (exp) && REG_EXPR (exp))
7298 type = TREE_TYPE (REG_EXPR (exp));
7299 else if (MEM_P (exp) && MEM_EXPR (exp))
7300 type = TREE_TYPE (MEM_EXPR (exp));
7301 else
7302 type = NULL_TREE;
7303 if (type && TYPE_MODE (type) == mode)
7304 TREE_TYPE (ddecl) = type;
7305 else
7306 TREE_TYPE (ddecl) = lang_hooks.types.type_for_mode (mode, 1);
7307 DECL_MODE (ddecl) = mode;
7308 dval = gen_rtx_DEBUG_EXPR (mode);
7309 DEBUG_EXPR_TREE_DECL (dval) = ddecl;
7310 SET_DECL_RTL (ddecl, dval);
7311 return dval;
7314 #ifdef ELF_ASCII_ESCAPES
7315 /* Default ASM_OUTPUT_LIMITED_STRING for ELF targets. */
7317 void
7318 default_elf_asm_output_limited_string (FILE *f, const char *s)
7320 int escape;
7321 unsigned char c;
7323 fputs (STRING_ASM_OP, f);
7324 putc ('"', f);
7325 while (*s != '\0')
7327 c = *s;
7328 escape = ELF_ASCII_ESCAPES[c];
7329 switch (escape)
7331 case 0:
7332 putc (c, f);
7333 break;
7334 case 1:
7335 /* TODO: Print in hex with fast function, important for -flto. */
7336 fprintf (f, "\\%03o", c);
7337 break;
7338 default:
7339 putc ('\\', f);
7340 putc (escape, f);
7341 break;
7343 s++;
7345 putc ('\"', f);
7346 putc ('\n', f);
7349 /* Default ASM_OUTPUT_ASCII for ELF targets. */
7351 void
7352 default_elf_asm_output_ascii (FILE *f, const char *s, unsigned int len)
7354 const char *limit = s + len;
7355 const char *last_null = NULL;
7356 unsigned bytes_in_chunk = 0;
7357 unsigned char c;
7358 int escape;
7360 for (; s < limit; s++)
7362 const char *p;
7364 if (bytes_in_chunk >= 60)
7366 putc ('\"', f);
7367 putc ('\n', f);
7368 bytes_in_chunk = 0;
7371 if (s > last_null)
7373 for (p = s; p < limit && *p != '\0'; p++)
7374 continue;
7375 last_null = p;
7377 else
7378 p = last_null;
7380 if (p < limit && (p - s) <= (long) ELF_STRING_LIMIT)
7382 if (bytes_in_chunk > 0)
7384 putc ('\"', f);
7385 putc ('\n', f);
7386 bytes_in_chunk = 0;
7389 default_elf_asm_output_limited_string (f, s);
7390 s = p;
7392 else
7394 if (bytes_in_chunk == 0)
7395 fputs (ASCII_DATA_ASM_OP "\"", f);
7397 c = *s;
7398 escape = ELF_ASCII_ESCAPES[c];
7399 switch (escape)
7401 case 0:
7402 putc (c, f);
7403 bytes_in_chunk++;
7404 break;
7405 case 1:
7406 /* TODO: Print in hex with fast function, important for -flto. */
7407 fprintf (f, "\\%03o", c);
7408 bytes_in_chunk += 4;
7409 break;
7410 default:
7411 putc ('\\', f);
7412 putc (escape, f);
7413 bytes_in_chunk += 2;
7414 break;
7420 if (bytes_in_chunk > 0)
7422 putc ('\"', f);
7423 putc ('\n', f);
7426 #endif
7428 static GTY(()) section *elf_init_array_section;
7429 static GTY(()) section *elf_fini_array_section;
7431 static section *
7432 get_elf_initfini_array_priority_section (int priority,
7433 bool constructor_p)
7435 section *sec;
7436 if (priority != DEFAULT_INIT_PRIORITY)
7438 char buf[18];
7439 sprintf (buf, "%s.%.5u",
7440 constructor_p ? ".init_array" : ".fini_array",
7441 priority);
7442 sec = get_section (buf, SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
7444 else
7446 if (constructor_p)
7448 if (elf_init_array_section == NULL)
7449 elf_init_array_section
7450 = get_section (".init_array",
7451 SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
7452 sec = elf_init_array_section;
7454 else
7456 if (elf_fini_array_section == NULL)
7457 elf_fini_array_section
7458 = get_section (".fini_array",
7459 SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
7460 sec = elf_fini_array_section;
7463 return sec;
7466 /* Use .init_array section for constructors. */
7468 void
7469 default_elf_init_array_asm_out_constructor (rtx symbol, int priority)
7471 section *sec = get_elf_initfini_array_priority_section (priority,
7472 true);
7473 assemble_addr_to_section (symbol, sec);
7476 /* Use .fini_array section for destructors. */
7478 void
7479 default_elf_fini_array_asm_out_destructor (rtx symbol, int priority)
7481 section *sec = get_elf_initfini_array_priority_section (priority,
7482 false);
7483 assemble_addr_to_section (symbol, sec);
7486 /* Default TARGET_ASM_OUTPUT_IDENT hook.
7488 This is a bit of a cheat. The real default is a no-op, but this
7489 hook is the default for all targets with a .ident directive. */
7491 void
7492 default_asm_output_ident_directive (const char *ident_str)
7494 const char *ident_asm_op = "\t.ident\t";
7496 /* If we are still in the front end, do not write out the string
7497 to asm_out_file. Instead, add a fake top-level asm statement.
7498 This allows the front ends to use this hook without actually
7499 writing to asm_out_file, to handle #ident or Pragma Ident. */
7500 if (cgraph_state == CGRAPH_STATE_PARSING)
7502 char *buf = ACONCAT ((ident_asm_op, "\"", ident_str, "\"\n", NULL));
7503 add_asm_node (build_string (strlen (buf), buf));
7505 else
7506 fprintf (asm_out_file, "%s\"%s\"\n", ident_asm_op, ident_str);
7509 #include "gt-varasm.h"