-dA enhancement
[official-gcc.git] / gcc / varasm.c
blob332f0f46033c62ff3e7eb62dd497e0857a99e8d0
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 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 "tm.h"
34 #include "rtl.h"
35 #include "tree.h"
36 #include "flags.h"
37 #include "function.h"
38 #include "expr.h"
39 #include "hard-reg-set.h"
40 #include "regs.h"
41 #include "output.h"
42 #include "diagnostic-core.h"
43 #include "hashtab.h"
44 #include "ggc.h"
45 #include "langhooks.h"
46 #include "tm_p.h"
47 #include "debug.h"
48 #include "target.h"
49 #include "targhooks.h"
50 #include "tree-mudflap.h"
51 #include "cgraph.h"
52 #include "cfglayout.h"
53 #include "basic-block.h"
54 #include "tree-iterator.h"
55 #include "pointer-set.h"
57 #ifdef XCOFF_DEBUGGING_INFO
58 #include "xcoffout.h" /* Needed for external data
59 declarations for e.g. AIX 4.x. */
60 #endif
62 /* The (assembler) name of the first globally-visible object output. */
63 extern GTY(()) const char *first_global_object_name;
64 extern GTY(()) const char *weak_global_object_name;
66 const char *first_global_object_name;
67 const char *weak_global_object_name;
69 struct addr_const;
70 struct constant_descriptor_rtx;
71 struct rtx_constant_pool;
73 #define n_deferred_constants (crtl->varasm.deferred_constants)
75 /* Number for making the label on the next
76 constant that is stored in memory. */
78 static GTY(()) int const_labelno;
80 /* Carry information from ASM_DECLARE_OBJECT_NAME
81 to ASM_FINISH_DECLARE_OBJECT. */
83 int size_directive_output;
85 /* The last decl for which assemble_variable was called,
86 if it did ASM_DECLARE_OBJECT_NAME.
87 If the last call to assemble_variable didn't do that,
88 this holds 0. */
90 tree last_assemble_variable_decl;
92 /* The following global variable indicates if the first basic block
93 in a function belongs to the cold partition or not. */
95 bool first_function_block_is_cold;
97 /* We give all constants their own alias set. Perhaps redundant with
98 MEM_READONLY_P, but pre-dates it. */
100 static alias_set_type const_alias_set;
102 /* Whether we saw any functions with no_split_stack. */
104 static bool saw_no_split_stack;
106 static const char *strip_reg_name (const char *);
107 static int contains_pointers_p (tree);
108 #ifdef ASM_OUTPUT_EXTERNAL
109 static bool incorporeal_function_p (tree);
110 #endif
111 static void decode_addr_const (tree, struct addr_const *);
112 static hashval_t const_desc_hash (const void *);
113 static int const_desc_eq (const void *, const void *);
114 static hashval_t const_hash_1 (const tree);
115 static int compare_constant (const tree, const tree);
116 static tree copy_constant (tree);
117 static void output_constant_def_contents (rtx);
118 static void output_addressed_constants (tree);
119 static unsigned HOST_WIDE_INT array_size_for_constructor (tree);
120 static unsigned min_align (unsigned, unsigned);
121 static void globalize_decl (tree);
122 static bool decl_readonly_section_1 (enum section_category);
123 #ifdef BSS_SECTION_ASM_OP
124 #ifdef ASM_OUTPUT_ALIGNED_BSS
125 static void asm_output_aligned_bss (FILE *, tree, const char *,
126 unsigned HOST_WIDE_INT, int)
127 ATTRIBUTE_UNUSED;
128 #endif
129 #endif /* BSS_SECTION_ASM_OP */
130 static void mark_weak (tree);
131 static void output_constant_pool (const char *, tree);
133 /* Well-known sections, each one associated with some sort of *_ASM_OP. */
134 section *text_section;
135 section *data_section;
136 section *readonly_data_section;
137 section *sdata_section;
138 section *ctors_section;
139 section *dtors_section;
140 section *bss_section;
141 section *sbss_section;
143 /* Various forms of common section. All are guaranteed to be nonnull. */
144 section *tls_comm_section;
145 section *comm_section;
146 section *lcomm_section;
148 /* A SECTION_NOSWITCH section used for declaring global BSS variables.
149 May be null. */
150 section *bss_noswitch_section;
152 /* The section that holds the main exception table, when known. The section
153 is set either by the target's init_sections hook or by the first call to
154 switch_to_exception_section. */
155 section *exception_section;
157 /* The section that holds the DWARF2 frame unwind information, when known.
158 The section is set either by the target's init_sections hook or by the
159 first call to switch_to_eh_frame_section. */
160 section *eh_frame_section;
162 /* asm_out_file's current section. This is NULL if no section has yet
163 been selected or if we lose track of what the current section is. */
164 section *in_section;
166 /* True if code for the current function is currently being directed
167 at the cold section. */
168 bool in_cold_section_p;
170 /* A linked list of all the unnamed sections. */
171 static GTY(()) section *unnamed_sections;
173 /* Return a nonzero value if DECL has a section attribute. */
174 #define IN_NAMED_SECTION(DECL) \
175 ((TREE_CODE (DECL) == FUNCTION_DECL || TREE_CODE (DECL) == VAR_DECL) \
176 && DECL_SECTION_NAME (DECL) != NULL_TREE)
178 /* Hash table of named sections. */
179 static GTY((param_is (section))) htab_t section_htab;
181 /* A table of object_blocks, indexed by section. */
182 static GTY((param_is (struct object_block))) htab_t object_block_htab;
184 /* The next number to use for internal anchor labels. */
185 static GTY(()) int anchor_labelno;
187 /* A pool of constants that can be shared between functions. */
188 static GTY(()) struct rtx_constant_pool *shared_constant_pool;
190 /* Helper routines for maintaining section_htab. */
192 static int
193 section_entry_eq (const void *p1, const void *p2)
195 const section *old = (const section *) p1;
196 const char *new_name = (const char *) p2;
198 return strcmp (old->named.name, new_name) == 0;
201 static hashval_t
202 section_entry_hash (const void *p)
204 const section *old = (const section *) p;
205 return htab_hash_string (old->named.name);
208 /* Return a hash value for section SECT. */
210 static hashval_t
211 hash_section (section *sect)
213 if (sect->common.flags & SECTION_NAMED)
214 return htab_hash_string (sect->named.name);
215 return sect->common.flags;
218 /* Helper routines for maintaining object_block_htab. */
220 static int
221 object_block_entry_eq (const void *p1, const void *p2)
223 const struct object_block *old = (const struct object_block *) p1;
224 const section *new_section = (const section *) p2;
226 return old->sect == new_section;
229 static hashval_t
230 object_block_entry_hash (const void *p)
232 const struct object_block *old = (const struct object_block *) p;
233 return hash_section (old->sect);
236 /* Return a new unnamed section with the given fields. */
238 section *
239 get_unnamed_section (unsigned int flags, void (*callback) (const void *),
240 const void *data)
242 section *sect;
244 sect = ggc_alloc_section ();
245 sect->unnamed.common.flags = flags | SECTION_UNNAMED;
246 sect->unnamed.callback = callback;
247 sect->unnamed.data = data;
248 sect->unnamed.next = unnamed_sections;
250 unnamed_sections = sect;
251 return sect;
254 /* Return a SECTION_NOSWITCH section with the given fields. */
256 static section *
257 get_noswitch_section (unsigned int flags, noswitch_section_callback callback)
259 section *sect;
261 sect = ggc_alloc_section ();
262 sect->noswitch.common.flags = flags | SECTION_NOSWITCH;
263 sect->noswitch.callback = callback;
265 return sect;
268 /* Return the named section structure associated with NAME. Create
269 a new section with the given fields if no such structure exists. */
271 section *
272 get_section (const char *name, unsigned int flags, tree decl)
274 section *sect, **slot;
276 slot = (section **)
277 htab_find_slot_with_hash (section_htab, name,
278 htab_hash_string (name), INSERT);
279 flags |= SECTION_NAMED;
280 if (*slot == NULL)
282 sect = ggc_alloc_section ();
283 sect->named.common.flags = flags;
284 sect->named.name = ggc_strdup (name);
285 sect->named.decl = decl;
286 *slot = sect;
288 else
290 sect = *slot;
291 if ((sect->common.flags & ~SECTION_DECLARED) != flags
292 && ((sect->common.flags | flags) & SECTION_OVERRIDE) == 0)
294 /* It is fine if one of the section flags is
295 SECTION_WRITE | SECTION_RELRO and the other has none of these
296 flags (i.e. read-only) in named sections and either the
297 section hasn't been declared yet or has been declared as writable.
298 In that case just make sure the resulting flags are
299 SECTION_WRITE | SECTION_RELRO, ie. writable only because of
300 relocations. */
301 if (((sect->common.flags ^ flags) & (SECTION_WRITE | SECTION_RELRO))
302 == (SECTION_WRITE | SECTION_RELRO)
303 && (sect->common.flags
304 & ~(SECTION_DECLARED | SECTION_WRITE | SECTION_RELRO))
305 == (flags & ~(SECTION_WRITE | SECTION_RELRO))
306 && ((sect->common.flags & SECTION_DECLARED) == 0
307 || (sect->common.flags & SECTION_WRITE)))
309 sect->common.flags |= (SECTION_WRITE | SECTION_RELRO);
310 return sect;
312 /* Sanity check user variables for flag changes. */
313 if (decl == 0)
314 decl = sect->named.decl;
315 gcc_assert (decl);
316 error ("%+D causes a section type conflict", decl);
317 /* Make sure we don't error about one section multiple times. */
318 sect->common.flags |= SECTION_OVERRIDE;
321 return sect;
324 /* Return true if the current compilation mode benefits from having
325 objects grouped into blocks. */
327 static bool
328 use_object_blocks_p (void)
330 return flag_section_anchors;
333 /* Return the object_block structure for section SECT. Create a new
334 structure if we haven't created one already. Return null if SECT
335 itself is null. */
337 static struct object_block *
338 get_block_for_section (section *sect)
340 struct object_block *block;
341 void **slot;
343 if (sect == NULL)
344 return NULL;
346 slot = htab_find_slot_with_hash (object_block_htab, sect,
347 hash_section (sect), INSERT);
348 block = (struct object_block *) *slot;
349 if (block == NULL)
351 block = ggc_alloc_cleared_object_block ();
352 block->sect = sect;
353 *slot = block;
355 return block;
358 /* Create a symbol with label LABEL and place it at byte offset
359 OFFSET in BLOCK. OFFSET can be negative if the symbol's offset
360 is not yet known. LABEL must be a garbage-collected string. */
362 static rtx
363 create_block_symbol (const char *label, struct object_block *block,
364 HOST_WIDE_INT offset)
366 rtx symbol;
367 unsigned int size;
369 /* Create the extended SYMBOL_REF. */
370 size = RTX_HDR_SIZE + sizeof (struct block_symbol);
371 symbol = ggc_alloc_zone_rtx_def (size, &rtl_zone);
373 /* Initialize the normal SYMBOL_REF fields. */
374 memset (symbol, 0, size);
375 PUT_CODE (symbol, SYMBOL_REF);
376 PUT_MODE (symbol, Pmode);
377 XSTR (symbol, 0) = label;
378 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_HAS_BLOCK_INFO;
380 /* Initialize the block_symbol stuff. */
381 SYMBOL_REF_BLOCK (symbol) = block;
382 SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
384 return symbol;
387 /* Return a section with a particular name and with whatever SECTION_*
388 flags section_type_flags deems appropriate. The name of the section
389 is taken from NAME if nonnull, otherwise it is taken from DECL's
390 DECL_SECTION_NAME. DECL is the decl associated with the section
391 (see the section comment for details) and RELOC is as for
392 section_type_flags. */
394 section *
395 get_named_section (tree decl, const char *name, int reloc)
397 unsigned int flags;
399 gcc_assert (!decl || DECL_P (decl));
400 if (name == NULL)
401 name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
403 flags = targetm.section_type_flags (decl, name, reloc);
405 return get_section (name, flags, decl);
408 /* If required, set DECL_SECTION_NAME to a unique name. */
410 void
411 resolve_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED,
412 int flag_function_or_data_sections)
414 if (DECL_SECTION_NAME (decl) == NULL_TREE
415 && targetm.have_named_sections
416 && (flag_function_or_data_sections
417 || DECL_ONE_ONLY (decl)))
419 targetm.asm_out.unique_section (decl, reloc);
420 DECL_HAS_IMPLICIT_SECTION_NAME_P (decl) = true;
424 #ifdef BSS_SECTION_ASM_OP
426 #ifdef ASM_OUTPUT_ALIGNED_BSS
428 /* Utility function for targets to use in implementing
429 ASM_OUTPUT_ALIGNED_BSS.
430 ??? It is believed that this function will work in most cases so such
431 support is localized here. */
433 static void
434 asm_output_aligned_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
435 const char *name, unsigned HOST_WIDE_INT size,
436 int align)
438 switch_to_section (bss_section);
439 ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
440 #ifdef ASM_DECLARE_OBJECT_NAME
441 last_assemble_variable_decl = decl;
442 ASM_DECLARE_OBJECT_NAME (file, name, decl);
443 #else
444 /* Standard thing is just output label for the object. */
445 ASM_OUTPUT_LABEL (file, name);
446 #endif /* ASM_DECLARE_OBJECT_NAME */
447 ASM_OUTPUT_SKIP (file, size ? size : 1);
450 #endif
452 #endif /* BSS_SECTION_ASM_OP */
454 #ifndef USE_SELECT_SECTION_FOR_FUNCTIONS
455 /* Return the hot section for function DECL. Return text_section for
456 null DECLs. */
458 static section *
459 hot_function_section (tree decl)
461 if (decl != NULL_TREE
462 && DECL_SECTION_NAME (decl) != NULL_TREE
463 && targetm.have_named_sections)
464 return get_named_section (decl, NULL, 0);
465 else
466 return text_section;
468 #endif
470 /* Return section for TEXT_SECTION_NAME if DECL or DECL_SECTION_NAME (DECL)
471 is NULL.
473 When DECL_SECTION_NAME is non-NULL and it is implicit section and
474 NAMED_SECTION_SUFFIX is non-NULL, then produce section called
475 concatenate the name with NAMED_SECTION_SUFFIX.
476 Otherwise produce "TEXT_SECTION_NAME.IMPLICIT_NAME". */
478 section *
479 get_named_text_section (tree decl,
480 const char *text_section_name,
481 const char *named_section_suffix)
483 if (decl && DECL_SECTION_NAME (decl))
485 if (named_section_suffix)
487 tree dsn = DECL_SECTION_NAME (decl);
488 const char *stripped_name;
489 char *name, *buffer;
491 name = (char *) alloca (TREE_STRING_LENGTH (dsn) + 1);
492 memcpy (name, TREE_STRING_POINTER (dsn),
493 TREE_STRING_LENGTH (dsn) + 1);
495 stripped_name = targetm.strip_name_encoding (name);
497 buffer = ACONCAT ((stripped_name, named_section_suffix, NULL));
498 return get_named_section (decl, buffer, 0);
500 else if (DECL_HAS_IMPLICIT_SECTION_NAME_P (decl))
502 const char *name;
504 /* Do not try to split gnu_linkonce functions. This gets somewhat
505 slipperly. */
506 if (DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP)
507 return NULL;
508 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
509 name = targetm.strip_name_encoding (name);
510 return get_named_section (decl, ACONCAT ((text_section_name, ".",
511 name, NULL)), 0);
513 else
514 return NULL;
516 return get_named_section (decl, text_section_name, 0);
519 /* Choose named function section based on its frequency. */
521 section *
522 default_function_section (tree decl, enum node_frequency freq,
523 bool startup, bool exit)
525 #if defined HAVE_LD_EH_GC_SECTIONS && defined HAVE_LD_EH_GC_SECTIONS_BUG
526 /* Old GNU linkers have buggy --gc-section support, which sometimes
527 results in .gcc_except_table* sections being garbage collected. */
528 if (decl
529 && DECL_SECTION_NAME (decl)
530 && DECL_HAS_IMPLICIT_SECTION_NAME_P (decl))
531 return NULL;
532 #endif
534 if (!flag_reorder_functions
535 || !targetm.have_named_sections)
536 return NULL;
537 /* Startup code should go to startup subsection unless it is
538 unlikely executed (this happens especially with function splitting
539 where we can split away unnecesary parts of static constructors. */
540 if (startup && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
541 return get_named_text_section (decl, ".text.startup", NULL);
543 /* Similarly for exit. */
544 if (exit && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
545 return get_named_text_section (decl, ".text.exit", NULL);
547 /* Group cold functions together, similarly for hot code. */
548 switch (freq)
550 case NODE_FREQUENCY_UNLIKELY_EXECUTED:
551 return get_named_text_section (decl, ".text.unlikely", NULL);
552 case NODE_FREQUENCY_HOT:
553 return get_named_text_section (decl, ".text.hot", NULL);
554 default:
555 return NULL;
559 /* Return the section for function DECL.
561 If DECL is NULL_TREE, return the text section. We can be passed
562 NULL_TREE under some circumstances by dbxout.c at least.
564 If FORCE_COLD is true, return cold function section ignoring
565 the frequency info of cgraph_node. */
567 static section *
568 function_section_1 (tree decl, bool force_cold)
570 section *section = NULL;
571 enum node_frequency freq = NODE_FREQUENCY_NORMAL;
572 bool startup = false, exit = false;
574 if (decl)
576 struct cgraph_node *node = cgraph_node (decl);
578 freq = node->frequency;
579 startup = node->only_called_at_startup;
580 exit = node->only_called_at_exit;
582 if (force_cold)
583 freq = NODE_FREQUENCY_UNLIKELY_EXECUTED;
585 #ifdef USE_SELECT_SECTION_FOR_FUNCTIONS
586 if (decl != NULL_TREE
587 && DECL_SECTION_NAME (decl) != NULL_TREE)
589 if (targetm.asm_out.function_section)
590 section = targetm.asm_out.function_section (decl, freq,
591 startup, exit);
592 if (section)
593 return section;
594 return get_named_section (decl, NULL, 0);
596 else
597 return targetm.asm_out.select_section
598 (decl, freq == NODE_FREQUENCY_UNLIKELY_EXECUTED,
599 DECL_ALIGN (decl));
600 #else
601 if (targetm.asm_out.function_section)
602 section = targetm.asm_out.function_section (decl, freq, startup, exit);
603 if (section)
604 return section;
605 return hot_function_section (decl);
606 #endif
609 /* Return the section for function DECL.
611 If DECL is NULL_TREE, return the text section. We can be passed
612 NULL_TREE under some circumstances by dbxout.c at least. */
614 section *
615 function_section (tree decl)
617 /* Handle cases where function splitting code decides
618 to put function entry point into unlikely executed section
619 despite the fact that the function itself is not cold
620 (i.e. it is called rarely but contains a hot loop that is
621 better to live in hot subsection for the code locality). */
622 return function_section_1 (decl,
623 first_function_block_is_cold);
626 /* Return the section for the current function, take IN_COLD_SECTION_P
627 into account. */
629 section *
630 current_function_section (void)
632 return function_section_1 (current_function_decl, in_cold_section_p);
635 /* Tell assembler to switch to unlikely-to-be-executed text section. */
637 section *
638 unlikely_text_section (void)
640 return function_section_1 (current_function_decl, true);
643 /* When called within a function context, return true if the function
644 has been assigned a cold text section and if SECT is that section.
645 When called outside a function context, return true if SECT is the
646 default cold section. */
648 bool
649 unlikely_text_section_p (section *sect)
651 return sect == function_section_1 (current_function_decl, true);
654 /* Return the read-only data section associated with function DECL. */
656 section *
657 default_function_rodata_section (tree decl)
659 if (decl != NULL_TREE && DECL_SECTION_NAME (decl))
661 const char *name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
663 if (DECL_ONE_ONLY (decl) && HAVE_COMDAT_GROUP)
665 const char *dot;
666 size_t len;
667 char* rname;
669 dot = strchr (name + 1, '.');
670 if (!dot)
671 dot = name;
672 len = strlen (dot) + 8;
673 rname = (char *) alloca (len);
675 strcpy (rname, ".rodata");
676 strcat (rname, dot);
677 return get_section (rname, SECTION_LINKONCE, decl);
679 /* For .gnu.linkonce.t.foo we want to use .gnu.linkonce.r.foo. */
680 else if (DECL_ONE_ONLY (decl)
681 && strncmp (name, ".gnu.linkonce.t.", 16) == 0)
683 size_t len = strlen (name) + 1;
684 char *rname = (char *) alloca (len);
686 memcpy (rname, name, len);
687 rname[14] = 'r';
688 return get_section (rname, SECTION_LINKONCE, decl);
690 /* For .text.foo we want to use .rodata.foo. */
691 else if (flag_function_sections && flag_data_sections
692 && strncmp (name, ".text.", 6) == 0)
694 size_t len = strlen (name) + 1;
695 char *rname = (char *) alloca (len + 2);
697 memcpy (rname, ".rodata", 7);
698 memcpy (rname + 7, name + 5, len - 5);
699 return get_section (rname, 0, decl);
703 return readonly_data_section;
706 /* Return the read-only data section associated with function DECL
707 for targets where that section should be always the single
708 readonly data section. */
710 section *
711 default_no_function_rodata_section (tree decl ATTRIBUTE_UNUSED)
713 return readonly_data_section;
716 /* Return the section to use for string merging. */
718 static section *
719 mergeable_string_section (tree decl ATTRIBUTE_UNUSED,
720 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
721 unsigned int flags ATTRIBUTE_UNUSED)
723 HOST_WIDE_INT len;
725 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
726 && TREE_CODE (decl) == STRING_CST
727 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
728 && align <= 256
729 && (len = int_size_in_bytes (TREE_TYPE (decl))) > 0
730 && TREE_STRING_LENGTH (decl) >= len)
732 enum machine_mode mode;
733 unsigned int modesize;
734 const char *str;
735 HOST_WIDE_INT i;
736 int j, unit;
737 char name[30];
739 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (decl)));
740 modesize = GET_MODE_BITSIZE (mode);
741 if (modesize >= 8 && modesize <= 256
742 && (modesize & (modesize - 1)) == 0)
744 if (align < modesize)
745 align = modesize;
747 str = TREE_STRING_POINTER (decl);
748 unit = GET_MODE_SIZE (mode);
750 /* Check for embedded NUL characters. */
751 for (i = 0; i < len; i += unit)
753 for (j = 0; j < unit; j++)
754 if (str[i + j] != '\0')
755 break;
756 if (j == unit)
757 break;
759 if (i == len - unit)
761 sprintf (name, ".rodata.str%d.%d", modesize / 8,
762 (int) (align / 8));
763 flags |= (modesize / 8) | SECTION_MERGE | SECTION_STRINGS;
764 return get_section (name, flags, NULL);
769 return readonly_data_section;
772 /* Return the section to use for constant merging. */
774 section *
775 mergeable_constant_section (enum machine_mode mode ATTRIBUTE_UNUSED,
776 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
777 unsigned int flags ATTRIBUTE_UNUSED)
779 unsigned int modesize = GET_MODE_BITSIZE (mode);
781 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
782 && mode != VOIDmode
783 && mode != BLKmode
784 && modesize <= align
785 && align >= 8
786 && align <= 256
787 && (align & (align - 1)) == 0)
789 char name[24];
791 sprintf (name, ".rodata.cst%d", (int) (align / 8));
792 flags |= (align / 8) | SECTION_MERGE;
793 return get_section (name, flags, NULL);
795 return readonly_data_section;
798 /* Given NAME, a putative register name, discard any customary prefixes. */
800 static const char *
801 strip_reg_name (const char *name)
803 #ifdef REGISTER_PREFIX
804 if (!strncmp (name, REGISTER_PREFIX, strlen (REGISTER_PREFIX)))
805 name += strlen (REGISTER_PREFIX);
806 #endif
807 if (name[0] == '%' || name[0] == '#')
808 name++;
809 return name;
812 /* The user has asked for a DECL to have a particular name. Set (or
813 change) it in such a way that we don't prefix an underscore to
814 it. */
815 void
816 set_user_assembler_name (tree decl, const char *name)
818 char *starred = (char *) alloca (strlen (name) + 2);
819 starred[0] = '*';
820 strcpy (starred + 1, name);
821 change_decl_assembler_name (decl, get_identifier (starred));
822 SET_DECL_RTL (decl, NULL_RTX);
825 /* Decode an `asm' spec for a declaration as a register name.
826 Return the register number, or -1 if nothing specified,
827 or -2 if the ASMSPEC is not `cc' or `memory' and is not recognized,
828 or -3 if ASMSPEC is `cc' and is not recognized,
829 or -4 if ASMSPEC is `memory' and is not recognized.
830 Accept an exact spelling or a decimal number.
831 Prefixes such as % are optional. */
834 decode_reg_name_and_count (const char *asmspec, int *pnregs)
836 /* Presume just one register is clobbered. */
837 *pnregs = 1;
839 if (asmspec != 0)
841 int i;
843 /* Get rid of confusing prefixes. */
844 asmspec = strip_reg_name (asmspec);
846 /* Allow a decimal number as a "register name". */
847 for (i = strlen (asmspec) - 1; i >= 0; i--)
848 if (! ISDIGIT (asmspec[i]))
849 break;
850 if (asmspec[0] != 0 && i < 0)
852 i = atoi (asmspec);
853 if (i < FIRST_PSEUDO_REGISTER && i >= 0)
854 return i;
855 else
856 return -2;
859 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
860 if (reg_names[i][0]
861 && ! strcmp (asmspec, strip_reg_name (reg_names[i])))
862 return i;
864 #ifdef OVERLAPPING_REGISTER_NAMES
866 static const struct
868 const char *const name;
869 const int number;
870 const int nregs;
871 } table[] = OVERLAPPING_REGISTER_NAMES;
873 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
874 if (table[i].name[0]
875 && ! strcmp (asmspec, table[i].name))
877 *pnregs = table[i].nregs;
878 return table[i].number;
881 #endif /* OVERLAPPING_REGISTER_NAMES */
883 #ifdef ADDITIONAL_REGISTER_NAMES
885 static const struct { const char *const name; const int number; } table[]
886 = ADDITIONAL_REGISTER_NAMES;
888 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
889 if (table[i].name[0]
890 && ! strcmp (asmspec, table[i].name))
891 return table[i].number;
893 #endif /* ADDITIONAL_REGISTER_NAMES */
895 if (!strcmp (asmspec, "memory"))
896 return -4;
898 if (!strcmp (asmspec, "cc"))
899 return -3;
901 return -2;
904 return -1;
908 decode_reg_name (const char *name)
910 int count;
911 return decode_reg_name_and_count (name, &count);
915 /* Return true if DECL's initializer is suitable for a BSS section. */
917 static bool
918 bss_initializer_p (const_tree decl)
920 return (DECL_INITIAL (decl) == NULL
921 || DECL_INITIAL (decl) == error_mark_node
922 || (flag_zero_initialized_in_bss
923 /* Leave constant zeroes in .rodata so they
924 can be shared. */
925 && !TREE_READONLY (decl)
926 && initializer_zerop (DECL_INITIAL (decl))));
929 /* Compute the alignment of variable specified by DECL.
930 DONT_OUTPUT_DATA is from assemble_variable. */
932 void
933 align_variable (tree decl, bool dont_output_data)
935 unsigned int align = DECL_ALIGN (decl);
937 /* In the case for initialing an array whose length isn't specified,
938 where we have not yet been able to do the layout,
939 figure out the proper alignment now. */
940 if (dont_output_data && DECL_SIZE (decl) == 0
941 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
942 align = MAX (align, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (decl))));
944 /* Some object file formats have a maximum alignment which they support.
945 In particular, a.out format supports a maximum alignment of 4. */
946 if (align > MAX_OFILE_ALIGNMENT)
948 warning (0, "alignment of %q+D is greater than maximum object "
949 "file alignment. Using %d", decl,
950 MAX_OFILE_ALIGNMENT/BITS_PER_UNIT);
951 align = MAX_OFILE_ALIGNMENT;
954 /* On some machines, it is good to increase alignment sometimes. */
955 if (! DECL_USER_ALIGN (decl))
957 #ifdef DATA_ALIGNMENT
958 unsigned int data_align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
959 /* Don't increase alignment too much for TLS variables - TLS space
960 is too precious. */
961 if (! DECL_THREAD_LOCAL_P (decl) || data_align <= BITS_PER_WORD)
962 align = data_align;
963 #endif
964 #ifdef CONSTANT_ALIGNMENT
965 if (DECL_INITIAL (decl) != 0 && DECL_INITIAL (decl) != error_mark_node)
967 unsigned int const_align = CONSTANT_ALIGNMENT (DECL_INITIAL (decl),
968 align);
969 /* Don't increase alignment too much for TLS variables - TLS space
970 is too precious. */
971 if (! DECL_THREAD_LOCAL_P (decl) || const_align <= BITS_PER_WORD)
972 align = const_align;
974 #endif
977 /* Reset the alignment in case we have made it tighter, so we can benefit
978 from it in get_pointer_alignment. */
979 DECL_ALIGN (decl) = align;
982 /* Return the section into which the given VAR_DECL or CONST_DECL
983 should be placed. PREFER_NOSWITCH_P is true if a noswitch
984 section should be used wherever possible. */
986 section *
987 get_variable_section (tree decl, bool prefer_noswitch_p)
989 addr_space_t as = ADDR_SPACE_GENERIC;
990 int reloc;
992 if (TREE_TYPE (decl) != error_mark_node)
993 as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
995 if (DECL_COMMON (decl))
997 /* If the decl has been given an explicit section name, or it resides
998 in a non-generic address space, then it isn't common, and shouldn't
999 be handled as such. */
1000 gcc_assert (DECL_SECTION_NAME (decl) == NULL
1001 && ADDR_SPACE_GENERIC_P (as));
1002 if (DECL_THREAD_LOCAL_P (decl))
1003 return tls_comm_section;
1004 else if (TREE_PUBLIC (decl) && bss_initializer_p (decl))
1005 return comm_section;
1008 if (DECL_INITIAL (decl) == error_mark_node)
1009 reloc = contains_pointers_p (TREE_TYPE (decl)) ? 3 : 0;
1010 else if (DECL_INITIAL (decl))
1011 reloc = compute_reloc_for_constant (DECL_INITIAL (decl));
1012 else
1013 reloc = 0;
1015 resolve_unique_section (decl, reloc, flag_data_sections);
1016 if (IN_NAMED_SECTION (decl))
1017 return get_named_section (decl, NULL, reloc);
1019 if (ADDR_SPACE_GENERIC_P (as)
1020 && !DECL_THREAD_LOCAL_P (decl)
1021 && !(prefer_noswitch_p && targetm.have_switchable_bss_sections)
1022 && bss_initializer_p (decl))
1024 if (!TREE_PUBLIC (decl))
1025 return lcomm_section;
1026 if (bss_noswitch_section)
1027 return bss_noswitch_section;
1030 return targetm.asm_out.select_section (decl, reloc, DECL_ALIGN (decl));
1033 /* Return the block into which object_block DECL should be placed. */
1035 static struct object_block *
1036 get_block_for_decl (tree decl)
1038 section *sect;
1040 if (TREE_CODE (decl) == VAR_DECL)
1042 /* The object must be defined in this translation unit. */
1043 if (DECL_EXTERNAL (decl))
1044 return NULL;
1046 /* There's no point using object blocks for something that is
1047 isolated by definition. */
1048 if (DECL_ONE_ONLY (decl))
1049 return NULL;
1052 /* We can only calculate block offsets if the decl has a known
1053 constant size. */
1054 if (DECL_SIZE_UNIT (decl) == NULL)
1055 return NULL;
1056 if (!host_integerp (DECL_SIZE_UNIT (decl), 1))
1057 return NULL;
1059 /* Find out which section should contain DECL. We cannot put it into
1060 an object block if it requires a standalone definition. */
1061 if (TREE_CODE (decl) == VAR_DECL)
1062 align_variable (decl, 0);
1063 sect = get_variable_section (decl, true);
1064 if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
1065 return NULL;
1067 return get_block_for_section (sect);
1070 /* Make sure block symbol SYMBOL is in block BLOCK. */
1072 static void
1073 change_symbol_block (rtx symbol, struct object_block *block)
1075 if (block != SYMBOL_REF_BLOCK (symbol))
1077 gcc_assert (SYMBOL_REF_BLOCK_OFFSET (symbol) < 0);
1078 SYMBOL_REF_BLOCK (symbol) = block;
1082 /* Return true if it is possible to put DECL in an object_block. */
1084 static bool
1085 use_blocks_for_decl_p (tree decl)
1087 /* Only data DECLs can be placed into object blocks. */
1088 if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != CONST_DECL)
1089 return false;
1091 /* Detect decls created by dw2_force_const_mem. Such decls are
1092 special because DECL_INITIAL doesn't specify the decl's true value.
1093 dw2_output_indirect_constants will instead call assemble_variable
1094 with dont_output_data set to 1 and then print the contents itself. */
1095 if (DECL_INITIAL (decl) == decl)
1096 return false;
1098 /* If this decl is an alias, then we don't want to emit a
1099 definition. */
1100 if (lookup_attribute ("alias", DECL_ATTRIBUTES (decl)))
1101 return false;
1103 return true;
1106 /* Create the DECL_RTL for a VAR_DECL or FUNCTION_DECL. DECL should
1107 have static storage duration. In other words, it should not be an
1108 automatic variable, including PARM_DECLs.
1110 There is, however, one exception: this function handles variables
1111 explicitly placed in a particular register by the user.
1113 This is never called for PARM_DECL nodes. */
1115 void
1116 make_decl_rtl (tree decl)
1118 const char *name = 0;
1119 int reg_number;
1120 rtx x;
1122 /* Check that we are not being given an automatic variable. */
1123 gcc_assert (TREE_CODE (decl) != PARM_DECL
1124 && TREE_CODE (decl) != RESULT_DECL);
1126 /* A weak alias has TREE_PUBLIC set but not the other bits. */
1127 gcc_assert (TREE_CODE (decl) != VAR_DECL
1128 || TREE_STATIC (decl)
1129 || TREE_PUBLIC (decl)
1130 || DECL_EXTERNAL (decl)
1131 || DECL_REGISTER (decl));
1133 /* And that we were not given a type or a label. */
1134 gcc_assert (TREE_CODE (decl) != TYPE_DECL
1135 && TREE_CODE (decl) != LABEL_DECL);
1137 /* For a duplicate declaration, we can be called twice on the
1138 same DECL node. Don't discard the RTL already made. */
1139 if (DECL_RTL_SET_P (decl))
1141 /* If the old RTL had the wrong mode, fix the mode. */
1142 x = DECL_RTL (decl);
1143 if (GET_MODE (x) != DECL_MODE (decl))
1144 SET_DECL_RTL (decl, adjust_address_nv (x, DECL_MODE (decl), 0));
1146 if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1147 return;
1149 /* ??? Another way to do this would be to maintain a hashed
1150 table of such critters. Instead of adding stuff to a DECL
1151 to give certain attributes to it, we could use an external
1152 hash map from DECL to set of attributes. */
1154 /* Let the target reassign the RTL if it wants.
1155 This is necessary, for example, when one machine specific
1156 decl attribute overrides another. */
1157 targetm.encode_section_info (decl, DECL_RTL (decl), false);
1159 /* If the symbol has a SYMBOL_REF_BLOCK field, update it based
1160 on the new decl information. */
1161 if (MEM_P (x)
1162 && GET_CODE (XEXP (x, 0)) == SYMBOL_REF
1163 && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (x, 0)))
1164 change_symbol_block (XEXP (x, 0), get_block_for_decl (decl));
1166 /* Make this function static known to the mudflap runtime. */
1167 if (flag_mudflap && TREE_CODE (decl) == VAR_DECL)
1168 mudflap_enqueue_decl (decl);
1170 return;
1173 /* If this variable belongs to the global constant pool, retrieve the
1174 pre-computed RTL or recompute it in LTO mode. */
1175 if (TREE_CODE (decl) == VAR_DECL && DECL_IN_CONSTANT_POOL (decl))
1177 SET_DECL_RTL (decl, output_constant_def (DECL_INITIAL (decl), 1));
1178 return;
1181 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
1183 if (name[0] != '*' && TREE_CODE (decl) != FUNCTION_DECL
1184 && DECL_REGISTER (decl))
1186 error ("register name not specified for %q+D", decl);
1188 else if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1190 const char *asmspec = name+1;
1191 reg_number = decode_reg_name (asmspec);
1192 /* First detect errors in declaring global registers. */
1193 if (reg_number == -1)
1194 error ("register name not specified for %q+D", decl);
1195 else if (reg_number < 0)
1196 error ("invalid register name for %q+D", decl);
1197 else if (TYPE_MODE (TREE_TYPE (decl)) == BLKmode)
1198 error ("data type of %q+D isn%'t suitable for a register",
1199 decl);
1200 else if (! HARD_REGNO_MODE_OK (reg_number, TYPE_MODE (TREE_TYPE (decl))))
1201 error ("register specified for %q+D isn%'t suitable for data type",
1202 decl);
1203 /* Now handle properly declared static register variables. */
1204 else
1206 int nregs;
1208 if (DECL_INITIAL (decl) != 0 && TREE_STATIC (decl))
1210 DECL_INITIAL (decl) = 0;
1211 error ("global register variable has initial value");
1213 if (TREE_THIS_VOLATILE (decl))
1214 warning (OPT_Wvolatile_register_var,
1215 "optimization may eliminate reads and/or "
1216 "writes to register variables");
1218 /* If the user specified one of the eliminables registers here,
1219 e.g., FRAME_POINTER_REGNUM, we don't want to get this variable
1220 confused with that register and be eliminated. This usage is
1221 somewhat suspect... */
1223 SET_DECL_RTL (decl, gen_rtx_raw_REG (DECL_MODE (decl), reg_number));
1224 ORIGINAL_REGNO (DECL_RTL (decl)) = reg_number;
1225 REG_USERVAR_P (DECL_RTL (decl)) = 1;
1227 if (TREE_STATIC (decl))
1229 /* Make this register global, so not usable for anything
1230 else. */
1231 #ifdef ASM_DECLARE_REGISTER_GLOBAL
1232 name = IDENTIFIER_POINTER (DECL_NAME (decl));
1233 ASM_DECLARE_REGISTER_GLOBAL (asm_out_file, decl, reg_number, name);
1234 #endif
1235 nregs = hard_regno_nregs[reg_number][DECL_MODE (decl)];
1236 while (nregs > 0)
1237 globalize_reg (reg_number + --nregs);
1240 /* As a register variable, it has no section. */
1241 return;
1244 /* Now handle ordinary static variables and functions (in memory).
1245 Also handle vars declared register invalidly. */
1246 else if (name[0] == '*')
1248 #ifdef REGISTER_PREFIX
1249 if (strlen (REGISTER_PREFIX) != 0)
1251 reg_number = decode_reg_name (name);
1252 if (reg_number >= 0 || reg_number == -3)
1253 error ("register name given for non-register variable %q+D", decl);
1255 #endif
1258 /* Specifying a section attribute on a variable forces it into a
1259 non-.bss section, and thus it cannot be common. */
1260 /* FIXME: In general this code should not be necessary because
1261 visibility pass is doing the same work. But notice_global_symbol
1262 is called early and it needs to make DECL_RTL to get the name.
1263 we take care of recomputing the DECL_RTL after visibility is changed. */
1264 if (TREE_CODE (decl) == VAR_DECL
1265 && DECL_SECTION_NAME (decl) != NULL_TREE
1266 && DECL_INITIAL (decl) == NULL_TREE
1267 && DECL_COMMON (decl))
1268 DECL_COMMON (decl) = 0;
1270 /* Variables can't be both common and weak. */
1271 if (TREE_CODE (decl) == VAR_DECL && DECL_WEAK (decl))
1272 DECL_COMMON (decl) = 0;
1274 if (use_object_blocks_p () && use_blocks_for_decl_p (decl))
1275 x = create_block_symbol (name, get_block_for_decl (decl), -1);
1276 else
1278 enum machine_mode address_mode = Pmode;
1279 if (TREE_TYPE (decl) != error_mark_node)
1281 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
1282 address_mode = targetm.addr_space.address_mode (as);
1284 x = gen_rtx_SYMBOL_REF (address_mode, name);
1286 SYMBOL_REF_WEAK (x) = DECL_WEAK (decl);
1287 SET_SYMBOL_REF_DECL (x, decl);
1289 x = gen_rtx_MEM (DECL_MODE (decl), x);
1290 if (TREE_CODE (decl) != FUNCTION_DECL)
1291 set_mem_attributes (x, decl, 1);
1292 SET_DECL_RTL (decl, x);
1294 /* Optionally set flags or add text to the name to record information
1295 such as that it is a function name.
1296 If the name is changed, the macro ASM_OUTPUT_LABELREF
1297 will have to know how to strip this information. */
1298 targetm.encode_section_info (decl, DECL_RTL (decl), true);
1300 /* Make this function static known to the mudflap runtime. */
1301 if (flag_mudflap && TREE_CODE (decl) == VAR_DECL)
1302 mudflap_enqueue_decl (decl);
1305 /* Like make_decl_rtl, but inhibit creation of new alias sets when
1306 calling make_decl_rtl. Also, reset DECL_RTL before returning the
1307 rtl. */
1310 make_decl_rtl_for_debug (tree decl)
1312 unsigned int save_aliasing_flag, save_mudflap_flag;
1313 rtx rtl;
1315 if (DECL_RTL_SET_P (decl))
1316 return DECL_RTL (decl);
1318 /* Kludge alert! Somewhere down the call chain, make_decl_rtl will
1319 call new_alias_set. If running with -fcompare-debug, sometimes
1320 we do not want to create alias sets that will throw the alias
1321 numbers off in the comparison dumps. So... clearing
1322 flag_strict_aliasing will keep new_alias_set() from creating a
1323 new set. It is undesirable to register decl with mudflap
1324 in this case as well. */
1325 save_aliasing_flag = flag_strict_aliasing;
1326 flag_strict_aliasing = 0;
1327 save_mudflap_flag = flag_mudflap;
1328 flag_mudflap = 0;
1330 rtl = DECL_RTL (decl);
1331 /* Reset DECL_RTL back, as various parts of the compiler expects
1332 DECL_RTL set meaning it is actually going to be output. */
1333 SET_DECL_RTL (decl, NULL);
1335 flag_strict_aliasing = save_aliasing_flag;
1336 flag_mudflap = save_mudflap_flag;
1338 return rtl;
1341 /* Output a string of literal assembler code
1342 for an `asm' keyword used between functions. */
1344 void
1345 assemble_asm (tree string)
1347 app_enable ();
1349 if (TREE_CODE (string) == ADDR_EXPR)
1350 string = TREE_OPERAND (string, 0);
1352 fprintf (asm_out_file, "\t%s\n", TREE_STRING_POINTER (string));
1355 /* Record an element in the table of global destructors. SYMBOL is
1356 a SYMBOL_REF of the function to be called; PRIORITY is a number
1357 between 0 and MAX_INIT_PRIORITY. */
1359 void
1360 default_stabs_asm_out_destructor (rtx symbol ATTRIBUTE_UNUSED,
1361 int priority ATTRIBUTE_UNUSED)
1363 #if defined DBX_DEBUGGING_INFO || defined XCOFF_DEBUGGING_INFO
1364 /* Tell GNU LD that this is part of the static destructor set.
1365 This will work for any system that uses stabs, most usefully
1366 aout systems. */
1367 dbxout_begin_simple_stabs ("___DTOR_LIST__", 22 /* N_SETT */);
1368 dbxout_stab_value_label (XSTR (symbol, 0));
1369 #else
1370 sorry ("global destructors not supported on this target");
1371 #endif
1374 /* Write the address of the entity given by SYMBOL to SEC. */
1375 void
1376 assemble_addr_to_section (rtx symbol, section *sec)
1378 switch_to_section (sec);
1379 assemble_align (POINTER_SIZE);
1380 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1383 /* Return the numbered .ctors.N (if CONSTRUCTOR_P) or .dtors.N (if
1384 not) section for PRIORITY. */
1385 section *
1386 get_cdtor_priority_section (int priority, bool constructor_p)
1388 char buf[16];
1390 /* ??? This only works reliably with the GNU linker. */
1391 sprintf (buf, "%s.%.5u",
1392 constructor_p ? ".ctors" : ".dtors",
1393 /* Invert the numbering so the linker puts us in the proper
1394 order; constructors are run from right to left, and the
1395 linker sorts in increasing order. */
1396 MAX_INIT_PRIORITY - priority);
1397 return get_section (buf, SECTION_WRITE, NULL);
1400 void
1401 default_named_section_asm_out_destructor (rtx symbol, int priority)
1403 section *sec;
1405 if (priority != DEFAULT_INIT_PRIORITY)
1406 sec = get_cdtor_priority_section (priority,
1407 /*constructor_p=*/false);
1408 else
1409 sec = get_section (".dtors", SECTION_WRITE, NULL);
1411 assemble_addr_to_section (symbol, sec);
1414 #ifdef DTORS_SECTION_ASM_OP
1415 void
1416 default_dtor_section_asm_out_destructor (rtx symbol,
1417 int priority ATTRIBUTE_UNUSED)
1419 assemble_addr_to_section (symbol, dtors_section);
1421 #endif
1423 /* Likewise for global constructors. */
1425 void
1426 default_stabs_asm_out_constructor (rtx symbol ATTRIBUTE_UNUSED,
1427 int priority ATTRIBUTE_UNUSED)
1429 #if defined DBX_DEBUGGING_INFO || defined XCOFF_DEBUGGING_INFO
1430 /* Tell GNU LD that this is part of the static destructor set.
1431 This will work for any system that uses stabs, most usefully
1432 aout systems. */
1433 dbxout_begin_simple_stabs ("___CTOR_LIST__", 22 /* N_SETT */);
1434 dbxout_stab_value_label (XSTR (symbol, 0));
1435 #else
1436 sorry ("global constructors not supported on this target");
1437 #endif
1440 void
1441 default_named_section_asm_out_constructor (rtx symbol, int priority)
1443 section *sec;
1445 if (priority != DEFAULT_INIT_PRIORITY)
1446 sec = get_cdtor_priority_section (priority,
1447 /*constructor_p=*/true);
1448 else
1449 sec = get_section (".ctors", SECTION_WRITE, NULL);
1451 assemble_addr_to_section (symbol, sec);
1454 #ifdef CTORS_SECTION_ASM_OP
1455 void
1456 default_ctor_section_asm_out_constructor (rtx symbol,
1457 int priority ATTRIBUTE_UNUSED)
1459 assemble_addr_to_section (symbol, ctors_section);
1461 #endif
1463 /* CONSTANT_POOL_BEFORE_FUNCTION may be defined as an expression with
1464 a nonzero value if the constant pool should be output before the
1465 start of the function, or a zero value if the pool should output
1466 after the end of the function. The default is to put it before the
1467 start. */
1469 #ifndef CONSTANT_POOL_BEFORE_FUNCTION
1470 #define CONSTANT_POOL_BEFORE_FUNCTION 1
1471 #endif
1473 /* DECL is an object (either VAR_DECL or FUNCTION_DECL) which is going
1474 to be output to assembler.
1475 Set first_global_object_name and weak_global_object_name as appropriate. */
1477 void
1478 notice_global_symbol (tree decl)
1480 const char **type = &first_global_object_name;
1482 if (first_global_object_name
1483 || !TREE_PUBLIC (decl)
1484 || DECL_EXTERNAL (decl)
1485 || !DECL_NAME (decl)
1486 || (TREE_CODE (decl) != FUNCTION_DECL
1487 && (TREE_CODE (decl) != VAR_DECL
1488 || (DECL_COMMON (decl)
1489 && (DECL_INITIAL (decl) == 0
1490 || DECL_INITIAL (decl) == error_mark_node))))
1491 || !MEM_P (DECL_RTL (decl)))
1492 return;
1494 /* We win when global object is found, but it is useful to know about weak
1495 symbol as well so we can produce nicer unique names. */
1496 if (DECL_WEAK (decl) || DECL_ONE_ONLY (decl) || flag_shlib)
1497 type = &weak_global_object_name;
1499 if (!*type)
1501 const char *p;
1502 const char *name;
1503 rtx decl_rtl = DECL_RTL (decl);
1505 p = targetm.strip_name_encoding (XSTR (XEXP (decl_rtl, 0), 0));
1506 name = ggc_strdup (p);
1508 *type = name;
1512 /* Output assembler code for the constant pool of a function and associated
1513 with defining the name of the function. DECL describes the function.
1514 NAME is the function's name. For the constant pool, we use the current
1515 constant pool data. */
1517 void
1518 assemble_start_function (tree decl, const char *fnname)
1520 int align;
1521 char tmp_label[100];
1522 bool hot_label_written = false;
1524 first_function_block_is_cold = false;
1525 if (flag_reorder_blocks_and_partition)
1527 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTB", const_labelno);
1528 crtl->subsections.hot_section_label = ggc_strdup (tmp_label);
1529 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDB", const_labelno);
1530 crtl->subsections.cold_section_label = ggc_strdup (tmp_label);
1531 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTE", const_labelno);
1532 crtl->subsections.hot_section_end_label = ggc_strdup (tmp_label);
1533 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDE", const_labelno);
1534 crtl->subsections.cold_section_end_label = ggc_strdup (tmp_label);
1535 const_labelno++;
1537 else
1539 crtl->subsections.hot_section_label = NULL;
1540 crtl->subsections.cold_section_label = NULL;
1541 crtl->subsections.hot_section_end_label = NULL;
1542 crtl->subsections.cold_section_end_label = NULL;
1545 /* The following code does not need preprocessing in the assembler. */
1547 app_disable ();
1549 if (CONSTANT_POOL_BEFORE_FUNCTION)
1550 output_constant_pool (fnname, decl);
1552 /* Make sure the not and cold text (code) sections are properly
1553 aligned. This is necessary here in the case where the function
1554 has both hot and cold sections, because we don't want to re-set
1555 the alignment when the section switch happens mid-function. */
1557 if (flag_reorder_blocks_and_partition)
1559 switch_to_section (unlikely_text_section ());
1560 assemble_align (DECL_ALIGN (decl));
1561 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_label);
1563 /* When the function starts with a cold section, we need to explicitly
1564 align the hot section and write out the hot section label.
1565 But if the current function is a thunk, we do not have a CFG. */
1566 if (!cfun->is_thunk
1567 && BB_PARTITION (ENTRY_BLOCK_PTR->next_bb) == BB_COLD_PARTITION)
1569 switch_to_section (text_section);
1570 assemble_align (DECL_ALIGN (decl));
1571 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
1572 hot_label_written = true;
1573 first_function_block_is_cold = true;
1576 else if (DECL_SECTION_NAME (decl))
1578 /* Calls to function_section rely on first_function_block_is_cold
1579 being accurate. */
1580 first_function_block_is_cold
1581 = (cgraph_node (current_function_decl)->frequency
1582 == NODE_FREQUENCY_UNLIKELY_EXECUTED);
1585 in_cold_section_p = first_function_block_is_cold;
1587 /* Switch to the correct text section for the start of the function. */
1589 switch_to_section (function_section (decl));
1590 if (flag_reorder_blocks_and_partition
1591 && !hot_label_written)
1592 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
1594 /* Tell assembler to move to target machine's alignment for functions. */
1595 align = floor_log2 (DECL_ALIGN (decl) / BITS_PER_UNIT);
1596 if (align > 0)
1598 ASM_OUTPUT_ALIGN (asm_out_file, align);
1601 /* Handle a user-specified function alignment.
1602 Note that we still need to align to DECL_ALIGN, as above,
1603 because ASM_OUTPUT_MAX_SKIP_ALIGN might not do any alignment at all. */
1604 if (! DECL_USER_ALIGN (decl)
1605 && align_functions_log > align
1606 && optimize_function_for_speed_p (cfun))
1608 #ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
1609 ASM_OUTPUT_MAX_SKIP_ALIGN (asm_out_file,
1610 align_functions_log, align_functions - 1);
1611 #else
1612 ASM_OUTPUT_ALIGN (asm_out_file, align_functions_log);
1613 #endif
1616 #ifdef ASM_OUTPUT_FUNCTION_PREFIX
1617 ASM_OUTPUT_FUNCTION_PREFIX (asm_out_file, fnname);
1618 #endif
1620 if (!DECL_IGNORED_P (decl))
1621 (*debug_hooks->begin_function) (decl);
1623 /* Make function name accessible from other files, if appropriate. */
1625 if (TREE_PUBLIC (decl))
1627 notice_global_symbol (decl);
1629 globalize_decl (decl);
1631 maybe_assemble_visibility (decl);
1634 if (DECL_PRESERVE_P (decl))
1635 targetm.asm_out.mark_decl_preserved (fnname);
1637 /* Do any machine/system dependent processing of the function name. */
1638 #ifdef ASM_DECLARE_FUNCTION_NAME
1639 ASM_DECLARE_FUNCTION_NAME (asm_out_file, fnname, current_function_decl);
1640 #else
1641 /* Standard thing is just output label for the function. */
1642 ASM_OUTPUT_FUNCTION_LABEL (asm_out_file, fnname, current_function_decl);
1643 #endif /* ASM_DECLARE_FUNCTION_NAME */
1645 if (lookup_attribute ("no_split_stack", DECL_ATTRIBUTES (decl)))
1646 saw_no_split_stack = true;
1649 /* Output assembler code associated with defining the size of the
1650 function. DECL describes the function. NAME is the function's name. */
1652 void
1653 assemble_end_function (tree decl, const char *fnname ATTRIBUTE_UNUSED)
1655 #ifdef ASM_DECLARE_FUNCTION_SIZE
1656 /* We could have switched section in the middle of the function. */
1657 if (flag_reorder_blocks_and_partition)
1658 switch_to_section (function_section (decl));
1659 ASM_DECLARE_FUNCTION_SIZE (asm_out_file, fnname, decl);
1660 #endif
1661 if (! CONSTANT_POOL_BEFORE_FUNCTION)
1663 output_constant_pool (fnname, decl);
1664 switch_to_section (function_section (decl)); /* need to switch back */
1666 /* Output labels for end of hot/cold text sections (to be used by
1667 debug info.) */
1668 if (flag_reorder_blocks_and_partition)
1670 section *save_text_section;
1672 save_text_section = in_section;
1673 switch_to_section (unlikely_text_section ());
1674 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_end_label);
1675 if (first_function_block_is_cold)
1676 switch_to_section (text_section);
1677 else
1678 switch_to_section (function_section (decl));
1679 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_end_label);
1680 switch_to_section (save_text_section);
1684 /* Assemble code to leave SIZE bytes of zeros. */
1686 void
1687 assemble_zeros (unsigned HOST_WIDE_INT size)
1689 /* Do no output if -fsyntax-only. */
1690 if (flag_syntax_only)
1691 return;
1693 #ifdef ASM_NO_SKIP_IN_TEXT
1694 /* The `space' pseudo in the text section outputs nop insns rather than 0s,
1695 so we must output 0s explicitly in the text section. */
1696 if (ASM_NO_SKIP_IN_TEXT && (in_section->common.flags & SECTION_CODE) != 0)
1698 unsigned HOST_WIDE_INT i;
1699 for (i = 0; i < size; i++)
1700 assemble_integer (const0_rtx, 1, BITS_PER_UNIT, 1);
1702 else
1703 #endif
1704 if (size > 0)
1705 ASM_OUTPUT_SKIP (asm_out_file, size);
1708 /* Assemble an alignment pseudo op for an ALIGN-bit boundary. */
1710 void
1711 assemble_align (int align)
1713 if (align > BITS_PER_UNIT)
1715 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
1719 /* Assemble a string constant with the specified C string as contents. */
1721 void
1722 assemble_string (const char *p, int size)
1724 int pos = 0;
1725 int maximum = 2000;
1727 /* If the string is very long, split it up. */
1729 while (pos < size)
1731 int thissize = size - pos;
1732 if (thissize > maximum)
1733 thissize = maximum;
1735 ASM_OUTPUT_ASCII (asm_out_file, p, thissize);
1737 pos += thissize;
1738 p += thissize;
1743 /* A noswitch_section_callback for lcomm_section. */
1745 static bool
1746 emit_local (tree decl ATTRIBUTE_UNUSED,
1747 const char *name ATTRIBUTE_UNUSED,
1748 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1749 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1751 #if defined ASM_OUTPUT_ALIGNED_DECL_LOCAL
1752 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, decl, name,
1753 size, DECL_ALIGN (decl));
1754 return true;
1755 #elif defined ASM_OUTPUT_ALIGNED_LOCAL
1756 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, DECL_ALIGN (decl));
1757 return true;
1758 #else
1759 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
1760 return false;
1761 #endif
1764 /* A noswitch_section_callback for bss_noswitch_section. */
1766 #if defined ASM_OUTPUT_ALIGNED_BSS
1767 static bool
1768 emit_bss (tree decl ATTRIBUTE_UNUSED,
1769 const char *name ATTRIBUTE_UNUSED,
1770 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1771 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1773 #if defined ASM_OUTPUT_ALIGNED_BSS
1774 ASM_OUTPUT_ALIGNED_BSS (asm_out_file, decl, name, size, DECL_ALIGN (decl));
1775 return true;
1776 #endif
1778 #endif
1780 /* A noswitch_section_callback for comm_section. */
1782 static bool
1783 emit_common (tree decl ATTRIBUTE_UNUSED,
1784 const char *name ATTRIBUTE_UNUSED,
1785 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1786 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1788 #if defined ASM_OUTPUT_ALIGNED_DECL_COMMON
1789 ASM_OUTPUT_ALIGNED_DECL_COMMON (asm_out_file, decl, name,
1790 size, DECL_ALIGN (decl));
1791 return true;
1792 #elif defined ASM_OUTPUT_ALIGNED_COMMON
1793 ASM_OUTPUT_ALIGNED_COMMON (asm_out_file, name, size, DECL_ALIGN (decl));
1794 return true;
1795 #else
1796 ASM_OUTPUT_COMMON (asm_out_file, name, size, rounded);
1797 return false;
1798 #endif
1801 /* A noswitch_section_callback for tls_comm_section. */
1803 static bool
1804 emit_tls_common (tree decl ATTRIBUTE_UNUSED,
1805 const char *name ATTRIBUTE_UNUSED,
1806 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1807 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1809 #ifdef ASM_OUTPUT_TLS_COMMON
1810 ASM_OUTPUT_TLS_COMMON (asm_out_file, decl, name, size);
1811 return true;
1812 #else
1813 sorry ("thread-local COMMON data not implemented");
1814 return true;
1815 #endif
1818 /* Assemble DECL given that it belongs in SECTION_NOSWITCH section SECT.
1819 NAME is the name of DECL's SYMBOL_REF. */
1821 static void
1822 assemble_noswitch_variable (tree decl, const char *name, section *sect)
1824 unsigned HOST_WIDE_INT size, rounded;
1826 size = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
1827 rounded = size;
1829 /* Don't allocate zero bytes of common,
1830 since that means "undefined external" in the linker. */
1831 if (size == 0)
1832 rounded = 1;
1834 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
1835 so that each uninitialized object starts on such a boundary. */
1836 rounded += (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1;
1837 rounded = (rounded / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
1838 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
1840 if (!sect->noswitch.callback (decl, name, size, rounded)
1841 && (unsigned HOST_WIDE_INT) DECL_ALIGN_UNIT (decl) > rounded)
1842 warning (0, "requested alignment for %q+D is greater than "
1843 "implemented alignment of %wu", decl, rounded);
1846 /* A subroutine of assemble_variable. Output the label and contents of
1847 DECL, whose address is a SYMBOL_REF with name NAME. DONT_OUTPUT_DATA
1848 is as for assemble_variable. */
1850 static void
1851 assemble_variable_contents (tree decl, const char *name,
1852 bool dont_output_data)
1854 /* Do any machine/system dependent processing of the object. */
1855 #ifdef ASM_DECLARE_OBJECT_NAME
1856 last_assemble_variable_decl = decl;
1857 ASM_DECLARE_OBJECT_NAME (asm_out_file, name, decl);
1858 #else
1859 /* Standard thing is just output label for the object. */
1860 ASM_OUTPUT_LABEL (asm_out_file, name);
1861 #endif /* ASM_DECLARE_OBJECT_NAME */
1863 if (!dont_output_data)
1865 if (DECL_INITIAL (decl)
1866 && DECL_INITIAL (decl) != error_mark_node
1867 && !initializer_zerop (DECL_INITIAL (decl)))
1868 /* Output the actual data. */
1869 output_constant (DECL_INITIAL (decl),
1870 tree_low_cst (DECL_SIZE_UNIT (decl), 1),
1871 DECL_ALIGN (decl));
1872 else
1873 /* Leave space for it. */
1874 assemble_zeros (tree_low_cst (DECL_SIZE_UNIT (decl), 1));
1878 /* Assemble everything that is needed for a variable or function declaration.
1879 Not used for automatic variables, and not used for function definitions.
1880 Should not be called for variables of incomplete structure type.
1882 TOP_LEVEL is nonzero if this variable has file scope.
1883 AT_END is nonzero if this is the special handling, at end of compilation,
1884 to define things that have had only tentative definitions.
1885 DONT_OUTPUT_DATA if nonzero means don't actually output the
1886 initial value (that will be done by the caller). */
1888 void
1889 assemble_variable (tree decl, int top_level ATTRIBUTE_UNUSED,
1890 int at_end ATTRIBUTE_UNUSED, int dont_output_data)
1892 const char *name;
1893 rtx decl_rtl, symbol;
1894 section *sect;
1896 /* This function is supposed to handle VARIABLES. Ensure we have one. */
1897 gcc_assert (TREE_CODE (decl) == VAR_DECL);
1899 /* Emulated TLS had better not get this far. */
1900 gcc_checking_assert (targetm.have_tls || !DECL_THREAD_LOCAL_P (decl));
1902 last_assemble_variable_decl = 0;
1904 /* Normally no need to say anything here for external references,
1905 since assemble_external is called by the language-specific code
1906 when a declaration is first seen. */
1908 if (DECL_EXTERNAL (decl))
1909 return;
1911 /* Do nothing for global register variables. */
1912 if (DECL_RTL_SET_P (decl) && REG_P (DECL_RTL (decl)))
1914 TREE_ASM_WRITTEN (decl) = 1;
1915 return;
1918 /* If type was incomplete when the variable was declared,
1919 see if it is complete now. */
1921 if (DECL_SIZE (decl) == 0)
1922 layout_decl (decl, 0);
1924 /* Still incomplete => don't allocate it; treat the tentative defn
1925 (which is what it must have been) as an `extern' reference. */
1927 if (!dont_output_data && DECL_SIZE (decl) == 0)
1929 error ("storage size of %q+D isn%'t known", decl);
1930 TREE_ASM_WRITTEN (decl) = 1;
1931 return;
1934 /* The first declaration of a variable that comes through this function
1935 decides whether it is global (in C, has external linkage)
1936 or local (in C, has internal linkage). So do nothing more
1937 if this function has already run. */
1939 if (TREE_ASM_WRITTEN (decl))
1940 return;
1942 /* Make sure targetm.encode_section_info is invoked before we set
1943 ASM_WRITTEN. */
1944 decl_rtl = DECL_RTL (decl);
1946 TREE_ASM_WRITTEN (decl) = 1;
1948 /* Do no output if -fsyntax-only. */
1949 if (flag_syntax_only)
1950 return;
1952 if (! dont_output_data
1953 && ! host_integerp (DECL_SIZE_UNIT (decl), 1))
1955 error ("size of variable %q+D is too large", decl);
1956 return;
1959 gcc_assert (MEM_P (decl_rtl));
1960 gcc_assert (GET_CODE (XEXP (decl_rtl, 0)) == SYMBOL_REF);
1961 symbol = XEXP (decl_rtl, 0);
1963 /* If this symbol belongs to the tree constant pool, output the constant
1964 if it hasn't already been written. */
1965 if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
1967 tree decl = SYMBOL_REF_DECL (symbol);
1968 if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
1969 output_constant_def_contents (symbol);
1970 return;
1973 app_disable ();
1975 name = XSTR (symbol, 0);
1976 if (TREE_PUBLIC (decl) && DECL_NAME (decl))
1977 notice_global_symbol (decl);
1979 /* Compute the alignment of this data. */
1981 align_variable (decl, dont_output_data);
1982 set_mem_align (decl_rtl, DECL_ALIGN (decl));
1984 if (TREE_PUBLIC (decl))
1985 maybe_assemble_visibility (decl);
1987 if (DECL_PRESERVE_P (decl))
1988 targetm.asm_out.mark_decl_preserved (name);
1990 /* First make the assembler name(s) global if appropriate. */
1991 sect = get_variable_section (decl, false);
1992 if (TREE_PUBLIC (decl)
1993 && (sect->common.flags & SECTION_COMMON) == 0)
1994 globalize_decl (decl);
1996 /* Output any data that we will need to use the address of. */
1997 if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node)
1998 output_addressed_constants (DECL_INITIAL (decl));
2000 /* dbxout.c needs to know this. */
2001 if (sect && (sect->common.flags & SECTION_CODE) != 0)
2002 DECL_IN_TEXT_SECTION (decl) = 1;
2004 /* If the decl is part of an object_block, make sure that the decl
2005 has been positioned within its block, but do not write out its
2006 definition yet. output_object_blocks will do that later. */
2007 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
2009 gcc_assert (!dont_output_data);
2010 place_block_symbol (symbol);
2012 else if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
2013 assemble_noswitch_variable (decl, name, sect);
2014 else
2016 switch_to_section (sect);
2017 if (DECL_ALIGN (decl) > BITS_PER_UNIT)
2018 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (DECL_ALIGN_UNIT (decl)));
2019 assemble_variable_contents (decl, name, dont_output_data);
2023 /* Return 1 if type TYPE contains any pointers. */
2025 static int
2026 contains_pointers_p (tree type)
2028 switch (TREE_CODE (type))
2030 case POINTER_TYPE:
2031 case REFERENCE_TYPE:
2032 /* I'm not sure whether OFFSET_TYPE needs this treatment,
2033 so I'll play safe and return 1. */
2034 case OFFSET_TYPE:
2035 return 1;
2037 case RECORD_TYPE:
2038 case UNION_TYPE:
2039 case QUAL_UNION_TYPE:
2041 tree fields;
2042 /* For a type that has fields, see if the fields have pointers. */
2043 for (fields = TYPE_FIELDS (type); fields; fields = DECL_CHAIN (fields))
2044 if (TREE_CODE (fields) == FIELD_DECL
2045 && contains_pointers_p (TREE_TYPE (fields)))
2046 return 1;
2047 return 0;
2050 case ARRAY_TYPE:
2051 /* An array type contains pointers if its element type does. */
2052 return contains_pointers_p (TREE_TYPE (type));
2054 default:
2055 return 0;
2059 /* We delay assemble_external processing until
2060 the compilation unit is finalized. This is the best we can do for
2061 right now (i.e. stage 3 of GCC 4.0) - the right thing is to delay
2062 it all the way to final. See PR 17982 for further discussion. */
2063 static GTY(()) tree pending_assemble_externals;
2065 #ifdef ASM_OUTPUT_EXTERNAL
2066 /* True if DECL is a function decl for which no out-of-line copy exists.
2067 It is assumed that DECL's assembler name has been set. */
2069 static bool
2070 incorporeal_function_p (tree decl)
2072 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
2074 const char *name;
2076 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
2077 && DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA)
2078 return true;
2080 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2081 if (is_builtin_name (name))
2082 return true;
2084 return false;
2087 /* Actually do the tests to determine if this is necessary, and invoke
2088 ASM_OUTPUT_EXTERNAL. */
2089 static void
2090 assemble_external_real (tree decl)
2092 rtx rtl = DECL_RTL (decl);
2094 if (MEM_P (rtl) && GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF
2095 && !SYMBOL_REF_USED (XEXP (rtl, 0))
2096 && !incorporeal_function_p (decl))
2098 /* Some systems do require some output. */
2099 SYMBOL_REF_USED (XEXP (rtl, 0)) = 1;
2100 ASM_OUTPUT_EXTERNAL (asm_out_file, decl, XSTR (XEXP (rtl, 0), 0));
2103 #endif
2105 void
2106 process_pending_assemble_externals (void)
2108 #ifdef ASM_OUTPUT_EXTERNAL
2109 tree list;
2110 for (list = pending_assemble_externals; list; list = TREE_CHAIN (list))
2111 assemble_external_real (TREE_VALUE (list));
2113 pending_assemble_externals = 0;
2114 #endif
2117 /* This TREE_LIST contains any weak symbol declarations waiting
2118 to be emitted. */
2119 static GTY(()) tree weak_decls;
2121 /* Output something to declare an external symbol to the assembler,
2122 and qualifiers such as weakness. (Most assemblers don't need
2123 extern declaration, so we normally output nothing.) Do nothing if
2124 DECL is not external. */
2126 void
2127 assemble_external (tree decl ATTRIBUTE_UNUSED)
2129 /* Because most platforms do not define ASM_OUTPUT_EXTERNAL, the
2130 main body of this code is only rarely exercised. To provide some
2131 testing, on all platforms, we make sure that the ASM_OUT_FILE is
2132 open. If it's not, we should not be calling this function. */
2133 gcc_assert (asm_out_file);
2135 if (!DECL_P (decl) || !DECL_EXTERNAL (decl) || !TREE_PUBLIC (decl))
2136 return;
2138 /* We want to output annotation for weak and external symbols at
2139 very last to check if they are references or not. */
2141 if (TARGET_SUPPORTS_WEAK
2142 && DECL_WEAK (decl)
2143 /* TREE_STATIC is a weird and abused creature which is not
2144 generally the right test for whether an entity has been
2145 locally emitted, inlined or otherwise not-really-extern, but
2146 for declarations that can be weak, it happens to be
2147 match. */
2148 && !TREE_STATIC (decl)
2149 && lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
2150 && value_member (decl, weak_decls) == NULL_TREE)
2151 weak_decls = tree_cons (NULL, decl, weak_decls);
2153 #ifdef ASM_OUTPUT_EXTERNAL
2154 if (value_member (decl, pending_assemble_externals) == NULL_TREE)
2155 pending_assemble_externals = tree_cons (NULL, decl,
2156 pending_assemble_externals);
2157 #endif
2160 /* Similar, for calling a library function FUN. */
2162 void
2163 assemble_external_libcall (rtx fun)
2165 /* Declare library function name external when first used, if nec. */
2166 if (! SYMBOL_REF_USED (fun))
2168 SYMBOL_REF_USED (fun) = 1;
2169 targetm.asm_out.external_libcall (fun);
2173 /* Assemble a label named NAME. */
2175 void
2176 assemble_label (FILE *file, const char *name)
2178 ASM_OUTPUT_LABEL (file, name);
2181 /* Set the symbol_referenced flag for ID. */
2182 void
2183 mark_referenced (tree id)
2185 TREE_SYMBOL_REFERENCED (id) = 1;
2188 /* Set the symbol_referenced flag for DECL and notify callgraph. */
2189 void
2190 mark_decl_referenced (tree decl)
2192 if (TREE_CODE (decl) == FUNCTION_DECL)
2194 /* Extern inline functions don't become needed when referenced.
2195 If we know a method will be emitted in other TU and no new
2196 functions can be marked reachable, just use the external
2197 definition. */
2198 struct cgraph_node *node = cgraph_node (decl);
2199 if (!DECL_EXTERNAL (decl)
2200 && (!node->local.vtable_method || !cgraph_global_info_ready
2201 || !node->local.finalized))
2202 cgraph_mark_needed_node (node);
2204 else if (TREE_CODE (decl) == VAR_DECL)
2206 struct varpool_node *node = varpool_node (decl);
2207 varpool_mark_needed_node (node);
2208 /* C++ frontend use mark_decl_references to force COMDAT variables
2209 to be output that might appear dead otherwise. */
2210 node->force_output = true;
2212 /* else do nothing - we can get various sorts of CST nodes here,
2213 which do not need to be marked. */
2217 /* Follow the IDENTIFIER_TRANSPARENT_ALIAS chain starting at *ALIAS
2218 until we find an identifier that is not itself a transparent alias.
2219 Modify the alias passed to it by reference (and all aliases on the
2220 way to the ultimate target), such that they do not have to be
2221 followed again, and return the ultimate target of the alias
2222 chain. */
2224 static inline tree
2225 ultimate_transparent_alias_target (tree *alias)
2227 tree target = *alias;
2229 if (IDENTIFIER_TRANSPARENT_ALIAS (target))
2231 gcc_assert (TREE_CHAIN (target));
2232 target = ultimate_transparent_alias_target (&TREE_CHAIN (target));
2233 gcc_assert (! IDENTIFIER_TRANSPARENT_ALIAS (target)
2234 && ! TREE_CHAIN (target));
2235 *alias = target;
2238 return target;
2241 /* Output to FILE (an assembly file) a reference to NAME. If NAME
2242 starts with a *, the rest of NAME is output verbatim. Otherwise
2243 NAME is transformed in a target-specific way (usually by the
2244 addition of an underscore). */
2246 void
2247 assemble_name_raw (FILE *file, const char *name)
2249 if (name[0] == '*')
2250 fputs (&name[1], file);
2251 else
2252 ASM_OUTPUT_LABELREF (file, name);
2255 /* Like assemble_name_raw, but should be used when NAME might refer to
2256 an entity that is also represented as a tree (like a function or
2257 variable). If NAME does refer to such an entity, that entity will
2258 be marked as referenced. */
2260 void
2261 assemble_name (FILE *file, const char *name)
2263 const char *real_name;
2264 tree id;
2266 real_name = targetm.strip_name_encoding (name);
2268 id = maybe_get_identifier (real_name);
2269 if (id)
2271 tree id_orig = id;
2273 mark_referenced (id);
2274 ultimate_transparent_alias_target (&id);
2275 if (id != id_orig)
2276 name = IDENTIFIER_POINTER (id);
2277 gcc_assert (! TREE_CHAIN (id));
2280 assemble_name_raw (file, name);
2283 /* Allocate SIZE bytes writable static space with a gensym name
2284 and return an RTX to refer to its address. */
2287 assemble_static_space (unsigned HOST_WIDE_INT size)
2289 char name[12];
2290 const char *namestring;
2291 rtx x;
2293 ASM_GENERATE_INTERNAL_LABEL (name, "LF", const_labelno);
2294 ++const_labelno;
2295 namestring = ggc_strdup (name);
2297 x = gen_rtx_SYMBOL_REF (Pmode, namestring);
2298 SYMBOL_REF_FLAGS (x) = SYMBOL_FLAG_LOCAL;
2300 #ifdef ASM_OUTPUT_ALIGNED_DECL_LOCAL
2301 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, NULL_TREE, name, size,
2302 BIGGEST_ALIGNMENT);
2303 #else
2304 #ifdef ASM_OUTPUT_ALIGNED_LOCAL
2305 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, BIGGEST_ALIGNMENT);
2306 #else
2308 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
2309 so that each uninitialized object starts on such a boundary. */
2310 /* Variable `rounded' might or might not be used in ASM_OUTPUT_LOCAL. */
2311 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED
2312 = ((size + (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1)
2313 / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
2314 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
2315 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
2317 #endif
2318 #endif
2319 return x;
2322 /* Assemble the static constant template for function entry trampolines.
2323 This is done at most once per compilation.
2324 Returns an RTX for the address of the template. */
2326 static GTY(()) rtx initial_trampoline;
2329 assemble_trampoline_template (void)
2331 char label[256];
2332 const char *name;
2333 int align;
2334 rtx symbol;
2336 gcc_assert (targetm.asm_out.trampoline_template != NULL);
2338 if (initial_trampoline)
2339 return initial_trampoline;
2341 /* By default, put trampoline templates in read-only data section. */
2343 #ifdef TRAMPOLINE_SECTION
2344 switch_to_section (TRAMPOLINE_SECTION);
2345 #else
2346 switch_to_section (readonly_data_section);
2347 #endif
2349 /* Write the assembler code to define one. */
2350 align = floor_log2 (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
2351 if (align > 0)
2352 ASM_OUTPUT_ALIGN (asm_out_file, align);
2354 targetm.asm_out.internal_label (asm_out_file, "LTRAMP", 0);
2355 targetm.asm_out.trampoline_template (asm_out_file);
2357 /* Record the rtl to refer to it. */
2358 ASM_GENERATE_INTERNAL_LABEL (label, "LTRAMP", 0);
2359 name = ggc_strdup (label);
2360 symbol = gen_rtx_SYMBOL_REF (Pmode, name);
2361 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_LOCAL;
2363 initial_trampoline = gen_const_mem (BLKmode, symbol);
2364 set_mem_align (initial_trampoline, TRAMPOLINE_ALIGNMENT);
2365 set_mem_size (initial_trampoline, GEN_INT (TRAMPOLINE_SIZE));
2367 return initial_trampoline;
2370 /* A and B are either alignments or offsets. Return the minimum alignment
2371 that may be assumed after adding the two together. */
2373 static inline unsigned
2374 min_align (unsigned int a, unsigned int b)
2376 return (a | b) & -(a | b);
2379 /* Return the assembler directive for creating a given kind of integer
2380 object. SIZE is the number of bytes in the object and ALIGNED_P
2381 indicates whether it is known to be aligned. Return NULL if the
2382 assembly dialect has no such directive.
2384 The returned string should be printed at the start of a new line and
2385 be followed immediately by the object's initial value. */
2387 const char *
2388 integer_asm_op (int size, int aligned_p)
2390 struct asm_int_op *ops;
2392 if (aligned_p)
2393 ops = &targetm.asm_out.aligned_op;
2394 else
2395 ops = &targetm.asm_out.unaligned_op;
2397 switch (size)
2399 case 1:
2400 return targetm.asm_out.byte_op;
2401 case 2:
2402 return ops->hi;
2403 case 4:
2404 return ops->si;
2405 case 8:
2406 return ops->di;
2407 case 16:
2408 return ops->ti;
2409 default:
2410 return NULL;
2414 /* Use directive OP to assemble an integer object X. Print OP at the
2415 start of the line, followed immediately by the value of X. */
2417 void
2418 assemble_integer_with_op (const char *op, rtx x)
2420 fputs (op, asm_out_file);
2421 output_addr_const (asm_out_file, x);
2422 fputc ('\n', asm_out_file);
2425 /* The default implementation of the asm_out.integer target hook. */
2427 bool
2428 default_assemble_integer (rtx x ATTRIBUTE_UNUSED,
2429 unsigned int size ATTRIBUTE_UNUSED,
2430 int aligned_p ATTRIBUTE_UNUSED)
2432 const char *op = integer_asm_op (size, aligned_p);
2433 /* Avoid GAS bugs for large values. Specifically negative values whose
2434 absolute value fits in a bfd_vma, but not in a bfd_signed_vma. */
2435 if (size > UNITS_PER_WORD && size > POINTER_SIZE / BITS_PER_UNIT)
2436 return false;
2437 return op && (assemble_integer_with_op (op, x), true);
2440 /* Assemble the integer constant X into an object of SIZE bytes. ALIGN is
2441 the alignment of the integer in bits. Return 1 if we were able to output
2442 the constant, otherwise 0. We must be able to output the constant,
2443 if FORCE is nonzero. */
2445 bool
2446 assemble_integer (rtx x, unsigned int size, unsigned int align, int force)
2448 int aligned_p;
2450 aligned_p = (align >= MIN (size * BITS_PER_UNIT, BIGGEST_ALIGNMENT));
2452 /* See if the target hook can handle this kind of object. */
2453 if (targetm.asm_out.integer (x, size, aligned_p))
2454 return true;
2456 /* If the object is a multi-byte one, try splitting it up. Split
2457 it into words it if is multi-word, otherwise split it into bytes. */
2458 if (size > 1)
2460 enum machine_mode omode, imode;
2461 unsigned int subalign;
2462 unsigned int subsize, i;
2463 enum mode_class mclass;
2465 subsize = size > UNITS_PER_WORD? UNITS_PER_WORD : 1;
2466 subalign = MIN (align, subsize * BITS_PER_UNIT);
2467 if (GET_CODE (x) == CONST_FIXED)
2468 mclass = GET_MODE_CLASS (GET_MODE (x));
2469 else
2470 mclass = MODE_INT;
2472 omode = mode_for_size (subsize * BITS_PER_UNIT, mclass, 0);
2473 imode = mode_for_size (size * BITS_PER_UNIT, mclass, 0);
2475 for (i = 0; i < size; i += subsize)
2477 rtx partial = simplify_subreg (omode, x, imode, i);
2478 if (!partial || !assemble_integer (partial, subsize, subalign, 0))
2479 break;
2481 if (i == size)
2482 return true;
2484 /* If we've printed some of it, but not all of it, there's no going
2485 back now. */
2486 gcc_assert (!i);
2489 gcc_assert (!force);
2491 return false;
2494 void
2495 assemble_real (REAL_VALUE_TYPE d, enum machine_mode mode, unsigned int align)
2497 long data[4] = {0, 0, 0, 0};
2498 int i;
2499 int bitsize, nelts, nunits, units_per;
2501 /* This is hairy. We have a quantity of known size. real_to_target
2502 will put it into an array of *host* longs, 32 bits per element
2503 (even if long is more than 32 bits). We need to determine the
2504 number of array elements that are occupied (nelts) and the number
2505 of *target* min-addressable units that will be occupied in the
2506 object file (nunits). We cannot assume that 32 divides the
2507 mode's bitsize (size * BITS_PER_UNIT) evenly.
2509 size * BITS_PER_UNIT is used here to make sure that padding bits
2510 (which might appear at either end of the value; real_to_target
2511 will include the padding bits in its output array) are included. */
2513 nunits = GET_MODE_SIZE (mode);
2514 bitsize = nunits * BITS_PER_UNIT;
2515 nelts = CEIL (bitsize, 32);
2516 units_per = 32 / BITS_PER_UNIT;
2518 real_to_target (data, &d, mode);
2520 /* Put out the first word with the specified alignment. */
2521 assemble_integer (GEN_INT (data[0]), MIN (nunits, units_per), align, 1);
2522 nunits -= units_per;
2524 /* Subsequent words need only 32-bit alignment. */
2525 align = min_align (align, 32);
2527 for (i = 1; i < nelts; i++)
2529 assemble_integer (GEN_INT (data[i]), MIN (nunits, units_per), align, 1);
2530 nunits -= units_per;
2534 /* Given an expression EXP with a constant value,
2535 reduce it to the sum of an assembler symbol and an integer.
2536 Store them both in the structure *VALUE.
2537 EXP must be reducible. */
2539 struct addr_const {
2540 rtx base;
2541 HOST_WIDE_INT offset;
2544 static void
2545 decode_addr_const (tree exp, struct addr_const *value)
2547 tree target = TREE_OPERAND (exp, 0);
2548 int offset = 0;
2549 rtx x;
2551 while (1)
2553 if (TREE_CODE (target) == COMPONENT_REF
2554 && host_integerp (byte_position (TREE_OPERAND (target, 1)), 0))
2556 offset += int_byte_position (TREE_OPERAND (target, 1));
2557 target = TREE_OPERAND (target, 0);
2559 else if (TREE_CODE (target) == ARRAY_REF
2560 || TREE_CODE (target) == ARRAY_RANGE_REF)
2562 offset += (tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (target)), 1)
2563 * tree_low_cst (TREE_OPERAND (target, 1), 0));
2564 target = TREE_OPERAND (target, 0);
2566 else if (TREE_CODE (target) == INDIRECT_REF
2567 && TREE_CODE (TREE_OPERAND (target, 0)) == NOP_EXPR
2568 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (target, 0), 0))
2569 == ADDR_EXPR)
2570 target = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (target, 0), 0), 0);
2571 else
2572 break;
2575 switch (TREE_CODE (target))
2577 case VAR_DECL:
2578 case FUNCTION_DECL:
2579 x = DECL_RTL (target);
2580 break;
2582 case LABEL_DECL:
2583 x = gen_rtx_MEM (FUNCTION_MODE,
2584 gen_rtx_LABEL_REF (Pmode, force_label_rtx (target)));
2585 break;
2587 case REAL_CST:
2588 case FIXED_CST:
2589 case STRING_CST:
2590 case COMPLEX_CST:
2591 case CONSTRUCTOR:
2592 case INTEGER_CST:
2593 x = output_constant_def (target, 1);
2594 break;
2596 default:
2597 gcc_unreachable ();
2600 gcc_assert (MEM_P (x));
2601 x = XEXP (x, 0);
2603 value->base = x;
2604 value->offset = offset;
2608 static GTY((param_is (struct constant_descriptor_tree)))
2609 htab_t const_desc_htab;
2611 static void maybe_output_constant_def_contents (struct constant_descriptor_tree *, int);
2613 /* Constant pool accessor function. */
2615 htab_t
2616 constant_pool_htab (void)
2618 return const_desc_htab;
2621 /* Compute a hash code for a constant expression. */
2623 static hashval_t
2624 const_desc_hash (const void *ptr)
2626 return ((const struct constant_descriptor_tree *)ptr)->hash;
2629 static hashval_t
2630 const_hash_1 (const tree exp)
2632 const char *p;
2633 hashval_t hi;
2634 int len, i;
2635 enum tree_code code = TREE_CODE (exp);
2637 /* Either set P and LEN to the address and len of something to hash and
2638 exit the switch or return a value. */
2640 switch (code)
2642 case INTEGER_CST:
2643 p = (char *) &TREE_INT_CST (exp);
2644 len = sizeof TREE_INT_CST (exp);
2645 break;
2647 case REAL_CST:
2648 return real_hash (TREE_REAL_CST_PTR (exp));
2650 case FIXED_CST:
2651 return fixed_hash (TREE_FIXED_CST_PTR (exp));
2653 case STRING_CST:
2654 p = TREE_STRING_POINTER (exp);
2655 len = TREE_STRING_LENGTH (exp);
2656 break;
2658 case COMPLEX_CST:
2659 return (const_hash_1 (TREE_REALPART (exp)) * 5
2660 + const_hash_1 (TREE_IMAGPART (exp)));
2662 case VECTOR_CST:
2664 tree link;
2666 hi = 7 + TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp));
2668 for (link = TREE_VECTOR_CST_ELTS (exp); link; link = TREE_CHAIN (link))
2669 hi = hi * 563 + const_hash_1 (TREE_VALUE (link));
2671 return hi;
2674 case CONSTRUCTOR:
2676 unsigned HOST_WIDE_INT idx;
2677 tree value;
2679 hi = 5 + int_size_in_bytes (TREE_TYPE (exp));
2681 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
2682 if (value)
2683 hi = hi * 603 + const_hash_1 (value);
2685 return hi;
2688 case ADDR_EXPR:
2689 case FDESC_EXPR:
2691 struct addr_const value;
2693 decode_addr_const (exp, &value);
2694 switch (GET_CODE (value.base))
2696 case SYMBOL_REF:
2697 /* Don't hash the address of the SYMBOL_REF;
2698 only use the offset and the symbol name. */
2699 hi = value.offset;
2700 p = XSTR (value.base, 0);
2701 for (i = 0; p[i] != 0; i++)
2702 hi = ((hi * 613) + (unsigned) (p[i]));
2703 break;
2705 case LABEL_REF:
2706 hi = value.offset + CODE_LABEL_NUMBER (XEXP (value.base, 0)) * 13;
2707 break;
2709 default:
2710 gcc_unreachable ();
2713 return hi;
2715 case PLUS_EXPR:
2716 case POINTER_PLUS_EXPR:
2717 case MINUS_EXPR:
2718 return (const_hash_1 (TREE_OPERAND (exp, 0)) * 9
2719 + const_hash_1 (TREE_OPERAND (exp, 1)));
2721 CASE_CONVERT:
2722 return const_hash_1 (TREE_OPERAND (exp, 0)) * 7 + 2;
2724 default:
2725 /* A language specific constant. Just hash the code. */
2726 return code;
2729 /* Compute hashing function. */
2730 hi = len;
2731 for (i = 0; i < len; i++)
2732 hi = ((hi * 613) + (unsigned) (p[i]));
2734 return hi;
2737 /* Wrapper of compare_constant, for the htab interface. */
2738 static int
2739 const_desc_eq (const void *p1, const void *p2)
2741 const struct constant_descriptor_tree *const c1
2742 = (const struct constant_descriptor_tree *) p1;
2743 const struct constant_descriptor_tree *const c2
2744 = (const struct constant_descriptor_tree *) p2;
2745 if (c1->hash != c2->hash)
2746 return 0;
2747 return compare_constant (c1->value, c2->value);
2750 /* Compare t1 and t2, and return 1 only if they are known to result in
2751 the same bit pattern on output. */
2753 static int
2754 compare_constant (const tree t1, const tree t2)
2756 enum tree_code typecode;
2758 if (t1 == NULL_TREE)
2759 return t2 == NULL_TREE;
2760 if (t2 == NULL_TREE)
2761 return 0;
2763 if (TREE_CODE (t1) != TREE_CODE (t2))
2764 return 0;
2766 switch (TREE_CODE (t1))
2768 case INTEGER_CST:
2769 /* Integer constants are the same only if the same width of type. */
2770 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
2771 return 0;
2772 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
2773 return 0;
2774 return tree_int_cst_equal (t1, t2);
2776 case REAL_CST:
2777 /* Real constants are the same only if the same width of type. */
2778 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
2779 return 0;
2781 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
2783 case FIXED_CST:
2784 /* Fixed constants are the same only if the same width of type. */
2785 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
2786 return 0;
2788 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
2790 case STRING_CST:
2791 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
2792 return 0;
2794 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
2795 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
2796 TREE_STRING_LENGTH (t1)));
2798 case COMPLEX_CST:
2799 return (compare_constant (TREE_REALPART (t1), TREE_REALPART (t2))
2800 && compare_constant (TREE_IMAGPART (t1), TREE_IMAGPART (t2)));
2802 case VECTOR_CST:
2804 tree link1, link2;
2806 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (t1))
2807 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (t2)))
2808 return 0;
2810 link2 = TREE_VECTOR_CST_ELTS (t2);
2811 for (link1 = TREE_VECTOR_CST_ELTS (t1);
2812 link1;
2813 link1 = TREE_CHAIN (link1))
2815 if (!compare_constant (TREE_VALUE (link1), TREE_VALUE (link2)))
2816 return 0;
2817 link2 = TREE_CHAIN (link2);
2820 return 1;
2823 case CONSTRUCTOR:
2825 VEC(constructor_elt, gc) *v1, *v2;
2826 unsigned HOST_WIDE_INT idx;
2828 typecode = TREE_CODE (TREE_TYPE (t1));
2829 if (typecode != TREE_CODE (TREE_TYPE (t2)))
2830 return 0;
2832 if (typecode == ARRAY_TYPE)
2834 HOST_WIDE_INT size_1 = int_size_in_bytes (TREE_TYPE (t1));
2835 /* For arrays, check that the sizes all match. */
2836 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))
2837 || size_1 == -1
2838 || size_1 != int_size_in_bytes (TREE_TYPE (t2)))
2839 return 0;
2841 else
2843 /* For record and union constructors, require exact type
2844 equality. */
2845 if (TREE_TYPE (t1) != TREE_TYPE (t2))
2846 return 0;
2849 v1 = CONSTRUCTOR_ELTS (t1);
2850 v2 = CONSTRUCTOR_ELTS (t2);
2851 if (VEC_length (constructor_elt, v1)
2852 != VEC_length (constructor_elt, v2))
2853 return 0;
2855 for (idx = 0; idx < VEC_length (constructor_elt, v1); ++idx)
2857 constructor_elt *c1 = VEC_index (constructor_elt, v1, idx);
2858 constructor_elt *c2 = VEC_index (constructor_elt, v2, idx);
2860 /* Check that each value is the same... */
2861 if (!compare_constant (c1->value, c2->value))
2862 return 0;
2863 /* ... and that they apply to the same fields! */
2864 if (typecode == ARRAY_TYPE)
2866 if (!compare_constant (c1->index, c2->index))
2867 return 0;
2869 else
2871 if (c1->index != c2->index)
2872 return 0;
2876 return 1;
2879 case ADDR_EXPR:
2880 case FDESC_EXPR:
2882 struct addr_const value1, value2;
2883 enum rtx_code code;
2884 int ret;
2886 decode_addr_const (t1, &value1);
2887 decode_addr_const (t2, &value2);
2889 if (value1.offset != value2.offset)
2890 return 0;
2892 code = GET_CODE (value1.base);
2893 if (code != GET_CODE (value2.base))
2894 return 0;
2896 switch (code)
2898 case SYMBOL_REF:
2899 ret = (strcmp (XSTR (value1.base, 0), XSTR (value2.base, 0)) == 0);
2900 break;
2902 case LABEL_REF:
2903 ret = (CODE_LABEL_NUMBER (XEXP (value1.base, 0))
2904 == CODE_LABEL_NUMBER (XEXP (value2.base, 0)));
2905 break;
2907 default:
2908 gcc_unreachable ();
2910 return ret;
2913 case PLUS_EXPR:
2914 case POINTER_PLUS_EXPR:
2915 case MINUS_EXPR:
2916 case RANGE_EXPR:
2917 return (compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
2918 && compare_constant(TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
2920 CASE_CONVERT:
2921 case VIEW_CONVERT_EXPR:
2922 return compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2924 default:
2925 return 0;
2928 gcc_unreachable ();
2931 /* Make a copy of the whole tree structure for a constant. This
2932 handles the same types of nodes that compare_constant handles. */
2934 static tree
2935 copy_constant (tree exp)
2937 switch (TREE_CODE (exp))
2939 case ADDR_EXPR:
2940 /* For ADDR_EXPR, we do not want to copy the decl whose address
2941 is requested. We do want to copy constants though. */
2942 if (CONSTANT_CLASS_P (TREE_OPERAND (exp, 0)))
2943 return build1 (TREE_CODE (exp), TREE_TYPE (exp),
2944 copy_constant (TREE_OPERAND (exp, 0)));
2945 else
2946 return copy_node (exp);
2948 case INTEGER_CST:
2949 case REAL_CST:
2950 case FIXED_CST:
2951 case STRING_CST:
2952 return copy_node (exp);
2954 case COMPLEX_CST:
2955 return build_complex (TREE_TYPE (exp),
2956 copy_constant (TREE_REALPART (exp)),
2957 copy_constant (TREE_IMAGPART (exp)));
2959 case PLUS_EXPR:
2960 case POINTER_PLUS_EXPR:
2961 case MINUS_EXPR:
2962 return build2 (TREE_CODE (exp), TREE_TYPE (exp),
2963 copy_constant (TREE_OPERAND (exp, 0)),
2964 copy_constant (TREE_OPERAND (exp, 1)));
2966 CASE_CONVERT:
2967 case VIEW_CONVERT_EXPR:
2968 return build1 (TREE_CODE (exp), TREE_TYPE (exp),
2969 copy_constant (TREE_OPERAND (exp, 0)));
2971 case VECTOR_CST:
2972 return build_vector (TREE_TYPE (exp),
2973 copy_list (TREE_VECTOR_CST_ELTS (exp)));
2975 case CONSTRUCTOR:
2977 tree copy = copy_node (exp);
2978 VEC(constructor_elt, gc) *v;
2979 unsigned HOST_WIDE_INT idx;
2980 tree purpose, value;
2982 v = VEC_alloc(constructor_elt, gc, VEC_length(constructor_elt,
2983 CONSTRUCTOR_ELTS (exp)));
2984 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, purpose, value)
2986 constructor_elt *ce = VEC_quick_push (constructor_elt, v, NULL);
2987 ce->index = purpose;
2988 ce->value = copy_constant (value);
2990 CONSTRUCTOR_ELTS (copy) = v;
2991 return copy;
2994 default:
2995 gcc_unreachable ();
2999 /* Return the section into which constant EXP should be placed. */
3001 static section *
3002 get_constant_section (tree exp, unsigned int align)
3004 return targetm.asm_out.select_section (exp,
3005 compute_reloc_for_constant (exp),
3006 align);
3009 /* Return the size of constant EXP in bytes. */
3011 static HOST_WIDE_INT
3012 get_constant_size (tree exp)
3014 HOST_WIDE_INT size;
3016 size = int_size_in_bytes (TREE_TYPE (exp));
3017 if (TREE_CODE (exp) == STRING_CST)
3018 size = MAX (TREE_STRING_LENGTH (exp), size);
3019 return size;
3022 /* Subroutine of output_constant_def:
3023 No constant equal to EXP is known to have been output.
3024 Make a constant descriptor to enter EXP in the hash table.
3025 Assign the label number and construct RTL to refer to the
3026 constant's location in memory.
3027 Caller is responsible for updating the hash table. */
3029 static struct constant_descriptor_tree *
3030 build_constant_desc (tree exp)
3032 struct constant_descriptor_tree *desc;
3033 rtx symbol, rtl;
3034 char label[256];
3035 int labelno;
3036 tree decl;
3038 desc = ggc_alloc_constant_descriptor_tree ();
3039 desc->value = copy_constant (exp);
3041 /* Propagate marked-ness to copied constant. */
3042 if (flag_mudflap && mf_marked_p (exp))
3043 mf_mark (desc->value);
3045 /* Create a string containing the label name, in LABEL. */
3046 labelno = const_labelno++;
3047 ASM_GENERATE_INTERNAL_LABEL (label, "LC", labelno);
3049 /* Construct the VAR_DECL associated with the constant. */
3050 decl = build_decl (UNKNOWN_LOCATION, VAR_DECL, get_identifier (label),
3051 TREE_TYPE (exp));
3052 DECL_ARTIFICIAL (decl) = 1;
3053 DECL_IGNORED_P (decl) = 1;
3054 TREE_READONLY (decl) = 1;
3055 TREE_STATIC (decl) = 1;
3056 TREE_ADDRESSABLE (decl) = 1;
3057 /* We don't set the RTL yet as this would cause varpool to assume that the
3058 variable is referenced. Moreover, it would just be dropped in LTO mode.
3059 Instead we set the flag that will be recognized in make_decl_rtl. */
3060 DECL_IN_CONSTANT_POOL (decl) = 1;
3061 DECL_INITIAL (decl) = desc->value;
3062 /* ??? CONSTANT_ALIGNMENT hasn't been updated for vector types on most
3063 architectures so use DATA_ALIGNMENT as well, except for strings. */
3064 if (TREE_CODE (exp) == STRING_CST)
3066 #ifdef CONSTANT_ALIGNMENT
3067 DECL_ALIGN (decl) = CONSTANT_ALIGNMENT (exp, DECL_ALIGN (decl));
3068 #endif
3070 else
3071 align_variable (decl, 0);
3073 /* Now construct the SYMBOL_REF and the MEM. */
3074 if (use_object_blocks_p ())
3076 section *sect = get_constant_section (exp, DECL_ALIGN (decl));
3077 symbol = create_block_symbol (ggc_strdup (label),
3078 get_block_for_section (sect), -1);
3080 else
3081 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3082 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3083 SET_SYMBOL_REF_DECL (symbol, decl);
3084 TREE_CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3086 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (exp)), symbol);
3087 set_mem_attributes (rtl, exp, 1);
3088 set_mem_alias_set (rtl, 0);
3089 set_mem_alias_set (rtl, const_alias_set);
3091 /* We cannot share RTX'es in pool entries.
3092 Mark this piece of RTL as required for unsharing. */
3093 RTX_FLAG (rtl, used) = 1;
3095 /* Set flags or add text to the name to record information, such as
3096 that it is a local symbol. If the name is changed, the macro
3097 ASM_OUTPUT_LABELREF will have to know how to strip this
3098 information. This call might invalidate our local variable
3099 SYMBOL; we can't use it afterward. */
3100 targetm.encode_section_info (exp, rtl, true);
3102 desc->rtl = rtl;
3104 return desc;
3107 /* Return an rtx representing a reference to constant data in memory
3108 for the constant expression EXP.
3110 If assembler code for such a constant has already been output,
3111 return an rtx to refer to it.
3112 Otherwise, output such a constant in memory
3113 and generate an rtx for it.
3115 If DEFER is nonzero, this constant can be deferred and output only
3116 if referenced in the function after all optimizations.
3118 `const_desc_table' records which constants already have label strings. */
3121 output_constant_def (tree exp, int defer)
3123 struct constant_descriptor_tree *desc;
3124 struct constant_descriptor_tree key;
3125 void **loc;
3127 /* Look up EXP in the table of constant descriptors. If we didn't find
3128 it, create a new one. */
3129 key.value = exp;
3130 key.hash = const_hash_1 (exp);
3131 loc = htab_find_slot_with_hash (const_desc_htab, &key, key.hash, INSERT);
3133 desc = (struct constant_descriptor_tree *) *loc;
3134 if (desc == 0)
3136 desc = build_constant_desc (exp);
3137 desc->hash = key.hash;
3138 *loc = desc;
3141 maybe_output_constant_def_contents (desc, defer);
3142 return desc->rtl;
3145 /* Subroutine of output_constant_def: Decide whether or not we need to
3146 output the constant DESC now, and if so, do it. */
3147 static void
3148 maybe_output_constant_def_contents (struct constant_descriptor_tree *desc,
3149 int defer)
3151 rtx symbol = XEXP (desc->rtl, 0);
3152 tree exp = desc->value;
3154 if (flag_syntax_only)
3155 return;
3157 if (TREE_ASM_WRITTEN (exp))
3158 /* Already output; don't do it again. */
3159 return;
3161 /* We can always defer constants as long as the context allows
3162 doing so. */
3163 if (defer)
3165 /* Increment n_deferred_constants if it exists. It needs to be at
3166 least as large as the number of constants actually referred to
3167 by the function. If it's too small we'll stop looking too early
3168 and fail to emit constants; if it's too large we'll only look
3169 through the entire function when we could have stopped earlier. */
3170 if (cfun)
3171 n_deferred_constants++;
3172 return;
3175 output_constant_def_contents (symbol);
3178 /* Subroutine of output_constant_def_contents. Output the definition
3179 of constant EXP, which is pointed to by label LABEL. ALIGN is the
3180 constant's alignment in bits. */
3182 static void
3183 assemble_constant_contents (tree exp, const char *label, unsigned int align)
3185 HOST_WIDE_INT size;
3187 size = get_constant_size (exp);
3189 /* Do any machine/system dependent processing of the constant. */
3190 targetm.asm_out.declare_constant_name (asm_out_file, label, exp, size);
3192 /* Output the value of EXP. */
3193 output_constant (exp, size, align);
3196 /* We must output the constant data referred to by SYMBOL; do so. */
3198 static void
3199 output_constant_def_contents (rtx symbol)
3201 tree decl = SYMBOL_REF_DECL (symbol);
3202 tree exp = DECL_INITIAL (decl);
3203 unsigned int align;
3205 /* Make sure any other constants whose addresses appear in EXP
3206 are assigned label numbers. */
3207 output_addressed_constants (exp);
3209 /* We are no longer deferring this constant. */
3210 TREE_ASM_WRITTEN (decl) = TREE_ASM_WRITTEN (exp) = 1;
3212 /* If the constant is part of an object block, make sure that the
3213 decl has been positioned within its block, but do not write out
3214 its definition yet. output_object_blocks will do that later. */
3215 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
3216 place_block_symbol (symbol);
3217 else
3219 align = DECL_ALIGN (decl);
3220 switch_to_section (get_constant_section (exp, align));
3221 if (align > BITS_PER_UNIT)
3222 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
3223 assemble_constant_contents (exp, XSTR (symbol, 0), align);
3225 if (flag_mudflap)
3226 mudflap_enqueue_constant (exp);
3229 /* Look up EXP in the table of constant descriptors. Return the rtl
3230 if it has been emitted, else null. */
3233 lookup_constant_def (tree exp)
3235 struct constant_descriptor_tree *desc;
3236 struct constant_descriptor_tree key;
3238 key.value = exp;
3239 key.hash = const_hash_1 (exp);
3240 desc = (struct constant_descriptor_tree *)
3241 htab_find_with_hash (const_desc_htab, &key, key.hash);
3243 return (desc ? desc->rtl : NULL_RTX);
3246 /* Return a tree representing a reference to constant data in memory
3247 for the constant expression EXP.
3249 This is the counterpart of output_constant_def at the Tree level. */
3251 tree
3252 tree_output_constant_def (tree exp)
3254 struct constant_descriptor_tree *desc, key;
3255 void **loc;
3256 tree decl;
3258 /* Look up EXP in the table of constant descriptors. If we didn't find
3259 it, create a new one. */
3260 key.value = exp;
3261 key.hash = const_hash_1 (exp);
3262 loc = htab_find_slot_with_hash (const_desc_htab, &key, key.hash, INSERT);
3264 desc = (struct constant_descriptor_tree *) *loc;
3265 if (desc == 0)
3267 desc = build_constant_desc (exp);
3268 desc->hash = key.hash;
3269 *loc = desc;
3272 decl = SYMBOL_REF_DECL (XEXP (desc->rtl, 0));
3273 varpool_finalize_decl (decl);
3274 return decl;
3277 /* Used in the hash tables to avoid outputting the same constant
3278 twice. Unlike 'struct constant_descriptor_tree', RTX constants
3279 are output once per function, not once per file. */
3280 /* ??? Only a few targets need per-function constant pools. Most
3281 can use one per-file pool. Should add a targetm bit to tell the
3282 difference. */
3284 struct GTY(()) rtx_constant_pool {
3285 /* Pointers to first and last constant in pool, as ordered by offset. */
3286 struct constant_descriptor_rtx *first;
3287 struct constant_descriptor_rtx *last;
3289 /* Hash facility for making memory-constants from constant rtl-expressions.
3290 It is used on RISC machines where immediate integer arguments and
3291 constant addresses are restricted so that such constants must be stored
3292 in memory. */
3293 htab_t GTY((param_is (struct constant_descriptor_rtx))) const_rtx_htab;
3295 /* Current offset in constant pool (does not include any
3296 machine-specific header). */
3297 HOST_WIDE_INT offset;
3300 struct GTY((chain_next ("%h.next"))) constant_descriptor_rtx {
3301 struct constant_descriptor_rtx *next;
3302 rtx mem;
3303 rtx sym;
3304 rtx constant;
3305 HOST_WIDE_INT offset;
3306 hashval_t hash;
3307 enum machine_mode mode;
3308 unsigned int align;
3309 int labelno;
3310 int mark;
3313 /* Hash and compare functions for const_rtx_htab. */
3315 static hashval_t
3316 const_desc_rtx_hash (const void *ptr)
3318 const struct constant_descriptor_rtx *const desc
3319 = (const struct constant_descriptor_rtx *) ptr;
3320 return desc->hash;
3323 static int
3324 const_desc_rtx_eq (const void *a, const void *b)
3326 const struct constant_descriptor_rtx *const x
3327 = (const struct constant_descriptor_rtx *) a;
3328 const struct constant_descriptor_rtx *const y
3329 = (const struct constant_descriptor_rtx *) b;
3331 if (x->mode != y->mode)
3332 return 0;
3333 return rtx_equal_p (x->constant, y->constant);
3336 /* This is the worker function for const_rtx_hash, called via for_each_rtx. */
3338 static int
3339 const_rtx_hash_1 (rtx *xp, void *data)
3341 unsigned HOST_WIDE_INT hwi;
3342 enum machine_mode mode;
3343 enum rtx_code code;
3344 hashval_t h, *hp;
3345 rtx x;
3347 x = *xp;
3348 code = GET_CODE (x);
3349 mode = GET_MODE (x);
3350 h = (hashval_t) code * 1048573 + mode;
3352 switch (code)
3354 case CONST_INT:
3355 hwi = INTVAL (x);
3356 fold_hwi:
3358 int shift = sizeof (hashval_t) * CHAR_BIT;
3359 const int n = sizeof (HOST_WIDE_INT) / sizeof (hashval_t);
3360 int i;
3362 h ^= (hashval_t) hwi;
3363 for (i = 1; i < n; ++i)
3365 hwi >>= shift;
3366 h ^= (hashval_t) hwi;
3369 break;
3371 case CONST_DOUBLE:
3372 if (mode == VOIDmode)
3374 hwi = CONST_DOUBLE_LOW (x) ^ CONST_DOUBLE_HIGH (x);
3375 goto fold_hwi;
3377 else
3378 h ^= real_hash (CONST_DOUBLE_REAL_VALUE (x));
3379 break;
3381 case CONST_FIXED:
3382 h ^= fixed_hash (CONST_FIXED_VALUE (x));
3383 break;
3385 case CONST_VECTOR:
3387 int i;
3388 for (i = XVECLEN (x, 0); i-- > 0; )
3389 h = h * 251 + const_rtx_hash_1 (&XVECEXP (x, 0, i), data);
3391 break;
3393 case SYMBOL_REF:
3394 h ^= htab_hash_string (XSTR (x, 0));
3395 break;
3397 case LABEL_REF:
3398 h = h * 251 + CODE_LABEL_NUMBER (XEXP (x, 0));
3399 break;
3401 case UNSPEC:
3402 case UNSPEC_VOLATILE:
3403 h = h * 251 + XINT (x, 1);
3404 break;
3406 default:
3407 break;
3410 hp = (hashval_t *) data;
3411 *hp = *hp * 509 + h;
3412 return 0;
3415 /* Compute a hash value for X, which should be a constant. */
3417 static hashval_t
3418 const_rtx_hash (rtx x)
3420 hashval_t h = 0;
3421 for_each_rtx (&x, const_rtx_hash_1, &h);
3422 return h;
3426 /* Create and return a new rtx constant pool. */
3428 static struct rtx_constant_pool *
3429 create_constant_pool (void)
3431 struct rtx_constant_pool *pool;
3433 pool = ggc_alloc_rtx_constant_pool ();
3434 pool->const_rtx_htab = htab_create_ggc (31, const_desc_rtx_hash,
3435 const_desc_rtx_eq, NULL);
3436 pool->first = NULL;
3437 pool->last = NULL;
3438 pool->offset = 0;
3439 return pool;
3442 /* Initialize constant pool hashing for a new function. */
3444 void
3445 init_varasm_status (void)
3447 crtl->varasm.pool = create_constant_pool ();
3448 crtl->varasm.deferred_constants = 0;
3451 /* Given a MINUS expression, simplify it if both sides
3452 include the same symbol. */
3455 simplify_subtraction (rtx x)
3457 rtx r = simplify_rtx (x);
3458 return r ? r : x;
3461 /* Given a constant rtx X, make (or find) a memory constant for its value
3462 and return a MEM rtx to refer to it in memory. */
3465 force_const_mem (enum machine_mode mode, rtx x)
3467 struct constant_descriptor_rtx *desc, tmp;
3468 struct rtx_constant_pool *pool;
3469 char label[256];
3470 rtx def, symbol;
3471 hashval_t hash;
3472 unsigned int align;
3473 void **slot;
3475 /* If we're not allowed to drop X into the constant pool, don't. */
3476 if (targetm.cannot_force_const_mem (x))
3477 return NULL_RTX;
3479 /* Record that this function has used a constant pool entry. */
3480 crtl->uses_const_pool = 1;
3482 /* Decide which pool to use. */
3483 pool = (targetm.use_blocks_for_constant_p (mode, x)
3484 ? shared_constant_pool
3485 : crtl->varasm.pool);
3487 /* Lookup the value in the hashtable. */
3488 tmp.constant = x;
3489 tmp.mode = mode;
3490 hash = const_rtx_hash (x);
3491 slot = htab_find_slot_with_hash (pool->const_rtx_htab, &tmp, hash, INSERT);
3492 desc = (struct constant_descriptor_rtx *) *slot;
3494 /* If the constant was already present, return its memory. */
3495 if (desc)
3496 return copy_rtx (desc->mem);
3498 /* Otherwise, create a new descriptor. */
3499 desc = ggc_alloc_constant_descriptor_rtx ();
3500 *slot = desc;
3502 /* Align the location counter as required by EXP's data type. */
3503 align = GET_MODE_ALIGNMENT (mode == VOIDmode ? word_mode : mode);
3504 #ifdef CONSTANT_ALIGNMENT
3506 tree type = lang_hooks.types.type_for_mode (mode, 0);
3507 if (type != NULL_TREE)
3508 align = CONSTANT_ALIGNMENT (make_tree (type, x), align);
3510 #endif
3512 pool->offset += (align / BITS_PER_UNIT) - 1;
3513 pool->offset &= ~ ((align / BITS_PER_UNIT) - 1);
3515 desc->next = NULL;
3516 desc->constant = copy_rtx (tmp.constant);
3517 desc->offset = pool->offset;
3518 desc->hash = hash;
3519 desc->mode = mode;
3520 desc->align = align;
3521 desc->labelno = const_labelno;
3522 desc->mark = 0;
3524 pool->offset += GET_MODE_SIZE (mode);
3525 if (pool->last)
3526 pool->last->next = desc;
3527 else
3528 pool->first = pool->last = desc;
3529 pool->last = desc;
3531 /* Create a string containing the label name, in LABEL. */
3532 ASM_GENERATE_INTERNAL_LABEL (label, "LC", const_labelno);
3533 ++const_labelno;
3535 /* Construct the SYMBOL_REF. Make sure to mark it as belonging to
3536 the constants pool. */
3537 if (use_object_blocks_p () && targetm.use_blocks_for_constant_p (mode, x))
3539 section *sect = targetm.asm_out.select_rtx_section (mode, x, align);
3540 symbol = create_block_symbol (ggc_strdup (label),
3541 get_block_for_section (sect), -1);
3543 else
3544 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3545 desc->sym = symbol;
3546 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3547 CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3548 SET_SYMBOL_REF_CONSTANT (symbol, desc);
3550 /* Construct the MEM. */
3551 desc->mem = def = gen_const_mem (mode, symbol);
3552 set_mem_attributes (def, lang_hooks.types.type_for_mode (mode, 0), 1);
3553 set_mem_align (def, align);
3555 /* If we're dropping a label to the constant pool, make sure we
3556 don't delete it. */
3557 if (GET_CODE (x) == LABEL_REF)
3558 LABEL_PRESERVE_P (XEXP (x, 0)) = 1;
3560 return copy_rtx (def);
3563 /* Given a constant pool SYMBOL_REF, return the corresponding constant. */
3566 get_pool_constant (rtx addr)
3568 return SYMBOL_REF_CONSTANT (addr)->constant;
3571 /* Given a constant pool SYMBOL_REF, return the corresponding constant
3572 and whether it has been output or not. */
3575 get_pool_constant_mark (rtx addr, bool *pmarked)
3577 struct constant_descriptor_rtx *desc;
3579 desc = SYMBOL_REF_CONSTANT (addr);
3580 *pmarked = (desc->mark != 0);
3581 return desc->constant;
3584 /* Similar, return the mode. */
3586 enum machine_mode
3587 get_pool_mode (const_rtx addr)
3589 return SYMBOL_REF_CONSTANT (addr)->mode;
3592 /* Return the size of the constant pool. */
3595 get_pool_size (void)
3597 return crtl->varasm.pool->offset;
3600 /* Worker function for output_constant_pool_1. Emit assembly for X
3601 in MODE with known alignment ALIGN. */
3603 static void
3604 output_constant_pool_2 (enum machine_mode mode, rtx x, unsigned int align)
3606 switch (GET_MODE_CLASS (mode))
3608 case MODE_FLOAT:
3609 case MODE_DECIMAL_FLOAT:
3611 REAL_VALUE_TYPE r;
3613 gcc_assert (GET_CODE (x) == CONST_DOUBLE);
3614 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
3615 assemble_real (r, mode, align);
3616 break;
3619 case MODE_INT:
3620 case MODE_PARTIAL_INT:
3621 case MODE_FRACT:
3622 case MODE_UFRACT:
3623 case MODE_ACCUM:
3624 case MODE_UACCUM:
3625 assemble_integer (x, GET_MODE_SIZE (mode), align, 1);
3626 break;
3628 case MODE_VECTOR_FLOAT:
3629 case MODE_VECTOR_INT:
3630 case MODE_VECTOR_FRACT:
3631 case MODE_VECTOR_UFRACT:
3632 case MODE_VECTOR_ACCUM:
3633 case MODE_VECTOR_UACCUM:
3635 int i, units;
3636 enum machine_mode submode = GET_MODE_INNER (mode);
3637 unsigned int subalign = MIN (align, GET_MODE_BITSIZE (submode));
3639 gcc_assert (GET_CODE (x) == CONST_VECTOR);
3640 units = CONST_VECTOR_NUNITS (x);
3642 for (i = 0; i < units; i++)
3644 rtx elt = CONST_VECTOR_ELT (x, i);
3645 output_constant_pool_2 (submode, elt, i ? subalign : align);
3648 break;
3650 default:
3651 gcc_unreachable ();
3655 /* Worker function for output_constant_pool. Emit constant DESC,
3656 giving it ALIGN bits of alignment. */
3658 static void
3659 output_constant_pool_1 (struct constant_descriptor_rtx *desc,
3660 unsigned int align)
3662 rtx x, tmp;
3664 x = desc->constant;
3666 /* See if X is a LABEL_REF (or a CONST referring to a LABEL_REF)
3667 whose CODE_LABEL has been deleted. This can occur if a jump table
3668 is eliminated by optimization. If so, write a constant of zero
3669 instead. Note that this can also happen by turning the
3670 CODE_LABEL into a NOTE. */
3671 /* ??? This seems completely and utterly wrong. Certainly it's
3672 not true for NOTE_INSN_DELETED_LABEL, but I disbelieve proper
3673 functioning even with INSN_DELETED_P and friends. */
3675 tmp = x;
3676 switch (GET_CODE (tmp))
3678 case CONST:
3679 if (GET_CODE (XEXP (tmp, 0)) != PLUS
3680 || GET_CODE (XEXP (XEXP (tmp, 0), 0)) != LABEL_REF)
3681 break;
3682 tmp = XEXP (XEXP (tmp, 0), 0);
3683 /* FALLTHRU */
3685 case LABEL_REF:
3686 tmp = XEXP (tmp, 0);
3687 gcc_assert (!INSN_DELETED_P (tmp));
3688 gcc_assert (!NOTE_P (tmp)
3689 || NOTE_KIND (tmp) != NOTE_INSN_DELETED);
3690 break;
3692 default:
3693 break;
3696 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3697 ASM_OUTPUT_SPECIAL_POOL_ENTRY (asm_out_file, x, desc->mode,
3698 align, desc->labelno, done);
3699 #endif
3701 assemble_align (align);
3703 /* Output the label. */
3704 targetm.asm_out.internal_label (asm_out_file, "LC", desc->labelno);
3706 /* Output the data. */
3707 output_constant_pool_2 (desc->mode, x, align);
3709 /* Make sure all constants in SECTION_MERGE and not SECTION_STRINGS
3710 sections have proper size. */
3711 if (align > GET_MODE_BITSIZE (desc->mode)
3712 && in_section
3713 && (in_section->common.flags & SECTION_MERGE))
3714 assemble_align (align);
3716 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3717 done:
3718 #endif
3719 return;
3722 /* Given a SYMBOL_REF CURRENT_RTX, mark it and all constants it refers
3723 to as used. Emit referenced deferred strings. This function can
3724 be used with for_each_rtx to mark all SYMBOL_REFs in an rtx. */
3726 static int
3727 mark_constant (rtx *current_rtx, void *data ATTRIBUTE_UNUSED)
3729 rtx x = *current_rtx;
3731 if (x == NULL_RTX || GET_CODE (x) != SYMBOL_REF)
3732 return 0;
3734 if (CONSTANT_POOL_ADDRESS_P (x))
3736 struct constant_descriptor_rtx *desc = SYMBOL_REF_CONSTANT (x);
3737 if (desc->mark == 0)
3739 desc->mark = 1;
3740 for_each_rtx (&desc->constant, mark_constant, NULL);
3743 else if (TREE_CONSTANT_POOL_ADDRESS_P (x))
3745 tree decl = SYMBOL_REF_DECL (x);
3746 if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
3748 n_deferred_constants--;
3749 output_constant_def_contents (x);
3753 return -1;
3756 /* Look through appropriate parts of INSN, marking all entries in the
3757 constant pool which are actually being used. Entries that are only
3758 referenced by other constants are also marked as used. Emit
3759 deferred strings that are used. */
3761 static void
3762 mark_constants (rtx insn)
3764 if (!INSN_P (insn))
3765 return;
3767 /* Insns may appear inside a SEQUENCE. Only check the patterns of
3768 insns, not any notes that may be attached. We don't want to mark
3769 a constant just because it happens to appear in a REG_EQUIV note. */
3770 if (GET_CODE (PATTERN (insn)) == SEQUENCE)
3772 rtx seq = PATTERN (insn);
3773 int i, n = XVECLEN (seq, 0);
3774 for (i = 0; i < n; ++i)
3776 rtx subinsn = XVECEXP (seq, 0, i);
3777 if (INSN_P (subinsn))
3778 for_each_rtx (&PATTERN (subinsn), mark_constant, NULL);
3781 else
3782 for_each_rtx (&PATTERN (insn), mark_constant, NULL);
3785 /* Look through the instructions for this function, and mark all the
3786 entries in POOL which are actually being used. Emit deferred constants
3787 which have indeed been used. */
3789 static void
3790 mark_constant_pool (void)
3792 rtx insn, link;
3794 if (!crtl->uses_const_pool && n_deferred_constants == 0)
3795 return;
3797 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3798 mark_constants (insn);
3800 for (link = crtl->epilogue_delay_list;
3801 link;
3802 link = XEXP (link, 1))
3803 mark_constants (XEXP (link, 0));
3806 /* Write all the constants in POOL. */
3808 static void
3809 output_constant_pool_contents (struct rtx_constant_pool *pool)
3811 struct constant_descriptor_rtx *desc;
3813 for (desc = pool->first; desc ; desc = desc->next)
3814 if (desc->mark)
3816 /* If the constant is part of an object_block, make sure that
3817 the constant has been positioned within its block, but do not
3818 write out its definition yet. output_object_blocks will do
3819 that later. */
3820 if (SYMBOL_REF_HAS_BLOCK_INFO_P (desc->sym)
3821 && SYMBOL_REF_BLOCK (desc->sym))
3822 place_block_symbol (desc->sym);
3823 else
3825 switch_to_section (targetm.asm_out.select_rtx_section
3826 (desc->mode, desc->constant, desc->align));
3827 output_constant_pool_1 (desc, desc->align);
3832 /* Mark all constants that are used in the current function, then write
3833 out the function's private constant pool. */
3835 static void
3836 output_constant_pool (const char *fnname ATTRIBUTE_UNUSED,
3837 tree fndecl ATTRIBUTE_UNUSED)
3839 struct rtx_constant_pool *pool = crtl->varasm.pool;
3841 /* It is possible for gcc to call force_const_mem and then to later
3842 discard the instructions which refer to the constant. In such a
3843 case we do not need to output the constant. */
3844 mark_constant_pool ();
3846 #ifdef ASM_OUTPUT_POOL_PROLOGUE
3847 ASM_OUTPUT_POOL_PROLOGUE (asm_out_file, fnname, fndecl, pool->offset);
3848 #endif
3850 output_constant_pool_contents (pool);
3852 #ifdef ASM_OUTPUT_POOL_EPILOGUE
3853 ASM_OUTPUT_POOL_EPILOGUE (asm_out_file, fnname, fndecl, pool->offset);
3854 #endif
3857 /* Write the contents of the shared constant pool. */
3859 void
3860 output_shared_constant_pool (void)
3862 output_constant_pool_contents (shared_constant_pool);
3865 /* Determine what kind of relocations EXP may need. */
3868 compute_reloc_for_constant (tree exp)
3870 int reloc = 0, reloc2;
3871 tree tem;
3873 switch (TREE_CODE (exp))
3875 case ADDR_EXPR:
3876 case FDESC_EXPR:
3877 /* Go inside any operations that get_inner_reference can handle and see
3878 if what's inside is a constant: no need to do anything here for
3879 addresses of variables or functions. */
3880 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
3881 tem = TREE_OPERAND (tem, 0))
3884 if (TREE_PUBLIC (tem))
3885 reloc |= 2;
3886 else
3887 reloc |= 1;
3888 break;
3890 case PLUS_EXPR:
3891 case POINTER_PLUS_EXPR:
3892 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3893 reloc |= compute_reloc_for_constant (TREE_OPERAND (exp, 1));
3894 break;
3896 case MINUS_EXPR:
3897 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3898 reloc2 = compute_reloc_for_constant (TREE_OPERAND (exp, 1));
3899 /* The difference of two local labels is computable at link time. */
3900 if (reloc == 1 && reloc2 == 1)
3901 reloc = 0;
3902 else
3903 reloc |= reloc2;
3904 break;
3906 CASE_CONVERT:
3907 case VIEW_CONVERT_EXPR:
3908 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3909 break;
3911 case CONSTRUCTOR:
3913 unsigned HOST_WIDE_INT idx;
3914 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
3915 if (tem != 0)
3916 reloc |= compute_reloc_for_constant (tem);
3918 break;
3920 default:
3921 break;
3923 return reloc;
3926 /* Find all the constants whose addresses are referenced inside of EXP,
3927 and make sure assembler code with a label has been output for each one.
3928 Indicate whether an ADDR_EXPR has been encountered. */
3930 static void
3931 output_addressed_constants (tree exp)
3933 tree tem;
3935 switch (TREE_CODE (exp))
3937 case ADDR_EXPR:
3938 case FDESC_EXPR:
3939 /* Go inside any operations that get_inner_reference can handle and see
3940 if what's inside is a constant: no need to do anything here for
3941 addresses of variables or functions. */
3942 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
3943 tem = TREE_OPERAND (tem, 0))
3946 /* If we have an initialized CONST_DECL, retrieve the initializer. */
3947 if (TREE_CODE (tem) == CONST_DECL && DECL_INITIAL (tem))
3948 tem = DECL_INITIAL (tem);
3950 if (CONSTANT_CLASS_P (tem) || TREE_CODE (tem) == CONSTRUCTOR)
3951 output_constant_def (tem, 0);
3952 break;
3954 case PLUS_EXPR:
3955 case POINTER_PLUS_EXPR:
3956 case MINUS_EXPR:
3957 output_addressed_constants (TREE_OPERAND (exp, 1));
3958 /* Fall through. */
3960 CASE_CONVERT:
3961 case VIEW_CONVERT_EXPR:
3962 output_addressed_constants (TREE_OPERAND (exp, 0));
3963 break;
3965 case CONSTRUCTOR:
3967 unsigned HOST_WIDE_INT idx;
3968 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
3969 if (tem != 0)
3970 output_addressed_constants (tem);
3972 break;
3974 default:
3975 break;
3979 /* Whether a constructor CTOR is a valid static constant initializer if all
3980 its elements are. This used to be internal to initializer_constant_valid_p
3981 and has been exposed to let other functions like categorize_ctor_elements
3982 evaluate the property while walking a constructor for other purposes. */
3984 bool
3985 constructor_static_from_elts_p (const_tree ctor)
3987 return (TREE_CONSTANT (ctor)
3988 && (TREE_CODE (TREE_TYPE (ctor)) == UNION_TYPE
3989 || TREE_CODE (TREE_TYPE (ctor)) == RECORD_TYPE
3990 || TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE));
3993 static tree initializer_constant_valid_p_1 (tree value, tree endtype,
3994 tree *cache);
3996 /* A subroutine of initializer_constant_valid_p. VALUE is a MINUS_EXPR,
3997 PLUS_EXPR or POINTER_PLUS_EXPR. This looks for cases of VALUE
3998 which are valid when ENDTYPE is an integer of any size; in
3999 particular, this does not accept a pointer minus a constant. This
4000 returns null_pointer_node if the VALUE is an absolute constant
4001 which can be used to initialize a static variable. Otherwise it
4002 returns NULL. */
4004 static tree
4005 narrowing_initializer_constant_valid_p (tree value, tree endtype, tree *cache)
4007 tree op0, op1;
4009 if (!INTEGRAL_TYPE_P (endtype))
4010 return NULL_TREE;
4012 op0 = TREE_OPERAND (value, 0);
4013 op1 = TREE_OPERAND (value, 1);
4015 /* Like STRIP_NOPS except allow the operand mode to widen. This
4016 works around a feature of fold that simplifies (int)(p1 - p2) to
4017 ((int)p1 - (int)p2) under the theory that the narrower operation
4018 is cheaper. */
4020 while (CONVERT_EXPR_P (op0)
4021 || TREE_CODE (op0) == NON_LVALUE_EXPR)
4023 tree inner = TREE_OPERAND (op0, 0);
4024 if (inner == error_mark_node
4025 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4026 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))
4027 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
4028 break;
4029 op0 = inner;
4032 while (CONVERT_EXPR_P (op1)
4033 || TREE_CODE (op1) == NON_LVALUE_EXPR)
4035 tree inner = TREE_OPERAND (op1, 0);
4036 if (inner == error_mark_node
4037 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4038 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op1)))
4039 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
4040 break;
4041 op1 = inner;
4044 op0 = initializer_constant_valid_p_1 (op0, endtype, cache);
4045 if (!op0)
4046 return NULL_TREE;
4048 op1 = initializer_constant_valid_p_1 (op1, endtype,
4049 cache ? cache + 2 : NULL);
4050 /* Both initializers must be known. */
4051 if (op1)
4053 if (op0 == op1
4054 && (op0 == null_pointer_node
4055 || TREE_CODE (value) == MINUS_EXPR))
4056 return null_pointer_node;
4058 /* Support differences between labels. */
4059 if (TREE_CODE (op0) == LABEL_DECL
4060 && TREE_CODE (op1) == LABEL_DECL)
4061 return null_pointer_node;
4063 if (TREE_CODE (op0) == STRING_CST
4064 && TREE_CODE (op1) == STRING_CST
4065 && operand_equal_p (op0, op1, 1))
4066 return null_pointer_node;
4069 return NULL_TREE;
4072 /* Helper function of initializer_constant_valid_p.
4073 Return nonzero if VALUE is a valid constant-valued expression
4074 for use in initializing a static variable; one that can be an
4075 element of a "constant" initializer.
4077 Return null_pointer_node if the value is absolute;
4078 if it is relocatable, return the variable that determines the relocation.
4079 We assume that VALUE has been folded as much as possible;
4080 therefore, we do not need to check for such things as
4081 arithmetic-combinations of integers.
4083 Use CACHE (pointer to 2 tree values) for caching if non-NULL. */
4085 static tree
4086 initializer_constant_valid_p_1 (tree value, tree endtype, tree *cache)
4088 tree ret;
4090 switch (TREE_CODE (value))
4092 case CONSTRUCTOR:
4093 if (constructor_static_from_elts_p (value))
4095 unsigned HOST_WIDE_INT idx;
4096 tree elt;
4097 bool absolute = true;
4099 if (cache && cache[0] == value)
4100 return cache[1];
4101 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
4103 tree reloc;
4104 reloc = initializer_constant_valid_p_1 (elt, TREE_TYPE (elt),
4105 NULL);
4106 if (!reloc)
4108 if (cache)
4110 cache[0] = value;
4111 cache[1] = NULL_TREE;
4113 return NULL_TREE;
4115 if (reloc != null_pointer_node)
4116 absolute = false;
4118 /* For a non-absolute relocation, there is no single
4119 variable that can be "the variable that determines the
4120 relocation." */
4121 if (cache)
4123 cache[0] = value;
4124 cache[1] = absolute ? null_pointer_node : error_mark_node;
4126 return absolute ? null_pointer_node : error_mark_node;
4129 return TREE_STATIC (value) ? null_pointer_node : NULL_TREE;
4131 case INTEGER_CST:
4132 case VECTOR_CST:
4133 case REAL_CST:
4134 case FIXED_CST:
4135 case STRING_CST:
4136 case COMPLEX_CST:
4137 return null_pointer_node;
4139 case ADDR_EXPR:
4140 case FDESC_EXPR:
4142 tree op0 = staticp (TREE_OPERAND (value, 0));
4143 if (op0)
4145 /* "&(*a).f" is like unto pointer arithmetic. If "a" turns out
4146 to be a constant, this is old-skool offsetof-like nonsense. */
4147 if (TREE_CODE (op0) == INDIRECT_REF
4148 && TREE_CONSTANT (TREE_OPERAND (op0, 0)))
4149 return null_pointer_node;
4150 /* Taking the address of a nested function involves a trampoline,
4151 unless we don't need or want one. */
4152 if (TREE_CODE (op0) == FUNCTION_DECL
4153 && DECL_STATIC_CHAIN (op0)
4154 && !TREE_NO_TRAMPOLINE (value))
4155 return NULL_TREE;
4156 /* "&{...}" requires a temporary to hold the constructed
4157 object. */
4158 if (TREE_CODE (op0) == CONSTRUCTOR)
4159 return NULL_TREE;
4161 return op0;
4164 case NON_LVALUE_EXPR:
4165 return initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4166 endtype, cache);
4168 case VIEW_CONVERT_EXPR:
4170 tree src = TREE_OPERAND (value, 0);
4171 tree src_type = TREE_TYPE (src);
4172 tree dest_type = TREE_TYPE (value);
4174 /* Allow view-conversions from aggregate to non-aggregate type only
4175 if the bit pattern is fully preserved afterwards; otherwise, the
4176 RTL expander won't be able to apply a subsequent transformation
4177 to the underlying constructor. */
4178 if (AGGREGATE_TYPE_P (src_type) && !AGGREGATE_TYPE_P (dest_type))
4180 if (TYPE_MODE (endtype) == TYPE_MODE (dest_type))
4181 return initializer_constant_valid_p_1 (src, endtype, cache);
4182 else
4183 return NULL_TREE;
4186 /* Allow all other kinds of view-conversion. */
4187 return initializer_constant_valid_p_1 (src, endtype, cache);
4190 CASE_CONVERT:
4192 tree src = TREE_OPERAND (value, 0);
4193 tree src_type = TREE_TYPE (src);
4194 tree dest_type = TREE_TYPE (value);
4196 /* Allow conversions between pointer types, floating-point
4197 types, and offset types. */
4198 if ((POINTER_TYPE_P (dest_type) && POINTER_TYPE_P (src_type))
4199 || (FLOAT_TYPE_P (dest_type) && FLOAT_TYPE_P (src_type))
4200 || (TREE_CODE (dest_type) == OFFSET_TYPE
4201 && TREE_CODE (src_type) == OFFSET_TYPE))
4202 return initializer_constant_valid_p_1 (src, endtype, cache);
4204 /* Allow length-preserving conversions between integer types. */
4205 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type)
4206 && (TYPE_PRECISION (dest_type) == TYPE_PRECISION (src_type)))
4207 return initializer_constant_valid_p_1 (src, endtype, cache);
4209 /* Allow conversions between other integer types only if
4210 explicit value. */
4211 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type))
4213 tree inner = initializer_constant_valid_p_1 (src, endtype, cache);
4214 if (inner == null_pointer_node)
4215 return null_pointer_node;
4216 break;
4219 /* Allow (int) &foo provided int is as wide as a pointer. */
4220 if (INTEGRAL_TYPE_P (dest_type) && POINTER_TYPE_P (src_type)
4221 && (TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type)))
4222 return initializer_constant_valid_p_1 (src, endtype, cache);
4224 /* Likewise conversions from int to pointers, but also allow
4225 conversions from 0. */
4226 if ((POINTER_TYPE_P (dest_type)
4227 || TREE_CODE (dest_type) == OFFSET_TYPE)
4228 && INTEGRAL_TYPE_P (src_type))
4230 if (TREE_CODE (src) == INTEGER_CST
4231 && TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type))
4232 return null_pointer_node;
4233 if (integer_zerop (src))
4234 return null_pointer_node;
4235 else if (TYPE_PRECISION (dest_type) <= TYPE_PRECISION (src_type))
4236 return initializer_constant_valid_p_1 (src, endtype, cache);
4239 /* Allow conversions to struct or union types if the value
4240 inside is okay. */
4241 if (TREE_CODE (dest_type) == RECORD_TYPE
4242 || TREE_CODE (dest_type) == UNION_TYPE)
4243 return initializer_constant_valid_p_1 (src, endtype, cache);
4245 break;
4247 case POINTER_PLUS_EXPR:
4248 case PLUS_EXPR:
4249 /* Any valid floating-point constants will have been folded by now;
4250 with -frounding-math we hit this with addition of two constants. */
4251 if (TREE_CODE (endtype) == REAL_TYPE)
4252 return NULL_TREE;
4253 if (cache && cache[0] == value)
4254 return cache[1];
4255 if (! INTEGRAL_TYPE_P (endtype)
4256 || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
4258 tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
4259 tree valid0
4260 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4261 endtype, ncache);
4262 tree valid1
4263 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
4264 endtype, ncache + 2);
4265 /* If either term is absolute, use the other term's relocation. */
4266 if (valid0 == null_pointer_node)
4267 ret = valid1;
4268 else if (valid1 == null_pointer_node)
4269 ret = valid0;
4270 /* Support narrowing pointer differences. */
4271 else
4272 ret = narrowing_initializer_constant_valid_p (value, endtype,
4273 ncache);
4275 else
4276 /* Support narrowing pointer differences. */
4277 ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
4278 if (cache)
4280 cache[0] = value;
4281 cache[1] = ret;
4283 return ret;
4285 case MINUS_EXPR:
4286 if (TREE_CODE (endtype) == REAL_TYPE)
4287 return NULL_TREE;
4288 if (cache && cache[0] == value)
4289 return cache[1];
4290 if (! INTEGRAL_TYPE_P (endtype)
4291 || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
4293 tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
4294 tree valid0
4295 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4296 endtype, ncache);
4297 tree valid1
4298 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
4299 endtype, ncache + 2);
4300 /* Win if second argument is absolute. */
4301 if (valid1 == null_pointer_node)
4302 ret = valid0;
4303 /* Win if both arguments have the same relocation.
4304 Then the value is absolute. */
4305 else if (valid0 == valid1 && valid0 != 0)
4306 ret = null_pointer_node;
4307 /* Since GCC guarantees that string constants are unique in the
4308 generated code, a subtraction between two copies of the same
4309 constant string is absolute. */
4310 else if (valid0 && TREE_CODE (valid0) == STRING_CST
4311 && valid1 && TREE_CODE (valid1) == STRING_CST
4312 && operand_equal_p (valid0, valid1, 1))
4313 ret = null_pointer_node;
4314 /* Support narrowing differences. */
4315 else
4316 ret = narrowing_initializer_constant_valid_p (value, endtype,
4317 ncache);
4319 else
4320 /* Support narrowing differences. */
4321 ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
4322 if (cache)
4324 cache[0] = value;
4325 cache[1] = ret;
4327 return ret;
4329 default:
4330 break;
4333 return NULL_TREE;
4336 /* Return nonzero if VALUE is a valid constant-valued expression
4337 for use in initializing a static variable; one that can be an
4338 element of a "constant" initializer.
4340 Return null_pointer_node if the value is absolute;
4341 if it is relocatable, return the variable that determines the relocation.
4342 We assume that VALUE has been folded as much as possible;
4343 therefore, we do not need to check for such things as
4344 arithmetic-combinations of integers. */
4345 tree
4346 initializer_constant_valid_p (tree value, tree endtype)
4348 return initializer_constant_valid_p_1 (value, endtype, NULL);
4351 /* Return true if VALUE is a valid constant-valued expression
4352 for use in initializing a static bit-field; one that can be
4353 an element of a "constant" initializer. */
4355 bool
4356 initializer_constant_valid_for_bitfield_p (tree value)
4358 /* For bitfields we support integer constants or possibly nested aggregates
4359 of such. */
4360 switch (TREE_CODE (value))
4362 case CONSTRUCTOR:
4364 unsigned HOST_WIDE_INT idx;
4365 tree elt;
4367 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
4368 if (!initializer_constant_valid_for_bitfield_p (elt))
4369 return false;
4370 return true;
4373 case INTEGER_CST:
4374 return true;
4376 case VIEW_CONVERT_EXPR:
4377 case NON_LVALUE_EXPR:
4378 return
4379 initializer_constant_valid_for_bitfield_p (TREE_OPERAND (value, 0));
4381 default:
4382 break;
4385 return false;
4388 /* output_constructor outer state of relevance in recursive calls, typically
4389 for nested aggregate bitfields. */
4391 typedef struct {
4392 unsigned int bit_offset; /* current position in ... */
4393 int byte; /* ... the outer byte buffer. */
4394 } oc_outer_state;
4396 static unsigned HOST_WIDE_INT
4397 output_constructor (tree, unsigned HOST_WIDE_INT, unsigned int,
4398 oc_outer_state *);
4400 /* Output assembler code for constant EXP to FILE, with no label.
4401 This includes the pseudo-op such as ".int" or ".byte", and a newline.
4402 Assumes output_addressed_constants has been done on EXP already.
4404 Generate exactly SIZE bytes of assembler data, padding at the end
4405 with zeros if necessary. SIZE must always be specified.
4407 SIZE is important for structure constructors,
4408 since trailing members may have been omitted from the constructor.
4409 It is also important for initialization of arrays from string constants
4410 since the full length of the string constant might not be wanted.
4411 It is also needed for initialization of unions, where the initializer's
4412 type is just one member, and that may not be as long as the union.
4414 There a case in which we would fail to output exactly SIZE bytes:
4415 for a structure constructor that wants to produce more than SIZE bytes.
4416 But such constructors will never be generated for any possible input.
4418 ALIGN is the alignment of the data in bits. */
4420 void
4421 output_constant (tree exp, unsigned HOST_WIDE_INT size, unsigned int align)
4423 enum tree_code code;
4424 unsigned HOST_WIDE_INT thissize;
4426 if (size == 0 || flag_syntax_only)
4427 return;
4429 /* See if we're trying to initialize a pointer in a non-default mode
4430 to the address of some declaration somewhere. If the target says
4431 the mode is valid for pointers, assume the target has a way of
4432 resolving it. */
4433 if (TREE_CODE (exp) == NOP_EXPR
4434 && POINTER_TYPE_P (TREE_TYPE (exp))
4435 && targetm.addr_space.valid_pointer_mode
4436 (TYPE_MODE (TREE_TYPE (exp)),
4437 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
4439 tree saved_type = TREE_TYPE (exp);
4441 /* Peel off any intermediate conversions-to-pointer for valid
4442 pointer modes. */
4443 while (TREE_CODE (exp) == NOP_EXPR
4444 && POINTER_TYPE_P (TREE_TYPE (exp))
4445 && targetm.addr_space.valid_pointer_mode
4446 (TYPE_MODE (TREE_TYPE (exp)),
4447 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
4448 exp = TREE_OPERAND (exp, 0);
4450 /* If what we're left with is the address of something, we can
4451 convert the address to the final type and output it that
4452 way. */
4453 if (TREE_CODE (exp) == ADDR_EXPR)
4454 exp = build1 (ADDR_EXPR, saved_type, TREE_OPERAND (exp, 0));
4455 /* Likewise for constant ints. */
4456 else if (TREE_CODE (exp) == INTEGER_CST)
4457 exp = build_int_cst_wide (saved_type, TREE_INT_CST_LOW (exp),
4458 TREE_INT_CST_HIGH (exp));
4462 /* Eliminate any conversions since we'll be outputting the underlying
4463 constant. */
4464 while (CONVERT_EXPR_P (exp)
4465 || TREE_CODE (exp) == NON_LVALUE_EXPR
4466 || TREE_CODE (exp) == VIEW_CONVERT_EXPR)
4468 HOST_WIDE_INT type_size = int_size_in_bytes (TREE_TYPE (exp));
4469 HOST_WIDE_INT op_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (exp, 0)));
4471 /* Make sure eliminating the conversion is really a no-op, except with
4472 VIEW_CONVERT_EXPRs to allow for wild Ada unchecked conversions and
4473 union types to allow for Ada unchecked unions. */
4474 if (type_size > op_size
4475 && TREE_CODE (exp) != VIEW_CONVERT_EXPR
4476 && TREE_CODE (TREE_TYPE (exp)) != UNION_TYPE)
4477 /* Keep the conversion. */
4478 break;
4479 else
4480 exp = TREE_OPERAND (exp, 0);
4483 code = TREE_CODE (TREE_TYPE (exp));
4484 thissize = int_size_in_bytes (TREE_TYPE (exp));
4486 /* Allow a constructor with no elements for any data type.
4487 This means to fill the space with zeros. */
4488 if (TREE_CODE (exp) == CONSTRUCTOR
4489 && VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (exp)))
4491 assemble_zeros (size);
4492 return;
4495 if (TREE_CODE (exp) == FDESC_EXPR)
4497 #ifdef ASM_OUTPUT_FDESC
4498 HOST_WIDE_INT part = tree_low_cst (TREE_OPERAND (exp, 1), 0);
4499 tree decl = TREE_OPERAND (exp, 0);
4500 ASM_OUTPUT_FDESC (asm_out_file, decl, part);
4501 #else
4502 gcc_unreachable ();
4503 #endif
4504 return;
4507 /* Now output the underlying data. If we've handling the padding, return.
4508 Otherwise, break and ensure SIZE is the size written. */
4509 switch (code)
4511 case BOOLEAN_TYPE:
4512 case INTEGER_TYPE:
4513 case ENUMERAL_TYPE:
4514 case POINTER_TYPE:
4515 case REFERENCE_TYPE:
4516 case OFFSET_TYPE:
4517 case FIXED_POINT_TYPE:
4518 if (! assemble_integer (expand_expr (exp, NULL_RTX, VOIDmode,
4519 EXPAND_INITIALIZER),
4520 MIN (size, thissize), align, 0))
4521 error ("initializer for integer/fixed-point value is too complicated");
4522 break;
4524 case REAL_TYPE:
4525 if (TREE_CODE (exp) != REAL_CST)
4526 error ("initializer for floating value is not a floating constant");
4527 else
4528 assemble_real (TREE_REAL_CST (exp), TYPE_MODE (TREE_TYPE (exp)), align);
4529 break;
4531 case COMPLEX_TYPE:
4532 output_constant (TREE_REALPART (exp), thissize / 2, align);
4533 output_constant (TREE_IMAGPART (exp), thissize / 2,
4534 min_align (align, BITS_PER_UNIT * (thissize / 2)));
4535 break;
4537 case ARRAY_TYPE:
4538 case VECTOR_TYPE:
4539 switch (TREE_CODE (exp))
4541 case CONSTRUCTOR:
4542 output_constructor (exp, size, align, NULL);
4543 return;
4544 case STRING_CST:
4545 thissize = MIN ((unsigned HOST_WIDE_INT)TREE_STRING_LENGTH (exp),
4546 size);
4547 assemble_string (TREE_STRING_POINTER (exp), thissize);
4548 break;
4550 case VECTOR_CST:
4552 int elt_size;
4553 tree link;
4554 unsigned int nalign;
4555 enum machine_mode inner;
4557 inner = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
4558 nalign = MIN (align, GET_MODE_ALIGNMENT (inner));
4560 elt_size = GET_MODE_SIZE (inner);
4562 link = TREE_VECTOR_CST_ELTS (exp);
4563 output_constant (TREE_VALUE (link), elt_size, align);
4564 thissize = elt_size;
4565 while ((link = TREE_CHAIN (link)) != NULL)
4567 output_constant (TREE_VALUE (link), elt_size, nalign);
4568 thissize += elt_size;
4570 break;
4572 default:
4573 gcc_unreachable ();
4575 break;
4577 case RECORD_TYPE:
4578 case UNION_TYPE:
4579 gcc_assert (TREE_CODE (exp) == CONSTRUCTOR);
4580 output_constructor (exp, size, align, NULL);
4581 return;
4583 case ERROR_MARK:
4584 return;
4586 default:
4587 gcc_unreachable ();
4590 if (size > thissize)
4591 assemble_zeros (size - thissize);
4595 /* Subroutine of output_constructor, used for computing the size of
4596 arrays of unspecified length. VAL must be a CONSTRUCTOR of an array
4597 type with an unspecified upper bound. */
4599 static unsigned HOST_WIDE_INT
4600 array_size_for_constructor (tree val)
4602 tree max_index, i;
4603 unsigned HOST_WIDE_INT cnt;
4604 tree index, value, tmp;
4606 /* This code used to attempt to handle string constants that are not
4607 arrays of single-bytes, but nothing else does, so there's no point in
4608 doing it here. */
4609 if (TREE_CODE (val) == STRING_CST)
4610 return TREE_STRING_LENGTH (val);
4612 max_index = NULL_TREE;
4613 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (val), cnt, index, value)
4615 if (TREE_CODE (index) == RANGE_EXPR)
4616 index = TREE_OPERAND (index, 1);
4617 if (max_index == NULL_TREE || tree_int_cst_lt (max_index, index))
4618 max_index = index;
4621 if (max_index == NULL_TREE)
4622 return 0;
4624 /* Compute the total number of array elements. */
4625 tmp = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (val)));
4626 i = size_binop (MINUS_EXPR, fold_convert (sizetype, max_index),
4627 fold_convert (sizetype, tmp));
4628 i = size_binop (PLUS_EXPR, i, size_one_node);
4630 /* Multiply by the array element unit size to find number of bytes. */
4631 i = size_binop (MULT_EXPR, i, TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (val))));
4633 return tree_low_cst (i, 1);
4636 /* Other datastructures + helpers for output_constructor. */
4638 /* output_constructor local state to support interaction with helpers. */
4640 typedef struct {
4642 /* Received arguments. */
4643 tree exp; /* Constructor expression. */
4644 unsigned HOST_WIDE_INT size; /* # bytes to output - pad if necessary. */
4645 unsigned int align; /* Known initial alignment. */
4647 /* Constructor expression data. */
4648 tree type; /* Expression type. */
4649 tree field; /* Current field decl in a record. */
4650 tree min_index; /* Lower bound if specified for an array. */
4652 /* Output processing state. */
4653 HOST_WIDE_INT total_bytes; /* # bytes output so far / current position. */
4654 bool byte_buffer_in_use; /* Whether byte ... */
4655 int byte; /* ... contains part of a bitfield byte yet to
4656 be output. */
4658 int last_relative_index; /* Implicit or explicit index of the last
4659 array element output within a bitfield. */
4660 /* Current element. */
4661 tree val; /* Current element value. */
4662 tree index; /* Current element index. */
4664 } oc_local_state;
4666 /* Helper for output_constructor. From the current LOCAL state, output a
4667 RANGE_EXPR element. */
4669 static void
4670 output_constructor_array_range (oc_local_state *local)
4672 unsigned HOST_WIDE_INT fieldsize
4673 = int_size_in_bytes (TREE_TYPE (local->type));
4675 HOST_WIDE_INT lo_index
4676 = tree_low_cst (TREE_OPERAND (local->index, 0), 0);
4677 HOST_WIDE_INT hi_index
4678 = tree_low_cst (TREE_OPERAND (local->index, 1), 0);
4679 HOST_WIDE_INT index;
4681 unsigned int align2
4682 = min_align (local->align, fieldsize * BITS_PER_UNIT);
4684 for (index = lo_index; index <= hi_index; index++)
4686 /* Output the element's initial value. */
4687 if (local->val == NULL_TREE)
4688 assemble_zeros (fieldsize);
4689 else
4690 output_constant (local->val, fieldsize, align2);
4692 /* Count its size. */
4693 local->total_bytes += fieldsize;
4697 /* Helper for output_constructor. From the current LOCAL state, output a
4698 field element that is not true bitfield or part of an outer one. */
4700 static void
4701 output_constructor_regular_field (oc_local_state *local)
4703 /* Field size and position. Since this structure is static, we know the
4704 positions are constant. */
4705 unsigned HOST_WIDE_INT fieldsize;
4706 HOST_WIDE_INT fieldpos;
4708 unsigned int align2;
4710 if (local->index != NULL_TREE)
4711 fieldpos = (tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (local->val)), 1)
4712 * ((tree_low_cst (local->index, 0)
4713 - tree_low_cst (local->min_index, 0))));
4714 else if (local->field != NULL_TREE)
4715 fieldpos = int_byte_position (local->field);
4716 else
4717 fieldpos = 0;
4719 /* Output any buffered-up bit-fields preceding this element. */
4720 if (local->byte_buffer_in_use)
4722 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
4723 local->total_bytes++;
4724 local->byte_buffer_in_use = false;
4727 /* Advance to offset of this element.
4728 Note no alignment needed in an array, since that is guaranteed
4729 if each element has the proper size. */
4730 if ((local->field != NULL_TREE || local->index != NULL_TREE)
4731 && fieldpos != local->total_bytes)
4733 gcc_assert (fieldpos >= local->total_bytes);
4734 assemble_zeros (fieldpos - local->total_bytes);
4735 local->total_bytes = fieldpos;
4738 /* Find the alignment of this element. */
4739 align2 = min_align (local->align, BITS_PER_UNIT * fieldpos);
4741 /* Determine size this element should occupy. */
4742 if (local->field)
4744 fieldsize = 0;
4746 /* If this is an array with an unspecified upper bound,
4747 the initializer determines the size. */
4748 /* ??? This ought to only checked if DECL_SIZE_UNIT is NULL,
4749 but we cannot do this until the deprecated support for
4750 initializing zero-length array members is removed. */
4751 if (TREE_CODE (TREE_TYPE (local->field)) == ARRAY_TYPE
4752 && TYPE_DOMAIN (TREE_TYPE (local->field))
4753 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (local->field))))
4755 fieldsize = array_size_for_constructor (local->val);
4756 /* Given a non-empty initialization, this field had
4757 better be last. */
4758 gcc_assert (!fieldsize || !DECL_CHAIN (local->field));
4760 else if (DECL_SIZE_UNIT (local->field))
4762 /* ??? This can't be right. If the decl size overflows
4763 a host integer we will silently emit no data. */
4764 if (host_integerp (DECL_SIZE_UNIT (local->field), 1))
4765 fieldsize = tree_low_cst (DECL_SIZE_UNIT (local->field), 1);
4768 else
4769 fieldsize = int_size_in_bytes (TREE_TYPE (local->type));
4771 /* Output the element's initial value. */
4772 if (local->val == NULL_TREE)
4773 assemble_zeros (fieldsize);
4774 else
4775 output_constant (local->val, fieldsize, align2);
4777 /* Count its size. */
4778 local->total_bytes += fieldsize;
4781 /* Helper for output_constructor. From the current LOCAL and OUTER states,
4782 output an element that is a true bitfield or part of an outer one. */
4784 static void
4785 output_constructor_bitfield (oc_local_state *local, oc_outer_state *outer)
4787 /* Bit size of this element. */
4788 HOST_WIDE_INT ebitsize
4789 = (local->field
4790 ? tree_low_cst (DECL_SIZE (local->field), 1)
4791 : tree_low_cst (TYPE_SIZE (TREE_TYPE (local->type)), 1));
4793 /* Relative index of this element if this is an array component. */
4794 HOST_WIDE_INT relative_index
4795 = (!local->field
4796 ? (local->index
4797 ? (tree_low_cst (local->index, 0)
4798 - tree_low_cst (local->min_index, 0))
4799 : local->last_relative_index + 1)
4800 : 0);
4802 /* Bit position of this element from the start of the containing
4803 constructor. */
4804 HOST_WIDE_INT constructor_relative_ebitpos
4805 = (local->field
4806 ? int_bit_position (local->field)
4807 : ebitsize * relative_index);
4809 /* Bit position of this element from the start of a possibly ongoing
4810 outer byte buffer. */
4811 HOST_WIDE_INT byte_relative_ebitpos
4812 = ((outer ? outer->bit_offset : 0) + constructor_relative_ebitpos);
4814 /* From the start of a possibly ongoing outer byte buffer, offsets to
4815 the first bit of this element and to the first bit past the end of
4816 this element. */
4817 HOST_WIDE_INT next_offset = byte_relative_ebitpos;
4818 HOST_WIDE_INT end_offset = byte_relative_ebitpos + ebitsize;
4820 local->last_relative_index = relative_index;
4822 if (local->val == NULL_TREE)
4823 local->val = integer_zero_node;
4825 while (TREE_CODE (local->val) == VIEW_CONVERT_EXPR
4826 || TREE_CODE (local->val) == NON_LVALUE_EXPR)
4827 local->val = TREE_OPERAND (local->val, 0);
4829 if (TREE_CODE (local->val) != INTEGER_CST
4830 && TREE_CODE (local->val) != CONSTRUCTOR)
4832 error ("invalid initial value for member %qE", DECL_NAME (local->field));
4833 return;
4836 /* If this field does not start in this (or, next) byte,
4837 skip some bytes. */
4838 if (next_offset / BITS_PER_UNIT != local->total_bytes)
4840 /* Output remnant of any bit field in previous bytes. */
4841 if (local->byte_buffer_in_use)
4843 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
4844 local->total_bytes++;
4845 local->byte_buffer_in_use = false;
4848 /* If still not at proper byte, advance to there. */
4849 if (next_offset / BITS_PER_UNIT != local->total_bytes)
4851 gcc_assert (next_offset / BITS_PER_UNIT >= local->total_bytes);
4852 assemble_zeros (next_offset / BITS_PER_UNIT - local->total_bytes);
4853 local->total_bytes = next_offset / BITS_PER_UNIT;
4857 /* Set up the buffer if necessary. */
4858 if (!local->byte_buffer_in_use)
4860 local->byte = 0;
4861 if (ebitsize > 0)
4862 local->byte_buffer_in_use = true;
4865 /* If this is nested constructor, recurse passing the bit offset and the
4866 pending data, then retrieve the new pending data afterwards. */
4867 if (TREE_CODE (local->val) == CONSTRUCTOR)
4869 oc_outer_state output_state;
4871 output_state.bit_offset = next_offset % BITS_PER_UNIT;
4872 output_state.byte = local->byte;
4873 local->total_bytes
4874 += output_constructor (local->val, 0, 0, &output_state);
4875 local->byte = output_state.byte;
4876 return;
4879 /* Otherwise, we must split the element into pieces that fall within
4880 separate bytes, and combine each byte with previous or following
4881 bit-fields. */
4882 while (next_offset < end_offset)
4884 int this_time;
4885 int shift;
4886 HOST_WIDE_INT value;
4887 HOST_WIDE_INT next_byte = next_offset / BITS_PER_UNIT;
4888 HOST_WIDE_INT next_bit = next_offset % BITS_PER_UNIT;
4890 /* Advance from byte to byte
4891 within this element when necessary. */
4892 while (next_byte != local->total_bytes)
4894 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
4895 local->total_bytes++;
4896 local->byte = 0;
4899 /* Number of bits we can process at once
4900 (all part of the same byte). */
4901 this_time = MIN (end_offset - next_offset,
4902 BITS_PER_UNIT - next_bit);
4903 if (BYTES_BIG_ENDIAN)
4905 /* On big-endian machine, take the most significant bits
4906 first (of the bits that are significant)
4907 and put them into bytes from the most significant end. */
4908 shift = end_offset - next_offset - this_time;
4910 /* Don't try to take a bunch of bits that cross
4911 the word boundary in the INTEGER_CST. We can
4912 only select bits from the LOW or HIGH part
4913 not from both. */
4914 if (shift < HOST_BITS_PER_WIDE_INT
4915 && shift + this_time > HOST_BITS_PER_WIDE_INT)
4917 this_time = shift + this_time - HOST_BITS_PER_WIDE_INT;
4918 shift = HOST_BITS_PER_WIDE_INT;
4921 /* Now get the bits from the appropriate constant word. */
4922 if (shift < HOST_BITS_PER_WIDE_INT)
4923 value = TREE_INT_CST_LOW (local->val);
4924 else
4926 gcc_assert (shift < 2 * HOST_BITS_PER_WIDE_INT);
4927 value = TREE_INT_CST_HIGH (local->val);
4928 shift -= HOST_BITS_PER_WIDE_INT;
4931 /* Get the result. This works only when:
4932 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
4933 local->byte |= (((value >> shift)
4934 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
4935 << (BITS_PER_UNIT - this_time - next_bit));
4937 else
4939 /* On little-endian machines,
4940 take first the least significant bits of the value
4941 and pack them starting at the least significant
4942 bits of the bytes. */
4943 shift = next_offset - byte_relative_ebitpos;
4945 /* Don't try to take a bunch of bits that cross
4946 the word boundary in the INTEGER_CST. We can
4947 only select bits from the LOW or HIGH part
4948 not from both. */
4949 if (shift < HOST_BITS_PER_WIDE_INT
4950 && shift + this_time > HOST_BITS_PER_WIDE_INT)
4951 this_time = (HOST_BITS_PER_WIDE_INT - shift);
4953 /* Now get the bits from the appropriate constant word. */
4954 if (shift < HOST_BITS_PER_WIDE_INT)
4955 value = TREE_INT_CST_LOW (local->val);
4956 else
4958 gcc_assert (shift < 2 * HOST_BITS_PER_WIDE_INT);
4959 value = TREE_INT_CST_HIGH (local->val);
4960 shift -= HOST_BITS_PER_WIDE_INT;
4963 /* Get the result. This works only when:
4964 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
4965 local->byte |= (((value >> shift)
4966 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
4967 << next_bit);
4970 next_offset += this_time;
4971 local->byte_buffer_in_use = true;
4975 /* Subroutine of output_constant, used for CONSTRUCTORs (aggregate constants).
4976 Generate at least SIZE bytes, padding if necessary. OUTER designates the
4977 caller output state of relevance in recursive invocations. */
4979 static unsigned HOST_WIDE_INT
4980 output_constructor (tree exp, unsigned HOST_WIDE_INT size,
4981 unsigned int align, oc_outer_state * outer)
4983 unsigned HOST_WIDE_INT cnt;
4984 constructor_elt *ce;
4986 oc_local_state local;
4988 /* Setup our local state to communicate with helpers. */
4989 local.exp = exp;
4990 local.size = size;
4991 local.align = align;
4993 local.total_bytes = 0;
4994 local.byte_buffer_in_use = outer != NULL;
4995 local.byte = outer ? outer->byte : 0;
4997 local.type = TREE_TYPE (exp);
4999 local.last_relative_index = -1;
5001 local.min_index = NULL_TREE;
5002 if (TREE_CODE (local.type) == ARRAY_TYPE
5003 && TYPE_DOMAIN (local.type) != NULL_TREE)
5004 local.min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (local.type));
5006 gcc_assert (HOST_BITS_PER_WIDE_INT >= BITS_PER_UNIT);
5008 /* As CE goes through the elements of the constant, FIELD goes through the
5009 structure fields if the constant is a structure. If the constant is a
5010 union, we override this by getting the field from the TREE_LIST element.
5011 But the constant could also be an array. Then FIELD is zero.
5013 There is always a maximum of one element in the chain LINK for unions
5014 (even if the initializer in a source program incorrectly contains
5015 more one). */
5017 local.field = NULL_TREE;
5018 if (TREE_CODE (local.type) == RECORD_TYPE)
5019 local.field = TYPE_FIELDS (local.type);
5021 for (cnt = 0;
5022 VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (exp), cnt, ce);
5023 cnt++, local.field = local.field ? DECL_CHAIN (local.field) : 0)
5025 local.val = ce->value;
5026 local.index = NULL_TREE;
5028 /* The element in a union constructor specifies the proper field
5029 or index. */
5030 if ((TREE_CODE (local.type) == RECORD_TYPE
5031 || TREE_CODE (local.type) == UNION_TYPE
5032 || TREE_CODE (local.type) == QUAL_UNION_TYPE)
5033 && ce->index != NULL_TREE)
5034 local.field = ce->index;
5036 else if (TREE_CODE (local.type) == ARRAY_TYPE)
5037 local.index = ce->index;
5039 #ifdef ASM_COMMENT_START
5040 if (local.field && flag_verbose_asm)
5041 fprintf (asm_out_file, "%s %s:\n",
5042 ASM_COMMENT_START,
5043 DECL_NAME (local.field)
5044 ? IDENTIFIER_POINTER (DECL_NAME (local.field))
5045 : "<anonymous>");
5046 #endif
5048 /* Eliminate the marker that makes a cast not be an lvalue. */
5049 if (local.val != NULL_TREE)
5050 STRIP_NOPS (local.val);
5052 /* Output the current element, using the appropriate helper ... */
5054 /* For an array slice not part of an outer bitfield. */
5055 if (!outer
5056 && local.index != NULL_TREE
5057 && TREE_CODE (local.index) == RANGE_EXPR)
5058 output_constructor_array_range (&local);
5060 /* For a field that is neither a true bitfield nor part of an outer one,
5061 known to be at least byte aligned and multiple-of-bytes long. */
5062 else if (!outer
5063 && (local.field == NULL_TREE
5064 || !CONSTRUCTOR_BITFIELD_P (local.field)))
5065 output_constructor_regular_field (&local);
5067 /* For a true bitfield or part of an outer one. */
5068 else
5069 output_constructor_bitfield (&local, outer);
5072 /* If we are not at toplevel, save the pending data for our caller.
5073 Otherwise output the pending data and padding zeros as needed. */
5074 if (outer)
5075 outer->byte = local.byte;
5076 else
5078 if (local.byte_buffer_in_use)
5080 assemble_integer (GEN_INT (local.byte), 1, BITS_PER_UNIT, 1);
5081 local.total_bytes++;
5084 if ((unsigned HOST_WIDE_INT)local.total_bytes < local.size)
5086 assemble_zeros (local.size - local.total_bytes);
5087 local.total_bytes = local.size;
5091 return local.total_bytes;
5094 /* Mark DECL as weak. */
5096 static void
5097 mark_weak (tree decl)
5099 DECL_WEAK (decl) = 1;
5101 if (DECL_RTL_SET_P (decl)
5102 && MEM_P (DECL_RTL (decl))
5103 && XEXP (DECL_RTL (decl), 0)
5104 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == SYMBOL_REF)
5105 SYMBOL_REF_WEAK (XEXP (DECL_RTL (decl), 0)) = 1;
5108 /* Merge weak status between NEWDECL and OLDDECL. */
5110 void
5111 merge_weak (tree newdecl, tree olddecl)
5113 if (DECL_WEAK (newdecl) == DECL_WEAK (olddecl))
5115 if (DECL_WEAK (newdecl) && TARGET_SUPPORTS_WEAK)
5117 tree *pwd;
5118 /* We put the NEWDECL on the weak_decls list at some point
5119 and OLDDECL as well. Keep just OLDDECL on the list. */
5120 for (pwd = &weak_decls; *pwd; pwd = &TREE_CHAIN (*pwd))
5121 if (TREE_VALUE (*pwd) == newdecl)
5123 *pwd = TREE_CHAIN (*pwd);
5124 break;
5127 return;
5130 if (DECL_WEAK (newdecl))
5132 tree wd;
5134 /* NEWDECL is weak, but OLDDECL is not. */
5136 /* If we already output the OLDDECL, we're in trouble; we can't
5137 go back and make it weak. This should never happen in
5138 unit-at-a-time compilation. */
5139 gcc_assert (!TREE_ASM_WRITTEN (olddecl));
5141 /* If we've already generated rtl referencing OLDDECL, we may
5142 have done so in a way that will not function properly with
5143 a weak symbol. Again in unit-at-a-time this should be
5144 impossible. */
5145 gcc_assert (!TREE_USED (olddecl)
5146 || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (olddecl)));
5148 if (TARGET_SUPPORTS_WEAK)
5150 /* We put the NEWDECL on the weak_decls list at some point.
5151 Replace it with the OLDDECL. */
5152 for (wd = weak_decls; wd; wd = TREE_CHAIN (wd))
5153 if (TREE_VALUE (wd) == newdecl)
5155 TREE_VALUE (wd) = olddecl;
5156 break;
5158 /* We may not find the entry on the list. If NEWDECL is a
5159 weak alias, then we will have already called
5160 globalize_decl to remove the entry; in that case, we do
5161 not need to do anything. */
5164 /* Make the OLDDECL weak; it's OLDDECL that we'll be keeping. */
5165 mark_weak (olddecl);
5167 else
5168 /* OLDDECL was weak, but NEWDECL was not explicitly marked as
5169 weak. Just update NEWDECL to indicate that it's weak too. */
5170 mark_weak (newdecl);
5173 /* Declare DECL to be a weak symbol. */
5175 void
5176 declare_weak (tree decl)
5178 gcc_assert (TREE_CODE (decl) != FUNCTION_DECL || !TREE_ASM_WRITTEN (decl));
5179 if (! TREE_PUBLIC (decl))
5180 error ("weak declaration of %q+D must be public", decl);
5181 else if (!TARGET_SUPPORTS_WEAK)
5182 warning (0, "weak declaration of %q+D not supported", decl);
5184 mark_weak (decl);
5185 if (!lookup_attribute ("weak", DECL_ATTRIBUTES (decl)))
5186 DECL_ATTRIBUTES (decl)
5187 = tree_cons (get_identifier ("weak"), NULL, DECL_ATTRIBUTES (decl));
5190 static void
5191 weak_finish_1 (tree decl)
5193 #if defined (ASM_WEAKEN_DECL) || defined (ASM_WEAKEN_LABEL)
5194 const char *const name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5195 #endif
5197 if (! TREE_USED (decl))
5198 return;
5200 #ifdef ASM_WEAKEN_DECL
5201 ASM_WEAKEN_DECL (asm_out_file, decl, name, NULL);
5202 #else
5203 #ifdef ASM_WEAKEN_LABEL
5204 ASM_WEAKEN_LABEL (asm_out_file, name);
5205 #else
5206 #ifdef ASM_OUTPUT_WEAK_ALIAS
5208 static bool warn_once = 0;
5209 if (! warn_once)
5211 warning (0, "only weak aliases are supported in this configuration");
5212 warn_once = 1;
5214 return;
5216 #endif
5217 #endif
5218 #endif
5221 /* This TREE_LIST contains weakref targets. */
5223 static GTY(()) tree weakref_targets;
5225 /* Forward declaration. */
5226 static tree find_decl_and_mark_needed (tree decl, tree target);
5228 /* Emit any pending weak declarations. */
5230 void
5231 weak_finish (void)
5233 tree t;
5235 for (t = weakref_targets; t; t = TREE_CHAIN (t))
5237 tree alias_decl = TREE_PURPOSE (t);
5238 tree target = ultimate_transparent_alias_target (&TREE_VALUE (t));
5240 if (! TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (alias_decl)))
5241 /* Remove alias_decl from the weak list, but leave entries for
5242 the target alone. */
5243 target = NULL_TREE;
5244 #ifndef ASM_OUTPUT_WEAKREF
5245 else if (! TREE_SYMBOL_REFERENCED (target))
5247 /* Use ASM_WEAKEN_LABEL only if ASM_WEAKEN_DECL is not
5248 defined, otherwise we and weak_finish_1 would use
5249 different macros. */
5250 # if defined ASM_WEAKEN_LABEL && ! defined ASM_WEAKEN_DECL
5251 ASM_WEAKEN_LABEL (asm_out_file, IDENTIFIER_POINTER (target));
5252 # else
5253 tree decl = find_decl_and_mark_needed (alias_decl, target);
5255 if (! decl)
5257 decl = build_decl (DECL_SOURCE_LOCATION (alias_decl),
5258 TREE_CODE (alias_decl), target,
5259 TREE_TYPE (alias_decl));
5261 DECL_EXTERNAL (decl) = 1;
5262 TREE_PUBLIC (decl) = 1;
5263 DECL_ARTIFICIAL (decl) = 1;
5264 TREE_NOTHROW (decl) = TREE_NOTHROW (alias_decl);
5265 TREE_USED (decl) = 1;
5268 weak_finish_1 (decl);
5269 # endif
5271 #endif
5274 tree *p;
5275 tree t2;
5277 /* Remove the alias and the target from the pending weak list
5278 so that we do not emit any .weak directives for the former,
5279 nor multiple .weak directives for the latter. */
5280 for (p = &weak_decls; (t2 = *p) ; )
5282 if (TREE_VALUE (t2) == alias_decl
5283 || target == DECL_ASSEMBLER_NAME (TREE_VALUE (t2)))
5284 *p = TREE_CHAIN (t2);
5285 else
5286 p = &TREE_CHAIN (t2);
5289 /* Remove other weakrefs to the same target, to speed things up. */
5290 for (p = &TREE_CHAIN (t); (t2 = *p) ; )
5292 if (target == ultimate_transparent_alias_target (&TREE_VALUE (t2)))
5293 *p = TREE_CHAIN (t2);
5294 else
5295 p = &TREE_CHAIN (t2);
5300 for (t = weak_decls; t; t = TREE_CHAIN (t))
5302 tree decl = TREE_VALUE (t);
5304 weak_finish_1 (decl);
5308 /* Emit the assembly bits to indicate that DECL is globally visible. */
5310 static void
5311 globalize_decl (tree decl)
5314 #if defined (ASM_WEAKEN_LABEL) || defined (ASM_WEAKEN_DECL)
5315 if (DECL_WEAK (decl))
5317 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
5318 tree *p, t;
5320 #ifdef ASM_WEAKEN_DECL
5321 ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
5322 #else
5323 ASM_WEAKEN_LABEL (asm_out_file, name);
5324 #endif
5326 /* Remove this function from the pending weak list so that
5327 we do not emit multiple .weak directives for it. */
5328 for (p = &weak_decls; (t = *p) ; )
5330 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
5331 *p = TREE_CHAIN (t);
5332 else
5333 p = &TREE_CHAIN (t);
5336 /* Remove weakrefs to the same target from the pending weakref
5337 list, for the same reason. */
5338 for (p = &weakref_targets; (t = *p) ; )
5340 if (DECL_ASSEMBLER_NAME (decl)
5341 == ultimate_transparent_alias_target (&TREE_VALUE (t)))
5342 *p = TREE_CHAIN (t);
5343 else
5344 p = &TREE_CHAIN (t);
5347 return;
5349 #endif
5351 targetm.asm_out.globalize_decl_name (asm_out_file, decl);
5354 VEC(alias_pair,gc) *alias_pairs;
5356 /* Given an assembly name, find the decl it is associated with. At the
5357 same time, mark it needed for cgraph. */
5359 static tree
5360 find_decl_and_mark_needed (tree decl, tree target)
5362 struct cgraph_node *fnode = NULL;
5363 struct varpool_node *vnode = NULL;
5365 if (TREE_CODE (decl) == FUNCTION_DECL)
5367 fnode = cgraph_node_for_asm (target);
5368 if (fnode == NULL)
5369 vnode = varpool_node_for_asm (target);
5371 else
5373 vnode = varpool_node_for_asm (target);
5374 if (vnode == NULL)
5375 fnode = cgraph_node_for_asm (target);
5378 if (fnode)
5380 cgraph_mark_needed_node (fnode);
5381 return fnode->decl;
5383 else if (vnode)
5385 varpool_mark_needed_node (vnode);
5386 vnode->force_output = 1;
5387 return vnode->decl;
5389 else
5390 return NULL_TREE;
5393 /* Output the assembler code for a define (equate) using ASM_OUTPUT_DEF
5394 or ASM_OUTPUT_DEF_FROM_DECLS. The function defines the symbol whose
5395 tree node is DECL to have the value of the tree node TARGET. */
5397 static void
5398 do_assemble_alias (tree decl, tree target)
5400 /* Emulated TLS had better not get this var. */
5401 gcc_assert(!(!targetm.have_tls
5402 && TREE_CODE (decl) == VAR_DECL
5403 && DECL_THREAD_LOCAL_P (decl)));
5405 if (TREE_ASM_WRITTEN (decl))
5406 return;
5408 /* We must force creation of DECL_RTL for debug info generation, even though
5409 we don't use it here. */
5410 make_decl_rtl (decl);
5412 TREE_ASM_WRITTEN (decl) = 1;
5413 TREE_ASM_WRITTEN (DECL_ASSEMBLER_NAME (decl)) = 1;
5415 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
5417 ultimate_transparent_alias_target (&target);
5419 if (!TREE_SYMBOL_REFERENCED (target))
5420 weakref_targets = tree_cons (decl, target, weakref_targets);
5422 #ifdef ASM_OUTPUT_WEAKREF
5423 ASM_OUTPUT_WEAKREF (asm_out_file, decl,
5424 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
5425 IDENTIFIER_POINTER (target));
5426 #else
5427 if (!TARGET_SUPPORTS_WEAK)
5429 error_at (DECL_SOURCE_LOCATION (decl),
5430 "weakref is not supported in this configuration");
5431 return;
5433 #endif
5434 return;
5437 #ifdef ASM_OUTPUT_DEF
5438 /* Make name accessible from other files, if appropriate. */
5440 if (TREE_PUBLIC (decl))
5442 globalize_decl (decl);
5443 maybe_assemble_visibility (decl);
5445 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
5447 #if defined (ASM_OUTPUT_TYPE_DIRECTIVE) && HAVE_GNU_INDIRECT_FUNCTION
5448 ASM_OUTPUT_TYPE_DIRECTIVE
5449 (asm_out_file, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
5450 IFUNC_ASM_TYPE);
5451 #else
5452 error_at (DECL_SOURCE_LOCATION (decl),
5453 "ifunc is not supported in this configuration");
5454 #endif
5457 # ifdef ASM_OUTPUT_DEF_FROM_DECLS
5458 ASM_OUTPUT_DEF_FROM_DECLS (asm_out_file, decl, target);
5459 # else
5460 ASM_OUTPUT_DEF (asm_out_file,
5461 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
5462 IDENTIFIER_POINTER (target));
5463 # endif
5464 #elif defined (ASM_OUTPUT_WEAK_ALIAS) || defined (ASM_WEAKEN_DECL)
5466 const char *name;
5467 tree *p, t;
5469 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5470 # ifdef ASM_WEAKEN_DECL
5471 ASM_WEAKEN_DECL (asm_out_file, decl, name, IDENTIFIER_POINTER (target));
5472 # else
5473 ASM_OUTPUT_WEAK_ALIAS (asm_out_file, name, IDENTIFIER_POINTER (target));
5474 # endif
5475 /* Remove this function from the pending weak list so that
5476 we do not emit multiple .weak directives for it. */
5477 for (p = &weak_decls; (t = *p) ; )
5478 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
5479 *p = TREE_CHAIN (t);
5480 else
5481 p = &TREE_CHAIN (t);
5483 /* Remove weakrefs to the same target from the pending weakref
5484 list, for the same reason. */
5485 for (p = &weakref_targets; (t = *p) ; )
5487 if (DECL_ASSEMBLER_NAME (decl)
5488 == ultimate_transparent_alias_target (&TREE_VALUE (t)))
5489 *p = TREE_CHAIN (t);
5490 else
5491 p = &TREE_CHAIN (t);
5494 #endif
5498 /* Allocate and construct a symbol alias set. */
5500 static symbol_alias_set_t *
5501 symbol_alias_set_create (void)
5503 return pointer_set_create ();
5506 /* Destruct and free a symbol alias set. */
5508 void
5509 symbol_alias_set_destroy (symbol_alias_set_t *aset)
5511 pointer_set_destroy (aset);
5514 /* Test if a symbol alias set contains a given name. */
5517 symbol_alias_set_contains (const symbol_alias_set_t *aset, tree t)
5519 /* We accept either a DECL or an IDENTIFIER directly. */
5520 if (TREE_CODE (t) != IDENTIFIER_NODE)
5521 t = DECL_ASSEMBLER_NAME (t);
5522 t = targetm.asm_out.mangle_assembler_name (IDENTIFIER_POINTER (t));
5523 return pointer_set_contains (aset, t);
5526 /* Enter a new name into a symbol alias set. */
5528 static int
5529 symbol_alias_set_insert (symbol_alias_set_t *aset, tree t)
5531 /* We accept either a DECL or an IDENTIFIER directly. */
5532 if (TREE_CODE (t) != IDENTIFIER_NODE)
5533 t = DECL_ASSEMBLER_NAME (t);
5534 t = targetm.asm_out.mangle_assembler_name (IDENTIFIER_POINTER (t));
5535 return pointer_set_insert (aset, t);
5538 /* IN_SET_P is a predicate function assuming to be taken
5539 alias_pair->decl, alias_pair->target and DATA arguments.
5541 Compute set of aliases by including everything where TRIVIALLY_VISIBLE
5542 predeicate is true and propagate across aliases such that when
5543 alias DECL is included, its TARGET is included too. */
5545 static symbol_alias_set_t *
5546 propagate_aliases_forward (bool (*in_set_p)
5547 (tree decl, tree target, void *data),
5548 void *data)
5550 symbol_alias_set_t *set;
5551 unsigned i;
5552 alias_pair *p;
5553 bool changed;
5555 set = symbol_alias_set_create ();
5556 for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); ++i)
5557 if (in_set_p (p->decl, p->target, data))
5558 symbol_alias_set_insert (set, p->decl);
5561 changed = false;
5562 for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); ++i)
5563 if (symbol_alias_set_contains (set, p->decl)
5564 && !symbol_alias_set_insert (set, p->target))
5565 changed = true;
5567 while (changed);
5569 return set;
5572 /* Like propagate_aliases_forward but do backward propagation. */
5574 symbol_alias_set_t *
5575 propagate_aliases_backward (bool (*in_set_p)
5576 (tree decl, tree target, void *data),
5577 void *data)
5579 symbol_alias_set_t *set;
5580 unsigned i;
5581 alias_pair *p;
5582 bool changed;
5584 /* We have to compute the set of set nodes including aliases
5585 themselves. */
5586 set = symbol_alias_set_create ();
5587 for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); ++i)
5588 if (in_set_p (p->decl, p->target, data))
5589 symbol_alias_set_insert (set, p->target);
5592 changed = false;
5593 for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); ++i)
5594 if (symbol_alias_set_contains (set, p->target)
5595 && !symbol_alias_set_insert (set, p->decl))
5596 changed = true;
5598 while (changed);
5600 return set;
5602 /* See if the alias is trivially visible. This means
5603 1) alias is expoerted from the unit or
5604 2) alias is used in the code.
5605 We assume that unused cgraph/varpool nodes has been
5606 removed.
5607 Used as callback for propagate_aliases. */
5609 static bool
5610 trivially_visible_alias (tree decl, tree target ATTRIBUTE_UNUSED,
5611 void *data ATTRIBUTE_UNUSED)
5613 struct cgraph_node *fnode = NULL;
5614 struct varpool_node *vnode = NULL;
5616 if (!TREE_PUBLIC (decl))
5618 if (TREE_CODE (decl) == FUNCTION_DECL)
5619 fnode = cgraph_get_node (decl);
5620 else
5621 vnode = varpool_get_node (decl);
5622 return vnode || fnode;
5624 else
5625 return true;
5628 /* See if the target of alias is defined in this unit.
5629 Used as callback for propagate_aliases. */
5631 static bool
5632 trivially_defined_alias (tree decl ATTRIBUTE_UNUSED,
5633 tree target,
5634 void *data ATTRIBUTE_UNUSED)
5636 struct cgraph_node *fnode = NULL;
5637 struct varpool_node *vnode = NULL;
5639 fnode = cgraph_node_for_asm (target);
5640 vnode = (fnode == NULL) ? varpool_node_for_asm (target) : NULL;
5641 return (fnode && fnode->analyzed) || (vnode && vnode->finalized);
5644 /* Remove the alias pairing for functions that are no longer in the call
5645 graph. */
5647 void
5648 remove_unreachable_alias_pairs (void)
5650 symbol_alias_set_t *visible;
5651 unsigned i;
5652 alias_pair *p;
5654 if (alias_pairs == NULL)
5655 return;
5657 /* We have to compute the set of visible nodes including aliases
5658 themselves. */
5659 visible = propagate_aliases_forward (trivially_visible_alias, NULL);
5661 for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); )
5663 if (!DECL_EXTERNAL (p->decl)
5664 && !symbol_alias_set_contains (visible, p->decl))
5666 VEC_unordered_remove (alias_pair, alias_pairs, i);
5667 continue;
5670 i++;
5673 symbol_alias_set_destroy (visible);
5677 /* First pass of completing pending aliases. Make sure that cgraph knows
5678 which symbols will be required. */
5680 void
5681 finish_aliases_1 (void)
5683 symbol_alias_set_t *defined;
5684 unsigned i;
5685 alias_pair *p;
5687 if (alias_pairs == NULL)
5688 return;
5690 /* We have to compute the set of defined nodes including aliases
5691 themselves. */
5692 defined = propagate_aliases_backward (trivially_defined_alias, NULL);
5694 FOR_EACH_VEC_ELT (alias_pair, alias_pairs, i, p)
5696 tree target_decl;
5698 target_decl = find_decl_and_mark_needed (p->decl, p->target);
5699 if (target_decl == NULL)
5701 if (symbol_alias_set_contains (defined, p->target))
5702 continue;
5704 if (! (p->emitted_diags & ALIAS_DIAG_TO_UNDEF)
5705 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
5707 error ("%q+D aliased to undefined symbol %qE",
5708 p->decl, p->target);
5709 p->emitted_diags |= ALIAS_DIAG_TO_UNDEF;
5712 else if (! (p->emitted_diags & ALIAS_DIAG_TO_EXTERN)
5713 && DECL_EXTERNAL (target_decl)
5714 /* We use local aliases for C++ thunks to force the tailcall
5715 to bind locally. This is a hack - to keep it working do
5716 the following (which is not strictly correct). */
5717 && (! TREE_CODE (target_decl) == FUNCTION_DECL
5718 || ! DECL_VIRTUAL_P (target_decl))
5719 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
5721 error ("%q+D aliased to external symbol %qE",
5722 p->decl, p->target);
5723 p->emitted_diags |= ALIAS_DIAG_TO_EXTERN;
5727 symbol_alias_set_destroy (defined);
5730 /* Second pass of completing pending aliases. Emit the actual assembly.
5731 This happens at the end of compilation and thus it is assured that the
5732 target symbol has been emitted. */
5734 void
5735 finish_aliases_2 (void)
5737 unsigned i;
5738 alias_pair *p;
5740 FOR_EACH_VEC_ELT (alias_pair, alias_pairs, i, p)
5741 do_assemble_alias (p->decl, p->target);
5743 VEC_truncate (alias_pair, alias_pairs, 0);
5746 /* Emit an assembler directive to make the symbol for DECL an alias to
5747 the symbol for TARGET. */
5749 void
5750 assemble_alias (tree decl, tree target)
5752 tree target_decl;
5753 bool is_weakref = false;
5755 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
5757 tree alias = DECL_ASSEMBLER_NAME (decl);
5759 is_weakref = true;
5761 ultimate_transparent_alias_target (&target);
5763 if (alias == target)
5764 error ("weakref %q+D ultimately targets itself", decl);
5765 else
5767 #ifndef ASM_OUTPUT_WEAKREF
5768 IDENTIFIER_TRANSPARENT_ALIAS (alias) = 1;
5769 TREE_CHAIN (alias) = target;
5770 #endif
5772 if (TREE_PUBLIC (decl))
5773 error ("weakref %q+D must have static linkage", decl);
5775 else
5777 #if !defined (ASM_OUTPUT_DEF)
5778 # if !defined(ASM_OUTPUT_WEAK_ALIAS) && !defined (ASM_WEAKEN_DECL)
5779 error_at (DECL_SOURCE_LOCATION (decl),
5780 "alias definitions not supported in this configuration");
5781 return;
5782 # else
5783 if (!DECL_WEAK (decl))
5785 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
5786 error_at (DECL_SOURCE_LOCATION (decl),
5787 "ifunc is not supported in this configuration");
5788 else
5789 error_at (DECL_SOURCE_LOCATION (decl),
5790 "only weak aliases are supported in this configuration");
5791 return;
5793 # endif
5794 #endif
5796 TREE_USED (decl) = 1;
5798 /* A quirk of the initial implementation of aliases required that the user
5799 add "extern" to all of them. Which is silly, but now historical. Do
5800 note that the symbol is in fact locally defined. */
5801 if (! is_weakref)
5802 DECL_EXTERNAL (decl) = 0;
5804 /* Allow aliases to aliases. */
5805 if (TREE_CODE (decl) == FUNCTION_DECL)
5806 cgraph_node (decl)->alias = true;
5807 else
5808 varpool_node (decl)->alias = true;
5810 /* If the target has already been emitted, we don't have to queue the
5811 alias. This saves a tad of memory. */
5812 if (cgraph_global_info_ready)
5813 target_decl = find_decl_and_mark_needed (decl, target);
5814 else
5815 target_decl= NULL;
5816 if (target_decl && TREE_ASM_WRITTEN (target_decl))
5817 do_assemble_alias (decl, target);
5818 else
5820 alias_pair *p = VEC_safe_push (alias_pair, gc, alias_pairs, NULL);
5821 p->decl = decl;
5822 p->target = target;
5823 p->emitted_diags = ALIAS_DIAG_NONE;
5827 /* Emit an assembler directive to set symbol for DECL visibility to
5828 the visibility type VIS, which must not be VISIBILITY_DEFAULT. */
5830 void
5831 default_assemble_visibility (tree decl ATTRIBUTE_UNUSED,
5832 int vis ATTRIBUTE_UNUSED)
5834 #ifdef HAVE_GAS_HIDDEN
5835 static const char * const visibility_types[] = {
5836 NULL, "protected", "hidden", "internal"
5839 const char *name, *type;
5841 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5842 type = visibility_types[vis];
5844 fprintf (asm_out_file, "\t.%s\t", type);
5845 assemble_name (asm_out_file, name);
5846 fprintf (asm_out_file, "\n");
5847 #else
5848 warning (OPT_Wattributes, "visibility attribute not supported "
5849 "in this configuration; ignored");
5850 #endif
5853 /* A helper function to call assemble_visibility when needed for a decl. */
5856 maybe_assemble_visibility (tree decl)
5858 enum symbol_visibility vis = DECL_VISIBILITY (decl);
5860 if (vis != VISIBILITY_DEFAULT)
5862 targetm.asm_out.assemble_visibility (decl, vis);
5863 return 1;
5865 else
5866 return 0;
5869 /* Returns 1 if the target configuration supports defining public symbols
5870 so that one of them will be chosen at link time instead of generating a
5871 multiply-defined symbol error, whether through the use of weak symbols or
5872 a target-specific mechanism for having duplicates discarded. */
5875 supports_one_only (void)
5877 if (SUPPORTS_ONE_ONLY)
5878 return 1;
5879 return TARGET_SUPPORTS_WEAK;
5882 /* Set up DECL as a public symbol that can be defined in multiple
5883 translation units without generating a linker error. */
5885 void
5886 make_decl_one_only (tree decl, tree comdat_group)
5888 gcc_assert (TREE_CODE (decl) == VAR_DECL
5889 || TREE_CODE (decl) == FUNCTION_DECL);
5891 TREE_PUBLIC (decl) = 1;
5893 if (SUPPORTS_ONE_ONLY)
5895 #ifdef MAKE_DECL_ONE_ONLY
5896 MAKE_DECL_ONE_ONLY (decl);
5897 #endif
5898 DECL_COMDAT_GROUP (decl) = comdat_group;
5900 else if (TREE_CODE (decl) == VAR_DECL
5901 && (DECL_INITIAL (decl) == 0 || DECL_INITIAL (decl) == error_mark_node))
5902 DECL_COMMON (decl) = 1;
5903 else
5905 gcc_assert (TARGET_SUPPORTS_WEAK);
5906 DECL_WEAK (decl) = 1;
5910 void
5911 init_varasm_once (void)
5913 section_htab = htab_create_ggc (31, section_entry_hash,
5914 section_entry_eq, NULL);
5915 object_block_htab = htab_create_ggc (31, object_block_entry_hash,
5916 object_block_entry_eq, NULL);
5917 const_desc_htab = htab_create_ggc (1009, const_desc_hash,
5918 const_desc_eq, NULL);
5920 const_alias_set = new_alias_set ();
5921 shared_constant_pool = create_constant_pool ();
5923 #ifdef TEXT_SECTION_ASM_OP
5924 text_section = get_unnamed_section (SECTION_CODE, output_section_asm_op,
5925 TEXT_SECTION_ASM_OP);
5926 #endif
5928 #ifdef DATA_SECTION_ASM_OP
5929 data_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
5930 DATA_SECTION_ASM_OP);
5931 #endif
5933 #ifdef SDATA_SECTION_ASM_OP
5934 sdata_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
5935 SDATA_SECTION_ASM_OP);
5936 #endif
5938 #ifdef READONLY_DATA_SECTION_ASM_OP
5939 readonly_data_section = get_unnamed_section (0, output_section_asm_op,
5940 READONLY_DATA_SECTION_ASM_OP);
5941 #endif
5943 #ifdef CTORS_SECTION_ASM_OP
5944 ctors_section = get_unnamed_section (0, output_section_asm_op,
5945 CTORS_SECTION_ASM_OP);
5946 #endif
5948 #ifdef DTORS_SECTION_ASM_OP
5949 dtors_section = get_unnamed_section (0, output_section_asm_op,
5950 DTORS_SECTION_ASM_OP);
5951 #endif
5953 #ifdef BSS_SECTION_ASM_OP
5954 bss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
5955 output_section_asm_op,
5956 BSS_SECTION_ASM_OP);
5957 #endif
5959 #ifdef SBSS_SECTION_ASM_OP
5960 sbss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
5961 output_section_asm_op,
5962 SBSS_SECTION_ASM_OP);
5963 #endif
5965 tls_comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
5966 | SECTION_COMMON, emit_tls_common);
5967 lcomm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
5968 | SECTION_COMMON, emit_local);
5969 comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
5970 | SECTION_COMMON, emit_common);
5972 #if defined ASM_OUTPUT_ALIGNED_BSS
5973 bss_noswitch_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS,
5974 emit_bss);
5975 #endif
5977 targetm.asm_out.init_sections ();
5979 if (readonly_data_section == NULL)
5980 readonly_data_section = text_section;
5983 enum tls_model
5984 decl_default_tls_model (const_tree decl)
5986 enum tls_model kind;
5987 bool is_local;
5989 is_local = targetm.binds_local_p (decl);
5990 if (!flag_shlib)
5992 if (is_local)
5993 kind = TLS_MODEL_LOCAL_EXEC;
5994 else
5995 kind = TLS_MODEL_INITIAL_EXEC;
5998 /* Local dynamic is inefficient when we're not combining the
5999 parts of the address. */
6000 else if (optimize && is_local)
6001 kind = TLS_MODEL_LOCAL_DYNAMIC;
6002 else
6003 kind = TLS_MODEL_GLOBAL_DYNAMIC;
6004 if (kind < flag_tls_default)
6005 kind = flag_tls_default;
6007 return kind;
6010 /* Select a set of attributes for section NAME based on the properties
6011 of DECL and whether or not RELOC indicates that DECL's initializer
6012 might contain runtime relocations.
6014 We make the section read-only and executable for a function decl,
6015 read-only for a const data decl, and writable for a non-const data decl. */
6017 unsigned int
6018 default_section_type_flags (tree decl, const char *name, int reloc)
6020 unsigned int flags;
6022 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
6023 flags = SECTION_CODE;
6024 else if (decl)
6026 enum section_category category
6027 = categorize_decl_for_section (decl, reloc);
6028 if (decl_readonly_section_1 (category))
6029 flags = 0;
6030 else if (category == SECCAT_DATA_REL_RO
6031 || category == SECCAT_DATA_REL_RO_LOCAL)
6032 flags = SECTION_WRITE | SECTION_RELRO;
6033 else
6034 flags = SECTION_WRITE;
6036 else
6038 flags = SECTION_WRITE;
6039 if (strcmp (name, ".data.rel.ro") == 0
6040 || strcmp (name, ".data.rel.ro.local") == 0)
6041 flags |= SECTION_RELRO;
6044 if (decl && DECL_ONE_ONLY (decl))
6045 flags |= SECTION_LINKONCE;
6047 if (decl && TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
6048 flags |= SECTION_TLS | SECTION_WRITE;
6050 if (strcmp (name, ".bss") == 0
6051 || strncmp (name, ".bss.", 5) == 0
6052 || strncmp (name, ".gnu.linkonce.b.", 16) == 0
6053 || strcmp (name, ".sbss") == 0
6054 || strncmp (name, ".sbss.", 6) == 0
6055 || strncmp (name, ".gnu.linkonce.sb.", 17) == 0)
6056 flags |= SECTION_BSS;
6058 if (strcmp (name, ".tdata") == 0
6059 || strncmp (name, ".tdata.", 7) == 0
6060 || strncmp (name, ".gnu.linkonce.td.", 17) == 0)
6061 flags |= SECTION_TLS;
6063 if (strcmp (name, ".tbss") == 0
6064 || strncmp (name, ".tbss.", 6) == 0
6065 || strncmp (name, ".gnu.linkonce.tb.", 17) == 0)
6066 flags |= SECTION_TLS | SECTION_BSS;
6068 /* These three sections have special ELF types. They are neither
6069 SHT_PROGBITS nor SHT_NOBITS, so when changing sections we don't
6070 want to print a section type (@progbits or @nobits). If someone
6071 is silly enough to emit code or TLS variables to one of these
6072 sections, then don't handle them specially. */
6073 if (!(flags & (SECTION_CODE | SECTION_BSS | SECTION_TLS))
6074 && (strcmp (name, ".init_array") == 0
6075 || strcmp (name, ".fini_array") == 0
6076 || strcmp (name, ".preinit_array") == 0))
6077 flags |= SECTION_NOTYPE;
6079 return flags;
6082 /* Return true if the target supports some form of global BSS,
6083 either through bss_noswitch_section, or by selecting a BSS
6084 section in TARGET_ASM_SELECT_SECTION. */
6086 bool
6087 have_global_bss_p (void)
6089 return bss_noswitch_section || targetm.have_switchable_bss_sections;
6092 /* Output assembly to switch to section NAME with attribute FLAGS.
6093 Four variants for common object file formats. */
6095 void
6096 default_no_named_section (const char *name ATTRIBUTE_UNUSED,
6097 unsigned int flags ATTRIBUTE_UNUSED,
6098 tree decl ATTRIBUTE_UNUSED)
6100 /* Some object formats don't support named sections at all. The
6101 front-end should already have flagged this as an error. */
6102 gcc_unreachable ();
6105 #ifndef TLS_SECTION_ASM_FLAG
6106 #define TLS_SECTION_ASM_FLAG 'T'
6107 #endif
6109 void
6110 default_elf_asm_named_section (const char *name, unsigned int flags,
6111 tree decl ATTRIBUTE_UNUSED)
6113 char flagchars[10], *f = flagchars;
6115 /* If we have already declared this section, we can use an
6116 abbreviated form to switch back to it -- unless this section is
6117 part of a COMDAT groups, in which case GAS requires the full
6118 declaration every time. */
6119 if (!(HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6120 && (flags & SECTION_DECLARED))
6122 fprintf (asm_out_file, "\t.section\t%s\n", name);
6123 return;
6126 if (!(flags & SECTION_DEBUG))
6127 *f++ = 'a';
6128 if (flags & SECTION_WRITE)
6129 *f++ = 'w';
6130 if (flags & SECTION_CODE)
6131 *f++ = 'x';
6132 if (flags & SECTION_SMALL)
6133 *f++ = 's';
6134 if (flags & SECTION_MERGE)
6135 *f++ = 'M';
6136 if (flags & SECTION_STRINGS)
6137 *f++ = 'S';
6138 if (flags & SECTION_TLS)
6139 *f++ = TLS_SECTION_ASM_FLAG;
6140 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6141 *f++ = 'G';
6142 *f = '\0';
6144 fprintf (asm_out_file, "\t.section\t%s,\"%s\"", name, flagchars);
6146 if (!(flags & SECTION_NOTYPE))
6148 const char *type;
6149 const char *format;
6151 if (flags & SECTION_BSS)
6152 type = "nobits";
6153 else
6154 type = "progbits";
6156 format = ",@%s";
6157 #ifdef ASM_COMMENT_START
6158 /* On platforms that use "@" as the assembly comment character,
6159 use "%" instead. */
6160 if (strcmp (ASM_COMMENT_START, "@") == 0)
6161 format = ",%%%s";
6162 #endif
6163 fprintf (asm_out_file, format, type);
6165 if (flags & SECTION_ENTSIZE)
6166 fprintf (asm_out_file, ",%d", flags & SECTION_ENTSIZE);
6167 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6169 if (TREE_CODE (decl) == IDENTIFIER_NODE)
6170 fprintf (asm_out_file, ",%s,comdat", IDENTIFIER_POINTER (decl));
6171 else
6172 fprintf (asm_out_file, ",%s,comdat",
6173 IDENTIFIER_POINTER (DECL_COMDAT_GROUP (decl)));
6177 putc ('\n', asm_out_file);
6180 void
6181 default_coff_asm_named_section (const char *name, unsigned int flags,
6182 tree decl ATTRIBUTE_UNUSED)
6184 char flagchars[8], *f = flagchars;
6186 if (flags & SECTION_WRITE)
6187 *f++ = 'w';
6188 if (flags & SECTION_CODE)
6189 *f++ = 'x';
6190 *f = '\0';
6192 fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
6195 void
6196 default_pe_asm_named_section (const char *name, unsigned int flags,
6197 tree decl)
6199 default_coff_asm_named_section (name, flags, decl);
6201 if (flags & SECTION_LINKONCE)
6203 /* Functions may have been compiled at various levels of
6204 optimization so we can't use `same_size' here.
6205 Instead, have the linker pick one. */
6206 fprintf (asm_out_file, "\t.linkonce %s\n",
6207 (flags & SECTION_CODE ? "discard" : "same_size"));
6211 /* The lame default section selector. */
6213 section *
6214 default_select_section (tree decl, int reloc,
6215 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
6217 if (DECL_P (decl))
6219 if (decl_readonly_section (decl, reloc))
6220 return readonly_data_section;
6222 else if (TREE_CODE (decl) == CONSTRUCTOR)
6224 if (! ((flag_pic && reloc)
6225 || !TREE_READONLY (decl)
6226 || TREE_SIDE_EFFECTS (decl)
6227 || !TREE_CONSTANT (decl)))
6228 return readonly_data_section;
6230 else if (TREE_CODE (decl) == STRING_CST)
6231 return readonly_data_section;
6232 else if (! (flag_pic && reloc))
6233 return readonly_data_section;
6235 return data_section;
6238 enum section_category
6239 categorize_decl_for_section (const_tree decl, int reloc)
6241 enum section_category ret;
6243 if (TREE_CODE (decl) == FUNCTION_DECL)
6244 return SECCAT_TEXT;
6245 else if (TREE_CODE (decl) == STRING_CST)
6247 if (flag_mudflap) /* or !flag_merge_constants */
6248 return SECCAT_RODATA;
6249 else
6250 return SECCAT_RODATA_MERGE_STR;
6252 else if (TREE_CODE (decl) == VAR_DECL)
6254 if (bss_initializer_p (decl))
6255 ret = SECCAT_BSS;
6256 else if (! TREE_READONLY (decl)
6257 || TREE_SIDE_EFFECTS (decl)
6258 || ! TREE_CONSTANT (DECL_INITIAL (decl)))
6260 /* Here the reloc_rw_mask is not testing whether the section should
6261 be read-only or not, but whether the dynamic link will have to
6262 do something. If so, we wish to segregate the data in order to
6263 minimize cache misses inside the dynamic linker. */
6264 if (reloc & targetm.asm_out.reloc_rw_mask ())
6265 ret = reloc == 1 ? SECCAT_DATA_REL_LOCAL : SECCAT_DATA_REL;
6266 else
6267 ret = SECCAT_DATA;
6269 else if (reloc & targetm.asm_out.reloc_rw_mask ())
6270 ret = reloc == 1 ? SECCAT_DATA_REL_RO_LOCAL : SECCAT_DATA_REL_RO;
6271 else if (reloc || flag_merge_constants < 2)
6272 /* C and C++ don't allow different variables to share the same
6273 location. -fmerge-all-constants allows even that (at the
6274 expense of not conforming). */
6275 ret = SECCAT_RODATA;
6276 else if (TREE_CODE (DECL_INITIAL (decl)) == STRING_CST)
6277 ret = SECCAT_RODATA_MERGE_STR_INIT;
6278 else
6279 ret = SECCAT_RODATA_MERGE_CONST;
6281 else if (TREE_CODE (decl) == CONSTRUCTOR)
6283 if ((reloc & targetm.asm_out.reloc_rw_mask ())
6284 || TREE_SIDE_EFFECTS (decl)
6285 || ! TREE_CONSTANT (decl))
6286 ret = SECCAT_DATA;
6287 else
6288 ret = SECCAT_RODATA;
6290 else
6291 ret = SECCAT_RODATA;
6293 /* There are no read-only thread-local sections. */
6294 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
6296 /* Note that this would be *just* SECCAT_BSS, except that there's
6297 no concept of a read-only thread-local-data section. */
6298 if (ret == SECCAT_BSS
6299 || (flag_zero_initialized_in_bss
6300 && initializer_zerop (DECL_INITIAL (decl))))
6301 ret = SECCAT_TBSS;
6302 else
6303 ret = SECCAT_TDATA;
6306 /* If the target uses small data sections, select it. */
6307 else if (targetm.in_small_data_p (decl))
6309 if (ret == SECCAT_BSS)
6310 ret = SECCAT_SBSS;
6311 else if (targetm.have_srodata_section && ret == SECCAT_RODATA)
6312 ret = SECCAT_SRODATA;
6313 else
6314 ret = SECCAT_SDATA;
6317 return ret;
6320 static bool
6321 decl_readonly_section_1 (enum section_category category)
6323 switch (category)
6325 case SECCAT_RODATA:
6326 case SECCAT_RODATA_MERGE_STR:
6327 case SECCAT_RODATA_MERGE_STR_INIT:
6328 case SECCAT_RODATA_MERGE_CONST:
6329 case SECCAT_SRODATA:
6330 return true;
6331 default:
6332 return false;
6336 bool
6337 decl_readonly_section (const_tree decl, int reloc)
6339 return decl_readonly_section_1 (categorize_decl_for_section (decl, reloc));
6342 /* Select a section based on the above categorization. */
6344 section *
6345 default_elf_select_section (tree decl, int reloc,
6346 unsigned HOST_WIDE_INT align)
6348 const char *sname;
6349 switch (categorize_decl_for_section (decl, reloc))
6351 case SECCAT_TEXT:
6352 /* We're not supposed to be called on FUNCTION_DECLs. */
6353 gcc_unreachable ();
6354 case SECCAT_RODATA:
6355 return readonly_data_section;
6356 case SECCAT_RODATA_MERGE_STR:
6357 return mergeable_string_section (decl, align, 0);
6358 case SECCAT_RODATA_MERGE_STR_INIT:
6359 return mergeable_string_section (DECL_INITIAL (decl), align, 0);
6360 case SECCAT_RODATA_MERGE_CONST:
6361 return mergeable_constant_section (DECL_MODE (decl), align, 0);
6362 case SECCAT_SRODATA:
6363 sname = ".sdata2";
6364 break;
6365 case SECCAT_DATA:
6366 return data_section;
6367 case SECCAT_DATA_REL:
6368 sname = ".data.rel";
6369 break;
6370 case SECCAT_DATA_REL_LOCAL:
6371 sname = ".data.rel.local";
6372 break;
6373 case SECCAT_DATA_REL_RO:
6374 sname = ".data.rel.ro";
6375 break;
6376 case SECCAT_DATA_REL_RO_LOCAL:
6377 sname = ".data.rel.ro.local";
6378 break;
6379 case SECCAT_SDATA:
6380 sname = ".sdata";
6381 break;
6382 case SECCAT_TDATA:
6383 sname = ".tdata";
6384 break;
6385 case SECCAT_BSS:
6386 if (bss_section)
6387 return bss_section;
6388 sname = ".bss";
6389 break;
6390 case SECCAT_SBSS:
6391 sname = ".sbss";
6392 break;
6393 case SECCAT_TBSS:
6394 sname = ".tbss";
6395 break;
6396 default:
6397 gcc_unreachable ();
6400 if (!DECL_P (decl))
6401 decl = NULL_TREE;
6402 return get_named_section (decl, sname, reloc);
6405 /* Construct a unique section name based on the decl name and the
6406 categorization performed above. */
6408 void
6409 default_unique_section (tree decl, int reloc)
6411 /* We only need to use .gnu.linkonce if we don't have COMDAT groups. */
6412 bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
6413 const char *prefix, *name, *linkonce;
6414 char *string;
6416 switch (categorize_decl_for_section (decl, reloc))
6418 case SECCAT_TEXT:
6419 prefix = one_only ? ".t" : ".text";
6420 break;
6421 case SECCAT_RODATA:
6422 case SECCAT_RODATA_MERGE_STR:
6423 case SECCAT_RODATA_MERGE_STR_INIT:
6424 case SECCAT_RODATA_MERGE_CONST:
6425 prefix = one_only ? ".r" : ".rodata";
6426 break;
6427 case SECCAT_SRODATA:
6428 prefix = one_only ? ".s2" : ".sdata2";
6429 break;
6430 case SECCAT_DATA:
6431 prefix = one_only ? ".d" : ".data";
6432 break;
6433 case SECCAT_DATA_REL:
6434 prefix = one_only ? ".d.rel" : ".data.rel";
6435 break;
6436 case SECCAT_DATA_REL_LOCAL:
6437 prefix = one_only ? ".d.rel.local" : ".data.rel.local";
6438 break;
6439 case SECCAT_DATA_REL_RO:
6440 prefix = one_only ? ".d.rel.ro" : ".data.rel.ro";
6441 break;
6442 case SECCAT_DATA_REL_RO_LOCAL:
6443 prefix = one_only ? ".d.rel.ro.local" : ".data.rel.ro.local";
6444 break;
6445 case SECCAT_SDATA:
6446 prefix = one_only ? ".s" : ".sdata";
6447 break;
6448 case SECCAT_BSS:
6449 prefix = one_only ? ".b" : ".bss";
6450 break;
6451 case SECCAT_SBSS:
6452 prefix = one_only ? ".sb" : ".sbss";
6453 break;
6454 case SECCAT_TDATA:
6455 prefix = one_only ? ".td" : ".tdata";
6456 break;
6457 case SECCAT_TBSS:
6458 prefix = one_only ? ".tb" : ".tbss";
6459 break;
6460 default:
6461 gcc_unreachable ();
6464 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
6465 name = targetm.strip_name_encoding (name);
6467 /* If we're using one_only, then there needs to be a .gnu.linkonce
6468 prefix to the section name. */
6469 linkonce = one_only ? ".gnu.linkonce" : "";
6471 string = ACONCAT ((linkonce, prefix, ".", name, NULL));
6473 DECL_SECTION_NAME (decl) = build_string (strlen (string), string);
6476 /* Like compute_reloc_for_constant, except for an RTX. The return value
6477 is a mask for which bit 1 indicates a global relocation, and bit 0
6478 indicates a local relocation. */
6480 static int
6481 compute_reloc_for_rtx_1 (rtx *xp, void *data)
6483 int *preloc = (int *) data;
6484 rtx x = *xp;
6486 switch (GET_CODE (x))
6488 case SYMBOL_REF:
6489 *preloc |= SYMBOL_REF_LOCAL_P (x) ? 1 : 2;
6490 break;
6491 case LABEL_REF:
6492 *preloc |= 1;
6493 break;
6494 default:
6495 break;
6498 return 0;
6501 static int
6502 compute_reloc_for_rtx (rtx x)
6504 int reloc;
6506 switch (GET_CODE (x))
6508 case CONST:
6509 case SYMBOL_REF:
6510 case LABEL_REF:
6511 reloc = 0;
6512 for_each_rtx (&x, compute_reloc_for_rtx_1, &reloc);
6513 return reloc;
6515 default:
6516 return 0;
6520 section *
6521 default_select_rtx_section (enum machine_mode mode ATTRIBUTE_UNUSED,
6522 rtx x,
6523 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
6525 if (compute_reloc_for_rtx (x) & targetm.asm_out.reloc_rw_mask ())
6526 return data_section;
6527 else
6528 return readonly_data_section;
6531 section *
6532 default_elf_select_rtx_section (enum machine_mode mode, rtx x,
6533 unsigned HOST_WIDE_INT align)
6535 int reloc = compute_reloc_for_rtx (x);
6537 /* ??? Handle small data here somehow. */
6539 if (reloc & targetm.asm_out.reloc_rw_mask ())
6541 if (reloc == 1)
6542 return get_named_section (NULL, ".data.rel.ro.local", 1);
6543 else
6544 return get_named_section (NULL, ".data.rel.ro", 3);
6547 return mergeable_constant_section (mode, align, 0);
6550 /* Set the generally applicable flags on the SYMBOL_REF for EXP. */
6552 void
6553 default_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
6555 rtx symbol;
6556 int flags;
6558 /* Careful not to prod global register variables. */
6559 if (!MEM_P (rtl))
6560 return;
6561 symbol = XEXP (rtl, 0);
6562 if (GET_CODE (symbol) != SYMBOL_REF)
6563 return;
6565 flags = SYMBOL_REF_FLAGS (symbol) & SYMBOL_FLAG_HAS_BLOCK_INFO;
6566 if (TREE_CODE (decl) == FUNCTION_DECL)
6567 flags |= SYMBOL_FLAG_FUNCTION;
6568 if (targetm.binds_local_p (decl))
6569 flags |= SYMBOL_FLAG_LOCAL;
6570 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
6571 flags |= DECL_TLS_MODEL (decl) << SYMBOL_FLAG_TLS_SHIFT;
6572 else if (targetm.in_small_data_p (decl))
6573 flags |= SYMBOL_FLAG_SMALL;
6574 /* ??? Why is DECL_EXTERNAL ever set for non-PUBLIC names? Without
6575 being PUBLIC, the thing *must* be defined in this translation unit.
6576 Prevent this buglet from being propagated into rtl code as well. */
6577 if (DECL_P (decl) && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl))
6578 flags |= SYMBOL_FLAG_EXTERNAL;
6580 SYMBOL_REF_FLAGS (symbol) = flags;
6583 /* By default, we do nothing for encode_section_info, so we need not
6584 do anything but discard the '*' marker. */
6586 const char *
6587 default_strip_name_encoding (const char *str)
6589 return str + (*str == '*');
6592 #ifdef ASM_OUTPUT_DEF
6593 /* The default implementation of TARGET_ASM_OUTPUT_ANCHOR. Define the
6594 anchor relative to ".", the current section position. */
6596 void
6597 default_asm_output_anchor (rtx symbol)
6599 char buffer[100];
6601 sprintf (buffer, "*. + " HOST_WIDE_INT_PRINT_DEC,
6602 SYMBOL_REF_BLOCK_OFFSET (symbol));
6603 ASM_OUTPUT_DEF (asm_out_file, XSTR (symbol, 0), buffer);
6605 #endif
6607 /* The default implementation of TARGET_USE_ANCHORS_FOR_SYMBOL_P. */
6609 bool
6610 default_use_anchors_for_symbol_p (const_rtx symbol)
6612 section *sect;
6613 tree decl;
6615 /* Don't use anchors for mergeable sections. The linker might move
6616 the objects around. */
6617 sect = SYMBOL_REF_BLOCK (symbol)->sect;
6618 if (sect->common.flags & SECTION_MERGE)
6619 return false;
6621 /* Don't use anchors for small data sections. The small data register
6622 acts as an anchor for such sections. */
6623 if (sect->common.flags & SECTION_SMALL)
6624 return false;
6626 decl = SYMBOL_REF_DECL (symbol);
6627 if (decl && DECL_P (decl))
6629 /* Don't use section anchors for decls that might be defined by
6630 other modules. */
6631 if (!targetm.binds_local_p (decl))
6632 return false;
6634 /* Don't use section anchors for decls that will be placed in a
6635 small data section. */
6636 /* ??? Ideally, this check would be redundant with the SECTION_SMALL
6637 one above. The problem is that we only use SECTION_SMALL for
6638 sections that should be marked as small in the section directive. */
6639 if (targetm.in_small_data_p (decl))
6640 return false;
6642 return true;
6645 /* Return true when RESOLUTION indicate that symbol will be bound to the
6646 definition provided by current .o file. */
6648 static bool
6649 resolution_to_local_definition_p (enum ld_plugin_symbol_resolution resolution)
6651 return (resolution == LDPR_PREVAILING_DEF
6652 || resolution == LDPR_PREVAILING_DEF_IRONLY);
6655 /* Return true when RESOLUTION indicate that symbol will be bound locally
6656 within current executable or DSO. */
6658 static bool
6659 resolution_local_p (enum ld_plugin_symbol_resolution resolution)
6661 return (resolution == LDPR_PREVAILING_DEF
6662 || resolution == LDPR_PREVAILING_DEF_IRONLY
6663 || resolution == LDPR_PREEMPTED_REG
6664 || resolution == LDPR_PREEMPTED_IR
6665 || resolution == LDPR_RESOLVED_IR
6666 || resolution == LDPR_RESOLVED_EXEC);
6669 /* Assume ELF-ish defaults, since that's pretty much the most liberal
6670 wrt cross-module name binding. */
6672 bool
6673 default_binds_local_p (const_tree exp)
6675 return default_binds_local_p_1 (exp, flag_shlib);
6678 bool
6679 default_binds_local_p_1 (const_tree exp, int shlib)
6681 bool local_p;
6682 bool resolved_locally = false;
6683 bool resolved_to_local_def = false;
6685 /* With resolution file in hands, take look into resolutions.
6686 We can't just return true for resolved_localy symbols,
6687 because dynamic linking might overwrite symbols
6688 in shared libraries. */
6689 if (TREE_CODE (exp) == VAR_DECL && TREE_PUBLIC (exp)
6690 && (TREE_STATIC (exp) || DECL_EXTERNAL (exp)))
6692 struct varpool_node *vnode = varpool_get_node (exp);
6693 if (vnode && resolution_local_p (vnode->resolution))
6694 resolved_locally = true;
6695 if (vnode
6696 && resolution_to_local_definition_p (vnode->resolution))
6697 resolved_to_local_def = true;
6699 else if (TREE_CODE (exp) == FUNCTION_DECL && TREE_PUBLIC (exp))
6701 struct cgraph_node *node = cgraph_get_node_or_alias (exp);
6702 if (node
6703 && resolution_local_p (node->resolution))
6704 resolved_locally = true;
6705 if (node
6706 && resolution_to_local_definition_p (node->resolution))
6707 resolved_to_local_def = true;
6710 /* A non-decl is an entry in the constant pool. */
6711 if (!DECL_P (exp))
6712 local_p = true;
6713 /* Weakrefs may not bind locally, even though the weakref itself is
6714 always static and therefore local.
6715 FIXME: We can resolve this more curefuly by looking at the weakref
6716 alias. */
6717 else if (lookup_attribute ("weakref", DECL_ATTRIBUTES (exp)))
6718 local_p = false;
6719 /* Static variables are always local. */
6720 else if (! TREE_PUBLIC (exp))
6721 local_p = true;
6722 /* A variable is local if the user has said explicitly that it will
6723 be. */
6724 else if ((DECL_VISIBILITY_SPECIFIED (exp)
6725 || resolved_to_local_def)
6726 && DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
6727 local_p = true;
6728 /* Variables defined outside this object might not be local. */
6729 else if (DECL_EXTERNAL (exp) && !resolved_locally)
6730 local_p = false;
6731 /* If defined in this object and visibility is not default, must be
6732 local. */
6733 else if (DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
6734 local_p = true;
6735 /* Default visibility weak data can be overridden by a strong symbol
6736 in another module and so are not local. */
6737 else if (DECL_WEAK (exp)
6738 && !resolved_locally)
6739 local_p = false;
6740 /* If PIC, then assume that any global name can be overridden by
6741 symbols resolved from other modules. */
6742 else if (shlib)
6743 local_p = false;
6744 /* Uninitialized COMMON variable may be unified with symbols
6745 resolved from other modules. */
6746 else if (DECL_COMMON (exp)
6747 && !resolved_locally
6748 && (DECL_INITIAL (exp) == NULL
6749 || DECL_INITIAL (exp) == error_mark_node))
6750 local_p = false;
6751 /* Otherwise we're left with initialized (or non-common) global data
6752 which is of necessity defined locally. */
6753 else
6754 local_p = true;
6756 return local_p;
6759 /* Return true when references to DECL must bind to current definition in
6760 final executable.
6762 The condition is usually equivalent to whether the function binds to the
6763 current module (shared library or executable), that is to binds_local_p.
6764 We use this fact to avoid need for another target hook and implement
6765 the logic using binds_local_p and just special cases where
6766 decl_binds_to_current_def_p is stronger than binds_local_p. In particular
6767 the weak definitions (that can be overwritten at linktime by other
6768 definition from different object file) and when resolution info is available
6769 we simply use the knowledge passed to us by linker plugin. */
6770 bool
6771 decl_binds_to_current_def_p (tree decl)
6773 gcc_assert (DECL_P (decl));
6774 if (!TREE_PUBLIC (decl))
6775 return true;
6776 if (!targetm.binds_local_p (decl))
6777 return false;
6778 /* When resolution is available, just use it. */
6779 if (TREE_CODE (decl) == VAR_DECL
6780 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
6782 struct varpool_node *vnode = varpool_get_node (decl);
6783 if (vnode
6784 && vnode->resolution != LDPR_UNKNOWN)
6785 return resolution_to_local_definition_p (vnode->resolution);
6787 else if (TREE_CODE (decl) == FUNCTION_DECL)
6789 struct cgraph_node *node = cgraph_get_node_or_alias (decl);
6790 if (node
6791 && node->resolution != LDPR_UNKNOWN)
6792 return resolution_to_local_definition_p (node->resolution);
6794 /* Otherwise we have to assume the worst for DECL_WEAK (hidden weaks
6795 binds localy but still can be overwritten).
6796 This rely on fact that binds_local_p behave as decl_replaceable_p
6797 for all other declaration types. */
6798 return !DECL_WEAK (decl);
6801 /* A replaceable function or variable is one which may be replaced
6802 at link-time with an entirely different definition, provided that the
6803 replacement has the same type. For example, functions declared
6804 with __attribute__((weak)) on most systems are replaceable.
6806 COMDAT functions are not replaceable, since all definitions of the
6807 function must be equivalent. It is important that COMDAT functions
6808 not be treated as replaceable so that use of C++ template
6809 instantiations is not penalized. */
6811 bool
6812 decl_replaceable_p (tree decl)
6814 gcc_assert (DECL_P (decl));
6815 if (!TREE_PUBLIC (decl) || DECL_COMDAT (decl))
6816 return false;
6817 return !decl_binds_to_current_def_p (decl);
6820 /* Default function to output code that will globalize a label. A
6821 target must define GLOBAL_ASM_OP or provide its own function to
6822 globalize a label. */
6823 #ifdef GLOBAL_ASM_OP
6824 void
6825 default_globalize_label (FILE * stream, const char *name)
6827 fputs (GLOBAL_ASM_OP, stream);
6828 assemble_name (stream, name);
6829 putc ('\n', stream);
6831 #endif /* GLOBAL_ASM_OP */
6833 /* Default function to output code that will globalize a declaration. */
6834 void
6835 default_globalize_decl_name (FILE * stream, tree decl)
6837 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
6838 targetm.asm_out.globalize_label (stream, name);
6841 /* Default function to output a label for unwind information. The
6842 default is to do nothing. A target that needs nonlocal labels for
6843 unwind information must provide its own function to do this. */
6844 void
6845 default_emit_unwind_label (FILE * stream ATTRIBUTE_UNUSED,
6846 tree decl ATTRIBUTE_UNUSED,
6847 int for_eh ATTRIBUTE_UNUSED,
6848 int empty ATTRIBUTE_UNUSED)
6852 /* Default function to output a label to divide up the exception table.
6853 The default is to do nothing. A target that needs/wants to divide
6854 up the table must provide it's own function to do this. */
6855 void
6856 default_emit_except_table_label (FILE * stream ATTRIBUTE_UNUSED)
6860 /* This is how to output an internal numbered label where PREFIX is
6861 the class of label and LABELNO is the number within the class. */
6863 void
6864 default_generate_internal_label (char *buf, const char *prefix,
6865 unsigned long labelno)
6867 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
6870 /* This is how to output an internal numbered label where PREFIX is
6871 the class of label and LABELNO is the number within the class. */
6873 void
6874 default_internal_label (FILE *stream, const char *prefix,
6875 unsigned long labelno)
6877 char *const buf = (char *) alloca (40 + strlen (prefix));
6878 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
6879 ASM_OUTPUT_INTERNAL_LABEL (stream, buf);
6883 /* The default implementation of ASM_DECLARE_CONSTANT_NAME. */
6885 void
6886 default_asm_declare_constant_name (FILE *file, const char *name,
6887 const_tree exp ATTRIBUTE_UNUSED,
6888 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
6890 assemble_label (file, name);
6893 /* This is the default behavior at the beginning of a file. It's
6894 controlled by two other target-hook toggles. */
6895 void
6896 default_file_start (void)
6898 if (targetm.asm_file_start_app_off
6899 && !(flag_verbose_asm || flag_debug_asm || flag_dump_rtl_in_asm))
6900 fputs (ASM_APP_OFF, asm_out_file);
6902 if (targetm.asm_file_start_file_directive)
6903 output_file_directive (asm_out_file, main_input_filename);
6906 /* This is a generic routine suitable for use as TARGET_ASM_FILE_END
6907 which emits a special section directive used to indicate whether or
6908 not this object file needs an executable stack. This is primarily
6909 a GNU extension to ELF but could be used on other targets. */
6911 int trampolines_created;
6913 void
6914 file_end_indicate_exec_stack (void)
6916 unsigned int flags = SECTION_DEBUG;
6917 if (trampolines_created)
6918 flags |= SECTION_CODE;
6920 switch_to_section (get_section (".note.GNU-stack", flags, NULL));
6923 /* Emit a special section directive to indicate that this object file
6924 was compiled with -fsplit-stack. This is used to let the linker
6925 detect calls between split-stack code and non-split-stack code, so
6926 that it can modify the split-stack code to allocate a sufficiently
6927 large stack. We emit another special section if there are any
6928 functions in this file which have the no_split_stack attribute, to
6929 prevent the linker from warning about being unable to convert the
6930 functions if they call non-split-stack code. */
6932 void
6933 file_end_indicate_split_stack (void)
6935 if (flag_split_stack)
6937 switch_to_section (get_section (".note.GNU-split-stack", SECTION_DEBUG,
6938 NULL));
6939 if (saw_no_split_stack)
6940 switch_to_section (get_section (".note.GNU-no-split-stack",
6941 SECTION_DEBUG, NULL));
6945 /* Output DIRECTIVE (a C string) followed by a newline. This is used as
6946 a get_unnamed_section callback. */
6948 void
6949 output_section_asm_op (const void *directive)
6951 fprintf (asm_out_file, "%s\n", (const char *) directive);
6954 /* Emit assembly code to switch to section NEW_SECTION. Do nothing if
6955 the current section is NEW_SECTION. */
6957 void
6958 switch_to_section (section *new_section)
6960 if (in_section == new_section)
6961 return;
6963 if (new_section->common.flags & SECTION_FORGET)
6964 in_section = NULL;
6965 else
6966 in_section = new_section;
6968 switch (SECTION_STYLE (new_section))
6970 case SECTION_NAMED:
6971 targetm.asm_out.named_section (new_section->named.name,
6972 new_section->named.common.flags,
6973 new_section->named.decl);
6974 break;
6976 case SECTION_UNNAMED:
6977 new_section->unnamed.callback (new_section->unnamed.data);
6978 break;
6980 case SECTION_NOSWITCH:
6981 gcc_unreachable ();
6982 break;
6985 new_section->common.flags |= SECTION_DECLARED;
6988 /* If block symbol SYMBOL has not yet been assigned an offset, place
6989 it at the end of its block. */
6991 void
6992 place_block_symbol (rtx symbol)
6994 unsigned HOST_WIDE_INT size, mask, offset;
6995 struct constant_descriptor_rtx *desc;
6996 unsigned int alignment;
6997 struct object_block *block;
6998 tree decl;
7000 gcc_assert (SYMBOL_REF_BLOCK (symbol));
7001 if (SYMBOL_REF_BLOCK_OFFSET (symbol) >= 0)
7002 return;
7004 /* Work out the symbol's size and alignment. */
7005 if (CONSTANT_POOL_ADDRESS_P (symbol))
7007 desc = SYMBOL_REF_CONSTANT (symbol);
7008 alignment = desc->align;
7009 size = GET_MODE_SIZE (desc->mode);
7011 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
7013 decl = SYMBOL_REF_DECL (symbol);
7014 alignment = DECL_ALIGN (decl);
7015 size = get_constant_size (DECL_INITIAL (decl));
7017 else
7019 decl = SYMBOL_REF_DECL (symbol);
7020 alignment = DECL_ALIGN (decl);
7021 size = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
7024 /* Calculate the object's offset from the start of the block. */
7025 block = SYMBOL_REF_BLOCK (symbol);
7026 mask = alignment / BITS_PER_UNIT - 1;
7027 offset = (block->size + mask) & ~mask;
7028 SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
7030 /* Record the block's new alignment and size. */
7031 block->alignment = MAX (block->alignment, alignment);
7032 block->size = offset + size;
7034 VEC_safe_push (rtx, gc, block->objects, symbol);
7037 /* Return the anchor that should be used to address byte offset OFFSET
7038 from the first object in BLOCK. MODEL is the TLS model used
7039 to access it. */
7042 get_section_anchor (struct object_block *block, HOST_WIDE_INT offset,
7043 enum tls_model model)
7045 char label[100];
7046 unsigned int begin, middle, end;
7047 unsigned HOST_WIDE_INT min_offset, max_offset, range, bias, delta;
7048 rtx anchor;
7050 /* Work out the anchor's offset. Use an offset of 0 for the first
7051 anchor so that we don't pessimize the case where we take the address
7052 of a variable at the beginning of the block. This is particularly
7053 useful when a block has only one variable assigned to it.
7055 We try to place anchors RANGE bytes apart, so there can then be
7056 anchors at +/-RANGE, +/-2 * RANGE, and so on, up to the limits of
7057 a ptr_mode offset. With some target settings, the lowest such
7058 anchor might be out of range for the lowest ptr_mode offset;
7059 likewise the highest anchor for the highest offset. Use anchors
7060 at the extreme ends of the ptr_mode range in such cases.
7062 All arithmetic uses unsigned integers in order to avoid
7063 signed overflow. */
7064 max_offset = (unsigned HOST_WIDE_INT) targetm.max_anchor_offset;
7065 min_offset = (unsigned HOST_WIDE_INT) targetm.min_anchor_offset;
7066 range = max_offset - min_offset + 1;
7067 if (range == 0)
7068 offset = 0;
7069 else
7071 bias = 1 << (GET_MODE_BITSIZE (ptr_mode) - 1);
7072 if (offset < 0)
7074 delta = -(unsigned HOST_WIDE_INT) offset + max_offset;
7075 delta -= delta % range;
7076 if (delta > bias)
7077 delta = bias;
7078 offset = (HOST_WIDE_INT) (-delta);
7080 else
7082 delta = (unsigned HOST_WIDE_INT) offset - min_offset;
7083 delta -= delta % range;
7084 if (delta > bias - 1)
7085 delta = bias - 1;
7086 offset = (HOST_WIDE_INT) delta;
7090 /* Do a binary search to see if there's already an anchor we can use.
7091 Set BEGIN to the new anchor's index if not. */
7092 begin = 0;
7093 end = VEC_length (rtx, block->anchors);
7094 while (begin != end)
7096 middle = (end + begin) / 2;
7097 anchor = VEC_index (rtx, block->anchors, middle);
7098 if (SYMBOL_REF_BLOCK_OFFSET (anchor) > offset)
7099 end = middle;
7100 else if (SYMBOL_REF_BLOCK_OFFSET (anchor) < offset)
7101 begin = middle + 1;
7102 else if (SYMBOL_REF_TLS_MODEL (anchor) > model)
7103 end = middle;
7104 else if (SYMBOL_REF_TLS_MODEL (anchor) < model)
7105 begin = middle + 1;
7106 else
7107 return anchor;
7110 /* Create a new anchor with a unique label. */
7111 ASM_GENERATE_INTERNAL_LABEL (label, "LANCHOR", anchor_labelno++);
7112 anchor = create_block_symbol (ggc_strdup (label), block, offset);
7113 SYMBOL_REF_FLAGS (anchor) |= SYMBOL_FLAG_LOCAL | SYMBOL_FLAG_ANCHOR;
7114 SYMBOL_REF_FLAGS (anchor) |= model << SYMBOL_FLAG_TLS_SHIFT;
7116 /* Insert it at index BEGIN. */
7117 VEC_safe_insert (rtx, gc, block->anchors, begin, anchor);
7118 return anchor;
7121 /* Output the objects in BLOCK. */
7123 static void
7124 output_object_block (struct object_block *block)
7126 struct constant_descriptor_rtx *desc;
7127 unsigned int i;
7128 HOST_WIDE_INT offset;
7129 tree decl;
7130 rtx symbol;
7132 if (block->objects == NULL)
7133 return;
7135 /* Switch to the section and make sure that the first byte is
7136 suitably aligned. */
7137 switch_to_section (block->sect);
7138 assemble_align (block->alignment);
7140 /* Define the values of all anchors relative to the current section
7141 position. */
7142 FOR_EACH_VEC_ELT (rtx, block->anchors, i, symbol)
7143 targetm.asm_out.output_anchor (symbol);
7145 /* Output the objects themselves. */
7146 offset = 0;
7147 FOR_EACH_VEC_ELT (rtx, block->objects, i, symbol)
7149 /* Move to the object's offset, padding with zeros if necessary. */
7150 assemble_zeros (SYMBOL_REF_BLOCK_OFFSET (symbol) - offset);
7151 offset = SYMBOL_REF_BLOCK_OFFSET (symbol);
7152 if (CONSTANT_POOL_ADDRESS_P (symbol))
7154 desc = SYMBOL_REF_CONSTANT (symbol);
7155 output_constant_pool_1 (desc, 1);
7156 offset += GET_MODE_SIZE (desc->mode);
7158 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
7160 decl = SYMBOL_REF_DECL (symbol);
7161 assemble_constant_contents (DECL_INITIAL (decl), XSTR (symbol, 0),
7162 DECL_ALIGN (decl));
7163 offset += get_constant_size (DECL_INITIAL (decl));
7165 else
7167 decl = SYMBOL_REF_DECL (symbol);
7168 assemble_variable_contents (decl, XSTR (symbol, 0), false);
7169 offset += tree_low_cst (DECL_SIZE_UNIT (decl), 1);
7174 /* A htab_traverse callback used to call output_object_block for
7175 each member of object_block_htab. */
7177 static int
7178 output_object_block_htab (void **slot, void *data ATTRIBUTE_UNUSED)
7180 output_object_block ((struct object_block *) (*slot));
7181 return 1;
7184 /* Output the definitions of all object_blocks. */
7186 void
7187 output_object_blocks (void)
7189 htab_traverse (object_block_htab, output_object_block_htab, NULL);
7192 /* This function provides a possible implementation of the
7193 TARGET_ASM_RECORD_GCC_SWITCHES target hook for ELF targets. When triggered
7194 by -frecord-gcc-switches it creates a new mergeable, string section in the
7195 assembler output file called TARGET_ASM_RECORD_GCC_SWITCHES_SECTION which
7196 contains the switches in ASCII format.
7198 FIXME: This code does not correctly handle double quote characters
7199 that appear inside strings, (it strips them rather than preserving them).
7200 FIXME: ASM_OUTPUT_ASCII, as defined in config/elfos.h will not emit NUL
7201 characters - instead it treats them as sub-string separators. Since
7202 we want to emit NUL strings terminators into the object file we have to use
7203 ASM_OUTPUT_SKIP. */
7206 elf_record_gcc_switches (print_switch_type type, const char * name)
7208 switch (type)
7210 case SWITCH_TYPE_PASSED:
7211 ASM_OUTPUT_ASCII (asm_out_file, name, strlen (name));
7212 ASM_OUTPUT_SKIP (asm_out_file, (unsigned HOST_WIDE_INT) 1);
7213 break;
7215 case SWITCH_TYPE_DESCRIPTIVE:
7216 if (name == NULL)
7218 /* Distinguish between invocations where name is NULL. */
7219 static bool started = false;
7221 if (!started)
7223 section * sec;
7225 sec = get_section (targetm.asm_out.record_gcc_switches_section,
7226 SECTION_DEBUG
7227 | SECTION_MERGE
7228 | SECTION_STRINGS
7229 | (SECTION_ENTSIZE & 1),
7230 NULL);
7231 switch_to_section (sec);
7232 started = true;
7236 default:
7237 break;
7240 /* The return value is currently ignored by the caller, but must be 0.
7241 For -fverbose-asm the return value would be the number of characters
7242 emitted into the assembler file. */
7243 return 0;
7246 /* Emit text to declare externally defined symbols. It is needed to
7247 properly support non-default visibility. */
7248 void
7249 default_elf_asm_output_external (FILE *file ATTRIBUTE_UNUSED,
7250 tree decl,
7251 const char *name ATTRIBUTE_UNUSED)
7253 /* We output the name if and only if TREE_SYMBOL_REFERENCED is
7254 set in order to avoid putting out names that are never really
7255 used. */
7256 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
7257 && targetm.binds_local_p (decl))
7258 maybe_assemble_visibility (decl);
7261 /* The default hook for TARGET_ASM_OUTPUT_SOURCE_FILENAME. */
7263 void
7264 default_asm_output_source_filename (FILE *file, const char *name)
7266 #ifdef ASM_OUTPUT_SOURCE_FILENAME
7267 ASM_OUTPUT_SOURCE_FILENAME (file, name);
7268 #else
7269 fprintf (file, "\t.file\t");
7270 output_quoted_string (file, name);
7271 putc ('\n', file);
7272 #endif
7275 /* Output a file name in the form wanted by System V. */
7277 void
7278 output_file_directive (FILE *asm_file, const char *input_name)
7280 int len;
7281 const char *na;
7283 if (input_name == NULL)
7284 input_name = "<stdin>";
7285 else
7286 input_name = remap_debug_filename (input_name);
7288 len = strlen (input_name);
7289 na = input_name + len;
7291 /* NA gets INPUT_NAME sans directory names. */
7292 while (na > input_name)
7294 if (IS_DIR_SEPARATOR (na[-1]))
7295 break;
7296 na--;
7299 targetm.asm_out.output_source_filename (asm_file, na);
7302 /* Create a DEBUG_EXPR_DECL / DEBUG_EXPR pair from RTL expression
7303 EXP. */
7305 make_debug_expr_from_rtl (const_rtx exp)
7307 tree ddecl = make_node (DEBUG_EXPR_DECL), type;
7308 enum machine_mode mode = GET_MODE (exp);
7309 rtx dval;
7311 DECL_ARTIFICIAL (ddecl) = 1;
7312 if (REG_P (exp) && REG_EXPR (exp))
7313 type = TREE_TYPE (REG_EXPR (exp));
7314 else if (MEM_P (exp) && MEM_EXPR (exp))
7315 type = TREE_TYPE (MEM_EXPR (exp));
7316 else
7317 type = NULL_TREE;
7318 if (type && TYPE_MODE (type) == mode)
7319 TREE_TYPE (ddecl) = type;
7320 else
7321 TREE_TYPE (ddecl) = lang_hooks.types.type_for_mode (mode, 1);
7322 DECL_MODE (ddecl) = mode;
7323 dval = gen_rtx_DEBUG_EXPR (mode);
7324 DEBUG_EXPR_TREE_DECL (dval) = ddecl;
7325 SET_DECL_RTL (ddecl, dval);
7326 return dval;
7329 #include "gt-varasm.h"