2011-05-31 Gabriel Charette <gchare@google.com>
[official-gcc.git] / gcc / varasm.c
blob2d2284b88813310f729d6bd129ef0027825e49d4
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_get_node (decl);
578 if (node)
580 freq = node->frequency;
581 startup = node->only_called_at_startup;
582 exit = node->only_called_at_exit;
585 if (force_cold)
586 freq = NODE_FREQUENCY_UNLIKELY_EXECUTED;
588 #ifdef USE_SELECT_SECTION_FOR_FUNCTIONS
589 if (decl != NULL_TREE
590 && DECL_SECTION_NAME (decl) != NULL_TREE)
592 if (targetm.asm_out.function_section)
593 section = targetm.asm_out.function_section (decl, freq,
594 startup, exit);
595 if (section)
596 return section;
597 return get_named_section (decl, NULL, 0);
599 else
600 return targetm.asm_out.select_section
601 (decl, freq == NODE_FREQUENCY_UNLIKELY_EXECUTED,
602 DECL_ALIGN (decl));
603 #else
604 if (targetm.asm_out.function_section)
605 section = targetm.asm_out.function_section (decl, freq, startup, exit);
606 if (section)
607 return section;
608 return hot_function_section (decl);
609 #endif
612 /* Return the section for function DECL.
614 If DECL is NULL_TREE, return the text section. We can be passed
615 NULL_TREE under some circumstances by dbxout.c at least. */
617 section *
618 function_section (tree decl)
620 /* Handle cases where function splitting code decides
621 to put function entry point into unlikely executed section
622 despite the fact that the function itself is not cold
623 (i.e. it is called rarely but contains a hot loop that is
624 better to live in hot subsection for the code locality). */
625 return function_section_1 (decl,
626 first_function_block_is_cold);
629 /* Return the section for the current function, take IN_COLD_SECTION_P
630 into account. */
632 section *
633 current_function_section (void)
635 return function_section_1 (current_function_decl, in_cold_section_p);
638 /* Tell assembler to switch to unlikely-to-be-executed text section. */
640 section *
641 unlikely_text_section (void)
643 return function_section_1 (current_function_decl, true);
646 /* When called within a function context, return true if the function
647 has been assigned a cold text section and if SECT is that section.
648 When called outside a function context, return true if SECT is the
649 default cold section. */
651 bool
652 unlikely_text_section_p (section *sect)
654 return sect == function_section_1 (current_function_decl, true);
657 /* Return the read-only data section associated with function DECL. */
659 section *
660 default_function_rodata_section (tree decl)
662 if (decl != NULL_TREE && DECL_SECTION_NAME (decl))
664 const char *name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
666 if (DECL_ONE_ONLY (decl) && HAVE_COMDAT_GROUP)
668 const char *dot;
669 size_t len;
670 char* rname;
672 dot = strchr (name + 1, '.');
673 if (!dot)
674 dot = name;
675 len = strlen (dot) + 8;
676 rname = (char *) alloca (len);
678 strcpy (rname, ".rodata");
679 strcat (rname, dot);
680 return get_section (rname, SECTION_LINKONCE, decl);
682 /* For .gnu.linkonce.t.foo we want to use .gnu.linkonce.r.foo. */
683 else if (DECL_ONE_ONLY (decl)
684 && strncmp (name, ".gnu.linkonce.t.", 16) == 0)
686 size_t len = strlen (name) + 1;
687 char *rname = (char *) alloca (len);
689 memcpy (rname, name, len);
690 rname[14] = 'r';
691 return get_section (rname, SECTION_LINKONCE, decl);
693 /* For .text.foo we want to use .rodata.foo. */
694 else if (flag_function_sections && flag_data_sections
695 && strncmp (name, ".text.", 6) == 0)
697 size_t len = strlen (name) + 1;
698 char *rname = (char *) alloca (len + 2);
700 memcpy (rname, ".rodata", 7);
701 memcpy (rname + 7, name + 5, len - 5);
702 return get_section (rname, 0, decl);
706 return readonly_data_section;
709 /* Return the read-only data section associated with function DECL
710 for targets where that section should be always the single
711 readonly data section. */
713 section *
714 default_no_function_rodata_section (tree decl ATTRIBUTE_UNUSED)
716 return readonly_data_section;
719 /* Return the section to use for string merging. */
721 static section *
722 mergeable_string_section (tree decl ATTRIBUTE_UNUSED,
723 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
724 unsigned int flags ATTRIBUTE_UNUSED)
726 HOST_WIDE_INT len;
728 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
729 && TREE_CODE (decl) == STRING_CST
730 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
731 && align <= 256
732 && (len = int_size_in_bytes (TREE_TYPE (decl))) > 0
733 && TREE_STRING_LENGTH (decl) >= len)
735 enum machine_mode mode;
736 unsigned int modesize;
737 const char *str;
738 HOST_WIDE_INT i;
739 int j, unit;
740 char name[30];
742 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (decl)));
743 modesize = GET_MODE_BITSIZE (mode);
744 if (modesize >= 8 && modesize <= 256
745 && (modesize & (modesize - 1)) == 0)
747 if (align < modesize)
748 align = modesize;
750 str = TREE_STRING_POINTER (decl);
751 unit = GET_MODE_SIZE (mode);
753 /* Check for embedded NUL characters. */
754 for (i = 0; i < len; i += unit)
756 for (j = 0; j < unit; j++)
757 if (str[i + j] != '\0')
758 break;
759 if (j == unit)
760 break;
762 if (i == len - unit)
764 sprintf (name, ".rodata.str%d.%d", modesize / 8,
765 (int) (align / 8));
766 flags |= (modesize / 8) | SECTION_MERGE | SECTION_STRINGS;
767 return get_section (name, flags, NULL);
772 return readonly_data_section;
775 /* Return the section to use for constant merging. */
777 section *
778 mergeable_constant_section (enum machine_mode mode ATTRIBUTE_UNUSED,
779 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
780 unsigned int flags ATTRIBUTE_UNUSED)
782 unsigned int modesize = GET_MODE_BITSIZE (mode);
784 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
785 && mode != VOIDmode
786 && mode != BLKmode
787 && modesize <= align
788 && align >= 8
789 && align <= 256
790 && (align & (align - 1)) == 0)
792 char name[24];
794 sprintf (name, ".rodata.cst%d", (int) (align / 8));
795 flags |= (align / 8) | SECTION_MERGE;
796 return get_section (name, flags, NULL);
798 return readonly_data_section;
801 /* Given NAME, a putative register name, discard any customary prefixes. */
803 static const char *
804 strip_reg_name (const char *name)
806 #ifdef REGISTER_PREFIX
807 if (!strncmp (name, REGISTER_PREFIX, strlen (REGISTER_PREFIX)))
808 name += strlen (REGISTER_PREFIX);
809 #endif
810 if (name[0] == '%' || name[0] == '#')
811 name++;
812 return name;
815 /* The user has asked for a DECL to have a particular name. Set (or
816 change) it in such a way that we don't prefix an underscore to
817 it. */
818 void
819 set_user_assembler_name (tree decl, const char *name)
821 char *starred = (char *) alloca (strlen (name) + 2);
822 starred[0] = '*';
823 strcpy (starred + 1, name);
824 change_decl_assembler_name (decl, get_identifier (starred));
825 SET_DECL_RTL (decl, NULL_RTX);
828 /* Decode an `asm' spec for a declaration as a register name.
829 Return the register number, or -1 if nothing specified,
830 or -2 if the ASMSPEC is not `cc' or `memory' and is not recognized,
831 or -3 if ASMSPEC is `cc' and is not recognized,
832 or -4 if ASMSPEC is `memory' and is not recognized.
833 Accept an exact spelling or a decimal number.
834 Prefixes such as % are optional. */
837 decode_reg_name_and_count (const char *asmspec, int *pnregs)
839 /* Presume just one register is clobbered. */
840 *pnregs = 1;
842 if (asmspec != 0)
844 int i;
846 /* Get rid of confusing prefixes. */
847 asmspec = strip_reg_name (asmspec);
849 /* Allow a decimal number as a "register name". */
850 for (i = strlen (asmspec) - 1; i >= 0; i--)
851 if (! ISDIGIT (asmspec[i]))
852 break;
853 if (asmspec[0] != 0 && i < 0)
855 i = atoi (asmspec);
856 if (i < FIRST_PSEUDO_REGISTER && i >= 0)
857 return i;
858 else
859 return -2;
862 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
863 if (reg_names[i][0]
864 && ! strcmp (asmspec, strip_reg_name (reg_names[i])))
865 return i;
867 #ifdef OVERLAPPING_REGISTER_NAMES
869 static const struct
871 const char *const name;
872 const int number;
873 const int nregs;
874 } table[] = OVERLAPPING_REGISTER_NAMES;
876 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
877 if (table[i].name[0]
878 && ! strcmp (asmspec, table[i].name))
880 *pnregs = table[i].nregs;
881 return table[i].number;
884 #endif /* OVERLAPPING_REGISTER_NAMES */
886 #ifdef ADDITIONAL_REGISTER_NAMES
888 static const struct { const char *const name; const int number; } table[]
889 = ADDITIONAL_REGISTER_NAMES;
891 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
892 if (table[i].name[0]
893 && ! strcmp (asmspec, table[i].name))
894 return table[i].number;
896 #endif /* ADDITIONAL_REGISTER_NAMES */
898 if (!strcmp (asmspec, "memory"))
899 return -4;
901 if (!strcmp (asmspec, "cc"))
902 return -3;
904 return -2;
907 return -1;
911 decode_reg_name (const char *name)
913 int count;
914 return decode_reg_name_and_count (name, &count);
918 /* Return true if DECL's initializer is suitable for a BSS section. */
920 static bool
921 bss_initializer_p (const_tree decl)
923 return (DECL_INITIAL (decl) == NULL
924 || DECL_INITIAL (decl) == error_mark_node
925 || (flag_zero_initialized_in_bss
926 /* Leave constant zeroes in .rodata so they
927 can be shared. */
928 && !TREE_READONLY (decl)
929 && initializer_zerop (DECL_INITIAL (decl))));
932 /* Compute the alignment of variable specified by DECL.
933 DONT_OUTPUT_DATA is from assemble_variable. */
935 void
936 align_variable (tree decl, bool dont_output_data)
938 unsigned int align = DECL_ALIGN (decl);
940 /* In the case for initialing an array whose length isn't specified,
941 where we have not yet been able to do the layout,
942 figure out the proper alignment now. */
943 if (dont_output_data && DECL_SIZE (decl) == 0
944 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
945 align = MAX (align, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (decl))));
947 /* Some object file formats have a maximum alignment which they support.
948 In particular, a.out format supports a maximum alignment of 4. */
949 if (align > MAX_OFILE_ALIGNMENT)
951 warning (0, "alignment of %q+D is greater than maximum object "
952 "file alignment. Using %d", decl,
953 MAX_OFILE_ALIGNMENT/BITS_PER_UNIT);
954 align = MAX_OFILE_ALIGNMENT;
957 /* On some machines, it is good to increase alignment sometimes. */
958 if (! DECL_USER_ALIGN (decl))
960 #ifdef DATA_ALIGNMENT
961 unsigned int data_align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
962 /* Don't increase alignment too much for TLS variables - TLS space
963 is too precious. */
964 if (! DECL_THREAD_LOCAL_P (decl) || data_align <= BITS_PER_WORD)
965 align = data_align;
966 #endif
967 #ifdef CONSTANT_ALIGNMENT
968 if (DECL_INITIAL (decl) != 0 && DECL_INITIAL (decl) != error_mark_node)
970 unsigned int const_align = CONSTANT_ALIGNMENT (DECL_INITIAL (decl),
971 align);
972 /* Don't increase alignment too much for TLS variables - TLS space
973 is too precious. */
974 if (! DECL_THREAD_LOCAL_P (decl) || const_align <= BITS_PER_WORD)
975 align = const_align;
977 #endif
980 /* Reset the alignment in case we have made it tighter, so we can benefit
981 from it in get_pointer_alignment. */
982 DECL_ALIGN (decl) = align;
985 /* Return the section into which the given VAR_DECL or CONST_DECL
986 should be placed. PREFER_NOSWITCH_P is true if a noswitch
987 section should be used wherever possible. */
989 section *
990 get_variable_section (tree decl, bool prefer_noswitch_p)
992 addr_space_t as = ADDR_SPACE_GENERIC;
993 int reloc;
995 if (TREE_TYPE (decl) != error_mark_node)
996 as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
998 if (DECL_COMMON (decl))
1000 /* If the decl has been given an explicit section name, or it resides
1001 in a non-generic address space, then it isn't common, and shouldn't
1002 be handled as such. */
1003 gcc_assert (DECL_SECTION_NAME (decl) == NULL
1004 && ADDR_SPACE_GENERIC_P (as));
1005 if (DECL_THREAD_LOCAL_P (decl))
1006 return tls_comm_section;
1007 else if (TREE_PUBLIC (decl) && bss_initializer_p (decl))
1008 return comm_section;
1011 if (DECL_INITIAL (decl) == error_mark_node)
1012 reloc = contains_pointers_p (TREE_TYPE (decl)) ? 3 : 0;
1013 else if (DECL_INITIAL (decl))
1014 reloc = compute_reloc_for_constant (DECL_INITIAL (decl));
1015 else
1016 reloc = 0;
1018 resolve_unique_section (decl, reloc, flag_data_sections);
1019 if (IN_NAMED_SECTION (decl))
1020 return get_named_section (decl, NULL, reloc);
1022 if (ADDR_SPACE_GENERIC_P (as)
1023 && !DECL_THREAD_LOCAL_P (decl)
1024 && !(prefer_noswitch_p && targetm.have_switchable_bss_sections)
1025 && bss_initializer_p (decl))
1027 if (!TREE_PUBLIC (decl))
1028 return lcomm_section;
1029 if (bss_noswitch_section)
1030 return bss_noswitch_section;
1033 return targetm.asm_out.select_section (decl, reloc, DECL_ALIGN (decl));
1036 /* Return the block into which object_block DECL should be placed. */
1038 static struct object_block *
1039 get_block_for_decl (tree decl)
1041 section *sect;
1043 if (TREE_CODE (decl) == VAR_DECL)
1045 /* The object must be defined in this translation unit. */
1046 if (DECL_EXTERNAL (decl))
1047 return NULL;
1049 /* There's no point using object blocks for something that is
1050 isolated by definition. */
1051 if (DECL_ONE_ONLY (decl))
1052 return NULL;
1055 /* We can only calculate block offsets if the decl has a known
1056 constant size. */
1057 if (DECL_SIZE_UNIT (decl) == NULL)
1058 return NULL;
1059 if (!host_integerp (DECL_SIZE_UNIT (decl), 1))
1060 return NULL;
1062 /* Find out which section should contain DECL. We cannot put it into
1063 an object block if it requires a standalone definition. */
1064 if (TREE_CODE (decl) == VAR_DECL)
1065 align_variable (decl, 0);
1066 sect = get_variable_section (decl, true);
1067 if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
1068 return NULL;
1070 return get_block_for_section (sect);
1073 /* Make sure block symbol SYMBOL is in block BLOCK. */
1075 static void
1076 change_symbol_block (rtx symbol, struct object_block *block)
1078 if (block != SYMBOL_REF_BLOCK (symbol))
1080 gcc_assert (SYMBOL_REF_BLOCK_OFFSET (symbol) < 0);
1081 SYMBOL_REF_BLOCK (symbol) = block;
1085 /* Return true if it is possible to put DECL in an object_block. */
1087 static bool
1088 use_blocks_for_decl_p (tree decl)
1090 /* Only data DECLs can be placed into object blocks. */
1091 if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != CONST_DECL)
1092 return false;
1094 /* Detect decls created by dw2_force_const_mem. Such decls are
1095 special because DECL_INITIAL doesn't specify the decl's true value.
1096 dw2_output_indirect_constants will instead call assemble_variable
1097 with dont_output_data set to 1 and then print the contents itself. */
1098 if (DECL_INITIAL (decl) == decl)
1099 return false;
1101 /* If this decl is an alias, then we don't want to emit a
1102 definition. */
1103 if (lookup_attribute ("alias", DECL_ATTRIBUTES (decl)))
1104 return false;
1106 return true;
1109 /* Create the DECL_RTL for a VAR_DECL or FUNCTION_DECL. DECL should
1110 have static storage duration. In other words, it should not be an
1111 automatic variable, including PARM_DECLs.
1113 There is, however, one exception: this function handles variables
1114 explicitly placed in a particular register by the user.
1116 This is never called for PARM_DECL nodes. */
1118 void
1119 make_decl_rtl (tree decl)
1121 const char *name = 0;
1122 int reg_number;
1123 rtx x;
1125 /* Check that we are not being given an automatic variable. */
1126 gcc_assert (TREE_CODE (decl) != PARM_DECL
1127 && TREE_CODE (decl) != RESULT_DECL);
1129 /* A weak alias has TREE_PUBLIC set but not the other bits. */
1130 gcc_assert (TREE_CODE (decl) != VAR_DECL
1131 || TREE_STATIC (decl)
1132 || TREE_PUBLIC (decl)
1133 || DECL_EXTERNAL (decl)
1134 || DECL_REGISTER (decl));
1136 /* And that we were not given a type or a label. */
1137 gcc_assert (TREE_CODE (decl) != TYPE_DECL
1138 && TREE_CODE (decl) != LABEL_DECL);
1140 /* For a duplicate declaration, we can be called twice on the
1141 same DECL node. Don't discard the RTL already made. */
1142 if (DECL_RTL_SET_P (decl))
1144 /* If the old RTL had the wrong mode, fix the mode. */
1145 x = DECL_RTL (decl);
1146 if (GET_MODE (x) != DECL_MODE (decl))
1147 SET_DECL_RTL (decl, adjust_address_nv (x, DECL_MODE (decl), 0));
1149 if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1150 return;
1152 /* ??? Another way to do this would be to maintain a hashed
1153 table of such critters. Instead of adding stuff to a DECL
1154 to give certain attributes to it, we could use an external
1155 hash map from DECL to set of attributes. */
1157 /* Let the target reassign the RTL if it wants.
1158 This is necessary, for example, when one machine specific
1159 decl attribute overrides another. */
1160 targetm.encode_section_info (decl, DECL_RTL (decl), false);
1162 /* If the symbol has a SYMBOL_REF_BLOCK field, update it based
1163 on the new decl information. */
1164 if (MEM_P (x)
1165 && GET_CODE (XEXP (x, 0)) == SYMBOL_REF
1166 && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (x, 0)))
1167 change_symbol_block (XEXP (x, 0), get_block_for_decl (decl));
1169 /* Make this function static known to the mudflap runtime. */
1170 if (flag_mudflap && TREE_CODE (decl) == VAR_DECL)
1171 mudflap_enqueue_decl (decl);
1173 return;
1176 /* If this variable belongs to the global constant pool, retrieve the
1177 pre-computed RTL or recompute it in LTO mode. */
1178 if (TREE_CODE (decl) == VAR_DECL && DECL_IN_CONSTANT_POOL (decl))
1180 SET_DECL_RTL (decl, output_constant_def (DECL_INITIAL (decl), 1));
1181 return;
1184 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
1186 if (name[0] != '*' && TREE_CODE (decl) != FUNCTION_DECL
1187 && DECL_REGISTER (decl))
1189 error ("register name not specified for %q+D", decl);
1191 else if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1193 const char *asmspec = name+1;
1194 reg_number = decode_reg_name (asmspec);
1195 /* First detect errors in declaring global registers. */
1196 if (reg_number == -1)
1197 error ("register name not specified for %q+D", decl);
1198 else if (reg_number < 0)
1199 error ("invalid register name for %q+D", decl);
1200 else if (TYPE_MODE (TREE_TYPE (decl)) == BLKmode)
1201 error ("data type of %q+D isn%'t suitable for a register",
1202 decl);
1203 else if (! HARD_REGNO_MODE_OK (reg_number, TYPE_MODE (TREE_TYPE (decl))))
1204 error ("register specified for %q+D isn%'t suitable for data type",
1205 decl);
1206 /* Now handle properly declared static register variables. */
1207 else
1209 int nregs;
1211 if (DECL_INITIAL (decl) != 0 && TREE_STATIC (decl))
1213 DECL_INITIAL (decl) = 0;
1214 error ("global register variable has initial value");
1216 if (TREE_THIS_VOLATILE (decl))
1217 warning (OPT_Wvolatile_register_var,
1218 "optimization may eliminate reads and/or "
1219 "writes to register variables");
1221 /* If the user specified one of the eliminables registers here,
1222 e.g., FRAME_POINTER_REGNUM, we don't want to get this variable
1223 confused with that register and be eliminated. This usage is
1224 somewhat suspect... */
1226 SET_DECL_RTL (decl, gen_rtx_raw_REG (DECL_MODE (decl), reg_number));
1227 ORIGINAL_REGNO (DECL_RTL (decl)) = reg_number;
1228 REG_USERVAR_P (DECL_RTL (decl)) = 1;
1230 if (TREE_STATIC (decl))
1232 /* Make this register global, so not usable for anything
1233 else. */
1234 #ifdef ASM_DECLARE_REGISTER_GLOBAL
1235 name = IDENTIFIER_POINTER (DECL_NAME (decl));
1236 ASM_DECLARE_REGISTER_GLOBAL (asm_out_file, decl, reg_number, name);
1237 #endif
1238 nregs = hard_regno_nregs[reg_number][DECL_MODE (decl)];
1239 while (nregs > 0)
1240 globalize_reg (reg_number + --nregs);
1243 /* As a register variable, it has no section. */
1244 return;
1247 /* Now handle ordinary static variables and functions (in memory).
1248 Also handle vars declared register invalidly. */
1249 else if (name[0] == '*')
1251 #ifdef REGISTER_PREFIX
1252 if (strlen (REGISTER_PREFIX) != 0)
1254 reg_number = decode_reg_name (name);
1255 if (reg_number >= 0 || reg_number == -3)
1256 error ("register name given for non-register variable %q+D", decl);
1258 #endif
1261 /* Specifying a section attribute on a variable forces it into a
1262 non-.bss section, and thus it cannot be common. */
1263 /* FIXME: In general this code should not be necessary because
1264 visibility pass is doing the same work. But notice_global_symbol
1265 is called early and it needs to make DECL_RTL to get the name.
1266 we take care of recomputing the DECL_RTL after visibility is changed. */
1267 if (TREE_CODE (decl) == VAR_DECL
1268 && DECL_SECTION_NAME (decl) != NULL_TREE
1269 && DECL_INITIAL (decl) == NULL_TREE
1270 && DECL_COMMON (decl))
1271 DECL_COMMON (decl) = 0;
1273 /* Variables can't be both common and weak. */
1274 if (TREE_CODE (decl) == VAR_DECL && DECL_WEAK (decl))
1275 DECL_COMMON (decl) = 0;
1277 if (use_object_blocks_p () && use_blocks_for_decl_p (decl))
1278 x = create_block_symbol (name, get_block_for_decl (decl), -1);
1279 else
1281 enum machine_mode address_mode = Pmode;
1282 if (TREE_TYPE (decl) != error_mark_node)
1284 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
1285 address_mode = targetm.addr_space.address_mode (as);
1287 x = gen_rtx_SYMBOL_REF (address_mode, name);
1289 SYMBOL_REF_WEAK (x) = DECL_WEAK (decl);
1290 SET_SYMBOL_REF_DECL (x, decl);
1292 x = gen_rtx_MEM (DECL_MODE (decl), x);
1293 if (TREE_CODE (decl) != FUNCTION_DECL)
1294 set_mem_attributes (x, decl, 1);
1295 SET_DECL_RTL (decl, x);
1297 /* Optionally set flags or add text to the name to record information
1298 such as that it is a function name.
1299 If the name is changed, the macro ASM_OUTPUT_LABELREF
1300 will have to know how to strip this information. */
1301 targetm.encode_section_info (decl, DECL_RTL (decl), true);
1303 /* Make this function static known to the mudflap runtime. */
1304 if (flag_mudflap && TREE_CODE (decl) == VAR_DECL)
1305 mudflap_enqueue_decl (decl);
1308 /* Like make_decl_rtl, but inhibit creation of new alias sets when
1309 calling make_decl_rtl. Also, reset DECL_RTL before returning the
1310 rtl. */
1313 make_decl_rtl_for_debug (tree decl)
1315 unsigned int save_aliasing_flag, save_mudflap_flag;
1316 rtx rtl;
1318 if (DECL_RTL_SET_P (decl))
1319 return DECL_RTL (decl);
1321 /* Kludge alert! Somewhere down the call chain, make_decl_rtl will
1322 call new_alias_set. If running with -fcompare-debug, sometimes
1323 we do not want to create alias sets that will throw the alias
1324 numbers off in the comparison dumps. So... clearing
1325 flag_strict_aliasing will keep new_alias_set() from creating a
1326 new set. It is undesirable to register decl with mudflap
1327 in this case as well. */
1328 save_aliasing_flag = flag_strict_aliasing;
1329 flag_strict_aliasing = 0;
1330 save_mudflap_flag = flag_mudflap;
1331 flag_mudflap = 0;
1333 rtl = DECL_RTL (decl);
1334 /* Reset DECL_RTL back, as various parts of the compiler expects
1335 DECL_RTL set meaning it is actually going to be output. */
1336 SET_DECL_RTL (decl, NULL);
1338 flag_strict_aliasing = save_aliasing_flag;
1339 flag_mudflap = save_mudflap_flag;
1341 return rtl;
1344 /* Output a string of literal assembler code
1345 for an `asm' keyword used between functions. */
1347 void
1348 assemble_asm (tree string)
1350 app_enable ();
1352 if (TREE_CODE (string) == ADDR_EXPR)
1353 string = TREE_OPERAND (string, 0);
1355 fprintf (asm_out_file, "\t%s\n", TREE_STRING_POINTER (string));
1358 /* Record an element in the table of global destructors. SYMBOL is
1359 a SYMBOL_REF of the function to be called; PRIORITY is a number
1360 between 0 and MAX_INIT_PRIORITY. */
1362 void
1363 default_stabs_asm_out_destructor (rtx symbol ATTRIBUTE_UNUSED,
1364 int priority ATTRIBUTE_UNUSED)
1366 #if defined DBX_DEBUGGING_INFO || defined XCOFF_DEBUGGING_INFO
1367 /* Tell GNU LD that this is part of the static destructor set.
1368 This will work for any system that uses stabs, most usefully
1369 aout systems. */
1370 dbxout_begin_simple_stabs ("___DTOR_LIST__", 22 /* N_SETT */);
1371 dbxout_stab_value_label (XSTR (symbol, 0));
1372 #else
1373 sorry ("global destructors not supported on this target");
1374 #endif
1377 /* Write the address of the entity given by SYMBOL to SEC. */
1378 void
1379 assemble_addr_to_section (rtx symbol, section *sec)
1381 switch_to_section (sec);
1382 assemble_align (POINTER_SIZE);
1383 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1386 /* Return the numbered .ctors.N (if CONSTRUCTOR_P) or .dtors.N (if
1387 not) section for PRIORITY. */
1388 section *
1389 get_cdtor_priority_section (int priority, bool constructor_p)
1391 char buf[16];
1393 /* ??? This only works reliably with the GNU linker. */
1394 sprintf (buf, "%s.%.5u",
1395 constructor_p ? ".ctors" : ".dtors",
1396 /* Invert the numbering so the linker puts us in the proper
1397 order; constructors are run from right to left, and the
1398 linker sorts in increasing order. */
1399 MAX_INIT_PRIORITY - priority);
1400 return get_section (buf, SECTION_WRITE, NULL);
1403 void
1404 default_named_section_asm_out_destructor (rtx symbol, int priority)
1406 section *sec;
1408 if (priority != DEFAULT_INIT_PRIORITY)
1409 sec = get_cdtor_priority_section (priority,
1410 /*constructor_p=*/false);
1411 else
1412 sec = get_section (".dtors", SECTION_WRITE, NULL);
1414 assemble_addr_to_section (symbol, sec);
1417 #ifdef DTORS_SECTION_ASM_OP
1418 void
1419 default_dtor_section_asm_out_destructor (rtx symbol,
1420 int priority ATTRIBUTE_UNUSED)
1422 assemble_addr_to_section (symbol, dtors_section);
1424 #endif
1426 /* Likewise for global constructors. */
1428 void
1429 default_stabs_asm_out_constructor (rtx symbol ATTRIBUTE_UNUSED,
1430 int priority ATTRIBUTE_UNUSED)
1432 #if defined DBX_DEBUGGING_INFO || defined XCOFF_DEBUGGING_INFO
1433 /* Tell GNU LD that this is part of the static destructor set.
1434 This will work for any system that uses stabs, most usefully
1435 aout systems. */
1436 dbxout_begin_simple_stabs ("___CTOR_LIST__", 22 /* N_SETT */);
1437 dbxout_stab_value_label (XSTR (symbol, 0));
1438 #else
1439 sorry ("global constructors not supported on this target");
1440 #endif
1443 void
1444 default_named_section_asm_out_constructor (rtx symbol, int priority)
1446 section *sec;
1448 if (priority != DEFAULT_INIT_PRIORITY)
1449 sec = get_cdtor_priority_section (priority,
1450 /*constructor_p=*/true);
1451 else
1452 sec = get_section (".ctors", SECTION_WRITE, NULL);
1454 assemble_addr_to_section (symbol, sec);
1457 #ifdef CTORS_SECTION_ASM_OP
1458 void
1459 default_ctor_section_asm_out_constructor (rtx symbol,
1460 int priority ATTRIBUTE_UNUSED)
1462 assemble_addr_to_section (symbol, ctors_section);
1464 #endif
1466 /* CONSTANT_POOL_BEFORE_FUNCTION may be defined as an expression with
1467 a nonzero value if the constant pool should be output before the
1468 start of the function, or a zero value if the pool should output
1469 after the end of the function. The default is to put it before the
1470 start. */
1472 #ifndef CONSTANT_POOL_BEFORE_FUNCTION
1473 #define CONSTANT_POOL_BEFORE_FUNCTION 1
1474 #endif
1476 /* DECL is an object (either VAR_DECL or FUNCTION_DECL) which is going
1477 to be output to assembler.
1478 Set first_global_object_name and weak_global_object_name as appropriate. */
1480 void
1481 notice_global_symbol (tree decl)
1483 const char **type = &first_global_object_name;
1485 if (first_global_object_name
1486 || !TREE_PUBLIC (decl)
1487 || DECL_EXTERNAL (decl)
1488 || !DECL_NAME (decl)
1489 || (TREE_CODE (decl) != FUNCTION_DECL
1490 && (TREE_CODE (decl) != VAR_DECL
1491 || (DECL_COMMON (decl)
1492 && (DECL_INITIAL (decl) == 0
1493 || DECL_INITIAL (decl) == error_mark_node))))
1494 || !MEM_P (DECL_RTL (decl)))
1495 return;
1497 /* We win when global object is found, but it is useful to know about weak
1498 symbol as well so we can produce nicer unique names. */
1499 if (DECL_WEAK (decl) || DECL_ONE_ONLY (decl) || flag_shlib)
1500 type = &weak_global_object_name;
1502 if (!*type)
1504 const char *p;
1505 const char *name;
1506 rtx decl_rtl = DECL_RTL (decl);
1508 p = targetm.strip_name_encoding (XSTR (XEXP (decl_rtl, 0), 0));
1509 name = ggc_strdup (p);
1511 *type = name;
1515 /* If not using flag_reorder_blocks_and_partition, decide early whether the
1516 current function goes into the cold section, so that targets can use
1517 current_function_section during RTL expansion. DECL describes the
1518 function. */
1520 void
1521 decide_function_section (tree decl)
1523 first_function_block_is_cold = false;
1525 if (flag_reorder_blocks_and_partition)
1526 /* We will decide in assemble_start_function. */
1527 return;
1529 if (DECL_SECTION_NAME (decl))
1531 struct cgraph_node *node = cgraph_get_node (current_function_decl);
1532 /* Calls to function_section rely on first_function_block_is_cold
1533 being accurate. */
1534 first_function_block_is_cold = (node
1535 && node->frequency
1536 == NODE_FREQUENCY_UNLIKELY_EXECUTED);
1539 in_cold_section_p = first_function_block_is_cold;
1542 /* Output assembler code for the constant pool of a function and associated
1543 with defining the name of the function. DECL describes the function.
1544 NAME is the function's name. For the constant pool, we use the current
1545 constant pool data. */
1547 void
1548 assemble_start_function (tree decl, const char *fnname)
1550 int align;
1551 char tmp_label[100];
1552 bool hot_label_written = false;
1554 if (flag_reorder_blocks_and_partition)
1556 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTB", const_labelno);
1557 crtl->subsections.hot_section_label = ggc_strdup (tmp_label);
1558 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDB", const_labelno);
1559 crtl->subsections.cold_section_label = ggc_strdup (tmp_label);
1560 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTE", const_labelno);
1561 crtl->subsections.hot_section_end_label = ggc_strdup (tmp_label);
1562 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDE", const_labelno);
1563 crtl->subsections.cold_section_end_label = ggc_strdup (tmp_label);
1564 const_labelno++;
1566 else
1568 crtl->subsections.hot_section_label = NULL;
1569 crtl->subsections.cold_section_label = NULL;
1570 crtl->subsections.hot_section_end_label = NULL;
1571 crtl->subsections.cold_section_end_label = NULL;
1574 /* The following code does not need preprocessing in the assembler. */
1576 app_disable ();
1578 if (CONSTANT_POOL_BEFORE_FUNCTION)
1579 output_constant_pool (fnname, decl);
1581 /* Make sure the not and cold text (code) sections are properly
1582 aligned. This is necessary here in the case where the function
1583 has both hot and cold sections, because we don't want to re-set
1584 the alignment when the section switch happens mid-function. */
1586 if (flag_reorder_blocks_and_partition)
1588 first_function_block_is_cold = false;
1590 switch_to_section (unlikely_text_section ());
1591 assemble_align (DECL_ALIGN (decl));
1592 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_label);
1594 /* When the function starts with a cold section, we need to explicitly
1595 align the hot section and write out the hot section label.
1596 But if the current function is a thunk, we do not have a CFG. */
1597 if (!cfun->is_thunk
1598 && BB_PARTITION (ENTRY_BLOCK_PTR->next_bb) == BB_COLD_PARTITION)
1600 switch_to_section (text_section);
1601 assemble_align (DECL_ALIGN (decl));
1602 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
1603 hot_label_written = true;
1604 first_function_block_is_cold = true;
1606 in_cold_section_p = first_function_block_is_cold;
1610 /* Switch to the correct text section for the start of the function. */
1612 switch_to_section (function_section (decl));
1613 if (flag_reorder_blocks_and_partition
1614 && !hot_label_written)
1615 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
1617 /* Tell assembler to move to target machine's alignment for functions. */
1618 align = floor_log2 (DECL_ALIGN (decl) / BITS_PER_UNIT);
1619 if (align > 0)
1621 ASM_OUTPUT_ALIGN (asm_out_file, align);
1624 /* Handle a user-specified function alignment.
1625 Note that we still need to align to DECL_ALIGN, as above,
1626 because ASM_OUTPUT_MAX_SKIP_ALIGN might not do any alignment at all. */
1627 if (! DECL_USER_ALIGN (decl)
1628 && align_functions_log > align
1629 && optimize_function_for_speed_p (cfun))
1631 #ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
1632 ASM_OUTPUT_MAX_SKIP_ALIGN (asm_out_file,
1633 align_functions_log, align_functions - 1);
1634 #else
1635 ASM_OUTPUT_ALIGN (asm_out_file, align_functions_log);
1636 #endif
1639 #ifdef ASM_OUTPUT_FUNCTION_PREFIX
1640 ASM_OUTPUT_FUNCTION_PREFIX (asm_out_file, fnname);
1641 #endif
1643 if (!DECL_IGNORED_P (decl))
1644 (*debug_hooks->begin_function) (decl);
1646 /* Make function name accessible from other files, if appropriate. */
1648 if (TREE_PUBLIC (decl))
1650 notice_global_symbol (decl);
1652 globalize_decl (decl);
1654 maybe_assemble_visibility (decl);
1657 if (DECL_PRESERVE_P (decl))
1658 targetm.asm_out.mark_decl_preserved (fnname);
1660 /* Do any machine/system dependent processing of the function name. */
1661 #ifdef ASM_DECLARE_FUNCTION_NAME
1662 ASM_DECLARE_FUNCTION_NAME (asm_out_file, fnname, current_function_decl);
1663 #else
1664 /* Standard thing is just output label for the function. */
1665 ASM_OUTPUT_FUNCTION_LABEL (asm_out_file, fnname, current_function_decl);
1666 #endif /* ASM_DECLARE_FUNCTION_NAME */
1668 if (lookup_attribute ("no_split_stack", DECL_ATTRIBUTES (decl)))
1669 saw_no_split_stack = true;
1672 /* Output assembler code associated with defining the size of the
1673 function. DECL describes the function. NAME is the function's name. */
1675 void
1676 assemble_end_function (tree decl, const char *fnname ATTRIBUTE_UNUSED)
1678 #ifdef ASM_DECLARE_FUNCTION_SIZE
1679 /* We could have switched section in the middle of the function. */
1680 if (flag_reorder_blocks_and_partition)
1681 switch_to_section (function_section (decl));
1682 ASM_DECLARE_FUNCTION_SIZE (asm_out_file, fnname, decl);
1683 #endif
1684 if (! CONSTANT_POOL_BEFORE_FUNCTION)
1686 output_constant_pool (fnname, decl);
1687 switch_to_section (function_section (decl)); /* need to switch back */
1689 /* Output labels for end of hot/cold text sections (to be used by
1690 debug info.) */
1691 if (flag_reorder_blocks_and_partition)
1693 section *save_text_section;
1695 save_text_section = in_section;
1696 switch_to_section (unlikely_text_section ());
1697 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_end_label);
1698 if (first_function_block_is_cold)
1699 switch_to_section (text_section);
1700 else
1701 switch_to_section (function_section (decl));
1702 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_end_label);
1703 switch_to_section (save_text_section);
1707 /* Assemble code to leave SIZE bytes of zeros. */
1709 void
1710 assemble_zeros (unsigned HOST_WIDE_INT size)
1712 /* Do no output if -fsyntax-only. */
1713 if (flag_syntax_only)
1714 return;
1716 #ifdef ASM_NO_SKIP_IN_TEXT
1717 /* The `space' pseudo in the text section outputs nop insns rather than 0s,
1718 so we must output 0s explicitly in the text section. */
1719 if (ASM_NO_SKIP_IN_TEXT && (in_section->common.flags & SECTION_CODE) != 0)
1721 unsigned HOST_WIDE_INT i;
1722 for (i = 0; i < size; i++)
1723 assemble_integer (const0_rtx, 1, BITS_PER_UNIT, 1);
1725 else
1726 #endif
1727 if (size > 0)
1728 ASM_OUTPUT_SKIP (asm_out_file, size);
1731 /* Assemble an alignment pseudo op for an ALIGN-bit boundary. */
1733 void
1734 assemble_align (int align)
1736 if (align > BITS_PER_UNIT)
1738 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
1742 /* Assemble a string constant with the specified C string as contents. */
1744 void
1745 assemble_string (const char *p, int size)
1747 int pos = 0;
1748 int maximum = 2000;
1750 /* If the string is very long, split it up. */
1752 while (pos < size)
1754 int thissize = size - pos;
1755 if (thissize > maximum)
1756 thissize = maximum;
1758 ASM_OUTPUT_ASCII (asm_out_file, p, thissize);
1760 pos += thissize;
1761 p += thissize;
1766 /* A noswitch_section_callback for lcomm_section. */
1768 static bool
1769 emit_local (tree decl ATTRIBUTE_UNUSED,
1770 const char *name ATTRIBUTE_UNUSED,
1771 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1772 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1774 #if defined ASM_OUTPUT_ALIGNED_DECL_LOCAL
1775 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, decl, name,
1776 size, DECL_ALIGN (decl));
1777 return true;
1778 #elif defined ASM_OUTPUT_ALIGNED_LOCAL
1779 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, DECL_ALIGN (decl));
1780 return true;
1781 #else
1782 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
1783 return false;
1784 #endif
1787 /* A noswitch_section_callback for bss_noswitch_section. */
1789 #if defined ASM_OUTPUT_ALIGNED_BSS
1790 static bool
1791 emit_bss (tree decl ATTRIBUTE_UNUSED,
1792 const char *name ATTRIBUTE_UNUSED,
1793 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1794 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1796 #if defined ASM_OUTPUT_ALIGNED_BSS
1797 ASM_OUTPUT_ALIGNED_BSS (asm_out_file, decl, name, size, DECL_ALIGN (decl));
1798 return true;
1799 #endif
1801 #endif
1803 /* A noswitch_section_callback for comm_section. */
1805 static bool
1806 emit_common (tree decl ATTRIBUTE_UNUSED,
1807 const char *name ATTRIBUTE_UNUSED,
1808 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1809 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1811 #if defined ASM_OUTPUT_ALIGNED_DECL_COMMON
1812 ASM_OUTPUT_ALIGNED_DECL_COMMON (asm_out_file, decl, name,
1813 size, DECL_ALIGN (decl));
1814 return true;
1815 #elif defined ASM_OUTPUT_ALIGNED_COMMON
1816 ASM_OUTPUT_ALIGNED_COMMON (asm_out_file, name, size, DECL_ALIGN (decl));
1817 return true;
1818 #else
1819 ASM_OUTPUT_COMMON (asm_out_file, name, size, rounded);
1820 return false;
1821 #endif
1824 /* A noswitch_section_callback for tls_comm_section. */
1826 static bool
1827 emit_tls_common (tree decl ATTRIBUTE_UNUSED,
1828 const char *name ATTRIBUTE_UNUSED,
1829 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1830 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1832 #ifdef ASM_OUTPUT_TLS_COMMON
1833 ASM_OUTPUT_TLS_COMMON (asm_out_file, decl, name, size);
1834 return true;
1835 #else
1836 sorry ("thread-local COMMON data not implemented");
1837 return true;
1838 #endif
1841 /* Assemble DECL given that it belongs in SECTION_NOSWITCH section SECT.
1842 NAME is the name of DECL's SYMBOL_REF. */
1844 static void
1845 assemble_noswitch_variable (tree decl, const char *name, section *sect)
1847 unsigned HOST_WIDE_INT size, rounded;
1849 size = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
1850 rounded = size;
1852 /* Don't allocate zero bytes of common,
1853 since that means "undefined external" in the linker. */
1854 if (size == 0)
1855 rounded = 1;
1857 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
1858 so that each uninitialized object starts on such a boundary. */
1859 rounded += (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1;
1860 rounded = (rounded / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
1861 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
1863 if (!sect->noswitch.callback (decl, name, size, rounded)
1864 && (unsigned HOST_WIDE_INT) DECL_ALIGN_UNIT (decl) > rounded)
1865 warning (0, "requested alignment for %q+D is greater than "
1866 "implemented alignment of %wu", decl, rounded);
1869 /* A subroutine of assemble_variable. Output the label and contents of
1870 DECL, whose address is a SYMBOL_REF with name NAME. DONT_OUTPUT_DATA
1871 is as for assemble_variable. */
1873 static void
1874 assemble_variable_contents (tree decl, const char *name,
1875 bool dont_output_data)
1877 /* Do any machine/system dependent processing of the object. */
1878 #ifdef ASM_DECLARE_OBJECT_NAME
1879 last_assemble_variable_decl = decl;
1880 ASM_DECLARE_OBJECT_NAME (asm_out_file, name, decl);
1881 #else
1882 /* Standard thing is just output label for the object. */
1883 ASM_OUTPUT_LABEL (asm_out_file, name);
1884 #endif /* ASM_DECLARE_OBJECT_NAME */
1886 if (!dont_output_data)
1888 if (DECL_INITIAL (decl)
1889 && DECL_INITIAL (decl) != error_mark_node
1890 && !initializer_zerop (DECL_INITIAL (decl)))
1891 /* Output the actual data. */
1892 output_constant (DECL_INITIAL (decl),
1893 tree_low_cst (DECL_SIZE_UNIT (decl), 1),
1894 DECL_ALIGN (decl));
1895 else
1896 /* Leave space for it. */
1897 assemble_zeros (tree_low_cst (DECL_SIZE_UNIT (decl), 1));
1901 /* Assemble everything that is needed for a variable or function declaration.
1902 Not used for automatic variables, and not used for function definitions.
1903 Should not be called for variables of incomplete structure type.
1905 TOP_LEVEL is nonzero if this variable has file scope.
1906 AT_END is nonzero if this is the special handling, at end of compilation,
1907 to define things that have had only tentative definitions.
1908 DONT_OUTPUT_DATA if nonzero means don't actually output the
1909 initial value (that will be done by the caller). */
1911 void
1912 assemble_variable (tree decl, int top_level ATTRIBUTE_UNUSED,
1913 int at_end ATTRIBUTE_UNUSED, int dont_output_data)
1915 const char *name;
1916 rtx decl_rtl, symbol;
1917 section *sect;
1919 /* This function is supposed to handle VARIABLES. Ensure we have one. */
1920 gcc_assert (TREE_CODE (decl) == VAR_DECL);
1922 /* Emulated TLS had better not get this far. */
1923 gcc_checking_assert (targetm.have_tls || !DECL_THREAD_LOCAL_P (decl));
1925 last_assemble_variable_decl = 0;
1927 /* Normally no need to say anything here for external references,
1928 since assemble_external is called by the language-specific code
1929 when a declaration is first seen. */
1931 if (DECL_EXTERNAL (decl))
1932 return;
1934 /* Do nothing for global register variables. */
1935 if (DECL_RTL_SET_P (decl) && REG_P (DECL_RTL (decl)))
1937 TREE_ASM_WRITTEN (decl) = 1;
1938 return;
1941 /* If type was incomplete when the variable was declared,
1942 see if it is complete now. */
1944 if (DECL_SIZE (decl) == 0)
1945 layout_decl (decl, 0);
1947 /* Still incomplete => don't allocate it; treat the tentative defn
1948 (which is what it must have been) as an `extern' reference. */
1950 if (!dont_output_data && DECL_SIZE (decl) == 0)
1952 error ("storage size of %q+D isn%'t known", decl);
1953 TREE_ASM_WRITTEN (decl) = 1;
1954 return;
1957 /* The first declaration of a variable that comes through this function
1958 decides whether it is global (in C, has external linkage)
1959 or local (in C, has internal linkage). So do nothing more
1960 if this function has already run. */
1962 if (TREE_ASM_WRITTEN (decl))
1963 return;
1965 /* Make sure targetm.encode_section_info is invoked before we set
1966 ASM_WRITTEN. */
1967 decl_rtl = DECL_RTL (decl);
1969 TREE_ASM_WRITTEN (decl) = 1;
1971 /* Do no output if -fsyntax-only. */
1972 if (flag_syntax_only)
1973 return;
1975 if (! dont_output_data
1976 && ! host_integerp (DECL_SIZE_UNIT (decl), 1))
1978 error ("size of variable %q+D is too large", decl);
1979 return;
1982 gcc_assert (MEM_P (decl_rtl));
1983 gcc_assert (GET_CODE (XEXP (decl_rtl, 0)) == SYMBOL_REF);
1984 symbol = XEXP (decl_rtl, 0);
1986 /* If this symbol belongs to the tree constant pool, output the constant
1987 if it hasn't already been written. */
1988 if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
1990 tree decl = SYMBOL_REF_DECL (symbol);
1991 if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
1992 output_constant_def_contents (symbol);
1993 return;
1996 app_disable ();
1998 name = XSTR (symbol, 0);
1999 if (TREE_PUBLIC (decl) && DECL_NAME (decl))
2000 notice_global_symbol (decl);
2002 /* Compute the alignment of this data. */
2004 align_variable (decl, dont_output_data);
2005 set_mem_align (decl_rtl, DECL_ALIGN (decl));
2007 if (TREE_PUBLIC (decl))
2008 maybe_assemble_visibility (decl);
2010 if (DECL_PRESERVE_P (decl))
2011 targetm.asm_out.mark_decl_preserved (name);
2013 /* First make the assembler name(s) global if appropriate. */
2014 sect = get_variable_section (decl, false);
2015 if (TREE_PUBLIC (decl)
2016 && (sect->common.flags & SECTION_COMMON) == 0)
2017 globalize_decl (decl);
2019 /* Output any data that we will need to use the address of. */
2020 if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node)
2021 output_addressed_constants (DECL_INITIAL (decl));
2023 /* dbxout.c needs to know this. */
2024 if (sect && (sect->common.flags & SECTION_CODE) != 0)
2025 DECL_IN_TEXT_SECTION (decl) = 1;
2027 /* If the decl is part of an object_block, make sure that the decl
2028 has been positioned within its block, but do not write out its
2029 definition yet. output_object_blocks will do that later. */
2030 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
2032 gcc_assert (!dont_output_data);
2033 place_block_symbol (symbol);
2035 else if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
2036 assemble_noswitch_variable (decl, name, sect);
2037 else
2039 switch_to_section (sect);
2040 if (DECL_ALIGN (decl) > BITS_PER_UNIT)
2041 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (DECL_ALIGN_UNIT (decl)));
2042 assemble_variable_contents (decl, name, dont_output_data);
2046 /* Return 1 if type TYPE contains any pointers. */
2048 static int
2049 contains_pointers_p (tree type)
2051 switch (TREE_CODE (type))
2053 case POINTER_TYPE:
2054 case REFERENCE_TYPE:
2055 /* I'm not sure whether OFFSET_TYPE needs this treatment,
2056 so I'll play safe and return 1. */
2057 case OFFSET_TYPE:
2058 return 1;
2060 case RECORD_TYPE:
2061 case UNION_TYPE:
2062 case QUAL_UNION_TYPE:
2064 tree fields;
2065 /* For a type that has fields, see if the fields have pointers. */
2066 for (fields = TYPE_FIELDS (type); fields; fields = DECL_CHAIN (fields))
2067 if (TREE_CODE (fields) == FIELD_DECL
2068 && contains_pointers_p (TREE_TYPE (fields)))
2069 return 1;
2070 return 0;
2073 case ARRAY_TYPE:
2074 /* An array type contains pointers if its element type does. */
2075 return contains_pointers_p (TREE_TYPE (type));
2077 default:
2078 return 0;
2082 /* We delay assemble_external processing until
2083 the compilation unit is finalized. This is the best we can do for
2084 right now (i.e. stage 3 of GCC 4.0) - the right thing is to delay
2085 it all the way to final. See PR 17982 for further discussion. */
2086 static GTY(()) tree pending_assemble_externals;
2088 #ifdef ASM_OUTPUT_EXTERNAL
2089 /* True if DECL is a function decl for which no out-of-line copy exists.
2090 It is assumed that DECL's assembler name has been set. */
2092 static bool
2093 incorporeal_function_p (tree decl)
2095 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
2097 const char *name;
2099 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
2100 && DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA)
2101 return true;
2103 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2104 if (is_builtin_name (name))
2105 return true;
2107 return false;
2110 /* Actually do the tests to determine if this is necessary, and invoke
2111 ASM_OUTPUT_EXTERNAL. */
2112 static void
2113 assemble_external_real (tree decl)
2115 rtx rtl = DECL_RTL (decl);
2117 if (MEM_P (rtl) && GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF
2118 && !SYMBOL_REF_USED (XEXP (rtl, 0))
2119 && !incorporeal_function_p (decl))
2121 /* Some systems do require some output. */
2122 SYMBOL_REF_USED (XEXP (rtl, 0)) = 1;
2123 ASM_OUTPUT_EXTERNAL (asm_out_file, decl, XSTR (XEXP (rtl, 0), 0));
2126 #endif
2128 void
2129 process_pending_assemble_externals (void)
2131 #ifdef ASM_OUTPUT_EXTERNAL
2132 tree list;
2133 for (list = pending_assemble_externals; list; list = TREE_CHAIN (list))
2134 assemble_external_real (TREE_VALUE (list));
2136 pending_assemble_externals = 0;
2137 #endif
2140 /* This TREE_LIST contains any weak symbol declarations waiting
2141 to be emitted. */
2142 static GTY(()) tree weak_decls;
2144 /* Output something to declare an external symbol to the assembler,
2145 and qualifiers such as weakness. (Most assemblers don't need
2146 extern declaration, so we normally output nothing.) Do nothing if
2147 DECL is not external. */
2149 void
2150 assemble_external (tree decl ATTRIBUTE_UNUSED)
2152 /* Because most platforms do not define ASM_OUTPUT_EXTERNAL, the
2153 main body of this code is only rarely exercised. To provide some
2154 testing, on all platforms, we make sure that the ASM_OUT_FILE is
2155 open. If it's not, we should not be calling this function. */
2156 gcc_assert (asm_out_file);
2158 if (!DECL_P (decl) || !DECL_EXTERNAL (decl) || !TREE_PUBLIC (decl))
2159 return;
2161 /* We want to output annotation for weak and external symbols at
2162 very last to check if they are references or not. */
2164 if (TARGET_SUPPORTS_WEAK
2165 && DECL_WEAK (decl)
2166 /* TREE_STATIC is a weird and abused creature which is not
2167 generally the right test for whether an entity has been
2168 locally emitted, inlined or otherwise not-really-extern, but
2169 for declarations that can be weak, it happens to be
2170 match. */
2171 && !TREE_STATIC (decl)
2172 && lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
2173 && value_member (decl, weak_decls) == NULL_TREE)
2174 weak_decls = tree_cons (NULL, decl, weak_decls);
2176 #ifdef ASM_OUTPUT_EXTERNAL
2177 if (value_member (decl, pending_assemble_externals) == NULL_TREE)
2178 pending_assemble_externals = tree_cons (NULL, decl,
2179 pending_assemble_externals);
2180 #endif
2183 /* Similar, for calling a library function FUN. */
2185 void
2186 assemble_external_libcall (rtx fun)
2188 /* Declare library function name external when first used, if nec. */
2189 if (! SYMBOL_REF_USED (fun))
2191 SYMBOL_REF_USED (fun) = 1;
2192 targetm.asm_out.external_libcall (fun);
2196 /* Assemble a label named NAME. */
2198 void
2199 assemble_label (FILE *file, const char *name)
2201 ASM_OUTPUT_LABEL (file, name);
2204 /* Set the symbol_referenced flag for ID. */
2205 void
2206 mark_referenced (tree id)
2208 TREE_SYMBOL_REFERENCED (id) = 1;
2211 /* Set the symbol_referenced flag for DECL and notify callgraph. */
2212 void
2213 mark_decl_referenced (tree decl)
2215 if (TREE_CODE (decl) == FUNCTION_DECL)
2217 /* Extern inline functions don't become needed when referenced.
2218 If we know a method will be emitted in other TU and no new
2219 functions can be marked reachable, just use the external
2220 definition. */
2221 struct cgraph_node *node = cgraph_get_create_node (decl);
2222 if (!DECL_EXTERNAL (decl)
2223 && !node->local.finalized)
2224 cgraph_mark_needed_node (node);
2226 else if (TREE_CODE (decl) == VAR_DECL)
2228 struct varpool_node *node = varpool_node (decl);
2229 varpool_mark_needed_node (node);
2230 /* C++ frontend use mark_decl_references to force COMDAT variables
2231 to be output that might appear dead otherwise. */
2232 node->force_output = true;
2234 /* else do nothing - we can get various sorts of CST nodes here,
2235 which do not need to be marked. */
2239 /* Follow the IDENTIFIER_TRANSPARENT_ALIAS chain starting at *ALIAS
2240 until we find an identifier that is not itself a transparent alias.
2241 Modify the alias passed to it by reference (and all aliases on the
2242 way to the ultimate target), such that they do not have to be
2243 followed again, and return the ultimate target of the alias
2244 chain. */
2246 static inline tree
2247 ultimate_transparent_alias_target (tree *alias)
2249 tree target = *alias;
2251 if (IDENTIFIER_TRANSPARENT_ALIAS (target))
2253 gcc_assert (TREE_CHAIN (target));
2254 target = ultimate_transparent_alias_target (&TREE_CHAIN (target));
2255 gcc_assert (! IDENTIFIER_TRANSPARENT_ALIAS (target)
2256 && ! TREE_CHAIN (target));
2257 *alias = target;
2260 return target;
2263 /* Output to FILE (an assembly file) a reference to NAME. If NAME
2264 starts with a *, the rest of NAME is output verbatim. Otherwise
2265 NAME is transformed in a target-specific way (usually by the
2266 addition of an underscore). */
2268 void
2269 assemble_name_raw (FILE *file, const char *name)
2271 if (name[0] == '*')
2272 fputs (&name[1], file);
2273 else
2274 ASM_OUTPUT_LABELREF (file, name);
2277 /* Like assemble_name_raw, but should be used when NAME might refer to
2278 an entity that is also represented as a tree (like a function or
2279 variable). If NAME does refer to such an entity, that entity will
2280 be marked as referenced. */
2282 void
2283 assemble_name (FILE *file, const char *name)
2285 const char *real_name;
2286 tree id;
2288 real_name = targetm.strip_name_encoding (name);
2290 id = maybe_get_identifier (real_name);
2291 if (id)
2293 tree id_orig = id;
2295 mark_referenced (id);
2296 ultimate_transparent_alias_target (&id);
2297 if (id != id_orig)
2298 name = IDENTIFIER_POINTER (id);
2301 assemble_name_raw (file, name);
2304 /* Allocate SIZE bytes writable static space with a gensym name
2305 and return an RTX to refer to its address. */
2308 assemble_static_space (unsigned HOST_WIDE_INT size)
2310 char name[12];
2311 const char *namestring;
2312 rtx x;
2314 ASM_GENERATE_INTERNAL_LABEL (name, "LF", const_labelno);
2315 ++const_labelno;
2316 namestring = ggc_strdup (name);
2318 x = gen_rtx_SYMBOL_REF (Pmode, namestring);
2319 SYMBOL_REF_FLAGS (x) = SYMBOL_FLAG_LOCAL;
2321 #ifdef ASM_OUTPUT_ALIGNED_DECL_LOCAL
2322 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, NULL_TREE, name, size,
2323 BIGGEST_ALIGNMENT);
2324 #else
2325 #ifdef ASM_OUTPUT_ALIGNED_LOCAL
2326 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, BIGGEST_ALIGNMENT);
2327 #else
2329 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
2330 so that each uninitialized object starts on such a boundary. */
2331 /* Variable `rounded' might or might not be used in ASM_OUTPUT_LOCAL. */
2332 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED
2333 = ((size + (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1)
2334 / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
2335 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
2336 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
2338 #endif
2339 #endif
2340 return x;
2343 /* Assemble the static constant template for function entry trampolines.
2344 This is done at most once per compilation.
2345 Returns an RTX for the address of the template. */
2347 static GTY(()) rtx initial_trampoline;
2350 assemble_trampoline_template (void)
2352 char label[256];
2353 const char *name;
2354 int align;
2355 rtx symbol;
2357 gcc_assert (targetm.asm_out.trampoline_template != NULL);
2359 if (initial_trampoline)
2360 return initial_trampoline;
2362 /* By default, put trampoline templates in read-only data section. */
2364 #ifdef TRAMPOLINE_SECTION
2365 switch_to_section (TRAMPOLINE_SECTION);
2366 #else
2367 switch_to_section (readonly_data_section);
2368 #endif
2370 /* Write the assembler code to define one. */
2371 align = floor_log2 (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
2372 if (align > 0)
2373 ASM_OUTPUT_ALIGN (asm_out_file, align);
2375 targetm.asm_out.internal_label (asm_out_file, "LTRAMP", 0);
2376 targetm.asm_out.trampoline_template (asm_out_file);
2378 /* Record the rtl to refer to it. */
2379 ASM_GENERATE_INTERNAL_LABEL (label, "LTRAMP", 0);
2380 name = ggc_strdup (label);
2381 symbol = gen_rtx_SYMBOL_REF (Pmode, name);
2382 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_LOCAL;
2384 initial_trampoline = gen_const_mem (BLKmode, symbol);
2385 set_mem_align (initial_trampoline, TRAMPOLINE_ALIGNMENT);
2386 set_mem_size (initial_trampoline, GEN_INT (TRAMPOLINE_SIZE));
2388 return initial_trampoline;
2391 /* A and B are either alignments or offsets. Return the minimum alignment
2392 that may be assumed after adding the two together. */
2394 static inline unsigned
2395 min_align (unsigned int a, unsigned int b)
2397 return (a | b) & -(a | b);
2400 /* Return the assembler directive for creating a given kind of integer
2401 object. SIZE is the number of bytes in the object and ALIGNED_P
2402 indicates whether it is known to be aligned. Return NULL if the
2403 assembly dialect has no such directive.
2405 The returned string should be printed at the start of a new line and
2406 be followed immediately by the object's initial value. */
2408 const char *
2409 integer_asm_op (int size, int aligned_p)
2411 struct asm_int_op *ops;
2413 if (aligned_p)
2414 ops = &targetm.asm_out.aligned_op;
2415 else
2416 ops = &targetm.asm_out.unaligned_op;
2418 switch (size)
2420 case 1:
2421 return targetm.asm_out.byte_op;
2422 case 2:
2423 return ops->hi;
2424 case 4:
2425 return ops->si;
2426 case 8:
2427 return ops->di;
2428 case 16:
2429 return ops->ti;
2430 default:
2431 return NULL;
2435 /* Use directive OP to assemble an integer object X. Print OP at the
2436 start of the line, followed immediately by the value of X. */
2438 void
2439 assemble_integer_with_op (const char *op, rtx x)
2441 fputs (op, asm_out_file);
2442 output_addr_const (asm_out_file, x);
2443 fputc ('\n', asm_out_file);
2446 /* The default implementation of the asm_out.integer target hook. */
2448 bool
2449 default_assemble_integer (rtx x ATTRIBUTE_UNUSED,
2450 unsigned int size ATTRIBUTE_UNUSED,
2451 int aligned_p ATTRIBUTE_UNUSED)
2453 const char *op = integer_asm_op (size, aligned_p);
2454 /* Avoid GAS bugs for large values. Specifically negative values whose
2455 absolute value fits in a bfd_vma, but not in a bfd_signed_vma. */
2456 if (size > UNITS_PER_WORD && size > POINTER_SIZE / BITS_PER_UNIT)
2457 return false;
2458 return op && (assemble_integer_with_op (op, x), true);
2461 /* Assemble the integer constant X into an object of SIZE bytes. ALIGN is
2462 the alignment of the integer in bits. Return 1 if we were able to output
2463 the constant, otherwise 0. We must be able to output the constant,
2464 if FORCE is nonzero. */
2466 bool
2467 assemble_integer (rtx x, unsigned int size, unsigned int align, int force)
2469 int aligned_p;
2471 aligned_p = (align >= MIN (size * BITS_PER_UNIT, BIGGEST_ALIGNMENT));
2473 /* See if the target hook can handle this kind of object. */
2474 if (targetm.asm_out.integer (x, size, aligned_p))
2475 return true;
2477 /* If the object is a multi-byte one, try splitting it up. Split
2478 it into words it if is multi-word, otherwise split it into bytes. */
2479 if (size > 1)
2481 enum machine_mode omode, imode;
2482 unsigned int subalign;
2483 unsigned int subsize, i;
2484 enum mode_class mclass;
2486 subsize = size > UNITS_PER_WORD? UNITS_PER_WORD : 1;
2487 subalign = MIN (align, subsize * BITS_PER_UNIT);
2488 if (GET_CODE (x) == CONST_FIXED)
2489 mclass = GET_MODE_CLASS (GET_MODE (x));
2490 else
2491 mclass = MODE_INT;
2493 omode = mode_for_size (subsize * BITS_PER_UNIT, mclass, 0);
2494 imode = mode_for_size (size * BITS_PER_UNIT, mclass, 0);
2496 for (i = 0; i < size; i += subsize)
2498 rtx partial = simplify_subreg (omode, x, imode, i);
2499 if (!partial || !assemble_integer (partial, subsize, subalign, 0))
2500 break;
2502 if (i == size)
2503 return true;
2505 /* If we've printed some of it, but not all of it, there's no going
2506 back now. */
2507 gcc_assert (!i);
2510 gcc_assert (!force);
2512 return false;
2515 void
2516 assemble_real (REAL_VALUE_TYPE d, enum machine_mode mode, unsigned int align)
2518 long data[4] = {0, 0, 0, 0};
2519 int i;
2520 int bitsize, nelts, nunits, units_per;
2522 /* This is hairy. We have a quantity of known size. real_to_target
2523 will put it into an array of *host* longs, 32 bits per element
2524 (even if long is more than 32 bits). We need to determine the
2525 number of array elements that are occupied (nelts) and the number
2526 of *target* min-addressable units that will be occupied in the
2527 object file (nunits). We cannot assume that 32 divides the
2528 mode's bitsize (size * BITS_PER_UNIT) evenly.
2530 size * BITS_PER_UNIT is used here to make sure that padding bits
2531 (which might appear at either end of the value; real_to_target
2532 will include the padding bits in its output array) are included. */
2534 nunits = GET_MODE_SIZE (mode);
2535 bitsize = nunits * BITS_PER_UNIT;
2536 nelts = CEIL (bitsize, 32);
2537 units_per = 32 / BITS_PER_UNIT;
2539 real_to_target (data, &d, mode);
2541 /* Put out the first word with the specified alignment. */
2542 assemble_integer (GEN_INT (data[0]), MIN (nunits, units_per), align, 1);
2543 nunits -= units_per;
2545 /* Subsequent words need only 32-bit alignment. */
2546 align = min_align (align, 32);
2548 for (i = 1; i < nelts; i++)
2550 assemble_integer (GEN_INT (data[i]), MIN (nunits, units_per), align, 1);
2551 nunits -= units_per;
2555 /* Given an expression EXP with a constant value,
2556 reduce it to the sum of an assembler symbol and an integer.
2557 Store them both in the structure *VALUE.
2558 EXP must be reducible. */
2560 struct addr_const {
2561 rtx base;
2562 HOST_WIDE_INT offset;
2565 static void
2566 decode_addr_const (tree exp, struct addr_const *value)
2568 tree target = TREE_OPERAND (exp, 0);
2569 int offset = 0;
2570 rtx x;
2572 while (1)
2574 if (TREE_CODE (target) == COMPONENT_REF
2575 && host_integerp (byte_position (TREE_OPERAND (target, 1)), 0))
2577 offset += int_byte_position (TREE_OPERAND (target, 1));
2578 target = TREE_OPERAND (target, 0);
2580 else if (TREE_CODE (target) == ARRAY_REF
2581 || TREE_CODE (target) == ARRAY_RANGE_REF)
2583 offset += (tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (target)), 1)
2584 * tree_low_cst (TREE_OPERAND (target, 1), 0));
2585 target = TREE_OPERAND (target, 0);
2587 else if (TREE_CODE (target) == INDIRECT_REF
2588 && TREE_CODE (TREE_OPERAND (target, 0)) == NOP_EXPR
2589 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (target, 0), 0))
2590 == ADDR_EXPR)
2591 target = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (target, 0), 0), 0);
2592 else
2593 break;
2596 switch (TREE_CODE (target))
2598 case VAR_DECL:
2599 case FUNCTION_DECL:
2600 x = DECL_RTL (target);
2601 break;
2603 case LABEL_DECL:
2604 x = gen_rtx_MEM (FUNCTION_MODE,
2605 gen_rtx_LABEL_REF (Pmode, force_label_rtx (target)));
2606 break;
2608 case REAL_CST:
2609 case FIXED_CST:
2610 case STRING_CST:
2611 case COMPLEX_CST:
2612 case CONSTRUCTOR:
2613 case INTEGER_CST:
2614 x = output_constant_def (target, 1);
2615 break;
2617 default:
2618 gcc_unreachable ();
2621 gcc_assert (MEM_P (x));
2622 x = XEXP (x, 0);
2624 value->base = x;
2625 value->offset = offset;
2629 static GTY((param_is (struct constant_descriptor_tree)))
2630 htab_t const_desc_htab;
2632 static void maybe_output_constant_def_contents (struct constant_descriptor_tree *, int);
2634 /* Constant pool accessor function. */
2636 htab_t
2637 constant_pool_htab (void)
2639 return const_desc_htab;
2642 /* Compute a hash code for a constant expression. */
2644 static hashval_t
2645 const_desc_hash (const void *ptr)
2647 return ((const struct constant_descriptor_tree *)ptr)->hash;
2650 static hashval_t
2651 const_hash_1 (const tree exp)
2653 const char *p;
2654 hashval_t hi;
2655 int len, i;
2656 enum tree_code code = TREE_CODE (exp);
2658 /* Either set P and LEN to the address and len of something to hash and
2659 exit the switch or return a value. */
2661 switch (code)
2663 case INTEGER_CST:
2664 p = (char *) &TREE_INT_CST (exp);
2665 len = sizeof TREE_INT_CST (exp);
2666 break;
2668 case REAL_CST:
2669 return real_hash (TREE_REAL_CST_PTR (exp));
2671 case FIXED_CST:
2672 return fixed_hash (TREE_FIXED_CST_PTR (exp));
2674 case STRING_CST:
2675 p = TREE_STRING_POINTER (exp);
2676 len = TREE_STRING_LENGTH (exp);
2677 break;
2679 case COMPLEX_CST:
2680 return (const_hash_1 (TREE_REALPART (exp)) * 5
2681 + const_hash_1 (TREE_IMAGPART (exp)));
2683 case VECTOR_CST:
2685 tree link;
2687 hi = 7 + TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp));
2689 for (link = TREE_VECTOR_CST_ELTS (exp); link; link = TREE_CHAIN (link))
2690 hi = hi * 563 + const_hash_1 (TREE_VALUE (link));
2692 return hi;
2695 case CONSTRUCTOR:
2697 unsigned HOST_WIDE_INT idx;
2698 tree value;
2700 hi = 5 + int_size_in_bytes (TREE_TYPE (exp));
2702 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
2703 if (value)
2704 hi = hi * 603 + const_hash_1 (value);
2706 return hi;
2709 case ADDR_EXPR:
2710 case FDESC_EXPR:
2712 struct addr_const value;
2714 decode_addr_const (exp, &value);
2715 switch (GET_CODE (value.base))
2717 case SYMBOL_REF:
2718 /* Don't hash the address of the SYMBOL_REF;
2719 only use the offset and the symbol name. */
2720 hi = value.offset;
2721 p = XSTR (value.base, 0);
2722 for (i = 0; p[i] != 0; i++)
2723 hi = ((hi * 613) + (unsigned) (p[i]));
2724 break;
2726 case LABEL_REF:
2727 hi = value.offset + CODE_LABEL_NUMBER (XEXP (value.base, 0)) * 13;
2728 break;
2730 default:
2731 gcc_unreachable ();
2734 return hi;
2736 case PLUS_EXPR:
2737 case POINTER_PLUS_EXPR:
2738 case MINUS_EXPR:
2739 return (const_hash_1 (TREE_OPERAND (exp, 0)) * 9
2740 + const_hash_1 (TREE_OPERAND (exp, 1)));
2742 CASE_CONVERT:
2743 return const_hash_1 (TREE_OPERAND (exp, 0)) * 7 + 2;
2745 default:
2746 /* A language specific constant. Just hash the code. */
2747 return code;
2750 /* Compute hashing function. */
2751 hi = len;
2752 for (i = 0; i < len; i++)
2753 hi = ((hi * 613) + (unsigned) (p[i]));
2755 return hi;
2758 /* Wrapper of compare_constant, for the htab interface. */
2759 static int
2760 const_desc_eq (const void *p1, const void *p2)
2762 const struct constant_descriptor_tree *const c1
2763 = (const struct constant_descriptor_tree *) p1;
2764 const struct constant_descriptor_tree *const c2
2765 = (const struct constant_descriptor_tree *) p2;
2766 if (c1->hash != c2->hash)
2767 return 0;
2768 return compare_constant (c1->value, c2->value);
2771 /* Compare t1 and t2, and return 1 only if they are known to result in
2772 the same bit pattern on output. */
2774 static int
2775 compare_constant (const tree t1, const tree t2)
2777 enum tree_code typecode;
2779 if (t1 == NULL_TREE)
2780 return t2 == NULL_TREE;
2781 if (t2 == NULL_TREE)
2782 return 0;
2784 if (TREE_CODE (t1) != TREE_CODE (t2))
2785 return 0;
2787 switch (TREE_CODE (t1))
2789 case INTEGER_CST:
2790 /* Integer constants are the same only if the same width of type. */
2791 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
2792 return 0;
2793 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
2794 return 0;
2795 return tree_int_cst_equal (t1, t2);
2797 case REAL_CST:
2798 /* Real constants are the same only if the same width of type. */
2799 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
2800 return 0;
2802 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
2804 case FIXED_CST:
2805 /* Fixed constants are the same only if the same width of type. */
2806 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
2807 return 0;
2809 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
2811 case STRING_CST:
2812 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
2813 return 0;
2815 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
2816 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
2817 TREE_STRING_LENGTH (t1)));
2819 case COMPLEX_CST:
2820 return (compare_constant (TREE_REALPART (t1), TREE_REALPART (t2))
2821 && compare_constant (TREE_IMAGPART (t1), TREE_IMAGPART (t2)));
2823 case VECTOR_CST:
2825 tree link1, link2;
2827 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (t1))
2828 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (t2)))
2829 return 0;
2831 link2 = TREE_VECTOR_CST_ELTS (t2);
2832 for (link1 = TREE_VECTOR_CST_ELTS (t1);
2833 link1;
2834 link1 = TREE_CHAIN (link1))
2836 if (!compare_constant (TREE_VALUE (link1), TREE_VALUE (link2)))
2837 return 0;
2838 link2 = TREE_CHAIN (link2);
2841 return 1;
2844 case CONSTRUCTOR:
2846 VEC(constructor_elt, gc) *v1, *v2;
2847 unsigned HOST_WIDE_INT idx;
2849 typecode = TREE_CODE (TREE_TYPE (t1));
2850 if (typecode != TREE_CODE (TREE_TYPE (t2)))
2851 return 0;
2853 if (typecode == ARRAY_TYPE)
2855 HOST_WIDE_INT size_1 = int_size_in_bytes (TREE_TYPE (t1));
2856 /* For arrays, check that the sizes all match. */
2857 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))
2858 || size_1 == -1
2859 || size_1 != int_size_in_bytes (TREE_TYPE (t2)))
2860 return 0;
2862 else
2864 /* For record and union constructors, require exact type
2865 equality. */
2866 if (TREE_TYPE (t1) != TREE_TYPE (t2))
2867 return 0;
2870 v1 = CONSTRUCTOR_ELTS (t1);
2871 v2 = CONSTRUCTOR_ELTS (t2);
2872 if (VEC_length (constructor_elt, v1)
2873 != VEC_length (constructor_elt, v2))
2874 return 0;
2876 for (idx = 0; idx < VEC_length (constructor_elt, v1); ++idx)
2878 constructor_elt *c1 = VEC_index (constructor_elt, v1, idx);
2879 constructor_elt *c2 = VEC_index (constructor_elt, v2, idx);
2881 /* Check that each value is the same... */
2882 if (!compare_constant (c1->value, c2->value))
2883 return 0;
2884 /* ... and that they apply to the same fields! */
2885 if (typecode == ARRAY_TYPE)
2887 if (!compare_constant (c1->index, c2->index))
2888 return 0;
2890 else
2892 if (c1->index != c2->index)
2893 return 0;
2897 return 1;
2900 case ADDR_EXPR:
2901 case FDESC_EXPR:
2903 struct addr_const value1, value2;
2904 enum rtx_code code;
2905 int ret;
2907 decode_addr_const (t1, &value1);
2908 decode_addr_const (t2, &value2);
2910 if (value1.offset != value2.offset)
2911 return 0;
2913 code = GET_CODE (value1.base);
2914 if (code != GET_CODE (value2.base))
2915 return 0;
2917 switch (code)
2919 case SYMBOL_REF:
2920 ret = (strcmp (XSTR (value1.base, 0), XSTR (value2.base, 0)) == 0);
2921 break;
2923 case LABEL_REF:
2924 ret = (CODE_LABEL_NUMBER (XEXP (value1.base, 0))
2925 == CODE_LABEL_NUMBER (XEXP (value2.base, 0)));
2926 break;
2928 default:
2929 gcc_unreachable ();
2931 return ret;
2934 case PLUS_EXPR:
2935 case POINTER_PLUS_EXPR:
2936 case MINUS_EXPR:
2937 case RANGE_EXPR:
2938 return (compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
2939 && compare_constant(TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
2941 CASE_CONVERT:
2942 case VIEW_CONVERT_EXPR:
2943 return compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2945 default:
2946 return 0;
2949 gcc_unreachable ();
2952 /* Make a copy of the whole tree structure for a constant. This
2953 handles the same types of nodes that compare_constant handles. */
2955 static tree
2956 copy_constant (tree exp)
2958 switch (TREE_CODE (exp))
2960 case ADDR_EXPR:
2961 /* For ADDR_EXPR, we do not want to copy the decl whose address
2962 is requested. We do want to copy constants though. */
2963 if (CONSTANT_CLASS_P (TREE_OPERAND (exp, 0)))
2964 return build1 (TREE_CODE (exp), TREE_TYPE (exp),
2965 copy_constant (TREE_OPERAND (exp, 0)));
2966 else
2967 return copy_node (exp);
2969 case INTEGER_CST:
2970 case REAL_CST:
2971 case FIXED_CST:
2972 case STRING_CST:
2973 return copy_node (exp);
2975 case COMPLEX_CST:
2976 return build_complex (TREE_TYPE (exp),
2977 copy_constant (TREE_REALPART (exp)),
2978 copy_constant (TREE_IMAGPART (exp)));
2980 case PLUS_EXPR:
2981 case POINTER_PLUS_EXPR:
2982 case MINUS_EXPR:
2983 return build2 (TREE_CODE (exp), TREE_TYPE (exp),
2984 copy_constant (TREE_OPERAND (exp, 0)),
2985 copy_constant (TREE_OPERAND (exp, 1)));
2987 CASE_CONVERT:
2988 case VIEW_CONVERT_EXPR:
2989 return build1 (TREE_CODE (exp), TREE_TYPE (exp),
2990 copy_constant (TREE_OPERAND (exp, 0)));
2992 case VECTOR_CST:
2993 return build_vector (TREE_TYPE (exp),
2994 copy_list (TREE_VECTOR_CST_ELTS (exp)));
2996 case CONSTRUCTOR:
2998 tree copy = copy_node (exp);
2999 VEC(constructor_elt, gc) *v;
3000 unsigned HOST_WIDE_INT idx;
3001 tree purpose, value;
3003 v = VEC_alloc(constructor_elt, gc, VEC_length(constructor_elt,
3004 CONSTRUCTOR_ELTS (exp)));
3005 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, purpose, value)
3007 constructor_elt *ce = VEC_quick_push (constructor_elt, v, NULL);
3008 ce->index = purpose;
3009 ce->value = copy_constant (value);
3011 CONSTRUCTOR_ELTS (copy) = v;
3012 return copy;
3015 default:
3016 gcc_unreachable ();
3020 /* Return the section into which constant EXP should be placed. */
3022 static section *
3023 get_constant_section (tree exp, unsigned int align)
3025 return targetm.asm_out.select_section (exp,
3026 compute_reloc_for_constant (exp),
3027 align);
3030 /* Return the size of constant EXP in bytes. */
3032 static HOST_WIDE_INT
3033 get_constant_size (tree exp)
3035 HOST_WIDE_INT size;
3037 size = int_size_in_bytes (TREE_TYPE (exp));
3038 if (TREE_CODE (exp) == STRING_CST)
3039 size = MAX (TREE_STRING_LENGTH (exp), size);
3040 return size;
3043 /* Subroutine of output_constant_def:
3044 No constant equal to EXP is known to have been output.
3045 Make a constant descriptor to enter EXP in the hash table.
3046 Assign the label number and construct RTL to refer to the
3047 constant's location in memory.
3048 Caller is responsible for updating the hash table. */
3050 static struct constant_descriptor_tree *
3051 build_constant_desc (tree exp)
3053 struct constant_descriptor_tree *desc;
3054 rtx symbol, rtl;
3055 char label[256];
3056 int labelno;
3057 tree decl;
3059 desc = ggc_alloc_constant_descriptor_tree ();
3060 desc->value = copy_constant (exp);
3062 /* Propagate marked-ness to copied constant. */
3063 if (flag_mudflap && mf_marked_p (exp))
3064 mf_mark (desc->value);
3066 /* Create a string containing the label name, in LABEL. */
3067 labelno = const_labelno++;
3068 ASM_GENERATE_INTERNAL_LABEL (label, "LC", labelno);
3070 /* Construct the VAR_DECL associated with the constant. */
3071 decl = build_decl (UNKNOWN_LOCATION, VAR_DECL, get_identifier (label),
3072 TREE_TYPE (exp));
3073 DECL_ARTIFICIAL (decl) = 1;
3074 DECL_IGNORED_P (decl) = 1;
3075 TREE_READONLY (decl) = 1;
3076 TREE_STATIC (decl) = 1;
3077 TREE_ADDRESSABLE (decl) = 1;
3078 /* We don't set the RTL yet as this would cause varpool to assume that the
3079 variable is referenced. Moreover, it would just be dropped in LTO mode.
3080 Instead we set the flag that will be recognized in make_decl_rtl. */
3081 DECL_IN_CONSTANT_POOL (decl) = 1;
3082 DECL_INITIAL (decl) = desc->value;
3083 /* ??? CONSTANT_ALIGNMENT hasn't been updated for vector types on most
3084 architectures so use DATA_ALIGNMENT as well, except for strings. */
3085 if (TREE_CODE (exp) == STRING_CST)
3087 #ifdef CONSTANT_ALIGNMENT
3088 DECL_ALIGN (decl) = CONSTANT_ALIGNMENT (exp, DECL_ALIGN (decl));
3089 #endif
3091 else
3092 align_variable (decl, 0);
3094 /* Now construct the SYMBOL_REF and the MEM. */
3095 if (use_object_blocks_p ())
3097 section *sect = get_constant_section (exp, DECL_ALIGN (decl));
3098 symbol = create_block_symbol (ggc_strdup (label),
3099 get_block_for_section (sect), -1);
3101 else
3102 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3103 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3104 SET_SYMBOL_REF_DECL (symbol, decl);
3105 TREE_CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3107 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (exp)), symbol);
3108 set_mem_attributes (rtl, exp, 1);
3109 set_mem_alias_set (rtl, 0);
3110 set_mem_alias_set (rtl, const_alias_set);
3112 /* We cannot share RTX'es in pool entries.
3113 Mark this piece of RTL as required for unsharing. */
3114 RTX_FLAG (rtl, used) = 1;
3116 /* Set flags or add text to the name to record information, such as
3117 that it is a local symbol. If the name is changed, the macro
3118 ASM_OUTPUT_LABELREF will have to know how to strip this
3119 information. This call might invalidate our local variable
3120 SYMBOL; we can't use it afterward. */
3121 targetm.encode_section_info (exp, rtl, true);
3123 desc->rtl = rtl;
3125 return desc;
3128 /* Return an rtx representing a reference to constant data in memory
3129 for the constant expression EXP.
3131 If assembler code for such a constant has already been output,
3132 return an rtx to refer to it.
3133 Otherwise, output such a constant in memory
3134 and generate an rtx for it.
3136 If DEFER is nonzero, this constant can be deferred and output only
3137 if referenced in the function after all optimizations.
3139 `const_desc_table' records which constants already have label strings. */
3142 output_constant_def (tree exp, int defer)
3144 struct constant_descriptor_tree *desc;
3145 struct constant_descriptor_tree key;
3146 void **loc;
3148 /* Look up EXP in the table of constant descriptors. If we didn't find
3149 it, create a new one. */
3150 key.value = exp;
3151 key.hash = const_hash_1 (exp);
3152 loc = htab_find_slot_with_hash (const_desc_htab, &key, key.hash, INSERT);
3154 desc = (struct constant_descriptor_tree *) *loc;
3155 if (desc == 0)
3157 desc = build_constant_desc (exp);
3158 desc->hash = key.hash;
3159 *loc = desc;
3162 maybe_output_constant_def_contents (desc, defer);
3163 return desc->rtl;
3166 /* Subroutine of output_constant_def: Decide whether or not we need to
3167 output the constant DESC now, and if so, do it. */
3168 static void
3169 maybe_output_constant_def_contents (struct constant_descriptor_tree *desc,
3170 int defer)
3172 rtx symbol = XEXP (desc->rtl, 0);
3173 tree exp = desc->value;
3175 if (flag_syntax_only)
3176 return;
3178 if (TREE_ASM_WRITTEN (exp))
3179 /* Already output; don't do it again. */
3180 return;
3182 /* We can always defer constants as long as the context allows
3183 doing so. */
3184 if (defer)
3186 /* Increment n_deferred_constants if it exists. It needs to be at
3187 least as large as the number of constants actually referred to
3188 by the function. If it's too small we'll stop looking too early
3189 and fail to emit constants; if it's too large we'll only look
3190 through the entire function when we could have stopped earlier. */
3191 if (cfun)
3192 n_deferred_constants++;
3193 return;
3196 output_constant_def_contents (symbol);
3199 /* Subroutine of output_constant_def_contents. Output the definition
3200 of constant EXP, which is pointed to by label LABEL. ALIGN is the
3201 constant's alignment in bits. */
3203 static void
3204 assemble_constant_contents (tree exp, const char *label, unsigned int align)
3206 HOST_WIDE_INT size;
3208 size = get_constant_size (exp);
3210 /* Do any machine/system dependent processing of the constant. */
3211 targetm.asm_out.declare_constant_name (asm_out_file, label, exp, size);
3213 /* Output the value of EXP. */
3214 output_constant (exp, size, align);
3217 /* We must output the constant data referred to by SYMBOL; do so. */
3219 static void
3220 output_constant_def_contents (rtx symbol)
3222 tree decl = SYMBOL_REF_DECL (symbol);
3223 tree exp = DECL_INITIAL (decl);
3224 unsigned int align;
3226 /* Make sure any other constants whose addresses appear in EXP
3227 are assigned label numbers. */
3228 output_addressed_constants (exp);
3230 /* We are no longer deferring this constant. */
3231 TREE_ASM_WRITTEN (decl) = TREE_ASM_WRITTEN (exp) = 1;
3233 /* If the constant is part of an object block, make sure that the
3234 decl has been positioned within its block, but do not write out
3235 its definition yet. output_object_blocks will do that later. */
3236 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
3237 place_block_symbol (symbol);
3238 else
3240 align = DECL_ALIGN (decl);
3241 switch_to_section (get_constant_section (exp, align));
3242 if (align > BITS_PER_UNIT)
3243 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
3244 assemble_constant_contents (exp, XSTR (symbol, 0), align);
3246 if (flag_mudflap)
3247 mudflap_enqueue_constant (exp);
3250 /* Look up EXP in the table of constant descriptors. Return the rtl
3251 if it has been emitted, else null. */
3254 lookup_constant_def (tree exp)
3256 struct constant_descriptor_tree *desc;
3257 struct constant_descriptor_tree key;
3259 key.value = exp;
3260 key.hash = const_hash_1 (exp);
3261 desc = (struct constant_descriptor_tree *)
3262 htab_find_with_hash (const_desc_htab, &key, key.hash);
3264 return (desc ? desc->rtl : NULL_RTX);
3267 /* Return a tree representing a reference to constant data in memory
3268 for the constant expression EXP.
3270 This is the counterpart of output_constant_def at the Tree level. */
3272 tree
3273 tree_output_constant_def (tree exp)
3275 struct constant_descriptor_tree *desc, key;
3276 void **loc;
3277 tree decl;
3279 /* Look up EXP in the table of constant descriptors. If we didn't find
3280 it, create a new one. */
3281 key.value = exp;
3282 key.hash = const_hash_1 (exp);
3283 loc = htab_find_slot_with_hash (const_desc_htab, &key, key.hash, INSERT);
3285 desc = (struct constant_descriptor_tree *) *loc;
3286 if (desc == 0)
3288 desc = build_constant_desc (exp);
3289 desc->hash = key.hash;
3290 *loc = desc;
3293 decl = SYMBOL_REF_DECL (XEXP (desc->rtl, 0));
3294 varpool_finalize_decl (decl);
3295 return decl;
3298 /* Used in the hash tables to avoid outputting the same constant
3299 twice. Unlike 'struct constant_descriptor_tree', RTX constants
3300 are output once per function, not once per file. */
3301 /* ??? Only a few targets need per-function constant pools. Most
3302 can use one per-file pool. Should add a targetm bit to tell the
3303 difference. */
3305 struct GTY(()) rtx_constant_pool {
3306 /* Pointers to first and last constant in pool, as ordered by offset. */
3307 struct constant_descriptor_rtx *first;
3308 struct constant_descriptor_rtx *last;
3310 /* Hash facility for making memory-constants from constant rtl-expressions.
3311 It is used on RISC machines where immediate integer arguments and
3312 constant addresses are restricted so that such constants must be stored
3313 in memory. */
3314 htab_t GTY((param_is (struct constant_descriptor_rtx))) const_rtx_htab;
3316 /* Current offset in constant pool (does not include any
3317 machine-specific header). */
3318 HOST_WIDE_INT offset;
3321 struct GTY((chain_next ("%h.next"))) constant_descriptor_rtx {
3322 struct constant_descriptor_rtx *next;
3323 rtx mem;
3324 rtx sym;
3325 rtx constant;
3326 HOST_WIDE_INT offset;
3327 hashval_t hash;
3328 enum machine_mode mode;
3329 unsigned int align;
3330 int labelno;
3331 int mark;
3334 /* Hash and compare functions for const_rtx_htab. */
3336 static hashval_t
3337 const_desc_rtx_hash (const void *ptr)
3339 const struct constant_descriptor_rtx *const desc
3340 = (const struct constant_descriptor_rtx *) ptr;
3341 return desc->hash;
3344 static int
3345 const_desc_rtx_eq (const void *a, const void *b)
3347 const struct constant_descriptor_rtx *const x
3348 = (const struct constant_descriptor_rtx *) a;
3349 const struct constant_descriptor_rtx *const y
3350 = (const struct constant_descriptor_rtx *) b;
3352 if (x->mode != y->mode)
3353 return 0;
3354 return rtx_equal_p (x->constant, y->constant);
3357 /* This is the worker function for const_rtx_hash, called via for_each_rtx. */
3359 static int
3360 const_rtx_hash_1 (rtx *xp, void *data)
3362 unsigned HOST_WIDE_INT hwi;
3363 enum machine_mode mode;
3364 enum rtx_code code;
3365 hashval_t h, *hp;
3366 rtx x;
3368 x = *xp;
3369 code = GET_CODE (x);
3370 mode = GET_MODE (x);
3371 h = (hashval_t) code * 1048573 + mode;
3373 switch (code)
3375 case CONST_INT:
3376 hwi = INTVAL (x);
3377 fold_hwi:
3379 int shift = sizeof (hashval_t) * CHAR_BIT;
3380 const int n = sizeof (HOST_WIDE_INT) / sizeof (hashval_t);
3381 int i;
3383 h ^= (hashval_t) hwi;
3384 for (i = 1; i < n; ++i)
3386 hwi >>= shift;
3387 h ^= (hashval_t) hwi;
3390 break;
3392 case CONST_DOUBLE:
3393 if (mode == VOIDmode)
3395 hwi = CONST_DOUBLE_LOW (x) ^ CONST_DOUBLE_HIGH (x);
3396 goto fold_hwi;
3398 else
3399 h ^= real_hash (CONST_DOUBLE_REAL_VALUE (x));
3400 break;
3402 case CONST_FIXED:
3403 h ^= fixed_hash (CONST_FIXED_VALUE (x));
3404 break;
3406 case CONST_VECTOR:
3408 int i;
3409 for (i = XVECLEN (x, 0); i-- > 0; )
3410 h = h * 251 + const_rtx_hash_1 (&XVECEXP (x, 0, i), data);
3412 break;
3414 case SYMBOL_REF:
3415 h ^= htab_hash_string (XSTR (x, 0));
3416 break;
3418 case LABEL_REF:
3419 h = h * 251 + CODE_LABEL_NUMBER (XEXP (x, 0));
3420 break;
3422 case UNSPEC:
3423 case UNSPEC_VOLATILE:
3424 h = h * 251 + XINT (x, 1);
3425 break;
3427 default:
3428 break;
3431 hp = (hashval_t *) data;
3432 *hp = *hp * 509 + h;
3433 return 0;
3436 /* Compute a hash value for X, which should be a constant. */
3438 static hashval_t
3439 const_rtx_hash (rtx x)
3441 hashval_t h = 0;
3442 for_each_rtx (&x, const_rtx_hash_1, &h);
3443 return h;
3447 /* Create and return a new rtx constant pool. */
3449 static struct rtx_constant_pool *
3450 create_constant_pool (void)
3452 struct rtx_constant_pool *pool;
3454 pool = ggc_alloc_rtx_constant_pool ();
3455 pool->const_rtx_htab = htab_create_ggc (31, const_desc_rtx_hash,
3456 const_desc_rtx_eq, NULL);
3457 pool->first = NULL;
3458 pool->last = NULL;
3459 pool->offset = 0;
3460 return pool;
3463 /* Initialize constant pool hashing for a new function. */
3465 void
3466 init_varasm_status (void)
3468 crtl->varasm.pool = create_constant_pool ();
3469 crtl->varasm.deferred_constants = 0;
3472 /* Given a MINUS expression, simplify it if both sides
3473 include the same symbol. */
3476 simplify_subtraction (rtx x)
3478 rtx r = simplify_rtx (x);
3479 return r ? r : x;
3482 /* Given a constant rtx X, make (or find) a memory constant for its value
3483 and return a MEM rtx to refer to it in memory. */
3486 force_const_mem (enum machine_mode mode, rtx x)
3488 struct constant_descriptor_rtx *desc, tmp;
3489 struct rtx_constant_pool *pool;
3490 char label[256];
3491 rtx def, symbol;
3492 hashval_t hash;
3493 unsigned int align;
3494 void **slot;
3496 /* If we're not allowed to drop X into the constant pool, don't. */
3497 if (targetm.cannot_force_const_mem (mode, x))
3498 return NULL_RTX;
3500 /* Record that this function has used a constant pool entry. */
3501 crtl->uses_const_pool = 1;
3503 /* Decide which pool to use. */
3504 pool = (targetm.use_blocks_for_constant_p (mode, x)
3505 ? shared_constant_pool
3506 : crtl->varasm.pool);
3508 /* Lookup the value in the hashtable. */
3509 tmp.constant = x;
3510 tmp.mode = mode;
3511 hash = const_rtx_hash (x);
3512 slot = htab_find_slot_with_hash (pool->const_rtx_htab, &tmp, hash, INSERT);
3513 desc = (struct constant_descriptor_rtx *) *slot;
3515 /* If the constant was already present, return its memory. */
3516 if (desc)
3517 return copy_rtx (desc->mem);
3519 /* Otherwise, create a new descriptor. */
3520 desc = ggc_alloc_constant_descriptor_rtx ();
3521 *slot = desc;
3523 /* Align the location counter as required by EXP's data type. */
3524 align = GET_MODE_ALIGNMENT (mode == VOIDmode ? word_mode : mode);
3525 #ifdef CONSTANT_ALIGNMENT
3527 tree type = lang_hooks.types.type_for_mode (mode, 0);
3528 if (type != NULL_TREE)
3529 align = CONSTANT_ALIGNMENT (make_tree (type, x), align);
3531 #endif
3533 pool->offset += (align / BITS_PER_UNIT) - 1;
3534 pool->offset &= ~ ((align / BITS_PER_UNIT) - 1);
3536 desc->next = NULL;
3537 desc->constant = copy_rtx (tmp.constant);
3538 desc->offset = pool->offset;
3539 desc->hash = hash;
3540 desc->mode = mode;
3541 desc->align = align;
3542 desc->labelno = const_labelno;
3543 desc->mark = 0;
3545 pool->offset += GET_MODE_SIZE (mode);
3546 if (pool->last)
3547 pool->last->next = desc;
3548 else
3549 pool->first = pool->last = desc;
3550 pool->last = desc;
3552 /* Create a string containing the label name, in LABEL. */
3553 ASM_GENERATE_INTERNAL_LABEL (label, "LC", const_labelno);
3554 ++const_labelno;
3556 /* Construct the SYMBOL_REF. Make sure to mark it as belonging to
3557 the constants pool. */
3558 if (use_object_blocks_p () && targetm.use_blocks_for_constant_p (mode, x))
3560 section *sect = targetm.asm_out.select_rtx_section (mode, x, align);
3561 symbol = create_block_symbol (ggc_strdup (label),
3562 get_block_for_section (sect), -1);
3564 else
3565 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3566 desc->sym = symbol;
3567 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3568 CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3569 SET_SYMBOL_REF_CONSTANT (symbol, desc);
3571 /* Construct the MEM. */
3572 desc->mem = def = gen_const_mem (mode, symbol);
3573 set_mem_attributes (def, lang_hooks.types.type_for_mode (mode, 0), 1);
3574 set_mem_align (def, align);
3576 /* If we're dropping a label to the constant pool, make sure we
3577 don't delete it. */
3578 if (GET_CODE (x) == LABEL_REF)
3579 LABEL_PRESERVE_P (XEXP (x, 0)) = 1;
3581 return copy_rtx (def);
3584 /* Given a constant pool SYMBOL_REF, return the corresponding constant. */
3587 get_pool_constant (rtx addr)
3589 return SYMBOL_REF_CONSTANT (addr)->constant;
3592 /* Given a constant pool SYMBOL_REF, return the corresponding constant
3593 and whether it has been output or not. */
3596 get_pool_constant_mark (rtx addr, bool *pmarked)
3598 struct constant_descriptor_rtx *desc;
3600 desc = SYMBOL_REF_CONSTANT (addr);
3601 *pmarked = (desc->mark != 0);
3602 return desc->constant;
3605 /* Similar, return the mode. */
3607 enum machine_mode
3608 get_pool_mode (const_rtx addr)
3610 return SYMBOL_REF_CONSTANT (addr)->mode;
3613 /* Return the size of the constant pool. */
3616 get_pool_size (void)
3618 return crtl->varasm.pool->offset;
3621 /* Worker function for output_constant_pool_1. Emit assembly for X
3622 in MODE with known alignment ALIGN. */
3624 static void
3625 output_constant_pool_2 (enum machine_mode mode, rtx x, unsigned int align)
3627 switch (GET_MODE_CLASS (mode))
3629 case MODE_FLOAT:
3630 case MODE_DECIMAL_FLOAT:
3632 REAL_VALUE_TYPE r;
3634 gcc_assert (GET_CODE (x) == CONST_DOUBLE);
3635 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
3636 assemble_real (r, mode, align);
3637 break;
3640 case MODE_INT:
3641 case MODE_PARTIAL_INT:
3642 case MODE_FRACT:
3643 case MODE_UFRACT:
3644 case MODE_ACCUM:
3645 case MODE_UACCUM:
3646 assemble_integer (x, GET_MODE_SIZE (mode), align, 1);
3647 break;
3649 case MODE_VECTOR_FLOAT:
3650 case MODE_VECTOR_INT:
3651 case MODE_VECTOR_FRACT:
3652 case MODE_VECTOR_UFRACT:
3653 case MODE_VECTOR_ACCUM:
3654 case MODE_VECTOR_UACCUM:
3656 int i, units;
3657 enum machine_mode submode = GET_MODE_INNER (mode);
3658 unsigned int subalign = MIN (align, GET_MODE_BITSIZE (submode));
3660 gcc_assert (GET_CODE (x) == CONST_VECTOR);
3661 units = CONST_VECTOR_NUNITS (x);
3663 for (i = 0; i < units; i++)
3665 rtx elt = CONST_VECTOR_ELT (x, i);
3666 output_constant_pool_2 (submode, elt, i ? subalign : align);
3669 break;
3671 default:
3672 gcc_unreachable ();
3676 /* Worker function for output_constant_pool. Emit constant DESC,
3677 giving it ALIGN bits of alignment. */
3679 static void
3680 output_constant_pool_1 (struct constant_descriptor_rtx *desc,
3681 unsigned int align)
3683 rtx x, tmp;
3685 x = desc->constant;
3687 /* See if X is a LABEL_REF (or a CONST referring to a LABEL_REF)
3688 whose CODE_LABEL has been deleted. This can occur if a jump table
3689 is eliminated by optimization. If so, write a constant of zero
3690 instead. Note that this can also happen by turning the
3691 CODE_LABEL into a NOTE. */
3692 /* ??? This seems completely and utterly wrong. Certainly it's
3693 not true for NOTE_INSN_DELETED_LABEL, but I disbelieve proper
3694 functioning even with INSN_DELETED_P and friends. */
3696 tmp = x;
3697 switch (GET_CODE (tmp))
3699 case CONST:
3700 if (GET_CODE (XEXP (tmp, 0)) != PLUS
3701 || GET_CODE (XEXP (XEXP (tmp, 0), 0)) != LABEL_REF)
3702 break;
3703 tmp = XEXP (XEXP (tmp, 0), 0);
3704 /* FALLTHRU */
3706 case LABEL_REF:
3707 tmp = XEXP (tmp, 0);
3708 gcc_assert (!INSN_DELETED_P (tmp));
3709 gcc_assert (!NOTE_P (tmp)
3710 || NOTE_KIND (tmp) != NOTE_INSN_DELETED);
3711 break;
3713 default:
3714 break;
3717 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3718 ASM_OUTPUT_SPECIAL_POOL_ENTRY (asm_out_file, x, desc->mode,
3719 align, desc->labelno, done);
3720 #endif
3722 assemble_align (align);
3724 /* Output the label. */
3725 targetm.asm_out.internal_label (asm_out_file, "LC", desc->labelno);
3727 /* Output the data. */
3728 output_constant_pool_2 (desc->mode, x, align);
3730 /* Make sure all constants in SECTION_MERGE and not SECTION_STRINGS
3731 sections have proper size. */
3732 if (align > GET_MODE_BITSIZE (desc->mode)
3733 && in_section
3734 && (in_section->common.flags & SECTION_MERGE))
3735 assemble_align (align);
3737 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3738 done:
3739 #endif
3740 return;
3743 /* Given a SYMBOL_REF CURRENT_RTX, mark it and all constants it refers
3744 to as used. Emit referenced deferred strings. This function can
3745 be used with for_each_rtx to mark all SYMBOL_REFs in an rtx. */
3747 static int
3748 mark_constant (rtx *current_rtx, void *data ATTRIBUTE_UNUSED)
3750 rtx x = *current_rtx;
3752 if (x == NULL_RTX || GET_CODE (x) != SYMBOL_REF)
3753 return 0;
3755 if (CONSTANT_POOL_ADDRESS_P (x))
3757 struct constant_descriptor_rtx *desc = SYMBOL_REF_CONSTANT (x);
3758 if (desc->mark == 0)
3760 desc->mark = 1;
3761 for_each_rtx (&desc->constant, mark_constant, NULL);
3764 else if (TREE_CONSTANT_POOL_ADDRESS_P (x))
3766 tree decl = SYMBOL_REF_DECL (x);
3767 if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
3769 n_deferred_constants--;
3770 output_constant_def_contents (x);
3774 return -1;
3777 /* Look through appropriate parts of INSN, marking all entries in the
3778 constant pool which are actually being used. Entries that are only
3779 referenced by other constants are also marked as used. Emit
3780 deferred strings that are used. */
3782 static void
3783 mark_constants (rtx insn)
3785 if (!INSN_P (insn))
3786 return;
3788 /* Insns may appear inside a SEQUENCE. Only check the patterns of
3789 insns, not any notes that may be attached. We don't want to mark
3790 a constant just because it happens to appear in a REG_EQUIV note. */
3791 if (GET_CODE (PATTERN (insn)) == SEQUENCE)
3793 rtx seq = PATTERN (insn);
3794 int i, n = XVECLEN (seq, 0);
3795 for (i = 0; i < n; ++i)
3797 rtx subinsn = XVECEXP (seq, 0, i);
3798 if (INSN_P (subinsn))
3799 for_each_rtx (&PATTERN (subinsn), mark_constant, NULL);
3802 else
3803 for_each_rtx (&PATTERN (insn), mark_constant, NULL);
3806 /* Look through the instructions for this function, and mark all the
3807 entries in POOL which are actually being used. Emit deferred constants
3808 which have indeed been used. */
3810 static void
3811 mark_constant_pool (void)
3813 rtx insn, link;
3815 if (!crtl->uses_const_pool && n_deferred_constants == 0)
3816 return;
3818 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3819 mark_constants (insn);
3821 for (link = crtl->epilogue_delay_list;
3822 link;
3823 link = XEXP (link, 1))
3824 mark_constants (XEXP (link, 0));
3827 /* Write all the constants in POOL. */
3829 static void
3830 output_constant_pool_contents (struct rtx_constant_pool *pool)
3832 struct constant_descriptor_rtx *desc;
3834 for (desc = pool->first; desc ; desc = desc->next)
3835 if (desc->mark)
3837 /* If the constant is part of an object_block, make sure that
3838 the constant has been positioned within its block, but do not
3839 write out its definition yet. output_object_blocks will do
3840 that later. */
3841 if (SYMBOL_REF_HAS_BLOCK_INFO_P (desc->sym)
3842 && SYMBOL_REF_BLOCK (desc->sym))
3843 place_block_symbol (desc->sym);
3844 else
3846 switch_to_section (targetm.asm_out.select_rtx_section
3847 (desc->mode, desc->constant, desc->align));
3848 output_constant_pool_1 (desc, desc->align);
3853 /* Mark all constants that are used in the current function, then write
3854 out the function's private constant pool. */
3856 static void
3857 output_constant_pool (const char *fnname ATTRIBUTE_UNUSED,
3858 tree fndecl ATTRIBUTE_UNUSED)
3860 struct rtx_constant_pool *pool = crtl->varasm.pool;
3862 /* It is possible for gcc to call force_const_mem and then to later
3863 discard the instructions which refer to the constant. In such a
3864 case we do not need to output the constant. */
3865 mark_constant_pool ();
3867 #ifdef ASM_OUTPUT_POOL_PROLOGUE
3868 ASM_OUTPUT_POOL_PROLOGUE (asm_out_file, fnname, fndecl, pool->offset);
3869 #endif
3871 output_constant_pool_contents (pool);
3873 #ifdef ASM_OUTPUT_POOL_EPILOGUE
3874 ASM_OUTPUT_POOL_EPILOGUE (asm_out_file, fnname, fndecl, pool->offset);
3875 #endif
3878 /* Write the contents of the shared constant pool. */
3880 void
3881 output_shared_constant_pool (void)
3883 output_constant_pool_contents (shared_constant_pool);
3886 /* Determine what kind of relocations EXP may need. */
3889 compute_reloc_for_constant (tree exp)
3891 int reloc = 0, reloc2;
3892 tree tem;
3894 switch (TREE_CODE (exp))
3896 case ADDR_EXPR:
3897 case FDESC_EXPR:
3898 /* Go inside any operations that get_inner_reference can handle and see
3899 if what's inside is a constant: no need to do anything here for
3900 addresses of variables or functions. */
3901 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
3902 tem = TREE_OPERAND (tem, 0))
3905 if (TREE_PUBLIC (tem))
3906 reloc |= 2;
3907 else
3908 reloc |= 1;
3909 break;
3911 case PLUS_EXPR:
3912 case POINTER_PLUS_EXPR:
3913 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3914 reloc |= compute_reloc_for_constant (TREE_OPERAND (exp, 1));
3915 break;
3917 case MINUS_EXPR:
3918 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3919 reloc2 = compute_reloc_for_constant (TREE_OPERAND (exp, 1));
3920 /* The difference of two local labels is computable at link time. */
3921 if (reloc == 1 && reloc2 == 1)
3922 reloc = 0;
3923 else
3924 reloc |= reloc2;
3925 break;
3927 CASE_CONVERT:
3928 case VIEW_CONVERT_EXPR:
3929 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3930 break;
3932 case CONSTRUCTOR:
3934 unsigned HOST_WIDE_INT idx;
3935 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
3936 if (tem != 0)
3937 reloc |= compute_reloc_for_constant (tem);
3939 break;
3941 default:
3942 break;
3944 return reloc;
3947 /* Find all the constants whose addresses are referenced inside of EXP,
3948 and make sure assembler code with a label has been output for each one.
3949 Indicate whether an ADDR_EXPR has been encountered. */
3951 static void
3952 output_addressed_constants (tree exp)
3954 tree tem;
3956 switch (TREE_CODE (exp))
3958 case ADDR_EXPR:
3959 case FDESC_EXPR:
3960 /* Go inside any operations that get_inner_reference can handle and see
3961 if what's inside is a constant: no need to do anything here for
3962 addresses of variables or functions. */
3963 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
3964 tem = TREE_OPERAND (tem, 0))
3967 /* If we have an initialized CONST_DECL, retrieve the initializer. */
3968 if (TREE_CODE (tem) == CONST_DECL && DECL_INITIAL (tem))
3969 tem = DECL_INITIAL (tem);
3971 if (CONSTANT_CLASS_P (tem) || TREE_CODE (tem) == CONSTRUCTOR)
3972 output_constant_def (tem, 0);
3973 break;
3975 case PLUS_EXPR:
3976 case POINTER_PLUS_EXPR:
3977 case MINUS_EXPR:
3978 output_addressed_constants (TREE_OPERAND (exp, 1));
3979 /* Fall through. */
3981 CASE_CONVERT:
3982 case VIEW_CONVERT_EXPR:
3983 output_addressed_constants (TREE_OPERAND (exp, 0));
3984 break;
3986 case CONSTRUCTOR:
3988 unsigned HOST_WIDE_INT idx;
3989 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
3990 if (tem != 0)
3991 output_addressed_constants (tem);
3993 break;
3995 default:
3996 break;
4000 /* Whether a constructor CTOR is a valid static constant initializer if all
4001 its elements are. This used to be internal to initializer_constant_valid_p
4002 and has been exposed to let other functions like categorize_ctor_elements
4003 evaluate the property while walking a constructor for other purposes. */
4005 bool
4006 constructor_static_from_elts_p (const_tree ctor)
4008 return (TREE_CONSTANT (ctor)
4009 && (TREE_CODE (TREE_TYPE (ctor)) == UNION_TYPE
4010 || TREE_CODE (TREE_TYPE (ctor)) == RECORD_TYPE
4011 || TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE));
4014 static tree initializer_constant_valid_p_1 (tree value, tree endtype,
4015 tree *cache);
4017 /* A subroutine of initializer_constant_valid_p. VALUE is a MINUS_EXPR,
4018 PLUS_EXPR or POINTER_PLUS_EXPR. This looks for cases of VALUE
4019 which are valid when ENDTYPE is an integer of any size; in
4020 particular, this does not accept a pointer minus a constant. This
4021 returns null_pointer_node if the VALUE is an absolute constant
4022 which can be used to initialize a static variable. Otherwise it
4023 returns NULL. */
4025 static tree
4026 narrowing_initializer_constant_valid_p (tree value, tree endtype, tree *cache)
4028 tree op0, op1;
4030 if (!INTEGRAL_TYPE_P (endtype))
4031 return NULL_TREE;
4033 op0 = TREE_OPERAND (value, 0);
4034 op1 = TREE_OPERAND (value, 1);
4036 /* Like STRIP_NOPS except allow the operand mode to widen. This
4037 works around a feature of fold that simplifies (int)(p1 - p2) to
4038 ((int)p1 - (int)p2) under the theory that the narrower operation
4039 is cheaper. */
4041 while (CONVERT_EXPR_P (op0)
4042 || TREE_CODE (op0) == NON_LVALUE_EXPR)
4044 tree inner = TREE_OPERAND (op0, 0);
4045 if (inner == error_mark_node
4046 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4047 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))
4048 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
4049 break;
4050 op0 = inner;
4053 while (CONVERT_EXPR_P (op1)
4054 || TREE_CODE (op1) == NON_LVALUE_EXPR)
4056 tree inner = TREE_OPERAND (op1, 0);
4057 if (inner == error_mark_node
4058 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4059 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op1)))
4060 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
4061 break;
4062 op1 = inner;
4065 op0 = initializer_constant_valid_p_1 (op0, endtype, cache);
4066 if (!op0)
4067 return NULL_TREE;
4069 op1 = initializer_constant_valid_p_1 (op1, endtype,
4070 cache ? cache + 2 : NULL);
4071 /* Both initializers must be known. */
4072 if (op1)
4074 if (op0 == op1
4075 && (op0 == null_pointer_node
4076 || TREE_CODE (value) == MINUS_EXPR))
4077 return null_pointer_node;
4079 /* Support differences between labels. */
4080 if (TREE_CODE (op0) == LABEL_DECL
4081 && TREE_CODE (op1) == LABEL_DECL)
4082 return null_pointer_node;
4084 if (TREE_CODE (op0) == STRING_CST
4085 && TREE_CODE (op1) == STRING_CST
4086 && operand_equal_p (op0, op1, 1))
4087 return null_pointer_node;
4090 return NULL_TREE;
4093 /* Helper function of initializer_constant_valid_p.
4094 Return nonzero if VALUE is a valid constant-valued expression
4095 for use in initializing a static variable; one that can be an
4096 element of a "constant" initializer.
4098 Return null_pointer_node if the value is absolute;
4099 if it is relocatable, return the variable that determines the relocation.
4100 We assume that VALUE has been folded as much as possible;
4101 therefore, we do not need to check for such things as
4102 arithmetic-combinations of integers.
4104 Use CACHE (pointer to 2 tree values) for caching if non-NULL. */
4106 static tree
4107 initializer_constant_valid_p_1 (tree value, tree endtype, tree *cache)
4109 tree ret;
4111 switch (TREE_CODE (value))
4113 case CONSTRUCTOR:
4114 if (constructor_static_from_elts_p (value))
4116 unsigned HOST_WIDE_INT idx;
4117 tree elt;
4118 bool absolute = true;
4120 if (cache && cache[0] == value)
4121 return cache[1];
4122 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
4124 tree reloc;
4125 reloc = initializer_constant_valid_p_1 (elt, TREE_TYPE (elt),
4126 NULL);
4127 if (!reloc)
4129 if (cache)
4131 cache[0] = value;
4132 cache[1] = NULL_TREE;
4134 return NULL_TREE;
4136 if (reloc != null_pointer_node)
4137 absolute = false;
4139 /* For a non-absolute relocation, there is no single
4140 variable that can be "the variable that determines the
4141 relocation." */
4142 if (cache)
4144 cache[0] = value;
4145 cache[1] = absolute ? null_pointer_node : error_mark_node;
4147 return absolute ? null_pointer_node : error_mark_node;
4150 return TREE_STATIC (value) ? null_pointer_node : NULL_TREE;
4152 case INTEGER_CST:
4153 case VECTOR_CST:
4154 case REAL_CST:
4155 case FIXED_CST:
4156 case STRING_CST:
4157 case COMPLEX_CST:
4158 return null_pointer_node;
4160 case ADDR_EXPR:
4161 case FDESC_EXPR:
4163 tree op0 = staticp (TREE_OPERAND (value, 0));
4164 if (op0)
4166 /* "&(*a).f" is like unto pointer arithmetic. If "a" turns out
4167 to be a constant, this is old-skool offsetof-like nonsense. */
4168 if (TREE_CODE (op0) == INDIRECT_REF
4169 && TREE_CONSTANT (TREE_OPERAND (op0, 0)))
4170 return null_pointer_node;
4171 /* Taking the address of a nested function involves a trampoline,
4172 unless we don't need or want one. */
4173 if (TREE_CODE (op0) == FUNCTION_DECL
4174 && DECL_STATIC_CHAIN (op0)
4175 && !TREE_NO_TRAMPOLINE (value))
4176 return NULL_TREE;
4177 /* "&{...}" requires a temporary to hold the constructed
4178 object. */
4179 if (TREE_CODE (op0) == CONSTRUCTOR)
4180 return NULL_TREE;
4182 return op0;
4185 case NON_LVALUE_EXPR:
4186 return initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4187 endtype, cache);
4189 case VIEW_CONVERT_EXPR:
4191 tree src = TREE_OPERAND (value, 0);
4192 tree src_type = TREE_TYPE (src);
4193 tree dest_type = TREE_TYPE (value);
4195 /* Allow view-conversions from aggregate to non-aggregate type only
4196 if the bit pattern is fully preserved afterwards; otherwise, the
4197 RTL expander won't be able to apply a subsequent transformation
4198 to the underlying constructor. */
4199 if (AGGREGATE_TYPE_P (src_type) && !AGGREGATE_TYPE_P (dest_type))
4201 if (TYPE_MODE (endtype) == TYPE_MODE (dest_type))
4202 return initializer_constant_valid_p_1 (src, endtype, cache);
4203 else
4204 return NULL_TREE;
4207 /* Allow all other kinds of view-conversion. */
4208 return initializer_constant_valid_p_1 (src, endtype, cache);
4211 CASE_CONVERT:
4213 tree src = TREE_OPERAND (value, 0);
4214 tree src_type = TREE_TYPE (src);
4215 tree dest_type = TREE_TYPE (value);
4217 /* Allow conversions between pointer types, floating-point
4218 types, and offset types. */
4219 if ((POINTER_TYPE_P (dest_type) && POINTER_TYPE_P (src_type))
4220 || (FLOAT_TYPE_P (dest_type) && FLOAT_TYPE_P (src_type))
4221 || (TREE_CODE (dest_type) == OFFSET_TYPE
4222 && TREE_CODE (src_type) == OFFSET_TYPE))
4223 return initializer_constant_valid_p_1 (src, endtype, cache);
4225 /* Allow length-preserving conversions between integer types. */
4226 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type)
4227 && (TYPE_PRECISION (dest_type) == TYPE_PRECISION (src_type)))
4228 return initializer_constant_valid_p_1 (src, endtype, cache);
4230 /* Allow conversions between other integer types only if
4231 explicit value. */
4232 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type))
4234 tree inner = initializer_constant_valid_p_1 (src, endtype, cache);
4235 if (inner == null_pointer_node)
4236 return null_pointer_node;
4237 break;
4240 /* Allow (int) &foo provided int is as wide as a pointer. */
4241 if (INTEGRAL_TYPE_P (dest_type) && POINTER_TYPE_P (src_type)
4242 && (TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type)))
4243 return initializer_constant_valid_p_1 (src, endtype, cache);
4245 /* Likewise conversions from int to pointers, but also allow
4246 conversions from 0. */
4247 if ((POINTER_TYPE_P (dest_type)
4248 || TREE_CODE (dest_type) == OFFSET_TYPE)
4249 && INTEGRAL_TYPE_P (src_type))
4251 if (TREE_CODE (src) == INTEGER_CST
4252 && TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type))
4253 return null_pointer_node;
4254 if (integer_zerop (src))
4255 return null_pointer_node;
4256 else if (TYPE_PRECISION (dest_type) <= TYPE_PRECISION (src_type))
4257 return initializer_constant_valid_p_1 (src, endtype, cache);
4260 /* Allow conversions to struct or union types if the value
4261 inside is okay. */
4262 if (TREE_CODE (dest_type) == RECORD_TYPE
4263 || TREE_CODE (dest_type) == UNION_TYPE)
4264 return initializer_constant_valid_p_1 (src, endtype, cache);
4266 break;
4268 case POINTER_PLUS_EXPR:
4269 case PLUS_EXPR:
4270 /* Any valid floating-point constants will have been folded by now;
4271 with -frounding-math we hit this with addition of two constants. */
4272 if (TREE_CODE (endtype) == REAL_TYPE)
4273 return NULL_TREE;
4274 if (cache && cache[0] == value)
4275 return cache[1];
4276 if (! INTEGRAL_TYPE_P (endtype)
4277 || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
4279 tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
4280 tree valid0
4281 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4282 endtype, ncache);
4283 tree valid1
4284 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
4285 endtype, ncache + 2);
4286 /* If either term is absolute, use the other term's relocation. */
4287 if (valid0 == null_pointer_node)
4288 ret = valid1;
4289 else if (valid1 == null_pointer_node)
4290 ret = valid0;
4291 /* Support narrowing pointer differences. */
4292 else
4293 ret = narrowing_initializer_constant_valid_p (value, endtype,
4294 ncache);
4296 else
4297 /* Support narrowing pointer differences. */
4298 ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
4299 if (cache)
4301 cache[0] = value;
4302 cache[1] = ret;
4304 return ret;
4306 case MINUS_EXPR:
4307 if (TREE_CODE (endtype) == REAL_TYPE)
4308 return NULL_TREE;
4309 if (cache && cache[0] == value)
4310 return cache[1];
4311 if (! INTEGRAL_TYPE_P (endtype)
4312 || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
4314 tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
4315 tree valid0
4316 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4317 endtype, ncache);
4318 tree valid1
4319 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
4320 endtype, ncache + 2);
4321 /* Win if second argument is absolute. */
4322 if (valid1 == null_pointer_node)
4323 ret = valid0;
4324 /* Win if both arguments have the same relocation.
4325 Then the value is absolute. */
4326 else if (valid0 == valid1 && valid0 != 0)
4327 ret = null_pointer_node;
4328 /* Since GCC guarantees that string constants are unique in the
4329 generated code, a subtraction between two copies of the same
4330 constant string is absolute. */
4331 else if (valid0 && TREE_CODE (valid0) == STRING_CST
4332 && valid1 && TREE_CODE (valid1) == STRING_CST
4333 && operand_equal_p (valid0, valid1, 1))
4334 ret = null_pointer_node;
4335 /* Support narrowing differences. */
4336 else
4337 ret = narrowing_initializer_constant_valid_p (value, endtype,
4338 ncache);
4340 else
4341 /* Support narrowing differences. */
4342 ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
4343 if (cache)
4345 cache[0] = value;
4346 cache[1] = ret;
4348 return ret;
4350 default:
4351 break;
4354 return NULL_TREE;
4357 /* Return nonzero if VALUE is a valid constant-valued expression
4358 for use in initializing a static variable; one that can be an
4359 element of a "constant" initializer.
4361 Return null_pointer_node if the value is absolute;
4362 if it is relocatable, return the variable that determines the relocation.
4363 We assume that VALUE has been folded as much as possible;
4364 therefore, we do not need to check for such things as
4365 arithmetic-combinations of integers. */
4366 tree
4367 initializer_constant_valid_p (tree value, tree endtype)
4369 return initializer_constant_valid_p_1 (value, endtype, NULL);
4372 /* Return true if VALUE is a valid constant-valued expression
4373 for use in initializing a static bit-field; one that can be
4374 an element of a "constant" initializer. */
4376 bool
4377 initializer_constant_valid_for_bitfield_p (tree value)
4379 /* For bitfields we support integer constants or possibly nested aggregates
4380 of such. */
4381 switch (TREE_CODE (value))
4383 case CONSTRUCTOR:
4385 unsigned HOST_WIDE_INT idx;
4386 tree elt;
4388 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
4389 if (!initializer_constant_valid_for_bitfield_p (elt))
4390 return false;
4391 return true;
4394 case INTEGER_CST:
4395 return true;
4397 case VIEW_CONVERT_EXPR:
4398 case NON_LVALUE_EXPR:
4399 return
4400 initializer_constant_valid_for_bitfield_p (TREE_OPERAND (value, 0));
4402 default:
4403 break;
4406 return false;
4409 /* output_constructor outer state of relevance in recursive calls, typically
4410 for nested aggregate bitfields. */
4412 typedef struct {
4413 unsigned int bit_offset; /* current position in ... */
4414 int byte; /* ... the outer byte buffer. */
4415 } oc_outer_state;
4417 static unsigned HOST_WIDE_INT
4418 output_constructor (tree, unsigned HOST_WIDE_INT, unsigned int,
4419 oc_outer_state *);
4421 /* Output assembler code for constant EXP to FILE, with no label.
4422 This includes the pseudo-op such as ".int" or ".byte", and a newline.
4423 Assumes output_addressed_constants has been done on EXP already.
4425 Generate exactly SIZE bytes of assembler data, padding at the end
4426 with zeros if necessary. SIZE must always be specified.
4428 SIZE is important for structure constructors,
4429 since trailing members may have been omitted from the constructor.
4430 It is also important for initialization of arrays from string constants
4431 since the full length of the string constant might not be wanted.
4432 It is also needed for initialization of unions, where the initializer's
4433 type is just one member, and that may not be as long as the union.
4435 There a case in which we would fail to output exactly SIZE bytes:
4436 for a structure constructor that wants to produce more than SIZE bytes.
4437 But such constructors will never be generated for any possible input.
4439 ALIGN is the alignment of the data in bits. */
4441 void
4442 output_constant (tree exp, unsigned HOST_WIDE_INT size, unsigned int align)
4444 enum tree_code code;
4445 unsigned HOST_WIDE_INT thissize;
4447 if (size == 0 || flag_syntax_only)
4448 return;
4450 /* See if we're trying to initialize a pointer in a non-default mode
4451 to the address of some declaration somewhere. If the target says
4452 the mode is valid for pointers, assume the target has a way of
4453 resolving it. */
4454 if (TREE_CODE (exp) == NOP_EXPR
4455 && POINTER_TYPE_P (TREE_TYPE (exp))
4456 && targetm.addr_space.valid_pointer_mode
4457 (TYPE_MODE (TREE_TYPE (exp)),
4458 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
4460 tree saved_type = TREE_TYPE (exp);
4462 /* Peel off any intermediate conversions-to-pointer for valid
4463 pointer modes. */
4464 while (TREE_CODE (exp) == NOP_EXPR
4465 && POINTER_TYPE_P (TREE_TYPE (exp))
4466 && targetm.addr_space.valid_pointer_mode
4467 (TYPE_MODE (TREE_TYPE (exp)),
4468 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
4469 exp = TREE_OPERAND (exp, 0);
4471 /* If what we're left with is the address of something, we can
4472 convert the address to the final type and output it that
4473 way. */
4474 if (TREE_CODE (exp) == ADDR_EXPR)
4475 exp = build1 (ADDR_EXPR, saved_type, TREE_OPERAND (exp, 0));
4476 /* Likewise for constant ints. */
4477 else if (TREE_CODE (exp) == INTEGER_CST)
4478 exp = build_int_cst_wide (saved_type, TREE_INT_CST_LOW (exp),
4479 TREE_INT_CST_HIGH (exp));
4483 /* Eliminate any conversions since we'll be outputting the underlying
4484 constant. */
4485 while (CONVERT_EXPR_P (exp)
4486 || TREE_CODE (exp) == NON_LVALUE_EXPR
4487 || TREE_CODE (exp) == VIEW_CONVERT_EXPR)
4489 HOST_WIDE_INT type_size = int_size_in_bytes (TREE_TYPE (exp));
4490 HOST_WIDE_INT op_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (exp, 0)));
4492 /* Make sure eliminating the conversion is really a no-op, except with
4493 VIEW_CONVERT_EXPRs to allow for wild Ada unchecked conversions and
4494 union types to allow for Ada unchecked unions. */
4495 if (type_size > op_size
4496 && TREE_CODE (exp) != VIEW_CONVERT_EXPR
4497 && TREE_CODE (TREE_TYPE (exp)) != UNION_TYPE)
4498 /* Keep the conversion. */
4499 break;
4500 else
4501 exp = TREE_OPERAND (exp, 0);
4504 code = TREE_CODE (TREE_TYPE (exp));
4505 thissize = int_size_in_bytes (TREE_TYPE (exp));
4507 /* Allow a constructor with no elements for any data type.
4508 This means to fill the space with zeros. */
4509 if (TREE_CODE (exp) == CONSTRUCTOR
4510 && VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (exp)))
4512 assemble_zeros (size);
4513 return;
4516 if (TREE_CODE (exp) == FDESC_EXPR)
4518 #ifdef ASM_OUTPUT_FDESC
4519 HOST_WIDE_INT part = tree_low_cst (TREE_OPERAND (exp, 1), 0);
4520 tree decl = TREE_OPERAND (exp, 0);
4521 ASM_OUTPUT_FDESC (asm_out_file, decl, part);
4522 #else
4523 gcc_unreachable ();
4524 #endif
4525 return;
4528 /* Now output the underlying data. If we've handling the padding, return.
4529 Otherwise, break and ensure SIZE is the size written. */
4530 switch (code)
4532 case BOOLEAN_TYPE:
4533 case INTEGER_TYPE:
4534 case ENUMERAL_TYPE:
4535 case POINTER_TYPE:
4536 case REFERENCE_TYPE:
4537 case OFFSET_TYPE:
4538 case FIXED_POINT_TYPE:
4539 if (! assemble_integer (expand_expr (exp, NULL_RTX, VOIDmode,
4540 EXPAND_INITIALIZER),
4541 MIN (size, thissize), align, 0))
4542 error ("initializer for integer/fixed-point value is too complicated");
4543 break;
4545 case REAL_TYPE:
4546 if (TREE_CODE (exp) != REAL_CST)
4547 error ("initializer for floating value is not a floating constant");
4548 else
4549 assemble_real (TREE_REAL_CST (exp), TYPE_MODE (TREE_TYPE (exp)), align);
4550 break;
4552 case COMPLEX_TYPE:
4553 output_constant (TREE_REALPART (exp), thissize / 2, align);
4554 output_constant (TREE_IMAGPART (exp), thissize / 2,
4555 min_align (align, BITS_PER_UNIT * (thissize / 2)));
4556 break;
4558 case ARRAY_TYPE:
4559 case VECTOR_TYPE:
4560 switch (TREE_CODE (exp))
4562 case CONSTRUCTOR:
4563 output_constructor (exp, size, align, NULL);
4564 return;
4565 case STRING_CST:
4566 thissize = MIN ((unsigned HOST_WIDE_INT)TREE_STRING_LENGTH (exp),
4567 size);
4568 assemble_string (TREE_STRING_POINTER (exp), thissize);
4569 break;
4571 case VECTOR_CST:
4573 int elt_size;
4574 tree link;
4575 unsigned int nalign;
4576 enum machine_mode inner;
4578 inner = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
4579 nalign = MIN (align, GET_MODE_ALIGNMENT (inner));
4581 elt_size = GET_MODE_SIZE (inner);
4583 link = TREE_VECTOR_CST_ELTS (exp);
4584 output_constant (TREE_VALUE (link), elt_size, align);
4585 thissize = elt_size;
4586 while ((link = TREE_CHAIN (link)) != NULL)
4588 output_constant (TREE_VALUE (link), elt_size, nalign);
4589 thissize += elt_size;
4591 break;
4593 default:
4594 gcc_unreachable ();
4596 break;
4598 case RECORD_TYPE:
4599 case UNION_TYPE:
4600 gcc_assert (TREE_CODE (exp) == CONSTRUCTOR);
4601 output_constructor (exp, size, align, NULL);
4602 return;
4604 case ERROR_MARK:
4605 return;
4607 default:
4608 gcc_unreachable ();
4611 if (size > thissize)
4612 assemble_zeros (size - thissize);
4616 /* Subroutine of output_constructor, used for computing the size of
4617 arrays of unspecified length. VAL must be a CONSTRUCTOR of an array
4618 type with an unspecified upper bound. */
4620 static unsigned HOST_WIDE_INT
4621 array_size_for_constructor (tree val)
4623 tree max_index, i;
4624 unsigned HOST_WIDE_INT cnt;
4625 tree index, value, tmp;
4627 /* This code used to attempt to handle string constants that are not
4628 arrays of single-bytes, but nothing else does, so there's no point in
4629 doing it here. */
4630 if (TREE_CODE (val) == STRING_CST)
4631 return TREE_STRING_LENGTH (val);
4633 max_index = NULL_TREE;
4634 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (val), cnt, index, value)
4636 if (TREE_CODE (index) == RANGE_EXPR)
4637 index = TREE_OPERAND (index, 1);
4638 if (max_index == NULL_TREE || tree_int_cst_lt (max_index, index))
4639 max_index = index;
4642 if (max_index == NULL_TREE)
4643 return 0;
4645 /* Compute the total number of array elements. */
4646 tmp = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (val)));
4647 i = size_binop (MINUS_EXPR, fold_convert (sizetype, max_index),
4648 fold_convert (sizetype, tmp));
4649 i = size_binop (PLUS_EXPR, i, size_one_node);
4651 /* Multiply by the array element unit size to find number of bytes. */
4652 i = size_binop (MULT_EXPR, i, TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (val))));
4654 return tree_low_cst (i, 1);
4657 /* Other datastructures + helpers for output_constructor. */
4659 /* output_constructor local state to support interaction with helpers. */
4661 typedef struct {
4663 /* Received arguments. */
4664 tree exp; /* Constructor expression. */
4665 unsigned HOST_WIDE_INT size; /* # bytes to output - pad if necessary. */
4666 unsigned int align; /* Known initial alignment. */
4668 /* Constructor expression data. */
4669 tree type; /* Expression type. */
4670 tree field; /* Current field decl in a record. */
4671 tree min_index; /* Lower bound if specified for an array. */
4673 /* Output processing state. */
4674 HOST_WIDE_INT total_bytes; /* # bytes output so far / current position. */
4675 bool byte_buffer_in_use; /* Whether byte ... */
4676 int byte; /* ... contains part of a bitfield byte yet to
4677 be output. */
4679 int last_relative_index; /* Implicit or explicit index of the last
4680 array element output within a bitfield. */
4681 /* Current element. */
4682 tree val; /* Current element value. */
4683 tree index; /* Current element index. */
4685 } oc_local_state;
4687 /* Helper for output_constructor. From the current LOCAL state, output a
4688 RANGE_EXPR element. */
4690 static void
4691 output_constructor_array_range (oc_local_state *local)
4693 unsigned HOST_WIDE_INT fieldsize
4694 = int_size_in_bytes (TREE_TYPE (local->type));
4696 HOST_WIDE_INT lo_index
4697 = tree_low_cst (TREE_OPERAND (local->index, 0), 0);
4698 HOST_WIDE_INT hi_index
4699 = tree_low_cst (TREE_OPERAND (local->index, 1), 0);
4700 HOST_WIDE_INT index;
4702 unsigned int align2
4703 = min_align (local->align, fieldsize * BITS_PER_UNIT);
4705 for (index = lo_index; index <= hi_index; index++)
4707 /* Output the element's initial value. */
4708 if (local->val == NULL_TREE)
4709 assemble_zeros (fieldsize);
4710 else
4711 output_constant (local->val, fieldsize, align2);
4713 /* Count its size. */
4714 local->total_bytes += fieldsize;
4718 /* Helper for output_constructor. From the current LOCAL state, output a
4719 field element that is not true bitfield or part of an outer one. */
4721 static void
4722 output_constructor_regular_field (oc_local_state *local)
4724 /* Field size and position. Since this structure is static, we know the
4725 positions are constant. */
4726 unsigned HOST_WIDE_INT fieldsize;
4727 HOST_WIDE_INT fieldpos;
4729 unsigned int align2;
4731 if (local->index != NULL_TREE)
4733 double_int idx = double_int_sub (tree_to_double_int (local->index),
4734 tree_to_double_int (local->min_index));
4735 gcc_assert (double_int_fits_in_shwi_p (idx));
4736 fieldpos = (tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (local->val)), 1)
4737 * idx.low);
4739 else if (local->field != NULL_TREE)
4740 fieldpos = int_byte_position (local->field);
4741 else
4742 fieldpos = 0;
4744 /* Output any buffered-up bit-fields preceding this element. */
4745 if (local->byte_buffer_in_use)
4747 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
4748 local->total_bytes++;
4749 local->byte_buffer_in_use = false;
4752 /* Advance to offset of this element.
4753 Note no alignment needed in an array, since that is guaranteed
4754 if each element has the proper size. */
4755 if ((local->field != NULL_TREE || local->index != NULL_TREE)
4756 && fieldpos != local->total_bytes)
4758 gcc_assert (fieldpos >= local->total_bytes);
4759 assemble_zeros (fieldpos - local->total_bytes);
4760 local->total_bytes = fieldpos;
4763 /* Find the alignment of this element. */
4764 align2 = min_align (local->align, BITS_PER_UNIT * fieldpos);
4766 /* Determine size this element should occupy. */
4767 if (local->field)
4769 fieldsize = 0;
4771 /* If this is an array with an unspecified upper bound,
4772 the initializer determines the size. */
4773 /* ??? This ought to only checked if DECL_SIZE_UNIT is NULL,
4774 but we cannot do this until the deprecated support for
4775 initializing zero-length array members is removed. */
4776 if (TREE_CODE (TREE_TYPE (local->field)) == ARRAY_TYPE
4777 && TYPE_DOMAIN (TREE_TYPE (local->field))
4778 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (local->field))))
4780 fieldsize = array_size_for_constructor (local->val);
4781 /* Given a non-empty initialization, this field had
4782 better be last. */
4783 gcc_assert (!fieldsize || !DECL_CHAIN (local->field));
4785 else
4786 fieldsize = tree_low_cst (DECL_SIZE_UNIT (local->field), 1);
4788 else
4789 fieldsize = int_size_in_bytes (TREE_TYPE (local->type));
4791 /* Output the element's initial value. */
4792 if (local->val == NULL_TREE)
4793 assemble_zeros (fieldsize);
4794 else
4795 output_constant (local->val, fieldsize, align2);
4797 /* Count its size. */
4798 local->total_bytes += fieldsize;
4801 /* Helper for output_constructor. From the current LOCAL and OUTER states,
4802 output an element that is a true bitfield or part of an outer one. */
4804 static void
4805 output_constructor_bitfield (oc_local_state *local, oc_outer_state *outer)
4807 /* Bit size of this element. */
4808 HOST_WIDE_INT ebitsize
4809 = (local->field
4810 ? tree_low_cst (DECL_SIZE (local->field), 1)
4811 : tree_low_cst (TYPE_SIZE (TREE_TYPE (local->type)), 1));
4813 /* Relative index of this element if this is an array component. */
4814 HOST_WIDE_INT relative_index
4815 = (!local->field
4816 ? (local->index
4817 ? (tree_low_cst (local->index, 0)
4818 - tree_low_cst (local->min_index, 0))
4819 : local->last_relative_index + 1)
4820 : 0);
4822 /* Bit position of this element from the start of the containing
4823 constructor. */
4824 HOST_WIDE_INT constructor_relative_ebitpos
4825 = (local->field
4826 ? int_bit_position (local->field)
4827 : ebitsize * relative_index);
4829 /* Bit position of this element from the start of a possibly ongoing
4830 outer byte buffer. */
4831 HOST_WIDE_INT byte_relative_ebitpos
4832 = ((outer ? outer->bit_offset : 0) + constructor_relative_ebitpos);
4834 /* From the start of a possibly ongoing outer byte buffer, offsets to
4835 the first bit of this element and to the first bit past the end of
4836 this element. */
4837 HOST_WIDE_INT next_offset = byte_relative_ebitpos;
4838 HOST_WIDE_INT end_offset = byte_relative_ebitpos + ebitsize;
4840 local->last_relative_index = relative_index;
4842 if (local->val == NULL_TREE)
4843 local->val = integer_zero_node;
4845 while (TREE_CODE (local->val) == VIEW_CONVERT_EXPR
4846 || TREE_CODE (local->val) == NON_LVALUE_EXPR)
4847 local->val = TREE_OPERAND (local->val, 0);
4849 if (TREE_CODE (local->val) != INTEGER_CST
4850 && TREE_CODE (local->val) != CONSTRUCTOR)
4852 error ("invalid initial value for member %qE", DECL_NAME (local->field));
4853 return;
4856 /* If this field does not start in this (or, next) byte,
4857 skip some bytes. */
4858 if (next_offset / BITS_PER_UNIT != local->total_bytes)
4860 /* Output remnant of any bit field in previous bytes. */
4861 if (local->byte_buffer_in_use)
4863 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
4864 local->total_bytes++;
4865 local->byte_buffer_in_use = false;
4868 /* If still not at proper byte, advance to there. */
4869 if (next_offset / BITS_PER_UNIT != local->total_bytes)
4871 gcc_assert (next_offset / BITS_PER_UNIT >= local->total_bytes);
4872 assemble_zeros (next_offset / BITS_PER_UNIT - local->total_bytes);
4873 local->total_bytes = next_offset / BITS_PER_UNIT;
4877 /* Set up the buffer if necessary. */
4878 if (!local->byte_buffer_in_use)
4880 local->byte = 0;
4881 if (ebitsize > 0)
4882 local->byte_buffer_in_use = true;
4885 /* If this is nested constructor, recurse passing the bit offset and the
4886 pending data, then retrieve the new pending data afterwards. */
4887 if (TREE_CODE (local->val) == CONSTRUCTOR)
4889 oc_outer_state output_state;
4891 output_state.bit_offset = next_offset % BITS_PER_UNIT;
4892 output_state.byte = local->byte;
4893 local->total_bytes
4894 += output_constructor (local->val, 0, 0, &output_state);
4895 local->byte = output_state.byte;
4896 return;
4899 /* Otherwise, we must split the element into pieces that fall within
4900 separate bytes, and combine each byte with previous or following
4901 bit-fields. */
4902 while (next_offset < end_offset)
4904 int this_time;
4905 int shift;
4906 HOST_WIDE_INT value;
4907 HOST_WIDE_INT next_byte = next_offset / BITS_PER_UNIT;
4908 HOST_WIDE_INT next_bit = next_offset % BITS_PER_UNIT;
4910 /* Advance from byte to byte
4911 within this element when necessary. */
4912 while (next_byte != local->total_bytes)
4914 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
4915 local->total_bytes++;
4916 local->byte = 0;
4919 /* Number of bits we can process at once
4920 (all part of the same byte). */
4921 this_time = MIN (end_offset - next_offset,
4922 BITS_PER_UNIT - next_bit);
4923 if (BYTES_BIG_ENDIAN)
4925 /* On big-endian machine, take the most significant bits
4926 first (of the bits that are significant)
4927 and put them into bytes from the most significant end. */
4928 shift = end_offset - next_offset - this_time;
4930 /* Don't try to take a bunch of bits that cross
4931 the word boundary in the INTEGER_CST. We can
4932 only select bits from the LOW or HIGH part
4933 not from both. */
4934 if (shift < HOST_BITS_PER_WIDE_INT
4935 && shift + this_time > HOST_BITS_PER_WIDE_INT)
4937 this_time = shift + this_time - HOST_BITS_PER_WIDE_INT;
4938 shift = HOST_BITS_PER_WIDE_INT;
4941 /* Now get the bits from the appropriate constant word. */
4942 if (shift < HOST_BITS_PER_WIDE_INT)
4943 value = TREE_INT_CST_LOW (local->val);
4944 else
4946 gcc_assert (shift < 2 * HOST_BITS_PER_WIDE_INT);
4947 value = TREE_INT_CST_HIGH (local->val);
4948 shift -= HOST_BITS_PER_WIDE_INT;
4951 /* Get the result. This works only when:
4952 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
4953 local->byte |= (((value >> shift)
4954 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
4955 << (BITS_PER_UNIT - this_time - next_bit));
4957 else
4959 /* On little-endian machines,
4960 take first the least significant bits of the value
4961 and pack them starting at the least significant
4962 bits of the bytes. */
4963 shift = next_offset - byte_relative_ebitpos;
4965 /* Don't try to take a bunch of bits that cross
4966 the word boundary in the INTEGER_CST. We can
4967 only select bits from the LOW or HIGH part
4968 not from both. */
4969 if (shift < HOST_BITS_PER_WIDE_INT
4970 && shift + this_time > HOST_BITS_PER_WIDE_INT)
4971 this_time = (HOST_BITS_PER_WIDE_INT - shift);
4973 /* Now get the bits from the appropriate constant word. */
4974 if (shift < HOST_BITS_PER_WIDE_INT)
4975 value = TREE_INT_CST_LOW (local->val);
4976 else
4978 gcc_assert (shift < 2 * HOST_BITS_PER_WIDE_INT);
4979 value = TREE_INT_CST_HIGH (local->val);
4980 shift -= HOST_BITS_PER_WIDE_INT;
4983 /* Get the result. This works only when:
4984 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
4985 local->byte |= (((value >> shift)
4986 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
4987 << next_bit);
4990 next_offset += this_time;
4991 local->byte_buffer_in_use = true;
4995 /* Subroutine of output_constant, used for CONSTRUCTORs (aggregate constants).
4996 Generate at least SIZE bytes, padding if necessary. OUTER designates the
4997 caller output state of relevance in recursive invocations. */
4999 static unsigned HOST_WIDE_INT
5000 output_constructor (tree exp, unsigned HOST_WIDE_INT size,
5001 unsigned int align, oc_outer_state * outer)
5003 unsigned HOST_WIDE_INT cnt;
5004 constructor_elt *ce;
5006 oc_local_state local;
5008 /* Setup our local state to communicate with helpers. */
5009 local.exp = exp;
5010 local.size = size;
5011 local.align = align;
5013 local.total_bytes = 0;
5014 local.byte_buffer_in_use = outer != NULL;
5015 local.byte = outer ? outer->byte : 0;
5017 local.type = TREE_TYPE (exp);
5019 local.last_relative_index = -1;
5021 local.min_index = NULL_TREE;
5022 if (TREE_CODE (local.type) == ARRAY_TYPE
5023 && TYPE_DOMAIN (local.type) != NULL_TREE)
5024 local.min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (local.type));
5026 gcc_assert (HOST_BITS_PER_WIDE_INT >= BITS_PER_UNIT);
5028 /* As CE goes through the elements of the constant, FIELD goes through the
5029 structure fields if the constant is a structure. If the constant is a
5030 union, we override this by getting the field from the TREE_LIST element.
5031 But the constant could also be an array. Then FIELD is zero.
5033 There is always a maximum of one element in the chain LINK for unions
5034 (even if the initializer in a source program incorrectly contains
5035 more one). */
5037 local.field = NULL_TREE;
5038 if (TREE_CODE (local.type) == RECORD_TYPE)
5039 local.field = TYPE_FIELDS (local.type);
5041 for (cnt = 0;
5042 VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (exp), cnt, ce);
5043 cnt++, local.field = local.field ? DECL_CHAIN (local.field) : 0)
5045 local.val = ce->value;
5046 local.index = NULL_TREE;
5048 /* The element in a union constructor specifies the proper field
5049 or index. */
5050 if ((TREE_CODE (local.type) == RECORD_TYPE
5051 || TREE_CODE (local.type) == UNION_TYPE
5052 || TREE_CODE (local.type) == QUAL_UNION_TYPE)
5053 && ce->index != NULL_TREE)
5054 local.field = ce->index;
5056 else if (TREE_CODE (local.type) == ARRAY_TYPE)
5057 local.index = ce->index;
5059 #ifdef ASM_COMMENT_START
5060 if (local.field && flag_verbose_asm)
5061 fprintf (asm_out_file, "%s %s:\n",
5062 ASM_COMMENT_START,
5063 DECL_NAME (local.field)
5064 ? IDENTIFIER_POINTER (DECL_NAME (local.field))
5065 : "<anonymous>");
5066 #endif
5068 /* Eliminate the marker that makes a cast not be an lvalue. */
5069 if (local.val != NULL_TREE)
5070 STRIP_NOPS (local.val);
5072 /* Output the current element, using the appropriate helper ... */
5074 /* For an array slice not part of an outer bitfield. */
5075 if (!outer
5076 && local.index != NULL_TREE
5077 && TREE_CODE (local.index) == RANGE_EXPR)
5078 output_constructor_array_range (&local);
5080 /* For a field that is neither a true bitfield nor part of an outer one,
5081 known to be at least byte aligned and multiple-of-bytes long. */
5082 else if (!outer
5083 && (local.field == NULL_TREE
5084 || !CONSTRUCTOR_BITFIELD_P (local.field)))
5085 output_constructor_regular_field (&local);
5087 /* For a true bitfield or part of an outer one. */
5088 else
5089 output_constructor_bitfield (&local, outer);
5092 /* If we are not at toplevel, save the pending data for our caller.
5093 Otherwise output the pending data and padding zeros as needed. */
5094 if (outer)
5095 outer->byte = local.byte;
5096 else
5098 if (local.byte_buffer_in_use)
5100 assemble_integer (GEN_INT (local.byte), 1, BITS_PER_UNIT, 1);
5101 local.total_bytes++;
5104 if ((unsigned HOST_WIDE_INT)local.total_bytes < local.size)
5106 assemble_zeros (local.size - local.total_bytes);
5107 local.total_bytes = local.size;
5111 return local.total_bytes;
5114 /* Mark DECL as weak. */
5116 static void
5117 mark_weak (tree decl)
5119 DECL_WEAK (decl) = 1;
5121 if (DECL_RTL_SET_P (decl)
5122 && MEM_P (DECL_RTL (decl))
5123 && XEXP (DECL_RTL (decl), 0)
5124 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == SYMBOL_REF)
5125 SYMBOL_REF_WEAK (XEXP (DECL_RTL (decl), 0)) = 1;
5128 /* Merge weak status between NEWDECL and OLDDECL. */
5130 void
5131 merge_weak (tree newdecl, tree olddecl)
5133 if (DECL_WEAK (newdecl) == DECL_WEAK (olddecl))
5135 if (DECL_WEAK (newdecl) && TARGET_SUPPORTS_WEAK)
5137 tree *pwd;
5138 /* We put the NEWDECL on the weak_decls list at some point
5139 and OLDDECL as well. Keep just OLDDECL on the list. */
5140 for (pwd = &weak_decls; *pwd; pwd = &TREE_CHAIN (*pwd))
5141 if (TREE_VALUE (*pwd) == newdecl)
5143 *pwd = TREE_CHAIN (*pwd);
5144 break;
5147 return;
5150 if (DECL_WEAK (newdecl))
5152 tree wd;
5154 /* NEWDECL is weak, but OLDDECL is not. */
5156 /* If we already output the OLDDECL, we're in trouble; we can't
5157 go back and make it weak. This should never happen in
5158 unit-at-a-time compilation. */
5159 gcc_assert (!TREE_ASM_WRITTEN (olddecl));
5161 /* If we've already generated rtl referencing OLDDECL, we may
5162 have done so in a way that will not function properly with
5163 a weak symbol. Again in unit-at-a-time this should be
5164 impossible. */
5165 gcc_assert (!TREE_USED (olddecl)
5166 || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (olddecl)));
5168 if (TARGET_SUPPORTS_WEAK)
5170 /* We put the NEWDECL on the weak_decls list at some point.
5171 Replace it with the OLDDECL. */
5172 for (wd = weak_decls; wd; wd = TREE_CHAIN (wd))
5173 if (TREE_VALUE (wd) == newdecl)
5175 TREE_VALUE (wd) = olddecl;
5176 break;
5178 /* We may not find the entry on the list. If NEWDECL is a
5179 weak alias, then we will have already called
5180 globalize_decl to remove the entry; in that case, we do
5181 not need to do anything. */
5184 /* Make the OLDDECL weak; it's OLDDECL that we'll be keeping. */
5185 mark_weak (olddecl);
5187 else
5188 /* OLDDECL was weak, but NEWDECL was not explicitly marked as
5189 weak. Just update NEWDECL to indicate that it's weak too. */
5190 mark_weak (newdecl);
5193 /* Declare DECL to be a weak symbol. */
5195 void
5196 declare_weak (tree decl)
5198 gcc_assert (TREE_CODE (decl) != FUNCTION_DECL || !TREE_ASM_WRITTEN (decl));
5199 if (! TREE_PUBLIC (decl))
5200 error ("weak declaration of %q+D must be public", decl);
5201 else if (!TARGET_SUPPORTS_WEAK)
5202 warning (0, "weak declaration of %q+D not supported", decl);
5204 mark_weak (decl);
5205 if (!lookup_attribute ("weak", DECL_ATTRIBUTES (decl)))
5206 DECL_ATTRIBUTES (decl)
5207 = tree_cons (get_identifier ("weak"), NULL, DECL_ATTRIBUTES (decl));
5210 static void
5211 weak_finish_1 (tree decl)
5213 #if defined (ASM_WEAKEN_DECL) || defined (ASM_WEAKEN_LABEL)
5214 const char *const name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5215 #endif
5217 if (! TREE_USED (decl))
5218 return;
5220 #ifdef ASM_WEAKEN_DECL
5221 ASM_WEAKEN_DECL (asm_out_file, decl, name, NULL);
5222 #else
5223 #ifdef ASM_WEAKEN_LABEL
5224 ASM_WEAKEN_LABEL (asm_out_file, name);
5225 #else
5226 #ifdef ASM_OUTPUT_WEAK_ALIAS
5228 static bool warn_once = 0;
5229 if (! warn_once)
5231 warning (0, "only weak aliases are supported in this configuration");
5232 warn_once = 1;
5234 return;
5236 #endif
5237 #endif
5238 #endif
5241 /* This TREE_LIST contains weakref targets. */
5243 static GTY(()) tree weakref_targets;
5245 /* Forward declaration. */
5246 static tree find_decl_and_mark_needed (tree decl, tree target);
5248 /* Emit any pending weak declarations. */
5250 void
5251 weak_finish (void)
5253 tree t;
5255 for (t = weakref_targets; t; t = TREE_CHAIN (t))
5257 tree alias_decl = TREE_PURPOSE (t);
5258 tree target = ultimate_transparent_alias_target (&TREE_VALUE (t));
5260 if (! TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (alias_decl)))
5261 /* Remove alias_decl from the weak list, but leave entries for
5262 the target alone. */
5263 target = NULL_TREE;
5264 #ifndef ASM_OUTPUT_WEAKREF
5265 else if (! TREE_SYMBOL_REFERENCED (target))
5267 /* Use ASM_WEAKEN_LABEL only if ASM_WEAKEN_DECL is not
5268 defined, otherwise we and weak_finish_1 would use
5269 different macros. */
5270 # if defined ASM_WEAKEN_LABEL && ! defined ASM_WEAKEN_DECL
5271 ASM_WEAKEN_LABEL (asm_out_file, IDENTIFIER_POINTER (target));
5272 # else
5273 tree decl = find_decl_and_mark_needed (alias_decl, target);
5275 if (! decl)
5277 decl = build_decl (DECL_SOURCE_LOCATION (alias_decl),
5278 TREE_CODE (alias_decl), target,
5279 TREE_TYPE (alias_decl));
5281 DECL_EXTERNAL (decl) = 1;
5282 TREE_PUBLIC (decl) = 1;
5283 DECL_ARTIFICIAL (decl) = 1;
5284 TREE_NOTHROW (decl) = TREE_NOTHROW (alias_decl);
5285 TREE_USED (decl) = 1;
5288 weak_finish_1 (decl);
5289 # endif
5291 #endif
5294 tree *p;
5295 tree t2;
5297 /* Remove the alias and the target from the pending weak list
5298 so that we do not emit any .weak directives for the former,
5299 nor multiple .weak directives for the latter. */
5300 for (p = &weak_decls; (t2 = *p) ; )
5302 if (TREE_VALUE (t2) == alias_decl
5303 || target == DECL_ASSEMBLER_NAME (TREE_VALUE (t2)))
5304 *p = TREE_CHAIN (t2);
5305 else
5306 p = &TREE_CHAIN (t2);
5309 /* Remove other weakrefs to the same target, to speed things up. */
5310 for (p = &TREE_CHAIN (t); (t2 = *p) ; )
5312 if (target == ultimate_transparent_alias_target (&TREE_VALUE (t2)))
5313 *p = TREE_CHAIN (t2);
5314 else
5315 p = &TREE_CHAIN (t2);
5320 for (t = weak_decls; t; t = TREE_CHAIN (t))
5322 tree decl = TREE_VALUE (t);
5324 weak_finish_1 (decl);
5328 /* Emit the assembly bits to indicate that DECL is globally visible. */
5330 static void
5331 globalize_decl (tree decl)
5334 #if defined (ASM_WEAKEN_LABEL) || defined (ASM_WEAKEN_DECL)
5335 if (DECL_WEAK (decl))
5337 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
5338 tree *p, t;
5340 #ifdef ASM_WEAKEN_DECL
5341 ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
5342 #else
5343 ASM_WEAKEN_LABEL (asm_out_file, name);
5344 #endif
5346 /* Remove this function from the pending weak list so that
5347 we do not emit multiple .weak directives for it. */
5348 for (p = &weak_decls; (t = *p) ; )
5350 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
5351 *p = TREE_CHAIN (t);
5352 else
5353 p = &TREE_CHAIN (t);
5356 /* Remove weakrefs to the same target from the pending weakref
5357 list, for the same reason. */
5358 for (p = &weakref_targets; (t = *p) ; )
5360 if (DECL_ASSEMBLER_NAME (decl)
5361 == ultimate_transparent_alias_target (&TREE_VALUE (t)))
5362 *p = TREE_CHAIN (t);
5363 else
5364 p = &TREE_CHAIN (t);
5367 return;
5369 #endif
5371 targetm.asm_out.globalize_decl_name (asm_out_file, decl);
5374 VEC(alias_pair,gc) *alias_pairs;
5376 /* Given an assembly name, find the decl it is associated with. At the
5377 same time, mark it needed for cgraph. */
5379 static tree
5380 find_decl_and_mark_needed (tree decl, tree target)
5382 struct cgraph_node *fnode = NULL;
5383 struct varpool_node *vnode = NULL;
5385 if (TREE_CODE (decl) == FUNCTION_DECL)
5387 fnode = cgraph_node_for_asm (target);
5388 if (fnode == NULL)
5389 vnode = varpool_node_for_asm (target);
5391 else
5393 vnode = varpool_node_for_asm (target);
5394 if (vnode == NULL)
5395 fnode = cgraph_node_for_asm (target);
5398 if (fnode)
5400 cgraph_mark_needed_node (fnode);
5401 return fnode->decl;
5403 else if (vnode)
5405 varpool_mark_needed_node (vnode);
5406 vnode->force_output = 1;
5407 return vnode->decl;
5409 else
5410 return NULL_TREE;
5413 /* Output the assembler code for a define (equate) using ASM_OUTPUT_DEF
5414 or ASM_OUTPUT_DEF_FROM_DECLS. The function defines the symbol whose
5415 tree node is DECL to have the value of the tree node TARGET. */
5417 static void
5418 do_assemble_alias (tree decl, tree target)
5420 /* Emulated TLS had better not get this var. */
5421 gcc_assert(!(!targetm.have_tls
5422 && TREE_CODE (decl) == VAR_DECL
5423 && DECL_THREAD_LOCAL_P (decl)));
5425 if (TREE_ASM_WRITTEN (decl))
5426 return;
5428 /* We must force creation of DECL_RTL for debug info generation, even though
5429 we don't use it here. */
5430 make_decl_rtl (decl);
5432 TREE_ASM_WRITTEN (decl) = 1;
5433 TREE_ASM_WRITTEN (DECL_ASSEMBLER_NAME (decl)) = 1;
5435 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
5437 ultimate_transparent_alias_target (&target);
5439 if (!TREE_SYMBOL_REFERENCED (target))
5440 weakref_targets = tree_cons (decl, target, weakref_targets);
5442 #ifdef ASM_OUTPUT_WEAKREF
5443 ASM_OUTPUT_WEAKREF (asm_out_file, decl,
5444 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
5445 IDENTIFIER_POINTER (target));
5446 #else
5447 if (!TARGET_SUPPORTS_WEAK)
5449 error_at (DECL_SOURCE_LOCATION (decl),
5450 "weakref is not supported in this configuration");
5451 return;
5453 #endif
5454 return;
5457 #ifdef ASM_OUTPUT_DEF
5458 /* Make name accessible from other files, if appropriate. */
5460 if (TREE_PUBLIC (decl))
5462 globalize_decl (decl);
5463 maybe_assemble_visibility (decl);
5465 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
5467 #if defined (ASM_OUTPUT_TYPE_DIRECTIVE) && HAVE_GNU_INDIRECT_FUNCTION
5468 ASM_OUTPUT_TYPE_DIRECTIVE
5469 (asm_out_file, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
5470 IFUNC_ASM_TYPE);
5471 #else
5472 error_at (DECL_SOURCE_LOCATION (decl),
5473 "ifunc is not supported in this configuration");
5474 #endif
5477 # ifdef ASM_OUTPUT_DEF_FROM_DECLS
5478 ASM_OUTPUT_DEF_FROM_DECLS (asm_out_file, decl, target);
5479 # else
5480 ASM_OUTPUT_DEF (asm_out_file,
5481 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
5482 IDENTIFIER_POINTER (target));
5483 # endif
5484 #elif defined (ASM_OUTPUT_WEAK_ALIAS) || defined (ASM_WEAKEN_DECL)
5486 const char *name;
5487 tree *p, t;
5489 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5490 # ifdef ASM_WEAKEN_DECL
5491 ASM_WEAKEN_DECL (asm_out_file, decl, name, IDENTIFIER_POINTER (target));
5492 # else
5493 ASM_OUTPUT_WEAK_ALIAS (asm_out_file, name, IDENTIFIER_POINTER (target));
5494 # endif
5495 /* Remove this function from the pending weak list so that
5496 we do not emit multiple .weak directives for it. */
5497 for (p = &weak_decls; (t = *p) ; )
5498 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
5499 *p = TREE_CHAIN (t);
5500 else
5501 p = &TREE_CHAIN (t);
5503 /* Remove weakrefs to the same target from the pending weakref
5504 list, for the same reason. */
5505 for (p = &weakref_targets; (t = *p) ; )
5507 if (DECL_ASSEMBLER_NAME (decl)
5508 == ultimate_transparent_alias_target (&TREE_VALUE (t)))
5509 *p = TREE_CHAIN (t);
5510 else
5511 p = &TREE_CHAIN (t);
5514 #endif
5518 /* Allocate and construct a symbol alias set. */
5520 static symbol_alias_set_t *
5521 symbol_alias_set_create (void)
5523 return pointer_set_create ();
5526 /* Destruct and free a symbol alias set. */
5528 void
5529 symbol_alias_set_destroy (symbol_alias_set_t *aset)
5531 pointer_set_destroy (aset);
5534 /* Test if a symbol alias set contains a given name. */
5537 symbol_alias_set_contains (const symbol_alias_set_t *aset, tree t)
5539 /* We accept either a DECL or an IDENTIFIER directly. */
5540 if (TREE_CODE (t) != IDENTIFIER_NODE)
5541 t = DECL_ASSEMBLER_NAME (t);
5542 t = targetm.asm_out.mangle_assembler_name (IDENTIFIER_POINTER (t));
5543 return pointer_set_contains (aset, t);
5546 /* Enter a new name into a symbol alias set. */
5548 static int
5549 symbol_alias_set_insert (symbol_alias_set_t *aset, tree t)
5551 /* We accept either a DECL or an IDENTIFIER directly. */
5552 if (TREE_CODE (t) != IDENTIFIER_NODE)
5553 t = DECL_ASSEMBLER_NAME (t);
5554 t = targetm.asm_out.mangle_assembler_name (IDENTIFIER_POINTER (t));
5555 return pointer_set_insert (aset, t);
5558 /* IN_SET_P is a predicate function assuming to be taken
5559 alias_pair->decl, alias_pair->target and DATA arguments.
5561 Compute set of aliases by including everything where TRIVIALLY_VISIBLE
5562 predeicate is true and propagate across aliases such that when
5563 alias DECL is included, its TARGET is included too. */
5565 static symbol_alias_set_t *
5566 propagate_aliases_forward (bool (*in_set_p)
5567 (tree decl, tree target, void *data),
5568 void *data)
5570 symbol_alias_set_t *set;
5571 unsigned i;
5572 alias_pair *p;
5573 bool changed;
5575 set = symbol_alias_set_create ();
5576 for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); ++i)
5577 if (in_set_p (p->decl, p->target, data))
5578 symbol_alias_set_insert (set, p->decl);
5581 changed = false;
5582 for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); ++i)
5583 if (symbol_alias_set_contains (set, p->decl)
5584 && !symbol_alias_set_insert (set, p->target))
5585 changed = true;
5587 while (changed);
5589 return set;
5592 /* Like propagate_aliases_forward but do backward propagation. */
5594 symbol_alias_set_t *
5595 propagate_aliases_backward (bool (*in_set_p)
5596 (tree decl, tree target, void *data),
5597 void *data)
5599 symbol_alias_set_t *set;
5600 unsigned i;
5601 alias_pair *p;
5602 bool changed;
5604 /* We have to compute the set of set nodes including aliases
5605 themselves. */
5606 set = symbol_alias_set_create ();
5607 for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); ++i)
5608 if (in_set_p (p->decl, p->target, data))
5609 symbol_alias_set_insert (set, p->target);
5612 changed = false;
5613 for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); ++i)
5614 if (symbol_alias_set_contains (set, p->target)
5615 && !symbol_alias_set_insert (set, p->decl))
5616 changed = true;
5618 while (changed);
5620 return set;
5622 /* See if the alias is trivially visible. This means
5623 1) alias is expoerted from the unit or
5624 2) alias is used in the code.
5625 We assume that unused cgraph/varpool nodes has been
5626 removed.
5627 Used as callback for propagate_aliases. */
5629 static bool
5630 trivially_visible_alias (tree decl, tree target ATTRIBUTE_UNUSED,
5631 void *data ATTRIBUTE_UNUSED)
5633 struct cgraph_node *fnode = NULL;
5634 struct varpool_node *vnode = NULL;
5636 if (!TREE_PUBLIC (decl))
5638 if (TREE_CODE (decl) == FUNCTION_DECL)
5639 fnode = cgraph_get_node (decl);
5640 else
5641 vnode = varpool_get_node (decl);
5642 return vnode || fnode;
5644 else
5645 return true;
5648 /* See if the target of alias is defined in this unit.
5649 Used as callback for propagate_aliases. */
5651 static bool
5652 trivially_defined_alias (tree decl ATTRIBUTE_UNUSED,
5653 tree target,
5654 void *data ATTRIBUTE_UNUSED)
5656 struct cgraph_node *fnode = NULL;
5657 struct varpool_node *vnode = NULL;
5659 fnode = cgraph_node_for_asm (target);
5660 vnode = (fnode == NULL) ? varpool_node_for_asm (target) : NULL;
5661 return (fnode && fnode->analyzed) || (vnode && vnode->finalized);
5664 /* Remove the alias pairing for functions that are no longer in the call
5665 graph. */
5667 void
5668 remove_unreachable_alias_pairs (void)
5670 symbol_alias_set_t *visible;
5671 unsigned i;
5672 alias_pair *p;
5674 if (alias_pairs == NULL)
5675 return;
5677 /* We have to compute the set of visible nodes including aliases
5678 themselves. */
5679 visible = propagate_aliases_forward (trivially_visible_alias, NULL);
5681 for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); )
5683 if (!DECL_EXTERNAL (p->decl)
5684 && !symbol_alias_set_contains (visible, p->decl))
5686 VEC_unordered_remove (alias_pair, alias_pairs, i);
5687 continue;
5690 i++;
5693 symbol_alias_set_destroy (visible);
5697 /* First pass of completing pending aliases. Make sure that cgraph knows
5698 which symbols will be required. */
5700 void
5701 finish_aliases_1 (void)
5703 symbol_alias_set_t *defined;
5704 unsigned i;
5705 alias_pair *p;
5707 if (alias_pairs == NULL)
5708 return;
5710 /* We have to compute the set of defined nodes including aliases
5711 themselves. */
5712 defined = propagate_aliases_backward (trivially_defined_alias, NULL);
5714 FOR_EACH_VEC_ELT (alias_pair, alias_pairs, i, p)
5716 tree target_decl;
5718 target_decl = find_decl_and_mark_needed (p->decl, p->target);
5719 if (target_decl == NULL)
5721 if (symbol_alias_set_contains (defined, p->target))
5722 continue;
5724 if (! (p->emitted_diags & ALIAS_DIAG_TO_UNDEF)
5725 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
5727 error ("%q+D aliased to undefined symbol %qE",
5728 p->decl, p->target);
5729 p->emitted_diags |= ALIAS_DIAG_TO_UNDEF;
5732 else if (! (p->emitted_diags & ALIAS_DIAG_TO_EXTERN)
5733 && DECL_EXTERNAL (target_decl)
5734 /* We use local aliases for C++ thunks to force the tailcall
5735 to bind locally. This is a hack - to keep it working do
5736 the following (which is not strictly correct). */
5737 && (! TREE_CODE (target_decl) == FUNCTION_DECL
5738 || ! DECL_VIRTUAL_P (target_decl))
5739 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
5741 error ("%q+D aliased to external symbol %qE",
5742 p->decl, p->target);
5743 p->emitted_diags |= ALIAS_DIAG_TO_EXTERN;
5747 symbol_alias_set_destroy (defined);
5750 /* Second pass of completing pending aliases. Emit the actual assembly.
5751 This happens at the end of compilation and thus it is assured that the
5752 target symbol has been emitted. */
5754 void
5755 finish_aliases_2 (void)
5757 unsigned i;
5758 alias_pair *p;
5760 FOR_EACH_VEC_ELT (alias_pair, alias_pairs, i, p)
5761 do_assemble_alias (p->decl, p->target);
5763 VEC_truncate (alias_pair, alias_pairs, 0);
5766 /* Emit an assembler directive to make the symbol for DECL an alias to
5767 the symbol for TARGET. */
5769 void
5770 assemble_alias (tree decl, tree target)
5772 tree target_decl;
5773 bool is_weakref = false;
5775 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
5777 tree alias = DECL_ASSEMBLER_NAME (decl);
5779 is_weakref = true;
5781 ultimate_transparent_alias_target (&target);
5783 if (alias == target)
5784 error ("weakref %q+D ultimately targets itself", decl);
5785 else
5787 #ifndef ASM_OUTPUT_WEAKREF
5788 IDENTIFIER_TRANSPARENT_ALIAS (alias) = 1;
5789 TREE_CHAIN (alias) = target;
5790 #endif
5792 if (TREE_PUBLIC (decl))
5793 error ("weakref %q+D must have static linkage", decl);
5795 else
5797 #if !defined (ASM_OUTPUT_DEF)
5798 # if !defined(ASM_OUTPUT_WEAK_ALIAS) && !defined (ASM_WEAKEN_DECL)
5799 error_at (DECL_SOURCE_LOCATION (decl),
5800 "alias definitions not supported in this configuration");
5801 return;
5802 # else
5803 if (!DECL_WEAK (decl))
5805 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
5806 error_at (DECL_SOURCE_LOCATION (decl),
5807 "ifunc is not supported in this configuration");
5808 else
5809 error_at (DECL_SOURCE_LOCATION (decl),
5810 "only weak aliases are supported in this configuration");
5811 return;
5813 # endif
5814 #endif
5816 TREE_USED (decl) = 1;
5818 /* A quirk of the initial implementation of aliases required that the user
5819 add "extern" to all of them. Which is silly, but now historical. Do
5820 note that the symbol is in fact locally defined. */
5821 if (! is_weakref)
5822 DECL_EXTERNAL (decl) = 0;
5824 /* Allow aliases to aliases. */
5825 if (TREE_CODE (decl) == FUNCTION_DECL)
5826 cgraph_get_create_node (decl)->alias = true;
5827 else
5828 varpool_node (decl)->alias = true;
5830 /* If the target has already been emitted, we don't have to queue the
5831 alias. This saves a tad of memory. */
5832 if (cgraph_global_info_ready)
5833 target_decl = find_decl_and_mark_needed (decl, target);
5834 else
5835 target_decl= NULL;
5836 if (target_decl && TREE_ASM_WRITTEN (target_decl))
5837 do_assemble_alias (decl, target);
5838 else
5840 alias_pair *p = VEC_safe_push (alias_pair, gc, alias_pairs, NULL);
5841 p->decl = decl;
5842 p->target = target;
5843 p->emitted_diags = ALIAS_DIAG_NONE;
5847 /* Emit an assembler directive to set symbol for DECL visibility to
5848 the visibility type VIS, which must not be VISIBILITY_DEFAULT. */
5850 void
5851 default_assemble_visibility (tree decl ATTRIBUTE_UNUSED,
5852 int vis ATTRIBUTE_UNUSED)
5854 #ifdef HAVE_GAS_HIDDEN
5855 static const char * const visibility_types[] = {
5856 NULL, "protected", "hidden", "internal"
5859 const char *name, *type;
5861 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5862 type = visibility_types[vis];
5864 fprintf (asm_out_file, "\t.%s\t", type);
5865 assemble_name (asm_out_file, name);
5866 fprintf (asm_out_file, "\n");
5867 #else
5868 warning (OPT_Wattributes, "visibility attribute not supported "
5869 "in this configuration; ignored");
5870 #endif
5873 /* A helper function to call assemble_visibility when needed for a decl. */
5876 maybe_assemble_visibility (tree decl)
5878 enum symbol_visibility vis = DECL_VISIBILITY (decl);
5880 if (vis != VISIBILITY_DEFAULT)
5882 targetm.asm_out.assemble_visibility (decl, vis);
5883 return 1;
5885 else
5886 return 0;
5889 /* Returns 1 if the target configuration supports defining public symbols
5890 so that one of them will be chosen at link time instead of generating a
5891 multiply-defined symbol error, whether through the use of weak symbols or
5892 a target-specific mechanism for having duplicates discarded. */
5895 supports_one_only (void)
5897 if (SUPPORTS_ONE_ONLY)
5898 return 1;
5899 return TARGET_SUPPORTS_WEAK;
5902 /* Set up DECL as a public symbol that can be defined in multiple
5903 translation units without generating a linker error. */
5905 void
5906 make_decl_one_only (tree decl, tree comdat_group)
5908 gcc_assert (TREE_CODE (decl) == VAR_DECL
5909 || TREE_CODE (decl) == FUNCTION_DECL);
5911 TREE_PUBLIC (decl) = 1;
5913 if (SUPPORTS_ONE_ONLY)
5915 #ifdef MAKE_DECL_ONE_ONLY
5916 MAKE_DECL_ONE_ONLY (decl);
5917 #endif
5918 DECL_COMDAT_GROUP (decl) = comdat_group;
5920 else if (TREE_CODE (decl) == VAR_DECL
5921 && (DECL_INITIAL (decl) == 0 || DECL_INITIAL (decl) == error_mark_node))
5922 DECL_COMMON (decl) = 1;
5923 else
5925 gcc_assert (TARGET_SUPPORTS_WEAK);
5926 DECL_WEAK (decl) = 1;
5930 void
5931 init_varasm_once (void)
5933 section_htab = htab_create_ggc (31, section_entry_hash,
5934 section_entry_eq, NULL);
5935 object_block_htab = htab_create_ggc (31, object_block_entry_hash,
5936 object_block_entry_eq, NULL);
5937 const_desc_htab = htab_create_ggc (1009, const_desc_hash,
5938 const_desc_eq, NULL);
5940 const_alias_set = new_alias_set ();
5941 shared_constant_pool = create_constant_pool ();
5943 #ifdef TEXT_SECTION_ASM_OP
5944 text_section = get_unnamed_section (SECTION_CODE, output_section_asm_op,
5945 TEXT_SECTION_ASM_OP);
5946 #endif
5948 #ifdef DATA_SECTION_ASM_OP
5949 data_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
5950 DATA_SECTION_ASM_OP);
5951 #endif
5953 #ifdef SDATA_SECTION_ASM_OP
5954 sdata_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
5955 SDATA_SECTION_ASM_OP);
5956 #endif
5958 #ifdef READONLY_DATA_SECTION_ASM_OP
5959 readonly_data_section = get_unnamed_section (0, output_section_asm_op,
5960 READONLY_DATA_SECTION_ASM_OP);
5961 #endif
5963 #ifdef CTORS_SECTION_ASM_OP
5964 ctors_section = get_unnamed_section (0, output_section_asm_op,
5965 CTORS_SECTION_ASM_OP);
5966 #endif
5968 #ifdef DTORS_SECTION_ASM_OP
5969 dtors_section = get_unnamed_section (0, output_section_asm_op,
5970 DTORS_SECTION_ASM_OP);
5971 #endif
5973 #ifdef BSS_SECTION_ASM_OP
5974 bss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
5975 output_section_asm_op,
5976 BSS_SECTION_ASM_OP);
5977 #endif
5979 #ifdef SBSS_SECTION_ASM_OP
5980 sbss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
5981 output_section_asm_op,
5982 SBSS_SECTION_ASM_OP);
5983 #endif
5985 tls_comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
5986 | SECTION_COMMON, emit_tls_common);
5987 lcomm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
5988 | SECTION_COMMON, emit_local);
5989 comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
5990 | SECTION_COMMON, emit_common);
5992 #if defined ASM_OUTPUT_ALIGNED_BSS
5993 bss_noswitch_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS,
5994 emit_bss);
5995 #endif
5997 targetm.asm_out.init_sections ();
5999 if (readonly_data_section == NULL)
6000 readonly_data_section = text_section;
6003 enum tls_model
6004 decl_default_tls_model (const_tree decl)
6006 enum tls_model kind;
6007 bool is_local;
6009 is_local = targetm.binds_local_p (decl);
6010 if (!flag_shlib)
6012 if (is_local)
6013 kind = TLS_MODEL_LOCAL_EXEC;
6014 else
6015 kind = TLS_MODEL_INITIAL_EXEC;
6018 /* Local dynamic is inefficient when we're not combining the
6019 parts of the address. */
6020 else if (optimize && is_local)
6021 kind = TLS_MODEL_LOCAL_DYNAMIC;
6022 else
6023 kind = TLS_MODEL_GLOBAL_DYNAMIC;
6024 if (kind < flag_tls_default)
6025 kind = flag_tls_default;
6027 return kind;
6030 /* Select a set of attributes for section NAME based on the properties
6031 of DECL and whether or not RELOC indicates that DECL's initializer
6032 might contain runtime relocations.
6034 We make the section read-only and executable for a function decl,
6035 read-only for a const data decl, and writable for a non-const data decl. */
6037 unsigned int
6038 default_section_type_flags (tree decl, const char *name, int reloc)
6040 unsigned int flags;
6042 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
6043 flags = SECTION_CODE;
6044 else if (decl)
6046 enum section_category category
6047 = categorize_decl_for_section (decl, reloc);
6048 if (decl_readonly_section_1 (category))
6049 flags = 0;
6050 else if (category == SECCAT_DATA_REL_RO
6051 || category == SECCAT_DATA_REL_RO_LOCAL)
6052 flags = SECTION_WRITE | SECTION_RELRO;
6053 else
6054 flags = SECTION_WRITE;
6056 else
6058 flags = SECTION_WRITE;
6059 if (strcmp (name, ".data.rel.ro") == 0
6060 || strcmp (name, ".data.rel.ro.local") == 0)
6061 flags |= SECTION_RELRO;
6064 if (decl && DECL_ONE_ONLY (decl))
6065 flags |= SECTION_LINKONCE;
6067 if (decl && TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
6068 flags |= SECTION_TLS | SECTION_WRITE;
6070 if (strcmp (name, ".bss") == 0
6071 || strncmp (name, ".bss.", 5) == 0
6072 || strncmp (name, ".gnu.linkonce.b.", 16) == 0
6073 || strcmp (name, ".sbss") == 0
6074 || strncmp (name, ".sbss.", 6) == 0
6075 || strncmp (name, ".gnu.linkonce.sb.", 17) == 0)
6076 flags |= SECTION_BSS;
6078 if (strcmp (name, ".tdata") == 0
6079 || strncmp (name, ".tdata.", 7) == 0
6080 || strncmp (name, ".gnu.linkonce.td.", 17) == 0)
6081 flags |= SECTION_TLS;
6083 if (strcmp (name, ".tbss") == 0
6084 || strncmp (name, ".tbss.", 6) == 0
6085 || strncmp (name, ".gnu.linkonce.tb.", 17) == 0)
6086 flags |= SECTION_TLS | SECTION_BSS;
6088 /* These three sections have special ELF types. They are neither
6089 SHT_PROGBITS nor SHT_NOBITS, so when changing sections we don't
6090 want to print a section type (@progbits or @nobits). If someone
6091 is silly enough to emit code or TLS variables to one of these
6092 sections, then don't handle them specially. */
6093 if (!(flags & (SECTION_CODE | SECTION_BSS | SECTION_TLS))
6094 && (strcmp (name, ".init_array") == 0
6095 || strcmp (name, ".fini_array") == 0
6096 || strcmp (name, ".preinit_array") == 0))
6097 flags |= SECTION_NOTYPE;
6099 return flags;
6102 /* Return true if the target supports some form of global BSS,
6103 either through bss_noswitch_section, or by selecting a BSS
6104 section in TARGET_ASM_SELECT_SECTION. */
6106 bool
6107 have_global_bss_p (void)
6109 return bss_noswitch_section || targetm.have_switchable_bss_sections;
6112 /* Output assembly to switch to section NAME with attribute FLAGS.
6113 Four variants for common object file formats. */
6115 void
6116 default_no_named_section (const char *name ATTRIBUTE_UNUSED,
6117 unsigned int flags ATTRIBUTE_UNUSED,
6118 tree decl ATTRIBUTE_UNUSED)
6120 /* Some object formats don't support named sections at all. The
6121 front-end should already have flagged this as an error. */
6122 gcc_unreachable ();
6125 #ifndef TLS_SECTION_ASM_FLAG
6126 #define TLS_SECTION_ASM_FLAG 'T'
6127 #endif
6129 void
6130 default_elf_asm_named_section (const char *name, unsigned int flags,
6131 tree decl ATTRIBUTE_UNUSED)
6133 char flagchars[10], *f = flagchars;
6135 /* If we have already declared this section, we can use an
6136 abbreviated form to switch back to it -- unless this section is
6137 part of a COMDAT groups, in which case GAS requires the full
6138 declaration every time. */
6139 if (!(HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6140 && (flags & SECTION_DECLARED))
6142 fprintf (asm_out_file, "\t.section\t%s\n", name);
6143 return;
6146 if (!(flags & SECTION_DEBUG))
6147 *f++ = 'a';
6148 if (flags & SECTION_WRITE)
6149 *f++ = 'w';
6150 if (flags & SECTION_CODE)
6151 *f++ = 'x';
6152 if (flags & SECTION_SMALL)
6153 *f++ = 's';
6154 if (flags & SECTION_MERGE)
6155 *f++ = 'M';
6156 if (flags & SECTION_STRINGS)
6157 *f++ = 'S';
6158 if (flags & SECTION_TLS)
6159 *f++ = TLS_SECTION_ASM_FLAG;
6160 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6161 *f++ = 'G';
6162 *f = '\0';
6164 fprintf (asm_out_file, "\t.section\t%s,\"%s\"", name, flagchars);
6166 if (!(flags & SECTION_NOTYPE))
6168 const char *type;
6169 const char *format;
6171 if (flags & SECTION_BSS)
6172 type = "nobits";
6173 else
6174 type = "progbits";
6176 format = ",@%s";
6177 #ifdef ASM_COMMENT_START
6178 /* On platforms that use "@" as the assembly comment character,
6179 use "%" instead. */
6180 if (strcmp (ASM_COMMENT_START, "@") == 0)
6181 format = ",%%%s";
6182 #endif
6183 fprintf (asm_out_file, format, type);
6185 if (flags & SECTION_ENTSIZE)
6186 fprintf (asm_out_file, ",%d", flags & SECTION_ENTSIZE);
6187 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6189 if (TREE_CODE (decl) == IDENTIFIER_NODE)
6190 fprintf (asm_out_file, ",%s,comdat", IDENTIFIER_POINTER (decl));
6191 else
6192 fprintf (asm_out_file, ",%s,comdat",
6193 IDENTIFIER_POINTER (DECL_COMDAT_GROUP (decl)));
6197 putc ('\n', asm_out_file);
6200 void
6201 default_coff_asm_named_section (const char *name, unsigned int flags,
6202 tree decl ATTRIBUTE_UNUSED)
6204 char flagchars[8], *f = flagchars;
6206 if (flags & SECTION_WRITE)
6207 *f++ = 'w';
6208 if (flags & SECTION_CODE)
6209 *f++ = 'x';
6210 *f = '\0';
6212 fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
6215 void
6216 default_pe_asm_named_section (const char *name, unsigned int flags,
6217 tree decl)
6219 default_coff_asm_named_section (name, flags, decl);
6221 if (flags & SECTION_LINKONCE)
6223 /* Functions may have been compiled at various levels of
6224 optimization so we can't use `same_size' here.
6225 Instead, have the linker pick one. */
6226 fprintf (asm_out_file, "\t.linkonce %s\n",
6227 (flags & SECTION_CODE ? "discard" : "same_size"));
6231 /* The lame default section selector. */
6233 section *
6234 default_select_section (tree decl, int reloc,
6235 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
6237 if (DECL_P (decl))
6239 if (decl_readonly_section (decl, reloc))
6240 return readonly_data_section;
6242 else if (TREE_CODE (decl) == CONSTRUCTOR)
6244 if (! ((flag_pic && reloc)
6245 || !TREE_READONLY (decl)
6246 || TREE_SIDE_EFFECTS (decl)
6247 || !TREE_CONSTANT (decl)))
6248 return readonly_data_section;
6250 else if (TREE_CODE (decl) == STRING_CST)
6251 return readonly_data_section;
6252 else if (! (flag_pic && reloc))
6253 return readonly_data_section;
6255 return data_section;
6258 enum section_category
6259 categorize_decl_for_section (const_tree decl, int reloc)
6261 enum section_category ret;
6263 if (TREE_CODE (decl) == FUNCTION_DECL)
6264 return SECCAT_TEXT;
6265 else if (TREE_CODE (decl) == STRING_CST)
6267 if (flag_mudflap) /* or !flag_merge_constants */
6268 return SECCAT_RODATA;
6269 else
6270 return SECCAT_RODATA_MERGE_STR;
6272 else if (TREE_CODE (decl) == VAR_DECL)
6274 if (bss_initializer_p (decl))
6275 ret = SECCAT_BSS;
6276 else if (! TREE_READONLY (decl)
6277 || TREE_SIDE_EFFECTS (decl)
6278 || ! TREE_CONSTANT (DECL_INITIAL (decl)))
6280 /* Here the reloc_rw_mask is not testing whether the section should
6281 be read-only or not, but whether the dynamic link will have to
6282 do something. If so, we wish to segregate the data in order to
6283 minimize cache misses inside the dynamic linker. */
6284 if (reloc & targetm.asm_out.reloc_rw_mask ())
6285 ret = reloc == 1 ? SECCAT_DATA_REL_LOCAL : SECCAT_DATA_REL;
6286 else
6287 ret = SECCAT_DATA;
6289 else if (reloc & targetm.asm_out.reloc_rw_mask ())
6290 ret = reloc == 1 ? SECCAT_DATA_REL_RO_LOCAL : SECCAT_DATA_REL_RO;
6291 else if (reloc || flag_merge_constants < 2)
6292 /* C and C++ don't allow different variables to share the same
6293 location. -fmerge-all-constants allows even that (at the
6294 expense of not conforming). */
6295 ret = SECCAT_RODATA;
6296 else if (TREE_CODE (DECL_INITIAL (decl)) == STRING_CST)
6297 ret = SECCAT_RODATA_MERGE_STR_INIT;
6298 else
6299 ret = SECCAT_RODATA_MERGE_CONST;
6301 else if (TREE_CODE (decl) == CONSTRUCTOR)
6303 if ((reloc & targetm.asm_out.reloc_rw_mask ())
6304 || TREE_SIDE_EFFECTS (decl)
6305 || ! TREE_CONSTANT (decl))
6306 ret = SECCAT_DATA;
6307 else
6308 ret = SECCAT_RODATA;
6310 else
6311 ret = SECCAT_RODATA;
6313 /* There are no read-only thread-local sections. */
6314 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
6316 /* Note that this would be *just* SECCAT_BSS, except that there's
6317 no concept of a read-only thread-local-data section. */
6318 if (ret == SECCAT_BSS
6319 || (flag_zero_initialized_in_bss
6320 && initializer_zerop (DECL_INITIAL (decl))))
6321 ret = SECCAT_TBSS;
6322 else
6323 ret = SECCAT_TDATA;
6326 /* If the target uses small data sections, select it. */
6327 else if (targetm.in_small_data_p (decl))
6329 if (ret == SECCAT_BSS)
6330 ret = SECCAT_SBSS;
6331 else if (targetm.have_srodata_section && ret == SECCAT_RODATA)
6332 ret = SECCAT_SRODATA;
6333 else
6334 ret = SECCAT_SDATA;
6337 return ret;
6340 static bool
6341 decl_readonly_section_1 (enum section_category category)
6343 switch (category)
6345 case SECCAT_RODATA:
6346 case SECCAT_RODATA_MERGE_STR:
6347 case SECCAT_RODATA_MERGE_STR_INIT:
6348 case SECCAT_RODATA_MERGE_CONST:
6349 case SECCAT_SRODATA:
6350 return true;
6351 default:
6352 return false;
6356 bool
6357 decl_readonly_section (const_tree decl, int reloc)
6359 return decl_readonly_section_1 (categorize_decl_for_section (decl, reloc));
6362 /* Select a section based on the above categorization. */
6364 section *
6365 default_elf_select_section (tree decl, int reloc,
6366 unsigned HOST_WIDE_INT align)
6368 const char *sname;
6369 switch (categorize_decl_for_section (decl, reloc))
6371 case SECCAT_TEXT:
6372 /* We're not supposed to be called on FUNCTION_DECLs. */
6373 gcc_unreachable ();
6374 case SECCAT_RODATA:
6375 return readonly_data_section;
6376 case SECCAT_RODATA_MERGE_STR:
6377 return mergeable_string_section (decl, align, 0);
6378 case SECCAT_RODATA_MERGE_STR_INIT:
6379 return mergeable_string_section (DECL_INITIAL (decl), align, 0);
6380 case SECCAT_RODATA_MERGE_CONST:
6381 return mergeable_constant_section (DECL_MODE (decl), align, 0);
6382 case SECCAT_SRODATA:
6383 sname = ".sdata2";
6384 break;
6385 case SECCAT_DATA:
6386 return data_section;
6387 case SECCAT_DATA_REL:
6388 sname = ".data.rel";
6389 break;
6390 case SECCAT_DATA_REL_LOCAL:
6391 sname = ".data.rel.local";
6392 break;
6393 case SECCAT_DATA_REL_RO:
6394 sname = ".data.rel.ro";
6395 break;
6396 case SECCAT_DATA_REL_RO_LOCAL:
6397 sname = ".data.rel.ro.local";
6398 break;
6399 case SECCAT_SDATA:
6400 sname = ".sdata";
6401 break;
6402 case SECCAT_TDATA:
6403 sname = ".tdata";
6404 break;
6405 case SECCAT_BSS:
6406 if (bss_section)
6407 return bss_section;
6408 sname = ".bss";
6409 break;
6410 case SECCAT_SBSS:
6411 sname = ".sbss";
6412 break;
6413 case SECCAT_TBSS:
6414 sname = ".tbss";
6415 break;
6416 default:
6417 gcc_unreachable ();
6420 if (!DECL_P (decl))
6421 decl = NULL_TREE;
6422 return get_named_section (decl, sname, reloc);
6425 /* Construct a unique section name based on the decl name and the
6426 categorization performed above. */
6428 void
6429 default_unique_section (tree decl, int reloc)
6431 /* We only need to use .gnu.linkonce if we don't have COMDAT groups. */
6432 bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
6433 const char *prefix, *name, *linkonce;
6434 char *string;
6436 switch (categorize_decl_for_section (decl, reloc))
6438 case SECCAT_TEXT:
6439 prefix = one_only ? ".t" : ".text";
6440 break;
6441 case SECCAT_RODATA:
6442 case SECCAT_RODATA_MERGE_STR:
6443 case SECCAT_RODATA_MERGE_STR_INIT:
6444 case SECCAT_RODATA_MERGE_CONST:
6445 prefix = one_only ? ".r" : ".rodata";
6446 break;
6447 case SECCAT_SRODATA:
6448 prefix = one_only ? ".s2" : ".sdata2";
6449 break;
6450 case SECCAT_DATA:
6451 prefix = one_only ? ".d" : ".data";
6452 break;
6453 case SECCAT_DATA_REL:
6454 prefix = one_only ? ".d.rel" : ".data.rel";
6455 break;
6456 case SECCAT_DATA_REL_LOCAL:
6457 prefix = one_only ? ".d.rel.local" : ".data.rel.local";
6458 break;
6459 case SECCAT_DATA_REL_RO:
6460 prefix = one_only ? ".d.rel.ro" : ".data.rel.ro";
6461 break;
6462 case SECCAT_DATA_REL_RO_LOCAL:
6463 prefix = one_only ? ".d.rel.ro.local" : ".data.rel.ro.local";
6464 break;
6465 case SECCAT_SDATA:
6466 prefix = one_only ? ".s" : ".sdata";
6467 break;
6468 case SECCAT_BSS:
6469 prefix = one_only ? ".b" : ".bss";
6470 break;
6471 case SECCAT_SBSS:
6472 prefix = one_only ? ".sb" : ".sbss";
6473 break;
6474 case SECCAT_TDATA:
6475 prefix = one_only ? ".td" : ".tdata";
6476 break;
6477 case SECCAT_TBSS:
6478 prefix = one_only ? ".tb" : ".tbss";
6479 break;
6480 default:
6481 gcc_unreachable ();
6484 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
6485 name = targetm.strip_name_encoding (name);
6487 /* If we're using one_only, then there needs to be a .gnu.linkonce
6488 prefix to the section name. */
6489 linkonce = one_only ? ".gnu.linkonce" : "";
6491 string = ACONCAT ((linkonce, prefix, ".", name, NULL));
6493 DECL_SECTION_NAME (decl) = build_string (strlen (string), string);
6496 /* Like compute_reloc_for_constant, except for an RTX. The return value
6497 is a mask for which bit 1 indicates a global relocation, and bit 0
6498 indicates a local relocation. */
6500 static int
6501 compute_reloc_for_rtx_1 (rtx *xp, void *data)
6503 int *preloc = (int *) data;
6504 rtx x = *xp;
6506 switch (GET_CODE (x))
6508 case SYMBOL_REF:
6509 *preloc |= SYMBOL_REF_LOCAL_P (x) ? 1 : 2;
6510 break;
6511 case LABEL_REF:
6512 *preloc |= 1;
6513 break;
6514 default:
6515 break;
6518 return 0;
6521 static int
6522 compute_reloc_for_rtx (rtx x)
6524 int reloc;
6526 switch (GET_CODE (x))
6528 case CONST:
6529 case SYMBOL_REF:
6530 case LABEL_REF:
6531 reloc = 0;
6532 for_each_rtx (&x, compute_reloc_for_rtx_1, &reloc);
6533 return reloc;
6535 default:
6536 return 0;
6540 section *
6541 default_select_rtx_section (enum machine_mode mode ATTRIBUTE_UNUSED,
6542 rtx x,
6543 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
6545 if (compute_reloc_for_rtx (x) & targetm.asm_out.reloc_rw_mask ())
6546 return data_section;
6547 else
6548 return readonly_data_section;
6551 section *
6552 default_elf_select_rtx_section (enum machine_mode mode, rtx x,
6553 unsigned HOST_WIDE_INT align)
6555 int reloc = compute_reloc_for_rtx (x);
6557 /* ??? Handle small data here somehow. */
6559 if (reloc & targetm.asm_out.reloc_rw_mask ())
6561 if (reloc == 1)
6562 return get_named_section (NULL, ".data.rel.ro.local", 1);
6563 else
6564 return get_named_section (NULL, ".data.rel.ro", 3);
6567 return mergeable_constant_section (mode, align, 0);
6570 /* Set the generally applicable flags on the SYMBOL_REF for EXP. */
6572 void
6573 default_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
6575 rtx symbol;
6576 int flags;
6578 /* Careful not to prod global register variables. */
6579 if (!MEM_P (rtl))
6580 return;
6581 symbol = XEXP (rtl, 0);
6582 if (GET_CODE (symbol) != SYMBOL_REF)
6583 return;
6585 flags = SYMBOL_REF_FLAGS (symbol) & SYMBOL_FLAG_HAS_BLOCK_INFO;
6586 if (TREE_CODE (decl) == FUNCTION_DECL)
6587 flags |= SYMBOL_FLAG_FUNCTION;
6588 if (targetm.binds_local_p (decl))
6589 flags |= SYMBOL_FLAG_LOCAL;
6590 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
6591 flags |= DECL_TLS_MODEL (decl) << SYMBOL_FLAG_TLS_SHIFT;
6592 else if (targetm.in_small_data_p (decl))
6593 flags |= SYMBOL_FLAG_SMALL;
6594 /* ??? Why is DECL_EXTERNAL ever set for non-PUBLIC names? Without
6595 being PUBLIC, the thing *must* be defined in this translation unit.
6596 Prevent this buglet from being propagated into rtl code as well. */
6597 if (DECL_P (decl) && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl))
6598 flags |= SYMBOL_FLAG_EXTERNAL;
6600 SYMBOL_REF_FLAGS (symbol) = flags;
6603 /* By default, we do nothing for encode_section_info, so we need not
6604 do anything but discard the '*' marker. */
6606 const char *
6607 default_strip_name_encoding (const char *str)
6609 return str + (*str == '*');
6612 #ifdef ASM_OUTPUT_DEF
6613 /* The default implementation of TARGET_ASM_OUTPUT_ANCHOR. Define the
6614 anchor relative to ".", the current section position. */
6616 void
6617 default_asm_output_anchor (rtx symbol)
6619 char buffer[100];
6621 sprintf (buffer, "*. + " HOST_WIDE_INT_PRINT_DEC,
6622 SYMBOL_REF_BLOCK_OFFSET (symbol));
6623 ASM_OUTPUT_DEF (asm_out_file, XSTR (symbol, 0), buffer);
6625 #endif
6627 /* The default implementation of TARGET_USE_ANCHORS_FOR_SYMBOL_P. */
6629 bool
6630 default_use_anchors_for_symbol_p (const_rtx symbol)
6632 section *sect;
6633 tree decl;
6635 /* Don't use anchors for mergeable sections. The linker might move
6636 the objects around. */
6637 sect = SYMBOL_REF_BLOCK (symbol)->sect;
6638 if (sect->common.flags & SECTION_MERGE)
6639 return false;
6641 /* Don't use anchors for small data sections. The small data register
6642 acts as an anchor for such sections. */
6643 if (sect->common.flags & SECTION_SMALL)
6644 return false;
6646 decl = SYMBOL_REF_DECL (symbol);
6647 if (decl && DECL_P (decl))
6649 /* Don't use section anchors for decls that might be defined by
6650 other modules. */
6651 if (!targetm.binds_local_p (decl))
6652 return false;
6654 /* Don't use section anchors for decls that will be placed in a
6655 small data section. */
6656 /* ??? Ideally, this check would be redundant with the SECTION_SMALL
6657 one above. The problem is that we only use SECTION_SMALL for
6658 sections that should be marked as small in the section directive. */
6659 if (targetm.in_small_data_p (decl))
6660 return false;
6662 return true;
6665 /* Return true when RESOLUTION indicate that symbol will be bound to the
6666 definition provided by current .o file. */
6668 static bool
6669 resolution_to_local_definition_p (enum ld_plugin_symbol_resolution resolution)
6671 return (resolution == LDPR_PREVAILING_DEF
6672 || resolution == LDPR_PREVAILING_DEF_IRONLY);
6675 /* Return true when RESOLUTION indicate that symbol will be bound locally
6676 within current executable or DSO. */
6678 static bool
6679 resolution_local_p (enum ld_plugin_symbol_resolution resolution)
6681 return (resolution == LDPR_PREVAILING_DEF
6682 || resolution == LDPR_PREVAILING_DEF_IRONLY
6683 || resolution == LDPR_PREEMPTED_REG
6684 || resolution == LDPR_PREEMPTED_IR
6685 || resolution == LDPR_RESOLVED_IR
6686 || resolution == LDPR_RESOLVED_EXEC);
6689 /* Assume ELF-ish defaults, since that's pretty much the most liberal
6690 wrt cross-module name binding. */
6692 bool
6693 default_binds_local_p (const_tree exp)
6695 return default_binds_local_p_1 (exp, flag_shlib);
6698 bool
6699 default_binds_local_p_1 (const_tree exp, int shlib)
6701 bool local_p;
6702 bool resolved_locally = false;
6703 bool resolved_to_local_def = false;
6705 /* With resolution file in hands, take look into resolutions.
6706 We can't just return true for resolved_localy symbols,
6707 because dynamic linking might overwrite symbols
6708 in shared libraries. */
6709 if (TREE_CODE (exp) == VAR_DECL && TREE_PUBLIC (exp)
6710 && (TREE_STATIC (exp) || DECL_EXTERNAL (exp)))
6712 struct varpool_node *vnode = varpool_get_node (exp);
6713 if (vnode && resolution_local_p (vnode->resolution))
6714 resolved_locally = true;
6715 if (vnode
6716 && resolution_to_local_definition_p (vnode->resolution))
6717 resolved_to_local_def = true;
6719 else if (TREE_CODE (exp) == FUNCTION_DECL && TREE_PUBLIC (exp))
6721 struct cgraph_node *node = cgraph_get_node_or_alias (exp);
6722 if (node
6723 && resolution_local_p (node->resolution))
6724 resolved_locally = true;
6725 if (node
6726 && resolution_to_local_definition_p (node->resolution))
6727 resolved_to_local_def = true;
6730 /* A non-decl is an entry in the constant pool. */
6731 if (!DECL_P (exp))
6732 local_p = true;
6733 /* Weakrefs may not bind locally, even though the weakref itself is
6734 always static and therefore local.
6735 FIXME: We can resolve this more curefuly by looking at the weakref
6736 alias. */
6737 else if (lookup_attribute ("weakref", DECL_ATTRIBUTES (exp)))
6738 local_p = false;
6739 /* Static variables are always local. */
6740 else if (! TREE_PUBLIC (exp))
6741 local_p = true;
6742 /* A variable is local if the user has said explicitly that it will
6743 be. */
6744 else if ((DECL_VISIBILITY_SPECIFIED (exp)
6745 || resolved_to_local_def)
6746 && DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
6747 local_p = true;
6748 /* Variables defined outside this object might not be local. */
6749 else if (DECL_EXTERNAL (exp) && !resolved_locally)
6750 local_p = false;
6751 /* If defined in this object and visibility is not default, must be
6752 local. */
6753 else if (DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
6754 local_p = true;
6755 /* Default visibility weak data can be overridden by a strong symbol
6756 in another module and so are not local. */
6757 else if (DECL_WEAK (exp)
6758 && !resolved_locally)
6759 local_p = false;
6760 /* If PIC, then assume that any global name can be overridden by
6761 symbols resolved from other modules. */
6762 else if (shlib)
6763 local_p = false;
6764 /* Uninitialized COMMON variable may be unified with symbols
6765 resolved from other modules. */
6766 else if (DECL_COMMON (exp)
6767 && !resolved_locally
6768 && (DECL_INITIAL (exp) == NULL
6769 || DECL_INITIAL (exp) == error_mark_node))
6770 local_p = false;
6771 /* Otherwise we're left with initialized (or non-common) global data
6772 which is of necessity defined locally. */
6773 else
6774 local_p = true;
6776 return local_p;
6779 /* Return true when references to DECL must bind to current definition in
6780 final executable.
6782 The condition is usually equivalent to whether the function binds to the
6783 current module (shared library or executable), that is to binds_local_p.
6784 We use this fact to avoid need for another target hook and implement
6785 the logic using binds_local_p and just special cases where
6786 decl_binds_to_current_def_p is stronger than binds_local_p. In particular
6787 the weak definitions (that can be overwritten at linktime by other
6788 definition from different object file) and when resolution info is available
6789 we simply use the knowledge passed to us by linker plugin. */
6790 bool
6791 decl_binds_to_current_def_p (tree decl)
6793 gcc_assert (DECL_P (decl));
6794 if (!TREE_PUBLIC (decl))
6795 return true;
6796 if (!targetm.binds_local_p (decl))
6797 return false;
6798 /* When resolution is available, just use it. */
6799 if (TREE_CODE (decl) == VAR_DECL
6800 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
6802 struct varpool_node *vnode = varpool_get_node (decl);
6803 if (vnode
6804 && vnode->resolution != LDPR_UNKNOWN)
6805 return resolution_to_local_definition_p (vnode->resolution);
6807 else if (TREE_CODE (decl) == FUNCTION_DECL)
6809 struct cgraph_node *node = cgraph_get_node_or_alias (decl);
6810 if (node
6811 && node->resolution != LDPR_UNKNOWN)
6812 return resolution_to_local_definition_p (node->resolution);
6814 /* Otherwise we have to assume the worst for DECL_WEAK (hidden weaks
6815 binds localy but still can be overwritten).
6816 This rely on fact that binds_local_p behave as decl_replaceable_p
6817 for all other declaration types. */
6818 return !DECL_WEAK (decl);
6821 /* A replaceable function or variable is one which may be replaced
6822 at link-time with an entirely different definition, provided that the
6823 replacement has the same type. For example, functions declared
6824 with __attribute__((weak)) on most systems are replaceable.
6826 COMDAT functions are not replaceable, since all definitions of the
6827 function must be equivalent. It is important that COMDAT functions
6828 not be treated as replaceable so that use of C++ template
6829 instantiations is not penalized. */
6831 bool
6832 decl_replaceable_p (tree decl)
6834 gcc_assert (DECL_P (decl));
6835 if (!TREE_PUBLIC (decl) || DECL_COMDAT (decl))
6836 return false;
6837 return !decl_binds_to_current_def_p (decl);
6840 /* Default function to output code that will globalize a label. A
6841 target must define GLOBAL_ASM_OP or provide its own function to
6842 globalize a label. */
6843 #ifdef GLOBAL_ASM_OP
6844 void
6845 default_globalize_label (FILE * stream, const char *name)
6847 fputs (GLOBAL_ASM_OP, stream);
6848 assemble_name (stream, name);
6849 putc ('\n', stream);
6851 #endif /* GLOBAL_ASM_OP */
6853 /* Default function to output code that will globalize a declaration. */
6854 void
6855 default_globalize_decl_name (FILE * stream, tree decl)
6857 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
6858 targetm.asm_out.globalize_label (stream, name);
6861 /* Default function to output a label for unwind information. The
6862 default is to do nothing. A target that needs nonlocal labels for
6863 unwind information must provide its own function to do this. */
6864 void
6865 default_emit_unwind_label (FILE * stream ATTRIBUTE_UNUSED,
6866 tree decl ATTRIBUTE_UNUSED,
6867 int for_eh ATTRIBUTE_UNUSED,
6868 int empty ATTRIBUTE_UNUSED)
6872 /* Default function to output a label to divide up the exception table.
6873 The default is to do nothing. A target that needs/wants to divide
6874 up the table must provide it's own function to do this. */
6875 void
6876 default_emit_except_table_label (FILE * stream ATTRIBUTE_UNUSED)
6880 /* This is how to output an internal numbered label where PREFIX is
6881 the class of label and LABELNO is the number within the class. */
6883 void
6884 default_generate_internal_label (char *buf, const char *prefix,
6885 unsigned long labelno)
6887 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
6890 /* This is how to output an internal numbered label where PREFIX is
6891 the class of label and LABELNO is the number within the class. */
6893 void
6894 default_internal_label (FILE *stream, const char *prefix,
6895 unsigned long labelno)
6897 char *const buf = (char *) alloca (40 + strlen (prefix));
6898 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
6899 ASM_OUTPUT_INTERNAL_LABEL (stream, buf);
6903 /* The default implementation of ASM_DECLARE_CONSTANT_NAME. */
6905 void
6906 default_asm_declare_constant_name (FILE *file, const char *name,
6907 const_tree exp ATTRIBUTE_UNUSED,
6908 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
6910 assemble_label (file, name);
6913 /* This is the default behavior at the beginning of a file. It's
6914 controlled by two other target-hook toggles. */
6915 void
6916 default_file_start (void)
6918 if (targetm.asm_file_start_app_off
6919 && !(flag_verbose_asm || flag_debug_asm || flag_dump_rtl_in_asm))
6920 fputs (ASM_APP_OFF, asm_out_file);
6922 if (targetm.asm_file_start_file_directive)
6923 output_file_directive (asm_out_file, main_input_filename);
6926 /* This is a generic routine suitable for use as TARGET_ASM_FILE_END
6927 which emits a special section directive used to indicate whether or
6928 not this object file needs an executable stack. This is primarily
6929 a GNU extension to ELF but could be used on other targets. */
6931 int trampolines_created;
6933 void
6934 file_end_indicate_exec_stack (void)
6936 unsigned int flags = SECTION_DEBUG;
6937 if (trampolines_created)
6938 flags |= SECTION_CODE;
6940 switch_to_section (get_section (".note.GNU-stack", flags, NULL));
6943 /* Emit a special section directive to indicate that this object file
6944 was compiled with -fsplit-stack. This is used to let the linker
6945 detect calls between split-stack code and non-split-stack code, so
6946 that it can modify the split-stack code to allocate a sufficiently
6947 large stack. We emit another special section if there are any
6948 functions in this file which have the no_split_stack attribute, to
6949 prevent the linker from warning about being unable to convert the
6950 functions if they call non-split-stack code. */
6952 void
6953 file_end_indicate_split_stack (void)
6955 if (flag_split_stack)
6957 switch_to_section (get_section (".note.GNU-split-stack", SECTION_DEBUG,
6958 NULL));
6959 if (saw_no_split_stack)
6960 switch_to_section (get_section (".note.GNU-no-split-stack",
6961 SECTION_DEBUG, NULL));
6965 /* Output DIRECTIVE (a C string) followed by a newline. This is used as
6966 a get_unnamed_section callback. */
6968 void
6969 output_section_asm_op (const void *directive)
6971 fprintf (asm_out_file, "%s\n", (const char *) directive);
6974 /* Emit assembly code to switch to section NEW_SECTION. Do nothing if
6975 the current section is NEW_SECTION. */
6977 void
6978 switch_to_section (section *new_section)
6980 if (in_section == new_section)
6981 return;
6983 if (new_section->common.flags & SECTION_FORGET)
6984 in_section = NULL;
6985 else
6986 in_section = new_section;
6988 switch (SECTION_STYLE (new_section))
6990 case SECTION_NAMED:
6991 targetm.asm_out.named_section (new_section->named.name,
6992 new_section->named.common.flags,
6993 new_section->named.decl);
6994 break;
6996 case SECTION_UNNAMED:
6997 new_section->unnamed.callback (new_section->unnamed.data);
6998 break;
7000 case SECTION_NOSWITCH:
7001 gcc_unreachable ();
7002 break;
7005 new_section->common.flags |= SECTION_DECLARED;
7008 /* If block symbol SYMBOL has not yet been assigned an offset, place
7009 it at the end of its block. */
7011 void
7012 place_block_symbol (rtx symbol)
7014 unsigned HOST_WIDE_INT size, mask, offset;
7015 struct constant_descriptor_rtx *desc;
7016 unsigned int alignment;
7017 struct object_block *block;
7018 tree decl;
7020 gcc_assert (SYMBOL_REF_BLOCK (symbol));
7021 if (SYMBOL_REF_BLOCK_OFFSET (symbol) >= 0)
7022 return;
7024 /* Work out the symbol's size and alignment. */
7025 if (CONSTANT_POOL_ADDRESS_P (symbol))
7027 desc = SYMBOL_REF_CONSTANT (symbol);
7028 alignment = desc->align;
7029 size = GET_MODE_SIZE (desc->mode);
7031 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
7033 decl = SYMBOL_REF_DECL (symbol);
7034 alignment = DECL_ALIGN (decl);
7035 size = get_constant_size (DECL_INITIAL (decl));
7037 else
7039 decl = SYMBOL_REF_DECL (symbol);
7040 alignment = DECL_ALIGN (decl);
7041 size = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
7044 /* Calculate the object's offset from the start of the block. */
7045 block = SYMBOL_REF_BLOCK (symbol);
7046 mask = alignment / BITS_PER_UNIT - 1;
7047 offset = (block->size + mask) & ~mask;
7048 SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
7050 /* Record the block's new alignment and size. */
7051 block->alignment = MAX (block->alignment, alignment);
7052 block->size = offset + size;
7054 VEC_safe_push (rtx, gc, block->objects, symbol);
7057 /* Return the anchor that should be used to address byte offset OFFSET
7058 from the first object in BLOCK. MODEL is the TLS model used
7059 to access it. */
7062 get_section_anchor (struct object_block *block, HOST_WIDE_INT offset,
7063 enum tls_model model)
7065 char label[100];
7066 unsigned int begin, middle, end;
7067 unsigned HOST_WIDE_INT min_offset, max_offset, range, bias, delta;
7068 rtx anchor;
7070 /* Work out the anchor's offset. Use an offset of 0 for the first
7071 anchor so that we don't pessimize the case where we take the address
7072 of a variable at the beginning of the block. This is particularly
7073 useful when a block has only one variable assigned to it.
7075 We try to place anchors RANGE bytes apart, so there can then be
7076 anchors at +/-RANGE, +/-2 * RANGE, and so on, up to the limits of
7077 a ptr_mode offset. With some target settings, the lowest such
7078 anchor might be out of range for the lowest ptr_mode offset;
7079 likewise the highest anchor for the highest offset. Use anchors
7080 at the extreme ends of the ptr_mode range in such cases.
7082 All arithmetic uses unsigned integers in order to avoid
7083 signed overflow. */
7084 max_offset = (unsigned HOST_WIDE_INT) targetm.max_anchor_offset;
7085 min_offset = (unsigned HOST_WIDE_INT) targetm.min_anchor_offset;
7086 range = max_offset - min_offset + 1;
7087 if (range == 0)
7088 offset = 0;
7089 else
7091 bias = 1 << (GET_MODE_BITSIZE (ptr_mode) - 1);
7092 if (offset < 0)
7094 delta = -(unsigned HOST_WIDE_INT) offset + max_offset;
7095 delta -= delta % range;
7096 if (delta > bias)
7097 delta = bias;
7098 offset = (HOST_WIDE_INT) (-delta);
7100 else
7102 delta = (unsigned HOST_WIDE_INT) offset - min_offset;
7103 delta -= delta % range;
7104 if (delta > bias - 1)
7105 delta = bias - 1;
7106 offset = (HOST_WIDE_INT) delta;
7110 /* Do a binary search to see if there's already an anchor we can use.
7111 Set BEGIN to the new anchor's index if not. */
7112 begin = 0;
7113 end = VEC_length (rtx, block->anchors);
7114 while (begin != end)
7116 middle = (end + begin) / 2;
7117 anchor = VEC_index (rtx, block->anchors, middle);
7118 if (SYMBOL_REF_BLOCK_OFFSET (anchor) > offset)
7119 end = middle;
7120 else if (SYMBOL_REF_BLOCK_OFFSET (anchor) < offset)
7121 begin = middle + 1;
7122 else if (SYMBOL_REF_TLS_MODEL (anchor) > model)
7123 end = middle;
7124 else if (SYMBOL_REF_TLS_MODEL (anchor) < model)
7125 begin = middle + 1;
7126 else
7127 return anchor;
7130 /* Create a new anchor with a unique label. */
7131 ASM_GENERATE_INTERNAL_LABEL (label, "LANCHOR", anchor_labelno++);
7132 anchor = create_block_symbol (ggc_strdup (label), block, offset);
7133 SYMBOL_REF_FLAGS (anchor) |= SYMBOL_FLAG_LOCAL | SYMBOL_FLAG_ANCHOR;
7134 SYMBOL_REF_FLAGS (anchor) |= model << SYMBOL_FLAG_TLS_SHIFT;
7136 /* Insert it at index BEGIN. */
7137 VEC_safe_insert (rtx, gc, block->anchors, begin, anchor);
7138 return anchor;
7141 /* Output the objects in BLOCK. */
7143 static void
7144 output_object_block (struct object_block *block)
7146 struct constant_descriptor_rtx *desc;
7147 unsigned int i;
7148 HOST_WIDE_INT offset;
7149 tree decl;
7150 rtx symbol;
7152 if (block->objects == NULL)
7153 return;
7155 /* Switch to the section and make sure that the first byte is
7156 suitably aligned. */
7157 switch_to_section (block->sect);
7158 assemble_align (block->alignment);
7160 /* Define the values of all anchors relative to the current section
7161 position. */
7162 FOR_EACH_VEC_ELT (rtx, block->anchors, i, symbol)
7163 targetm.asm_out.output_anchor (symbol);
7165 /* Output the objects themselves. */
7166 offset = 0;
7167 FOR_EACH_VEC_ELT (rtx, block->objects, i, symbol)
7169 /* Move to the object's offset, padding with zeros if necessary. */
7170 assemble_zeros (SYMBOL_REF_BLOCK_OFFSET (symbol) - offset);
7171 offset = SYMBOL_REF_BLOCK_OFFSET (symbol);
7172 if (CONSTANT_POOL_ADDRESS_P (symbol))
7174 desc = SYMBOL_REF_CONSTANT (symbol);
7175 output_constant_pool_1 (desc, 1);
7176 offset += GET_MODE_SIZE (desc->mode);
7178 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
7180 decl = SYMBOL_REF_DECL (symbol);
7181 assemble_constant_contents (DECL_INITIAL (decl), XSTR (symbol, 0),
7182 DECL_ALIGN (decl));
7183 offset += get_constant_size (DECL_INITIAL (decl));
7185 else
7187 decl = SYMBOL_REF_DECL (symbol);
7188 assemble_variable_contents (decl, XSTR (symbol, 0), false);
7189 offset += tree_low_cst (DECL_SIZE_UNIT (decl), 1);
7194 /* A htab_traverse callback used to call output_object_block for
7195 each member of object_block_htab. */
7197 static int
7198 output_object_block_htab (void **slot, void *data ATTRIBUTE_UNUSED)
7200 output_object_block ((struct object_block *) (*slot));
7201 return 1;
7204 /* Output the definitions of all object_blocks. */
7206 void
7207 output_object_blocks (void)
7209 htab_traverse (object_block_htab, output_object_block_htab, NULL);
7212 /* This function provides a possible implementation of the
7213 TARGET_ASM_RECORD_GCC_SWITCHES target hook for ELF targets. When triggered
7214 by -frecord-gcc-switches it creates a new mergeable, string section in the
7215 assembler output file called TARGET_ASM_RECORD_GCC_SWITCHES_SECTION which
7216 contains the switches in ASCII format.
7218 FIXME: This code does not correctly handle double quote characters
7219 that appear inside strings, (it strips them rather than preserving them).
7220 FIXME: ASM_OUTPUT_ASCII, as defined in config/elfos.h will not emit NUL
7221 characters - instead it treats them as sub-string separators. Since
7222 we want to emit NUL strings terminators into the object file we have to use
7223 ASM_OUTPUT_SKIP. */
7226 elf_record_gcc_switches (print_switch_type type, const char * name)
7228 switch (type)
7230 case SWITCH_TYPE_PASSED:
7231 ASM_OUTPUT_ASCII (asm_out_file, name, strlen (name));
7232 ASM_OUTPUT_SKIP (asm_out_file, (unsigned HOST_WIDE_INT) 1);
7233 break;
7235 case SWITCH_TYPE_DESCRIPTIVE:
7236 if (name == NULL)
7238 /* Distinguish between invocations where name is NULL. */
7239 static bool started = false;
7241 if (!started)
7243 section * sec;
7245 sec = get_section (targetm.asm_out.record_gcc_switches_section,
7246 SECTION_DEBUG
7247 | SECTION_MERGE
7248 | SECTION_STRINGS
7249 | (SECTION_ENTSIZE & 1),
7250 NULL);
7251 switch_to_section (sec);
7252 started = true;
7256 default:
7257 break;
7260 /* The return value is currently ignored by the caller, but must be 0.
7261 For -fverbose-asm the return value would be the number of characters
7262 emitted into the assembler file. */
7263 return 0;
7266 /* Emit text to declare externally defined symbols. It is needed to
7267 properly support non-default visibility. */
7268 void
7269 default_elf_asm_output_external (FILE *file ATTRIBUTE_UNUSED,
7270 tree decl,
7271 const char *name ATTRIBUTE_UNUSED)
7273 /* We output the name if and only if TREE_SYMBOL_REFERENCED is
7274 set in order to avoid putting out names that are never really
7275 used. */
7276 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
7277 && targetm.binds_local_p (decl))
7278 maybe_assemble_visibility (decl);
7281 /* The default hook for TARGET_ASM_OUTPUT_SOURCE_FILENAME. */
7283 void
7284 default_asm_output_source_filename (FILE *file, const char *name)
7286 #ifdef ASM_OUTPUT_SOURCE_FILENAME
7287 ASM_OUTPUT_SOURCE_FILENAME (file, name);
7288 #else
7289 fprintf (file, "\t.file\t");
7290 output_quoted_string (file, name);
7291 putc ('\n', file);
7292 #endif
7295 /* Output a file name in the form wanted by System V. */
7297 void
7298 output_file_directive (FILE *asm_file, const char *input_name)
7300 int len;
7301 const char *na;
7303 if (input_name == NULL)
7304 input_name = "<stdin>";
7305 else
7306 input_name = remap_debug_filename (input_name);
7308 len = strlen (input_name);
7309 na = input_name + len;
7311 /* NA gets INPUT_NAME sans directory names. */
7312 while (na > input_name)
7314 if (IS_DIR_SEPARATOR (na[-1]))
7315 break;
7316 na--;
7319 targetm.asm_out.output_source_filename (asm_file, na);
7322 /* Create a DEBUG_EXPR_DECL / DEBUG_EXPR pair from RTL expression
7323 EXP. */
7325 make_debug_expr_from_rtl (const_rtx exp)
7327 tree ddecl = make_node (DEBUG_EXPR_DECL), type;
7328 enum machine_mode mode = GET_MODE (exp);
7329 rtx dval;
7331 DECL_ARTIFICIAL (ddecl) = 1;
7332 if (REG_P (exp) && REG_EXPR (exp))
7333 type = TREE_TYPE (REG_EXPR (exp));
7334 else if (MEM_P (exp) && MEM_EXPR (exp))
7335 type = TREE_TYPE (MEM_EXPR (exp));
7336 else
7337 type = NULL_TREE;
7338 if (type && TYPE_MODE (type) == mode)
7339 TREE_TYPE (ddecl) = type;
7340 else
7341 TREE_TYPE (ddecl) = lang_hooks.types.type_for_mode (mode, 1);
7342 DECL_MODE (ddecl) = mode;
7343 dval = gen_rtx_DEBUG_EXPR (mode);
7344 DEBUG_EXPR_TREE_DECL (dval) = ddecl;
7345 SET_DECL_RTL (ddecl, dval);
7346 return dval;
7349 #include "gt-varasm.h"