Sync with upstream 4.9 branch
[official-gcc.git] / embedded-4_9-branch / gcc / varasm.c
blob5e590f713a0d923750e7a80eeaffc6c9f74a7c35
1 /* Output variables, constants and external declarations, for GNU compiler.
2 Copyright (C) 1987-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
21 /* This file handles generation of all the assembler code
22 *except* the instructions of a function.
23 This includes declarations of variables and their initial values.
25 We also output the assembler code for constants stored in memory
26 and are responsible for combining constants with the same value. */
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tm.h"
32 #include "rtl.h"
33 #include "tree.h"
34 #include "stor-layout.h"
35 #include "stringpool.h"
36 #include "gcc-symtab.h"
37 #include "varasm.h"
38 #include "flags.h"
39 #include "function.h"
40 #include "expr.h"
41 #include "hard-reg-set.h"
42 #include "regs.h"
43 #include "output.h"
44 #include "diagnostic-core.h"
45 #include "hashtab.h"
46 #include "ggc.h"
47 #include "langhooks.h"
48 #include "tm_p.h"
49 #include "debug.h"
50 #include "target.h"
51 #include "common/common-target.h"
52 #include "targhooks.h"
53 #include "cgraph.h"
54 #include "pointer-set.h"
55 #include "asan.h"
56 #include "basic-block.h"
58 #ifdef XCOFF_DEBUGGING_INFO
59 #include "xcoffout.h" /* Needed for external data
60 declarations for e.g. AIX 4.x. */
61 #endif
63 /* The (assembler) name of the first globally-visible object output. */
64 extern GTY(()) const char *first_global_object_name;
65 extern GTY(()) const char *weak_global_object_name;
67 const char *first_global_object_name;
68 const char *weak_global_object_name;
70 struct addr_const;
71 struct constant_descriptor_rtx;
72 struct rtx_constant_pool;
74 #define n_deferred_constants (crtl->varasm.deferred_constants)
76 /* Number for making the label on the next
77 constant that is stored in memory. */
79 static GTY(()) int const_labelno;
81 /* Carry information from ASM_DECLARE_OBJECT_NAME
82 to ASM_FINISH_DECLARE_OBJECT. */
84 int size_directive_output;
86 /* The last decl for which assemble_variable was called,
87 if it did ASM_DECLARE_OBJECT_NAME.
88 If the last call to assemble_variable didn't do that,
89 this holds 0. */
91 tree last_assemble_variable_decl;
93 /* The following global variable indicates if the first basic block
94 in a function belongs to the cold partition or not. */
96 bool first_function_block_is_cold;
98 /* Whether we saw any functions with no_split_stack. */
100 static bool saw_no_split_stack;
102 static const char *strip_reg_name (const char *);
103 static int contains_pointers_p (tree);
104 #ifdef ASM_OUTPUT_EXTERNAL
105 static bool incorporeal_function_p (tree);
106 #endif
107 static void decode_addr_const (tree, struct addr_const *);
108 static hashval_t const_desc_hash (const void *);
109 static int const_desc_eq (const void *, const void *);
110 static hashval_t const_hash_1 (const tree);
111 static int compare_constant (const tree, const tree);
112 static tree copy_constant (tree);
113 static void output_constant_def_contents (rtx);
114 static void output_addressed_constants (tree);
115 static unsigned HOST_WIDE_INT output_constant (tree, unsigned HOST_WIDE_INT,
116 unsigned int);
117 static void globalize_decl (tree);
118 static bool decl_readonly_section_1 (enum section_category);
119 #ifdef BSS_SECTION_ASM_OP
120 #ifdef ASM_OUTPUT_ALIGNED_BSS
121 static void asm_output_aligned_bss (FILE *, tree, const char *,
122 unsigned HOST_WIDE_INT, int)
123 ATTRIBUTE_UNUSED;
124 #endif
125 #endif /* BSS_SECTION_ASM_OP */
126 static void mark_weak (tree);
127 static void output_constant_pool (const char *, tree);
129 /* Well-known sections, each one associated with some sort of *_ASM_OP. */
130 section *text_section;
131 section *data_section;
132 section *readonly_data_section;
133 section *sdata_section;
134 section *ctors_section;
135 section *dtors_section;
136 section *bss_section;
137 section *sbss_section;
139 /* Various forms of common section. All are guaranteed to be nonnull. */
140 section *tls_comm_section;
141 section *comm_section;
142 section *lcomm_section;
144 /* A SECTION_NOSWITCH section used for declaring global BSS variables.
145 May be null. */
146 section *bss_noswitch_section;
148 /* The section that holds the main exception table, when known. The section
149 is set either by the target's init_sections hook or by the first call to
150 switch_to_exception_section. */
151 section *exception_section;
153 /* The section that holds the DWARF2 frame unwind information, when known.
154 The section is set either by the target's init_sections hook or by the
155 first call to switch_to_eh_frame_section. */
156 section *eh_frame_section;
158 /* asm_out_file's current section. This is NULL if no section has yet
159 been selected or if we lose track of what the current section is. */
160 section *in_section;
162 /* True if code for the current function is currently being directed
163 at the cold section. */
164 bool in_cold_section_p;
166 /* A linked list of all the unnamed sections. */
167 static GTY(()) section *unnamed_sections;
169 /* Return a nonzero value if DECL has a section attribute. */
170 #define IN_NAMED_SECTION(DECL) \
171 ((TREE_CODE (DECL) == FUNCTION_DECL || TREE_CODE (DECL) == VAR_DECL) \
172 && DECL_SECTION_NAME (DECL) != NULL_TREE)
174 /* Hash table of named sections. */
175 static GTY((param_is (section))) htab_t section_htab;
177 /* A table of object_blocks, indexed by section. */
178 static GTY((param_is (struct object_block))) htab_t object_block_htab;
180 /* The next number to use for internal anchor labels. */
181 static GTY(()) int anchor_labelno;
183 /* A pool of constants that can be shared between functions. */
184 static GTY(()) struct rtx_constant_pool *shared_constant_pool;
186 /* Helper routines for maintaining section_htab. */
188 static int
189 section_entry_eq (const void *p1, const void *p2)
191 const section *old = (const section *) p1;
192 const char *new_name = (const char *) p2;
194 return strcmp (old->named.name, new_name) == 0;
197 static hashval_t
198 section_entry_hash (const void *p)
200 const section *old = (const section *) p;
201 return htab_hash_string (old->named.name);
204 /* Return a hash value for section SECT. */
206 static hashval_t
207 hash_section (section *sect)
209 if (sect->common.flags & SECTION_NAMED)
210 return htab_hash_string (sect->named.name);
211 return sect->common.flags;
214 /* Helper routines for maintaining object_block_htab. */
216 static int
217 object_block_entry_eq (const void *p1, const void *p2)
219 const struct object_block *old = (const struct object_block *) p1;
220 const section *new_section = (const section *) p2;
222 return old->sect == new_section;
225 static hashval_t
226 object_block_entry_hash (const void *p)
228 const struct object_block *old = (const struct object_block *) p;
229 return hash_section (old->sect);
232 /* Return a new unnamed section with the given fields. */
234 section *
235 get_unnamed_section (unsigned int flags, void (*callback) (const void *),
236 const void *data)
238 section *sect;
240 sect = ggc_alloc_section ();
241 sect->unnamed.common.flags = flags | SECTION_UNNAMED;
242 sect->unnamed.callback = callback;
243 sect->unnamed.data = data;
244 sect->unnamed.next = unnamed_sections;
246 unnamed_sections = sect;
247 return sect;
250 /* Return a SECTION_NOSWITCH section with the given fields. */
252 static section *
253 get_noswitch_section (unsigned int flags, noswitch_section_callback callback)
255 section *sect;
257 sect = ggc_alloc_section ();
258 sect->noswitch.common.flags = flags | SECTION_NOSWITCH;
259 sect->noswitch.callback = callback;
261 return sect;
264 /* Return the named section structure associated with NAME. Create
265 a new section with the given fields if no such structure exists. */
267 section *
268 get_section (const char *name, unsigned int flags, tree decl)
270 section *sect, **slot;
272 slot = (section **)
273 htab_find_slot_with_hash (section_htab, name,
274 htab_hash_string (name), INSERT);
275 flags |= SECTION_NAMED;
276 if (*slot == NULL)
278 sect = ggc_alloc_section ();
279 sect->named.common.flags = flags;
280 sect->named.name = ggc_strdup (name);
281 sect->named.decl = decl;
282 *slot = sect;
284 else
286 sect = *slot;
287 if ((sect->common.flags & ~SECTION_DECLARED) != flags
288 && ((sect->common.flags | flags) & SECTION_OVERRIDE) == 0)
290 /* It is fine if one of the section flags is
291 SECTION_WRITE | SECTION_RELRO and the other has none of these
292 flags (i.e. read-only) in named sections and either the
293 section hasn't been declared yet or has been declared as writable.
294 In that case just make sure the resulting flags are
295 SECTION_WRITE | SECTION_RELRO, ie. writable only because of
296 relocations. */
297 if (((sect->common.flags ^ flags) & (SECTION_WRITE | SECTION_RELRO))
298 == (SECTION_WRITE | SECTION_RELRO)
299 && (sect->common.flags
300 & ~(SECTION_DECLARED | SECTION_WRITE | SECTION_RELRO))
301 == (flags & ~(SECTION_WRITE | SECTION_RELRO))
302 && ((sect->common.flags & SECTION_DECLARED) == 0
303 || (sect->common.flags & SECTION_WRITE)))
305 sect->common.flags |= (SECTION_WRITE | SECTION_RELRO);
306 return sect;
308 /* Sanity check user variables for flag changes. */
309 if (sect->named.decl != NULL
310 && DECL_P (sect->named.decl)
311 && decl != sect->named.decl)
313 if (decl != NULL && DECL_P (decl))
314 error ("%+D causes a section type conflict with %D",
315 decl, sect->named.decl);
316 else
317 error ("section type conflict with %D", sect->named.decl);
318 inform (DECL_SOURCE_LOCATION (sect->named.decl),
319 "%qD was declared here", sect->named.decl);
321 else if (decl != NULL && DECL_P (decl))
322 error ("%+D causes a section type conflict", decl);
323 else
324 error ("section type conflict");
325 /* Make sure we don't error about one section multiple times. */
326 sect->common.flags |= SECTION_OVERRIDE;
329 return sect;
332 /* Return true if the current compilation mode benefits from having
333 objects grouped into blocks. */
335 static bool
336 use_object_blocks_p (void)
338 return flag_section_anchors;
341 /* Return the object_block structure for section SECT. Create a new
342 structure if we haven't created one already. Return null if SECT
343 itself is null. */
345 static struct object_block *
346 get_block_for_section (section *sect)
348 struct object_block *block;
349 void **slot;
351 if (sect == NULL)
352 return NULL;
354 slot = htab_find_slot_with_hash (object_block_htab, sect,
355 hash_section (sect), INSERT);
356 block = (struct object_block *) *slot;
357 if (block == NULL)
359 block = ggc_alloc_cleared_object_block ();
360 block->sect = sect;
361 *slot = block;
363 return block;
366 /* Create a symbol with label LABEL and place it at byte offset
367 OFFSET in BLOCK. OFFSET can be negative if the symbol's offset
368 is not yet known. LABEL must be a garbage-collected string. */
370 static rtx
371 create_block_symbol (const char *label, struct object_block *block,
372 HOST_WIDE_INT offset)
374 rtx symbol;
375 unsigned int size;
377 /* Create the extended SYMBOL_REF. */
378 size = RTX_HDR_SIZE + sizeof (struct block_symbol);
379 symbol = ggc_alloc_rtx_def (size);
381 /* Initialize the normal SYMBOL_REF fields. */
382 memset (symbol, 0, size);
383 PUT_CODE (symbol, SYMBOL_REF);
384 PUT_MODE (symbol, Pmode);
385 XSTR (symbol, 0) = label;
386 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_HAS_BLOCK_INFO;
388 /* Initialize the block_symbol stuff. */
389 SYMBOL_REF_BLOCK (symbol) = block;
390 SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
392 return symbol;
395 /* Return a section with a particular name and with whatever SECTION_*
396 flags section_type_flags deems appropriate. The name of the section
397 is taken from NAME if nonnull, otherwise it is taken from DECL's
398 DECL_SECTION_NAME. DECL is the decl associated with the section
399 (see the section comment for details) and RELOC is as for
400 section_type_flags. */
402 section *
403 get_named_section (tree decl, const char *name, int reloc)
405 unsigned int flags;
407 if (name == NULL)
409 gcc_assert (decl && DECL_P (decl) && DECL_SECTION_NAME (decl));
410 name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
413 flags = targetm.section_type_flags (decl, name, reloc);
414 return get_section (name, flags, decl);
417 /* If required, set DECL_SECTION_NAME to a unique name. */
419 void
420 resolve_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED,
421 int flag_function_or_data_sections)
423 if (DECL_SECTION_NAME (decl) == NULL_TREE
424 && targetm_common.have_named_sections
425 && (flag_function_or_data_sections
426 || DECL_ONE_ONLY (decl)))
428 targetm.asm_out.unique_section (decl, reloc);
429 DECL_HAS_IMPLICIT_SECTION_NAME_P (decl) = true;
433 #ifdef BSS_SECTION_ASM_OP
435 #ifdef ASM_OUTPUT_ALIGNED_BSS
437 /* Utility function for targets to use in implementing
438 ASM_OUTPUT_ALIGNED_BSS.
439 ??? It is believed that this function will work in most cases so such
440 support is localized here. */
442 static void
443 asm_output_aligned_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
444 const char *name, unsigned HOST_WIDE_INT size,
445 int align)
447 switch_to_section (bss_section);
448 ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
449 #ifdef ASM_DECLARE_OBJECT_NAME
450 last_assemble_variable_decl = decl;
451 ASM_DECLARE_OBJECT_NAME (file, name, decl);
452 #else
453 /* Standard thing is just output label for the object. */
454 ASM_OUTPUT_LABEL (file, name);
455 #endif /* ASM_DECLARE_OBJECT_NAME */
456 ASM_OUTPUT_SKIP (file, size ? size : 1);
459 #endif
461 #endif /* BSS_SECTION_ASM_OP */
463 #ifndef USE_SELECT_SECTION_FOR_FUNCTIONS
464 /* Return the hot section for function DECL. Return text_section for
465 null DECLs. */
467 static section *
468 hot_function_section (tree decl)
470 if (decl != NULL_TREE
471 && DECL_SECTION_NAME (decl) != NULL_TREE
472 && targetm_common.have_named_sections)
473 return get_named_section (decl, NULL, 0);
474 else
475 return text_section;
477 #endif
479 /* Return section for TEXT_SECTION_NAME if DECL or DECL_SECTION_NAME (DECL)
480 is NULL.
482 When DECL_SECTION_NAME is non-NULL and it is implicit section and
483 NAMED_SECTION_SUFFIX is non-NULL, then produce section called
484 concatenate the name with NAMED_SECTION_SUFFIX.
485 Otherwise produce "TEXT_SECTION_NAME.IMPLICIT_NAME". */
487 section *
488 get_named_text_section (tree decl,
489 const char *text_section_name,
490 const char *named_section_suffix)
492 if (decl && DECL_SECTION_NAME (decl))
494 if (named_section_suffix)
496 tree dsn = DECL_SECTION_NAME (decl);
497 const char *stripped_name;
498 char *name, *buffer;
500 name = (char *) alloca (TREE_STRING_LENGTH (dsn) + 1);
501 memcpy (name, TREE_STRING_POINTER (dsn),
502 TREE_STRING_LENGTH (dsn) + 1);
504 stripped_name = targetm.strip_name_encoding (name);
506 buffer = ACONCAT ((stripped_name, named_section_suffix, NULL));
507 return get_named_section (decl, buffer, 0);
509 else if (DECL_HAS_IMPLICIT_SECTION_NAME_P (decl))
511 const char *name;
513 /* Do not try to split gnu_linkonce functions. This gets somewhat
514 slipperly. */
515 if (DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP)
516 return NULL;
517 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
518 name = targetm.strip_name_encoding (name);
519 return get_named_section (decl, ACONCAT ((text_section_name, ".",
520 name, NULL)), 0);
522 else
523 return NULL;
525 return get_named_section (decl, text_section_name, 0);
528 /* Choose named function section based on its frequency. */
530 section *
531 default_function_section (tree decl, enum node_frequency freq,
532 bool startup, bool exit)
534 #if defined HAVE_LD_EH_GC_SECTIONS && defined HAVE_LD_EH_GC_SECTIONS_BUG
535 /* Old GNU linkers have buggy --gc-section support, which sometimes
536 results in .gcc_except_table* sections being garbage collected. */
537 if (decl
538 && DECL_SECTION_NAME (decl)
539 && DECL_HAS_IMPLICIT_SECTION_NAME_P (decl))
540 return NULL;
541 #endif
543 if (!flag_reorder_functions
544 || !targetm_common.have_named_sections)
545 return NULL;
546 /* Startup code should go to startup subsection unless it is
547 unlikely executed (this happens especially with function splitting
548 where we can split away unnecessary parts of static constructors. */
549 if (startup && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
551 /* If we do have a profile or(and) LTO phase is executed, we do not need
552 these ELF section. */
553 if (!in_lto_p || !flag_profile_values)
554 return get_named_text_section (decl, ".text.startup", NULL);
555 else
556 return NULL;
559 /* Similarly for exit. */
560 if (exit && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
561 return get_named_text_section (decl, ".text.exit", NULL);
563 /* Group cold functions together, similarly for hot code. */
564 switch (freq)
566 case NODE_FREQUENCY_UNLIKELY_EXECUTED:
567 return get_named_text_section (decl, ".text.unlikely", NULL);
568 case NODE_FREQUENCY_HOT:
569 /* If we do have a profile or(and) LTO phase is executed, we do not need
570 these ELF section. */
571 if (!in_lto_p || !flag_profile_values)
572 return get_named_text_section (decl, ".text.hot", NULL);
573 default:
574 return NULL;
578 /* Return the section for function DECL.
580 If DECL is NULL_TREE, return the text section. We can be passed
581 NULL_TREE under some circumstances by dbxout.c at least.
583 If FORCE_COLD is true, return cold function section ignoring
584 the frequency info of cgraph_node. */
586 static section *
587 function_section_1 (tree decl, bool force_cold)
589 section *section = NULL;
590 enum node_frequency freq = NODE_FREQUENCY_NORMAL;
591 bool startup = false, exit = false;
593 if (decl)
595 struct cgraph_node *node = cgraph_get_node (decl);
597 if (node)
599 freq = node->frequency;
600 startup = node->only_called_at_startup;
601 exit = node->only_called_at_exit;
604 if (force_cold)
605 freq = NODE_FREQUENCY_UNLIKELY_EXECUTED;
607 #ifdef USE_SELECT_SECTION_FOR_FUNCTIONS
608 if (decl != NULL_TREE
609 && DECL_SECTION_NAME (decl) != NULL_TREE)
611 if (targetm.asm_out.function_section)
612 section = targetm.asm_out.function_section (decl, freq,
613 startup, exit);
614 if (section)
615 return section;
616 return get_named_section (decl, NULL, 0);
618 else
619 return targetm.asm_out.select_section
620 (decl, freq == NODE_FREQUENCY_UNLIKELY_EXECUTED,
621 DECL_ALIGN (decl));
622 #else
623 if (targetm.asm_out.function_section)
624 section = targetm.asm_out.function_section (decl, freq, startup, exit);
625 if (section)
626 return section;
627 return hot_function_section (decl);
628 #endif
631 /* Return the section for function DECL.
633 If DECL is NULL_TREE, return the text section. We can be passed
634 NULL_TREE under some circumstances by dbxout.c at least. */
636 section *
637 function_section (tree decl)
639 /* Handle cases where function splitting code decides
640 to put function entry point into unlikely executed section
641 despite the fact that the function itself is not cold
642 (i.e. it is called rarely but contains a hot loop that is
643 better to live in hot subsection for the code locality). */
644 return function_section_1 (decl,
645 first_function_block_is_cold);
648 /* Return the section for the current function, take IN_COLD_SECTION_P
649 into account. */
651 section *
652 current_function_section (void)
654 return function_section_1 (current_function_decl, in_cold_section_p);
657 /* Tell assembler to switch to unlikely-to-be-executed text section. */
659 section *
660 unlikely_text_section (void)
662 return function_section_1 (current_function_decl, true);
665 /* When called within a function context, return true if the function
666 has been assigned a cold text section and if SECT is that section.
667 When called outside a function context, return true if SECT is the
668 default cold section. */
670 bool
671 unlikely_text_section_p (section *sect)
673 return sect == function_section_1 (current_function_decl, true);
676 /* Return the read-only data section associated with function DECL. */
678 section *
679 default_function_rodata_section (tree decl)
681 if (decl != NULL_TREE && DECL_SECTION_NAME (decl))
683 const char *name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
685 if (DECL_ONE_ONLY (decl) && HAVE_COMDAT_GROUP)
687 const char *dot;
688 size_t len;
689 char* rname;
691 dot = strchr (name + 1, '.');
692 if (!dot)
693 dot = name;
694 len = strlen (dot) + 8;
695 rname = (char *) alloca (len);
697 strcpy (rname, ".rodata");
698 strcat (rname, dot);
699 return get_section (rname, SECTION_LINKONCE, decl);
701 /* For .gnu.linkonce.t.foo we want to use .gnu.linkonce.r.foo. */
702 else if (DECL_ONE_ONLY (decl)
703 && strncmp (name, ".gnu.linkonce.t.", 16) == 0)
705 size_t len = strlen (name) + 1;
706 char *rname = (char *) alloca (len);
708 memcpy (rname, name, len);
709 rname[14] = 'r';
710 return get_section (rname, SECTION_LINKONCE, decl);
712 /* For .text.foo we want to use .rodata.foo. */
713 else if (flag_function_sections && flag_data_sections
714 && strncmp (name, ".text.", 6) == 0)
716 size_t len = strlen (name) + 1;
717 char *rname = (char *) alloca (len + 2);
719 memcpy (rname, ".rodata", 7);
720 memcpy (rname + 7, name + 5, len - 5);
721 return get_section (rname, 0, decl);
725 return readonly_data_section;
728 /* Return the read-only data section associated with function DECL
729 for targets where that section should be always the single
730 readonly data section. */
732 section *
733 default_no_function_rodata_section (tree decl ATTRIBUTE_UNUSED)
735 return readonly_data_section;
738 /* Return the section to use for string merging. */
740 static section *
741 mergeable_string_section (tree decl ATTRIBUTE_UNUSED,
742 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
743 unsigned int flags ATTRIBUTE_UNUSED)
745 HOST_WIDE_INT len;
747 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
748 && TREE_CODE (decl) == STRING_CST
749 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
750 && align <= 256
751 && (len = int_size_in_bytes (TREE_TYPE (decl))) > 0
752 && TREE_STRING_LENGTH (decl) >= len)
754 enum machine_mode mode;
755 unsigned int modesize;
756 const char *str;
757 HOST_WIDE_INT i;
758 int j, unit;
759 const char *prefix = targetm.asm_out.mergeable_rodata_prefix;
760 char *name = (char *) alloca (strlen (prefix) + 30);
762 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (decl)));
763 modesize = GET_MODE_BITSIZE (mode);
764 if (modesize >= 8 && modesize <= 256
765 && (modesize & (modesize - 1)) == 0)
767 if (align < modesize)
768 align = modesize;
770 str = TREE_STRING_POINTER (decl);
771 unit = GET_MODE_SIZE (mode);
773 /* Check for embedded NUL characters. */
774 for (i = 0; i < len; i += unit)
776 for (j = 0; j < unit; j++)
777 if (str[i + j] != '\0')
778 break;
779 if (j == unit)
780 break;
782 if (i == len - unit)
784 sprintf (name, "%s.str%d.%d", prefix,
785 modesize / 8, (int) (align / 8));
786 flags |= (modesize / 8) | SECTION_MERGE | SECTION_STRINGS;
787 return get_section (name, flags, NULL);
792 return readonly_data_section;
795 /* Return the section to use for constant merging. */
797 section *
798 mergeable_constant_section (enum machine_mode mode ATTRIBUTE_UNUSED,
799 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
800 unsigned int flags ATTRIBUTE_UNUSED)
802 unsigned int modesize = GET_MODE_BITSIZE (mode);
804 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
805 && mode != VOIDmode
806 && mode != BLKmode
807 && modesize <= align
808 && align >= 8
809 && align <= 256
810 && (align & (align - 1)) == 0)
812 const char *prefix = targetm.asm_out.mergeable_rodata_prefix;
813 char *name = (char *) alloca (strlen (prefix) + 30);
815 sprintf (name, "%s.cst%d", prefix, (int) (align / 8));
816 flags |= (align / 8) | SECTION_MERGE;
817 return get_section (name, flags, NULL);
819 return readonly_data_section;
822 /* Given NAME, a putative register name, discard any customary prefixes. */
824 static const char *
825 strip_reg_name (const char *name)
827 #ifdef REGISTER_PREFIX
828 if (!strncmp (name, REGISTER_PREFIX, strlen (REGISTER_PREFIX)))
829 name += strlen (REGISTER_PREFIX);
830 #endif
831 if (name[0] == '%' || name[0] == '#')
832 name++;
833 return name;
836 /* The user has asked for a DECL to have a particular name. Set (or
837 change) it in such a way that we don't prefix an underscore to
838 it. */
839 void
840 set_user_assembler_name (tree decl, const char *name)
842 char *starred = (char *) alloca (strlen (name) + 2);
843 starred[0] = '*';
844 strcpy (starred + 1, name);
845 change_decl_assembler_name (decl, get_identifier (starred));
846 SET_DECL_RTL (decl, NULL_RTX);
849 /* Decode an `asm' spec for a declaration as a register name.
850 Return the register number, or -1 if nothing specified,
851 or -2 if the ASMSPEC is not `cc' or `memory' and is not recognized,
852 or -3 if ASMSPEC is `cc' and is not recognized,
853 or -4 if ASMSPEC is `memory' and is not recognized.
854 Accept an exact spelling or a decimal number.
855 Prefixes such as % are optional. */
858 decode_reg_name_and_count (const char *asmspec, int *pnregs)
860 /* Presume just one register is clobbered. */
861 *pnregs = 1;
863 if (asmspec != 0)
865 int i;
867 /* Get rid of confusing prefixes. */
868 asmspec = strip_reg_name (asmspec);
870 /* Allow a decimal number as a "register name". */
871 for (i = strlen (asmspec) - 1; i >= 0; i--)
872 if (! ISDIGIT (asmspec[i]))
873 break;
874 if (asmspec[0] != 0 && i < 0)
876 i = atoi (asmspec);
877 if (i < FIRST_PSEUDO_REGISTER && i >= 0)
878 return i;
879 else
880 return -2;
883 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
884 if (reg_names[i][0]
885 && ! strcmp (asmspec, strip_reg_name (reg_names[i])))
886 return i;
888 #ifdef OVERLAPPING_REGISTER_NAMES
890 static const struct
892 const char *const name;
893 const int number;
894 const int nregs;
895 } table[] = OVERLAPPING_REGISTER_NAMES;
897 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
898 if (table[i].name[0]
899 && ! strcmp (asmspec, table[i].name))
901 *pnregs = table[i].nregs;
902 return table[i].number;
905 #endif /* OVERLAPPING_REGISTER_NAMES */
907 #ifdef ADDITIONAL_REGISTER_NAMES
909 static const struct { const char *const name; const int number; } table[]
910 = ADDITIONAL_REGISTER_NAMES;
912 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
913 if (table[i].name[0]
914 && ! strcmp (asmspec, table[i].name))
915 return table[i].number;
917 #endif /* ADDITIONAL_REGISTER_NAMES */
919 if (!strcmp (asmspec, "memory"))
920 return -4;
922 if (!strcmp (asmspec, "cc"))
923 return -3;
925 return -2;
928 return -1;
932 decode_reg_name (const char *name)
934 int count;
935 return decode_reg_name_and_count (name, &count);
939 /* Return true if DECL's initializer is suitable for a BSS section. */
941 bool
942 bss_initializer_p (const_tree decl)
944 return (DECL_INITIAL (decl) == NULL
945 || DECL_INITIAL (decl) == error_mark_node
946 || (flag_zero_initialized_in_bss
947 /* Leave constant zeroes in .rodata so they
948 can be shared. */
949 && !TREE_READONLY (decl)
950 && initializer_zerop (DECL_INITIAL (decl))));
953 /* Compute the alignment of variable specified by DECL.
954 DONT_OUTPUT_DATA is from assemble_variable. */
956 void
957 align_variable (tree decl, bool dont_output_data)
959 unsigned int align = DECL_ALIGN (decl);
961 /* In the case for initialing an array whose length isn't specified,
962 where we have not yet been able to do the layout,
963 figure out the proper alignment now. */
964 if (dont_output_data && DECL_SIZE (decl) == 0
965 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
966 align = MAX (align, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (decl))));
968 /* Some object file formats have a maximum alignment which they support.
969 In particular, a.out format supports a maximum alignment of 4. */
970 if (align > MAX_OFILE_ALIGNMENT)
972 error ("alignment of %q+D is greater than maximum object "
973 "file alignment %d", decl,
974 MAX_OFILE_ALIGNMENT/BITS_PER_UNIT);
975 align = MAX_OFILE_ALIGNMENT;
978 if (! DECL_USER_ALIGN (decl))
980 #ifdef DATA_ABI_ALIGNMENT
981 unsigned int data_abi_align
982 = DATA_ABI_ALIGNMENT (TREE_TYPE (decl), align);
983 /* For backwards compatibility, don't assume the ABI alignment for
984 TLS variables. */
985 if (! DECL_THREAD_LOCAL_P (decl) || data_abi_align <= BITS_PER_WORD)
986 align = data_abi_align;
987 #endif
989 /* On some machines, it is good to increase alignment sometimes.
990 But as DECL_ALIGN is used both for actually emitting the variable
991 and for code accessing the variable as guaranteed alignment, we
992 can only increase the alignment if it is a performance optimization
993 if the references to it must bind to the current definition. */
994 if (decl_binds_to_current_def_p (decl))
996 #ifdef DATA_ALIGNMENT
997 unsigned int data_align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
998 /* Don't increase alignment too much for TLS variables - TLS space
999 is too precious. */
1000 if (! DECL_THREAD_LOCAL_P (decl) || data_align <= BITS_PER_WORD)
1001 align = data_align;
1002 #endif
1003 #ifdef CONSTANT_ALIGNMENT
1004 if (DECL_INITIAL (decl) != 0
1005 && DECL_INITIAL (decl) != error_mark_node)
1007 unsigned int const_align
1008 = CONSTANT_ALIGNMENT (DECL_INITIAL (decl), align);
1009 /* Don't increase alignment too much for TLS variables - TLS
1010 space is too precious. */
1011 if (! DECL_THREAD_LOCAL_P (decl) || const_align <= BITS_PER_WORD)
1012 align = const_align;
1014 #endif
1018 /* Reset the alignment in case we have made it tighter, so we can benefit
1019 from it in get_pointer_alignment. */
1020 DECL_ALIGN (decl) = align;
1023 /* Return DECL_ALIGN (decl), possibly increased for optimization purposes
1024 beyond what align_variable returned. */
1026 static unsigned int
1027 get_variable_align (tree decl)
1029 unsigned int align = DECL_ALIGN (decl);
1031 /* For user aligned vars or static vars align_variable already did
1032 everything. */
1033 if (DECL_USER_ALIGN (decl) || !TREE_PUBLIC (decl))
1034 return align;
1036 #ifdef DATA_ABI_ALIGNMENT
1037 if (DECL_THREAD_LOCAL_P (decl))
1038 align = DATA_ABI_ALIGNMENT (TREE_TYPE (decl), align);
1039 #endif
1041 /* For decls that bind to the current definition, align_variable
1042 did also everything, except for not assuming ABI required alignment
1043 of TLS variables. For other vars, increase the alignment here
1044 as an optimization. */
1045 if (!decl_binds_to_current_def_p (decl))
1047 /* On some machines, it is good to increase alignment sometimes. */
1048 #ifdef DATA_ALIGNMENT
1049 unsigned int data_align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
1050 /* Don't increase alignment too much for TLS variables - TLS space
1051 is too precious. */
1052 if (! DECL_THREAD_LOCAL_P (decl) || data_align <= BITS_PER_WORD)
1053 align = data_align;
1054 #endif
1055 #ifdef CONSTANT_ALIGNMENT
1056 if (DECL_INITIAL (decl) != 0 && DECL_INITIAL (decl) != error_mark_node)
1058 unsigned int const_align = CONSTANT_ALIGNMENT (DECL_INITIAL (decl),
1059 align);
1060 /* Don't increase alignment too much for TLS variables - TLS space
1061 is too precious. */
1062 if (! DECL_THREAD_LOCAL_P (decl) || const_align <= BITS_PER_WORD)
1063 align = const_align;
1065 #endif
1068 return align;
1071 /* Return the section into which the given VAR_DECL or CONST_DECL
1072 should be placed. PREFER_NOSWITCH_P is true if a noswitch
1073 section should be used wherever possible. */
1075 section *
1076 get_variable_section (tree decl, bool prefer_noswitch_p)
1078 addr_space_t as = ADDR_SPACE_GENERIC;
1079 int reloc;
1081 if (TREE_TYPE (decl) != error_mark_node)
1082 as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
1084 if (DECL_COMMON (decl))
1086 /* If the decl has been given an explicit section name, or it resides
1087 in a non-generic address space, then it isn't common, and shouldn't
1088 be handled as such. */
1089 gcc_assert (DECL_SECTION_NAME (decl) == NULL
1090 && ADDR_SPACE_GENERIC_P (as));
1091 if (DECL_THREAD_LOCAL_P (decl))
1092 return tls_comm_section;
1093 else if (TREE_PUBLIC (decl) && bss_initializer_p (decl))
1094 return comm_section;
1097 if (DECL_INITIAL (decl) == error_mark_node)
1098 reloc = contains_pointers_p (TREE_TYPE (decl)) ? 3 : 0;
1099 else if (DECL_INITIAL (decl))
1100 reloc = compute_reloc_for_constant (DECL_INITIAL (decl));
1101 else
1102 reloc = 0;
1104 resolve_unique_section (decl, reloc, flag_data_sections);
1105 if (IN_NAMED_SECTION (decl))
1106 return get_named_section (decl, NULL, reloc);
1108 if (ADDR_SPACE_GENERIC_P (as)
1109 && !DECL_THREAD_LOCAL_P (decl)
1110 && !(prefer_noswitch_p && targetm.have_switchable_bss_sections)
1111 && bss_initializer_p (decl))
1113 if (!TREE_PUBLIC (decl)
1114 && !((flag_sanitize & SANITIZE_ADDRESS)
1115 && asan_protect_global (decl)))
1116 return lcomm_section;
1117 if (bss_noswitch_section)
1118 return bss_noswitch_section;
1121 return targetm.asm_out.select_section (decl, reloc,
1122 get_variable_align (decl));
1125 /* Return the block into which object_block DECL should be placed. */
1127 static struct object_block *
1128 get_block_for_decl (tree decl)
1130 section *sect;
1132 if (TREE_CODE (decl) == VAR_DECL)
1134 /* The object must be defined in this translation unit. */
1135 if (DECL_EXTERNAL (decl))
1136 return NULL;
1138 /* There's no point using object blocks for something that is
1139 isolated by definition. */
1140 if (DECL_ONE_ONLY (decl))
1141 return NULL;
1144 /* We can only calculate block offsets if the decl has a known
1145 constant size. */
1146 if (DECL_SIZE_UNIT (decl) == NULL)
1147 return NULL;
1148 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (decl)))
1149 return NULL;
1151 /* Find out which section should contain DECL. We cannot put it into
1152 an object block if it requires a standalone definition. */
1153 if (TREE_CODE (decl) == VAR_DECL)
1154 align_variable (decl, 0);
1155 sect = get_variable_section (decl, true);
1156 if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
1157 return NULL;
1159 return get_block_for_section (sect);
1162 /* Make sure block symbol SYMBOL is in block BLOCK. */
1164 static void
1165 change_symbol_block (rtx symbol, struct object_block *block)
1167 if (block != SYMBOL_REF_BLOCK (symbol))
1169 gcc_assert (SYMBOL_REF_BLOCK_OFFSET (symbol) < 0);
1170 SYMBOL_REF_BLOCK (symbol) = block;
1174 /* Return true if it is possible to put DECL in an object_block. */
1176 static bool
1177 use_blocks_for_decl_p (tree decl)
1179 /* Only data DECLs can be placed into object blocks. */
1180 if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != CONST_DECL)
1181 return false;
1183 /* Detect decls created by dw2_force_const_mem. Such decls are
1184 special because DECL_INITIAL doesn't specify the decl's true value.
1185 dw2_output_indirect_constants will instead call assemble_variable
1186 with dont_output_data set to 1 and then print the contents itself. */
1187 if (DECL_INITIAL (decl) == decl)
1188 return false;
1190 /* If this decl is an alias, then we don't want to emit a
1191 definition. */
1192 if (lookup_attribute ("alias", DECL_ATTRIBUTES (decl)))
1193 return false;
1195 return targetm.use_blocks_for_decl_p (decl);
1198 /* Create the DECL_RTL for a VAR_DECL or FUNCTION_DECL. DECL should
1199 have static storage duration. In other words, it should not be an
1200 automatic variable, including PARM_DECLs.
1202 There is, however, one exception: this function handles variables
1203 explicitly placed in a particular register by the user.
1205 This is never called for PARM_DECL nodes. */
1207 void
1208 make_decl_rtl (tree decl)
1210 const char *name = 0;
1211 int reg_number;
1212 rtx x;
1214 /* Check that we are not being given an automatic variable. */
1215 gcc_assert (TREE_CODE (decl) != PARM_DECL
1216 && TREE_CODE (decl) != RESULT_DECL);
1218 /* A weak alias has TREE_PUBLIC set but not the other bits. */
1219 gcc_assert (TREE_CODE (decl) != VAR_DECL
1220 || TREE_STATIC (decl)
1221 || TREE_PUBLIC (decl)
1222 || DECL_EXTERNAL (decl)
1223 || DECL_REGISTER (decl));
1225 /* And that we were not given a type or a label. */
1226 gcc_assert (TREE_CODE (decl) != TYPE_DECL
1227 && TREE_CODE (decl) != LABEL_DECL);
1229 /* For a duplicate declaration, we can be called twice on the
1230 same DECL node. Don't discard the RTL already made. */
1231 if (DECL_RTL_SET_P (decl))
1233 /* If the old RTL had the wrong mode, fix the mode. */
1234 x = DECL_RTL (decl);
1235 if (GET_MODE (x) != DECL_MODE (decl))
1236 SET_DECL_RTL (decl, adjust_address_nv (x, DECL_MODE (decl), 0));
1238 if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1239 return;
1241 /* ??? Another way to do this would be to maintain a hashed
1242 table of such critters. Instead of adding stuff to a DECL
1243 to give certain attributes to it, we could use an external
1244 hash map from DECL to set of attributes. */
1246 /* Let the target reassign the RTL if it wants.
1247 This is necessary, for example, when one machine specific
1248 decl attribute overrides another. */
1249 targetm.encode_section_info (decl, DECL_RTL (decl), false);
1251 /* If the symbol has a SYMBOL_REF_BLOCK field, update it based
1252 on the new decl information. */
1253 if (MEM_P (x)
1254 && GET_CODE (XEXP (x, 0)) == SYMBOL_REF
1255 && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (x, 0)))
1256 change_symbol_block (XEXP (x, 0), get_block_for_decl (decl));
1258 return;
1261 /* If this variable belongs to the global constant pool, retrieve the
1262 pre-computed RTL or recompute it in LTO mode. */
1263 if (TREE_CODE (decl) == VAR_DECL && DECL_IN_CONSTANT_POOL (decl))
1265 SET_DECL_RTL (decl, output_constant_def (DECL_INITIAL (decl), 1));
1266 return;
1269 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
1271 if (name[0] != '*' && TREE_CODE (decl) != FUNCTION_DECL
1272 && DECL_REGISTER (decl))
1274 error ("register name not specified for %q+D", decl);
1276 else if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1278 const char *asmspec = name+1;
1279 enum machine_mode mode = DECL_MODE (decl);
1280 reg_number = decode_reg_name (asmspec);
1281 /* First detect errors in declaring global registers. */
1282 if (reg_number == -1)
1283 error ("register name not specified for %q+D", decl);
1284 else if (reg_number < 0)
1285 error ("invalid register name for %q+D", decl);
1286 else if (mode == BLKmode)
1287 error ("data type of %q+D isn%'t suitable for a register",
1288 decl);
1289 else if (!in_hard_reg_set_p (accessible_reg_set, mode, reg_number))
1290 error ("the register specified for %q+D cannot be accessed"
1291 " by the current target", decl);
1292 else if (!in_hard_reg_set_p (operand_reg_set, mode, reg_number))
1293 error ("the register specified for %q+D is not general enough"
1294 " to be used as a register variable", decl);
1295 else if (!HARD_REGNO_MODE_OK (reg_number, mode))
1296 error ("register specified for %q+D isn%'t suitable for data type",
1297 decl);
1298 /* Now handle properly declared static register variables. */
1299 else
1301 int nregs;
1303 if (DECL_INITIAL (decl) != 0 && TREE_STATIC (decl))
1305 DECL_INITIAL (decl) = 0;
1306 error ("global register variable has initial value");
1308 if (TREE_THIS_VOLATILE (decl))
1309 warning (OPT_Wvolatile_register_var,
1310 "optimization may eliminate reads and/or "
1311 "writes to register variables");
1313 /* If the user specified one of the eliminables registers here,
1314 e.g., FRAME_POINTER_REGNUM, we don't want to get this variable
1315 confused with that register and be eliminated. This usage is
1316 somewhat suspect... */
1318 SET_DECL_RTL (decl, gen_rtx_raw_REG (mode, reg_number));
1319 ORIGINAL_REGNO (DECL_RTL (decl)) = reg_number;
1320 REG_USERVAR_P (DECL_RTL (decl)) = 1;
1322 if (TREE_STATIC (decl))
1324 /* Make this register global, so not usable for anything
1325 else. */
1326 #ifdef ASM_DECLARE_REGISTER_GLOBAL
1327 name = IDENTIFIER_POINTER (DECL_NAME (decl));
1328 ASM_DECLARE_REGISTER_GLOBAL (asm_out_file, decl, reg_number, name);
1329 #endif
1330 nregs = hard_regno_nregs[reg_number][mode];
1331 while (nregs > 0)
1332 globalize_reg (decl, reg_number + --nregs);
1335 /* As a register variable, it has no section. */
1336 return;
1339 /* Now handle ordinary static variables and functions (in memory).
1340 Also handle vars declared register invalidly. */
1341 else if (name[0] == '*')
1343 #ifdef REGISTER_PREFIX
1344 if (strlen (REGISTER_PREFIX) != 0)
1346 reg_number = decode_reg_name (name);
1347 if (reg_number >= 0 || reg_number == -3)
1348 error ("register name given for non-register variable %q+D", decl);
1350 #endif
1353 /* Specifying a section attribute on a variable forces it into a
1354 non-.bss section, and thus it cannot be common. */
1355 /* FIXME: In general this code should not be necessary because
1356 visibility pass is doing the same work. But notice_global_symbol
1357 is called early and it needs to make DECL_RTL to get the name.
1358 we take care of recomputing the DECL_RTL after visibility is changed. */
1359 if (TREE_CODE (decl) == VAR_DECL
1360 && DECL_SECTION_NAME (decl) != NULL_TREE
1361 && DECL_INITIAL (decl) == NULL_TREE
1362 && DECL_COMMON (decl))
1363 DECL_COMMON (decl) = 0;
1365 /* Variables can't be both common and weak. */
1366 if (TREE_CODE (decl) == VAR_DECL && DECL_WEAK (decl))
1367 DECL_COMMON (decl) = 0;
1369 if (use_object_blocks_p () && use_blocks_for_decl_p (decl))
1370 x = create_block_symbol (name, get_block_for_decl (decl), -1);
1371 else
1373 enum machine_mode address_mode = Pmode;
1374 if (TREE_TYPE (decl) != error_mark_node)
1376 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
1377 address_mode = targetm.addr_space.address_mode (as);
1379 x = gen_rtx_SYMBOL_REF (address_mode, name);
1381 SYMBOL_REF_WEAK (x) = DECL_WEAK (decl);
1382 SET_SYMBOL_REF_DECL (x, decl);
1384 x = gen_rtx_MEM (DECL_MODE (decl), x);
1385 if (TREE_CODE (decl) != FUNCTION_DECL)
1386 set_mem_attributes (x, decl, 1);
1387 SET_DECL_RTL (decl, x);
1389 /* Optionally set flags or add text to the name to record information
1390 such as that it is a function name.
1391 If the name is changed, the macro ASM_OUTPUT_LABELREF
1392 will have to know how to strip this information. */
1393 targetm.encode_section_info (decl, DECL_RTL (decl), true);
1396 /* Like make_decl_rtl, but inhibit creation of new alias sets when
1397 calling make_decl_rtl. Also, reset DECL_RTL before returning the
1398 rtl. */
1401 make_decl_rtl_for_debug (tree decl)
1403 unsigned int save_aliasing_flag;
1404 rtx rtl;
1406 if (DECL_RTL_SET_P (decl))
1407 return DECL_RTL (decl);
1409 /* Kludge alert! Somewhere down the call chain, make_decl_rtl will
1410 call new_alias_set. If running with -fcompare-debug, sometimes
1411 we do not want to create alias sets that will throw the alias
1412 numbers off in the comparison dumps. So... clearing
1413 flag_strict_aliasing will keep new_alias_set() from creating a
1414 new set. */
1415 save_aliasing_flag = flag_strict_aliasing;
1416 flag_strict_aliasing = 0;
1418 rtl = DECL_RTL (decl);
1419 /* Reset DECL_RTL back, as various parts of the compiler expects
1420 DECL_RTL set meaning it is actually going to be output. */
1421 SET_DECL_RTL (decl, NULL);
1423 flag_strict_aliasing = save_aliasing_flag;
1424 return rtl;
1427 /* Output a string of literal assembler code
1428 for an `asm' keyword used between functions. */
1430 void
1431 assemble_asm (tree string)
1433 const char *p;
1434 app_enable ();
1436 if (TREE_CODE (string) == ADDR_EXPR)
1437 string = TREE_OPERAND (string, 0);
1439 p = TREE_STRING_POINTER (string);
1440 fprintf (asm_out_file, "%s%s\n", p[0] == '\t' ? "" : "\t", p);
1443 /* Write the address of the entity given by SYMBOL to SEC. */
1444 void
1445 assemble_addr_to_section (rtx symbol, section *sec)
1447 switch_to_section (sec);
1448 assemble_align (POINTER_SIZE);
1449 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1452 /* Return the numbered .ctors.N (if CONSTRUCTOR_P) or .dtors.N (if
1453 not) section for PRIORITY. */
1454 section *
1455 get_cdtor_priority_section (int priority, bool constructor_p)
1457 char buf[16];
1459 /* ??? This only works reliably with the GNU linker. */
1460 sprintf (buf, "%s.%.5u",
1461 constructor_p ? ".ctors" : ".dtors",
1462 /* Invert the numbering so the linker puts us in the proper
1463 order; constructors are run from right to left, and the
1464 linker sorts in increasing order. */
1465 MAX_INIT_PRIORITY - priority);
1466 return get_section (buf, SECTION_WRITE, NULL);
1469 void
1470 default_named_section_asm_out_destructor (rtx symbol, int priority)
1472 section *sec;
1474 if (priority != DEFAULT_INIT_PRIORITY)
1475 sec = get_cdtor_priority_section (priority,
1476 /*constructor_p=*/false);
1477 else
1478 sec = get_section (".dtors", SECTION_WRITE, NULL);
1480 assemble_addr_to_section (symbol, sec);
1483 #ifdef DTORS_SECTION_ASM_OP
1484 void
1485 default_dtor_section_asm_out_destructor (rtx symbol,
1486 int priority ATTRIBUTE_UNUSED)
1488 assemble_addr_to_section (symbol, dtors_section);
1490 #endif
1492 void
1493 default_named_section_asm_out_constructor (rtx symbol, int priority)
1495 section *sec;
1497 if (priority != DEFAULT_INIT_PRIORITY)
1498 sec = get_cdtor_priority_section (priority,
1499 /*constructor_p=*/true);
1500 else
1501 sec = get_section (".ctors", SECTION_WRITE, NULL);
1503 assemble_addr_to_section (symbol, sec);
1506 #ifdef CTORS_SECTION_ASM_OP
1507 void
1508 default_ctor_section_asm_out_constructor (rtx symbol,
1509 int priority ATTRIBUTE_UNUSED)
1511 assemble_addr_to_section (symbol, ctors_section);
1513 #endif
1515 /* CONSTANT_POOL_BEFORE_FUNCTION may be defined as an expression with
1516 a nonzero value if the constant pool should be output before the
1517 start of the function, or a zero value if the pool should output
1518 after the end of the function. The default is to put it before the
1519 start. */
1521 #ifndef CONSTANT_POOL_BEFORE_FUNCTION
1522 #define CONSTANT_POOL_BEFORE_FUNCTION 1
1523 #endif
1525 /* DECL is an object (either VAR_DECL or FUNCTION_DECL) which is going
1526 to be output to assembler.
1527 Set first_global_object_name and weak_global_object_name as appropriate. */
1529 void
1530 notice_global_symbol (tree decl)
1532 const char **type = &first_global_object_name;
1534 if (first_global_object_name
1535 || !TREE_PUBLIC (decl)
1536 || DECL_EXTERNAL (decl)
1537 || !DECL_NAME (decl)
1538 || (TREE_CODE (decl) != FUNCTION_DECL
1539 && (TREE_CODE (decl) != VAR_DECL
1540 || (DECL_COMMON (decl)
1541 && (DECL_INITIAL (decl) == 0
1542 || DECL_INITIAL (decl) == error_mark_node))))
1543 || !MEM_P (DECL_RTL (decl)))
1544 return;
1546 /* We win when global object is found, but it is useful to know about weak
1547 symbol as well so we can produce nicer unique names. */
1548 if (DECL_WEAK (decl) || DECL_ONE_ONLY (decl) || flag_shlib)
1549 type = &weak_global_object_name;
1551 if (!*type)
1553 const char *p;
1554 const char *name;
1555 rtx decl_rtl = DECL_RTL (decl);
1557 p = targetm.strip_name_encoding (XSTR (XEXP (decl_rtl, 0), 0));
1558 name = ggc_strdup (p);
1560 *type = name;
1564 /* If not using flag_reorder_blocks_and_partition, decide early whether the
1565 current function goes into the cold section, so that targets can use
1566 current_function_section during RTL expansion. DECL describes the
1567 function. */
1569 void
1570 decide_function_section (tree decl)
1572 first_function_block_is_cold = false;
1574 if (flag_reorder_blocks_and_partition)
1575 /* We will decide in assemble_start_function. */
1576 return;
1578 if (DECL_SECTION_NAME (decl))
1580 struct cgraph_node *node = cgraph_get_node (current_function_decl);
1581 /* Calls to function_section rely on first_function_block_is_cold
1582 being accurate. */
1583 first_function_block_is_cold = (node
1584 && node->frequency
1585 == NODE_FREQUENCY_UNLIKELY_EXECUTED);
1588 in_cold_section_p = first_function_block_is_cold;
1591 /* Output assembler code for the constant pool of a function and associated
1592 with defining the name of the function. DECL describes the function.
1593 NAME is the function's name. For the constant pool, we use the current
1594 constant pool data. */
1596 void
1597 assemble_start_function (tree decl, const char *fnname)
1599 int align;
1600 char tmp_label[100];
1601 bool hot_label_written = false;
1603 if (flag_reorder_blocks_and_partition)
1605 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTB", const_labelno);
1606 crtl->subsections.hot_section_label = ggc_strdup (tmp_label);
1607 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDB", const_labelno);
1608 crtl->subsections.cold_section_label = ggc_strdup (tmp_label);
1609 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTE", const_labelno);
1610 crtl->subsections.hot_section_end_label = ggc_strdup (tmp_label);
1611 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDE", const_labelno);
1612 crtl->subsections.cold_section_end_label = ggc_strdup (tmp_label);
1613 const_labelno++;
1615 else
1617 crtl->subsections.hot_section_label = NULL;
1618 crtl->subsections.cold_section_label = NULL;
1619 crtl->subsections.hot_section_end_label = NULL;
1620 crtl->subsections.cold_section_end_label = NULL;
1623 /* The following code does not need preprocessing in the assembler. */
1625 app_disable ();
1627 if (CONSTANT_POOL_BEFORE_FUNCTION)
1628 output_constant_pool (fnname, decl);
1630 /* Make sure the not and cold text (code) sections are properly
1631 aligned. This is necessary here in the case where the function
1632 has both hot and cold sections, because we don't want to re-set
1633 the alignment when the section switch happens mid-function. */
1635 if (flag_reorder_blocks_and_partition)
1637 first_function_block_is_cold = false;
1639 switch_to_section (unlikely_text_section ());
1640 assemble_align (DECL_ALIGN (decl));
1641 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_label);
1643 /* When the function starts with a cold section, we need to explicitly
1644 align the hot section and write out the hot section label.
1645 But if the current function is a thunk, we do not have a CFG. */
1646 if (!cfun->is_thunk
1647 && BB_PARTITION (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb) == BB_COLD_PARTITION)
1649 switch_to_section (text_section);
1650 assemble_align (DECL_ALIGN (decl));
1651 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
1652 hot_label_written = true;
1653 first_function_block_is_cold = true;
1655 in_cold_section_p = first_function_block_is_cold;
1659 /* Switch to the correct text section for the start of the function. */
1661 switch_to_section (function_section (decl));
1662 if (flag_reorder_blocks_and_partition
1663 && !hot_label_written)
1664 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
1666 /* Tell assembler to move to target machine's alignment for functions. */
1667 align = floor_log2 (DECL_ALIGN (decl) / BITS_PER_UNIT);
1668 if (align > 0)
1670 ASM_OUTPUT_ALIGN (asm_out_file, align);
1673 /* Handle a user-specified function alignment.
1674 Note that we still need to align to DECL_ALIGN, as above,
1675 because ASM_OUTPUT_MAX_SKIP_ALIGN might not do any alignment at all. */
1676 if (! DECL_USER_ALIGN (decl)
1677 && align_functions_log > align
1678 && optimize_function_for_speed_p (cfun))
1680 #ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
1681 ASM_OUTPUT_MAX_SKIP_ALIGN (asm_out_file,
1682 align_functions_log, align_functions - 1);
1683 #else
1684 ASM_OUTPUT_ALIGN (asm_out_file, align_functions_log);
1685 #endif
1688 #ifdef ASM_OUTPUT_FUNCTION_PREFIX
1689 ASM_OUTPUT_FUNCTION_PREFIX (asm_out_file, fnname);
1690 #endif
1692 if (!DECL_IGNORED_P (decl))
1693 (*debug_hooks->begin_function) (decl);
1695 /* Make function name accessible from other files, if appropriate. */
1697 if (TREE_PUBLIC (decl))
1699 notice_global_symbol (decl);
1701 globalize_decl (decl);
1703 maybe_assemble_visibility (decl);
1706 if (DECL_PRESERVE_P (decl))
1707 targetm.asm_out.mark_decl_preserved (fnname);
1709 /* Do any machine/system dependent processing of the function name. */
1710 #ifdef ASM_DECLARE_FUNCTION_NAME
1711 ASM_DECLARE_FUNCTION_NAME (asm_out_file, fnname, current_function_decl);
1712 #else
1713 /* Standard thing is just output label for the function. */
1714 ASM_OUTPUT_FUNCTION_LABEL (asm_out_file, fnname, current_function_decl);
1715 #endif /* ASM_DECLARE_FUNCTION_NAME */
1717 if (lookup_attribute ("no_split_stack", DECL_ATTRIBUTES (decl)))
1718 saw_no_split_stack = true;
1721 /* Output assembler code associated with defining the size of the
1722 function. DECL describes the function. NAME is the function's name. */
1724 void
1725 assemble_end_function (tree decl, const char *fnname ATTRIBUTE_UNUSED)
1727 #ifdef ASM_DECLARE_FUNCTION_SIZE
1728 /* We could have switched section in the middle of the function. */
1729 if (flag_reorder_blocks_and_partition)
1730 switch_to_section (function_section (decl));
1731 ASM_DECLARE_FUNCTION_SIZE (asm_out_file, fnname, decl);
1732 #endif
1733 if (! CONSTANT_POOL_BEFORE_FUNCTION)
1735 output_constant_pool (fnname, decl);
1736 switch_to_section (function_section (decl)); /* need to switch back */
1738 /* Output labels for end of hot/cold text sections (to be used by
1739 debug info.) */
1740 if (flag_reorder_blocks_and_partition)
1742 section *save_text_section;
1744 save_text_section = in_section;
1745 switch_to_section (unlikely_text_section ());
1746 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_end_label);
1747 if (first_function_block_is_cold)
1748 switch_to_section (text_section);
1749 else
1750 switch_to_section (function_section (decl));
1751 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_end_label);
1752 switch_to_section (save_text_section);
1756 /* Assemble code to leave SIZE bytes of zeros. */
1758 void
1759 assemble_zeros (unsigned HOST_WIDE_INT size)
1761 /* Do no output if -fsyntax-only. */
1762 if (flag_syntax_only)
1763 return;
1765 #ifdef ASM_NO_SKIP_IN_TEXT
1766 /* The `space' pseudo in the text section outputs nop insns rather than 0s,
1767 so we must output 0s explicitly in the text section. */
1768 if (ASM_NO_SKIP_IN_TEXT && (in_section->common.flags & SECTION_CODE) != 0)
1770 unsigned HOST_WIDE_INT i;
1771 for (i = 0; i < size; i++)
1772 assemble_integer (const0_rtx, 1, BITS_PER_UNIT, 1);
1774 else
1775 #endif
1776 if (size > 0)
1777 ASM_OUTPUT_SKIP (asm_out_file, size);
1780 /* Assemble an alignment pseudo op for an ALIGN-bit boundary. */
1782 void
1783 assemble_align (int align)
1785 if (align > BITS_PER_UNIT)
1787 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
1791 /* Assemble a string constant with the specified C string as contents. */
1793 void
1794 assemble_string (const char *p, int size)
1796 int pos = 0;
1797 int maximum = 2000;
1799 /* If the string is very long, split it up. */
1801 while (pos < size)
1803 int thissize = size - pos;
1804 if (thissize > maximum)
1805 thissize = maximum;
1807 ASM_OUTPUT_ASCII (asm_out_file, p, thissize);
1809 pos += thissize;
1810 p += thissize;
1815 /* A noswitch_section_callback for lcomm_section. */
1817 static bool
1818 emit_local (tree decl ATTRIBUTE_UNUSED,
1819 const char *name ATTRIBUTE_UNUSED,
1820 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1821 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1823 #if defined ASM_OUTPUT_ALIGNED_DECL_LOCAL
1824 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, decl, name,
1825 size, DECL_ALIGN (decl));
1826 return true;
1827 #elif defined ASM_OUTPUT_ALIGNED_LOCAL
1828 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, DECL_ALIGN (decl));
1829 return true;
1830 #else
1831 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
1832 return false;
1833 #endif
1836 /* A noswitch_section_callback for bss_noswitch_section. */
1838 #if defined ASM_OUTPUT_ALIGNED_BSS
1839 static bool
1840 emit_bss (tree decl ATTRIBUTE_UNUSED,
1841 const char *name ATTRIBUTE_UNUSED,
1842 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1843 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1845 #if defined ASM_OUTPUT_ALIGNED_BSS
1846 ASM_OUTPUT_ALIGNED_BSS (asm_out_file, decl, name, size,
1847 get_variable_align (decl));
1848 return true;
1849 #endif
1851 #endif
1853 /* A noswitch_section_callback for comm_section. */
1855 static bool
1856 emit_common (tree decl ATTRIBUTE_UNUSED,
1857 const char *name ATTRIBUTE_UNUSED,
1858 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1859 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1861 #if defined ASM_OUTPUT_ALIGNED_DECL_COMMON
1862 ASM_OUTPUT_ALIGNED_DECL_COMMON (asm_out_file, decl, name,
1863 size, get_variable_align (decl));
1864 return true;
1865 #elif defined ASM_OUTPUT_ALIGNED_COMMON
1866 ASM_OUTPUT_ALIGNED_COMMON (asm_out_file, name, size,
1867 get_variable_align (decl));
1868 return true;
1869 #else
1870 ASM_OUTPUT_COMMON (asm_out_file, name, size, rounded);
1871 return false;
1872 #endif
1875 /* A noswitch_section_callback for tls_comm_section. */
1877 static bool
1878 emit_tls_common (tree decl ATTRIBUTE_UNUSED,
1879 const char *name ATTRIBUTE_UNUSED,
1880 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1881 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1883 #ifdef ASM_OUTPUT_TLS_COMMON
1884 ASM_OUTPUT_TLS_COMMON (asm_out_file, decl, name, size);
1885 return true;
1886 #else
1887 sorry ("thread-local COMMON data not implemented");
1888 return true;
1889 #endif
1892 /* Assemble DECL given that it belongs in SECTION_NOSWITCH section SECT.
1893 NAME is the name of DECL's SYMBOL_REF. */
1895 static void
1896 assemble_noswitch_variable (tree decl, const char *name, section *sect,
1897 unsigned int align)
1899 unsigned HOST_WIDE_INT size, rounded;
1901 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
1902 rounded = size;
1904 if ((flag_sanitize & SANITIZE_ADDRESS) && asan_protect_global (decl))
1905 size += asan_red_zone_size (size);
1907 /* Don't allocate zero bytes of common,
1908 since that means "undefined external" in the linker. */
1909 if (size == 0)
1910 rounded = 1;
1912 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
1913 so that each uninitialized object starts on such a boundary. */
1914 rounded += (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1;
1915 rounded = (rounded / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
1916 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
1918 if (!sect->noswitch.callback (decl, name, size, rounded)
1919 && (unsigned HOST_WIDE_INT) (align / BITS_PER_UNIT) > rounded)
1920 error ("requested alignment for %q+D is greater than "
1921 "implemented alignment of %wu", decl, rounded);
1924 /* A subroutine of assemble_variable. Output the label and contents of
1925 DECL, whose address is a SYMBOL_REF with name NAME. DONT_OUTPUT_DATA
1926 is as for assemble_variable. */
1928 static void
1929 assemble_variable_contents (tree decl, const char *name,
1930 bool dont_output_data)
1932 /* Do any machine/system dependent processing of the object. */
1933 #ifdef ASM_DECLARE_OBJECT_NAME
1934 last_assemble_variable_decl = decl;
1935 ASM_DECLARE_OBJECT_NAME (asm_out_file, name, decl);
1936 #else
1937 /* Standard thing is just output label for the object. */
1938 ASM_OUTPUT_LABEL (asm_out_file, name);
1939 #endif /* ASM_DECLARE_OBJECT_NAME */
1941 if (!dont_output_data)
1943 if (DECL_INITIAL (decl)
1944 && DECL_INITIAL (decl) != error_mark_node
1945 && !initializer_zerop (DECL_INITIAL (decl)))
1946 /* Output the actual data. */
1947 output_constant (DECL_INITIAL (decl),
1948 tree_to_uhwi (DECL_SIZE_UNIT (decl)),
1949 get_variable_align (decl));
1950 else
1951 /* Leave space for it. */
1952 assemble_zeros (tree_to_uhwi (DECL_SIZE_UNIT (decl)));
1956 /* Assemble everything that is needed for a variable or function declaration.
1957 Not used for automatic variables, and not used for function definitions.
1958 Should not be called for variables of incomplete structure type.
1960 TOP_LEVEL is nonzero if this variable has file scope.
1961 AT_END is nonzero if this is the special handling, at end of compilation,
1962 to define things that have had only tentative definitions.
1963 DONT_OUTPUT_DATA if nonzero means don't actually output the
1964 initial value (that will be done by the caller). */
1966 void
1967 assemble_variable (tree decl, int top_level ATTRIBUTE_UNUSED,
1968 int at_end ATTRIBUTE_UNUSED, int dont_output_data)
1970 const char *name;
1971 rtx decl_rtl, symbol;
1972 section *sect;
1973 unsigned int align;
1974 bool asan_protected = false;
1976 /* This function is supposed to handle VARIABLES. Ensure we have one. */
1977 gcc_assert (TREE_CODE (decl) == VAR_DECL);
1979 /* Emulated TLS had better not get this far. */
1980 gcc_checking_assert (targetm.have_tls || !DECL_THREAD_LOCAL_P (decl));
1982 last_assemble_variable_decl = 0;
1984 /* Normally no need to say anything here for external references,
1985 since assemble_external is called by the language-specific code
1986 when a declaration is first seen. */
1988 if (DECL_EXTERNAL (decl))
1989 return;
1991 /* Do nothing for global register variables. */
1992 if (DECL_RTL_SET_P (decl) && REG_P (DECL_RTL (decl)))
1994 TREE_ASM_WRITTEN (decl) = 1;
1995 return;
1998 /* If type was incomplete when the variable was declared,
1999 see if it is complete now. */
2001 if (DECL_SIZE (decl) == 0)
2002 layout_decl (decl, 0);
2004 /* Still incomplete => don't allocate it; treat the tentative defn
2005 (which is what it must have been) as an `extern' reference. */
2007 if (!dont_output_data && DECL_SIZE (decl) == 0)
2009 error ("storage size of %q+D isn%'t known", decl);
2010 TREE_ASM_WRITTEN (decl) = 1;
2011 return;
2014 /* The first declaration of a variable that comes through this function
2015 decides whether it is global (in C, has external linkage)
2016 or local (in C, has internal linkage). So do nothing more
2017 if this function has already run. */
2019 if (TREE_ASM_WRITTEN (decl))
2020 return;
2022 /* Make sure targetm.encode_section_info is invoked before we set
2023 ASM_WRITTEN. */
2024 decl_rtl = DECL_RTL (decl);
2026 TREE_ASM_WRITTEN (decl) = 1;
2028 /* Do no output if -fsyntax-only. */
2029 if (flag_syntax_only)
2030 return;
2032 if (! dont_output_data
2033 && ! valid_constant_size_p (DECL_SIZE_UNIT (decl)))
2035 error ("size of variable %q+D is too large", decl);
2036 return;
2039 gcc_assert (MEM_P (decl_rtl));
2040 gcc_assert (GET_CODE (XEXP (decl_rtl, 0)) == SYMBOL_REF);
2041 symbol = XEXP (decl_rtl, 0);
2043 /* If this symbol belongs to the tree constant pool, output the constant
2044 if it hasn't already been written. */
2045 if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
2047 tree decl = SYMBOL_REF_DECL (symbol);
2048 if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
2049 output_constant_def_contents (symbol);
2050 return;
2053 app_disable ();
2055 name = XSTR (symbol, 0);
2056 if (TREE_PUBLIC (decl) && DECL_NAME (decl))
2057 notice_global_symbol (decl);
2059 /* Compute the alignment of this data. */
2061 align_variable (decl, dont_output_data);
2063 if ((flag_sanitize & SANITIZE_ADDRESS)
2064 && asan_protect_global (decl))
2066 asan_protected = true;
2067 DECL_ALIGN (decl) = MAX (DECL_ALIGN (decl),
2068 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
2071 set_mem_align (decl_rtl, DECL_ALIGN (decl));
2073 align = get_variable_align (decl);
2075 if (TREE_PUBLIC (decl))
2076 maybe_assemble_visibility (decl);
2078 if (DECL_PRESERVE_P (decl))
2079 targetm.asm_out.mark_decl_preserved (name);
2081 /* First make the assembler name(s) global if appropriate. */
2082 sect = get_variable_section (decl, false);
2083 if (TREE_PUBLIC (decl)
2084 && (sect->common.flags & SECTION_COMMON) == 0)
2085 globalize_decl (decl);
2087 /* Output any data that we will need to use the address of. */
2088 if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node)
2089 output_addressed_constants (DECL_INITIAL (decl));
2091 /* dbxout.c needs to know this. */
2092 if (sect && (sect->common.flags & SECTION_CODE) != 0)
2093 DECL_IN_TEXT_SECTION (decl) = 1;
2095 /* If the decl is part of an object_block, make sure that the decl
2096 has been positioned within its block, but do not write out its
2097 definition yet. output_object_blocks will do that later. */
2098 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
2100 gcc_assert (!dont_output_data);
2101 place_block_symbol (symbol);
2103 else if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
2104 assemble_noswitch_variable (decl, name, sect, align);
2105 else
2107 /* The following bit of code ensures that vtable_map
2108 variables are not only in the comdat section, but that
2109 each variable has its own unique comdat name. If this
2110 code is removed, the variables end up in the same section
2111 with a single comdat name.
2113 FIXME: resolve_unique_section needs to deal better with
2114 decls with both DECL_SECTION_NAME and DECL_ONE_ONLY. Once
2115 that is fixed, this if-else statement can be replaced with
2116 a single call to "switch_to_section (sect)". */
2117 if (sect->named.name
2118 && (strcmp (sect->named.name, ".vtable_map_vars") == 0))
2120 #if defined (OBJECT_FORMAT_ELF)
2121 targetm.asm_out.named_section (sect->named.name,
2122 sect->named.common.flags
2123 | SECTION_LINKONCE,
2124 DECL_NAME (decl));
2125 in_section = sect;
2126 #else
2127 switch_to_section (sect);
2128 #endif
2130 else
2131 switch_to_section (sect);
2132 if (align > BITS_PER_UNIT)
2133 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
2134 assemble_variable_contents (decl, name, dont_output_data);
2135 if (asan_protected)
2137 unsigned HOST_WIDE_INT int size
2138 = tree_to_uhwi (DECL_SIZE_UNIT (decl));
2139 assemble_zeros (asan_red_zone_size (size));
2145 /* Given a function declaration (FN_DECL), this function assembles the
2146 function into the .preinit_array section. */
2148 void
2149 assemble_vtv_preinit_initializer (tree fn_decl)
2151 section *sect;
2152 unsigned flags = SECTION_WRITE;
2153 rtx symbol = XEXP (DECL_RTL (fn_decl), 0);
2155 flags |= SECTION_NOTYPE;
2156 sect = get_section (".preinit_array", flags, fn_decl);
2157 switch_to_section (sect);
2158 assemble_addr_to_section (symbol, sect);
2161 /* Return 1 if type TYPE contains any pointers. */
2163 static int
2164 contains_pointers_p (tree type)
2166 switch (TREE_CODE (type))
2168 case POINTER_TYPE:
2169 case REFERENCE_TYPE:
2170 /* I'm not sure whether OFFSET_TYPE needs this treatment,
2171 so I'll play safe and return 1. */
2172 case OFFSET_TYPE:
2173 return 1;
2175 case RECORD_TYPE:
2176 case UNION_TYPE:
2177 case QUAL_UNION_TYPE:
2179 tree fields;
2180 /* For a type that has fields, see if the fields have pointers. */
2181 for (fields = TYPE_FIELDS (type); fields; fields = DECL_CHAIN (fields))
2182 if (TREE_CODE (fields) == FIELD_DECL
2183 && contains_pointers_p (TREE_TYPE (fields)))
2184 return 1;
2185 return 0;
2188 case ARRAY_TYPE:
2189 /* An array type contains pointers if its element type does. */
2190 return contains_pointers_p (TREE_TYPE (type));
2192 default:
2193 return 0;
2197 /* We delay assemble_external processing until
2198 the compilation unit is finalized. This is the best we can do for
2199 right now (i.e. stage 3 of GCC 4.0) - the right thing is to delay
2200 it all the way to final. See PR 17982 for further discussion. */
2201 static GTY(()) tree pending_assemble_externals;
2203 #ifdef ASM_OUTPUT_EXTERNAL
2204 /* Some targets delay some output to final using TARGET_ASM_FILE_END.
2205 As a result, assemble_external can be called after the list of externals
2206 is processed and the pointer set destroyed. */
2207 static bool pending_assemble_externals_processed;
2209 /* Avoid O(external_decls**2) lookups in the pending_assemble_externals
2210 TREE_LIST in assemble_external. */
2211 static struct pointer_set_t *pending_assemble_externals_set;
2213 /* True if DECL is a function decl for which no out-of-line copy exists.
2214 It is assumed that DECL's assembler name has been set. */
2216 static bool
2217 incorporeal_function_p (tree decl)
2219 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
2221 const char *name;
2223 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
2224 && (DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA
2225 || DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA_WITH_ALIGN))
2226 return true;
2228 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2229 /* Atomic or sync builtins which have survived this far will be
2230 resolved externally and therefore are not incorporeal. */
2231 if (strncmp (name, "__builtin_", 10) == 0)
2232 return true;
2234 return false;
2237 /* Actually do the tests to determine if this is necessary, and invoke
2238 ASM_OUTPUT_EXTERNAL. */
2239 static void
2240 assemble_external_real (tree decl)
2242 rtx rtl = DECL_RTL (decl);
2244 if (MEM_P (rtl) && GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF
2245 && !SYMBOL_REF_USED (XEXP (rtl, 0))
2246 && !incorporeal_function_p (decl))
2248 /* Some systems do require some output. */
2249 SYMBOL_REF_USED (XEXP (rtl, 0)) = 1;
2250 ASM_OUTPUT_EXTERNAL (asm_out_file, decl, XSTR (XEXP (rtl, 0), 0));
2253 #endif
2255 void
2256 process_pending_assemble_externals (void)
2258 #ifdef ASM_OUTPUT_EXTERNAL
2259 tree list;
2260 for (list = pending_assemble_externals; list; list = TREE_CHAIN (list))
2261 assemble_external_real (TREE_VALUE (list));
2263 pending_assemble_externals = 0;
2264 pending_assemble_externals_processed = true;
2265 pointer_set_destroy (pending_assemble_externals_set);
2266 #endif
2269 /* This TREE_LIST contains any weak symbol declarations waiting
2270 to be emitted. */
2271 static GTY(()) tree weak_decls;
2273 /* Output something to declare an external symbol to the assembler,
2274 and qualifiers such as weakness. (Most assemblers don't need
2275 extern declaration, so we normally output nothing.) Do nothing if
2276 DECL is not external. */
2278 void
2279 assemble_external (tree decl ATTRIBUTE_UNUSED)
2281 /* Make sure that the ASM_OUT_FILE is open.
2282 If it's not, we should not be calling this function. */
2283 gcc_assert (asm_out_file);
2285 /* In a perfect world, the following condition would be true.
2286 Sadly, the Java and Go front ends emit assembly *from the front end*,
2287 bypassing the call graph. See PR52739. Fix before GCC 4.8. */
2288 #if 0
2289 /* This function should only be called if we are expanding, or have
2290 expanded, to RTL.
2291 Ideally, only final.c would be calling this function, but it is
2292 not clear whether that would break things somehow. See PR 17982
2293 for further discussion. */
2294 gcc_assert (cgraph_state == CGRAPH_STATE_EXPANSION
2295 || cgraph_state == CGRAPH_STATE_FINISHED);
2296 #endif
2298 if (!DECL_P (decl) || !DECL_EXTERNAL (decl) || !TREE_PUBLIC (decl))
2299 return;
2301 /* We want to output annotation for weak and external symbols at
2302 very last to check if they are references or not. */
2304 if (TARGET_SUPPORTS_WEAK
2305 && DECL_WEAK (decl)
2306 /* TREE_STATIC is a weird and abused creature which is not
2307 generally the right test for whether an entity has been
2308 locally emitted, inlined or otherwise not-really-extern, but
2309 for declarations that can be weak, it happens to be
2310 match. */
2311 && !TREE_STATIC (decl)
2312 && lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
2313 && value_member (decl, weak_decls) == NULL_TREE)
2314 weak_decls = tree_cons (NULL, decl, weak_decls);
2316 #ifdef ASM_OUTPUT_EXTERNAL
2317 if (pending_assemble_externals_processed)
2319 assemble_external_real (decl);
2320 return;
2323 if (! pointer_set_insert (pending_assemble_externals_set, decl))
2324 pending_assemble_externals = tree_cons (NULL, decl,
2325 pending_assemble_externals);
2326 #endif
2329 /* Similar, for calling a library function FUN. */
2331 void
2332 assemble_external_libcall (rtx fun)
2334 /* Declare library function name external when first used, if nec. */
2335 if (! SYMBOL_REF_USED (fun))
2337 SYMBOL_REF_USED (fun) = 1;
2338 targetm.asm_out.external_libcall (fun);
2342 /* Assemble a label named NAME. */
2344 void
2345 assemble_label (FILE *file, const char *name)
2347 ASM_OUTPUT_LABEL (file, name);
2350 /* Set the symbol_referenced flag for ID. */
2351 void
2352 mark_referenced (tree id)
2354 TREE_SYMBOL_REFERENCED (id) = 1;
2357 /* Set the symbol_referenced flag for DECL and notify callgraph. */
2358 void
2359 mark_decl_referenced (tree decl)
2361 if (TREE_CODE (decl) == FUNCTION_DECL)
2363 /* Extern inline functions don't become needed when referenced.
2364 If we know a method will be emitted in other TU and no new
2365 functions can be marked reachable, just use the external
2366 definition. */
2367 struct cgraph_node *node = cgraph_get_create_node (decl);
2368 if (!DECL_EXTERNAL (decl)
2369 && !node->definition)
2370 cgraph_mark_force_output_node (node);
2372 else if (TREE_CODE (decl) == VAR_DECL)
2374 varpool_node *node = varpool_node_for_decl (decl);
2375 /* C++ frontend use mark_decl_references to force COMDAT variables
2376 to be output that might appear dead otherwise. */
2377 node->force_output = true;
2379 /* else do nothing - we can get various sorts of CST nodes here,
2380 which do not need to be marked. */
2384 /* Follow the IDENTIFIER_TRANSPARENT_ALIAS chain starting at *ALIAS
2385 until we find an identifier that is not itself a transparent alias.
2386 Modify the alias passed to it by reference (and all aliases on the
2387 way to the ultimate target), such that they do not have to be
2388 followed again, and return the ultimate target of the alias
2389 chain. */
2391 static inline tree
2392 ultimate_transparent_alias_target (tree *alias)
2394 tree target = *alias;
2396 if (IDENTIFIER_TRANSPARENT_ALIAS (target))
2398 gcc_assert (TREE_CHAIN (target));
2399 target = ultimate_transparent_alias_target (&TREE_CHAIN (target));
2400 gcc_assert (! IDENTIFIER_TRANSPARENT_ALIAS (target)
2401 && ! TREE_CHAIN (target));
2402 *alias = target;
2405 return target;
2408 /* Output to FILE (an assembly file) a reference to NAME. If NAME
2409 starts with a *, the rest of NAME is output verbatim. Otherwise
2410 NAME is transformed in a target-specific way (usually by the
2411 addition of an underscore). */
2413 void
2414 assemble_name_raw (FILE *file, const char *name)
2416 if (name[0] == '*')
2417 fputs (&name[1], file);
2418 else
2419 ASM_OUTPUT_LABELREF (file, name);
2422 /* Like assemble_name_raw, but should be used when NAME might refer to
2423 an entity that is also represented as a tree (like a function or
2424 variable). If NAME does refer to such an entity, that entity will
2425 be marked as referenced. */
2427 void
2428 assemble_name (FILE *file, const char *name)
2430 const char *real_name;
2431 tree id;
2433 real_name = targetm.strip_name_encoding (name);
2435 id = maybe_get_identifier (real_name);
2436 if (id)
2438 tree id_orig = id;
2440 mark_referenced (id);
2441 ultimate_transparent_alias_target (&id);
2442 if (id != id_orig)
2443 name = IDENTIFIER_POINTER (id);
2444 gcc_assert (! TREE_CHAIN (id));
2447 assemble_name_raw (file, name);
2450 /* Allocate SIZE bytes writable static space with a gensym name
2451 and return an RTX to refer to its address. */
2454 assemble_static_space (unsigned HOST_WIDE_INT size)
2456 char name[12];
2457 const char *namestring;
2458 rtx x;
2460 ASM_GENERATE_INTERNAL_LABEL (name, "LF", const_labelno);
2461 ++const_labelno;
2462 namestring = ggc_strdup (name);
2464 x = gen_rtx_SYMBOL_REF (Pmode, namestring);
2465 SYMBOL_REF_FLAGS (x) = SYMBOL_FLAG_LOCAL;
2467 #ifdef ASM_OUTPUT_ALIGNED_DECL_LOCAL
2468 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, NULL_TREE, name, size,
2469 BIGGEST_ALIGNMENT);
2470 #else
2471 #ifdef ASM_OUTPUT_ALIGNED_LOCAL
2472 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, BIGGEST_ALIGNMENT);
2473 #else
2475 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
2476 so that each uninitialized object starts on such a boundary. */
2477 /* Variable `rounded' might or might not be used in ASM_OUTPUT_LOCAL. */
2478 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED
2479 = ((size + (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1)
2480 / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
2481 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
2482 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
2484 #endif
2485 #endif
2486 return x;
2489 /* Assemble the static constant template for function entry trampolines.
2490 This is done at most once per compilation.
2491 Returns an RTX for the address of the template. */
2493 static GTY(()) rtx initial_trampoline;
2496 assemble_trampoline_template (void)
2498 char label[256];
2499 const char *name;
2500 int align;
2501 rtx symbol;
2503 gcc_assert (targetm.asm_out.trampoline_template != NULL);
2505 if (initial_trampoline)
2506 return initial_trampoline;
2508 /* By default, put trampoline templates in read-only data section. */
2510 #ifdef TRAMPOLINE_SECTION
2511 switch_to_section (TRAMPOLINE_SECTION);
2512 #else
2513 switch_to_section (readonly_data_section);
2514 #endif
2516 /* Write the assembler code to define one. */
2517 align = floor_log2 (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
2518 if (align > 0)
2519 ASM_OUTPUT_ALIGN (asm_out_file, align);
2521 targetm.asm_out.internal_label (asm_out_file, "LTRAMP", 0);
2522 targetm.asm_out.trampoline_template (asm_out_file);
2524 /* Record the rtl to refer to it. */
2525 ASM_GENERATE_INTERNAL_LABEL (label, "LTRAMP", 0);
2526 name = ggc_strdup (label);
2527 symbol = gen_rtx_SYMBOL_REF (Pmode, name);
2528 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_LOCAL;
2530 initial_trampoline = gen_const_mem (BLKmode, symbol);
2531 set_mem_align (initial_trampoline, TRAMPOLINE_ALIGNMENT);
2532 set_mem_size (initial_trampoline, TRAMPOLINE_SIZE);
2534 return initial_trampoline;
2537 /* A and B are either alignments or offsets. Return the minimum alignment
2538 that may be assumed after adding the two together. */
2540 static inline unsigned
2541 min_align (unsigned int a, unsigned int b)
2543 return (a | b) & -(a | b);
2546 /* Return the assembler directive for creating a given kind of integer
2547 object. SIZE is the number of bytes in the object and ALIGNED_P
2548 indicates whether it is known to be aligned. Return NULL if the
2549 assembly dialect has no such directive.
2551 The returned string should be printed at the start of a new line and
2552 be followed immediately by the object's initial value. */
2554 const char *
2555 integer_asm_op (int size, int aligned_p)
2557 struct asm_int_op *ops;
2559 if (aligned_p)
2560 ops = &targetm.asm_out.aligned_op;
2561 else
2562 ops = &targetm.asm_out.unaligned_op;
2564 switch (size)
2566 case 1:
2567 return targetm.asm_out.byte_op;
2568 case 2:
2569 return ops->hi;
2570 case 4:
2571 return ops->si;
2572 case 8:
2573 return ops->di;
2574 case 16:
2575 return ops->ti;
2576 default:
2577 return NULL;
2581 /* Use directive OP to assemble an integer object X. Print OP at the
2582 start of the line, followed immediately by the value of X. */
2584 void
2585 assemble_integer_with_op (const char *op, rtx x)
2587 fputs (op, asm_out_file);
2588 output_addr_const (asm_out_file, x);
2589 fputc ('\n', asm_out_file);
2592 /* The default implementation of the asm_out.integer target hook. */
2594 bool
2595 default_assemble_integer (rtx x ATTRIBUTE_UNUSED,
2596 unsigned int size ATTRIBUTE_UNUSED,
2597 int aligned_p ATTRIBUTE_UNUSED)
2599 const char *op = integer_asm_op (size, aligned_p);
2600 /* Avoid GAS bugs for large values. Specifically negative values whose
2601 absolute value fits in a bfd_vma, but not in a bfd_signed_vma. */
2602 if (size > UNITS_PER_WORD && size > POINTER_SIZE / BITS_PER_UNIT)
2603 return false;
2604 return op && (assemble_integer_with_op (op, x), true);
2607 /* Assemble the integer constant X into an object of SIZE bytes. ALIGN is
2608 the alignment of the integer in bits. Return 1 if we were able to output
2609 the constant, otherwise 0. We must be able to output the constant,
2610 if FORCE is nonzero. */
2612 bool
2613 assemble_integer (rtx x, unsigned int size, unsigned int align, int force)
2615 int aligned_p;
2617 aligned_p = (align >= MIN (size * BITS_PER_UNIT, BIGGEST_ALIGNMENT));
2619 /* See if the target hook can handle this kind of object. */
2620 if (targetm.asm_out.integer (x, size, aligned_p))
2621 return true;
2623 /* If the object is a multi-byte one, try splitting it up. Split
2624 it into words it if is multi-word, otherwise split it into bytes. */
2625 if (size > 1)
2627 enum machine_mode omode, imode;
2628 unsigned int subalign;
2629 unsigned int subsize, i;
2630 enum mode_class mclass;
2632 subsize = size > UNITS_PER_WORD? UNITS_PER_WORD : 1;
2633 subalign = MIN (align, subsize * BITS_PER_UNIT);
2634 if (GET_CODE (x) == CONST_FIXED)
2635 mclass = GET_MODE_CLASS (GET_MODE (x));
2636 else
2637 mclass = MODE_INT;
2639 omode = mode_for_size (subsize * BITS_PER_UNIT, mclass, 0);
2640 imode = mode_for_size (size * BITS_PER_UNIT, mclass, 0);
2642 for (i = 0; i < size; i += subsize)
2644 rtx partial = simplify_subreg (omode, x, imode, i);
2645 if (!partial || !assemble_integer (partial, subsize, subalign, 0))
2646 break;
2648 if (i == size)
2649 return true;
2651 /* If we've printed some of it, but not all of it, there's no going
2652 back now. */
2653 gcc_assert (!i);
2656 gcc_assert (!force);
2658 return false;
2661 void
2662 assemble_real (REAL_VALUE_TYPE d, enum machine_mode mode, unsigned int align)
2664 long data[4] = {0, 0, 0, 0};
2665 int i;
2666 int bitsize, nelts, nunits, units_per;
2668 /* This is hairy. We have a quantity of known size. real_to_target
2669 will put it into an array of *host* longs, 32 bits per element
2670 (even if long is more than 32 bits). We need to determine the
2671 number of array elements that are occupied (nelts) and the number
2672 of *target* min-addressable units that will be occupied in the
2673 object file (nunits). We cannot assume that 32 divides the
2674 mode's bitsize (size * BITS_PER_UNIT) evenly.
2676 size * BITS_PER_UNIT is used here to make sure that padding bits
2677 (which might appear at either end of the value; real_to_target
2678 will include the padding bits in its output array) are included. */
2680 nunits = GET_MODE_SIZE (mode);
2681 bitsize = nunits * BITS_PER_UNIT;
2682 nelts = CEIL (bitsize, 32);
2683 units_per = 32 / BITS_PER_UNIT;
2685 real_to_target (data, &d, mode);
2687 /* Put out the first word with the specified alignment. */
2688 assemble_integer (GEN_INT (data[0]), MIN (nunits, units_per), align, 1);
2689 nunits -= units_per;
2691 /* Subsequent words need only 32-bit alignment. */
2692 align = min_align (align, 32);
2694 for (i = 1; i < nelts; i++)
2696 assemble_integer (GEN_INT (data[i]), MIN (nunits, units_per), align, 1);
2697 nunits -= units_per;
2701 /* Given an expression EXP with a constant value,
2702 reduce it to the sum of an assembler symbol and an integer.
2703 Store them both in the structure *VALUE.
2704 EXP must be reducible. */
2706 struct addr_const {
2707 rtx base;
2708 HOST_WIDE_INT offset;
2711 static void
2712 decode_addr_const (tree exp, struct addr_const *value)
2714 tree target = TREE_OPERAND (exp, 0);
2715 int offset = 0;
2716 rtx x;
2718 while (1)
2720 if (TREE_CODE (target) == COMPONENT_REF
2721 && tree_fits_shwi_p (byte_position (TREE_OPERAND (target, 1))))
2723 offset += int_byte_position (TREE_OPERAND (target, 1));
2724 target = TREE_OPERAND (target, 0);
2726 else if (TREE_CODE (target) == ARRAY_REF
2727 || TREE_CODE (target) == ARRAY_RANGE_REF)
2729 offset += (tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (target)))
2730 * tree_to_shwi (TREE_OPERAND (target, 1)));
2731 target = TREE_OPERAND (target, 0);
2733 else if (TREE_CODE (target) == MEM_REF
2734 && TREE_CODE (TREE_OPERAND (target, 0)) == ADDR_EXPR)
2736 offset += mem_ref_offset (target).low;
2737 target = TREE_OPERAND (TREE_OPERAND (target, 0), 0);
2739 else if (TREE_CODE (target) == INDIRECT_REF
2740 && TREE_CODE (TREE_OPERAND (target, 0)) == NOP_EXPR
2741 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (target, 0), 0))
2742 == ADDR_EXPR)
2743 target = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (target, 0), 0), 0);
2744 else
2745 break;
2748 switch (TREE_CODE (target))
2750 case VAR_DECL:
2751 case FUNCTION_DECL:
2752 x = DECL_RTL (target);
2753 break;
2755 case LABEL_DECL:
2756 x = gen_rtx_MEM (FUNCTION_MODE,
2757 gen_rtx_LABEL_REF (Pmode, force_label_rtx (target)));
2758 break;
2760 case REAL_CST:
2761 case FIXED_CST:
2762 case STRING_CST:
2763 case COMPLEX_CST:
2764 case CONSTRUCTOR:
2765 case INTEGER_CST:
2766 x = output_constant_def (target, 1);
2767 break;
2769 default:
2770 gcc_unreachable ();
2773 gcc_assert (MEM_P (x));
2774 x = XEXP (x, 0);
2776 value->base = x;
2777 value->offset = offset;
2781 static GTY((param_is (struct constant_descriptor_tree)))
2782 htab_t const_desc_htab;
2784 static void maybe_output_constant_def_contents (struct constant_descriptor_tree *, int);
2786 /* Constant pool accessor function. */
2788 htab_t
2789 constant_pool_htab (void)
2791 return const_desc_htab;
2794 /* Compute a hash code for a constant expression. */
2796 static hashval_t
2797 const_desc_hash (const void *ptr)
2799 return ((const struct constant_descriptor_tree *)ptr)->hash;
2802 static hashval_t
2803 const_hash_1 (const tree exp)
2805 const char *p;
2806 hashval_t hi;
2807 int len, i;
2808 enum tree_code code = TREE_CODE (exp);
2810 /* Either set P and LEN to the address and len of something to hash and
2811 exit the switch or return a value. */
2813 switch (code)
2815 case INTEGER_CST:
2816 p = (char *) &TREE_INT_CST (exp);
2817 len = sizeof TREE_INT_CST (exp);
2818 break;
2820 case REAL_CST:
2821 return real_hash (TREE_REAL_CST_PTR (exp));
2823 case FIXED_CST:
2824 return fixed_hash (TREE_FIXED_CST_PTR (exp));
2826 case STRING_CST:
2827 p = TREE_STRING_POINTER (exp);
2828 len = TREE_STRING_LENGTH (exp);
2829 break;
2831 case COMPLEX_CST:
2832 return (const_hash_1 (TREE_REALPART (exp)) * 5
2833 + const_hash_1 (TREE_IMAGPART (exp)));
2835 case VECTOR_CST:
2837 unsigned i;
2839 hi = 7 + VECTOR_CST_NELTS (exp);
2841 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
2842 hi = hi * 563 + const_hash_1 (VECTOR_CST_ELT (exp, i));
2844 return hi;
2847 case CONSTRUCTOR:
2849 unsigned HOST_WIDE_INT idx;
2850 tree value;
2852 hi = 5 + int_size_in_bytes (TREE_TYPE (exp));
2854 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
2855 if (value)
2856 hi = hi * 603 + const_hash_1 (value);
2858 return hi;
2861 case ADDR_EXPR:
2862 case FDESC_EXPR:
2864 struct addr_const value;
2866 decode_addr_const (exp, &value);
2867 switch (GET_CODE (value.base))
2869 case SYMBOL_REF:
2870 /* Don't hash the address of the SYMBOL_REF;
2871 only use the offset and the symbol name. */
2872 hi = value.offset;
2873 p = XSTR (value.base, 0);
2874 for (i = 0; p[i] != 0; i++)
2875 hi = ((hi * 613) + (unsigned) (p[i]));
2876 break;
2878 case LABEL_REF:
2879 hi = value.offset + CODE_LABEL_NUMBER (XEXP (value.base, 0)) * 13;
2880 break;
2882 default:
2883 gcc_unreachable ();
2886 return hi;
2888 case PLUS_EXPR:
2889 case POINTER_PLUS_EXPR:
2890 case MINUS_EXPR:
2891 return (const_hash_1 (TREE_OPERAND (exp, 0)) * 9
2892 + const_hash_1 (TREE_OPERAND (exp, 1)));
2894 CASE_CONVERT:
2895 return const_hash_1 (TREE_OPERAND (exp, 0)) * 7 + 2;
2897 default:
2898 /* A language specific constant. Just hash the code. */
2899 return code;
2902 /* Compute hashing function. */
2903 hi = len;
2904 for (i = 0; i < len; i++)
2905 hi = ((hi * 613) + (unsigned) (p[i]));
2907 return hi;
2910 /* Wrapper of compare_constant, for the htab interface. */
2911 static int
2912 const_desc_eq (const void *p1, const void *p2)
2914 const struct constant_descriptor_tree *const c1
2915 = (const struct constant_descriptor_tree *) p1;
2916 const struct constant_descriptor_tree *const c2
2917 = (const struct constant_descriptor_tree *) p2;
2918 if (c1->hash != c2->hash)
2919 return 0;
2920 return compare_constant (c1->value, c2->value);
2923 /* Compare t1 and t2, and return 1 only if they are known to result in
2924 the same bit pattern on output. */
2926 static int
2927 compare_constant (const tree t1, const tree t2)
2929 enum tree_code typecode;
2931 if (t1 == NULL_TREE)
2932 return t2 == NULL_TREE;
2933 if (t2 == NULL_TREE)
2934 return 0;
2936 if (TREE_CODE (t1) != TREE_CODE (t2))
2937 return 0;
2939 switch (TREE_CODE (t1))
2941 case INTEGER_CST:
2942 /* Integer constants are the same only if the same width of type. */
2943 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
2944 return 0;
2945 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
2946 return 0;
2947 return tree_int_cst_equal (t1, t2);
2949 case REAL_CST:
2950 /* Real constants are the same only if the same width of type. */
2951 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
2952 return 0;
2954 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
2956 case FIXED_CST:
2957 /* Fixed constants are the same only if the same width of type. */
2958 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
2959 return 0;
2961 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
2963 case STRING_CST:
2964 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
2965 return 0;
2967 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
2968 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
2969 TREE_STRING_LENGTH (t1)));
2971 case COMPLEX_CST:
2972 return (compare_constant (TREE_REALPART (t1), TREE_REALPART (t2))
2973 && compare_constant (TREE_IMAGPART (t1), TREE_IMAGPART (t2)));
2975 case VECTOR_CST:
2977 unsigned i;
2979 if (VECTOR_CST_NELTS (t1) != VECTOR_CST_NELTS (t2))
2980 return 0;
2982 for (i = 0; i < VECTOR_CST_NELTS (t1); ++i)
2983 if (!compare_constant (VECTOR_CST_ELT (t1, i),
2984 VECTOR_CST_ELT (t2, i)))
2985 return 0;
2987 return 1;
2990 case CONSTRUCTOR:
2992 vec<constructor_elt, va_gc> *v1, *v2;
2993 unsigned HOST_WIDE_INT idx;
2995 typecode = TREE_CODE (TREE_TYPE (t1));
2996 if (typecode != TREE_CODE (TREE_TYPE (t2)))
2997 return 0;
2999 if (typecode == ARRAY_TYPE)
3001 HOST_WIDE_INT size_1 = int_size_in_bytes (TREE_TYPE (t1));
3002 /* For arrays, check that the sizes all match. */
3003 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))
3004 || size_1 == -1
3005 || size_1 != int_size_in_bytes (TREE_TYPE (t2)))
3006 return 0;
3008 else
3010 /* For record and union constructors, require exact type
3011 equality. */
3012 if (TREE_TYPE (t1) != TREE_TYPE (t2))
3013 return 0;
3016 v1 = CONSTRUCTOR_ELTS (t1);
3017 v2 = CONSTRUCTOR_ELTS (t2);
3018 if (vec_safe_length (v1) != vec_safe_length (v2))
3019 return 0;
3021 for (idx = 0; idx < vec_safe_length (v1); ++idx)
3023 constructor_elt *c1 = &(*v1)[idx];
3024 constructor_elt *c2 = &(*v2)[idx];
3026 /* Check that each value is the same... */
3027 if (!compare_constant (c1->value, c2->value))
3028 return 0;
3029 /* ... and that they apply to the same fields! */
3030 if (typecode == ARRAY_TYPE)
3032 if (!compare_constant (c1->index, c2->index))
3033 return 0;
3035 else
3037 if (c1->index != c2->index)
3038 return 0;
3042 return 1;
3045 case ADDR_EXPR:
3046 case FDESC_EXPR:
3048 struct addr_const value1, value2;
3049 enum rtx_code code;
3050 int ret;
3052 decode_addr_const (t1, &value1);
3053 decode_addr_const (t2, &value2);
3055 if (value1.offset != value2.offset)
3056 return 0;
3058 code = GET_CODE (value1.base);
3059 if (code != GET_CODE (value2.base))
3060 return 0;
3062 switch (code)
3064 case SYMBOL_REF:
3065 ret = (strcmp (XSTR (value1.base, 0), XSTR (value2.base, 0)) == 0);
3066 break;
3068 case LABEL_REF:
3069 ret = (CODE_LABEL_NUMBER (XEXP (value1.base, 0))
3070 == CODE_LABEL_NUMBER (XEXP (value2.base, 0)));
3071 break;
3073 default:
3074 gcc_unreachable ();
3076 return ret;
3079 case PLUS_EXPR:
3080 case POINTER_PLUS_EXPR:
3081 case MINUS_EXPR:
3082 case RANGE_EXPR:
3083 return (compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
3084 && compare_constant (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
3086 CASE_CONVERT:
3087 case VIEW_CONVERT_EXPR:
3088 return compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3090 default:
3091 return 0;
3094 gcc_unreachable ();
3097 /* Make a copy of the whole tree structure for a constant. This
3098 handles the same types of nodes that compare_constant handles. */
3100 static tree
3101 copy_constant (tree exp)
3103 switch (TREE_CODE (exp))
3105 case ADDR_EXPR:
3106 /* For ADDR_EXPR, we do not want to copy the decl whose address
3107 is requested. We do want to copy constants though. */
3108 if (CONSTANT_CLASS_P (TREE_OPERAND (exp, 0)))
3109 return build1 (TREE_CODE (exp), TREE_TYPE (exp),
3110 copy_constant (TREE_OPERAND (exp, 0)));
3111 else
3112 return copy_node (exp);
3114 case INTEGER_CST:
3115 case REAL_CST:
3116 case FIXED_CST:
3117 case STRING_CST:
3118 return copy_node (exp);
3120 case COMPLEX_CST:
3121 return build_complex (TREE_TYPE (exp),
3122 copy_constant (TREE_REALPART (exp)),
3123 copy_constant (TREE_IMAGPART (exp)));
3125 case PLUS_EXPR:
3126 case POINTER_PLUS_EXPR:
3127 case MINUS_EXPR:
3128 return build2 (TREE_CODE (exp), TREE_TYPE (exp),
3129 copy_constant (TREE_OPERAND (exp, 0)),
3130 copy_constant (TREE_OPERAND (exp, 1)));
3132 CASE_CONVERT:
3133 case VIEW_CONVERT_EXPR:
3134 return build1 (TREE_CODE (exp), TREE_TYPE (exp),
3135 copy_constant (TREE_OPERAND (exp, 0)));
3137 case VECTOR_CST:
3138 return build_vector (TREE_TYPE (exp), VECTOR_CST_ELTS (exp));
3140 case CONSTRUCTOR:
3142 tree copy = copy_node (exp);
3143 vec<constructor_elt, va_gc> *v;
3144 unsigned HOST_WIDE_INT idx;
3145 tree purpose, value;
3147 vec_alloc (v, vec_safe_length (CONSTRUCTOR_ELTS (exp)));
3148 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, purpose, value)
3150 constructor_elt ce = {purpose, copy_constant (value)};
3151 v->quick_push (ce);
3153 CONSTRUCTOR_ELTS (copy) = v;
3154 return copy;
3157 default:
3158 gcc_unreachable ();
3162 /* Return the section into which constant EXP should be placed. */
3164 static section *
3165 get_constant_section (tree exp, unsigned int align)
3167 return targetm.asm_out.select_section (exp,
3168 compute_reloc_for_constant (exp),
3169 align);
3172 /* Return the size of constant EXP in bytes. */
3174 static HOST_WIDE_INT
3175 get_constant_size (tree exp)
3177 HOST_WIDE_INT size;
3179 size = int_size_in_bytes (TREE_TYPE (exp));
3180 if (TREE_CODE (exp) == STRING_CST)
3181 size = MAX (TREE_STRING_LENGTH (exp), size);
3182 return size;
3185 /* Subroutine of output_constant_def:
3186 No constant equal to EXP is known to have been output.
3187 Make a constant descriptor to enter EXP in the hash table.
3188 Assign the label number and construct RTL to refer to the
3189 constant's location in memory.
3190 Caller is responsible for updating the hash table. */
3192 static struct constant_descriptor_tree *
3193 build_constant_desc (tree exp)
3195 struct constant_descriptor_tree *desc;
3196 rtx symbol, rtl;
3197 char label[256];
3198 int labelno;
3199 tree decl;
3201 desc = ggc_alloc_constant_descriptor_tree ();
3202 desc->value = copy_constant (exp);
3204 /* Create a string containing the label name, in LABEL. */
3205 labelno = const_labelno++;
3206 ASM_GENERATE_INTERNAL_LABEL (label, "LC", labelno);
3208 /* Construct the VAR_DECL associated with the constant. */
3209 decl = build_decl (UNKNOWN_LOCATION, VAR_DECL, get_identifier (label),
3210 TREE_TYPE (exp));
3211 DECL_ARTIFICIAL (decl) = 1;
3212 DECL_IGNORED_P (decl) = 1;
3213 TREE_READONLY (decl) = 1;
3214 TREE_STATIC (decl) = 1;
3215 TREE_ADDRESSABLE (decl) = 1;
3216 /* We don't set the RTL yet as this would cause varpool to assume that the
3217 variable is referenced. Moreover, it would just be dropped in LTO mode.
3218 Instead we set the flag that will be recognized in make_decl_rtl. */
3219 DECL_IN_CONSTANT_POOL (decl) = 1;
3220 DECL_INITIAL (decl) = desc->value;
3221 /* ??? CONSTANT_ALIGNMENT hasn't been updated for vector types on most
3222 architectures so use DATA_ALIGNMENT as well, except for strings. */
3223 if (TREE_CODE (exp) == STRING_CST)
3225 #ifdef CONSTANT_ALIGNMENT
3226 DECL_ALIGN (decl) = CONSTANT_ALIGNMENT (exp, DECL_ALIGN (decl));
3227 #endif
3229 else
3230 align_variable (decl, 0);
3232 /* Now construct the SYMBOL_REF and the MEM. */
3233 if (use_object_blocks_p ())
3235 section *sect = get_constant_section (exp, DECL_ALIGN (decl));
3236 symbol = create_block_symbol (ggc_strdup (label),
3237 get_block_for_section (sect), -1);
3239 else
3240 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3241 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3242 SET_SYMBOL_REF_DECL (symbol, decl);
3243 TREE_CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3245 rtl = gen_const_mem (TYPE_MODE (TREE_TYPE (exp)), symbol);
3246 set_mem_attributes (rtl, exp, 1);
3247 set_mem_alias_set (rtl, 0);
3249 /* We cannot share RTX'es in pool entries.
3250 Mark this piece of RTL as required for unsharing. */
3251 RTX_FLAG (rtl, used) = 1;
3253 /* Set flags or add text to the name to record information, such as
3254 that it is a local symbol. If the name is changed, the macro
3255 ASM_OUTPUT_LABELREF will have to know how to strip this
3256 information. This call might invalidate our local variable
3257 SYMBOL; we can't use it afterward. */
3258 targetm.encode_section_info (exp, rtl, true);
3260 desc->rtl = rtl;
3262 return desc;
3265 /* Return an rtx representing a reference to constant data in memory
3266 for the constant expression EXP.
3268 If assembler code for such a constant has already been output,
3269 return an rtx to refer to it.
3270 Otherwise, output such a constant in memory
3271 and generate an rtx for it.
3273 If DEFER is nonzero, this constant can be deferred and output only
3274 if referenced in the function after all optimizations.
3276 `const_desc_table' records which constants already have label strings. */
3279 output_constant_def (tree exp, int defer)
3281 struct constant_descriptor_tree *desc;
3282 struct constant_descriptor_tree key;
3283 void **loc;
3285 /* Look up EXP in the table of constant descriptors. If we didn't find
3286 it, create a new one. */
3287 key.value = exp;
3288 key.hash = const_hash_1 (exp);
3289 loc = htab_find_slot_with_hash (const_desc_htab, &key, key.hash, INSERT);
3291 desc = (struct constant_descriptor_tree *) *loc;
3292 if (desc == 0)
3294 desc = build_constant_desc (exp);
3295 desc->hash = key.hash;
3296 *loc = desc;
3299 maybe_output_constant_def_contents (desc, defer);
3300 return desc->rtl;
3303 /* Subroutine of output_constant_def: Decide whether or not we need to
3304 output the constant DESC now, and if so, do it. */
3305 static void
3306 maybe_output_constant_def_contents (struct constant_descriptor_tree *desc,
3307 int defer)
3309 rtx symbol = XEXP (desc->rtl, 0);
3310 tree exp = desc->value;
3312 if (flag_syntax_only)
3313 return;
3315 if (TREE_ASM_WRITTEN (exp))
3316 /* Already output; don't do it again. */
3317 return;
3319 /* We can always defer constants as long as the context allows
3320 doing so. */
3321 if (defer)
3323 /* Increment n_deferred_constants if it exists. It needs to be at
3324 least as large as the number of constants actually referred to
3325 by the function. If it's too small we'll stop looking too early
3326 and fail to emit constants; if it's too large we'll only look
3327 through the entire function when we could have stopped earlier. */
3328 if (cfun)
3329 n_deferred_constants++;
3330 return;
3333 output_constant_def_contents (symbol);
3336 /* Subroutine of output_constant_def_contents. Output the definition
3337 of constant EXP, which is pointed to by label LABEL. ALIGN is the
3338 constant's alignment in bits. */
3340 static void
3341 assemble_constant_contents (tree exp, const char *label, unsigned int align)
3343 HOST_WIDE_INT size;
3345 size = get_constant_size (exp);
3347 /* Do any machine/system dependent processing of the constant. */
3348 targetm.asm_out.declare_constant_name (asm_out_file, label, exp, size);
3350 /* Output the value of EXP. */
3351 output_constant (exp, size, align);
3354 /* We must output the constant data referred to by SYMBOL; do so. */
3356 static void
3357 output_constant_def_contents (rtx symbol)
3359 tree decl = SYMBOL_REF_DECL (symbol);
3360 tree exp = DECL_INITIAL (decl);
3361 unsigned int align;
3362 bool asan_protected = false;
3364 /* Make sure any other constants whose addresses appear in EXP
3365 are assigned label numbers. */
3366 output_addressed_constants (exp);
3368 /* We are no longer deferring this constant. */
3369 TREE_ASM_WRITTEN (decl) = TREE_ASM_WRITTEN (exp) = 1;
3371 if ((flag_sanitize & SANITIZE_ADDRESS)
3372 && TREE_CODE (exp) == STRING_CST
3373 && asan_protect_global (exp))
3375 asan_protected = true;
3376 DECL_ALIGN (decl) = MAX (DECL_ALIGN (decl),
3377 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
3380 /* If the constant is part of an object block, make sure that the
3381 decl has been positioned within its block, but do not write out
3382 its definition yet. output_object_blocks will do that later. */
3383 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
3384 place_block_symbol (symbol);
3385 else
3387 align = DECL_ALIGN (decl);
3388 switch_to_section (get_constant_section (exp, align));
3389 if (align > BITS_PER_UNIT)
3390 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
3391 assemble_constant_contents (exp, XSTR (symbol, 0), align);
3392 if (asan_protected)
3394 HOST_WIDE_INT size = get_constant_size (exp);
3395 assemble_zeros (asan_red_zone_size (size));
3400 /* Look up EXP in the table of constant descriptors. Return the rtl
3401 if it has been emitted, else null. */
3404 lookup_constant_def (tree exp)
3406 struct constant_descriptor_tree *desc;
3407 struct constant_descriptor_tree key;
3409 key.value = exp;
3410 key.hash = const_hash_1 (exp);
3411 desc = (struct constant_descriptor_tree *)
3412 htab_find_with_hash (const_desc_htab, &key, key.hash);
3414 return (desc ? desc->rtl : NULL_RTX);
3417 /* Return a tree representing a reference to constant data in memory
3418 for the constant expression EXP.
3420 This is the counterpart of output_constant_def at the Tree level. */
3422 tree
3423 tree_output_constant_def (tree exp)
3425 struct constant_descriptor_tree *desc, key;
3426 void **loc;
3427 tree decl;
3429 /* Look up EXP in the table of constant descriptors. If we didn't find
3430 it, create a new one. */
3431 key.value = exp;
3432 key.hash = const_hash_1 (exp);
3433 loc = htab_find_slot_with_hash (const_desc_htab, &key, key.hash, INSERT);
3435 desc = (struct constant_descriptor_tree *) *loc;
3436 if (desc == 0)
3438 desc = build_constant_desc (exp);
3439 desc->hash = key.hash;
3440 *loc = desc;
3443 decl = SYMBOL_REF_DECL (XEXP (desc->rtl, 0));
3444 varpool_finalize_decl (decl);
3445 return decl;
3448 /* Used in the hash tables to avoid outputting the same constant
3449 twice. Unlike 'struct constant_descriptor_tree', RTX constants
3450 are output once per function, not once per file. */
3451 /* ??? Only a few targets need per-function constant pools. Most
3452 can use one per-file pool. Should add a targetm bit to tell the
3453 difference. */
3455 struct GTY(()) rtx_constant_pool {
3456 /* Pointers to first and last constant in pool, as ordered by offset. */
3457 struct constant_descriptor_rtx *first;
3458 struct constant_descriptor_rtx *last;
3460 /* Hash facility for making memory-constants from constant rtl-expressions.
3461 It is used on RISC machines where immediate integer arguments and
3462 constant addresses are restricted so that such constants must be stored
3463 in memory. */
3464 htab_t GTY((param_is (struct constant_descriptor_rtx))) const_rtx_htab;
3466 /* Current offset in constant pool (does not include any
3467 machine-specific header). */
3468 HOST_WIDE_INT offset;
3471 struct GTY((chain_next ("%h.next"))) constant_descriptor_rtx {
3472 struct constant_descriptor_rtx *next;
3473 rtx mem;
3474 rtx sym;
3475 rtx constant;
3476 HOST_WIDE_INT offset;
3477 hashval_t hash;
3478 enum machine_mode mode;
3479 unsigned int align;
3480 int labelno;
3481 int mark;
3484 /* Hash and compare functions for const_rtx_htab. */
3486 static hashval_t
3487 const_desc_rtx_hash (const void *ptr)
3489 const struct constant_descriptor_rtx *const desc
3490 = (const struct constant_descriptor_rtx *) ptr;
3491 return desc->hash;
3494 static int
3495 const_desc_rtx_eq (const void *a, const void *b)
3497 const struct constant_descriptor_rtx *const x
3498 = (const struct constant_descriptor_rtx *) a;
3499 const struct constant_descriptor_rtx *const y
3500 = (const struct constant_descriptor_rtx *) b;
3502 if (x->mode != y->mode)
3503 return 0;
3504 return rtx_equal_p (x->constant, y->constant);
3507 /* This is the worker function for const_rtx_hash, called via for_each_rtx. */
3509 static int
3510 const_rtx_hash_1 (rtx *xp, void *data)
3512 unsigned HOST_WIDE_INT hwi;
3513 enum machine_mode mode;
3514 enum rtx_code code;
3515 hashval_t h, *hp;
3516 rtx x;
3518 x = *xp;
3519 code = GET_CODE (x);
3520 mode = GET_MODE (x);
3521 h = (hashval_t) code * 1048573 + mode;
3523 switch (code)
3525 case CONST_INT:
3526 hwi = INTVAL (x);
3527 fold_hwi:
3529 int shift = sizeof (hashval_t) * CHAR_BIT;
3530 const int n = sizeof (HOST_WIDE_INT) / sizeof (hashval_t);
3531 int i;
3533 h ^= (hashval_t) hwi;
3534 for (i = 1; i < n; ++i)
3536 hwi >>= shift;
3537 h ^= (hashval_t) hwi;
3540 break;
3542 case CONST_DOUBLE:
3543 if (mode == VOIDmode)
3545 hwi = CONST_DOUBLE_LOW (x) ^ CONST_DOUBLE_HIGH (x);
3546 goto fold_hwi;
3548 else
3549 h ^= real_hash (CONST_DOUBLE_REAL_VALUE (x));
3550 break;
3552 case CONST_FIXED:
3553 h ^= fixed_hash (CONST_FIXED_VALUE (x));
3554 break;
3556 case CONST_VECTOR:
3558 int i;
3559 for (i = XVECLEN (x, 0); i-- > 0; )
3560 h = h * 251 + const_rtx_hash_1 (&XVECEXP (x, 0, i), data);
3562 break;
3564 case SYMBOL_REF:
3565 h ^= htab_hash_string (XSTR (x, 0));
3566 break;
3568 case LABEL_REF:
3569 h = h * 251 + CODE_LABEL_NUMBER (XEXP (x, 0));
3570 break;
3572 case UNSPEC:
3573 case UNSPEC_VOLATILE:
3574 h = h * 251 + XINT (x, 1);
3575 break;
3577 default:
3578 break;
3581 hp = (hashval_t *) data;
3582 *hp = *hp * 509 + h;
3583 return 0;
3586 /* Compute a hash value for X, which should be a constant. */
3588 static hashval_t
3589 const_rtx_hash (rtx x)
3591 hashval_t h = 0;
3592 for_each_rtx (&x, const_rtx_hash_1, &h);
3593 return h;
3597 /* Create and return a new rtx constant pool. */
3599 static struct rtx_constant_pool *
3600 create_constant_pool (void)
3602 struct rtx_constant_pool *pool;
3604 pool = ggc_alloc_rtx_constant_pool ();
3605 pool->const_rtx_htab = htab_create_ggc (31, const_desc_rtx_hash,
3606 const_desc_rtx_eq, NULL);
3607 pool->first = NULL;
3608 pool->last = NULL;
3609 pool->offset = 0;
3610 return pool;
3613 /* Initialize constant pool hashing for a new function. */
3615 void
3616 init_varasm_status (void)
3618 crtl->varasm.pool = create_constant_pool ();
3619 crtl->varasm.deferred_constants = 0;
3622 /* Given a MINUS expression, simplify it if both sides
3623 include the same symbol. */
3626 simplify_subtraction (rtx x)
3628 rtx r = simplify_rtx (x);
3629 return r ? r : x;
3632 /* Given a constant rtx X, make (or find) a memory constant for its value
3633 and return a MEM rtx to refer to it in memory. */
3636 force_const_mem (enum machine_mode mode, rtx x)
3638 struct constant_descriptor_rtx *desc, tmp;
3639 struct rtx_constant_pool *pool;
3640 char label[256];
3641 rtx def, symbol;
3642 hashval_t hash;
3643 unsigned int align;
3644 void **slot;
3646 /* If we're not allowed to drop X into the constant pool, don't. */
3647 if (targetm.cannot_force_const_mem (mode, x))
3648 return NULL_RTX;
3650 /* Record that this function has used a constant pool entry. */
3651 crtl->uses_const_pool = 1;
3653 /* Decide which pool to use. */
3654 pool = (targetm.use_blocks_for_constant_p (mode, x)
3655 ? shared_constant_pool
3656 : crtl->varasm.pool);
3658 /* Lookup the value in the hashtable. */
3659 tmp.constant = x;
3660 tmp.mode = mode;
3661 hash = const_rtx_hash (x);
3662 slot = htab_find_slot_with_hash (pool->const_rtx_htab, &tmp, hash, INSERT);
3663 desc = (struct constant_descriptor_rtx *) *slot;
3665 /* If the constant was already present, return its memory. */
3666 if (desc)
3667 return copy_rtx (desc->mem);
3669 /* Otherwise, create a new descriptor. */
3670 desc = ggc_alloc_constant_descriptor_rtx ();
3671 *slot = desc;
3673 /* Align the location counter as required by EXP's data type. */
3674 align = GET_MODE_ALIGNMENT (mode == VOIDmode ? word_mode : mode);
3675 #ifdef CONSTANT_ALIGNMENT
3677 tree type = lang_hooks.types.type_for_mode (mode, 0);
3678 if (type != NULL_TREE)
3679 align = CONSTANT_ALIGNMENT (make_tree (type, x), align);
3681 #endif
3683 pool->offset += (align / BITS_PER_UNIT) - 1;
3684 pool->offset &= ~ ((align / BITS_PER_UNIT) - 1);
3686 desc->next = NULL;
3687 desc->constant = copy_rtx (tmp.constant);
3688 desc->offset = pool->offset;
3689 desc->hash = hash;
3690 desc->mode = mode;
3691 desc->align = align;
3692 desc->labelno = const_labelno;
3693 desc->mark = 0;
3695 pool->offset += GET_MODE_SIZE (mode);
3696 if (pool->last)
3697 pool->last->next = desc;
3698 else
3699 pool->first = pool->last = desc;
3700 pool->last = desc;
3702 /* Create a string containing the label name, in LABEL. */
3703 ASM_GENERATE_INTERNAL_LABEL (label, "LC", const_labelno);
3704 ++const_labelno;
3706 /* Construct the SYMBOL_REF. Make sure to mark it as belonging to
3707 the constants pool. */
3708 if (use_object_blocks_p () && targetm.use_blocks_for_constant_p (mode, x))
3710 section *sect = targetm.asm_out.select_rtx_section (mode, x, align);
3711 symbol = create_block_symbol (ggc_strdup (label),
3712 get_block_for_section (sect), -1);
3714 else
3715 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3716 desc->sym = symbol;
3717 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3718 CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3719 SET_SYMBOL_REF_CONSTANT (symbol, desc);
3721 /* Construct the MEM. */
3722 desc->mem = def = gen_const_mem (mode, symbol);
3723 set_mem_attributes (def, lang_hooks.types.type_for_mode (mode, 0), 1);
3724 set_mem_align (def, align);
3726 /* If we're dropping a label to the constant pool, make sure we
3727 don't delete it. */
3728 if (GET_CODE (x) == LABEL_REF)
3729 LABEL_PRESERVE_P (XEXP (x, 0)) = 1;
3731 return copy_rtx (def);
3734 /* Given a constant pool SYMBOL_REF, return the corresponding constant. */
3737 get_pool_constant (rtx addr)
3739 return SYMBOL_REF_CONSTANT (addr)->constant;
3742 /* Given a constant pool SYMBOL_REF, return the corresponding constant
3743 and whether it has been output or not. */
3746 get_pool_constant_mark (rtx addr, bool *pmarked)
3748 struct constant_descriptor_rtx *desc;
3750 desc = SYMBOL_REF_CONSTANT (addr);
3751 *pmarked = (desc->mark != 0);
3752 return desc->constant;
3755 /* Similar, return the mode. */
3757 enum machine_mode
3758 get_pool_mode (const_rtx addr)
3760 return SYMBOL_REF_CONSTANT (addr)->mode;
3763 /* Return the size of the constant pool. */
3766 get_pool_size (void)
3768 return crtl->varasm.pool->offset;
3771 /* Worker function for output_constant_pool_1. Emit assembly for X
3772 in MODE with known alignment ALIGN. */
3774 static void
3775 output_constant_pool_2 (enum machine_mode mode, rtx x, unsigned int align)
3777 switch (GET_MODE_CLASS (mode))
3779 case MODE_FLOAT:
3780 case MODE_DECIMAL_FLOAT:
3782 REAL_VALUE_TYPE r;
3784 gcc_assert (CONST_DOUBLE_AS_FLOAT_P (x));
3785 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
3786 assemble_real (r, mode, align);
3787 break;
3790 case MODE_INT:
3791 case MODE_PARTIAL_INT:
3792 case MODE_FRACT:
3793 case MODE_UFRACT:
3794 case MODE_ACCUM:
3795 case MODE_UACCUM:
3796 assemble_integer (x, GET_MODE_SIZE (mode), align, 1);
3797 break;
3799 case MODE_VECTOR_FLOAT:
3800 case MODE_VECTOR_INT:
3801 case MODE_VECTOR_FRACT:
3802 case MODE_VECTOR_UFRACT:
3803 case MODE_VECTOR_ACCUM:
3804 case MODE_VECTOR_UACCUM:
3806 int i, units;
3807 enum machine_mode submode = GET_MODE_INNER (mode);
3808 unsigned int subalign = MIN (align, GET_MODE_BITSIZE (submode));
3810 gcc_assert (GET_CODE (x) == CONST_VECTOR);
3811 units = CONST_VECTOR_NUNITS (x);
3813 for (i = 0; i < units; i++)
3815 rtx elt = CONST_VECTOR_ELT (x, i);
3816 output_constant_pool_2 (submode, elt, i ? subalign : align);
3819 break;
3821 default:
3822 gcc_unreachable ();
3826 /* Worker function for output_constant_pool. Emit constant DESC,
3827 giving it ALIGN bits of alignment. */
3829 static void
3830 output_constant_pool_1 (struct constant_descriptor_rtx *desc,
3831 unsigned int align)
3833 rtx x, tmp;
3835 x = desc->constant;
3837 /* See if X is a LABEL_REF (or a CONST referring to a LABEL_REF)
3838 whose CODE_LABEL has been deleted. This can occur if a jump table
3839 is eliminated by optimization. If so, write a constant of zero
3840 instead. Note that this can also happen by turning the
3841 CODE_LABEL into a NOTE. */
3842 /* ??? This seems completely and utterly wrong. Certainly it's
3843 not true for NOTE_INSN_DELETED_LABEL, but I disbelieve proper
3844 functioning even with INSN_DELETED_P and friends. */
3846 tmp = x;
3847 switch (GET_CODE (tmp))
3849 case CONST:
3850 if (GET_CODE (XEXP (tmp, 0)) != PLUS
3851 || GET_CODE (XEXP (XEXP (tmp, 0), 0)) != LABEL_REF)
3852 break;
3853 tmp = XEXP (XEXP (tmp, 0), 0);
3854 /* FALLTHRU */
3856 case LABEL_REF:
3857 tmp = XEXP (tmp, 0);
3858 gcc_assert (!INSN_DELETED_P (tmp));
3859 gcc_assert (!NOTE_P (tmp)
3860 || NOTE_KIND (tmp) != NOTE_INSN_DELETED);
3861 break;
3863 default:
3864 break;
3867 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3868 ASM_OUTPUT_SPECIAL_POOL_ENTRY (asm_out_file, x, desc->mode,
3869 align, desc->labelno, done);
3870 #endif
3872 assemble_align (align);
3874 /* Output the label. */
3875 targetm.asm_out.internal_label (asm_out_file, "LC", desc->labelno);
3877 /* Output the data. */
3878 output_constant_pool_2 (desc->mode, x, align);
3880 /* Make sure all constants in SECTION_MERGE and not SECTION_STRINGS
3881 sections have proper size. */
3882 if (align > GET_MODE_BITSIZE (desc->mode)
3883 && in_section
3884 && (in_section->common.flags & SECTION_MERGE))
3885 assemble_align (align);
3887 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3888 done:
3889 #endif
3890 return;
3893 /* Given a SYMBOL_REF CURRENT_RTX, mark it and all constants it refers
3894 to as used. Emit referenced deferred strings. This function can
3895 be used with for_each_rtx to mark all SYMBOL_REFs in an rtx. */
3897 static int
3898 mark_constant (rtx *current_rtx, void *data ATTRIBUTE_UNUSED)
3900 rtx x = *current_rtx;
3902 if (x == NULL_RTX || GET_CODE (x) != SYMBOL_REF)
3903 return 0;
3905 if (CONSTANT_POOL_ADDRESS_P (x))
3907 struct constant_descriptor_rtx *desc = SYMBOL_REF_CONSTANT (x);
3908 if (desc->mark == 0)
3910 desc->mark = 1;
3911 for_each_rtx (&desc->constant, mark_constant, NULL);
3914 else if (TREE_CONSTANT_POOL_ADDRESS_P (x))
3916 tree decl = SYMBOL_REF_DECL (x);
3917 if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
3919 n_deferred_constants--;
3920 output_constant_def_contents (x);
3924 return -1;
3927 /* Look through appropriate parts of INSN, marking all entries in the
3928 constant pool which are actually being used. Entries that are only
3929 referenced by other constants are also marked as used. Emit
3930 deferred strings that are used. */
3932 static void
3933 mark_constants (rtx insn)
3935 if (!INSN_P (insn))
3936 return;
3938 /* Insns may appear inside a SEQUENCE. Only check the patterns of
3939 insns, not any notes that may be attached. We don't want to mark
3940 a constant just because it happens to appear in a REG_EQUIV note. */
3941 if (GET_CODE (PATTERN (insn)) == SEQUENCE)
3943 rtx seq = PATTERN (insn);
3944 int i, n = XVECLEN (seq, 0);
3945 for (i = 0; i < n; ++i)
3947 rtx subinsn = XVECEXP (seq, 0, i);
3948 if (INSN_P (subinsn))
3949 for_each_rtx (&PATTERN (subinsn), mark_constant, NULL);
3952 else
3953 for_each_rtx (&PATTERN (insn), mark_constant, NULL);
3956 /* Look through the instructions for this function, and mark all the
3957 entries in POOL which are actually being used. Emit deferred constants
3958 which have indeed been used. */
3960 static void
3961 mark_constant_pool (void)
3963 rtx insn;
3965 if (!crtl->uses_const_pool && n_deferred_constants == 0)
3966 return;
3968 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3969 mark_constants (insn);
3972 /* Write all the constants in POOL. */
3974 static void
3975 output_constant_pool_contents (struct rtx_constant_pool *pool)
3977 struct constant_descriptor_rtx *desc;
3979 for (desc = pool->first; desc ; desc = desc->next)
3980 if (desc->mark)
3982 /* If the constant is part of an object_block, make sure that
3983 the constant has been positioned within its block, but do not
3984 write out its definition yet. output_object_blocks will do
3985 that later. */
3986 if (SYMBOL_REF_HAS_BLOCK_INFO_P (desc->sym)
3987 && SYMBOL_REF_BLOCK (desc->sym))
3988 place_block_symbol (desc->sym);
3989 else
3991 switch_to_section (targetm.asm_out.select_rtx_section
3992 (desc->mode, desc->constant, desc->align));
3993 output_constant_pool_1 (desc, desc->align);
3998 /* Mark all constants that are used in the current function, then write
3999 out the function's private constant pool. */
4001 static void
4002 output_constant_pool (const char *fnname ATTRIBUTE_UNUSED,
4003 tree fndecl ATTRIBUTE_UNUSED)
4005 struct rtx_constant_pool *pool = crtl->varasm.pool;
4007 /* It is possible for gcc to call force_const_mem and then to later
4008 discard the instructions which refer to the constant. In such a
4009 case we do not need to output the constant. */
4010 mark_constant_pool ();
4012 #ifdef ASM_OUTPUT_POOL_PROLOGUE
4013 ASM_OUTPUT_POOL_PROLOGUE (asm_out_file, fnname, fndecl, pool->offset);
4014 #endif
4016 output_constant_pool_contents (pool);
4018 #ifdef ASM_OUTPUT_POOL_EPILOGUE
4019 ASM_OUTPUT_POOL_EPILOGUE (asm_out_file, fnname, fndecl, pool->offset);
4020 #endif
4023 /* Write the contents of the shared constant pool. */
4025 void
4026 output_shared_constant_pool (void)
4028 output_constant_pool_contents (shared_constant_pool);
4031 /* Determine what kind of relocations EXP may need. */
4034 compute_reloc_for_constant (tree exp)
4036 int reloc = 0, reloc2;
4037 tree tem;
4039 switch (TREE_CODE (exp))
4041 case ADDR_EXPR:
4042 case FDESC_EXPR:
4043 /* Go inside any operations that get_inner_reference can handle and see
4044 if what's inside is a constant: no need to do anything here for
4045 addresses of variables or functions. */
4046 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
4047 tem = TREE_OPERAND (tem, 0))
4050 if (TREE_CODE (tem) == MEM_REF
4051 && TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR)
4053 reloc = compute_reloc_for_constant (TREE_OPERAND (tem, 0));
4054 break;
4057 if (!targetm.binds_local_p (tem))
4058 reloc |= 2;
4059 else
4060 reloc |= 1;
4061 break;
4063 case PLUS_EXPR:
4064 case POINTER_PLUS_EXPR:
4065 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4066 reloc |= compute_reloc_for_constant (TREE_OPERAND (exp, 1));
4067 break;
4069 case MINUS_EXPR:
4070 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4071 reloc2 = compute_reloc_for_constant (TREE_OPERAND (exp, 1));
4072 /* The difference of two local labels is computable at link time. */
4073 if (reloc == 1 && reloc2 == 1)
4074 reloc = 0;
4075 else
4076 reloc |= reloc2;
4077 break;
4079 CASE_CONVERT:
4080 case VIEW_CONVERT_EXPR:
4081 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4082 break;
4084 case CONSTRUCTOR:
4086 unsigned HOST_WIDE_INT idx;
4087 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
4088 if (tem != 0)
4089 reloc |= compute_reloc_for_constant (tem);
4091 break;
4093 default:
4094 break;
4096 return reloc;
4099 /* Find all the constants whose addresses are referenced inside of EXP,
4100 and make sure assembler code with a label has been output for each one.
4101 Indicate whether an ADDR_EXPR has been encountered. */
4103 static void
4104 output_addressed_constants (tree exp)
4106 tree tem;
4108 switch (TREE_CODE (exp))
4110 case ADDR_EXPR:
4111 case FDESC_EXPR:
4112 /* Go inside any operations that get_inner_reference can handle and see
4113 if what's inside is a constant: no need to do anything here for
4114 addresses of variables or functions. */
4115 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
4116 tem = TREE_OPERAND (tem, 0))
4119 /* If we have an initialized CONST_DECL, retrieve the initializer. */
4120 if (TREE_CODE (tem) == CONST_DECL && DECL_INITIAL (tem))
4121 tem = DECL_INITIAL (tem);
4123 if (CONSTANT_CLASS_P (tem) || TREE_CODE (tem) == CONSTRUCTOR)
4124 output_constant_def (tem, 0);
4126 if (TREE_CODE (tem) == MEM_REF)
4127 output_addressed_constants (TREE_OPERAND (tem, 0));
4128 break;
4130 case PLUS_EXPR:
4131 case POINTER_PLUS_EXPR:
4132 case MINUS_EXPR:
4133 output_addressed_constants (TREE_OPERAND (exp, 1));
4134 /* Fall through. */
4136 CASE_CONVERT:
4137 case VIEW_CONVERT_EXPR:
4138 output_addressed_constants (TREE_OPERAND (exp, 0));
4139 break;
4141 case CONSTRUCTOR:
4143 unsigned HOST_WIDE_INT idx;
4144 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
4145 if (tem != 0)
4146 output_addressed_constants (tem);
4148 break;
4150 default:
4151 break;
4155 /* Whether a constructor CTOR is a valid static constant initializer if all
4156 its elements are. This used to be internal to initializer_constant_valid_p
4157 and has been exposed to let other functions like categorize_ctor_elements
4158 evaluate the property while walking a constructor for other purposes. */
4160 bool
4161 constructor_static_from_elts_p (const_tree ctor)
4163 return (TREE_CONSTANT (ctor)
4164 && (TREE_CODE (TREE_TYPE (ctor)) == UNION_TYPE
4165 || TREE_CODE (TREE_TYPE (ctor)) == RECORD_TYPE
4166 || TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE));
4169 static tree initializer_constant_valid_p_1 (tree value, tree endtype,
4170 tree *cache);
4172 /* A subroutine of initializer_constant_valid_p. VALUE is a MINUS_EXPR,
4173 PLUS_EXPR or POINTER_PLUS_EXPR. This looks for cases of VALUE
4174 which are valid when ENDTYPE is an integer of any size; in
4175 particular, this does not accept a pointer minus a constant. This
4176 returns null_pointer_node if the VALUE is an absolute constant
4177 which can be used to initialize a static variable. Otherwise it
4178 returns NULL. */
4180 static tree
4181 narrowing_initializer_constant_valid_p (tree value, tree endtype, tree *cache)
4183 tree op0, op1;
4185 if (!INTEGRAL_TYPE_P (endtype))
4186 return NULL_TREE;
4188 op0 = TREE_OPERAND (value, 0);
4189 op1 = TREE_OPERAND (value, 1);
4191 /* Like STRIP_NOPS except allow the operand mode to widen. This
4192 works around a feature of fold that simplifies (int)(p1 - p2) to
4193 ((int)p1 - (int)p2) under the theory that the narrower operation
4194 is cheaper. */
4196 while (CONVERT_EXPR_P (op0)
4197 || TREE_CODE (op0) == NON_LVALUE_EXPR)
4199 tree inner = TREE_OPERAND (op0, 0);
4200 if (inner == error_mark_node
4201 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4202 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))
4203 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
4204 break;
4205 op0 = inner;
4208 while (CONVERT_EXPR_P (op1)
4209 || TREE_CODE (op1) == NON_LVALUE_EXPR)
4211 tree inner = TREE_OPERAND (op1, 0);
4212 if (inner == error_mark_node
4213 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4214 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op1)))
4215 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
4216 break;
4217 op1 = inner;
4220 op0 = initializer_constant_valid_p_1 (op0, endtype, cache);
4221 if (!op0)
4222 return NULL_TREE;
4224 op1 = initializer_constant_valid_p_1 (op1, endtype,
4225 cache ? cache + 2 : NULL);
4226 /* Both initializers must be known. */
4227 if (op1)
4229 if (op0 == op1
4230 && (op0 == null_pointer_node
4231 || TREE_CODE (value) == MINUS_EXPR))
4232 return null_pointer_node;
4234 /* Support differences between labels. */
4235 if (TREE_CODE (op0) == LABEL_DECL
4236 && TREE_CODE (op1) == LABEL_DECL)
4237 return null_pointer_node;
4239 if (TREE_CODE (op0) == STRING_CST
4240 && TREE_CODE (op1) == STRING_CST
4241 && operand_equal_p (op0, op1, 1))
4242 return null_pointer_node;
4245 return NULL_TREE;
4248 /* Helper function of initializer_constant_valid_p.
4249 Return nonzero if VALUE is a valid constant-valued expression
4250 for use in initializing a static variable; one that can be an
4251 element of a "constant" initializer.
4253 Return null_pointer_node if the value is absolute;
4254 if it is relocatable, return the variable that determines the relocation.
4255 We assume that VALUE has been folded as much as possible;
4256 therefore, we do not need to check for such things as
4257 arithmetic-combinations of integers.
4259 Use CACHE (pointer to 2 tree values) for caching if non-NULL. */
4261 static tree
4262 initializer_constant_valid_p_1 (tree value, tree endtype, tree *cache)
4264 tree ret;
4266 switch (TREE_CODE (value))
4268 case CONSTRUCTOR:
4269 if (constructor_static_from_elts_p (value))
4271 unsigned HOST_WIDE_INT idx;
4272 tree elt;
4273 bool absolute = true;
4275 if (cache && cache[0] == value)
4276 return cache[1];
4277 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
4279 tree reloc;
4280 reloc = initializer_constant_valid_p_1 (elt, TREE_TYPE (elt),
4281 NULL);
4282 if (!reloc)
4284 if (cache)
4286 cache[0] = value;
4287 cache[1] = NULL_TREE;
4289 return NULL_TREE;
4291 if (reloc != null_pointer_node)
4292 absolute = false;
4294 /* For a non-absolute relocation, there is no single
4295 variable that can be "the variable that determines the
4296 relocation." */
4297 if (cache)
4299 cache[0] = value;
4300 cache[1] = absolute ? null_pointer_node : error_mark_node;
4302 return absolute ? null_pointer_node : error_mark_node;
4305 return TREE_STATIC (value) ? null_pointer_node : NULL_TREE;
4307 case INTEGER_CST:
4308 case VECTOR_CST:
4309 case REAL_CST:
4310 case FIXED_CST:
4311 case STRING_CST:
4312 case COMPLEX_CST:
4313 return null_pointer_node;
4315 case ADDR_EXPR:
4316 case FDESC_EXPR:
4318 tree op0 = staticp (TREE_OPERAND (value, 0));
4319 if (op0)
4321 /* "&(*a).f" is like unto pointer arithmetic. If "a" turns out
4322 to be a constant, this is old-skool offsetof-like nonsense. */
4323 if (TREE_CODE (op0) == INDIRECT_REF
4324 && TREE_CONSTANT (TREE_OPERAND (op0, 0)))
4325 return null_pointer_node;
4326 /* Taking the address of a nested function involves a trampoline,
4327 unless we don't need or want one. */
4328 if (TREE_CODE (op0) == FUNCTION_DECL
4329 && DECL_STATIC_CHAIN (op0)
4330 && !TREE_NO_TRAMPOLINE (value))
4331 return NULL_TREE;
4332 /* "&{...}" requires a temporary to hold the constructed
4333 object. */
4334 if (TREE_CODE (op0) == CONSTRUCTOR)
4335 return NULL_TREE;
4337 return op0;
4340 case NON_LVALUE_EXPR:
4341 return initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4342 endtype, cache);
4344 case VIEW_CONVERT_EXPR:
4346 tree src = TREE_OPERAND (value, 0);
4347 tree src_type = TREE_TYPE (src);
4348 tree dest_type = TREE_TYPE (value);
4350 /* Allow view-conversions from aggregate to non-aggregate type only
4351 if the bit pattern is fully preserved afterwards; otherwise, the
4352 RTL expander won't be able to apply a subsequent transformation
4353 to the underlying constructor. */
4354 if (AGGREGATE_TYPE_P (src_type) && !AGGREGATE_TYPE_P (dest_type))
4356 if (TYPE_MODE (endtype) == TYPE_MODE (dest_type))
4357 return initializer_constant_valid_p_1 (src, endtype, cache);
4358 else
4359 return NULL_TREE;
4362 /* Allow all other kinds of view-conversion. */
4363 return initializer_constant_valid_p_1 (src, endtype, cache);
4366 CASE_CONVERT:
4368 tree src = TREE_OPERAND (value, 0);
4369 tree src_type = TREE_TYPE (src);
4370 tree dest_type = TREE_TYPE (value);
4372 /* Allow conversions between pointer types, floating-point
4373 types, and offset types. */
4374 if ((POINTER_TYPE_P (dest_type) && POINTER_TYPE_P (src_type))
4375 || (FLOAT_TYPE_P (dest_type) && FLOAT_TYPE_P (src_type))
4376 || (TREE_CODE (dest_type) == OFFSET_TYPE
4377 && TREE_CODE (src_type) == OFFSET_TYPE))
4378 return initializer_constant_valid_p_1 (src, endtype, cache);
4380 /* Allow length-preserving conversions between integer types. */
4381 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type)
4382 && (TYPE_PRECISION (dest_type) == TYPE_PRECISION (src_type)))
4383 return initializer_constant_valid_p_1 (src, endtype, cache);
4385 /* Allow conversions between other integer types only if
4386 explicit value. */
4387 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type))
4389 tree inner = initializer_constant_valid_p_1 (src, endtype, cache);
4390 if (inner == null_pointer_node)
4391 return null_pointer_node;
4392 break;
4395 /* Allow (int) &foo provided int is as wide as a pointer. */
4396 if (INTEGRAL_TYPE_P (dest_type) && POINTER_TYPE_P (src_type)
4397 && (TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type)))
4398 return initializer_constant_valid_p_1 (src, endtype, cache);
4400 /* Likewise conversions from int to pointers, but also allow
4401 conversions from 0. */
4402 if ((POINTER_TYPE_P (dest_type)
4403 || TREE_CODE (dest_type) == OFFSET_TYPE)
4404 && INTEGRAL_TYPE_P (src_type))
4406 if (TREE_CODE (src) == INTEGER_CST
4407 && TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type))
4408 return null_pointer_node;
4409 if (integer_zerop (src))
4410 return null_pointer_node;
4411 else if (TYPE_PRECISION (dest_type) <= TYPE_PRECISION (src_type))
4412 return initializer_constant_valid_p_1 (src, endtype, cache);
4415 /* Allow conversions to struct or union types if the value
4416 inside is okay. */
4417 if (TREE_CODE (dest_type) == RECORD_TYPE
4418 || TREE_CODE (dest_type) == UNION_TYPE)
4419 return initializer_constant_valid_p_1 (src, endtype, cache);
4421 break;
4423 case POINTER_PLUS_EXPR:
4424 case PLUS_EXPR:
4425 /* Any valid floating-point constants will have been folded by now;
4426 with -frounding-math we hit this with addition of two constants. */
4427 if (TREE_CODE (endtype) == REAL_TYPE)
4428 return NULL_TREE;
4429 if (cache && cache[0] == value)
4430 return cache[1];
4431 if (! INTEGRAL_TYPE_P (endtype)
4432 || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
4434 tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
4435 tree valid0
4436 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4437 endtype, ncache);
4438 tree valid1
4439 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
4440 endtype, ncache + 2);
4441 /* If either term is absolute, use the other term's relocation. */
4442 if (valid0 == null_pointer_node)
4443 ret = valid1;
4444 else if (valid1 == null_pointer_node)
4445 ret = valid0;
4446 /* Support narrowing pointer differences. */
4447 else
4448 ret = narrowing_initializer_constant_valid_p (value, endtype,
4449 ncache);
4451 else
4452 /* Support narrowing pointer differences. */
4453 ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
4454 if (cache)
4456 cache[0] = value;
4457 cache[1] = ret;
4459 return ret;
4461 case MINUS_EXPR:
4462 if (TREE_CODE (endtype) == REAL_TYPE)
4463 return NULL_TREE;
4464 if (cache && cache[0] == value)
4465 return cache[1];
4466 if (! INTEGRAL_TYPE_P (endtype)
4467 || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
4469 tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
4470 tree valid0
4471 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4472 endtype, ncache);
4473 tree valid1
4474 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
4475 endtype, ncache + 2);
4476 /* Win if second argument is absolute. */
4477 if (valid1 == null_pointer_node)
4478 ret = valid0;
4479 /* Win if both arguments have the same relocation.
4480 Then the value is absolute. */
4481 else if (valid0 == valid1 && valid0 != 0)
4482 ret = null_pointer_node;
4483 /* Since GCC guarantees that string constants are unique in the
4484 generated code, a subtraction between two copies of the same
4485 constant string is absolute. */
4486 else if (valid0 && TREE_CODE (valid0) == STRING_CST
4487 && valid1 && TREE_CODE (valid1) == STRING_CST
4488 && operand_equal_p (valid0, valid1, 1))
4489 ret = null_pointer_node;
4490 /* Support narrowing differences. */
4491 else
4492 ret = narrowing_initializer_constant_valid_p (value, endtype,
4493 ncache);
4495 else
4496 /* Support narrowing differences. */
4497 ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
4498 if (cache)
4500 cache[0] = value;
4501 cache[1] = ret;
4503 return ret;
4505 default:
4506 break;
4509 return NULL_TREE;
4512 /* Return nonzero if VALUE is a valid constant-valued expression
4513 for use in initializing a static variable; one that can be an
4514 element of a "constant" initializer.
4516 Return null_pointer_node if the value is absolute;
4517 if it is relocatable, return the variable that determines the relocation.
4518 We assume that VALUE has been folded as much as possible;
4519 therefore, we do not need to check for such things as
4520 arithmetic-combinations of integers. */
4521 tree
4522 initializer_constant_valid_p (tree value, tree endtype)
4524 return initializer_constant_valid_p_1 (value, endtype, NULL);
4527 /* Return true if VALUE is a valid constant-valued expression
4528 for use in initializing a static bit-field; one that can be
4529 an element of a "constant" initializer. */
4531 bool
4532 initializer_constant_valid_for_bitfield_p (tree value)
4534 /* For bitfields we support integer constants or possibly nested aggregates
4535 of such. */
4536 switch (TREE_CODE (value))
4538 case CONSTRUCTOR:
4540 unsigned HOST_WIDE_INT idx;
4541 tree elt;
4543 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
4544 if (!initializer_constant_valid_for_bitfield_p (elt))
4545 return false;
4546 return true;
4549 case INTEGER_CST:
4550 case REAL_CST:
4551 return true;
4553 case VIEW_CONVERT_EXPR:
4554 case NON_LVALUE_EXPR:
4555 return
4556 initializer_constant_valid_for_bitfield_p (TREE_OPERAND (value, 0));
4558 default:
4559 break;
4562 return false;
4565 /* output_constructor outer state of relevance in recursive calls, typically
4566 for nested aggregate bitfields. */
4568 typedef struct {
4569 unsigned int bit_offset; /* current position in ... */
4570 int byte; /* ... the outer byte buffer. */
4571 } oc_outer_state;
4573 static unsigned HOST_WIDE_INT
4574 output_constructor (tree, unsigned HOST_WIDE_INT, unsigned int,
4575 oc_outer_state *);
4577 /* Output assembler code for constant EXP, with no label.
4578 This includes the pseudo-op such as ".int" or ".byte", and a newline.
4579 Assumes output_addressed_constants has been done on EXP already.
4581 Generate at least SIZE bytes of assembler data, padding at the end
4582 with zeros if necessary. SIZE must always be specified. The returned
4583 value is the actual number of bytes of assembler data generated, which
4584 may be bigger than SIZE if the object contains a variable length field.
4586 SIZE is important for structure constructors,
4587 since trailing members may have been omitted from the constructor.
4588 It is also important for initialization of arrays from string constants
4589 since the full length of the string constant might not be wanted.
4590 It is also needed for initialization of unions, where the initializer's
4591 type is just one member, and that may not be as long as the union.
4593 There a case in which we would fail to output exactly SIZE bytes:
4594 for a structure constructor that wants to produce more than SIZE bytes.
4595 But such constructors will never be generated for any possible input.
4597 ALIGN is the alignment of the data in bits. */
4599 static unsigned HOST_WIDE_INT
4600 output_constant (tree exp, unsigned HOST_WIDE_INT size, unsigned int align)
4602 enum tree_code code;
4603 unsigned HOST_WIDE_INT thissize;
4605 if (size == 0 || flag_syntax_only)
4606 return size;
4608 /* See if we're trying to initialize a pointer in a non-default mode
4609 to the address of some declaration somewhere. If the target says
4610 the mode is valid for pointers, assume the target has a way of
4611 resolving it. */
4612 if (TREE_CODE (exp) == NOP_EXPR
4613 && POINTER_TYPE_P (TREE_TYPE (exp))
4614 && targetm.addr_space.valid_pointer_mode
4615 (TYPE_MODE (TREE_TYPE (exp)),
4616 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
4618 tree saved_type = TREE_TYPE (exp);
4620 /* Peel off any intermediate conversions-to-pointer for valid
4621 pointer modes. */
4622 while (TREE_CODE (exp) == NOP_EXPR
4623 && POINTER_TYPE_P (TREE_TYPE (exp))
4624 && targetm.addr_space.valid_pointer_mode
4625 (TYPE_MODE (TREE_TYPE (exp)),
4626 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
4627 exp = TREE_OPERAND (exp, 0);
4629 /* If what we're left with is the address of something, we can
4630 convert the address to the final type and output it that
4631 way. */
4632 if (TREE_CODE (exp) == ADDR_EXPR)
4633 exp = build1 (ADDR_EXPR, saved_type, TREE_OPERAND (exp, 0));
4634 /* Likewise for constant ints. */
4635 else if (TREE_CODE (exp) == INTEGER_CST)
4636 exp = build_int_cst_wide (saved_type, TREE_INT_CST_LOW (exp),
4637 TREE_INT_CST_HIGH (exp));
4641 /* Eliminate any conversions since we'll be outputting the underlying
4642 constant. */
4643 while (CONVERT_EXPR_P (exp)
4644 || TREE_CODE (exp) == NON_LVALUE_EXPR
4645 || TREE_CODE (exp) == VIEW_CONVERT_EXPR)
4647 HOST_WIDE_INT type_size = int_size_in_bytes (TREE_TYPE (exp));
4648 HOST_WIDE_INT op_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (exp, 0)));
4650 /* Make sure eliminating the conversion is really a no-op, except with
4651 VIEW_CONVERT_EXPRs to allow for wild Ada unchecked conversions and
4652 union types to allow for Ada unchecked unions. */
4653 if (type_size > op_size
4654 && TREE_CODE (exp) != VIEW_CONVERT_EXPR
4655 && TREE_CODE (TREE_TYPE (exp)) != UNION_TYPE)
4656 /* Keep the conversion. */
4657 break;
4658 else
4659 exp = TREE_OPERAND (exp, 0);
4662 code = TREE_CODE (TREE_TYPE (exp));
4663 thissize = int_size_in_bytes (TREE_TYPE (exp));
4665 /* Allow a constructor with no elements for any data type.
4666 This means to fill the space with zeros. */
4667 if (TREE_CODE (exp) == CONSTRUCTOR
4668 && vec_safe_is_empty (CONSTRUCTOR_ELTS (exp)))
4670 assemble_zeros (size);
4671 return size;
4674 if (TREE_CODE (exp) == FDESC_EXPR)
4676 #ifdef ASM_OUTPUT_FDESC
4677 HOST_WIDE_INT part = tree_to_shwi (TREE_OPERAND (exp, 1));
4678 tree decl = TREE_OPERAND (exp, 0);
4679 ASM_OUTPUT_FDESC (asm_out_file, decl, part);
4680 #else
4681 gcc_unreachable ();
4682 #endif
4683 return size;
4686 /* Now output the underlying data. If we've handling the padding, return.
4687 Otherwise, break and ensure SIZE is the size written. */
4688 switch (code)
4690 case BOOLEAN_TYPE:
4691 case INTEGER_TYPE:
4692 case ENUMERAL_TYPE:
4693 case POINTER_TYPE:
4694 case REFERENCE_TYPE:
4695 case OFFSET_TYPE:
4696 case FIXED_POINT_TYPE:
4697 case NULLPTR_TYPE:
4698 if (! assemble_integer (expand_expr (exp, NULL_RTX, VOIDmode,
4699 EXPAND_INITIALIZER),
4700 MIN (size, thissize), align, 0))
4701 error ("initializer for integer/fixed-point value is too complicated");
4702 break;
4704 case REAL_TYPE:
4705 if (TREE_CODE (exp) != REAL_CST)
4706 error ("initializer for floating value is not a floating constant");
4707 else
4708 assemble_real (TREE_REAL_CST (exp), TYPE_MODE (TREE_TYPE (exp)), align);
4709 break;
4711 case COMPLEX_TYPE:
4712 output_constant (TREE_REALPART (exp), thissize / 2, align);
4713 output_constant (TREE_IMAGPART (exp), thissize / 2,
4714 min_align (align, BITS_PER_UNIT * (thissize / 2)));
4715 break;
4717 case ARRAY_TYPE:
4718 case VECTOR_TYPE:
4719 switch (TREE_CODE (exp))
4721 case CONSTRUCTOR:
4722 return output_constructor (exp, size, align, NULL);
4723 case STRING_CST:
4724 thissize
4725 = MIN ((unsigned HOST_WIDE_INT)TREE_STRING_LENGTH (exp), size);
4726 assemble_string (TREE_STRING_POINTER (exp), thissize);
4727 break;
4728 case VECTOR_CST:
4730 enum machine_mode inner = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
4731 unsigned int nalign = MIN (align, GET_MODE_ALIGNMENT (inner));
4732 int elt_size = GET_MODE_SIZE (inner);
4733 output_constant (VECTOR_CST_ELT (exp, 0), elt_size, align);
4734 thissize = elt_size;
4735 for (unsigned int i = 1; i < VECTOR_CST_NELTS (exp); i++)
4737 output_constant (VECTOR_CST_ELT (exp, i), elt_size, nalign);
4738 thissize += elt_size;
4740 break;
4742 default:
4743 gcc_unreachable ();
4745 break;
4747 case RECORD_TYPE:
4748 case UNION_TYPE:
4749 gcc_assert (TREE_CODE (exp) == CONSTRUCTOR);
4750 return output_constructor (exp, size, align, NULL);
4752 case ERROR_MARK:
4753 return 0;
4755 default:
4756 gcc_unreachable ();
4759 if (size > thissize)
4760 assemble_zeros (size - thissize);
4762 return size;
4766 /* Subroutine of output_constructor, used for computing the size of
4767 arrays of unspecified length. VAL must be a CONSTRUCTOR of an array
4768 type with an unspecified upper bound. */
4770 static unsigned HOST_WIDE_INT
4771 array_size_for_constructor (tree val)
4773 tree max_index;
4774 unsigned HOST_WIDE_INT cnt;
4775 tree index, value, tmp;
4776 double_int i;
4778 /* This code used to attempt to handle string constants that are not
4779 arrays of single-bytes, but nothing else does, so there's no point in
4780 doing it here. */
4781 if (TREE_CODE (val) == STRING_CST)
4782 return TREE_STRING_LENGTH (val);
4784 max_index = NULL_TREE;
4785 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (val), cnt, index, value)
4787 if (TREE_CODE (index) == RANGE_EXPR)
4788 index = TREE_OPERAND (index, 1);
4789 if (max_index == NULL_TREE || tree_int_cst_lt (max_index, index))
4790 max_index = index;
4793 if (max_index == NULL_TREE)
4794 return 0;
4796 /* Compute the total number of array elements. */
4797 tmp = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (val)));
4798 i = tree_to_double_int (max_index) - tree_to_double_int (tmp);
4799 i += double_int_one;
4801 /* Multiply by the array element unit size to find number of bytes. */
4802 i *= tree_to_double_int (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (val))));
4804 gcc_assert (i.fits_uhwi ());
4805 return i.low;
4808 /* Other datastructures + helpers for output_constructor. */
4810 /* output_constructor local state to support interaction with helpers. */
4812 typedef struct {
4814 /* Received arguments. */
4815 tree exp; /* Constructor expression. */
4816 tree type; /* Type of constructor expression. */
4817 unsigned HOST_WIDE_INT size; /* # bytes to output - pad if necessary. */
4818 unsigned int align; /* Known initial alignment. */
4819 tree min_index; /* Lower bound if specified for an array. */
4821 /* Output processing state. */
4822 HOST_WIDE_INT total_bytes; /* # bytes output so far / current position. */
4823 int byte; /* Part of a bitfield byte yet to be output. */
4824 int last_relative_index; /* Implicit or explicit index of the last
4825 array element output within a bitfield. */
4826 bool byte_buffer_in_use; /* Whether BYTE is in use. */
4828 /* Current element. */
4829 tree field; /* Current field decl in a record. */
4830 tree val; /* Current element value. */
4831 tree index; /* Current element index. */
4833 } oc_local_state;
4835 /* Helper for output_constructor. From the current LOCAL state, output a
4836 RANGE_EXPR element. */
4838 static void
4839 output_constructor_array_range (oc_local_state *local)
4841 unsigned HOST_WIDE_INT fieldsize
4842 = int_size_in_bytes (TREE_TYPE (local->type));
4844 HOST_WIDE_INT lo_index
4845 = tree_to_shwi (TREE_OPERAND (local->index, 0));
4846 HOST_WIDE_INT hi_index
4847 = tree_to_shwi (TREE_OPERAND (local->index, 1));
4848 HOST_WIDE_INT index;
4850 unsigned int align2
4851 = min_align (local->align, fieldsize * BITS_PER_UNIT);
4853 for (index = lo_index; index <= hi_index; index++)
4855 /* Output the element's initial value. */
4856 if (local->val == NULL_TREE)
4857 assemble_zeros (fieldsize);
4858 else
4859 fieldsize = output_constant (local->val, fieldsize, align2);
4861 /* Count its size. */
4862 local->total_bytes += fieldsize;
4866 /* Helper for output_constructor. From the current LOCAL state, output a
4867 field element that is not true bitfield or part of an outer one. */
4869 static void
4870 output_constructor_regular_field (oc_local_state *local)
4872 /* Field size and position. Since this structure is static, we know the
4873 positions are constant. */
4874 unsigned HOST_WIDE_INT fieldsize;
4875 HOST_WIDE_INT fieldpos;
4877 unsigned int align2;
4879 if (local->index != NULL_TREE)
4881 /* Perform the index calculation in modulo arithmetic but
4882 sign-extend the result because Ada has negative DECL_FIELD_OFFSETs
4883 but we are using an unsigned sizetype. */
4884 unsigned prec = TYPE_PRECISION (sizetype);
4885 double_int idx = tree_to_double_int (local->index)
4886 - tree_to_double_int (local->min_index);
4887 idx = idx.sext (prec);
4888 fieldpos = (tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (local->val)))
4889 * idx.low);
4891 else if (local->field != NULL_TREE)
4892 fieldpos = int_byte_position (local->field);
4893 else
4894 fieldpos = 0;
4896 /* Output any buffered-up bit-fields preceding this element. */
4897 if (local->byte_buffer_in_use)
4899 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
4900 local->total_bytes++;
4901 local->byte_buffer_in_use = false;
4904 /* Advance to offset of this element.
4905 Note no alignment needed in an array, since that is guaranteed
4906 if each element has the proper size. */
4907 if ((local->field != NULL_TREE || local->index != NULL_TREE)
4908 && fieldpos > local->total_bytes)
4910 assemble_zeros (fieldpos - local->total_bytes);
4911 local->total_bytes = fieldpos;
4914 /* Find the alignment of this element. */
4915 align2 = min_align (local->align, BITS_PER_UNIT * fieldpos);
4917 /* Determine size this element should occupy. */
4918 if (local->field)
4920 fieldsize = 0;
4922 /* If this is an array with an unspecified upper bound,
4923 the initializer determines the size. */
4924 /* ??? This ought to only checked if DECL_SIZE_UNIT is NULL,
4925 but we cannot do this until the deprecated support for
4926 initializing zero-length array members is removed. */
4927 if (TREE_CODE (TREE_TYPE (local->field)) == ARRAY_TYPE
4928 && TYPE_DOMAIN (TREE_TYPE (local->field))
4929 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (local->field))))
4931 fieldsize = array_size_for_constructor (local->val);
4932 /* Given a non-empty initialization, this field had
4933 better be last. */
4934 gcc_assert (!fieldsize || !DECL_CHAIN (local->field));
4936 else
4937 fieldsize = tree_to_uhwi (DECL_SIZE_UNIT (local->field));
4939 else
4940 fieldsize = int_size_in_bytes (TREE_TYPE (local->type));
4942 /* Output the element's initial value. */
4943 if (local->val == NULL_TREE)
4944 assemble_zeros (fieldsize);
4945 else
4946 fieldsize = output_constant (local->val, fieldsize, align2);
4948 /* Count its size. */
4949 local->total_bytes += fieldsize;
4952 /* Helper for output_constructor. From the LOCAL state, output an element
4953 that is a true bitfield or part of an outer one. BIT_OFFSET is the offset
4954 from the start of a possibly ongoing outer byte buffer. */
4956 static void
4957 output_constructor_bitfield (oc_local_state *local, unsigned int bit_offset)
4959 /* Bit size of this element. */
4960 HOST_WIDE_INT ebitsize
4961 = (local->field
4962 ? tree_to_uhwi (DECL_SIZE (local->field))
4963 : tree_to_uhwi (TYPE_SIZE (TREE_TYPE (local->type))));
4965 /* Relative index of this element if this is an array component. */
4966 HOST_WIDE_INT relative_index
4967 = (!local->field
4968 ? (local->index
4969 ? (tree_to_shwi (local->index)
4970 - tree_to_shwi (local->min_index))
4971 : local->last_relative_index + 1)
4972 : 0);
4974 /* Bit position of this element from the start of the containing
4975 constructor. */
4976 HOST_WIDE_INT constructor_relative_ebitpos
4977 = (local->field
4978 ? int_bit_position (local->field)
4979 : ebitsize * relative_index);
4981 /* Bit position of this element from the start of a possibly ongoing
4982 outer byte buffer. */
4983 HOST_WIDE_INT byte_relative_ebitpos
4984 = bit_offset + constructor_relative_ebitpos;
4986 /* From the start of a possibly ongoing outer byte buffer, offsets to
4987 the first bit of this element and to the first bit past the end of
4988 this element. */
4989 HOST_WIDE_INT next_offset = byte_relative_ebitpos;
4990 HOST_WIDE_INT end_offset = byte_relative_ebitpos + ebitsize;
4992 local->last_relative_index = relative_index;
4994 if (local->val == NULL_TREE)
4995 local->val = integer_zero_node;
4997 while (TREE_CODE (local->val) == VIEW_CONVERT_EXPR
4998 || TREE_CODE (local->val) == NON_LVALUE_EXPR)
4999 local->val = TREE_OPERAND (local->val, 0);
5001 if (TREE_CODE (local->val) != INTEGER_CST
5002 && TREE_CODE (local->val) != CONSTRUCTOR)
5004 error ("invalid initial value for member %qE", DECL_NAME (local->field));
5005 return;
5008 /* If this field does not start in this (or next) byte, skip some bytes. */
5009 if (next_offset / BITS_PER_UNIT != local->total_bytes)
5011 /* Output remnant of any bit field in previous bytes. */
5012 if (local->byte_buffer_in_use)
5014 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5015 local->total_bytes++;
5016 local->byte_buffer_in_use = false;
5019 /* If still not at proper byte, advance to there. */
5020 if (next_offset / BITS_PER_UNIT != local->total_bytes)
5022 gcc_assert (next_offset / BITS_PER_UNIT >= local->total_bytes);
5023 assemble_zeros (next_offset / BITS_PER_UNIT - local->total_bytes);
5024 local->total_bytes = next_offset / BITS_PER_UNIT;
5028 /* Set up the buffer if necessary. */
5029 if (!local->byte_buffer_in_use)
5031 local->byte = 0;
5032 if (ebitsize > 0)
5033 local->byte_buffer_in_use = true;
5036 /* If this is nested constructor, recurse passing the bit offset and the
5037 pending data, then retrieve the new pending data afterwards. */
5038 if (TREE_CODE (local->val) == CONSTRUCTOR)
5040 oc_outer_state temp_state;
5041 temp_state.bit_offset = next_offset % BITS_PER_UNIT;
5042 temp_state.byte = local->byte;
5043 local->total_bytes
5044 += output_constructor (local->val, 0, 0, &temp_state);
5045 local->byte = temp_state.byte;
5046 return;
5049 /* Otherwise, we must split the element into pieces that fall within
5050 separate bytes, and combine each byte with previous or following
5051 bit-fields. */
5052 while (next_offset < end_offset)
5054 int this_time;
5055 int shift;
5056 HOST_WIDE_INT value;
5057 HOST_WIDE_INT next_byte = next_offset / BITS_PER_UNIT;
5058 HOST_WIDE_INT next_bit = next_offset % BITS_PER_UNIT;
5060 /* Advance from byte to byte within this element when necessary. */
5061 while (next_byte != local->total_bytes)
5063 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5064 local->total_bytes++;
5065 local->byte = 0;
5068 /* Number of bits we can process at once (all part of the same byte). */
5069 this_time = MIN (end_offset - next_offset, BITS_PER_UNIT - next_bit);
5070 if (BYTES_BIG_ENDIAN)
5072 /* On big-endian machine, take the most significant bits
5073 first (of the bits that are significant)
5074 and put them into bytes from the most significant end. */
5075 shift = end_offset - next_offset - this_time;
5077 /* Don't try to take a bunch of bits that cross
5078 the word boundary in the INTEGER_CST. We can
5079 only select bits from the LOW or HIGH part
5080 not from both. */
5081 if (shift < HOST_BITS_PER_WIDE_INT
5082 && shift + this_time > HOST_BITS_PER_WIDE_INT)
5084 this_time = shift + this_time - HOST_BITS_PER_WIDE_INT;
5085 shift = HOST_BITS_PER_WIDE_INT;
5088 /* Now get the bits from the appropriate constant word. */
5089 if (shift < HOST_BITS_PER_WIDE_INT)
5090 value = TREE_INT_CST_LOW (local->val);
5091 else
5093 gcc_assert (shift < HOST_BITS_PER_DOUBLE_INT);
5094 value = TREE_INT_CST_HIGH (local->val);
5095 shift -= HOST_BITS_PER_WIDE_INT;
5098 /* Get the result. This works only when:
5099 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
5100 local->byte |= (((value >> shift)
5101 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
5102 << (BITS_PER_UNIT - this_time - next_bit));
5104 else
5106 /* On little-endian machines,
5107 take first the least significant bits of the value
5108 and pack them starting at the least significant
5109 bits of the bytes. */
5110 shift = next_offset - byte_relative_ebitpos;
5112 /* Don't try to take a bunch of bits that cross
5113 the word boundary in the INTEGER_CST. We can
5114 only select bits from the LOW or HIGH part
5115 not from both. */
5116 if (shift < HOST_BITS_PER_WIDE_INT
5117 && shift + this_time > HOST_BITS_PER_WIDE_INT)
5118 this_time = (HOST_BITS_PER_WIDE_INT - shift);
5120 /* Now get the bits from the appropriate constant word. */
5121 if (shift < HOST_BITS_PER_WIDE_INT)
5122 value = TREE_INT_CST_LOW (local->val);
5123 else
5125 gcc_assert (shift < HOST_BITS_PER_DOUBLE_INT);
5126 value = TREE_INT_CST_HIGH (local->val);
5127 shift -= HOST_BITS_PER_WIDE_INT;
5130 /* Get the result. This works only when:
5131 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
5132 local->byte |= (((value >> shift)
5133 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
5134 << next_bit);
5137 next_offset += this_time;
5138 local->byte_buffer_in_use = true;
5142 /* Subroutine of output_constant, used for CONSTRUCTORs (aggregate constants).
5143 Generate at least SIZE bytes, padding if necessary. OUTER designates the
5144 caller output state of relevance in recursive invocations. */
5146 static unsigned HOST_WIDE_INT
5147 output_constructor (tree exp, unsigned HOST_WIDE_INT size,
5148 unsigned int align, oc_outer_state *outer)
5150 unsigned HOST_WIDE_INT cnt;
5151 constructor_elt *ce;
5153 oc_local_state local;
5155 /* Setup our local state to communicate with helpers. */
5156 local.exp = exp;
5157 local.type = TREE_TYPE (exp);
5158 local.size = size;
5159 local.align = align;
5160 if (TREE_CODE (local.type) == ARRAY_TYPE && TYPE_DOMAIN (local.type))
5161 local.min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (local.type));
5162 else
5163 local.min_index = NULL_TREE;
5165 local.total_bytes = 0;
5166 local.byte_buffer_in_use = outer != NULL;
5167 local.byte = outer ? outer->byte : 0;
5168 local.last_relative_index = -1;
5170 gcc_assert (HOST_BITS_PER_WIDE_INT >= BITS_PER_UNIT);
5172 /* As CE goes through the elements of the constant, FIELD goes through the
5173 structure fields if the constant is a structure. If the constant is a
5174 union, we override this by getting the field from the TREE_LIST element.
5175 But the constant could also be an array. Then FIELD is zero.
5177 There is always a maximum of one element in the chain LINK for unions
5178 (even if the initializer in a source program incorrectly contains
5179 more one). */
5181 if (TREE_CODE (local.type) == RECORD_TYPE)
5182 local.field = TYPE_FIELDS (local.type);
5183 else
5184 local.field = NULL_TREE;
5186 for (cnt = 0;
5187 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), cnt, &ce);
5188 cnt++, local.field = local.field ? DECL_CHAIN (local.field) : 0)
5190 local.val = ce->value;
5191 local.index = NULL_TREE;
5193 /* The element in a union constructor specifies the proper field
5194 or index. */
5195 if (RECORD_OR_UNION_TYPE_P (local.type) && ce->index != NULL_TREE)
5196 local.field = ce->index;
5198 else if (TREE_CODE (local.type) == ARRAY_TYPE)
5199 local.index = ce->index;
5201 if (local.field && flag_verbose_asm)
5202 fprintf (asm_out_file, "%s %s:\n",
5203 ASM_COMMENT_START,
5204 DECL_NAME (local.field)
5205 ? IDENTIFIER_POINTER (DECL_NAME (local.field))
5206 : "<anonymous>");
5208 /* Eliminate the marker that makes a cast not be an lvalue. */
5209 if (local.val != NULL_TREE)
5210 STRIP_NOPS (local.val);
5212 /* Output the current element, using the appropriate helper ... */
5214 /* For an array slice not part of an outer bitfield. */
5215 if (!outer
5216 && local.index != NULL_TREE
5217 && TREE_CODE (local.index) == RANGE_EXPR)
5218 output_constructor_array_range (&local);
5220 /* For a field that is neither a true bitfield nor part of an outer one,
5221 known to be at least byte aligned and multiple-of-bytes long. */
5222 else if (!outer
5223 && (local.field == NULL_TREE
5224 || !CONSTRUCTOR_BITFIELD_P (local.field)))
5225 output_constructor_regular_field (&local);
5227 /* For a true bitfield or part of an outer one. Only INTEGER_CSTs are
5228 supported for scalar fields, so we may need to convert first. */
5229 else
5231 if (TREE_CODE (local.val) == REAL_CST)
5232 local.val
5233 = fold_unary (VIEW_CONVERT_EXPR,
5234 build_nonstandard_integer_type
5235 (TYPE_PRECISION (TREE_TYPE (local.val)), 0),
5236 local.val);
5237 output_constructor_bitfield (&local, outer ? outer->bit_offset : 0);
5241 /* If we are not at toplevel, save the pending data for our caller.
5242 Otherwise output the pending data and padding zeros as needed. */
5243 if (outer)
5244 outer->byte = local.byte;
5245 else
5247 if (local.byte_buffer_in_use)
5249 assemble_integer (GEN_INT (local.byte), 1, BITS_PER_UNIT, 1);
5250 local.total_bytes++;
5253 if ((unsigned HOST_WIDE_INT)local.total_bytes < local.size)
5255 assemble_zeros (local.size - local.total_bytes);
5256 local.total_bytes = local.size;
5260 return local.total_bytes;
5263 /* Mark DECL as weak. */
5265 static void
5266 mark_weak (tree decl)
5268 DECL_WEAK (decl) = 1;
5270 if (DECL_RTL_SET_P (decl)
5271 && MEM_P (DECL_RTL (decl))
5272 && XEXP (DECL_RTL (decl), 0)
5273 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == SYMBOL_REF)
5274 SYMBOL_REF_WEAK (XEXP (DECL_RTL (decl), 0)) = 1;
5277 /* Merge weak status between NEWDECL and OLDDECL. */
5279 void
5280 merge_weak (tree newdecl, tree olddecl)
5282 if (DECL_WEAK (newdecl) == DECL_WEAK (olddecl))
5284 if (DECL_WEAK (newdecl) && TARGET_SUPPORTS_WEAK)
5286 tree *pwd;
5287 /* We put the NEWDECL on the weak_decls list at some point
5288 and OLDDECL as well. Keep just OLDDECL on the list. */
5289 for (pwd = &weak_decls; *pwd; pwd = &TREE_CHAIN (*pwd))
5290 if (TREE_VALUE (*pwd) == newdecl)
5292 *pwd = TREE_CHAIN (*pwd);
5293 break;
5296 return;
5299 if (DECL_WEAK (newdecl))
5301 tree wd;
5303 /* NEWDECL is weak, but OLDDECL is not. */
5305 /* If we already output the OLDDECL, we're in trouble; we can't
5306 go back and make it weak. This should never happen in
5307 unit-at-a-time compilation. */
5308 gcc_assert (!TREE_ASM_WRITTEN (olddecl));
5310 /* If we've already generated rtl referencing OLDDECL, we may
5311 have done so in a way that will not function properly with
5312 a weak symbol. Again in unit-at-a-time this should be
5313 impossible. */
5314 gcc_assert (!TREE_USED (olddecl)
5315 || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (olddecl)));
5317 if (TARGET_SUPPORTS_WEAK)
5319 /* We put the NEWDECL on the weak_decls list at some point.
5320 Replace it with the OLDDECL. */
5321 for (wd = weak_decls; wd; wd = TREE_CHAIN (wd))
5322 if (TREE_VALUE (wd) == newdecl)
5324 TREE_VALUE (wd) = olddecl;
5325 break;
5327 /* We may not find the entry on the list. If NEWDECL is a
5328 weak alias, then we will have already called
5329 globalize_decl to remove the entry; in that case, we do
5330 not need to do anything. */
5333 /* Make the OLDDECL weak; it's OLDDECL that we'll be keeping. */
5334 mark_weak (olddecl);
5336 else
5337 /* OLDDECL was weak, but NEWDECL was not explicitly marked as
5338 weak. Just update NEWDECL to indicate that it's weak too. */
5339 mark_weak (newdecl);
5342 /* Declare DECL to be a weak symbol. */
5344 void
5345 declare_weak (tree decl)
5347 gcc_assert (TREE_CODE (decl) != FUNCTION_DECL || !TREE_ASM_WRITTEN (decl));
5348 if (! TREE_PUBLIC (decl))
5349 error ("weak declaration of %q+D must be public", decl);
5350 else if (!TARGET_SUPPORTS_WEAK)
5351 warning (0, "weak declaration of %q+D not supported", decl);
5353 mark_weak (decl);
5354 if (!lookup_attribute ("weak", DECL_ATTRIBUTES (decl)))
5355 DECL_ATTRIBUTES (decl)
5356 = tree_cons (get_identifier ("weak"), NULL, DECL_ATTRIBUTES (decl));
5359 static void
5360 weak_finish_1 (tree decl)
5362 #if defined (ASM_WEAKEN_DECL) || defined (ASM_WEAKEN_LABEL)
5363 const char *const name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5364 #endif
5366 if (! TREE_USED (decl))
5367 return;
5369 #ifdef ASM_WEAKEN_DECL
5370 ASM_WEAKEN_DECL (asm_out_file, decl, name, NULL);
5371 #else
5372 #ifdef ASM_WEAKEN_LABEL
5373 ASM_WEAKEN_LABEL (asm_out_file, name);
5374 #else
5375 #ifdef ASM_OUTPUT_WEAK_ALIAS
5377 static bool warn_once = 0;
5378 if (! warn_once)
5380 warning (0, "only weak aliases are supported in this configuration");
5381 warn_once = 1;
5383 return;
5385 #endif
5386 #endif
5387 #endif
5390 /* Fiven an assembly name, find the decl it is associated with. */
5391 static tree
5392 find_decl (tree target)
5394 symtab_node *node = symtab_node_for_asm (target);
5395 if (node)
5396 return node->decl;
5397 return NULL_TREE;
5400 /* This TREE_LIST contains weakref targets. */
5402 static GTY(()) tree weakref_targets;
5404 /* Emit any pending weak declarations. */
5406 void
5407 weak_finish (void)
5409 tree t;
5411 for (t = weakref_targets; t; t = TREE_CHAIN (t))
5413 tree alias_decl = TREE_PURPOSE (t);
5414 tree target = ultimate_transparent_alias_target (&TREE_VALUE (t));
5416 if (! TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (alias_decl)))
5417 /* Remove alias_decl from the weak list, but leave entries for
5418 the target alone. */
5419 target = NULL_TREE;
5420 #ifndef ASM_OUTPUT_WEAKREF
5421 else if (! TREE_SYMBOL_REFERENCED (target))
5423 /* Use ASM_WEAKEN_LABEL only if ASM_WEAKEN_DECL is not
5424 defined, otherwise we and weak_finish_1 would use
5425 different macros. */
5426 # if defined ASM_WEAKEN_LABEL && ! defined ASM_WEAKEN_DECL
5427 ASM_WEAKEN_LABEL (asm_out_file, IDENTIFIER_POINTER (target));
5428 # else
5429 tree decl = find_decl (target);
5431 if (! decl)
5433 decl = build_decl (DECL_SOURCE_LOCATION (alias_decl),
5434 TREE_CODE (alias_decl), target,
5435 TREE_TYPE (alias_decl));
5437 DECL_EXTERNAL (decl) = 1;
5438 TREE_PUBLIC (decl) = 1;
5439 DECL_ARTIFICIAL (decl) = 1;
5440 TREE_NOTHROW (decl) = TREE_NOTHROW (alias_decl);
5441 TREE_USED (decl) = 1;
5444 weak_finish_1 (decl);
5445 # endif
5447 #endif
5450 tree *p;
5451 tree t2;
5453 /* Remove the alias and the target from the pending weak list
5454 so that we do not emit any .weak directives for the former,
5455 nor multiple .weak directives for the latter. */
5456 for (p = &weak_decls; (t2 = *p) ; )
5458 if (TREE_VALUE (t2) == alias_decl
5459 || target == DECL_ASSEMBLER_NAME (TREE_VALUE (t2)))
5460 *p = TREE_CHAIN (t2);
5461 else
5462 p = &TREE_CHAIN (t2);
5465 /* Remove other weakrefs to the same target, to speed things up. */
5466 for (p = &TREE_CHAIN (t); (t2 = *p) ; )
5468 if (target == ultimate_transparent_alias_target (&TREE_VALUE (t2)))
5469 *p = TREE_CHAIN (t2);
5470 else
5471 p = &TREE_CHAIN (t2);
5476 for (t = weak_decls; t; t = TREE_CHAIN (t))
5478 tree decl = TREE_VALUE (t);
5480 weak_finish_1 (decl);
5484 /* Emit the assembly bits to indicate that DECL is globally visible. */
5486 static void
5487 globalize_decl (tree decl)
5490 #if defined (ASM_WEAKEN_LABEL) || defined (ASM_WEAKEN_DECL)
5491 if (DECL_WEAK (decl))
5493 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
5494 tree *p, t;
5496 #ifdef ASM_WEAKEN_DECL
5497 ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
5498 #else
5499 ASM_WEAKEN_LABEL (asm_out_file, name);
5500 #endif
5502 /* Remove this function from the pending weak list so that
5503 we do not emit multiple .weak directives for it. */
5504 for (p = &weak_decls; (t = *p) ; )
5506 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
5507 *p = TREE_CHAIN (t);
5508 else
5509 p = &TREE_CHAIN (t);
5512 /* Remove weakrefs to the same target from the pending weakref
5513 list, for the same reason. */
5514 for (p = &weakref_targets; (t = *p) ; )
5516 if (DECL_ASSEMBLER_NAME (decl)
5517 == ultimate_transparent_alias_target (&TREE_VALUE (t)))
5518 *p = TREE_CHAIN (t);
5519 else
5520 p = &TREE_CHAIN (t);
5523 return;
5525 #endif
5527 targetm.asm_out.globalize_decl_name (asm_out_file, decl);
5530 vec<alias_pair, va_gc> *alias_pairs;
5532 /* Output the assembler code for a define (equate) using ASM_OUTPUT_DEF
5533 or ASM_OUTPUT_DEF_FROM_DECLS. The function defines the symbol whose
5534 tree node is DECL to have the value of the tree node TARGET. */
5536 void
5537 do_assemble_alias (tree decl, tree target)
5539 /* Emulated TLS had better not get this var. */
5540 gcc_assert (!(!targetm.have_tls
5541 && TREE_CODE (decl) == VAR_DECL
5542 && DECL_THREAD_LOCAL_P (decl)));
5544 if (TREE_ASM_WRITTEN (decl))
5545 return;
5547 /* We must force creation of DECL_RTL for debug info generation, even though
5548 we don't use it here. */
5549 make_decl_rtl (decl);
5551 TREE_ASM_WRITTEN (decl) = 1;
5552 TREE_ASM_WRITTEN (DECL_ASSEMBLER_NAME (decl)) = 1;
5554 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
5556 ultimate_transparent_alias_target (&target);
5558 if (!TREE_SYMBOL_REFERENCED (target))
5559 weakref_targets = tree_cons (decl, target, weakref_targets);
5561 #ifdef ASM_OUTPUT_WEAKREF
5562 ASM_OUTPUT_WEAKREF (asm_out_file, decl,
5563 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
5564 IDENTIFIER_POINTER (target));
5565 #else
5566 if (!TARGET_SUPPORTS_WEAK)
5568 error_at (DECL_SOURCE_LOCATION (decl),
5569 "weakref is not supported in this configuration");
5570 return;
5572 #endif
5573 return;
5576 #ifdef ASM_OUTPUT_DEF
5577 /* Make name accessible from other files, if appropriate. */
5579 if (TREE_PUBLIC (decl))
5581 globalize_decl (decl);
5582 maybe_assemble_visibility (decl);
5584 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
5586 #if defined (ASM_OUTPUT_TYPE_DIRECTIVE)
5587 if (targetm.has_ifunc_p ())
5588 ASM_OUTPUT_TYPE_DIRECTIVE
5589 (asm_out_file, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
5590 IFUNC_ASM_TYPE);
5591 else
5592 #endif
5593 error_at (DECL_SOURCE_LOCATION (decl),
5594 "ifunc is not supported on this target");
5597 # ifdef ASM_OUTPUT_DEF_FROM_DECLS
5598 ASM_OUTPUT_DEF_FROM_DECLS (asm_out_file, decl, target);
5599 # else
5600 ASM_OUTPUT_DEF (asm_out_file,
5601 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
5602 IDENTIFIER_POINTER (target));
5603 # endif
5604 #elif defined (ASM_OUTPUT_WEAK_ALIAS) || defined (ASM_WEAKEN_DECL)
5606 const char *name;
5607 tree *p, t;
5609 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5610 # ifdef ASM_WEAKEN_DECL
5611 ASM_WEAKEN_DECL (asm_out_file, decl, name, IDENTIFIER_POINTER (target));
5612 # else
5613 ASM_OUTPUT_WEAK_ALIAS (asm_out_file, name, IDENTIFIER_POINTER (target));
5614 # endif
5615 /* Remove this function from the pending weak list so that
5616 we do not emit multiple .weak directives for it. */
5617 for (p = &weak_decls; (t = *p) ; )
5618 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
5619 *p = TREE_CHAIN (t);
5620 else
5621 p = &TREE_CHAIN (t);
5623 /* Remove weakrefs to the same target from the pending weakref
5624 list, for the same reason. */
5625 for (p = &weakref_targets; (t = *p) ; )
5627 if (DECL_ASSEMBLER_NAME (decl)
5628 == ultimate_transparent_alias_target (&TREE_VALUE (t)))
5629 *p = TREE_CHAIN (t);
5630 else
5631 p = &TREE_CHAIN (t);
5634 #endif
5637 /* Emit an assembler directive to make the symbol for DECL an alias to
5638 the symbol for TARGET. */
5640 void
5641 assemble_alias (tree decl, tree target)
5643 tree target_decl;
5645 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
5647 tree alias = DECL_ASSEMBLER_NAME (decl);
5649 ultimate_transparent_alias_target (&target);
5651 if (alias == target)
5652 error ("weakref %q+D ultimately targets itself", decl);
5653 if (TREE_PUBLIC (decl))
5654 error ("weakref %q+D must have static linkage", decl);
5656 else
5658 #if !defined (ASM_OUTPUT_DEF)
5659 # if !defined(ASM_OUTPUT_WEAK_ALIAS) && !defined (ASM_WEAKEN_DECL)
5660 error_at (DECL_SOURCE_LOCATION (decl),
5661 "alias definitions not supported in this configuration");
5662 TREE_ASM_WRITTEN (decl) = 1;
5663 return;
5664 # else
5665 if (!DECL_WEAK (decl))
5667 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
5668 error_at (DECL_SOURCE_LOCATION (decl),
5669 "ifunc is not supported in this configuration");
5670 else
5671 error_at (DECL_SOURCE_LOCATION (decl),
5672 "only weak aliases are supported in this configuration");
5673 TREE_ASM_WRITTEN (decl) = 1;
5674 return;
5676 # endif
5677 #endif
5679 TREE_USED (decl) = 1;
5681 /* Allow aliases to aliases. */
5682 if (TREE_CODE (decl) == FUNCTION_DECL)
5683 cgraph_get_create_node (decl)->alias = true;
5684 else
5685 varpool_node_for_decl (decl)->alias = true;
5687 /* If the target has already been emitted, we don't have to queue the
5688 alias. This saves a tad of memory. */
5689 if (cgraph_global_info_ready)
5690 target_decl = find_decl (target);
5691 else
5692 target_decl= NULL;
5693 if ((target_decl && TREE_ASM_WRITTEN (target_decl))
5694 || cgraph_state >= CGRAPH_STATE_EXPANSION)
5695 do_assemble_alias (decl, target);
5696 else
5698 alias_pair p = {decl, target};
5699 vec_safe_push (alias_pairs, p);
5703 /* Record and output a table of translations from original function
5704 to its transaction aware clone. Note that tm_pure functions are
5705 considered to be their own clone. */
5707 static GTY((if_marked ("tree_map_marked_p"), param_is (struct tree_map)))
5708 htab_t tm_clone_hash;
5710 void
5711 record_tm_clone_pair (tree o, tree n)
5713 struct tree_map **slot, *h;
5715 if (tm_clone_hash == NULL)
5716 tm_clone_hash = htab_create_ggc (32, tree_map_hash, tree_map_eq, 0);
5718 h = ggc_alloc_tree_map ();
5719 h->hash = htab_hash_pointer (o);
5720 h->base.from = o;
5721 h->to = n;
5723 slot = (struct tree_map **)
5724 htab_find_slot_with_hash (tm_clone_hash, h, h->hash, INSERT);
5725 *slot = h;
5728 tree
5729 get_tm_clone_pair (tree o)
5731 if (tm_clone_hash)
5733 struct tree_map *h, in;
5735 in.base.from = o;
5736 in.hash = htab_hash_pointer (o);
5737 h = (struct tree_map *) htab_find_with_hash (tm_clone_hash,
5738 &in, in.hash);
5739 if (h)
5740 return h->to;
5742 return NULL_TREE;
5745 typedef struct tm_alias_pair
5747 unsigned int uid;
5748 tree from;
5749 tree to;
5750 } tm_alias_pair;
5753 /* Helper function for finish_tm_clone_pairs. Dump a hash table entry
5754 into a VEC in INFO. */
5756 static int
5757 dump_tm_clone_to_vec (void **slot, void *info)
5759 struct tree_map *map = (struct tree_map *) *slot;
5760 vec<tm_alias_pair> *tm_alias_pairs = (vec<tm_alias_pair> *) info;
5761 tm_alias_pair p = {DECL_UID (map->base.from), map->base.from, map->to};
5762 tm_alias_pairs->safe_push (p);
5763 return 1;
5766 /* Dump the actual pairs to the .tm_clone_table section. */
5768 static void
5769 dump_tm_clone_pairs (vec<tm_alias_pair> tm_alias_pairs)
5771 unsigned i;
5772 tm_alias_pair *p;
5773 bool switched = false;
5775 FOR_EACH_VEC_ELT (tm_alias_pairs, i, p)
5777 tree src = p->from;
5778 tree dst = p->to;
5779 struct cgraph_node *src_n = cgraph_get_node (src);
5780 struct cgraph_node *dst_n = cgraph_get_node (dst);
5782 /* The function ipa_tm_create_version() marks the clone as needed if
5783 the original function was needed. But we also mark the clone as
5784 needed if we ever called the clone indirectly through
5785 TM_GETTMCLONE. If neither of these are true, we didn't generate
5786 a clone, and we didn't call it indirectly... no sense keeping it
5787 in the clone table. */
5788 if (!dst_n || !dst_n->definition)
5789 continue;
5791 /* This covers the case where we have optimized the original
5792 function away, and only access the transactional clone. */
5793 if (!src_n || !src_n->definition)
5794 continue;
5796 if (!switched)
5798 switch_to_section (targetm.asm_out.tm_clone_table_section ());
5799 assemble_align (POINTER_SIZE);
5800 switched = true;
5803 assemble_integer (XEXP (DECL_RTL (src), 0),
5804 POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
5805 assemble_integer (XEXP (DECL_RTL (dst), 0),
5806 POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
5810 /* Provide a default for the tm_clone_table section. */
5812 section *
5813 default_clone_table_section (void)
5815 return get_named_section (NULL, ".tm_clone_table", 3);
5818 /* Helper comparison function for qsorting by the DECL_UID stored in
5819 alias_pair->emitted_diags. */
5821 static int
5822 tm_alias_pair_cmp (const void *x, const void *y)
5824 const tm_alias_pair *p1 = (const tm_alias_pair *) x;
5825 const tm_alias_pair *p2 = (const tm_alias_pair *) y;
5826 if (p1->uid < p2->uid)
5827 return -1;
5828 if (p1->uid > p2->uid)
5829 return 1;
5830 return 0;
5833 void
5834 finish_tm_clone_pairs (void)
5836 vec<tm_alias_pair> tm_alias_pairs = vNULL;
5838 if (tm_clone_hash == NULL)
5839 return;
5841 /* We need a determenistic order for the .tm_clone_table, otherwise
5842 we will get bootstrap comparison failures, so dump the hash table
5843 to a vector, sort it, and dump the vector. */
5845 /* Dump the hashtable to a vector. */
5846 htab_traverse_noresize (tm_clone_hash, dump_tm_clone_to_vec,
5847 (void *) &tm_alias_pairs);
5848 /* Sort it. */
5849 tm_alias_pairs.qsort (tm_alias_pair_cmp);
5851 /* Dump it. */
5852 dump_tm_clone_pairs (tm_alias_pairs);
5854 htab_delete (tm_clone_hash);
5855 tm_clone_hash = NULL;
5856 tm_alias_pairs.release ();
5860 /* Emit an assembler directive to set symbol for DECL visibility to
5861 the visibility type VIS, which must not be VISIBILITY_DEFAULT. */
5863 void
5864 default_assemble_visibility (tree decl ATTRIBUTE_UNUSED,
5865 int vis ATTRIBUTE_UNUSED)
5867 #ifdef HAVE_GAS_HIDDEN
5868 static const char * const visibility_types[] = {
5869 NULL, "protected", "hidden", "internal"
5872 const char *name, *type;
5874 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5875 type = visibility_types[vis];
5877 fprintf (asm_out_file, "\t.%s\t", type);
5878 assemble_name (asm_out_file, name);
5879 fprintf (asm_out_file, "\n");
5880 #else
5881 if (!DECL_ARTIFICIAL (decl))
5882 warning (OPT_Wattributes, "visibility attribute not supported "
5883 "in this configuration; ignored");
5884 #endif
5887 /* A helper function to call assemble_visibility when needed for a decl. */
5890 maybe_assemble_visibility (tree decl)
5892 enum symbol_visibility vis = DECL_VISIBILITY (decl);
5894 if (vis != VISIBILITY_DEFAULT)
5896 targetm.asm_out.assemble_visibility (decl, vis);
5897 return 1;
5899 else
5900 return 0;
5903 /* Returns 1 if the target configuration supports defining public symbols
5904 so that one of them will be chosen at link time instead of generating a
5905 multiply-defined symbol error, whether through the use of weak symbols or
5906 a target-specific mechanism for having duplicates discarded. */
5909 supports_one_only (void)
5911 if (SUPPORTS_ONE_ONLY)
5912 return 1;
5913 return TARGET_SUPPORTS_WEAK;
5916 /* Set up DECL as a public symbol that can be defined in multiple
5917 translation units without generating a linker error. */
5919 void
5920 make_decl_one_only (tree decl, tree comdat_group)
5922 gcc_assert (TREE_CODE (decl) == VAR_DECL
5923 || TREE_CODE (decl) == FUNCTION_DECL);
5925 TREE_PUBLIC (decl) = 1;
5927 if (SUPPORTS_ONE_ONLY)
5929 #ifdef MAKE_DECL_ONE_ONLY
5930 MAKE_DECL_ONE_ONLY (decl);
5931 #endif
5932 DECL_COMDAT_GROUP (decl) = comdat_group;
5934 else if (TREE_CODE (decl) == VAR_DECL
5935 && (DECL_INITIAL (decl) == 0 || DECL_INITIAL (decl) == error_mark_node))
5936 DECL_COMMON (decl) = 1;
5937 else
5939 gcc_assert (TARGET_SUPPORTS_WEAK);
5940 DECL_WEAK (decl) = 1;
5944 void
5945 init_varasm_once (void)
5947 section_htab = htab_create_ggc (31, section_entry_hash,
5948 section_entry_eq, NULL);
5949 object_block_htab = htab_create_ggc (31, object_block_entry_hash,
5950 object_block_entry_eq, NULL);
5951 const_desc_htab = htab_create_ggc (1009, const_desc_hash,
5952 const_desc_eq, NULL);
5954 shared_constant_pool = create_constant_pool ();
5956 #ifdef TEXT_SECTION_ASM_OP
5957 text_section = get_unnamed_section (SECTION_CODE, output_section_asm_op,
5958 TEXT_SECTION_ASM_OP);
5959 #endif
5961 #ifdef DATA_SECTION_ASM_OP
5962 data_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
5963 DATA_SECTION_ASM_OP);
5964 #endif
5966 #ifdef SDATA_SECTION_ASM_OP
5967 sdata_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
5968 SDATA_SECTION_ASM_OP);
5969 #endif
5971 #ifdef READONLY_DATA_SECTION_ASM_OP
5972 readonly_data_section = get_unnamed_section (0, output_section_asm_op,
5973 READONLY_DATA_SECTION_ASM_OP);
5974 #endif
5976 #ifdef CTORS_SECTION_ASM_OP
5977 ctors_section = get_unnamed_section (0, output_section_asm_op,
5978 CTORS_SECTION_ASM_OP);
5979 #endif
5981 #ifdef DTORS_SECTION_ASM_OP
5982 dtors_section = get_unnamed_section (0, output_section_asm_op,
5983 DTORS_SECTION_ASM_OP);
5984 #endif
5986 #ifdef BSS_SECTION_ASM_OP
5987 bss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
5988 output_section_asm_op,
5989 BSS_SECTION_ASM_OP);
5990 #endif
5992 #ifdef SBSS_SECTION_ASM_OP
5993 sbss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
5994 output_section_asm_op,
5995 SBSS_SECTION_ASM_OP);
5996 #endif
5998 tls_comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
5999 | SECTION_COMMON, emit_tls_common);
6000 lcomm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6001 | SECTION_COMMON, emit_local);
6002 comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6003 | SECTION_COMMON, emit_common);
6005 #if defined ASM_OUTPUT_ALIGNED_BSS
6006 bss_noswitch_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS,
6007 emit_bss);
6008 #endif
6010 targetm.asm_out.init_sections ();
6012 if (readonly_data_section == NULL)
6013 readonly_data_section = text_section;
6015 #ifdef ASM_OUTPUT_EXTERNAL
6016 pending_assemble_externals_set = pointer_set_create ();
6017 #endif
6020 enum tls_model
6021 decl_default_tls_model (const_tree decl)
6023 enum tls_model kind;
6024 bool is_local;
6026 is_local = targetm.binds_local_p (decl);
6027 if (!flag_shlib)
6029 if (is_local)
6030 kind = TLS_MODEL_LOCAL_EXEC;
6031 else
6032 kind = TLS_MODEL_INITIAL_EXEC;
6035 /* Local dynamic is inefficient when we're not combining the
6036 parts of the address. */
6037 else if (optimize && is_local)
6038 kind = TLS_MODEL_LOCAL_DYNAMIC;
6039 else
6040 kind = TLS_MODEL_GLOBAL_DYNAMIC;
6041 if (kind < flag_tls_default)
6042 kind = flag_tls_default;
6044 return kind;
6047 /* Select a set of attributes for section NAME based on the properties
6048 of DECL and whether or not RELOC indicates that DECL's initializer
6049 might contain runtime relocations.
6051 We make the section read-only and executable for a function decl,
6052 read-only for a const data decl, and writable for a non-const data decl. */
6054 unsigned int
6055 default_section_type_flags (tree decl, const char *name, int reloc)
6057 unsigned int flags;
6059 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
6060 flags = SECTION_CODE;
6061 else if (decl)
6063 enum section_category category
6064 = categorize_decl_for_section (decl, reloc);
6065 if (decl_readonly_section_1 (category))
6066 flags = 0;
6067 else if (category == SECCAT_DATA_REL_RO
6068 || category == SECCAT_DATA_REL_RO_LOCAL)
6069 flags = SECTION_WRITE | SECTION_RELRO;
6070 else
6071 flags = SECTION_WRITE;
6073 else
6075 flags = SECTION_WRITE;
6076 if (strcmp (name, ".data.rel.ro") == 0
6077 || strcmp (name, ".data.rel.ro.local") == 0)
6078 flags |= SECTION_RELRO;
6081 if (decl && DECL_P (decl) && DECL_ONE_ONLY (decl))
6082 flags |= SECTION_LINKONCE;
6084 if (strcmp (name, ".vtable_map_vars") == 0)
6085 flags |= SECTION_LINKONCE;
6087 if (decl && TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
6088 flags |= SECTION_TLS | SECTION_WRITE;
6090 if (strcmp (name, ".bss") == 0
6091 || strncmp (name, ".bss.", 5) == 0
6092 || strncmp (name, ".gnu.linkonce.b.", 16) == 0
6093 || strcmp (name, ".sbss") == 0
6094 || strncmp (name, ".sbss.", 6) == 0
6095 || strncmp (name, ".gnu.linkonce.sb.", 17) == 0)
6096 flags |= SECTION_BSS;
6098 if (strcmp (name, ".tdata") == 0
6099 || strncmp (name, ".tdata.", 7) == 0
6100 || strncmp (name, ".gnu.linkonce.td.", 17) == 0)
6101 flags |= SECTION_TLS;
6103 if (strcmp (name, ".tbss") == 0
6104 || strncmp (name, ".tbss.", 6) == 0
6105 || strncmp (name, ".gnu.linkonce.tb.", 17) == 0)
6106 flags |= SECTION_TLS | SECTION_BSS;
6108 /* These three sections have special ELF types. They are neither
6109 SHT_PROGBITS nor SHT_NOBITS, so when changing sections we don't
6110 want to print a section type (@progbits or @nobits). If someone
6111 is silly enough to emit code or TLS variables to one of these
6112 sections, then don't handle them specially. */
6113 if (!(flags & (SECTION_CODE | SECTION_BSS | SECTION_TLS))
6114 && (strcmp (name, ".init_array") == 0
6115 || strcmp (name, ".fini_array") == 0
6116 || strcmp (name, ".preinit_array") == 0))
6117 flags |= SECTION_NOTYPE;
6119 return flags;
6122 /* Return true if the target supports some form of global BSS,
6123 either through bss_noswitch_section, or by selecting a BSS
6124 section in TARGET_ASM_SELECT_SECTION. */
6126 bool
6127 have_global_bss_p (void)
6129 return bss_noswitch_section || targetm.have_switchable_bss_sections;
6132 /* Output assembly to switch to section NAME with attribute FLAGS.
6133 Four variants for common object file formats. */
6135 void
6136 default_no_named_section (const char *name ATTRIBUTE_UNUSED,
6137 unsigned int flags ATTRIBUTE_UNUSED,
6138 tree decl ATTRIBUTE_UNUSED)
6140 /* Some object formats don't support named sections at all. The
6141 front-end should already have flagged this as an error. */
6142 gcc_unreachable ();
6145 #ifndef TLS_SECTION_ASM_FLAG
6146 #define TLS_SECTION_ASM_FLAG 'T'
6147 #endif
6149 void
6150 default_elf_asm_named_section (const char *name, unsigned int flags,
6151 tree decl ATTRIBUTE_UNUSED)
6153 char flagchars[10], *f = flagchars;
6155 /* If we have already declared this section, we can use an
6156 abbreviated form to switch back to it -- unless this section is
6157 part of a COMDAT groups, in which case GAS requires the full
6158 declaration every time. */
6159 if (!(HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6160 && (flags & SECTION_DECLARED))
6162 fprintf (asm_out_file, "\t.section\t%s\n", name);
6163 return;
6166 if (!(flags & SECTION_DEBUG))
6167 *f++ = 'a';
6168 if (flags & SECTION_EXCLUDE)
6169 *f++ = 'e';
6170 if (flags & SECTION_WRITE)
6171 *f++ = 'w';
6172 if (flags & SECTION_CODE)
6173 *f++ = 'x';
6174 if (flags & SECTION_SMALL)
6175 *f++ = 's';
6176 if (flags & SECTION_MERGE)
6177 *f++ = 'M';
6178 if (flags & SECTION_STRINGS)
6179 *f++ = 'S';
6180 if (flags & SECTION_TLS)
6181 *f++ = TLS_SECTION_ASM_FLAG;
6182 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6183 *f++ = 'G';
6184 *f = '\0';
6186 fprintf (asm_out_file, "\t.section\t%s,\"%s\"", name, flagchars);
6188 if (!(flags & SECTION_NOTYPE))
6190 const char *type;
6191 const char *format;
6193 if (flags & SECTION_BSS)
6194 type = "nobits";
6195 else
6196 type = "progbits";
6198 format = ",@%s";
6199 /* On platforms that use "@" as the assembly comment character,
6200 use "%" instead. */
6201 if (strcmp (ASM_COMMENT_START, "@") == 0)
6202 format = ",%%%s";
6203 fprintf (asm_out_file, format, type);
6205 if (flags & SECTION_ENTSIZE)
6206 fprintf (asm_out_file, ",%d", flags & SECTION_ENTSIZE);
6207 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6209 if (TREE_CODE (decl) == IDENTIFIER_NODE)
6210 fprintf (asm_out_file, ",%s,comdat", IDENTIFIER_POINTER (decl));
6211 else
6212 fprintf (asm_out_file, ",%s,comdat",
6213 IDENTIFIER_POINTER (DECL_COMDAT_GROUP (decl)));
6217 putc ('\n', asm_out_file);
6220 void
6221 default_coff_asm_named_section (const char *name, unsigned int flags,
6222 tree decl ATTRIBUTE_UNUSED)
6224 char flagchars[8], *f = flagchars;
6226 if (flags & SECTION_WRITE)
6227 *f++ = 'w';
6228 if (flags & SECTION_CODE)
6229 *f++ = 'x';
6230 *f = '\0';
6232 fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
6235 void
6236 default_pe_asm_named_section (const char *name, unsigned int flags,
6237 tree decl)
6239 default_coff_asm_named_section (name, flags, decl);
6241 if (flags & SECTION_LINKONCE)
6243 /* Functions may have been compiled at various levels of
6244 optimization so we can't use `same_size' here.
6245 Instead, have the linker pick one. */
6246 fprintf (asm_out_file, "\t.linkonce %s\n",
6247 (flags & SECTION_CODE ? "discard" : "same_size"));
6251 /* The lame default section selector. */
6253 section *
6254 default_select_section (tree decl, int reloc,
6255 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
6257 if (DECL_P (decl))
6259 if (decl_readonly_section (decl, reloc))
6260 return readonly_data_section;
6262 else if (TREE_CODE (decl) == CONSTRUCTOR)
6264 if (! ((flag_pic && reloc)
6265 || !TREE_READONLY (decl)
6266 || TREE_SIDE_EFFECTS (decl)
6267 || !TREE_CONSTANT (decl)))
6268 return readonly_data_section;
6270 else if (TREE_CODE (decl) == STRING_CST)
6271 return readonly_data_section;
6272 else if (! (flag_pic && reloc))
6273 return readonly_data_section;
6275 return data_section;
6278 enum section_category
6279 categorize_decl_for_section (const_tree decl, int reloc)
6281 enum section_category ret;
6283 if (TREE_CODE (decl) == FUNCTION_DECL)
6284 return SECCAT_TEXT;
6285 else if (TREE_CODE (decl) == STRING_CST)
6287 if ((flag_sanitize & SANITIZE_ADDRESS)
6288 && asan_protect_global (CONST_CAST_TREE (decl)))
6289 /* or !flag_merge_constants */
6290 return SECCAT_RODATA;
6291 else
6292 return SECCAT_RODATA_MERGE_STR;
6294 else if (TREE_CODE (decl) == VAR_DECL)
6296 if (bss_initializer_p (decl))
6297 ret = SECCAT_BSS;
6298 else if (! TREE_READONLY (decl)
6299 || TREE_SIDE_EFFECTS (decl)
6300 || ! TREE_CONSTANT (DECL_INITIAL (decl)))
6302 /* Here the reloc_rw_mask is not testing whether the section should
6303 be read-only or not, but whether the dynamic link will have to
6304 do something. If so, we wish to segregate the data in order to
6305 minimize cache misses inside the dynamic linker. */
6306 if (reloc & targetm.asm_out.reloc_rw_mask ())
6307 ret = reloc == 1 ? SECCAT_DATA_REL_LOCAL : SECCAT_DATA_REL;
6308 else
6309 ret = SECCAT_DATA;
6311 else if (reloc & targetm.asm_out.reloc_rw_mask ())
6312 ret = reloc == 1 ? SECCAT_DATA_REL_RO_LOCAL : SECCAT_DATA_REL_RO;
6313 else if (reloc || flag_merge_constants < 2
6314 || ((flag_sanitize & SANITIZE_ADDRESS)
6315 && asan_protect_global (CONST_CAST_TREE (decl))))
6316 /* C and C++ don't allow different variables to share the same
6317 location. -fmerge-all-constants allows even that (at the
6318 expense of not conforming). */
6319 ret = SECCAT_RODATA;
6320 else if (TREE_CODE (DECL_INITIAL (decl)) == STRING_CST)
6321 ret = SECCAT_RODATA_MERGE_STR_INIT;
6322 else
6323 ret = SECCAT_RODATA_MERGE_CONST;
6325 else if (TREE_CODE (decl) == CONSTRUCTOR)
6327 if ((reloc & targetm.asm_out.reloc_rw_mask ())
6328 || TREE_SIDE_EFFECTS (decl)
6329 || ! TREE_CONSTANT (decl))
6330 ret = SECCAT_DATA;
6331 else
6332 ret = SECCAT_RODATA;
6334 else
6335 ret = SECCAT_RODATA;
6337 /* There are no read-only thread-local sections. */
6338 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
6340 /* Note that this would be *just* SECCAT_BSS, except that there's
6341 no concept of a read-only thread-local-data section. */
6342 if (ret == SECCAT_BSS
6343 || (flag_zero_initialized_in_bss
6344 && initializer_zerop (DECL_INITIAL (decl))))
6345 ret = SECCAT_TBSS;
6346 else
6347 ret = SECCAT_TDATA;
6350 /* If the target uses small data sections, select it. */
6351 else if (targetm.in_small_data_p (decl))
6353 if (ret == SECCAT_BSS)
6354 ret = SECCAT_SBSS;
6355 else if (targetm.have_srodata_section && ret == SECCAT_RODATA)
6356 ret = SECCAT_SRODATA;
6357 else
6358 ret = SECCAT_SDATA;
6361 return ret;
6364 static bool
6365 decl_readonly_section_1 (enum section_category category)
6367 switch (category)
6369 case SECCAT_RODATA:
6370 case SECCAT_RODATA_MERGE_STR:
6371 case SECCAT_RODATA_MERGE_STR_INIT:
6372 case SECCAT_RODATA_MERGE_CONST:
6373 case SECCAT_SRODATA:
6374 return true;
6375 default:
6376 return false;
6380 bool
6381 decl_readonly_section (const_tree decl, int reloc)
6383 return decl_readonly_section_1 (categorize_decl_for_section (decl, reloc));
6386 /* Select a section based on the above categorization. */
6388 section *
6389 default_elf_select_section (tree decl, int reloc,
6390 unsigned HOST_WIDE_INT align)
6392 const char *sname;
6393 switch (categorize_decl_for_section (decl, reloc))
6395 case SECCAT_TEXT:
6396 /* We're not supposed to be called on FUNCTION_DECLs. */
6397 gcc_unreachable ();
6398 case SECCAT_RODATA:
6399 return readonly_data_section;
6400 case SECCAT_RODATA_MERGE_STR:
6401 return mergeable_string_section (decl, align, 0);
6402 case SECCAT_RODATA_MERGE_STR_INIT:
6403 return mergeable_string_section (DECL_INITIAL (decl), align, 0);
6404 case SECCAT_RODATA_MERGE_CONST:
6405 return mergeable_constant_section (DECL_MODE (decl), align, 0);
6406 case SECCAT_SRODATA:
6407 sname = ".sdata2";
6408 break;
6409 case SECCAT_DATA:
6410 return data_section;
6411 case SECCAT_DATA_REL:
6412 sname = ".data.rel";
6413 break;
6414 case SECCAT_DATA_REL_LOCAL:
6415 sname = ".data.rel.local";
6416 break;
6417 case SECCAT_DATA_REL_RO:
6418 sname = ".data.rel.ro";
6419 break;
6420 case SECCAT_DATA_REL_RO_LOCAL:
6421 sname = ".data.rel.ro.local";
6422 break;
6423 case SECCAT_SDATA:
6424 sname = ".sdata";
6425 break;
6426 case SECCAT_TDATA:
6427 sname = ".tdata";
6428 break;
6429 case SECCAT_BSS:
6430 if (bss_section)
6431 return bss_section;
6432 sname = ".bss";
6433 break;
6434 case SECCAT_SBSS:
6435 sname = ".sbss";
6436 break;
6437 case SECCAT_TBSS:
6438 sname = ".tbss";
6439 break;
6440 default:
6441 gcc_unreachable ();
6444 return get_named_section (decl, sname, reloc);
6447 /* Construct a unique section name based on the decl name and the
6448 categorization performed above. */
6450 void
6451 default_unique_section (tree decl, int reloc)
6453 /* We only need to use .gnu.linkonce if we don't have COMDAT groups. */
6454 bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
6455 const char *prefix, *name, *linkonce;
6456 char *string;
6458 switch (categorize_decl_for_section (decl, reloc))
6460 case SECCAT_TEXT:
6461 prefix = one_only ? ".t" : ".text";
6462 break;
6463 case SECCAT_RODATA:
6464 case SECCAT_RODATA_MERGE_STR:
6465 case SECCAT_RODATA_MERGE_STR_INIT:
6466 case SECCAT_RODATA_MERGE_CONST:
6467 prefix = one_only ? ".r" : ".rodata";
6468 break;
6469 case SECCAT_SRODATA:
6470 prefix = one_only ? ".s2" : ".sdata2";
6471 break;
6472 case SECCAT_DATA:
6473 prefix = one_only ? ".d" : ".data";
6474 break;
6475 case SECCAT_DATA_REL:
6476 prefix = one_only ? ".d.rel" : ".data.rel";
6477 break;
6478 case SECCAT_DATA_REL_LOCAL:
6479 prefix = one_only ? ".d.rel.local" : ".data.rel.local";
6480 break;
6481 case SECCAT_DATA_REL_RO:
6482 prefix = one_only ? ".d.rel.ro" : ".data.rel.ro";
6483 break;
6484 case SECCAT_DATA_REL_RO_LOCAL:
6485 prefix = one_only ? ".d.rel.ro.local" : ".data.rel.ro.local";
6486 break;
6487 case SECCAT_SDATA:
6488 prefix = one_only ? ".s" : ".sdata";
6489 break;
6490 case SECCAT_BSS:
6491 prefix = one_only ? ".b" : ".bss";
6492 break;
6493 case SECCAT_SBSS:
6494 prefix = one_only ? ".sb" : ".sbss";
6495 break;
6496 case SECCAT_TDATA:
6497 prefix = one_only ? ".td" : ".tdata";
6498 break;
6499 case SECCAT_TBSS:
6500 prefix = one_only ? ".tb" : ".tbss";
6501 break;
6502 default:
6503 gcc_unreachable ();
6506 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
6507 name = targetm.strip_name_encoding (name);
6509 /* If we're using one_only, then there needs to be a .gnu.linkonce
6510 prefix to the section name. */
6511 linkonce = one_only ? ".gnu.linkonce" : "";
6513 string = ACONCAT ((linkonce, prefix, ".", name, NULL));
6515 DECL_SECTION_NAME (decl) = build_string (strlen (string), string);
6518 /* Like compute_reloc_for_constant, except for an RTX. The return value
6519 is a mask for which bit 1 indicates a global relocation, and bit 0
6520 indicates a local relocation. */
6522 static int
6523 compute_reloc_for_rtx_1 (rtx *xp, void *data)
6525 int *preloc = (int *) data;
6526 rtx x = *xp;
6528 switch (GET_CODE (x))
6530 case SYMBOL_REF:
6531 *preloc |= SYMBOL_REF_LOCAL_P (x) ? 1 : 2;
6532 break;
6533 case LABEL_REF:
6534 *preloc |= 1;
6535 break;
6536 default:
6537 break;
6540 return 0;
6543 static int
6544 compute_reloc_for_rtx (rtx x)
6546 int reloc;
6548 switch (GET_CODE (x))
6550 case CONST:
6551 case SYMBOL_REF:
6552 case LABEL_REF:
6553 reloc = 0;
6554 for_each_rtx (&x, compute_reloc_for_rtx_1, &reloc);
6555 return reloc;
6557 default:
6558 return 0;
6562 section *
6563 default_select_rtx_section (enum machine_mode mode ATTRIBUTE_UNUSED,
6564 rtx x,
6565 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
6567 if (compute_reloc_for_rtx (x) & targetm.asm_out.reloc_rw_mask ())
6568 return data_section;
6569 else
6570 return readonly_data_section;
6573 section *
6574 default_elf_select_rtx_section (enum machine_mode mode, rtx x,
6575 unsigned HOST_WIDE_INT align)
6577 int reloc = compute_reloc_for_rtx (x);
6579 /* ??? Handle small data here somehow. */
6581 if (reloc & targetm.asm_out.reloc_rw_mask ())
6583 if (reloc == 1)
6584 return get_named_section (NULL, ".data.rel.ro.local", 1);
6585 else
6586 return get_named_section (NULL, ".data.rel.ro", 3);
6589 return mergeable_constant_section (mode, align, 0);
6592 /* Set the generally applicable flags on the SYMBOL_REF for EXP. */
6594 void
6595 default_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
6597 rtx symbol;
6598 int flags;
6600 /* Careful not to prod global register variables. */
6601 if (!MEM_P (rtl))
6602 return;
6603 symbol = XEXP (rtl, 0);
6604 if (GET_CODE (symbol) != SYMBOL_REF)
6605 return;
6607 flags = SYMBOL_REF_FLAGS (symbol) & SYMBOL_FLAG_HAS_BLOCK_INFO;
6608 if (TREE_CODE (decl) == FUNCTION_DECL)
6609 flags |= SYMBOL_FLAG_FUNCTION;
6610 if (targetm.binds_local_p (decl))
6611 flags |= SYMBOL_FLAG_LOCAL;
6612 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
6613 flags |= DECL_TLS_MODEL (decl) << SYMBOL_FLAG_TLS_SHIFT;
6614 else if (targetm.in_small_data_p (decl))
6615 flags |= SYMBOL_FLAG_SMALL;
6616 /* ??? Why is DECL_EXTERNAL ever set for non-PUBLIC names? Without
6617 being PUBLIC, the thing *must* be defined in this translation unit.
6618 Prevent this buglet from being propagated into rtl code as well. */
6619 if (DECL_P (decl) && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl))
6620 flags |= SYMBOL_FLAG_EXTERNAL;
6622 SYMBOL_REF_FLAGS (symbol) = flags;
6625 /* By default, we do nothing for encode_section_info, so we need not
6626 do anything but discard the '*' marker. */
6628 const char *
6629 default_strip_name_encoding (const char *str)
6631 return str + (*str == '*');
6634 #ifdef ASM_OUTPUT_DEF
6635 /* The default implementation of TARGET_ASM_OUTPUT_ANCHOR. Define the
6636 anchor relative to ".", the current section position. */
6638 void
6639 default_asm_output_anchor (rtx symbol)
6641 char buffer[100];
6643 sprintf (buffer, "*. + " HOST_WIDE_INT_PRINT_DEC,
6644 SYMBOL_REF_BLOCK_OFFSET (symbol));
6645 ASM_OUTPUT_DEF (asm_out_file, XSTR (symbol, 0), buffer);
6647 #endif
6649 /* The default implementation of TARGET_USE_ANCHORS_FOR_SYMBOL_P. */
6651 bool
6652 default_use_anchors_for_symbol_p (const_rtx symbol)
6654 section *sect;
6655 tree decl;
6657 /* Don't use anchors for mergeable sections. The linker might move
6658 the objects around. */
6659 sect = SYMBOL_REF_BLOCK (symbol)->sect;
6660 if (sect->common.flags & SECTION_MERGE)
6661 return false;
6663 /* Don't use anchors for small data sections. The small data register
6664 acts as an anchor for such sections. */
6665 if (sect->common.flags & SECTION_SMALL)
6666 return false;
6668 decl = SYMBOL_REF_DECL (symbol);
6669 if (decl && DECL_P (decl))
6671 /* Don't use section anchors for decls that might be defined or
6672 usurped by other modules. */
6673 if (TREE_PUBLIC (decl) && !decl_binds_to_current_def_p (decl))
6674 return false;
6676 /* Don't use section anchors for decls that will be placed in a
6677 small data section. */
6678 /* ??? Ideally, this check would be redundant with the SECTION_SMALL
6679 one above. The problem is that we only use SECTION_SMALL for
6680 sections that should be marked as small in the section directive. */
6681 if (targetm.in_small_data_p (decl))
6682 return false;
6684 return true;
6687 /* Return true when RESOLUTION indicate that symbol will be bound to the
6688 definition provided by current .o file. */
6690 static bool
6691 resolution_to_local_definition_p (enum ld_plugin_symbol_resolution resolution)
6693 return (resolution == LDPR_PREVAILING_DEF
6694 || resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
6695 || resolution == LDPR_PREVAILING_DEF_IRONLY);
6698 /* Return true when RESOLUTION indicate that symbol will be bound locally
6699 within current executable or DSO. */
6701 static bool
6702 resolution_local_p (enum ld_plugin_symbol_resolution resolution)
6704 return (resolution == LDPR_PREVAILING_DEF
6705 || resolution == LDPR_PREVAILING_DEF_IRONLY
6706 || resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
6707 || resolution == LDPR_PREEMPTED_REG
6708 || resolution == LDPR_PREEMPTED_IR
6709 || resolution == LDPR_RESOLVED_IR
6710 || resolution == LDPR_RESOLVED_EXEC);
6713 /* Assume ELF-ish defaults, since that's pretty much the most liberal
6714 wrt cross-module name binding. */
6716 bool
6717 default_binds_local_p (const_tree exp)
6719 return default_binds_local_p_1 (exp, flag_shlib);
6722 bool
6723 default_binds_local_p_1 (const_tree exp, int shlib)
6725 bool local_p;
6726 bool resolved_locally = false;
6727 bool resolved_to_local_def = false;
6729 /* With resolution file in hands, take look into resolutions.
6730 We can't just return true for resolved_locally symbols,
6731 because dynamic linking might overwrite symbols
6732 in shared libraries. */
6733 if (TREE_CODE (exp) == VAR_DECL && TREE_PUBLIC (exp)
6734 && (TREE_STATIC (exp) || DECL_EXTERNAL (exp)))
6736 varpool_node *vnode = varpool_get_node (exp);
6737 if (vnode && (resolution_local_p (vnode->resolution) || vnode->in_other_partition))
6738 resolved_locally = true;
6739 if (vnode
6740 && resolution_to_local_definition_p (vnode->resolution))
6741 resolved_to_local_def = true;
6743 else if (TREE_CODE (exp) == FUNCTION_DECL && TREE_PUBLIC (exp))
6745 struct cgraph_node *node = cgraph_get_node (exp);
6746 if (node
6747 && (resolution_local_p (node->resolution) || node->in_other_partition))
6748 resolved_locally = true;
6749 if (node
6750 && resolution_to_local_definition_p (node->resolution))
6751 resolved_to_local_def = true;
6754 /* A non-decl is an entry in the constant pool. */
6755 if (!DECL_P (exp))
6756 local_p = true;
6757 /* Weakrefs may not bind locally, even though the weakref itself is always
6758 static and therefore local. Similarly, the resolver for ifunc functions
6759 might resolve to a non-local function.
6760 FIXME: We can resolve the weakref case more curefuly by looking at the
6761 weakref alias. */
6762 else if (lookup_attribute ("weakref", DECL_ATTRIBUTES (exp))
6763 || (TREE_CODE (exp) == FUNCTION_DECL
6764 && lookup_attribute ("ifunc", DECL_ATTRIBUTES (exp))))
6765 local_p = false;
6766 /* Static variables are always local. */
6767 else if (! TREE_PUBLIC (exp))
6768 local_p = true;
6769 /* A variable is local if the user has said explicitly that it will
6770 be. */
6771 else if ((DECL_VISIBILITY_SPECIFIED (exp)
6772 || resolved_to_local_def)
6773 && DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
6774 local_p = true;
6775 /* Variables defined outside this object might not be local. */
6776 else if (DECL_EXTERNAL (exp) && !resolved_locally)
6777 local_p = false;
6778 /* If defined in this object and visibility is not default, must be
6779 local. */
6780 else if (DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
6781 local_p = true;
6782 /* Default visibility weak data can be overridden by a strong symbol
6783 in another module and so are not local. */
6784 else if (DECL_WEAK (exp)
6785 && !resolved_locally)
6786 local_p = false;
6787 /* If PIC, then assume that any global name can be overridden by
6788 symbols resolved from other modules. */
6789 else if (shlib)
6790 local_p = false;
6791 /* Uninitialized COMMON variable may be unified with symbols
6792 resolved from other modules. */
6793 else if (DECL_COMMON (exp)
6794 && !resolved_locally
6795 && (DECL_INITIAL (exp) == NULL
6796 || DECL_INITIAL (exp) == error_mark_node))
6797 local_p = false;
6798 /* Otherwise we're left with initialized (or non-common) global data
6799 which is of necessity defined locally. */
6800 else
6801 local_p = true;
6803 return local_p;
6806 /* Return true when references to DECL must bind to current definition in
6807 final executable.
6809 The condition is usually equivalent to whether the function binds to the
6810 current module (shared library or executable), that is to binds_local_p.
6811 We use this fact to avoid need for another target hook and implement
6812 the logic using binds_local_p and just special cases where
6813 decl_binds_to_current_def_p is stronger than binds_local_p. In particular
6814 the weak definitions (that can be overwritten at linktime by other
6815 definition from different object file) and when resolution info is available
6816 we simply use the knowledge passed to us by linker plugin. */
6817 bool
6818 decl_binds_to_current_def_p (tree decl)
6820 gcc_assert (DECL_P (decl));
6821 if (!targetm.binds_local_p (decl))
6822 return false;
6823 if (!TREE_PUBLIC (decl))
6824 return true;
6825 /* When resolution is available, just use it. */
6826 if (TREE_CODE (decl) == VAR_DECL
6827 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
6829 varpool_node *vnode = varpool_get_node (decl);
6830 if (vnode
6831 && vnode->resolution != LDPR_UNKNOWN)
6832 return resolution_to_local_definition_p (vnode->resolution);
6834 else if (TREE_CODE (decl) == FUNCTION_DECL)
6836 struct cgraph_node *node = cgraph_get_node (decl);
6837 if (node
6838 && node->resolution != LDPR_UNKNOWN)
6839 return resolution_to_local_definition_p (node->resolution);
6841 /* Otherwise we have to assume the worst for DECL_WEAK (hidden weaks
6842 binds locally but still can be overwritten), DECL_COMMON (can be merged
6843 with a non-common definition somewhere in the same module) or
6844 DECL_EXTERNAL.
6845 This rely on fact that binds_local_p behave as decl_replaceable_p
6846 for all other declaration types. */
6847 if (DECL_WEAK (decl))
6848 return false;
6849 if (DECL_COMMON (decl)
6850 && (DECL_INITIAL (decl) == NULL
6851 || DECL_INITIAL (decl) == error_mark_node))
6852 return false;
6853 if (DECL_EXTERNAL (decl))
6854 return false;
6855 return true;
6858 /* A replaceable function or variable is one which may be replaced
6859 at link-time with an entirely different definition, provided that the
6860 replacement has the same type. For example, functions declared
6861 with __attribute__((weak)) on most systems are replaceable.
6863 COMDAT functions are not replaceable, since all definitions of the
6864 function must be equivalent. It is important that COMDAT functions
6865 not be treated as replaceable so that use of C++ template
6866 instantiations is not penalized. */
6868 bool
6869 decl_replaceable_p (tree decl)
6871 gcc_assert (DECL_P (decl));
6872 if (!TREE_PUBLIC (decl) || DECL_COMDAT (decl))
6873 return false;
6874 return !decl_binds_to_current_def_p (decl);
6877 /* Default function to output code that will globalize a label. A
6878 target must define GLOBAL_ASM_OP or provide its own function to
6879 globalize a label. */
6880 #ifdef GLOBAL_ASM_OP
6881 void
6882 default_globalize_label (FILE * stream, const char *name)
6884 fputs (GLOBAL_ASM_OP, stream);
6885 assemble_name (stream, name);
6886 putc ('\n', stream);
6888 #endif /* GLOBAL_ASM_OP */
6890 /* Default function to output code that will globalize a declaration. */
6891 void
6892 default_globalize_decl_name (FILE * stream, tree decl)
6894 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
6895 targetm.asm_out.globalize_label (stream, name);
6898 /* Default function to output a label for unwind information. The
6899 default is to do nothing. A target that needs nonlocal labels for
6900 unwind information must provide its own function to do this. */
6901 void
6902 default_emit_unwind_label (FILE * stream ATTRIBUTE_UNUSED,
6903 tree decl ATTRIBUTE_UNUSED,
6904 int for_eh ATTRIBUTE_UNUSED,
6905 int empty ATTRIBUTE_UNUSED)
6909 /* Default function to output a label to divide up the exception table.
6910 The default is to do nothing. A target that needs/wants to divide
6911 up the table must provide it's own function to do this. */
6912 void
6913 default_emit_except_table_label (FILE * stream ATTRIBUTE_UNUSED)
6917 /* This is how to output an internal numbered label where PREFIX is
6918 the class of label and LABELNO is the number within the class. */
6920 void
6921 default_generate_internal_label (char *buf, const char *prefix,
6922 unsigned long labelno)
6924 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
6927 /* This is how to output an internal numbered label where PREFIX is
6928 the class of label and LABELNO is the number within the class. */
6930 void
6931 default_internal_label (FILE *stream, const char *prefix,
6932 unsigned long labelno)
6934 char *const buf = (char *) alloca (40 + strlen (prefix));
6935 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
6936 ASM_OUTPUT_INTERNAL_LABEL (stream, buf);
6940 /* The default implementation of ASM_DECLARE_CONSTANT_NAME. */
6942 void
6943 default_asm_declare_constant_name (FILE *file, const char *name,
6944 const_tree exp ATTRIBUTE_UNUSED,
6945 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
6947 assemble_label (file, name);
6950 /* This is the default behavior at the beginning of a file. It's
6951 controlled by two other target-hook toggles. */
6952 void
6953 default_file_start (void)
6955 if (targetm.asm_file_start_app_off
6956 && !(flag_verbose_asm || flag_debug_asm || flag_dump_rtl_in_asm))
6957 fputs (ASM_APP_OFF, asm_out_file);
6959 if (targetm.asm_file_start_file_directive)
6960 output_file_directive (asm_out_file, main_input_filename);
6963 /* This is a generic routine suitable for use as TARGET_ASM_FILE_END
6964 which emits a special section directive used to indicate whether or
6965 not this object file needs an executable stack. This is primarily
6966 a GNU extension to ELF but could be used on other targets. */
6968 int trampolines_created;
6970 void
6971 file_end_indicate_exec_stack (void)
6973 unsigned int flags = SECTION_DEBUG;
6974 if (trampolines_created)
6975 flags |= SECTION_CODE;
6977 switch_to_section (get_section (".note.GNU-stack", flags, NULL));
6980 /* Emit a special section directive to indicate that this object file
6981 was compiled with -fsplit-stack. This is used to let the linker
6982 detect calls between split-stack code and non-split-stack code, so
6983 that it can modify the split-stack code to allocate a sufficiently
6984 large stack. We emit another special section if there are any
6985 functions in this file which have the no_split_stack attribute, to
6986 prevent the linker from warning about being unable to convert the
6987 functions if they call non-split-stack code. */
6989 void
6990 file_end_indicate_split_stack (void)
6992 if (flag_split_stack)
6994 switch_to_section (get_section (".note.GNU-split-stack", SECTION_DEBUG,
6995 NULL));
6996 if (saw_no_split_stack)
6997 switch_to_section (get_section (".note.GNU-no-split-stack",
6998 SECTION_DEBUG, NULL));
7002 /* Output DIRECTIVE (a C string) followed by a newline. This is used as
7003 a get_unnamed_section callback. */
7005 void
7006 output_section_asm_op (const void *directive)
7008 fprintf (asm_out_file, "%s\n", (const char *) directive);
7011 /* Emit assembly code to switch to section NEW_SECTION. Do nothing if
7012 the current section is NEW_SECTION. */
7014 void
7015 switch_to_section (section *new_section)
7017 if (in_section == new_section)
7018 return;
7020 if (new_section->common.flags & SECTION_FORGET)
7021 in_section = NULL;
7022 else
7023 in_section = new_section;
7025 switch (SECTION_STYLE (new_section))
7027 case SECTION_NAMED:
7028 targetm.asm_out.named_section (new_section->named.name,
7029 new_section->named.common.flags,
7030 new_section->named.decl);
7031 break;
7033 case SECTION_UNNAMED:
7034 new_section->unnamed.callback (new_section->unnamed.data);
7035 break;
7037 case SECTION_NOSWITCH:
7038 gcc_unreachable ();
7039 break;
7042 new_section->common.flags |= SECTION_DECLARED;
7045 /* If block symbol SYMBOL has not yet been assigned an offset, place
7046 it at the end of its block. */
7048 void
7049 place_block_symbol (rtx symbol)
7051 unsigned HOST_WIDE_INT size, mask, offset;
7052 struct constant_descriptor_rtx *desc;
7053 unsigned int alignment;
7054 struct object_block *block;
7055 tree decl;
7057 gcc_assert (SYMBOL_REF_BLOCK (symbol));
7058 if (SYMBOL_REF_BLOCK_OFFSET (symbol) >= 0)
7059 return;
7061 /* Work out the symbol's size and alignment. */
7062 if (CONSTANT_POOL_ADDRESS_P (symbol))
7064 desc = SYMBOL_REF_CONSTANT (symbol);
7065 alignment = desc->align;
7066 size = GET_MODE_SIZE (desc->mode);
7068 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
7070 decl = SYMBOL_REF_DECL (symbol);
7071 alignment = DECL_ALIGN (decl);
7072 size = get_constant_size (DECL_INITIAL (decl));
7073 if ((flag_sanitize & SANITIZE_ADDRESS)
7074 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
7075 && asan_protect_global (DECL_INITIAL (decl)))
7076 size += asan_red_zone_size (size);
7078 else
7080 decl = SYMBOL_REF_DECL (symbol);
7081 alignment = get_variable_align (decl);
7082 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
7083 if ((flag_sanitize & SANITIZE_ADDRESS)
7084 && asan_protect_global (decl))
7086 size += asan_red_zone_size (size);
7087 alignment = MAX (alignment,
7088 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
7092 /* Calculate the object's offset from the start of the block. */
7093 block = SYMBOL_REF_BLOCK (symbol);
7094 mask = alignment / BITS_PER_UNIT - 1;
7095 offset = (block->size + mask) & ~mask;
7096 SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
7098 /* Record the block's new alignment and size. */
7099 block->alignment = MAX (block->alignment, alignment);
7100 block->size = offset + size;
7102 vec_safe_push (block->objects, symbol);
7105 /* Return the anchor that should be used to address byte offset OFFSET
7106 from the first object in BLOCK. MODEL is the TLS model used
7107 to access it. */
7110 get_section_anchor (struct object_block *block, HOST_WIDE_INT offset,
7111 enum tls_model model)
7113 char label[100];
7114 unsigned int begin, middle, end;
7115 unsigned HOST_WIDE_INT min_offset, max_offset, range, bias, delta;
7116 rtx anchor;
7118 /* Work out the anchor's offset. Use an offset of 0 for the first
7119 anchor so that we don't pessimize the case where we take the address
7120 of a variable at the beginning of the block. This is particularly
7121 useful when a block has only one variable assigned to it.
7123 We try to place anchors RANGE bytes apart, so there can then be
7124 anchors at +/-RANGE, +/-2 * RANGE, and so on, up to the limits of
7125 a ptr_mode offset. With some target settings, the lowest such
7126 anchor might be out of range for the lowest ptr_mode offset;
7127 likewise the highest anchor for the highest offset. Use anchors
7128 at the extreme ends of the ptr_mode range in such cases.
7130 All arithmetic uses unsigned integers in order to avoid
7131 signed overflow. */
7132 max_offset = (unsigned HOST_WIDE_INT) targetm.max_anchor_offset;
7133 min_offset = (unsigned HOST_WIDE_INT) targetm.min_anchor_offset;
7134 range = max_offset - min_offset + 1;
7135 if (range == 0)
7136 offset = 0;
7137 else
7139 bias = 1 << (GET_MODE_BITSIZE (ptr_mode) - 1);
7140 if (offset < 0)
7142 delta = -(unsigned HOST_WIDE_INT) offset + max_offset;
7143 delta -= delta % range;
7144 if (delta > bias)
7145 delta = bias;
7146 offset = (HOST_WIDE_INT) (-delta);
7148 else
7150 delta = (unsigned HOST_WIDE_INT) offset - min_offset;
7151 delta -= delta % range;
7152 if (delta > bias - 1)
7153 delta = bias - 1;
7154 offset = (HOST_WIDE_INT) delta;
7158 /* Do a binary search to see if there's already an anchor we can use.
7159 Set BEGIN to the new anchor's index if not. */
7160 begin = 0;
7161 end = vec_safe_length (block->anchors);
7162 while (begin != end)
7164 middle = (end + begin) / 2;
7165 anchor = (*block->anchors)[middle];
7166 if (SYMBOL_REF_BLOCK_OFFSET (anchor) > offset)
7167 end = middle;
7168 else if (SYMBOL_REF_BLOCK_OFFSET (anchor) < offset)
7169 begin = middle + 1;
7170 else if (SYMBOL_REF_TLS_MODEL (anchor) > model)
7171 end = middle;
7172 else if (SYMBOL_REF_TLS_MODEL (anchor) < model)
7173 begin = middle + 1;
7174 else
7175 return anchor;
7178 /* Create a new anchor with a unique label. */
7179 ASM_GENERATE_INTERNAL_LABEL (label, "LANCHOR", anchor_labelno++);
7180 anchor = create_block_symbol (ggc_strdup (label), block, offset);
7181 SYMBOL_REF_FLAGS (anchor) |= SYMBOL_FLAG_LOCAL | SYMBOL_FLAG_ANCHOR;
7182 SYMBOL_REF_FLAGS (anchor) |= model << SYMBOL_FLAG_TLS_SHIFT;
7184 /* Insert it at index BEGIN. */
7185 vec_safe_insert (block->anchors, begin, anchor);
7186 return anchor;
7189 /* Output the objects in BLOCK. */
7191 static void
7192 output_object_block (struct object_block *block)
7194 struct constant_descriptor_rtx *desc;
7195 unsigned int i;
7196 HOST_WIDE_INT offset;
7197 tree decl;
7198 rtx symbol;
7200 if (!block->objects)
7201 return;
7203 /* Switch to the section and make sure that the first byte is
7204 suitably aligned. */
7205 switch_to_section (block->sect);
7206 assemble_align (block->alignment);
7208 /* Define the values of all anchors relative to the current section
7209 position. */
7210 FOR_EACH_VEC_SAFE_ELT (block->anchors, i, symbol)
7211 targetm.asm_out.output_anchor (symbol);
7213 /* Output the objects themselves. */
7214 offset = 0;
7215 FOR_EACH_VEC_ELT (*block->objects, i, symbol)
7217 /* Move to the object's offset, padding with zeros if necessary. */
7218 assemble_zeros (SYMBOL_REF_BLOCK_OFFSET (symbol) - offset);
7219 offset = SYMBOL_REF_BLOCK_OFFSET (symbol);
7220 if (CONSTANT_POOL_ADDRESS_P (symbol))
7222 desc = SYMBOL_REF_CONSTANT (symbol);
7223 output_constant_pool_1 (desc, 1);
7224 offset += GET_MODE_SIZE (desc->mode);
7226 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
7228 HOST_WIDE_INT size;
7229 decl = SYMBOL_REF_DECL (symbol);
7230 assemble_constant_contents (DECL_INITIAL (decl), XSTR (symbol, 0),
7231 DECL_ALIGN (decl));
7232 size = get_constant_size (DECL_INITIAL (decl));
7233 offset += size;
7234 if ((flag_sanitize & SANITIZE_ADDRESS)
7235 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
7236 && asan_protect_global (DECL_INITIAL (decl)))
7238 size = asan_red_zone_size (size);
7239 assemble_zeros (size);
7240 offset += size;
7243 else
7245 HOST_WIDE_INT size;
7246 decl = SYMBOL_REF_DECL (symbol);
7247 assemble_variable_contents (decl, XSTR (symbol, 0), false);
7248 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
7249 offset += size;
7250 if ((flag_sanitize & SANITIZE_ADDRESS)
7251 && asan_protect_global (decl))
7253 size = asan_red_zone_size (size);
7254 assemble_zeros (size);
7255 offset += size;
7261 /* A htab_traverse callback used to call output_object_block for
7262 each member of object_block_htab. */
7264 static int
7265 output_object_block_htab (void **slot, void *data ATTRIBUTE_UNUSED)
7267 output_object_block ((struct object_block *) (*slot));
7268 return 1;
7271 /* Output the definitions of all object_blocks. */
7273 void
7274 output_object_blocks (void)
7276 htab_traverse (object_block_htab, output_object_block_htab, NULL);
7279 /* This function provides a possible implementation of the
7280 TARGET_ASM_RECORD_GCC_SWITCHES target hook for ELF targets. When triggered
7281 by -frecord-gcc-switches it creates a new mergeable, string section in the
7282 assembler output file called TARGET_ASM_RECORD_GCC_SWITCHES_SECTION which
7283 contains the switches in ASCII format.
7285 FIXME: This code does not correctly handle double quote characters
7286 that appear inside strings, (it strips them rather than preserving them).
7287 FIXME: ASM_OUTPUT_ASCII, as defined in config/elfos.h will not emit NUL
7288 characters - instead it treats them as sub-string separators. Since
7289 we want to emit NUL strings terminators into the object file we have to use
7290 ASM_OUTPUT_SKIP. */
7293 elf_record_gcc_switches (print_switch_type type, const char * name)
7295 switch (type)
7297 case SWITCH_TYPE_PASSED:
7298 ASM_OUTPUT_ASCII (asm_out_file, name, strlen (name));
7299 ASM_OUTPUT_SKIP (asm_out_file, (unsigned HOST_WIDE_INT) 1);
7300 break;
7302 case SWITCH_TYPE_DESCRIPTIVE:
7303 if (name == NULL)
7305 /* Distinguish between invocations where name is NULL. */
7306 static bool started = false;
7308 if (!started)
7310 section * sec;
7312 sec = get_section (targetm.asm_out.record_gcc_switches_section,
7313 SECTION_DEBUG
7314 | SECTION_MERGE
7315 | SECTION_STRINGS
7316 | (SECTION_ENTSIZE & 1),
7317 NULL);
7318 switch_to_section (sec);
7319 started = true;
7323 default:
7324 break;
7327 /* The return value is currently ignored by the caller, but must be 0.
7328 For -fverbose-asm the return value would be the number of characters
7329 emitted into the assembler file. */
7330 return 0;
7333 /* Emit text to declare externally defined symbols. It is needed to
7334 properly support non-default visibility. */
7335 void
7336 default_elf_asm_output_external (FILE *file ATTRIBUTE_UNUSED,
7337 tree decl,
7338 const char *name ATTRIBUTE_UNUSED)
7340 /* We output the name if and only if TREE_SYMBOL_REFERENCED is
7341 set in order to avoid putting out names that are never really
7342 used. */
7343 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
7344 && targetm.binds_local_p (decl))
7345 maybe_assemble_visibility (decl);
7348 /* The default hook for TARGET_ASM_OUTPUT_SOURCE_FILENAME. */
7350 void
7351 default_asm_output_source_filename (FILE *file, const char *name)
7353 #ifdef ASM_OUTPUT_SOURCE_FILENAME
7354 ASM_OUTPUT_SOURCE_FILENAME (file, name);
7355 #else
7356 fprintf (file, "\t.file\t");
7357 output_quoted_string (file, name);
7358 putc ('\n', file);
7359 #endif
7362 /* Output a file name in the form wanted by System V. */
7364 void
7365 output_file_directive (FILE *asm_file, const char *input_name)
7367 int len;
7368 const char *na;
7370 if (input_name == NULL)
7371 input_name = "<stdin>";
7372 else
7373 input_name = remap_debug_filename (input_name);
7375 len = strlen (input_name);
7376 na = input_name + len;
7378 /* NA gets INPUT_NAME sans directory names. */
7379 while (na > input_name)
7381 if (IS_DIR_SEPARATOR (na[-1]))
7382 break;
7383 na--;
7386 targetm.asm_out.output_source_filename (asm_file, na);
7389 /* Create a DEBUG_EXPR_DECL / DEBUG_EXPR pair from RTL expression
7390 EXP. */
7392 make_debug_expr_from_rtl (const_rtx exp)
7394 tree ddecl = make_node (DEBUG_EXPR_DECL), type;
7395 enum machine_mode mode = GET_MODE (exp);
7396 rtx dval;
7398 DECL_ARTIFICIAL (ddecl) = 1;
7399 if (REG_P (exp) && REG_EXPR (exp))
7400 type = TREE_TYPE (REG_EXPR (exp));
7401 else if (MEM_P (exp) && MEM_EXPR (exp))
7402 type = TREE_TYPE (MEM_EXPR (exp));
7403 else
7404 type = NULL_TREE;
7405 if (type && TYPE_MODE (type) == mode)
7406 TREE_TYPE (ddecl) = type;
7407 else
7408 TREE_TYPE (ddecl) = lang_hooks.types.type_for_mode (mode, 1);
7409 DECL_MODE (ddecl) = mode;
7410 dval = gen_rtx_DEBUG_EXPR (mode);
7411 DEBUG_EXPR_TREE_DECL (dval) = ddecl;
7412 SET_DECL_RTL (ddecl, dval);
7413 return dval;
7416 #ifdef ELF_ASCII_ESCAPES
7417 /* Default ASM_OUTPUT_LIMITED_STRING for ELF targets. */
7419 void
7420 default_elf_asm_output_limited_string (FILE *f, const char *s)
7422 int escape;
7423 unsigned char c;
7425 fputs (STRING_ASM_OP, f);
7426 putc ('"', f);
7427 while (*s != '\0')
7429 c = *s;
7430 escape = ELF_ASCII_ESCAPES[c];
7431 switch (escape)
7433 case 0:
7434 putc (c, f);
7435 break;
7436 case 1:
7437 /* TODO: Print in hex with fast function, important for -flto. */
7438 fprintf (f, "\\%03o", c);
7439 break;
7440 default:
7441 putc ('\\', f);
7442 putc (escape, f);
7443 break;
7445 s++;
7447 putc ('\"', f);
7448 putc ('\n', f);
7451 /* Default ASM_OUTPUT_ASCII for ELF targets. */
7453 void
7454 default_elf_asm_output_ascii (FILE *f, const char *s, unsigned int len)
7456 const char *limit = s + len;
7457 const char *last_null = NULL;
7458 unsigned bytes_in_chunk = 0;
7459 unsigned char c;
7460 int escape;
7462 for (; s < limit; s++)
7464 const char *p;
7466 if (bytes_in_chunk >= 60)
7468 putc ('\"', f);
7469 putc ('\n', f);
7470 bytes_in_chunk = 0;
7473 if (s > last_null)
7475 for (p = s; p < limit && *p != '\0'; p++)
7476 continue;
7477 last_null = p;
7479 else
7480 p = last_null;
7482 if (p < limit && (p - s) <= (long) ELF_STRING_LIMIT)
7484 if (bytes_in_chunk > 0)
7486 putc ('\"', f);
7487 putc ('\n', f);
7488 bytes_in_chunk = 0;
7491 default_elf_asm_output_limited_string (f, s);
7492 s = p;
7494 else
7496 if (bytes_in_chunk == 0)
7497 fputs (ASCII_DATA_ASM_OP "\"", f);
7499 c = *s;
7500 escape = ELF_ASCII_ESCAPES[c];
7501 switch (escape)
7503 case 0:
7504 putc (c, f);
7505 bytes_in_chunk++;
7506 break;
7507 case 1:
7508 /* TODO: Print in hex with fast function, important for -flto. */
7509 fprintf (f, "\\%03o", c);
7510 bytes_in_chunk += 4;
7511 break;
7512 default:
7513 putc ('\\', f);
7514 putc (escape, f);
7515 bytes_in_chunk += 2;
7516 break;
7522 if (bytes_in_chunk > 0)
7524 putc ('\"', f);
7525 putc ('\n', f);
7528 #endif
7530 static GTY(()) section *elf_init_array_section;
7531 static GTY(()) section *elf_fini_array_section;
7533 static section *
7534 get_elf_initfini_array_priority_section (int priority,
7535 bool constructor_p)
7537 section *sec;
7538 if (priority != DEFAULT_INIT_PRIORITY)
7540 char buf[18];
7541 sprintf (buf, "%s.%.5u",
7542 constructor_p ? ".init_array" : ".fini_array",
7543 priority);
7544 sec = get_section (buf, SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
7546 else
7548 if (constructor_p)
7550 if (elf_init_array_section == NULL)
7551 elf_init_array_section
7552 = get_section (".init_array",
7553 SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
7554 sec = elf_init_array_section;
7556 else
7558 if (elf_fini_array_section == NULL)
7559 elf_fini_array_section
7560 = get_section (".fini_array",
7561 SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
7562 sec = elf_fini_array_section;
7565 return sec;
7568 /* Use .init_array section for constructors. */
7570 void
7571 default_elf_init_array_asm_out_constructor (rtx symbol, int priority)
7573 section *sec = get_elf_initfini_array_priority_section (priority,
7574 true);
7575 assemble_addr_to_section (symbol, sec);
7578 /* Use .fini_array section for destructors. */
7580 void
7581 default_elf_fini_array_asm_out_destructor (rtx symbol, int priority)
7583 section *sec = get_elf_initfini_array_priority_section (priority,
7584 false);
7585 assemble_addr_to_section (symbol, sec);
7588 /* Default TARGET_ASM_OUTPUT_IDENT hook.
7590 This is a bit of a cheat. The real default is a no-op, but this
7591 hook is the default for all targets with a .ident directive. */
7593 void
7594 default_asm_output_ident_directive (const char *ident_str)
7596 const char *ident_asm_op = "\t.ident\t";
7598 /* If we are still in the front end, do not write out the string
7599 to asm_out_file. Instead, add a fake top-level asm statement.
7600 This allows the front ends to use this hook without actually
7601 writing to asm_out_file, to handle #ident or Pragma Ident. */
7602 if (cgraph_state == CGRAPH_STATE_PARSING)
7604 char *buf = ACONCAT ((ident_asm_op, "\"", ident_str, "\"\n", NULL));
7605 add_asm_node (build_string (strlen (buf), buf));
7607 else
7608 fprintf (asm_out_file, "%s\"%s\"\n", ident_asm_op, ident_str);
7611 #include "gt-varasm.h"