2010-07-27 Paolo Carlini <paolo.carlini@oracle.com>
[official-gcc/alias-decl.git] / gcc / varasm.c
blob47423041bcde677d670e7bf01c4f9ca00474260e
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 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 "toplev.h"
44 #include "hashtab.h"
45 #include "ggc.h"
46 #include "langhooks.h"
47 #include "tm_p.h"
48 #include "debug.h"
49 #include "target.h"
50 #include "targhooks.h"
51 #include "tree-mudflap.h"
52 #include "cgraph.h"
53 #include "cfglayout.h"
54 #include "basic-block.h"
55 #include "tree-iterator.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 static const char *strip_reg_name (const char *);
103 static int contains_pointers_p (tree);
104 #ifdef ASM_OUTPUT_EXTERNAL
105 static bool incorporeal_function_p (tree);
106 #endif
107 static void decode_addr_const (tree, struct addr_const *);
108 static hashval_t const_desc_hash (const void *);
109 static int const_desc_eq (const void *, const void *);
110 static hashval_t const_hash_1 (const tree);
111 static int compare_constant (const tree, const tree);
112 static tree copy_constant (tree);
113 static void output_constant_def_contents (rtx);
114 static void output_addressed_constants (tree);
115 static unsigned HOST_WIDE_INT array_size_for_constructor (tree);
116 static unsigned min_align (unsigned, unsigned);
117 static void globalize_decl (tree);
118 #ifdef BSS_SECTION_ASM_OP
119 #ifdef ASM_OUTPUT_BSS
120 static void asm_output_bss (FILE *, tree, const char *,
121 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
122 #endif
123 #ifdef ASM_OUTPUT_ALIGNED_BSS
124 static void asm_output_aligned_bss (FILE *, tree, const char *,
125 unsigned HOST_WIDE_INT, int)
126 ATTRIBUTE_UNUSED;
127 #endif
128 #endif /* BSS_SECTION_ASM_OP */
129 static void mark_weak (tree);
130 static void output_constant_pool (const char *, tree);
132 /* Well-known sections, each one associated with some sort of *_ASM_OP. */
133 section *text_section;
134 section *data_section;
135 section *readonly_data_section;
136 section *sdata_section;
137 section *ctors_section;
138 section *dtors_section;
139 section *bss_section;
140 section *sbss_section;
142 /* Various forms of common section. All are guaranteed to be nonnull. */
143 section *tls_comm_section;
144 section *comm_section;
145 section *lcomm_section;
147 /* A SECTION_NOSWITCH section used for declaring global BSS variables.
148 May be null. */
149 section *bss_noswitch_section;
151 /* The section that holds the main exception table, when known. The section
152 is set either by the target's init_sections hook or by the first call to
153 switch_to_exception_section. */
154 section *exception_section;
156 /* The section that holds the DWARF2 frame unwind information, when known.
157 The section is set either by the target's init_sections hook or by the
158 first call to switch_to_eh_frame_section. */
159 section *eh_frame_section;
161 /* asm_out_file's current section. This is NULL if no section has yet
162 been selected or if we lose track of what the current section is. */
163 section *in_section;
165 /* True if code for the current function is currently being directed
166 at the cold section. */
167 bool in_cold_section_p;
169 /* A linked list of all the unnamed sections. */
170 static GTY(()) section *unnamed_sections;
172 /* Return a nonzero value if DECL has a section attribute. */
173 #define IN_NAMED_SECTION(DECL) \
174 ((TREE_CODE (DECL) == FUNCTION_DECL || TREE_CODE (DECL) == VAR_DECL) \
175 && DECL_SECTION_NAME (DECL) != NULL_TREE)
177 /* Hash table of named sections. */
178 static GTY((param_is (section))) htab_t section_htab;
180 /* A table of object_blocks, indexed by section. */
181 static GTY((param_is (struct object_block))) htab_t object_block_htab;
183 /* The next number to use for internal anchor labels. */
184 static GTY(()) int anchor_labelno;
186 /* A pool of constants that can be shared between functions. */
187 static GTY(()) struct rtx_constant_pool *shared_constant_pool;
189 /* Helper routines for maintaining section_htab. */
191 static int
192 section_entry_eq (const void *p1, const void *p2)
194 const section *old = (const section *) p1;
195 const char *new_name = (const char *) p2;
197 return strcmp (old->named.name, new_name) == 0;
200 static hashval_t
201 section_entry_hash (const void *p)
203 const section *old = (const section *) p;
204 return htab_hash_string (old->named.name);
207 /* Return a hash value for section SECT. */
209 static hashval_t
210 hash_section (section *sect)
212 if (sect->common.flags & SECTION_NAMED)
213 return htab_hash_string (sect->named.name);
214 return sect->common.flags;
217 /* Helper routines for maintaining object_block_htab. */
219 static int
220 object_block_entry_eq (const void *p1, const void *p2)
222 const struct object_block *old = (const struct object_block *) p1;
223 const section *new_section = (const section *) p2;
225 return old->sect == new_section;
228 static hashval_t
229 object_block_entry_hash (const void *p)
231 const struct object_block *old = (const struct object_block *) p;
232 return hash_section (old->sect);
235 /* Return a new unnamed section with the given fields. */
237 section *
238 get_unnamed_section (unsigned int flags, void (*callback) (const void *),
239 const void *data)
241 section *sect;
243 sect = ggc_alloc_section ();
244 sect->unnamed.common.flags = flags | SECTION_UNNAMED;
245 sect->unnamed.callback = callback;
246 sect->unnamed.data = data;
247 sect->unnamed.next = unnamed_sections;
249 unnamed_sections = sect;
250 return sect;
253 /* Return a SECTION_NOSWITCH section with the given fields. */
255 static section *
256 get_noswitch_section (unsigned int flags, noswitch_section_callback callback)
258 section *sect;
260 sect = ggc_alloc_section ();
261 sect->noswitch.common.flags = flags | SECTION_NOSWITCH;
262 sect->noswitch.callback = callback;
264 return sect;
267 /* Return the named section structure associated with NAME. Create
268 a new section with the given fields if no such structure exists. */
270 section *
271 get_section (const char *name, unsigned int flags, tree decl)
273 section *sect, **slot;
275 slot = (section **)
276 htab_find_slot_with_hash (section_htab, name,
277 htab_hash_string (name), INSERT);
278 flags |= SECTION_NAMED;
279 if (*slot == NULL)
281 sect = ggc_alloc_section ();
282 sect->named.common.flags = flags;
283 sect->named.name = ggc_strdup (name);
284 sect->named.decl = decl;
285 *slot = sect;
287 else
289 sect = *slot;
290 if ((sect->common.flags & ~SECTION_DECLARED) != flags
291 && ((sect->common.flags | flags) & SECTION_OVERRIDE) == 0)
293 /* Sanity check user variables for flag changes. */
294 if (decl == 0)
295 decl = sect->named.decl;
296 gcc_assert (decl);
297 error ("%+D causes a section type conflict", decl);
300 return sect;
303 /* Return true if the current compilation mode benefits from having
304 objects grouped into blocks. */
306 static bool
307 use_object_blocks_p (void)
309 return flag_section_anchors;
312 /* Return the object_block structure for section SECT. Create a new
313 structure if we haven't created one already. Return null if SECT
314 itself is null. */
316 static struct object_block *
317 get_block_for_section (section *sect)
319 struct object_block *block;
320 void **slot;
322 if (sect == NULL)
323 return NULL;
325 slot = htab_find_slot_with_hash (object_block_htab, sect,
326 hash_section (sect), INSERT);
327 block = (struct object_block *) *slot;
328 if (block == NULL)
330 block = ggc_alloc_cleared_object_block ();
331 block->sect = sect;
332 *slot = block;
334 return block;
337 /* Create a symbol with label LABEL and place it at byte offset
338 OFFSET in BLOCK. OFFSET can be negative if the symbol's offset
339 is not yet known. LABEL must be a garbage-collected string. */
341 static rtx
342 create_block_symbol (const char *label, struct object_block *block,
343 HOST_WIDE_INT offset)
345 rtx symbol;
346 unsigned int size;
348 /* Create the extended SYMBOL_REF. */
349 size = RTX_HDR_SIZE + sizeof (struct block_symbol);
350 symbol = ggc_alloc_zone_rtx_def (size, &rtl_zone);
352 /* Initialize the normal SYMBOL_REF fields. */
353 memset (symbol, 0, size);
354 PUT_CODE (symbol, SYMBOL_REF);
355 PUT_MODE (symbol, Pmode);
356 XSTR (symbol, 0) = label;
357 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_HAS_BLOCK_INFO;
359 /* Initialize the block_symbol stuff. */
360 SYMBOL_REF_BLOCK (symbol) = block;
361 SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
363 return symbol;
366 static void
367 initialize_cold_section_name (void)
369 const char *stripped_name;
370 char *name, *buffer;
371 tree dsn;
373 gcc_assert (cfun && current_function_decl);
374 if (crtl->subsections.unlikely_text_section_name)
375 return;
377 dsn = DECL_SECTION_NAME (current_function_decl);
378 if (flag_function_sections && dsn)
380 name = (char *) alloca (TREE_STRING_LENGTH (dsn) + 1);
381 memcpy (name, TREE_STRING_POINTER (dsn), TREE_STRING_LENGTH (dsn) + 1);
383 stripped_name = targetm.strip_name_encoding (name);
385 buffer = ACONCAT ((stripped_name, "_unlikely", NULL));
386 crtl->subsections.unlikely_text_section_name = ggc_strdup (buffer);
388 else
389 crtl->subsections.unlikely_text_section_name = UNLIKELY_EXECUTED_TEXT_SECTION_NAME;
392 /* Tell assembler to switch to unlikely-to-be-executed text section. */
394 section *
395 unlikely_text_section (void)
397 if (cfun)
399 if (!crtl->subsections.unlikely_text_section_name)
400 initialize_cold_section_name ();
402 return get_named_section (NULL, crtl->subsections.unlikely_text_section_name, 0);
404 else
405 return get_named_section (NULL, UNLIKELY_EXECUTED_TEXT_SECTION_NAME, 0);
408 /* When called within a function context, return true if the function
409 has been assigned a cold text section and if SECT is that section.
410 When called outside a function context, return true if SECT is the
411 default cold section. */
413 bool
414 unlikely_text_section_p (section *sect)
416 const char *name;
418 if (cfun)
419 name = crtl->subsections.unlikely_text_section_name;
420 else
421 name = UNLIKELY_EXECUTED_TEXT_SECTION_NAME;
423 return (name
424 && sect
425 && SECTION_STYLE (sect) == SECTION_NAMED
426 && strcmp (name, sect->named.name) == 0);
429 /* Return a section with a particular name and with whatever SECTION_*
430 flags section_type_flags deems appropriate. The name of the section
431 is taken from NAME if nonnull, otherwise it is taken from DECL's
432 DECL_SECTION_NAME. DECL is the decl associated with the section
433 (see the section comment for details) and RELOC is as for
434 section_type_flags. */
436 section *
437 get_named_section (tree decl, const char *name, int reloc)
439 unsigned int flags;
441 gcc_assert (!decl || DECL_P (decl));
442 if (name == NULL)
443 name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
445 flags = targetm.section_type_flags (decl, name, reloc);
447 return get_section (name, flags, decl);
450 /* If required, set DECL_SECTION_NAME to a unique name. */
452 void
453 resolve_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED,
454 int flag_function_or_data_sections)
456 if (DECL_SECTION_NAME (decl) == NULL_TREE
457 && targetm.have_named_sections
458 && (flag_function_or_data_sections
459 || DECL_ONE_ONLY (decl)))
460 targetm.asm_out.unique_section (decl, reloc);
463 #ifdef BSS_SECTION_ASM_OP
465 #ifdef ASM_OUTPUT_BSS
467 /* Utility function for ASM_OUTPUT_BSS for targets to use if
468 they don't support alignments in .bss.
469 ??? It is believed that this function will work in most cases so such
470 support is localized here. */
472 static void
473 asm_output_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
474 const char *name,
475 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
476 unsigned HOST_WIDE_INT rounded)
478 gcc_assert (strcmp (XSTR (XEXP (DECL_RTL (decl), 0), 0), name) == 0);
479 targetm.asm_out.globalize_decl_name (file, decl);
480 switch_to_section (bss_section);
481 #ifdef ASM_DECLARE_OBJECT_NAME
482 last_assemble_variable_decl = decl;
483 ASM_DECLARE_OBJECT_NAME (file, name, decl);
484 #else
485 /* Standard thing is just output label for the object. */
486 ASM_OUTPUT_LABEL (file, name);
487 #endif /* ASM_DECLARE_OBJECT_NAME */
488 ASM_OUTPUT_SKIP (file, rounded ? rounded : 1);
491 #endif
493 #ifdef ASM_OUTPUT_ALIGNED_BSS
495 /* Utility function for targets to use in implementing
496 ASM_OUTPUT_ALIGNED_BSS.
497 ??? It is believed that this function will work in most cases so such
498 support is localized here. */
500 static void
501 asm_output_aligned_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
502 const char *name, unsigned HOST_WIDE_INT size,
503 int align)
505 switch_to_section (bss_section);
506 ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
507 #ifdef ASM_DECLARE_OBJECT_NAME
508 last_assemble_variable_decl = decl;
509 ASM_DECLARE_OBJECT_NAME (file, name, decl);
510 #else
511 /* Standard thing is just output label for the object. */
512 ASM_OUTPUT_LABEL (file, name);
513 #endif /* ASM_DECLARE_OBJECT_NAME */
514 ASM_OUTPUT_SKIP (file, size ? size : 1);
517 #endif
519 #endif /* BSS_SECTION_ASM_OP */
521 #ifndef USE_SELECT_SECTION_FOR_FUNCTIONS
522 /* Return the hot section for function DECL. Return text_section for
523 null DECLs. */
525 static section *
526 hot_function_section (tree decl)
528 if (decl != NULL_TREE
529 && DECL_SECTION_NAME (decl) != NULL_TREE
530 && targetm.have_named_sections)
531 return get_named_section (decl, NULL, 0);
532 else
533 return text_section;
535 #endif
537 /* Return the section for function DECL.
539 If DECL is NULL_TREE, return the text section. We can be passed
540 NULL_TREE under some circumstances by dbxout.c at least. */
542 section *
543 function_section (tree decl)
545 int reloc = 0;
547 if (first_function_block_is_cold)
548 reloc = 1;
550 #ifdef USE_SELECT_SECTION_FOR_FUNCTIONS
551 if (decl != NULL_TREE
552 && DECL_SECTION_NAME (decl) != NULL_TREE)
553 return reloc ? unlikely_text_section ()
554 : get_named_section (decl, NULL, 0);
555 else
556 return targetm.asm_out.select_section (decl, reloc, DECL_ALIGN (decl));
557 #else
558 return reloc ? unlikely_text_section () : hot_function_section (decl);
559 #endif
562 section *
563 current_function_section (void)
565 #ifdef USE_SELECT_SECTION_FOR_FUNCTIONS
566 if (current_function_decl != NULL_TREE
567 && DECL_SECTION_NAME (current_function_decl) != NULL_TREE)
568 return in_cold_section_p ? unlikely_text_section ()
569 : get_named_section (current_function_decl,
570 NULL, 0);
571 else
572 return targetm.asm_out.select_section (current_function_decl,
573 in_cold_section_p,
574 DECL_ALIGN (current_function_decl));
575 #else
576 return (in_cold_section_p
577 ? unlikely_text_section ()
578 : hot_function_section (current_function_decl));
579 #endif
582 /* Return the read-only data section associated with function DECL. */
584 section *
585 default_function_rodata_section (tree decl)
587 if (decl != NULL_TREE && DECL_SECTION_NAME (decl))
589 const char *name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
591 if (DECL_ONE_ONLY (decl) && HAVE_COMDAT_GROUP)
593 const char *dot;
594 size_t len;
595 char* rname;
597 dot = strchr (name + 1, '.');
598 if (!dot)
599 dot = name;
600 len = strlen (dot) + 8;
601 rname = (char *) alloca (len);
603 strcpy (rname, ".rodata");
604 strcat (rname, dot);
605 return get_section (rname, SECTION_LINKONCE, decl);
607 /* For .gnu.linkonce.t.foo we want to use .gnu.linkonce.r.foo. */
608 else if (DECL_ONE_ONLY (decl)
609 && strncmp (name, ".gnu.linkonce.t.", 16) == 0)
611 size_t len = strlen (name) + 1;
612 char *rname = (char *) alloca (len);
614 memcpy (rname, name, len);
615 rname[14] = 'r';
616 return get_section (rname, SECTION_LINKONCE, decl);
618 /* For .text.foo we want to use .rodata.foo. */
619 else if (flag_function_sections && flag_data_sections
620 && strncmp (name, ".text.", 6) == 0)
622 size_t len = strlen (name) + 1;
623 char *rname = (char *) alloca (len + 2);
625 memcpy (rname, ".rodata", 7);
626 memcpy (rname + 7, name + 5, len - 5);
627 return get_section (rname, 0, decl);
631 return readonly_data_section;
634 /* Return the read-only data section associated with function DECL
635 for targets where that section should be always the single
636 readonly data section. */
638 section *
639 default_no_function_rodata_section (tree decl ATTRIBUTE_UNUSED)
641 return readonly_data_section;
644 /* Return the section to use for string merging. */
646 static section *
647 mergeable_string_section (tree decl ATTRIBUTE_UNUSED,
648 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
649 unsigned int flags ATTRIBUTE_UNUSED)
651 HOST_WIDE_INT len;
653 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
654 && TREE_CODE (decl) == STRING_CST
655 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
656 && align <= 256
657 && (len = int_size_in_bytes (TREE_TYPE (decl))) > 0
658 && TREE_STRING_LENGTH (decl) >= len)
660 enum machine_mode mode;
661 unsigned int modesize;
662 const char *str;
663 HOST_WIDE_INT i;
664 int j, unit;
665 char name[30];
667 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (decl)));
668 modesize = GET_MODE_BITSIZE (mode);
669 if (modesize >= 8 && modesize <= 256
670 && (modesize & (modesize - 1)) == 0)
672 if (align < modesize)
673 align = modesize;
675 str = TREE_STRING_POINTER (decl);
676 unit = GET_MODE_SIZE (mode);
678 /* Check for embedded NUL characters. */
679 for (i = 0; i < len; i += unit)
681 for (j = 0; j < unit; j++)
682 if (str[i + j] != '\0')
683 break;
684 if (j == unit)
685 break;
687 if (i == len - unit)
689 sprintf (name, ".rodata.str%d.%d", modesize / 8,
690 (int) (align / 8));
691 flags |= (modesize / 8) | SECTION_MERGE | SECTION_STRINGS;
692 return get_section (name, flags, NULL);
697 return readonly_data_section;
700 /* Return the section to use for constant merging. */
702 section *
703 mergeable_constant_section (enum machine_mode mode ATTRIBUTE_UNUSED,
704 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
705 unsigned int flags ATTRIBUTE_UNUSED)
707 unsigned int modesize = GET_MODE_BITSIZE (mode);
709 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
710 && mode != VOIDmode
711 && mode != BLKmode
712 && modesize <= align
713 && align >= 8
714 && align <= 256
715 && (align & (align - 1)) == 0)
717 char name[24];
719 sprintf (name, ".rodata.cst%d", (int) (align / 8));
720 flags |= (align / 8) | SECTION_MERGE;
721 return get_section (name, flags, NULL);
723 return readonly_data_section;
726 /* Given NAME, a putative register name, discard any customary prefixes. */
728 static const char *
729 strip_reg_name (const char *name)
731 #ifdef REGISTER_PREFIX
732 if (!strncmp (name, REGISTER_PREFIX, strlen (REGISTER_PREFIX)))
733 name += strlen (REGISTER_PREFIX);
734 #endif
735 if (name[0] == '%' || name[0] == '#')
736 name++;
737 return name;
740 /* The user has asked for a DECL to have a particular name. Set (or
741 change) it in such a way that we don't prefix an underscore to
742 it. */
743 void
744 set_user_assembler_name (tree decl, const char *name)
746 char *starred = (char *) alloca (strlen (name) + 2);
747 starred[0] = '*';
748 strcpy (starred + 1, name);
749 change_decl_assembler_name (decl, get_identifier (starred));
750 SET_DECL_RTL (decl, NULL_RTX);
753 /* Decode an `asm' spec for a declaration as a register name.
754 Return the register number, or -1 if nothing specified,
755 or -2 if the ASMSPEC is not `cc' or `memory' and is not recognized,
756 or -3 if ASMSPEC is `cc' and is not recognized,
757 or -4 if ASMSPEC is `memory' and is not recognized.
758 Accept an exact spelling or a decimal number.
759 Prefixes such as % are optional. */
762 decode_reg_name (const char *asmspec)
764 if (asmspec != 0)
766 int i;
768 /* Get rid of confusing prefixes. */
769 asmspec = strip_reg_name (asmspec);
771 /* Allow a decimal number as a "register name". */
772 for (i = strlen (asmspec) - 1; i >= 0; i--)
773 if (! ISDIGIT (asmspec[i]))
774 break;
775 if (asmspec[0] != 0 && i < 0)
777 i = atoi (asmspec);
778 if (i < FIRST_PSEUDO_REGISTER && i >= 0)
779 return i;
780 else
781 return -2;
784 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
785 if (reg_names[i][0]
786 && ! strcmp (asmspec, strip_reg_name (reg_names[i])))
787 return i;
789 #ifdef ADDITIONAL_REGISTER_NAMES
791 static const struct { const char *const name; const int number; } table[]
792 = ADDITIONAL_REGISTER_NAMES;
794 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
795 if (table[i].name[0]
796 && ! strcmp (asmspec, table[i].name))
797 return table[i].number;
799 #endif /* ADDITIONAL_REGISTER_NAMES */
801 if (!strcmp (asmspec, "memory"))
802 return -4;
804 if (!strcmp (asmspec, "cc"))
805 return -3;
807 return -2;
810 return -1;
813 /* Return true if DECL's initializer is suitable for a BSS section. */
815 static bool
816 bss_initializer_p (const_tree decl)
818 return (DECL_INITIAL (decl) == NULL
819 || DECL_INITIAL (decl) == error_mark_node
820 || (flag_zero_initialized_in_bss
821 /* Leave constant zeroes in .rodata so they
822 can be shared. */
823 && !TREE_READONLY (decl)
824 && initializer_zerop (DECL_INITIAL (decl))));
827 /* Compute the alignment of variable specified by DECL.
828 DONT_OUTPUT_DATA is from assemble_variable. */
830 void
831 align_variable (tree decl, bool dont_output_data)
833 unsigned int align = DECL_ALIGN (decl);
835 /* In the case for initialing an array whose length isn't specified,
836 where we have not yet been able to do the layout,
837 figure out the proper alignment now. */
838 if (dont_output_data && DECL_SIZE (decl) == 0
839 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
840 align = MAX (align, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (decl))));
842 /* Some object file formats have a maximum alignment which they support.
843 In particular, a.out format supports a maximum alignment of 4. */
844 if (align > MAX_OFILE_ALIGNMENT)
846 warning (0, "alignment of %q+D is greater than maximum object "
847 "file alignment. Using %d", decl,
848 MAX_OFILE_ALIGNMENT/BITS_PER_UNIT);
849 align = MAX_OFILE_ALIGNMENT;
852 /* On some machines, it is good to increase alignment sometimes. */
853 if (! DECL_USER_ALIGN (decl))
855 #ifdef DATA_ALIGNMENT
856 unsigned int data_align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
857 /* Don't increase alignment too much for TLS variables - TLS space
858 is too precious. */
859 if (! DECL_THREAD_LOCAL_P (decl) || data_align <= BITS_PER_WORD)
860 align = data_align;
861 #endif
862 #ifdef CONSTANT_ALIGNMENT
863 if (DECL_INITIAL (decl) != 0 && DECL_INITIAL (decl) != error_mark_node)
865 unsigned int const_align = CONSTANT_ALIGNMENT (DECL_INITIAL (decl),
866 align);
867 /* Don't increase alignment too much for TLS variables - TLS space
868 is too precious. */
869 if (! DECL_THREAD_LOCAL_P (decl) || const_align <= BITS_PER_WORD)
870 align = const_align;
872 #endif
875 /* Reset the alignment in case we have made it tighter, so we can benefit
876 from it in get_pointer_alignment. */
877 DECL_ALIGN (decl) = align;
880 /* Return the section into which the given VAR_DECL or CONST_DECL
881 should be placed. PREFER_NOSWITCH_P is true if a noswitch
882 section should be used wherever possible. */
884 static section *
885 get_variable_section (tree decl, bool prefer_noswitch_p)
887 addr_space_t as = ADDR_SPACE_GENERIC;
888 int reloc;
890 if (TREE_TYPE (decl) != error_mark_node)
891 as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
893 if (DECL_COMMON (decl))
895 /* If the decl has been given an explicit section name, or it resides
896 in a non-generic address space, then it isn't common, and shouldn't
897 be handled as such. */
898 gcc_assert (DECL_SECTION_NAME (decl) == NULL
899 && ADDR_SPACE_GENERIC_P (as));
900 if (DECL_THREAD_LOCAL_P (decl))
901 return tls_comm_section;
902 else if (TREE_PUBLIC (decl) && bss_initializer_p (decl))
903 return comm_section;
906 if (DECL_INITIAL (decl) == error_mark_node)
907 reloc = contains_pointers_p (TREE_TYPE (decl)) ? 3 : 0;
908 else if (DECL_INITIAL (decl))
909 reloc = compute_reloc_for_constant (DECL_INITIAL (decl));
910 else
911 reloc = 0;
913 resolve_unique_section (decl, reloc, flag_data_sections);
914 if (IN_NAMED_SECTION (decl))
915 return get_named_section (decl, NULL, reloc);
917 if (ADDR_SPACE_GENERIC_P (as)
918 && !DECL_THREAD_LOCAL_P (decl)
919 && !(prefer_noswitch_p && targetm.have_switchable_bss_sections)
920 && bss_initializer_p (decl))
922 if (!TREE_PUBLIC (decl))
923 return lcomm_section;
924 if (bss_noswitch_section)
925 return bss_noswitch_section;
928 return targetm.asm_out.select_section (decl, reloc, DECL_ALIGN (decl));
931 /* Return the block into which object_block DECL should be placed. */
933 static struct object_block *
934 get_block_for_decl (tree decl)
936 section *sect;
938 if (TREE_CODE (decl) == VAR_DECL)
940 /* The object must be defined in this translation unit. */
941 if (DECL_EXTERNAL (decl))
942 return NULL;
944 /* There's no point using object blocks for something that is
945 isolated by definition. */
946 if (DECL_ONE_ONLY (decl))
947 return NULL;
950 /* We can only calculate block offsets if the decl has a known
951 constant size. */
952 if (DECL_SIZE_UNIT (decl) == NULL)
953 return NULL;
954 if (!host_integerp (DECL_SIZE_UNIT (decl), 1))
955 return NULL;
957 /* Find out which section should contain DECL. We cannot put it into
958 an object block if it requires a standalone definition. */
959 if (TREE_CODE (decl) == VAR_DECL)
960 align_variable (decl, 0);
961 sect = get_variable_section (decl, true);
962 if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
963 return NULL;
965 return get_block_for_section (sect);
968 /* Make sure block symbol SYMBOL is in block BLOCK. */
970 static void
971 change_symbol_block (rtx symbol, struct object_block *block)
973 if (block != SYMBOL_REF_BLOCK (symbol))
975 gcc_assert (SYMBOL_REF_BLOCK_OFFSET (symbol) < 0);
976 SYMBOL_REF_BLOCK (symbol) = block;
980 /* Return true if it is possible to put DECL in an object_block. */
982 static bool
983 use_blocks_for_decl_p (tree decl)
985 /* Only data DECLs can be placed into object blocks. */
986 if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != CONST_DECL)
987 return false;
989 /* Detect decls created by dw2_force_const_mem. Such decls are
990 special because DECL_INITIAL doesn't specify the decl's true value.
991 dw2_output_indirect_constants will instead call assemble_variable
992 with dont_output_data set to 1 and then print the contents itself. */
993 if (DECL_INITIAL (decl) == decl)
994 return false;
996 /* If this decl is an alias, then we don't want to emit a definition. */
997 if (lookup_attribute ("alias", DECL_ATTRIBUTES (decl)))
998 return false;
1000 return true;
1003 /* Create the DECL_RTL for a VAR_DECL or FUNCTION_DECL. DECL should
1004 have static storage duration. In other words, it should not be an
1005 automatic variable, including PARM_DECLs.
1007 There is, however, one exception: this function handles variables
1008 explicitly placed in a particular register by the user.
1010 This is never called for PARM_DECL nodes. */
1012 void
1013 make_decl_rtl (tree decl)
1015 const char *name = 0;
1016 int reg_number;
1017 rtx x;
1019 /* Check that we are not being given an automatic variable. */
1020 gcc_assert (TREE_CODE (decl) != PARM_DECL
1021 && TREE_CODE (decl) != RESULT_DECL);
1023 /* A weak alias has TREE_PUBLIC set but not the other bits. */
1024 gcc_assert (TREE_CODE (decl) != VAR_DECL
1025 || TREE_STATIC (decl)
1026 || TREE_PUBLIC (decl)
1027 || DECL_EXTERNAL (decl)
1028 || DECL_REGISTER (decl));
1030 /* And that we were not given a type or a label. */
1031 gcc_assert (TREE_CODE (decl) != TYPE_DECL
1032 && TREE_CODE (decl) != LABEL_DECL);
1034 /* For a duplicate declaration, we can be called twice on the
1035 same DECL node. Don't discard the RTL already made. */
1036 if (DECL_RTL_SET_P (decl))
1038 /* If the old RTL had the wrong mode, fix the mode. */
1039 x = DECL_RTL (decl);
1040 if (GET_MODE (x) != DECL_MODE (decl))
1041 SET_DECL_RTL (decl, adjust_address_nv (x, DECL_MODE (decl), 0));
1043 if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1044 return;
1046 /* ??? Another way to do this would be to maintain a hashed
1047 table of such critters. Instead of adding stuff to a DECL
1048 to give certain attributes to it, we could use an external
1049 hash map from DECL to set of attributes. */
1051 /* Let the target reassign the RTL if it wants.
1052 This is necessary, for example, when one machine specific
1053 decl attribute overrides another. */
1054 targetm.encode_section_info (decl, DECL_RTL (decl), false);
1056 /* If the symbol has a SYMBOL_REF_BLOCK field, update it based
1057 on the new decl information. */
1058 if (MEM_P (x)
1059 && GET_CODE (XEXP (x, 0)) == SYMBOL_REF
1060 && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (x, 0)))
1061 change_symbol_block (XEXP (x, 0), get_block_for_decl (decl));
1063 /* Make this function static known to the mudflap runtime. */
1064 if (flag_mudflap && TREE_CODE (decl) == VAR_DECL)
1065 mudflap_enqueue_decl (decl);
1067 return;
1070 /* If this variable belongs to the global constant pool, retrieve the
1071 pre-computed RTL or recompute it in LTO mode. */
1072 if (TREE_CODE (decl) == VAR_DECL && DECL_IN_CONSTANT_POOL (decl))
1074 SET_DECL_RTL (decl, output_constant_def (DECL_INITIAL (decl), 1));
1075 return;
1078 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
1080 if (name[0] != '*' && TREE_CODE (decl) != FUNCTION_DECL
1081 && DECL_REGISTER (decl))
1083 error ("register name not specified for %q+D", decl);
1085 else if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1087 const char *asmspec = name+1;
1088 reg_number = decode_reg_name (asmspec);
1089 /* First detect errors in declaring global registers. */
1090 if (reg_number == -1)
1091 error ("register name not specified for %q+D", decl);
1092 else if (reg_number < 0)
1093 error ("invalid register name for %q+D", decl);
1094 else if (TYPE_MODE (TREE_TYPE (decl)) == BLKmode)
1095 error ("data type of %q+D isn%'t suitable for a register",
1096 decl);
1097 else if (! HARD_REGNO_MODE_OK (reg_number, TYPE_MODE (TREE_TYPE (decl))))
1098 error ("register specified for %q+D isn%'t suitable for data type",
1099 decl);
1100 /* Now handle properly declared static register variables. */
1101 else
1103 int nregs;
1105 if (DECL_INITIAL (decl) != 0 && TREE_STATIC (decl))
1107 DECL_INITIAL (decl) = 0;
1108 error ("global register variable has initial value");
1110 if (TREE_THIS_VOLATILE (decl))
1111 warning (OPT_Wvolatile_register_var,
1112 "optimization may eliminate reads and/or "
1113 "writes to register variables");
1115 /* If the user specified one of the eliminables registers here,
1116 e.g., FRAME_POINTER_REGNUM, we don't want to get this variable
1117 confused with that register and be eliminated. This usage is
1118 somewhat suspect... */
1120 SET_DECL_RTL (decl, gen_rtx_raw_REG (DECL_MODE (decl), reg_number));
1121 ORIGINAL_REGNO (DECL_RTL (decl)) = reg_number;
1122 REG_USERVAR_P (DECL_RTL (decl)) = 1;
1124 if (TREE_STATIC (decl))
1126 /* Make this register global, so not usable for anything
1127 else. */
1128 #ifdef ASM_DECLARE_REGISTER_GLOBAL
1129 name = IDENTIFIER_POINTER (DECL_NAME (decl));
1130 ASM_DECLARE_REGISTER_GLOBAL (asm_out_file, decl, reg_number, name);
1131 #endif
1132 nregs = hard_regno_nregs[reg_number][DECL_MODE (decl)];
1133 while (nregs > 0)
1134 globalize_reg (reg_number + --nregs);
1137 /* As a register variable, it has no section. */
1138 return;
1141 /* Now handle ordinary static variables and functions (in memory).
1142 Also handle vars declared register invalidly. */
1143 else if (name[0] == '*')
1145 #ifdef REGISTER_PREFIX
1146 if (strlen (REGISTER_PREFIX) != 0)
1148 reg_number = decode_reg_name (name);
1149 if (reg_number >= 0 || reg_number == -3)
1150 error ("register name given for non-register variable %q+D", decl);
1152 #endif
1155 /* Specifying a section attribute on a variable forces it into a
1156 non-.bss section, and thus it cannot be common. */
1157 /* FIXME: In general this code should not be necessary because
1158 visibility pass is doing the same work. But notice_global_symbol
1159 is called early and it needs to make DECL_RTL to get the name.
1160 we take care of recomputing the DECL_RTL after visibility is changed. */
1161 if (TREE_CODE (decl) == VAR_DECL
1162 && DECL_SECTION_NAME (decl) != NULL_TREE
1163 && DECL_INITIAL (decl) == NULL_TREE
1164 && DECL_COMMON (decl))
1165 DECL_COMMON (decl) = 0;
1167 /* Variables can't be both common and weak. */
1168 if (TREE_CODE (decl) == VAR_DECL && DECL_WEAK (decl))
1169 DECL_COMMON (decl) = 0;
1171 if (use_object_blocks_p () && use_blocks_for_decl_p (decl))
1172 x = create_block_symbol (name, get_block_for_decl (decl), -1);
1173 else
1175 enum machine_mode address_mode = Pmode;
1176 if (TREE_TYPE (decl) != error_mark_node)
1178 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
1179 address_mode = targetm.addr_space.address_mode (as);
1181 x = gen_rtx_SYMBOL_REF (address_mode, name);
1183 SYMBOL_REF_WEAK (x) = DECL_WEAK (decl);
1184 SET_SYMBOL_REF_DECL (x, decl);
1186 x = gen_rtx_MEM (DECL_MODE (decl), x);
1187 if (TREE_CODE (decl) != FUNCTION_DECL)
1188 set_mem_attributes (x, decl, 1);
1189 SET_DECL_RTL (decl, x);
1191 /* Optionally set flags or add text to the name to record information
1192 such as that it is a function name.
1193 If the name is changed, the macro ASM_OUTPUT_LABELREF
1194 will have to know how to strip this information. */
1195 targetm.encode_section_info (decl, DECL_RTL (decl), true);
1197 /* Make this function static known to the mudflap runtime. */
1198 if (flag_mudflap && TREE_CODE (decl) == VAR_DECL)
1199 mudflap_enqueue_decl (decl);
1202 /* Like make_decl_rtl, but inhibit creation of new alias sets when
1203 calling make_decl_rtl. Also, reset DECL_RTL before returning the
1204 rtl. */
1207 make_decl_rtl_for_debug (tree decl)
1209 unsigned int save_aliasing_flag, save_mudflap_flag;
1210 rtx rtl;
1212 if (DECL_RTL_SET_P (decl))
1213 return DECL_RTL (decl);
1215 /* Kludge alert! Somewhere down the call chain, make_decl_rtl will
1216 call new_alias_set. If running with -fcompare-debug, sometimes
1217 we do not want to create alias sets that will throw the alias
1218 numbers off in the comparison dumps. So... clearing
1219 flag_strict_aliasing will keep new_alias_set() from creating a
1220 new set. It is undesirable to register decl with mudflap
1221 in this case as well. */
1222 save_aliasing_flag = flag_strict_aliasing;
1223 flag_strict_aliasing = 0;
1224 save_mudflap_flag = flag_mudflap;
1225 flag_mudflap = 0;
1227 rtl = DECL_RTL (decl);
1228 /* Reset DECL_RTL back, as various parts of the compiler expects
1229 DECL_RTL set meaning it is actually going to be output. */
1230 SET_DECL_RTL (decl, NULL);
1232 flag_strict_aliasing = save_aliasing_flag;
1233 flag_mudflap = save_mudflap_flag;
1235 return rtl;
1238 /* Output a string of literal assembler code
1239 for an `asm' keyword used between functions. */
1241 void
1242 assemble_asm (tree string)
1244 app_enable ();
1246 if (TREE_CODE (string) == ADDR_EXPR)
1247 string = TREE_OPERAND (string, 0);
1249 fprintf (asm_out_file, "\t%s\n", TREE_STRING_POINTER (string));
1252 /* Record an element in the table of global destructors. SYMBOL is
1253 a SYMBOL_REF of the function to be called; PRIORITY is a number
1254 between 0 and MAX_INIT_PRIORITY. */
1256 void
1257 default_stabs_asm_out_destructor (rtx symbol ATTRIBUTE_UNUSED,
1258 int priority ATTRIBUTE_UNUSED)
1260 #if defined DBX_DEBUGGING_INFO || defined XCOFF_DEBUGGING_INFO
1261 /* Tell GNU LD that this is part of the static destructor set.
1262 This will work for any system that uses stabs, most usefully
1263 aout systems. */
1264 dbxout_begin_simple_stabs ("___DTOR_LIST__", 22 /* N_SETT */);
1265 dbxout_stab_value_label (XSTR (symbol, 0));
1266 #else
1267 sorry ("global destructors not supported on this target");
1268 #endif
1271 /* Write the address of the entity given by SYMBOL to SEC. */
1272 void
1273 assemble_addr_to_section (rtx symbol, section *sec)
1275 switch_to_section (sec);
1276 assemble_align (POINTER_SIZE);
1277 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1280 /* Return the numbered .ctors.N (if CONSTRUCTOR_P) or .dtors.N (if
1281 not) section for PRIORITY. */
1282 section *
1283 get_cdtor_priority_section (int priority, bool constructor_p)
1285 char buf[16];
1287 /* ??? This only works reliably with the GNU linker. */
1288 sprintf (buf, "%s.%.5u",
1289 constructor_p ? ".ctors" : ".dtors",
1290 /* Invert the numbering so the linker puts us in the proper
1291 order; constructors are run from right to left, and the
1292 linker sorts in increasing order. */
1293 MAX_INIT_PRIORITY - priority);
1294 return get_section (buf, SECTION_WRITE, NULL);
1297 void
1298 default_named_section_asm_out_destructor (rtx symbol, int priority)
1300 section *sec;
1302 if (priority != DEFAULT_INIT_PRIORITY)
1303 sec = get_cdtor_priority_section (priority,
1304 /*constructor_p=*/false);
1305 else
1306 sec = get_section (".dtors", SECTION_WRITE, NULL);
1308 assemble_addr_to_section (symbol, sec);
1311 #ifdef DTORS_SECTION_ASM_OP
1312 void
1313 default_dtor_section_asm_out_destructor (rtx symbol,
1314 int priority ATTRIBUTE_UNUSED)
1316 assemble_addr_to_section (symbol, dtors_section);
1318 #endif
1320 /* Likewise for global constructors. */
1322 void
1323 default_stabs_asm_out_constructor (rtx symbol ATTRIBUTE_UNUSED,
1324 int priority ATTRIBUTE_UNUSED)
1326 #if defined DBX_DEBUGGING_INFO || defined XCOFF_DEBUGGING_INFO
1327 /* Tell GNU LD that this is part of the static destructor set.
1328 This will work for any system that uses stabs, most usefully
1329 aout systems. */
1330 dbxout_begin_simple_stabs ("___CTOR_LIST__", 22 /* N_SETT */);
1331 dbxout_stab_value_label (XSTR (symbol, 0));
1332 #else
1333 sorry ("global constructors not supported on this target");
1334 #endif
1337 void
1338 default_named_section_asm_out_constructor (rtx symbol, int priority)
1340 section *sec;
1342 if (priority != DEFAULT_INIT_PRIORITY)
1343 sec = get_cdtor_priority_section (priority,
1344 /*constructor_p=*/true);
1345 else
1346 sec = get_section (".ctors", SECTION_WRITE, NULL);
1348 assemble_addr_to_section (symbol, sec);
1351 #ifdef CTORS_SECTION_ASM_OP
1352 void
1353 default_ctor_section_asm_out_constructor (rtx symbol,
1354 int priority ATTRIBUTE_UNUSED)
1356 assemble_addr_to_section (symbol, ctors_section);
1358 #endif
1360 /* CONSTANT_POOL_BEFORE_FUNCTION may be defined as an expression with
1361 a nonzero value if the constant pool should be output before the
1362 start of the function, or a zero value if the pool should output
1363 after the end of the function. The default is to put it before the
1364 start. */
1366 #ifndef CONSTANT_POOL_BEFORE_FUNCTION
1367 #define CONSTANT_POOL_BEFORE_FUNCTION 1
1368 #endif
1370 /* DECL is an object (either VAR_DECL or FUNCTION_DECL) which is going
1371 to be output to assembler.
1372 Set first_global_object_name and weak_global_object_name as appropriate. */
1374 void
1375 notice_global_symbol (tree decl)
1377 const char **type = &first_global_object_name;
1379 if (first_global_object_name
1380 || !TREE_PUBLIC (decl)
1381 || DECL_EXTERNAL (decl)
1382 || !DECL_NAME (decl)
1383 || (TREE_CODE (decl) != FUNCTION_DECL
1384 && (TREE_CODE (decl) != VAR_DECL
1385 || (DECL_COMMON (decl)
1386 && (DECL_INITIAL (decl) == 0
1387 || DECL_INITIAL (decl) == error_mark_node))))
1388 || !MEM_P (DECL_RTL (decl)))
1389 return;
1391 /* We win when global object is found, but it is useful to know about weak
1392 symbol as well so we can produce nicer unique names. */
1393 if (DECL_WEAK (decl) || DECL_ONE_ONLY (decl) || flag_shlib)
1394 type = &weak_global_object_name;
1396 if (!*type)
1398 const char *p;
1399 const char *name;
1400 rtx decl_rtl = DECL_RTL (decl);
1402 p = targetm.strip_name_encoding (XSTR (XEXP (decl_rtl, 0), 0));
1403 name = ggc_strdup (p);
1405 *type = name;
1409 /* Output assembler code for the constant pool of a function and associated
1410 with defining the name of the function. DECL describes the function.
1411 NAME is the function's name. For the constant pool, we use the current
1412 constant pool data. */
1414 void
1415 assemble_start_function (tree decl, const char *fnname)
1417 int align;
1418 char tmp_label[100];
1419 bool hot_label_written = false;
1421 crtl->subsections.unlikely_text_section_name = NULL;
1423 first_function_block_is_cold = false;
1424 if (flag_reorder_blocks_and_partition)
1426 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTB", const_labelno);
1427 crtl->subsections.hot_section_label = ggc_strdup (tmp_label);
1428 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDB", const_labelno);
1429 crtl->subsections.cold_section_label = ggc_strdup (tmp_label);
1430 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTE", const_labelno);
1431 crtl->subsections.hot_section_end_label = ggc_strdup (tmp_label);
1432 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDE", const_labelno);
1433 crtl->subsections.cold_section_end_label = ggc_strdup (tmp_label);
1434 const_labelno++;
1436 else
1438 crtl->subsections.hot_section_label = NULL;
1439 crtl->subsections.cold_section_label = NULL;
1440 crtl->subsections.hot_section_end_label = NULL;
1441 crtl->subsections.cold_section_end_label = NULL;
1444 /* The following code does not need preprocessing in the assembler. */
1446 app_disable ();
1448 if (CONSTANT_POOL_BEFORE_FUNCTION)
1449 output_constant_pool (fnname, decl);
1451 resolve_unique_section (decl, 0, flag_function_sections);
1453 /* Make sure the not and cold text (code) sections are properly
1454 aligned. This is necessary here in the case where the function
1455 has both hot and cold sections, because we don't want to re-set
1456 the alignment when the section switch happens mid-function. */
1458 if (flag_reorder_blocks_and_partition)
1460 switch_to_section (unlikely_text_section ());
1461 assemble_align (DECL_ALIGN (decl));
1462 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_label);
1464 /* When the function starts with a cold section, we need to explicitly
1465 align the hot section and write out the hot section label.
1466 But if the current function is a thunk, we do not have a CFG. */
1467 if (!cfun->is_thunk
1468 && BB_PARTITION (ENTRY_BLOCK_PTR->next_bb) == BB_COLD_PARTITION)
1470 switch_to_section (text_section);
1471 assemble_align (DECL_ALIGN (decl));
1472 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
1473 hot_label_written = true;
1474 first_function_block_is_cold = true;
1477 else if (DECL_SECTION_NAME (decl))
1479 /* Calls to function_section rely on first_function_block_is_cold
1480 being accurate. The first block may be cold even if we aren't
1481 doing partitioning, if the entire function was decided by
1482 choose_function_section (predict.c) to be cold. */
1484 initialize_cold_section_name ();
1486 if (crtl->subsections.unlikely_text_section_name
1487 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
1488 crtl->subsections.unlikely_text_section_name) == 0)
1489 first_function_block_is_cold = true;
1492 in_cold_section_p = first_function_block_is_cold;
1494 /* Switch to the correct text section for the start of the function. */
1496 switch_to_section (function_section (decl));
1497 if (flag_reorder_blocks_and_partition
1498 && !hot_label_written)
1499 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
1501 /* Tell assembler to move to target machine's alignment for functions. */
1502 align = floor_log2 (DECL_ALIGN (decl) / BITS_PER_UNIT);
1503 if (align > 0)
1505 ASM_OUTPUT_ALIGN (asm_out_file, align);
1508 /* Handle a user-specified function alignment.
1509 Note that we still need to align to DECL_ALIGN, as above,
1510 because ASM_OUTPUT_MAX_SKIP_ALIGN might not do any alignment at all. */
1511 if (! DECL_USER_ALIGN (decl)
1512 && align_functions_log > align
1513 && optimize_function_for_speed_p (cfun))
1515 #ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
1516 ASM_OUTPUT_MAX_SKIP_ALIGN (asm_out_file,
1517 align_functions_log, align_functions - 1);
1518 #else
1519 ASM_OUTPUT_ALIGN (asm_out_file, align_functions_log);
1520 #endif
1523 #ifdef ASM_OUTPUT_FUNCTION_PREFIX
1524 ASM_OUTPUT_FUNCTION_PREFIX (asm_out_file, fnname);
1525 #endif
1527 if (!DECL_IGNORED_P (decl))
1528 (*debug_hooks->begin_function) (decl);
1530 /* Make function name accessible from other files, if appropriate. */
1532 if (TREE_PUBLIC (decl))
1534 notice_global_symbol (decl);
1536 globalize_decl (decl);
1538 maybe_assemble_visibility (decl);
1541 if (DECL_PRESERVE_P (decl))
1542 targetm.asm_out.mark_decl_preserved (fnname);
1544 /* Do any machine/system dependent processing of the function name. */
1545 #ifdef ASM_DECLARE_FUNCTION_NAME
1546 ASM_DECLARE_FUNCTION_NAME (asm_out_file, fnname, current_function_decl);
1547 #else
1548 /* Standard thing is just output label for the function. */
1549 ASM_OUTPUT_FUNCTION_LABEL (asm_out_file, fnname, current_function_decl);
1550 #endif /* ASM_DECLARE_FUNCTION_NAME */
1553 /* Output assembler code associated with defining the size of the
1554 function. DECL describes the function. NAME is the function's name. */
1556 void
1557 assemble_end_function (tree decl, const char *fnname ATTRIBUTE_UNUSED)
1559 #ifdef ASM_DECLARE_FUNCTION_SIZE
1560 /* We could have switched section in the middle of the function. */
1561 if (flag_reorder_blocks_and_partition)
1562 switch_to_section (function_section (decl));
1563 ASM_DECLARE_FUNCTION_SIZE (asm_out_file, fnname, decl);
1564 #endif
1565 if (! CONSTANT_POOL_BEFORE_FUNCTION)
1567 output_constant_pool (fnname, decl);
1568 switch_to_section (function_section (decl)); /* need to switch back */
1570 /* Output labels for end of hot/cold text sections (to be used by
1571 debug info.) */
1572 if (flag_reorder_blocks_and_partition)
1574 section *save_text_section;
1576 save_text_section = in_section;
1577 switch_to_section (unlikely_text_section ());
1578 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_end_label);
1579 if (first_function_block_is_cold)
1580 switch_to_section (text_section);
1581 else
1582 switch_to_section (function_section (decl));
1583 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_end_label);
1584 switch_to_section (save_text_section);
1588 /* Assemble code to leave SIZE bytes of zeros. */
1590 void
1591 assemble_zeros (unsigned HOST_WIDE_INT size)
1593 /* Do no output if -fsyntax-only. */
1594 if (flag_syntax_only)
1595 return;
1597 #ifdef ASM_NO_SKIP_IN_TEXT
1598 /* The `space' pseudo in the text section outputs nop insns rather than 0s,
1599 so we must output 0s explicitly in the text section. */
1600 if (ASM_NO_SKIP_IN_TEXT && (in_section->common.flags & SECTION_CODE) != 0)
1602 unsigned HOST_WIDE_INT i;
1603 for (i = 0; i < size; i++)
1604 assemble_integer (const0_rtx, 1, BITS_PER_UNIT, 1);
1606 else
1607 #endif
1608 if (size > 0)
1609 ASM_OUTPUT_SKIP (asm_out_file, size);
1612 /* Assemble an alignment pseudo op for an ALIGN-bit boundary. */
1614 void
1615 assemble_align (int align)
1617 if (align > BITS_PER_UNIT)
1619 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
1623 /* Assemble a string constant with the specified C string as contents. */
1625 void
1626 assemble_string (const char *p, int size)
1628 int pos = 0;
1629 int maximum = 2000;
1631 /* If the string is very long, split it up. */
1633 while (pos < size)
1635 int thissize = size - pos;
1636 if (thissize > maximum)
1637 thissize = maximum;
1639 ASM_OUTPUT_ASCII (asm_out_file, p, thissize);
1641 pos += thissize;
1642 p += thissize;
1647 /* A noswitch_section_callback for lcomm_section. */
1649 static bool
1650 emit_local (tree decl ATTRIBUTE_UNUSED,
1651 const char *name ATTRIBUTE_UNUSED,
1652 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1653 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1655 #if defined ASM_OUTPUT_ALIGNED_DECL_LOCAL
1656 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, decl, name,
1657 size, DECL_ALIGN (decl));
1658 return true;
1659 #elif defined ASM_OUTPUT_ALIGNED_LOCAL
1660 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, DECL_ALIGN (decl));
1661 return true;
1662 #else
1663 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
1664 return false;
1665 #endif
1668 /* A noswitch_section_callback for bss_noswitch_section. */
1670 #if defined ASM_OUTPUT_ALIGNED_BSS || defined ASM_OUTPUT_BSS
1671 static bool
1672 emit_bss (tree decl ATTRIBUTE_UNUSED,
1673 const char *name ATTRIBUTE_UNUSED,
1674 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1675 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1677 #if defined ASM_OUTPUT_ALIGNED_BSS
1678 ASM_OUTPUT_ALIGNED_BSS (asm_out_file, decl, name, size, DECL_ALIGN (decl));
1679 return true;
1680 #else
1681 ASM_OUTPUT_BSS (asm_out_file, decl, name, size, rounded);
1682 return false;
1683 #endif
1685 #endif
1687 /* A noswitch_section_callback for comm_section. */
1689 static bool
1690 emit_common (tree decl ATTRIBUTE_UNUSED,
1691 const char *name ATTRIBUTE_UNUSED,
1692 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1693 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1695 #if defined ASM_OUTPUT_ALIGNED_DECL_COMMON
1696 ASM_OUTPUT_ALIGNED_DECL_COMMON (asm_out_file, decl, name,
1697 size, DECL_ALIGN (decl));
1698 return true;
1699 #elif defined ASM_OUTPUT_ALIGNED_COMMON
1700 ASM_OUTPUT_ALIGNED_COMMON (asm_out_file, name, size, DECL_ALIGN (decl));
1701 return true;
1702 #else
1703 ASM_OUTPUT_COMMON (asm_out_file, name, size, rounded);
1704 return false;
1705 #endif
1708 /* A noswitch_section_callback for tls_comm_section. */
1710 static bool
1711 emit_tls_common (tree decl ATTRIBUTE_UNUSED,
1712 const char *name ATTRIBUTE_UNUSED,
1713 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1714 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1716 #ifdef ASM_OUTPUT_TLS_COMMON
1717 ASM_OUTPUT_TLS_COMMON (asm_out_file, decl, name, size);
1718 return true;
1719 #else
1720 sorry ("thread-local COMMON data not implemented");
1721 return true;
1722 #endif
1725 /* Assemble DECL given that it belongs in SECTION_NOSWITCH section SECT.
1726 NAME is the name of DECL's SYMBOL_REF. */
1728 static void
1729 assemble_noswitch_variable (tree decl, const char *name, section *sect)
1731 unsigned HOST_WIDE_INT size, rounded;
1733 size = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
1734 rounded = size;
1736 /* Don't allocate zero bytes of common,
1737 since that means "undefined external" in the linker. */
1738 if (size == 0)
1739 rounded = 1;
1741 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
1742 so that each uninitialized object starts on such a boundary. */
1743 rounded += (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1;
1744 rounded = (rounded / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
1745 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
1747 if (!sect->noswitch.callback (decl, name, size, rounded)
1748 && (unsigned HOST_WIDE_INT) DECL_ALIGN_UNIT (decl) > rounded)
1749 warning (0, "requested alignment for %q+D is greater than "
1750 "implemented alignment of %wu", decl, rounded);
1753 /* A subroutine of assemble_variable. Output the label and contents of
1754 DECL, whose address is a SYMBOL_REF with name NAME. DONT_OUTPUT_DATA
1755 is as for assemble_variable. */
1757 static void
1758 assemble_variable_contents (tree decl, const char *name,
1759 bool dont_output_data)
1761 /* Do any machine/system dependent processing of the object. */
1762 #ifdef ASM_DECLARE_OBJECT_NAME
1763 last_assemble_variable_decl = decl;
1764 ASM_DECLARE_OBJECT_NAME (asm_out_file, name, decl);
1765 #else
1766 /* Standard thing is just output label for the object. */
1767 ASM_OUTPUT_LABEL (asm_out_file, name);
1768 #endif /* ASM_DECLARE_OBJECT_NAME */
1770 if (!dont_output_data)
1772 if (DECL_INITIAL (decl)
1773 && DECL_INITIAL (decl) != error_mark_node
1774 && !initializer_zerop (DECL_INITIAL (decl)))
1775 /* Output the actual data. */
1776 output_constant (DECL_INITIAL (decl),
1777 tree_low_cst (DECL_SIZE_UNIT (decl), 1),
1778 DECL_ALIGN (decl));
1779 else
1780 /* Leave space for it. */
1781 assemble_zeros (tree_low_cst (DECL_SIZE_UNIT (decl), 1));
1785 /* Assemble everything that is needed for a variable or function declaration.
1786 Not used for automatic variables, and not used for function definitions.
1787 Should not be called for variables of incomplete structure type.
1789 TOP_LEVEL is nonzero if this variable has file scope.
1790 AT_END is nonzero if this is the special handling, at end of compilation,
1791 to define things that have had only tentative definitions.
1792 DONT_OUTPUT_DATA if nonzero means don't actually output the
1793 initial value (that will be done by the caller). */
1795 void
1796 assemble_variable (tree decl, int top_level ATTRIBUTE_UNUSED,
1797 int at_end ATTRIBUTE_UNUSED, int dont_output_data)
1799 const char *name;
1800 rtx decl_rtl, symbol;
1801 section *sect;
1803 /* This function is supposed to handle VARIABLES. Ensure we have one. */
1804 gcc_assert (TREE_CODE (decl) == VAR_DECL);
1806 /* Emulated TLS had better not get this far. */
1807 gcc_checking_assert (targetm.have_tls || !DECL_THREAD_LOCAL_P (decl));
1809 last_assemble_variable_decl = 0;
1811 /* Normally no need to say anything here for external references,
1812 since assemble_external is called by the language-specific code
1813 when a declaration is first seen. */
1815 if (DECL_EXTERNAL (decl))
1816 return;
1818 /* Do nothing for global register variables. */
1819 if (DECL_RTL_SET_P (decl) && REG_P (DECL_RTL (decl)))
1821 TREE_ASM_WRITTEN (decl) = 1;
1822 return;
1825 /* If type was incomplete when the variable was declared,
1826 see if it is complete now. */
1828 if (DECL_SIZE (decl) == 0)
1829 layout_decl (decl, 0);
1831 /* Still incomplete => don't allocate it; treat the tentative defn
1832 (which is what it must have been) as an `extern' reference. */
1834 if (!dont_output_data && DECL_SIZE (decl) == 0)
1836 error ("storage size of %q+D isn%'t known", decl);
1837 TREE_ASM_WRITTEN (decl) = 1;
1838 return;
1841 /* The first declaration of a variable that comes through this function
1842 decides whether it is global (in C, has external linkage)
1843 or local (in C, has internal linkage). So do nothing more
1844 if this function has already run. */
1846 if (TREE_ASM_WRITTEN (decl))
1847 return;
1849 /* Make sure targetm.encode_section_info is invoked before we set
1850 ASM_WRITTEN. */
1851 decl_rtl = DECL_RTL (decl);
1853 TREE_ASM_WRITTEN (decl) = 1;
1855 /* Do no output if -fsyntax-only. */
1856 if (flag_syntax_only)
1857 return;
1859 if (! dont_output_data
1860 && ! host_integerp (DECL_SIZE_UNIT (decl), 1))
1862 error ("size of variable %q+D is too large", decl);
1863 return;
1866 gcc_assert (MEM_P (decl_rtl));
1867 gcc_assert (GET_CODE (XEXP (decl_rtl, 0)) == SYMBOL_REF);
1868 symbol = XEXP (decl_rtl, 0);
1870 /* If this symbol belongs to the tree constant pool, output the constant
1871 if it hasn't already been written. */
1872 if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
1874 tree decl = SYMBOL_REF_DECL (symbol);
1875 if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
1876 output_constant_def_contents (symbol);
1877 return;
1880 app_disable ();
1882 name = XSTR (symbol, 0);
1883 if (TREE_PUBLIC (decl) && DECL_NAME (decl))
1884 notice_global_symbol (decl);
1886 /* Compute the alignment of this data. */
1888 align_variable (decl, dont_output_data);
1889 set_mem_align (decl_rtl, DECL_ALIGN (decl));
1891 if (TREE_PUBLIC (decl))
1892 maybe_assemble_visibility (decl);
1894 if (DECL_PRESERVE_P (decl))
1895 targetm.asm_out.mark_decl_preserved (name);
1897 /* First make the assembler name(s) global if appropriate. */
1898 sect = get_variable_section (decl, false);
1899 if (TREE_PUBLIC (decl)
1900 && (sect->common.flags & SECTION_COMMON) == 0)
1901 globalize_decl (decl);
1903 /* Output any data that we will need to use the address of. */
1904 if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node)
1905 output_addressed_constants (DECL_INITIAL (decl));
1907 /* dbxout.c needs to know this. */
1908 if (sect && (sect->common.flags & SECTION_CODE) != 0)
1909 DECL_IN_TEXT_SECTION (decl) = 1;
1911 /* If the decl is part of an object_block, make sure that the decl
1912 has been positioned within its block, but do not write out its
1913 definition yet. output_object_blocks will do that later. */
1914 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
1916 gcc_assert (!dont_output_data);
1917 place_block_symbol (symbol);
1919 else if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
1920 assemble_noswitch_variable (decl, name, sect);
1921 else
1923 switch_to_section (sect);
1924 if (DECL_ALIGN (decl) > BITS_PER_UNIT)
1925 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (DECL_ALIGN_UNIT (decl)));
1926 assemble_variable_contents (decl, name, dont_output_data);
1930 /* Return 1 if type TYPE contains any pointers. */
1932 static int
1933 contains_pointers_p (tree type)
1935 switch (TREE_CODE (type))
1937 case POINTER_TYPE:
1938 case REFERENCE_TYPE:
1939 /* I'm not sure whether OFFSET_TYPE needs this treatment,
1940 so I'll play safe and return 1. */
1941 case OFFSET_TYPE:
1942 return 1;
1944 case RECORD_TYPE:
1945 case UNION_TYPE:
1946 case QUAL_UNION_TYPE:
1948 tree fields;
1949 /* For a type that has fields, see if the fields have pointers. */
1950 for (fields = TYPE_FIELDS (type); fields; fields = DECL_CHAIN (fields))
1951 if (TREE_CODE (fields) == FIELD_DECL
1952 && contains_pointers_p (TREE_TYPE (fields)))
1953 return 1;
1954 return 0;
1957 case ARRAY_TYPE:
1958 /* An array type contains pointers if its element type does. */
1959 return contains_pointers_p (TREE_TYPE (type));
1961 default:
1962 return 0;
1966 /* We delay assemble_external processing until
1967 the compilation unit is finalized. This is the best we can do for
1968 right now (i.e. stage 3 of GCC 4.0) - the right thing is to delay
1969 it all the way to final. See PR 17982 for further discussion. */
1970 static GTY(()) tree pending_assemble_externals;
1972 #ifdef ASM_OUTPUT_EXTERNAL
1973 /* True if DECL is a function decl for which no out-of-line copy exists.
1974 It is assumed that DECL's assembler name has been set. */
1976 static bool
1977 incorporeal_function_p (tree decl)
1979 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1981 const char *name;
1983 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
1984 && DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA)
1985 return true;
1987 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
1988 if (is_builtin_name (name))
1989 return true;
1991 return false;
1994 /* Actually do the tests to determine if this is necessary, and invoke
1995 ASM_OUTPUT_EXTERNAL. */
1996 static void
1997 assemble_external_real (tree decl)
1999 rtx rtl = DECL_RTL (decl);
2001 if (MEM_P (rtl) && GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF
2002 && !SYMBOL_REF_USED (XEXP (rtl, 0))
2003 && !incorporeal_function_p (decl))
2005 /* Some systems do require some output. */
2006 SYMBOL_REF_USED (XEXP (rtl, 0)) = 1;
2007 ASM_OUTPUT_EXTERNAL (asm_out_file, decl, XSTR (XEXP (rtl, 0), 0));
2010 #endif
2012 void
2013 process_pending_assemble_externals (void)
2015 #ifdef ASM_OUTPUT_EXTERNAL
2016 tree list;
2017 for (list = pending_assemble_externals; list; list = TREE_CHAIN (list))
2018 assemble_external_real (TREE_VALUE (list));
2020 pending_assemble_externals = 0;
2021 #endif
2024 /* This TREE_LIST contains any weak symbol declarations waiting
2025 to be emitted. */
2026 static GTY(()) tree weak_decls;
2028 /* Output something to declare an external symbol to the assembler,
2029 and qualifiers such as weakness. (Most assemblers don't need
2030 extern declaration, so we normally output nothing.) Do nothing if
2031 DECL is not external. */
2033 void
2034 assemble_external (tree decl ATTRIBUTE_UNUSED)
2036 /* Because most platforms do not define ASM_OUTPUT_EXTERNAL, the
2037 main body of this code is only rarely exercised. To provide some
2038 testing, on all platforms, we make sure that the ASM_OUT_FILE is
2039 open. If it's not, we should not be calling this function. */
2040 gcc_assert (asm_out_file);
2042 if (!DECL_P (decl) || !DECL_EXTERNAL (decl) || !TREE_PUBLIC (decl))
2043 return;
2045 /* We want to output annotation for weak and external symbols at
2046 very last to check if they are references or not. */
2048 if (SUPPORTS_WEAK
2049 && DECL_WEAK (decl)
2050 /* TREE_STATIC is a weird and abused creature which is not
2051 generally the right test for whether an entity has been
2052 locally emitted, inlined or otherwise not-really-extern, but
2053 for declarations that can be weak, it happens to be
2054 match. */
2055 && !TREE_STATIC (decl)
2056 && lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
2057 && value_member (decl, weak_decls) == NULL_TREE)
2058 weak_decls = tree_cons (NULL, decl, weak_decls);
2060 #ifdef ASM_OUTPUT_EXTERNAL
2061 if (value_member (decl, pending_assemble_externals) == NULL_TREE)
2062 pending_assemble_externals = tree_cons (NULL, decl,
2063 pending_assemble_externals);
2064 #endif
2067 /* Similar, for calling a library function FUN. */
2069 void
2070 assemble_external_libcall (rtx fun)
2072 /* Declare library function name external when first used, if nec. */
2073 if (! SYMBOL_REF_USED (fun))
2075 SYMBOL_REF_USED (fun) = 1;
2076 targetm.asm_out.external_libcall (fun);
2080 /* Assemble a label named NAME. */
2082 void
2083 assemble_label (FILE *file, const char *name)
2085 ASM_OUTPUT_LABEL (file, name);
2088 /* Set the symbol_referenced flag for ID. */
2089 void
2090 mark_referenced (tree id)
2092 TREE_SYMBOL_REFERENCED (id) = 1;
2095 /* Set the symbol_referenced flag for DECL and notify callgraph. */
2096 void
2097 mark_decl_referenced (tree decl)
2099 if (TREE_CODE (decl) == FUNCTION_DECL)
2101 /* Extern inline functions don't become needed when referenced.
2102 If we know a method will be emitted in other TU and no new
2103 functions can be marked reachable, just use the external
2104 definition. */
2105 struct cgraph_node *node = cgraph_node (decl);
2106 if (!DECL_EXTERNAL (decl)
2107 && (!node->local.vtable_method || !cgraph_global_info_ready
2108 || !node->local.finalized))
2109 cgraph_mark_needed_node (node);
2111 else if (TREE_CODE (decl) == VAR_DECL)
2113 struct varpool_node *node = varpool_node (decl);
2114 varpool_mark_needed_node (node);
2115 /* C++ frontend use mark_decl_references to force COMDAT variables
2116 to be output that might appear dead otherwise. */
2117 node->force_output = true;
2119 /* else do nothing - we can get various sorts of CST nodes here,
2120 which do not need to be marked. */
2124 /* Follow the IDENTIFIER_TRANSPARENT_ALIAS chain starting at *ALIAS
2125 until we find an identifier that is not itself a transparent alias.
2126 Modify the alias passed to it by reference (and all aliases on the
2127 way to the ultimate target), such that they do not have to be
2128 followed again, and return the ultimate target of the alias
2129 chain. */
2131 static inline tree
2132 ultimate_transparent_alias_target (tree *alias)
2134 tree target = *alias;
2136 if (IDENTIFIER_TRANSPARENT_ALIAS (target))
2138 gcc_assert (TREE_CHAIN (target));
2139 target = ultimate_transparent_alias_target (&TREE_CHAIN (target));
2140 gcc_assert (! IDENTIFIER_TRANSPARENT_ALIAS (target)
2141 && ! TREE_CHAIN (target));
2142 *alias = target;
2145 return target;
2148 /* Output to FILE (an assembly file) a reference to NAME. If NAME
2149 starts with a *, the rest of NAME is output verbatim. Otherwise
2150 NAME is transformed in a target-specific way (usually by the
2151 addition of an underscore). */
2153 void
2154 assemble_name_raw (FILE *file, const char *name)
2156 if (name[0] == '*')
2157 fputs (&name[1], file);
2158 else
2159 ASM_OUTPUT_LABELREF (file, name);
2162 /* Like assemble_name_raw, but should be used when NAME might refer to
2163 an entity that is also represented as a tree (like a function or
2164 variable). If NAME does refer to such an entity, that entity will
2165 be marked as referenced. */
2167 void
2168 assemble_name (FILE *file, const char *name)
2170 const char *real_name;
2171 tree id;
2173 real_name = targetm.strip_name_encoding (name);
2175 id = maybe_get_identifier (real_name);
2176 if (id)
2178 tree id_orig = id;
2180 mark_referenced (id);
2181 ultimate_transparent_alias_target (&id);
2182 if (id != id_orig)
2183 name = IDENTIFIER_POINTER (id);
2184 gcc_assert (! TREE_CHAIN (id));
2187 assemble_name_raw (file, name);
2190 /* Allocate SIZE bytes writable static space with a gensym name
2191 and return an RTX to refer to its address. */
2194 assemble_static_space (unsigned HOST_WIDE_INT size)
2196 char name[12];
2197 const char *namestring;
2198 rtx x;
2200 ASM_GENERATE_INTERNAL_LABEL (name, "LF", const_labelno);
2201 ++const_labelno;
2202 namestring = ggc_strdup (name);
2204 x = gen_rtx_SYMBOL_REF (Pmode, namestring);
2205 SYMBOL_REF_FLAGS (x) = SYMBOL_FLAG_LOCAL;
2207 #ifdef ASM_OUTPUT_ALIGNED_DECL_LOCAL
2208 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, NULL_TREE, name, size,
2209 BIGGEST_ALIGNMENT);
2210 #else
2211 #ifdef ASM_OUTPUT_ALIGNED_LOCAL
2212 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, BIGGEST_ALIGNMENT);
2213 #else
2215 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
2216 so that each uninitialized object starts on such a boundary. */
2217 /* Variable `rounded' might or might not be used in ASM_OUTPUT_LOCAL. */
2218 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED
2219 = ((size + (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1)
2220 / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
2221 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
2222 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
2224 #endif
2225 #endif
2226 return x;
2229 /* Assemble the static constant template for function entry trampolines.
2230 This is done at most once per compilation.
2231 Returns an RTX for the address of the template. */
2233 static GTY(()) rtx initial_trampoline;
2236 assemble_trampoline_template (void)
2238 char label[256];
2239 const char *name;
2240 int align;
2241 rtx symbol;
2243 gcc_assert (targetm.asm_out.trampoline_template != NULL);
2245 if (initial_trampoline)
2246 return initial_trampoline;
2248 /* By default, put trampoline templates in read-only data section. */
2250 #ifdef TRAMPOLINE_SECTION
2251 switch_to_section (TRAMPOLINE_SECTION);
2252 #else
2253 switch_to_section (readonly_data_section);
2254 #endif
2256 /* Write the assembler code to define one. */
2257 align = floor_log2 (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
2258 if (align > 0)
2259 ASM_OUTPUT_ALIGN (asm_out_file, align);
2261 targetm.asm_out.internal_label (asm_out_file, "LTRAMP", 0);
2262 targetm.asm_out.trampoline_template (asm_out_file);
2264 /* Record the rtl to refer to it. */
2265 ASM_GENERATE_INTERNAL_LABEL (label, "LTRAMP", 0);
2266 name = ggc_strdup (label);
2267 symbol = gen_rtx_SYMBOL_REF (Pmode, name);
2268 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_LOCAL;
2270 initial_trampoline = gen_const_mem (BLKmode, symbol);
2271 set_mem_align (initial_trampoline, TRAMPOLINE_ALIGNMENT);
2272 set_mem_size (initial_trampoline, GEN_INT (TRAMPOLINE_SIZE));
2274 return initial_trampoline;
2277 /* A and B are either alignments or offsets. Return the minimum alignment
2278 that may be assumed after adding the two together. */
2280 static inline unsigned
2281 min_align (unsigned int a, unsigned int b)
2283 return (a | b) & -(a | b);
2286 /* Return the assembler directive for creating a given kind of integer
2287 object. SIZE is the number of bytes in the object and ALIGNED_P
2288 indicates whether it is known to be aligned. Return NULL if the
2289 assembly dialect has no such directive.
2291 The returned string should be printed at the start of a new line and
2292 be followed immediately by the object's initial value. */
2294 const char *
2295 integer_asm_op (int size, int aligned_p)
2297 struct asm_int_op *ops;
2299 if (aligned_p)
2300 ops = &targetm.asm_out.aligned_op;
2301 else
2302 ops = &targetm.asm_out.unaligned_op;
2304 switch (size)
2306 case 1:
2307 return targetm.asm_out.byte_op;
2308 case 2:
2309 return ops->hi;
2310 case 4:
2311 return ops->si;
2312 case 8:
2313 return ops->di;
2314 case 16:
2315 return ops->ti;
2316 default:
2317 return NULL;
2321 /* Use directive OP to assemble an integer object X. Print OP at the
2322 start of the line, followed immediately by the value of X. */
2324 void
2325 assemble_integer_with_op (const char *op, rtx x)
2327 fputs (op, asm_out_file);
2328 output_addr_const (asm_out_file, x);
2329 fputc ('\n', asm_out_file);
2332 /* The default implementation of the asm_out.integer target hook. */
2334 bool
2335 default_assemble_integer (rtx x ATTRIBUTE_UNUSED,
2336 unsigned int size ATTRIBUTE_UNUSED,
2337 int aligned_p ATTRIBUTE_UNUSED)
2339 const char *op = integer_asm_op (size, aligned_p);
2340 /* Avoid GAS bugs for large values. Specifically negative values whose
2341 absolute value fits in a bfd_vma, but not in a bfd_signed_vma. */
2342 if (size > UNITS_PER_WORD && size > POINTER_SIZE / BITS_PER_UNIT)
2343 return false;
2344 return op && (assemble_integer_with_op (op, x), true);
2347 /* Assemble the integer constant X into an object of SIZE bytes. ALIGN is
2348 the alignment of the integer in bits. Return 1 if we were able to output
2349 the constant, otherwise 0. We must be able to output the constant,
2350 if FORCE is nonzero. */
2352 bool
2353 assemble_integer (rtx x, unsigned int size, unsigned int align, int force)
2355 int aligned_p;
2357 aligned_p = (align >= MIN (size * BITS_PER_UNIT, BIGGEST_ALIGNMENT));
2359 /* See if the target hook can handle this kind of object. */
2360 if (targetm.asm_out.integer (x, size, aligned_p))
2361 return true;
2363 /* If the object is a multi-byte one, try splitting it up. Split
2364 it into words it if is multi-word, otherwise split it into bytes. */
2365 if (size > 1)
2367 enum machine_mode omode, imode;
2368 unsigned int subalign;
2369 unsigned int subsize, i;
2370 enum mode_class mclass;
2372 subsize = size > UNITS_PER_WORD? UNITS_PER_WORD : 1;
2373 subalign = MIN (align, subsize * BITS_PER_UNIT);
2374 if (GET_CODE (x) == CONST_FIXED)
2375 mclass = GET_MODE_CLASS (GET_MODE (x));
2376 else
2377 mclass = MODE_INT;
2379 omode = mode_for_size (subsize * BITS_PER_UNIT, mclass, 0);
2380 imode = mode_for_size (size * BITS_PER_UNIT, mclass, 0);
2382 for (i = 0; i < size; i += subsize)
2384 rtx partial = simplify_subreg (omode, x, imode, i);
2385 if (!partial || !assemble_integer (partial, subsize, subalign, 0))
2386 break;
2388 if (i == size)
2389 return true;
2391 /* If we've printed some of it, but not all of it, there's no going
2392 back now. */
2393 gcc_assert (!i);
2396 gcc_assert (!force);
2398 return false;
2401 void
2402 assemble_real (REAL_VALUE_TYPE d, enum machine_mode mode, unsigned int align)
2404 long data[4] = {0, 0, 0, 0};
2405 int i;
2406 int bitsize, nelts, nunits, units_per;
2408 /* This is hairy. We have a quantity of known size. real_to_target
2409 will put it into an array of *host* longs, 32 bits per element
2410 (even if long is more than 32 bits). We need to determine the
2411 number of array elements that are occupied (nelts) and the number
2412 of *target* min-addressable units that will be occupied in the
2413 object file (nunits). We cannot assume that 32 divides the
2414 mode's bitsize (size * BITS_PER_UNIT) evenly.
2416 size * BITS_PER_UNIT is used here to make sure that padding bits
2417 (which might appear at either end of the value; real_to_target
2418 will include the padding bits in its output array) are included. */
2420 nunits = GET_MODE_SIZE (mode);
2421 bitsize = nunits * BITS_PER_UNIT;
2422 nelts = CEIL (bitsize, 32);
2423 units_per = 32 / BITS_PER_UNIT;
2425 real_to_target (data, &d, mode);
2427 /* Put out the first word with the specified alignment. */
2428 assemble_integer (GEN_INT (data[0]), MIN (nunits, units_per), align, 1);
2429 nunits -= units_per;
2431 /* Subsequent words need only 32-bit alignment. */
2432 align = min_align (align, 32);
2434 for (i = 1; i < nelts; i++)
2436 assemble_integer (GEN_INT (data[i]), MIN (nunits, units_per), align, 1);
2437 nunits -= units_per;
2441 /* Given an expression EXP with a constant value,
2442 reduce it to the sum of an assembler symbol and an integer.
2443 Store them both in the structure *VALUE.
2444 EXP must be reducible. */
2446 struct GTY(()) addr_const {
2447 rtx base;
2448 HOST_WIDE_INT offset;
2451 static void
2452 decode_addr_const (tree exp, struct addr_const *value)
2454 tree target = TREE_OPERAND (exp, 0);
2455 int offset = 0;
2456 rtx x;
2458 while (1)
2460 if (TREE_CODE (target) == COMPONENT_REF
2461 && host_integerp (byte_position (TREE_OPERAND (target, 1)), 0))
2463 offset += int_byte_position (TREE_OPERAND (target, 1));
2464 target = TREE_OPERAND (target, 0);
2466 else if (TREE_CODE (target) == ARRAY_REF
2467 || TREE_CODE (target) == ARRAY_RANGE_REF)
2469 offset += (tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (target)), 1)
2470 * tree_low_cst (TREE_OPERAND (target, 1), 0));
2471 target = TREE_OPERAND (target, 0);
2473 else if (TREE_CODE (target) == INDIRECT_REF
2474 && TREE_CODE (TREE_OPERAND (target, 0)) == NOP_EXPR
2475 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (target, 0), 0))
2476 == ADDR_EXPR)
2477 target = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (target, 0), 0), 0);
2478 else
2479 break;
2482 switch (TREE_CODE (target))
2484 case VAR_DECL:
2485 case FUNCTION_DECL:
2486 x = DECL_RTL (target);
2487 break;
2489 case LABEL_DECL:
2490 x = gen_rtx_MEM (FUNCTION_MODE,
2491 gen_rtx_LABEL_REF (Pmode, force_label_rtx (target)));
2492 break;
2494 case REAL_CST:
2495 case FIXED_CST:
2496 case STRING_CST:
2497 case COMPLEX_CST:
2498 case CONSTRUCTOR:
2499 case INTEGER_CST:
2500 x = output_constant_def (target, 1);
2501 break;
2503 default:
2504 gcc_unreachable ();
2507 gcc_assert (MEM_P (x));
2508 x = XEXP (x, 0);
2510 value->base = x;
2511 value->offset = offset;
2515 static GTY((param_is (struct constant_descriptor_tree)))
2516 htab_t const_desc_htab;
2518 static void maybe_output_constant_def_contents (struct constant_descriptor_tree *, int);
2520 /* Constant pool accessor function. */
2522 htab_t
2523 constant_pool_htab (void)
2525 return const_desc_htab;
2528 /* Compute a hash code for a constant expression. */
2530 static hashval_t
2531 const_desc_hash (const void *ptr)
2533 return ((const struct constant_descriptor_tree *)ptr)->hash;
2536 static hashval_t
2537 const_hash_1 (const tree exp)
2539 const char *p;
2540 hashval_t hi;
2541 int len, i;
2542 enum tree_code code = TREE_CODE (exp);
2544 /* Either set P and LEN to the address and len of something to hash and
2545 exit the switch or return a value. */
2547 switch (code)
2549 case INTEGER_CST:
2550 p = (char *) &TREE_INT_CST (exp);
2551 len = sizeof TREE_INT_CST (exp);
2552 break;
2554 case REAL_CST:
2555 return real_hash (TREE_REAL_CST_PTR (exp));
2557 case FIXED_CST:
2558 return fixed_hash (TREE_FIXED_CST_PTR (exp));
2560 case STRING_CST:
2561 p = TREE_STRING_POINTER (exp);
2562 len = TREE_STRING_LENGTH (exp);
2563 break;
2565 case COMPLEX_CST:
2566 return (const_hash_1 (TREE_REALPART (exp)) * 5
2567 + const_hash_1 (TREE_IMAGPART (exp)));
2569 case VECTOR_CST:
2571 tree link;
2573 hi = 7 + TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp));
2575 for (link = TREE_VECTOR_CST_ELTS (exp); link; link = TREE_CHAIN (link))
2576 hi = hi * 563 + const_hash_1 (TREE_VALUE (link));
2578 return hi;
2581 case CONSTRUCTOR:
2583 unsigned HOST_WIDE_INT idx;
2584 tree value;
2586 hi = 5 + int_size_in_bytes (TREE_TYPE (exp));
2588 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
2589 if (value)
2590 hi = hi * 603 + const_hash_1 (value);
2592 return hi;
2595 case ADDR_EXPR:
2596 case FDESC_EXPR:
2598 struct addr_const value;
2600 decode_addr_const (exp, &value);
2601 switch (GET_CODE (value.base))
2603 case SYMBOL_REF:
2604 /* Don't hash the address of the SYMBOL_REF;
2605 only use the offset and the symbol name. */
2606 hi = value.offset;
2607 p = XSTR (value.base, 0);
2608 for (i = 0; p[i] != 0; i++)
2609 hi = ((hi * 613) + (unsigned) (p[i]));
2610 break;
2612 case LABEL_REF:
2613 hi = value.offset + CODE_LABEL_NUMBER (XEXP (value.base, 0)) * 13;
2614 break;
2616 default:
2617 gcc_unreachable ();
2620 return hi;
2622 case PLUS_EXPR:
2623 case POINTER_PLUS_EXPR:
2624 case MINUS_EXPR:
2625 return (const_hash_1 (TREE_OPERAND (exp, 0)) * 9
2626 + const_hash_1 (TREE_OPERAND (exp, 1)));
2628 CASE_CONVERT:
2629 return const_hash_1 (TREE_OPERAND (exp, 0)) * 7 + 2;
2631 default:
2632 /* A language specific constant. Just hash the code. */
2633 return code;
2636 /* Compute hashing function. */
2637 hi = len;
2638 for (i = 0; i < len; i++)
2639 hi = ((hi * 613) + (unsigned) (p[i]));
2641 return hi;
2644 /* Wrapper of compare_constant, for the htab interface. */
2645 static int
2646 const_desc_eq (const void *p1, const void *p2)
2648 const struct constant_descriptor_tree *const c1
2649 = (const struct constant_descriptor_tree *) p1;
2650 const struct constant_descriptor_tree *const c2
2651 = (const struct constant_descriptor_tree *) p2;
2652 if (c1->hash != c2->hash)
2653 return 0;
2654 return compare_constant (c1->value, c2->value);
2657 /* Compare t1 and t2, and return 1 only if they are known to result in
2658 the same bit pattern on output. */
2660 static int
2661 compare_constant (const tree t1, const tree t2)
2663 enum tree_code typecode;
2665 if (t1 == NULL_TREE)
2666 return t2 == NULL_TREE;
2667 if (t2 == NULL_TREE)
2668 return 0;
2670 if (TREE_CODE (t1) != TREE_CODE (t2))
2671 return 0;
2673 switch (TREE_CODE (t1))
2675 case INTEGER_CST:
2676 /* Integer constants are the same only if the same width of type. */
2677 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
2678 return 0;
2679 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
2680 return 0;
2681 return tree_int_cst_equal (t1, t2);
2683 case REAL_CST:
2684 /* Real constants are the same only if the same width of type. */
2685 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
2686 return 0;
2688 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
2690 case FIXED_CST:
2691 /* Fixed constants are the same only if the same width of type. */
2692 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
2693 return 0;
2695 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
2697 case STRING_CST:
2698 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
2699 return 0;
2701 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
2702 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
2703 TREE_STRING_LENGTH (t1)));
2705 case COMPLEX_CST:
2706 return (compare_constant (TREE_REALPART (t1), TREE_REALPART (t2))
2707 && compare_constant (TREE_IMAGPART (t1), TREE_IMAGPART (t2)));
2709 case VECTOR_CST:
2711 tree link1, link2;
2713 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (t1))
2714 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (t2)))
2715 return 0;
2717 link2 = TREE_VECTOR_CST_ELTS (t2);
2718 for (link1 = TREE_VECTOR_CST_ELTS (t1);
2719 link1;
2720 link1 = TREE_CHAIN (link1))
2722 if (!compare_constant (TREE_VALUE (link1), TREE_VALUE (link2)))
2723 return 0;
2724 link2 = TREE_CHAIN (link2);
2727 return 1;
2730 case CONSTRUCTOR:
2732 VEC(constructor_elt, gc) *v1, *v2;
2733 unsigned HOST_WIDE_INT idx;
2735 typecode = TREE_CODE (TREE_TYPE (t1));
2736 if (typecode != TREE_CODE (TREE_TYPE (t2)))
2737 return 0;
2739 if (typecode == ARRAY_TYPE)
2741 HOST_WIDE_INT size_1 = int_size_in_bytes (TREE_TYPE (t1));
2742 /* For arrays, check that the sizes all match. */
2743 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))
2744 || size_1 == -1
2745 || size_1 != int_size_in_bytes (TREE_TYPE (t2)))
2746 return 0;
2748 else
2750 /* For record and union constructors, require exact type
2751 equality. */
2752 if (TREE_TYPE (t1) != TREE_TYPE (t2))
2753 return 0;
2756 v1 = CONSTRUCTOR_ELTS (t1);
2757 v2 = CONSTRUCTOR_ELTS (t2);
2758 if (VEC_length (constructor_elt, v1)
2759 != VEC_length (constructor_elt, v2))
2760 return 0;
2762 for (idx = 0; idx < VEC_length (constructor_elt, v1); ++idx)
2764 constructor_elt *c1 = VEC_index (constructor_elt, v1, idx);
2765 constructor_elt *c2 = VEC_index (constructor_elt, v2, idx);
2767 /* Check that each value is the same... */
2768 if (!compare_constant (c1->value, c2->value))
2769 return 0;
2770 /* ... and that they apply to the same fields! */
2771 if (typecode == ARRAY_TYPE)
2773 if (!compare_constant (c1->index, c2->index))
2774 return 0;
2776 else
2778 if (c1->index != c2->index)
2779 return 0;
2783 return 1;
2786 case ADDR_EXPR:
2787 case FDESC_EXPR:
2789 struct addr_const value1, value2;
2790 enum rtx_code code;
2791 int ret;
2793 decode_addr_const (t1, &value1);
2794 decode_addr_const (t2, &value2);
2796 if (value1.offset != value2.offset)
2797 return 0;
2799 code = GET_CODE (value1.base);
2800 if (code != GET_CODE (value2.base))
2801 return 0;
2803 switch (code)
2805 case SYMBOL_REF:
2806 ret = (strcmp (XSTR (value1.base, 0), XSTR (value2.base, 0)) == 0);
2807 break;
2809 case LABEL_REF:
2810 ret = (CODE_LABEL_NUMBER (XEXP (value1.base, 0))
2811 == CODE_LABEL_NUMBER (XEXP (value2.base, 0)));
2812 break;
2814 default:
2815 gcc_unreachable ();
2817 return ret;
2820 case PLUS_EXPR:
2821 case POINTER_PLUS_EXPR:
2822 case MINUS_EXPR:
2823 case RANGE_EXPR:
2824 return (compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
2825 && compare_constant(TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
2827 CASE_CONVERT:
2828 case VIEW_CONVERT_EXPR:
2829 return compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2831 default:
2832 return 0;
2835 gcc_unreachable ();
2838 /* Make a copy of the whole tree structure for a constant. This
2839 handles the same types of nodes that compare_constant handles. */
2841 static tree
2842 copy_constant (tree exp)
2844 switch (TREE_CODE (exp))
2846 case ADDR_EXPR:
2847 /* For ADDR_EXPR, we do not want to copy the decl whose address
2848 is requested. We do want to copy constants though. */
2849 if (CONSTANT_CLASS_P (TREE_OPERAND (exp, 0)))
2850 return build1 (TREE_CODE (exp), TREE_TYPE (exp),
2851 copy_constant (TREE_OPERAND (exp, 0)));
2852 else
2853 return copy_node (exp);
2855 case INTEGER_CST:
2856 case REAL_CST:
2857 case FIXED_CST:
2858 case STRING_CST:
2859 return copy_node (exp);
2861 case COMPLEX_CST:
2862 return build_complex (TREE_TYPE (exp),
2863 copy_constant (TREE_REALPART (exp)),
2864 copy_constant (TREE_IMAGPART (exp)));
2866 case PLUS_EXPR:
2867 case POINTER_PLUS_EXPR:
2868 case MINUS_EXPR:
2869 return build2 (TREE_CODE (exp), TREE_TYPE (exp),
2870 copy_constant (TREE_OPERAND (exp, 0)),
2871 copy_constant (TREE_OPERAND (exp, 1)));
2873 CASE_CONVERT:
2874 case VIEW_CONVERT_EXPR:
2875 return build1 (TREE_CODE (exp), TREE_TYPE (exp),
2876 copy_constant (TREE_OPERAND (exp, 0)));
2878 case VECTOR_CST:
2879 return build_vector (TREE_TYPE (exp),
2880 copy_list (TREE_VECTOR_CST_ELTS (exp)));
2882 case CONSTRUCTOR:
2884 tree copy = copy_node (exp);
2885 VEC(constructor_elt, gc) *v;
2886 unsigned HOST_WIDE_INT idx;
2887 tree purpose, value;
2889 v = VEC_alloc(constructor_elt, gc, VEC_length(constructor_elt,
2890 CONSTRUCTOR_ELTS (exp)));
2891 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, purpose, value)
2893 constructor_elt *ce = VEC_quick_push (constructor_elt, v, NULL);
2894 ce->index = purpose;
2895 ce->value = copy_constant (value);
2897 CONSTRUCTOR_ELTS (copy) = v;
2898 return copy;
2901 default:
2902 gcc_unreachable ();
2906 /* Return the section into which constant EXP should be placed. */
2908 static section *
2909 get_constant_section (tree exp, unsigned int align)
2911 return targetm.asm_out.select_section (exp,
2912 compute_reloc_for_constant (exp),
2913 align);
2916 /* Return the size of constant EXP in bytes. */
2918 static HOST_WIDE_INT
2919 get_constant_size (tree exp)
2921 HOST_WIDE_INT size;
2923 size = int_size_in_bytes (TREE_TYPE (exp));
2924 if (TREE_CODE (exp) == STRING_CST)
2925 size = MAX (TREE_STRING_LENGTH (exp), size);
2926 return size;
2929 /* Subroutine of output_constant_def:
2930 No constant equal to EXP is known to have been output.
2931 Make a constant descriptor to enter EXP in the hash table.
2932 Assign the label number and construct RTL to refer to the
2933 constant's location in memory.
2934 Caller is responsible for updating the hash table. */
2936 static struct constant_descriptor_tree *
2937 build_constant_desc (tree exp)
2939 struct constant_descriptor_tree *desc;
2940 rtx symbol, rtl;
2941 char label[256];
2942 int labelno;
2943 tree decl;
2945 desc = ggc_alloc_constant_descriptor_tree ();
2946 desc->value = copy_constant (exp);
2948 /* Propagate marked-ness to copied constant. */
2949 if (flag_mudflap && mf_marked_p (exp))
2950 mf_mark (desc->value);
2952 /* Create a string containing the label name, in LABEL. */
2953 labelno = const_labelno++;
2954 ASM_GENERATE_INTERNAL_LABEL (label, "LC", labelno);
2956 /* Construct the VAR_DECL associated with the constant. */
2957 decl = build_decl (UNKNOWN_LOCATION, VAR_DECL, get_identifier (label),
2958 TREE_TYPE (exp));
2959 DECL_ARTIFICIAL (decl) = 1;
2960 DECL_IGNORED_P (decl) = 1;
2961 TREE_READONLY (decl) = 1;
2962 TREE_STATIC (decl) = 1;
2963 TREE_ADDRESSABLE (decl) = 1;
2964 /* We don't set the RTL yet as this would cause varpool to assume that the
2965 variable is referenced. Moreover, it would just be dropped in LTO mode.
2966 Instead we set the flag that will be recognized in make_decl_rtl. */
2967 DECL_IN_CONSTANT_POOL (decl) = 1;
2968 DECL_INITIAL (decl) = desc->value;
2969 /* ??? CONSTANT_ALIGNMENT hasn't been updated for vector types on most
2970 architectures so use DATA_ALIGNMENT as well, except for strings. */
2971 if (TREE_CODE (exp) == STRING_CST)
2973 #ifdef CONSTANT_ALIGNMENT
2974 DECL_ALIGN (decl) = CONSTANT_ALIGNMENT (exp, DECL_ALIGN (decl));
2975 #endif
2977 else
2978 align_variable (decl, 0);
2980 /* Now construct the SYMBOL_REF and the MEM. */
2981 if (use_object_blocks_p ())
2983 section *sect = get_constant_section (exp, DECL_ALIGN (decl));
2984 symbol = create_block_symbol (ggc_strdup (label),
2985 get_block_for_section (sect), -1);
2987 else
2988 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
2989 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
2990 SET_SYMBOL_REF_DECL (symbol, decl);
2991 TREE_CONSTANT_POOL_ADDRESS_P (symbol) = 1;
2993 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (exp)), symbol);
2994 set_mem_attributes (rtl, exp, 1);
2995 set_mem_alias_set (rtl, 0);
2996 set_mem_alias_set (rtl, const_alias_set);
2998 /* We cannot share RTX'es in pool entries.
2999 Mark this piece of RTL as required for unsharing. */
3000 RTX_FLAG (rtl, used) = 1;
3002 /* Set flags or add text to the name to record information, such as
3003 that it is a local symbol. If the name is changed, the macro
3004 ASM_OUTPUT_LABELREF will have to know how to strip this
3005 information. This call might invalidate our local variable
3006 SYMBOL; we can't use it afterward. */
3007 targetm.encode_section_info (exp, rtl, true);
3009 desc->rtl = rtl;
3011 return desc;
3014 /* Return an rtx representing a reference to constant data in memory
3015 for the constant expression EXP.
3017 If assembler code for such a constant has already been output,
3018 return an rtx to refer to it.
3019 Otherwise, output such a constant in memory
3020 and generate an rtx for it.
3022 If DEFER is nonzero, this constant can be deferred and output only
3023 if referenced in the function after all optimizations.
3025 `const_desc_table' records which constants already have label strings. */
3028 output_constant_def (tree exp, int defer)
3030 struct constant_descriptor_tree *desc;
3031 struct constant_descriptor_tree key;
3032 void **loc;
3034 /* Look up EXP in the table of constant descriptors. If we didn't find
3035 it, create a new one. */
3036 key.value = exp;
3037 key.hash = const_hash_1 (exp);
3038 loc = htab_find_slot_with_hash (const_desc_htab, &key, key.hash, INSERT);
3040 desc = (struct constant_descriptor_tree *) *loc;
3041 if (desc == 0)
3043 desc = build_constant_desc (exp);
3044 desc->hash = key.hash;
3045 *loc = desc;
3048 maybe_output_constant_def_contents (desc, defer);
3049 return desc->rtl;
3052 /* Subroutine of output_constant_def: Decide whether or not we need to
3053 output the constant DESC now, and if so, do it. */
3054 static void
3055 maybe_output_constant_def_contents (struct constant_descriptor_tree *desc,
3056 int defer)
3058 rtx symbol = XEXP (desc->rtl, 0);
3059 tree exp = desc->value;
3061 if (flag_syntax_only)
3062 return;
3064 if (TREE_ASM_WRITTEN (exp))
3065 /* Already output; don't do it again. */
3066 return;
3068 /* We can always defer constants as long as the context allows
3069 doing so. */
3070 if (defer)
3072 /* Increment n_deferred_constants if it exists. It needs to be at
3073 least as large as the number of constants actually referred to
3074 by the function. If it's too small we'll stop looking too early
3075 and fail to emit constants; if it's too large we'll only look
3076 through the entire function when we could have stopped earlier. */
3077 if (cfun)
3078 n_deferred_constants++;
3079 return;
3082 output_constant_def_contents (symbol);
3085 /* Subroutine of output_constant_def_contents. Output the definition
3086 of constant EXP, which is pointed to by label LABEL. ALIGN is the
3087 constant's alignment in bits. */
3089 static void
3090 assemble_constant_contents (tree exp, const char *label, unsigned int align)
3092 HOST_WIDE_INT size;
3094 size = get_constant_size (exp);
3096 /* Do any machine/system dependent processing of the constant. */
3097 targetm.asm_out.declare_constant_name (asm_out_file, label, exp, size);
3099 /* Output the value of EXP. */
3100 output_constant (exp, size, align);
3103 /* We must output the constant data referred to by SYMBOL; do so. */
3105 static void
3106 output_constant_def_contents (rtx symbol)
3108 tree decl = SYMBOL_REF_DECL (symbol);
3109 tree exp = DECL_INITIAL (decl);
3110 unsigned int align;
3112 /* Make sure any other constants whose addresses appear in EXP
3113 are assigned label numbers. */
3114 output_addressed_constants (exp);
3116 /* We are no longer deferring this constant. */
3117 TREE_ASM_WRITTEN (exp) = 1;
3119 /* If the constant is part of an object block, make sure that the
3120 decl has been positioned within its block, but do not write out
3121 its definition yet. output_object_blocks will do that later. */
3122 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
3123 place_block_symbol (symbol);
3124 else
3126 align = DECL_ALIGN (decl);
3127 switch_to_section (get_constant_section (exp, align));
3128 if (align > BITS_PER_UNIT)
3129 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
3130 assemble_constant_contents (exp, XSTR (symbol, 0), align);
3132 if (flag_mudflap)
3133 mudflap_enqueue_constant (exp);
3136 /* Look up EXP in the table of constant descriptors. Return the rtl
3137 if it has been emitted, else null. */
3140 lookup_constant_def (tree exp)
3142 struct constant_descriptor_tree *desc;
3143 struct constant_descriptor_tree key;
3145 key.value = exp;
3146 key.hash = const_hash_1 (exp);
3147 desc = (struct constant_descriptor_tree *)
3148 htab_find_with_hash (const_desc_htab, &key, key.hash);
3150 return (desc ? desc->rtl : NULL_RTX);
3153 /* Return a tree representing a reference to constant data in memory
3154 for the constant expression EXP.
3156 This is the counterpart of output_constant_def at the Tree level. */
3158 tree
3159 tree_output_constant_def (tree exp)
3161 struct constant_descriptor_tree *desc, key;
3162 void **loc;
3163 tree decl;
3165 /* Look up EXP in the table of constant descriptors. If we didn't find
3166 it, create a new one. */
3167 key.value = exp;
3168 key.hash = const_hash_1 (exp);
3169 loc = htab_find_slot_with_hash (const_desc_htab, &key, key.hash, INSERT);
3171 desc = (struct constant_descriptor_tree *) *loc;
3172 if (desc == 0)
3174 desc = build_constant_desc (exp);
3175 desc->hash = key.hash;
3176 *loc = desc;
3179 decl = SYMBOL_REF_DECL (XEXP (desc->rtl, 0));
3180 varpool_finalize_decl (decl);
3181 return decl;
3184 /* Used in the hash tables to avoid outputting the same constant
3185 twice. Unlike 'struct constant_descriptor_tree', RTX constants
3186 are output once per function, not once per file. */
3187 /* ??? Only a few targets need per-function constant pools. Most
3188 can use one per-file pool. Should add a targetm bit to tell the
3189 difference. */
3191 struct GTY(()) rtx_constant_pool {
3192 /* Pointers to first and last constant in pool, as ordered by offset. */
3193 struct constant_descriptor_rtx *first;
3194 struct constant_descriptor_rtx *last;
3196 /* Hash facility for making memory-constants from constant rtl-expressions.
3197 It is used on RISC machines where immediate integer arguments and
3198 constant addresses are restricted so that such constants must be stored
3199 in memory. */
3200 htab_t GTY((param_is (struct constant_descriptor_rtx))) const_rtx_htab;
3202 /* Current offset in constant pool (does not include any
3203 machine-specific header). */
3204 HOST_WIDE_INT offset;
3207 struct GTY((chain_next ("%h.next"))) constant_descriptor_rtx {
3208 struct constant_descriptor_rtx *next;
3209 rtx mem;
3210 rtx sym;
3211 rtx constant;
3212 HOST_WIDE_INT offset;
3213 hashval_t hash;
3214 enum machine_mode mode;
3215 unsigned int align;
3216 int labelno;
3217 int mark;
3220 /* Hash and compare functions for const_rtx_htab. */
3222 static hashval_t
3223 const_desc_rtx_hash (const void *ptr)
3225 const struct constant_descriptor_rtx *const desc
3226 = (const struct constant_descriptor_rtx *) ptr;
3227 return desc->hash;
3230 static int
3231 const_desc_rtx_eq (const void *a, const void *b)
3233 const struct constant_descriptor_rtx *const x
3234 = (const struct constant_descriptor_rtx *) a;
3235 const struct constant_descriptor_rtx *const y
3236 = (const struct constant_descriptor_rtx *) b;
3238 if (x->mode != y->mode)
3239 return 0;
3240 return rtx_equal_p (x->constant, y->constant);
3243 /* This is the worker function for const_rtx_hash, called via for_each_rtx. */
3245 static int
3246 const_rtx_hash_1 (rtx *xp, void *data)
3248 unsigned HOST_WIDE_INT hwi;
3249 enum machine_mode mode;
3250 enum rtx_code code;
3251 hashval_t h, *hp;
3252 rtx x;
3254 x = *xp;
3255 code = GET_CODE (x);
3256 mode = GET_MODE (x);
3257 h = (hashval_t) code * 1048573 + mode;
3259 switch (code)
3261 case CONST_INT:
3262 hwi = INTVAL (x);
3263 fold_hwi:
3265 int shift = sizeof (hashval_t) * CHAR_BIT;
3266 const int n = sizeof (HOST_WIDE_INT) / sizeof (hashval_t);
3267 int i;
3269 h ^= (hashval_t) hwi;
3270 for (i = 1; i < n; ++i)
3272 hwi >>= shift;
3273 h ^= (hashval_t) hwi;
3276 break;
3278 case CONST_DOUBLE:
3279 if (mode == VOIDmode)
3281 hwi = CONST_DOUBLE_LOW (x) ^ CONST_DOUBLE_HIGH (x);
3282 goto fold_hwi;
3284 else
3285 h ^= real_hash (CONST_DOUBLE_REAL_VALUE (x));
3286 break;
3288 case CONST_FIXED:
3289 h ^= fixed_hash (CONST_FIXED_VALUE (x));
3290 break;
3292 case CONST_VECTOR:
3294 int i;
3295 for (i = XVECLEN (x, 0); i-- > 0; )
3296 h = h * 251 + const_rtx_hash_1 (&XVECEXP (x, 0, i), data);
3298 break;
3300 case SYMBOL_REF:
3301 h ^= htab_hash_string (XSTR (x, 0));
3302 break;
3304 case LABEL_REF:
3305 h = h * 251 + CODE_LABEL_NUMBER (XEXP (x, 0));
3306 break;
3308 case UNSPEC:
3309 case UNSPEC_VOLATILE:
3310 h = h * 251 + XINT (x, 1);
3311 break;
3313 default:
3314 break;
3317 hp = (hashval_t *) data;
3318 *hp = *hp * 509 + h;
3319 return 0;
3322 /* Compute a hash value for X, which should be a constant. */
3324 static hashval_t
3325 const_rtx_hash (rtx x)
3327 hashval_t h = 0;
3328 for_each_rtx (&x, const_rtx_hash_1, &h);
3329 return h;
3333 /* Create and return a new rtx constant pool. */
3335 static struct rtx_constant_pool *
3336 create_constant_pool (void)
3338 struct rtx_constant_pool *pool;
3340 pool = ggc_alloc_rtx_constant_pool ();
3341 pool->const_rtx_htab = htab_create_ggc (31, const_desc_rtx_hash,
3342 const_desc_rtx_eq, NULL);
3343 pool->first = NULL;
3344 pool->last = NULL;
3345 pool->offset = 0;
3346 return pool;
3349 /* Initialize constant pool hashing for a new function. */
3351 void
3352 init_varasm_status (void)
3354 crtl->varasm.pool = create_constant_pool ();
3355 crtl->varasm.deferred_constants = 0;
3358 /* Given a MINUS expression, simplify it if both sides
3359 include the same symbol. */
3362 simplify_subtraction (rtx x)
3364 rtx r = simplify_rtx (x);
3365 return r ? r : x;
3368 /* Given a constant rtx X, make (or find) a memory constant for its value
3369 and return a MEM rtx to refer to it in memory. */
3372 force_const_mem (enum machine_mode mode, rtx x)
3374 struct constant_descriptor_rtx *desc, tmp;
3375 struct rtx_constant_pool *pool;
3376 char label[256];
3377 rtx def, symbol;
3378 hashval_t hash;
3379 unsigned int align;
3380 void **slot;
3382 /* If we're not allowed to drop X into the constant pool, don't. */
3383 if (targetm.cannot_force_const_mem (x))
3384 return NULL_RTX;
3386 /* Record that this function has used a constant pool entry. */
3387 crtl->uses_const_pool = 1;
3389 /* Decide which pool to use. */
3390 pool = (targetm.use_blocks_for_constant_p (mode, x)
3391 ? shared_constant_pool
3392 : crtl->varasm.pool);
3394 /* Lookup the value in the hashtable. */
3395 tmp.constant = x;
3396 tmp.mode = mode;
3397 hash = const_rtx_hash (x);
3398 slot = htab_find_slot_with_hash (pool->const_rtx_htab, &tmp, hash, INSERT);
3399 desc = (struct constant_descriptor_rtx *) *slot;
3401 /* If the constant was already present, return its memory. */
3402 if (desc)
3403 return copy_rtx (desc->mem);
3405 /* Otherwise, create a new descriptor. */
3406 desc = ggc_alloc_constant_descriptor_rtx ();
3407 *slot = desc;
3409 /* Align the location counter as required by EXP's data type. */
3410 align = GET_MODE_ALIGNMENT (mode == VOIDmode ? word_mode : mode);
3411 #ifdef CONSTANT_ALIGNMENT
3413 tree type = lang_hooks.types.type_for_mode (mode, 0);
3414 if (type != NULL_TREE)
3415 align = CONSTANT_ALIGNMENT (make_tree (type, x), align);
3417 #endif
3419 pool->offset += (align / BITS_PER_UNIT) - 1;
3420 pool->offset &= ~ ((align / BITS_PER_UNIT) - 1);
3422 desc->next = NULL;
3423 desc->constant = tmp.constant;
3424 desc->offset = pool->offset;
3425 desc->hash = hash;
3426 desc->mode = mode;
3427 desc->align = align;
3428 desc->labelno = const_labelno;
3429 desc->mark = 0;
3431 pool->offset += GET_MODE_SIZE (mode);
3432 if (pool->last)
3433 pool->last->next = desc;
3434 else
3435 pool->first = pool->last = desc;
3436 pool->last = desc;
3438 /* Create a string containing the label name, in LABEL. */
3439 ASM_GENERATE_INTERNAL_LABEL (label, "LC", const_labelno);
3440 ++const_labelno;
3442 /* Construct the SYMBOL_REF. Make sure to mark it as belonging to
3443 the constants pool. */
3444 if (use_object_blocks_p () && targetm.use_blocks_for_constant_p (mode, x))
3446 section *sect = targetm.asm_out.select_rtx_section (mode, x, align);
3447 symbol = create_block_symbol (ggc_strdup (label),
3448 get_block_for_section (sect), -1);
3450 else
3451 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3452 desc->sym = symbol;
3453 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3454 CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3455 SET_SYMBOL_REF_CONSTANT (symbol, desc);
3457 /* Construct the MEM. */
3458 desc->mem = def = gen_const_mem (mode, symbol);
3459 set_mem_attributes (def, lang_hooks.types.type_for_mode (mode, 0), 1);
3460 set_mem_align (def, align);
3462 /* If we're dropping a label to the constant pool, make sure we
3463 don't delete it. */
3464 if (GET_CODE (x) == LABEL_REF)
3465 LABEL_PRESERVE_P (XEXP (x, 0)) = 1;
3467 return copy_rtx (def);
3470 /* Given a constant pool SYMBOL_REF, return the corresponding constant. */
3473 get_pool_constant (rtx addr)
3475 return SYMBOL_REF_CONSTANT (addr)->constant;
3478 /* Given a constant pool SYMBOL_REF, return the corresponding constant
3479 and whether it has been output or not. */
3482 get_pool_constant_mark (rtx addr, bool *pmarked)
3484 struct constant_descriptor_rtx *desc;
3486 desc = SYMBOL_REF_CONSTANT (addr);
3487 *pmarked = (desc->mark != 0);
3488 return desc->constant;
3491 /* Similar, return the mode. */
3493 enum machine_mode
3494 get_pool_mode (const_rtx addr)
3496 return SYMBOL_REF_CONSTANT (addr)->mode;
3499 /* Return the size of the constant pool. */
3502 get_pool_size (void)
3504 return crtl->varasm.pool->offset;
3507 /* Worker function for output_constant_pool_1. Emit assembly for X
3508 in MODE with known alignment ALIGN. */
3510 static void
3511 output_constant_pool_2 (enum machine_mode mode, rtx x, unsigned int align)
3513 switch (GET_MODE_CLASS (mode))
3515 case MODE_FLOAT:
3516 case MODE_DECIMAL_FLOAT:
3518 REAL_VALUE_TYPE r;
3520 gcc_assert (GET_CODE (x) == CONST_DOUBLE);
3521 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
3522 assemble_real (r, mode, align);
3523 break;
3526 case MODE_INT:
3527 case MODE_PARTIAL_INT:
3528 case MODE_FRACT:
3529 case MODE_UFRACT:
3530 case MODE_ACCUM:
3531 case MODE_UACCUM:
3532 assemble_integer (x, GET_MODE_SIZE (mode), align, 1);
3533 break;
3535 case MODE_VECTOR_FLOAT:
3536 case MODE_VECTOR_INT:
3537 case MODE_VECTOR_FRACT:
3538 case MODE_VECTOR_UFRACT:
3539 case MODE_VECTOR_ACCUM:
3540 case MODE_VECTOR_UACCUM:
3542 int i, units;
3543 enum machine_mode submode = GET_MODE_INNER (mode);
3544 unsigned int subalign = MIN (align, GET_MODE_BITSIZE (submode));
3546 gcc_assert (GET_CODE (x) == CONST_VECTOR);
3547 units = CONST_VECTOR_NUNITS (x);
3549 for (i = 0; i < units; i++)
3551 rtx elt = CONST_VECTOR_ELT (x, i);
3552 output_constant_pool_2 (submode, elt, i ? subalign : align);
3555 break;
3557 default:
3558 gcc_unreachable ();
3562 /* Worker function for output_constant_pool. Emit constant DESC,
3563 giving it ALIGN bits of alignment. */
3565 static void
3566 output_constant_pool_1 (struct constant_descriptor_rtx *desc,
3567 unsigned int align)
3569 rtx x, tmp;
3571 x = desc->constant;
3573 /* See if X is a LABEL_REF (or a CONST referring to a LABEL_REF)
3574 whose CODE_LABEL has been deleted. This can occur if a jump table
3575 is eliminated by optimization. If so, write a constant of zero
3576 instead. Note that this can also happen by turning the
3577 CODE_LABEL into a NOTE. */
3578 /* ??? This seems completely and utterly wrong. Certainly it's
3579 not true for NOTE_INSN_DELETED_LABEL, but I disbelieve proper
3580 functioning even with INSN_DELETED_P and friends. */
3582 tmp = x;
3583 switch (GET_CODE (tmp))
3585 case CONST:
3586 if (GET_CODE (XEXP (tmp, 0)) != PLUS
3587 || GET_CODE (XEXP (XEXP (tmp, 0), 0)) != LABEL_REF)
3588 break;
3589 tmp = XEXP (XEXP (tmp, 0), 0);
3590 /* FALLTHRU */
3592 case LABEL_REF:
3593 tmp = XEXP (tmp, 0);
3594 gcc_assert (!INSN_DELETED_P (tmp));
3595 gcc_assert (!NOTE_P (tmp)
3596 || NOTE_KIND (tmp) != NOTE_INSN_DELETED);
3597 break;
3599 default:
3600 break;
3603 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3604 ASM_OUTPUT_SPECIAL_POOL_ENTRY (asm_out_file, x, desc->mode,
3605 align, desc->labelno, done);
3606 #endif
3608 assemble_align (align);
3610 /* Output the label. */
3611 targetm.asm_out.internal_label (asm_out_file, "LC", desc->labelno);
3613 /* Output the data. */
3614 output_constant_pool_2 (desc->mode, x, align);
3616 /* Make sure all constants in SECTION_MERGE and not SECTION_STRINGS
3617 sections have proper size. */
3618 if (align > GET_MODE_BITSIZE (desc->mode)
3619 && in_section
3620 && (in_section->common.flags & SECTION_MERGE))
3621 assemble_align (align);
3623 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3624 done:
3625 #endif
3626 return;
3629 /* Given a SYMBOL_REF CURRENT_RTX, mark it and all constants it refers
3630 to as used. Emit referenced deferred strings. This function can
3631 be used with for_each_rtx to mark all SYMBOL_REFs in an rtx. */
3633 static int
3634 mark_constant (rtx *current_rtx, void *data ATTRIBUTE_UNUSED)
3636 rtx x = *current_rtx;
3638 if (x == NULL_RTX || GET_CODE (x) != SYMBOL_REF)
3639 return 0;
3641 if (CONSTANT_POOL_ADDRESS_P (x))
3643 struct constant_descriptor_rtx *desc = SYMBOL_REF_CONSTANT (x);
3644 if (desc->mark == 0)
3646 desc->mark = 1;
3647 for_each_rtx (&desc->constant, mark_constant, NULL);
3650 else if (TREE_CONSTANT_POOL_ADDRESS_P (x))
3652 tree decl = SYMBOL_REF_DECL (x);
3653 if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
3655 n_deferred_constants--;
3656 output_constant_def_contents (x);
3660 return -1;
3663 /* Look through appropriate parts of INSN, marking all entries in the
3664 constant pool which are actually being used. Entries that are only
3665 referenced by other constants are also marked as used. Emit
3666 deferred strings that are used. */
3668 static void
3669 mark_constants (rtx insn)
3671 if (!INSN_P (insn))
3672 return;
3674 /* Insns may appear inside a SEQUENCE. Only check the patterns of
3675 insns, not any notes that may be attached. We don't want to mark
3676 a constant just because it happens to appear in a REG_EQUIV note. */
3677 if (GET_CODE (PATTERN (insn)) == SEQUENCE)
3679 rtx seq = PATTERN (insn);
3680 int i, n = XVECLEN (seq, 0);
3681 for (i = 0; i < n; ++i)
3683 rtx subinsn = XVECEXP (seq, 0, i);
3684 if (INSN_P (subinsn))
3685 for_each_rtx (&PATTERN (subinsn), mark_constant, NULL);
3688 else
3689 for_each_rtx (&PATTERN (insn), mark_constant, NULL);
3692 /* Look through the instructions for this function, and mark all the
3693 entries in POOL which are actually being used. Emit deferred constants
3694 which have indeed been used. */
3696 static void
3697 mark_constant_pool (void)
3699 rtx insn, link;
3701 if (!crtl->uses_const_pool && n_deferred_constants == 0)
3702 return;
3704 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3705 mark_constants (insn);
3707 for (link = crtl->epilogue_delay_list;
3708 link;
3709 link = XEXP (link, 1))
3710 mark_constants (XEXP (link, 0));
3713 /* Write all the constants in POOL. */
3715 static void
3716 output_constant_pool_contents (struct rtx_constant_pool *pool)
3718 struct constant_descriptor_rtx *desc;
3720 for (desc = pool->first; desc ; desc = desc->next)
3721 if (desc->mark)
3723 /* If the constant is part of an object_block, make sure that
3724 the constant has been positioned within its block, but do not
3725 write out its definition yet. output_object_blocks will do
3726 that later. */
3727 if (SYMBOL_REF_HAS_BLOCK_INFO_P (desc->sym)
3728 && SYMBOL_REF_BLOCK (desc->sym))
3729 place_block_symbol (desc->sym);
3730 else
3732 switch_to_section (targetm.asm_out.select_rtx_section
3733 (desc->mode, desc->constant, desc->align));
3734 output_constant_pool_1 (desc, desc->align);
3739 /* Mark all constants that are used in the current function, then write
3740 out the function's private constant pool. */
3742 static void
3743 output_constant_pool (const char *fnname ATTRIBUTE_UNUSED,
3744 tree fndecl ATTRIBUTE_UNUSED)
3746 struct rtx_constant_pool *pool = crtl->varasm.pool;
3748 /* It is possible for gcc to call force_const_mem and then to later
3749 discard the instructions which refer to the constant. In such a
3750 case we do not need to output the constant. */
3751 mark_constant_pool ();
3753 #ifdef ASM_OUTPUT_POOL_PROLOGUE
3754 ASM_OUTPUT_POOL_PROLOGUE (asm_out_file, fnname, fndecl, pool->offset);
3755 #endif
3757 output_constant_pool_contents (pool);
3759 #ifdef ASM_OUTPUT_POOL_EPILOGUE
3760 ASM_OUTPUT_POOL_EPILOGUE (asm_out_file, fnname, fndecl, pool->offset);
3761 #endif
3764 /* Write the contents of the shared constant pool. */
3766 void
3767 output_shared_constant_pool (void)
3769 output_constant_pool_contents (shared_constant_pool);
3772 /* Determine what kind of relocations EXP may need. */
3775 compute_reloc_for_constant (tree exp)
3777 int reloc = 0, reloc2;
3778 tree tem;
3780 switch (TREE_CODE (exp))
3782 case ADDR_EXPR:
3783 case FDESC_EXPR:
3784 /* Go inside any operations that get_inner_reference can handle and see
3785 if what's inside is a constant: no need to do anything here for
3786 addresses of variables or functions. */
3787 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
3788 tem = TREE_OPERAND (tem, 0))
3791 if (TREE_PUBLIC (tem))
3792 reloc |= 2;
3793 else
3794 reloc |= 1;
3795 break;
3797 case PLUS_EXPR:
3798 case POINTER_PLUS_EXPR:
3799 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3800 reloc |= compute_reloc_for_constant (TREE_OPERAND (exp, 1));
3801 break;
3803 case MINUS_EXPR:
3804 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3805 reloc2 = compute_reloc_for_constant (TREE_OPERAND (exp, 1));
3806 /* The difference of two local labels is computable at link time. */
3807 if (reloc == 1 && reloc2 == 1)
3808 reloc = 0;
3809 else
3810 reloc |= reloc2;
3811 break;
3813 CASE_CONVERT:
3814 case VIEW_CONVERT_EXPR:
3815 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3816 break;
3818 case CONSTRUCTOR:
3820 unsigned HOST_WIDE_INT idx;
3821 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
3822 if (tem != 0)
3823 reloc |= compute_reloc_for_constant (tem);
3825 break;
3827 default:
3828 break;
3830 return reloc;
3833 /* Find all the constants whose addresses are referenced inside of EXP,
3834 and make sure assembler code with a label has been output for each one.
3835 Indicate whether an ADDR_EXPR has been encountered. */
3837 static void
3838 output_addressed_constants (tree exp)
3840 tree tem;
3842 switch (TREE_CODE (exp))
3844 case ADDR_EXPR:
3845 case FDESC_EXPR:
3846 /* Go inside any operations that get_inner_reference can handle and see
3847 if what's inside is a constant: no need to do anything here for
3848 addresses of variables or functions. */
3849 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
3850 tem = TREE_OPERAND (tem, 0))
3853 /* If we have an initialized CONST_DECL, retrieve the initializer. */
3854 if (TREE_CODE (tem) == CONST_DECL && DECL_INITIAL (tem))
3855 tem = DECL_INITIAL (tem);
3857 if (CONSTANT_CLASS_P (tem) || TREE_CODE (tem) == CONSTRUCTOR)
3858 output_constant_def (tem, 0);
3859 break;
3861 case PLUS_EXPR:
3862 case POINTER_PLUS_EXPR:
3863 case MINUS_EXPR:
3864 output_addressed_constants (TREE_OPERAND (exp, 1));
3865 /* Fall through. */
3867 CASE_CONVERT:
3868 case VIEW_CONVERT_EXPR:
3869 output_addressed_constants (TREE_OPERAND (exp, 0));
3870 break;
3872 case CONSTRUCTOR:
3874 unsigned HOST_WIDE_INT idx;
3875 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
3876 if (tem != 0)
3877 output_addressed_constants (tem);
3879 break;
3881 default:
3882 break;
3886 /* Whether a constructor CTOR is a valid static constant initializer if all
3887 its elements are. This used to be internal to initializer_constant_valid_p
3888 and has been exposed to let other functions like categorize_ctor_elements
3889 evaluate the property while walking a constructor for other purposes. */
3891 bool
3892 constructor_static_from_elts_p (const_tree ctor)
3894 return (TREE_CONSTANT (ctor)
3895 && (TREE_CODE (TREE_TYPE (ctor)) == UNION_TYPE
3896 || TREE_CODE (TREE_TYPE (ctor)) == RECORD_TYPE)
3897 && !VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (ctor)));
3900 static tree initializer_constant_valid_p_1 (tree value, tree endtype,
3901 tree *cache);
3903 /* A subroutine of initializer_constant_valid_p. VALUE is a MINUS_EXPR,
3904 PLUS_EXPR or POINTER_PLUS_EXPR. This looks for cases of VALUE
3905 which are valid when ENDTYPE is an integer of any size; in
3906 particular, this does not accept a pointer minus a constant. This
3907 returns null_pointer_node if the VALUE is an absolute constant
3908 which can be used to initialize a static variable. Otherwise it
3909 returns NULL. */
3911 static tree
3912 narrowing_initializer_constant_valid_p (tree value, tree endtype, tree *cache)
3914 tree op0, op1;
3916 if (!INTEGRAL_TYPE_P (endtype))
3917 return NULL_TREE;
3919 op0 = TREE_OPERAND (value, 0);
3920 op1 = TREE_OPERAND (value, 1);
3922 /* Like STRIP_NOPS except allow the operand mode to widen. This
3923 works around a feature of fold that simplifies (int)(p1 - p2) to
3924 ((int)p1 - (int)p2) under the theory that the narrower operation
3925 is cheaper. */
3927 while (CONVERT_EXPR_P (op0)
3928 || TREE_CODE (op0) == NON_LVALUE_EXPR)
3930 tree inner = TREE_OPERAND (op0, 0);
3931 if (inner == error_mark_node
3932 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
3933 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))
3934 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
3935 break;
3936 op0 = inner;
3939 while (CONVERT_EXPR_P (op1)
3940 || TREE_CODE (op1) == NON_LVALUE_EXPR)
3942 tree inner = TREE_OPERAND (op1, 0);
3943 if (inner == error_mark_node
3944 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
3945 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op1)))
3946 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
3947 break;
3948 op1 = inner;
3951 op0 = initializer_constant_valid_p_1 (op0, endtype, cache);
3952 if (!op0)
3953 return NULL_TREE;
3955 op1 = initializer_constant_valid_p_1 (op1, endtype,
3956 cache ? cache + 2 : NULL);
3957 /* Both initializers must be known. */
3958 if (op1)
3960 if (op0 == op1
3961 && (op0 == null_pointer_node
3962 || TREE_CODE (value) == MINUS_EXPR))
3963 return null_pointer_node;
3965 /* Support differences between labels. */
3966 if (TREE_CODE (op0) == LABEL_DECL
3967 && TREE_CODE (op1) == LABEL_DECL)
3968 return null_pointer_node;
3970 if (TREE_CODE (op0) == STRING_CST
3971 && TREE_CODE (op1) == STRING_CST
3972 && operand_equal_p (op0, op1, 1))
3973 return null_pointer_node;
3976 return NULL_TREE;
3979 /* Helper function of initializer_constant_valid_p.
3980 Return nonzero if VALUE is a valid constant-valued expression
3981 for use in initializing a static variable; one that can be an
3982 element of a "constant" initializer.
3984 Return null_pointer_node if the value is absolute;
3985 if it is relocatable, return the variable that determines the relocation.
3986 We assume that VALUE has been folded as much as possible;
3987 therefore, we do not need to check for such things as
3988 arithmetic-combinations of integers.
3990 Use CACHE (pointer to 2 tree values) for caching if non-NULL. */
3992 static tree
3993 initializer_constant_valid_p_1 (tree value, tree endtype, tree *cache)
3995 tree ret;
3997 switch (TREE_CODE (value))
3999 case CONSTRUCTOR:
4000 if (constructor_static_from_elts_p (value))
4002 unsigned HOST_WIDE_INT idx;
4003 tree elt;
4004 bool absolute = true;
4006 if (cache && cache[0] == value)
4007 return cache[1];
4008 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
4010 tree reloc;
4011 reloc = initializer_constant_valid_p_1 (elt, TREE_TYPE (elt),
4012 NULL);
4013 if (!reloc)
4015 if (cache)
4017 cache[0] = value;
4018 cache[1] = NULL_TREE;
4020 return NULL_TREE;
4022 if (reloc != null_pointer_node)
4023 absolute = false;
4025 /* For a non-absolute relocation, there is no single
4026 variable that can be "the variable that determines the
4027 relocation." */
4028 if (cache)
4030 cache[0] = value;
4031 cache[1] = absolute ? null_pointer_node : error_mark_node;
4033 return absolute ? null_pointer_node : error_mark_node;
4036 return TREE_STATIC (value) ? null_pointer_node : NULL_TREE;
4038 case INTEGER_CST:
4039 case VECTOR_CST:
4040 case REAL_CST:
4041 case FIXED_CST:
4042 case STRING_CST:
4043 case COMPLEX_CST:
4044 return null_pointer_node;
4046 case ADDR_EXPR:
4047 case FDESC_EXPR:
4049 tree op0 = staticp (TREE_OPERAND (value, 0));
4050 if (op0)
4052 /* "&(*a).f" is like unto pointer arithmetic. If "a" turns out
4053 to be a constant, this is old-skool offsetof-like nonsense. */
4054 if (TREE_CODE (op0) == INDIRECT_REF
4055 && TREE_CONSTANT (TREE_OPERAND (op0, 0)))
4056 return null_pointer_node;
4057 /* Taking the address of a nested function involves a trampoline,
4058 unless we don't need or want one. */
4059 if (TREE_CODE (op0) == FUNCTION_DECL
4060 && DECL_STATIC_CHAIN (op0)
4061 && !TREE_NO_TRAMPOLINE (value))
4062 return NULL_TREE;
4063 /* "&{...}" requires a temporary to hold the constructed
4064 object. */
4065 if (TREE_CODE (op0) == CONSTRUCTOR)
4066 return NULL_TREE;
4068 return op0;
4071 case NON_LVALUE_EXPR:
4072 return initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4073 endtype, cache);
4075 case VIEW_CONVERT_EXPR:
4077 tree src = TREE_OPERAND (value, 0);
4078 tree src_type = TREE_TYPE (src);
4079 tree dest_type = TREE_TYPE (value);
4081 /* Allow view-conversions from aggregate to non-aggregate type only
4082 if the bit pattern is fully preserved afterwards; otherwise, the
4083 RTL expander won't be able to apply a subsequent transformation
4084 to the underlying constructor. */
4085 if (AGGREGATE_TYPE_P (src_type) && !AGGREGATE_TYPE_P (dest_type))
4087 if (TYPE_MODE (endtype) == TYPE_MODE (dest_type))
4088 return initializer_constant_valid_p_1 (src, endtype, cache);
4089 else
4090 return NULL_TREE;
4093 /* Allow all other kinds of view-conversion. */
4094 return initializer_constant_valid_p_1 (src, endtype, cache);
4097 CASE_CONVERT:
4099 tree src = TREE_OPERAND (value, 0);
4100 tree src_type = TREE_TYPE (src);
4101 tree dest_type = TREE_TYPE (value);
4103 /* Allow conversions between pointer types, floating-point
4104 types, and offset types. */
4105 if ((POINTER_TYPE_P (dest_type) && POINTER_TYPE_P (src_type))
4106 || (FLOAT_TYPE_P (dest_type) && FLOAT_TYPE_P (src_type))
4107 || (TREE_CODE (dest_type) == OFFSET_TYPE
4108 && TREE_CODE (src_type) == OFFSET_TYPE))
4109 return initializer_constant_valid_p_1 (src, endtype, cache);
4111 /* Allow length-preserving conversions between integer types. */
4112 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type)
4113 && (TYPE_PRECISION (dest_type) == TYPE_PRECISION (src_type)))
4114 return initializer_constant_valid_p_1 (src, endtype, cache);
4116 /* Allow conversions between other integer types only if
4117 explicit value. */
4118 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type))
4120 tree inner = initializer_constant_valid_p_1 (src, endtype, cache);
4121 if (inner == null_pointer_node)
4122 return null_pointer_node;
4123 break;
4126 /* Allow (int) &foo provided int is as wide as a pointer. */
4127 if (INTEGRAL_TYPE_P (dest_type) && POINTER_TYPE_P (src_type)
4128 && (TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type)))
4129 return initializer_constant_valid_p_1 (src, endtype, cache);
4131 /* Likewise conversions from int to pointers, but also allow
4132 conversions from 0. */
4133 if ((POINTER_TYPE_P (dest_type)
4134 || TREE_CODE (dest_type) == OFFSET_TYPE)
4135 && INTEGRAL_TYPE_P (src_type))
4137 if (TREE_CODE (src) == INTEGER_CST
4138 && TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type))
4139 return null_pointer_node;
4140 if (integer_zerop (src))
4141 return null_pointer_node;
4142 else if (TYPE_PRECISION (dest_type) <= TYPE_PRECISION (src_type))
4143 return initializer_constant_valid_p_1 (src, endtype, cache);
4146 /* Allow conversions to struct or union types if the value
4147 inside is okay. */
4148 if (TREE_CODE (dest_type) == RECORD_TYPE
4149 || TREE_CODE (dest_type) == UNION_TYPE)
4150 return initializer_constant_valid_p_1 (src, endtype, cache);
4152 break;
4154 case POINTER_PLUS_EXPR:
4155 case PLUS_EXPR:
4156 /* Any valid floating-point constants will have been folded by now;
4157 with -frounding-math we hit this with addition of two constants. */
4158 if (TREE_CODE (endtype) == REAL_TYPE)
4159 return NULL_TREE;
4160 if (cache && cache[0] == value)
4161 return cache[1];
4162 if (! INTEGRAL_TYPE_P (endtype)
4163 || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
4165 tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
4166 tree valid0
4167 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4168 endtype, ncache);
4169 tree valid1
4170 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
4171 endtype, ncache + 2);
4172 /* If either term is absolute, use the other term's relocation. */
4173 if (valid0 == null_pointer_node)
4174 ret = valid1;
4175 else if (valid1 == null_pointer_node)
4176 ret = valid0;
4177 /* Support narrowing pointer differences. */
4178 else
4179 ret = narrowing_initializer_constant_valid_p (value, endtype,
4180 ncache);
4182 else
4183 /* Support narrowing pointer differences. */
4184 ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
4185 if (cache)
4187 cache[0] = value;
4188 cache[1] = ret;
4190 return ret;
4192 case MINUS_EXPR:
4193 if (TREE_CODE (endtype) == REAL_TYPE)
4194 return NULL_TREE;
4195 if (cache && cache[0] == value)
4196 return cache[1];
4197 if (! INTEGRAL_TYPE_P (endtype)
4198 || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
4200 tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
4201 tree valid0
4202 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4203 endtype, ncache);
4204 tree valid1
4205 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
4206 endtype, ncache + 2);
4207 /* Win if second argument is absolute. */
4208 if (valid1 == null_pointer_node)
4209 ret = valid0;
4210 /* Win if both arguments have the same relocation.
4211 Then the value is absolute. */
4212 else if (valid0 == valid1 && valid0 != 0)
4213 ret = null_pointer_node;
4214 /* Since GCC guarantees that string constants are unique in the
4215 generated code, a subtraction between two copies of the same
4216 constant string is absolute. */
4217 else if (valid0 && TREE_CODE (valid0) == STRING_CST
4218 && valid1 && TREE_CODE (valid1) == STRING_CST
4219 && operand_equal_p (valid0, valid1, 1))
4220 ret = null_pointer_node;
4221 /* Support narrowing differences. */
4222 else
4223 ret = narrowing_initializer_constant_valid_p (value, endtype,
4224 ncache);
4226 else
4227 /* Support narrowing differences. */
4228 ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
4229 if (cache)
4231 cache[0] = value;
4232 cache[1] = ret;
4234 return ret;
4236 default:
4237 break;
4240 return NULL_TREE;
4243 /* Return nonzero if VALUE is a valid constant-valued expression
4244 for use in initializing a static variable; one that can be an
4245 element of a "constant" initializer.
4247 Return null_pointer_node if the value is absolute;
4248 if it is relocatable, return the variable that determines the relocation.
4249 We assume that VALUE has been folded as much as possible;
4250 therefore, we do not need to check for such things as
4251 arithmetic-combinations of integers. */
4252 tree
4253 initializer_constant_valid_p (tree value, tree endtype)
4255 return initializer_constant_valid_p_1 (value, endtype, NULL);
4258 /* Return true if VALUE is a valid constant-valued expression
4259 for use in initializing a static bit-field; one that can be
4260 an element of a "constant" initializer. */
4262 bool
4263 initializer_constant_valid_for_bitfield_p (tree value)
4265 /* For bitfields we support integer constants or possibly nested aggregates
4266 of such. */
4267 switch (TREE_CODE (value))
4269 case CONSTRUCTOR:
4271 unsigned HOST_WIDE_INT idx;
4272 tree elt;
4274 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
4275 if (!initializer_constant_valid_for_bitfield_p (elt))
4276 return false;
4277 return true;
4280 case INTEGER_CST:
4281 return true;
4283 case VIEW_CONVERT_EXPR:
4284 case NON_LVALUE_EXPR:
4285 return
4286 initializer_constant_valid_for_bitfield_p (TREE_OPERAND (value, 0));
4288 default:
4289 break;
4292 return false;
4295 /* output_constructor outer state of relevance in recursive calls, typically
4296 for nested aggregate bitfields. */
4298 typedef struct {
4299 unsigned int bit_offset; /* current position in ... */
4300 int byte; /* ... the outer byte buffer. */
4301 } oc_outer_state;
4303 static unsigned HOST_WIDE_INT
4304 output_constructor (tree, unsigned HOST_WIDE_INT, unsigned int,
4305 oc_outer_state *);
4307 /* Output assembler code for constant EXP to FILE, with no label.
4308 This includes the pseudo-op such as ".int" or ".byte", and a newline.
4309 Assumes output_addressed_constants has been done on EXP already.
4311 Generate exactly SIZE bytes of assembler data, padding at the end
4312 with zeros if necessary. SIZE must always be specified.
4314 SIZE is important for structure constructors,
4315 since trailing members may have been omitted from the constructor.
4316 It is also important for initialization of arrays from string constants
4317 since the full length of the string constant might not be wanted.
4318 It is also needed for initialization of unions, where the initializer's
4319 type is just one member, and that may not be as long as the union.
4321 There a case in which we would fail to output exactly SIZE bytes:
4322 for a structure constructor that wants to produce more than SIZE bytes.
4323 But such constructors will never be generated for any possible input.
4325 ALIGN is the alignment of the data in bits. */
4327 void
4328 output_constant (tree exp, unsigned HOST_WIDE_INT size, unsigned int align)
4330 enum tree_code code;
4331 unsigned HOST_WIDE_INT thissize;
4333 if (size == 0 || flag_syntax_only)
4334 return;
4336 /* See if we're trying to initialize a pointer in a non-default mode
4337 to the address of some declaration somewhere. If the target says
4338 the mode is valid for pointers, assume the target has a way of
4339 resolving it. */
4340 if (TREE_CODE (exp) == NOP_EXPR
4341 && POINTER_TYPE_P (TREE_TYPE (exp))
4342 && targetm.addr_space.valid_pointer_mode
4343 (TYPE_MODE (TREE_TYPE (exp)),
4344 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
4346 tree saved_type = TREE_TYPE (exp);
4348 /* Peel off any intermediate conversions-to-pointer for valid
4349 pointer modes. */
4350 while (TREE_CODE (exp) == NOP_EXPR
4351 && POINTER_TYPE_P (TREE_TYPE (exp))
4352 && targetm.addr_space.valid_pointer_mode
4353 (TYPE_MODE (TREE_TYPE (exp)),
4354 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
4355 exp = TREE_OPERAND (exp, 0);
4357 /* If what we're left with is the address of something, we can
4358 convert the address to the final type and output it that
4359 way. */
4360 if (TREE_CODE (exp) == ADDR_EXPR)
4361 exp = build1 (ADDR_EXPR, saved_type, TREE_OPERAND (exp, 0));
4362 /* Likewise for constant ints. */
4363 else if (TREE_CODE (exp) == INTEGER_CST)
4364 exp = build_int_cst_wide (saved_type, TREE_INT_CST_LOW (exp),
4365 TREE_INT_CST_HIGH (exp));
4369 /* Eliminate any conversions since we'll be outputting the underlying
4370 constant. */
4371 while (CONVERT_EXPR_P (exp)
4372 || TREE_CODE (exp) == NON_LVALUE_EXPR
4373 || TREE_CODE (exp) == VIEW_CONVERT_EXPR)
4375 HOST_WIDE_INT type_size = int_size_in_bytes (TREE_TYPE (exp));
4376 HOST_WIDE_INT op_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (exp, 0)));
4378 /* Make sure eliminating the conversion is really a no-op, except with
4379 VIEW_CONVERT_EXPRs to allow for wild Ada unchecked conversions and
4380 union types to allow for Ada unchecked unions. */
4381 if (type_size > op_size
4382 && TREE_CODE (exp) != VIEW_CONVERT_EXPR
4383 && TREE_CODE (TREE_TYPE (exp)) != UNION_TYPE)
4384 /* Keep the conversion. */
4385 break;
4386 else
4387 exp = TREE_OPERAND (exp, 0);
4390 code = TREE_CODE (TREE_TYPE (exp));
4391 thissize = int_size_in_bytes (TREE_TYPE (exp));
4393 /* Allow a constructor with no elements for any data type.
4394 This means to fill the space with zeros. */
4395 if (TREE_CODE (exp) == CONSTRUCTOR
4396 && VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (exp)))
4398 assemble_zeros (size);
4399 return;
4402 if (TREE_CODE (exp) == FDESC_EXPR)
4404 #ifdef ASM_OUTPUT_FDESC
4405 HOST_WIDE_INT part = tree_low_cst (TREE_OPERAND (exp, 1), 0);
4406 tree decl = TREE_OPERAND (exp, 0);
4407 ASM_OUTPUT_FDESC (asm_out_file, decl, part);
4408 #else
4409 gcc_unreachable ();
4410 #endif
4411 return;
4414 /* Now output the underlying data. If we've handling the padding, return.
4415 Otherwise, break and ensure SIZE is the size written. */
4416 switch (code)
4418 case BOOLEAN_TYPE:
4419 case INTEGER_TYPE:
4420 case ENUMERAL_TYPE:
4421 case POINTER_TYPE:
4422 case REFERENCE_TYPE:
4423 case OFFSET_TYPE:
4424 case FIXED_POINT_TYPE:
4425 if (! assemble_integer (expand_expr (exp, NULL_RTX, VOIDmode,
4426 EXPAND_INITIALIZER),
4427 MIN (size, thissize), align, 0))
4428 error ("initializer for integer/fixed-point value is too complicated");
4429 break;
4431 case REAL_TYPE:
4432 if (TREE_CODE (exp) != REAL_CST)
4433 error ("initializer for floating value is not a floating constant");
4434 else
4435 assemble_real (TREE_REAL_CST (exp), TYPE_MODE (TREE_TYPE (exp)), align);
4436 break;
4438 case COMPLEX_TYPE:
4439 output_constant (TREE_REALPART (exp), thissize / 2, align);
4440 output_constant (TREE_IMAGPART (exp), thissize / 2,
4441 min_align (align, BITS_PER_UNIT * (thissize / 2)));
4442 break;
4444 case ARRAY_TYPE:
4445 case VECTOR_TYPE:
4446 switch (TREE_CODE (exp))
4448 case CONSTRUCTOR:
4449 output_constructor (exp, size, align, NULL);
4450 return;
4451 case STRING_CST:
4452 thissize = MIN ((unsigned HOST_WIDE_INT)TREE_STRING_LENGTH (exp),
4453 size);
4454 assemble_string (TREE_STRING_POINTER (exp), thissize);
4455 break;
4457 case VECTOR_CST:
4459 int elt_size;
4460 tree link;
4461 unsigned int nalign;
4462 enum machine_mode inner;
4464 inner = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
4465 nalign = MIN (align, GET_MODE_ALIGNMENT (inner));
4467 elt_size = GET_MODE_SIZE (inner);
4469 link = TREE_VECTOR_CST_ELTS (exp);
4470 output_constant (TREE_VALUE (link), elt_size, align);
4471 thissize = elt_size;
4472 while ((link = TREE_CHAIN (link)) != NULL)
4474 output_constant (TREE_VALUE (link), elt_size, nalign);
4475 thissize += elt_size;
4477 break;
4479 default:
4480 gcc_unreachable ();
4482 break;
4484 case RECORD_TYPE:
4485 case UNION_TYPE:
4486 gcc_assert (TREE_CODE (exp) == CONSTRUCTOR);
4487 output_constructor (exp, size, align, NULL);
4488 return;
4490 case ERROR_MARK:
4491 return;
4493 default:
4494 gcc_unreachable ();
4497 if (size > thissize)
4498 assemble_zeros (size - thissize);
4502 /* Subroutine of output_constructor, used for computing the size of
4503 arrays of unspecified length. VAL must be a CONSTRUCTOR of an array
4504 type with an unspecified upper bound. */
4506 static unsigned HOST_WIDE_INT
4507 array_size_for_constructor (tree val)
4509 tree max_index, i;
4510 unsigned HOST_WIDE_INT cnt;
4511 tree index, value, tmp;
4513 /* This code used to attempt to handle string constants that are not
4514 arrays of single-bytes, but nothing else does, so there's no point in
4515 doing it here. */
4516 if (TREE_CODE (val) == STRING_CST)
4517 return TREE_STRING_LENGTH (val);
4519 max_index = NULL_TREE;
4520 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (val), cnt, index, value)
4522 if (TREE_CODE (index) == RANGE_EXPR)
4523 index = TREE_OPERAND (index, 1);
4524 if (max_index == NULL_TREE || tree_int_cst_lt (max_index, index))
4525 max_index = index;
4528 if (max_index == NULL_TREE)
4529 return 0;
4531 /* Compute the total number of array elements. */
4532 tmp = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (val)));
4533 i = size_binop (MINUS_EXPR, fold_convert (sizetype, max_index),
4534 fold_convert (sizetype, tmp));
4535 i = size_binop (PLUS_EXPR, i, size_one_node);
4537 /* Multiply by the array element unit size to find number of bytes. */
4538 i = size_binop (MULT_EXPR, i, TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (val))));
4540 return tree_low_cst (i, 1);
4543 /* Other datastructures + helpers for output_constructor. */
4545 /* output_constructor local state to support interaction with helpers. */
4547 typedef struct {
4549 /* Received arguments. */
4550 tree exp; /* Constructor expression. */
4551 unsigned HOST_WIDE_INT size; /* # bytes to output - pad if necessary. */
4552 unsigned int align; /* Known initial alignment. */
4554 /* Constructor expression data. */
4555 tree type; /* Expression type. */
4556 tree field; /* Current field decl in a record. */
4557 tree min_index; /* Lower bound if specified for an array. */
4559 /* Output processing state. */
4560 HOST_WIDE_INT total_bytes; /* # bytes output so far / current position. */
4561 bool byte_buffer_in_use; /* Whether byte ... */
4562 int byte; /* ... contains part of a bitfield byte yet to
4563 be output. */
4565 int last_relative_index; /* Implicit or explicit index of the last
4566 array element output within a bitfield. */
4567 /* Current element. */
4568 tree val; /* Current element value. */
4569 tree index; /* Current element index. */
4571 } oc_local_state;
4573 /* Helper for output_constructor. From the current LOCAL state, output a
4574 RANGE_EXPR element. */
4576 static void
4577 output_constructor_array_range (oc_local_state *local)
4579 unsigned HOST_WIDE_INT fieldsize
4580 = int_size_in_bytes (TREE_TYPE (local->type));
4582 HOST_WIDE_INT lo_index
4583 = tree_low_cst (TREE_OPERAND (local->index, 0), 0);
4584 HOST_WIDE_INT hi_index
4585 = tree_low_cst (TREE_OPERAND (local->index, 1), 0);
4586 HOST_WIDE_INT index;
4588 unsigned int align2
4589 = min_align (local->align, fieldsize * BITS_PER_UNIT);
4591 for (index = lo_index; index <= hi_index; index++)
4593 /* Output the element's initial value. */
4594 if (local->val == NULL_TREE)
4595 assemble_zeros (fieldsize);
4596 else
4597 output_constant (local->val, fieldsize, align2);
4599 /* Count its size. */
4600 local->total_bytes += fieldsize;
4604 /* Helper for output_constructor. From the current LOCAL state, output a
4605 field element that is not true bitfield or part of an outer one. */
4607 static void
4608 output_constructor_regular_field (oc_local_state *local)
4610 /* Field size and position. Since this structure is static, we know the
4611 positions are constant. */
4612 unsigned HOST_WIDE_INT fieldsize;
4613 HOST_WIDE_INT fieldpos;
4615 unsigned int align2;
4617 if (local->index != NULL_TREE)
4618 fieldpos = (tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (local->val)), 1)
4619 * ((tree_low_cst (local->index, 0)
4620 - tree_low_cst (local->min_index, 0))));
4621 else if (local->field != NULL_TREE)
4622 fieldpos = int_byte_position (local->field);
4623 else
4624 fieldpos = 0;
4626 /* Output any buffered-up bit-fields preceding this element. */
4627 if (local->byte_buffer_in_use)
4629 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
4630 local->total_bytes++;
4631 local->byte_buffer_in_use = false;
4634 /* Advance to offset of this element.
4635 Note no alignment needed in an array, since that is guaranteed
4636 if each element has the proper size. */
4637 if ((local->field != NULL_TREE || local->index != NULL_TREE)
4638 && fieldpos != local->total_bytes)
4640 gcc_assert (fieldpos >= local->total_bytes);
4641 assemble_zeros (fieldpos - local->total_bytes);
4642 local->total_bytes = fieldpos;
4645 /* Find the alignment of this element. */
4646 align2 = min_align (local->align, BITS_PER_UNIT * fieldpos);
4648 /* Determine size this element should occupy. */
4649 if (local->field)
4651 fieldsize = 0;
4653 /* If this is an array with an unspecified upper bound,
4654 the initializer determines the size. */
4655 /* ??? This ought to only checked if DECL_SIZE_UNIT is NULL,
4656 but we cannot do this until the deprecated support for
4657 initializing zero-length array members is removed. */
4658 if (TREE_CODE (TREE_TYPE (local->field)) == ARRAY_TYPE
4659 && TYPE_DOMAIN (TREE_TYPE (local->field))
4660 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (local->field))))
4662 fieldsize = array_size_for_constructor (local->val);
4663 /* Given a non-empty initialization, this field had
4664 better be last. */
4665 gcc_assert (!fieldsize || !DECL_CHAIN (local->field));
4667 else if (DECL_SIZE_UNIT (local->field))
4669 /* ??? This can't be right. If the decl size overflows
4670 a host integer we will silently emit no data. */
4671 if (host_integerp (DECL_SIZE_UNIT (local->field), 1))
4672 fieldsize = tree_low_cst (DECL_SIZE_UNIT (local->field), 1);
4675 else
4676 fieldsize = int_size_in_bytes (TREE_TYPE (local->type));
4678 /* Output the element's initial value. */
4679 if (local->val == NULL_TREE)
4680 assemble_zeros (fieldsize);
4681 else
4682 output_constant (local->val, fieldsize, align2);
4684 /* Count its size. */
4685 local->total_bytes += fieldsize;
4688 /* Helper for output_constructor. From the current LOCAL and OUTER states,
4689 output an element that is a true bitfield or part of an outer one. */
4691 static void
4692 output_constructor_bitfield (oc_local_state *local, oc_outer_state *outer)
4694 /* Bit size of this element. */
4695 HOST_WIDE_INT ebitsize
4696 = (local->field
4697 ? tree_low_cst (DECL_SIZE (local->field), 1)
4698 : tree_low_cst (TYPE_SIZE (TREE_TYPE (local->type)), 1));
4700 /* Relative index of this element if this is an array component. */
4701 HOST_WIDE_INT relative_index
4702 = (!local->field
4703 ? (local->index
4704 ? (tree_low_cst (local->index, 0)
4705 - tree_low_cst (local->min_index, 0))
4706 : local->last_relative_index + 1)
4707 : 0);
4709 /* Bit position of this element from the start of the containing
4710 constructor. */
4711 HOST_WIDE_INT constructor_relative_ebitpos
4712 = (local->field
4713 ? int_bit_position (local->field)
4714 : ebitsize * relative_index);
4716 /* Bit position of this element from the start of a possibly ongoing
4717 outer byte buffer. */
4718 HOST_WIDE_INT byte_relative_ebitpos
4719 = ((outer ? outer->bit_offset : 0) + constructor_relative_ebitpos);
4721 /* From the start of a possibly ongoing outer byte buffer, offsets to
4722 the first bit of this element and to the first bit past the end of
4723 this element. */
4724 HOST_WIDE_INT next_offset = byte_relative_ebitpos;
4725 HOST_WIDE_INT end_offset = byte_relative_ebitpos + ebitsize;
4727 local->last_relative_index = relative_index;
4729 if (local->val == NULL_TREE)
4730 local->val = integer_zero_node;
4732 while (TREE_CODE (local->val) == VIEW_CONVERT_EXPR
4733 || TREE_CODE (local->val) == NON_LVALUE_EXPR)
4734 local->val = TREE_OPERAND (local->val, 0);
4736 if (TREE_CODE (local->val) != INTEGER_CST
4737 && TREE_CODE (local->val) != CONSTRUCTOR)
4739 error ("invalid initial value for member %qE", DECL_NAME (local->field));
4740 return;
4743 /* If this field does not start in this (or, next) byte,
4744 skip some bytes. */
4745 if (next_offset / BITS_PER_UNIT != local->total_bytes)
4747 /* Output remnant of any bit field in previous bytes. */
4748 if (local->byte_buffer_in_use)
4750 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
4751 local->total_bytes++;
4752 local->byte_buffer_in_use = false;
4755 /* If still not at proper byte, advance to there. */
4756 if (next_offset / BITS_PER_UNIT != local->total_bytes)
4758 gcc_assert (next_offset / BITS_PER_UNIT >= local->total_bytes);
4759 assemble_zeros (next_offset / BITS_PER_UNIT - local->total_bytes);
4760 local->total_bytes = next_offset / BITS_PER_UNIT;
4764 /* Set up the buffer if necessary. */
4765 if (!local->byte_buffer_in_use)
4767 local->byte = 0;
4768 if (ebitsize > 0)
4769 local->byte_buffer_in_use = true;
4772 /* If this is nested constructor, recurse passing the bit offset and the
4773 pending data, then retrieve the new pending data afterwards. */
4774 if (TREE_CODE (local->val) == CONSTRUCTOR)
4776 oc_outer_state output_state;
4778 output_state.bit_offset = next_offset % BITS_PER_UNIT;
4779 output_state.byte = local->byte;
4780 local->total_bytes
4781 += output_constructor (local->val, 0, 0, &output_state);
4782 local->byte = output_state.byte;
4783 return;
4786 /* Otherwise, we must split the element into pieces that fall within
4787 separate bytes, and combine each byte with previous or following
4788 bit-fields. */
4789 while (next_offset < end_offset)
4791 int this_time;
4792 int shift;
4793 HOST_WIDE_INT value;
4794 HOST_WIDE_INT next_byte = next_offset / BITS_PER_UNIT;
4795 HOST_WIDE_INT next_bit = next_offset % BITS_PER_UNIT;
4797 /* Advance from byte to byte
4798 within this element when necessary. */
4799 while (next_byte != local->total_bytes)
4801 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
4802 local->total_bytes++;
4803 local->byte = 0;
4806 /* Number of bits we can process at once
4807 (all part of the same byte). */
4808 this_time = MIN (end_offset - next_offset,
4809 BITS_PER_UNIT - next_bit);
4810 if (BYTES_BIG_ENDIAN)
4812 /* On big-endian machine, take the most significant bits
4813 first (of the bits that are significant)
4814 and put them into bytes from the most significant end. */
4815 shift = end_offset - next_offset - this_time;
4817 /* Don't try to take a bunch of bits that cross
4818 the word boundary in the INTEGER_CST. We can
4819 only select bits from the LOW or HIGH part
4820 not from both. */
4821 if (shift < HOST_BITS_PER_WIDE_INT
4822 && shift + this_time > HOST_BITS_PER_WIDE_INT)
4824 this_time = shift + this_time - HOST_BITS_PER_WIDE_INT;
4825 shift = HOST_BITS_PER_WIDE_INT;
4828 /* Now get the bits from the appropriate constant word. */
4829 if (shift < HOST_BITS_PER_WIDE_INT)
4830 value = TREE_INT_CST_LOW (local->val);
4831 else
4833 gcc_assert (shift < 2 * HOST_BITS_PER_WIDE_INT);
4834 value = TREE_INT_CST_HIGH (local->val);
4835 shift -= HOST_BITS_PER_WIDE_INT;
4838 /* Get the result. This works only when:
4839 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
4840 local->byte |= (((value >> shift)
4841 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
4842 << (BITS_PER_UNIT - this_time - next_bit));
4844 else
4846 /* On little-endian machines,
4847 take first the least significant bits of the value
4848 and pack them starting at the least significant
4849 bits of the bytes. */
4850 shift = next_offset - byte_relative_ebitpos;
4852 /* Don't try to take a bunch of bits that cross
4853 the word boundary in the INTEGER_CST. We can
4854 only select bits from the LOW or HIGH part
4855 not from both. */
4856 if (shift < HOST_BITS_PER_WIDE_INT
4857 && shift + this_time > HOST_BITS_PER_WIDE_INT)
4858 this_time = (HOST_BITS_PER_WIDE_INT - shift);
4860 /* Now get the bits from the appropriate constant word. */
4861 if (shift < HOST_BITS_PER_WIDE_INT)
4862 value = TREE_INT_CST_LOW (local->val);
4863 else
4865 gcc_assert (shift < 2 * HOST_BITS_PER_WIDE_INT);
4866 value = TREE_INT_CST_HIGH (local->val);
4867 shift -= HOST_BITS_PER_WIDE_INT;
4870 /* Get the result. This works only when:
4871 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
4872 local->byte |= (((value >> shift)
4873 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
4874 << next_bit);
4877 next_offset += this_time;
4878 local->byte_buffer_in_use = true;
4882 /* Subroutine of output_constant, used for CONSTRUCTORs (aggregate constants).
4883 Generate at least SIZE bytes, padding if necessary. OUTER designates the
4884 caller output state of relevance in recursive invocations. */
4886 static unsigned HOST_WIDE_INT
4887 output_constructor (tree exp, unsigned HOST_WIDE_INT size,
4888 unsigned int align, oc_outer_state * outer)
4890 unsigned HOST_WIDE_INT cnt;
4891 constructor_elt *ce;
4893 oc_local_state local;
4895 /* Setup our local state to communicate with helpers. */
4896 local.exp = exp;
4897 local.size = size;
4898 local.align = align;
4900 local.total_bytes = 0;
4901 local.byte_buffer_in_use = outer != NULL;
4902 local.byte = outer ? outer->byte : 0;
4904 local.type = TREE_TYPE (exp);
4906 local.last_relative_index = -1;
4908 local.min_index = NULL_TREE;
4909 if (TREE_CODE (local.type) == ARRAY_TYPE
4910 && TYPE_DOMAIN (local.type) != NULL_TREE)
4911 local.min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (local.type));
4913 gcc_assert (HOST_BITS_PER_WIDE_INT >= BITS_PER_UNIT);
4915 /* As CE goes through the elements of the constant, FIELD goes through the
4916 structure fields if the constant is a structure. If the constant is a
4917 union, we override this by getting the field from the TREE_LIST element.
4918 But the constant could also be an array. Then FIELD is zero.
4920 There is always a maximum of one element in the chain LINK for unions
4921 (even if the initializer in a source program incorrectly contains
4922 more one). */
4924 local.field = NULL_TREE;
4925 if (TREE_CODE (local.type) == RECORD_TYPE)
4926 local.field = TYPE_FIELDS (local.type);
4928 for (cnt = 0;
4929 VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (exp), cnt, ce);
4930 cnt++, local.field = local.field ? DECL_CHAIN (local.field) : 0)
4932 local.val = ce->value;
4933 local.index = NULL_TREE;
4935 /* The element in a union constructor specifies the proper field
4936 or index. */
4937 if ((TREE_CODE (local.type) == RECORD_TYPE
4938 || TREE_CODE (local.type) == UNION_TYPE
4939 || TREE_CODE (local.type) == QUAL_UNION_TYPE)
4940 && ce->index != NULL_TREE)
4941 local.field = ce->index;
4943 else if (TREE_CODE (local.type) == ARRAY_TYPE)
4944 local.index = ce->index;
4946 #ifdef ASM_COMMENT_START
4947 if (local.field && flag_verbose_asm)
4948 fprintf (asm_out_file, "%s %s:\n",
4949 ASM_COMMENT_START,
4950 DECL_NAME (local.field)
4951 ? IDENTIFIER_POINTER (DECL_NAME (local.field))
4952 : "<anonymous>");
4953 #endif
4955 /* Eliminate the marker that makes a cast not be an lvalue. */
4956 if (local.val != NULL_TREE)
4957 STRIP_NOPS (local.val);
4959 /* Output the current element, using the appropriate helper ... */
4961 /* For an array slice not part of an outer bitfield. */
4962 if (!outer
4963 && local.index != NULL_TREE
4964 && TREE_CODE (local.index) == RANGE_EXPR)
4965 output_constructor_array_range (&local);
4967 /* For a field that is neither a true bitfield nor part of an outer one,
4968 known to be at least byte aligned and multiple-of-bytes long. */
4969 else if (!outer
4970 && (local.field == NULL_TREE
4971 || !CONSTRUCTOR_BITFIELD_P (local.field)))
4972 output_constructor_regular_field (&local);
4974 /* For a true bitfield or part of an outer one. */
4975 else
4976 output_constructor_bitfield (&local, outer);
4979 /* If we are not at toplevel, save the pending data for our caller.
4980 Otherwise output the pending data and padding zeros as needed. */
4981 if (outer)
4982 outer->byte = local.byte;
4983 else
4985 if (local.byte_buffer_in_use)
4987 assemble_integer (GEN_INT (local.byte), 1, BITS_PER_UNIT, 1);
4988 local.total_bytes++;
4991 if ((unsigned HOST_WIDE_INT)local.total_bytes < local.size)
4993 assemble_zeros (local.size - local.total_bytes);
4994 local.total_bytes = local.size;
4998 return local.total_bytes;
5001 /* Mark DECL as weak. */
5003 static void
5004 mark_weak (tree decl)
5006 DECL_WEAK (decl) = 1;
5008 if (DECL_RTL_SET_P (decl)
5009 && MEM_P (DECL_RTL (decl))
5010 && XEXP (DECL_RTL (decl), 0)
5011 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == SYMBOL_REF)
5012 SYMBOL_REF_WEAK (XEXP (DECL_RTL (decl), 0)) = 1;
5015 /* Merge weak status between NEWDECL and OLDDECL. */
5017 void
5018 merge_weak (tree newdecl, tree olddecl)
5020 if (DECL_WEAK (newdecl) == DECL_WEAK (olddecl))
5022 if (DECL_WEAK (newdecl) && SUPPORTS_WEAK)
5024 tree *pwd;
5025 /* We put the NEWDECL on the weak_decls list at some point
5026 and OLDDECL as well. Keep just OLDDECL on the list. */
5027 for (pwd = &weak_decls; *pwd; pwd = &TREE_CHAIN (*pwd))
5028 if (TREE_VALUE (*pwd) == newdecl)
5030 *pwd = TREE_CHAIN (*pwd);
5031 break;
5034 return;
5037 if (DECL_WEAK (newdecl))
5039 tree wd;
5041 /* NEWDECL is weak, but OLDDECL is not. */
5043 /* If we already output the OLDDECL, we're in trouble; we can't
5044 go back and make it weak. This error cannot be caught in
5045 declare_weak because the NEWDECL and OLDDECL was not yet
5046 been merged; therefore, TREE_ASM_WRITTEN was not set. */
5047 if (TREE_ASM_WRITTEN (olddecl))
5048 error ("weak declaration of %q+D must precede definition",
5049 newdecl);
5051 /* If we've already generated rtl referencing OLDDECL, we may
5052 have done so in a way that will not function properly with
5053 a weak symbol. */
5054 else if (TREE_USED (olddecl)
5055 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (olddecl)))
5056 warning (0, "weak declaration of %q+D after first use results "
5057 "in unspecified behavior", newdecl);
5059 if (SUPPORTS_WEAK)
5061 /* We put the NEWDECL on the weak_decls list at some point.
5062 Replace it with the OLDDECL. */
5063 for (wd = weak_decls; wd; wd = TREE_CHAIN (wd))
5064 if (TREE_VALUE (wd) == newdecl)
5066 TREE_VALUE (wd) = olddecl;
5067 break;
5069 /* We may not find the entry on the list. If NEWDECL is a
5070 weak alias, then we will have already called
5071 globalize_decl to remove the entry; in that case, we do
5072 not need to do anything. */
5075 /* Make the OLDDECL weak; it's OLDDECL that we'll be keeping. */
5076 mark_weak (olddecl);
5078 else
5079 /* OLDDECL was weak, but NEWDECL was not explicitly marked as
5080 weak. Just update NEWDECL to indicate that it's weak too. */
5081 mark_weak (newdecl);
5084 /* Declare DECL to be a weak symbol. */
5086 void
5087 declare_weak (tree decl)
5089 if (! TREE_PUBLIC (decl))
5090 error ("weak declaration of %q+D must be public", decl);
5091 else if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
5092 error ("weak declaration of %q+D must precede definition", decl);
5093 else if (!SUPPORTS_WEAK)
5094 warning (0, "weak declaration of %q+D not supported", decl);
5096 mark_weak (decl);
5097 if (!lookup_attribute ("weak", DECL_ATTRIBUTES (decl)))
5098 DECL_ATTRIBUTES (decl)
5099 = tree_cons (get_identifier ("weak"), NULL, DECL_ATTRIBUTES (decl));
5102 static void
5103 weak_finish_1 (tree decl)
5105 #if defined (ASM_WEAKEN_DECL) || defined (ASM_WEAKEN_LABEL)
5106 const char *const name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5107 #endif
5109 if (! TREE_USED (decl))
5110 return;
5112 #ifdef ASM_WEAKEN_DECL
5113 ASM_WEAKEN_DECL (asm_out_file, decl, name, NULL);
5114 #else
5115 #ifdef ASM_WEAKEN_LABEL
5116 ASM_WEAKEN_LABEL (asm_out_file, name);
5117 #else
5118 #ifdef ASM_OUTPUT_WEAK_ALIAS
5120 static bool warn_once = 0;
5121 if (! warn_once)
5123 warning (0, "only weak aliases are supported in this configuration");
5124 warn_once = 1;
5126 return;
5128 #endif
5129 #endif
5130 #endif
5133 /* This TREE_LIST contains weakref targets. */
5135 static GTY(()) tree weakref_targets;
5137 /* Forward declaration. */
5138 static tree find_decl_and_mark_needed (tree decl, tree target);
5140 /* Emit any pending weak declarations. */
5142 void
5143 weak_finish (void)
5145 tree t;
5147 for (t = weakref_targets; t; t = TREE_CHAIN (t))
5149 tree alias_decl = TREE_PURPOSE (t);
5150 tree target = ultimate_transparent_alias_target (&TREE_VALUE (t));
5152 if (! TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (alias_decl)))
5153 /* Remove alias_decl from the weak list, but leave entries for
5154 the target alone. */
5155 target = NULL_TREE;
5156 #ifndef ASM_OUTPUT_WEAKREF
5157 else if (! TREE_SYMBOL_REFERENCED (target))
5159 /* Use ASM_WEAKEN_LABEL only if ASM_WEAKEN_DECL is not
5160 defined, otherwise we and weak_finish_1 would use
5161 different macros. */
5162 # if defined ASM_WEAKEN_LABEL && ! defined ASM_WEAKEN_DECL
5163 ASM_WEAKEN_LABEL (asm_out_file, IDENTIFIER_POINTER (target));
5164 # else
5165 tree decl = find_decl_and_mark_needed (alias_decl, target);
5167 if (! decl)
5169 decl = build_decl (DECL_SOURCE_LOCATION (alias_decl),
5170 TREE_CODE (alias_decl), target,
5171 TREE_TYPE (alias_decl));
5173 DECL_EXTERNAL (decl) = 1;
5174 TREE_PUBLIC (decl) = 1;
5175 DECL_ARTIFICIAL (decl) = 1;
5176 TREE_NOTHROW (decl) = TREE_NOTHROW (alias_decl);
5177 TREE_USED (decl) = 1;
5180 weak_finish_1 (decl);
5181 # endif
5183 #endif
5186 tree *p;
5187 tree t2;
5189 /* Remove the alias and the target from the pending weak list
5190 so that we do not emit any .weak directives for the former,
5191 nor multiple .weak directives for the latter. */
5192 for (p = &weak_decls; (t2 = *p) ; )
5194 if (TREE_VALUE (t2) == alias_decl
5195 || target == DECL_ASSEMBLER_NAME (TREE_VALUE (t2)))
5196 *p = TREE_CHAIN (t2);
5197 else
5198 p = &TREE_CHAIN (t2);
5201 /* Remove other weakrefs to the same target, to speed things up. */
5202 for (p = &TREE_CHAIN (t); (t2 = *p) ; )
5204 if (target == ultimate_transparent_alias_target (&TREE_VALUE (t2)))
5205 *p = TREE_CHAIN (t2);
5206 else
5207 p = &TREE_CHAIN (t2);
5212 for (t = weak_decls; t; t = TREE_CHAIN (t))
5214 tree decl = TREE_VALUE (t);
5216 weak_finish_1 (decl);
5220 /* Emit the assembly bits to indicate that DECL is globally visible. */
5222 static void
5223 globalize_decl (tree decl)
5226 #if defined (ASM_WEAKEN_LABEL) || defined (ASM_WEAKEN_DECL)
5227 if (DECL_WEAK (decl))
5229 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
5230 tree *p, t;
5232 #ifdef ASM_WEAKEN_DECL
5233 ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
5234 #else
5235 ASM_WEAKEN_LABEL (asm_out_file, name);
5236 #endif
5238 /* Remove this function from the pending weak list so that
5239 we do not emit multiple .weak directives for it. */
5240 for (p = &weak_decls; (t = *p) ; )
5242 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
5243 *p = TREE_CHAIN (t);
5244 else
5245 p = &TREE_CHAIN (t);
5248 /* Remove weakrefs to the same target from the pending weakref
5249 list, for the same reason. */
5250 for (p = &weakref_targets; (t = *p) ; )
5252 if (DECL_ASSEMBLER_NAME (decl)
5253 == ultimate_transparent_alias_target (&TREE_VALUE (t)))
5254 *p = TREE_CHAIN (t);
5255 else
5256 p = &TREE_CHAIN (t);
5259 return;
5261 #endif
5263 targetm.asm_out.globalize_decl_name (asm_out_file, decl);
5266 VEC(alias_pair,gc) *alias_pairs;
5268 /* Given an assembly name, find the decl it is associated with. At the
5269 same time, mark it needed for cgraph. */
5271 static tree
5272 find_decl_and_mark_needed (tree decl, tree target)
5274 struct cgraph_node *fnode = NULL;
5275 struct varpool_node *vnode = NULL;
5277 if (TREE_CODE (decl) == FUNCTION_DECL)
5279 fnode = cgraph_node_for_asm (target);
5280 if (fnode == NULL)
5281 vnode = varpool_node_for_asm (target);
5283 else
5285 vnode = varpool_node_for_asm (target);
5286 if (vnode == NULL)
5287 fnode = cgraph_node_for_asm (target);
5290 if (fnode)
5292 cgraph_mark_needed_node (fnode);
5293 return fnode->decl;
5295 else if (vnode)
5297 varpool_mark_needed_node (vnode);
5298 vnode->force_output = 1;
5299 return vnode->decl;
5301 else
5302 return NULL_TREE;
5305 /* Output the assembler code for a define (equate) using ASM_OUTPUT_DEF
5306 or ASM_OUTPUT_DEF_FROM_DECLS. The function defines the symbol whose
5307 tree node is DECL to have the value of the tree node TARGET. */
5309 static void
5310 do_assemble_alias (tree decl, tree target)
5312 /* Emulated TLS had better not get this var. */
5313 gcc_assert(!(!targetm.have_tls
5314 && TREE_CODE (decl) == VAR_DECL
5315 && DECL_THREAD_LOCAL_P (decl)));
5317 if (TREE_ASM_WRITTEN (decl))
5318 return;
5320 /* We must force creation of DECL_RTL for debug info generation, even though
5321 we don't use it here. */
5322 make_decl_rtl (decl);
5324 TREE_ASM_WRITTEN (decl) = 1;
5325 TREE_ASM_WRITTEN (DECL_ASSEMBLER_NAME (decl)) = 1;
5327 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
5329 ultimate_transparent_alias_target (&target);
5331 if (!TREE_SYMBOL_REFERENCED (target))
5332 weakref_targets = tree_cons (decl, target, weakref_targets);
5334 #ifdef ASM_OUTPUT_WEAKREF
5335 ASM_OUTPUT_WEAKREF (asm_out_file, decl,
5336 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
5337 IDENTIFIER_POINTER (target));
5338 #else
5339 if (!SUPPORTS_WEAK)
5341 error_at (DECL_SOURCE_LOCATION (decl),
5342 "weakref is not supported in this configuration");
5343 return;
5345 #endif
5346 return;
5349 #ifdef ASM_OUTPUT_DEF
5350 /* Make name accessible from other files, if appropriate. */
5352 if (TREE_PUBLIC (decl))
5354 globalize_decl (decl);
5355 maybe_assemble_visibility (decl);
5358 # ifdef ASM_OUTPUT_DEF_FROM_DECLS
5359 ASM_OUTPUT_DEF_FROM_DECLS (asm_out_file, decl, target);
5360 # else
5361 ASM_OUTPUT_DEF (asm_out_file,
5362 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
5363 IDENTIFIER_POINTER (target));
5364 # endif
5365 #elif defined (ASM_OUTPUT_WEAK_ALIAS) || defined (ASM_WEAKEN_DECL)
5367 const char *name;
5368 tree *p, t;
5370 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5371 # ifdef ASM_WEAKEN_DECL
5372 ASM_WEAKEN_DECL (asm_out_file, decl, name, IDENTIFIER_POINTER (target));
5373 # else
5374 ASM_OUTPUT_WEAK_ALIAS (asm_out_file, name, IDENTIFIER_POINTER (target));
5375 # endif
5376 /* Remove this function from the pending weak list so that
5377 we do not emit multiple .weak directives for it. */
5378 for (p = &weak_decls; (t = *p) ; )
5379 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
5380 *p = TREE_CHAIN (t);
5381 else
5382 p = &TREE_CHAIN (t);
5384 /* Remove weakrefs to the same target from the pending weakref
5385 list, for the same reason. */
5386 for (p = &weakref_targets; (t = *p) ; )
5388 if (DECL_ASSEMBLER_NAME (decl)
5389 == ultimate_transparent_alias_target (&TREE_VALUE (t)))
5390 *p = TREE_CHAIN (t);
5391 else
5392 p = &TREE_CHAIN (t);
5395 #endif
5399 /* Remove the alias pairing for functions that are no longer in the call
5400 graph. */
5402 void
5403 remove_unreachable_alias_pairs (void)
5405 unsigned i;
5406 alias_pair *p;
5408 if (alias_pairs == NULL)
5409 return;
5411 for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); )
5413 if (!DECL_EXTERNAL (p->decl))
5415 struct cgraph_node *fnode = NULL;
5416 struct varpool_node *vnode = NULL;
5417 fnode = cgraph_node_for_asm (p->target);
5418 vnode = (fnode == NULL) ? varpool_node_for_asm (p->target) : NULL;
5419 if (fnode == NULL && vnode == NULL)
5421 VEC_unordered_remove (alias_pair, alias_pairs, i);
5422 continue;
5426 i++;
5431 /* First pass of completing pending aliases. Make sure that cgraph knows
5432 which symbols will be required. */
5434 void
5435 finish_aliases_1 (void)
5437 unsigned i;
5438 alias_pair *p;
5440 for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); i++)
5442 tree target_decl;
5444 target_decl = find_decl_and_mark_needed (p->decl, p->target);
5445 if (target_decl == NULL)
5447 if (! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
5448 error ("%q+D aliased to undefined symbol %qE",
5449 p->decl, p->target);
5451 else if (DECL_EXTERNAL (target_decl)
5452 /* We use local aliases for C++ thunks to force the tailcall
5453 to bind locally. Of course this is a hack - to keep it
5454 working do the following (which is not strictly correct). */
5455 && (! TREE_CODE (target_decl) == FUNCTION_DECL
5456 || ! DECL_VIRTUAL_P (target_decl))
5457 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
5458 error ("%q+D aliased to external symbol %qE",
5459 p->decl, p->target);
5463 /* Second pass of completing pending aliases. Emit the actual assembly.
5464 This happens at the end of compilation and thus it is assured that the
5465 target symbol has been emitted. */
5467 void
5468 finish_aliases_2 (void)
5470 unsigned i;
5471 alias_pair *p;
5473 for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); i++)
5474 do_assemble_alias (p->decl, p->target);
5476 VEC_truncate (alias_pair, alias_pairs, 0);
5479 /* Emit an assembler directive to make the symbol for DECL an alias to
5480 the symbol for TARGET. */
5482 void
5483 assemble_alias (tree decl, tree target)
5485 tree target_decl;
5486 bool is_weakref = false;
5488 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
5490 tree alias = DECL_ASSEMBLER_NAME (decl);
5492 is_weakref = true;
5494 ultimate_transparent_alias_target (&target);
5496 if (alias == target)
5497 error ("weakref %q+D ultimately targets itself", decl);
5498 else
5500 #ifndef ASM_OUTPUT_WEAKREF
5501 IDENTIFIER_TRANSPARENT_ALIAS (alias) = 1;
5502 TREE_CHAIN (alias) = target;
5503 #endif
5505 if (TREE_PUBLIC (decl))
5506 error ("weakref %q+D must have static linkage", decl);
5508 else
5510 #if !defined (ASM_OUTPUT_DEF)
5511 # if !defined(ASM_OUTPUT_WEAK_ALIAS) && !defined (ASM_WEAKEN_DECL)
5512 error_at (DECL_SOURCE_LOCATION (decl),
5513 "alias definitions not supported in this configuration");
5514 return;
5515 # else
5516 if (!DECL_WEAK (decl))
5518 error_at (DECL_SOURCE_LOCATION (decl),
5519 "only weak aliases are supported in this configuration");
5520 return;
5522 # endif
5523 #endif
5525 TREE_USED (decl) = 1;
5527 /* A quirk of the initial implementation of aliases required that the user
5528 add "extern" to all of them. Which is silly, but now historical. Do
5529 note that the symbol is in fact locally defined. */
5530 if (! is_weakref)
5531 DECL_EXTERNAL (decl) = 0;
5533 /* Allow aliases to aliases. */
5534 if (TREE_CODE (decl) == FUNCTION_DECL)
5535 cgraph_node (decl)->alias = true;
5536 else
5537 varpool_node (decl)->alias = true;
5539 /* If the target has already been emitted, we don't have to queue the
5540 alias. This saves a tad of memory. */
5541 if (cgraph_global_info_ready)
5542 target_decl = find_decl_and_mark_needed (decl, target);
5543 else
5544 target_decl= NULL;
5545 if (target_decl && TREE_ASM_WRITTEN (target_decl))
5546 do_assemble_alias (decl, target);
5547 else
5549 alias_pair *p = VEC_safe_push (alias_pair, gc, alias_pairs, NULL);
5550 p->decl = decl;
5551 p->target = target;
5555 /* Emit an assembler directive to set symbol for DECL visibility to
5556 the visibility type VIS, which must not be VISIBILITY_DEFAULT. */
5558 void
5559 default_assemble_visibility (tree decl ATTRIBUTE_UNUSED,
5560 int vis ATTRIBUTE_UNUSED)
5562 #ifdef HAVE_GAS_HIDDEN
5563 static const char * const visibility_types[] = {
5564 NULL, "protected", "hidden", "internal"
5567 const char *name, *type;
5569 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5570 type = visibility_types[vis];
5572 fprintf (asm_out_file, "\t.%s\t", type);
5573 assemble_name (asm_out_file, name);
5574 fprintf (asm_out_file, "\n");
5575 #else
5576 warning (OPT_Wattributes, "visibility attribute not supported "
5577 "in this configuration; ignored");
5578 #endif
5581 /* A helper function to call assemble_visibility when needed for a decl. */
5584 maybe_assemble_visibility (tree decl)
5586 enum symbol_visibility vis = DECL_VISIBILITY (decl);
5588 if (vis != VISIBILITY_DEFAULT)
5590 targetm.asm_out.assemble_visibility (decl, vis);
5591 return 1;
5593 else
5594 return 0;
5597 /* Returns 1 if the target configuration supports defining public symbols
5598 so that one of them will be chosen at link time instead of generating a
5599 multiply-defined symbol error, whether through the use of weak symbols or
5600 a target-specific mechanism for having duplicates discarded. */
5603 supports_one_only (void)
5605 if (SUPPORTS_ONE_ONLY)
5606 return 1;
5607 return SUPPORTS_WEAK;
5610 /* Set up DECL as a public symbol that can be defined in multiple
5611 translation units without generating a linker error. */
5613 void
5614 make_decl_one_only (tree decl, tree comdat_group)
5616 gcc_assert (TREE_CODE (decl) == VAR_DECL
5617 || TREE_CODE (decl) == FUNCTION_DECL);
5619 TREE_PUBLIC (decl) = 1;
5621 if (SUPPORTS_ONE_ONLY)
5623 #ifdef MAKE_DECL_ONE_ONLY
5624 MAKE_DECL_ONE_ONLY (decl);
5625 #endif
5626 DECL_COMDAT_GROUP (decl) = comdat_group;
5628 else if (TREE_CODE (decl) == VAR_DECL
5629 && (DECL_INITIAL (decl) == 0 || DECL_INITIAL (decl) == error_mark_node))
5630 DECL_COMMON (decl) = 1;
5631 else
5633 gcc_assert (SUPPORTS_WEAK);
5634 DECL_WEAK (decl) = 1;
5638 void
5639 init_varasm_once (void)
5641 section_htab = htab_create_ggc (31, section_entry_hash,
5642 section_entry_eq, NULL);
5643 object_block_htab = htab_create_ggc (31, object_block_entry_hash,
5644 object_block_entry_eq, NULL);
5645 const_desc_htab = htab_create_ggc (1009, const_desc_hash,
5646 const_desc_eq, NULL);
5648 const_alias_set = new_alias_set ();
5649 shared_constant_pool = create_constant_pool ();
5651 #ifdef TEXT_SECTION_ASM_OP
5652 text_section = get_unnamed_section (SECTION_CODE, output_section_asm_op,
5653 TEXT_SECTION_ASM_OP);
5654 #endif
5656 #ifdef DATA_SECTION_ASM_OP
5657 data_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
5658 DATA_SECTION_ASM_OP);
5659 #endif
5661 #ifdef SDATA_SECTION_ASM_OP
5662 sdata_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
5663 SDATA_SECTION_ASM_OP);
5664 #endif
5666 #ifdef READONLY_DATA_SECTION_ASM_OP
5667 readonly_data_section = get_unnamed_section (0, output_section_asm_op,
5668 READONLY_DATA_SECTION_ASM_OP);
5669 #endif
5671 #ifdef CTORS_SECTION_ASM_OP
5672 ctors_section = get_unnamed_section (0, output_section_asm_op,
5673 CTORS_SECTION_ASM_OP);
5674 #endif
5676 #ifdef DTORS_SECTION_ASM_OP
5677 dtors_section = get_unnamed_section (0, output_section_asm_op,
5678 DTORS_SECTION_ASM_OP);
5679 #endif
5681 #ifdef BSS_SECTION_ASM_OP
5682 bss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
5683 output_section_asm_op,
5684 BSS_SECTION_ASM_OP);
5685 #endif
5687 #ifdef SBSS_SECTION_ASM_OP
5688 sbss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
5689 output_section_asm_op,
5690 SBSS_SECTION_ASM_OP);
5691 #endif
5693 tls_comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
5694 | SECTION_COMMON, emit_tls_common);
5695 lcomm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
5696 | SECTION_COMMON, emit_local);
5697 comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
5698 | SECTION_COMMON, emit_common);
5700 #if defined ASM_OUTPUT_ALIGNED_BSS || defined ASM_OUTPUT_BSS
5701 bss_noswitch_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS,
5702 emit_bss);
5703 #endif
5705 targetm.asm_out.init_sections ();
5707 if (readonly_data_section == NULL)
5708 readonly_data_section = text_section;
5711 enum tls_model
5712 decl_default_tls_model (const_tree decl)
5714 enum tls_model kind;
5715 bool is_local;
5717 is_local = targetm.binds_local_p (decl);
5718 if (!flag_shlib)
5720 if (is_local)
5721 kind = TLS_MODEL_LOCAL_EXEC;
5722 else
5723 kind = TLS_MODEL_INITIAL_EXEC;
5726 /* Local dynamic is inefficient when we're not combining the
5727 parts of the address. */
5728 else if (optimize && is_local)
5729 kind = TLS_MODEL_LOCAL_DYNAMIC;
5730 else
5731 kind = TLS_MODEL_GLOBAL_DYNAMIC;
5732 if (kind < flag_tls_default)
5733 kind = flag_tls_default;
5735 return kind;
5738 /* Select a set of attributes for section NAME based on the properties
5739 of DECL and whether or not RELOC indicates that DECL's initializer
5740 might contain runtime relocations.
5742 We make the section read-only and executable for a function decl,
5743 read-only for a const data decl, and writable for a non-const data decl. */
5745 unsigned int
5746 default_section_type_flags (tree decl, const char *name, int reloc)
5748 unsigned int flags;
5750 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
5751 flags = SECTION_CODE;
5752 else if (decl && decl_readonly_section (decl, reloc))
5753 flags = 0;
5754 else if (current_function_decl
5755 && cfun
5756 && crtl->subsections.unlikely_text_section_name
5757 && strcmp (name, crtl->subsections.unlikely_text_section_name) == 0)
5758 flags = SECTION_CODE;
5759 else if (!decl
5760 && (!current_function_decl || !cfun)
5761 && strcmp (name, UNLIKELY_EXECUTED_TEXT_SECTION_NAME) == 0)
5762 flags = SECTION_CODE;
5763 else
5764 flags = SECTION_WRITE;
5766 if (decl && DECL_ONE_ONLY (decl))
5767 flags |= SECTION_LINKONCE;
5769 if (decl && TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
5770 flags |= SECTION_TLS | SECTION_WRITE;
5772 if (strcmp (name, ".bss") == 0
5773 || strncmp (name, ".bss.", 5) == 0
5774 || strncmp (name, ".gnu.linkonce.b.", 16) == 0
5775 || strcmp (name, ".sbss") == 0
5776 || strncmp (name, ".sbss.", 6) == 0
5777 || strncmp (name, ".gnu.linkonce.sb.", 17) == 0)
5778 flags |= SECTION_BSS;
5780 if (strcmp (name, ".tdata") == 0
5781 || strncmp (name, ".tdata.", 7) == 0
5782 || strncmp (name, ".gnu.linkonce.td.", 17) == 0)
5783 flags |= SECTION_TLS;
5785 if (strcmp (name, ".tbss") == 0
5786 || strncmp (name, ".tbss.", 6) == 0
5787 || strncmp (name, ".gnu.linkonce.tb.", 17) == 0)
5788 flags |= SECTION_TLS | SECTION_BSS;
5790 /* These three sections have special ELF types. They are neither
5791 SHT_PROGBITS nor SHT_NOBITS, so when changing sections we don't
5792 want to print a section type (@progbits or @nobits). If someone
5793 is silly enough to emit code or TLS variables to one of these
5794 sections, then don't handle them specially. */
5795 if (!(flags & (SECTION_CODE | SECTION_BSS | SECTION_TLS))
5796 && (strcmp (name, ".init_array") == 0
5797 || strcmp (name, ".fini_array") == 0
5798 || strcmp (name, ".preinit_array") == 0))
5799 flags |= SECTION_NOTYPE;
5801 return flags;
5804 /* Return true if the target supports some form of global BSS,
5805 either through bss_noswitch_section, or by selecting a BSS
5806 section in TARGET_ASM_SELECT_SECTION. */
5808 bool
5809 have_global_bss_p (void)
5811 return bss_noswitch_section || targetm.have_switchable_bss_sections;
5814 /* Output assembly to switch to section NAME with attribute FLAGS.
5815 Four variants for common object file formats. */
5817 void
5818 default_no_named_section (const char *name ATTRIBUTE_UNUSED,
5819 unsigned int flags ATTRIBUTE_UNUSED,
5820 tree decl ATTRIBUTE_UNUSED)
5822 /* Some object formats don't support named sections at all. The
5823 front-end should already have flagged this as an error. */
5824 gcc_unreachable ();
5827 #ifndef TLS_SECTION_ASM_FLAG
5828 #define TLS_SECTION_ASM_FLAG 'T'
5829 #endif
5831 void
5832 default_elf_asm_named_section (const char *name, unsigned int flags,
5833 tree decl ATTRIBUTE_UNUSED)
5835 char flagchars[10], *f = flagchars;
5837 /* If we have already declared this section, we can use an
5838 abbreviated form to switch back to it -- unless this section is
5839 part of a COMDAT groups, in which case GAS requires the full
5840 declaration every time. */
5841 if (!(HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
5842 && (flags & SECTION_DECLARED))
5844 fprintf (asm_out_file, "\t.section\t%s\n", name);
5845 return;
5848 if (!(flags & SECTION_DEBUG))
5849 *f++ = 'a';
5850 if (flags & SECTION_WRITE)
5851 *f++ = 'w';
5852 if (flags & SECTION_CODE)
5853 *f++ = 'x';
5854 if (flags & SECTION_SMALL)
5855 *f++ = 's';
5856 if (flags & SECTION_MERGE)
5857 *f++ = 'M';
5858 if (flags & SECTION_STRINGS)
5859 *f++ = 'S';
5860 if (flags & SECTION_TLS)
5861 *f++ = TLS_SECTION_ASM_FLAG;
5862 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
5863 *f++ = 'G';
5864 *f = '\0';
5866 fprintf (asm_out_file, "\t.section\t%s,\"%s\"", name, flagchars);
5868 if (!(flags & SECTION_NOTYPE))
5870 const char *type;
5871 const char *format;
5873 if (flags & SECTION_BSS)
5874 type = "nobits";
5875 else
5876 type = "progbits";
5878 format = ",@%s";
5879 #ifdef ASM_COMMENT_START
5880 /* On platforms that use "@" as the assembly comment character,
5881 use "%" instead. */
5882 if (strcmp (ASM_COMMENT_START, "@") == 0)
5883 format = ",%%%s";
5884 #endif
5885 fprintf (asm_out_file, format, type);
5887 if (flags & SECTION_ENTSIZE)
5888 fprintf (asm_out_file, ",%d", flags & SECTION_ENTSIZE);
5889 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
5891 if (TREE_CODE (decl) == IDENTIFIER_NODE)
5892 fprintf (asm_out_file, ",%s,comdat", IDENTIFIER_POINTER (decl));
5893 else
5894 fprintf (asm_out_file, ",%s,comdat",
5895 IDENTIFIER_POINTER (DECL_COMDAT_GROUP (decl)));
5899 putc ('\n', asm_out_file);
5902 void
5903 default_coff_asm_named_section (const char *name, unsigned int flags,
5904 tree decl ATTRIBUTE_UNUSED)
5906 char flagchars[8], *f = flagchars;
5908 if (flags & SECTION_WRITE)
5909 *f++ = 'w';
5910 if (flags & SECTION_CODE)
5911 *f++ = 'x';
5912 *f = '\0';
5914 fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
5917 void
5918 default_pe_asm_named_section (const char *name, unsigned int flags,
5919 tree decl)
5921 default_coff_asm_named_section (name, flags, decl);
5923 if (flags & SECTION_LINKONCE)
5925 /* Functions may have been compiled at various levels of
5926 optimization so we can't use `same_size' here.
5927 Instead, have the linker pick one. */
5928 fprintf (asm_out_file, "\t.linkonce %s\n",
5929 (flags & SECTION_CODE ? "discard" : "same_size"));
5933 /* The lame default section selector. */
5935 section *
5936 default_select_section (tree decl, int reloc,
5937 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
5939 if (DECL_P (decl))
5941 if (decl_readonly_section (decl, reloc))
5942 return readonly_data_section;
5944 else if (TREE_CODE (decl) == CONSTRUCTOR)
5946 if (! ((flag_pic && reloc)
5947 || !TREE_READONLY (decl)
5948 || TREE_SIDE_EFFECTS (decl)
5949 || !TREE_CONSTANT (decl)))
5950 return readonly_data_section;
5952 else if (TREE_CODE (decl) == STRING_CST)
5953 return readonly_data_section;
5954 else if (! (flag_pic && reloc))
5955 return readonly_data_section;
5957 return data_section;
5960 enum section_category
5961 categorize_decl_for_section (const_tree decl, int reloc)
5963 enum section_category ret;
5965 if (TREE_CODE (decl) == FUNCTION_DECL)
5966 return SECCAT_TEXT;
5967 else if (TREE_CODE (decl) == STRING_CST)
5969 if (flag_mudflap) /* or !flag_merge_constants */
5970 return SECCAT_RODATA;
5971 else
5972 return SECCAT_RODATA_MERGE_STR;
5974 else if (TREE_CODE (decl) == VAR_DECL)
5976 if (bss_initializer_p (decl))
5977 ret = SECCAT_BSS;
5978 else if (! TREE_READONLY (decl)
5979 || TREE_SIDE_EFFECTS (decl)
5980 || ! TREE_CONSTANT (DECL_INITIAL (decl)))
5982 /* Here the reloc_rw_mask is not testing whether the section should
5983 be read-only or not, but whether the dynamic link will have to
5984 do something. If so, we wish to segregate the data in order to
5985 minimize cache misses inside the dynamic linker. */
5986 if (reloc & targetm.asm_out.reloc_rw_mask ())
5987 ret = reloc == 1 ? SECCAT_DATA_REL_LOCAL : SECCAT_DATA_REL;
5988 else
5989 ret = SECCAT_DATA;
5991 else if (reloc & targetm.asm_out.reloc_rw_mask ())
5992 ret = reloc == 1 ? SECCAT_DATA_REL_RO_LOCAL : SECCAT_DATA_REL_RO;
5993 else if (reloc || flag_merge_constants < 2)
5994 /* C and C++ don't allow different variables to share the same
5995 location. -fmerge-all-constants allows even that (at the
5996 expense of not conforming). */
5997 ret = SECCAT_RODATA;
5998 else if (TREE_CODE (DECL_INITIAL (decl)) == STRING_CST)
5999 ret = SECCAT_RODATA_MERGE_STR_INIT;
6000 else
6001 ret = SECCAT_RODATA_MERGE_CONST;
6003 else if (TREE_CODE (decl) == CONSTRUCTOR)
6005 if ((reloc & targetm.asm_out.reloc_rw_mask ())
6006 || TREE_SIDE_EFFECTS (decl)
6007 || ! TREE_CONSTANT (decl))
6008 ret = SECCAT_DATA;
6009 else
6010 ret = SECCAT_RODATA;
6012 else
6013 ret = SECCAT_RODATA;
6015 /* There are no read-only thread-local sections. */
6016 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
6018 /* Note that this would be *just* SECCAT_BSS, except that there's
6019 no concept of a read-only thread-local-data section. */
6020 if (ret == SECCAT_BSS
6021 || (flag_zero_initialized_in_bss
6022 && initializer_zerop (DECL_INITIAL (decl))))
6023 ret = SECCAT_TBSS;
6024 else
6025 ret = SECCAT_TDATA;
6028 /* If the target uses small data sections, select it. */
6029 else if (targetm.in_small_data_p (decl))
6031 if (ret == SECCAT_BSS)
6032 ret = SECCAT_SBSS;
6033 else if (targetm.have_srodata_section && ret == SECCAT_RODATA)
6034 ret = SECCAT_SRODATA;
6035 else
6036 ret = SECCAT_SDATA;
6039 return ret;
6042 bool
6043 decl_readonly_section (const_tree decl, int reloc)
6045 switch (categorize_decl_for_section (decl, reloc))
6047 case SECCAT_RODATA:
6048 case SECCAT_RODATA_MERGE_STR:
6049 case SECCAT_RODATA_MERGE_STR_INIT:
6050 case SECCAT_RODATA_MERGE_CONST:
6051 case SECCAT_SRODATA:
6052 return true;
6053 break;
6054 default:
6055 return false;
6056 break;
6060 /* Select a section based on the above categorization. */
6062 section *
6063 default_elf_select_section (tree decl, int reloc,
6064 unsigned HOST_WIDE_INT align)
6066 const char *sname;
6067 switch (categorize_decl_for_section (decl, reloc))
6069 case SECCAT_TEXT:
6070 /* We're not supposed to be called on FUNCTION_DECLs. */
6071 gcc_unreachable ();
6072 case SECCAT_RODATA:
6073 return readonly_data_section;
6074 case SECCAT_RODATA_MERGE_STR:
6075 return mergeable_string_section (decl, align, 0);
6076 case SECCAT_RODATA_MERGE_STR_INIT:
6077 return mergeable_string_section (DECL_INITIAL (decl), align, 0);
6078 case SECCAT_RODATA_MERGE_CONST:
6079 return mergeable_constant_section (DECL_MODE (decl), align, 0);
6080 case SECCAT_SRODATA:
6081 sname = ".sdata2";
6082 break;
6083 case SECCAT_DATA:
6084 return data_section;
6085 case SECCAT_DATA_REL:
6086 sname = ".data.rel";
6087 break;
6088 case SECCAT_DATA_REL_LOCAL:
6089 sname = ".data.rel.local";
6090 break;
6091 case SECCAT_DATA_REL_RO:
6092 sname = ".data.rel.ro";
6093 break;
6094 case SECCAT_DATA_REL_RO_LOCAL:
6095 sname = ".data.rel.ro.local";
6096 break;
6097 case SECCAT_SDATA:
6098 sname = ".sdata";
6099 break;
6100 case SECCAT_TDATA:
6101 sname = ".tdata";
6102 break;
6103 case SECCAT_BSS:
6104 if (bss_section)
6105 return bss_section;
6106 sname = ".bss";
6107 break;
6108 case SECCAT_SBSS:
6109 sname = ".sbss";
6110 break;
6111 case SECCAT_TBSS:
6112 sname = ".tbss";
6113 break;
6114 default:
6115 gcc_unreachable ();
6118 if (!DECL_P (decl))
6119 decl = NULL_TREE;
6120 return get_named_section (decl, sname, reloc);
6123 /* Construct a unique section name based on the decl name and the
6124 categorization performed above. */
6126 void
6127 default_unique_section (tree decl, int reloc)
6129 /* We only need to use .gnu.linkonce if we don't have COMDAT groups. */
6130 bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
6131 const char *prefix, *name, *linkonce;
6132 char *string;
6134 switch (categorize_decl_for_section (decl, reloc))
6136 case SECCAT_TEXT:
6137 prefix = one_only ? ".t" : ".text";
6138 break;
6139 case SECCAT_RODATA:
6140 case SECCAT_RODATA_MERGE_STR:
6141 case SECCAT_RODATA_MERGE_STR_INIT:
6142 case SECCAT_RODATA_MERGE_CONST:
6143 prefix = one_only ? ".r" : ".rodata";
6144 break;
6145 case SECCAT_SRODATA:
6146 prefix = one_only ? ".s2" : ".sdata2";
6147 break;
6148 case SECCAT_DATA:
6149 prefix = one_only ? ".d" : ".data";
6150 break;
6151 case SECCAT_DATA_REL:
6152 prefix = one_only ? ".d.rel" : ".data.rel";
6153 break;
6154 case SECCAT_DATA_REL_LOCAL:
6155 prefix = one_only ? ".d.rel.local" : ".data.rel.local";
6156 break;
6157 case SECCAT_DATA_REL_RO:
6158 prefix = one_only ? ".d.rel.ro" : ".data.rel.ro";
6159 break;
6160 case SECCAT_DATA_REL_RO_LOCAL:
6161 prefix = one_only ? ".d.rel.ro.local" : ".data.rel.ro.local";
6162 break;
6163 case SECCAT_SDATA:
6164 prefix = one_only ? ".s" : ".sdata";
6165 break;
6166 case SECCAT_BSS:
6167 prefix = one_only ? ".b" : ".bss";
6168 break;
6169 case SECCAT_SBSS:
6170 prefix = one_only ? ".sb" : ".sbss";
6171 break;
6172 case SECCAT_TDATA:
6173 prefix = one_only ? ".td" : ".tdata";
6174 break;
6175 case SECCAT_TBSS:
6176 prefix = one_only ? ".tb" : ".tbss";
6177 break;
6178 default:
6179 gcc_unreachable ();
6182 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
6183 name = targetm.strip_name_encoding (name);
6185 /* If we're using one_only, then there needs to be a .gnu.linkonce
6186 prefix to the section name. */
6187 linkonce = one_only ? ".gnu.linkonce" : "";
6189 string = ACONCAT ((linkonce, prefix, ".", name, NULL));
6191 DECL_SECTION_NAME (decl) = build_string (strlen (string), string);
6194 /* Like compute_reloc_for_constant, except for an RTX. The return value
6195 is a mask for which bit 1 indicates a global relocation, and bit 0
6196 indicates a local relocation. */
6198 static int
6199 compute_reloc_for_rtx_1 (rtx *xp, void *data)
6201 int *preloc = (int *) data;
6202 rtx x = *xp;
6204 switch (GET_CODE (x))
6206 case SYMBOL_REF:
6207 *preloc |= SYMBOL_REF_LOCAL_P (x) ? 1 : 2;
6208 break;
6209 case LABEL_REF:
6210 *preloc |= 1;
6211 break;
6212 default:
6213 break;
6216 return 0;
6219 static int
6220 compute_reloc_for_rtx (rtx x)
6222 int reloc;
6224 switch (GET_CODE (x))
6226 case CONST:
6227 case SYMBOL_REF:
6228 case LABEL_REF:
6229 reloc = 0;
6230 for_each_rtx (&x, compute_reloc_for_rtx_1, &reloc);
6231 return reloc;
6233 default:
6234 return 0;
6238 section *
6239 default_select_rtx_section (enum machine_mode mode ATTRIBUTE_UNUSED,
6240 rtx x,
6241 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
6243 if (compute_reloc_for_rtx (x) & targetm.asm_out.reloc_rw_mask ())
6244 return data_section;
6245 else
6246 return readonly_data_section;
6249 section *
6250 default_elf_select_rtx_section (enum machine_mode mode, rtx x,
6251 unsigned HOST_WIDE_INT align)
6253 int reloc = compute_reloc_for_rtx (x);
6255 /* ??? Handle small data here somehow. */
6257 if (reloc & targetm.asm_out.reloc_rw_mask ())
6259 if (reloc == 1)
6260 return get_named_section (NULL, ".data.rel.ro.local", 1);
6261 else
6262 return get_named_section (NULL, ".data.rel.ro", 3);
6265 return mergeable_constant_section (mode, align, 0);
6268 /* Set the generally applicable flags on the SYMBOL_REF for EXP. */
6270 void
6271 default_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
6273 rtx symbol;
6274 int flags;
6276 /* Careful not to prod global register variables. */
6277 if (!MEM_P (rtl))
6278 return;
6279 symbol = XEXP (rtl, 0);
6280 if (GET_CODE (symbol) != SYMBOL_REF)
6281 return;
6283 flags = SYMBOL_REF_FLAGS (symbol) & SYMBOL_FLAG_HAS_BLOCK_INFO;
6284 if (TREE_CODE (decl) == FUNCTION_DECL)
6285 flags |= SYMBOL_FLAG_FUNCTION;
6286 if (targetm.binds_local_p (decl))
6287 flags |= SYMBOL_FLAG_LOCAL;
6288 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
6289 flags |= DECL_TLS_MODEL (decl) << SYMBOL_FLAG_TLS_SHIFT;
6290 else if (targetm.in_small_data_p (decl))
6291 flags |= SYMBOL_FLAG_SMALL;
6292 /* ??? Why is DECL_EXTERNAL ever set for non-PUBLIC names? Without
6293 being PUBLIC, the thing *must* be defined in this translation unit.
6294 Prevent this buglet from being propagated into rtl code as well. */
6295 if (DECL_P (decl) && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl))
6296 flags |= SYMBOL_FLAG_EXTERNAL;
6298 SYMBOL_REF_FLAGS (symbol) = flags;
6301 /* By default, we do nothing for encode_section_info, so we need not
6302 do anything but discard the '*' marker. */
6304 const char *
6305 default_strip_name_encoding (const char *str)
6307 return str + (*str == '*');
6310 #ifdef ASM_OUTPUT_DEF
6311 /* The default implementation of TARGET_ASM_OUTPUT_ANCHOR. Define the
6312 anchor relative to ".", the current section position. */
6314 void
6315 default_asm_output_anchor (rtx symbol)
6317 char buffer[100];
6319 sprintf (buffer, "*. + " HOST_WIDE_INT_PRINT_DEC,
6320 SYMBOL_REF_BLOCK_OFFSET (symbol));
6321 ASM_OUTPUT_DEF (asm_out_file, XSTR (symbol, 0), buffer);
6323 #endif
6325 /* The default implementation of TARGET_USE_ANCHORS_FOR_SYMBOL_P. */
6327 bool
6328 default_use_anchors_for_symbol_p (const_rtx symbol)
6330 section *sect;
6331 tree decl;
6333 /* Don't use anchors for mergeable sections. The linker might move
6334 the objects around. */
6335 sect = SYMBOL_REF_BLOCK (symbol)->sect;
6336 if (sect->common.flags & SECTION_MERGE)
6337 return false;
6339 /* Don't use anchors for small data sections. The small data register
6340 acts as an anchor for such sections. */
6341 if (sect->common.flags & SECTION_SMALL)
6342 return false;
6344 decl = SYMBOL_REF_DECL (symbol);
6345 if (decl && DECL_P (decl))
6347 /* Don't use section anchors for decls that might be defined by
6348 other modules. */
6349 if (!targetm.binds_local_p (decl))
6350 return false;
6352 /* Don't use section anchors for decls that will be placed in a
6353 small data section. */
6354 /* ??? Ideally, this check would be redundant with the SECTION_SMALL
6355 one above. The problem is that we only use SECTION_SMALL for
6356 sections that should be marked as small in the section directive. */
6357 if (targetm.in_small_data_p (decl))
6358 return false;
6360 return true;
6363 /* Assume ELF-ish defaults, since that's pretty much the most liberal
6364 wrt cross-module name binding. */
6366 bool
6367 default_binds_local_p (const_tree exp)
6369 return default_binds_local_p_1 (exp, flag_shlib);
6372 bool
6373 default_binds_local_p_1 (const_tree exp, int shlib)
6375 bool local_p;
6377 /* A non-decl is an entry in the constant pool. */
6378 if (!DECL_P (exp))
6379 local_p = true;
6380 /* Weakrefs may not bind locally, even though the weakref itself is
6381 always static and therefore local. */
6382 else if (lookup_attribute ("weakref", DECL_ATTRIBUTES (exp)))
6383 local_p = false;
6384 /* Static variables are always local. */
6385 else if (! TREE_PUBLIC (exp))
6386 local_p = true;
6387 /* A variable is local if the user has said explicitly that it will
6388 be. */
6389 else if (DECL_VISIBILITY_SPECIFIED (exp)
6390 && DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
6391 local_p = true;
6392 /* Variables defined outside this object might not be local. */
6393 else if (DECL_EXTERNAL (exp))
6394 local_p = false;
6395 /* If defined in this object and visibility is not default, must be
6396 local. */
6397 else if (DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
6398 local_p = true;
6399 /* Default visibility weak data can be overridden by a strong symbol
6400 in another module and so are not local. */
6401 else if (DECL_WEAK (exp))
6402 local_p = false;
6403 /* If PIC, then assume that any global name can be overridden by
6404 symbols resolved from other modules, unless we are compiling with
6405 -fwhole-program, which assumes that names are local. */
6406 else if (shlib)
6407 local_p = flag_whole_program;
6408 /* Uninitialized COMMON variable may be unified with symbols
6409 resolved from other modules. */
6410 else if (DECL_COMMON (exp)
6411 && (DECL_INITIAL (exp) == NULL
6412 || DECL_INITIAL (exp) == error_mark_node))
6413 local_p = false;
6414 /* Otherwise we're left with initialized (or non-common) global data
6415 which is of necessity defined locally. */
6416 else
6417 local_p = true;
6419 return local_p;
6422 /* Default function to output code that will globalize a label. A
6423 target must define GLOBAL_ASM_OP or provide its own function to
6424 globalize a label. */
6425 #ifdef GLOBAL_ASM_OP
6426 void
6427 default_globalize_label (FILE * stream, const char *name)
6429 fputs (GLOBAL_ASM_OP, stream);
6430 assemble_name (stream, name);
6431 putc ('\n', stream);
6433 #endif /* GLOBAL_ASM_OP */
6435 /* Default function to output code that will globalize a declaration. */
6436 void
6437 default_globalize_decl_name (FILE * stream, tree decl)
6439 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
6440 targetm.asm_out.globalize_label (stream, name);
6443 /* Default function to output a label for unwind information. The
6444 default is to do nothing. A target that needs nonlocal labels for
6445 unwind information must provide its own function to do this. */
6446 void
6447 default_emit_unwind_label (FILE * stream ATTRIBUTE_UNUSED,
6448 tree decl ATTRIBUTE_UNUSED,
6449 int for_eh ATTRIBUTE_UNUSED,
6450 int empty ATTRIBUTE_UNUSED)
6454 /* Default function to output a label to divide up the exception table.
6455 The default is to do nothing. A target that needs/wants to divide
6456 up the table must provide it's own function to do this. */
6457 void
6458 default_emit_except_table_label (FILE * stream ATTRIBUTE_UNUSED)
6462 /* This is how to output an internal numbered label where PREFIX is
6463 the class of label and LABELNO is the number within the class. */
6465 void
6466 default_internal_label (FILE *stream, const char *prefix,
6467 unsigned long labelno)
6469 char *const buf = (char *) alloca (40 + strlen (prefix));
6470 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
6471 ASM_OUTPUT_INTERNAL_LABEL (stream, buf);
6475 /* The default implementation of ASM_DECLARE_CONSTANT_NAME. */
6477 void
6478 default_asm_declare_constant_name (FILE *file, const char *name,
6479 const_tree exp ATTRIBUTE_UNUSED,
6480 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
6482 assemble_label (file, name);
6485 /* This is the default behavior at the beginning of a file. It's
6486 controlled by two other target-hook toggles. */
6487 void
6488 default_file_start (void)
6490 if (targetm.asm_file_start_app_off
6491 && !(flag_verbose_asm || flag_debug_asm || flag_dump_rtl_in_asm))
6492 fputs (ASM_APP_OFF, asm_out_file);
6494 if (targetm.asm_file_start_file_directive)
6495 output_file_directive (asm_out_file, main_input_filename);
6498 /* This is a generic routine suitable for use as TARGET_ASM_FILE_END
6499 which emits a special section directive used to indicate whether or
6500 not this object file needs an executable stack. This is primarily
6501 a GNU extension to ELF but could be used on other targets. */
6503 int trampolines_created;
6505 void
6506 file_end_indicate_exec_stack (void)
6508 unsigned int flags = SECTION_DEBUG;
6509 if (trampolines_created)
6510 flags |= SECTION_CODE;
6512 switch_to_section (get_section (".note.GNU-stack", flags, NULL));
6515 /* Output DIRECTIVE (a C string) followed by a newline. This is used as
6516 a get_unnamed_section callback. */
6518 void
6519 output_section_asm_op (const void *directive)
6521 fprintf (asm_out_file, "%s\n", (const char *) directive);
6524 /* Emit assembly code to switch to section NEW_SECTION. Do nothing if
6525 the current section is NEW_SECTION. */
6527 void
6528 switch_to_section (section *new_section)
6530 if (in_section == new_section)
6531 return;
6533 if (new_section->common.flags & SECTION_FORGET)
6534 in_section = NULL;
6535 else
6536 in_section = new_section;
6538 switch (SECTION_STYLE (new_section))
6540 case SECTION_NAMED:
6541 if (cfun
6542 && !crtl->subsections.unlikely_text_section_name
6543 && strcmp (new_section->named.name,
6544 UNLIKELY_EXECUTED_TEXT_SECTION_NAME) == 0)
6545 crtl->subsections.unlikely_text_section_name = UNLIKELY_EXECUTED_TEXT_SECTION_NAME;
6547 targetm.asm_out.named_section (new_section->named.name,
6548 new_section->named.common.flags,
6549 new_section->named.decl);
6550 break;
6552 case SECTION_UNNAMED:
6553 new_section->unnamed.callback (new_section->unnamed.data);
6554 break;
6556 case SECTION_NOSWITCH:
6557 gcc_unreachable ();
6558 break;
6561 new_section->common.flags |= SECTION_DECLARED;
6564 /* If block symbol SYMBOL has not yet been assigned an offset, place
6565 it at the end of its block. */
6567 void
6568 place_block_symbol (rtx symbol)
6570 unsigned HOST_WIDE_INT size, mask, offset;
6571 struct constant_descriptor_rtx *desc;
6572 unsigned int alignment;
6573 struct object_block *block;
6574 tree decl;
6576 gcc_assert (SYMBOL_REF_BLOCK (symbol));
6577 if (SYMBOL_REF_BLOCK_OFFSET (symbol) >= 0)
6578 return;
6580 /* Work out the symbol's size and alignment. */
6581 if (CONSTANT_POOL_ADDRESS_P (symbol))
6583 desc = SYMBOL_REF_CONSTANT (symbol);
6584 alignment = desc->align;
6585 size = GET_MODE_SIZE (desc->mode);
6587 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
6589 decl = SYMBOL_REF_DECL (symbol);
6590 alignment = DECL_ALIGN (decl);
6591 size = get_constant_size (DECL_INITIAL (decl));
6593 else
6595 decl = SYMBOL_REF_DECL (symbol);
6596 alignment = DECL_ALIGN (decl);
6597 size = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
6600 /* Calculate the object's offset from the start of the block. */
6601 block = SYMBOL_REF_BLOCK (symbol);
6602 mask = alignment / BITS_PER_UNIT - 1;
6603 offset = (block->size + mask) & ~mask;
6604 SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
6606 /* Record the block's new alignment and size. */
6607 block->alignment = MAX (block->alignment, alignment);
6608 block->size = offset + size;
6610 VEC_safe_push (rtx, gc, block->objects, symbol);
6613 /* Return the anchor that should be used to address byte offset OFFSET
6614 from the first object in BLOCK. MODEL is the TLS model used
6615 to access it. */
6618 get_section_anchor (struct object_block *block, HOST_WIDE_INT offset,
6619 enum tls_model model)
6621 char label[100];
6622 unsigned int begin, middle, end;
6623 unsigned HOST_WIDE_INT min_offset, max_offset, range, bias, delta;
6624 rtx anchor;
6626 /* Work out the anchor's offset. Use an offset of 0 for the first
6627 anchor so that we don't pessimize the case where we take the address
6628 of a variable at the beginning of the block. This is particularly
6629 useful when a block has only one variable assigned to it.
6631 We try to place anchors RANGE bytes apart, so there can then be
6632 anchors at +/-RANGE, +/-2 * RANGE, and so on, up to the limits of
6633 a ptr_mode offset. With some target settings, the lowest such
6634 anchor might be out of range for the lowest ptr_mode offset;
6635 likewise the highest anchor for the highest offset. Use anchors
6636 at the extreme ends of the ptr_mode range in such cases.
6638 All arithmetic uses unsigned integers in order to avoid
6639 signed overflow. */
6640 max_offset = (unsigned HOST_WIDE_INT) targetm.max_anchor_offset;
6641 min_offset = (unsigned HOST_WIDE_INT) targetm.min_anchor_offset;
6642 range = max_offset - min_offset + 1;
6643 if (range == 0)
6644 offset = 0;
6645 else
6647 bias = 1 << (GET_MODE_BITSIZE (ptr_mode) - 1);
6648 if (offset < 0)
6650 delta = -(unsigned HOST_WIDE_INT) offset + max_offset;
6651 delta -= delta % range;
6652 if (delta > bias)
6653 delta = bias;
6654 offset = (HOST_WIDE_INT) (-delta);
6656 else
6658 delta = (unsigned HOST_WIDE_INT) offset - min_offset;
6659 delta -= delta % range;
6660 if (delta > bias - 1)
6661 delta = bias - 1;
6662 offset = (HOST_WIDE_INT) delta;
6666 /* Do a binary search to see if there's already an anchor we can use.
6667 Set BEGIN to the new anchor's index if not. */
6668 begin = 0;
6669 end = VEC_length (rtx, block->anchors);
6670 while (begin != end)
6672 middle = (end + begin) / 2;
6673 anchor = VEC_index (rtx, block->anchors, middle);
6674 if (SYMBOL_REF_BLOCK_OFFSET (anchor) > offset)
6675 end = middle;
6676 else if (SYMBOL_REF_BLOCK_OFFSET (anchor) < offset)
6677 begin = middle + 1;
6678 else if (SYMBOL_REF_TLS_MODEL (anchor) > model)
6679 end = middle;
6680 else if (SYMBOL_REF_TLS_MODEL (anchor) < model)
6681 begin = middle + 1;
6682 else
6683 return anchor;
6686 /* Create a new anchor with a unique label. */
6687 ASM_GENERATE_INTERNAL_LABEL (label, "LANCHOR", anchor_labelno++);
6688 anchor = create_block_symbol (ggc_strdup (label), block, offset);
6689 SYMBOL_REF_FLAGS (anchor) |= SYMBOL_FLAG_LOCAL | SYMBOL_FLAG_ANCHOR;
6690 SYMBOL_REF_FLAGS (anchor) |= model << SYMBOL_FLAG_TLS_SHIFT;
6692 /* Insert it at index BEGIN. */
6693 VEC_safe_insert (rtx, gc, block->anchors, begin, anchor);
6694 return anchor;
6697 /* Output the objects in BLOCK. */
6699 static void
6700 output_object_block (struct object_block *block)
6702 struct constant_descriptor_rtx *desc;
6703 unsigned int i;
6704 HOST_WIDE_INT offset;
6705 tree decl;
6706 rtx symbol;
6708 if (block->objects == NULL)
6709 return;
6711 /* Switch to the section and make sure that the first byte is
6712 suitably aligned. */
6713 switch_to_section (block->sect);
6714 assemble_align (block->alignment);
6716 /* Define the values of all anchors relative to the current section
6717 position. */
6718 for (i = 0; VEC_iterate (rtx, block->anchors, i, symbol); i++)
6719 targetm.asm_out.output_anchor (symbol);
6721 /* Output the objects themselves. */
6722 offset = 0;
6723 for (i = 0; VEC_iterate (rtx, block->objects, i, symbol); i++)
6725 /* Move to the object's offset, padding with zeros if necessary. */
6726 assemble_zeros (SYMBOL_REF_BLOCK_OFFSET (symbol) - offset);
6727 offset = SYMBOL_REF_BLOCK_OFFSET (symbol);
6728 if (CONSTANT_POOL_ADDRESS_P (symbol))
6730 desc = SYMBOL_REF_CONSTANT (symbol);
6731 output_constant_pool_1 (desc, 1);
6732 offset += GET_MODE_SIZE (desc->mode);
6734 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
6736 decl = SYMBOL_REF_DECL (symbol);
6737 assemble_constant_contents (DECL_INITIAL (decl), XSTR (symbol, 0),
6738 DECL_ALIGN (decl));
6739 offset += get_constant_size (DECL_INITIAL (decl));
6741 else
6743 decl = SYMBOL_REF_DECL (symbol);
6744 assemble_variable_contents (decl, XSTR (symbol, 0), false);
6745 offset += tree_low_cst (DECL_SIZE_UNIT (decl), 1);
6750 /* A htab_traverse callback used to call output_object_block for
6751 each member of object_block_htab. */
6753 static int
6754 output_object_block_htab (void **slot, void *data ATTRIBUTE_UNUSED)
6756 output_object_block ((struct object_block *) (*slot));
6757 return 1;
6760 /* Output the definitions of all object_blocks. */
6762 void
6763 output_object_blocks (void)
6765 htab_traverse (object_block_htab, output_object_block_htab, NULL);
6768 /* This function provides a possible implementation of the
6769 TARGET_ASM_RECORD_GCC_SWITCHES target hook for ELF targets. When triggered
6770 by -frecord-gcc-switches it creates a new mergeable, string section in the
6771 assembler output file called TARGET_ASM_RECORD_GCC_SWITCHES_SECTION which
6772 contains the switches in ASCII format.
6774 FIXME: This code does not correctly handle double quote characters
6775 that appear inside strings, (it strips them rather than preserving them).
6776 FIXME: ASM_OUTPUT_ASCII, as defined in config/elfos.h will not emit NUL
6777 characters - instead it treats them as sub-string separators. Since
6778 we want to emit NUL strings terminators into the object file we have to use
6779 ASM_OUTPUT_SKIP. */
6782 elf_record_gcc_switches (print_switch_type type, const char * name)
6784 switch (type)
6786 case SWITCH_TYPE_PASSED:
6787 ASM_OUTPUT_ASCII (asm_out_file, name, strlen (name));
6788 ASM_OUTPUT_SKIP (asm_out_file, (unsigned HOST_WIDE_INT) 1);
6789 break;
6791 case SWITCH_TYPE_DESCRIPTIVE:
6792 if (name == NULL)
6794 /* Distinguish between invocations where name is NULL. */
6795 static bool started = false;
6797 if (!started)
6799 section * sec;
6801 sec = get_section (targetm.asm_out.record_gcc_switches_section,
6802 SECTION_DEBUG
6803 | SECTION_MERGE
6804 | SECTION_STRINGS
6805 | (SECTION_ENTSIZE & 1),
6806 NULL);
6807 switch_to_section (sec);
6808 started = true;
6812 default:
6813 break;
6816 /* The return value is currently ignored by the caller, but must be 0.
6817 For -fverbose-asm the return value would be the number of characters
6818 emitted into the assembler file. */
6819 return 0;
6822 /* Emit text to declare externally defined symbols. It is needed to
6823 properly support non-default visibility. */
6824 void
6825 default_elf_asm_output_external (FILE *file ATTRIBUTE_UNUSED,
6826 tree decl,
6827 const char *name ATTRIBUTE_UNUSED)
6829 /* We output the name if and only if TREE_SYMBOL_REFERENCED is
6830 set in order to avoid putting out names that are never really
6831 used. */
6832 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
6833 && targetm.binds_local_p (decl))
6834 maybe_assemble_visibility (decl);
6837 /* The default hook for TARGET_ASM_OUTPUT_SOURCE_FILENAME. */
6839 void
6840 default_asm_output_source_filename (FILE *file, const char *name)
6842 #ifdef ASM_OUTPUT_SOURCE_FILENAME
6843 ASM_OUTPUT_SOURCE_FILENAME (file, name);
6844 #else
6845 fprintf (file, "\t.file\t");
6846 output_quoted_string (file, name);
6847 putc ('\n', file);
6848 #endif
6851 /* Output a file name in the form wanted by System V. */
6853 void
6854 output_file_directive (FILE *asm_file, const char *input_name)
6856 int len;
6857 const char *na;
6859 if (input_name == NULL)
6860 input_name = "<stdin>";
6861 else
6862 input_name = remap_debug_filename (input_name);
6864 len = strlen (input_name);
6865 na = input_name + len;
6867 /* NA gets INPUT_NAME sans directory names. */
6868 while (na > input_name)
6870 if (IS_DIR_SEPARATOR (na[-1]))
6871 break;
6872 na--;
6875 targetm.asm_out.output_source_filename (asm_file, na);
6878 /* Create a DEBUG_EXPR_DECL / DEBUG_EXPR pair from RTL expression
6879 EXP. */
6881 make_debug_expr_from_rtl (const_rtx exp)
6883 tree ddecl = make_node (DEBUG_EXPR_DECL), type;
6884 enum machine_mode mode = GET_MODE (exp);
6885 rtx dval;
6887 DECL_ARTIFICIAL (ddecl) = 1;
6888 if (REG_P (exp) && REG_EXPR (exp))
6889 type = TREE_TYPE (REG_EXPR (exp));
6890 else if (MEM_P (exp) && MEM_EXPR (exp))
6891 type = TREE_TYPE (MEM_EXPR (exp));
6892 else
6893 type = NULL_TREE;
6894 if (type && TYPE_MODE (type) == mode)
6895 TREE_TYPE (ddecl) = type;
6896 else
6897 TREE_TYPE (ddecl) = lang_hooks.types.type_for_mode (mode, 1);
6898 DECL_MODE (ddecl) = mode;
6899 dval = gen_rtx_DEBUG_EXPR (mode);
6900 DEBUG_EXPR_TREE_DECL (dval) = ddecl;
6901 SET_DECL_RTL (ddecl, dval);
6902 return dval;
6905 #include "gt-varasm.h"