* doc/invoke.texi: Add cpu_type power6.
[official-gcc.git] / gcc / varasm.c
blob71158ba860b6c87ffb57ffa1bc08069ee4dbe0fa
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
4 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 2, 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 COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
24 /* This file handles generation of all the assembler code
25 *except* the instructions of a function.
26 This includes declarations of variables and their initial values.
28 We also output the assembler code for constants stored in memory
29 and are responsible for combining constants with the same value. */
31 #include "config.h"
32 #include "system.h"
33 #include "coretypes.h"
34 #include "tm.h"
35 #include "rtl.h"
36 #include "tree.h"
37 #include "flags.h"
38 #include "function.h"
39 #include "expr.h"
40 #include "hard-reg-set.h"
41 #include "regs.h"
42 #include "real.h"
43 #include "output.h"
44 #include "toplev.h"
45 #include "hashtab.h"
46 #include "c-pragma.h"
47 #include "ggc.h"
48 #include "langhooks.h"
49 #include "tm_p.h"
50 #include "debug.h"
51 #include "target.h"
52 #include "tree-mudflap.h"
53 #include "cgraph.h"
54 #include "cfglayout.h"
55 #include "basic-block.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 struct varasm_status GTY(())
75 /* If we're using a per-function constant pool, this is it. */
76 struct rtx_constant_pool *pool;
78 /* Number of tree-constants deferred during the expansion of this
79 function. */
80 unsigned int deferred_constants;
83 #define n_deferred_constants (cfun->varasm->deferred_constants)
85 /* Number for making the label on the next
86 constant that is stored in memory. */
88 static GTY(()) int const_labelno;
90 /* Carry information from ASM_DECLARE_OBJECT_NAME
91 to ASM_FINISH_DECLARE_OBJECT. */
93 int size_directive_output;
95 /* The last decl for which assemble_variable was called,
96 if it did ASM_DECLARE_OBJECT_NAME.
97 If the last call to assemble_variable didn't do that,
98 this holds 0. */
100 tree last_assemble_variable_decl;
102 /* The following global variable indicates if the first basic block
103 in a function belongs to the cold partition or not. */
105 bool first_function_block_is_cold;
107 /* We give all constants their own alias set. Perhaps redundant with
108 MEM_READONLY_P, but pre-dates it. */
110 static HOST_WIDE_INT const_alias_set;
112 static const char *strip_reg_name (const char *);
113 static int contains_pointers_p (tree);
114 #ifdef ASM_OUTPUT_EXTERNAL
115 static bool incorporeal_function_p (tree);
116 #endif
117 static void decode_addr_const (tree, struct addr_const *);
118 static hashval_t const_desc_hash (const void *);
119 static int const_desc_eq (const void *, const void *);
120 static hashval_t const_hash_1 (const tree);
121 static int compare_constant (const tree, const tree);
122 static tree copy_constant (tree);
123 static void output_constant_def_contents (rtx);
124 static void output_addressed_constants (tree);
125 static unsigned HOST_WIDE_INT array_size_for_constructor (tree);
126 static unsigned min_align (unsigned, unsigned);
127 static void output_constructor (tree, unsigned HOST_WIDE_INT, unsigned int);
128 static void globalize_decl (tree);
129 static void maybe_assemble_visibility (tree);
130 #ifdef BSS_SECTION_ASM_OP
131 #ifdef ASM_OUTPUT_BSS
132 static void asm_output_bss (FILE *, tree, const char *,
133 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
134 #endif
135 #ifdef ASM_OUTPUT_ALIGNED_BSS
136 static void asm_output_aligned_bss (FILE *, tree, const char *,
137 unsigned HOST_WIDE_INT, int)
138 ATTRIBUTE_UNUSED;
139 #endif
140 #endif /* BSS_SECTION_ASM_OP */
141 static void mark_weak (tree);
142 static void output_constant_pool (const char *, tree);
144 /* Well-known sections, each one associated with some sort of *_ASM_OP. */
145 section *text_section;
146 section *data_section;
147 section *readonly_data_section;
148 section *sdata_section;
149 section *ctors_section;
150 section *dtors_section;
151 section *bss_section;
152 section *sbss_section;
154 /* Various forms of common section. All are guaranteed to be nonnull. */
155 section *tls_comm_section;
156 section *comm_section;
157 section *lcomm_section;
159 /* A SECTION_NOSWITCH section used for declaring global BSS variables.
160 May be null. */
161 section *bss_noswitch_section;
163 /* The section that holds the main exception table, when known. The section
164 is set either by the target's init_sections hook or by the first call to
165 switch_to_exception_section. */
166 section *exception_section;
168 /* The section that holds the DWARF2 frame unwind information, when known.
169 The section is set either by the target's init_sections hook or by the
170 first call to switch_to_eh_frame_section. */
171 section *eh_frame_section;
173 /* asm_out_file's current section. This is NULL if no section has yet
174 been selected or if we lose track of what the current section is. */
175 section *in_section;
177 /* True if code for the current function is currently being directed
178 at the cold section. */
179 bool in_cold_section_p;
181 /* A linked list of all the unnamed sections. */
182 static GTY(()) section *unnamed_sections;
184 /* Return a nonzero value if DECL has a section attribute. */
185 #ifndef IN_NAMED_SECTION
186 #define IN_NAMED_SECTION(DECL) \
187 ((TREE_CODE (DECL) == FUNCTION_DECL || TREE_CODE (DECL) == VAR_DECL) \
188 && DECL_SECTION_NAME (DECL) != NULL_TREE)
189 #endif
191 /* Hash table of named sections. */
192 static GTY((param_is (section))) htab_t section_htab;
194 /* A table of object_blocks, indexed by section. */
195 static GTY((param_is (struct object_block))) htab_t object_block_htab;
197 /* The next number to use for internal anchor labels. */
198 static GTY(()) int anchor_labelno;
200 /* A pool of constants that can be shared between functions. */
201 static GTY(()) struct rtx_constant_pool *shared_constant_pool;
203 /* Helper routines for maintaining section_htab. */
205 static int
206 section_entry_eq (const void *p1, const void *p2)
208 const section *old = p1;
209 const char *new = p2;
211 return strcmp (old->named.name, new) == 0;
214 static hashval_t
215 section_entry_hash (const void *p)
217 const section *old = p;
218 return htab_hash_string (old->named.name);
221 /* Return a hash value for section SECT. */
223 static hashval_t
224 hash_section (section *sect)
226 if (sect->common.flags & SECTION_NAMED)
227 return htab_hash_string (sect->named.name);
228 return sect->common.flags;
231 /* Helper routines for maintaining object_block_htab. */
233 static int
234 object_block_entry_eq (const void *p1, const void *p2)
236 const struct object_block *old = p1;
237 const section *new = p2;
239 return old->sect == new;
242 static hashval_t
243 object_block_entry_hash (const void *p)
245 const struct object_block *old = p;
246 return hash_section (old->sect);
249 /* Return a new unnamed section with the given fields. */
251 section *
252 get_unnamed_section (unsigned int flags, void (*callback) (const void *),
253 const void *data)
255 section *sect;
257 sect = ggc_alloc (sizeof (struct unnamed_section));
258 sect->unnamed.common.flags = flags | SECTION_UNNAMED;
259 sect->unnamed.callback = callback;
260 sect->unnamed.data = data;
261 sect->unnamed.next = unnamed_sections;
263 unnamed_sections = sect;
264 return sect;
267 /* Return a SECTION_NOSWITCH section with the given fields. */
269 static section *
270 get_noswitch_section (unsigned int flags, noswitch_section_callback callback)
272 section *sect;
274 sect = ggc_alloc (sizeof (struct unnamed_section));
275 sect->noswitch.common.flags = flags | SECTION_NOSWITCH;
276 sect->noswitch.callback = callback;
278 return sect;
281 /* Return the named section structure associated with NAME. Create
282 a new section with the given fields if no such structure exists. */
284 section *
285 get_section (const char *name, unsigned int flags, tree decl)
287 section *sect, **slot;
289 slot = (section **)
290 htab_find_slot_with_hash (section_htab, name,
291 htab_hash_string (name), INSERT);
292 flags |= SECTION_NAMED;
293 if (*slot == NULL)
295 sect = ggc_alloc (sizeof (struct named_section));
296 sect->named.common.flags = flags;
297 sect->named.name = ggc_strdup (name);
298 sect->named.decl = decl;
299 *slot = sect;
301 else
303 sect = *slot;
304 if ((sect->common.flags & ~SECTION_DECLARED) != flags
305 && ((sect->common.flags | flags) & SECTION_OVERRIDE) == 0)
307 /* Sanity check user variables for flag changes. */
308 if (decl == 0)
309 decl = sect->named.decl;
310 gcc_assert (decl);
311 error ("%+D causes a section type conflict", decl);
314 return sect;
317 /* Return true if the current compilation mode benefits from having
318 objects grouped into blocks. */
320 static bool
321 use_object_blocks_p (void)
323 return flag_section_anchors;
326 /* Return the object_block structure for section SECT. Create a new
327 structure if we haven't created one already. Return null if SECT
328 itself is null. */
330 static struct object_block *
331 get_block_for_section (section *sect)
333 struct object_block *block;
334 void **slot;
336 if (sect == NULL)
337 return NULL;
339 slot = htab_find_slot_with_hash (object_block_htab, sect,
340 hash_section (sect), INSERT);
341 block = (struct object_block *) *slot;
342 if (block == NULL)
344 block = (struct object_block *)
345 ggc_alloc_cleared (sizeof (struct object_block));
346 block->sect = sect;
347 *slot = block;
349 return block;
352 /* Create a symbol with label LABEL and place it at byte offset
353 OFFSET in BLOCK. OFFSET can be negative if the symbol's offset
354 is not yet known. LABEL must be a garbage-collected string. */
356 static rtx
357 create_block_symbol (const char *label, struct object_block *block,
358 HOST_WIDE_INT offset)
360 rtx symbol;
361 unsigned int size;
363 /* Create the extended SYMBOL_REF. */
364 size = RTX_HDR_SIZE + sizeof (struct block_symbol);
365 symbol = ggc_alloc_zone (size, &rtl_zone);
367 /* Initialize the normal SYMBOL_REF fields. */
368 memset (symbol, 0, size);
369 PUT_CODE (symbol, SYMBOL_REF);
370 PUT_MODE (symbol, Pmode);
371 XSTR (symbol, 0) = label;
372 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_HAS_BLOCK_INFO;
374 /* Initialize the block_symbol stuff. */
375 SYMBOL_REF_BLOCK (symbol) = block;
376 SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
378 return symbol;
381 static void
382 initialize_cold_section_name (void)
384 const char *stripped_name;
385 char *name, *buffer;
386 tree dsn;
388 gcc_assert (cfun && current_function_decl);
389 if (cfun->unlikely_text_section_name)
390 return;
392 dsn = DECL_SECTION_NAME (current_function_decl);
393 if (flag_function_sections && dsn)
395 name = alloca (TREE_STRING_LENGTH (dsn) + 1);
396 memcpy (name, TREE_STRING_POINTER (dsn), TREE_STRING_LENGTH (dsn) + 1);
398 stripped_name = targetm.strip_name_encoding (name);
400 buffer = ACONCAT ((stripped_name, "_unlikely", NULL));
401 cfun->unlikely_text_section_name = ggc_strdup (buffer);
403 else
404 cfun->unlikely_text_section_name = UNLIKELY_EXECUTED_TEXT_SECTION_NAME;
407 /* Tell assembler to switch to unlikely-to-be-executed text section. */
409 section *
410 unlikely_text_section (void)
412 if (cfun)
414 if (!cfun->unlikely_text_section_name)
415 initialize_cold_section_name ();
417 return get_named_section (NULL, cfun->unlikely_text_section_name, 0);
419 else
420 return get_named_section (NULL, UNLIKELY_EXECUTED_TEXT_SECTION_NAME, 0);
423 /* When called within a function context, return true if the function
424 has been assigned a cold text section and if SECT is that section.
425 When called outside a function context, return true if SECT is the
426 default cold section. */
428 bool
429 unlikely_text_section_p (section *sect)
431 const char *name;
433 if (cfun)
434 name = cfun->unlikely_text_section_name;
435 else
436 name = UNLIKELY_EXECUTED_TEXT_SECTION_NAME;
438 return (name
439 && sect
440 && SECTION_STYLE (sect) == SECTION_NAMED
441 && strcmp (name, sect->named.name) == 0);
444 /* Return a section with a particular name and with whatever SECTION_*
445 flags section_type_flags deems appropriate. The name of the section
446 is taken from NAME if nonnull, otherwise it is taken from DECL's
447 DECL_SECTION_NAME. DECL is the decl associated with the section
448 (see the section comment for details) and RELOC is as for
449 section_type_flags. */
451 section *
452 get_named_section (tree decl, const char *name, int reloc)
454 unsigned int flags;
456 gcc_assert (!decl || DECL_P (decl));
457 if (name == NULL)
458 name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
460 flags = targetm.section_type_flags (decl, name, reloc);
462 return get_section (name, flags, decl);
465 /* If required, set DECL_SECTION_NAME to a unique name. */
467 void
468 resolve_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED,
469 int flag_function_or_data_sections)
471 if (DECL_SECTION_NAME (decl) == NULL_TREE
472 && targetm.have_named_sections
473 && (flag_function_or_data_sections
474 || DECL_ONE_ONLY (decl)))
475 targetm.asm_out.unique_section (decl, reloc);
478 #ifdef BSS_SECTION_ASM_OP
480 #ifdef ASM_OUTPUT_BSS
482 /* Utility function for ASM_OUTPUT_BSS for targets to use if
483 they don't support alignments in .bss.
484 ??? It is believed that this function will work in most cases so such
485 support is localized here. */
487 static void
488 asm_output_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
489 const char *name,
490 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
491 unsigned HOST_WIDE_INT rounded)
493 targetm.asm_out.globalize_label (file, name);
494 switch_to_section (bss_section);
495 #ifdef ASM_DECLARE_OBJECT_NAME
496 last_assemble_variable_decl = decl;
497 ASM_DECLARE_OBJECT_NAME (file, name, decl);
498 #else
499 /* Standard thing is just output label for the object. */
500 ASM_OUTPUT_LABEL (file, name);
501 #endif /* ASM_DECLARE_OBJECT_NAME */
502 ASM_OUTPUT_SKIP (file, rounded ? rounded : 1);
505 #endif
507 #ifdef ASM_OUTPUT_ALIGNED_BSS
509 /* Utility function for targets to use in implementing
510 ASM_OUTPUT_ALIGNED_BSS.
511 ??? It is believed that this function will work in most cases so such
512 support is localized here. */
514 static void
515 asm_output_aligned_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
516 const char *name, unsigned HOST_WIDE_INT size,
517 int align)
519 switch_to_section (bss_section);
520 ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
521 #ifdef ASM_DECLARE_OBJECT_NAME
522 last_assemble_variable_decl = decl;
523 ASM_DECLARE_OBJECT_NAME (file, name, decl);
524 #else
525 /* Standard thing is just output label for the object. */
526 ASM_OUTPUT_LABEL (file, name);
527 #endif /* ASM_DECLARE_OBJECT_NAME */
528 ASM_OUTPUT_SKIP (file, size ? size : 1);
531 #endif
533 #endif /* BSS_SECTION_ASM_OP */
535 #ifndef USE_SELECT_SECTION_FOR_FUNCTIONS
536 /* Return the hot section for function DECL. Return text_section for
537 null DECLs. */
539 static section *
540 hot_function_section (tree decl)
542 if (decl != NULL_TREE
543 && DECL_SECTION_NAME (decl) != NULL_TREE
544 && targetm.have_named_sections)
545 return get_named_section (decl, NULL, 0);
546 else
547 return text_section;
549 #endif
551 /* Return the section for function DECL.
553 If DECL is NULL_TREE, return the text section. We can be passed
554 NULL_TREE under some circumstances by dbxout.c at least. */
556 section *
557 function_section (tree decl)
559 int reloc = 0;
561 if (first_function_block_is_cold)
562 reloc = 1;
564 #ifdef USE_SELECT_SECTION_FOR_FUNCTIONS
565 if (decl != NULL_TREE
566 && DECL_SECTION_NAME (decl) != NULL_TREE)
567 return reloc ? unlikely_text_section ()
568 : get_named_section (decl, NULL, 0);
569 else
570 return targetm.asm_out.select_section (decl, reloc, DECL_ALIGN (decl));
571 #else
572 return reloc ? unlikely_text_section () : hot_function_section (decl);
573 #endif
576 section *
577 current_function_section (void)
579 #ifdef USE_SELECT_SECTION_FOR_FUNCTIONS
580 if (current_function_decl != NULL_TREE
581 && DECL_SECTION_NAME (current_function_decl) != NULL_TREE)
582 return in_cold_section_p ? unlikely_text_section ()
583 : get_named_section (current_function_decl,
584 NULL, 0);
585 else
586 return targetm.asm_out.select_section (current_function_decl,
587 in_cold_section_p,
588 DECL_ALIGN (current_function_decl));
589 #else
590 return (in_cold_section_p
591 ? unlikely_text_section ()
592 : hot_function_section (current_function_decl));
593 #endif
596 /* Return the read-only data section associated with function DECL. */
598 section *
599 default_function_rodata_section (tree decl)
601 if (decl != NULL_TREE && DECL_SECTION_NAME (decl))
603 const char *name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
605 if (DECL_ONE_ONLY (decl) && HAVE_COMDAT_GROUP)
607 size_t len = strlen (name) + 3;
608 char* rname = alloca (len);
610 strcpy (rname, ".rodata");
611 strcat (rname, name + 5);
612 return get_section (rname, SECTION_LINKONCE, decl);
614 /* For .gnu.linkonce.t.foo we want to use .gnu.linkonce.r.foo. */
615 else if (DECL_ONE_ONLY (decl)
616 && strncmp (name, ".gnu.linkonce.t.", 16) == 0)
618 size_t len = strlen (name) + 1;
619 char *rname = alloca (len);
621 memcpy (rname, name, len);
622 rname[14] = 'r';
623 return get_section (rname, SECTION_LINKONCE, decl);
625 /* For .text.foo we want to use .rodata.foo. */
626 else if (flag_function_sections && flag_data_sections
627 && strncmp (name, ".text.", 6) == 0)
629 size_t len = strlen (name) + 1;
630 char *rname = alloca (len + 2);
632 memcpy (rname, ".rodata", 7);
633 memcpy (rname + 7, name + 5, len - 5);
634 return get_section (rname, 0, decl);
638 return readonly_data_section;
641 /* Return the read-only data section associated with function DECL
642 for targets where that section should be always the single
643 readonly data section. */
645 section *
646 default_no_function_rodata_section (tree decl ATTRIBUTE_UNUSED)
648 return readonly_data_section;
651 /* Return the section to use for string merging. */
653 static section *
654 mergeable_string_section (tree decl ATTRIBUTE_UNUSED,
655 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
656 unsigned int flags ATTRIBUTE_UNUSED)
658 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
659 && TREE_CODE (decl) == STRING_CST
660 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
661 && align <= 256
662 && TREE_STRING_LENGTH (decl) >= int_size_in_bytes (TREE_TYPE (decl)))
664 enum machine_mode mode;
665 unsigned int modesize;
666 const char *str;
667 int i, j, len, unit;
668 char name[30];
670 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (decl)));
671 modesize = GET_MODE_BITSIZE (mode);
672 if (modesize >= 8 && modesize <= 256
673 && (modesize & (modesize - 1)) == 0)
675 if (align < modesize)
676 align = modesize;
678 str = TREE_STRING_POINTER (decl);
679 len = TREE_STRING_LENGTH (decl);
680 unit = GET_MODE_SIZE (mode);
682 /* Check for embedded NUL characters. */
683 for (i = 0; i < len; i += unit)
685 for (j = 0; j < unit; j++)
686 if (str[i + j] != '\0')
687 break;
688 if (j == unit)
689 break;
691 if (i == len - unit)
693 sprintf (name, ".rodata.str%d.%d", modesize / 8,
694 (int) (align / 8));
695 flags |= (modesize / 8) | SECTION_MERGE | SECTION_STRINGS;
696 return get_section (name, flags, NULL);
701 return readonly_data_section;
704 /* Return the section to use for constant merging. */
706 section *
707 mergeable_constant_section (enum machine_mode mode ATTRIBUTE_UNUSED,
708 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
709 unsigned int flags ATTRIBUTE_UNUSED)
711 unsigned int modesize = GET_MODE_BITSIZE (mode);
713 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
714 && mode != VOIDmode
715 && mode != BLKmode
716 && modesize <= align
717 && align >= 8
718 && align <= 256
719 && (align & (align - 1)) == 0)
721 char name[24];
723 sprintf (name, ".rodata.cst%d", (int) (align / 8));
724 flags |= (align / 8) | SECTION_MERGE;
725 return get_section (name, flags, NULL);
727 return readonly_data_section;
730 /* Given NAME, a putative register name, discard any customary prefixes. */
732 static const char *
733 strip_reg_name (const char *name)
735 #ifdef REGISTER_PREFIX
736 if (!strncmp (name, REGISTER_PREFIX, strlen (REGISTER_PREFIX)))
737 name += strlen (REGISTER_PREFIX);
738 #endif
739 if (name[0] == '%' || name[0] == '#')
740 name++;
741 return name;
744 /* The user has asked for a DECL to have a particular name. Set (or
745 change) it in such a way that we don't prefix an underscore to
746 it. */
747 void
748 set_user_assembler_name (tree decl, const char *name)
750 char *starred = alloca (strlen (name) + 2);
751 starred[0] = '*';
752 strcpy (starred + 1, name);
753 change_decl_assembler_name (decl, get_identifier (starred));
754 SET_DECL_RTL (decl, NULL_RTX);
757 /* Decode an `asm' spec for a declaration as a register name.
758 Return the register number, or -1 if nothing specified,
759 or -2 if the ASMSPEC is not `cc' or `memory' and is not recognized,
760 or -3 if ASMSPEC is `cc' and is not recognized,
761 or -4 if ASMSPEC is `memory' and is not recognized.
762 Accept an exact spelling or a decimal number.
763 Prefixes such as % are optional. */
766 decode_reg_name (const char *asmspec)
768 if (asmspec != 0)
770 int i;
772 /* Get rid of confusing prefixes. */
773 asmspec = strip_reg_name (asmspec);
775 /* Allow a decimal number as a "register name". */
776 for (i = strlen (asmspec) - 1; i >= 0; i--)
777 if (! ISDIGIT (asmspec[i]))
778 break;
779 if (asmspec[0] != 0 && i < 0)
781 i = atoi (asmspec);
782 if (i < FIRST_PSEUDO_REGISTER && i >= 0)
783 return i;
784 else
785 return -2;
788 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
789 if (reg_names[i][0]
790 && ! strcmp (asmspec, strip_reg_name (reg_names[i])))
791 return i;
793 #ifdef ADDITIONAL_REGISTER_NAMES
795 static const struct { const char *const name; const int number; } table[]
796 = ADDITIONAL_REGISTER_NAMES;
798 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
799 if (table[i].name[0]
800 && ! strcmp (asmspec, table[i].name))
801 return table[i].number;
803 #endif /* ADDITIONAL_REGISTER_NAMES */
805 if (!strcmp (asmspec, "memory"))
806 return -4;
808 if (!strcmp (asmspec, "cc"))
809 return -3;
811 return -2;
814 return -1;
817 /* Return true if DECL's initializer is suitable for a BSS section. */
819 static bool
820 bss_initializer_p (tree decl)
822 return (DECL_INITIAL (decl) == NULL
823 || DECL_INITIAL (decl) == error_mark_node
824 || (flag_zero_initialized_in_bss
825 /* Leave constant zeroes in .rodata so they
826 can be shared. */
827 && !TREE_READONLY (decl)
828 && initializer_zerop (DECL_INITIAL (decl))));
831 /* Compute the alignment of variable specified by DECL.
832 DONT_OUTPUT_DATA is from assemble_variable. */
834 static void
835 align_variable (tree decl, bool dont_output_data)
837 unsigned int align = DECL_ALIGN (decl);
839 /* In the case for initialing an array whose length isn't specified,
840 where we have not yet been able to do the layout,
841 figure out the proper alignment now. */
842 if (dont_output_data && DECL_SIZE (decl) == 0
843 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
844 align = MAX (align, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (decl))));
846 /* Some object file formats have a maximum alignment which they support.
847 In particular, a.out format supports a maximum alignment of 4. */
848 if (align > MAX_OFILE_ALIGNMENT)
850 warning (0, "alignment of %q+D is greater than maximum object "
851 "file alignment. Using %d", decl,
852 MAX_OFILE_ALIGNMENT/BITS_PER_UNIT);
853 align = MAX_OFILE_ALIGNMENT;
856 /* On some machines, it is good to increase alignment sometimes. */
857 if (! DECL_USER_ALIGN (decl))
859 #ifdef DATA_ALIGNMENT
860 align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
861 #endif
862 #ifdef CONSTANT_ALIGNMENT
863 if (DECL_INITIAL (decl) != 0 && DECL_INITIAL (decl) != error_mark_node)
864 align = CONSTANT_ALIGNMENT (DECL_INITIAL (decl), align);
865 #endif
868 /* Reset the alignment in case we have made it tighter, so we can benefit
869 from it in get_pointer_alignment. */
870 DECL_ALIGN (decl) = align;
873 /* Return the section into which the given VAR_DECL or CONST_DECL
874 should be placed. PREFER_NOSWITCH_P is true if a noswitch
875 section should be used wherever possible. */
877 static section *
878 get_variable_section (tree decl, bool prefer_noswitch_p)
880 int reloc;
882 /* If the decl has been given an explicit section name, then it
883 isn't common, and shouldn't be handled as such. */
884 if (DECL_COMMON (decl) && DECL_SECTION_NAME (decl) == NULL)
886 if (DECL_THREAD_LOCAL_P (decl))
887 return tls_comm_section;
888 if (TREE_PUBLIC (decl) && bss_initializer_p (decl))
889 return comm_section;
892 if (DECL_INITIAL (decl) == error_mark_node)
893 reloc = contains_pointers_p (TREE_TYPE (decl)) ? 3 : 0;
894 else if (DECL_INITIAL (decl))
895 reloc = compute_reloc_for_constant (DECL_INITIAL (decl));
896 else
897 reloc = 0;
899 resolve_unique_section (decl, reloc, flag_data_sections);
900 if (IN_NAMED_SECTION (decl))
901 return get_named_section (decl, NULL, reloc);
903 if (!DECL_THREAD_LOCAL_P (decl)
904 && !(prefer_noswitch_p && targetm.have_switchable_bss_sections)
905 && bss_initializer_p (decl))
907 if (!TREE_PUBLIC (decl))
908 return lcomm_section;
909 if (bss_noswitch_section)
910 return bss_noswitch_section;
913 return targetm.asm_out.select_section (decl, reloc, DECL_ALIGN (decl));
916 /* Return the block into which object_block DECL should be placed. */
918 static struct object_block *
919 get_block_for_decl (tree decl)
921 section *sect;
923 if (TREE_CODE (decl) == VAR_DECL)
925 /* The object must be defined in this translation unit. */
926 if (DECL_EXTERNAL (decl))
927 return NULL;
929 /* There's no point using object blocks for something that is
930 isolated by definition. */
931 if (DECL_ONE_ONLY (decl))
932 return NULL;
935 /* We can only calculate block offsets if the decl has a known
936 constant size. */
937 if (DECL_SIZE_UNIT (decl) == NULL)
938 return NULL;
939 if (!host_integerp (DECL_SIZE_UNIT (decl), 1))
940 return NULL;
942 /* Find out which section should contain DECL. We cannot put it into
943 an object block if it requires a standalone definition. */
944 if (TREE_CODE (decl) == VAR_DECL)
945 align_variable (decl, 0);
946 sect = get_variable_section (decl, true);
947 if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
948 return NULL;
950 return get_block_for_section (sect);
953 /* Make sure block symbol SYMBOL is in block BLOCK. */
955 static void
956 change_symbol_block (rtx symbol, struct object_block *block)
958 if (block != SYMBOL_REF_BLOCK (symbol))
960 gcc_assert (SYMBOL_REF_BLOCK_OFFSET (symbol) < 0);
961 SYMBOL_REF_BLOCK (symbol) = block;
965 /* Return true if it is possible to put DECL in an object_block. */
967 static bool
968 use_blocks_for_decl_p (tree decl)
970 /* Only data DECLs can be placed into object blocks. */
971 if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != CONST_DECL)
972 return false;
974 /* Detect decls created by dw2_force_const_mem. Such decls are
975 special because DECL_INITIAL doesn't specify the decl's true value.
976 dw2_output_indirect_constants will instead call assemble_variable
977 with dont_output_data set to 1 and then print the contents itself. */
978 if (DECL_INITIAL (decl) == decl)
979 return false;
981 return true;
984 /* Create the DECL_RTL for a VAR_DECL or FUNCTION_DECL. DECL should
985 have static storage duration. In other words, it should not be an
986 automatic variable, including PARM_DECLs.
988 There is, however, one exception: this function handles variables
989 explicitly placed in a particular register by the user.
991 This is never called for PARM_DECL nodes. */
993 void
994 make_decl_rtl (tree decl)
996 const char *name = 0;
997 int reg_number;
998 rtx x;
1000 /* Check that we are not being given an automatic variable. */
1001 gcc_assert (TREE_CODE (decl) != PARM_DECL
1002 && TREE_CODE (decl) != RESULT_DECL);
1004 /* A weak alias has TREE_PUBLIC set but not the other bits. */
1005 gcc_assert (TREE_CODE (decl) != VAR_DECL
1006 || TREE_STATIC (decl)
1007 || TREE_PUBLIC (decl)
1008 || DECL_EXTERNAL (decl)
1009 || DECL_REGISTER (decl));
1011 /* And that we were not given a type or a label. */
1012 gcc_assert (TREE_CODE (decl) != TYPE_DECL
1013 && TREE_CODE (decl) != LABEL_DECL);
1015 /* For a duplicate declaration, we can be called twice on the
1016 same DECL node. Don't discard the RTL already made. */
1017 if (DECL_RTL_SET_P (decl))
1019 /* If the old RTL had the wrong mode, fix the mode. */
1020 x = DECL_RTL (decl);
1021 if (GET_MODE (x) != DECL_MODE (decl))
1022 SET_DECL_RTL (decl, adjust_address_nv (x, DECL_MODE (decl), 0));
1024 if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1025 return;
1027 /* ??? Another way to do this would be to maintain a hashed
1028 table of such critters. Instead of adding stuff to a DECL
1029 to give certain attributes to it, we could use an external
1030 hash map from DECL to set of attributes. */
1032 /* Let the target reassign the RTL if it wants.
1033 This is necessary, for example, when one machine specific
1034 decl attribute overrides another. */
1035 targetm.encode_section_info (decl, DECL_RTL (decl), false);
1037 /* If the symbol has a SYMBOL_REF_BLOCK field, update it based
1038 on the new decl information. */
1039 if (MEM_P (x)
1040 && GET_CODE (XEXP (x, 0)) == SYMBOL_REF
1041 && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (x, 0)))
1042 change_symbol_block (XEXP (x, 0), get_block_for_decl (decl));
1044 /* Make this function static known to the mudflap runtime. */
1045 if (flag_mudflap && TREE_CODE (decl) == VAR_DECL)
1046 mudflap_enqueue_decl (decl);
1048 return;
1051 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
1053 if (name[0] != '*' && TREE_CODE (decl) != FUNCTION_DECL
1054 && DECL_REGISTER (decl))
1056 error ("register name not specified for %q+D", decl);
1058 else if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1060 const char *asmspec = name+1;
1061 reg_number = decode_reg_name (asmspec);
1062 /* First detect errors in declaring global registers. */
1063 if (reg_number == -1)
1064 error ("register name not specified for %q+D", decl);
1065 else if (reg_number < 0)
1066 error ("invalid register name for %q+D", decl);
1067 else if (TYPE_MODE (TREE_TYPE (decl)) == BLKmode)
1068 error ("data type of %q+D isn%'t suitable for a register",
1069 decl);
1070 else if (! HARD_REGNO_MODE_OK (reg_number, TYPE_MODE (TREE_TYPE (decl))))
1071 error ("register specified for %q+D isn%'t suitable for data type",
1072 decl);
1073 /* Now handle properly declared static register variables. */
1074 else
1076 int nregs;
1078 if (DECL_INITIAL (decl) != 0 && TREE_STATIC (decl))
1080 DECL_INITIAL (decl) = 0;
1081 error ("global register variable has initial value");
1083 if (TREE_THIS_VOLATILE (decl))
1084 warning (OPT_Wvolatile_register_var,
1085 "optimization may eliminate reads and/or "
1086 "writes to register variables");
1088 /* If the user specified one of the eliminables registers here,
1089 e.g., FRAME_POINTER_REGNUM, we don't want to get this variable
1090 confused with that register and be eliminated. This usage is
1091 somewhat suspect... */
1093 SET_DECL_RTL (decl, gen_rtx_raw_REG (DECL_MODE (decl), reg_number));
1094 ORIGINAL_REGNO (DECL_RTL (decl)) = reg_number;
1095 REG_USERVAR_P (DECL_RTL (decl)) = 1;
1097 if (TREE_STATIC (decl))
1099 /* Make this register global, so not usable for anything
1100 else. */
1101 #ifdef ASM_DECLARE_REGISTER_GLOBAL
1102 name = IDENTIFIER_POINTER (DECL_NAME (decl));
1103 ASM_DECLARE_REGISTER_GLOBAL (asm_out_file, decl, reg_number, name);
1104 #endif
1105 nregs = hard_regno_nregs[reg_number][DECL_MODE (decl)];
1106 while (nregs > 0)
1107 globalize_reg (reg_number + --nregs);
1110 /* As a register variable, it has no section. */
1111 return;
1114 /* Now handle ordinary static variables and functions (in memory).
1115 Also handle vars declared register invalidly. */
1116 else if (name[0] == '*')
1118 #ifdef REGISTER_PREFIX
1119 if (strlen (REGISTER_PREFIX) != 0)
1121 reg_number = decode_reg_name (name);
1122 if (reg_number >= 0 || reg_number == -3)
1123 error ("register name given for non-register variable %q+D", decl);
1125 #endif
1128 /* Specifying a section attribute on a variable forces it into a
1129 non-.bss section, and thus it cannot be common. */
1130 if (TREE_CODE (decl) == VAR_DECL
1131 && DECL_SECTION_NAME (decl) != NULL_TREE
1132 && DECL_INITIAL (decl) == NULL_TREE
1133 && DECL_COMMON (decl))
1134 DECL_COMMON (decl) = 0;
1136 /* Variables can't be both common and weak. */
1137 if (TREE_CODE (decl) == VAR_DECL && DECL_WEAK (decl))
1138 DECL_COMMON (decl) = 0;
1140 if (use_object_blocks_p () && use_blocks_for_decl_p (decl))
1141 x = create_block_symbol (name, get_block_for_decl (decl), -1);
1142 else
1143 x = gen_rtx_SYMBOL_REF (Pmode, name);
1144 SYMBOL_REF_WEAK (x) = DECL_WEAK (decl);
1145 SET_SYMBOL_REF_DECL (x, decl);
1147 x = gen_rtx_MEM (DECL_MODE (decl), x);
1148 if (TREE_CODE (decl) != FUNCTION_DECL)
1149 set_mem_attributes (x, decl, 1);
1150 SET_DECL_RTL (decl, x);
1152 /* Optionally set flags or add text to the name to record information
1153 such as that it is a function name.
1154 If the name is changed, the macro ASM_OUTPUT_LABELREF
1155 will have to know how to strip this information. */
1156 targetm.encode_section_info (decl, DECL_RTL (decl), true);
1158 /* Make this function static known to the mudflap runtime. */
1159 if (flag_mudflap && TREE_CODE (decl) == VAR_DECL)
1160 mudflap_enqueue_decl (decl);
1163 /* Make the rtl for variable VAR be volatile.
1164 Use this only for static variables. */
1166 void
1167 make_var_volatile (tree var)
1169 gcc_assert (MEM_P (DECL_RTL (var)));
1171 MEM_VOLATILE_P (DECL_RTL (var)) = 1;
1174 /* Output a string of literal assembler code
1175 for an `asm' keyword used between functions. */
1177 void
1178 assemble_asm (tree string)
1180 app_enable ();
1182 if (TREE_CODE (string) == ADDR_EXPR)
1183 string = TREE_OPERAND (string, 0);
1185 fprintf (asm_out_file, "\t%s\n", TREE_STRING_POINTER (string));
1188 /* Record an element in the table of global destructors. SYMBOL is
1189 a SYMBOL_REF of the function to be called; PRIORITY is a number
1190 between 0 and MAX_INIT_PRIORITY. */
1192 void
1193 default_stabs_asm_out_destructor (rtx symbol ATTRIBUTE_UNUSED,
1194 int priority ATTRIBUTE_UNUSED)
1196 #if defined DBX_DEBUGGING_INFO || defined XCOFF_DEBUGGING_INFO
1197 /* Tell GNU LD that this is part of the static destructor set.
1198 This will work for any system that uses stabs, most usefully
1199 aout systems. */
1200 dbxout_begin_simple_stabs ("___DTOR_LIST__", 22 /* N_SETT */);
1201 dbxout_stab_value_label (XSTR (symbol, 0));
1202 #else
1203 sorry ("global destructors not supported on this target");
1204 #endif
1207 void
1208 default_named_section_asm_out_destructor (rtx symbol, int priority)
1210 const char *section = ".dtors";
1211 char buf[16];
1213 /* ??? This only works reliably with the GNU linker. */
1214 if (priority != DEFAULT_INIT_PRIORITY)
1216 sprintf (buf, ".dtors.%.5u",
1217 /* Invert the numbering so the linker puts us in the proper
1218 order; constructors are run from right to left, and the
1219 linker sorts in increasing order. */
1220 MAX_INIT_PRIORITY - priority);
1221 section = buf;
1224 switch_to_section (get_section (section, SECTION_WRITE, NULL));
1225 assemble_align (POINTER_SIZE);
1226 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1229 #ifdef DTORS_SECTION_ASM_OP
1230 void
1231 default_dtor_section_asm_out_destructor (rtx symbol,
1232 int priority ATTRIBUTE_UNUSED)
1234 switch_to_section (dtors_section);
1235 assemble_align (POINTER_SIZE);
1236 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1238 #endif
1240 /* Likewise for global constructors. */
1242 void
1243 default_stabs_asm_out_constructor (rtx symbol ATTRIBUTE_UNUSED,
1244 int priority ATTRIBUTE_UNUSED)
1246 #if defined DBX_DEBUGGING_INFO || defined XCOFF_DEBUGGING_INFO
1247 /* Tell GNU LD that this is part of the static destructor set.
1248 This will work for any system that uses stabs, most usefully
1249 aout systems. */
1250 dbxout_begin_simple_stabs ("___CTOR_LIST__", 22 /* N_SETT */);
1251 dbxout_stab_value_label (XSTR (symbol, 0));
1252 #else
1253 sorry ("global constructors not supported on this target");
1254 #endif
1257 void
1258 default_named_section_asm_out_constructor (rtx symbol, int priority)
1260 const char *section = ".ctors";
1261 char buf[16];
1263 /* ??? This only works reliably with the GNU linker. */
1264 if (priority != DEFAULT_INIT_PRIORITY)
1266 sprintf (buf, ".ctors.%.5u",
1267 /* Invert the numbering so the linker puts us in the proper
1268 order; constructors are run from right to left, and the
1269 linker sorts in increasing order. */
1270 MAX_INIT_PRIORITY - priority);
1271 section = buf;
1274 switch_to_section (get_section (section, SECTION_WRITE, NULL));
1275 assemble_align (POINTER_SIZE);
1276 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1279 #ifdef CTORS_SECTION_ASM_OP
1280 void
1281 default_ctor_section_asm_out_constructor (rtx symbol,
1282 int priority ATTRIBUTE_UNUSED)
1284 switch_to_section (ctors_section);
1285 assemble_align (POINTER_SIZE);
1286 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1288 #endif
1290 /* CONSTANT_POOL_BEFORE_FUNCTION may be defined as an expression with
1291 a nonzero value if the constant pool should be output before the
1292 start of the function, or a zero value if the pool should output
1293 after the end of the function. The default is to put it before the
1294 start. */
1296 #ifndef CONSTANT_POOL_BEFORE_FUNCTION
1297 #define CONSTANT_POOL_BEFORE_FUNCTION 1
1298 #endif
1300 /* DECL is an object (either VAR_DECL or FUNCTION_DECL) which is going
1301 to be output to assembler.
1302 Set first_global_object_name and weak_global_object_name as appropriate. */
1304 void
1305 notice_global_symbol (tree decl)
1307 const char **type = &first_global_object_name;
1309 if (first_global_object_name
1310 || !TREE_PUBLIC (decl)
1311 || DECL_EXTERNAL (decl)
1312 || !DECL_NAME (decl)
1313 || (TREE_CODE (decl) != FUNCTION_DECL
1314 && (TREE_CODE (decl) != VAR_DECL
1315 || (DECL_COMMON (decl)
1316 && (DECL_INITIAL (decl) == 0
1317 || DECL_INITIAL (decl) == error_mark_node))))
1318 || !MEM_P (DECL_RTL (decl)))
1319 return;
1321 /* We win when global object is found, but it is useful to know about weak
1322 symbol as well so we can produce nicer unique names. */
1323 if (DECL_WEAK (decl) || DECL_ONE_ONLY (decl))
1324 type = &weak_global_object_name;
1326 if (!*type)
1328 const char *p;
1329 const char *name;
1330 rtx decl_rtl = DECL_RTL (decl);
1332 p = targetm.strip_name_encoding (XSTR (XEXP (decl_rtl, 0), 0));
1333 name = ggc_strdup (p);
1335 *type = name;
1339 /* Output assembler code for the constant pool of a function and associated
1340 with defining the name of the function. DECL describes the function.
1341 NAME is the function's name. For the constant pool, we use the current
1342 constant pool data. */
1344 void
1345 assemble_start_function (tree decl, const char *fnname)
1347 int align;
1348 char tmp_label[100];
1349 bool hot_label_written = false;
1351 cfun->unlikely_text_section_name = NULL;
1353 first_function_block_is_cold = false;
1354 if (flag_reorder_blocks_and_partition)
1356 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTB", const_labelno);
1357 cfun->hot_section_label = ggc_strdup (tmp_label);
1358 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDB", const_labelno);
1359 cfun->cold_section_label = ggc_strdup (tmp_label);
1360 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTE", const_labelno);
1361 cfun->hot_section_end_label = ggc_strdup (tmp_label);
1362 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDE", const_labelno);
1363 cfun->cold_section_end_label = ggc_strdup (tmp_label);
1364 const_labelno++;
1366 else
1368 cfun->hot_section_label = NULL;
1369 cfun->cold_section_label = NULL;
1370 cfun->hot_section_end_label = NULL;
1371 cfun->cold_section_end_label = NULL;
1374 /* The following code does not need preprocessing in the assembler. */
1376 app_disable ();
1378 if (CONSTANT_POOL_BEFORE_FUNCTION)
1379 output_constant_pool (fnname, decl);
1381 resolve_unique_section (decl, 0, flag_function_sections);
1383 /* Make sure the not and cold text (code) sections are properly
1384 aligned. This is necessary here in the case where the function
1385 has both hot and cold sections, because we don't want to re-set
1386 the alignment when the section switch happens mid-function. */
1388 if (flag_reorder_blocks_and_partition)
1390 switch_to_section (unlikely_text_section ());
1391 assemble_align (FUNCTION_BOUNDARY);
1392 ASM_OUTPUT_LABEL (asm_out_file, cfun->cold_section_label);
1394 /* When the function starts with a cold section, we need to explicitly
1395 align the hot section and write out the hot section label.
1396 But if the current function is a thunk, we do not have a CFG. */
1397 if (!current_function_is_thunk
1398 && BB_PARTITION (ENTRY_BLOCK_PTR->next_bb) == BB_COLD_PARTITION)
1400 switch_to_section (text_section);
1401 assemble_align (FUNCTION_BOUNDARY);
1402 ASM_OUTPUT_LABEL (asm_out_file, cfun->hot_section_label);
1403 hot_label_written = true;
1404 first_function_block_is_cold = true;
1407 else if (DECL_SECTION_NAME (decl))
1409 /* Calls to function_section rely on first_function_block_is_cold
1410 being accurate. The first block may be cold even if we aren't
1411 doing partitioning, if the entire function was decided by
1412 choose_function_section (predict.c) to be cold. */
1414 initialize_cold_section_name ();
1416 if (cfun->unlikely_text_section_name
1417 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
1418 cfun->unlikely_text_section_name) == 0)
1419 first_function_block_is_cold = true;
1422 in_cold_section_p = first_function_block_is_cold;
1424 /* Switch to the correct text section for the start of the function. */
1426 switch_to_section (function_section (decl));
1427 if (flag_reorder_blocks_and_partition
1428 && !hot_label_written)
1429 ASM_OUTPUT_LABEL (asm_out_file, cfun->hot_section_label);
1431 /* Tell assembler to move to target machine's alignment for functions. */
1432 align = floor_log2 (FUNCTION_BOUNDARY / BITS_PER_UNIT);
1433 if (align < force_align_functions_log)
1434 align = force_align_functions_log;
1435 if (align > 0)
1437 ASM_OUTPUT_ALIGN (asm_out_file, align);
1440 /* Handle a user-specified function alignment.
1441 Note that we still need to align to FUNCTION_BOUNDARY, as above,
1442 because ASM_OUTPUT_MAX_SKIP_ALIGN might not do any alignment at all. */
1443 if (align_functions_log > align
1444 && cfun->function_frequency != FUNCTION_FREQUENCY_UNLIKELY_EXECUTED)
1446 #ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
1447 ASM_OUTPUT_MAX_SKIP_ALIGN (asm_out_file,
1448 align_functions_log, align_functions - 1);
1449 #else
1450 ASM_OUTPUT_ALIGN (asm_out_file, align_functions_log);
1451 #endif
1454 #ifdef ASM_OUTPUT_FUNCTION_PREFIX
1455 ASM_OUTPUT_FUNCTION_PREFIX (asm_out_file, fnname);
1456 #endif
1458 (*debug_hooks->begin_function) (decl);
1460 /* Make function name accessible from other files, if appropriate. */
1462 if (TREE_PUBLIC (decl))
1464 notice_global_symbol (decl);
1466 globalize_decl (decl);
1468 maybe_assemble_visibility (decl);
1471 if (DECL_PRESERVE_P (decl))
1472 targetm.asm_out.mark_decl_preserved (fnname);
1474 /* Do any machine/system dependent processing of the function name. */
1475 #ifdef ASM_DECLARE_FUNCTION_NAME
1476 ASM_DECLARE_FUNCTION_NAME (asm_out_file, fnname, current_function_decl);
1477 #else
1478 /* Standard thing is just output label for the function. */
1479 ASM_OUTPUT_LABEL (asm_out_file, fnname);
1480 #endif /* ASM_DECLARE_FUNCTION_NAME */
1483 /* Output assembler code associated with defining the size of the
1484 function. DECL describes the function. NAME is the function's name. */
1486 void
1487 assemble_end_function (tree decl, const char *fnname ATTRIBUTE_UNUSED)
1489 #ifdef ASM_DECLARE_FUNCTION_SIZE
1490 /* We could have switched section in the middle of the function. */
1491 if (flag_reorder_blocks_and_partition)
1492 switch_to_section (function_section (decl));
1493 ASM_DECLARE_FUNCTION_SIZE (asm_out_file, fnname, decl);
1494 #endif
1495 if (! CONSTANT_POOL_BEFORE_FUNCTION)
1497 output_constant_pool (fnname, decl);
1498 switch_to_section (function_section (decl)); /* need to switch back */
1500 /* Output labels for end of hot/cold text sections (to be used by
1501 debug info.) */
1502 if (flag_reorder_blocks_and_partition)
1504 section *save_text_section;
1506 save_text_section = in_section;
1507 switch_to_section (unlikely_text_section ());
1508 ASM_OUTPUT_LABEL (asm_out_file, cfun->cold_section_end_label);
1509 if (first_function_block_is_cold)
1510 switch_to_section (text_section);
1511 else
1512 switch_to_section (function_section (decl));
1513 ASM_OUTPUT_LABEL (asm_out_file, cfun->hot_section_end_label);
1514 switch_to_section (save_text_section);
1518 /* Assemble code to leave SIZE bytes of zeros. */
1520 void
1521 assemble_zeros (unsigned HOST_WIDE_INT size)
1523 /* Do no output if -fsyntax-only. */
1524 if (flag_syntax_only)
1525 return;
1527 #ifdef ASM_NO_SKIP_IN_TEXT
1528 /* The `space' pseudo in the text section outputs nop insns rather than 0s,
1529 so we must output 0s explicitly in the text section. */
1530 if (ASM_NO_SKIP_IN_TEXT && (in_section->common.flags & SECTION_CODE) != 0)
1532 unsigned HOST_WIDE_INT i;
1533 for (i = 0; i < size; i++)
1534 assemble_integer (const0_rtx, 1, BITS_PER_UNIT, 1);
1536 else
1537 #endif
1538 if (size > 0)
1539 ASM_OUTPUT_SKIP (asm_out_file, size);
1542 /* Assemble an alignment pseudo op for an ALIGN-bit boundary. */
1544 void
1545 assemble_align (int align)
1547 if (align > BITS_PER_UNIT)
1549 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
1553 /* Assemble a string constant with the specified C string as contents. */
1555 void
1556 assemble_string (const char *p, int size)
1558 int pos = 0;
1559 int maximum = 2000;
1561 /* If the string is very long, split it up. */
1563 while (pos < size)
1565 int thissize = size - pos;
1566 if (thissize > maximum)
1567 thissize = maximum;
1569 ASM_OUTPUT_ASCII (asm_out_file, p, thissize);
1571 pos += thissize;
1572 p += thissize;
1577 /* A noswitch_section_callback for lcomm_section. */
1579 static bool
1580 emit_local (tree decl ATTRIBUTE_UNUSED,
1581 const char *name ATTRIBUTE_UNUSED,
1582 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1583 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1585 #if defined ASM_OUTPUT_ALIGNED_DECL_LOCAL
1586 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, decl, name,
1587 size, DECL_ALIGN (decl));
1588 return true;
1589 #elif defined ASM_OUTPUT_ALIGNED_LOCAL
1590 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, DECL_ALIGN (decl));
1591 return true;
1592 #else
1593 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
1594 return false;
1595 #endif
1598 /* A noswitch_section_callback for bss_noswitch_section. */
1600 #if defined ASM_OUTPUT_ALIGNED_BSS || defined ASM_OUTPUT_BSS
1601 static bool
1602 emit_bss (tree decl ATTRIBUTE_UNUSED,
1603 const char *name ATTRIBUTE_UNUSED,
1604 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1605 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1607 #if defined ASM_OUTPUT_ALIGNED_BSS
1608 ASM_OUTPUT_ALIGNED_BSS (asm_out_file, decl, name, size, DECL_ALIGN (decl));
1609 return true;
1610 #else
1611 ASM_OUTPUT_BSS (asm_out_file, decl, name, size, rounded);
1612 return false;
1613 #endif
1615 #endif
1617 /* A noswitch_section_callback for comm_section. */
1619 static bool
1620 emit_common (tree decl ATTRIBUTE_UNUSED,
1621 const char *name ATTRIBUTE_UNUSED,
1622 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1623 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1625 #if defined ASM_OUTPUT_ALIGNED_DECL_COMMON
1626 ASM_OUTPUT_ALIGNED_DECL_COMMON (asm_out_file, decl, name,
1627 size, DECL_ALIGN (decl));
1628 return true;
1629 #elif defined ASM_OUTPUT_ALIGNED_COMMON
1630 ASM_OUTPUT_ALIGNED_COMMON (asm_out_file, name, size, DECL_ALIGN (decl));
1631 return true;
1632 #else
1633 ASM_OUTPUT_COMMON (asm_out_file, name, size, rounded);
1634 return false;
1635 #endif
1638 /* A noswitch_section_callback for tls_comm_section. */
1640 static bool
1641 emit_tls_common (tree decl ATTRIBUTE_UNUSED,
1642 const char *name ATTRIBUTE_UNUSED,
1643 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1644 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1646 #ifdef ASM_OUTPUT_TLS_COMMON
1647 ASM_OUTPUT_TLS_COMMON (asm_out_file, decl, name, size);
1648 return true;
1649 #else
1650 sorry ("thread-local COMMON data not implemented");
1651 return true;
1652 #endif
1655 /* Assemble DECL given that it belongs in SECTION_NOSWITCH section SECT.
1656 NAME is the name of DECL's SYMBOL_REF. */
1658 static void
1659 assemble_noswitch_variable (tree decl, const char *name, section *sect)
1661 unsigned HOST_WIDE_INT size, rounded;
1663 size = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
1664 rounded = size;
1666 /* Don't allocate zero bytes of common,
1667 since that means "undefined external" in the linker. */
1668 if (size == 0)
1669 rounded = 1;
1671 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
1672 so that each uninitialized object starts on such a boundary. */
1673 rounded += (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1;
1674 rounded = (rounded / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
1675 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
1677 if (!sect->noswitch.callback (decl, name, size, rounded)
1678 && (unsigned HOST_WIDE_INT) DECL_ALIGN_UNIT (decl) > rounded)
1679 warning (0, "requested alignment for %q+D is greater than "
1680 "implemented alignment of %wu", decl, rounded);
1683 /* A subroutine of assemble_variable. Output the label and contents of
1684 DECL, whose address is a SYMBOL_REF with name NAME. DONT_OUTPUT_DATA
1685 is as for assemble_variable. */
1687 static void
1688 assemble_variable_contents (tree decl, const char *name,
1689 bool dont_output_data)
1691 /* Do any machine/system dependent processing of the object. */
1692 #ifdef ASM_DECLARE_OBJECT_NAME
1693 last_assemble_variable_decl = decl;
1694 ASM_DECLARE_OBJECT_NAME (asm_out_file, name, decl);
1695 #else
1696 /* Standard thing is just output label for the object. */
1697 ASM_OUTPUT_LABEL (asm_out_file, name);
1698 #endif /* ASM_DECLARE_OBJECT_NAME */
1700 if (!dont_output_data)
1702 if (DECL_INITIAL (decl)
1703 && DECL_INITIAL (decl) != error_mark_node
1704 && !initializer_zerop (DECL_INITIAL (decl)))
1705 /* Output the actual data. */
1706 output_constant (DECL_INITIAL (decl),
1707 tree_low_cst (DECL_SIZE_UNIT (decl), 1),
1708 DECL_ALIGN (decl));
1709 else
1710 /* Leave space for it. */
1711 assemble_zeros (tree_low_cst (DECL_SIZE_UNIT (decl), 1));
1715 /* Assemble everything that is needed for a variable or function declaration.
1716 Not used for automatic variables, and not used for function definitions.
1717 Should not be called for variables of incomplete structure type.
1719 TOP_LEVEL is nonzero if this variable has file scope.
1720 AT_END is nonzero if this is the special handling, at end of compilation,
1721 to define things that have had only tentative definitions.
1722 DONT_OUTPUT_DATA if nonzero means don't actually output the
1723 initial value (that will be done by the caller). */
1725 void
1726 assemble_variable (tree decl, int top_level ATTRIBUTE_UNUSED,
1727 int at_end ATTRIBUTE_UNUSED, int dont_output_data)
1729 const char *name;
1730 rtx decl_rtl, symbol;
1731 section *sect;
1733 if (lang_hooks.decls.prepare_assemble_variable)
1734 lang_hooks.decls.prepare_assemble_variable (decl);
1736 last_assemble_variable_decl = 0;
1738 /* Normally no need to say anything here for external references,
1739 since assemble_external is called by the language-specific code
1740 when a declaration is first seen. */
1742 if (DECL_EXTERNAL (decl))
1743 return;
1745 /* Output no assembler code for a function declaration.
1746 Only definitions of functions output anything. */
1748 if (TREE_CODE (decl) == FUNCTION_DECL)
1749 return;
1751 /* Do nothing for global register variables. */
1752 if (DECL_RTL_SET_P (decl) && REG_P (DECL_RTL (decl)))
1754 TREE_ASM_WRITTEN (decl) = 1;
1755 return;
1758 /* If type was incomplete when the variable was declared,
1759 see if it is complete now. */
1761 if (DECL_SIZE (decl) == 0)
1762 layout_decl (decl, 0);
1764 /* Still incomplete => don't allocate it; treat the tentative defn
1765 (which is what it must have been) as an `extern' reference. */
1767 if (!dont_output_data && DECL_SIZE (decl) == 0)
1769 error ("storage size of %q+D isn%'t known", decl);
1770 TREE_ASM_WRITTEN (decl) = 1;
1771 return;
1774 /* The first declaration of a variable that comes through this function
1775 decides whether it is global (in C, has external linkage)
1776 or local (in C, has internal linkage). So do nothing more
1777 if this function has already run. */
1779 if (TREE_ASM_WRITTEN (decl))
1780 return;
1782 /* Make sure targetm.encode_section_info is invoked before we set
1783 ASM_WRITTEN. */
1784 decl_rtl = DECL_RTL (decl);
1786 TREE_ASM_WRITTEN (decl) = 1;
1788 /* Do no output if -fsyntax-only. */
1789 if (flag_syntax_only)
1790 return;
1792 app_disable ();
1794 if (! dont_output_data
1795 && ! host_integerp (DECL_SIZE_UNIT (decl), 1))
1797 error ("size of variable %q+D is too large", decl);
1798 return;
1801 gcc_assert (MEM_P (decl_rtl));
1802 gcc_assert (GET_CODE (XEXP (decl_rtl, 0)) == SYMBOL_REF);
1803 symbol = XEXP (decl_rtl, 0);
1804 name = XSTR (symbol, 0);
1805 if (TREE_PUBLIC (decl) && DECL_NAME (decl))
1806 notice_global_symbol (decl);
1808 /* Compute the alignment of this data. */
1810 align_variable (decl, dont_output_data);
1811 set_mem_align (decl_rtl, DECL_ALIGN (decl));
1813 if (TREE_PUBLIC (decl))
1814 maybe_assemble_visibility (decl);
1816 if (DECL_PRESERVE_P (decl))
1817 targetm.asm_out.mark_decl_preserved (name);
1819 /* First make the assembler name(s) global if appropriate. */
1820 sect = get_variable_section (decl, false);
1821 if (TREE_PUBLIC (decl)
1822 && DECL_NAME (decl)
1823 && (sect->common.flags & SECTION_COMMON) == 0)
1824 globalize_decl (decl);
1826 /* Output any data that we will need to use the address of. */
1827 if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node)
1828 output_addressed_constants (DECL_INITIAL (decl));
1830 /* dbxout.c needs to know this. */
1831 if (sect && (sect->common.flags & SECTION_CODE) != 0)
1832 DECL_IN_TEXT_SECTION (decl) = 1;
1834 /* If the decl is part of an object_block, make sure that the decl
1835 has been positioned within its block, but do not write out its
1836 definition yet. output_object_blocks will do that later. */
1837 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
1839 gcc_assert (!dont_output_data);
1840 place_block_symbol (symbol);
1842 else if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
1843 assemble_noswitch_variable (decl, name, sect);
1844 else
1846 switch_to_section (sect);
1847 if (DECL_ALIGN (decl) > BITS_PER_UNIT)
1848 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (DECL_ALIGN_UNIT (decl)));
1849 assemble_variable_contents (decl, name, dont_output_data);
1853 /* Return 1 if type TYPE contains any pointers. */
1855 static int
1856 contains_pointers_p (tree type)
1858 switch (TREE_CODE (type))
1860 case POINTER_TYPE:
1861 case REFERENCE_TYPE:
1862 /* I'm not sure whether OFFSET_TYPE needs this treatment,
1863 so I'll play safe and return 1. */
1864 case OFFSET_TYPE:
1865 return 1;
1867 case RECORD_TYPE:
1868 case UNION_TYPE:
1869 case QUAL_UNION_TYPE:
1871 tree fields;
1872 /* For a type that has fields, see if the fields have pointers. */
1873 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
1874 if (TREE_CODE (fields) == FIELD_DECL
1875 && contains_pointers_p (TREE_TYPE (fields)))
1876 return 1;
1877 return 0;
1880 case ARRAY_TYPE:
1881 /* An array type contains pointers if its element type does. */
1882 return contains_pointers_p (TREE_TYPE (type));
1884 default:
1885 return 0;
1889 /* In unit-at-a-time mode, we delay assemble_external processing until
1890 the compilation unit is finalized. This is the best we can do for
1891 right now (i.e. stage 3 of GCC 4.0) - the right thing is to delay
1892 it all the way to final. See PR 17982 for further discussion. */
1893 static GTY(()) tree pending_assemble_externals;
1895 #ifdef ASM_OUTPUT_EXTERNAL
1896 /* True if DECL is a function decl for which no out-of-line copy exists.
1897 It is assumed that DECL's assembler name has been set. */
1899 static bool
1900 incorporeal_function_p (tree decl)
1902 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1904 const char *name;
1906 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
1907 && DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA)
1908 return true;
1910 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
1911 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
1912 return true;
1914 return false;
1917 /* Actually do the tests to determine if this is necessary, and invoke
1918 ASM_OUTPUT_EXTERNAL. */
1919 static void
1920 assemble_external_real (tree decl)
1922 rtx rtl = DECL_RTL (decl);
1924 if (MEM_P (rtl) && GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF
1925 && !SYMBOL_REF_USED (XEXP (rtl, 0))
1926 && !incorporeal_function_p (decl))
1928 /* Some systems do require some output. */
1929 SYMBOL_REF_USED (XEXP (rtl, 0)) = 1;
1930 ASM_OUTPUT_EXTERNAL (asm_out_file, decl, XSTR (XEXP (rtl, 0), 0));
1933 #endif
1935 void
1936 process_pending_assemble_externals (void)
1938 #ifdef ASM_OUTPUT_EXTERNAL
1939 tree list;
1940 for (list = pending_assemble_externals; list; list = TREE_CHAIN (list))
1941 assemble_external_real (TREE_VALUE (list));
1943 pending_assemble_externals = 0;
1944 #endif
1947 /* Output something to declare an external symbol to the assembler.
1948 (Most assemblers don't need this, so we normally output nothing.)
1949 Do nothing if DECL is not external. */
1951 void
1952 assemble_external (tree decl ATTRIBUTE_UNUSED)
1954 /* Because most platforms do not define ASM_OUTPUT_EXTERNAL, the
1955 main body of this code is only rarely exercised. To provide some
1956 testing, on all platforms, we make sure that the ASM_OUT_FILE is
1957 open. If it's not, we should not be calling this function. */
1958 gcc_assert (asm_out_file);
1960 #ifdef ASM_OUTPUT_EXTERNAL
1961 if (!DECL_P (decl) || !DECL_EXTERNAL (decl) || !TREE_PUBLIC (decl))
1962 return;
1964 if (flag_unit_at_a_time)
1965 pending_assemble_externals = tree_cons (0, decl,
1966 pending_assemble_externals);
1967 else
1968 assemble_external_real (decl);
1969 #endif
1972 /* Similar, for calling a library function FUN. */
1974 void
1975 assemble_external_libcall (rtx fun)
1977 /* Declare library function name external when first used, if nec. */
1978 if (! SYMBOL_REF_USED (fun))
1980 SYMBOL_REF_USED (fun) = 1;
1981 targetm.asm_out.external_libcall (fun);
1985 /* Assemble a label named NAME. */
1987 void
1988 assemble_label (const char *name)
1990 ASM_OUTPUT_LABEL (asm_out_file, name);
1993 /* Set the symbol_referenced flag for ID. */
1994 void
1995 mark_referenced (tree id)
1997 TREE_SYMBOL_REFERENCED (id) = 1;
2000 /* Set the symbol_referenced flag for DECL and notify callgraph. */
2001 void
2002 mark_decl_referenced (tree decl)
2004 if (TREE_CODE (decl) == FUNCTION_DECL)
2006 /* Extern inline functions don't become needed when referenced.
2007 If we know a method will be emitted in other TU and no new
2008 functions can be marked reachable, just use the external
2009 definition. */
2010 struct cgraph_node *node = cgraph_node (decl);
2011 if (!DECL_EXTERNAL (decl)
2012 && (!node->local.vtable_method || !cgraph_global_info_ready
2013 || !node->local.finalized))
2014 cgraph_mark_needed_node (node);
2016 else if (TREE_CODE (decl) == VAR_DECL)
2018 struct cgraph_varpool_node *node = cgraph_varpool_node (decl);
2019 cgraph_varpool_mark_needed_node (node);
2020 /* C++ frontend use mark_decl_references to force COMDAT variables
2021 to be output that might appear dead otherwise. */
2022 node->force_output = true;
2024 /* else do nothing - we can get various sorts of CST nodes here,
2025 which do not need to be marked. */
2029 /* Follow the IDENTIFIER_TRANSPARENT_ALIAS chain starting at *ALIAS
2030 until we find an identifier that is not itself a transparent alias.
2031 Modify the alias passed to it by reference (and all aliases on the
2032 way to the ultimate target), such that they do not have to be
2033 followed again, and return the ultimate target of the alias
2034 chain. */
2036 static inline tree
2037 ultimate_transparent_alias_target (tree *alias)
2039 tree target = *alias;
2041 if (IDENTIFIER_TRANSPARENT_ALIAS (target))
2043 gcc_assert (TREE_CHAIN (target));
2044 target = ultimate_transparent_alias_target (&TREE_CHAIN (target));
2045 gcc_assert (! IDENTIFIER_TRANSPARENT_ALIAS (target)
2046 && ! TREE_CHAIN (target));
2047 *alias = target;
2050 return target;
2053 /* Output to FILE (an assembly file) a reference to NAME. If NAME
2054 starts with a *, the rest of NAME is output verbatim. Otherwise
2055 NAME is transformed in a target-specific way (usually by the
2056 addition of an underscore). */
2058 void
2059 assemble_name_raw (FILE *file, const char *name)
2061 if (name[0] == '*')
2062 fputs (&name[1], file);
2063 else
2064 ASM_OUTPUT_LABELREF (file, name);
2067 /* Like assemble_name_raw, but should be used when NAME might refer to
2068 an entity that is also represented as a tree (like a function or
2069 variable). If NAME does refer to such an entity, that entity will
2070 be marked as referenced. */
2072 void
2073 assemble_name (FILE *file, const char *name)
2075 const char *real_name;
2076 tree id;
2078 real_name = targetm.strip_name_encoding (name);
2080 id = maybe_get_identifier (real_name);
2081 if (id)
2083 tree id_orig = id;
2085 mark_referenced (id);
2086 ultimate_transparent_alias_target (&id);
2087 if (id != id_orig)
2088 name = IDENTIFIER_POINTER (id);
2089 gcc_assert (! TREE_CHAIN (id));
2092 assemble_name_raw (file, name);
2095 /* Allocate SIZE bytes writable static space with a gensym name
2096 and return an RTX to refer to its address. */
2099 assemble_static_space (unsigned HOST_WIDE_INT size)
2101 char name[12];
2102 const char *namestring;
2103 rtx x;
2105 ASM_GENERATE_INTERNAL_LABEL (name, "LF", const_labelno);
2106 ++const_labelno;
2107 namestring = ggc_strdup (name);
2109 x = gen_rtx_SYMBOL_REF (Pmode, namestring);
2110 SYMBOL_REF_FLAGS (x) = SYMBOL_FLAG_LOCAL;
2112 #ifdef ASM_OUTPUT_ALIGNED_DECL_LOCAL
2113 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, NULL_TREE, name, size,
2114 BIGGEST_ALIGNMENT);
2115 #else
2116 #ifdef ASM_OUTPUT_ALIGNED_LOCAL
2117 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, BIGGEST_ALIGNMENT);
2118 #else
2120 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
2121 so that each uninitialized object starts on such a boundary. */
2122 /* Variable `rounded' might or might not be used in ASM_OUTPUT_LOCAL. */
2123 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED
2124 = ((size + (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1)
2125 / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
2126 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
2127 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
2129 #endif
2130 #endif
2131 return x;
2134 /* Assemble the static constant template for function entry trampolines.
2135 This is done at most once per compilation.
2136 Returns an RTX for the address of the template. */
2138 static GTY(()) rtx initial_trampoline;
2140 #ifdef TRAMPOLINE_TEMPLATE
2142 assemble_trampoline_template (void)
2144 char label[256];
2145 const char *name;
2146 int align;
2147 rtx symbol;
2149 if (initial_trampoline)
2150 return initial_trampoline;
2152 /* By default, put trampoline templates in read-only data section. */
2154 #ifdef TRAMPOLINE_SECTION
2155 switch_to_section (TRAMPOLINE_SECTION);
2156 #else
2157 switch_to_section (readonly_data_section);
2158 #endif
2160 /* Write the assembler code to define one. */
2161 align = floor_log2 (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
2162 if (align > 0)
2164 ASM_OUTPUT_ALIGN (asm_out_file, align);
2167 targetm.asm_out.internal_label (asm_out_file, "LTRAMP", 0);
2168 TRAMPOLINE_TEMPLATE (asm_out_file);
2170 /* Record the rtl to refer to it. */
2171 ASM_GENERATE_INTERNAL_LABEL (label, "LTRAMP", 0);
2172 name = ggc_strdup (label);
2173 symbol = gen_rtx_SYMBOL_REF (Pmode, name);
2174 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_LOCAL;
2176 initial_trampoline = gen_rtx_MEM (BLKmode, symbol);
2177 set_mem_align (initial_trampoline, TRAMPOLINE_ALIGNMENT);
2179 return initial_trampoline;
2181 #endif
2183 /* A and B are either alignments or offsets. Return the minimum alignment
2184 that may be assumed after adding the two together. */
2186 static inline unsigned
2187 min_align (unsigned int a, unsigned int b)
2189 return (a | b) & -(a | b);
2192 /* Return the assembler directive for creating a given kind of integer
2193 object. SIZE is the number of bytes in the object and ALIGNED_P
2194 indicates whether it is known to be aligned. Return NULL if the
2195 assembly dialect has no such directive.
2197 The returned string should be printed at the start of a new line and
2198 be followed immediately by the object's initial value. */
2200 const char *
2201 integer_asm_op (int size, int aligned_p)
2203 struct asm_int_op *ops;
2205 if (aligned_p)
2206 ops = &targetm.asm_out.aligned_op;
2207 else
2208 ops = &targetm.asm_out.unaligned_op;
2210 switch (size)
2212 case 1:
2213 return targetm.asm_out.byte_op;
2214 case 2:
2215 return ops->hi;
2216 case 4:
2217 return ops->si;
2218 case 8:
2219 return ops->di;
2220 case 16:
2221 return ops->ti;
2222 default:
2223 return NULL;
2227 /* Use directive OP to assemble an integer object X. Print OP at the
2228 start of the line, followed immediately by the value of X. */
2230 void
2231 assemble_integer_with_op (const char *op, rtx x)
2233 fputs (op, asm_out_file);
2234 output_addr_const (asm_out_file, x);
2235 fputc ('\n', asm_out_file);
2238 /* The default implementation of the asm_out.integer target hook. */
2240 bool
2241 default_assemble_integer (rtx x ATTRIBUTE_UNUSED,
2242 unsigned int size ATTRIBUTE_UNUSED,
2243 int aligned_p ATTRIBUTE_UNUSED)
2245 const char *op = integer_asm_op (size, aligned_p);
2246 /* Avoid GAS bugs for large values. Specifically negative values whose
2247 absolute value fits in a bfd_vma, but not in a bfd_signed_vma. */
2248 if (size > UNITS_PER_WORD && size > POINTER_SIZE / BITS_PER_UNIT)
2249 return false;
2250 return op && (assemble_integer_with_op (op, x), true);
2253 /* Assemble the integer constant X into an object of SIZE bytes. ALIGN is
2254 the alignment of the integer in bits. Return 1 if we were able to output
2255 the constant, otherwise 0. We must be able to output the constant,
2256 if FORCE is nonzero. */
2258 bool
2259 assemble_integer (rtx x, unsigned int size, unsigned int align, int force)
2261 int aligned_p;
2263 aligned_p = (align >= MIN (size * BITS_PER_UNIT, BIGGEST_ALIGNMENT));
2265 /* See if the target hook can handle this kind of object. */
2266 if (targetm.asm_out.integer (x, size, aligned_p))
2267 return true;
2269 /* If the object is a multi-byte one, try splitting it up. Split
2270 it into words it if is multi-word, otherwise split it into bytes. */
2271 if (size > 1)
2273 enum machine_mode omode, imode;
2274 unsigned int subalign;
2275 unsigned int subsize, i;
2277 subsize = size > UNITS_PER_WORD? UNITS_PER_WORD : 1;
2278 subalign = MIN (align, subsize * BITS_PER_UNIT);
2279 omode = mode_for_size (subsize * BITS_PER_UNIT, MODE_INT, 0);
2280 imode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
2282 for (i = 0; i < size; i += subsize)
2284 rtx partial = simplify_subreg (omode, x, imode, i);
2285 if (!partial || !assemble_integer (partial, subsize, subalign, 0))
2286 break;
2288 if (i == size)
2289 return true;
2291 /* If we've printed some of it, but not all of it, there's no going
2292 back now. */
2293 gcc_assert (!i);
2296 gcc_assert (!force);
2298 return false;
2301 void
2302 assemble_real (REAL_VALUE_TYPE d, enum machine_mode mode, unsigned int align)
2304 long data[4] = {0, 0, 0, 0};
2305 int i;
2306 int bitsize, nelts, nunits, units_per;
2308 /* This is hairy. We have a quantity of known size. real_to_target
2309 will put it into an array of *host* longs, 32 bits per element
2310 (even if long is more than 32 bits). We need to determine the
2311 number of array elements that are occupied (nelts) and the number
2312 of *target* min-addressable units that will be occupied in the
2313 object file (nunits). We cannot assume that 32 divides the
2314 mode's bitsize (size * BITS_PER_UNIT) evenly.
2316 size * BITS_PER_UNIT is used here to make sure that padding bits
2317 (which might appear at either end of the value; real_to_target
2318 will include the padding bits in its output array) are included. */
2320 nunits = GET_MODE_SIZE (mode);
2321 bitsize = nunits * BITS_PER_UNIT;
2322 nelts = CEIL (bitsize, 32);
2323 units_per = 32 / BITS_PER_UNIT;
2325 real_to_target (data, &d, mode);
2327 /* Put out the first word with the specified alignment. */
2328 assemble_integer (GEN_INT (data[0]), MIN (nunits, units_per), align, 1);
2329 nunits -= units_per;
2331 /* Subsequent words need only 32-bit alignment. */
2332 align = min_align (align, 32);
2334 for (i = 1; i < nelts; i++)
2336 assemble_integer (GEN_INT (data[i]), MIN (nunits, units_per), align, 1);
2337 nunits -= units_per;
2341 /* Given an expression EXP with a constant value,
2342 reduce it to the sum of an assembler symbol and an integer.
2343 Store them both in the structure *VALUE.
2344 EXP must be reducible. */
2346 struct addr_const GTY(())
2348 rtx base;
2349 HOST_WIDE_INT offset;
2352 static void
2353 decode_addr_const (tree exp, struct addr_const *value)
2355 tree target = TREE_OPERAND (exp, 0);
2356 int offset = 0;
2357 rtx x;
2359 while (1)
2361 if (TREE_CODE (target) == COMPONENT_REF
2362 && host_integerp (byte_position (TREE_OPERAND (target, 1)), 0))
2365 offset += int_byte_position (TREE_OPERAND (target, 1));
2366 target = TREE_OPERAND (target, 0);
2368 else if (TREE_CODE (target) == ARRAY_REF
2369 || TREE_CODE (target) == ARRAY_RANGE_REF)
2371 offset += (tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (target)), 1)
2372 * tree_low_cst (TREE_OPERAND (target, 1), 0));
2373 target = TREE_OPERAND (target, 0);
2375 else
2376 break;
2379 switch (TREE_CODE (target))
2381 case VAR_DECL:
2382 case FUNCTION_DECL:
2383 x = DECL_RTL (target);
2384 break;
2386 case LABEL_DECL:
2387 x = gen_rtx_MEM (FUNCTION_MODE,
2388 gen_rtx_LABEL_REF (Pmode, force_label_rtx (target)));
2389 break;
2391 case REAL_CST:
2392 case STRING_CST:
2393 case COMPLEX_CST:
2394 case CONSTRUCTOR:
2395 case INTEGER_CST:
2396 x = output_constant_def (target, 1);
2397 break;
2399 default:
2400 gcc_unreachable ();
2403 gcc_assert (MEM_P (x));
2404 x = XEXP (x, 0);
2406 value->base = x;
2407 value->offset = offset;
2410 /* Uniquize all constants that appear in memory.
2411 Each constant in memory thus far output is recorded
2412 in `const_desc_table'. */
2414 struct constant_descriptor_tree GTY(())
2416 /* A MEM for the constant. */
2417 rtx rtl;
2419 /* The value of the constant. */
2420 tree value;
2422 /* Hash of value. Computing the hash from value each time
2423 hashfn is called can't work properly, as that means recursive
2424 use of the hash table during hash table expansion. */
2425 hashval_t hash;
2428 static GTY((param_is (struct constant_descriptor_tree)))
2429 htab_t const_desc_htab;
2431 static struct constant_descriptor_tree * build_constant_desc (tree);
2432 static void maybe_output_constant_def_contents (struct constant_descriptor_tree *, int);
2434 /* Compute a hash code for a constant expression. */
2436 static hashval_t
2437 const_desc_hash (const void *ptr)
2439 return ((struct constant_descriptor_tree *)ptr)->hash;
2442 static hashval_t
2443 const_hash_1 (const tree exp)
2445 const char *p;
2446 hashval_t hi;
2447 int len, i;
2448 enum tree_code code = TREE_CODE (exp);
2450 /* Either set P and LEN to the address and len of something to hash and
2451 exit the switch or return a value. */
2453 switch (code)
2455 case INTEGER_CST:
2456 p = (char *) &TREE_INT_CST (exp);
2457 len = sizeof TREE_INT_CST (exp);
2458 break;
2460 case REAL_CST:
2461 return real_hash (TREE_REAL_CST_PTR (exp));
2463 case STRING_CST:
2464 p = TREE_STRING_POINTER (exp);
2465 len = TREE_STRING_LENGTH (exp);
2466 break;
2468 case COMPLEX_CST:
2469 return (const_hash_1 (TREE_REALPART (exp)) * 5
2470 + const_hash_1 (TREE_IMAGPART (exp)));
2472 case CONSTRUCTOR:
2474 unsigned HOST_WIDE_INT idx;
2475 tree value;
2477 hi = 5 + int_size_in_bytes (TREE_TYPE (exp));
2479 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
2480 if (value)
2481 hi = hi * 603 + const_hash_1 (value);
2483 return hi;
2486 case ADDR_EXPR:
2487 case FDESC_EXPR:
2489 struct addr_const value;
2491 decode_addr_const (exp, &value);
2492 switch (GET_CODE (value.base))
2494 case SYMBOL_REF:
2495 /* Don't hash the address of the SYMBOL_REF;
2496 only use the offset and the symbol name. */
2497 hi = value.offset;
2498 p = XSTR (value.base, 0);
2499 for (i = 0; p[i] != 0; i++)
2500 hi = ((hi * 613) + (unsigned) (p[i]));
2501 break;
2503 case LABEL_REF:
2504 hi = value.offset + CODE_LABEL_NUMBER (XEXP (value.base, 0)) * 13;
2505 break;
2507 default:
2508 gcc_unreachable ();
2511 return hi;
2513 case PLUS_EXPR:
2514 case MINUS_EXPR:
2515 return (const_hash_1 (TREE_OPERAND (exp, 0)) * 9
2516 + const_hash_1 (TREE_OPERAND (exp, 1)));
2518 case NOP_EXPR:
2519 case CONVERT_EXPR:
2520 case NON_LVALUE_EXPR:
2521 return const_hash_1 (TREE_OPERAND (exp, 0)) * 7 + 2;
2523 default:
2524 /* A language specific constant. Just hash the code. */
2525 return code;
2528 /* Compute hashing function. */
2529 hi = len;
2530 for (i = 0; i < len; i++)
2531 hi = ((hi * 613) + (unsigned) (p[i]));
2533 return hi;
2536 /* Wrapper of compare_constant, for the htab interface. */
2537 static int
2538 const_desc_eq (const void *p1, const void *p2)
2540 const struct constant_descriptor_tree *c1 = p1;
2541 const struct constant_descriptor_tree *c2 = p2;
2542 if (c1->hash != c2->hash)
2543 return 0;
2544 return compare_constant (c1->value, c2->value);
2547 /* Compare t1 and t2, and return 1 only if they are known to result in
2548 the same bit pattern on output. */
2550 static int
2551 compare_constant (const tree t1, const tree t2)
2553 enum tree_code typecode;
2555 if (t1 == NULL_TREE)
2556 return t2 == NULL_TREE;
2557 if (t2 == NULL_TREE)
2558 return 0;
2560 if (TREE_CODE (t1) != TREE_CODE (t2))
2561 return 0;
2563 switch (TREE_CODE (t1))
2565 case INTEGER_CST:
2566 /* Integer constants are the same only if the same width of type. */
2567 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
2568 return 0;
2569 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
2570 return 0;
2571 return tree_int_cst_equal (t1, t2);
2573 case REAL_CST:
2574 /* Real constants are the same only if the same width of type. */
2575 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
2576 return 0;
2578 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
2580 case STRING_CST:
2581 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
2582 return 0;
2584 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
2585 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
2586 TREE_STRING_LENGTH (t1)));
2588 case COMPLEX_CST:
2589 return (compare_constant (TREE_REALPART (t1), TREE_REALPART (t2))
2590 && compare_constant (TREE_IMAGPART (t1), TREE_IMAGPART (t2)));
2592 case CONSTRUCTOR:
2594 VEC(constructor_elt, gc) *v1, *v2;
2595 unsigned HOST_WIDE_INT idx;
2597 typecode = TREE_CODE (TREE_TYPE (t1));
2598 if (typecode != TREE_CODE (TREE_TYPE (t2)))
2599 return 0;
2601 if (typecode == ARRAY_TYPE)
2603 HOST_WIDE_INT size_1 = int_size_in_bytes (TREE_TYPE (t1));
2604 /* For arrays, check that the sizes all match. */
2605 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))
2606 || size_1 == -1
2607 || size_1 != int_size_in_bytes (TREE_TYPE (t2)))
2608 return 0;
2610 else
2612 /* For record and union constructors, require exact type
2613 equality. */
2614 if (TREE_TYPE (t1) != TREE_TYPE (t2))
2615 return 0;
2618 v1 = CONSTRUCTOR_ELTS (t1);
2619 v2 = CONSTRUCTOR_ELTS (t2);
2620 if (VEC_length (constructor_elt, v1)
2621 != VEC_length (constructor_elt, v2))
2622 return 0;
2624 for (idx = 0; idx < VEC_length (constructor_elt, v1); ++idx)
2626 constructor_elt *c1 = VEC_index (constructor_elt, v1, idx);
2627 constructor_elt *c2 = VEC_index (constructor_elt, v2, idx);
2629 /* Check that each value is the same... */
2630 if (!compare_constant (c1->value, c2->value))
2631 return 0;
2632 /* ... and that they apply to the same fields! */
2633 if (typecode == ARRAY_TYPE)
2635 if (!compare_constant (c1->index, c2->index))
2636 return 0;
2638 else
2640 if (c1->index != c2->index)
2641 return 0;
2645 return 1;
2648 case ADDR_EXPR:
2649 case FDESC_EXPR:
2651 struct addr_const value1, value2;
2653 decode_addr_const (t1, &value1);
2654 decode_addr_const (t2, &value2);
2655 return (value1.offset == value2.offset
2656 && strcmp (XSTR (value1.base, 0), XSTR (value2.base, 0)) == 0);
2659 case PLUS_EXPR:
2660 case MINUS_EXPR:
2661 case RANGE_EXPR:
2662 return (compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
2663 && compare_constant(TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
2665 case NOP_EXPR:
2666 case CONVERT_EXPR:
2667 case NON_LVALUE_EXPR:
2668 case VIEW_CONVERT_EXPR:
2669 return compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2671 default:
2673 tree nt1, nt2;
2674 nt1 = lang_hooks.expand_constant (t1);
2675 nt2 = lang_hooks.expand_constant (t2);
2676 if (nt1 != t1 || nt2 != t2)
2677 return compare_constant (nt1, nt2);
2678 else
2679 return 0;
2683 gcc_unreachable ();
2686 /* Make a copy of the whole tree structure for a constant. This
2687 handles the same types of nodes that compare_constant handles. */
2689 static tree
2690 copy_constant (tree exp)
2692 switch (TREE_CODE (exp))
2694 case ADDR_EXPR:
2695 /* For ADDR_EXPR, we do not want to copy the decl whose address
2696 is requested. We do want to copy constants though. */
2697 if (CONSTANT_CLASS_P (TREE_OPERAND (exp, 0)))
2698 return build1 (TREE_CODE (exp), TREE_TYPE (exp),
2699 copy_constant (TREE_OPERAND (exp, 0)));
2700 else
2701 return copy_node (exp);
2703 case INTEGER_CST:
2704 case REAL_CST:
2705 case STRING_CST:
2706 return copy_node (exp);
2708 case COMPLEX_CST:
2709 return build_complex (TREE_TYPE (exp),
2710 copy_constant (TREE_REALPART (exp)),
2711 copy_constant (TREE_IMAGPART (exp)));
2713 case PLUS_EXPR:
2714 case MINUS_EXPR:
2715 return build2 (TREE_CODE (exp), TREE_TYPE (exp),
2716 copy_constant (TREE_OPERAND (exp, 0)),
2717 copy_constant (TREE_OPERAND (exp, 1)));
2719 case NOP_EXPR:
2720 case CONVERT_EXPR:
2721 case NON_LVALUE_EXPR:
2722 case VIEW_CONVERT_EXPR:
2723 return build1 (TREE_CODE (exp), TREE_TYPE (exp),
2724 copy_constant (TREE_OPERAND (exp, 0)));
2726 case CONSTRUCTOR:
2728 tree copy = copy_node (exp);
2729 VEC(constructor_elt, gc) *v;
2730 unsigned HOST_WIDE_INT idx;
2731 tree purpose, value;
2733 v = VEC_alloc(constructor_elt, gc, VEC_length(constructor_elt,
2734 CONSTRUCTOR_ELTS (exp)));
2735 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, purpose, value)
2737 constructor_elt *ce = VEC_quick_push (constructor_elt, v, NULL);
2738 ce->index = purpose;
2739 ce->value = copy_constant (value);
2741 CONSTRUCTOR_ELTS (copy) = v;
2742 return copy;
2745 default:
2747 tree t = lang_hooks.expand_constant (exp);
2749 gcc_assert (t == exp);
2750 return copy_constant (t);
2755 /* Return the alignment of constant EXP in bits. */
2757 static unsigned int
2758 get_constant_alignment (tree exp)
2760 unsigned int align;
2762 align = TYPE_ALIGN (TREE_TYPE (exp));
2763 #ifdef CONSTANT_ALIGNMENT
2764 align = CONSTANT_ALIGNMENT (exp, align);
2765 #endif
2766 return align;
2769 /* Return the section into which constant EXP should be placed. */
2771 static section *
2772 get_constant_section (tree exp)
2774 if (IN_NAMED_SECTION (exp))
2775 return get_named_section (exp, NULL, compute_reloc_for_constant (exp));
2776 else
2777 return targetm.asm_out.select_section (exp,
2778 compute_reloc_for_constant (exp),
2779 get_constant_alignment (exp));
2782 /* Return the size of constant EXP in bytes. */
2784 static HOST_WIDE_INT
2785 get_constant_size (tree exp)
2787 HOST_WIDE_INT size;
2789 size = int_size_in_bytes (TREE_TYPE (exp));
2790 if (TREE_CODE (exp) == STRING_CST)
2791 size = MAX (TREE_STRING_LENGTH (exp), size);
2792 return size;
2795 /* Subroutine of output_constant_def:
2796 No constant equal to EXP is known to have been output.
2797 Make a constant descriptor to enter EXP in the hash table.
2798 Assign the label number and construct RTL to refer to the
2799 constant's location in memory.
2800 Caller is responsible for updating the hash table. */
2802 static struct constant_descriptor_tree *
2803 build_constant_desc (tree exp)
2805 rtx symbol;
2806 rtx rtl;
2807 char label[256];
2808 int labelno;
2809 struct constant_descriptor_tree *desc;
2811 desc = ggc_alloc (sizeof (*desc));
2812 desc->value = copy_constant (exp);
2814 /* Propagate marked-ness to copied constant. */
2815 if (flag_mudflap && mf_marked_p (exp))
2816 mf_mark (desc->value);
2818 /* Create a string containing the label name, in LABEL. */
2819 labelno = const_labelno++;
2820 ASM_GENERATE_INTERNAL_LABEL (label, "LC", labelno);
2822 /* We have a symbol name; construct the SYMBOL_REF and the MEM. */
2823 if (use_object_blocks_p ())
2825 section *sect = get_constant_section (exp);
2826 symbol = create_block_symbol (ggc_strdup (label),
2827 get_block_for_section (sect), -1);
2829 else
2830 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
2831 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
2832 SET_SYMBOL_REF_DECL (symbol, desc->value);
2833 TREE_CONSTANT_POOL_ADDRESS_P (symbol) = 1;
2835 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (exp)), symbol);
2836 set_mem_attributes (rtl, exp, 1);
2837 set_mem_alias_set (rtl, 0);
2838 set_mem_alias_set (rtl, const_alias_set);
2840 /* Set flags or add text to the name to record information, such as
2841 that it is a local symbol. If the name is changed, the macro
2842 ASM_OUTPUT_LABELREF will have to know how to strip this
2843 information. This call might invalidate our local variable
2844 SYMBOL; we can't use it afterward. */
2846 targetm.encode_section_info (exp, rtl, true);
2848 desc->rtl = rtl;
2850 return desc;
2853 /* Return an rtx representing a reference to constant data in memory
2854 for the constant expression EXP.
2856 If assembler code for such a constant has already been output,
2857 return an rtx to refer to it.
2858 Otherwise, output such a constant in memory
2859 and generate an rtx for it.
2861 If DEFER is nonzero, this constant can be deferred and output only
2862 if referenced in the function after all optimizations.
2864 `const_desc_table' records which constants already have label strings. */
2867 output_constant_def (tree exp, int defer)
2869 struct constant_descriptor_tree *desc;
2870 struct constant_descriptor_tree key;
2871 void **loc;
2873 /* Look up EXP in the table of constant descriptors. If we didn't find
2874 it, create a new one. */
2875 key.value = exp;
2876 key.hash = const_hash_1 (exp);
2877 loc = htab_find_slot_with_hash (const_desc_htab, &key, key.hash, INSERT);
2879 desc = *loc;
2880 if (desc == 0)
2882 desc = build_constant_desc (exp);
2883 desc->hash = key.hash;
2884 *loc = desc;
2887 maybe_output_constant_def_contents (desc, defer);
2888 return desc->rtl;
2891 /* Subroutine of output_constant_def: Decide whether or not we need to
2892 output the constant DESC now, and if so, do it. */
2893 static void
2894 maybe_output_constant_def_contents (struct constant_descriptor_tree *desc,
2895 int defer)
2897 rtx symbol = XEXP (desc->rtl, 0);
2898 tree exp = desc->value;
2900 if (flag_syntax_only)
2901 return;
2903 if (TREE_ASM_WRITTEN (exp))
2904 /* Already output; don't do it again. */
2905 return;
2907 /* We can always defer constants as long as the context allows
2908 doing so. */
2909 if (defer)
2911 /* Increment n_deferred_constants if it exists. It needs to be at
2912 least as large as the number of constants actually referred to
2913 by the function. If it's too small we'll stop looking too early
2914 and fail to emit constants; if it's too large we'll only look
2915 through the entire function when we could have stopped earlier. */
2916 if (cfun)
2917 n_deferred_constants++;
2918 return;
2921 output_constant_def_contents (symbol);
2924 /* Subroutine of output_constant_def_contents. Output the definition
2925 of constant EXP, which is pointed to by label LABEL. ALIGN is the
2926 constant's alignment in bits. */
2928 static void
2929 assemble_constant_contents (tree exp, const char *label, unsigned int align)
2931 HOST_WIDE_INT size;
2933 size = get_constant_size (exp);
2935 /* Do any machine/system dependent processing of the constant. */
2936 #ifdef ASM_DECLARE_CONSTANT_NAME
2937 ASM_DECLARE_CONSTANT_NAME (asm_out_file, label, exp, size);
2938 #else
2939 /* Standard thing is just output label for the constant. */
2940 ASM_OUTPUT_LABEL (asm_out_file, label);
2941 #endif /* ASM_DECLARE_CONSTANT_NAME */
2943 /* Output the value of EXP. */
2944 output_constant (exp, size, align);
2947 /* We must output the constant data referred to by SYMBOL; do so. */
2949 static void
2950 output_constant_def_contents (rtx symbol)
2952 tree exp = SYMBOL_REF_DECL (symbol);
2953 unsigned int align;
2955 /* Make sure any other constants whose addresses appear in EXP
2956 are assigned label numbers. */
2957 output_addressed_constants (exp);
2959 /* We are no longer deferring this constant. */
2960 TREE_ASM_WRITTEN (exp) = 1;
2962 /* If the constant is part of an object block, make sure that the
2963 decl has been positioned within its block, but do not write out
2964 its definition yet. output_object_blocks will do that later. */
2965 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
2966 place_block_symbol (symbol);
2967 else
2969 switch_to_section (get_constant_section (exp));
2970 align = get_constant_alignment (exp);
2971 if (align > BITS_PER_UNIT)
2972 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
2973 assemble_constant_contents (exp, XSTR (symbol, 0), align);
2975 if (flag_mudflap)
2976 mudflap_enqueue_constant (exp);
2979 /* Look up EXP in the table of constant descriptors. Return the rtl
2980 if it has been emitted, else null. */
2983 lookup_constant_def (tree exp)
2985 struct constant_descriptor_tree *desc;
2986 struct constant_descriptor_tree key;
2988 key.value = exp;
2989 key.hash = const_hash_1 (exp);
2990 desc = htab_find_with_hash (const_desc_htab, &key, key.hash);
2992 return (desc ? desc->rtl : NULL_RTX);
2995 /* Used in the hash tables to avoid outputting the same constant
2996 twice. Unlike 'struct constant_descriptor_tree', RTX constants
2997 are output once per function, not once per file. */
2998 /* ??? Only a few targets need per-function constant pools. Most
2999 can use one per-file pool. Should add a targetm bit to tell the
3000 difference. */
3002 struct rtx_constant_pool GTY(())
3004 /* Pointers to first and last constant in pool, as ordered by offset. */
3005 struct constant_descriptor_rtx *first;
3006 struct constant_descriptor_rtx *last;
3008 /* Hash facility for making memory-constants from constant rtl-expressions.
3009 It is used on RISC machines where immediate integer arguments and
3010 constant addresses are restricted so that such constants must be stored
3011 in memory. */
3012 htab_t GTY((param_is (struct constant_descriptor_rtx))) const_rtx_htab;
3014 /* Current offset in constant pool (does not include any
3015 machine-specific header). */
3016 HOST_WIDE_INT offset;
3019 struct constant_descriptor_rtx GTY((chain_next ("%h.next")))
3021 struct constant_descriptor_rtx *next;
3022 rtx mem;
3023 rtx sym;
3024 rtx constant;
3025 HOST_WIDE_INT offset;
3026 hashval_t hash;
3027 enum machine_mode mode;
3028 unsigned int align;
3029 int labelno;
3030 int mark;
3033 /* Hash and compare functions for const_rtx_htab. */
3035 static hashval_t
3036 const_desc_rtx_hash (const void *ptr)
3038 const struct constant_descriptor_rtx *desc = ptr;
3039 return desc->hash;
3042 static int
3043 const_desc_rtx_eq (const void *a, const void *b)
3045 const struct constant_descriptor_rtx *x = a;
3046 const struct constant_descriptor_rtx *y = b;
3048 if (x->mode != y->mode)
3049 return 0;
3050 return rtx_equal_p (x->constant, y->constant);
3053 /* This is the worker function for const_rtx_hash, called via for_each_rtx. */
3055 static int
3056 const_rtx_hash_1 (rtx *xp, void *data)
3058 unsigned HOST_WIDE_INT hwi;
3059 enum machine_mode mode;
3060 enum rtx_code code;
3061 hashval_t h, *hp;
3062 rtx x;
3064 x = *xp;
3065 code = GET_CODE (x);
3066 mode = GET_MODE (x);
3067 h = (hashval_t) code * 1048573 + mode;
3069 switch (code)
3071 case CONST_INT:
3072 hwi = INTVAL (x);
3073 fold_hwi:
3075 const int shift = sizeof (hashval_t) * CHAR_BIT;
3076 const int n = sizeof (HOST_WIDE_INT) / sizeof (hashval_t);
3077 int i;
3079 h ^= (hashval_t) hwi;
3080 for (i = 1; i < n; ++i)
3082 hwi >>= shift;
3083 h ^= (hashval_t) hwi;
3086 break;
3088 case CONST_DOUBLE:
3089 if (mode == VOIDmode)
3091 hwi = CONST_DOUBLE_LOW (x) ^ CONST_DOUBLE_HIGH (x);
3092 goto fold_hwi;
3094 else
3095 h ^= real_hash (CONST_DOUBLE_REAL_VALUE (x));
3096 break;
3098 case CONST_VECTOR:
3100 int i;
3101 for (i = XVECLEN (x, 0); i-- > 0; )
3102 h = h * 251 + const_rtx_hash_1 (&XVECEXP (x, 0, i), data);
3104 break;
3106 case SYMBOL_REF:
3107 h ^= htab_hash_string (XSTR (x, 0));
3108 break;
3110 case LABEL_REF:
3111 h = h * 251 + CODE_LABEL_NUMBER (XEXP (x, 0));
3112 break;
3114 case UNSPEC:
3115 case UNSPEC_VOLATILE:
3116 h = h * 251 + XINT (x, 1);
3117 break;
3119 default:
3120 break;
3123 hp = data;
3124 *hp = *hp * 509 + h;
3125 return 0;
3128 /* Compute a hash value for X, which should be a constant. */
3130 static hashval_t
3131 const_rtx_hash (rtx x)
3133 hashval_t h = 0;
3134 for_each_rtx (&x, const_rtx_hash_1, &h);
3135 return h;
3139 /* Create and return a new rtx constant pool. */
3141 static struct rtx_constant_pool *
3142 create_constant_pool (void)
3144 struct rtx_constant_pool *pool;
3146 pool = ggc_alloc (sizeof (struct rtx_constant_pool));
3147 pool->const_rtx_htab = htab_create_ggc (31, const_desc_rtx_hash,
3148 const_desc_rtx_eq, NULL);
3149 pool->first = NULL;
3150 pool->last = NULL;
3151 pool->offset = 0;
3152 return pool;
3155 /* Initialize constant pool hashing for a new function. */
3157 void
3158 init_varasm_status (struct function *f)
3160 struct varasm_status *p;
3162 p = ggc_alloc (sizeof (struct varasm_status));
3163 f->varasm = p;
3165 p->pool = create_constant_pool ();
3166 p->deferred_constants = 0;
3169 /* Given a MINUS expression, simplify it if both sides
3170 include the same symbol. */
3173 simplify_subtraction (rtx x)
3175 rtx r = simplify_rtx (x);
3176 return r ? r : x;
3179 /* Given a constant rtx X, make (or find) a memory constant for its value
3180 and return a MEM rtx to refer to it in memory. */
3183 force_const_mem (enum machine_mode mode, rtx x)
3185 struct constant_descriptor_rtx *desc, tmp;
3186 struct rtx_constant_pool *pool;
3187 char label[256];
3188 rtx def, symbol;
3189 hashval_t hash;
3190 unsigned int align;
3191 void **slot;
3193 /* If we're not allowed to drop X into the constant pool, don't. */
3194 if (targetm.cannot_force_const_mem (x))
3195 return NULL_RTX;
3197 /* Record that this function has used a constant pool entry. */
3198 current_function_uses_const_pool = 1;
3200 /* Decide which pool to use. */
3201 pool = (targetm.use_blocks_for_constant_p (mode, x)
3202 ? shared_constant_pool
3203 : cfun->varasm->pool);
3205 /* Lookup the value in the hashtable. */
3206 tmp.constant = x;
3207 tmp.mode = mode;
3208 hash = const_rtx_hash (x);
3209 slot = htab_find_slot_with_hash (pool->const_rtx_htab, &tmp, hash, INSERT);
3210 desc = *slot;
3212 /* If the constant was already present, return its memory. */
3213 if (desc)
3214 return copy_rtx (desc->mem);
3216 /* Otherwise, create a new descriptor. */
3217 desc = ggc_alloc (sizeof (*desc));
3218 *slot = desc;
3220 /* Align the location counter as required by EXP's data type. */
3221 align = GET_MODE_ALIGNMENT (mode == VOIDmode ? word_mode : mode);
3222 #ifdef CONSTANT_ALIGNMENT
3224 tree type = lang_hooks.types.type_for_mode (mode, 0);
3225 if (type != NULL_TREE)
3226 align = CONSTANT_ALIGNMENT (make_tree (type, x), align);
3228 #endif
3230 pool->offset += (align / BITS_PER_UNIT) - 1;
3231 pool->offset &= ~ ((align / BITS_PER_UNIT) - 1);
3233 desc->next = NULL;
3234 desc->constant = tmp.constant;
3235 desc->offset = pool->offset;
3236 desc->hash = hash;
3237 desc->mode = mode;
3238 desc->align = align;
3239 desc->labelno = const_labelno;
3240 desc->mark = 0;
3242 pool->offset += GET_MODE_SIZE (mode);
3243 if (pool->last)
3244 pool->last->next = desc;
3245 else
3246 pool->first = pool->last = desc;
3247 pool->last = desc;
3249 /* Create a string containing the label name, in LABEL. */
3250 ASM_GENERATE_INTERNAL_LABEL (label, "LC", const_labelno);
3251 ++const_labelno;
3253 /* Construct the SYMBOL_REF. Make sure to mark it as belonging to
3254 the constants pool. */
3255 if (use_object_blocks_p () && targetm.use_blocks_for_constant_p (mode, x))
3257 section *sect = targetm.asm_out.select_rtx_section (mode, x, align);
3258 symbol = create_block_symbol (ggc_strdup (label),
3259 get_block_for_section (sect), -1);
3261 else
3262 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3263 desc->sym = symbol;
3264 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3265 CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3266 SET_SYMBOL_REF_CONSTANT (symbol, desc);
3268 /* Construct the MEM. */
3269 desc->mem = def = gen_const_mem (mode, symbol);
3270 set_mem_attributes (def, lang_hooks.types.type_for_mode (mode, 0), 1);
3271 set_mem_align (def, align);
3273 /* If we're dropping a label to the constant pool, make sure we
3274 don't delete it. */
3275 if (GET_CODE (x) == LABEL_REF)
3276 LABEL_PRESERVE_P (XEXP (x, 0)) = 1;
3278 return copy_rtx (def);
3281 /* Given a constant pool SYMBOL_REF, return the corresponding constant. */
3284 get_pool_constant (rtx addr)
3286 return SYMBOL_REF_CONSTANT (addr)->constant;
3289 /* Given a constant pool SYMBOL_REF, return the corresponding constant
3290 and whether it has been output or not. */
3293 get_pool_constant_mark (rtx addr, bool *pmarked)
3295 struct constant_descriptor_rtx *desc;
3297 desc = SYMBOL_REF_CONSTANT (addr);
3298 *pmarked = (desc->mark != 0);
3299 return desc->constant;
3302 /* Similar, return the mode. */
3304 enum machine_mode
3305 get_pool_mode (rtx addr)
3307 return SYMBOL_REF_CONSTANT (addr)->mode;
3310 /* Return the size of the constant pool. */
3313 get_pool_size (void)
3315 return cfun->varasm->pool->offset;
3318 /* Worker function for output_constant_pool_1. Emit assembly for X
3319 in MODE with known alignment ALIGN. */
3321 static void
3322 output_constant_pool_2 (enum machine_mode mode, rtx x, unsigned int align)
3324 switch (GET_MODE_CLASS (mode))
3326 case MODE_FLOAT:
3327 case MODE_DECIMAL_FLOAT:
3329 REAL_VALUE_TYPE r;
3331 gcc_assert (GET_CODE (x) == CONST_DOUBLE);
3332 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
3333 assemble_real (r, mode, align);
3334 break;
3337 case MODE_INT:
3338 case MODE_PARTIAL_INT:
3339 assemble_integer (x, GET_MODE_SIZE (mode), align, 1);
3340 break;
3342 case MODE_VECTOR_FLOAT:
3343 case MODE_VECTOR_INT:
3345 int i, units;
3346 enum machine_mode submode = GET_MODE_INNER (mode);
3347 unsigned int subalign = MIN (align, GET_MODE_BITSIZE (submode));
3349 gcc_assert (GET_CODE (x) == CONST_VECTOR);
3350 units = CONST_VECTOR_NUNITS (x);
3352 for (i = 0; i < units; i++)
3354 rtx elt = CONST_VECTOR_ELT (x, i);
3355 output_constant_pool_2 (submode, elt, i ? subalign : align);
3358 break;
3360 default:
3361 gcc_unreachable ();
3365 /* Worker function for output_constant_pool. Emit constant DESC,
3366 giving it ALIGN bits of alignment. */
3368 static void
3369 output_constant_pool_1 (struct constant_descriptor_rtx *desc,
3370 unsigned int align)
3372 rtx x, tmp;
3374 x = desc->constant;
3376 /* See if X is a LABEL_REF (or a CONST referring to a LABEL_REF)
3377 whose CODE_LABEL has been deleted. This can occur if a jump table
3378 is eliminated by optimization. If so, write a constant of zero
3379 instead. Note that this can also happen by turning the
3380 CODE_LABEL into a NOTE. */
3381 /* ??? This seems completely and utterly wrong. Certainly it's
3382 not true for NOTE_INSN_DELETED_LABEL, but I disbelieve proper
3383 functioning even with INSN_DELETED_P and friends. */
3385 tmp = x;
3386 switch (GET_CODE (x))
3388 case CONST:
3389 if (GET_CODE (XEXP (x, 0)) != PLUS
3390 || GET_CODE (XEXP (XEXP (x, 0), 0)) != LABEL_REF)
3391 break;
3392 tmp = XEXP (XEXP (x, 0), 0);
3393 /* FALLTHRU */
3395 case LABEL_REF:
3396 tmp = XEXP (x, 0);
3397 gcc_assert (!INSN_DELETED_P (tmp));
3398 gcc_assert (!NOTE_P (tmp)
3399 || NOTE_LINE_NUMBER (tmp) != NOTE_INSN_DELETED);
3400 break;
3402 default:
3403 break;
3406 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3407 ASM_OUTPUT_SPECIAL_POOL_ENTRY (asm_out_file, x, desc->mode,
3408 align, desc->labelno, done);
3409 #endif
3411 assemble_align (align);
3413 /* Output the label. */
3414 targetm.asm_out.internal_label (asm_out_file, "LC", desc->labelno);
3416 /* Output the data. */
3417 output_constant_pool_2 (desc->mode, x, align);
3419 /* Make sure all constants in SECTION_MERGE and not SECTION_STRINGS
3420 sections have proper size. */
3421 if (align > GET_MODE_BITSIZE (desc->mode)
3422 && in_section
3423 && (in_section->common.flags & SECTION_MERGE))
3424 assemble_align (align);
3426 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3427 done:
3428 #endif
3429 return;
3432 /* Given a SYMBOL_REF CURRENT_RTX, mark it and all constants it refers
3433 to as used. Emit referenced deferred strings. This function can
3434 be used with for_each_rtx to mark all SYMBOL_REFs in an rtx. */
3436 static int
3437 mark_constant (rtx *current_rtx, void *data ATTRIBUTE_UNUSED)
3439 rtx x = *current_rtx;
3441 if (x == NULL_RTX || GET_CODE (x) != SYMBOL_REF)
3442 return 0;
3444 if (CONSTANT_POOL_ADDRESS_P (x))
3446 struct constant_descriptor_rtx *desc = SYMBOL_REF_CONSTANT (x);
3447 if (desc->mark == 0)
3449 desc->mark = 1;
3450 for_each_rtx (&desc->constant, mark_constant, NULL);
3453 else if (TREE_CONSTANT_POOL_ADDRESS_P (x))
3455 tree exp = SYMBOL_REF_DECL (x);
3456 if (!TREE_ASM_WRITTEN (exp))
3458 n_deferred_constants--;
3459 output_constant_def_contents (x);
3463 return -1;
3466 /* Look through appropriate parts of INSN, marking all entries in the
3467 constant pool which are actually being used. Entries that are only
3468 referenced by other constants are also marked as used. Emit
3469 deferred strings that are used. */
3471 static void
3472 mark_constants (rtx insn)
3474 if (!INSN_P (insn))
3475 return;
3477 /* Insns may appear inside a SEQUENCE. Only check the patterns of
3478 insns, not any notes that may be attached. We don't want to mark
3479 a constant just because it happens to appear in a REG_EQUIV note. */
3480 if (GET_CODE (PATTERN (insn)) == SEQUENCE)
3482 rtx seq = PATTERN (insn);
3483 int i, n = XVECLEN (seq, 0);
3484 for (i = 0; i < n; ++i)
3486 rtx subinsn = XVECEXP (seq, 0, i);
3487 if (INSN_P (subinsn))
3488 for_each_rtx (&PATTERN (subinsn), mark_constant, NULL);
3491 else
3492 for_each_rtx (&PATTERN (insn), mark_constant, NULL);
3495 /* Look through the instructions for this function, and mark all the
3496 entries in POOL which are actually being used. Emit deferred constants
3497 which have indeed been used. */
3499 static void
3500 mark_constant_pool (void)
3502 rtx insn, link;
3504 if (!current_function_uses_const_pool && n_deferred_constants == 0)
3505 return;
3507 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3508 mark_constants (insn);
3510 for (link = current_function_epilogue_delay_list;
3511 link;
3512 link = XEXP (link, 1))
3513 mark_constants (XEXP (link, 0));
3516 /* Write all the constants in POOL. */
3518 static void
3519 output_constant_pool_contents (struct rtx_constant_pool *pool)
3521 struct constant_descriptor_rtx *desc;
3523 for (desc = pool->first; desc ; desc = desc->next)
3524 if (desc->mark)
3526 /* If the constant is part of an object_block, make sure that
3527 the constant has been positioned within its block, but do not
3528 write out its definition yet. output_object_blocks will do
3529 that later. */
3530 if (SYMBOL_REF_HAS_BLOCK_INFO_P (desc->sym)
3531 && SYMBOL_REF_BLOCK (desc->sym))
3532 place_block_symbol (desc->sym);
3533 else
3535 switch_to_section (targetm.asm_out.select_rtx_section
3536 (desc->mode, desc->constant, desc->align));
3537 output_constant_pool_1 (desc, desc->align);
3542 /* Mark all constants that are used in the current function, then write
3543 out the function's private constant pool. */
3545 static void
3546 output_constant_pool (const char *fnname ATTRIBUTE_UNUSED,
3547 tree fndecl ATTRIBUTE_UNUSED)
3549 struct rtx_constant_pool *pool = cfun->varasm->pool;
3551 /* It is possible for gcc to call force_const_mem and then to later
3552 discard the instructions which refer to the constant. In such a
3553 case we do not need to output the constant. */
3554 mark_constant_pool ();
3556 #ifdef ASM_OUTPUT_POOL_PROLOGUE
3557 ASM_OUTPUT_POOL_PROLOGUE (asm_out_file, fnname, fndecl, pool->offset);
3558 #endif
3560 output_constant_pool_contents (pool);
3562 #ifdef ASM_OUTPUT_POOL_EPILOGUE
3563 ASM_OUTPUT_POOL_EPILOGUE (asm_out_file, fnname, fndecl, pool->offset);
3564 #endif
3567 /* Write the contents of the shared constant pool. */
3569 void
3570 output_shared_constant_pool (void)
3572 output_constant_pool_contents (shared_constant_pool);
3575 /* Determine what kind of relocations EXP may need. */
3578 compute_reloc_for_constant (tree exp)
3580 int reloc = 0, reloc2;
3581 tree tem;
3583 /* Give the front-end a chance to convert VALUE to something that
3584 looks more like a constant to the back-end. */
3585 exp = lang_hooks.expand_constant (exp);
3587 switch (TREE_CODE (exp))
3589 case ADDR_EXPR:
3590 case FDESC_EXPR:
3591 /* Go inside any operations that get_inner_reference can handle and see
3592 if what's inside is a constant: no need to do anything here for
3593 addresses of variables or functions. */
3594 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
3595 tem = TREE_OPERAND (tem, 0))
3598 if (TREE_PUBLIC (tem))
3599 reloc |= 2;
3600 else
3601 reloc |= 1;
3602 break;
3604 case PLUS_EXPR:
3605 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3606 reloc |= compute_reloc_for_constant (TREE_OPERAND (exp, 1));
3607 break;
3609 case MINUS_EXPR:
3610 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3611 reloc2 = compute_reloc_for_constant (TREE_OPERAND (exp, 1));
3612 /* The difference of two local labels is computable at link time. */
3613 if (reloc == 1 && reloc2 == 1)
3614 reloc = 0;
3615 else
3616 reloc |= reloc2;
3617 break;
3619 case NOP_EXPR:
3620 case CONVERT_EXPR:
3621 case NON_LVALUE_EXPR:
3622 case VIEW_CONVERT_EXPR:
3623 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3624 break;
3626 case CONSTRUCTOR:
3628 unsigned HOST_WIDE_INT idx;
3629 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
3630 if (tem != 0)
3631 reloc |= compute_reloc_for_constant (tem);
3633 break;
3635 default:
3636 break;
3638 return reloc;
3641 /* Find all the constants whose addresses are referenced inside of EXP,
3642 and make sure assembler code with a label has been output for each one.
3643 Indicate whether an ADDR_EXPR has been encountered. */
3645 static void
3646 output_addressed_constants (tree exp)
3648 tree tem;
3650 /* Give the front-end a chance to convert VALUE to something that
3651 looks more like a constant to the back-end. */
3652 exp = lang_hooks.expand_constant (exp);
3654 switch (TREE_CODE (exp))
3656 case ADDR_EXPR:
3657 case FDESC_EXPR:
3658 /* Go inside any operations that get_inner_reference can handle and see
3659 if what's inside is a constant: no need to do anything here for
3660 addresses of variables or functions. */
3661 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
3662 tem = TREE_OPERAND (tem, 0))
3665 /* If we have an initialized CONST_DECL, retrieve the initializer. */
3666 if (TREE_CODE (tem) == CONST_DECL && DECL_INITIAL (tem))
3667 tem = DECL_INITIAL (tem);
3669 if (CONSTANT_CLASS_P (tem) || TREE_CODE (tem) == CONSTRUCTOR)
3670 output_constant_def (tem, 0);
3671 break;
3673 case PLUS_EXPR:
3674 case MINUS_EXPR:
3675 output_addressed_constants (TREE_OPERAND (exp, 1));
3676 /* Fall through. */
3678 case NOP_EXPR:
3679 case CONVERT_EXPR:
3680 case NON_LVALUE_EXPR:
3681 case VIEW_CONVERT_EXPR:
3682 output_addressed_constants (TREE_OPERAND (exp, 0));
3683 break;
3685 case CONSTRUCTOR:
3687 unsigned HOST_WIDE_INT idx;
3688 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
3689 if (tem != 0)
3690 output_addressed_constants (tem);
3692 break;
3694 default:
3695 break;
3699 /* Return nonzero if VALUE is a valid constant-valued expression
3700 for use in initializing a static variable; one that can be an
3701 element of a "constant" initializer.
3703 Return null_pointer_node if the value is absolute;
3704 if it is relocatable, return the variable that determines the relocation.
3705 We assume that VALUE has been folded as much as possible;
3706 therefore, we do not need to check for such things as
3707 arithmetic-combinations of integers. */
3709 tree
3710 initializer_constant_valid_p (tree value, tree endtype)
3712 /* Give the front-end a chance to convert VALUE to something that
3713 looks more like a constant to the back-end. */
3714 value = lang_hooks.expand_constant (value);
3716 switch (TREE_CODE (value))
3718 case CONSTRUCTOR:
3719 if ((TREE_CODE (TREE_TYPE (value)) == UNION_TYPE
3720 || TREE_CODE (TREE_TYPE (value)) == RECORD_TYPE)
3721 && TREE_CONSTANT (value)
3722 && !VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (value)))
3724 unsigned HOST_WIDE_INT idx;
3725 tree elt;
3726 bool absolute = true;
3728 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
3730 tree reloc;
3731 reloc = initializer_constant_valid_p (elt, TREE_TYPE (elt));
3732 if (!reloc)
3733 return NULL_TREE;
3734 if (reloc != null_pointer_node)
3735 absolute = false;
3737 /* For a non-absolute relocation, there is no single
3738 variable that can be "the variable that determines the
3739 relocation." */
3740 return absolute ? null_pointer_node : error_mark_node;
3743 return TREE_STATIC (value) ? null_pointer_node : NULL_TREE;
3745 case INTEGER_CST:
3746 case VECTOR_CST:
3747 case REAL_CST:
3748 case STRING_CST:
3749 case COMPLEX_CST:
3750 return null_pointer_node;
3752 case ADDR_EXPR:
3753 case FDESC_EXPR:
3754 value = staticp (TREE_OPERAND (value, 0));
3755 if (value)
3757 /* "&(*a).f" is like unto pointer arithmetic. If "a" turns out to
3758 be a constant, this is old-skool offsetof-like nonsense. */
3759 if (TREE_CODE (value) == INDIRECT_REF
3760 && TREE_CONSTANT (TREE_OPERAND (value, 0)))
3761 return null_pointer_node;
3762 /* Taking the address of a nested function involves a trampoline. */
3763 if (TREE_CODE (value) == FUNCTION_DECL
3764 && ((decl_function_context (value)
3765 && !DECL_NO_STATIC_CHAIN (value))
3766 || DECL_DLLIMPORT_P (value)))
3767 return NULL_TREE;
3768 /* "&{...}" requires a temporary to hold the constructed
3769 object. */
3770 if (TREE_CODE (value) == CONSTRUCTOR)
3771 return NULL_TREE;
3773 return value;
3775 case VIEW_CONVERT_EXPR:
3776 case NON_LVALUE_EXPR:
3777 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
3779 case CONVERT_EXPR:
3780 case NOP_EXPR:
3782 tree src;
3783 tree src_type;
3784 tree dest_type;
3786 src = TREE_OPERAND (value, 0);
3787 src_type = TREE_TYPE (src);
3788 dest_type = TREE_TYPE (value);
3790 /* Allow conversions between pointer types, floating-point
3791 types, and offset types. */
3792 if ((POINTER_TYPE_P (dest_type) && POINTER_TYPE_P (src_type))
3793 || (FLOAT_TYPE_P (dest_type) && FLOAT_TYPE_P (src_type))
3794 || (TREE_CODE (dest_type) == OFFSET_TYPE
3795 && TREE_CODE (src_type) == OFFSET_TYPE))
3796 return initializer_constant_valid_p (src, endtype);
3798 /* Allow length-preserving conversions between integer types. */
3799 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type)
3800 && (TYPE_PRECISION (dest_type) == TYPE_PRECISION (src_type)))
3801 return initializer_constant_valid_p (src, endtype);
3803 /* Allow conversions between other integer types only if
3804 explicit value. */
3805 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type))
3807 tree inner = initializer_constant_valid_p (src, endtype);
3808 if (inner == null_pointer_node)
3809 return null_pointer_node;
3810 break;
3813 /* Allow (int) &foo provided int is as wide as a pointer. */
3814 if (INTEGRAL_TYPE_P (dest_type) && POINTER_TYPE_P (src_type)
3815 && (TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type)))
3816 return initializer_constant_valid_p (src, endtype);
3818 /* Likewise conversions from int to pointers, but also allow
3819 conversions from 0. */
3820 if ((POINTER_TYPE_P (dest_type)
3821 || TREE_CODE (dest_type) == OFFSET_TYPE)
3822 && INTEGRAL_TYPE_P (src_type))
3824 if (TREE_CODE (src) == INTEGER_CST
3825 && TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type))
3826 return null_pointer_node;
3827 if (integer_zerop (src))
3828 return null_pointer_node;
3829 else if (TYPE_PRECISION (dest_type) <= TYPE_PRECISION (src_type))
3830 return initializer_constant_valid_p (src, endtype);
3833 /* Allow conversions to struct or union types if the value
3834 inside is okay. */
3835 if (TREE_CODE (dest_type) == RECORD_TYPE
3836 || TREE_CODE (dest_type) == UNION_TYPE)
3837 return initializer_constant_valid_p (src, endtype);
3839 break;
3841 case PLUS_EXPR:
3842 if (! INTEGRAL_TYPE_P (endtype)
3843 || TYPE_PRECISION (endtype) >= POINTER_SIZE)
3845 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
3846 endtype);
3847 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
3848 endtype);
3849 /* If either term is absolute, use the other terms relocation. */
3850 if (valid0 == null_pointer_node)
3851 return valid1;
3852 if (valid1 == null_pointer_node)
3853 return valid0;
3855 break;
3857 case MINUS_EXPR:
3858 if (! INTEGRAL_TYPE_P (endtype)
3859 || TYPE_PRECISION (endtype) >= POINTER_SIZE)
3861 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
3862 endtype);
3863 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
3864 endtype);
3865 /* Win if second argument is absolute. */
3866 if (valid1 == null_pointer_node)
3867 return valid0;
3868 /* Win if both arguments have the same relocation.
3869 Then the value is absolute. */
3870 if (valid0 == valid1 && valid0 != 0)
3871 return null_pointer_node;
3873 /* Since GCC guarantees that string constants are unique in the
3874 generated code, a subtraction between two copies of the same
3875 constant string is absolute. */
3876 if (valid0 && TREE_CODE (valid0) == STRING_CST
3877 && valid1 && TREE_CODE (valid1) == STRING_CST
3878 && operand_equal_p (valid0, valid1, 1))
3879 return null_pointer_node;
3882 /* Support narrowing differences. */
3883 if (INTEGRAL_TYPE_P (endtype))
3885 tree op0, op1;
3887 op0 = TREE_OPERAND (value, 0);
3888 op1 = TREE_OPERAND (value, 1);
3890 /* Like STRIP_NOPS except allow the operand mode to widen.
3891 This works around a feature of fold that simplifies
3892 (int)(p1 - p2) to ((int)p1 - (int)p2) under the theory
3893 that the narrower operation is cheaper. */
3895 while (TREE_CODE (op0) == NOP_EXPR
3896 || TREE_CODE (op0) == CONVERT_EXPR
3897 || TREE_CODE (op0) == NON_LVALUE_EXPR)
3899 tree inner = TREE_OPERAND (op0, 0);
3900 if (inner == error_mark_node
3901 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
3902 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))
3903 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
3904 break;
3905 op0 = inner;
3908 while (TREE_CODE (op1) == NOP_EXPR
3909 || TREE_CODE (op1) == CONVERT_EXPR
3910 || TREE_CODE (op1) == NON_LVALUE_EXPR)
3912 tree inner = TREE_OPERAND (op1, 0);
3913 if (inner == error_mark_node
3914 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
3915 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op1)))
3916 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
3917 break;
3918 op1 = inner;
3921 op0 = initializer_constant_valid_p (op0, endtype);
3922 op1 = initializer_constant_valid_p (op1, endtype);
3924 /* Both initializers must be known. */
3925 if (op0 && op1)
3927 if (op0 == op1)
3928 return null_pointer_node;
3930 /* Support differences between labels. */
3931 if (TREE_CODE (op0) == LABEL_DECL
3932 && TREE_CODE (op1) == LABEL_DECL)
3933 return null_pointer_node;
3935 if (TREE_CODE (op0) == STRING_CST
3936 && TREE_CODE (op1) == STRING_CST
3937 && operand_equal_p (op0, op1, 1))
3938 return null_pointer_node;
3941 break;
3943 default:
3944 break;
3947 return 0;
3950 /* Output assembler code for constant EXP to FILE, with no label.
3951 This includes the pseudo-op such as ".int" or ".byte", and a newline.
3952 Assumes output_addressed_constants has been done on EXP already.
3954 Generate exactly SIZE bytes of assembler data, padding at the end
3955 with zeros if necessary. SIZE must always be specified.
3957 SIZE is important for structure constructors,
3958 since trailing members may have been omitted from the constructor.
3959 It is also important for initialization of arrays from string constants
3960 since the full length of the string constant might not be wanted.
3961 It is also needed for initialization of unions, where the initializer's
3962 type is just one member, and that may not be as long as the union.
3964 There a case in which we would fail to output exactly SIZE bytes:
3965 for a structure constructor that wants to produce more than SIZE bytes.
3966 But such constructors will never be generated for any possible input.
3968 ALIGN is the alignment of the data in bits. */
3970 void
3971 output_constant (tree exp, unsigned HOST_WIDE_INT size, unsigned int align)
3973 enum tree_code code;
3974 unsigned HOST_WIDE_INT thissize;
3976 /* Some front-ends use constants other than the standard language-independent
3977 varieties, but which may still be output directly. Give the front-end a
3978 chance to convert EXP to a language-independent representation. */
3979 exp = lang_hooks.expand_constant (exp);
3981 if (size == 0 || flag_syntax_only)
3982 return;
3984 /* See if we're trying to initialize a pointer in a non-default mode
3985 to the address of some declaration somewhere. If the target says
3986 the mode is valid for pointers, assume the target has a way of
3987 resolving it. */
3988 if (TREE_CODE (exp) == NOP_EXPR
3989 && POINTER_TYPE_P (TREE_TYPE (exp))
3990 && targetm.valid_pointer_mode (TYPE_MODE (TREE_TYPE (exp))))
3992 tree saved_type = TREE_TYPE (exp);
3994 /* Peel off any intermediate conversions-to-pointer for valid
3995 pointer modes. */
3996 while (TREE_CODE (exp) == NOP_EXPR
3997 && POINTER_TYPE_P (TREE_TYPE (exp))
3998 && targetm.valid_pointer_mode (TYPE_MODE (TREE_TYPE (exp))))
3999 exp = TREE_OPERAND (exp, 0);
4001 /* If what we're left with is the address of something, we can
4002 convert the address to the final type and output it that
4003 way. */
4004 if (TREE_CODE (exp) == ADDR_EXPR)
4005 exp = build1 (ADDR_EXPR, saved_type, TREE_OPERAND (exp, 0));
4006 /* Likewise for constant ints. */
4007 else if (TREE_CODE (exp) == INTEGER_CST)
4008 exp = build_int_cst_wide (saved_type, TREE_INT_CST_LOW (exp),
4009 TREE_INT_CST_HIGH (exp));
4013 /* Eliminate any conversions since we'll be outputting the underlying
4014 constant. */
4015 while (TREE_CODE (exp) == NOP_EXPR || TREE_CODE (exp) == CONVERT_EXPR
4016 || TREE_CODE (exp) == NON_LVALUE_EXPR
4017 || TREE_CODE (exp) == VIEW_CONVERT_EXPR)
4019 HOST_WIDE_INT type_size = int_size_in_bytes (TREE_TYPE (exp));
4020 HOST_WIDE_INT op_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (exp, 0)));
4022 /* Make sure eliminating the conversion is really a no-op, except with
4023 VIEW_CONVERT_EXPRs to allow for wild Ada unchecked conversions and
4024 union types to allow for Ada unchecked unions. */
4025 if (type_size > op_size
4026 && TREE_CODE (exp) != VIEW_CONVERT_EXPR
4027 && TREE_CODE (TREE_TYPE (exp)) != UNION_TYPE)
4028 internal_error ("no-op convert from %wd to %wd bytes in initializer",
4029 op_size, type_size);
4031 exp = TREE_OPERAND (exp, 0);
4034 code = TREE_CODE (TREE_TYPE (exp));
4035 thissize = int_size_in_bytes (TREE_TYPE (exp));
4037 /* Allow a constructor with no elements for any data type.
4038 This means to fill the space with zeros. */
4039 if (TREE_CODE (exp) == CONSTRUCTOR
4040 && VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (exp)))
4042 assemble_zeros (size);
4043 return;
4046 if (TREE_CODE (exp) == FDESC_EXPR)
4048 #ifdef ASM_OUTPUT_FDESC
4049 HOST_WIDE_INT part = tree_low_cst (TREE_OPERAND (exp, 1), 0);
4050 tree decl = TREE_OPERAND (exp, 0);
4051 ASM_OUTPUT_FDESC (asm_out_file, decl, part);
4052 #else
4053 gcc_unreachable ();
4054 #endif
4055 return;
4058 /* Now output the underlying data. If we've handling the padding, return.
4059 Otherwise, break and ensure SIZE is the size written. */
4060 switch (code)
4062 case BOOLEAN_TYPE:
4063 case INTEGER_TYPE:
4064 case ENUMERAL_TYPE:
4065 case POINTER_TYPE:
4066 case REFERENCE_TYPE:
4067 case OFFSET_TYPE:
4068 if (! assemble_integer (expand_expr (exp, NULL_RTX, VOIDmode,
4069 EXPAND_INITIALIZER),
4070 MIN (size, thissize), align, 0))
4071 error ("initializer for integer value is too complicated");
4072 break;
4074 case REAL_TYPE:
4075 if (TREE_CODE (exp) != REAL_CST)
4076 error ("initializer for floating value is not a floating constant");
4078 assemble_real (TREE_REAL_CST (exp), TYPE_MODE (TREE_TYPE (exp)), align);
4079 break;
4081 case COMPLEX_TYPE:
4082 output_constant (TREE_REALPART (exp), thissize / 2, align);
4083 output_constant (TREE_IMAGPART (exp), thissize / 2,
4084 min_align (align, BITS_PER_UNIT * (thissize / 2)));
4085 break;
4087 case ARRAY_TYPE:
4088 case VECTOR_TYPE:
4089 switch (TREE_CODE (exp))
4091 case CONSTRUCTOR:
4092 output_constructor (exp, size, align);
4093 return;
4094 case STRING_CST:
4095 thissize = MIN ((unsigned HOST_WIDE_INT)TREE_STRING_LENGTH (exp),
4096 size);
4097 assemble_string (TREE_STRING_POINTER (exp), thissize);
4098 break;
4100 case VECTOR_CST:
4102 int elt_size;
4103 tree link;
4104 unsigned int nalign;
4105 enum machine_mode inner;
4107 inner = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
4108 nalign = MIN (align, GET_MODE_ALIGNMENT (inner));
4110 elt_size = GET_MODE_SIZE (inner);
4112 link = TREE_VECTOR_CST_ELTS (exp);
4113 output_constant (TREE_VALUE (link), elt_size, align);
4114 while ((link = TREE_CHAIN (link)) != NULL)
4115 output_constant (TREE_VALUE (link), elt_size, nalign);
4116 break;
4118 default:
4119 gcc_unreachable ();
4121 break;
4123 case RECORD_TYPE:
4124 case UNION_TYPE:
4125 gcc_assert (TREE_CODE (exp) == CONSTRUCTOR);
4126 output_constructor (exp, size, align);
4127 return;
4129 case ERROR_MARK:
4130 return;
4132 default:
4133 gcc_unreachable ();
4136 if (size > thissize)
4137 assemble_zeros (size - thissize);
4141 /* Subroutine of output_constructor, used for computing the size of
4142 arrays of unspecified length. VAL must be a CONSTRUCTOR of an array
4143 type with an unspecified upper bound. */
4145 static unsigned HOST_WIDE_INT
4146 array_size_for_constructor (tree val)
4148 tree max_index, i;
4149 unsigned HOST_WIDE_INT cnt;
4150 tree index, value, tmp;
4152 /* This code used to attempt to handle string constants that are not
4153 arrays of single-bytes, but nothing else does, so there's no point in
4154 doing it here. */
4155 if (TREE_CODE (val) == STRING_CST)
4156 return TREE_STRING_LENGTH (val);
4158 max_index = NULL_TREE;
4159 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (val), cnt, index, value)
4161 if (TREE_CODE (index) == RANGE_EXPR)
4162 index = TREE_OPERAND (index, 1);
4163 if (max_index == NULL_TREE || tree_int_cst_lt (max_index, index))
4164 max_index = index;
4167 if (max_index == NULL_TREE)
4168 return 0;
4170 /* Compute the total number of array elements. */
4171 tmp = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (val)));
4172 i = size_binop (MINUS_EXPR, fold_convert (sizetype, max_index),
4173 fold_convert (sizetype, tmp));
4174 i = size_binop (PLUS_EXPR, i, build_int_cst (sizetype, 1));
4176 /* Multiply by the array element unit size to find number of bytes. */
4177 i = size_binop (MULT_EXPR, i, TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (val))));
4179 return tree_low_cst (i, 1);
4182 /* Subroutine of output_constant, used for CONSTRUCTORs (aggregate constants).
4183 Generate at least SIZE bytes, padding if necessary. */
4185 static void
4186 output_constructor (tree exp, unsigned HOST_WIDE_INT size,
4187 unsigned int align)
4189 tree type = TREE_TYPE (exp);
4190 tree field = 0;
4191 tree min_index = 0;
4192 /* Number of bytes output or skipped so far.
4193 In other words, current position within the constructor. */
4194 HOST_WIDE_INT total_bytes = 0;
4195 /* Nonzero means BYTE contains part of a byte, to be output. */
4196 int byte_buffer_in_use = 0;
4197 int byte = 0;
4198 unsigned HOST_WIDE_INT cnt;
4199 constructor_elt *ce;
4201 gcc_assert (HOST_BITS_PER_WIDE_INT >= BITS_PER_UNIT);
4203 if (TREE_CODE (type) == RECORD_TYPE)
4204 field = TYPE_FIELDS (type);
4206 if (TREE_CODE (type) == ARRAY_TYPE
4207 && TYPE_DOMAIN (type) != 0)
4208 min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
4210 /* As LINK goes through the elements of the constant,
4211 FIELD goes through the structure fields, if the constant is a structure.
4212 if the constant is a union, then we override this,
4213 by getting the field from the TREE_LIST element.
4214 But the constant could also be an array. Then FIELD is zero.
4216 There is always a maximum of one element in the chain LINK for unions
4217 (even if the initializer in a source program incorrectly contains
4218 more one). */
4219 for (cnt = 0;
4220 VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (exp), cnt, ce);
4221 cnt++, field = field ? TREE_CHAIN (field) : 0)
4223 tree val = ce->value;
4224 tree index = 0;
4226 /* The element in a union constructor specifies the proper field
4227 or index. */
4228 if ((TREE_CODE (type) == RECORD_TYPE || TREE_CODE (type) == UNION_TYPE
4229 || TREE_CODE (type) == QUAL_UNION_TYPE)
4230 && ce->index != 0)
4231 field = ce->index;
4233 else if (TREE_CODE (type) == ARRAY_TYPE)
4234 index = ce->index;
4236 #ifdef ASM_COMMENT_START
4237 if (field && flag_verbose_asm)
4238 fprintf (asm_out_file, "%s %s:\n",
4239 ASM_COMMENT_START,
4240 DECL_NAME (field)
4241 ? IDENTIFIER_POINTER (DECL_NAME (field))
4242 : "<anonymous>");
4243 #endif
4245 /* Eliminate the marker that makes a cast not be an lvalue. */
4246 if (val != 0)
4247 STRIP_NOPS (val);
4249 if (index && TREE_CODE (index) == RANGE_EXPR)
4251 unsigned HOST_WIDE_INT fieldsize
4252 = int_size_in_bytes (TREE_TYPE (type));
4253 HOST_WIDE_INT lo_index = tree_low_cst (TREE_OPERAND (index, 0), 0);
4254 HOST_WIDE_INT hi_index = tree_low_cst (TREE_OPERAND (index, 1), 0);
4255 HOST_WIDE_INT index;
4256 unsigned int align2 = min_align (align, fieldsize * BITS_PER_UNIT);
4258 for (index = lo_index; index <= hi_index; index++)
4260 /* Output the element's initial value. */
4261 if (val == 0)
4262 assemble_zeros (fieldsize);
4263 else
4264 output_constant (val, fieldsize, align2);
4266 /* Count its size. */
4267 total_bytes += fieldsize;
4270 else if (field == 0 || !DECL_BIT_FIELD (field))
4272 /* An element that is not a bit-field. */
4274 unsigned HOST_WIDE_INT fieldsize;
4275 /* Since this structure is static,
4276 we know the positions are constant. */
4277 HOST_WIDE_INT pos = field ? int_byte_position (field) : 0;
4278 unsigned int align2;
4280 if (index != 0)
4281 pos = (tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (val)), 1)
4282 * (tree_low_cst (index, 0) - tree_low_cst (min_index, 0)));
4284 /* Output any buffered-up bit-fields preceding this element. */
4285 if (byte_buffer_in_use)
4287 assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
4288 total_bytes++;
4289 byte_buffer_in_use = 0;
4292 /* Advance to offset of this element.
4293 Note no alignment needed in an array, since that is guaranteed
4294 if each element has the proper size. */
4295 if ((field != 0 || index != 0) && pos != total_bytes)
4297 gcc_assert (pos >= total_bytes);
4298 assemble_zeros (pos - total_bytes);
4299 total_bytes = pos;
4302 /* Find the alignment of this element. */
4303 align2 = min_align (align, BITS_PER_UNIT * pos);
4305 /* Determine size this element should occupy. */
4306 if (field)
4308 fieldsize = 0;
4310 /* If this is an array with an unspecified upper bound,
4311 the initializer determines the size. */
4312 /* ??? This ought to only checked if DECL_SIZE_UNIT is NULL,
4313 but we cannot do this until the deprecated support for
4314 initializing zero-length array members is removed. */
4315 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
4316 && TYPE_DOMAIN (TREE_TYPE (field))
4317 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
4319 fieldsize = array_size_for_constructor (val);
4320 /* Given a non-empty initialization, this field had
4321 better be last. */
4322 gcc_assert (!fieldsize || !TREE_CHAIN (field));
4324 else if (DECL_SIZE_UNIT (field))
4326 /* ??? This can't be right. If the decl size overflows
4327 a host integer we will silently emit no data. */
4328 if (host_integerp (DECL_SIZE_UNIT (field), 1))
4329 fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 1);
4332 else
4333 fieldsize = int_size_in_bytes (TREE_TYPE (type));
4335 /* Output the element's initial value. */
4336 if (val == 0)
4337 assemble_zeros (fieldsize);
4338 else
4339 output_constant (val, fieldsize, align2);
4341 /* Count its size. */
4342 total_bytes += fieldsize;
4344 else if (val != 0 && TREE_CODE (val) != INTEGER_CST)
4345 error ("invalid initial value for member %qs",
4346 IDENTIFIER_POINTER (DECL_NAME (field)));
4347 else
4349 /* Element that is a bit-field. */
4351 HOST_WIDE_INT next_offset = int_bit_position (field);
4352 HOST_WIDE_INT end_offset
4353 = (next_offset + tree_low_cst (DECL_SIZE (field), 1));
4355 if (val == 0)
4356 val = integer_zero_node;
4358 /* If this field does not start in this (or, next) byte,
4359 skip some bytes. */
4360 if (next_offset / BITS_PER_UNIT != total_bytes)
4362 /* Output remnant of any bit field in previous bytes. */
4363 if (byte_buffer_in_use)
4365 assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
4366 total_bytes++;
4367 byte_buffer_in_use = 0;
4370 /* If still not at proper byte, advance to there. */
4371 if (next_offset / BITS_PER_UNIT != total_bytes)
4373 gcc_assert (next_offset / BITS_PER_UNIT >= total_bytes);
4374 assemble_zeros (next_offset / BITS_PER_UNIT - total_bytes);
4375 total_bytes = next_offset / BITS_PER_UNIT;
4379 if (! byte_buffer_in_use)
4380 byte = 0;
4382 /* We must split the element into pieces that fall within
4383 separate bytes, and combine each byte with previous or
4384 following bit-fields. */
4386 /* next_offset is the offset n fbits from the beginning of
4387 the structure to the next bit of this element to be processed.
4388 end_offset is the offset of the first bit past the end of
4389 this element. */
4390 while (next_offset < end_offset)
4392 int this_time;
4393 int shift;
4394 HOST_WIDE_INT value;
4395 HOST_WIDE_INT next_byte = next_offset / BITS_PER_UNIT;
4396 HOST_WIDE_INT next_bit = next_offset % BITS_PER_UNIT;
4398 /* Advance from byte to byte
4399 within this element when necessary. */
4400 while (next_byte != total_bytes)
4402 assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
4403 total_bytes++;
4404 byte = 0;
4407 /* Number of bits we can process at once
4408 (all part of the same byte). */
4409 this_time = MIN (end_offset - next_offset,
4410 BITS_PER_UNIT - next_bit);
4411 if (BYTES_BIG_ENDIAN)
4413 /* On big-endian machine, take the most significant bits
4414 first (of the bits that are significant)
4415 and put them into bytes from the most significant end. */
4416 shift = end_offset - next_offset - this_time;
4418 /* Don't try to take a bunch of bits that cross
4419 the word boundary in the INTEGER_CST. We can
4420 only select bits from the LOW or HIGH part
4421 not from both. */
4422 if (shift < HOST_BITS_PER_WIDE_INT
4423 && shift + this_time > HOST_BITS_PER_WIDE_INT)
4425 this_time = shift + this_time - HOST_BITS_PER_WIDE_INT;
4426 shift = HOST_BITS_PER_WIDE_INT;
4429 /* Now get the bits from the appropriate constant word. */
4430 if (shift < HOST_BITS_PER_WIDE_INT)
4431 value = TREE_INT_CST_LOW (val);
4432 else
4434 gcc_assert (shift < 2 * HOST_BITS_PER_WIDE_INT);
4435 value = TREE_INT_CST_HIGH (val);
4436 shift -= HOST_BITS_PER_WIDE_INT;
4439 /* Get the result. This works only when:
4440 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
4441 byte |= (((value >> shift)
4442 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
4443 << (BITS_PER_UNIT - this_time - next_bit));
4445 else
4447 /* On little-endian machines,
4448 take first the least significant bits of the value
4449 and pack them starting at the least significant
4450 bits of the bytes. */
4451 shift = next_offset - int_bit_position (field);
4453 /* Don't try to take a bunch of bits that cross
4454 the word boundary in the INTEGER_CST. We can
4455 only select bits from the LOW or HIGH part
4456 not from both. */
4457 if (shift < HOST_BITS_PER_WIDE_INT
4458 && shift + this_time > HOST_BITS_PER_WIDE_INT)
4459 this_time = (HOST_BITS_PER_WIDE_INT - shift);
4461 /* Now get the bits from the appropriate constant word. */
4462 if (shift < HOST_BITS_PER_WIDE_INT)
4463 value = TREE_INT_CST_LOW (val);
4464 else
4466 gcc_assert (shift < 2 * HOST_BITS_PER_WIDE_INT);
4467 value = TREE_INT_CST_HIGH (val);
4468 shift -= HOST_BITS_PER_WIDE_INT;
4471 /* Get the result. This works only when:
4472 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
4473 byte |= (((value >> shift)
4474 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
4475 << next_bit);
4478 next_offset += this_time;
4479 byte_buffer_in_use = 1;
4484 if (byte_buffer_in_use)
4486 assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
4487 total_bytes++;
4490 if ((unsigned HOST_WIDE_INT)total_bytes < size)
4491 assemble_zeros (size - total_bytes);
4494 /* This TREE_LIST contains any weak symbol declarations waiting
4495 to be emitted. */
4496 static GTY(()) tree weak_decls;
4498 /* Mark DECL as weak. */
4500 static void
4501 mark_weak (tree decl)
4503 DECL_WEAK (decl) = 1;
4505 if (DECL_RTL_SET_P (decl)
4506 && MEM_P (DECL_RTL (decl))
4507 && XEXP (DECL_RTL (decl), 0)
4508 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == SYMBOL_REF)
4509 SYMBOL_REF_WEAK (XEXP (DECL_RTL (decl), 0)) = 1;
4512 /* Merge weak status between NEWDECL and OLDDECL. */
4514 void
4515 merge_weak (tree newdecl, tree olddecl)
4517 if (DECL_WEAK (newdecl) == DECL_WEAK (olddecl))
4519 if (DECL_WEAK (newdecl) && SUPPORTS_WEAK)
4521 tree *pwd;
4522 /* We put the NEWDECL on the weak_decls list at some point
4523 and OLDDECL as well. Keep just OLDDECL on the list. */
4524 for (pwd = &weak_decls; *pwd; pwd = &TREE_CHAIN (*pwd))
4525 if (TREE_VALUE (*pwd) == newdecl)
4527 *pwd = TREE_CHAIN (*pwd);
4528 break;
4531 return;
4534 if (DECL_WEAK (newdecl))
4536 tree wd;
4538 /* NEWDECL is weak, but OLDDECL is not. */
4540 /* If we already output the OLDDECL, we're in trouble; we can't
4541 go back and make it weak. This error cannot caught in
4542 declare_weak because the NEWDECL and OLDDECL was not yet
4543 been merged; therefore, TREE_ASM_WRITTEN was not set. */
4544 if (TREE_ASM_WRITTEN (olddecl))
4545 error ("weak declaration of %q+D must precede definition",
4546 newdecl);
4548 /* If we've already generated rtl referencing OLDDECL, we may
4549 have done so in a way that will not function properly with
4550 a weak symbol. */
4551 else if (TREE_USED (olddecl)
4552 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (olddecl)))
4553 warning (0, "weak declaration of %q+D after first use results "
4554 "in unspecified behavior", newdecl);
4556 if (SUPPORTS_WEAK)
4558 /* We put the NEWDECL on the weak_decls list at some point.
4559 Replace it with the OLDDECL. */
4560 for (wd = weak_decls; wd; wd = TREE_CHAIN (wd))
4561 if (TREE_VALUE (wd) == newdecl)
4563 TREE_VALUE (wd) = olddecl;
4564 break;
4566 /* We may not find the entry on the list. If NEWDECL is a
4567 weak alias, then we will have already called
4568 globalize_decl to remove the entry; in that case, we do
4569 not need to do anything. */
4572 /* Make the OLDDECL weak; it's OLDDECL that we'll be keeping. */
4573 mark_weak (olddecl);
4575 else
4576 /* OLDDECL was weak, but NEWDECL was not explicitly marked as
4577 weak. Just update NEWDECL to indicate that it's weak too. */
4578 mark_weak (newdecl);
4581 /* Declare DECL to be a weak symbol. */
4583 void
4584 declare_weak (tree decl)
4586 if (! TREE_PUBLIC (decl))
4587 error ("weak declaration of %q+D must be public", decl);
4588 else if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
4589 error ("weak declaration of %q+D must precede definition", decl);
4590 else if (SUPPORTS_WEAK)
4592 if (! DECL_WEAK (decl))
4593 weak_decls = tree_cons (NULL, decl, weak_decls);
4595 else
4596 warning (0, "weak declaration of %q+D not supported", decl);
4598 mark_weak (decl);
4601 static void
4602 weak_finish_1 (tree decl)
4604 #if defined (ASM_WEAKEN_DECL) || defined (ASM_WEAKEN_LABEL)
4605 const char *const name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
4606 #endif
4608 if (! TREE_USED (decl))
4609 return;
4611 #ifdef ASM_WEAKEN_DECL
4612 ASM_WEAKEN_DECL (asm_out_file, decl, name, NULL);
4613 #else
4614 #ifdef ASM_WEAKEN_LABEL
4615 ASM_WEAKEN_LABEL (asm_out_file, name);
4616 #else
4617 #ifdef ASM_OUTPUT_WEAK_ALIAS
4619 static bool warn_once = 0;
4620 if (! warn_once)
4622 warning (0, "only weak aliases are supported in this configuration");
4623 warn_once = 1;
4625 return;
4627 #endif
4628 #endif
4629 #endif
4632 /* This TREE_LIST contains weakref targets. */
4634 static GTY(()) tree weakref_targets;
4636 /* Forward declaration. */
4637 static tree find_decl_and_mark_needed (tree decl, tree target);
4639 /* Emit any pending weak declarations. */
4641 void
4642 weak_finish (void)
4644 tree t;
4646 for (t = weakref_targets; t; t = TREE_CHAIN (t))
4648 tree alias_decl = TREE_PURPOSE (t);
4649 tree target = ultimate_transparent_alias_target (&TREE_VALUE (t));
4651 if (! TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (alias_decl)))
4652 /* Remove alias_decl from the weak list, but leave entries for
4653 the target alone. */
4654 target = NULL_TREE;
4655 #ifndef ASM_OUTPUT_WEAKREF
4656 else if (! TREE_SYMBOL_REFERENCED (target))
4658 /* Use ASM_WEAKEN_LABEL only if ASM_WEAKEN_DECL is not
4659 defined, otherwise we and weak_finish_1 would use a
4660 different macros. */
4661 # if defined ASM_WEAKEN_LABEL && ! defined ASM_WEAKEN_DECL
4662 ASM_WEAKEN_LABEL (asm_out_file, IDENTIFIER_POINTER (target));
4663 # else
4664 tree decl = find_decl_and_mark_needed (alias_decl, target);
4666 if (! decl)
4668 decl = build_decl (TREE_CODE (alias_decl), target,
4669 TREE_TYPE (alias_decl));
4671 DECL_EXTERNAL (decl) = 1;
4672 TREE_PUBLIC (decl) = 1;
4673 DECL_ARTIFICIAL (decl) = 1;
4674 TREE_NOTHROW (decl) = TREE_NOTHROW (alias_decl);
4675 TREE_USED (decl) = 1;
4678 weak_finish_1 (decl);
4679 # endif
4681 #endif
4684 tree *p;
4685 tree t2;
4687 /* Remove the alias and the target from the pending weak list
4688 so that we do not emit any .weak directives for the former,
4689 nor multiple .weak directives for the latter. */
4690 for (p = &weak_decls; (t2 = *p) ; )
4692 if (TREE_VALUE (t2) == alias_decl
4693 || target == DECL_ASSEMBLER_NAME (TREE_VALUE (t2)))
4694 *p = TREE_CHAIN (t2);
4695 else
4696 p = &TREE_CHAIN (t2);
4699 /* Remove other weakrefs to the same target, to speed things up. */
4700 for (p = &TREE_CHAIN (t); (t2 = *p) ; )
4702 if (target == ultimate_transparent_alias_target (&TREE_VALUE (t2)))
4703 *p = TREE_CHAIN (t2);
4704 else
4705 p = &TREE_CHAIN (t2);
4710 for (t = weak_decls; t; t = TREE_CHAIN (t))
4712 tree decl = TREE_VALUE (t);
4714 weak_finish_1 (decl);
4718 /* Emit the assembly bits to indicate that DECL is globally visible. */
4720 static void
4721 globalize_decl (tree decl)
4723 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
4725 #if defined (ASM_WEAKEN_LABEL) || defined (ASM_WEAKEN_DECL)
4726 if (DECL_WEAK (decl))
4728 tree *p, t;
4730 #ifdef ASM_WEAKEN_DECL
4731 ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
4732 #else
4733 ASM_WEAKEN_LABEL (asm_out_file, name);
4734 #endif
4736 /* Remove this function from the pending weak list so that
4737 we do not emit multiple .weak directives for it. */
4738 for (p = &weak_decls; (t = *p) ; )
4740 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
4741 *p = TREE_CHAIN (t);
4742 else
4743 p = &TREE_CHAIN (t);
4746 /* Remove weakrefs to the same target from the pending weakref
4747 list, for the same reason. */
4748 for (p = &weakref_targets; (t = *p) ; )
4750 if (DECL_ASSEMBLER_NAME (decl)
4751 == ultimate_transparent_alias_target (&TREE_VALUE (t)))
4752 *p = TREE_CHAIN (t);
4753 else
4754 p = &TREE_CHAIN (t);
4757 return;
4759 #elif defined(ASM_MAKE_LABEL_LINKONCE)
4760 if (DECL_ONE_ONLY (decl))
4761 ASM_MAKE_LABEL_LINKONCE (asm_out_file, name);
4762 #endif
4764 targetm.asm_out.globalize_label (asm_out_file, name);
4767 /* We have to be able to tell cgraph about the needed-ness of the target
4768 of an alias. This requires that the decl have been defined. Aliases
4769 that precede their definition have to be queued for later processing. */
4771 typedef struct alias_pair GTY(())
4773 tree decl;
4774 tree target;
4775 } alias_pair;
4777 /* Define gc'd vector type. */
4778 DEF_VEC_O(alias_pair);
4779 DEF_VEC_ALLOC_O(alias_pair,gc);
4781 static GTY(()) VEC(alias_pair,gc) *alias_pairs;
4783 /* Given an assembly name, find the decl it is associated with. At the
4784 same time, mark it needed for cgraph. */
4786 static tree
4787 find_decl_and_mark_needed (tree decl, tree target)
4789 struct cgraph_node *fnode = NULL;
4790 struct cgraph_varpool_node *vnode = NULL;
4792 if (TREE_CODE (decl) == FUNCTION_DECL)
4794 fnode = cgraph_node_for_asm (target);
4795 if (fnode == NULL)
4796 vnode = cgraph_varpool_node_for_asm (target);
4798 else
4800 vnode = cgraph_varpool_node_for_asm (target);
4801 if (vnode == NULL)
4802 fnode = cgraph_node_for_asm (target);
4805 if (fnode)
4807 /* We can't mark function nodes as used after cgraph global info
4808 is finished. This wouldn't generally be necessary, but C++
4809 virtual table thunks are introduced late in the game and
4810 might seem like they need marking, although in fact they
4811 don't. */
4812 if (! cgraph_global_info_ready)
4813 cgraph_mark_needed_node (fnode);
4814 return fnode->decl;
4816 else if (vnode)
4818 cgraph_varpool_mark_needed_node (vnode);
4819 return vnode->decl;
4821 else
4822 return NULL_TREE;
4825 /* Output the assembler code for a define (equate) using ASM_OUTPUT_DEF
4826 or ASM_OUTPUT_DEF_FROM_DECLS. The function defines the symbol whose
4827 tree node is DECL to have the value of the tree node TARGET. */
4829 static void
4830 do_assemble_alias (tree decl, tree target)
4832 if (TREE_ASM_WRITTEN (decl))
4833 return;
4835 TREE_ASM_WRITTEN (decl) = 1;
4836 TREE_ASM_WRITTEN (DECL_ASSEMBLER_NAME (decl)) = 1;
4838 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
4840 ultimate_transparent_alias_target (&target);
4842 if (!TREE_SYMBOL_REFERENCED (target))
4843 weakref_targets = tree_cons (decl, target, weakref_targets);
4845 #ifdef ASM_OUTPUT_WEAKREF
4846 ASM_OUTPUT_WEAKREF (asm_out_file, decl,
4847 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
4848 IDENTIFIER_POINTER (target));
4849 #else
4850 if (!SUPPORTS_WEAK)
4852 error ("%Jweakref is not supported in this configuration", decl);
4853 return;
4855 #endif
4856 return;
4859 #ifdef ASM_OUTPUT_DEF
4860 /* Make name accessible from other files, if appropriate. */
4862 if (TREE_PUBLIC (decl))
4864 globalize_decl (decl);
4865 maybe_assemble_visibility (decl);
4868 # ifdef ASM_OUTPUT_DEF_FROM_DECLS
4869 ASM_OUTPUT_DEF_FROM_DECLS (asm_out_file, decl, target);
4870 # else
4871 ASM_OUTPUT_DEF (asm_out_file,
4872 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
4873 IDENTIFIER_POINTER (target));
4874 # endif
4875 #elif defined (ASM_OUTPUT_WEAK_ALIAS) || defined (ASM_WEAKEN_DECL)
4877 const char *name;
4878 tree *p, t;
4880 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
4881 # ifdef ASM_WEAKEN_DECL
4882 ASM_WEAKEN_DECL (asm_out_file, decl, name, IDENTIFIER_POINTER (target));
4883 # else
4884 ASM_OUTPUT_WEAK_ALIAS (asm_out_file, name, IDENTIFIER_POINTER (target));
4885 # endif
4886 /* Remove this function from the pending weak list so that
4887 we do not emit multiple .weak directives for it. */
4888 for (p = &weak_decls; (t = *p) ; )
4889 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
4890 *p = TREE_CHAIN (t);
4891 else
4892 p = &TREE_CHAIN (t);
4894 /* Remove weakrefs to the same target from the pending weakref
4895 list, for the same reason. */
4896 for (p = &weakref_targets; (t = *p) ; )
4898 if (DECL_ASSEMBLER_NAME (decl)
4899 == ultimate_transparent_alias_target (&TREE_VALUE (t)))
4900 *p = TREE_CHAIN (t);
4901 else
4902 p = &TREE_CHAIN (t);
4905 #endif
4908 /* First pass of completing pending aliases. Make sure that cgraph knows
4909 which symbols will be required. */
4911 void
4912 finish_aliases_1 (void)
4914 unsigned i;
4915 alias_pair *p;
4917 for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); i++)
4919 tree target_decl;
4921 target_decl = find_decl_and_mark_needed (p->decl, p->target);
4922 if (target_decl == NULL)
4924 if (! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
4925 error ("%q+D aliased to undefined symbol %qs",
4926 p->decl, IDENTIFIER_POINTER (p->target));
4928 else if (DECL_EXTERNAL (target_decl)
4929 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
4930 error ("%q+D aliased to external symbol %qs",
4931 p->decl, IDENTIFIER_POINTER (p->target));
4935 /* Second pass of completing pending aliases. Emit the actual assembly.
4936 This happens at the end of compilation and thus it is assured that the
4937 target symbol has been emitted. */
4939 void
4940 finish_aliases_2 (void)
4942 unsigned i;
4943 alias_pair *p;
4945 for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); i++)
4946 do_assemble_alias (p->decl, p->target);
4948 VEC_truncate (alias_pair, alias_pairs, 0);
4951 /* Emit an assembler directive to make the symbol for DECL an alias to
4952 the symbol for TARGET. */
4954 void
4955 assemble_alias (tree decl, tree target)
4957 tree target_decl;
4958 bool is_weakref = false;
4960 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
4962 tree alias = DECL_ASSEMBLER_NAME (decl);
4964 is_weakref = true;
4966 ultimate_transparent_alias_target (&target);
4968 if (alias == target)
4969 error ("weakref %q+D ultimately targets itself", decl);
4970 else
4972 #ifndef ASM_OUTPUT_WEAKREF
4973 IDENTIFIER_TRANSPARENT_ALIAS (alias) = 1;
4974 TREE_CHAIN (alias) = target;
4975 #endif
4977 if (TREE_PUBLIC (decl))
4978 error ("weakref %q+D must have static linkage", decl);
4980 else
4982 #if !defined (ASM_OUTPUT_DEF)
4983 # if !defined(ASM_OUTPUT_WEAK_ALIAS) && !defined (ASM_WEAKEN_DECL)
4984 error ("%Jalias definitions not supported in this configuration", decl);
4985 return;
4986 # else
4987 if (!DECL_WEAK (decl))
4989 error ("%Jonly weak aliases are supported in this configuration", decl);
4990 return;
4992 # endif
4993 #endif
4996 /* We must force creation of DECL_RTL for debug info generation, even though
4997 we don't use it here. */
4998 make_decl_rtl (decl);
4999 TREE_USED (decl) = 1;
5001 /* A quirk of the initial implementation of aliases required that the user
5002 add "extern" to all of them. Which is silly, but now historical. Do
5003 note that the symbol is in fact locally defined. */
5004 if (! is_weakref)
5005 DECL_EXTERNAL (decl) = 0;
5007 /* Allow aliases to aliases. */
5008 if (TREE_CODE (decl) == FUNCTION_DECL)
5009 cgraph_node (decl)->alias = true;
5010 else
5011 cgraph_varpool_node (decl)->alias = true;
5013 /* If the target has already been emitted, we don't have to queue the
5014 alias. This saves a tad o memory. */
5015 target_decl = find_decl_and_mark_needed (decl, target);
5016 if (target_decl && TREE_ASM_WRITTEN (target_decl))
5017 do_assemble_alias (decl, target);
5018 else
5020 alias_pair *p = VEC_safe_push (alias_pair, gc, alias_pairs, NULL);
5021 p->decl = decl;
5022 p->target = target;
5026 /* Emit an assembler directive to set symbol for DECL visibility to
5027 the visibility type VIS, which must not be VISIBILITY_DEFAULT. */
5029 void
5030 default_assemble_visibility (tree decl, int vis)
5032 static const char * const visibility_types[] = {
5033 NULL, "internal", "hidden", "protected"
5036 const char *name, *type;
5038 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5039 type = visibility_types[vis];
5041 #ifdef HAVE_GAS_HIDDEN
5042 fprintf (asm_out_file, "\t.%s\t", type);
5043 assemble_name (asm_out_file, name);
5044 fprintf (asm_out_file, "\n");
5045 #else
5046 warning (OPT_Wattributes, "visibility attribute not supported "
5047 "in this configuration; ignored");
5048 #endif
5051 /* A helper function to call assemble_visibility when needed for a decl. */
5053 static void
5054 maybe_assemble_visibility (tree decl)
5056 enum symbol_visibility vis = DECL_VISIBILITY (decl);
5058 if (vis != VISIBILITY_DEFAULT)
5059 targetm.asm_out.visibility (decl, vis);
5062 /* Returns 1 if the target configuration supports defining public symbols
5063 so that one of them will be chosen at link time instead of generating a
5064 multiply-defined symbol error, whether through the use of weak symbols or
5065 a target-specific mechanism for having duplicates discarded. */
5068 supports_one_only (void)
5070 if (SUPPORTS_ONE_ONLY)
5071 return 1;
5072 return SUPPORTS_WEAK;
5075 /* Set up DECL as a public symbol that can be defined in multiple
5076 translation units without generating a linker error. */
5078 void
5079 make_decl_one_only (tree decl)
5081 gcc_assert (TREE_CODE (decl) == VAR_DECL
5082 || TREE_CODE (decl) == FUNCTION_DECL);
5084 TREE_PUBLIC (decl) = 1;
5086 if (SUPPORTS_ONE_ONLY)
5088 #ifdef MAKE_DECL_ONE_ONLY
5089 MAKE_DECL_ONE_ONLY (decl);
5090 #endif
5091 DECL_ONE_ONLY (decl) = 1;
5093 else if (TREE_CODE (decl) == VAR_DECL
5094 && (DECL_INITIAL (decl) == 0 || DECL_INITIAL (decl) == error_mark_node))
5095 DECL_COMMON (decl) = 1;
5096 else
5098 gcc_assert (SUPPORTS_WEAK);
5099 DECL_WEAK (decl) = 1;
5103 void
5104 init_varasm_once (void)
5106 section_htab = htab_create_ggc (31, section_entry_hash,
5107 section_entry_eq, NULL);
5108 object_block_htab = htab_create_ggc (31, object_block_entry_hash,
5109 object_block_entry_eq, NULL);
5110 const_desc_htab = htab_create_ggc (1009, const_desc_hash,
5111 const_desc_eq, NULL);
5113 const_alias_set = new_alias_set ();
5114 shared_constant_pool = create_constant_pool ();
5116 #ifdef TEXT_SECTION_ASM_OP
5117 text_section = get_unnamed_section (SECTION_CODE, output_section_asm_op,
5118 TEXT_SECTION_ASM_OP);
5119 #endif
5121 #ifdef DATA_SECTION_ASM_OP
5122 data_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
5123 DATA_SECTION_ASM_OP);
5124 #endif
5126 #ifdef SDATA_SECTION_ASM_OP
5127 sdata_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
5128 SDATA_SECTION_ASM_OP);
5129 #endif
5131 #ifdef READONLY_DATA_SECTION_ASM_OP
5132 readonly_data_section = get_unnamed_section (0, output_section_asm_op,
5133 READONLY_DATA_SECTION_ASM_OP);
5134 #endif
5136 #ifdef CTORS_SECTION_ASM_OP
5137 ctors_section = get_unnamed_section (0, output_section_asm_op,
5138 CTORS_SECTION_ASM_OP);
5139 #endif
5141 #ifdef DTORS_SECTION_ASM_OP
5142 dtors_section = get_unnamed_section (0, output_section_asm_op,
5143 DTORS_SECTION_ASM_OP);
5144 #endif
5146 #ifdef BSS_SECTION_ASM_OP
5147 bss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
5148 output_section_asm_op,
5149 BSS_SECTION_ASM_OP);
5150 #endif
5152 #ifdef SBSS_SECTION_ASM_OP
5153 sbss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
5154 output_section_asm_op,
5155 SBSS_SECTION_ASM_OP);
5156 #endif
5158 tls_comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
5159 | SECTION_COMMON, emit_tls_common);
5160 lcomm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
5161 | SECTION_COMMON, emit_local);
5162 comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
5163 | SECTION_COMMON, emit_common);
5165 #if defined ASM_OUTPUT_ALIGNED_BSS || defined ASM_OUTPUT_BSS
5166 bss_noswitch_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS,
5167 emit_bss);
5168 #endif
5170 targetm.asm_out.init_sections ();
5172 if (readonly_data_section == NULL)
5173 readonly_data_section = text_section;
5176 enum tls_model
5177 decl_default_tls_model (tree decl)
5179 enum tls_model kind;
5180 bool is_local;
5182 is_local = targetm.binds_local_p (decl);
5183 if (!flag_shlib)
5185 if (is_local)
5186 kind = TLS_MODEL_LOCAL_EXEC;
5187 else
5188 kind = TLS_MODEL_INITIAL_EXEC;
5191 /* Local dynamic is inefficient when we're not combining the
5192 parts of the address. */
5193 else if (optimize && is_local)
5194 kind = TLS_MODEL_LOCAL_DYNAMIC;
5195 else
5196 kind = TLS_MODEL_GLOBAL_DYNAMIC;
5197 if (kind < flag_tls_default)
5198 kind = flag_tls_default;
5200 return kind;
5203 /* Select a set of attributes for section NAME based on the properties
5204 of DECL and whether or not RELOC indicates that DECL's initializer
5205 might contain runtime relocations.
5207 We make the section read-only and executable for a function decl,
5208 read-only for a const data decl, and writable for a non-const data decl. */
5210 unsigned int
5211 default_section_type_flags (tree decl, const char *name, int reloc)
5213 return default_section_type_flags_1 (decl, name, reloc, flag_pic);
5216 unsigned int
5217 default_section_type_flags_1 (tree decl, const char *name, int reloc,
5218 int shlib)
5220 unsigned int flags;
5222 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
5223 flags = SECTION_CODE;
5224 else if (decl && decl_readonly_section_1 (decl, reloc, shlib))
5225 flags = 0;
5226 else if (current_function_decl
5227 && cfun
5228 && cfun->unlikely_text_section_name
5229 && strcmp (name, cfun->unlikely_text_section_name) == 0)
5230 flags = SECTION_CODE;
5231 else if (!decl
5232 && (!current_function_decl || !cfun)
5233 && strcmp (name, UNLIKELY_EXECUTED_TEXT_SECTION_NAME) == 0)
5234 flags = SECTION_CODE;
5235 else
5236 flags = SECTION_WRITE;
5238 if (decl && DECL_ONE_ONLY (decl))
5239 flags |= SECTION_LINKONCE;
5241 if (decl && TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
5242 flags |= SECTION_TLS | SECTION_WRITE;
5244 if (strcmp (name, ".bss") == 0
5245 || strncmp (name, ".bss.", 5) == 0
5246 || strncmp (name, ".gnu.linkonce.b.", 16) == 0
5247 || strcmp (name, ".sbss") == 0
5248 || strncmp (name, ".sbss.", 6) == 0
5249 || strncmp (name, ".gnu.linkonce.sb.", 17) == 0)
5250 flags |= SECTION_BSS;
5252 if (strcmp (name, ".tdata") == 0
5253 || strncmp (name, ".tdata.", 7) == 0
5254 || strncmp (name, ".gnu.linkonce.td.", 17) == 0)
5255 flags |= SECTION_TLS;
5257 if (strcmp (name, ".tbss") == 0
5258 || strncmp (name, ".tbss.", 6) == 0
5259 || strncmp (name, ".gnu.linkonce.tb.", 17) == 0)
5260 flags |= SECTION_TLS | SECTION_BSS;
5262 /* These three sections have special ELF types. They are neither
5263 SHT_PROGBITS nor SHT_NOBITS, so when changing sections we don't
5264 want to print a section type (@progbits or @nobits). If someone
5265 is silly enough to emit code or TLS variables to one of these
5266 sections, then don't handle them specially. */
5267 if (!(flags & (SECTION_CODE | SECTION_BSS | SECTION_TLS))
5268 && (strcmp (name, ".init_array") == 0
5269 || strcmp (name, ".fini_array") == 0
5270 || strcmp (name, ".preinit_array") == 0))
5271 flags |= SECTION_NOTYPE;
5273 return flags;
5276 /* Return true if the target supports some form of global BSS,
5277 either through bss_noswitch_section, or by selecting a BSS
5278 section in TARGET_ASM_SELECT_SECTION. */
5280 bool
5281 have_global_bss_p (void)
5283 return bss_noswitch_section || targetm.have_switchable_bss_sections;
5286 /* Output assembly to switch to section NAME with attribute FLAGS.
5287 Four variants for common object file formats. */
5289 void
5290 default_no_named_section (const char *name ATTRIBUTE_UNUSED,
5291 unsigned int flags ATTRIBUTE_UNUSED,
5292 tree decl ATTRIBUTE_UNUSED)
5294 /* Some object formats don't support named sections at all. The
5295 front-end should already have flagged this as an error. */
5296 gcc_unreachable ();
5299 void
5300 default_elf_asm_named_section (const char *name, unsigned int flags,
5301 tree decl ATTRIBUTE_UNUSED)
5303 char flagchars[10], *f = flagchars;
5305 /* If we have already declared this section, we can use an
5306 abbreviated form to switch back to it -- unless this section is
5307 part of a COMDAT groups, in which case GAS requires the full
5308 declaration every time. */
5309 if (!(HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
5310 && (flags & SECTION_DECLARED))
5312 fprintf (asm_out_file, "\t.section\t%s\n", name);
5313 return;
5316 if (!(flags & SECTION_DEBUG))
5317 *f++ = 'a';
5318 if (flags & SECTION_WRITE)
5319 *f++ = 'w';
5320 if (flags & SECTION_CODE)
5321 *f++ = 'x';
5322 if (flags & SECTION_SMALL)
5323 *f++ = 's';
5324 if (flags & SECTION_MERGE)
5325 *f++ = 'M';
5326 if (flags & SECTION_STRINGS)
5327 *f++ = 'S';
5328 if (flags & SECTION_TLS)
5329 *f++ = 'T';
5330 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
5331 *f++ = 'G';
5332 *f = '\0';
5334 fprintf (asm_out_file, "\t.section\t%s,\"%s\"", name, flagchars);
5336 if (!(flags & SECTION_NOTYPE))
5338 const char *type;
5339 const char *format;
5341 if (flags & SECTION_BSS)
5342 type = "nobits";
5343 else
5344 type = "progbits";
5346 format = ",@%s";
5347 #ifdef ASM_COMMENT_START
5348 /* On platforms that use "@" as the assembly comment character,
5349 use "%" instead. */
5350 if (strcmp (ASM_COMMENT_START, "@") == 0)
5351 format = ",%%%s";
5352 #endif
5353 fprintf (asm_out_file, format, type);
5355 if (flags & SECTION_ENTSIZE)
5356 fprintf (asm_out_file, ",%d", flags & SECTION_ENTSIZE);
5357 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
5358 fprintf (asm_out_file, ",%s,comdat",
5359 lang_hooks.decls.comdat_group (decl));
5362 putc ('\n', asm_out_file);
5365 void
5366 default_coff_asm_named_section (const char *name, unsigned int flags,
5367 tree decl ATTRIBUTE_UNUSED)
5369 char flagchars[8], *f = flagchars;
5371 if (flags & SECTION_WRITE)
5372 *f++ = 'w';
5373 if (flags & SECTION_CODE)
5374 *f++ = 'x';
5375 *f = '\0';
5377 fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
5380 void
5381 default_pe_asm_named_section (const char *name, unsigned int flags,
5382 tree decl)
5384 default_coff_asm_named_section (name, flags, decl);
5386 if (flags & SECTION_LINKONCE)
5388 /* Functions may have been compiled at various levels of
5389 optimization so we can't use `same_size' here.
5390 Instead, have the linker pick one. */
5391 fprintf (asm_out_file, "\t.linkonce %s\n",
5392 (flags & SECTION_CODE ? "discard" : "same_size"));
5396 /* The lame default section selector. */
5398 section *
5399 default_select_section (tree decl, int reloc,
5400 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
5402 if (DECL_P (decl))
5404 if (decl_readonly_section (decl, reloc))
5405 return readonly_data_section;
5407 else if (TREE_CODE (decl) == CONSTRUCTOR)
5409 if (! ((flag_pic && reloc)
5410 || !TREE_READONLY (decl)
5411 || TREE_SIDE_EFFECTS (decl)
5412 || !TREE_CONSTANT (decl)))
5413 return readonly_data_section;
5415 else if (TREE_CODE (decl) == STRING_CST)
5416 return readonly_data_section;
5417 else if (! (flag_pic && reloc))
5418 return readonly_data_section;
5420 return data_section;
5423 enum section_category
5424 categorize_decl_for_section (tree decl, int reloc, int shlib)
5426 enum section_category ret;
5428 if (TREE_CODE (decl) == FUNCTION_DECL)
5429 return SECCAT_TEXT;
5430 else if (TREE_CODE (decl) == STRING_CST)
5432 if (flag_mudflap) /* or !flag_merge_constants */
5433 return SECCAT_RODATA;
5434 else
5435 return SECCAT_RODATA_MERGE_STR;
5437 else if (TREE_CODE (decl) == VAR_DECL)
5439 if (bss_initializer_p (decl))
5440 ret = SECCAT_BSS;
5441 else if (! TREE_READONLY (decl)
5442 || TREE_SIDE_EFFECTS (decl)
5443 || ! TREE_CONSTANT (DECL_INITIAL (decl)))
5445 if (shlib && (reloc & 2))
5446 ret = SECCAT_DATA_REL;
5447 else if (shlib && reloc)
5448 ret = SECCAT_DATA_REL_LOCAL;
5449 else
5450 ret = SECCAT_DATA;
5452 else if (shlib && (reloc & 2))
5453 ret = SECCAT_DATA_REL_RO;
5454 else if (shlib && reloc)
5455 ret = SECCAT_DATA_REL_RO_LOCAL;
5456 else if (reloc || flag_merge_constants < 2)
5457 /* C and C++ don't allow different variables to share the same
5458 location. -fmerge-all-constants allows even that (at the
5459 expense of not conforming). */
5460 ret = SECCAT_RODATA;
5461 else if (TREE_CODE (DECL_INITIAL (decl)) == STRING_CST)
5462 ret = SECCAT_RODATA_MERGE_STR_INIT;
5463 else
5464 ret = SECCAT_RODATA_MERGE_CONST;
5466 else if (TREE_CODE (decl) == CONSTRUCTOR)
5468 if ((shlib && reloc)
5469 || TREE_SIDE_EFFECTS (decl)
5470 || ! TREE_CONSTANT (decl))
5471 ret = SECCAT_DATA;
5472 else
5473 ret = SECCAT_RODATA;
5475 else
5476 ret = SECCAT_RODATA;
5478 /* There are no read-only thread-local sections. */
5479 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
5481 /* Note that this would be *just* SECCAT_BSS, except that there's
5482 no concept of a read-only thread-local-data section. */
5483 if (ret == SECCAT_BSS
5484 || (flag_zero_initialized_in_bss
5485 && initializer_zerop (DECL_INITIAL (decl))))
5486 ret = SECCAT_TBSS;
5487 else
5488 ret = SECCAT_TDATA;
5491 /* If the target uses small data sections, select it. */
5492 else if (targetm.in_small_data_p (decl))
5494 if (ret == SECCAT_BSS)
5495 ret = SECCAT_SBSS;
5496 else if (targetm.have_srodata_section && ret == SECCAT_RODATA)
5497 ret = SECCAT_SRODATA;
5498 else
5499 ret = SECCAT_SDATA;
5502 return ret;
5505 bool
5506 decl_readonly_section (tree decl, int reloc)
5508 return decl_readonly_section_1 (decl, reloc, flag_pic);
5511 bool
5512 decl_readonly_section_1 (tree decl, int reloc, int shlib)
5514 switch (categorize_decl_for_section (decl, reloc, shlib))
5516 case SECCAT_RODATA:
5517 case SECCAT_RODATA_MERGE_STR:
5518 case SECCAT_RODATA_MERGE_STR_INIT:
5519 case SECCAT_RODATA_MERGE_CONST:
5520 case SECCAT_SRODATA:
5521 return true;
5522 break;
5523 default:
5524 return false;
5525 break;
5529 /* Select a section based on the above categorization. */
5531 section *
5532 default_elf_select_section (tree decl, int reloc,
5533 unsigned HOST_WIDE_INT align)
5535 return default_elf_select_section_1 (decl, reloc, align, flag_pic);
5538 section *
5539 default_elf_select_section_1 (tree decl, int reloc,
5540 unsigned HOST_WIDE_INT align, int shlib)
5542 const char *sname;
5543 switch (categorize_decl_for_section (decl, reloc, shlib))
5545 case SECCAT_TEXT:
5546 /* We're not supposed to be called on FUNCTION_DECLs. */
5547 gcc_unreachable ();
5548 case SECCAT_RODATA:
5549 return readonly_data_section;
5550 case SECCAT_RODATA_MERGE_STR:
5551 return mergeable_string_section (decl, align, 0);
5552 case SECCAT_RODATA_MERGE_STR_INIT:
5553 return mergeable_string_section (DECL_INITIAL (decl), align, 0);
5554 case SECCAT_RODATA_MERGE_CONST:
5555 return mergeable_constant_section (DECL_MODE (decl), align, 0);
5556 case SECCAT_SRODATA:
5557 sname = ".sdata2";
5558 break;
5559 case SECCAT_DATA:
5560 return data_section;
5561 case SECCAT_DATA_REL:
5562 sname = ".data.rel";
5563 break;
5564 case SECCAT_DATA_REL_LOCAL:
5565 sname = ".data.rel.local";
5566 break;
5567 case SECCAT_DATA_REL_RO:
5568 sname = ".data.rel.ro";
5569 break;
5570 case SECCAT_DATA_REL_RO_LOCAL:
5571 sname = ".data.rel.ro.local";
5572 break;
5573 case SECCAT_SDATA:
5574 sname = ".sdata";
5575 break;
5576 case SECCAT_TDATA:
5577 sname = ".tdata";
5578 break;
5579 case SECCAT_BSS:
5580 if (bss_section)
5581 return bss_section;
5582 sname = ".bss";
5583 break;
5584 case SECCAT_SBSS:
5585 sname = ".sbss";
5586 break;
5587 case SECCAT_TBSS:
5588 sname = ".tbss";
5589 break;
5590 default:
5591 gcc_unreachable ();
5594 if (!DECL_P (decl))
5595 decl = NULL_TREE;
5596 return get_named_section (decl, sname, reloc);
5599 /* Construct a unique section name based on the decl name and the
5600 categorization performed above. */
5602 void
5603 default_unique_section (tree decl, int reloc)
5605 default_unique_section_1 (decl, reloc, flag_pic);
5608 void
5609 default_unique_section_1 (tree decl, int reloc, int shlib)
5611 /* We only need to use .gnu.linkonce if we don't have COMDAT groups. */
5612 bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
5613 const char *prefix, *name;
5614 size_t nlen, plen;
5615 char *string;
5617 switch (categorize_decl_for_section (decl, reloc, shlib))
5619 case SECCAT_TEXT:
5620 prefix = one_only ? ".gnu.linkonce.t." : ".text.";
5621 break;
5622 case SECCAT_RODATA:
5623 case SECCAT_RODATA_MERGE_STR:
5624 case SECCAT_RODATA_MERGE_STR_INIT:
5625 case SECCAT_RODATA_MERGE_CONST:
5626 prefix = one_only ? ".gnu.linkonce.r." : ".rodata.";
5627 break;
5628 case SECCAT_SRODATA:
5629 prefix = one_only ? ".gnu.linkonce.s2." : ".sdata2.";
5630 break;
5631 case SECCAT_DATA:
5632 prefix = one_only ? ".gnu.linkonce.d." : ".data.";
5633 break;
5634 case SECCAT_DATA_REL:
5635 prefix = one_only ? ".gnu.linkonce.d.rel." : ".data.rel.";
5636 break;
5637 case SECCAT_DATA_REL_LOCAL:
5638 prefix = one_only ? ".gnu.linkonce.d.rel.local." : ".data.rel.local.";
5639 break;
5640 case SECCAT_DATA_REL_RO:
5641 prefix = one_only ? ".gnu.linkonce.d.rel.ro." : ".data.rel.ro.";
5642 break;
5643 case SECCAT_DATA_REL_RO_LOCAL:
5644 prefix = one_only ? ".gnu.linkonce.d.rel.ro.local."
5645 : ".data.rel.ro.local.";
5646 break;
5647 case SECCAT_SDATA:
5648 prefix = one_only ? ".gnu.linkonce.s." : ".sdata.";
5649 break;
5650 case SECCAT_BSS:
5651 prefix = one_only ? ".gnu.linkonce.b." : ".bss.";
5652 break;
5653 case SECCAT_SBSS:
5654 prefix = one_only ? ".gnu.linkonce.sb." : ".sbss.";
5655 break;
5656 case SECCAT_TDATA:
5657 prefix = one_only ? ".gnu.linkonce.td." : ".tdata.";
5658 break;
5659 case SECCAT_TBSS:
5660 prefix = one_only ? ".gnu.linkonce.tb." : ".tbss.";
5661 break;
5662 default:
5663 gcc_unreachable ();
5665 plen = strlen (prefix);
5667 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5668 name = targetm.strip_name_encoding (name);
5669 nlen = strlen (name);
5671 string = alloca (nlen + plen + 1);
5672 memcpy (string, prefix, plen);
5673 memcpy (string + plen, name, nlen + 1);
5675 DECL_SECTION_NAME (decl) = build_string (nlen + plen, string);
5678 section *
5679 default_select_rtx_section (enum machine_mode mode ATTRIBUTE_UNUSED,
5680 rtx x,
5681 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
5683 if (flag_pic)
5684 switch (GET_CODE (x))
5686 case CONST:
5687 case SYMBOL_REF:
5688 case LABEL_REF:
5689 return data_section;
5691 default:
5692 break;
5695 return readonly_data_section;
5698 section *
5699 default_elf_select_rtx_section (enum machine_mode mode, rtx x,
5700 unsigned HOST_WIDE_INT align)
5702 /* ??? Handle small data here somehow. */
5704 if (flag_pic)
5705 switch (GET_CODE (x))
5707 case CONST:
5708 case SYMBOL_REF:
5709 return get_named_section (NULL, ".data.rel.ro", 3);
5711 case LABEL_REF:
5712 return get_named_section (NULL, ".data.rel.ro.local", 1);
5714 default:
5715 break;
5718 return mergeable_constant_section (mode, align, 0);
5721 /* Set the generally applicable flags on the SYMBOL_REF for EXP. */
5723 void
5724 default_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
5726 rtx symbol;
5727 int flags;
5729 /* Careful not to prod global register variables. */
5730 if (!MEM_P (rtl))
5731 return;
5732 symbol = XEXP (rtl, 0);
5733 if (GET_CODE (symbol) != SYMBOL_REF)
5734 return;
5736 flags = SYMBOL_REF_FLAGS (symbol) & SYMBOL_FLAG_HAS_BLOCK_INFO;
5737 if (TREE_CODE (decl) == FUNCTION_DECL)
5738 flags |= SYMBOL_FLAG_FUNCTION;
5739 if (targetm.binds_local_p (decl))
5740 flags |= SYMBOL_FLAG_LOCAL;
5741 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
5742 flags |= DECL_TLS_MODEL (decl) << SYMBOL_FLAG_TLS_SHIFT;
5743 else if (targetm.in_small_data_p (decl))
5744 flags |= SYMBOL_FLAG_SMALL;
5745 /* ??? Why is DECL_EXTERNAL ever set for non-PUBLIC names? Without
5746 being PUBLIC, the thing *must* be defined in this translation unit.
5747 Prevent this buglet from being propagated into rtl code as well. */
5748 if (DECL_P (decl) && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl))
5749 flags |= SYMBOL_FLAG_EXTERNAL;
5751 SYMBOL_REF_FLAGS (symbol) = flags;
5754 /* By default, we do nothing for encode_section_info, so we need not
5755 do anything but discard the '*' marker. */
5757 const char *
5758 default_strip_name_encoding (const char *str)
5760 return str + (*str == '*');
5763 #ifdef ASM_OUTPUT_DEF
5764 /* The default implementation of TARGET_ASM_OUTPUT_ANCHOR. Define the
5765 anchor relative to ".", the current section position. */
5767 void
5768 default_asm_output_anchor (rtx symbol)
5770 char buffer[100];
5772 sprintf (buffer, ". + " HOST_WIDE_INT_PRINT_DEC,
5773 SYMBOL_REF_BLOCK_OFFSET (symbol));
5774 ASM_OUTPUT_DEF (asm_out_file, XSTR (symbol, 0), buffer);
5776 #endif
5778 /* The default implementation of TARGET_USE_ANCHORS_FOR_SYMBOL_P. */
5780 bool
5781 default_use_anchors_for_symbol_p (rtx symbol)
5783 section *sect;
5784 tree decl;
5786 /* Don't use anchors for mergeable sections. The linker might move
5787 the objects around. */
5788 sect = SYMBOL_REF_BLOCK (symbol)->sect;
5789 if (sect->common.flags & SECTION_MERGE)
5790 return false;
5792 /* Don't use anchors for small data sections. The small data register
5793 acts as an anchor for such sections. */
5794 if (sect->common.flags & SECTION_SMALL)
5795 return false;
5797 decl = SYMBOL_REF_DECL (symbol);
5798 if (decl && DECL_P (decl))
5800 /* Don't use section anchors for decls that might be defined by
5801 other modules. */
5802 if (!targetm.binds_local_p (decl))
5803 return false;
5805 /* Don't use section anchors for decls that will be placed in a
5806 small data section. */
5807 /* ??? Ideally, this check would be redundant with the SECTION_SMALL
5808 one above. The problem is that we only use SECTION_SMALL for
5809 sections that should be marked as small in the section directive. */
5810 if (targetm.in_small_data_p (decl))
5811 return false;
5813 return true;
5816 /* Assume ELF-ish defaults, since that's pretty much the most liberal
5817 wrt cross-module name binding. */
5819 bool
5820 default_binds_local_p (tree exp)
5822 return default_binds_local_p_1 (exp, flag_shlib);
5825 bool
5826 default_binds_local_p_1 (tree exp, int shlib)
5828 bool local_p;
5830 /* A non-decl is an entry in the constant pool. */
5831 if (!DECL_P (exp))
5832 local_p = true;
5833 /* Weakrefs may not bind locally, even though the weakref itself is
5834 always static and therefore local. */
5835 else if (lookup_attribute ("weakref", DECL_ATTRIBUTES (exp)))
5836 local_p = false;
5837 /* Static variables are always local. */
5838 else if (! TREE_PUBLIC (exp))
5839 local_p = true;
5840 /* A variable is local if the user has said explicitly that it will
5841 be. */
5842 else if (DECL_VISIBILITY_SPECIFIED (exp)
5843 && DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
5844 local_p = true;
5845 /* Variables defined outside this object might not be local. */
5846 else if (DECL_EXTERNAL (exp))
5847 local_p = false;
5848 /* If defined in this object and visibility is not default, must be
5849 local. */
5850 else if (DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
5851 local_p = true;
5852 /* Default visibility weak data can be overridden by a strong symbol
5853 in another module and so are not local. */
5854 else if (DECL_WEAK (exp))
5855 local_p = false;
5856 /* If PIC, then assume that any global name can be overridden by
5857 symbols resolved from other modules. */
5858 else if (shlib)
5859 local_p = false;
5860 /* Uninitialized COMMON variable may be unified with symbols
5861 resolved from other modules. */
5862 else if (DECL_COMMON (exp)
5863 && (DECL_INITIAL (exp) == NULL
5864 || DECL_INITIAL (exp) == error_mark_node))
5865 local_p = false;
5866 /* Otherwise we're left with initialized (or non-common) global data
5867 which is of necessity defined locally. */
5868 else
5869 local_p = true;
5871 return local_p;
5874 /* Determine whether or not a pointer mode is valid. Assume defaults
5875 of ptr_mode or Pmode - can be overridden. */
5876 bool
5877 default_valid_pointer_mode (enum machine_mode mode)
5879 return (mode == ptr_mode || mode == Pmode);
5882 /* Default function to output code that will globalize a label. A
5883 target must define GLOBAL_ASM_OP or provide its own function to
5884 globalize a label. */
5885 #ifdef GLOBAL_ASM_OP
5886 void
5887 default_globalize_label (FILE * stream, const char *name)
5889 fputs (GLOBAL_ASM_OP, stream);
5890 assemble_name (stream, name);
5891 putc ('\n', stream);
5893 #endif /* GLOBAL_ASM_OP */
5895 /* Default function to output a label for unwind information. The
5896 default is to do nothing. A target that needs nonlocal labels for
5897 unwind information must provide its own function to do this. */
5898 void
5899 default_emit_unwind_label (FILE * stream ATTRIBUTE_UNUSED,
5900 tree decl ATTRIBUTE_UNUSED,
5901 int for_eh ATTRIBUTE_UNUSED,
5902 int empty ATTRIBUTE_UNUSED)
5906 /* Default function to output a label to divide up the exception table.
5907 The default is to do nothing. A target that needs/wants to divide
5908 up the table must provide it's own function to do this. */
5909 void
5910 default_emit_except_table_label (FILE * stream ATTRIBUTE_UNUSED)
5914 /* This is how to output an internal numbered label where PREFIX is
5915 the class of label and LABELNO is the number within the class. */
5917 void
5918 default_internal_label (FILE *stream, const char *prefix,
5919 unsigned long labelno)
5921 char *const buf = alloca (40 + strlen (prefix));
5922 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
5923 ASM_OUTPUT_INTERNAL_LABEL (stream, buf);
5926 /* This is the default behavior at the beginning of a file. It's
5927 controlled by two other target-hook toggles. */
5928 void
5929 default_file_start (void)
5931 if (targetm.file_start_app_off && !flag_verbose_asm)
5932 fputs (ASM_APP_OFF, asm_out_file);
5934 if (targetm.file_start_file_directive)
5935 output_file_directive (asm_out_file, main_input_filename);
5938 /* This is a generic routine suitable for use as TARGET_ASM_FILE_END
5939 which emits a special section directive used to indicate whether or
5940 not this object file needs an executable stack. This is primarily
5941 a GNU extension to ELF but could be used on other targets. */
5943 int trampolines_created;
5945 void
5946 file_end_indicate_exec_stack (void)
5948 unsigned int flags = SECTION_DEBUG;
5949 if (trampolines_created)
5950 flags |= SECTION_CODE;
5952 switch_to_section (get_section (".note.GNU-stack", flags, NULL));
5955 /* Output DIRECTIVE (a C string) followed by a newline. This is used as
5956 a get_unnamed_section callback. */
5958 void
5959 output_section_asm_op (const void *directive)
5961 fprintf (asm_out_file, "%s\n", (const char *) directive);
5964 /* Emit assembly code to switch to section NEW_SECTION. Do nothing if
5965 the current section is NEW_SECTION. */
5967 void
5968 switch_to_section (section *new_section)
5970 if (in_section == new_section)
5971 return;
5973 if (new_section->common.flags & SECTION_FORGET)
5974 in_section = NULL;
5975 else
5976 in_section = new_section;
5978 switch (SECTION_STYLE (new_section))
5980 case SECTION_NAMED:
5981 if (cfun
5982 && !cfun->unlikely_text_section_name
5983 && strcmp (new_section->named.name,
5984 UNLIKELY_EXECUTED_TEXT_SECTION_NAME) == 0)
5985 cfun->unlikely_text_section_name = UNLIKELY_EXECUTED_TEXT_SECTION_NAME;
5987 targetm.asm_out.named_section (new_section->named.name,
5988 new_section->named.common.flags,
5989 new_section->named.decl);
5990 break;
5992 case SECTION_UNNAMED:
5993 new_section->unnamed.callback (new_section->unnamed.data);
5994 break;
5996 case SECTION_NOSWITCH:
5997 gcc_unreachable ();
5998 break;
6001 new_section->common.flags |= SECTION_DECLARED;
6004 /* If block symbol SYMBOL has not yet been assigned an offset, place
6005 it at the end of its block. */
6007 void
6008 place_block_symbol (rtx symbol)
6010 unsigned HOST_WIDE_INT size, mask, offset;
6011 struct constant_descriptor_rtx *desc;
6012 unsigned int alignment;
6013 struct object_block *block;
6014 tree decl;
6016 gcc_assert (SYMBOL_REF_BLOCK (symbol));
6017 if (SYMBOL_REF_BLOCK_OFFSET (symbol) >= 0)
6018 return;
6020 /* Work out the symbol's size and alignment. */
6021 if (CONSTANT_POOL_ADDRESS_P (symbol))
6023 desc = SYMBOL_REF_CONSTANT (symbol);
6024 alignment = desc->align;
6025 size = GET_MODE_SIZE (desc->mode);
6027 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
6029 decl = SYMBOL_REF_DECL (symbol);
6030 alignment = get_constant_alignment (decl);
6031 size = get_constant_size (decl);
6033 else
6035 decl = SYMBOL_REF_DECL (symbol);
6036 alignment = DECL_ALIGN (decl);
6037 size = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
6040 /* Calculate the object's offset from the start of the block. */
6041 block = SYMBOL_REF_BLOCK (symbol);
6042 mask = alignment / BITS_PER_UNIT - 1;
6043 offset = (block->size + mask) & ~mask;
6044 SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
6046 /* Record the block's new alignment and size. */
6047 block->alignment = MAX (block->alignment, alignment);
6048 block->size = offset + size;
6050 VEC_safe_push (rtx, gc, block->objects, symbol);
6053 /* Return the anchor that should be used to address byte offset OFFSET
6054 from the first object in BLOCK. MODEL is the TLS model used
6055 to access it. */
6058 get_section_anchor (struct object_block *block, HOST_WIDE_INT offset,
6059 enum tls_model model)
6061 char label[100];
6062 unsigned int begin, middle, end;
6063 unsigned HOST_WIDE_INT min_offset, max_offset, range, bias, delta;
6064 rtx anchor;
6066 /* Work out the anchor's offset. Use an offset of 0 for the first
6067 anchor so that we don't pessimize the case where we take the address
6068 of a variable at the beginning of the block. This is particularly
6069 useful when a block has only one variable assigned to it.
6071 We try to place anchors RANGE bytes apart, so there can then be
6072 anchors at +/-RANGE, +/-2 * RANGE, and so on, up to the limits of
6073 a ptr_mode offset. With some target settings, the lowest such
6074 anchor might be out of range for the lowest ptr_mode offset;
6075 likewise the highest anchor for the highest offset. Use anchors
6076 at the extreme ends of the ptr_mode range in such cases.
6078 All arithmetic uses unsigned integers in order to avoid
6079 signed overflow. */
6080 max_offset = (unsigned HOST_WIDE_INT) targetm.max_anchor_offset;
6081 min_offset = (unsigned HOST_WIDE_INT) targetm.min_anchor_offset;
6082 range = max_offset - min_offset + 1;
6083 if (range == 0)
6084 offset = 0;
6085 else
6087 bias = 1 << (GET_MODE_BITSIZE (ptr_mode) - 1);
6088 if (offset < 0)
6090 delta = -(unsigned HOST_WIDE_INT) offset + max_offset;
6091 delta -= delta % range;
6092 if (delta > bias)
6093 delta = bias;
6094 offset = (HOST_WIDE_INT) (-delta);
6096 else
6098 delta = (unsigned HOST_WIDE_INT) offset - min_offset;
6099 delta -= delta % range;
6100 if (delta > bias - 1)
6101 delta = bias - 1;
6102 offset = (HOST_WIDE_INT) delta;
6106 /* Do a binary search to see if there's already an anchor we can use.
6107 Set BEGIN to the new anchor's index if not. */
6108 begin = 0;
6109 end = VEC_length (rtx, block->anchors);
6110 while (begin != end)
6112 middle = (end + begin) / 2;
6113 anchor = VEC_index (rtx, block->anchors, middle);
6114 if (SYMBOL_REF_BLOCK_OFFSET (anchor) > offset)
6115 end = middle;
6116 else if (SYMBOL_REF_BLOCK_OFFSET (anchor) < offset)
6117 begin = middle + 1;
6118 else if (SYMBOL_REF_TLS_MODEL (anchor) > model)
6119 end = middle;
6120 else if (SYMBOL_REF_TLS_MODEL (anchor) < model)
6121 begin = middle + 1;
6122 else
6123 return anchor;
6126 /* Create a new anchor with a unique label. */
6127 ASM_GENERATE_INTERNAL_LABEL (label, "LANCHOR", anchor_labelno++);
6128 anchor = create_block_symbol (ggc_strdup (label), block, offset);
6129 SYMBOL_REF_FLAGS (anchor) |= SYMBOL_FLAG_LOCAL | SYMBOL_FLAG_ANCHOR;
6130 SYMBOL_REF_FLAGS (anchor) |= model << SYMBOL_FLAG_TLS_SHIFT;
6132 /* Insert it at index BEGIN. */
6133 VEC_safe_insert (rtx, gc, block->anchors, begin, anchor);
6134 return anchor;
6137 /* Output the objects in BLOCK. */
6139 static void
6140 output_object_block (struct object_block *block)
6142 struct constant_descriptor_rtx *desc;
6143 unsigned int i;
6144 HOST_WIDE_INT offset;
6145 tree decl;
6146 rtx symbol;
6148 if (block->objects == NULL)
6149 return;
6151 /* Switch to the section and make sure that the first byte is
6152 suitably aligned. */
6153 switch_to_section (block->sect);
6154 assemble_align (block->alignment);
6156 /* Define the values of all anchors relative to the current section
6157 position. */
6158 for (i = 0; VEC_iterate (rtx, block->anchors, i, symbol); i++)
6159 targetm.asm_out.output_anchor (symbol);
6161 /* Output the objects themselves. */
6162 offset = 0;
6163 for (i = 0; VEC_iterate (rtx, block->objects, i, symbol); i++)
6165 /* Move to the object's offset, padding with zeros if necessary. */
6166 assemble_zeros (SYMBOL_REF_BLOCK_OFFSET (symbol) - offset);
6167 offset = SYMBOL_REF_BLOCK_OFFSET (symbol);
6168 if (CONSTANT_POOL_ADDRESS_P (symbol))
6170 desc = SYMBOL_REF_CONSTANT (symbol);
6171 output_constant_pool_1 (desc, 1);
6172 offset += GET_MODE_SIZE (desc->mode);
6174 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
6176 decl = SYMBOL_REF_DECL (symbol);
6177 assemble_constant_contents (decl, XSTR (symbol, 0),
6178 get_constant_alignment (decl));
6179 offset += get_constant_size (decl);
6181 else
6183 decl = SYMBOL_REF_DECL (symbol);
6184 assemble_variable_contents (decl, XSTR (symbol, 0), false);
6185 offset += tree_low_cst (DECL_SIZE_UNIT (decl), 1);
6190 /* A htab_traverse callback used to call output_object_block for
6191 each member of object_block_htab. */
6193 static int
6194 output_object_block_htab (void **slot, void *data ATTRIBUTE_UNUSED)
6196 output_object_block ((struct object_block *) (*slot));
6197 return 1;
6200 /* Output the definitions of all object_blocks. */
6202 void
6203 output_object_blocks (void)
6205 htab_traverse (object_block_htab, output_object_block_htab, NULL);
6208 #include "gt-varasm.h"