sort - Don't live-loop threads
[dragonfly.git] / contrib / gcc-4.7 / gcc / varasm.c
blobbb835bcd4b6046d15fc718998f85fc5a09a0621d
1 /* Output variables, constants and external declarations, for GNU compiler.
2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
4 2010, 2011, 2012 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
23 /* This file handles generation of all the assembler code
24 *except* the instructions of a function.
25 This includes declarations of variables and their initial values.
27 We also output the assembler code for constants stored in memory
28 and are responsible for combining constants with the same value. */
30 #include "config.h"
31 #include "system.h"
32 #include "coretypes.h"
33 #include "pointer-set.h"
34 #include "tm.h"
35 #include "rtl.h"
36 #include "tree.h"
37 #include "flags.h"
38 #include "function.h"
39 #include "expr.h"
40 #include "hard-reg-set.h"
41 #include "regs.h"
42 #include "output.h"
43 #include "diagnostic-core.h"
44 #include "hashtab.h"
45 #include "ggc.h"
46 #include "langhooks.h"
47 #include "tm_p.h"
48 #include "debug.h"
49 #include "target.h"
50 #include "common/common-target.h"
51 #include "targhooks.h"
52 #include "tree-mudflap.h"
53 #include "cgraph.h"
54 #include "cfglayout.h"
55 #include "basic-block.h"
56 #include "tree-iterator.h"
57 #include "pointer-set.h"
59 #ifdef XCOFF_DEBUGGING_INFO
60 #include "xcoffout.h" /* Needed for external data
61 declarations for e.g. AIX 4.x. */
62 #endif
64 /* The (assembler) name of the first globally-visible object output. */
65 extern GTY(()) const char *first_global_object_name;
66 extern GTY(()) const char *weak_global_object_name;
68 const char *first_global_object_name;
69 const char *weak_global_object_name;
71 struct addr_const;
72 struct constant_descriptor_rtx;
73 struct rtx_constant_pool;
75 #define n_deferred_constants (crtl->varasm.deferred_constants)
77 /* Number for making the label on the next
78 constant that is stored in memory. */
80 static GTY(()) int const_labelno;
82 /* Carry information from ASM_DECLARE_OBJECT_NAME
83 to ASM_FINISH_DECLARE_OBJECT. */
85 int size_directive_output;
87 /* The last decl for which assemble_variable was called,
88 if it did ASM_DECLARE_OBJECT_NAME.
89 If the last call to assemble_variable didn't do that,
90 this holds 0. */
92 tree last_assemble_variable_decl;
94 /* The following global variable indicates if the first basic block
95 in a function belongs to the cold partition or not. */
97 bool first_function_block_is_cold;
99 /* We give all constants their own alias set. Perhaps redundant with
100 MEM_READONLY_P, but pre-dates it. */
102 static alias_set_type const_alias_set;
104 /* Whether we saw any functions with no_split_stack. */
106 static bool saw_no_split_stack;
108 static const char *strip_reg_name (const char *);
109 static int contains_pointers_p (tree);
110 #ifdef ASM_OUTPUT_EXTERNAL
111 static bool incorporeal_function_p (tree);
112 #endif
113 static void decode_addr_const (tree, struct addr_const *);
114 static hashval_t const_desc_hash (const void *);
115 static int const_desc_eq (const void *, const void *);
116 static hashval_t const_hash_1 (const tree);
117 static int compare_constant (const tree, const tree);
118 static tree copy_constant (tree);
119 static void output_constant_def_contents (rtx);
120 static void output_addressed_constants (tree);
121 static unsigned HOST_WIDE_INT array_size_for_constructor (tree);
122 static unsigned min_align (unsigned, unsigned);
123 static void globalize_decl (tree);
124 static bool decl_readonly_section_1 (enum section_category);
125 #ifdef BSS_SECTION_ASM_OP
126 #ifdef ASM_OUTPUT_ALIGNED_BSS
127 static void asm_output_aligned_bss (FILE *, tree, const char *,
128 unsigned HOST_WIDE_INT, int)
129 ATTRIBUTE_UNUSED;
130 #endif
131 #endif /* BSS_SECTION_ASM_OP */
132 static void mark_weak (tree);
133 static void output_constant_pool (const char *, tree);
135 /* Well-known sections, each one associated with some sort of *_ASM_OP. */
136 section *text_section;
137 section *data_section;
138 section *readonly_data_section;
139 section *sdata_section;
140 section *ctors_section;
141 section *dtors_section;
142 section *bss_section;
143 section *sbss_section;
145 /* Various forms of common section. All are guaranteed to be nonnull. */
146 section *tls_comm_section;
147 section *comm_section;
148 section *lcomm_section;
150 /* A SECTION_NOSWITCH section used for declaring global BSS variables.
151 May be null. */
152 section *bss_noswitch_section;
154 /* The section that holds the main exception table, when known. The section
155 is set either by the target's init_sections hook or by the first call to
156 switch_to_exception_section. */
157 section *exception_section;
159 /* The section that holds the DWARF2 frame unwind information, when known.
160 The section is set either by the target's init_sections hook or by the
161 first call to switch_to_eh_frame_section. */
162 section *eh_frame_section;
164 /* asm_out_file's current section. This is NULL if no section has yet
165 been selected or if we lose track of what the current section is. */
166 section *in_section;
168 /* True if code for the current function is currently being directed
169 at the cold section. */
170 bool in_cold_section_p;
172 /* A linked list of all the unnamed sections. */
173 static GTY(()) section *unnamed_sections;
175 /* Return a nonzero value if DECL has a section attribute. */
176 #define IN_NAMED_SECTION(DECL) \
177 ((TREE_CODE (DECL) == FUNCTION_DECL || TREE_CODE (DECL) == VAR_DECL) \
178 && DECL_SECTION_NAME (DECL) != NULL_TREE)
180 /* Hash table of named sections. */
181 static GTY((param_is (section))) htab_t section_htab;
183 /* A table of object_blocks, indexed by section. */
184 static GTY((param_is (struct object_block))) htab_t object_block_htab;
186 /* The next number to use for internal anchor labels. */
187 static GTY(()) int anchor_labelno;
189 /* A pool of constants that can be shared between functions. */
190 static GTY(()) struct rtx_constant_pool *shared_constant_pool;
192 /* Helper routines for maintaining section_htab. */
194 static int
195 section_entry_eq (const void *p1, const void *p2)
197 const section *old = (const section *) p1;
198 const char *new_name = (const char *) p2;
200 return strcmp (old->named.name, new_name) == 0;
203 static hashval_t
204 section_entry_hash (const void *p)
206 const section *old = (const section *) p;
207 return htab_hash_string (old->named.name);
210 /* Return a hash value for section SECT. */
212 static hashval_t
213 hash_section (section *sect)
215 if (sect->common.flags & SECTION_NAMED)
216 return htab_hash_string (sect->named.name);
217 return sect->common.flags;
220 /* Helper routines for maintaining object_block_htab. */
222 static int
223 object_block_entry_eq (const void *p1, const void *p2)
225 const struct object_block *old = (const struct object_block *) p1;
226 const section *new_section = (const section *) p2;
228 return old->sect == new_section;
231 static hashval_t
232 object_block_entry_hash (const void *p)
234 const struct object_block *old = (const struct object_block *) p;
235 return hash_section (old->sect);
238 /* Return a new unnamed section with the given fields. */
240 section *
241 get_unnamed_section (unsigned int flags, void (*callback) (const void *),
242 const void *data)
244 section *sect;
246 sect = ggc_alloc_section ();
247 sect->unnamed.common.flags = flags | SECTION_UNNAMED;
248 sect->unnamed.callback = callback;
249 sect->unnamed.data = data;
250 sect->unnamed.next = unnamed_sections;
252 unnamed_sections = sect;
253 return sect;
256 /* Return a SECTION_NOSWITCH section with the given fields. */
258 static section *
259 get_noswitch_section (unsigned int flags, noswitch_section_callback callback)
261 section *sect;
263 sect = ggc_alloc_section ();
264 sect->noswitch.common.flags = flags | SECTION_NOSWITCH;
265 sect->noswitch.callback = callback;
267 return sect;
270 /* Return the named section structure associated with NAME. Create
271 a new section with the given fields if no such structure exists. */
273 section *
274 get_section (const char *name, unsigned int flags, tree decl)
276 section *sect, **slot;
278 slot = (section **)
279 htab_find_slot_with_hash (section_htab, name,
280 htab_hash_string (name), INSERT);
281 flags |= SECTION_NAMED;
282 if (*slot == NULL)
284 sect = ggc_alloc_section ();
285 sect->named.common.flags = flags;
286 sect->named.name = ggc_strdup (name);
287 sect->named.decl = decl;
288 *slot = sect;
290 else
292 sect = *slot;
293 if ((sect->common.flags & ~SECTION_DECLARED) != flags
294 && ((sect->common.flags | flags) & SECTION_OVERRIDE) == 0)
296 /* It is fine if one of the section flags is
297 SECTION_WRITE | SECTION_RELRO and the other has none of these
298 flags (i.e. read-only) in named sections and either the
299 section hasn't been declared yet or has been declared as writable.
300 In that case just make sure the resulting flags are
301 SECTION_WRITE | SECTION_RELRO, ie. writable only because of
302 relocations. */
303 if (((sect->common.flags ^ flags) & (SECTION_WRITE | SECTION_RELRO))
304 == (SECTION_WRITE | SECTION_RELRO)
305 && (sect->common.flags
306 & ~(SECTION_DECLARED | SECTION_WRITE | SECTION_RELRO))
307 == (flags & ~(SECTION_WRITE | SECTION_RELRO))
308 && ((sect->common.flags & SECTION_DECLARED) == 0
309 || (sect->common.flags & SECTION_WRITE)))
311 sect->common.flags |= (SECTION_WRITE | SECTION_RELRO);
312 return sect;
314 /* Sanity check user variables for flag changes. */
315 if (decl == 0)
316 decl = sect->named.decl;
317 gcc_assert (decl);
318 if (sect->named.decl == NULL)
319 error ("%+D causes a section type conflict", decl);
320 else
322 error ("%+D causes a section type conflict with %D",
323 decl, sect->named.decl);
324 if (decl != sect->named.decl)
325 inform (DECL_SOURCE_LOCATION (sect->named.decl),
326 "%qD was declared here", sect->named.decl);
328 /* Make sure we don't error about one section multiple times. */
329 sect->common.flags |= SECTION_OVERRIDE;
332 return sect;
335 /* Return true if the current compilation mode benefits from having
336 objects grouped into blocks. */
338 static bool
339 use_object_blocks_p (void)
341 return flag_section_anchors;
344 /* Return the object_block structure for section SECT. Create a new
345 structure if we haven't created one already. Return null if SECT
346 itself is null. */
348 static struct object_block *
349 get_block_for_section (section *sect)
351 struct object_block *block;
352 void **slot;
354 if (sect == NULL)
355 return NULL;
357 slot = htab_find_slot_with_hash (object_block_htab, sect,
358 hash_section (sect), INSERT);
359 block = (struct object_block *) *slot;
360 if (block == NULL)
362 block = ggc_alloc_cleared_object_block ();
363 block->sect = sect;
364 *slot = block;
366 return block;
369 /* Create a symbol with label LABEL and place it at byte offset
370 OFFSET in BLOCK. OFFSET can be negative if the symbol's offset
371 is not yet known. LABEL must be a garbage-collected string. */
373 static rtx
374 create_block_symbol (const char *label, struct object_block *block,
375 HOST_WIDE_INT offset)
377 rtx symbol;
378 unsigned int size;
380 /* Create the extended SYMBOL_REF. */
381 size = RTX_HDR_SIZE + sizeof (struct block_symbol);
382 symbol = ggc_alloc_zone_rtx_def (size, &rtl_zone);
384 /* Initialize the normal SYMBOL_REF fields. */
385 memset (symbol, 0, size);
386 PUT_CODE (symbol, SYMBOL_REF);
387 PUT_MODE (symbol, Pmode);
388 XSTR (symbol, 0) = label;
389 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_HAS_BLOCK_INFO;
391 /* Initialize the block_symbol stuff. */
392 SYMBOL_REF_BLOCK (symbol) = block;
393 SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
395 return symbol;
398 /* Return a section with a particular name and with whatever SECTION_*
399 flags section_type_flags deems appropriate. The name of the section
400 is taken from NAME if nonnull, otherwise it is taken from DECL's
401 DECL_SECTION_NAME. DECL is the decl associated with the section
402 (see the section comment for details) and RELOC is as for
403 section_type_flags. */
405 section *
406 get_named_section (tree decl, const char *name, int reloc)
408 unsigned int flags;
410 gcc_assert (!decl || DECL_P (decl));
411 if (name == NULL)
412 name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
414 flags = targetm.section_type_flags (decl, name, reloc);
416 return get_section (name, flags, decl);
419 /* If required, set DECL_SECTION_NAME to a unique name. */
421 void
422 resolve_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED,
423 int flag_function_or_data_sections)
425 if (DECL_SECTION_NAME (decl) == NULL_TREE
426 && targetm_common.have_named_sections
427 && (flag_function_or_data_sections
428 || DECL_ONE_ONLY (decl)))
430 targetm.asm_out.unique_section (decl, reloc);
431 DECL_HAS_IMPLICIT_SECTION_NAME_P (decl) = true;
435 #ifdef BSS_SECTION_ASM_OP
437 #ifdef ASM_OUTPUT_ALIGNED_BSS
439 /* Utility function for targets to use in implementing
440 ASM_OUTPUT_ALIGNED_BSS.
441 ??? It is believed that this function will work in most cases so such
442 support is localized here. */
444 static void
445 asm_output_aligned_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
446 const char *name, unsigned HOST_WIDE_INT size,
447 int align)
449 switch_to_section (bss_section);
450 ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
451 #ifdef ASM_DECLARE_OBJECT_NAME
452 last_assemble_variable_decl = decl;
453 ASM_DECLARE_OBJECT_NAME (file, name, decl);
454 #else
455 /* Standard thing is just output label for the object. */
456 ASM_OUTPUT_LABEL (file, name);
457 #endif /* ASM_DECLARE_OBJECT_NAME */
458 ASM_OUTPUT_SKIP (file, size ? size : 1);
461 #endif
463 #endif /* BSS_SECTION_ASM_OP */
465 #ifndef USE_SELECT_SECTION_FOR_FUNCTIONS
466 /* Return the hot section for function DECL. Return text_section for
467 null DECLs. */
469 static section *
470 hot_function_section (tree decl)
472 if (decl != NULL_TREE
473 && DECL_SECTION_NAME (decl) != NULL_TREE
474 && targetm_common.have_named_sections)
475 return get_named_section (decl, NULL, 0);
476 else
477 return text_section;
479 #endif
481 /* Return section for TEXT_SECTION_NAME if DECL or DECL_SECTION_NAME (DECL)
482 is NULL.
484 When DECL_SECTION_NAME is non-NULL and it is implicit section and
485 NAMED_SECTION_SUFFIX is non-NULL, then produce section called
486 concatenate the name with NAMED_SECTION_SUFFIX.
487 Otherwise produce "TEXT_SECTION_NAME.IMPLICIT_NAME". */
489 section *
490 get_named_text_section (tree decl,
491 const char *text_section_name,
492 const char *named_section_suffix)
494 if (decl && DECL_SECTION_NAME (decl))
496 if (named_section_suffix)
498 tree dsn = DECL_SECTION_NAME (decl);
499 const char *stripped_name;
500 char *name, *buffer;
502 name = (char *) alloca (TREE_STRING_LENGTH (dsn) + 1);
503 memcpy (name, TREE_STRING_POINTER (dsn),
504 TREE_STRING_LENGTH (dsn) + 1);
506 stripped_name = targetm.strip_name_encoding (name);
508 buffer = ACONCAT ((stripped_name, named_section_suffix, NULL));
509 return get_named_section (decl, buffer, 0);
511 else if (DECL_HAS_IMPLICIT_SECTION_NAME_P (decl))
513 const char *name;
515 /* Do not try to split gnu_linkonce functions. This gets somewhat
516 slipperly. */
517 if (DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP)
518 return NULL;
519 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
520 name = targetm.strip_name_encoding (name);
521 return get_named_section (decl, ACONCAT ((text_section_name, ".",
522 name, NULL)), 0);
524 else
525 return NULL;
527 return get_named_section (decl, text_section_name, 0);
530 /* Choose named function section based on its frequency. */
532 section *
533 default_function_section (tree decl, enum node_frequency freq,
534 bool startup, bool exit)
536 #if defined HAVE_LD_EH_GC_SECTIONS && defined HAVE_LD_EH_GC_SECTIONS_BUG
537 /* Old GNU linkers have buggy --gc-section support, which sometimes
538 results in .gcc_except_table* sections being garbage collected. */
539 if (decl
540 && DECL_SECTION_NAME (decl)
541 && DECL_HAS_IMPLICIT_SECTION_NAME_P (decl))
542 return NULL;
543 #endif
545 if (!flag_reorder_functions
546 || !targetm_common.have_named_sections)
547 return NULL;
548 /* Startup code should go to startup subsection unless it is
549 unlikely executed (this happens especially with function splitting
550 where we can split away unnecesary parts of static constructors. */
551 if (startup && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
552 return get_named_text_section (decl, ".text.startup", NULL);
554 /* Similarly for exit. */
555 if (exit && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
556 return get_named_text_section (decl, ".text.exit", NULL);
558 /* Group cold functions together, similarly for hot code. */
559 switch (freq)
561 case NODE_FREQUENCY_UNLIKELY_EXECUTED:
562 return get_named_text_section (decl, ".text.unlikely", NULL);
563 case NODE_FREQUENCY_HOT:
564 return get_named_text_section (decl, ".text.hot", NULL);
565 default:
566 return NULL;
570 /* Return the section for function DECL.
572 If DECL is NULL_TREE, return the text section. We can be passed
573 NULL_TREE under some circumstances by dbxout.c at least.
575 If FORCE_COLD is true, return cold function section ignoring
576 the frequency info of cgraph_node. */
578 static section *
579 function_section_1 (tree decl, bool force_cold)
581 section *section = NULL;
582 enum node_frequency freq = NODE_FREQUENCY_NORMAL;
583 bool startup = false, exit = false;
585 if (decl)
587 struct cgraph_node *node = cgraph_get_node (decl);
589 if (node)
591 freq = node->frequency;
592 startup = node->only_called_at_startup;
593 exit = node->only_called_at_exit;
596 if (force_cold)
597 freq = NODE_FREQUENCY_UNLIKELY_EXECUTED;
599 #ifdef USE_SELECT_SECTION_FOR_FUNCTIONS
600 if (decl != NULL_TREE
601 && DECL_SECTION_NAME (decl) != NULL_TREE)
603 if (targetm.asm_out.function_section)
604 section = targetm.asm_out.function_section (decl, freq,
605 startup, exit);
606 if (section)
607 return section;
608 return get_named_section (decl, NULL, 0);
610 else
611 return targetm.asm_out.select_section
612 (decl, freq == NODE_FREQUENCY_UNLIKELY_EXECUTED,
613 DECL_ALIGN (decl));
614 #else
615 if (targetm.asm_out.function_section)
616 section = targetm.asm_out.function_section (decl, freq, startup, exit);
617 if (section)
618 return section;
619 return hot_function_section (decl);
620 #endif
623 /* Return the section for function DECL.
625 If DECL is NULL_TREE, return the text section. We can be passed
626 NULL_TREE under some circumstances by dbxout.c at least. */
628 section *
629 function_section (tree decl)
631 /* Handle cases where function splitting code decides
632 to put function entry point into unlikely executed section
633 despite the fact that the function itself is not cold
634 (i.e. it is called rarely but contains a hot loop that is
635 better to live in hot subsection for the code locality). */
636 return function_section_1 (decl,
637 first_function_block_is_cold);
640 /* Return the section for the current function, take IN_COLD_SECTION_P
641 into account. */
643 section *
644 current_function_section (void)
646 return function_section_1 (current_function_decl, in_cold_section_p);
649 /* Tell assembler to switch to unlikely-to-be-executed text section. */
651 section *
652 unlikely_text_section (void)
654 return function_section_1 (current_function_decl, true);
657 /* When called within a function context, return true if the function
658 has been assigned a cold text section and if SECT is that section.
659 When called outside a function context, return true if SECT is the
660 default cold section. */
662 bool
663 unlikely_text_section_p (section *sect)
665 return sect == function_section_1 (current_function_decl, true);
668 /* Return the read-only data section associated with function DECL. */
670 section *
671 default_function_rodata_section (tree decl)
673 if (decl != NULL_TREE && DECL_SECTION_NAME (decl))
675 const char *name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
677 if (DECL_ONE_ONLY (decl) && HAVE_COMDAT_GROUP)
679 const char *dot;
680 size_t len;
681 char* rname;
683 dot = strchr (name + 1, '.');
684 if (!dot)
685 dot = name;
686 len = strlen (dot) + 8;
687 rname = (char *) alloca (len);
689 strcpy (rname, ".rodata");
690 strcat (rname, dot);
691 return get_section (rname, SECTION_LINKONCE, decl);
693 /* For .gnu.linkonce.t.foo we want to use .gnu.linkonce.r.foo. */
694 else if (DECL_ONE_ONLY (decl)
695 && strncmp (name, ".gnu.linkonce.t.", 16) == 0)
697 size_t len = strlen (name) + 1;
698 char *rname = (char *) alloca (len);
700 memcpy (rname, name, len);
701 rname[14] = 'r';
702 return get_section (rname, SECTION_LINKONCE, decl);
704 /* For .text.foo we want to use .rodata.foo. */
705 else if (flag_function_sections && flag_data_sections
706 && strncmp (name, ".text.", 6) == 0)
708 size_t len = strlen (name) + 1;
709 char *rname = (char *) alloca (len + 2);
711 memcpy (rname, ".rodata", 7);
712 memcpy (rname + 7, name + 5, len - 5);
713 return get_section (rname, 0, decl);
717 return readonly_data_section;
720 /* Return the read-only data section associated with function DECL
721 for targets where that section should be always the single
722 readonly data section. */
724 section *
725 default_no_function_rodata_section (tree decl ATTRIBUTE_UNUSED)
727 return readonly_data_section;
730 /* Return the section to use for string merging. */
732 static section *
733 mergeable_string_section (tree decl ATTRIBUTE_UNUSED,
734 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
735 unsigned int flags ATTRIBUTE_UNUSED)
737 HOST_WIDE_INT len;
739 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
740 && TREE_CODE (decl) == STRING_CST
741 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
742 && align <= 256
743 && (len = int_size_in_bytes (TREE_TYPE (decl))) > 0
744 && TREE_STRING_LENGTH (decl) >= len)
746 enum machine_mode mode;
747 unsigned int modesize;
748 const char *str;
749 HOST_WIDE_INT i;
750 int j, unit;
751 const char *prefix = targetm.asm_out.mergeable_rodata_prefix;
752 char *name = (char *) alloca (strlen (prefix) + 30);
754 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (decl)));
755 modesize = GET_MODE_BITSIZE (mode);
756 if (modesize >= 8 && modesize <= 256
757 && (modesize & (modesize - 1)) == 0)
759 if (align < modesize)
760 align = modesize;
762 str = TREE_STRING_POINTER (decl);
763 unit = GET_MODE_SIZE (mode);
765 /* Check for embedded NUL characters. */
766 for (i = 0; i < len; i += unit)
768 for (j = 0; j < unit; j++)
769 if (str[i + j] != '\0')
770 break;
771 if (j == unit)
772 break;
774 if (i == len - unit)
776 sprintf (name, "%s.str%d.%d", prefix,
777 modesize / 8, (int) (align / 8));
778 flags |= (modesize / 8) | SECTION_MERGE | SECTION_STRINGS;
779 return get_section (name, flags, NULL);
784 return readonly_data_section;
787 /* Return the section to use for constant merging. */
789 section *
790 mergeable_constant_section (enum machine_mode mode ATTRIBUTE_UNUSED,
791 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
792 unsigned int flags ATTRIBUTE_UNUSED)
794 unsigned int modesize = GET_MODE_BITSIZE (mode);
796 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
797 && mode != VOIDmode
798 && mode != BLKmode
799 && modesize <= align
800 && align >= 8
801 && align <= 256
802 && (align & (align - 1)) == 0)
804 const char *prefix = targetm.asm_out.mergeable_rodata_prefix;
805 char *name = (char *) alloca (strlen (prefix) + 30);
807 sprintf (name, "%s.cst%d", prefix, (int) (align / 8));
808 flags |= (align / 8) | SECTION_MERGE;
809 return get_section (name, flags, NULL);
811 return readonly_data_section;
814 /* Given NAME, a putative register name, discard any customary prefixes. */
816 static const char *
817 strip_reg_name (const char *name)
819 #ifdef REGISTER_PREFIX
820 if (!strncmp (name, REGISTER_PREFIX, strlen (REGISTER_PREFIX)))
821 name += strlen (REGISTER_PREFIX);
822 #endif
823 if (name[0] == '%' || name[0] == '#')
824 name++;
825 return name;
828 /* The user has asked for a DECL to have a particular name. Set (or
829 change) it in such a way that we don't prefix an underscore to
830 it. */
831 void
832 set_user_assembler_name (tree decl, const char *name)
834 char *starred = (char *) alloca (strlen (name) + 2);
835 starred[0] = '*';
836 strcpy (starred + 1, name);
837 change_decl_assembler_name (decl, get_identifier (starred));
838 SET_DECL_RTL (decl, NULL_RTX);
841 /* Decode an `asm' spec for a declaration as a register name.
842 Return the register number, or -1 if nothing specified,
843 or -2 if the ASMSPEC is not `cc' or `memory' and is not recognized,
844 or -3 if ASMSPEC is `cc' and is not recognized,
845 or -4 if ASMSPEC is `memory' and is not recognized.
846 Accept an exact spelling or a decimal number.
847 Prefixes such as % are optional. */
850 decode_reg_name_and_count (const char *asmspec, int *pnregs)
852 /* Presume just one register is clobbered. */
853 *pnregs = 1;
855 if (asmspec != 0)
857 int i;
859 /* Get rid of confusing prefixes. */
860 asmspec = strip_reg_name (asmspec);
862 /* Allow a decimal number as a "register name". */
863 for (i = strlen (asmspec) - 1; i >= 0; i--)
864 if (! ISDIGIT (asmspec[i]))
865 break;
866 if (asmspec[0] != 0 && i < 0)
868 i = atoi (asmspec);
869 if (i < FIRST_PSEUDO_REGISTER && i >= 0)
870 return i;
871 else
872 return -2;
875 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
876 if (reg_names[i][0]
877 && ! strcmp (asmspec, strip_reg_name (reg_names[i])))
878 return i;
880 #ifdef OVERLAPPING_REGISTER_NAMES
882 static const struct
884 const char *const name;
885 const int number;
886 const int nregs;
887 } table[] = OVERLAPPING_REGISTER_NAMES;
889 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
890 if (table[i].name[0]
891 && ! strcmp (asmspec, table[i].name))
893 *pnregs = table[i].nregs;
894 return table[i].number;
897 #endif /* OVERLAPPING_REGISTER_NAMES */
899 #ifdef ADDITIONAL_REGISTER_NAMES
901 static const struct { const char *const name; const int number; } table[]
902 = ADDITIONAL_REGISTER_NAMES;
904 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
905 if (table[i].name[0]
906 && ! strcmp (asmspec, table[i].name))
907 return table[i].number;
909 #endif /* ADDITIONAL_REGISTER_NAMES */
911 if (!strcmp (asmspec, "memory"))
912 return -4;
914 if (!strcmp (asmspec, "cc"))
915 return -3;
917 return -2;
920 return -1;
924 decode_reg_name (const char *name)
926 int count;
927 return decode_reg_name_and_count (name, &count);
931 /* Return true if DECL's initializer is suitable for a BSS section. */
933 static bool
934 bss_initializer_p (const_tree decl)
936 return (DECL_INITIAL (decl) == NULL
937 || DECL_INITIAL (decl) == error_mark_node
938 || (flag_zero_initialized_in_bss
939 /* Leave constant zeroes in .rodata so they
940 can be shared. */
941 && !TREE_READONLY (decl)
942 && initializer_zerop (DECL_INITIAL (decl))));
945 /* Compute the alignment of variable specified by DECL.
946 DONT_OUTPUT_DATA is from assemble_variable. */
948 void
949 align_variable (tree decl, bool dont_output_data)
951 unsigned int align = DECL_ALIGN (decl);
953 /* In the case for initialing an array whose length isn't specified,
954 where we have not yet been able to do the layout,
955 figure out the proper alignment now. */
956 if (dont_output_data && DECL_SIZE (decl) == 0
957 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
958 align = MAX (align, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (decl))));
960 /* Some object file formats have a maximum alignment which they support.
961 In particular, a.out format supports a maximum alignment of 4. */
962 if (align > MAX_OFILE_ALIGNMENT)
964 warning (0, "alignment of %q+D is greater than maximum object "
965 "file alignment. Using %d", decl,
966 MAX_OFILE_ALIGNMENT/BITS_PER_UNIT);
967 align = MAX_OFILE_ALIGNMENT;
970 /* On some machines, it is good to increase alignment sometimes. */
971 if (! DECL_USER_ALIGN (decl))
973 #ifdef DATA_ALIGNMENT
974 unsigned int data_align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
975 /* Don't increase alignment too much for TLS variables - TLS space
976 is too precious. */
977 if (! DECL_THREAD_LOCAL_P (decl) || data_align <= BITS_PER_WORD)
978 align = data_align;
979 #endif
980 #ifdef CONSTANT_ALIGNMENT
981 if (DECL_INITIAL (decl) != 0 && DECL_INITIAL (decl) != error_mark_node)
983 unsigned int const_align = CONSTANT_ALIGNMENT (DECL_INITIAL (decl),
984 align);
985 /* Don't increase alignment too much for TLS variables - TLS space
986 is too precious. */
987 if (! DECL_THREAD_LOCAL_P (decl) || const_align <= BITS_PER_WORD)
988 align = const_align;
990 #endif
993 /* Reset the alignment in case we have made it tighter, so we can benefit
994 from it in get_pointer_alignment. */
995 DECL_ALIGN (decl) = align;
998 /* Return the section into which the given VAR_DECL or CONST_DECL
999 should be placed. PREFER_NOSWITCH_P is true if a noswitch
1000 section should be used wherever possible. */
1002 section *
1003 get_variable_section (tree decl, bool prefer_noswitch_p)
1005 addr_space_t as = ADDR_SPACE_GENERIC;
1006 int reloc;
1008 if (TREE_TYPE (decl) != error_mark_node)
1009 as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
1011 if (DECL_COMMON (decl))
1013 /* If the decl has been given an explicit section name, or it resides
1014 in a non-generic address space, then it isn't common, and shouldn't
1015 be handled as such. */
1016 gcc_assert (DECL_SECTION_NAME (decl) == NULL
1017 && ADDR_SPACE_GENERIC_P (as));
1018 if (DECL_THREAD_LOCAL_P (decl))
1019 return tls_comm_section;
1020 else if (TREE_PUBLIC (decl) && bss_initializer_p (decl))
1021 return comm_section;
1024 if (DECL_INITIAL (decl) == error_mark_node)
1025 reloc = contains_pointers_p (TREE_TYPE (decl)) ? 3 : 0;
1026 else if (DECL_INITIAL (decl))
1027 reloc = compute_reloc_for_constant (DECL_INITIAL (decl));
1028 else
1029 reloc = 0;
1031 resolve_unique_section (decl, reloc, flag_data_sections);
1032 if (IN_NAMED_SECTION (decl))
1033 return get_named_section (decl, NULL, reloc);
1035 if (ADDR_SPACE_GENERIC_P (as)
1036 && !DECL_THREAD_LOCAL_P (decl)
1037 && !(prefer_noswitch_p && targetm.have_switchable_bss_sections)
1038 && bss_initializer_p (decl))
1040 if (!TREE_PUBLIC (decl))
1041 return lcomm_section;
1042 if (bss_noswitch_section)
1043 return bss_noswitch_section;
1046 return targetm.asm_out.select_section (decl, reloc, DECL_ALIGN (decl));
1049 /* Return the block into which object_block DECL should be placed. */
1051 static struct object_block *
1052 get_block_for_decl (tree decl)
1054 section *sect;
1056 if (TREE_CODE (decl) == VAR_DECL)
1058 /* The object must be defined in this translation unit. */
1059 if (DECL_EXTERNAL (decl))
1060 return NULL;
1062 /* There's no point using object blocks for something that is
1063 isolated by definition. */
1064 if (DECL_ONE_ONLY (decl))
1065 return NULL;
1068 /* We can only calculate block offsets if the decl has a known
1069 constant size. */
1070 if (DECL_SIZE_UNIT (decl) == NULL)
1071 return NULL;
1072 if (!host_integerp (DECL_SIZE_UNIT (decl), 1))
1073 return NULL;
1075 /* Find out which section should contain DECL. We cannot put it into
1076 an object block if it requires a standalone definition. */
1077 if (TREE_CODE (decl) == VAR_DECL)
1078 align_variable (decl, 0);
1079 sect = get_variable_section (decl, true);
1080 if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
1081 return NULL;
1083 return get_block_for_section (sect);
1086 /* Make sure block symbol SYMBOL is in block BLOCK. */
1088 static void
1089 change_symbol_block (rtx symbol, struct object_block *block)
1091 if (block != SYMBOL_REF_BLOCK (symbol))
1093 gcc_assert (SYMBOL_REF_BLOCK_OFFSET (symbol) < 0);
1094 SYMBOL_REF_BLOCK (symbol) = block;
1098 /* Return true if it is possible to put DECL in an object_block. */
1100 static bool
1101 use_blocks_for_decl_p (tree decl)
1103 /* Only data DECLs can be placed into object blocks. */
1104 if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != CONST_DECL)
1105 return false;
1107 /* Detect decls created by dw2_force_const_mem. Such decls are
1108 special because DECL_INITIAL doesn't specify the decl's true value.
1109 dw2_output_indirect_constants will instead call assemble_variable
1110 with dont_output_data set to 1 and then print the contents itself. */
1111 if (DECL_INITIAL (decl) == decl)
1112 return false;
1114 /* If this decl is an alias, then we don't want to emit a
1115 definition. */
1116 if (lookup_attribute ("alias", DECL_ATTRIBUTES (decl)))
1117 return false;
1119 return true;
1122 /* Create the DECL_RTL for a VAR_DECL or FUNCTION_DECL. DECL should
1123 have static storage duration. In other words, it should not be an
1124 automatic variable, including PARM_DECLs.
1126 There is, however, one exception: this function handles variables
1127 explicitly placed in a particular register by the user.
1129 This is never called for PARM_DECL nodes. */
1131 void
1132 make_decl_rtl (tree decl)
1134 const char *name = 0;
1135 int reg_number;
1136 rtx x;
1138 /* Check that we are not being given an automatic variable. */
1139 gcc_assert (TREE_CODE (decl) != PARM_DECL
1140 && TREE_CODE (decl) != RESULT_DECL);
1142 /* A weak alias has TREE_PUBLIC set but not the other bits. */
1143 gcc_assert (TREE_CODE (decl) != VAR_DECL
1144 || TREE_STATIC (decl)
1145 || TREE_PUBLIC (decl)
1146 || DECL_EXTERNAL (decl)
1147 || DECL_REGISTER (decl));
1149 /* And that we were not given a type or a label. */
1150 gcc_assert (TREE_CODE (decl) != TYPE_DECL
1151 && TREE_CODE (decl) != LABEL_DECL);
1153 /* For a duplicate declaration, we can be called twice on the
1154 same DECL node. Don't discard the RTL already made. */
1155 if (DECL_RTL_SET_P (decl))
1157 /* If the old RTL had the wrong mode, fix the mode. */
1158 x = DECL_RTL (decl);
1159 if (GET_MODE (x) != DECL_MODE (decl))
1160 SET_DECL_RTL (decl, adjust_address_nv (x, DECL_MODE (decl), 0));
1162 if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1163 return;
1165 /* ??? Another way to do this would be to maintain a hashed
1166 table of such critters. Instead of adding stuff to a DECL
1167 to give certain attributes to it, we could use an external
1168 hash map from DECL to set of attributes. */
1170 /* Let the target reassign the RTL if it wants.
1171 This is necessary, for example, when one machine specific
1172 decl attribute overrides another. */
1173 targetm.encode_section_info (decl, DECL_RTL (decl), false);
1175 /* If the symbol has a SYMBOL_REF_BLOCK field, update it based
1176 on the new decl information. */
1177 if (MEM_P (x)
1178 && GET_CODE (XEXP (x, 0)) == SYMBOL_REF
1179 && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (x, 0)))
1180 change_symbol_block (XEXP (x, 0), get_block_for_decl (decl));
1182 /* Make this function static known to the mudflap runtime. */
1183 if (flag_mudflap && TREE_CODE (decl) == VAR_DECL)
1184 mudflap_enqueue_decl (decl);
1186 return;
1189 /* If this variable belongs to the global constant pool, retrieve the
1190 pre-computed RTL or recompute it in LTO mode. */
1191 if (TREE_CODE (decl) == VAR_DECL && DECL_IN_CONSTANT_POOL (decl))
1193 SET_DECL_RTL (decl, output_constant_def (DECL_INITIAL (decl), 1));
1194 return;
1197 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
1199 if (name[0] != '*' && TREE_CODE (decl) != FUNCTION_DECL
1200 && DECL_REGISTER (decl))
1202 error ("register name not specified for %q+D", decl);
1204 else if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1206 const char *asmspec = name+1;
1207 enum machine_mode mode = DECL_MODE (decl);
1208 reg_number = decode_reg_name (asmspec);
1209 /* First detect errors in declaring global registers. */
1210 if (reg_number == -1)
1211 error ("register name not specified for %q+D", decl);
1212 else if (reg_number < 0)
1213 error ("invalid register name for %q+D", decl);
1214 else if (mode == BLKmode)
1215 error ("data type of %q+D isn%'t suitable for a register",
1216 decl);
1217 else if (!in_hard_reg_set_p (accessible_reg_set, mode, reg_number))
1218 error ("the register specified for %q+D cannot be accessed"
1219 " by the current target", decl);
1220 else if (!in_hard_reg_set_p (operand_reg_set, mode, reg_number))
1221 error ("the register specified for %q+D is not general enough"
1222 " to be used as a register variable", decl);
1223 else if (!HARD_REGNO_MODE_OK (reg_number, mode))
1224 error ("register specified for %q+D isn%'t suitable for data type",
1225 decl);
1226 /* Now handle properly declared static register variables. */
1227 else
1229 int nregs;
1231 if (DECL_INITIAL (decl) != 0 && TREE_STATIC (decl))
1233 DECL_INITIAL (decl) = 0;
1234 error ("global register variable has initial value");
1236 if (TREE_THIS_VOLATILE (decl))
1237 warning (OPT_Wvolatile_register_var,
1238 "optimization may eliminate reads and/or "
1239 "writes to register variables");
1241 /* If the user specified one of the eliminables registers here,
1242 e.g., FRAME_POINTER_REGNUM, we don't want to get this variable
1243 confused with that register and be eliminated. This usage is
1244 somewhat suspect... */
1246 SET_DECL_RTL (decl, gen_rtx_raw_REG (mode, reg_number));
1247 ORIGINAL_REGNO (DECL_RTL (decl)) = reg_number;
1248 REG_USERVAR_P (DECL_RTL (decl)) = 1;
1250 if (TREE_STATIC (decl))
1252 /* Make this register global, so not usable for anything
1253 else. */
1254 #ifdef ASM_DECLARE_REGISTER_GLOBAL
1255 name = IDENTIFIER_POINTER (DECL_NAME (decl));
1256 ASM_DECLARE_REGISTER_GLOBAL (asm_out_file, decl, reg_number, name);
1257 #endif
1258 nregs = hard_regno_nregs[reg_number][mode];
1259 while (nregs > 0)
1260 globalize_reg (decl, reg_number + --nregs);
1263 /* As a register variable, it has no section. */
1264 return;
1267 /* Now handle ordinary static variables and functions (in memory).
1268 Also handle vars declared register invalidly. */
1269 else if (name[0] == '*')
1271 #ifdef REGISTER_PREFIX
1272 if (strlen (REGISTER_PREFIX) != 0)
1274 reg_number = decode_reg_name (name);
1275 if (reg_number >= 0 || reg_number == -3)
1276 error ("register name given for non-register variable %q+D", decl);
1278 #endif
1281 /* Specifying a section attribute on a variable forces it into a
1282 non-.bss section, and thus it cannot be common. */
1283 /* FIXME: In general this code should not be necessary because
1284 visibility pass is doing the same work. But notice_global_symbol
1285 is called early and it needs to make DECL_RTL to get the name.
1286 we take care of recomputing the DECL_RTL after visibility is changed. */
1287 if (TREE_CODE (decl) == VAR_DECL
1288 && DECL_SECTION_NAME (decl) != NULL_TREE
1289 && DECL_INITIAL (decl) == NULL_TREE
1290 && DECL_COMMON (decl))
1291 DECL_COMMON (decl) = 0;
1293 /* Variables can't be both common and weak. */
1294 if (TREE_CODE (decl) == VAR_DECL && DECL_WEAK (decl))
1295 DECL_COMMON (decl) = 0;
1297 if (use_object_blocks_p () && use_blocks_for_decl_p (decl))
1298 x = create_block_symbol (name, get_block_for_decl (decl), -1);
1299 else
1301 enum machine_mode address_mode = Pmode;
1302 if (TREE_TYPE (decl) != error_mark_node)
1304 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
1305 address_mode = targetm.addr_space.address_mode (as);
1307 x = gen_rtx_SYMBOL_REF (address_mode, name);
1309 SYMBOL_REF_WEAK (x) = DECL_WEAK (decl);
1310 SET_SYMBOL_REF_DECL (x, decl);
1312 x = gen_rtx_MEM (DECL_MODE (decl), x);
1313 if (TREE_CODE (decl) != FUNCTION_DECL)
1314 set_mem_attributes (x, decl, 1);
1315 SET_DECL_RTL (decl, x);
1317 /* Optionally set flags or add text to the name to record information
1318 such as that it is a function name.
1319 If the name is changed, the macro ASM_OUTPUT_LABELREF
1320 will have to know how to strip this information. */
1321 targetm.encode_section_info (decl, DECL_RTL (decl), true);
1323 /* Make this function static known to the mudflap runtime. */
1324 if (flag_mudflap && TREE_CODE (decl) == VAR_DECL)
1325 mudflap_enqueue_decl (decl);
1328 /* Like make_decl_rtl, but inhibit creation of new alias sets when
1329 calling make_decl_rtl. Also, reset DECL_RTL before returning the
1330 rtl. */
1333 make_decl_rtl_for_debug (tree decl)
1335 unsigned int save_aliasing_flag, save_mudflap_flag;
1336 rtx rtl;
1338 if (DECL_RTL_SET_P (decl))
1339 return DECL_RTL (decl);
1341 /* Kludge alert! Somewhere down the call chain, make_decl_rtl will
1342 call new_alias_set. If running with -fcompare-debug, sometimes
1343 we do not want to create alias sets that will throw the alias
1344 numbers off in the comparison dumps. So... clearing
1345 flag_strict_aliasing will keep new_alias_set() from creating a
1346 new set. It is undesirable to register decl with mudflap
1347 in this case as well. */
1348 save_aliasing_flag = flag_strict_aliasing;
1349 flag_strict_aliasing = 0;
1350 save_mudflap_flag = flag_mudflap;
1351 flag_mudflap = 0;
1353 rtl = DECL_RTL (decl);
1354 /* Reset DECL_RTL back, as various parts of the compiler expects
1355 DECL_RTL set meaning it is actually going to be output. */
1356 SET_DECL_RTL (decl, NULL);
1358 flag_strict_aliasing = save_aliasing_flag;
1359 flag_mudflap = save_mudflap_flag;
1361 return rtl;
1364 /* Output a string of literal assembler code
1365 for an `asm' keyword used between functions. */
1367 void
1368 assemble_asm (tree string)
1370 app_enable ();
1372 if (TREE_CODE (string) == ADDR_EXPR)
1373 string = TREE_OPERAND (string, 0);
1375 fprintf (asm_out_file, "\t%s\n", TREE_STRING_POINTER (string));
1378 /* Record an element in the table of global destructors. SYMBOL is
1379 a SYMBOL_REF of the function to be called; PRIORITY is a number
1380 between 0 and MAX_INIT_PRIORITY. */
1382 void
1383 default_stabs_asm_out_destructor (rtx symbol ATTRIBUTE_UNUSED,
1384 int priority ATTRIBUTE_UNUSED)
1386 #if defined DBX_DEBUGGING_INFO || defined XCOFF_DEBUGGING_INFO
1387 /* Tell GNU LD that this is part of the static destructor set.
1388 This will work for any system that uses stabs, most usefully
1389 aout systems. */
1390 dbxout_begin_simple_stabs ("___DTOR_LIST__", 22 /* N_SETT */);
1391 dbxout_stab_value_label (XSTR (symbol, 0));
1392 #else
1393 sorry ("global destructors not supported on this target");
1394 #endif
1397 /* Write the address of the entity given by SYMBOL to SEC. */
1398 void
1399 assemble_addr_to_section (rtx symbol, section *sec)
1401 switch_to_section (sec);
1402 assemble_align (POINTER_SIZE);
1403 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1406 /* Return the numbered .ctors.N (if CONSTRUCTOR_P) or .dtors.N (if
1407 not) section for PRIORITY. */
1408 section *
1409 get_cdtor_priority_section (int priority, bool constructor_p)
1411 char buf[16];
1413 /* ??? This only works reliably with the GNU linker. */
1414 sprintf (buf, "%s.%.5u",
1415 constructor_p ? ".ctors" : ".dtors",
1416 /* Invert the numbering so the linker puts us in the proper
1417 order; constructors are run from right to left, and the
1418 linker sorts in increasing order. */
1419 MAX_INIT_PRIORITY - priority);
1420 return get_section (buf, SECTION_WRITE, NULL);
1423 void
1424 default_named_section_asm_out_destructor (rtx symbol, int priority)
1426 section *sec;
1428 if (priority != DEFAULT_INIT_PRIORITY)
1429 sec = get_cdtor_priority_section (priority,
1430 /*constructor_p=*/false);
1431 else
1432 sec = get_section (".dtors", SECTION_WRITE, NULL);
1434 assemble_addr_to_section (symbol, sec);
1437 #ifdef DTORS_SECTION_ASM_OP
1438 void
1439 default_dtor_section_asm_out_destructor (rtx symbol,
1440 int priority ATTRIBUTE_UNUSED)
1442 assemble_addr_to_section (symbol, dtors_section);
1444 #endif
1446 /* Likewise for global constructors. */
1448 void
1449 default_stabs_asm_out_constructor (rtx symbol ATTRIBUTE_UNUSED,
1450 int priority ATTRIBUTE_UNUSED)
1452 #if defined DBX_DEBUGGING_INFO || defined XCOFF_DEBUGGING_INFO
1453 /* Tell GNU LD that this is part of the static destructor set.
1454 This will work for any system that uses stabs, most usefully
1455 aout systems. */
1456 dbxout_begin_simple_stabs ("___CTOR_LIST__", 22 /* N_SETT */);
1457 dbxout_stab_value_label (XSTR (symbol, 0));
1458 #else
1459 sorry ("global constructors not supported on this target");
1460 #endif
1463 void
1464 default_named_section_asm_out_constructor (rtx symbol, int priority)
1466 section *sec;
1468 if (priority != DEFAULT_INIT_PRIORITY)
1469 sec = get_cdtor_priority_section (priority,
1470 /*constructor_p=*/true);
1471 else
1472 sec = get_section (".ctors", SECTION_WRITE, NULL);
1474 assemble_addr_to_section (symbol, sec);
1477 #ifdef CTORS_SECTION_ASM_OP
1478 void
1479 default_ctor_section_asm_out_constructor (rtx symbol,
1480 int priority ATTRIBUTE_UNUSED)
1482 assemble_addr_to_section (symbol, ctors_section);
1484 #endif
1486 /* CONSTANT_POOL_BEFORE_FUNCTION may be defined as an expression with
1487 a nonzero value if the constant pool should be output before the
1488 start of the function, or a zero value if the pool should output
1489 after the end of the function. The default is to put it before the
1490 start. */
1492 #ifndef CONSTANT_POOL_BEFORE_FUNCTION
1493 #define CONSTANT_POOL_BEFORE_FUNCTION 1
1494 #endif
1496 /* DECL is an object (either VAR_DECL or FUNCTION_DECL) which is going
1497 to be output to assembler.
1498 Set first_global_object_name and weak_global_object_name as appropriate. */
1500 void
1501 notice_global_symbol (tree decl)
1503 const char **type = &first_global_object_name;
1505 if (first_global_object_name
1506 || !TREE_PUBLIC (decl)
1507 || DECL_EXTERNAL (decl)
1508 || !DECL_NAME (decl)
1509 || (TREE_CODE (decl) != FUNCTION_DECL
1510 && (TREE_CODE (decl) != VAR_DECL
1511 || (DECL_COMMON (decl)
1512 && (DECL_INITIAL (decl) == 0
1513 || DECL_INITIAL (decl) == error_mark_node))))
1514 || !MEM_P (DECL_RTL (decl)))
1515 return;
1517 /* We win when global object is found, but it is useful to know about weak
1518 symbol as well so we can produce nicer unique names. */
1519 if (DECL_WEAK (decl) || DECL_ONE_ONLY (decl) || flag_shlib)
1520 type = &weak_global_object_name;
1522 if (!*type)
1524 const char *p;
1525 const char *name;
1526 rtx decl_rtl = DECL_RTL (decl);
1528 p = targetm.strip_name_encoding (XSTR (XEXP (decl_rtl, 0), 0));
1529 name = ggc_strdup (p);
1531 *type = name;
1535 /* If not using flag_reorder_blocks_and_partition, decide early whether the
1536 current function goes into the cold section, so that targets can use
1537 current_function_section during RTL expansion. DECL describes the
1538 function. */
1540 void
1541 decide_function_section (tree decl)
1543 first_function_block_is_cold = false;
1545 if (flag_reorder_blocks_and_partition)
1546 /* We will decide in assemble_start_function. */
1547 return;
1549 if (DECL_SECTION_NAME (decl))
1551 struct cgraph_node *node = cgraph_get_node (current_function_decl);
1552 /* Calls to function_section rely on first_function_block_is_cold
1553 being accurate. */
1554 first_function_block_is_cold = (node
1555 && node->frequency
1556 == NODE_FREQUENCY_UNLIKELY_EXECUTED);
1559 in_cold_section_p = first_function_block_is_cold;
1562 /* Output assembler code for the constant pool of a function and associated
1563 with defining the name of the function. DECL describes the function.
1564 NAME is the function's name. For the constant pool, we use the current
1565 constant pool data. */
1567 void
1568 assemble_start_function (tree decl, const char *fnname)
1570 int align;
1571 char tmp_label[100];
1572 bool hot_label_written = false;
1574 if (flag_reorder_blocks_and_partition)
1576 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTB", const_labelno);
1577 crtl->subsections.hot_section_label = ggc_strdup (tmp_label);
1578 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDB", const_labelno);
1579 crtl->subsections.cold_section_label = ggc_strdup (tmp_label);
1580 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTE", const_labelno);
1581 crtl->subsections.hot_section_end_label = ggc_strdup (tmp_label);
1582 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDE", const_labelno);
1583 crtl->subsections.cold_section_end_label = ggc_strdup (tmp_label);
1584 const_labelno++;
1586 else
1588 crtl->subsections.hot_section_label = NULL;
1589 crtl->subsections.cold_section_label = NULL;
1590 crtl->subsections.hot_section_end_label = NULL;
1591 crtl->subsections.cold_section_end_label = NULL;
1594 /* The following code does not need preprocessing in the assembler. */
1596 app_disable ();
1598 if (CONSTANT_POOL_BEFORE_FUNCTION)
1599 output_constant_pool (fnname, decl);
1601 /* Make sure the not and cold text (code) sections are properly
1602 aligned. This is necessary here in the case where the function
1603 has both hot and cold sections, because we don't want to re-set
1604 the alignment when the section switch happens mid-function. */
1606 if (flag_reorder_blocks_and_partition)
1608 first_function_block_is_cold = false;
1610 switch_to_section (unlikely_text_section ());
1611 assemble_align (DECL_ALIGN (decl));
1612 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_label);
1614 /* When the function starts with a cold section, we need to explicitly
1615 align the hot section and write out the hot section label.
1616 But if the current function is a thunk, we do not have a CFG. */
1617 if (!cfun->is_thunk
1618 && BB_PARTITION (ENTRY_BLOCK_PTR->next_bb) == BB_COLD_PARTITION)
1620 switch_to_section (text_section);
1621 assemble_align (DECL_ALIGN (decl));
1622 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
1623 hot_label_written = true;
1624 first_function_block_is_cold = true;
1626 in_cold_section_p = first_function_block_is_cold;
1630 /* Switch to the correct text section for the start of the function. */
1632 switch_to_section (function_section (decl));
1633 if (flag_reorder_blocks_and_partition
1634 && !hot_label_written)
1635 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
1637 /* Tell assembler to move to target machine's alignment for functions. */
1638 align = floor_log2 (DECL_ALIGN (decl) / BITS_PER_UNIT);
1639 if (align > 0)
1641 ASM_OUTPUT_ALIGN (asm_out_file, align);
1644 /* Handle a user-specified function alignment.
1645 Note that we still need to align to DECL_ALIGN, as above,
1646 because ASM_OUTPUT_MAX_SKIP_ALIGN might not do any alignment at all. */
1647 if (! DECL_USER_ALIGN (decl)
1648 && align_functions_log > align
1649 && optimize_function_for_speed_p (cfun))
1651 #ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
1652 ASM_OUTPUT_MAX_SKIP_ALIGN (asm_out_file,
1653 align_functions_log, align_functions - 1);
1654 #else
1655 ASM_OUTPUT_ALIGN (asm_out_file, align_functions_log);
1656 #endif
1659 #ifdef ASM_OUTPUT_FUNCTION_PREFIX
1660 ASM_OUTPUT_FUNCTION_PREFIX (asm_out_file, fnname);
1661 #endif
1663 if (!DECL_IGNORED_P (decl))
1664 (*debug_hooks->begin_function) (decl);
1666 /* Make function name accessible from other files, if appropriate. */
1668 if (TREE_PUBLIC (decl))
1670 notice_global_symbol (decl);
1672 globalize_decl (decl);
1674 maybe_assemble_visibility (decl);
1677 if (DECL_PRESERVE_P (decl))
1678 targetm.asm_out.mark_decl_preserved (fnname);
1680 /* Do any machine/system dependent processing of the function name. */
1681 #ifdef ASM_DECLARE_FUNCTION_NAME
1682 ASM_DECLARE_FUNCTION_NAME (asm_out_file, fnname, current_function_decl);
1683 #else
1684 /* Standard thing is just output label for the function. */
1685 ASM_OUTPUT_FUNCTION_LABEL (asm_out_file, fnname, current_function_decl);
1686 #endif /* ASM_DECLARE_FUNCTION_NAME */
1688 if (lookup_attribute ("no_split_stack", DECL_ATTRIBUTES (decl)))
1689 saw_no_split_stack = true;
1692 /* Output assembler code associated with defining the size of the
1693 function. DECL describes the function. NAME is the function's name. */
1695 void
1696 assemble_end_function (tree decl, const char *fnname ATTRIBUTE_UNUSED)
1698 #ifdef ASM_DECLARE_FUNCTION_SIZE
1699 /* We could have switched section in the middle of the function. */
1700 if (flag_reorder_blocks_and_partition)
1701 switch_to_section (function_section (decl));
1702 ASM_DECLARE_FUNCTION_SIZE (asm_out_file, fnname, decl);
1703 #endif
1704 if (! CONSTANT_POOL_BEFORE_FUNCTION)
1706 output_constant_pool (fnname, decl);
1707 switch_to_section (function_section (decl)); /* need to switch back */
1709 /* Output labels for end of hot/cold text sections (to be used by
1710 debug info.) */
1711 if (flag_reorder_blocks_and_partition)
1713 section *save_text_section;
1715 save_text_section = in_section;
1716 switch_to_section (unlikely_text_section ());
1717 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_end_label);
1718 if (first_function_block_is_cold)
1719 switch_to_section (text_section);
1720 else
1721 switch_to_section (function_section (decl));
1722 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_end_label);
1723 switch_to_section (save_text_section);
1727 /* Assemble code to leave SIZE bytes of zeros. */
1729 void
1730 assemble_zeros (unsigned HOST_WIDE_INT size)
1732 /* Do no output if -fsyntax-only. */
1733 if (flag_syntax_only)
1734 return;
1736 #ifdef ASM_NO_SKIP_IN_TEXT
1737 /* The `space' pseudo in the text section outputs nop insns rather than 0s,
1738 so we must output 0s explicitly in the text section. */
1739 if (ASM_NO_SKIP_IN_TEXT && (in_section->common.flags & SECTION_CODE) != 0)
1741 unsigned HOST_WIDE_INT i;
1742 for (i = 0; i < size; i++)
1743 assemble_integer (const0_rtx, 1, BITS_PER_UNIT, 1);
1745 else
1746 #endif
1747 if (size > 0)
1748 ASM_OUTPUT_SKIP (asm_out_file, size);
1751 /* Assemble an alignment pseudo op for an ALIGN-bit boundary. */
1753 void
1754 assemble_align (int align)
1756 if (align > BITS_PER_UNIT)
1758 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
1762 /* Assemble a string constant with the specified C string as contents. */
1764 void
1765 assemble_string (const char *p, int size)
1767 int pos = 0;
1768 int maximum = 2000;
1770 /* If the string is very long, split it up. */
1772 while (pos < size)
1774 int thissize = size - pos;
1775 if (thissize > maximum)
1776 thissize = maximum;
1778 ASM_OUTPUT_ASCII (asm_out_file, p, thissize);
1780 pos += thissize;
1781 p += thissize;
1786 /* A noswitch_section_callback for lcomm_section. */
1788 static bool
1789 emit_local (tree decl ATTRIBUTE_UNUSED,
1790 const char *name ATTRIBUTE_UNUSED,
1791 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1792 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1794 #if defined ASM_OUTPUT_ALIGNED_DECL_LOCAL
1795 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, decl, name,
1796 size, DECL_ALIGN (decl));
1797 return true;
1798 #elif defined ASM_OUTPUT_ALIGNED_LOCAL
1799 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, DECL_ALIGN (decl));
1800 return true;
1801 #else
1802 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
1803 return false;
1804 #endif
1807 /* A noswitch_section_callback for bss_noswitch_section. */
1809 #if defined ASM_OUTPUT_ALIGNED_BSS
1810 static bool
1811 emit_bss (tree decl ATTRIBUTE_UNUSED,
1812 const char *name ATTRIBUTE_UNUSED,
1813 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1814 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1816 #if defined ASM_OUTPUT_ALIGNED_BSS
1817 ASM_OUTPUT_ALIGNED_BSS (asm_out_file, decl, name, size, DECL_ALIGN (decl));
1818 return true;
1819 #endif
1821 #endif
1823 /* A noswitch_section_callback for comm_section. */
1825 static bool
1826 emit_common (tree decl ATTRIBUTE_UNUSED,
1827 const char *name ATTRIBUTE_UNUSED,
1828 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1829 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1831 #if defined ASM_OUTPUT_ALIGNED_DECL_COMMON
1832 ASM_OUTPUT_ALIGNED_DECL_COMMON (asm_out_file, decl, name,
1833 size, DECL_ALIGN (decl));
1834 return true;
1835 #elif defined ASM_OUTPUT_ALIGNED_COMMON
1836 ASM_OUTPUT_ALIGNED_COMMON (asm_out_file, name, size, DECL_ALIGN (decl));
1837 return true;
1838 #else
1839 ASM_OUTPUT_COMMON (asm_out_file, name, size, rounded);
1840 return false;
1841 #endif
1844 /* A noswitch_section_callback for tls_comm_section. */
1846 static bool
1847 emit_tls_common (tree decl ATTRIBUTE_UNUSED,
1848 const char *name ATTRIBUTE_UNUSED,
1849 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1850 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1852 #ifdef ASM_OUTPUT_TLS_COMMON
1853 ASM_OUTPUT_TLS_COMMON (asm_out_file, decl, name, size);
1854 return true;
1855 #else
1856 sorry ("thread-local COMMON data not implemented");
1857 return true;
1858 #endif
1861 /* Assemble DECL given that it belongs in SECTION_NOSWITCH section SECT.
1862 NAME is the name of DECL's SYMBOL_REF. */
1864 static void
1865 assemble_noswitch_variable (tree decl, const char *name, section *sect)
1867 unsigned HOST_WIDE_INT size, rounded;
1869 size = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
1870 rounded = size;
1872 /* Don't allocate zero bytes of common,
1873 since that means "undefined external" in the linker. */
1874 if (size == 0)
1875 rounded = 1;
1877 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
1878 so that each uninitialized object starts on such a boundary. */
1879 rounded += (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1;
1880 rounded = (rounded / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
1881 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
1883 if (!sect->noswitch.callback (decl, name, size, rounded)
1884 && (unsigned HOST_WIDE_INT) DECL_ALIGN_UNIT (decl) > rounded)
1885 warning (0, "requested alignment for %q+D is greater than "
1886 "implemented alignment of %wu", decl, rounded);
1889 /* A subroutine of assemble_variable. Output the label and contents of
1890 DECL, whose address is a SYMBOL_REF with name NAME. DONT_OUTPUT_DATA
1891 is as for assemble_variable. */
1893 static void
1894 assemble_variable_contents (tree decl, const char *name,
1895 bool dont_output_data)
1897 /* Do any machine/system dependent processing of the object. */
1898 #ifdef ASM_DECLARE_OBJECT_NAME
1899 last_assemble_variable_decl = decl;
1900 ASM_DECLARE_OBJECT_NAME (asm_out_file, name, decl);
1901 #else
1902 /* Standard thing is just output label for the object. */
1903 ASM_OUTPUT_LABEL (asm_out_file, name);
1904 #endif /* ASM_DECLARE_OBJECT_NAME */
1906 if (!dont_output_data)
1908 if (DECL_INITIAL (decl)
1909 && DECL_INITIAL (decl) != error_mark_node
1910 && !initializer_zerop (DECL_INITIAL (decl)))
1911 /* Output the actual data. */
1912 output_constant (DECL_INITIAL (decl),
1913 tree_low_cst (DECL_SIZE_UNIT (decl), 1),
1914 DECL_ALIGN (decl));
1915 else
1916 /* Leave space for it. */
1917 assemble_zeros (tree_low_cst (DECL_SIZE_UNIT (decl), 1));
1921 /* Assemble everything that is needed for a variable or function declaration.
1922 Not used for automatic variables, and not used for function definitions.
1923 Should not be called for variables of incomplete structure type.
1925 TOP_LEVEL is nonzero if this variable has file scope.
1926 AT_END is nonzero if this is the special handling, at end of compilation,
1927 to define things that have had only tentative definitions.
1928 DONT_OUTPUT_DATA if nonzero means don't actually output the
1929 initial value (that will be done by the caller). */
1931 void
1932 assemble_variable (tree decl, int top_level ATTRIBUTE_UNUSED,
1933 int at_end ATTRIBUTE_UNUSED, int dont_output_data)
1935 const char *name;
1936 rtx decl_rtl, symbol;
1937 section *sect;
1939 /* This function is supposed to handle VARIABLES. Ensure we have one. */
1940 gcc_assert (TREE_CODE (decl) == VAR_DECL);
1942 /* Emulated TLS had better not get this far. */
1943 gcc_checking_assert (targetm.have_tls || !DECL_THREAD_LOCAL_P (decl));
1945 last_assemble_variable_decl = 0;
1947 /* Normally no need to say anything here for external references,
1948 since assemble_external is called by the language-specific code
1949 when a declaration is first seen. */
1951 if (DECL_EXTERNAL (decl))
1952 return;
1954 /* Do nothing for global register variables. */
1955 if (DECL_RTL_SET_P (decl) && REG_P (DECL_RTL (decl)))
1957 TREE_ASM_WRITTEN (decl) = 1;
1958 return;
1961 /* If type was incomplete when the variable was declared,
1962 see if it is complete now. */
1964 if (DECL_SIZE (decl) == 0)
1965 layout_decl (decl, 0);
1967 /* Still incomplete => don't allocate it; treat the tentative defn
1968 (which is what it must have been) as an `extern' reference. */
1970 if (!dont_output_data && DECL_SIZE (decl) == 0)
1972 error ("storage size of %q+D isn%'t known", decl);
1973 TREE_ASM_WRITTEN (decl) = 1;
1974 return;
1977 /* The first declaration of a variable that comes through this function
1978 decides whether it is global (in C, has external linkage)
1979 or local (in C, has internal linkage). So do nothing more
1980 if this function has already run. */
1982 if (TREE_ASM_WRITTEN (decl))
1983 return;
1985 /* Make sure targetm.encode_section_info is invoked before we set
1986 ASM_WRITTEN. */
1987 decl_rtl = DECL_RTL (decl);
1989 TREE_ASM_WRITTEN (decl) = 1;
1991 /* Do no output if -fsyntax-only. */
1992 if (flag_syntax_only)
1993 return;
1995 if (! dont_output_data
1996 && ! host_integerp (DECL_SIZE_UNIT (decl), 1))
1998 error ("size of variable %q+D is too large", decl);
1999 return;
2002 gcc_assert (MEM_P (decl_rtl));
2003 gcc_assert (GET_CODE (XEXP (decl_rtl, 0)) == SYMBOL_REF);
2004 symbol = XEXP (decl_rtl, 0);
2006 /* If this symbol belongs to the tree constant pool, output the constant
2007 if it hasn't already been written. */
2008 if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
2010 tree decl = SYMBOL_REF_DECL (symbol);
2011 if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
2012 output_constant_def_contents (symbol);
2013 return;
2016 app_disable ();
2018 name = XSTR (symbol, 0);
2019 if (TREE_PUBLIC (decl) && DECL_NAME (decl))
2020 notice_global_symbol (decl);
2022 /* Compute the alignment of this data. */
2024 align_variable (decl, dont_output_data);
2025 set_mem_align (decl_rtl, DECL_ALIGN (decl));
2027 if (TREE_PUBLIC (decl))
2028 maybe_assemble_visibility (decl);
2030 if (DECL_PRESERVE_P (decl))
2031 targetm.asm_out.mark_decl_preserved (name);
2033 /* First make the assembler name(s) global if appropriate. */
2034 sect = get_variable_section (decl, false);
2035 if (TREE_PUBLIC (decl)
2036 && (sect->common.flags & SECTION_COMMON) == 0)
2037 globalize_decl (decl);
2039 /* Output any data that we will need to use the address of. */
2040 if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node)
2041 output_addressed_constants (DECL_INITIAL (decl));
2043 /* dbxout.c needs to know this. */
2044 if (sect && (sect->common.flags & SECTION_CODE) != 0)
2045 DECL_IN_TEXT_SECTION (decl) = 1;
2047 /* If the decl is part of an object_block, make sure that the decl
2048 has been positioned within its block, but do not write out its
2049 definition yet. output_object_blocks will do that later. */
2050 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
2052 gcc_assert (!dont_output_data);
2053 place_block_symbol (symbol);
2055 else if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
2056 assemble_noswitch_variable (decl, name, sect);
2057 else
2059 switch_to_section (sect);
2060 if (DECL_ALIGN (decl) > BITS_PER_UNIT)
2061 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (DECL_ALIGN_UNIT (decl)));
2062 assemble_variable_contents (decl, name, dont_output_data);
2066 /* Return 1 if type TYPE contains any pointers. */
2068 static int
2069 contains_pointers_p (tree type)
2071 switch (TREE_CODE (type))
2073 case POINTER_TYPE:
2074 case REFERENCE_TYPE:
2075 /* I'm not sure whether OFFSET_TYPE needs this treatment,
2076 so I'll play safe and return 1. */
2077 case OFFSET_TYPE:
2078 return 1;
2080 case RECORD_TYPE:
2081 case UNION_TYPE:
2082 case QUAL_UNION_TYPE:
2084 tree fields;
2085 /* For a type that has fields, see if the fields have pointers. */
2086 for (fields = TYPE_FIELDS (type); fields; fields = DECL_CHAIN (fields))
2087 if (TREE_CODE (fields) == FIELD_DECL
2088 && contains_pointers_p (TREE_TYPE (fields)))
2089 return 1;
2090 return 0;
2093 case ARRAY_TYPE:
2094 /* An array type contains pointers if its element type does. */
2095 return contains_pointers_p (TREE_TYPE (type));
2097 default:
2098 return 0;
2102 /* We delay assemble_external processing until
2103 the compilation unit is finalized. This is the best we can do for
2104 right now (i.e. stage 3 of GCC 4.0) - the right thing is to delay
2105 it all the way to final. See PR 17982 for further discussion. */
2106 static GTY(()) tree pending_assemble_externals;
2108 /* FIXME: Trunk is at GCC 4.8 now and the above problem still hasn't been
2109 addressed properly. This caused PR 52640 due to O(external_decls**2)
2110 lookups in the pending_assemble_externals TREE_LIST in assemble_external.
2111 Paper over with this pointer set, which we use to see if we have already
2112 added a decl to pending_assemble_externals without first traversing
2113 the entire pending_assemble_externals list. See assemble_external(). */
2114 static struct pointer_set_t *pending_assemble_externals_set;
2116 /* Some targets delay some output to final using TARGET_ASM_FILE_END.
2117 As a result, assemble_external can be called after the list of externals
2118 is processed and the pointer set destroyed. */
2119 static bool pending_assemble_externals_processed;
2121 #ifdef ASM_OUTPUT_EXTERNAL
2122 /* True if DECL is a function decl for which no out-of-line copy exists.
2123 It is assumed that DECL's assembler name has been set. */
2125 static bool
2126 incorporeal_function_p (tree decl)
2128 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
2130 const char *name;
2132 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
2133 && (DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA
2134 || DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA_WITH_ALIGN))
2135 return true;
2137 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2138 /* Atomic or sync builtins which have survived this far will be
2139 resolved externally and therefore are not incorporeal. */
2140 if (strncmp (name, "__builtin_", 10) == 0)
2141 return true;
2143 return false;
2146 /* Actually do the tests to determine if this is necessary, and invoke
2147 ASM_OUTPUT_EXTERNAL. */
2148 static void
2149 assemble_external_real (tree decl)
2151 rtx rtl = DECL_RTL (decl);
2153 if (MEM_P (rtl) && GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF
2154 && !SYMBOL_REF_USED (XEXP (rtl, 0))
2155 && !incorporeal_function_p (decl))
2157 /* Some systems do require some output. */
2158 SYMBOL_REF_USED (XEXP (rtl, 0)) = 1;
2159 ASM_OUTPUT_EXTERNAL (asm_out_file, decl, XSTR (XEXP (rtl, 0), 0));
2162 #endif
2164 void
2165 process_pending_assemble_externals (void)
2167 #ifdef ASM_OUTPUT_EXTERNAL
2168 tree list;
2169 for (list = pending_assemble_externals; list; list = TREE_CHAIN (list))
2170 assemble_external_real (TREE_VALUE (list));
2172 pending_assemble_externals = 0;
2173 pending_assemble_externals_processed = true;
2174 pointer_set_destroy (pending_assemble_externals_set);
2175 #endif
2178 /* This TREE_LIST contains any weak symbol declarations waiting
2179 to be emitted. */
2180 static GTY(()) tree weak_decls;
2182 /* Output something to declare an external symbol to the assembler,
2183 and qualifiers such as weakness. (Most assemblers don't need
2184 extern declaration, so we normally output nothing.) Do nothing if
2185 DECL is not external. */
2187 void
2188 assemble_external (tree decl ATTRIBUTE_UNUSED)
2190 /* Because most platforms do not define ASM_OUTPUT_EXTERNAL, the
2191 main body of this code is only rarely exercised. To provide some
2192 testing, on all platforms, we make sure that the ASM_OUT_FILE is
2193 open. If it's not, we should not be calling this function. */
2194 gcc_assert (asm_out_file);
2196 if (!DECL_P (decl) || !DECL_EXTERNAL (decl) || !TREE_PUBLIC (decl))
2197 return;
2199 /* We want to output annotation for weak and external symbols at
2200 very last to check if they are references or not. */
2202 if (TARGET_SUPPORTS_WEAK
2203 && DECL_WEAK (decl)
2204 /* TREE_STATIC is a weird and abused creature which is not
2205 generally the right test for whether an entity has been
2206 locally emitted, inlined or otherwise not-really-extern, but
2207 for declarations that can be weak, it happens to be
2208 match. */
2209 && !TREE_STATIC (decl)
2210 && lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
2211 && value_member (decl, weak_decls) == NULL_TREE)
2212 weak_decls = tree_cons (NULL, decl, weak_decls);
2214 #ifdef ASM_OUTPUT_EXTERNAL
2215 if (pending_assemble_externals_processed)
2217 assemble_external_real (decl);
2218 return;
2221 if (! pointer_set_insert (pending_assemble_externals_set, decl))
2222 pending_assemble_externals = tree_cons (NULL, decl,
2223 pending_assemble_externals);
2224 #endif
2227 /* Similar, for calling a library function FUN. */
2229 void
2230 assemble_external_libcall (rtx fun)
2232 /* Declare library function name external when first used, if nec. */
2233 if (! SYMBOL_REF_USED (fun))
2235 SYMBOL_REF_USED (fun) = 1;
2236 targetm.asm_out.external_libcall (fun);
2240 /* Assemble a label named NAME. */
2242 void
2243 assemble_label (FILE *file, const char *name)
2245 ASM_OUTPUT_LABEL (file, name);
2248 /* Set the symbol_referenced flag for ID. */
2249 void
2250 mark_referenced (tree id)
2252 TREE_SYMBOL_REFERENCED (id) = 1;
2255 /* Set the symbol_referenced flag for DECL and notify callgraph. */
2256 void
2257 mark_decl_referenced (tree decl)
2259 if (TREE_CODE (decl) == FUNCTION_DECL)
2261 /* Extern inline functions don't become needed when referenced.
2262 If we know a method will be emitted in other TU and no new
2263 functions can be marked reachable, just use the external
2264 definition. */
2265 struct cgraph_node *node = cgraph_get_create_node (decl);
2266 if (!DECL_EXTERNAL (decl)
2267 && !node->local.finalized)
2268 cgraph_mark_needed_node (node);
2270 else if (TREE_CODE (decl) == VAR_DECL)
2272 struct varpool_node *node = varpool_node (decl);
2273 varpool_mark_needed_node (node);
2274 /* C++ frontend use mark_decl_references to force COMDAT variables
2275 to be output that might appear dead otherwise. */
2276 node->force_output = true;
2278 /* else do nothing - we can get various sorts of CST nodes here,
2279 which do not need to be marked. */
2283 /* Follow the IDENTIFIER_TRANSPARENT_ALIAS chain starting at *ALIAS
2284 until we find an identifier that is not itself a transparent alias.
2285 Modify the alias passed to it by reference (and all aliases on the
2286 way to the ultimate target), such that they do not have to be
2287 followed again, and return the ultimate target of the alias
2288 chain. */
2290 static inline tree
2291 ultimate_transparent_alias_target (tree *alias)
2293 tree target = *alias;
2295 if (IDENTIFIER_TRANSPARENT_ALIAS (target))
2297 gcc_assert (TREE_CHAIN (target));
2298 target = ultimate_transparent_alias_target (&TREE_CHAIN (target));
2299 gcc_assert (! IDENTIFIER_TRANSPARENT_ALIAS (target)
2300 && ! TREE_CHAIN (target));
2301 *alias = target;
2304 return target;
2307 /* Output to FILE (an assembly file) a reference to NAME. If NAME
2308 starts with a *, the rest of NAME is output verbatim. Otherwise
2309 NAME is transformed in a target-specific way (usually by the
2310 addition of an underscore). */
2312 void
2313 assemble_name_raw (FILE *file, const char *name)
2315 if (name[0] == '*')
2316 fputs (&name[1], file);
2317 else
2318 ASM_OUTPUT_LABELREF (file, name);
2321 /* Like assemble_name_raw, but should be used when NAME might refer to
2322 an entity that is also represented as a tree (like a function or
2323 variable). If NAME does refer to such an entity, that entity will
2324 be marked as referenced. */
2326 void
2327 assemble_name (FILE *file, const char *name)
2329 const char *real_name;
2330 tree id;
2332 real_name = targetm.strip_name_encoding (name);
2334 id = maybe_get_identifier (real_name);
2335 if (id)
2337 tree id_orig = id;
2339 mark_referenced (id);
2340 ultimate_transparent_alias_target (&id);
2341 if (id != id_orig)
2342 name = IDENTIFIER_POINTER (id);
2343 gcc_assert (! TREE_CHAIN (id));
2346 assemble_name_raw (file, name);
2349 /* Allocate SIZE bytes writable static space with a gensym name
2350 and return an RTX to refer to its address. */
2353 assemble_static_space (unsigned HOST_WIDE_INT size)
2355 char name[12];
2356 const char *namestring;
2357 rtx x;
2359 ASM_GENERATE_INTERNAL_LABEL (name, "LF", const_labelno);
2360 ++const_labelno;
2361 namestring = ggc_strdup (name);
2363 x = gen_rtx_SYMBOL_REF (Pmode, namestring);
2364 SYMBOL_REF_FLAGS (x) = SYMBOL_FLAG_LOCAL;
2366 #ifdef ASM_OUTPUT_ALIGNED_DECL_LOCAL
2367 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, NULL_TREE, name, size,
2368 BIGGEST_ALIGNMENT);
2369 #else
2370 #ifdef ASM_OUTPUT_ALIGNED_LOCAL
2371 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, BIGGEST_ALIGNMENT);
2372 #else
2374 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
2375 so that each uninitialized object starts on such a boundary. */
2376 /* Variable `rounded' might or might not be used in ASM_OUTPUT_LOCAL. */
2377 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED
2378 = ((size + (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1)
2379 / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
2380 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
2381 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
2383 #endif
2384 #endif
2385 return x;
2388 /* Assemble the static constant template for function entry trampolines.
2389 This is done at most once per compilation.
2390 Returns an RTX for the address of the template. */
2392 static GTY(()) rtx initial_trampoline;
2395 assemble_trampoline_template (void)
2397 char label[256];
2398 const char *name;
2399 int align;
2400 rtx symbol;
2402 gcc_assert (targetm.asm_out.trampoline_template != NULL);
2404 if (initial_trampoline)
2405 return initial_trampoline;
2407 /* By default, put trampoline templates in read-only data section. */
2409 #ifdef TRAMPOLINE_SECTION
2410 switch_to_section (TRAMPOLINE_SECTION);
2411 #else
2412 switch_to_section (readonly_data_section);
2413 #endif
2415 /* Write the assembler code to define one. */
2416 align = floor_log2 (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
2417 if (align > 0)
2418 ASM_OUTPUT_ALIGN (asm_out_file, align);
2420 targetm.asm_out.internal_label (asm_out_file, "LTRAMP", 0);
2421 targetm.asm_out.trampoline_template (asm_out_file);
2423 /* Record the rtl to refer to it. */
2424 ASM_GENERATE_INTERNAL_LABEL (label, "LTRAMP", 0);
2425 name = ggc_strdup (label);
2426 symbol = gen_rtx_SYMBOL_REF (Pmode, name);
2427 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_LOCAL;
2429 initial_trampoline = gen_const_mem (BLKmode, symbol);
2430 set_mem_align (initial_trampoline, TRAMPOLINE_ALIGNMENT);
2431 set_mem_size (initial_trampoline, TRAMPOLINE_SIZE);
2433 return initial_trampoline;
2436 /* A and B are either alignments or offsets. Return the minimum alignment
2437 that may be assumed after adding the two together. */
2439 static inline unsigned
2440 min_align (unsigned int a, unsigned int b)
2442 return (a | b) & -(a | b);
2445 /* Return the assembler directive for creating a given kind of integer
2446 object. SIZE is the number of bytes in the object and ALIGNED_P
2447 indicates whether it is known to be aligned. Return NULL if the
2448 assembly dialect has no such directive.
2450 The returned string should be printed at the start of a new line and
2451 be followed immediately by the object's initial value. */
2453 const char *
2454 integer_asm_op (int size, int aligned_p)
2456 struct asm_int_op *ops;
2458 if (aligned_p)
2459 ops = &targetm.asm_out.aligned_op;
2460 else
2461 ops = &targetm.asm_out.unaligned_op;
2463 switch (size)
2465 case 1:
2466 return targetm.asm_out.byte_op;
2467 case 2:
2468 return ops->hi;
2469 case 4:
2470 return ops->si;
2471 case 8:
2472 return ops->di;
2473 case 16:
2474 return ops->ti;
2475 default:
2476 return NULL;
2480 /* Use directive OP to assemble an integer object X. Print OP at the
2481 start of the line, followed immediately by the value of X. */
2483 void
2484 assemble_integer_with_op (const char *op, rtx x)
2486 fputs (op, asm_out_file);
2487 output_addr_const (asm_out_file, x);
2488 fputc ('\n', asm_out_file);
2491 /* The default implementation of the asm_out.integer target hook. */
2493 bool
2494 default_assemble_integer (rtx x ATTRIBUTE_UNUSED,
2495 unsigned int size ATTRIBUTE_UNUSED,
2496 int aligned_p ATTRIBUTE_UNUSED)
2498 const char *op = integer_asm_op (size, aligned_p);
2499 /* Avoid GAS bugs for large values. Specifically negative values whose
2500 absolute value fits in a bfd_vma, but not in a bfd_signed_vma. */
2501 if (size > UNITS_PER_WORD && size > POINTER_SIZE / BITS_PER_UNIT)
2502 return false;
2503 return op && (assemble_integer_with_op (op, x), true);
2506 /* Assemble the integer constant X into an object of SIZE bytes. ALIGN is
2507 the alignment of the integer in bits. Return 1 if we were able to output
2508 the constant, otherwise 0. We must be able to output the constant,
2509 if FORCE is nonzero. */
2511 bool
2512 assemble_integer (rtx x, unsigned int size, unsigned int align, int force)
2514 int aligned_p;
2516 aligned_p = (align >= MIN (size * BITS_PER_UNIT, BIGGEST_ALIGNMENT));
2518 /* See if the target hook can handle this kind of object. */
2519 if (targetm.asm_out.integer (x, size, aligned_p))
2520 return true;
2522 /* If the object is a multi-byte one, try splitting it up. Split
2523 it into words it if is multi-word, otherwise split it into bytes. */
2524 if (size > 1)
2526 enum machine_mode omode, imode;
2527 unsigned int subalign;
2528 unsigned int subsize, i;
2529 enum mode_class mclass;
2531 subsize = size > UNITS_PER_WORD? UNITS_PER_WORD : 1;
2532 subalign = MIN (align, subsize * BITS_PER_UNIT);
2533 if (GET_CODE (x) == CONST_FIXED)
2534 mclass = GET_MODE_CLASS (GET_MODE (x));
2535 else
2536 mclass = MODE_INT;
2538 omode = mode_for_size (subsize * BITS_PER_UNIT, mclass, 0);
2539 imode = mode_for_size (size * BITS_PER_UNIT, mclass, 0);
2541 for (i = 0; i < size; i += subsize)
2543 rtx partial = simplify_subreg (omode, x, imode, i);
2544 if (!partial || !assemble_integer (partial, subsize, subalign, 0))
2545 break;
2547 if (i == size)
2548 return true;
2550 /* If we've printed some of it, but not all of it, there's no going
2551 back now. */
2552 gcc_assert (!i);
2555 gcc_assert (!force);
2557 return false;
2560 void
2561 assemble_real (REAL_VALUE_TYPE d, enum machine_mode mode, unsigned int align)
2563 long data[4] = {0, 0, 0, 0};
2564 int i;
2565 int bitsize, nelts, nunits, units_per;
2567 /* This is hairy. We have a quantity of known size. real_to_target
2568 will put it into an array of *host* longs, 32 bits per element
2569 (even if long is more than 32 bits). We need to determine the
2570 number of array elements that are occupied (nelts) and the number
2571 of *target* min-addressable units that will be occupied in the
2572 object file (nunits). We cannot assume that 32 divides the
2573 mode's bitsize (size * BITS_PER_UNIT) evenly.
2575 size * BITS_PER_UNIT is used here to make sure that padding bits
2576 (which might appear at either end of the value; real_to_target
2577 will include the padding bits in its output array) are included. */
2579 nunits = GET_MODE_SIZE (mode);
2580 bitsize = nunits * BITS_PER_UNIT;
2581 nelts = CEIL (bitsize, 32);
2582 units_per = 32 / BITS_PER_UNIT;
2584 real_to_target (data, &d, mode);
2586 /* Put out the first word with the specified alignment. */
2587 assemble_integer (GEN_INT (data[0]), MIN (nunits, units_per), align, 1);
2588 nunits -= units_per;
2590 /* Subsequent words need only 32-bit alignment. */
2591 align = min_align (align, 32);
2593 for (i = 1; i < nelts; i++)
2595 assemble_integer (GEN_INT (data[i]), MIN (nunits, units_per), align, 1);
2596 nunits -= units_per;
2600 /* Given an expression EXP with a constant value,
2601 reduce it to the sum of an assembler symbol and an integer.
2602 Store them both in the structure *VALUE.
2603 EXP must be reducible. */
2605 struct addr_const {
2606 rtx base;
2607 HOST_WIDE_INT offset;
2610 static void
2611 decode_addr_const (tree exp, struct addr_const *value)
2613 tree target = TREE_OPERAND (exp, 0);
2614 int offset = 0;
2615 rtx x;
2617 while (1)
2619 if (TREE_CODE (target) == COMPONENT_REF
2620 && host_integerp (byte_position (TREE_OPERAND (target, 1)), 0))
2622 offset += int_byte_position (TREE_OPERAND (target, 1));
2623 target = TREE_OPERAND (target, 0);
2625 else if (TREE_CODE (target) == ARRAY_REF
2626 || TREE_CODE (target) == ARRAY_RANGE_REF)
2628 offset += (tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (target)), 1)
2629 * tree_low_cst (TREE_OPERAND (target, 1), 0));
2630 target = TREE_OPERAND (target, 0);
2632 else if (TREE_CODE (target) == MEM_REF
2633 && TREE_CODE (TREE_OPERAND (target, 0)) == ADDR_EXPR)
2635 offset += mem_ref_offset (target).low;
2636 target = TREE_OPERAND (TREE_OPERAND (target, 0), 0);
2638 else if (TREE_CODE (target) == INDIRECT_REF
2639 && TREE_CODE (TREE_OPERAND (target, 0)) == NOP_EXPR
2640 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (target, 0), 0))
2641 == ADDR_EXPR)
2642 target = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (target, 0), 0), 0);
2643 else
2644 break;
2647 switch (TREE_CODE (target))
2649 case VAR_DECL:
2650 case FUNCTION_DECL:
2651 x = DECL_RTL (target);
2652 break;
2654 case LABEL_DECL:
2655 x = gen_rtx_MEM (FUNCTION_MODE,
2656 gen_rtx_LABEL_REF (Pmode, force_label_rtx (target)));
2657 break;
2659 case REAL_CST:
2660 case FIXED_CST:
2661 case STRING_CST:
2662 case COMPLEX_CST:
2663 case CONSTRUCTOR:
2664 case INTEGER_CST:
2665 x = output_constant_def (target, 1);
2666 break;
2668 default:
2669 gcc_unreachable ();
2672 gcc_assert (MEM_P (x));
2673 x = XEXP (x, 0);
2675 value->base = x;
2676 value->offset = offset;
2680 static GTY((param_is (struct constant_descriptor_tree)))
2681 htab_t const_desc_htab;
2683 static void maybe_output_constant_def_contents (struct constant_descriptor_tree *, int);
2685 /* Constant pool accessor function. */
2687 htab_t
2688 constant_pool_htab (void)
2690 return const_desc_htab;
2693 /* Compute a hash code for a constant expression. */
2695 static hashval_t
2696 const_desc_hash (const void *ptr)
2698 return ((const struct constant_descriptor_tree *)ptr)->hash;
2701 static hashval_t
2702 const_hash_1 (const tree exp)
2704 const char *p;
2705 hashval_t hi;
2706 int len, i;
2707 enum tree_code code = TREE_CODE (exp);
2709 /* Either set P and LEN to the address and len of something to hash and
2710 exit the switch or return a value. */
2712 switch (code)
2714 case INTEGER_CST:
2715 p = (char *) &TREE_INT_CST (exp);
2716 len = sizeof TREE_INT_CST (exp);
2717 break;
2719 case REAL_CST:
2720 return real_hash (TREE_REAL_CST_PTR (exp));
2722 case FIXED_CST:
2723 return fixed_hash (TREE_FIXED_CST_PTR (exp));
2725 case STRING_CST:
2726 p = TREE_STRING_POINTER (exp);
2727 len = TREE_STRING_LENGTH (exp);
2728 break;
2730 case COMPLEX_CST:
2731 return (const_hash_1 (TREE_REALPART (exp)) * 5
2732 + const_hash_1 (TREE_IMAGPART (exp)));
2734 case VECTOR_CST:
2736 tree link;
2738 hi = 7 + TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp));
2740 for (link = TREE_VECTOR_CST_ELTS (exp); link; link = TREE_CHAIN (link))
2741 hi = hi * 563 + const_hash_1 (TREE_VALUE (link));
2743 return hi;
2746 case CONSTRUCTOR:
2748 unsigned HOST_WIDE_INT idx;
2749 tree value;
2751 hi = 5 + int_size_in_bytes (TREE_TYPE (exp));
2753 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
2754 if (value)
2755 hi = hi * 603 + const_hash_1 (value);
2757 return hi;
2760 case ADDR_EXPR:
2761 case FDESC_EXPR:
2763 struct addr_const value;
2765 decode_addr_const (exp, &value);
2766 switch (GET_CODE (value.base))
2768 case SYMBOL_REF:
2769 /* Don't hash the address of the SYMBOL_REF;
2770 only use the offset and the symbol name. */
2771 hi = value.offset;
2772 p = XSTR (value.base, 0);
2773 for (i = 0; p[i] != 0; i++)
2774 hi = ((hi * 613) + (unsigned) (p[i]));
2775 break;
2777 case LABEL_REF:
2778 hi = value.offset + CODE_LABEL_NUMBER (XEXP (value.base, 0)) * 13;
2779 break;
2781 default:
2782 gcc_unreachable ();
2785 return hi;
2787 case PLUS_EXPR:
2788 case POINTER_PLUS_EXPR:
2789 case MINUS_EXPR:
2790 return (const_hash_1 (TREE_OPERAND (exp, 0)) * 9
2791 + const_hash_1 (TREE_OPERAND (exp, 1)));
2793 CASE_CONVERT:
2794 return const_hash_1 (TREE_OPERAND (exp, 0)) * 7 + 2;
2796 default:
2797 /* A language specific constant. Just hash the code. */
2798 return code;
2801 /* Compute hashing function. */
2802 hi = len;
2803 for (i = 0; i < len; i++)
2804 hi = ((hi * 613) + (unsigned) (p[i]));
2806 return hi;
2809 /* Wrapper of compare_constant, for the htab interface. */
2810 static int
2811 const_desc_eq (const void *p1, const void *p2)
2813 const struct constant_descriptor_tree *const c1
2814 = (const struct constant_descriptor_tree *) p1;
2815 const struct constant_descriptor_tree *const c2
2816 = (const struct constant_descriptor_tree *) p2;
2817 if (c1->hash != c2->hash)
2818 return 0;
2819 return compare_constant (c1->value, c2->value);
2822 /* Compare t1 and t2, and return 1 only if they are known to result in
2823 the same bit pattern on output. */
2825 static int
2826 compare_constant (const tree t1, const tree t2)
2828 enum tree_code typecode;
2830 if (t1 == NULL_TREE)
2831 return t2 == NULL_TREE;
2832 if (t2 == NULL_TREE)
2833 return 0;
2835 if (TREE_CODE (t1) != TREE_CODE (t2))
2836 return 0;
2838 switch (TREE_CODE (t1))
2840 case INTEGER_CST:
2841 /* Integer constants are the same only if the same width of type. */
2842 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
2843 return 0;
2844 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
2845 return 0;
2846 return tree_int_cst_equal (t1, t2);
2848 case REAL_CST:
2849 /* Real constants are the same only if the same width of type. */
2850 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
2851 return 0;
2853 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
2855 case FIXED_CST:
2856 /* Fixed constants are the same only if the same width of type. */
2857 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
2858 return 0;
2860 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
2862 case STRING_CST:
2863 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
2864 return 0;
2866 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
2867 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
2868 TREE_STRING_LENGTH (t1)));
2870 case COMPLEX_CST:
2871 return (compare_constant (TREE_REALPART (t1), TREE_REALPART (t2))
2872 && compare_constant (TREE_IMAGPART (t1), TREE_IMAGPART (t2)));
2874 case VECTOR_CST:
2876 tree link1, link2;
2878 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (t1))
2879 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (t2)))
2880 return 0;
2882 link2 = TREE_VECTOR_CST_ELTS (t2);
2883 for (link1 = TREE_VECTOR_CST_ELTS (t1);
2884 link1;
2885 link1 = TREE_CHAIN (link1))
2887 if (!compare_constant (TREE_VALUE (link1), TREE_VALUE (link2)))
2888 return 0;
2889 link2 = TREE_CHAIN (link2);
2892 return 1;
2895 case CONSTRUCTOR:
2897 VEC(constructor_elt, gc) *v1, *v2;
2898 unsigned HOST_WIDE_INT idx;
2900 typecode = TREE_CODE (TREE_TYPE (t1));
2901 if (typecode != TREE_CODE (TREE_TYPE (t2)))
2902 return 0;
2904 if (typecode == ARRAY_TYPE)
2906 HOST_WIDE_INT size_1 = int_size_in_bytes (TREE_TYPE (t1));
2907 /* For arrays, check that the sizes all match. */
2908 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))
2909 || size_1 == -1
2910 || size_1 != int_size_in_bytes (TREE_TYPE (t2)))
2911 return 0;
2913 else
2915 /* For record and union constructors, require exact type
2916 equality. */
2917 if (TREE_TYPE (t1) != TREE_TYPE (t2))
2918 return 0;
2921 v1 = CONSTRUCTOR_ELTS (t1);
2922 v2 = CONSTRUCTOR_ELTS (t2);
2923 if (VEC_length (constructor_elt, v1)
2924 != VEC_length (constructor_elt, v2))
2925 return 0;
2927 for (idx = 0; idx < VEC_length (constructor_elt, v1); ++idx)
2929 constructor_elt *c1 = VEC_index (constructor_elt, v1, idx);
2930 constructor_elt *c2 = VEC_index (constructor_elt, v2, idx);
2932 /* Check that each value is the same... */
2933 if (!compare_constant (c1->value, c2->value))
2934 return 0;
2935 /* ... and that they apply to the same fields! */
2936 if (typecode == ARRAY_TYPE)
2938 if (!compare_constant (c1->index, c2->index))
2939 return 0;
2941 else
2943 if (c1->index != c2->index)
2944 return 0;
2948 return 1;
2951 case ADDR_EXPR:
2952 case FDESC_EXPR:
2954 struct addr_const value1, value2;
2955 enum rtx_code code;
2956 int ret;
2958 decode_addr_const (t1, &value1);
2959 decode_addr_const (t2, &value2);
2961 if (value1.offset != value2.offset)
2962 return 0;
2964 code = GET_CODE (value1.base);
2965 if (code != GET_CODE (value2.base))
2966 return 0;
2968 switch (code)
2970 case SYMBOL_REF:
2971 ret = (strcmp (XSTR (value1.base, 0), XSTR (value2.base, 0)) == 0);
2972 break;
2974 case LABEL_REF:
2975 ret = (CODE_LABEL_NUMBER (XEXP (value1.base, 0))
2976 == CODE_LABEL_NUMBER (XEXP (value2.base, 0)));
2977 break;
2979 default:
2980 gcc_unreachable ();
2982 return ret;
2985 case PLUS_EXPR:
2986 case POINTER_PLUS_EXPR:
2987 case MINUS_EXPR:
2988 case RANGE_EXPR:
2989 return (compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
2990 && compare_constant(TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
2992 CASE_CONVERT:
2993 case VIEW_CONVERT_EXPR:
2994 return compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2996 default:
2997 return 0;
3000 gcc_unreachable ();
3003 /* Make a copy of the whole tree structure for a constant. This
3004 handles the same types of nodes that compare_constant handles. */
3006 static tree
3007 copy_constant (tree exp)
3009 switch (TREE_CODE (exp))
3011 case ADDR_EXPR:
3012 /* For ADDR_EXPR, we do not want to copy the decl whose address
3013 is requested. We do want to copy constants though. */
3014 if (CONSTANT_CLASS_P (TREE_OPERAND (exp, 0)))
3015 return build1 (TREE_CODE (exp), TREE_TYPE (exp),
3016 copy_constant (TREE_OPERAND (exp, 0)));
3017 else
3018 return copy_node (exp);
3020 case INTEGER_CST:
3021 case REAL_CST:
3022 case FIXED_CST:
3023 case STRING_CST:
3024 return copy_node (exp);
3026 case COMPLEX_CST:
3027 return build_complex (TREE_TYPE (exp),
3028 copy_constant (TREE_REALPART (exp)),
3029 copy_constant (TREE_IMAGPART (exp)));
3031 case PLUS_EXPR:
3032 case POINTER_PLUS_EXPR:
3033 case MINUS_EXPR:
3034 return build2 (TREE_CODE (exp), TREE_TYPE (exp),
3035 copy_constant (TREE_OPERAND (exp, 0)),
3036 copy_constant (TREE_OPERAND (exp, 1)));
3038 CASE_CONVERT:
3039 case VIEW_CONVERT_EXPR:
3040 return build1 (TREE_CODE (exp), TREE_TYPE (exp),
3041 copy_constant (TREE_OPERAND (exp, 0)));
3043 case VECTOR_CST:
3044 return build_vector (TREE_TYPE (exp),
3045 copy_list (TREE_VECTOR_CST_ELTS (exp)));
3047 case CONSTRUCTOR:
3049 tree copy = copy_node (exp);
3050 VEC(constructor_elt, gc) *v;
3051 unsigned HOST_WIDE_INT idx;
3052 tree purpose, value;
3054 v = VEC_alloc(constructor_elt, gc, VEC_length(constructor_elt,
3055 CONSTRUCTOR_ELTS (exp)));
3056 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, purpose, value)
3058 constructor_elt *ce = VEC_quick_push (constructor_elt, v, NULL);
3059 ce->index = purpose;
3060 ce->value = copy_constant (value);
3062 CONSTRUCTOR_ELTS (copy) = v;
3063 return copy;
3066 default:
3067 gcc_unreachable ();
3071 /* Return the section into which constant EXP should be placed. */
3073 static section *
3074 get_constant_section (tree exp, unsigned int align)
3076 return targetm.asm_out.select_section (exp,
3077 compute_reloc_for_constant (exp),
3078 align);
3081 /* Return the size of constant EXP in bytes. */
3083 static HOST_WIDE_INT
3084 get_constant_size (tree exp)
3086 HOST_WIDE_INT size;
3088 size = int_size_in_bytes (TREE_TYPE (exp));
3089 if (TREE_CODE (exp) == STRING_CST)
3090 size = MAX (TREE_STRING_LENGTH (exp), size);
3091 return size;
3094 /* Subroutine of output_constant_def:
3095 No constant equal to EXP is known to have been output.
3096 Make a constant descriptor to enter EXP in the hash table.
3097 Assign the label number and construct RTL to refer to the
3098 constant's location in memory.
3099 Caller is responsible for updating the hash table. */
3101 static struct constant_descriptor_tree *
3102 build_constant_desc (tree exp)
3104 struct constant_descriptor_tree *desc;
3105 rtx symbol, rtl;
3106 char label[256];
3107 int labelno;
3108 tree decl;
3110 desc = ggc_alloc_constant_descriptor_tree ();
3111 desc->value = copy_constant (exp);
3113 /* Propagate marked-ness to copied constant. */
3114 if (flag_mudflap && mf_marked_p (exp))
3115 mf_mark (desc->value);
3117 /* Create a string containing the label name, in LABEL. */
3118 labelno = const_labelno++;
3119 ASM_GENERATE_INTERNAL_LABEL (label, "LC", labelno);
3121 /* Construct the VAR_DECL associated with the constant. */
3122 decl = build_decl (UNKNOWN_LOCATION, VAR_DECL, get_identifier (label),
3123 TREE_TYPE (exp));
3124 DECL_ARTIFICIAL (decl) = 1;
3125 DECL_IGNORED_P (decl) = 1;
3126 TREE_READONLY (decl) = 1;
3127 TREE_STATIC (decl) = 1;
3128 TREE_ADDRESSABLE (decl) = 1;
3129 /* We don't set the RTL yet as this would cause varpool to assume that the
3130 variable is referenced. Moreover, it would just be dropped in LTO mode.
3131 Instead we set the flag that will be recognized in make_decl_rtl. */
3132 DECL_IN_CONSTANT_POOL (decl) = 1;
3133 DECL_INITIAL (decl) = desc->value;
3134 /* ??? CONSTANT_ALIGNMENT hasn't been updated for vector types on most
3135 architectures so use DATA_ALIGNMENT as well, except for strings. */
3136 if (TREE_CODE (exp) == STRING_CST)
3138 #ifdef CONSTANT_ALIGNMENT
3139 DECL_ALIGN (decl) = CONSTANT_ALIGNMENT (exp, DECL_ALIGN (decl));
3140 #endif
3142 else
3143 align_variable (decl, 0);
3145 /* Now construct the SYMBOL_REF and the MEM. */
3146 if (use_object_blocks_p ())
3148 section *sect = get_constant_section (exp, DECL_ALIGN (decl));
3149 symbol = create_block_symbol (ggc_strdup (label),
3150 get_block_for_section (sect), -1);
3152 else
3153 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3154 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3155 SET_SYMBOL_REF_DECL (symbol, decl);
3156 TREE_CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3158 rtl = gen_const_mem (TYPE_MODE (TREE_TYPE (exp)), symbol);
3159 set_mem_attributes (rtl, exp, 1);
3160 set_mem_alias_set (rtl, 0);
3161 set_mem_alias_set (rtl, const_alias_set);
3163 /* We cannot share RTX'es in pool entries.
3164 Mark this piece of RTL as required for unsharing. */
3165 RTX_FLAG (rtl, used) = 1;
3167 /* Set flags or add text to the name to record information, such as
3168 that it is a local symbol. If the name is changed, the macro
3169 ASM_OUTPUT_LABELREF will have to know how to strip this
3170 information. This call might invalidate our local variable
3171 SYMBOL; we can't use it afterward. */
3172 targetm.encode_section_info (exp, rtl, true);
3174 desc->rtl = rtl;
3176 return desc;
3179 /* Return an rtx representing a reference to constant data in memory
3180 for the constant expression EXP.
3182 If assembler code for such a constant has already been output,
3183 return an rtx to refer to it.
3184 Otherwise, output such a constant in memory
3185 and generate an rtx for it.
3187 If DEFER is nonzero, this constant can be deferred and output only
3188 if referenced in the function after all optimizations.
3190 `const_desc_table' records which constants already have label strings. */
3193 output_constant_def (tree exp, int defer)
3195 struct constant_descriptor_tree *desc;
3196 struct constant_descriptor_tree key;
3197 void **loc;
3199 /* Look up EXP in the table of constant descriptors. If we didn't find
3200 it, create a new one. */
3201 key.value = exp;
3202 key.hash = const_hash_1 (exp);
3203 loc = htab_find_slot_with_hash (const_desc_htab, &key, key.hash, INSERT);
3205 desc = (struct constant_descriptor_tree *) *loc;
3206 if (desc == 0)
3208 desc = build_constant_desc (exp);
3209 desc->hash = key.hash;
3210 *loc = desc;
3213 maybe_output_constant_def_contents (desc, defer);
3214 return desc->rtl;
3217 /* Subroutine of output_constant_def: Decide whether or not we need to
3218 output the constant DESC now, and if so, do it. */
3219 static void
3220 maybe_output_constant_def_contents (struct constant_descriptor_tree *desc,
3221 int defer)
3223 rtx symbol = XEXP (desc->rtl, 0);
3224 tree exp = desc->value;
3226 if (flag_syntax_only)
3227 return;
3229 if (TREE_ASM_WRITTEN (exp))
3230 /* Already output; don't do it again. */
3231 return;
3233 /* We can always defer constants as long as the context allows
3234 doing so. */
3235 if (defer)
3237 /* Increment n_deferred_constants if it exists. It needs to be at
3238 least as large as the number of constants actually referred to
3239 by the function. If it's too small we'll stop looking too early
3240 and fail to emit constants; if it's too large we'll only look
3241 through the entire function when we could have stopped earlier. */
3242 if (cfun)
3243 n_deferred_constants++;
3244 return;
3247 output_constant_def_contents (symbol);
3250 /* Subroutine of output_constant_def_contents. Output the definition
3251 of constant EXP, which is pointed to by label LABEL. ALIGN is the
3252 constant's alignment in bits. */
3254 static void
3255 assemble_constant_contents (tree exp, const char *label, unsigned int align)
3257 HOST_WIDE_INT size;
3259 size = get_constant_size (exp);
3261 /* Do any machine/system dependent processing of the constant. */
3262 targetm.asm_out.declare_constant_name (asm_out_file, label, exp, size);
3264 /* Output the value of EXP. */
3265 output_constant (exp, size, align);
3268 /* We must output the constant data referred to by SYMBOL; do so. */
3270 static void
3271 output_constant_def_contents (rtx symbol)
3273 tree decl = SYMBOL_REF_DECL (symbol);
3274 tree exp = DECL_INITIAL (decl);
3275 unsigned int align;
3277 /* Make sure any other constants whose addresses appear in EXP
3278 are assigned label numbers. */
3279 output_addressed_constants (exp);
3281 /* We are no longer deferring this constant. */
3282 TREE_ASM_WRITTEN (decl) = TREE_ASM_WRITTEN (exp) = 1;
3284 /* If the constant is part of an object block, make sure that the
3285 decl has been positioned within its block, but do not write out
3286 its definition yet. output_object_blocks will do that later. */
3287 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
3288 place_block_symbol (symbol);
3289 else
3291 align = DECL_ALIGN (decl);
3292 switch_to_section (get_constant_section (exp, align));
3293 if (align > BITS_PER_UNIT)
3294 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
3295 assemble_constant_contents (exp, XSTR (symbol, 0), align);
3297 if (flag_mudflap)
3298 mudflap_enqueue_constant (exp);
3301 /* Look up EXP in the table of constant descriptors. Return the rtl
3302 if it has been emitted, else null. */
3305 lookup_constant_def (tree exp)
3307 struct constant_descriptor_tree *desc;
3308 struct constant_descriptor_tree key;
3310 key.value = exp;
3311 key.hash = const_hash_1 (exp);
3312 desc = (struct constant_descriptor_tree *)
3313 htab_find_with_hash (const_desc_htab, &key, key.hash);
3315 return (desc ? desc->rtl : NULL_RTX);
3318 /* Return a tree representing a reference to constant data in memory
3319 for the constant expression EXP.
3321 This is the counterpart of output_constant_def at the Tree level. */
3323 tree
3324 tree_output_constant_def (tree exp)
3326 struct constant_descriptor_tree *desc, key;
3327 void **loc;
3328 tree decl;
3330 /* Look up EXP in the table of constant descriptors. If we didn't find
3331 it, create a new one. */
3332 key.value = exp;
3333 key.hash = const_hash_1 (exp);
3334 loc = htab_find_slot_with_hash (const_desc_htab, &key, key.hash, INSERT);
3336 desc = (struct constant_descriptor_tree *) *loc;
3337 if (desc == 0)
3339 desc = build_constant_desc (exp);
3340 desc->hash = key.hash;
3341 *loc = desc;
3344 decl = SYMBOL_REF_DECL (XEXP (desc->rtl, 0));
3345 varpool_finalize_decl (decl);
3346 return decl;
3349 /* Used in the hash tables to avoid outputting the same constant
3350 twice. Unlike 'struct constant_descriptor_tree', RTX constants
3351 are output once per function, not once per file. */
3352 /* ??? Only a few targets need per-function constant pools. Most
3353 can use one per-file pool. Should add a targetm bit to tell the
3354 difference. */
3356 struct GTY(()) rtx_constant_pool {
3357 /* Pointers to first and last constant in pool, as ordered by offset. */
3358 struct constant_descriptor_rtx *first;
3359 struct constant_descriptor_rtx *last;
3361 /* Hash facility for making memory-constants from constant rtl-expressions.
3362 It is used on RISC machines where immediate integer arguments and
3363 constant addresses are restricted so that such constants must be stored
3364 in memory. */
3365 htab_t GTY((param_is (struct constant_descriptor_rtx))) const_rtx_htab;
3367 /* Current offset in constant pool (does not include any
3368 machine-specific header). */
3369 HOST_WIDE_INT offset;
3372 struct GTY((chain_next ("%h.next"))) constant_descriptor_rtx {
3373 struct constant_descriptor_rtx *next;
3374 rtx mem;
3375 rtx sym;
3376 rtx constant;
3377 HOST_WIDE_INT offset;
3378 hashval_t hash;
3379 enum machine_mode mode;
3380 unsigned int align;
3381 int labelno;
3382 int mark;
3385 /* Hash and compare functions for const_rtx_htab. */
3387 static hashval_t
3388 const_desc_rtx_hash (const void *ptr)
3390 const struct constant_descriptor_rtx *const desc
3391 = (const struct constant_descriptor_rtx *) ptr;
3392 return desc->hash;
3395 static int
3396 const_desc_rtx_eq (const void *a, const void *b)
3398 const struct constant_descriptor_rtx *const x
3399 = (const struct constant_descriptor_rtx *) a;
3400 const struct constant_descriptor_rtx *const y
3401 = (const struct constant_descriptor_rtx *) b;
3403 if (x->mode != y->mode)
3404 return 0;
3405 return rtx_equal_p (x->constant, y->constant);
3408 /* This is the worker function for const_rtx_hash, called via for_each_rtx. */
3410 static int
3411 const_rtx_hash_1 (rtx *xp, void *data)
3413 unsigned HOST_WIDE_INT hwi;
3414 enum machine_mode mode;
3415 enum rtx_code code;
3416 hashval_t h, *hp;
3417 rtx x;
3419 x = *xp;
3420 code = GET_CODE (x);
3421 mode = GET_MODE (x);
3422 h = (hashval_t) code * 1048573 + mode;
3424 switch (code)
3426 case CONST_INT:
3427 hwi = INTVAL (x);
3428 fold_hwi:
3430 int shift = sizeof (hashval_t) * CHAR_BIT;
3431 const int n = sizeof (HOST_WIDE_INT) / sizeof (hashval_t);
3432 int i;
3434 h ^= (hashval_t) hwi;
3435 for (i = 1; i < n; ++i)
3437 hwi >>= shift;
3438 h ^= (hashval_t) hwi;
3441 break;
3443 case CONST_DOUBLE:
3444 if (mode == VOIDmode)
3446 hwi = CONST_DOUBLE_LOW (x) ^ CONST_DOUBLE_HIGH (x);
3447 goto fold_hwi;
3449 else
3450 h ^= real_hash (CONST_DOUBLE_REAL_VALUE (x));
3451 break;
3453 case CONST_FIXED:
3454 h ^= fixed_hash (CONST_FIXED_VALUE (x));
3455 break;
3457 case CONST_VECTOR:
3459 int i;
3460 for (i = XVECLEN (x, 0); i-- > 0; )
3461 h = h * 251 + const_rtx_hash_1 (&XVECEXP (x, 0, i), data);
3463 break;
3465 case SYMBOL_REF:
3466 h ^= htab_hash_string (XSTR (x, 0));
3467 break;
3469 case LABEL_REF:
3470 h = h * 251 + CODE_LABEL_NUMBER (XEXP (x, 0));
3471 break;
3473 case UNSPEC:
3474 case UNSPEC_VOLATILE:
3475 h = h * 251 + XINT (x, 1);
3476 break;
3478 default:
3479 break;
3482 hp = (hashval_t *) data;
3483 *hp = *hp * 509 + h;
3484 return 0;
3487 /* Compute a hash value for X, which should be a constant. */
3489 static hashval_t
3490 const_rtx_hash (rtx x)
3492 hashval_t h = 0;
3493 for_each_rtx (&x, const_rtx_hash_1, &h);
3494 return h;
3498 /* Create and return a new rtx constant pool. */
3500 static struct rtx_constant_pool *
3501 create_constant_pool (void)
3503 struct rtx_constant_pool *pool;
3505 pool = ggc_alloc_rtx_constant_pool ();
3506 pool->const_rtx_htab = htab_create_ggc (31, const_desc_rtx_hash,
3507 const_desc_rtx_eq, NULL);
3508 pool->first = NULL;
3509 pool->last = NULL;
3510 pool->offset = 0;
3511 return pool;
3514 /* Initialize constant pool hashing for a new function. */
3516 void
3517 init_varasm_status (void)
3519 crtl->varasm.pool = create_constant_pool ();
3520 crtl->varasm.deferred_constants = 0;
3523 /* Given a MINUS expression, simplify it if both sides
3524 include the same symbol. */
3527 simplify_subtraction (rtx x)
3529 rtx r = simplify_rtx (x);
3530 return r ? r : x;
3533 /* Given a constant rtx X, make (or find) a memory constant for its value
3534 and return a MEM rtx to refer to it in memory. */
3537 force_const_mem (enum machine_mode mode, rtx x)
3539 struct constant_descriptor_rtx *desc, tmp;
3540 struct rtx_constant_pool *pool;
3541 char label[256];
3542 rtx def, symbol;
3543 hashval_t hash;
3544 unsigned int align;
3545 void **slot;
3547 /* If we're not allowed to drop X into the constant pool, don't. */
3548 if (targetm.cannot_force_const_mem (mode, x))
3549 return NULL_RTX;
3551 /* Record that this function has used a constant pool entry. */
3552 crtl->uses_const_pool = 1;
3554 /* Decide which pool to use. */
3555 pool = (targetm.use_blocks_for_constant_p (mode, x)
3556 ? shared_constant_pool
3557 : crtl->varasm.pool);
3559 /* Lookup the value in the hashtable. */
3560 tmp.constant = x;
3561 tmp.mode = mode;
3562 hash = const_rtx_hash (x);
3563 slot = htab_find_slot_with_hash (pool->const_rtx_htab, &tmp, hash, INSERT);
3564 desc = (struct constant_descriptor_rtx *) *slot;
3566 /* If the constant was already present, return its memory. */
3567 if (desc)
3568 return copy_rtx (desc->mem);
3570 /* Otherwise, create a new descriptor. */
3571 desc = ggc_alloc_constant_descriptor_rtx ();
3572 *slot = desc;
3574 /* Align the location counter as required by EXP's data type. */
3575 align = GET_MODE_ALIGNMENT (mode == VOIDmode ? word_mode : mode);
3576 #ifdef CONSTANT_ALIGNMENT
3578 tree type = lang_hooks.types.type_for_mode (mode, 0);
3579 if (type != NULL_TREE)
3580 align = CONSTANT_ALIGNMENT (make_tree (type, x), align);
3582 #endif
3584 pool->offset += (align / BITS_PER_UNIT) - 1;
3585 pool->offset &= ~ ((align / BITS_PER_UNIT) - 1);
3587 desc->next = NULL;
3588 desc->constant = copy_rtx (tmp.constant);
3589 desc->offset = pool->offset;
3590 desc->hash = hash;
3591 desc->mode = mode;
3592 desc->align = align;
3593 desc->labelno = const_labelno;
3594 desc->mark = 0;
3596 pool->offset += GET_MODE_SIZE (mode);
3597 if (pool->last)
3598 pool->last->next = desc;
3599 else
3600 pool->first = pool->last = desc;
3601 pool->last = desc;
3603 /* Create a string containing the label name, in LABEL. */
3604 ASM_GENERATE_INTERNAL_LABEL (label, "LC", const_labelno);
3605 ++const_labelno;
3607 /* Construct the SYMBOL_REF. Make sure to mark it as belonging to
3608 the constants pool. */
3609 if (use_object_blocks_p () && targetm.use_blocks_for_constant_p (mode, x))
3611 section *sect = targetm.asm_out.select_rtx_section (mode, x, align);
3612 symbol = create_block_symbol (ggc_strdup (label),
3613 get_block_for_section (sect), -1);
3615 else
3616 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3617 desc->sym = symbol;
3618 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3619 CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3620 SET_SYMBOL_REF_CONSTANT (symbol, desc);
3622 /* Construct the MEM. */
3623 desc->mem = def = gen_const_mem (mode, symbol);
3624 set_mem_attributes (def, lang_hooks.types.type_for_mode (mode, 0), 1);
3625 set_mem_align (def, align);
3627 /* If we're dropping a label to the constant pool, make sure we
3628 don't delete it. */
3629 if (GET_CODE (x) == LABEL_REF)
3630 LABEL_PRESERVE_P (XEXP (x, 0)) = 1;
3632 return copy_rtx (def);
3635 /* Given a constant pool SYMBOL_REF, return the corresponding constant. */
3638 get_pool_constant (rtx addr)
3640 return SYMBOL_REF_CONSTANT (addr)->constant;
3643 /* Given a constant pool SYMBOL_REF, return the corresponding constant
3644 and whether it has been output or not. */
3647 get_pool_constant_mark (rtx addr, bool *pmarked)
3649 struct constant_descriptor_rtx *desc;
3651 desc = SYMBOL_REF_CONSTANT (addr);
3652 *pmarked = (desc->mark != 0);
3653 return desc->constant;
3656 /* Similar, return the mode. */
3658 enum machine_mode
3659 get_pool_mode (const_rtx addr)
3661 return SYMBOL_REF_CONSTANT (addr)->mode;
3664 /* Return the size of the constant pool. */
3667 get_pool_size (void)
3669 return crtl->varasm.pool->offset;
3672 /* Worker function for output_constant_pool_1. Emit assembly for X
3673 in MODE with known alignment ALIGN. */
3675 static void
3676 output_constant_pool_2 (enum machine_mode mode, rtx x, unsigned int align)
3678 switch (GET_MODE_CLASS (mode))
3680 case MODE_FLOAT:
3681 case MODE_DECIMAL_FLOAT:
3683 REAL_VALUE_TYPE r;
3685 gcc_assert (GET_CODE (x) == CONST_DOUBLE);
3686 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
3687 assemble_real (r, mode, align);
3688 break;
3691 case MODE_INT:
3692 case MODE_PARTIAL_INT:
3693 case MODE_FRACT:
3694 case MODE_UFRACT:
3695 case MODE_ACCUM:
3696 case MODE_UACCUM:
3697 assemble_integer (x, GET_MODE_SIZE (mode), align, 1);
3698 break;
3700 case MODE_VECTOR_FLOAT:
3701 case MODE_VECTOR_INT:
3702 case MODE_VECTOR_FRACT:
3703 case MODE_VECTOR_UFRACT:
3704 case MODE_VECTOR_ACCUM:
3705 case MODE_VECTOR_UACCUM:
3707 int i, units;
3708 enum machine_mode submode = GET_MODE_INNER (mode);
3709 unsigned int subalign = MIN (align, GET_MODE_BITSIZE (submode));
3711 gcc_assert (GET_CODE (x) == CONST_VECTOR);
3712 units = CONST_VECTOR_NUNITS (x);
3714 for (i = 0; i < units; i++)
3716 rtx elt = CONST_VECTOR_ELT (x, i);
3717 output_constant_pool_2 (submode, elt, i ? subalign : align);
3720 break;
3722 default:
3723 gcc_unreachable ();
3727 /* Worker function for output_constant_pool. Emit constant DESC,
3728 giving it ALIGN bits of alignment. */
3730 static void
3731 output_constant_pool_1 (struct constant_descriptor_rtx *desc,
3732 unsigned int align)
3734 rtx x, tmp;
3736 x = desc->constant;
3738 /* See if X is a LABEL_REF (or a CONST referring to a LABEL_REF)
3739 whose CODE_LABEL has been deleted. This can occur if a jump table
3740 is eliminated by optimization. If so, write a constant of zero
3741 instead. Note that this can also happen by turning the
3742 CODE_LABEL into a NOTE. */
3743 /* ??? This seems completely and utterly wrong. Certainly it's
3744 not true for NOTE_INSN_DELETED_LABEL, but I disbelieve proper
3745 functioning even with INSN_DELETED_P and friends. */
3747 tmp = x;
3748 switch (GET_CODE (tmp))
3750 case CONST:
3751 if (GET_CODE (XEXP (tmp, 0)) != PLUS
3752 || GET_CODE (XEXP (XEXP (tmp, 0), 0)) != LABEL_REF)
3753 break;
3754 tmp = XEXP (XEXP (tmp, 0), 0);
3755 /* FALLTHRU */
3757 case LABEL_REF:
3758 tmp = XEXP (tmp, 0);
3759 gcc_assert (!INSN_DELETED_P (tmp));
3760 gcc_assert (!NOTE_P (tmp)
3761 || NOTE_KIND (tmp) != NOTE_INSN_DELETED);
3762 break;
3764 default:
3765 break;
3768 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3769 ASM_OUTPUT_SPECIAL_POOL_ENTRY (asm_out_file, x, desc->mode,
3770 align, desc->labelno, done);
3771 #endif
3773 assemble_align (align);
3775 /* Output the label. */
3776 targetm.asm_out.internal_label (asm_out_file, "LC", desc->labelno);
3778 /* Output the data. */
3779 output_constant_pool_2 (desc->mode, x, align);
3781 /* Make sure all constants in SECTION_MERGE and not SECTION_STRINGS
3782 sections have proper size. */
3783 if (align > GET_MODE_BITSIZE (desc->mode)
3784 && in_section
3785 && (in_section->common.flags & SECTION_MERGE))
3786 assemble_align (align);
3788 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3789 done:
3790 #endif
3791 return;
3794 /* Given a SYMBOL_REF CURRENT_RTX, mark it and all constants it refers
3795 to as used. Emit referenced deferred strings. This function can
3796 be used with for_each_rtx to mark all SYMBOL_REFs in an rtx. */
3798 static int
3799 mark_constant (rtx *current_rtx, void *data ATTRIBUTE_UNUSED)
3801 rtx x = *current_rtx;
3803 if (x == NULL_RTX || GET_CODE (x) != SYMBOL_REF)
3804 return 0;
3806 if (CONSTANT_POOL_ADDRESS_P (x))
3808 struct constant_descriptor_rtx *desc = SYMBOL_REF_CONSTANT (x);
3809 if (desc->mark == 0)
3811 desc->mark = 1;
3812 for_each_rtx (&desc->constant, mark_constant, NULL);
3815 else if (TREE_CONSTANT_POOL_ADDRESS_P (x))
3817 tree decl = SYMBOL_REF_DECL (x);
3818 if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
3820 n_deferred_constants--;
3821 output_constant_def_contents (x);
3825 return -1;
3828 /* Look through appropriate parts of INSN, marking all entries in the
3829 constant pool which are actually being used. Entries that are only
3830 referenced by other constants are also marked as used. Emit
3831 deferred strings that are used. */
3833 static void
3834 mark_constants (rtx insn)
3836 if (!INSN_P (insn))
3837 return;
3839 /* Insns may appear inside a SEQUENCE. Only check the patterns of
3840 insns, not any notes that may be attached. We don't want to mark
3841 a constant just because it happens to appear in a REG_EQUIV note. */
3842 if (GET_CODE (PATTERN (insn)) == SEQUENCE)
3844 rtx seq = PATTERN (insn);
3845 int i, n = XVECLEN (seq, 0);
3846 for (i = 0; i < n; ++i)
3848 rtx subinsn = XVECEXP (seq, 0, i);
3849 if (INSN_P (subinsn))
3850 for_each_rtx (&PATTERN (subinsn), mark_constant, NULL);
3853 else
3854 for_each_rtx (&PATTERN (insn), mark_constant, NULL);
3857 /* Look through the instructions for this function, and mark all the
3858 entries in POOL which are actually being used. Emit deferred constants
3859 which have indeed been used. */
3861 static void
3862 mark_constant_pool (void)
3864 rtx insn, link;
3866 if (!crtl->uses_const_pool && n_deferred_constants == 0)
3867 return;
3869 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3870 mark_constants (insn);
3872 for (link = crtl->epilogue_delay_list;
3873 link;
3874 link = XEXP (link, 1))
3875 mark_constants (XEXP (link, 0));
3878 /* Write all the constants in POOL. */
3880 static void
3881 output_constant_pool_contents (struct rtx_constant_pool *pool)
3883 struct constant_descriptor_rtx *desc;
3885 for (desc = pool->first; desc ; desc = desc->next)
3886 if (desc->mark)
3888 /* If the constant is part of an object_block, make sure that
3889 the constant has been positioned within its block, but do not
3890 write out its definition yet. output_object_blocks will do
3891 that later. */
3892 if (SYMBOL_REF_HAS_BLOCK_INFO_P (desc->sym)
3893 && SYMBOL_REF_BLOCK (desc->sym))
3894 place_block_symbol (desc->sym);
3895 else
3897 switch_to_section (targetm.asm_out.select_rtx_section
3898 (desc->mode, desc->constant, desc->align));
3899 output_constant_pool_1 (desc, desc->align);
3904 /* Mark all constants that are used in the current function, then write
3905 out the function's private constant pool. */
3907 static void
3908 output_constant_pool (const char *fnname ATTRIBUTE_UNUSED,
3909 tree fndecl ATTRIBUTE_UNUSED)
3911 struct rtx_constant_pool *pool = crtl->varasm.pool;
3913 /* It is possible for gcc to call force_const_mem and then to later
3914 discard the instructions which refer to the constant. In such a
3915 case we do not need to output the constant. */
3916 mark_constant_pool ();
3918 #ifdef ASM_OUTPUT_POOL_PROLOGUE
3919 ASM_OUTPUT_POOL_PROLOGUE (asm_out_file, fnname, fndecl, pool->offset);
3920 #endif
3922 output_constant_pool_contents (pool);
3924 #ifdef ASM_OUTPUT_POOL_EPILOGUE
3925 ASM_OUTPUT_POOL_EPILOGUE (asm_out_file, fnname, fndecl, pool->offset);
3926 #endif
3929 /* Write the contents of the shared constant pool. */
3931 void
3932 output_shared_constant_pool (void)
3934 output_constant_pool_contents (shared_constant_pool);
3937 /* Determine what kind of relocations EXP may need. */
3940 compute_reloc_for_constant (tree exp)
3942 int reloc = 0, reloc2;
3943 tree tem;
3945 switch (TREE_CODE (exp))
3947 case ADDR_EXPR:
3948 case FDESC_EXPR:
3949 /* Go inside any operations that get_inner_reference can handle and see
3950 if what's inside is a constant: no need to do anything here for
3951 addresses of variables or functions. */
3952 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
3953 tem = TREE_OPERAND (tem, 0))
3956 if (TREE_CODE (tem) == MEM_REF
3957 && TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR)
3959 reloc = compute_reloc_for_constant (TREE_OPERAND (tem, 0));
3960 break;
3963 if (TREE_PUBLIC (tem))
3964 reloc |= 2;
3965 else
3966 reloc |= 1;
3967 break;
3969 case PLUS_EXPR:
3970 case POINTER_PLUS_EXPR:
3971 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3972 reloc |= compute_reloc_for_constant (TREE_OPERAND (exp, 1));
3973 break;
3975 case MINUS_EXPR:
3976 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3977 reloc2 = compute_reloc_for_constant (TREE_OPERAND (exp, 1));
3978 /* The difference of two local labels is computable at link time. */
3979 if (reloc == 1 && reloc2 == 1)
3980 reloc = 0;
3981 else
3982 reloc |= reloc2;
3983 break;
3985 CASE_CONVERT:
3986 case VIEW_CONVERT_EXPR:
3987 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3988 break;
3990 case CONSTRUCTOR:
3992 unsigned HOST_WIDE_INT idx;
3993 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
3994 if (tem != 0)
3995 reloc |= compute_reloc_for_constant (tem);
3997 break;
3999 default:
4000 break;
4002 return reloc;
4005 /* Find all the constants whose addresses are referenced inside of EXP,
4006 and make sure assembler code with a label has been output for each one.
4007 Indicate whether an ADDR_EXPR has been encountered. */
4009 static void
4010 output_addressed_constants (tree exp)
4012 tree tem;
4014 switch (TREE_CODE (exp))
4016 case ADDR_EXPR:
4017 case FDESC_EXPR:
4018 /* Go inside any operations that get_inner_reference can handle and see
4019 if what's inside is a constant: no need to do anything here for
4020 addresses of variables or functions. */
4021 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
4022 tem = TREE_OPERAND (tem, 0))
4025 /* If we have an initialized CONST_DECL, retrieve the initializer. */
4026 if (TREE_CODE (tem) == CONST_DECL && DECL_INITIAL (tem))
4027 tem = DECL_INITIAL (tem);
4029 if (CONSTANT_CLASS_P (tem) || TREE_CODE (tem) == CONSTRUCTOR)
4030 output_constant_def (tem, 0);
4032 if (TREE_CODE (tem) == MEM_REF)
4033 output_addressed_constants (TREE_OPERAND (tem, 0));
4034 break;
4036 case PLUS_EXPR:
4037 case POINTER_PLUS_EXPR:
4038 case MINUS_EXPR:
4039 output_addressed_constants (TREE_OPERAND (exp, 1));
4040 /* Fall through. */
4042 CASE_CONVERT:
4043 case VIEW_CONVERT_EXPR:
4044 output_addressed_constants (TREE_OPERAND (exp, 0));
4045 break;
4047 case CONSTRUCTOR:
4049 unsigned HOST_WIDE_INT idx;
4050 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
4051 if (tem != 0)
4052 output_addressed_constants (tem);
4054 break;
4056 default:
4057 break;
4061 /* Whether a constructor CTOR is a valid static constant initializer if all
4062 its elements are. This used to be internal to initializer_constant_valid_p
4063 and has been exposed to let other functions like categorize_ctor_elements
4064 evaluate the property while walking a constructor for other purposes. */
4066 bool
4067 constructor_static_from_elts_p (const_tree ctor)
4069 return (TREE_CONSTANT (ctor)
4070 && (TREE_CODE (TREE_TYPE (ctor)) == UNION_TYPE
4071 || TREE_CODE (TREE_TYPE (ctor)) == RECORD_TYPE
4072 || TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE));
4075 static tree initializer_constant_valid_p_1 (tree value, tree endtype,
4076 tree *cache);
4078 /* A subroutine of initializer_constant_valid_p. VALUE is a MINUS_EXPR,
4079 PLUS_EXPR or POINTER_PLUS_EXPR. This looks for cases of VALUE
4080 which are valid when ENDTYPE is an integer of any size; in
4081 particular, this does not accept a pointer minus a constant. This
4082 returns null_pointer_node if the VALUE is an absolute constant
4083 which can be used to initialize a static variable. Otherwise it
4084 returns NULL. */
4086 static tree
4087 narrowing_initializer_constant_valid_p (tree value, tree endtype, tree *cache)
4089 tree op0, op1;
4091 if (!INTEGRAL_TYPE_P (endtype))
4092 return NULL_TREE;
4094 op0 = TREE_OPERAND (value, 0);
4095 op1 = TREE_OPERAND (value, 1);
4097 /* Like STRIP_NOPS except allow the operand mode to widen. This
4098 works around a feature of fold that simplifies (int)(p1 - p2) to
4099 ((int)p1 - (int)p2) under the theory that the narrower operation
4100 is cheaper. */
4102 while (CONVERT_EXPR_P (op0)
4103 || TREE_CODE (op0) == NON_LVALUE_EXPR)
4105 tree inner = TREE_OPERAND (op0, 0);
4106 if (inner == error_mark_node
4107 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4108 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))
4109 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
4110 break;
4111 op0 = inner;
4114 while (CONVERT_EXPR_P (op1)
4115 || TREE_CODE (op1) == NON_LVALUE_EXPR)
4117 tree inner = TREE_OPERAND (op1, 0);
4118 if (inner == error_mark_node
4119 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4120 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op1)))
4121 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
4122 break;
4123 op1 = inner;
4126 op0 = initializer_constant_valid_p_1 (op0, endtype, cache);
4127 if (!op0)
4128 return NULL_TREE;
4130 op1 = initializer_constant_valid_p_1 (op1, endtype,
4131 cache ? cache + 2 : NULL);
4132 /* Both initializers must be known. */
4133 if (op1)
4135 if (op0 == op1
4136 && (op0 == null_pointer_node
4137 || TREE_CODE (value) == MINUS_EXPR))
4138 return null_pointer_node;
4140 /* Support differences between labels. */
4141 if (TREE_CODE (op0) == LABEL_DECL
4142 && TREE_CODE (op1) == LABEL_DECL)
4143 return null_pointer_node;
4145 if (TREE_CODE (op0) == STRING_CST
4146 && TREE_CODE (op1) == STRING_CST
4147 && operand_equal_p (op0, op1, 1))
4148 return null_pointer_node;
4151 return NULL_TREE;
4154 /* Helper function of initializer_constant_valid_p.
4155 Return nonzero if VALUE is a valid constant-valued expression
4156 for use in initializing a static variable; one that can be an
4157 element of a "constant" initializer.
4159 Return null_pointer_node if the value is absolute;
4160 if it is relocatable, return the variable that determines the relocation.
4161 We assume that VALUE has been folded as much as possible;
4162 therefore, we do not need to check for such things as
4163 arithmetic-combinations of integers.
4165 Use CACHE (pointer to 2 tree values) for caching if non-NULL. */
4167 static tree
4168 initializer_constant_valid_p_1 (tree value, tree endtype, tree *cache)
4170 tree ret;
4172 switch (TREE_CODE (value))
4174 case CONSTRUCTOR:
4175 if (constructor_static_from_elts_p (value))
4177 unsigned HOST_WIDE_INT idx;
4178 tree elt;
4179 bool absolute = true;
4181 if (cache && cache[0] == value)
4182 return cache[1];
4183 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
4185 tree reloc;
4186 reloc = initializer_constant_valid_p_1 (elt, TREE_TYPE (elt),
4187 NULL);
4188 if (!reloc)
4190 if (cache)
4192 cache[0] = value;
4193 cache[1] = NULL_TREE;
4195 return NULL_TREE;
4197 if (reloc != null_pointer_node)
4198 absolute = false;
4200 /* For a non-absolute relocation, there is no single
4201 variable that can be "the variable that determines the
4202 relocation." */
4203 if (cache)
4205 cache[0] = value;
4206 cache[1] = absolute ? null_pointer_node : error_mark_node;
4208 return absolute ? null_pointer_node : error_mark_node;
4211 return TREE_STATIC (value) ? null_pointer_node : NULL_TREE;
4213 case INTEGER_CST:
4214 case VECTOR_CST:
4215 case REAL_CST:
4216 case FIXED_CST:
4217 case STRING_CST:
4218 case COMPLEX_CST:
4219 return null_pointer_node;
4221 case ADDR_EXPR:
4222 case FDESC_EXPR:
4224 tree op0 = staticp (TREE_OPERAND (value, 0));
4225 if (op0)
4227 /* "&(*a).f" is like unto pointer arithmetic. If "a" turns out
4228 to be a constant, this is old-skool offsetof-like nonsense. */
4229 if (TREE_CODE (op0) == INDIRECT_REF
4230 && TREE_CONSTANT (TREE_OPERAND (op0, 0)))
4231 return null_pointer_node;
4232 /* Taking the address of a nested function involves a trampoline,
4233 unless we don't need or want one. */
4234 if (TREE_CODE (op0) == FUNCTION_DECL
4235 && DECL_STATIC_CHAIN (op0)
4236 && !TREE_NO_TRAMPOLINE (value))
4237 return NULL_TREE;
4238 /* "&{...}" requires a temporary to hold the constructed
4239 object. */
4240 if (TREE_CODE (op0) == CONSTRUCTOR)
4241 return NULL_TREE;
4243 return op0;
4246 case NON_LVALUE_EXPR:
4247 return initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4248 endtype, cache);
4250 case VIEW_CONVERT_EXPR:
4252 tree src = TREE_OPERAND (value, 0);
4253 tree src_type = TREE_TYPE (src);
4254 tree dest_type = TREE_TYPE (value);
4256 /* Allow view-conversions from aggregate to non-aggregate type only
4257 if the bit pattern is fully preserved afterwards; otherwise, the
4258 RTL expander won't be able to apply a subsequent transformation
4259 to the underlying constructor. */
4260 if (AGGREGATE_TYPE_P (src_type) && !AGGREGATE_TYPE_P (dest_type))
4262 if (TYPE_MODE (endtype) == TYPE_MODE (dest_type))
4263 return initializer_constant_valid_p_1 (src, endtype, cache);
4264 else
4265 return NULL_TREE;
4268 /* Allow all other kinds of view-conversion. */
4269 return initializer_constant_valid_p_1 (src, endtype, cache);
4272 CASE_CONVERT:
4274 tree src = TREE_OPERAND (value, 0);
4275 tree src_type = TREE_TYPE (src);
4276 tree dest_type = TREE_TYPE (value);
4278 /* Allow conversions between pointer types, floating-point
4279 types, and offset types. */
4280 if ((POINTER_TYPE_P (dest_type) && POINTER_TYPE_P (src_type))
4281 || (FLOAT_TYPE_P (dest_type) && FLOAT_TYPE_P (src_type))
4282 || (TREE_CODE (dest_type) == OFFSET_TYPE
4283 && TREE_CODE (src_type) == OFFSET_TYPE))
4284 return initializer_constant_valid_p_1 (src, endtype, cache);
4286 /* Allow length-preserving conversions between integer types. */
4287 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type)
4288 && (TYPE_PRECISION (dest_type) == TYPE_PRECISION (src_type)))
4289 return initializer_constant_valid_p_1 (src, endtype, cache);
4291 /* Allow conversions between other integer types only if
4292 explicit value. */
4293 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type))
4295 tree inner = initializer_constant_valid_p_1 (src, endtype, cache);
4296 if (inner == null_pointer_node)
4297 return null_pointer_node;
4298 break;
4301 /* Allow (int) &foo provided int is as wide as a pointer. */
4302 if (INTEGRAL_TYPE_P (dest_type) && POINTER_TYPE_P (src_type)
4303 && (TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type)))
4304 return initializer_constant_valid_p_1 (src, endtype, cache);
4306 /* Likewise conversions from int to pointers, but also allow
4307 conversions from 0. */
4308 if ((POINTER_TYPE_P (dest_type)
4309 || TREE_CODE (dest_type) == OFFSET_TYPE)
4310 && INTEGRAL_TYPE_P (src_type))
4312 if (TREE_CODE (src) == INTEGER_CST
4313 && TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type))
4314 return null_pointer_node;
4315 if (integer_zerop (src))
4316 return null_pointer_node;
4317 else if (TYPE_PRECISION (dest_type) <= TYPE_PRECISION (src_type))
4318 return initializer_constant_valid_p_1 (src, endtype, cache);
4321 /* Allow conversions to struct or union types if the value
4322 inside is okay. */
4323 if (TREE_CODE (dest_type) == RECORD_TYPE
4324 || TREE_CODE (dest_type) == UNION_TYPE)
4325 return initializer_constant_valid_p_1 (src, endtype, cache);
4327 break;
4329 case POINTER_PLUS_EXPR:
4330 case PLUS_EXPR:
4331 /* Any valid floating-point constants will have been folded by now;
4332 with -frounding-math we hit this with addition of two constants. */
4333 if (TREE_CODE (endtype) == REAL_TYPE)
4334 return NULL_TREE;
4335 if (cache && cache[0] == value)
4336 return cache[1];
4337 if (! INTEGRAL_TYPE_P (endtype)
4338 || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
4340 tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
4341 tree valid0
4342 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4343 endtype, ncache);
4344 tree valid1
4345 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
4346 endtype, ncache + 2);
4347 /* If either term is absolute, use the other term's relocation. */
4348 if (valid0 == null_pointer_node)
4349 ret = valid1;
4350 else if (valid1 == null_pointer_node)
4351 ret = valid0;
4352 /* Support narrowing pointer differences. */
4353 else
4354 ret = narrowing_initializer_constant_valid_p (value, endtype,
4355 ncache);
4357 else
4358 /* Support narrowing pointer differences. */
4359 ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
4360 if (cache)
4362 cache[0] = value;
4363 cache[1] = ret;
4365 return ret;
4367 case MINUS_EXPR:
4368 if (TREE_CODE (endtype) == REAL_TYPE)
4369 return NULL_TREE;
4370 if (cache && cache[0] == value)
4371 return cache[1];
4372 if (! INTEGRAL_TYPE_P (endtype)
4373 || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
4375 tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
4376 tree valid0
4377 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4378 endtype, ncache);
4379 tree valid1
4380 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
4381 endtype, ncache + 2);
4382 /* Win if second argument is absolute. */
4383 if (valid1 == null_pointer_node)
4384 ret = valid0;
4385 /* Win if both arguments have the same relocation.
4386 Then the value is absolute. */
4387 else if (valid0 == valid1 && valid0 != 0)
4388 ret = null_pointer_node;
4389 /* Since GCC guarantees that string constants are unique in the
4390 generated code, a subtraction between two copies of the same
4391 constant string is absolute. */
4392 else if (valid0 && TREE_CODE (valid0) == STRING_CST
4393 && valid1 && TREE_CODE (valid1) == STRING_CST
4394 && operand_equal_p (valid0, valid1, 1))
4395 ret = null_pointer_node;
4396 /* Support narrowing differences. */
4397 else
4398 ret = narrowing_initializer_constant_valid_p (value, endtype,
4399 ncache);
4401 else
4402 /* Support narrowing differences. */
4403 ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
4404 if (cache)
4406 cache[0] = value;
4407 cache[1] = ret;
4409 return ret;
4411 default:
4412 break;
4415 return NULL_TREE;
4418 /* Return nonzero if VALUE is a valid constant-valued expression
4419 for use in initializing a static variable; one that can be an
4420 element of a "constant" initializer.
4422 Return null_pointer_node if the value is absolute;
4423 if it is relocatable, return the variable that determines the relocation.
4424 We assume that VALUE has been folded as much as possible;
4425 therefore, we do not need to check for such things as
4426 arithmetic-combinations of integers. */
4427 tree
4428 initializer_constant_valid_p (tree value, tree endtype)
4430 return initializer_constant_valid_p_1 (value, endtype, NULL);
4433 /* Return true if VALUE is a valid constant-valued expression
4434 for use in initializing a static bit-field; one that can be
4435 an element of a "constant" initializer. */
4437 bool
4438 initializer_constant_valid_for_bitfield_p (tree value)
4440 /* For bitfields we support integer constants or possibly nested aggregates
4441 of such. */
4442 switch (TREE_CODE (value))
4444 case CONSTRUCTOR:
4446 unsigned HOST_WIDE_INT idx;
4447 tree elt;
4449 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
4450 if (!initializer_constant_valid_for_bitfield_p (elt))
4451 return false;
4452 return true;
4455 case INTEGER_CST:
4456 return true;
4458 case VIEW_CONVERT_EXPR:
4459 case NON_LVALUE_EXPR:
4460 return
4461 initializer_constant_valid_for_bitfield_p (TREE_OPERAND (value, 0));
4463 default:
4464 break;
4467 return false;
4470 /* output_constructor outer state of relevance in recursive calls, typically
4471 for nested aggregate bitfields. */
4473 typedef struct {
4474 unsigned int bit_offset; /* current position in ... */
4475 int byte; /* ... the outer byte buffer. */
4476 } oc_outer_state;
4478 static unsigned HOST_WIDE_INT
4479 output_constructor (tree, unsigned HOST_WIDE_INT, unsigned int,
4480 oc_outer_state *);
4482 /* Output assembler code for constant EXP to FILE, with no label.
4483 This includes the pseudo-op such as ".int" or ".byte", and a newline.
4484 Assumes output_addressed_constants has been done on EXP already.
4486 Generate exactly SIZE bytes of assembler data, padding at the end
4487 with zeros if necessary. SIZE must always be specified.
4489 SIZE is important for structure constructors,
4490 since trailing members may have been omitted from the constructor.
4491 It is also important for initialization of arrays from string constants
4492 since the full length of the string constant might not be wanted.
4493 It is also needed for initialization of unions, where the initializer's
4494 type is just one member, and that may not be as long as the union.
4496 There a case in which we would fail to output exactly SIZE bytes:
4497 for a structure constructor that wants to produce more than SIZE bytes.
4498 But such constructors will never be generated for any possible input.
4500 ALIGN is the alignment of the data in bits. */
4502 void
4503 output_constant (tree exp, unsigned HOST_WIDE_INT size, unsigned int align)
4505 enum tree_code code;
4506 unsigned HOST_WIDE_INT thissize;
4508 if (size == 0 || flag_syntax_only)
4509 return;
4511 /* See if we're trying to initialize a pointer in a non-default mode
4512 to the address of some declaration somewhere. If the target says
4513 the mode is valid for pointers, assume the target has a way of
4514 resolving it. */
4515 if (TREE_CODE (exp) == NOP_EXPR
4516 && POINTER_TYPE_P (TREE_TYPE (exp))
4517 && targetm.addr_space.valid_pointer_mode
4518 (TYPE_MODE (TREE_TYPE (exp)),
4519 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
4521 tree saved_type = TREE_TYPE (exp);
4523 /* Peel off any intermediate conversions-to-pointer for valid
4524 pointer modes. */
4525 while (TREE_CODE (exp) == NOP_EXPR
4526 && POINTER_TYPE_P (TREE_TYPE (exp))
4527 && targetm.addr_space.valid_pointer_mode
4528 (TYPE_MODE (TREE_TYPE (exp)),
4529 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
4530 exp = TREE_OPERAND (exp, 0);
4532 /* If what we're left with is the address of something, we can
4533 convert the address to the final type and output it that
4534 way. */
4535 if (TREE_CODE (exp) == ADDR_EXPR)
4536 exp = build1 (ADDR_EXPR, saved_type, TREE_OPERAND (exp, 0));
4537 /* Likewise for constant ints. */
4538 else if (TREE_CODE (exp) == INTEGER_CST)
4539 exp = build_int_cst_wide (saved_type, TREE_INT_CST_LOW (exp),
4540 TREE_INT_CST_HIGH (exp));
4544 /* Eliminate any conversions since we'll be outputting the underlying
4545 constant. */
4546 while (CONVERT_EXPR_P (exp)
4547 || TREE_CODE (exp) == NON_LVALUE_EXPR
4548 || TREE_CODE (exp) == VIEW_CONVERT_EXPR)
4550 HOST_WIDE_INT type_size = int_size_in_bytes (TREE_TYPE (exp));
4551 HOST_WIDE_INT op_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (exp, 0)));
4553 /* Make sure eliminating the conversion is really a no-op, except with
4554 VIEW_CONVERT_EXPRs to allow for wild Ada unchecked conversions and
4555 union types to allow for Ada unchecked unions. */
4556 if (type_size > op_size
4557 && TREE_CODE (exp) != VIEW_CONVERT_EXPR
4558 && TREE_CODE (TREE_TYPE (exp)) != UNION_TYPE)
4559 /* Keep the conversion. */
4560 break;
4561 else
4562 exp = TREE_OPERAND (exp, 0);
4565 code = TREE_CODE (TREE_TYPE (exp));
4566 thissize = int_size_in_bytes (TREE_TYPE (exp));
4568 /* Allow a constructor with no elements for any data type.
4569 This means to fill the space with zeros. */
4570 if (TREE_CODE (exp) == CONSTRUCTOR
4571 && VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (exp)))
4573 assemble_zeros (size);
4574 return;
4577 if (TREE_CODE (exp) == FDESC_EXPR)
4579 #ifdef ASM_OUTPUT_FDESC
4580 HOST_WIDE_INT part = tree_low_cst (TREE_OPERAND (exp, 1), 0);
4581 tree decl = TREE_OPERAND (exp, 0);
4582 ASM_OUTPUT_FDESC (asm_out_file, decl, part);
4583 #else
4584 gcc_unreachable ();
4585 #endif
4586 return;
4589 /* Now output the underlying data. If we've handling the padding, return.
4590 Otherwise, break and ensure SIZE is the size written. */
4591 switch (code)
4593 case BOOLEAN_TYPE:
4594 case INTEGER_TYPE:
4595 case ENUMERAL_TYPE:
4596 case POINTER_TYPE:
4597 case REFERENCE_TYPE:
4598 case OFFSET_TYPE:
4599 case FIXED_POINT_TYPE:
4600 if (! assemble_integer (expand_expr (exp, NULL_RTX, VOIDmode,
4601 EXPAND_INITIALIZER),
4602 MIN (size, thissize), align, 0))
4603 error ("initializer for integer/fixed-point value is too complicated");
4604 break;
4606 case REAL_TYPE:
4607 if (TREE_CODE (exp) != REAL_CST)
4608 error ("initializer for floating value is not a floating constant");
4609 else
4610 assemble_real (TREE_REAL_CST (exp), TYPE_MODE (TREE_TYPE (exp)), align);
4611 break;
4613 case COMPLEX_TYPE:
4614 output_constant (TREE_REALPART (exp), thissize / 2, align);
4615 output_constant (TREE_IMAGPART (exp), thissize / 2,
4616 min_align (align, BITS_PER_UNIT * (thissize / 2)));
4617 break;
4619 case ARRAY_TYPE:
4620 case VECTOR_TYPE:
4621 switch (TREE_CODE (exp))
4623 case CONSTRUCTOR:
4624 output_constructor (exp, size, align, NULL);
4625 return;
4626 case STRING_CST:
4627 thissize = MIN ((unsigned HOST_WIDE_INT)TREE_STRING_LENGTH (exp),
4628 size);
4629 assemble_string (TREE_STRING_POINTER (exp), thissize);
4630 break;
4632 case VECTOR_CST:
4634 int elt_size;
4635 tree link;
4636 unsigned int nalign;
4637 enum machine_mode inner;
4639 inner = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
4640 nalign = MIN (align, GET_MODE_ALIGNMENT (inner));
4642 elt_size = GET_MODE_SIZE (inner);
4644 link = TREE_VECTOR_CST_ELTS (exp);
4645 output_constant (TREE_VALUE (link), elt_size, align);
4646 thissize = elt_size;
4647 while ((link = TREE_CHAIN (link)) != NULL)
4649 output_constant (TREE_VALUE (link), elt_size, nalign);
4650 thissize += elt_size;
4652 break;
4654 default:
4655 gcc_unreachable ();
4657 break;
4659 case RECORD_TYPE:
4660 case UNION_TYPE:
4661 gcc_assert (TREE_CODE (exp) == CONSTRUCTOR);
4662 output_constructor (exp, size, align, NULL);
4663 return;
4665 case ERROR_MARK:
4666 return;
4668 default:
4669 gcc_unreachable ();
4672 if (size > thissize)
4673 assemble_zeros (size - thissize);
4677 /* Subroutine of output_constructor, used for computing the size of
4678 arrays of unspecified length. VAL must be a CONSTRUCTOR of an array
4679 type with an unspecified upper bound. */
4681 static unsigned HOST_WIDE_INT
4682 array_size_for_constructor (tree val)
4684 tree max_index;
4685 unsigned HOST_WIDE_INT cnt;
4686 tree index, value, tmp;
4687 double_int i;
4689 /* This code used to attempt to handle string constants that are not
4690 arrays of single-bytes, but nothing else does, so there's no point in
4691 doing it here. */
4692 if (TREE_CODE (val) == STRING_CST)
4693 return TREE_STRING_LENGTH (val);
4695 max_index = NULL_TREE;
4696 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (val), cnt, index, value)
4698 if (TREE_CODE (index) == RANGE_EXPR)
4699 index = TREE_OPERAND (index, 1);
4700 if (max_index == NULL_TREE || tree_int_cst_lt (max_index, index))
4701 max_index = index;
4704 if (max_index == NULL_TREE)
4705 return 0;
4707 /* Compute the total number of array elements. */
4708 tmp = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (val)));
4709 i = double_int_sub (tree_to_double_int (max_index), tree_to_double_int (tmp));
4710 i = double_int_add (i, double_int_one);
4712 /* Multiply by the array element unit size to find number of bytes. */
4713 i = double_int_mul (i, tree_to_double_int
4714 (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (val)))));
4716 gcc_assert (double_int_fits_in_uhwi_p (i));
4717 return i.low;
4720 /* Other datastructures + helpers for output_constructor. */
4722 /* output_constructor local state to support interaction with helpers. */
4724 typedef struct {
4726 /* Received arguments. */
4727 tree exp; /* Constructor expression. */
4728 unsigned HOST_WIDE_INT size; /* # bytes to output - pad if necessary. */
4729 unsigned int align; /* Known initial alignment. */
4731 /* Constructor expression data. */
4732 tree type; /* Expression type. */
4733 tree field; /* Current field decl in a record. */
4734 tree min_index; /* Lower bound if specified for an array. */
4736 /* Output processing state. */
4737 HOST_WIDE_INT total_bytes; /* # bytes output so far / current position. */
4738 bool byte_buffer_in_use; /* Whether byte ... */
4739 int byte; /* ... contains part of a bitfield byte yet to
4740 be output. */
4742 int last_relative_index; /* Implicit or explicit index of the last
4743 array element output within a bitfield. */
4744 /* Current element. */
4745 tree val; /* Current element value. */
4746 tree index; /* Current element index. */
4748 } oc_local_state;
4750 /* Helper for output_constructor. From the current LOCAL state, output a
4751 RANGE_EXPR element. */
4753 static void
4754 output_constructor_array_range (oc_local_state *local)
4756 unsigned HOST_WIDE_INT fieldsize
4757 = int_size_in_bytes (TREE_TYPE (local->type));
4759 HOST_WIDE_INT lo_index
4760 = tree_low_cst (TREE_OPERAND (local->index, 0), 0);
4761 HOST_WIDE_INT hi_index
4762 = tree_low_cst (TREE_OPERAND (local->index, 1), 0);
4763 HOST_WIDE_INT index;
4765 unsigned int align2
4766 = min_align (local->align, fieldsize * BITS_PER_UNIT);
4768 for (index = lo_index; index <= hi_index; index++)
4770 /* Output the element's initial value. */
4771 if (local->val == NULL_TREE)
4772 assemble_zeros (fieldsize);
4773 else
4774 output_constant (local->val, fieldsize, align2);
4776 /* Count its size. */
4777 local->total_bytes += fieldsize;
4781 /* Helper for output_constructor. From the current LOCAL state, output a
4782 field element that is not true bitfield or part of an outer one. */
4784 static void
4785 output_constructor_regular_field (oc_local_state *local)
4787 /* Field size and position. Since this structure is static, we know the
4788 positions are constant. */
4789 unsigned HOST_WIDE_INT fieldsize;
4790 HOST_WIDE_INT fieldpos;
4792 unsigned int align2;
4794 if (local->index != NULL_TREE)
4796 double_int idx = double_int_sub (tree_to_double_int (local->index),
4797 tree_to_double_int (local->min_index));
4798 gcc_assert (double_int_fits_in_shwi_p (idx));
4799 fieldpos = (tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (local->val)), 1)
4800 * idx.low);
4802 else if (local->field != NULL_TREE)
4803 fieldpos = int_byte_position (local->field);
4804 else
4805 fieldpos = 0;
4807 /* Output any buffered-up bit-fields preceding this element. */
4808 if (local->byte_buffer_in_use)
4810 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
4811 local->total_bytes++;
4812 local->byte_buffer_in_use = false;
4815 /* Advance to offset of this element.
4816 Note no alignment needed in an array, since that is guaranteed
4817 if each element has the proper size. */
4818 if ((local->field != NULL_TREE || local->index != NULL_TREE)
4819 && fieldpos != local->total_bytes)
4821 gcc_assert (fieldpos >= local->total_bytes);
4822 assemble_zeros (fieldpos - local->total_bytes);
4823 local->total_bytes = fieldpos;
4826 /* Find the alignment of this element. */
4827 align2 = min_align (local->align, BITS_PER_UNIT * fieldpos);
4829 /* Determine size this element should occupy. */
4830 if (local->field)
4832 fieldsize = 0;
4834 /* If this is an array with an unspecified upper bound,
4835 the initializer determines the size. */
4836 /* ??? This ought to only checked if DECL_SIZE_UNIT is NULL,
4837 but we cannot do this until the deprecated support for
4838 initializing zero-length array members is removed. */
4839 if (TREE_CODE (TREE_TYPE (local->field)) == ARRAY_TYPE
4840 && TYPE_DOMAIN (TREE_TYPE (local->field))
4841 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (local->field))))
4843 fieldsize = array_size_for_constructor (local->val);
4844 /* Given a non-empty initialization, this field had
4845 better be last. */
4846 gcc_assert (!fieldsize || !DECL_CHAIN (local->field));
4848 else
4849 fieldsize = tree_low_cst (DECL_SIZE_UNIT (local->field), 1);
4851 else
4852 fieldsize = int_size_in_bytes (TREE_TYPE (local->type));
4854 /* Output the element's initial value. */
4855 if (local->val == NULL_TREE)
4856 assemble_zeros (fieldsize);
4857 else
4858 output_constant (local->val, fieldsize, align2);
4860 /* Count its size. */
4861 local->total_bytes += fieldsize;
4864 /* Helper for output_constructor. From the current LOCAL and OUTER states,
4865 output an element that is a true bitfield or part of an outer one. */
4867 static void
4868 output_constructor_bitfield (oc_local_state *local, oc_outer_state *outer)
4870 /* Bit size of this element. */
4871 HOST_WIDE_INT ebitsize
4872 = (local->field
4873 ? tree_low_cst (DECL_SIZE (local->field), 1)
4874 : tree_low_cst (TYPE_SIZE (TREE_TYPE (local->type)), 1));
4876 /* Relative index of this element if this is an array component. */
4877 HOST_WIDE_INT relative_index
4878 = (!local->field
4879 ? (local->index
4880 ? (tree_low_cst (local->index, 0)
4881 - tree_low_cst (local->min_index, 0))
4882 : local->last_relative_index + 1)
4883 : 0);
4885 /* Bit position of this element from the start of the containing
4886 constructor. */
4887 HOST_WIDE_INT constructor_relative_ebitpos
4888 = (local->field
4889 ? int_bit_position (local->field)
4890 : ebitsize * relative_index);
4892 /* Bit position of this element from the start of a possibly ongoing
4893 outer byte buffer. */
4894 HOST_WIDE_INT byte_relative_ebitpos
4895 = ((outer ? outer->bit_offset : 0) + constructor_relative_ebitpos);
4897 /* From the start of a possibly ongoing outer byte buffer, offsets to
4898 the first bit of this element and to the first bit past the end of
4899 this element. */
4900 HOST_WIDE_INT next_offset = byte_relative_ebitpos;
4901 HOST_WIDE_INT end_offset = byte_relative_ebitpos + ebitsize;
4903 local->last_relative_index = relative_index;
4905 if (local->val == NULL_TREE)
4906 local->val = integer_zero_node;
4908 while (TREE_CODE (local->val) == VIEW_CONVERT_EXPR
4909 || TREE_CODE (local->val) == NON_LVALUE_EXPR)
4910 local->val = TREE_OPERAND (local->val, 0);
4912 if (TREE_CODE (local->val) != INTEGER_CST
4913 && TREE_CODE (local->val) != CONSTRUCTOR)
4915 error ("invalid initial value for member %qE", DECL_NAME (local->field));
4916 return;
4919 /* If this field does not start in this (or, next) byte,
4920 skip some bytes. */
4921 if (next_offset / BITS_PER_UNIT != local->total_bytes)
4923 /* Output remnant of any bit field in previous bytes. */
4924 if (local->byte_buffer_in_use)
4926 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
4927 local->total_bytes++;
4928 local->byte_buffer_in_use = false;
4931 /* If still not at proper byte, advance to there. */
4932 if (next_offset / BITS_PER_UNIT != local->total_bytes)
4934 gcc_assert (next_offset / BITS_PER_UNIT >= local->total_bytes);
4935 assemble_zeros (next_offset / BITS_PER_UNIT - local->total_bytes);
4936 local->total_bytes = next_offset / BITS_PER_UNIT;
4940 /* Set up the buffer if necessary. */
4941 if (!local->byte_buffer_in_use)
4943 local->byte = 0;
4944 if (ebitsize > 0)
4945 local->byte_buffer_in_use = true;
4948 /* If this is nested constructor, recurse passing the bit offset and the
4949 pending data, then retrieve the new pending data afterwards. */
4950 if (TREE_CODE (local->val) == CONSTRUCTOR)
4952 oc_outer_state output_state;
4954 output_state.bit_offset = next_offset % BITS_PER_UNIT;
4955 output_state.byte = local->byte;
4956 local->total_bytes
4957 += output_constructor (local->val, 0, 0, &output_state);
4958 local->byte = output_state.byte;
4959 return;
4962 /* Otherwise, we must split the element into pieces that fall within
4963 separate bytes, and combine each byte with previous or following
4964 bit-fields. */
4965 while (next_offset < end_offset)
4967 int this_time;
4968 int shift;
4969 HOST_WIDE_INT value;
4970 HOST_WIDE_INT next_byte = next_offset / BITS_PER_UNIT;
4971 HOST_WIDE_INT next_bit = next_offset % BITS_PER_UNIT;
4973 /* Advance from byte to byte
4974 within this element when necessary. */
4975 while (next_byte != local->total_bytes)
4977 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
4978 local->total_bytes++;
4979 local->byte = 0;
4982 /* Number of bits we can process at once
4983 (all part of the same byte). */
4984 this_time = MIN (end_offset - next_offset,
4985 BITS_PER_UNIT - next_bit);
4986 if (BYTES_BIG_ENDIAN)
4988 /* On big-endian machine, take the most significant bits
4989 first (of the bits that are significant)
4990 and put them into bytes from the most significant end. */
4991 shift = end_offset - next_offset - this_time;
4993 /* Don't try to take a bunch of bits that cross
4994 the word boundary in the INTEGER_CST. We can
4995 only select bits from the LOW or HIGH part
4996 not from both. */
4997 if (shift < HOST_BITS_PER_WIDE_INT
4998 && shift + this_time > HOST_BITS_PER_WIDE_INT)
5000 this_time = shift + this_time - HOST_BITS_PER_WIDE_INT;
5001 shift = HOST_BITS_PER_WIDE_INT;
5004 /* Now get the bits from the appropriate constant word. */
5005 if (shift < HOST_BITS_PER_WIDE_INT)
5006 value = TREE_INT_CST_LOW (local->val);
5007 else
5009 gcc_assert (shift < 2 * HOST_BITS_PER_WIDE_INT);
5010 value = TREE_INT_CST_HIGH (local->val);
5011 shift -= HOST_BITS_PER_WIDE_INT;
5014 /* Get the result. This works only when:
5015 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
5016 local->byte |= (((value >> shift)
5017 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
5018 << (BITS_PER_UNIT - this_time - next_bit));
5020 else
5022 /* On little-endian machines,
5023 take first the least significant bits of the value
5024 and pack them starting at the least significant
5025 bits of the bytes. */
5026 shift = next_offset - byte_relative_ebitpos;
5028 /* Don't try to take a bunch of bits that cross
5029 the word boundary in the INTEGER_CST. We can
5030 only select bits from the LOW or HIGH part
5031 not from both. */
5032 if (shift < HOST_BITS_PER_WIDE_INT
5033 && shift + this_time > HOST_BITS_PER_WIDE_INT)
5034 this_time = (HOST_BITS_PER_WIDE_INT - shift);
5036 /* Now get the bits from the appropriate constant word. */
5037 if (shift < HOST_BITS_PER_WIDE_INT)
5038 value = TREE_INT_CST_LOW (local->val);
5039 else
5041 gcc_assert (shift < 2 * HOST_BITS_PER_WIDE_INT);
5042 value = TREE_INT_CST_HIGH (local->val);
5043 shift -= HOST_BITS_PER_WIDE_INT;
5046 /* Get the result. This works only when:
5047 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
5048 local->byte |= (((value >> shift)
5049 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
5050 << next_bit);
5053 next_offset += this_time;
5054 local->byte_buffer_in_use = true;
5058 /* Subroutine of output_constant, used for CONSTRUCTORs (aggregate constants).
5059 Generate at least SIZE bytes, padding if necessary. OUTER designates the
5060 caller output state of relevance in recursive invocations. */
5062 static unsigned HOST_WIDE_INT
5063 output_constructor (tree exp, unsigned HOST_WIDE_INT size,
5064 unsigned int align, oc_outer_state * outer)
5066 unsigned HOST_WIDE_INT cnt;
5067 constructor_elt *ce;
5069 oc_local_state local;
5071 /* Setup our local state to communicate with helpers. */
5072 local.exp = exp;
5073 local.size = size;
5074 local.align = align;
5076 local.total_bytes = 0;
5077 local.byte_buffer_in_use = outer != NULL;
5078 local.byte = outer ? outer->byte : 0;
5080 local.type = TREE_TYPE (exp);
5082 local.last_relative_index = -1;
5084 local.min_index = NULL_TREE;
5085 if (TREE_CODE (local.type) == ARRAY_TYPE
5086 && TYPE_DOMAIN (local.type) != NULL_TREE)
5087 local.min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (local.type));
5089 gcc_assert (HOST_BITS_PER_WIDE_INT >= BITS_PER_UNIT);
5091 /* As CE goes through the elements of the constant, FIELD goes through the
5092 structure fields if the constant is a structure. If the constant is a
5093 union, we override this by getting the field from the TREE_LIST element.
5094 But the constant could also be an array. Then FIELD is zero.
5096 There is always a maximum of one element in the chain LINK for unions
5097 (even if the initializer in a source program incorrectly contains
5098 more one). */
5100 local.field = NULL_TREE;
5101 if (TREE_CODE (local.type) == RECORD_TYPE)
5102 local.field = TYPE_FIELDS (local.type);
5104 for (cnt = 0;
5105 VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (exp), cnt, ce);
5106 cnt++, local.field = local.field ? DECL_CHAIN (local.field) : 0)
5108 local.val = ce->value;
5109 local.index = NULL_TREE;
5111 /* The element in a union constructor specifies the proper field
5112 or index. */
5113 if ((TREE_CODE (local.type) == RECORD_TYPE
5114 || TREE_CODE (local.type) == UNION_TYPE
5115 || TREE_CODE (local.type) == QUAL_UNION_TYPE)
5116 && ce->index != NULL_TREE)
5117 local.field = ce->index;
5119 else if (TREE_CODE (local.type) == ARRAY_TYPE)
5120 local.index = ce->index;
5122 if (local.field && flag_verbose_asm)
5123 fprintf (asm_out_file, "%s %s:\n",
5124 ASM_COMMENT_START,
5125 DECL_NAME (local.field)
5126 ? IDENTIFIER_POINTER (DECL_NAME (local.field))
5127 : "<anonymous>");
5129 /* Eliminate the marker that makes a cast not be an lvalue. */
5130 if (local.val != NULL_TREE)
5131 STRIP_NOPS (local.val);
5133 /* Output the current element, using the appropriate helper ... */
5135 /* For an array slice not part of an outer bitfield. */
5136 if (!outer
5137 && local.index != NULL_TREE
5138 && TREE_CODE (local.index) == RANGE_EXPR)
5139 output_constructor_array_range (&local);
5141 /* For a field that is neither a true bitfield nor part of an outer one,
5142 known to be at least byte aligned and multiple-of-bytes long. */
5143 else if (!outer
5144 && (local.field == NULL_TREE
5145 || !CONSTRUCTOR_BITFIELD_P (local.field)))
5146 output_constructor_regular_field (&local);
5148 /* For a true bitfield or part of an outer one. */
5149 else
5150 output_constructor_bitfield (&local, outer);
5153 /* If we are not at toplevel, save the pending data for our caller.
5154 Otherwise output the pending data and padding zeros as needed. */
5155 if (outer)
5156 outer->byte = local.byte;
5157 else
5159 if (local.byte_buffer_in_use)
5161 assemble_integer (GEN_INT (local.byte), 1, BITS_PER_UNIT, 1);
5162 local.total_bytes++;
5165 if ((unsigned HOST_WIDE_INT)local.total_bytes < local.size)
5167 assemble_zeros (local.size - local.total_bytes);
5168 local.total_bytes = local.size;
5172 return local.total_bytes;
5175 /* Mark DECL as weak. */
5177 static void
5178 mark_weak (tree decl)
5180 DECL_WEAK (decl) = 1;
5182 if (DECL_RTL_SET_P (decl)
5183 && MEM_P (DECL_RTL (decl))
5184 && XEXP (DECL_RTL (decl), 0)
5185 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == SYMBOL_REF)
5186 SYMBOL_REF_WEAK (XEXP (DECL_RTL (decl), 0)) = 1;
5189 /* Merge weak status between NEWDECL and OLDDECL. */
5191 void
5192 merge_weak (tree newdecl, tree olddecl)
5194 if (DECL_WEAK (newdecl) == DECL_WEAK (olddecl))
5196 if (DECL_WEAK (newdecl) && TARGET_SUPPORTS_WEAK)
5198 tree *pwd;
5199 /* We put the NEWDECL on the weak_decls list at some point
5200 and OLDDECL as well. Keep just OLDDECL on the list. */
5201 for (pwd = &weak_decls; *pwd; pwd = &TREE_CHAIN (*pwd))
5202 if (TREE_VALUE (*pwd) == newdecl)
5204 *pwd = TREE_CHAIN (*pwd);
5205 break;
5208 return;
5211 if (DECL_WEAK (newdecl))
5213 tree wd;
5215 /* NEWDECL is weak, but OLDDECL is not. */
5217 /* If we already output the OLDDECL, we're in trouble; we can't
5218 go back and make it weak. This should never happen in
5219 unit-at-a-time compilation. */
5220 gcc_assert (!TREE_ASM_WRITTEN (olddecl));
5222 /* If we've already generated rtl referencing OLDDECL, we may
5223 have done so in a way that will not function properly with
5224 a weak symbol. Again in unit-at-a-time this should be
5225 impossible. */
5226 gcc_assert (!TREE_USED (olddecl)
5227 || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (olddecl)));
5229 if (TARGET_SUPPORTS_WEAK)
5231 /* We put the NEWDECL on the weak_decls list at some point.
5232 Replace it with the OLDDECL. */
5233 for (wd = weak_decls; wd; wd = TREE_CHAIN (wd))
5234 if (TREE_VALUE (wd) == newdecl)
5236 TREE_VALUE (wd) = olddecl;
5237 break;
5239 /* We may not find the entry on the list. If NEWDECL is a
5240 weak alias, then we will have already called
5241 globalize_decl to remove the entry; in that case, we do
5242 not need to do anything. */
5245 /* Make the OLDDECL weak; it's OLDDECL that we'll be keeping. */
5246 mark_weak (olddecl);
5248 else
5249 /* OLDDECL was weak, but NEWDECL was not explicitly marked as
5250 weak. Just update NEWDECL to indicate that it's weak too. */
5251 mark_weak (newdecl);
5254 /* Declare DECL to be a weak symbol. */
5256 void
5257 declare_weak (tree decl)
5259 gcc_assert (TREE_CODE (decl) != FUNCTION_DECL || !TREE_ASM_WRITTEN (decl));
5260 if (! TREE_PUBLIC (decl))
5261 error ("weak declaration of %q+D must be public", decl);
5262 else if (!TARGET_SUPPORTS_WEAK)
5263 warning (0, "weak declaration of %q+D not supported", decl);
5265 mark_weak (decl);
5266 if (!lookup_attribute ("weak", DECL_ATTRIBUTES (decl)))
5267 DECL_ATTRIBUTES (decl)
5268 = tree_cons (get_identifier ("weak"), NULL, DECL_ATTRIBUTES (decl));
5271 static void
5272 weak_finish_1 (tree decl)
5274 #if defined (ASM_WEAKEN_DECL) || defined (ASM_WEAKEN_LABEL)
5275 const char *const name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5276 #endif
5278 if (! TREE_USED (decl))
5279 return;
5281 #ifdef ASM_WEAKEN_DECL
5282 ASM_WEAKEN_DECL (asm_out_file, decl, name, NULL);
5283 #else
5284 #ifdef ASM_WEAKEN_LABEL
5285 ASM_WEAKEN_LABEL (asm_out_file, name);
5286 #else
5287 #ifdef ASM_OUTPUT_WEAK_ALIAS
5289 static bool warn_once = 0;
5290 if (! warn_once)
5292 warning (0, "only weak aliases are supported in this configuration");
5293 warn_once = 1;
5295 return;
5297 #endif
5298 #endif
5299 #endif
5302 /* This TREE_LIST contains weakref targets. */
5304 static GTY(()) tree weakref_targets;
5306 /* Forward declaration. */
5307 static tree find_decl_and_mark_needed (tree decl, tree target);
5309 /* Emit any pending weak declarations. */
5311 void
5312 weak_finish (void)
5314 tree t;
5316 for (t = weakref_targets; t; t = TREE_CHAIN (t))
5318 tree alias_decl = TREE_PURPOSE (t);
5319 tree target = ultimate_transparent_alias_target (&TREE_VALUE (t));
5321 if (! TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (alias_decl)))
5322 /* Remove alias_decl from the weak list, but leave entries for
5323 the target alone. */
5324 target = NULL_TREE;
5325 #ifndef ASM_OUTPUT_WEAKREF
5326 else if (! TREE_SYMBOL_REFERENCED (target))
5328 /* Use ASM_WEAKEN_LABEL only if ASM_WEAKEN_DECL is not
5329 defined, otherwise we and weak_finish_1 would use
5330 different macros. */
5331 # if defined ASM_WEAKEN_LABEL && ! defined ASM_WEAKEN_DECL
5332 ASM_WEAKEN_LABEL (asm_out_file, IDENTIFIER_POINTER (target));
5333 # else
5334 tree decl = find_decl_and_mark_needed (alias_decl, target);
5336 if (! decl)
5338 decl = build_decl (DECL_SOURCE_LOCATION (alias_decl),
5339 TREE_CODE (alias_decl), target,
5340 TREE_TYPE (alias_decl));
5342 DECL_EXTERNAL (decl) = 1;
5343 TREE_PUBLIC (decl) = 1;
5344 DECL_ARTIFICIAL (decl) = 1;
5345 TREE_NOTHROW (decl) = TREE_NOTHROW (alias_decl);
5346 TREE_USED (decl) = 1;
5349 weak_finish_1 (decl);
5350 # endif
5352 #endif
5355 tree *p;
5356 tree t2;
5358 /* Remove the alias and the target from the pending weak list
5359 so that we do not emit any .weak directives for the former,
5360 nor multiple .weak directives for the latter. */
5361 for (p = &weak_decls; (t2 = *p) ; )
5363 if (TREE_VALUE (t2) == alias_decl
5364 || target == DECL_ASSEMBLER_NAME (TREE_VALUE (t2)))
5365 *p = TREE_CHAIN (t2);
5366 else
5367 p = &TREE_CHAIN (t2);
5370 /* Remove other weakrefs to the same target, to speed things up. */
5371 for (p = &TREE_CHAIN (t); (t2 = *p) ; )
5373 if (target == ultimate_transparent_alias_target (&TREE_VALUE (t2)))
5374 *p = TREE_CHAIN (t2);
5375 else
5376 p = &TREE_CHAIN (t2);
5381 for (t = weak_decls; t; t = TREE_CHAIN (t))
5383 tree decl = TREE_VALUE (t);
5385 weak_finish_1 (decl);
5389 /* Emit the assembly bits to indicate that DECL is globally visible. */
5391 static void
5392 globalize_decl (tree decl)
5395 #if defined (ASM_WEAKEN_LABEL) || defined (ASM_WEAKEN_DECL)
5396 if (DECL_WEAK (decl))
5398 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
5399 tree *p, t;
5401 #ifdef ASM_WEAKEN_DECL
5402 ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
5403 #else
5404 ASM_WEAKEN_LABEL (asm_out_file, name);
5405 #endif
5407 /* Remove this function from the pending weak list so that
5408 we do not emit multiple .weak directives for it. */
5409 for (p = &weak_decls; (t = *p) ; )
5411 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
5412 *p = TREE_CHAIN (t);
5413 else
5414 p = &TREE_CHAIN (t);
5417 /* Remove weakrefs to the same target from the pending weakref
5418 list, for the same reason. */
5419 for (p = &weakref_targets; (t = *p) ; )
5421 if (DECL_ASSEMBLER_NAME (decl)
5422 == ultimate_transparent_alias_target (&TREE_VALUE (t)))
5423 *p = TREE_CHAIN (t);
5424 else
5425 p = &TREE_CHAIN (t);
5428 return;
5430 #endif
5432 targetm.asm_out.globalize_decl_name (asm_out_file, decl);
5435 VEC(alias_pair,gc) *alias_pairs;
5437 /* Given an assembly name, find the decl it is associated with. At the
5438 same time, mark it needed for cgraph. */
5440 static tree
5441 find_decl_and_mark_needed (tree decl, tree target)
5443 struct cgraph_node *fnode = NULL;
5444 struct varpool_node *vnode = NULL;
5446 if (TREE_CODE (decl) == FUNCTION_DECL)
5448 fnode = cgraph_node_for_asm (target);
5449 if (fnode == NULL)
5450 vnode = varpool_node_for_asm (target);
5452 else
5454 vnode = varpool_node_for_asm (target);
5455 if (vnode == NULL)
5456 fnode = cgraph_node_for_asm (target);
5459 if (fnode)
5461 cgraph_mark_needed_node (fnode);
5462 return fnode->decl;
5464 else if (vnode)
5466 varpool_mark_needed_node (vnode);
5467 vnode->force_output = 1;
5468 return vnode->decl;
5470 else
5471 return NULL_TREE;
5474 /* Output the assembler code for a define (equate) using ASM_OUTPUT_DEF
5475 or ASM_OUTPUT_DEF_FROM_DECLS. The function defines the symbol whose
5476 tree node is DECL to have the value of the tree node TARGET. */
5478 static void
5479 do_assemble_alias (tree decl, tree target)
5481 /* Emulated TLS had better not get this var. */
5482 gcc_assert(!(!targetm.have_tls
5483 && TREE_CODE (decl) == VAR_DECL
5484 && DECL_THREAD_LOCAL_P (decl)));
5486 if (TREE_ASM_WRITTEN (decl))
5487 return;
5489 /* We must force creation of DECL_RTL for debug info generation, even though
5490 we don't use it here. */
5491 make_decl_rtl (decl);
5493 TREE_ASM_WRITTEN (decl) = 1;
5494 TREE_ASM_WRITTEN (DECL_ASSEMBLER_NAME (decl)) = 1;
5496 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
5498 ultimate_transparent_alias_target (&target);
5500 if (!TREE_SYMBOL_REFERENCED (target))
5501 weakref_targets = tree_cons (decl, target, weakref_targets);
5503 #ifdef ASM_OUTPUT_WEAKREF
5504 ASM_OUTPUT_WEAKREF (asm_out_file, decl,
5505 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
5506 IDENTIFIER_POINTER (target));
5507 #else
5508 if (!TARGET_SUPPORTS_WEAK)
5510 error_at (DECL_SOURCE_LOCATION (decl),
5511 "weakref is not supported in this configuration");
5512 return;
5514 #endif
5515 return;
5518 #ifdef ASM_OUTPUT_DEF
5519 /* Make name accessible from other files, if appropriate. */
5521 if (TREE_PUBLIC (decl))
5523 globalize_decl (decl);
5524 maybe_assemble_visibility (decl);
5526 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
5528 #if defined (ASM_OUTPUT_TYPE_DIRECTIVE) && HAVE_GNU_INDIRECT_FUNCTION
5529 ASM_OUTPUT_TYPE_DIRECTIVE
5530 (asm_out_file, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
5531 IFUNC_ASM_TYPE);
5532 #else
5533 error_at (DECL_SOURCE_LOCATION (decl),
5534 "ifunc is not supported in this configuration");
5535 #endif
5538 # ifdef ASM_OUTPUT_DEF_FROM_DECLS
5539 ASM_OUTPUT_DEF_FROM_DECLS (asm_out_file, decl, target);
5540 # else
5541 ASM_OUTPUT_DEF (asm_out_file,
5542 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
5543 IDENTIFIER_POINTER (target));
5544 # endif
5545 #elif defined (ASM_OUTPUT_WEAK_ALIAS) || defined (ASM_WEAKEN_DECL)
5547 const char *name;
5548 tree *p, t;
5550 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5551 # ifdef ASM_WEAKEN_DECL
5552 ASM_WEAKEN_DECL (asm_out_file, decl, name, IDENTIFIER_POINTER (target));
5553 # else
5554 ASM_OUTPUT_WEAK_ALIAS (asm_out_file, name, IDENTIFIER_POINTER (target));
5555 # endif
5556 /* Remove this function from the pending weak list so that
5557 we do not emit multiple .weak directives for it. */
5558 for (p = &weak_decls; (t = *p) ; )
5559 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
5560 *p = TREE_CHAIN (t);
5561 else
5562 p = &TREE_CHAIN (t);
5564 /* Remove weakrefs to the same target from the pending weakref
5565 list, for the same reason. */
5566 for (p = &weakref_targets; (t = *p) ; )
5568 if (DECL_ASSEMBLER_NAME (decl)
5569 == ultimate_transparent_alias_target (&TREE_VALUE (t)))
5570 *p = TREE_CHAIN (t);
5571 else
5572 p = &TREE_CHAIN (t);
5575 #endif
5579 /* Allocate and construct a symbol alias set. */
5581 static symbol_alias_set_t *
5582 symbol_alias_set_create (void)
5584 return pointer_set_create ();
5587 /* Destruct and free a symbol alias set. */
5589 void
5590 symbol_alias_set_destroy (symbol_alias_set_t *aset)
5592 pointer_set_destroy (aset);
5595 /* Test if a symbol alias set contains a given name. */
5598 symbol_alias_set_contains (const symbol_alias_set_t *aset, tree t)
5600 /* We accept either a DECL or an IDENTIFIER directly. */
5601 if (TREE_CODE (t) != IDENTIFIER_NODE)
5602 t = DECL_ASSEMBLER_NAME (t);
5603 t = targetm.asm_out.mangle_assembler_name (IDENTIFIER_POINTER (t));
5604 return pointer_set_contains (aset, t);
5607 /* Enter a new name into a symbol alias set. */
5609 static int
5610 symbol_alias_set_insert (symbol_alias_set_t *aset, tree t)
5612 /* We accept either a DECL or an IDENTIFIER directly. */
5613 if (TREE_CODE (t) != IDENTIFIER_NODE)
5614 t = DECL_ASSEMBLER_NAME (t);
5615 t = targetm.asm_out.mangle_assembler_name (IDENTIFIER_POINTER (t));
5616 return pointer_set_insert (aset, t);
5619 /* IN_SET_P is a predicate function assuming to be taken
5620 alias_pair->decl, alias_pair->target and DATA arguments.
5622 Compute set of aliases by including everything where TRIVIALLY_VISIBLE
5623 predeicate is true and propagate across aliases such that when
5624 alias DECL is included, its TARGET is included too. */
5626 static symbol_alias_set_t *
5627 propagate_aliases_forward (bool (*in_set_p)
5628 (tree decl, tree target, void *data),
5629 void *data)
5631 symbol_alias_set_t *set;
5632 unsigned i;
5633 alias_pair *p;
5634 bool changed;
5636 set = symbol_alias_set_create ();
5637 for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); ++i)
5638 if (in_set_p (p->decl, p->target, data))
5639 symbol_alias_set_insert (set, p->decl);
5642 changed = false;
5643 for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); ++i)
5644 if (symbol_alias_set_contains (set, p->decl)
5645 && !symbol_alias_set_insert (set, p->target))
5646 changed = true;
5648 while (changed);
5650 return set;
5653 /* Like propagate_aliases_forward but do backward propagation. */
5655 symbol_alias_set_t *
5656 propagate_aliases_backward (bool (*in_set_p)
5657 (tree decl, tree target, void *data),
5658 void *data)
5660 symbol_alias_set_t *set;
5661 unsigned i;
5662 alias_pair *p;
5663 bool changed;
5665 /* We have to compute the set of set nodes including aliases
5666 themselves. */
5667 set = symbol_alias_set_create ();
5668 for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); ++i)
5669 if (in_set_p (p->decl, p->target, data))
5670 symbol_alias_set_insert (set, p->target);
5673 changed = false;
5674 for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); ++i)
5675 if (symbol_alias_set_contains (set, p->target)
5676 && !symbol_alias_set_insert (set, p->decl))
5677 changed = true;
5679 while (changed);
5681 return set;
5683 /* See if the alias is trivially visible. This means
5684 1) alias is expoerted from the unit or
5685 2) alias is used in the code.
5686 We assume that unused cgraph/varpool nodes has been
5687 removed.
5688 Used as callback for propagate_aliases. */
5690 static bool
5691 trivially_visible_alias (tree decl, tree target ATTRIBUTE_UNUSED,
5692 void *data ATTRIBUTE_UNUSED)
5694 struct cgraph_node *fnode = NULL;
5695 struct varpool_node *vnode = NULL;
5697 if (!TREE_PUBLIC (decl))
5699 if (TREE_CODE (decl) == FUNCTION_DECL)
5700 fnode = cgraph_get_node (decl);
5701 else
5702 vnode = varpool_get_node (decl);
5703 return vnode || fnode;
5705 else
5706 return true;
5709 /* See if the target of alias is defined in this unit.
5710 Used as callback for propagate_aliases. */
5712 static bool
5713 trivially_defined_alias (tree decl ATTRIBUTE_UNUSED,
5714 tree target,
5715 void *data ATTRIBUTE_UNUSED)
5717 struct cgraph_node *fnode = NULL;
5718 struct varpool_node *vnode = NULL;
5720 fnode = cgraph_node_for_asm (target);
5721 vnode = (fnode == NULL) ? varpool_node_for_asm (target) : NULL;
5722 return (fnode && fnode->analyzed) || (vnode && vnode->finalized);
5725 /* Remove the alias pairing for functions that are no longer in the call
5726 graph. */
5728 void
5729 remove_unreachable_alias_pairs (void)
5731 symbol_alias_set_t *visible;
5732 unsigned i;
5733 alias_pair *p;
5735 if (alias_pairs == NULL)
5736 return;
5738 /* We have to compute the set of visible nodes including aliases
5739 themselves. */
5740 visible = propagate_aliases_forward (trivially_visible_alias, NULL);
5742 for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); )
5744 if (!DECL_EXTERNAL (p->decl)
5745 && !symbol_alias_set_contains (visible, p->decl))
5747 VEC_unordered_remove (alias_pair, alias_pairs, i);
5748 continue;
5751 i++;
5754 symbol_alias_set_destroy (visible);
5758 /* First pass of completing pending aliases. Make sure that cgraph knows
5759 which symbols will be required. */
5761 void
5762 finish_aliases_1 (void)
5764 symbol_alias_set_t *defined;
5765 unsigned i;
5766 alias_pair *p;
5768 if (alias_pairs == NULL)
5769 return;
5771 /* We have to compute the set of defined nodes including aliases
5772 themselves. */
5773 defined = propagate_aliases_backward (trivially_defined_alias, NULL);
5775 FOR_EACH_VEC_ELT (alias_pair, alias_pairs, i, p)
5777 tree target_decl;
5779 target_decl = find_decl_and_mark_needed (p->decl, p->target);
5780 if (target_decl == NULL)
5782 if (symbol_alias_set_contains (defined, p->target))
5783 continue;
5785 if (! (p->emitted_diags & ALIAS_DIAG_TO_UNDEF)
5786 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
5788 error ("%q+D aliased to undefined symbol %qE",
5789 p->decl, p->target);
5790 p->emitted_diags |= ALIAS_DIAG_TO_UNDEF;
5793 else if (! (p->emitted_diags & ALIAS_DIAG_TO_EXTERN)
5794 && DECL_EXTERNAL (target_decl)
5795 /* We use local aliases for C++ thunks to force the tailcall
5796 to bind locally. This is a hack - to keep it working do
5797 the following (which is not strictly correct). */
5798 && (! TREE_CODE (target_decl) == FUNCTION_DECL
5799 || ! DECL_VIRTUAL_P (target_decl))
5800 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
5802 error ("%q+D aliased to external symbol %qE",
5803 p->decl, p->target);
5804 p->emitted_diags |= ALIAS_DIAG_TO_EXTERN;
5808 symbol_alias_set_destroy (defined);
5811 /* Second pass of completing pending aliases. Emit the actual assembly.
5812 This happens at the end of compilation and thus it is assured that the
5813 target symbol has been emitted. */
5815 void
5816 finish_aliases_2 (void)
5818 unsigned i;
5819 alias_pair *p;
5821 FOR_EACH_VEC_ELT (alias_pair, alias_pairs, i, p)
5822 do_assemble_alias (p->decl, p->target);
5824 VEC_truncate (alias_pair, alias_pairs, 0);
5827 /* Emit an assembler directive to make the symbol for DECL an alias to
5828 the symbol for TARGET. */
5830 void
5831 assemble_alias (tree decl, tree target)
5833 tree target_decl;
5835 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
5837 tree alias = DECL_ASSEMBLER_NAME (decl);
5839 ultimate_transparent_alias_target (&target);
5841 if (alias == target)
5842 error ("weakref %q+D ultimately targets itself", decl);
5843 else
5845 #ifndef ASM_OUTPUT_WEAKREF
5846 IDENTIFIER_TRANSPARENT_ALIAS (alias) = 1;
5847 TREE_CHAIN (alias) = target;
5848 #endif
5850 if (TREE_PUBLIC (decl))
5851 error ("weakref %q+D must have static linkage", decl);
5853 else
5855 #if !defined (ASM_OUTPUT_DEF)
5856 # if !defined(ASM_OUTPUT_WEAK_ALIAS) && !defined (ASM_WEAKEN_DECL)
5857 error_at (DECL_SOURCE_LOCATION (decl),
5858 "alias definitions not supported in this configuration");
5859 return;
5860 # else
5861 if (!DECL_WEAK (decl))
5863 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
5864 error_at (DECL_SOURCE_LOCATION (decl),
5865 "ifunc is not supported in this configuration");
5866 else
5867 error_at (DECL_SOURCE_LOCATION (decl),
5868 "only weak aliases are supported in this configuration");
5869 return;
5871 # endif
5872 #endif
5874 TREE_USED (decl) = 1;
5876 /* Allow aliases to aliases. */
5877 if (TREE_CODE (decl) == FUNCTION_DECL)
5878 cgraph_get_create_node (decl)->alias = true;
5879 else
5880 varpool_node (decl)->alias = true;
5882 /* If the target has already been emitted, we don't have to queue the
5883 alias. This saves a tad of memory. */
5884 if (cgraph_global_info_ready)
5885 target_decl = find_decl_and_mark_needed (decl, target);
5886 else
5887 target_decl= NULL;
5888 if (target_decl && TREE_ASM_WRITTEN (target_decl))
5889 do_assemble_alias (decl, target);
5890 else
5892 alias_pair *p = VEC_safe_push (alias_pair, gc, alias_pairs, NULL);
5893 p->decl = decl;
5894 p->target = target;
5895 p->emitted_diags = ALIAS_DIAG_NONE;
5899 /* Record and output a table of translations from original function
5900 to its transaction aware clone. Note that tm_pure functions are
5901 considered to be their own clone. */
5903 static GTY((if_marked ("tree_map_marked_p"), param_is (struct tree_map)))
5904 htab_t tm_clone_hash;
5906 void
5907 record_tm_clone_pair (tree o, tree n)
5909 struct tree_map **slot, *h;
5911 if (tm_clone_hash == NULL)
5912 tm_clone_hash = htab_create_ggc (32, tree_map_hash, tree_map_eq, 0);
5914 h = ggc_alloc_tree_map ();
5915 h->hash = htab_hash_pointer (o);
5916 h->base.from = o;
5917 h->to = n;
5919 slot = (struct tree_map **)
5920 htab_find_slot_with_hash (tm_clone_hash, h, h->hash, INSERT);
5921 *slot = h;
5924 tree
5925 get_tm_clone_pair (tree o)
5927 if (tm_clone_hash)
5929 struct tree_map *h, in;
5931 in.base.from = o;
5932 in.hash = htab_hash_pointer (o);
5933 h = (struct tree_map *) htab_find_with_hash (tm_clone_hash,
5934 &in, in.hash);
5935 if (h)
5936 return h->to;
5938 return NULL_TREE;
5941 typedef struct tm_alias_pair
5943 unsigned int uid;
5944 tree from;
5945 tree to;
5946 } tm_alias_pair;
5948 DEF_VEC_O(tm_alias_pair);
5949 DEF_VEC_ALLOC_O(tm_alias_pair,heap);
5951 /* Helper function for finish_tm_clone_pairs. Dump a hash table entry
5952 into a VEC in INFO. */
5954 static int
5955 dump_tm_clone_to_vec (void **slot, void *info)
5957 struct tree_map *map = (struct tree_map *) *slot;
5958 VEC(tm_alias_pair,heap) **tm_alias_pairs
5959 = (VEC(tm_alias_pair, heap) **) info;
5960 tm_alias_pair *p;
5962 p = VEC_safe_push (tm_alias_pair, heap, *tm_alias_pairs, NULL);
5963 p->from = map->base.from;
5964 p->to = map->to;
5965 p->uid = DECL_UID (p->from);
5966 return 1;
5969 /* Dump the actual pairs to the .tm_clone_table section. */
5971 static void
5972 dump_tm_clone_pairs (VEC(tm_alias_pair,heap) *tm_alias_pairs)
5974 unsigned i;
5975 tm_alias_pair *p;
5976 bool switched = false;
5978 FOR_EACH_VEC_ELT (tm_alias_pair, tm_alias_pairs, i, p)
5980 tree src = p->from;
5981 tree dst = p->to;
5982 struct cgraph_node *src_n = cgraph_get_node (src);
5983 struct cgraph_node *dst_n = cgraph_get_node (dst);
5985 /* The function ipa_tm_create_version() marks the clone as needed if
5986 the original function was needed. But we also mark the clone as
5987 needed if we ever called the clone indirectly through
5988 TM_GETTMCLONE. If neither of these are true, we didn't generate
5989 a clone, and we didn't call it indirectly... no sense keeping it
5990 in the clone table. */
5991 if (!dst_n || !dst_n->needed)
5992 continue;
5994 /* This covers the case where we have optimized the original
5995 function away, and only access the transactional clone. */
5996 if (!src_n || !src_n->needed)
5997 continue;
5999 if (!switched)
6001 switch_to_section (targetm.asm_out.tm_clone_table_section ());
6002 assemble_align (POINTER_SIZE);
6003 switched = true;
6006 assemble_integer (XEXP (DECL_RTL (src), 0),
6007 POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
6008 assemble_integer (XEXP (DECL_RTL (dst), 0),
6009 POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
6013 /* Provide a default for the tm_clone_table section. */
6015 section *
6016 default_clone_table_section (void)
6018 return get_named_section (NULL, ".tm_clone_table", 3);
6021 /* Helper comparison function for qsorting by the DECL_UID stored in
6022 alias_pair->emitted_diags. */
6024 static int
6025 tm_alias_pair_cmp (const void *x, const void *y)
6027 const tm_alias_pair *p1 = (const tm_alias_pair *) x;
6028 const tm_alias_pair *p2 = (const tm_alias_pair *) y;
6029 if (p1->uid < p2->uid)
6030 return -1;
6031 if (p1->uid > p2->uid)
6032 return 1;
6033 return 0;
6036 void
6037 finish_tm_clone_pairs (void)
6039 VEC(tm_alias_pair,heap) *tm_alias_pairs = NULL;
6041 if (tm_clone_hash == NULL)
6042 return;
6044 /* We need a determenistic order for the .tm_clone_table, otherwise
6045 we will get bootstrap comparison failures, so dump the hash table
6046 to a vector, sort it, and dump the vector. */
6048 /* Dump the hashtable to a vector. */
6049 htab_traverse_noresize (tm_clone_hash, dump_tm_clone_to_vec,
6050 (void *) &tm_alias_pairs);
6051 /* Sort it. */
6052 VEC_qsort (tm_alias_pair, tm_alias_pairs, tm_alias_pair_cmp);
6054 /* Dump it. */
6055 dump_tm_clone_pairs (tm_alias_pairs);
6057 htab_delete (tm_clone_hash);
6058 tm_clone_hash = NULL;
6059 VEC_free (tm_alias_pair, heap, tm_alias_pairs);
6063 /* Emit an assembler directive to set symbol for DECL visibility to
6064 the visibility type VIS, which must not be VISIBILITY_DEFAULT. */
6066 void
6067 default_assemble_visibility (tree decl ATTRIBUTE_UNUSED,
6068 int vis ATTRIBUTE_UNUSED)
6070 #ifdef HAVE_GAS_HIDDEN
6071 static const char * const visibility_types[] = {
6072 NULL, "protected", "hidden", "internal"
6075 const char *name, *type;
6077 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
6078 type = visibility_types[vis];
6080 fprintf (asm_out_file, "\t.%s\t", type);
6081 assemble_name (asm_out_file, name);
6082 fprintf (asm_out_file, "\n");
6083 #else
6084 warning (OPT_Wattributes, "visibility attribute not supported "
6085 "in this configuration; ignored");
6086 #endif
6089 /* A helper function to call assemble_visibility when needed for a decl. */
6092 maybe_assemble_visibility (tree decl)
6094 enum symbol_visibility vis = DECL_VISIBILITY (decl);
6096 if (vis != VISIBILITY_DEFAULT)
6098 targetm.asm_out.assemble_visibility (decl, vis);
6099 return 1;
6101 else
6102 return 0;
6105 /* Returns 1 if the target configuration supports defining public symbols
6106 so that one of them will be chosen at link time instead of generating a
6107 multiply-defined symbol error, whether through the use of weak symbols or
6108 a target-specific mechanism for having duplicates discarded. */
6111 supports_one_only (void)
6113 if (SUPPORTS_ONE_ONLY)
6114 return 1;
6115 return TARGET_SUPPORTS_WEAK;
6118 /* Set up DECL as a public symbol that can be defined in multiple
6119 translation units without generating a linker error. */
6121 void
6122 make_decl_one_only (tree decl, tree comdat_group)
6124 gcc_assert (TREE_CODE (decl) == VAR_DECL
6125 || TREE_CODE (decl) == FUNCTION_DECL);
6127 TREE_PUBLIC (decl) = 1;
6129 if (SUPPORTS_ONE_ONLY)
6131 #ifdef MAKE_DECL_ONE_ONLY
6132 MAKE_DECL_ONE_ONLY (decl);
6133 #endif
6134 DECL_COMDAT_GROUP (decl) = comdat_group;
6136 else if (TREE_CODE (decl) == VAR_DECL
6137 && (DECL_INITIAL (decl) == 0 || DECL_INITIAL (decl) == error_mark_node))
6138 DECL_COMMON (decl) = 1;
6139 else
6141 gcc_assert (TARGET_SUPPORTS_WEAK);
6142 DECL_WEAK (decl) = 1;
6146 void
6147 init_varasm_once (void)
6149 section_htab = htab_create_ggc (31, section_entry_hash,
6150 section_entry_eq, NULL);
6151 object_block_htab = htab_create_ggc (31, object_block_entry_hash,
6152 object_block_entry_eq, NULL);
6153 const_desc_htab = htab_create_ggc (1009, const_desc_hash,
6154 const_desc_eq, NULL);
6156 const_alias_set = new_alias_set ();
6157 shared_constant_pool = create_constant_pool ();
6159 #ifdef TEXT_SECTION_ASM_OP
6160 text_section = get_unnamed_section (SECTION_CODE, output_section_asm_op,
6161 TEXT_SECTION_ASM_OP);
6162 #endif
6164 #ifdef DATA_SECTION_ASM_OP
6165 data_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
6166 DATA_SECTION_ASM_OP);
6167 #endif
6169 #ifdef SDATA_SECTION_ASM_OP
6170 sdata_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
6171 SDATA_SECTION_ASM_OP);
6172 #endif
6174 #ifdef READONLY_DATA_SECTION_ASM_OP
6175 readonly_data_section = get_unnamed_section (0, output_section_asm_op,
6176 READONLY_DATA_SECTION_ASM_OP);
6177 #endif
6179 #ifdef CTORS_SECTION_ASM_OP
6180 ctors_section = get_unnamed_section (0, output_section_asm_op,
6181 CTORS_SECTION_ASM_OP);
6182 #endif
6184 #ifdef DTORS_SECTION_ASM_OP
6185 dtors_section = get_unnamed_section (0, output_section_asm_op,
6186 DTORS_SECTION_ASM_OP);
6187 #endif
6189 #ifdef BSS_SECTION_ASM_OP
6190 bss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
6191 output_section_asm_op,
6192 BSS_SECTION_ASM_OP);
6193 #endif
6195 #ifdef SBSS_SECTION_ASM_OP
6196 sbss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
6197 output_section_asm_op,
6198 SBSS_SECTION_ASM_OP);
6199 #endif
6201 tls_comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6202 | SECTION_COMMON, emit_tls_common);
6203 lcomm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6204 | SECTION_COMMON, emit_local);
6205 comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6206 | SECTION_COMMON, emit_common);
6208 #if defined ASM_OUTPUT_ALIGNED_BSS
6209 bss_noswitch_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS,
6210 emit_bss);
6211 #endif
6213 targetm.asm_out.init_sections ();
6215 if (readonly_data_section == NULL)
6216 readonly_data_section = text_section;
6218 #ifdef ASM_OUTPUT_EXTERNAL
6219 pending_assemble_externals_set = pointer_set_create ();
6220 #endif
6223 enum tls_model
6224 decl_default_tls_model (const_tree decl)
6226 enum tls_model kind;
6227 bool is_local;
6229 is_local = targetm.binds_local_p (decl);
6230 if (!flag_shlib)
6232 if (is_local)
6233 kind = TLS_MODEL_LOCAL_EXEC;
6234 else
6235 kind = TLS_MODEL_INITIAL_EXEC;
6238 /* Local dynamic is inefficient when we're not combining the
6239 parts of the address. */
6240 else if (optimize && is_local)
6241 kind = TLS_MODEL_LOCAL_DYNAMIC;
6242 else
6243 kind = TLS_MODEL_GLOBAL_DYNAMIC;
6244 if (kind < flag_tls_default)
6245 kind = flag_tls_default;
6247 return kind;
6250 /* Select a set of attributes for section NAME based on the properties
6251 of DECL and whether or not RELOC indicates that DECL's initializer
6252 might contain runtime relocations.
6254 We make the section read-only and executable for a function decl,
6255 read-only for a const data decl, and writable for a non-const data decl. */
6257 unsigned int
6258 default_section_type_flags (tree decl, const char *name, int reloc)
6260 unsigned int flags;
6262 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
6263 flags = SECTION_CODE;
6264 else if (decl)
6266 enum section_category category
6267 = categorize_decl_for_section (decl, reloc);
6268 if (decl_readonly_section_1 (category))
6269 flags = 0;
6270 else if (category == SECCAT_DATA_REL_RO
6271 || category == SECCAT_DATA_REL_RO_LOCAL)
6272 flags = SECTION_WRITE | SECTION_RELRO;
6273 else
6274 flags = SECTION_WRITE;
6276 else
6278 flags = SECTION_WRITE;
6279 if (strcmp (name, ".data.rel.ro") == 0
6280 || strcmp (name, ".data.rel.ro.local") == 0)
6281 flags |= SECTION_RELRO;
6284 if (decl && DECL_ONE_ONLY (decl))
6285 flags |= SECTION_LINKONCE;
6287 if (decl && TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
6288 flags |= SECTION_TLS | SECTION_WRITE;
6290 if (strcmp (name, ".bss") == 0
6291 || strncmp (name, ".bss.", 5) == 0
6292 || strncmp (name, ".gnu.linkonce.b.", 16) == 0
6293 || strcmp (name, ".sbss") == 0
6294 || strncmp (name, ".sbss.", 6) == 0
6295 || strncmp (name, ".gnu.linkonce.sb.", 17) == 0)
6296 flags |= SECTION_BSS;
6298 if (strcmp (name, ".tdata") == 0
6299 || strncmp (name, ".tdata.", 7) == 0
6300 || strncmp (name, ".gnu.linkonce.td.", 17) == 0)
6301 flags |= SECTION_TLS;
6303 if (strcmp (name, ".tbss") == 0
6304 || strncmp (name, ".tbss.", 6) == 0
6305 || strncmp (name, ".gnu.linkonce.tb.", 17) == 0)
6306 flags |= SECTION_TLS | SECTION_BSS;
6308 /* These three sections have special ELF types. They are neither
6309 SHT_PROGBITS nor SHT_NOBITS, so when changing sections we don't
6310 want to print a section type (@progbits or @nobits). If someone
6311 is silly enough to emit code or TLS variables to one of these
6312 sections, then don't handle them specially. */
6313 if (!(flags & (SECTION_CODE | SECTION_BSS | SECTION_TLS))
6314 && (strcmp (name, ".init_array") == 0
6315 || strcmp (name, ".fini_array") == 0
6316 || strcmp (name, ".preinit_array") == 0))
6317 flags |= SECTION_NOTYPE;
6319 return flags;
6322 /* Return true if the target supports some form of global BSS,
6323 either through bss_noswitch_section, or by selecting a BSS
6324 section in TARGET_ASM_SELECT_SECTION. */
6326 bool
6327 have_global_bss_p (void)
6329 return bss_noswitch_section || targetm.have_switchable_bss_sections;
6332 /* Output assembly to switch to section NAME with attribute FLAGS.
6333 Four variants for common object file formats. */
6335 void
6336 default_no_named_section (const char *name ATTRIBUTE_UNUSED,
6337 unsigned int flags ATTRIBUTE_UNUSED,
6338 tree decl ATTRIBUTE_UNUSED)
6340 /* Some object formats don't support named sections at all. The
6341 front-end should already have flagged this as an error. */
6342 gcc_unreachable ();
6345 #ifndef TLS_SECTION_ASM_FLAG
6346 #define TLS_SECTION_ASM_FLAG 'T'
6347 #endif
6349 void
6350 default_elf_asm_named_section (const char *name, unsigned int flags,
6351 tree decl ATTRIBUTE_UNUSED)
6353 char flagchars[10], *f = flagchars;
6355 /* If we have already declared this section, we can use an
6356 abbreviated form to switch back to it -- unless this section is
6357 part of a COMDAT groups, in which case GAS requires the full
6358 declaration every time. */
6359 if (!(HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6360 && (flags & SECTION_DECLARED))
6362 fprintf (asm_out_file, "\t.section\t%s\n", name);
6363 return;
6366 if (!(flags & SECTION_DEBUG))
6367 *f++ = 'a';
6368 if (flags & SECTION_EXCLUDE)
6369 *f++ = 'e';
6370 if (flags & SECTION_WRITE)
6371 *f++ = 'w';
6372 if (flags & SECTION_CODE)
6373 *f++ = 'x';
6374 if (flags & SECTION_SMALL)
6375 *f++ = 's';
6376 if (flags & SECTION_MERGE)
6377 *f++ = 'M';
6378 if (flags & SECTION_STRINGS)
6379 *f++ = 'S';
6380 if (flags & SECTION_TLS)
6381 *f++ = TLS_SECTION_ASM_FLAG;
6382 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6383 *f++ = 'G';
6384 *f = '\0';
6386 fprintf (asm_out_file, "\t.section\t%s,\"%s\"", name, flagchars);
6388 if (!(flags & SECTION_NOTYPE))
6390 const char *type;
6391 const char *format;
6393 if (flags & SECTION_BSS)
6394 type = "nobits";
6395 else
6396 type = "progbits";
6398 format = ",@%s";
6399 /* On platforms that use "@" as the assembly comment character,
6400 use "%" instead. */
6401 if (strcmp (ASM_COMMENT_START, "@") == 0)
6402 format = ",%%%s";
6403 fprintf (asm_out_file, format, type);
6405 if (flags & SECTION_ENTSIZE)
6406 fprintf (asm_out_file, ",%d", flags & SECTION_ENTSIZE);
6407 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6409 if (TREE_CODE (decl) == IDENTIFIER_NODE)
6410 fprintf (asm_out_file, ",%s,comdat", IDENTIFIER_POINTER (decl));
6411 else
6412 fprintf (asm_out_file, ",%s,comdat",
6413 IDENTIFIER_POINTER (DECL_COMDAT_GROUP (decl)));
6417 putc ('\n', asm_out_file);
6420 void
6421 default_coff_asm_named_section (const char *name, unsigned int flags,
6422 tree decl ATTRIBUTE_UNUSED)
6424 char flagchars[8], *f = flagchars;
6426 if (flags & SECTION_WRITE)
6427 *f++ = 'w';
6428 if (flags & SECTION_CODE)
6429 *f++ = 'x';
6430 *f = '\0';
6432 fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
6435 void
6436 default_pe_asm_named_section (const char *name, unsigned int flags,
6437 tree decl)
6439 default_coff_asm_named_section (name, flags, decl);
6441 if (flags & SECTION_LINKONCE)
6443 /* Functions may have been compiled at various levels of
6444 optimization so we can't use `same_size' here.
6445 Instead, have the linker pick one. */
6446 fprintf (asm_out_file, "\t.linkonce %s\n",
6447 (flags & SECTION_CODE ? "discard" : "same_size"));
6451 /* The lame default section selector. */
6453 section *
6454 default_select_section (tree decl, int reloc,
6455 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
6457 if (DECL_P (decl))
6459 if (decl_readonly_section (decl, reloc))
6460 return readonly_data_section;
6462 else if (TREE_CODE (decl) == CONSTRUCTOR)
6464 if (! ((flag_pic && reloc)
6465 || !TREE_READONLY (decl)
6466 || TREE_SIDE_EFFECTS (decl)
6467 || !TREE_CONSTANT (decl)))
6468 return readonly_data_section;
6470 else if (TREE_CODE (decl) == STRING_CST)
6471 return readonly_data_section;
6472 else if (! (flag_pic && reloc))
6473 return readonly_data_section;
6475 return data_section;
6478 enum section_category
6479 categorize_decl_for_section (const_tree decl, int reloc)
6481 enum section_category ret;
6483 if (TREE_CODE (decl) == FUNCTION_DECL)
6484 return SECCAT_TEXT;
6485 else if (TREE_CODE (decl) == STRING_CST)
6487 if (flag_mudflap) /* or !flag_merge_constants */
6488 return SECCAT_RODATA;
6489 else
6490 return SECCAT_RODATA_MERGE_STR;
6492 else if (TREE_CODE (decl) == VAR_DECL)
6494 if (bss_initializer_p (decl))
6495 ret = SECCAT_BSS;
6496 else if (! TREE_READONLY (decl)
6497 || TREE_SIDE_EFFECTS (decl)
6498 || ! TREE_CONSTANT (DECL_INITIAL (decl)))
6500 /* Here the reloc_rw_mask is not testing whether the section should
6501 be read-only or not, but whether the dynamic link will have to
6502 do something. If so, we wish to segregate the data in order to
6503 minimize cache misses inside the dynamic linker. */
6504 if (reloc & targetm.asm_out.reloc_rw_mask ())
6505 ret = reloc == 1 ? SECCAT_DATA_REL_LOCAL : SECCAT_DATA_REL;
6506 else
6507 ret = SECCAT_DATA;
6509 else if (reloc & targetm.asm_out.reloc_rw_mask ())
6510 ret = reloc == 1 ? SECCAT_DATA_REL_RO_LOCAL : SECCAT_DATA_REL_RO;
6511 else if (reloc || flag_merge_constants < 2)
6512 /* C and C++ don't allow different variables to share the same
6513 location. -fmerge-all-constants allows even that (at the
6514 expense of not conforming). */
6515 ret = SECCAT_RODATA;
6516 else if (TREE_CODE (DECL_INITIAL (decl)) == STRING_CST)
6517 ret = SECCAT_RODATA_MERGE_STR_INIT;
6518 else
6519 ret = SECCAT_RODATA_MERGE_CONST;
6521 else if (TREE_CODE (decl) == CONSTRUCTOR)
6523 if ((reloc & targetm.asm_out.reloc_rw_mask ())
6524 || TREE_SIDE_EFFECTS (decl)
6525 || ! TREE_CONSTANT (decl))
6526 ret = SECCAT_DATA;
6527 else
6528 ret = SECCAT_RODATA;
6530 else
6531 ret = SECCAT_RODATA;
6533 /* There are no read-only thread-local sections. */
6534 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
6536 /* Note that this would be *just* SECCAT_BSS, except that there's
6537 no concept of a read-only thread-local-data section. */
6538 if (ret == SECCAT_BSS
6539 || (flag_zero_initialized_in_bss
6540 && initializer_zerop (DECL_INITIAL (decl))))
6541 ret = SECCAT_TBSS;
6542 else
6543 ret = SECCAT_TDATA;
6546 /* If the target uses small data sections, select it. */
6547 else if (targetm.in_small_data_p (decl))
6549 if (ret == SECCAT_BSS)
6550 ret = SECCAT_SBSS;
6551 else if (targetm.have_srodata_section && ret == SECCAT_RODATA)
6552 ret = SECCAT_SRODATA;
6553 else
6554 ret = SECCAT_SDATA;
6557 return ret;
6560 static bool
6561 decl_readonly_section_1 (enum section_category category)
6563 switch (category)
6565 case SECCAT_RODATA:
6566 case SECCAT_RODATA_MERGE_STR:
6567 case SECCAT_RODATA_MERGE_STR_INIT:
6568 case SECCAT_RODATA_MERGE_CONST:
6569 case SECCAT_SRODATA:
6570 return true;
6571 default:
6572 return false;
6576 bool
6577 decl_readonly_section (const_tree decl, int reloc)
6579 return decl_readonly_section_1 (categorize_decl_for_section (decl, reloc));
6582 /* Select a section based on the above categorization. */
6584 section *
6585 default_elf_select_section (tree decl, int reloc,
6586 unsigned HOST_WIDE_INT align)
6588 const char *sname;
6589 switch (categorize_decl_for_section (decl, reloc))
6591 case SECCAT_TEXT:
6592 /* We're not supposed to be called on FUNCTION_DECLs. */
6593 gcc_unreachable ();
6594 case SECCAT_RODATA:
6595 return readonly_data_section;
6596 case SECCAT_RODATA_MERGE_STR:
6597 return mergeable_string_section (decl, align, 0);
6598 case SECCAT_RODATA_MERGE_STR_INIT:
6599 return mergeable_string_section (DECL_INITIAL (decl), align, 0);
6600 case SECCAT_RODATA_MERGE_CONST:
6601 return mergeable_constant_section (DECL_MODE (decl), align, 0);
6602 case SECCAT_SRODATA:
6603 sname = ".sdata2";
6604 break;
6605 case SECCAT_DATA:
6606 return data_section;
6607 case SECCAT_DATA_REL:
6608 sname = ".data.rel";
6609 break;
6610 case SECCAT_DATA_REL_LOCAL:
6611 sname = ".data.rel.local";
6612 break;
6613 case SECCAT_DATA_REL_RO:
6614 sname = ".data.rel.ro";
6615 break;
6616 case SECCAT_DATA_REL_RO_LOCAL:
6617 sname = ".data.rel.ro.local";
6618 break;
6619 case SECCAT_SDATA:
6620 sname = ".sdata";
6621 break;
6622 case SECCAT_TDATA:
6623 sname = ".tdata";
6624 break;
6625 case SECCAT_BSS:
6626 if (bss_section)
6627 return bss_section;
6628 sname = ".bss";
6629 break;
6630 case SECCAT_SBSS:
6631 sname = ".sbss";
6632 break;
6633 case SECCAT_TBSS:
6634 sname = ".tbss";
6635 break;
6636 default:
6637 gcc_unreachable ();
6640 if (!DECL_P (decl))
6641 decl = NULL_TREE;
6642 return get_named_section (decl, sname, reloc);
6645 /* Construct a unique section name based on the decl name and the
6646 categorization performed above. */
6648 void
6649 default_unique_section (tree decl, int reloc)
6651 /* We only need to use .gnu.linkonce if we don't have COMDAT groups. */
6652 bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
6653 const char *prefix, *name, *linkonce;
6654 char *string;
6656 switch (categorize_decl_for_section (decl, reloc))
6658 case SECCAT_TEXT:
6659 prefix = one_only ? ".t" : ".text";
6660 break;
6661 case SECCAT_RODATA:
6662 case SECCAT_RODATA_MERGE_STR:
6663 case SECCAT_RODATA_MERGE_STR_INIT:
6664 case SECCAT_RODATA_MERGE_CONST:
6665 prefix = one_only ? ".r" : ".rodata";
6666 break;
6667 case SECCAT_SRODATA:
6668 prefix = one_only ? ".s2" : ".sdata2";
6669 break;
6670 case SECCAT_DATA:
6671 prefix = one_only ? ".d" : ".data";
6672 break;
6673 case SECCAT_DATA_REL:
6674 prefix = one_only ? ".d.rel" : ".data.rel";
6675 break;
6676 case SECCAT_DATA_REL_LOCAL:
6677 prefix = one_only ? ".d.rel.local" : ".data.rel.local";
6678 break;
6679 case SECCAT_DATA_REL_RO:
6680 prefix = one_only ? ".d.rel.ro" : ".data.rel.ro";
6681 break;
6682 case SECCAT_DATA_REL_RO_LOCAL:
6683 prefix = one_only ? ".d.rel.ro.local" : ".data.rel.ro.local";
6684 break;
6685 case SECCAT_SDATA:
6686 prefix = one_only ? ".s" : ".sdata";
6687 break;
6688 case SECCAT_BSS:
6689 prefix = one_only ? ".b" : ".bss";
6690 break;
6691 case SECCAT_SBSS:
6692 prefix = one_only ? ".sb" : ".sbss";
6693 break;
6694 case SECCAT_TDATA:
6695 prefix = one_only ? ".td" : ".tdata";
6696 break;
6697 case SECCAT_TBSS:
6698 prefix = one_only ? ".tb" : ".tbss";
6699 break;
6700 default:
6701 gcc_unreachable ();
6704 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
6705 name = targetm.strip_name_encoding (name);
6707 /* If we're using one_only, then there needs to be a .gnu.linkonce
6708 prefix to the section name. */
6709 linkonce = one_only ? ".gnu.linkonce" : "";
6711 string = ACONCAT ((linkonce, prefix, ".", name, NULL));
6713 DECL_SECTION_NAME (decl) = build_string (strlen (string), string);
6716 /* Like compute_reloc_for_constant, except for an RTX. The return value
6717 is a mask for which bit 1 indicates a global relocation, and bit 0
6718 indicates a local relocation. */
6720 static int
6721 compute_reloc_for_rtx_1 (rtx *xp, void *data)
6723 int *preloc = (int *) data;
6724 rtx x = *xp;
6726 switch (GET_CODE (x))
6728 case SYMBOL_REF:
6729 *preloc |= SYMBOL_REF_LOCAL_P (x) ? 1 : 2;
6730 break;
6731 case LABEL_REF:
6732 *preloc |= 1;
6733 break;
6734 default:
6735 break;
6738 return 0;
6741 static int
6742 compute_reloc_for_rtx (rtx x)
6744 int reloc;
6746 switch (GET_CODE (x))
6748 case CONST:
6749 case SYMBOL_REF:
6750 case LABEL_REF:
6751 reloc = 0;
6752 for_each_rtx (&x, compute_reloc_for_rtx_1, &reloc);
6753 return reloc;
6755 default:
6756 return 0;
6760 section *
6761 default_select_rtx_section (enum machine_mode mode ATTRIBUTE_UNUSED,
6762 rtx x,
6763 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
6765 if (compute_reloc_for_rtx (x) & targetm.asm_out.reloc_rw_mask ())
6766 return data_section;
6767 else
6768 return readonly_data_section;
6771 section *
6772 default_elf_select_rtx_section (enum machine_mode mode, rtx x,
6773 unsigned HOST_WIDE_INT align)
6775 int reloc = compute_reloc_for_rtx (x);
6777 /* ??? Handle small data here somehow. */
6779 if (reloc & targetm.asm_out.reloc_rw_mask ())
6781 if (reloc == 1)
6782 return get_named_section (NULL, ".data.rel.ro.local", 1);
6783 else
6784 return get_named_section (NULL, ".data.rel.ro", 3);
6787 return mergeable_constant_section (mode, align, 0);
6790 /* Set the generally applicable flags on the SYMBOL_REF for EXP. */
6792 void
6793 default_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
6795 rtx symbol;
6796 int flags;
6798 /* Careful not to prod global register variables. */
6799 if (!MEM_P (rtl))
6800 return;
6801 symbol = XEXP (rtl, 0);
6802 if (GET_CODE (symbol) != SYMBOL_REF)
6803 return;
6805 flags = SYMBOL_REF_FLAGS (symbol) & SYMBOL_FLAG_HAS_BLOCK_INFO;
6806 if (TREE_CODE (decl) == FUNCTION_DECL)
6807 flags |= SYMBOL_FLAG_FUNCTION;
6808 if (targetm.binds_local_p (decl))
6809 flags |= SYMBOL_FLAG_LOCAL;
6810 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
6811 flags |= DECL_TLS_MODEL (decl) << SYMBOL_FLAG_TLS_SHIFT;
6812 else if (targetm.in_small_data_p (decl))
6813 flags |= SYMBOL_FLAG_SMALL;
6814 /* ??? Why is DECL_EXTERNAL ever set for non-PUBLIC names? Without
6815 being PUBLIC, the thing *must* be defined in this translation unit.
6816 Prevent this buglet from being propagated into rtl code as well. */
6817 if (DECL_P (decl) && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl))
6818 flags |= SYMBOL_FLAG_EXTERNAL;
6820 SYMBOL_REF_FLAGS (symbol) = flags;
6823 /* By default, we do nothing for encode_section_info, so we need not
6824 do anything but discard the '*' marker. */
6826 const char *
6827 default_strip_name_encoding (const char *str)
6829 return str + (*str == '*');
6832 #ifdef ASM_OUTPUT_DEF
6833 /* The default implementation of TARGET_ASM_OUTPUT_ANCHOR. Define the
6834 anchor relative to ".", the current section position. */
6836 void
6837 default_asm_output_anchor (rtx symbol)
6839 char buffer[100];
6841 sprintf (buffer, "*. + " HOST_WIDE_INT_PRINT_DEC,
6842 SYMBOL_REF_BLOCK_OFFSET (symbol));
6843 ASM_OUTPUT_DEF (asm_out_file, XSTR (symbol, 0), buffer);
6845 #endif
6847 /* The default implementation of TARGET_USE_ANCHORS_FOR_SYMBOL_P. */
6849 bool
6850 default_use_anchors_for_symbol_p (const_rtx symbol)
6852 section *sect;
6853 tree decl;
6855 /* Don't use anchors for mergeable sections. The linker might move
6856 the objects around. */
6857 sect = SYMBOL_REF_BLOCK (symbol)->sect;
6858 if (sect->common.flags & SECTION_MERGE)
6859 return false;
6861 /* Don't use anchors for small data sections. The small data register
6862 acts as an anchor for such sections. */
6863 if (sect->common.flags & SECTION_SMALL)
6864 return false;
6866 decl = SYMBOL_REF_DECL (symbol);
6867 if (decl && DECL_P (decl))
6869 /* Don't use section anchors for decls that might be defined by
6870 other modules. */
6871 if (!targetm.binds_local_p (decl))
6872 return false;
6874 /* Don't use section anchors for decls that will be placed in a
6875 small data section. */
6876 /* ??? Ideally, this check would be redundant with the SECTION_SMALL
6877 one above. The problem is that we only use SECTION_SMALL for
6878 sections that should be marked as small in the section directive. */
6879 if (targetm.in_small_data_p (decl))
6880 return false;
6882 return true;
6885 /* Return true when RESOLUTION indicate that symbol will be bound to the
6886 definition provided by current .o file. */
6888 static bool
6889 resolution_to_local_definition_p (enum ld_plugin_symbol_resolution resolution)
6891 return (resolution == LDPR_PREVAILING_DEF
6892 || resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
6893 || resolution == LDPR_PREVAILING_DEF_IRONLY);
6896 /* Return true when RESOLUTION indicate that symbol will be bound locally
6897 within current executable or DSO. */
6899 static bool
6900 resolution_local_p (enum ld_plugin_symbol_resolution resolution)
6902 return (resolution == LDPR_PREVAILING_DEF
6903 || resolution == LDPR_PREVAILING_DEF_IRONLY
6904 || resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
6905 || resolution == LDPR_PREEMPTED_REG
6906 || resolution == LDPR_PREEMPTED_IR
6907 || resolution == LDPR_RESOLVED_IR
6908 || resolution == LDPR_RESOLVED_EXEC);
6911 /* Assume ELF-ish defaults, since that's pretty much the most liberal
6912 wrt cross-module name binding. */
6914 bool
6915 default_binds_local_p (const_tree exp)
6917 return default_binds_local_p_1 (exp, flag_shlib);
6920 bool
6921 default_binds_local_p_1 (const_tree exp, int shlib)
6923 bool local_p;
6924 bool resolved_locally = false;
6925 bool resolved_to_local_def = false;
6927 /* With resolution file in hands, take look into resolutions.
6928 We can't just return true for resolved_locally symbols,
6929 because dynamic linking might overwrite symbols
6930 in shared libraries. */
6931 if (TREE_CODE (exp) == VAR_DECL && TREE_PUBLIC (exp)
6932 && (TREE_STATIC (exp) || DECL_EXTERNAL (exp)))
6934 struct varpool_node *vnode = varpool_get_node (exp);
6935 if (vnode && resolution_local_p (vnode->resolution))
6936 resolved_locally = true;
6937 if (vnode
6938 && resolution_to_local_definition_p (vnode->resolution))
6939 resolved_to_local_def = true;
6941 else if (TREE_CODE (exp) == FUNCTION_DECL && TREE_PUBLIC (exp))
6943 struct cgraph_node *node = cgraph_get_node (exp);
6944 if (node
6945 && resolution_local_p (node->resolution))
6946 resolved_locally = true;
6947 if (node
6948 && resolution_to_local_definition_p (node->resolution))
6949 resolved_to_local_def = true;
6952 /* A non-decl is an entry in the constant pool. */
6953 if (!DECL_P (exp))
6954 local_p = true;
6955 /* Weakrefs may not bind locally, even though the weakref itself is always
6956 static and therefore local. Similarly, the resolver for ifunc functions
6957 might resolve to a non-local function.
6958 FIXME: We can resolve the weakref case more curefuly by looking at the
6959 weakref alias. */
6960 else if (lookup_attribute ("weakref", DECL_ATTRIBUTES (exp))
6961 || (TREE_CODE (exp) == FUNCTION_DECL
6962 && lookup_attribute ("ifunc", DECL_ATTRIBUTES (exp))))
6963 local_p = false;
6964 /* Static variables are always local. */
6965 else if (! TREE_PUBLIC (exp))
6966 local_p = true;
6967 /* A variable is local if the user has said explicitly that it will
6968 be. */
6969 else if ((DECL_VISIBILITY_SPECIFIED (exp)
6970 || resolved_to_local_def)
6971 && DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
6972 local_p = true;
6973 /* Variables defined outside this object might not be local. */
6974 else if (DECL_EXTERNAL (exp) && !resolved_locally)
6975 local_p = false;
6976 /* If defined in this object and visibility is not default, must be
6977 local. */
6978 else if (DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
6979 local_p = true;
6980 /* Default visibility weak data can be overridden by a strong symbol
6981 in another module and so are not local. */
6982 else if (DECL_WEAK (exp)
6983 && !resolved_locally)
6984 local_p = false;
6985 /* If PIC, then assume that any global name can be overridden by
6986 symbols resolved from other modules. */
6987 else if (shlib)
6988 local_p = false;
6989 /* Uninitialized COMMON variable may be unified with symbols
6990 resolved from other modules. */
6991 else if (DECL_COMMON (exp)
6992 && !resolved_locally
6993 && (DECL_INITIAL (exp) == NULL
6994 || DECL_INITIAL (exp) == error_mark_node))
6995 local_p = false;
6996 /* Otherwise we're left with initialized (or non-common) global data
6997 which is of necessity defined locally. */
6998 else
6999 local_p = true;
7001 return local_p;
7004 /* Return true when references to DECL must bind to current definition in
7005 final executable.
7007 The condition is usually equivalent to whether the function binds to the
7008 current module (shared library or executable), that is to binds_local_p.
7009 We use this fact to avoid need for another target hook and implement
7010 the logic using binds_local_p and just special cases where
7011 decl_binds_to_current_def_p is stronger than binds_local_p. In particular
7012 the weak definitions (that can be overwritten at linktime by other
7013 definition from different object file) and when resolution info is available
7014 we simply use the knowledge passed to us by linker plugin. */
7015 bool
7016 decl_binds_to_current_def_p (tree decl)
7018 gcc_assert (DECL_P (decl));
7019 if (!TREE_PUBLIC (decl))
7020 return true;
7021 if (!targetm.binds_local_p (decl))
7022 return false;
7023 /* When resolution is available, just use it. */
7024 if (TREE_CODE (decl) == VAR_DECL
7025 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
7027 struct varpool_node *vnode = varpool_get_node (decl);
7028 if (vnode
7029 && vnode->resolution != LDPR_UNKNOWN)
7030 return resolution_to_local_definition_p (vnode->resolution);
7032 else if (TREE_CODE (decl) == FUNCTION_DECL)
7034 struct cgraph_node *node = cgraph_get_node (decl);
7035 if (node
7036 && node->resolution != LDPR_UNKNOWN)
7037 return resolution_to_local_definition_p (node->resolution);
7039 /* Otherwise we have to assume the worst for DECL_WEAK (hidden weaks
7040 binds locally but still can be overwritten).
7041 This rely on fact that binds_local_p behave as decl_replaceable_p
7042 for all other declaration types. */
7043 return !DECL_WEAK (decl);
7046 /* A replaceable function or variable is one which may be replaced
7047 at link-time with an entirely different definition, provided that the
7048 replacement has the same type. For example, functions declared
7049 with __attribute__((weak)) on most systems are replaceable.
7051 COMDAT functions are not replaceable, since all definitions of the
7052 function must be equivalent. It is important that COMDAT functions
7053 not be treated as replaceable so that use of C++ template
7054 instantiations is not penalized. */
7056 bool
7057 decl_replaceable_p (tree decl)
7059 gcc_assert (DECL_P (decl));
7060 if (!TREE_PUBLIC (decl) || DECL_COMDAT (decl))
7061 return false;
7062 return !decl_binds_to_current_def_p (decl);
7065 /* Default function to output code that will globalize a label. A
7066 target must define GLOBAL_ASM_OP or provide its own function to
7067 globalize a label. */
7068 #ifdef GLOBAL_ASM_OP
7069 void
7070 default_globalize_label (FILE * stream, const char *name)
7072 fputs (GLOBAL_ASM_OP, stream);
7073 assemble_name (stream, name);
7074 putc ('\n', stream);
7076 #endif /* GLOBAL_ASM_OP */
7078 /* Default function to output code that will globalize a declaration. */
7079 void
7080 default_globalize_decl_name (FILE * stream, tree decl)
7082 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
7083 targetm.asm_out.globalize_label (stream, name);
7086 /* Default function to output a label for unwind information. The
7087 default is to do nothing. A target that needs nonlocal labels for
7088 unwind information must provide its own function to do this. */
7089 void
7090 default_emit_unwind_label (FILE * stream ATTRIBUTE_UNUSED,
7091 tree decl ATTRIBUTE_UNUSED,
7092 int for_eh ATTRIBUTE_UNUSED,
7093 int empty ATTRIBUTE_UNUSED)
7097 /* Default function to output a label to divide up the exception table.
7098 The default is to do nothing. A target that needs/wants to divide
7099 up the table must provide it's own function to do this. */
7100 void
7101 default_emit_except_table_label (FILE * stream ATTRIBUTE_UNUSED)
7105 /* This is how to output an internal numbered label where PREFIX is
7106 the class of label and LABELNO is the number within the class. */
7108 void
7109 default_generate_internal_label (char *buf, const char *prefix,
7110 unsigned long labelno)
7112 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
7115 /* This is how to output an internal numbered label where PREFIX is
7116 the class of label and LABELNO is the number within the class. */
7118 void
7119 default_internal_label (FILE *stream, const char *prefix,
7120 unsigned long labelno)
7122 char *const buf = (char *) alloca (40 + strlen (prefix));
7123 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
7124 ASM_OUTPUT_INTERNAL_LABEL (stream, buf);
7128 /* The default implementation of ASM_DECLARE_CONSTANT_NAME. */
7130 void
7131 default_asm_declare_constant_name (FILE *file, const char *name,
7132 const_tree exp ATTRIBUTE_UNUSED,
7133 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
7135 assemble_label (file, name);
7138 /* This is the default behavior at the beginning of a file. It's
7139 controlled by two other target-hook toggles. */
7140 void
7141 default_file_start (void)
7143 if (targetm.asm_file_start_app_off
7144 && !(flag_verbose_asm || flag_debug_asm || flag_dump_rtl_in_asm))
7145 fputs (ASM_APP_OFF, asm_out_file);
7147 if (targetm.asm_file_start_file_directive)
7148 output_file_directive (asm_out_file, main_input_filename);
7151 /* This is a generic routine suitable for use as TARGET_ASM_FILE_END
7152 which emits a special section directive used to indicate whether or
7153 not this object file needs an executable stack. This is primarily
7154 a GNU extension to ELF but could be used on other targets. */
7156 int trampolines_created;
7158 void
7159 file_end_indicate_exec_stack (void)
7161 unsigned int flags = SECTION_DEBUG;
7162 if (trampolines_created)
7163 flags |= SECTION_CODE;
7165 switch_to_section (get_section (".note.GNU-stack", flags, NULL));
7168 /* Emit a special section directive to indicate that this object file
7169 was compiled with -fsplit-stack. This is used to let the linker
7170 detect calls between split-stack code and non-split-stack code, so
7171 that it can modify the split-stack code to allocate a sufficiently
7172 large stack. We emit another special section if there are any
7173 functions in this file which have the no_split_stack attribute, to
7174 prevent the linker from warning about being unable to convert the
7175 functions if they call non-split-stack code. */
7177 void
7178 file_end_indicate_split_stack (void)
7180 if (flag_split_stack)
7182 switch_to_section (get_section (".note.GNU-split-stack", SECTION_DEBUG,
7183 NULL));
7184 if (saw_no_split_stack)
7185 switch_to_section (get_section (".note.GNU-no-split-stack",
7186 SECTION_DEBUG, NULL));
7190 /* Output DIRECTIVE (a C string) followed by a newline. This is used as
7191 a get_unnamed_section callback. */
7193 void
7194 output_section_asm_op (const void *directive)
7196 fprintf (asm_out_file, "%s\n", (const char *) directive);
7199 /* Emit assembly code to switch to section NEW_SECTION. Do nothing if
7200 the current section is NEW_SECTION. */
7202 void
7203 switch_to_section (section *new_section)
7205 if (in_section == new_section)
7206 return;
7208 if (new_section->common.flags & SECTION_FORGET)
7209 in_section = NULL;
7210 else
7211 in_section = new_section;
7213 switch (SECTION_STYLE (new_section))
7215 case SECTION_NAMED:
7216 targetm.asm_out.named_section (new_section->named.name,
7217 new_section->named.common.flags,
7218 new_section->named.decl);
7219 break;
7221 case SECTION_UNNAMED:
7222 new_section->unnamed.callback (new_section->unnamed.data);
7223 break;
7225 case SECTION_NOSWITCH:
7226 gcc_unreachable ();
7227 break;
7230 new_section->common.flags |= SECTION_DECLARED;
7233 /* If block symbol SYMBOL has not yet been assigned an offset, place
7234 it at the end of its block. */
7236 void
7237 place_block_symbol (rtx symbol)
7239 unsigned HOST_WIDE_INT size, mask, offset;
7240 struct constant_descriptor_rtx *desc;
7241 unsigned int alignment;
7242 struct object_block *block;
7243 tree decl;
7245 gcc_assert (SYMBOL_REF_BLOCK (symbol));
7246 if (SYMBOL_REF_BLOCK_OFFSET (symbol) >= 0)
7247 return;
7249 /* Work out the symbol's size and alignment. */
7250 if (CONSTANT_POOL_ADDRESS_P (symbol))
7252 desc = SYMBOL_REF_CONSTANT (symbol);
7253 alignment = desc->align;
7254 size = GET_MODE_SIZE (desc->mode);
7256 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
7258 decl = SYMBOL_REF_DECL (symbol);
7259 alignment = DECL_ALIGN (decl);
7260 size = get_constant_size (DECL_INITIAL (decl));
7262 else
7264 decl = SYMBOL_REF_DECL (symbol);
7265 alignment = DECL_ALIGN (decl);
7266 size = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
7269 /* Calculate the object's offset from the start of the block. */
7270 block = SYMBOL_REF_BLOCK (symbol);
7271 mask = alignment / BITS_PER_UNIT - 1;
7272 offset = (block->size + mask) & ~mask;
7273 SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
7275 /* Record the block's new alignment and size. */
7276 block->alignment = MAX (block->alignment, alignment);
7277 block->size = offset + size;
7279 VEC_safe_push (rtx, gc, block->objects, symbol);
7282 /* Return the anchor that should be used to address byte offset OFFSET
7283 from the first object in BLOCK. MODEL is the TLS model used
7284 to access it. */
7287 get_section_anchor (struct object_block *block, HOST_WIDE_INT offset,
7288 enum tls_model model)
7290 char label[100];
7291 unsigned int begin, middle, end;
7292 unsigned HOST_WIDE_INT min_offset, max_offset, range, bias, delta;
7293 rtx anchor;
7295 /* Work out the anchor's offset. Use an offset of 0 for the first
7296 anchor so that we don't pessimize the case where we take the address
7297 of a variable at the beginning of the block. This is particularly
7298 useful when a block has only one variable assigned to it.
7300 We try to place anchors RANGE bytes apart, so there can then be
7301 anchors at +/-RANGE, +/-2 * RANGE, and so on, up to the limits of
7302 a ptr_mode offset. With some target settings, the lowest such
7303 anchor might be out of range for the lowest ptr_mode offset;
7304 likewise the highest anchor for the highest offset. Use anchors
7305 at the extreme ends of the ptr_mode range in such cases.
7307 All arithmetic uses unsigned integers in order to avoid
7308 signed overflow. */
7309 max_offset = (unsigned HOST_WIDE_INT) targetm.max_anchor_offset;
7310 min_offset = (unsigned HOST_WIDE_INT) targetm.min_anchor_offset;
7311 range = max_offset - min_offset + 1;
7312 if (range == 0)
7313 offset = 0;
7314 else
7316 bias = 1 << (GET_MODE_BITSIZE (ptr_mode) - 1);
7317 if (offset < 0)
7319 delta = -(unsigned HOST_WIDE_INT) offset + max_offset;
7320 delta -= delta % range;
7321 if (delta > bias)
7322 delta = bias;
7323 offset = (HOST_WIDE_INT) (-delta);
7325 else
7327 delta = (unsigned HOST_WIDE_INT) offset - min_offset;
7328 delta -= delta % range;
7329 if (delta > bias - 1)
7330 delta = bias - 1;
7331 offset = (HOST_WIDE_INT) delta;
7335 /* Do a binary search to see if there's already an anchor we can use.
7336 Set BEGIN to the new anchor's index if not. */
7337 begin = 0;
7338 end = VEC_length (rtx, block->anchors);
7339 while (begin != end)
7341 middle = (end + begin) / 2;
7342 anchor = VEC_index (rtx, block->anchors, middle);
7343 if (SYMBOL_REF_BLOCK_OFFSET (anchor) > offset)
7344 end = middle;
7345 else if (SYMBOL_REF_BLOCK_OFFSET (anchor) < offset)
7346 begin = middle + 1;
7347 else if (SYMBOL_REF_TLS_MODEL (anchor) > model)
7348 end = middle;
7349 else if (SYMBOL_REF_TLS_MODEL (anchor) < model)
7350 begin = middle + 1;
7351 else
7352 return anchor;
7355 /* Create a new anchor with a unique label. */
7356 ASM_GENERATE_INTERNAL_LABEL (label, "LANCHOR", anchor_labelno++);
7357 anchor = create_block_symbol (ggc_strdup (label), block, offset);
7358 SYMBOL_REF_FLAGS (anchor) |= SYMBOL_FLAG_LOCAL | SYMBOL_FLAG_ANCHOR;
7359 SYMBOL_REF_FLAGS (anchor) |= model << SYMBOL_FLAG_TLS_SHIFT;
7361 /* Insert it at index BEGIN. */
7362 VEC_safe_insert (rtx, gc, block->anchors, begin, anchor);
7363 return anchor;
7366 /* Output the objects in BLOCK. */
7368 static void
7369 output_object_block (struct object_block *block)
7371 struct constant_descriptor_rtx *desc;
7372 unsigned int i;
7373 HOST_WIDE_INT offset;
7374 tree decl;
7375 rtx symbol;
7377 if (block->objects == NULL)
7378 return;
7380 /* Switch to the section and make sure that the first byte is
7381 suitably aligned. */
7382 switch_to_section (block->sect);
7383 assemble_align (block->alignment);
7385 /* Define the values of all anchors relative to the current section
7386 position. */
7387 FOR_EACH_VEC_ELT (rtx, block->anchors, i, symbol)
7388 targetm.asm_out.output_anchor (symbol);
7390 /* Output the objects themselves. */
7391 offset = 0;
7392 FOR_EACH_VEC_ELT (rtx, block->objects, i, symbol)
7394 /* Move to the object's offset, padding with zeros if necessary. */
7395 assemble_zeros (SYMBOL_REF_BLOCK_OFFSET (symbol) - offset);
7396 offset = SYMBOL_REF_BLOCK_OFFSET (symbol);
7397 if (CONSTANT_POOL_ADDRESS_P (symbol))
7399 desc = SYMBOL_REF_CONSTANT (symbol);
7400 output_constant_pool_1 (desc, 1);
7401 offset += GET_MODE_SIZE (desc->mode);
7403 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
7405 decl = SYMBOL_REF_DECL (symbol);
7406 assemble_constant_contents (DECL_INITIAL (decl), XSTR (symbol, 0),
7407 DECL_ALIGN (decl));
7408 offset += get_constant_size (DECL_INITIAL (decl));
7410 else
7412 decl = SYMBOL_REF_DECL (symbol);
7413 assemble_variable_contents (decl, XSTR (symbol, 0), false);
7414 offset += tree_low_cst (DECL_SIZE_UNIT (decl), 1);
7419 /* A htab_traverse callback used to call output_object_block for
7420 each member of object_block_htab. */
7422 static int
7423 output_object_block_htab (void **slot, void *data ATTRIBUTE_UNUSED)
7425 output_object_block ((struct object_block *) (*slot));
7426 return 1;
7429 /* Output the definitions of all object_blocks. */
7431 void
7432 output_object_blocks (void)
7434 htab_traverse (object_block_htab, output_object_block_htab, NULL);
7437 /* This function provides a possible implementation of the
7438 TARGET_ASM_RECORD_GCC_SWITCHES target hook for ELF targets. When triggered
7439 by -frecord-gcc-switches it creates a new mergeable, string section in the
7440 assembler output file called TARGET_ASM_RECORD_GCC_SWITCHES_SECTION which
7441 contains the switches in ASCII format.
7443 FIXME: This code does not correctly handle double quote characters
7444 that appear inside strings, (it strips them rather than preserving them).
7445 FIXME: ASM_OUTPUT_ASCII, as defined in config/elfos.h will not emit NUL
7446 characters - instead it treats them as sub-string separators. Since
7447 we want to emit NUL strings terminators into the object file we have to use
7448 ASM_OUTPUT_SKIP. */
7451 elf_record_gcc_switches (print_switch_type type, const char * name)
7453 switch (type)
7455 case SWITCH_TYPE_PASSED:
7456 ASM_OUTPUT_ASCII (asm_out_file, name, strlen (name));
7457 ASM_OUTPUT_SKIP (asm_out_file, (unsigned HOST_WIDE_INT) 1);
7458 break;
7460 case SWITCH_TYPE_DESCRIPTIVE:
7461 if (name == NULL)
7463 /* Distinguish between invocations where name is NULL. */
7464 static bool started = false;
7466 if (!started)
7468 section * sec;
7470 sec = get_section (targetm.asm_out.record_gcc_switches_section,
7471 SECTION_DEBUG
7472 | SECTION_MERGE
7473 | SECTION_STRINGS
7474 | (SECTION_ENTSIZE & 1),
7475 NULL);
7476 switch_to_section (sec);
7477 started = true;
7481 default:
7482 break;
7485 /* The return value is currently ignored by the caller, but must be 0.
7486 For -fverbose-asm the return value would be the number of characters
7487 emitted into the assembler file. */
7488 return 0;
7491 /* Emit text to declare externally defined symbols. It is needed to
7492 properly support non-default visibility. */
7493 void
7494 default_elf_asm_output_external (FILE *file ATTRIBUTE_UNUSED,
7495 tree decl,
7496 const char *name ATTRIBUTE_UNUSED)
7498 /* We output the name if and only if TREE_SYMBOL_REFERENCED is
7499 set in order to avoid putting out names that are never really
7500 used. */
7501 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
7502 && targetm.binds_local_p (decl))
7503 maybe_assemble_visibility (decl);
7506 /* The default hook for TARGET_ASM_OUTPUT_SOURCE_FILENAME. */
7508 void
7509 default_asm_output_source_filename (FILE *file, const char *name)
7511 #ifdef ASM_OUTPUT_SOURCE_FILENAME
7512 ASM_OUTPUT_SOURCE_FILENAME (file, name);
7513 #else
7514 fprintf (file, "\t.file\t");
7515 output_quoted_string (file, name);
7516 putc ('\n', file);
7517 #endif
7520 /* Output a file name in the form wanted by System V. */
7522 void
7523 output_file_directive (FILE *asm_file, const char *input_name)
7525 int len;
7526 const char *na;
7528 if (input_name == NULL)
7529 input_name = "<stdin>";
7530 else
7531 input_name = remap_debug_filename (input_name);
7533 len = strlen (input_name);
7534 na = input_name + len;
7536 /* NA gets INPUT_NAME sans directory names. */
7537 while (na > input_name)
7539 if (IS_DIR_SEPARATOR (na[-1]))
7540 break;
7541 na--;
7544 targetm.asm_out.output_source_filename (asm_file, na);
7547 /* Create a DEBUG_EXPR_DECL / DEBUG_EXPR pair from RTL expression
7548 EXP. */
7550 make_debug_expr_from_rtl (const_rtx exp)
7552 tree ddecl = make_node (DEBUG_EXPR_DECL), type;
7553 enum machine_mode mode = GET_MODE (exp);
7554 rtx dval;
7556 DECL_ARTIFICIAL (ddecl) = 1;
7557 if (REG_P (exp) && REG_EXPR (exp))
7558 type = TREE_TYPE (REG_EXPR (exp));
7559 else if (MEM_P (exp) && MEM_EXPR (exp))
7560 type = TREE_TYPE (MEM_EXPR (exp));
7561 else
7562 type = NULL_TREE;
7563 if (type && TYPE_MODE (type) == mode)
7564 TREE_TYPE (ddecl) = type;
7565 else
7566 TREE_TYPE (ddecl) = lang_hooks.types.type_for_mode (mode, 1);
7567 DECL_MODE (ddecl) = mode;
7568 dval = gen_rtx_DEBUG_EXPR (mode);
7569 DEBUG_EXPR_TREE_DECL (dval) = ddecl;
7570 SET_DECL_RTL (ddecl, dval);
7571 return dval;
7574 #ifdef ELF_ASCII_ESCAPES
7575 /* Default ASM_OUTPUT_LIMITED_STRING for ELF targets. */
7577 void
7578 default_elf_asm_output_limited_string (FILE *f, const char *s)
7580 int escape;
7581 unsigned char c;
7583 fputs (STRING_ASM_OP, f);
7584 putc ('"', f);
7585 while (*s != '\0')
7587 c = *s;
7588 escape = ELF_ASCII_ESCAPES[c];
7589 switch (escape)
7591 case 0:
7592 putc (c, f);
7593 break;
7594 case 1:
7595 /* TODO: Print in hex with fast function, important for -flto. */
7596 fprintf (f, "\\%03o", c);
7597 break;
7598 default:
7599 putc ('\\', f);
7600 putc (escape, f);
7601 break;
7603 s++;
7605 putc ('\"', f);
7606 putc ('\n', f);
7609 /* Default ASM_OUTPUT_ASCII for ELF targets. */
7611 void
7612 default_elf_asm_output_ascii (FILE *f, const char *s, unsigned int len)
7614 const char *limit = s + len;
7615 const char *last_null = NULL;
7616 unsigned bytes_in_chunk = 0;
7617 unsigned char c;
7618 int escape;
7620 for (; s < limit; s++)
7622 const char *p;
7624 if (bytes_in_chunk >= 60)
7626 putc ('\"', f);
7627 putc ('\n', f);
7628 bytes_in_chunk = 0;
7631 if (s > last_null)
7633 for (p = s; p < limit && *p != '\0'; p++)
7634 continue;
7635 last_null = p;
7637 else
7638 p = last_null;
7640 if (p < limit && (p - s) <= (long) ELF_STRING_LIMIT)
7642 if (bytes_in_chunk > 0)
7644 putc ('\"', f);
7645 putc ('\n', f);
7646 bytes_in_chunk = 0;
7649 default_elf_asm_output_limited_string (f, s);
7650 s = p;
7652 else
7654 if (bytes_in_chunk == 0)
7655 fputs (ASCII_DATA_ASM_OP "\"", f);
7657 c = *s;
7658 escape = ELF_ASCII_ESCAPES[c];
7659 switch (escape)
7661 case 0:
7662 putc (c, f);
7663 bytes_in_chunk++;
7664 break;
7665 case 1:
7666 /* TODO: Print in hex with fast function, important for -flto. */
7667 fprintf (f, "\\%03o", c);
7668 bytes_in_chunk += 4;
7669 break;
7670 default:
7671 putc ('\\', f);
7672 putc (escape, f);
7673 bytes_in_chunk += 2;
7674 break;
7680 if (bytes_in_chunk > 0)
7682 putc ('\"', f);
7683 putc ('\n', f);
7686 #endif
7688 static GTY(()) section *elf_init_array_section;
7689 static GTY(()) section *elf_fini_array_section;
7691 static section *
7692 get_elf_initfini_array_priority_section (int priority,
7693 bool constructor_p)
7695 section *sec;
7696 if (priority != DEFAULT_INIT_PRIORITY)
7698 char buf[18];
7699 sprintf (buf, "%s.%.5u",
7700 constructor_p ? ".init_array" : ".fini_array",
7701 priority);
7702 sec = get_section (buf, SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
7704 else
7706 if (constructor_p)
7708 if (elf_init_array_section == NULL)
7709 elf_init_array_section
7710 = get_section (".init_array",
7711 SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
7712 sec = elf_init_array_section;
7714 else
7716 if (elf_fini_array_section == NULL)
7717 elf_fini_array_section
7718 = get_section (".fini_array",
7719 SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
7720 sec = elf_fini_array_section;
7723 return sec;
7726 /* Use .init_array section for constructors. */
7728 void
7729 default_elf_init_array_asm_out_constructor (rtx symbol, int priority)
7731 section *sec = get_elf_initfini_array_priority_section (priority,
7732 true);
7733 assemble_addr_to_section (symbol, sec);
7736 /* Use .fini_array section for destructors. */
7738 void
7739 default_elf_fini_array_asm_out_destructor (rtx symbol, int priority)
7741 section *sec = get_elf_initfini_array_priority_section (priority,
7742 false);
7743 assemble_addr_to_section (symbol, sec);
7746 #include "gt-varasm.h"