2004-02-11 Eric Christopher <echristo@redhat.com>
[official-gcc.git] / gcc / varasm.c
blobcbcef0a1e1d15537577865bb1c5185cddaab309f
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 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
23 /* This file handles generation of all the assembler code
24 *except* the instructions of a function.
25 This includes declarations of variables and their initial values.
27 We also output the assembler code for constants stored in memory
28 and are responsible for combining constants with the same value. */
30 #include "config.h"
31 #include "system.h"
32 #include "coretypes.h"
33 #include "tm.h"
34 #include "rtl.h"
35 #include "tree.h"
36 #include "flags.h"
37 #include "function.h"
38 #include "expr.h"
39 #include "hard-reg-set.h"
40 #include "regs.h"
41 #include "real.h"
42 #include "output.h"
43 #include "toplev.h"
44 #include "hashtab.h"
45 #include "c-pragma.h"
46 #include "ggc.h"
47 #include "langhooks.h"
48 #include "tm_p.h"
49 #include "debug.h"
50 #include "target.h"
51 #include "cgraph.h"
53 #ifdef XCOFF_DEBUGGING_INFO
54 #include "xcoffout.h" /* Needed for external data
55 declarations for e.g. AIX 4.x. */
56 #endif
58 #ifndef TRAMPOLINE_ALIGNMENT
59 #define TRAMPOLINE_ALIGNMENT FUNCTION_BOUNDARY
60 #endif
62 #ifndef ASM_STABS_OP
63 #define ASM_STABS_OP "\t.stabs\t"
64 #endif
66 /* The (assembler) name of the first globally-visible object output. */
67 const char *first_global_object_name;
68 const char *weak_global_object_name;
70 struct addr_const;
71 struct constant_descriptor_rtx;
72 struct rtx_constant_pool;
74 struct varasm_status GTY(())
76 /* If we're using a per-function constant pool, this is it. */
77 struct rtx_constant_pool *pool;
79 /* Number of tree-constants deferred during the expansion of this
80 function. */
81 unsigned int deferred_constants;
84 #define n_deferred_constants (cfun->varasm->deferred_constants)
86 /* Number for making the label on the next
87 constant that is stored in memory. */
89 static GTY(()) int const_labelno;
91 /* Carry information from ASM_DECLARE_OBJECT_NAME
92 to ASM_FINISH_DECLARE_OBJECT. */
94 int size_directive_output;
96 /* The last decl for which assemble_variable was called,
97 if it did ASM_DECLARE_OBJECT_NAME.
98 If the last call to assemble_variable didn't do that,
99 this holds 0. */
101 tree last_assemble_variable_decl;
103 /* RTX_UNCHANGING_P in a MEM can mean it is stored into, for initialization.
104 So giving constant the alias set for the type will allow such
105 initializations to appear to conflict with the load of the constant. We
106 avoid this by giving all constants an alias set for just constants.
107 Since there will be no stores to that alias set, nothing will ever
108 conflict with them. */
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 static int in_named_entry_eq (const void *, const void *);
131 static hashval_t in_named_entry_hash (const void *);
132 #ifdef ASM_OUTPUT_BSS
133 static void asm_output_bss (FILE *, tree, const char *,
134 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
135 #endif
136 #ifdef BSS_SECTION_ASM_OP
137 #ifdef ASM_OUTPUT_ALIGNED_BSS
138 static void asm_output_aligned_bss (FILE *, tree, const char *,
139 unsigned HOST_WIDE_INT, int)
140 ATTRIBUTE_UNUSED;
141 #endif
142 #endif /* BSS_SECTION_ASM_OP */
143 static bool asm_emit_uninitialised (tree, const char*,
144 unsigned HOST_WIDE_INT,
145 unsigned HOST_WIDE_INT);
146 static void mark_weak (tree);
148 enum in_section { no_section, in_text, in_data, in_named
149 #ifdef BSS_SECTION_ASM_OP
150 , in_bss
151 #endif
152 #ifdef CTORS_SECTION_ASM_OP
153 , in_ctors
154 #endif
155 #ifdef DTORS_SECTION_ASM_OP
156 , in_dtors
157 #endif
158 #ifdef READONLY_DATA_SECTION_ASM_OP
159 , in_readonly_data
160 #endif
161 #ifdef EXTRA_SECTIONS
162 , EXTRA_SECTIONS
163 #endif
165 static GTY(()) enum in_section in_section = no_section;
167 /* Return a nonzero value if DECL has a section attribute. */
168 #ifndef IN_NAMED_SECTION
169 #define IN_NAMED_SECTION(DECL) \
170 ((TREE_CODE (DECL) == FUNCTION_DECL || TREE_CODE (DECL) == VAR_DECL) \
171 && DECL_SECTION_NAME (DECL) != NULL_TREE)
172 #endif
174 /* Text of section name when in_section == in_named. */
175 static GTY(()) const char *in_named_name;
177 /* Hash table of flags that have been used for a particular named section. */
179 struct in_named_entry GTY(())
181 const char *name;
182 unsigned int flags;
183 bool declared;
186 static GTY((param_is (struct in_named_entry))) htab_t in_named_htab;
188 /* Define functions like text_section for any extra sections. */
189 #ifdef EXTRA_SECTION_FUNCTIONS
190 EXTRA_SECTION_FUNCTIONS
191 #endif
193 /* Tell assembler to switch to text section. */
195 void
196 text_section (void)
198 if (in_section != in_text)
200 in_section = in_text;
201 fprintf (asm_out_file, "%s\n", TEXT_SECTION_ASM_OP);
205 /* Tell assembler to switch to data section. */
207 void
208 data_section (void)
210 if (in_section != in_data)
212 in_section = in_data;
213 fprintf (asm_out_file, "%s\n", DATA_SECTION_ASM_OP);
217 /* Tell assembler to switch to read-only data section. This is normally
218 the text section. */
220 void
221 readonly_data_section (void)
223 #ifdef READONLY_DATA_SECTION
224 READONLY_DATA_SECTION (); /* Note this can call data_section. */
225 #else
226 #ifdef READONLY_DATA_SECTION_ASM_OP
227 if (in_section != in_readonly_data)
229 in_section = in_readonly_data;
230 fputs (READONLY_DATA_SECTION_ASM_OP, asm_out_file);
231 fputc ('\n', asm_out_file);
233 #else
234 text_section ();
235 #endif
236 #endif
239 /* Determine if we're in the text section. */
242 in_text_section (void)
244 return in_section == in_text;
247 /* Determine if we're in the data section. */
250 in_data_section (void)
252 return in_section == in_data;
255 /* Helper routines for maintaining in_named_htab. */
257 static int
258 in_named_entry_eq (const void *p1, const void *p2)
260 const struct in_named_entry *old = p1;
261 const char *new = p2;
263 return strcmp (old->name, new) == 0;
266 static hashval_t
267 in_named_entry_hash (const void *p)
269 const struct in_named_entry *old = p;
270 return htab_hash_string (old->name);
273 /* If SECTION has been seen before as a named section, return the flags
274 that were used. Otherwise, return 0. Note, that 0 is a perfectly valid
275 set of flags for a section to have, so 0 does not mean that the section
276 has not been seen. */
278 unsigned int
279 get_named_section_flags (const char *section)
281 struct in_named_entry **slot;
283 slot = (struct in_named_entry **)
284 htab_find_slot_with_hash (in_named_htab, section,
285 htab_hash_string (section), NO_INSERT);
287 return slot ? (*slot)->flags : 0;
290 /* Returns true if the section has been declared before. Sets internal
291 flag on this section in in_named_hash so subsequent calls on this
292 section will return false. */
294 bool
295 named_section_first_declaration (const char *name)
297 struct in_named_entry **slot;
299 slot = (struct in_named_entry **)
300 htab_find_slot_with_hash (in_named_htab, name,
301 htab_hash_string (name), NO_INSERT);
302 if (! (*slot)->declared)
304 (*slot)->declared = true;
305 return true;
307 else
309 return false;
314 /* Record FLAGS for SECTION. If SECTION was previously recorded with a
315 different set of flags, return false. */
317 bool
318 set_named_section_flags (const char *section, unsigned int flags)
320 struct in_named_entry **slot, *entry;
322 slot = (struct in_named_entry **)
323 htab_find_slot_with_hash (in_named_htab, section,
324 htab_hash_string (section), INSERT);
325 entry = *slot;
327 if (!entry)
329 entry = ggc_alloc (sizeof (*entry));
330 *slot = entry;
331 entry->name = ggc_strdup (section);
332 entry->flags = flags;
333 entry->declared = false;
335 else if (entry->flags != flags)
336 return false;
338 return true;
341 /* Tell assembler to change to section NAME with attributes FLAGS. */
343 void
344 named_section_flags (const char *name, unsigned int flags)
346 if (in_section != in_named || strcmp (name, in_named_name) != 0)
348 if (! set_named_section_flags (name, flags))
349 abort ();
351 (*targetm.asm_out.named_section) (name, flags);
353 if (flags & SECTION_FORGET)
354 in_section = no_section;
355 else
357 in_named_name = ggc_strdup (name);
358 in_section = in_named;
363 /* Tell assembler to change to section NAME for DECL.
364 If DECL is NULL, just switch to section NAME.
365 If NAME is NULL, get the name from DECL.
366 If RELOC is 1, the initializer for DECL contains relocs. */
368 void
369 named_section (tree decl, const char *name, int reloc)
371 unsigned int flags;
373 if (decl != NULL_TREE && !DECL_P (decl))
374 abort ();
375 if (name == NULL)
376 name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
378 flags = (* targetm.section_type_flags) (decl, name, reloc);
380 /* Sanity check user variables for flag changes. Non-user
381 section flag changes will abort in named_section_flags.
382 However, don't complain if SECTION_OVERRIDE is set.
383 We trust that the setter knows that it is safe to ignore
384 the default flags for this decl. */
385 if (decl && ! set_named_section_flags (name, flags))
387 flags = get_named_section_flags (name);
388 if ((flags & SECTION_OVERRIDE) == 0)
389 error ("%J%D causes a section type conflict", decl, decl);
392 named_section_flags (name, flags);
395 /* If required, set DECL_SECTION_NAME to a unique name. */
397 void
398 resolve_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED,
399 int flag_function_or_data_sections)
401 if (DECL_SECTION_NAME (decl) == NULL_TREE
402 && targetm.have_named_sections
403 && (flag_function_or_data_sections
404 || DECL_ONE_ONLY (decl)))
405 (*targetm.asm_out.unique_section) (decl, reloc);
408 #ifdef BSS_SECTION_ASM_OP
410 /* Tell the assembler to switch to the bss section. */
412 void
413 bss_section (void)
415 if (in_section != in_bss)
417 fprintf (asm_out_file, "%s\n", BSS_SECTION_ASM_OP);
418 in_section = in_bss;
422 #ifdef ASM_OUTPUT_BSS
424 /* Utility function for ASM_OUTPUT_BSS for targets to use if
425 they don't support alignments in .bss.
426 ??? It is believed that this function will work in most cases so such
427 support is localized here. */
429 static void
430 asm_output_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
431 const char *name,
432 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
433 unsigned HOST_WIDE_INT rounded)
435 (*targetm.asm_out.globalize_label) (file, name);
436 bss_section ();
437 #ifdef ASM_DECLARE_OBJECT_NAME
438 last_assemble_variable_decl = decl;
439 ASM_DECLARE_OBJECT_NAME (file, name, decl);
440 #else
441 /* Standard thing is just output label for the object. */
442 ASM_OUTPUT_LABEL (file, name);
443 #endif /* ASM_DECLARE_OBJECT_NAME */
444 ASM_OUTPUT_SKIP (file, rounded ? rounded : 1);
447 #endif
449 #ifdef ASM_OUTPUT_ALIGNED_BSS
451 /* Utility function for targets to use in implementing
452 ASM_OUTPUT_ALIGNED_BSS.
453 ??? It is believed that this function will work in most cases so such
454 support is localized here. */
456 static void
457 asm_output_aligned_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
458 const char *name, unsigned HOST_WIDE_INT size,
459 int align)
461 bss_section ();
462 ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
463 #ifdef ASM_DECLARE_OBJECT_NAME
464 last_assemble_variable_decl = decl;
465 ASM_DECLARE_OBJECT_NAME (file, name, decl);
466 #else
467 /* Standard thing is just output label for the object. */
468 ASM_OUTPUT_LABEL (file, name);
469 #endif /* ASM_DECLARE_OBJECT_NAME */
470 ASM_OUTPUT_SKIP (file, size ? size : 1);
473 #endif
475 #endif /* BSS_SECTION_ASM_OP */
477 /* Switch to the section for function DECL.
479 If DECL is NULL_TREE, switch to the text section.
480 ??? It's not clear that we will ever be passed NULL_TREE, but it's
481 safer to handle it. */
483 void
484 function_section (tree decl)
486 if (decl != NULL_TREE
487 && DECL_SECTION_NAME (decl) != NULL_TREE)
488 named_section (decl, (char *) 0, 0);
489 else
490 text_section ();
493 /* Switch to section for variable DECL. RELOC is the same as the
494 argument to SELECT_SECTION. */
496 void
497 variable_section (tree decl, int reloc)
499 if (IN_NAMED_SECTION (decl))
500 named_section (decl, NULL, reloc);
501 else
502 (*targetm.asm_out.select_section) (decl, reloc, DECL_ALIGN (decl));
505 /* Tell assembler to switch to the section for string merging. */
507 void
508 mergeable_string_section (tree decl ATTRIBUTE_UNUSED,
509 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
510 unsigned int flags ATTRIBUTE_UNUSED)
512 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
513 && TREE_CODE (decl) == STRING_CST
514 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
515 && align <= 256
516 && TREE_STRING_LENGTH (decl) >= int_size_in_bytes (TREE_TYPE (decl)))
518 enum machine_mode mode;
519 unsigned int modesize;
520 const char *str;
521 int i, j, len, unit;
522 char name[30];
524 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (decl)));
525 modesize = GET_MODE_BITSIZE (mode);
526 if (modesize >= 8 && modesize <= 256
527 && (modesize & (modesize - 1)) == 0)
529 if (align < modesize)
530 align = modesize;
532 str = TREE_STRING_POINTER (decl);
533 len = TREE_STRING_LENGTH (decl);
534 unit = GET_MODE_SIZE (mode);
536 /* Check for embedded NUL characters. */
537 for (i = 0; i < len; i += unit)
539 for (j = 0; j < unit; j++)
540 if (str[i + j] != '\0')
541 break;
542 if (j == unit)
543 break;
545 if (i == len - unit)
547 sprintf (name, ".rodata.str%d.%d", modesize / 8,
548 (int) (align / 8));
549 flags |= (modesize / 8) | SECTION_MERGE | SECTION_STRINGS;
550 if (!i && modesize < align)
552 /* A "" string with requested alignment greater than
553 character size might cause a problem:
554 if some other string required even bigger
555 alignment than "", then linker might think the
556 "" is just part of padding after some other string
557 and not put it into the hash table initially.
558 But this means "" could have smaller alignment
559 than requested. */
560 #ifdef ASM_OUTPUT_SECTION_START
561 named_section_flags (name, flags);
562 ASM_OUTPUT_SECTION_START (asm_out_file);
563 #else
564 readonly_data_section ();
565 #endif
566 return;
569 named_section_flags (name, flags);
570 return;
575 readonly_data_section ();
578 /* Tell assembler to switch to the section for constant merging. */
580 void
581 mergeable_constant_section (enum machine_mode mode ATTRIBUTE_UNUSED,
582 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
583 unsigned int flags ATTRIBUTE_UNUSED)
585 unsigned int modesize = GET_MODE_BITSIZE (mode);
587 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
588 && mode != VOIDmode
589 && mode != BLKmode
590 && modesize <= align
591 && align >= 8
592 && align <= 256
593 && (align & (align - 1)) == 0)
595 char name[24];
597 sprintf (name, ".rodata.cst%d", (int) (align / 8));
598 flags |= (align / 8) | SECTION_MERGE;
599 named_section_flags (name, flags);
600 return;
603 readonly_data_section ();
606 /* Given NAME, a putative register name, discard any customary prefixes. */
608 static const char *
609 strip_reg_name (const char *name)
611 #ifdef REGISTER_PREFIX
612 if (!strncmp (name, REGISTER_PREFIX, strlen (REGISTER_PREFIX)))
613 name += strlen (REGISTER_PREFIX);
614 #endif
615 if (name[0] == '%' || name[0] == '#')
616 name++;
617 return name;
620 /* Decode an `asm' spec for a declaration as a register name.
621 Return the register number, or -1 if nothing specified,
622 or -2 if the ASMSPEC is not `cc' or `memory' and is not recognized,
623 or -3 if ASMSPEC is `cc' and is not recognized,
624 or -4 if ASMSPEC is `memory' and is not recognized.
625 Accept an exact spelling or a decimal number.
626 Prefixes such as % are optional. */
629 decode_reg_name (const char *asmspec)
631 if (asmspec != 0)
633 int i;
635 /* Get rid of confusing prefixes. */
636 asmspec = strip_reg_name (asmspec);
638 /* Allow a decimal number as a "register name". */
639 for (i = strlen (asmspec) - 1; i >= 0; i--)
640 if (! ISDIGIT (asmspec[i]))
641 break;
642 if (asmspec[0] != 0 && i < 0)
644 i = atoi (asmspec);
645 if (i < FIRST_PSEUDO_REGISTER && i >= 0)
646 return i;
647 else
648 return -2;
651 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
652 if (reg_names[i][0]
653 && ! strcmp (asmspec, strip_reg_name (reg_names[i])))
654 return i;
656 #ifdef ADDITIONAL_REGISTER_NAMES
658 static const struct { const char *const name; const int number; } table[]
659 = ADDITIONAL_REGISTER_NAMES;
661 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
662 if (! strcmp (asmspec, table[i].name))
663 return table[i].number;
665 #endif /* ADDITIONAL_REGISTER_NAMES */
667 if (!strcmp (asmspec, "memory"))
668 return -4;
670 if (!strcmp (asmspec, "cc"))
671 return -3;
673 return -2;
676 return -1;
679 /* Create the DECL_RTL for a VAR_DECL or FUNCTION_DECL. DECL should
680 have static storage duration. In other words, it should not be an
681 automatic variable, including PARM_DECLs.
683 There is, however, one exception: this function handles variables
684 explicitly placed in a particular register by the user.
686 ASMSPEC, if not 0, is the string which the user specified as the
687 assembler symbol name.
689 This is never called for PARM_DECL nodes. */
691 void
692 make_decl_rtl (tree decl, const char *asmspec)
694 const char *name = 0;
695 int reg_number;
696 rtx x;
698 /* Check that we are not being given an automatic variable. */
699 /* A weak alias has TREE_PUBLIC set but not the other bits. */
700 if (TREE_CODE (decl) == PARM_DECL
701 || TREE_CODE (decl) == RESULT_DECL
702 || (TREE_CODE (decl) == VAR_DECL
703 && !TREE_STATIC (decl)
704 && !TREE_PUBLIC (decl)
705 && !DECL_EXTERNAL (decl)
706 && !DECL_REGISTER (decl)))
707 abort ();
708 /* And that we were not given a type or a label. */
709 else if (TREE_CODE (decl) == TYPE_DECL
710 || TREE_CODE (decl) == LABEL_DECL)
711 abort ();
713 /* For a duplicate declaration, we can be called twice on the
714 same DECL node. Don't discard the RTL already made. */
715 if (DECL_RTL_SET_P (decl))
717 /* If the old RTL had the wrong mode, fix the mode. */
718 if (GET_MODE (DECL_RTL (decl)) != DECL_MODE (decl))
719 SET_DECL_RTL (decl, adjust_address_nv (DECL_RTL (decl),
720 DECL_MODE (decl), 0));
722 /* ??? Another way to do this would be to maintain a hashed
723 table of such critters. Instead of adding stuff to a DECL
724 to give certain attributes to it, we could use an external
725 hash map from DECL to set of attributes. */
727 /* Let the target reassign the RTL if it wants.
728 This is necessary, for example, when one machine specific
729 decl attribute overrides another. */
730 (* targetm.encode_section_info) (decl, DECL_RTL (decl), false);
731 return;
734 reg_number = decode_reg_name (asmspec);
735 if (reg_number == -2)
737 /* ASMSPEC is given, and not the name of a register. Mark the
738 name with a star so assemble_name won't munge it. */
739 char *starred = alloca (strlen (asmspec) + 2);
740 starred[0] = '*';
741 strcpy (starred + 1, asmspec);
742 change_decl_assembler_name (decl, get_identifier (starred));
745 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
747 if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
749 /* First detect errors in declaring global registers. */
750 if (reg_number == -1)
751 error ("%Jregister name not specified for '%D'", decl, decl);
752 else if (reg_number < 0)
753 error ("%Jinvalid register name for '%D'", decl, decl);
754 else if (TYPE_MODE (TREE_TYPE (decl)) == BLKmode)
755 error ("%Jdata type of '%D' isn't suitable for a register",
756 decl, decl);
757 else if (! HARD_REGNO_MODE_OK (reg_number, TYPE_MODE (TREE_TYPE (decl))))
758 error ("%Jregister specified for '%D' isn't suitable for data type",
759 decl, decl);
760 /* Now handle properly declared static register variables. */
761 else
763 int nregs;
765 if (DECL_INITIAL (decl) != 0 && TREE_STATIC (decl))
767 DECL_INITIAL (decl) = 0;
768 error ("global register variable has initial value");
770 if (TREE_THIS_VOLATILE (decl))
771 warning ("volatile register variables don't work as you might wish");
773 /* If the user specified one of the eliminables registers here,
774 e.g., FRAME_POINTER_REGNUM, we don't want to get this variable
775 confused with that register and be eliminated. This usage is
776 somewhat suspect... */
778 SET_DECL_RTL (decl, gen_rtx_raw_REG (DECL_MODE (decl), reg_number));
779 ORIGINAL_REGNO (DECL_RTL (decl)) = reg_number;
780 REG_USERVAR_P (DECL_RTL (decl)) = 1;
782 if (TREE_STATIC (decl))
784 /* Make this register global, so not usable for anything
785 else. */
786 #ifdef ASM_DECLARE_REGISTER_GLOBAL
787 ASM_DECLARE_REGISTER_GLOBAL (asm_out_file, decl, reg_number, name);
788 #endif
789 nregs = hard_regno_nregs[reg_number][DECL_MODE (decl)];
790 while (nregs > 0)
791 globalize_reg (reg_number + --nregs);
794 /* As a register variable, it has no section. */
795 return;
799 /* Now handle ordinary static variables and functions (in memory).
800 Also handle vars declared register invalidly. */
802 if (reg_number >= 0 || reg_number == -3)
803 error ("%Jregister name given for non-register variable '%D'", decl, decl);
805 /* Specifying a section attribute on a variable forces it into a
806 non-.bss section, and thus it cannot be common. */
807 if (TREE_CODE (decl) == VAR_DECL
808 && DECL_SECTION_NAME (decl) != NULL_TREE
809 && DECL_INITIAL (decl) == NULL_TREE
810 && DECL_COMMON (decl))
811 DECL_COMMON (decl) = 0;
813 /* Variables can't be both common and weak. */
814 if (TREE_CODE (decl) == VAR_DECL && DECL_WEAK (decl))
815 DECL_COMMON (decl) = 0;
817 x = gen_rtx_SYMBOL_REF (Pmode, name);
818 SYMBOL_REF_WEAK (x) = DECL_WEAK (decl);
819 SYMBOL_REF_DECL (x) = decl;
821 x = gen_rtx_MEM (DECL_MODE (decl), x);
822 if (TREE_CODE (decl) != FUNCTION_DECL)
823 set_mem_attributes (x, decl, 1);
824 SET_DECL_RTL (decl, x);
826 /* Optionally set flags or add text to the name to record information
827 such as that it is a function name.
828 If the name is changed, the macro ASM_OUTPUT_LABELREF
829 will have to know how to strip this information. */
830 (* targetm.encode_section_info) (decl, DECL_RTL (decl), true);
833 /* Make the rtl for variable VAR be volatile.
834 Use this only for static variables. */
836 void
837 make_var_volatile (tree var)
839 if (GET_CODE (DECL_RTL (var)) != MEM)
840 abort ();
842 MEM_VOLATILE_P (DECL_RTL (var)) = 1;
845 /* Output a string of literal assembler code
846 for an `asm' keyword used between functions. */
848 void
849 assemble_asm (tree string)
851 app_enable ();
853 if (TREE_CODE (string) == ADDR_EXPR)
854 string = TREE_OPERAND (string, 0);
856 fprintf (asm_out_file, "\t%s\n", TREE_STRING_POINTER (string));
859 /* Record an element in the table of global destructors. SYMBOL is
860 a SYMBOL_REF of the function to be called; PRIORITY is a number
861 between 0 and MAX_INIT_PRIORITY. */
863 void
864 default_stabs_asm_out_destructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
866 /* Tell GNU LD that this is part of the static destructor set.
867 This will work for any system that uses stabs, most usefully
868 aout systems. */
869 fprintf (asm_out_file, "%s\"___DTOR_LIST__\",22,0,0,", ASM_STABS_OP);
870 assemble_name (asm_out_file, XSTR (symbol, 0));
871 fputc ('\n', asm_out_file);
874 void
875 default_named_section_asm_out_destructor (rtx symbol, int priority)
877 const char *section = ".dtors";
878 char buf[16];
880 /* ??? This only works reliably with the GNU linker. */
881 if (priority != DEFAULT_INIT_PRIORITY)
883 sprintf (buf, ".dtors.%.5u",
884 /* Invert the numbering so the linker puts us in the proper
885 order; constructors are run from right to left, and the
886 linker sorts in increasing order. */
887 MAX_INIT_PRIORITY - priority);
888 section = buf;
891 named_section_flags (section, SECTION_WRITE);
892 assemble_align (POINTER_SIZE);
893 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
896 #ifdef DTORS_SECTION_ASM_OP
897 void
898 dtors_section (void)
900 if (in_section != in_dtors)
902 in_section = in_dtors;
903 fputs (DTORS_SECTION_ASM_OP, asm_out_file);
904 fputc ('\n', asm_out_file);
908 void
909 default_dtor_section_asm_out_destructor (rtx symbol,
910 int priority ATTRIBUTE_UNUSED)
912 dtors_section ();
913 assemble_align (POINTER_SIZE);
914 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
916 #endif
918 /* Likewise for global constructors. */
920 void
921 default_stabs_asm_out_constructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
923 /* Tell GNU LD that this is part of the static destructor set.
924 This will work for any system that uses stabs, most usefully
925 aout systems. */
926 fprintf (asm_out_file, "%s\"___CTOR_LIST__\",22,0,0,", ASM_STABS_OP);
927 assemble_name (asm_out_file, XSTR (symbol, 0));
928 fputc ('\n', asm_out_file);
931 void
932 default_named_section_asm_out_constructor (rtx symbol, int priority)
934 const char *section = ".ctors";
935 char buf[16];
937 /* ??? This only works reliably with the GNU linker. */
938 if (priority != DEFAULT_INIT_PRIORITY)
940 sprintf (buf, ".ctors.%.5u",
941 /* Invert the numbering so the linker puts us in the proper
942 order; constructors are run from right to left, and the
943 linker sorts in increasing order. */
944 MAX_INIT_PRIORITY - priority);
945 section = buf;
948 named_section_flags (section, SECTION_WRITE);
949 assemble_align (POINTER_SIZE);
950 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
953 #ifdef CTORS_SECTION_ASM_OP
954 void
955 ctors_section (void)
957 if (in_section != in_ctors)
959 in_section = in_ctors;
960 fputs (CTORS_SECTION_ASM_OP, asm_out_file);
961 fputc ('\n', asm_out_file);
965 void
966 default_ctor_section_asm_out_constructor (rtx symbol,
967 int priority ATTRIBUTE_UNUSED)
969 ctors_section ();
970 assemble_align (POINTER_SIZE);
971 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
973 #endif
975 /* CONSTANT_POOL_BEFORE_FUNCTION may be defined as an expression with
976 a nonzero value if the constant pool should be output before the
977 start of the function, or a zero value if the pool should output
978 after the end of the function. The default is to put it before the
979 start. */
981 #ifndef CONSTANT_POOL_BEFORE_FUNCTION
982 #define CONSTANT_POOL_BEFORE_FUNCTION 1
983 #endif
985 /* DECL is an object (either VAR_DECL or FUNCTION_DECL) which is going
986 to be output to assembler.
987 Set first_global_object_name and weak_global_object_name as appropriate. */
989 void
990 notice_global_symbol (tree decl)
992 const char **type = &first_global_object_name;
994 if (first_global_object_name
995 || !TREE_PUBLIC (decl) || DECL_EXTERNAL (decl)
996 || !DECL_NAME (decl)
997 || (TREE_CODE (decl) != FUNCTION_DECL
998 && (TREE_CODE (decl) != VAR_DECL
999 || (DECL_COMMON (decl)
1000 && (DECL_INITIAL (decl) == 0
1001 || DECL_INITIAL (decl) == error_mark_node))))
1002 || GET_CODE (DECL_RTL (decl)) != MEM)
1003 return;
1005 /* We win when global object is found, but it is usefull to know about weak
1006 symbol as well so we can produce nicer unique names. */
1007 if (DECL_WEAK (decl) || DECL_ONE_ONLY (decl))
1008 type = &weak_global_object_name;
1010 if (!*type)
1012 const char *p;
1013 char *name;
1014 rtx decl_rtl = DECL_RTL (decl);
1016 p = (* targetm.strip_name_encoding) (XSTR (XEXP (decl_rtl, 0), 0));
1017 name = xstrdup (p);
1019 *type = name;
1023 /* Output assembler code for the constant pool of a function and associated
1024 with defining the name of the function. DECL describes the function.
1025 NAME is the function's name. For the constant pool, we use the current
1026 constant pool data. */
1028 void
1029 assemble_start_function (tree decl, const char *fnname)
1031 int align;
1033 /* The following code does not need preprocessing in the assembler. */
1035 app_disable ();
1037 if (CONSTANT_POOL_BEFORE_FUNCTION)
1038 output_constant_pool (fnname, decl);
1040 resolve_unique_section (decl, 0, flag_function_sections);
1041 function_section (decl);
1043 /* Tell assembler to move to target machine's alignment for functions. */
1044 align = floor_log2 (FUNCTION_BOUNDARY / BITS_PER_UNIT);
1045 if (align < force_align_functions_log)
1046 align = force_align_functions_log;
1047 if (align > 0)
1049 ASM_OUTPUT_ALIGN (asm_out_file, align);
1052 /* Handle a user-specified function alignment.
1053 Note that we still need to align to FUNCTION_BOUNDARY, as above,
1054 because ASM_OUTPUT_MAX_SKIP_ALIGN might not do any alignment at all. */
1055 if (align_functions_log > align
1056 && cfun->function_frequency != FUNCTION_FREQUENCY_UNLIKELY_EXECUTED)
1058 #ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
1059 ASM_OUTPUT_MAX_SKIP_ALIGN (asm_out_file,
1060 align_functions_log, align_functions - 1);
1061 #else
1062 ASM_OUTPUT_ALIGN (asm_out_file, align_functions_log);
1063 #endif
1066 #ifdef ASM_OUTPUT_FUNCTION_PREFIX
1067 ASM_OUTPUT_FUNCTION_PREFIX (asm_out_file, fnname);
1068 #endif
1070 (*debug_hooks->begin_function) (decl);
1072 /* Make function name accessible from other files, if appropriate. */
1074 if (TREE_PUBLIC (decl))
1076 notice_global_symbol (decl);
1078 globalize_decl (decl);
1080 maybe_assemble_visibility (decl);
1083 /* Do any machine/system dependent processing of the function name. */
1084 #ifdef ASM_DECLARE_FUNCTION_NAME
1085 ASM_DECLARE_FUNCTION_NAME (asm_out_file, fnname, current_function_decl);
1086 #else
1087 /* Standard thing is just output label for the function. */
1088 ASM_OUTPUT_LABEL (asm_out_file, fnname);
1089 #endif /* ASM_DECLARE_FUNCTION_NAME */
1092 /* Output assembler code associated with defining the size of the
1093 function. DECL describes the function. NAME is the function's name. */
1095 void
1096 assemble_end_function (tree decl, const char *fnname)
1098 #ifdef ASM_DECLARE_FUNCTION_SIZE
1099 ASM_DECLARE_FUNCTION_SIZE (asm_out_file, fnname, decl);
1100 #endif
1101 if (! CONSTANT_POOL_BEFORE_FUNCTION)
1103 output_constant_pool (fnname, decl);
1104 function_section (decl); /* need to switch back */
1108 /* Assemble code to leave SIZE bytes of zeros. */
1110 void
1111 assemble_zeros (unsigned HOST_WIDE_INT size)
1113 /* Do no output if -fsyntax-only. */
1114 if (flag_syntax_only)
1115 return;
1117 #ifdef ASM_NO_SKIP_IN_TEXT
1118 /* The `space' pseudo in the text section outputs nop insns rather than 0s,
1119 so we must output 0s explicitly in the text section. */
1120 if (ASM_NO_SKIP_IN_TEXT && in_text_section ())
1122 unsigned HOST_WIDE_INT i;
1123 for (i = 0; i < size; i++)
1124 assemble_integer (const0_rtx, 1, BITS_PER_UNIT, 1);
1126 else
1127 #endif
1128 if (size > 0)
1129 ASM_OUTPUT_SKIP (asm_out_file, size);
1132 /* Assemble an alignment pseudo op for an ALIGN-bit boundary. */
1134 void
1135 assemble_align (int align)
1137 if (align > BITS_PER_UNIT)
1139 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
1143 /* Assemble a string constant with the specified C string as contents. */
1145 void
1146 assemble_string (const char *p, int size)
1148 int pos = 0;
1149 int maximum = 2000;
1151 /* If the string is very long, split it up. */
1153 while (pos < size)
1155 int thissize = size - pos;
1156 if (thissize > maximum)
1157 thissize = maximum;
1159 ASM_OUTPUT_ASCII (asm_out_file, p, thissize);
1161 pos += thissize;
1162 p += thissize;
1167 #if defined ASM_OUTPUT_ALIGNED_DECL_LOCAL
1168 #define ASM_EMIT_LOCAL(decl, name, size, rounded) \
1169 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, decl, name, size, DECL_ALIGN (decl))
1170 #else
1171 #if defined ASM_OUTPUT_ALIGNED_LOCAL
1172 #define ASM_EMIT_LOCAL(decl, name, size, rounded) \
1173 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, DECL_ALIGN (decl))
1174 #else
1175 #define ASM_EMIT_LOCAL(decl, name, size, rounded) \
1176 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded)
1177 #endif
1178 #endif
1180 #if defined ASM_OUTPUT_ALIGNED_BSS
1181 #define ASM_EMIT_BSS(decl, name, size, rounded) \
1182 ASM_OUTPUT_ALIGNED_BSS (asm_out_file, decl, name, size, DECL_ALIGN (decl))
1183 #else
1184 #if defined ASM_OUTPUT_BSS
1185 #define ASM_EMIT_BSS(decl, name, size, rounded) \
1186 ASM_OUTPUT_BSS (asm_out_file, decl, name, size, rounded)
1187 #else
1188 #undef ASM_EMIT_BSS
1189 #endif
1190 #endif
1192 #if defined ASM_OUTPUT_ALIGNED_DECL_COMMON
1193 #define ASM_EMIT_COMMON(decl, name, size, rounded) \
1194 ASM_OUTPUT_ALIGNED_DECL_COMMON (asm_out_file, decl, name, size, DECL_ALIGN (decl))
1195 #else
1196 #if defined ASM_OUTPUT_ALIGNED_COMMON
1197 #define ASM_EMIT_COMMON(decl, name, size, rounded) \
1198 ASM_OUTPUT_ALIGNED_COMMON (asm_out_file, name, size, DECL_ALIGN (decl))
1199 #else
1200 #define ASM_EMIT_COMMON(decl, name, size, rounded) \
1201 ASM_OUTPUT_COMMON (asm_out_file, name, size, rounded)
1202 #endif
1203 #endif
1205 static bool
1206 asm_emit_uninitialised (tree decl, const char *name,
1207 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1208 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1210 enum
1212 asm_dest_common,
1213 asm_dest_bss,
1214 asm_dest_local
1216 destination = asm_dest_local;
1218 /* ??? We should handle .bss via select_section mechanisms rather than
1219 via special target hooks. That would eliminate this special case. */
1220 if (TREE_PUBLIC (decl))
1222 if (!DECL_COMMON (decl))
1223 #ifdef ASM_EMIT_BSS
1224 destination = asm_dest_bss;
1225 #else
1226 return false;
1227 #endif
1228 else
1229 destination = asm_dest_common;
1232 if (destination == asm_dest_bss)
1233 globalize_decl (decl);
1234 resolve_unique_section (decl, 0, flag_data_sections);
1236 if (flag_shared_data)
1238 switch (destination)
1240 #ifdef ASM_OUTPUT_SHARED_BSS
1241 case asm_dest_bss:
1242 ASM_OUTPUT_SHARED_BSS (asm_out_file, decl, name, size, rounded);
1243 return;
1244 #endif
1245 #ifdef ASM_OUTPUT_SHARED_COMMON
1246 case asm_dest_common:
1247 ASM_OUTPUT_SHARED_COMMON (asm_out_file, name, size, rounded);
1248 return;
1249 #endif
1250 #ifdef ASM_OUTPUT_SHARED_LOCAL
1251 case asm_dest_local:
1252 ASM_OUTPUT_SHARED_LOCAL (asm_out_file, name, size, rounded);
1253 return;
1254 #endif
1255 default:
1256 break;
1260 switch (destination)
1262 #ifdef ASM_EMIT_BSS
1263 case asm_dest_bss:
1264 ASM_EMIT_BSS (decl, name, size, rounded);
1265 break;
1266 #endif
1267 case asm_dest_common:
1268 ASM_EMIT_COMMON (decl, name, size, rounded);
1269 break;
1270 case asm_dest_local:
1271 ASM_EMIT_LOCAL (decl, name, size, rounded);
1272 break;
1273 default:
1274 abort ();
1277 return true;
1280 /* Assemble everything that is needed for a variable or function declaration.
1281 Not used for automatic variables, and not used for function definitions.
1282 Should not be called for variables of incomplete structure type.
1284 TOP_LEVEL is nonzero if this variable has file scope.
1285 AT_END is nonzero if this is the special handling, at end of compilation,
1286 to define things that have had only tentative definitions.
1287 DONT_OUTPUT_DATA if nonzero means don't actually output the
1288 initial value (that will be done by the caller). */
1290 void
1291 assemble_variable (tree decl, int top_level ATTRIBUTE_UNUSED,
1292 int at_end ATTRIBUTE_UNUSED, int dont_output_data)
1294 const char *name;
1295 unsigned int align;
1296 int reloc = 0;
1297 rtx decl_rtl;
1299 if (lang_hooks.decls.prepare_assemble_variable)
1300 (*lang_hooks.decls.prepare_assemble_variable) (decl);
1302 last_assemble_variable_decl = 0;
1304 /* Normally no need to say anything here for external references,
1305 since assemble_external is called by the language-specific code
1306 when a declaration is first seen. */
1308 if (DECL_EXTERNAL (decl))
1309 return;
1311 /* Output no assembler code for a function declaration.
1312 Only definitions of functions output anything. */
1314 if (TREE_CODE (decl) == FUNCTION_DECL)
1315 return;
1317 /* Do nothing for global register variables. */
1318 if (DECL_RTL_SET_P (decl) && GET_CODE (DECL_RTL (decl)) == REG)
1320 TREE_ASM_WRITTEN (decl) = 1;
1321 return;
1324 /* If type was incomplete when the variable was declared,
1325 see if it is complete now. */
1327 if (DECL_SIZE (decl) == 0)
1328 layout_decl (decl, 0);
1330 /* Still incomplete => don't allocate it; treat the tentative defn
1331 (which is what it must have been) as an `extern' reference. */
1333 if (!dont_output_data && DECL_SIZE (decl) == 0)
1335 error ("%Jstorage size of `%D' isn't known", decl, decl);
1336 TREE_ASM_WRITTEN (decl) = 1;
1337 return;
1340 /* The first declaration of a variable that comes through this function
1341 decides whether it is global (in C, has external linkage)
1342 or local (in C, has internal linkage). So do nothing more
1343 if this function has already run. */
1345 if (TREE_ASM_WRITTEN (decl))
1346 return;
1348 /* Make sure targetm.encode_section_info is invoked before we set
1349 ASM_WRITTEN. */
1350 decl_rtl = DECL_RTL (decl);
1352 TREE_ASM_WRITTEN (decl) = 1;
1354 /* Do no output if -fsyntax-only. */
1355 if (flag_syntax_only)
1356 return;
1358 app_disable ();
1360 if (! dont_output_data
1361 && ! host_integerp (DECL_SIZE_UNIT (decl), 1))
1363 error ("%Jsize of variable '%D' is too large", decl, decl);
1364 return;
1367 name = XSTR (XEXP (decl_rtl, 0), 0);
1368 if (TREE_PUBLIC (decl) && DECL_NAME (decl))
1369 notice_global_symbol (decl);
1371 /* Compute the alignment of this data. */
1373 align = DECL_ALIGN (decl);
1375 /* In the case for initialing an array whose length isn't specified,
1376 where we have not yet been able to do the layout,
1377 figure out the proper alignment now. */
1378 if (dont_output_data && DECL_SIZE (decl) == 0
1379 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
1380 align = MAX (align, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (decl))));
1382 /* Some object file formats have a maximum alignment which they support.
1383 In particular, a.out format supports a maximum alignment of 4. */
1384 #ifndef MAX_OFILE_ALIGNMENT
1385 #define MAX_OFILE_ALIGNMENT BIGGEST_ALIGNMENT
1386 #endif
1387 if (align > MAX_OFILE_ALIGNMENT)
1389 warning ("%Jalignment of '%D' is greater than maximum object "
1390 "file alignment. Using %d", decl, decl,
1391 MAX_OFILE_ALIGNMENT/BITS_PER_UNIT);
1392 align = MAX_OFILE_ALIGNMENT;
1395 /* On some machines, it is good to increase alignment sometimes. */
1396 if (! DECL_USER_ALIGN (decl))
1398 #ifdef DATA_ALIGNMENT
1399 align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
1400 #endif
1401 #ifdef CONSTANT_ALIGNMENT
1402 if (DECL_INITIAL (decl) != 0 && DECL_INITIAL (decl) != error_mark_node)
1403 align = CONSTANT_ALIGNMENT (DECL_INITIAL (decl), align);
1404 #endif
1407 /* Reset the alignment in case we have made it tighter, so we can benefit
1408 from it in get_pointer_alignment. */
1409 DECL_ALIGN (decl) = align;
1410 set_mem_align (decl_rtl, align);
1412 if (TREE_PUBLIC (decl))
1413 maybe_assemble_visibility (decl);
1415 /* Output any data that we will need to use the address of. */
1416 if (DECL_INITIAL (decl) == error_mark_node)
1417 reloc = contains_pointers_p (TREE_TYPE (decl)) ? 3 : 0;
1418 else if (DECL_INITIAL (decl))
1420 reloc = compute_reloc_for_constant (DECL_INITIAL (decl));
1421 output_addressed_constants (DECL_INITIAL (decl));
1423 resolve_unique_section (decl, reloc, flag_data_sections);
1425 /* Handle uninitialized definitions. */
1427 /* If the decl has been given an explicit section name, then it
1428 isn't common, and shouldn't be handled as such. */
1429 if (DECL_SECTION_NAME (decl) || dont_output_data)
1431 /* We don't implement common thread-local data at present. */
1432 else if (DECL_THREAD_LOCAL (decl))
1434 if (DECL_COMMON (decl))
1435 sorry ("thread-local COMMON data not implemented");
1437 else if (DECL_INITIAL (decl) == 0
1438 || DECL_INITIAL (decl) == error_mark_node
1439 || (flag_zero_initialized_in_bss
1440 /* Leave constant zeroes in .rodata so they can be shared. */
1441 && !TREE_READONLY (decl)
1442 && initializer_zerop (DECL_INITIAL (decl))))
1444 unsigned HOST_WIDE_INT size = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
1445 unsigned HOST_WIDE_INT rounded = size;
1447 /* Don't allocate zero bytes of common,
1448 since that means "undefined external" in the linker. */
1449 if (size == 0)
1450 rounded = 1;
1452 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
1453 so that each uninitialized object starts on such a boundary. */
1454 rounded += (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1;
1455 rounded = (rounded / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
1456 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
1458 #if !defined(ASM_OUTPUT_ALIGNED_COMMON) && !defined(ASM_OUTPUT_ALIGNED_DECL_COMMON) && !defined(ASM_OUTPUT_ALIGNED_BSS)
1459 if ((unsigned HOST_WIDE_INT) DECL_ALIGN (decl) / BITS_PER_UNIT > rounded)
1460 warning ("%Jrequested alignment for '%D' is greater than "
1461 "implemented alignment of %d", decl, decl, rounded);
1462 #endif
1464 /* If the target cannot output uninitialized but not common global data
1465 in .bss, then we have to use .data, so fall through. */
1466 if (asm_emit_uninitialised (decl, name, size, rounded))
1467 return;
1470 /* Handle initialized definitions.
1471 Also handle uninitialized global definitions if -fno-common and the
1472 target doesn't support ASM_OUTPUT_BSS. */
1474 /* First make the assembler name(s) global if appropriate. */
1475 if (TREE_PUBLIC (decl) && DECL_NAME (decl))
1476 globalize_decl (decl);
1478 /* Switch to the appropriate section. */
1479 variable_section (decl, reloc);
1481 /* dbxout.c needs to know this. */
1482 if (in_text_section ())
1483 DECL_IN_TEXT_SECTION (decl) = 1;
1485 /* Output the alignment of this data. */
1486 if (align > BITS_PER_UNIT)
1488 ASM_OUTPUT_ALIGN (asm_out_file,
1489 floor_log2 (DECL_ALIGN (decl) / BITS_PER_UNIT));
1492 /* Do any machine/system dependent processing of the object. */
1493 #ifdef ASM_DECLARE_OBJECT_NAME
1494 last_assemble_variable_decl = decl;
1495 ASM_DECLARE_OBJECT_NAME (asm_out_file, name, decl);
1496 #else
1497 /* Standard thing is just output label for the object. */
1498 ASM_OUTPUT_LABEL (asm_out_file, name);
1499 #endif /* ASM_DECLARE_OBJECT_NAME */
1501 if (!dont_output_data)
1503 if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node)
1504 /* Output the actual data. */
1505 output_constant (DECL_INITIAL (decl),
1506 tree_low_cst (DECL_SIZE_UNIT (decl), 1),
1507 align);
1508 else
1509 /* Leave space for it. */
1510 assemble_zeros (tree_low_cst (DECL_SIZE_UNIT (decl), 1));
1514 /* Return 1 if type TYPE contains any pointers. */
1516 static int
1517 contains_pointers_p (tree type)
1519 switch (TREE_CODE (type))
1521 case POINTER_TYPE:
1522 case REFERENCE_TYPE:
1523 /* I'm not sure whether OFFSET_TYPE needs this treatment,
1524 so I'll play safe and return 1. */
1525 case OFFSET_TYPE:
1526 return 1;
1528 case RECORD_TYPE:
1529 case UNION_TYPE:
1530 case QUAL_UNION_TYPE:
1532 tree fields;
1533 /* For a type that has fields, see if the fields have pointers. */
1534 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
1535 if (TREE_CODE (fields) == FIELD_DECL
1536 && contains_pointers_p (TREE_TYPE (fields)))
1537 return 1;
1538 return 0;
1541 case ARRAY_TYPE:
1542 /* An array type contains pointers if its element type does. */
1543 return contains_pointers_p (TREE_TYPE (type));
1545 default:
1546 return 0;
1550 #ifdef ASM_OUTPUT_EXTERNAL
1551 /* True if DECL is a function decl for which no out-of-line copy exists.
1552 It is assumed that DECL's assembler name has been set. */
1554 static bool
1555 incorporeal_function_p (tree decl)
1557 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1559 const char *name;
1561 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
1562 && DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA)
1563 return true;
1565 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
1566 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
1567 return true;
1569 return false;
1571 #endif
1573 /* Output something to declare an external symbol to the assembler.
1574 (Most assemblers don't need this, so we normally output nothing.)
1575 Do nothing if DECL is not external. */
1577 void
1578 assemble_external (tree decl ATTRIBUTE_UNUSED)
1580 /* Because most platforms do not define ASM_OUTPUT_EXTERNAL, the
1581 main body of this code is only rarely exercised. To provide some
1582 testing, on all platforms, we make sure that the ASM_OUT_FILE is
1583 open. If it's not, we should not be calling this function. */
1584 if (!asm_out_file)
1585 abort ();
1587 #ifdef ASM_OUTPUT_EXTERNAL
1588 if (DECL_P (decl) && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl))
1590 rtx rtl = DECL_RTL (decl);
1592 if (GET_CODE (rtl) == MEM && GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF
1593 && !SYMBOL_REF_USED (XEXP (rtl, 0))
1594 && !incorporeal_function_p (decl))
1596 /* Some systems do require some output. */
1597 SYMBOL_REF_USED (XEXP (rtl, 0)) = 1;
1598 ASM_OUTPUT_EXTERNAL (asm_out_file, decl, XSTR (XEXP (rtl, 0), 0));
1601 #endif
1604 /* Similar, for calling a library function FUN. */
1606 void
1607 assemble_external_libcall (rtx fun)
1609 /* Declare library function name external when first used, if nec. */
1610 if (! SYMBOL_REF_USED (fun))
1612 SYMBOL_REF_USED (fun) = 1;
1613 (*targetm.asm_out.external_libcall) (fun);
1617 /* Assemble a label named NAME. */
1619 void
1620 assemble_label (const char *name)
1622 ASM_OUTPUT_LABEL (asm_out_file, name);
1625 /* Set the symbol_referenced flag for ID and notify callgraph code. */
1626 void
1627 mark_referenced (tree id)
1629 if (!TREE_SYMBOL_REFERENCED (id))
1631 struct cgraph_node *node;
1632 struct cgraph_varpool_node *vnode;
1634 if (!cgraph_global_info_ready)
1636 node = cgraph_node_for_identifier (id);
1637 if (node)
1638 cgraph_mark_needed_node (node);
1641 vnode = cgraph_varpool_node_for_identifier (id);
1642 if (vnode)
1643 cgraph_varpool_mark_needed_node (vnode);
1645 TREE_SYMBOL_REFERENCED (id) = 1;
1648 /* Output to FILE a reference to the assembler name of a C-level name NAME.
1649 If NAME starts with a *, the rest of NAME is output verbatim.
1650 Otherwise NAME is transformed in an implementation-defined way
1651 (usually by the addition of an underscore).
1652 Many macros in the tm file are defined to call this function. */
1654 void
1655 assemble_name (FILE *file, const char *name)
1657 const char *real_name;
1658 tree id;
1660 real_name = (* targetm.strip_name_encoding) (name);
1662 id = maybe_get_identifier (real_name);
1663 if (id)
1664 mark_referenced (id);
1666 if (name[0] == '*')
1667 fputs (&name[1], file);
1668 else
1669 ASM_OUTPUT_LABELREF (file, name);
1672 /* Allocate SIZE bytes writable static space with a gensym name
1673 and return an RTX to refer to its address. */
1676 assemble_static_space (unsigned HOST_WIDE_INT size)
1678 char name[12];
1679 const char *namestring;
1680 rtx x;
1682 #if 0
1683 if (flag_shared_data)
1684 data_section ();
1685 #endif
1687 ASM_GENERATE_INTERNAL_LABEL (name, "LF", const_labelno);
1688 ++const_labelno;
1689 namestring = ggc_strdup (name);
1691 x = gen_rtx_SYMBOL_REF (Pmode, namestring);
1692 SYMBOL_REF_FLAGS (x) = SYMBOL_FLAG_LOCAL;
1694 #ifdef ASM_OUTPUT_ALIGNED_DECL_LOCAL
1695 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, NULL_TREE, name, size,
1696 BIGGEST_ALIGNMENT);
1697 #else
1698 #ifdef ASM_OUTPUT_ALIGNED_LOCAL
1699 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, BIGGEST_ALIGNMENT);
1700 #else
1702 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
1703 so that each uninitialized object starts on such a boundary. */
1704 /* Variable `rounded' might or might not be used in ASM_OUTPUT_LOCAL. */
1705 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED
1706 = ((size + (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1)
1707 / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
1708 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
1709 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
1711 #endif
1712 #endif
1713 return x;
1716 /* Assemble the static constant template for function entry trampolines.
1717 This is done at most once per compilation.
1718 Returns an RTX for the address of the template. */
1720 #ifdef TRAMPOLINE_TEMPLATE
1722 assemble_trampoline_template (void)
1724 char label[256];
1725 const char *name;
1726 int align;
1727 rtx symbol;
1729 /* By default, put trampoline templates in read-only data section. */
1731 #ifdef TRAMPOLINE_SECTION
1732 TRAMPOLINE_SECTION ();
1733 #else
1734 readonly_data_section ();
1735 #endif
1737 /* Write the assembler code to define one. */
1738 align = floor_log2 (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
1739 if (align > 0)
1741 ASM_OUTPUT_ALIGN (asm_out_file, align);
1744 (*targetm.asm_out.internal_label) (asm_out_file, "LTRAMP", 0);
1745 TRAMPOLINE_TEMPLATE (asm_out_file);
1747 /* Record the rtl to refer to it. */
1748 ASM_GENERATE_INTERNAL_LABEL (label, "LTRAMP", 0);
1749 name = ggc_strdup (label);
1750 symbol = gen_rtx_SYMBOL_REF (Pmode, name);
1751 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_LOCAL;
1753 return symbol;
1755 #endif
1757 /* A and B are either alignments or offsets. Return the minimum alignment
1758 that may be assumed after adding the two together. */
1760 static inline unsigned
1761 min_align (unsigned int a, unsigned int b)
1763 return (a | b) & -(a | b);
1766 /* Return the assembler directive for creating a given kind of integer
1767 object. SIZE is the number of bytes in the object and ALIGNED_P
1768 indicates whether it is known to be aligned. Return NULL if the
1769 assembly dialect has no such directive.
1771 The returned string should be printed at the start of a new line and
1772 be followed immediately by the object's initial value. */
1774 const char *
1775 integer_asm_op (int size, int aligned_p)
1777 struct asm_int_op *ops;
1779 if (aligned_p)
1780 ops = &targetm.asm_out.aligned_op;
1781 else
1782 ops = &targetm.asm_out.unaligned_op;
1784 switch (size)
1786 case 1:
1787 return targetm.asm_out.byte_op;
1788 case 2:
1789 return ops->hi;
1790 case 4:
1791 return ops->si;
1792 case 8:
1793 return ops->di;
1794 case 16:
1795 return ops->ti;
1796 default:
1797 return NULL;
1801 /* Use directive OP to assemble an integer object X. Print OP at the
1802 start of the line, followed immediately by the value of X. */
1804 void
1805 assemble_integer_with_op (const char *op, rtx x)
1807 fputs (op, asm_out_file);
1808 output_addr_const (asm_out_file, x);
1809 fputc ('\n', asm_out_file);
1812 /* The default implementation of the asm_out.integer target hook. */
1814 bool
1815 default_assemble_integer (rtx x ATTRIBUTE_UNUSED,
1816 unsigned int size ATTRIBUTE_UNUSED,
1817 int aligned_p ATTRIBUTE_UNUSED)
1819 const char *op = integer_asm_op (size, aligned_p);
1820 return op && (assemble_integer_with_op (op, x), true);
1823 /* Assemble the integer constant X into an object of SIZE bytes. ALIGN is
1824 the alignment of the integer in bits. Return 1 if we were able to output
1825 the constant, otherwise 0. If FORCE is nonzero, abort if we can't output
1826 the constant. */
1828 bool
1829 assemble_integer (rtx x, unsigned int size, unsigned int align, int force)
1831 int aligned_p;
1833 aligned_p = (align >= MIN (size * BITS_PER_UNIT, BIGGEST_ALIGNMENT));
1835 /* See if the target hook can handle this kind of object. */
1836 if ((*targetm.asm_out.integer) (x, size, aligned_p))
1837 return true;
1839 /* If the object is a multi-byte one, try splitting it up. Split
1840 it into words it if is multi-word, otherwise split it into bytes. */
1841 if (size > 1)
1843 enum machine_mode omode, imode;
1844 unsigned int subalign;
1845 unsigned int subsize, i;
1847 subsize = size > UNITS_PER_WORD? UNITS_PER_WORD : 1;
1848 subalign = MIN (align, subsize * BITS_PER_UNIT);
1849 omode = mode_for_size (subsize * BITS_PER_UNIT, MODE_INT, 0);
1850 imode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
1852 for (i = 0; i < size; i += subsize)
1854 rtx partial = simplify_subreg (omode, x, imode, i);
1855 if (!partial || !assemble_integer (partial, subsize, subalign, 0))
1856 break;
1858 if (i == size)
1859 return true;
1861 /* If we've printed some of it, but not all of it, there's no going
1862 back now. */
1863 if (i > 0)
1864 abort ();
1867 if (force)
1868 abort ();
1870 return false;
1873 void
1874 assemble_real (REAL_VALUE_TYPE d, enum machine_mode mode, unsigned int align)
1876 long data[4];
1877 int i;
1878 int bitsize, nelts, nunits, units_per;
1880 /* This is hairy. We have a quantity of known size. real_to_target
1881 will put it into an array of *host* longs, 32 bits per element
1882 (even if long is more than 32 bits). We need to determine the
1883 number of array elements that are occupied (nelts) and the number
1884 of *target* min-addressable units that will be occupied in the
1885 object file (nunits). We cannot assume that 32 divides the
1886 mode's bitsize (size * BITS_PER_UNIT) evenly.
1888 size * BITS_PER_UNIT is used here to make sure that padding bits
1889 (which might appear at either end of the value; real_to_target
1890 will include the padding bits in its output array) are included. */
1892 nunits = GET_MODE_SIZE (mode);
1893 bitsize = nunits * BITS_PER_UNIT;
1894 nelts = CEIL (bitsize, 32);
1895 units_per = 32 / BITS_PER_UNIT;
1897 real_to_target (data, &d, mode);
1899 /* Put out the first word with the specified alignment. */
1900 assemble_integer (GEN_INT (data[0]), MIN (nunits, units_per), align, 1);
1901 nunits -= units_per;
1903 /* Subsequent words need only 32-bit alignment. */
1904 align = min_align (align, 32);
1906 for (i = 1; i < nelts; i++)
1908 assemble_integer (GEN_INT (data[i]), MIN (nunits, units_per), align, 1);
1909 nunits -= units_per;
1913 /* Given an expression EXP with a constant value,
1914 reduce it to the sum of an assembler symbol and an integer.
1915 Store them both in the structure *VALUE.
1916 Abort if EXP does not reduce. */
1918 struct addr_const GTY(())
1920 rtx base;
1921 HOST_WIDE_INT offset;
1924 static void
1925 decode_addr_const (tree exp, struct addr_const *value)
1927 tree target = TREE_OPERAND (exp, 0);
1928 int offset = 0;
1929 rtx x;
1931 while (1)
1933 if (TREE_CODE (target) == COMPONENT_REF
1934 && host_integerp (byte_position (TREE_OPERAND (target, 1)), 0))
1937 offset += int_byte_position (TREE_OPERAND (target, 1));
1938 target = TREE_OPERAND (target, 0);
1940 else if (TREE_CODE (target) == ARRAY_REF
1941 || TREE_CODE (target) == ARRAY_RANGE_REF)
1943 offset += (tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (target)), 1)
1944 * tree_low_cst (TREE_OPERAND (target, 1), 0));
1945 target = TREE_OPERAND (target, 0);
1947 else
1948 break;
1951 switch (TREE_CODE (target))
1953 case VAR_DECL:
1954 case FUNCTION_DECL:
1955 x = DECL_RTL (target);
1956 break;
1958 case LABEL_DECL:
1959 x = gen_rtx_MEM (FUNCTION_MODE,
1960 gen_rtx_LABEL_REF (VOIDmode, force_label_rtx (target)));
1961 break;
1963 case REAL_CST:
1964 case STRING_CST:
1965 case COMPLEX_CST:
1966 case CONSTRUCTOR:
1967 case INTEGER_CST:
1968 x = output_constant_def (target, 1);
1969 break;
1971 default:
1972 abort ();
1975 if (GET_CODE (x) != MEM)
1976 abort ();
1977 x = XEXP (x, 0);
1979 value->base = x;
1980 value->offset = offset;
1983 /* Uniquize all constants that appear in memory.
1984 Each constant in memory thus far output is recorded
1985 in `const_desc_table'. */
1987 struct constant_descriptor_tree GTY(())
1989 /* A MEM for the constant. */
1990 rtx rtl;
1992 /* The value of the constant. */
1993 tree value;
1996 static GTY((param_is (struct constant_descriptor_tree)))
1997 htab_t const_desc_htab;
1999 static struct constant_descriptor_tree * build_constant_desc (tree);
2000 static void maybe_output_constant_def_contents (struct constant_descriptor_tree *, int);
2002 /* Compute a hash code for a constant expression. */
2004 static hashval_t
2005 const_desc_hash (const void *ptr)
2007 return const_hash_1 (((struct constant_descriptor_tree *)ptr)->value);
2010 static hashval_t
2011 const_hash_1 (const tree exp)
2013 const char *p;
2014 hashval_t hi;
2015 int len, i;
2016 enum tree_code code = TREE_CODE (exp);
2018 /* Either set P and LEN to the address and len of something to hash and
2019 exit the switch or return a value. */
2021 switch (code)
2023 case INTEGER_CST:
2024 p = (char *) &TREE_INT_CST (exp);
2025 len = sizeof TREE_INT_CST (exp);
2026 break;
2028 case REAL_CST:
2029 return real_hash (TREE_REAL_CST_PTR (exp));
2031 case STRING_CST:
2032 if (flag_writable_strings)
2034 p = (char *) &exp;
2035 len = sizeof exp;
2037 else
2039 p = TREE_STRING_POINTER (exp);
2040 len = TREE_STRING_LENGTH (exp);
2042 break;
2044 case COMPLEX_CST:
2045 return (const_hash_1 (TREE_REALPART (exp)) * 5
2046 + const_hash_1 (TREE_IMAGPART (exp)));
2048 case CONSTRUCTOR:
2049 if (TREE_CODE (TREE_TYPE (exp)) == SET_TYPE)
2051 char *tmp;
2053 len = int_size_in_bytes (TREE_TYPE (exp));
2054 tmp = alloca (len);
2055 get_set_constructor_bytes (exp, (unsigned char *) tmp, len);
2056 p = tmp;
2057 break;
2059 else
2061 tree link;
2063 hi = 5 + int_size_in_bytes (TREE_TYPE (exp));
2065 for (link = CONSTRUCTOR_ELTS (exp); link; link = TREE_CHAIN (link))
2066 if (TREE_VALUE (link))
2067 hi = hi * 603 + const_hash_1 (TREE_VALUE (link));
2069 return hi;
2072 case ADDR_EXPR:
2073 case FDESC_EXPR:
2075 struct addr_const value;
2077 decode_addr_const (exp, &value);
2078 if (GET_CODE (value.base) == SYMBOL_REF)
2080 /* Don't hash the address of the SYMBOL_REF;
2081 only use the offset and the symbol name. */
2082 hi = value.offset;
2083 p = XSTR (value.base, 0);
2084 for (i = 0; p[i] != 0; i++)
2085 hi = ((hi * 613) + (unsigned) (p[i]));
2087 else if (GET_CODE (value.base) == LABEL_REF)
2088 hi = value.offset + CODE_LABEL_NUMBER (XEXP (value.base, 0)) * 13;
2089 else
2090 abort ();
2092 return hi;
2094 case PLUS_EXPR:
2095 case MINUS_EXPR:
2096 return (const_hash_1 (TREE_OPERAND (exp, 0)) * 9
2097 + const_hash_1 (TREE_OPERAND (exp, 1)));
2099 case NOP_EXPR:
2100 case CONVERT_EXPR:
2101 case NON_LVALUE_EXPR:
2102 return const_hash_1 (TREE_OPERAND (exp, 0)) * 7 + 2;
2104 default:
2105 /* A language specific constant. Just hash the code. */
2106 return code;
2109 /* Compute hashing function. */
2110 hi = len;
2111 for (i = 0; i < len; i++)
2112 hi = ((hi * 613) + (unsigned) (p[i]));
2114 return hi;
2117 /* Wrapper of compare_constant, for the htab interface. */
2118 static int
2119 const_desc_eq (const void *p1, const void *p2)
2121 return compare_constant (((struct constant_descriptor_tree *)p1)->value,
2122 ((struct constant_descriptor_tree *)p2)->value);
2125 /* Compare t1 and t2, and return 1 only if they are known to result in
2126 the same bit pattern on output. */
2128 static int
2129 compare_constant (const tree t1, const tree t2)
2131 enum tree_code typecode;
2133 if (t1 == NULL_TREE)
2134 return t2 == NULL_TREE;
2135 if (t2 == NULL_TREE)
2136 return 0;
2138 if (TREE_CODE (t1) != TREE_CODE (t2))
2139 return 0;
2141 switch (TREE_CODE (t1))
2143 case INTEGER_CST:
2144 /* Integer constants are the same only if the same width of type. */
2145 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
2146 return 0;
2147 return tree_int_cst_equal (t1, t2);
2149 case REAL_CST:
2150 /* Real constants are the same only if the same width of type. */
2151 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
2152 return 0;
2154 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
2156 case STRING_CST:
2157 if (flag_writable_strings)
2158 return t1 == t2;
2160 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
2161 return 0;
2163 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
2164 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
2165 TREE_STRING_LENGTH (t1)));
2167 case COMPLEX_CST:
2168 return (compare_constant (TREE_REALPART (t1), TREE_REALPART (t2))
2169 && compare_constant (TREE_IMAGPART (t1), TREE_IMAGPART (t2)));
2171 case CONSTRUCTOR:
2172 typecode = TREE_CODE (TREE_TYPE (t1));
2173 if (typecode != TREE_CODE (TREE_TYPE (t2)))
2174 return 0;
2176 if (typecode == SET_TYPE)
2178 int len = int_size_in_bytes (TREE_TYPE (t2));
2179 unsigned char *tmp1, *tmp2;
2181 if (int_size_in_bytes (TREE_TYPE (t1)) != len)
2182 return 0;
2184 tmp1 = alloca (len);
2185 tmp2 = alloca (len);
2187 if (get_set_constructor_bytes (t1, tmp1, len) != NULL_TREE)
2188 return 0;
2189 if (get_set_constructor_bytes (t2, tmp2, len) != NULL_TREE)
2190 return 0;
2192 return memcmp (tmp1, tmp2, len) == 0;
2194 else
2196 tree l1, l2;
2198 if (typecode == ARRAY_TYPE)
2200 HOST_WIDE_INT size_1 = int_size_in_bytes (TREE_TYPE (t1));
2201 /* For arrays, check that the sizes all match. */
2202 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))
2203 || size_1 == -1
2204 || size_1 != int_size_in_bytes (TREE_TYPE (t2)))
2205 return 0;
2207 else
2209 /* For record and union constructors, require exact type
2210 equality. */
2211 if (TREE_TYPE (t1) != TREE_TYPE (t2))
2212 return 0;
2215 for (l1 = CONSTRUCTOR_ELTS (t1), l2 = CONSTRUCTOR_ELTS (t2);
2216 l1 && l2;
2217 l1 = TREE_CHAIN (l1), l2 = TREE_CHAIN (l2))
2219 /* Check that each value is the same... */
2220 if (! compare_constant (TREE_VALUE (l1), TREE_VALUE (l2)))
2221 return 0;
2222 /* ... and that they apply to the same fields! */
2223 if (typecode == ARRAY_TYPE)
2225 if (! compare_constant (TREE_PURPOSE (l1),
2226 TREE_PURPOSE (l2)))
2227 return 0;
2229 else
2231 if (TREE_PURPOSE (l1) != TREE_PURPOSE (l2))
2232 return 0;
2236 return l1 == NULL_TREE && l2 == NULL_TREE;
2239 case ADDR_EXPR:
2240 case FDESC_EXPR:
2242 struct addr_const value1, value2;
2244 decode_addr_const (t1, &value1);
2245 decode_addr_const (t2, &value2);
2246 return (value1.offset == value2.offset
2247 && strcmp (XSTR (value1.base, 0), XSTR (value2.base, 0)) == 0);
2250 case PLUS_EXPR:
2251 case MINUS_EXPR:
2252 case RANGE_EXPR:
2253 return (compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
2254 && compare_constant(TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
2256 case NOP_EXPR:
2257 case CONVERT_EXPR:
2258 case NON_LVALUE_EXPR:
2259 return compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2261 default:
2263 tree nt1, nt2;
2264 nt1 = (*lang_hooks.expand_constant) (t1);
2265 nt2 = (*lang_hooks.expand_constant) (t2);
2266 if (nt1 != t1 || nt2 != t2)
2267 return compare_constant (nt1, nt2);
2268 else
2269 return 0;
2273 /* Should not get here. */
2274 abort ();
2277 /* Make a copy of the whole tree structure for a constant. This
2278 handles the same types of nodes that compare_constant handles. */
2280 static tree
2281 copy_constant (tree exp)
2283 switch (TREE_CODE (exp))
2285 case ADDR_EXPR:
2286 /* For ADDR_EXPR, we do not want to copy the decl whose address
2287 is requested. We do want to copy constants though. */
2288 if (TREE_CODE_CLASS (TREE_CODE (TREE_OPERAND (exp, 0))) == 'c')
2289 return build1 (TREE_CODE (exp), TREE_TYPE (exp),
2290 copy_constant (TREE_OPERAND (exp, 0)));
2291 else
2292 return copy_node (exp);
2294 case INTEGER_CST:
2295 case REAL_CST:
2296 case STRING_CST:
2297 return copy_node (exp);
2299 case COMPLEX_CST:
2300 return build_complex (TREE_TYPE (exp),
2301 copy_constant (TREE_REALPART (exp)),
2302 copy_constant (TREE_IMAGPART (exp)));
2304 case PLUS_EXPR:
2305 case MINUS_EXPR:
2306 return build (TREE_CODE (exp), TREE_TYPE (exp),
2307 copy_constant (TREE_OPERAND (exp, 0)),
2308 copy_constant (TREE_OPERAND (exp, 1)));
2310 case NOP_EXPR:
2311 case CONVERT_EXPR:
2312 case NON_LVALUE_EXPR:
2313 case VIEW_CONVERT_EXPR:
2314 return build1 (TREE_CODE (exp), TREE_TYPE (exp),
2315 copy_constant (TREE_OPERAND (exp, 0)));
2317 case CONSTRUCTOR:
2319 tree copy = copy_node (exp);
2320 tree list = copy_list (CONSTRUCTOR_ELTS (exp));
2321 tree tail;
2323 CONSTRUCTOR_ELTS (copy) = list;
2324 for (tail = list; tail; tail = TREE_CHAIN (tail))
2325 TREE_VALUE (tail) = copy_constant (TREE_VALUE (tail));
2326 if (TREE_CODE (TREE_TYPE (exp)) == SET_TYPE)
2327 for (tail = list; tail; tail = TREE_CHAIN (tail))
2328 TREE_PURPOSE (tail) = copy_constant (TREE_PURPOSE (tail));
2330 return copy;
2333 default:
2335 tree t;
2336 t = (*lang_hooks.expand_constant) (exp);
2337 if (t != exp)
2338 return copy_constant (t);
2339 else
2340 abort ();
2345 /* Subroutine of output_constant_def:
2346 No constant equal to EXP is known to have been output.
2347 Make a constant descriptor to enter EXP in the hash table.
2348 Assign the label number and construct RTL to refer to the
2349 constant's location in memory.
2350 Caller is responsible for updating the hash table. */
2352 static struct constant_descriptor_tree *
2353 build_constant_desc (tree exp)
2355 rtx symbol;
2356 rtx rtl;
2357 char label[256];
2358 int labelno;
2359 struct constant_descriptor_tree *desc;
2361 desc = ggc_alloc (sizeof (*desc));
2362 if (flag_writable_strings && TREE_CODE (exp) == STRING_CST)
2363 desc->value = exp;
2364 else
2365 desc->value = copy_constant (exp);
2367 /* Create a string containing the label name, in LABEL. */
2368 labelno = const_labelno++;
2369 ASM_GENERATE_INTERNAL_LABEL (label, "LC", labelno);
2371 /* We have a symbol name; construct the SYMBOL_REF and the MEM. */
2372 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
2373 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_LOCAL;
2374 SYMBOL_REF_DECL (symbol) = desc->value;
2375 TREE_CONSTANT_POOL_ADDRESS_P (symbol) = 1;
2377 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (exp)), symbol);
2378 set_mem_attributes (rtl, exp, 1);
2379 set_mem_alias_set (rtl, 0);
2380 set_mem_alias_set (rtl, const_alias_set);
2382 /* Set flags or add text to the name to record information, such as
2383 that it is a local symbol. If the name is changed, the macro
2384 ASM_OUTPUT_LABELREF will have to know how to strip this
2385 information. This call might invalidate our local variable
2386 SYMBOL; we can't use it afterward. */
2388 (*targetm.encode_section_info) (exp, rtl, true);
2390 desc->rtl = rtl;
2392 return desc;
2395 /* Return an rtx representing a reference to constant data in memory
2396 for the constant expression EXP.
2398 If assembler code for such a constant has already been output,
2399 return an rtx to refer to it.
2400 Otherwise, output such a constant in memory
2401 and generate an rtx for it.
2403 If DEFER is nonzero, this constant can be deferred and output only
2404 if referenced in the function after all optimizations.
2406 `const_desc_table' records which constants already have label strings. */
2409 output_constant_def (tree exp, int defer)
2411 struct constant_descriptor_tree *desc;
2412 struct constant_descriptor_tree key;
2413 void **loc;
2415 /* Look up EXP in the table of constant descriptors. If we didn't find
2416 it, create a new one. */
2417 key.value = exp;
2418 loc = htab_find_slot (const_desc_htab, &key, INSERT);
2420 desc = *loc;
2421 if (desc == 0)
2423 desc = build_constant_desc (exp);
2424 *loc = desc;
2427 maybe_output_constant_def_contents (desc, defer);
2428 return desc->rtl;
2431 /* Subroutine of output_constant_def: Decide whether or not we need to
2432 output the constant DESC now, and if so, do it. */
2433 static void
2434 maybe_output_constant_def_contents (struct constant_descriptor_tree *desc,
2435 int defer)
2437 rtx symbol = XEXP (desc->rtl, 0);
2438 tree exp = desc->value;
2440 if (flag_syntax_only)
2441 return;
2443 if (TREE_ASM_WRITTEN (exp))
2444 /* Already output; don't do it again. */
2445 return;
2447 /* The only constants that cannot safely be deferred, assuming the
2448 context allows it, are strings under flag_writable_strings. */
2449 if (defer && (TREE_CODE (exp) != STRING_CST || !flag_writable_strings))
2451 /* Increment n_deferred_constants if it exists. It needs to be at
2452 least as large as the number of constants actually referred to
2453 by the function. If it's too small we'll stop looking too early
2454 and fail to emit constants; if it's too large we'll only look
2455 through the entire function when we could have stopped earlier. */
2456 if (cfun)
2457 n_deferred_constants++;
2458 return;
2461 output_constant_def_contents (symbol);
2464 /* We must output the constant data referred to by SYMBOL; do so. */
2466 static void
2467 output_constant_def_contents (rtx symbol)
2469 tree exp = SYMBOL_REF_DECL (symbol);
2470 const char *label = XSTR (symbol, 0);
2471 HOST_WIDE_INT size;
2473 /* Make sure any other constants whose addresses appear in EXP
2474 are assigned label numbers. */
2475 int reloc = compute_reloc_for_constant (exp);
2477 /* Align the location counter as required by EXP's data type. */
2478 int align = TYPE_ALIGN (TREE_TYPE (exp));
2479 #ifdef CONSTANT_ALIGNMENT
2480 align = CONSTANT_ALIGNMENT (exp, align);
2481 #endif
2483 output_addressed_constants (exp);
2485 /* We are no longer deferring this constant. */
2486 TREE_ASM_WRITTEN (exp) = 1;
2488 if (IN_NAMED_SECTION (exp))
2489 named_section (exp, NULL, reloc);
2490 else
2491 (*targetm.asm_out.select_section) (exp, reloc, align);
2493 if (align > BITS_PER_UNIT)
2495 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
2498 size = int_size_in_bytes (TREE_TYPE (exp));
2499 if (TREE_CODE (exp) == STRING_CST)
2500 size = MAX (TREE_STRING_LENGTH (exp), size);
2502 /* Do any machine/system dependent processing of the constant. */
2503 #ifdef ASM_DECLARE_CONSTANT_NAME
2504 ASM_DECLARE_CONSTANT_NAME (asm_out_file, label, exp, size);
2505 #else
2506 /* Standard thing is just output label for the constant. */
2507 ASM_OUTPUT_LABEL (asm_out_file, label);
2508 #endif /* ASM_DECLARE_CONSTANT_NAME */
2510 /* Output the value of EXP. */
2511 output_constant (exp, size, align);
2514 /* A constant which was deferred in its original location has been
2515 inserted by the RTL inliner into a different function. The
2516 current function's deferred constant count must be incremented. */
2517 void
2518 notice_rtl_inlining_of_deferred_constant (void)
2520 n_deferred_constants++;
2523 /* Look up EXP in the table of constant descriptors. Return the rtl
2524 if it has been emitted, else null. */
2527 lookup_constant_def (tree exp)
2529 struct constant_descriptor_tree *desc;
2530 struct constant_descriptor_tree key;
2532 key.value = exp;
2533 desc = htab_find (const_desc_htab, &key);
2535 return (desc ? desc->rtl : NULL_RTX);
2538 /* Used in the hash tables to avoid outputting the same constant
2539 twice. Unlike 'struct constant_descriptor_tree', RTX constants
2540 are output once per function, not once per file. */
2541 /* ??? Only a few targets need per-function constant pools. Most
2542 can use one per-file pool. Should add a targetm bit to tell the
2543 difference. */
2545 struct rtx_constant_pool GTY(())
2547 /* Pointers to first and last constant in pool, as ordered by offset. */
2548 struct constant_descriptor_rtx *first;
2549 struct constant_descriptor_rtx *last;
2551 /* Hash facility for making memory-constants from constant rtl-expressions.
2552 It is used on RISC machines where immediate integer arguments and
2553 constant addresses are restricted so that such constants must be stored
2554 in memory. */
2555 htab_t GTY((param_is (struct constant_descriptor_rtx))) const_rtx_htab;
2556 htab_t GTY((param_is (struct constant_descriptor_rtx))) const_rtx_sym_htab;
2558 /* Current offset in constant pool (does not include any
2559 machine-specific header). */
2560 HOST_WIDE_INT offset;
2563 struct constant_descriptor_rtx GTY((chain_next ("%h.next")))
2565 struct constant_descriptor_rtx *next;
2566 rtx mem;
2567 rtx sym;
2568 rtx constant;
2569 HOST_WIDE_INT offset;
2570 hashval_t hash;
2571 enum machine_mode mode;
2572 unsigned int align;
2573 int labelno;
2574 int mark;
2577 /* Hash and compare functions for const_rtx_htab. */
2579 static hashval_t
2580 const_desc_rtx_hash (const void *ptr)
2582 const struct constant_descriptor_rtx *desc = ptr;
2583 return desc->hash;
2586 static int
2587 const_desc_rtx_eq (const void *a, const void *b)
2589 const struct constant_descriptor_rtx *x = a;
2590 const struct constant_descriptor_rtx *y = b;
2592 if (x->mode != y->mode)
2593 return 0;
2594 return rtx_equal_p (x->constant, y->constant);
2597 /* Hash and compare functions for const_rtx_sym_htab. */
2599 static hashval_t
2600 const_desc_rtx_sym_hash (const void *ptr)
2602 const struct constant_descriptor_rtx *desc = ptr;
2603 return htab_hash_string (XSTR (desc->sym, 0));
2606 static int
2607 const_desc_rtx_sym_eq (const void *a, const void *b)
2609 const struct constant_descriptor_rtx *x = a;
2610 const struct constant_descriptor_rtx *y = b;
2611 return XSTR (x->sym, 0) == XSTR (y->sym, 0);
2614 /* This is the worker function for const_rtx_hash, called via for_each_rtx. */
2616 static int
2617 const_rtx_hash_1 (rtx *xp, void *data)
2619 unsigned HOST_WIDE_INT hwi;
2620 enum machine_mode mode;
2621 enum rtx_code code;
2622 hashval_t h, *hp;
2623 rtx x;
2625 x = *xp;
2626 code = GET_CODE (x);
2627 mode = GET_MODE (x);
2628 h = (hashval_t) code * 1048573 + mode;
2630 switch (code)
2632 case CONST_INT:
2633 hwi = INTVAL (x);
2634 fold_hwi:
2636 const int shift = sizeof (hashval_t) * CHAR_BIT;
2637 const int n = sizeof (HOST_WIDE_INT) / sizeof (hashval_t);
2638 int i;
2640 h ^= (hashval_t) hwi;
2641 for (i = 1; i < n; ++i)
2643 hwi >>= shift;
2644 h ^= (hashval_t) hwi;
2647 break;
2649 case CONST_DOUBLE:
2650 if (mode == VOIDmode)
2652 hwi = CONST_DOUBLE_LOW (x) ^ CONST_DOUBLE_HIGH (x);
2653 goto fold_hwi;
2655 else
2656 h ^= real_hash (CONST_DOUBLE_REAL_VALUE (x));
2657 break;
2659 case SYMBOL_REF:
2660 h ^= htab_hash_string (XSTR (x, 0));
2661 break;
2663 case LABEL_REF:
2664 h = h * 251 + CODE_LABEL_NUMBER (XEXP (x, 0));
2665 break;
2667 case UNSPEC:
2668 case UNSPEC_VOLATILE:
2669 h = h * 251 + XINT (x, 1);
2670 break;
2672 default:
2673 break;
2676 hp = data;
2677 *hp = *hp * 509 + h;
2678 return 0;
2681 /* Compute a hash value for X, which should be a constant. */
2683 static hashval_t
2684 const_rtx_hash (rtx x)
2686 hashval_t h = 0;
2687 for_each_rtx (&x, const_rtx_hash_1, &h);
2688 return h;
2692 /* Initialize constant pool hashing for a new function. */
2694 void
2695 init_varasm_status (struct function *f)
2697 struct varasm_status *p;
2698 struct rtx_constant_pool *pool;
2700 p = ggc_alloc (sizeof (struct varasm_status));
2701 f->varasm = p;
2703 pool = ggc_alloc (sizeof (struct rtx_constant_pool));
2704 p->pool = pool;
2705 p->deferred_constants = 0;
2707 pool->const_rtx_htab = htab_create_ggc (31, const_desc_rtx_hash,
2708 const_desc_rtx_eq, NULL);
2709 pool->const_rtx_sym_htab = htab_create_ggc (31, const_desc_rtx_sym_hash,
2710 const_desc_rtx_sym_eq, NULL);
2711 pool->first = pool->last = NULL;
2712 pool->offset = 0;
2715 /* Given a MINUS expression, simplify it if both sides
2716 include the same symbol. */
2719 simplify_subtraction (rtx x)
2721 rtx r = simplify_rtx (x);
2722 return r ? r : x;
2725 /* Given a constant rtx X, make (or find) a memory constant for its value
2726 and return a MEM rtx to refer to it in memory. */
2729 force_const_mem (enum machine_mode mode, rtx x)
2731 struct constant_descriptor_rtx *desc, tmp;
2732 struct rtx_constant_pool *pool = cfun->varasm->pool;
2733 char label[256];
2734 rtx def, symbol;
2735 hashval_t hash;
2736 unsigned int align;
2737 void **slot;
2739 /* If we're not allowed to drop X into the constant pool, don't. */
2740 if ((*targetm.cannot_force_const_mem) (x))
2741 return NULL_RTX;
2743 /* Lookup the value in the hashtable. */
2744 tmp.constant = x;
2745 tmp.mode = mode;
2746 hash = const_rtx_hash (x);
2747 slot = htab_find_slot_with_hash (pool->const_rtx_htab, &tmp, hash, INSERT);
2748 desc = *slot;
2750 /* If the constant was already present, return its memory. */
2751 if (desc)
2752 return copy_rtx (desc->mem);
2754 /* Otherwise, create a new descriptor. */
2755 desc = ggc_alloc (sizeof (*desc));
2756 *slot = desc;
2758 /* Align the location counter as required by EXP's data type. */
2759 align = GET_MODE_ALIGNMENT (mode == VOIDmode ? word_mode : mode);
2760 #ifdef CONSTANT_ALIGNMENT
2762 tree type = (*lang_hooks.types.type_for_mode) (mode, 0);
2763 if (type != NULL_TREE)
2764 align = CONSTANT_ALIGNMENT (make_tree (type, x), align);
2766 #endif
2768 pool->offset += (align / BITS_PER_UNIT) - 1;
2769 pool->offset &= ~ ((align / BITS_PER_UNIT) - 1);
2771 desc->next = NULL;
2772 desc->constant = tmp.constant;
2773 desc->offset = pool->offset;
2774 desc->hash = hash;
2775 desc->mode = mode;
2776 desc->align = align;
2777 desc->labelno = const_labelno;
2778 desc->mark = 0;
2780 pool->offset += GET_MODE_SIZE (mode);
2781 if (pool->last)
2782 pool->last->next = desc;
2783 else
2784 pool->first = pool->last = desc;
2785 pool->last = desc;
2787 /* Create a string containing the label name, in LABEL. */
2788 ASM_GENERATE_INTERNAL_LABEL (label, "LC", const_labelno);
2789 ++const_labelno;
2791 /* Construct the SYMBOL_REF. Make sure to mark it as belonging to
2792 the constants pool. */
2793 desc->sym = symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
2794 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_LOCAL;
2795 CONSTANT_POOL_ADDRESS_P (symbol) = 1;
2796 current_function_uses_const_pool = 1;
2798 /* Insert the descriptor into the symbol cross-reference table too. */
2799 slot = htab_find_slot (pool->const_rtx_sym_htab, desc, INSERT);
2800 if (*slot)
2801 abort ();
2802 *slot = desc;
2804 /* Construct the MEM. */
2805 desc->mem = def = gen_rtx_MEM (mode, symbol);
2806 set_mem_attributes (def, (*lang_hooks.types.type_for_mode) (mode, 0), 1);
2807 RTX_UNCHANGING_P (def) = 1;
2809 /* If we're dropping a label to the constant pool, make sure we
2810 don't delete it. */
2811 if (GET_CODE (x) == LABEL_REF)
2812 LABEL_PRESERVE_P (XEXP (x, 0)) = 1;
2814 return copy_rtx (def);
2817 /* Given a SYMBOL_REF with CONSTANT_POOL_ADDRESS_P true, return a pointer to
2818 the corresponding constant_descriptor_rtx structure. */
2820 static struct constant_descriptor_rtx *
2821 find_pool_constant (struct rtx_constant_pool *pool, rtx sym)
2823 struct constant_descriptor_rtx tmp;
2824 tmp.sym = sym;
2825 return htab_find (pool->const_rtx_sym_htab, &tmp);
2828 /* Given a constant pool SYMBOL_REF, return the corresponding constant. */
2831 get_pool_constant (rtx addr)
2833 return find_pool_constant (cfun->varasm->pool, addr)->constant;
2836 /* Given a constant pool SYMBOL_REF, return the corresponding constant
2837 and whether it has been output or not. */
2840 get_pool_constant_mark (rtx addr, bool *pmarked)
2842 struct constant_descriptor_rtx *desc;
2844 desc = find_pool_constant (cfun->varasm->pool, addr);
2845 *pmarked = (desc->mark != 0);
2846 return desc->constant;
2849 /* Likewise, but for the constant pool of a specific function. */
2852 get_pool_constant_for_function (struct function *f, rtx addr)
2854 return find_pool_constant (f->varasm->pool, addr)->constant;
2857 /* Similar, return the mode. */
2859 enum machine_mode
2860 get_pool_mode (rtx addr)
2862 return find_pool_constant (cfun->varasm->pool, addr)->mode;
2865 enum machine_mode
2866 get_pool_mode_for_function (struct function *f, rtx addr)
2868 return find_pool_constant (f->varasm->pool, addr)->mode;
2871 /* Similar, return the offset in the constant pool. */
2874 get_pool_offset (rtx addr)
2876 return find_pool_constant (cfun->varasm->pool, addr)->offset;
2879 /* Return the size of the constant pool. */
2882 get_pool_size (void)
2884 return cfun->varasm->pool->offset;
2887 /* Worker function for output_constant_pool_1. Emit assembly for X
2888 in MODE with known alignment ALIGN. */
2890 static void
2891 output_constant_pool_2 (enum machine_mode mode, rtx x, unsigned int align)
2893 switch (GET_MODE_CLASS (mode))
2895 case MODE_FLOAT:
2896 if (GET_CODE (x) != CONST_DOUBLE)
2897 abort ();
2898 else
2900 REAL_VALUE_TYPE r;
2901 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
2902 assemble_real (r, mode, align);
2904 break;
2906 case MODE_INT:
2907 case MODE_PARTIAL_INT:
2908 assemble_integer (x, GET_MODE_SIZE (mode), align, 1);
2909 break;
2911 case MODE_VECTOR_FLOAT:
2912 case MODE_VECTOR_INT:
2914 int i, units;
2915 enum machine_mode submode = GET_MODE_INNER (mode);
2916 unsigned int subalign = MIN (align, GET_MODE_BITSIZE (submode));
2918 if (GET_CODE (x) != CONST_VECTOR)
2919 abort ();
2920 units = CONST_VECTOR_NUNITS (x);
2922 for (i = 0; i < units; i++)
2924 rtx elt = CONST_VECTOR_ELT (x, i);
2925 output_constant_pool_2 (submode, elt, i ? subalign : align);
2928 break;
2930 default:
2931 abort ();
2935 /* Worker function for output_constant_pool. Emit POOL. */
2937 static void
2938 output_constant_pool_1 (struct constant_descriptor_rtx *desc)
2940 rtx x, tmp;
2942 if (!desc->mark)
2943 return;
2944 x = desc->constant;
2946 /* See if X is a LABEL_REF (or a CONST referring to a LABEL_REF)
2947 whose CODE_LABEL has been deleted. This can occur if a jump table
2948 is eliminated by optimization. If so, write a constant of zero
2949 instead. Note that this can also happen by turning the
2950 CODE_LABEL into a NOTE. */
2951 /* ??? This seems completely and utterly wrong. Certainly it's
2952 not true for NOTE_INSN_DELETED_LABEL, but I disbelieve proper
2953 functioning even with INSN_DELETED_P and friends. */
2955 tmp = x;
2956 switch (GET_CODE (x))
2958 case CONST:
2959 if (GET_CODE (XEXP (x, 0)) != PLUS
2960 || GET_CODE (XEXP (XEXP (x, 0), 0)) != LABEL_REF)
2961 break;
2962 tmp = XEXP (XEXP (x, 0), 0);
2963 /* FALLTHRU */
2965 case LABEL_REF:
2966 tmp = XEXP (x, 0);
2967 if (INSN_DELETED_P (tmp)
2968 || (GET_CODE (tmp) == NOTE
2969 && NOTE_LINE_NUMBER (tmp) == NOTE_INSN_DELETED))
2971 abort ();
2972 x = const0_rtx;
2974 break;
2976 default:
2977 break;
2980 /* First switch to correct section. */
2981 (*targetm.asm_out.select_rtx_section) (desc->mode, x, desc->align);
2983 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
2984 ASM_OUTPUT_SPECIAL_POOL_ENTRY (asm_out_file, x, desc->mode,
2985 desc->align, desc->labelno, done);
2986 #endif
2988 assemble_align (desc->align);
2990 /* Output the label. */
2991 (*targetm.asm_out.internal_label) (asm_out_file, "LC", desc->labelno);
2993 /* Output the data. */
2994 output_constant_pool_2 (desc->mode, x, desc->align);
2996 /* Make sure all constants in SECTION_MERGE and not SECTION_STRINGS
2997 sections have proper size. */
2998 if (desc->align > GET_MODE_BITSIZE (desc->mode)
2999 && in_section == in_named
3000 && get_named_section_flags (in_named_name) & SECTION_MERGE)
3001 assemble_align (desc->align);
3003 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3004 done:
3005 #endif
3006 return;
3009 /* Given a SYMBOL_REF CURRENT_RTX, mark it and all constants it refers
3010 to as used. Emit referenced deferred strings. This function can
3011 be used with for_each_rtx to mark all SYMBOL_REFs in an rtx. */
3013 static int
3014 mark_constant (rtx *current_rtx, void *data)
3016 struct rtx_constant_pool *pool = data;
3017 rtx x = *current_rtx;
3019 if (x == NULL_RTX || GET_CODE (x) != SYMBOL_REF)
3020 return 0;
3022 if (CONSTANT_POOL_ADDRESS_P (x))
3024 struct constant_descriptor_rtx *desc = find_pool_constant (pool, x);
3025 if (desc->mark == 0)
3027 desc->mark = 1;
3028 for_each_rtx (&desc->constant, mark_constant, pool);
3031 else if (TREE_CONSTANT_POOL_ADDRESS_P (x))
3033 tree exp = SYMBOL_REF_DECL (x);
3034 if (!TREE_ASM_WRITTEN (exp))
3036 n_deferred_constants--;
3037 output_constant_def_contents (x);
3041 return -1;
3044 /* Look through appropriate parts of INSN, marking all entries in the
3045 constant pool which are actually being used. Entries that are only
3046 referenced by other constants are also marked as used. Emit
3047 deferred strings that are used. */
3049 static void
3050 mark_constants (struct rtx_constant_pool *pool, rtx insn)
3052 if (!INSN_P (insn))
3053 return;
3055 /* Insns may appear inside a SEQUENCE. Only check the patterns of
3056 insns, not any notes that may be attached. We don't want to mark
3057 a constant just because it happens to appear in a REG_EQUIV note. */
3058 if (GET_CODE (PATTERN (insn)) == SEQUENCE)
3060 rtx seq = PATTERN (insn);
3061 int i, n = XVECLEN (seq, 0);
3062 for (i = 0; i < n; ++i)
3064 rtx subinsn = XVECEXP (seq, 0, i);
3065 if (INSN_P (subinsn))
3066 for_each_rtx (&PATTERN (subinsn), mark_constant, pool);
3069 else
3070 for_each_rtx (&PATTERN (insn), mark_constant, pool);
3073 /* Look through the instructions for this function, and mark all the
3074 entries in POOL which are actually being used. Emit deferred constants
3075 which have indeed been used. */
3077 static void
3078 mark_constant_pool (struct rtx_constant_pool *pool)
3080 rtx insn, link;
3082 if (pool->first == 0 && n_deferred_constants == 0)
3083 return;
3085 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3086 mark_constants (pool, insn);
3088 for (link = current_function_epilogue_delay_list;
3089 link;
3090 link = XEXP (link, 1))
3091 mark_constants (pool, XEXP (link, 0));
3094 /* Write all the constants in the constant pool. */
3096 void
3097 output_constant_pool (const char *fnname ATTRIBUTE_UNUSED,
3098 tree fndecl ATTRIBUTE_UNUSED)
3100 struct rtx_constant_pool *pool = cfun->varasm->pool;
3101 struct constant_descriptor_rtx *desc;
3103 /* It is possible for gcc to call force_const_mem and then to later
3104 discard the instructions which refer to the constant. In such a
3105 case we do not need to output the constant. */
3106 mark_constant_pool (pool);
3108 #ifdef ASM_OUTPUT_POOL_PROLOGUE
3109 ASM_OUTPUT_POOL_PROLOGUE (asm_out_file, fnname, fndecl, pool->offset);
3110 #endif
3112 for (desc = pool->first; desc ; desc = desc->next)
3113 output_constant_pool_1 (desc);
3115 #ifdef ASM_OUTPUT_POOL_EPILOGUE
3116 ASM_OUTPUT_POOL_EPILOGUE (asm_out_file, fnname, fndecl, pool->offset);
3117 #endif
3120 /* Determine what kind of relocations EXP may need. */
3123 compute_reloc_for_constant (tree exp)
3125 int reloc = 0, reloc2;
3126 tree tem;
3128 /* Give the front-end a chance to convert VALUE to something that
3129 looks more like a constant to the back-end. */
3130 exp = (*lang_hooks.expand_constant) (exp);
3132 switch (TREE_CODE (exp))
3134 case ADDR_EXPR:
3135 case FDESC_EXPR:
3136 /* Go inside any operations that get_inner_reference can handle and see
3137 if what's inside is a constant: no need to do anything here for
3138 addresses of variables or functions. */
3139 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
3140 tem = TREE_OPERAND (tem, 0))
3143 if (TREE_PUBLIC (tem))
3144 reloc |= 2;
3145 else
3146 reloc |= 1;
3147 break;
3149 case PLUS_EXPR:
3150 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3151 reloc |= compute_reloc_for_constant (TREE_OPERAND (exp, 1));
3152 break;
3154 case MINUS_EXPR:
3155 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3156 reloc2 = compute_reloc_for_constant (TREE_OPERAND (exp, 1));
3157 /* The difference of two local labels is computable at link time. */
3158 if (reloc == 1 && reloc2 == 1)
3159 reloc = 0;
3160 else
3161 reloc |= reloc2;
3162 break;
3164 case NOP_EXPR:
3165 case CONVERT_EXPR:
3166 case NON_LVALUE_EXPR:
3167 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3168 break;
3170 case CONSTRUCTOR:
3171 for (tem = CONSTRUCTOR_ELTS (exp); tem; tem = TREE_CHAIN (tem))
3172 if (TREE_VALUE (tem) != 0)
3173 reloc |= compute_reloc_for_constant (TREE_VALUE (tem));
3175 break;
3177 default:
3178 break;
3180 return reloc;
3183 /* Find all the constants whose addresses are referenced inside of EXP,
3184 and make sure assembler code with a label has been output for each one.
3185 Indicate whether an ADDR_EXPR has been encountered. */
3187 static void
3188 output_addressed_constants (tree exp)
3190 tree tem;
3192 /* Give the front-end a chance to convert VALUE to something that
3193 looks more like a constant to the back-end. */
3194 exp = (*lang_hooks.expand_constant) (exp);
3196 switch (TREE_CODE (exp))
3198 case ADDR_EXPR:
3199 case FDESC_EXPR:
3200 /* Go inside any operations that get_inner_reference can handle and see
3201 if what's inside is a constant: no need to do anything here for
3202 addresses of variables or functions. */
3203 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
3204 tem = TREE_OPERAND (tem, 0))
3207 if (TREE_CODE_CLASS (TREE_CODE (tem)) == 'c'
3208 || TREE_CODE (tem) == CONSTRUCTOR)
3209 output_constant_def (tem, 0);
3210 break;
3212 case PLUS_EXPR:
3213 case MINUS_EXPR:
3214 output_addressed_constants (TREE_OPERAND (exp, 1));
3215 /* Fall through. */
3217 case NOP_EXPR:
3218 case CONVERT_EXPR:
3219 case NON_LVALUE_EXPR:
3220 output_addressed_constants (TREE_OPERAND (exp, 0));
3221 break;
3223 case CONSTRUCTOR:
3224 for (tem = CONSTRUCTOR_ELTS (exp); tem; tem = TREE_CHAIN (tem))
3225 if (TREE_VALUE (tem) != 0)
3226 output_addressed_constants (TREE_VALUE (tem));
3228 break;
3230 default:
3231 break;
3235 /* Return nonzero if VALUE is a valid constant-valued expression
3236 for use in initializing a static variable; one that can be an
3237 element of a "constant" initializer.
3239 Return null_pointer_node if the value is absolute;
3240 if it is relocatable, return the variable that determines the relocation.
3241 We assume that VALUE has been folded as much as possible;
3242 therefore, we do not need to check for such things as
3243 arithmetic-combinations of integers. */
3245 tree
3246 initializer_constant_valid_p (tree value, tree endtype)
3248 /* Give the front-end a chance to convert VALUE to something that
3249 looks more like a constant to the back-end. */
3250 value = (*lang_hooks.expand_constant) (value);
3252 switch (TREE_CODE (value))
3254 case CONSTRUCTOR:
3255 if ((TREE_CODE (TREE_TYPE (value)) == UNION_TYPE
3256 || TREE_CODE (TREE_TYPE (value)) == RECORD_TYPE)
3257 && TREE_CONSTANT (value)
3258 && CONSTRUCTOR_ELTS (value))
3260 tree elt;
3261 bool absolute = true;
3263 for (elt = CONSTRUCTOR_ELTS (value); elt; elt = TREE_CHAIN (elt))
3265 tree reloc;
3266 value = TREE_VALUE (elt);
3267 reloc = initializer_constant_valid_p (value, TREE_TYPE (value));
3268 if (!reloc)
3269 return NULL_TREE;
3270 if (reloc != null_pointer_node)
3271 absolute = false;
3273 /* For a non-absolute relocation, there is no single
3274 variable that can be "the variable that determines the
3275 relocation." */
3276 return absolute ? null_pointer_node : error_mark_node;
3279 return TREE_STATIC (value) ? null_pointer_node : NULL_TREE;
3281 case INTEGER_CST:
3282 case VECTOR_CST:
3283 case REAL_CST:
3284 case STRING_CST:
3285 case COMPLEX_CST:
3286 return null_pointer_node;
3288 case ADDR_EXPR:
3289 case FDESC_EXPR:
3290 return staticp (TREE_OPERAND (value, 0)) ? TREE_OPERAND (value, 0) : 0;
3292 case VIEW_CONVERT_EXPR:
3293 case NON_LVALUE_EXPR:
3294 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
3296 case CONVERT_EXPR:
3297 case NOP_EXPR:
3298 /* Allow conversions between pointer types. */
3299 if (POINTER_TYPE_P (TREE_TYPE (value))
3300 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (value, 0))))
3301 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
3303 /* Allow conversions between real types. */
3304 if (FLOAT_TYPE_P (TREE_TYPE (value))
3305 && FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (value, 0))))
3306 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
3308 /* Allow length-preserving conversions between integer types. */
3309 if (INTEGRAL_TYPE_P (TREE_TYPE (value))
3310 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (value, 0)))
3311 && (TYPE_PRECISION (TREE_TYPE (value))
3312 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0)))))
3313 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
3315 /* Allow conversions between other integer types only if
3316 explicit value. */
3317 if (INTEGRAL_TYPE_P (TREE_TYPE (value))
3318 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (value, 0))))
3320 tree inner = initializer_constant_valid_p (TREE_OPERAND (value, 0),
3321 endtype);
3322 if (inner == null_pointer_node)
3323 return null_pointer_node;
3324 break;
3327 /* Allow (int) &foo provided int is as wide as a pointer. */
3328 if (INTEGRAL_TYPE_P (TREE_TYPE (value))
3329 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (value, 0)))
3330 && (TYPE_PRECISION (TREE_TYPE (value))
3331 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0)))))
3332 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
3333 endtype);
3335 /* Likewise conversions from int to pointers, but also allow
3336 conversions from 0. */
3337 if ((POINTER_TYPE_P (TREE_TYPE (value))
3338 || TREE_CODE (TREE_TYPE (value)) == OFFSET_TYPE)
3339 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (value, 0))))
3341 if (integer_zerop (TREE_OPERAND (value, 0)))
3342 return null_pointer_node;
3343 else if (TYPE_PRECISION (TREE_TYPE (value))
3344 <= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0))))
3345 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
3346 endtype);
3349 /* Allow conversions to union types if the value inside is okay. */
3350 if (TREE_CODE (TREE_TYPE (value)) == UNION_TYPE)
3351 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
3352 endtype);
3353 break;
3355 case PLUS_EXPR:
3356 if (! INTEGRAL_TYPE_P (endtype)
3357 || TYPE_PRECISION (endtype) >= POINTER_SIZE)
3359 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
3360 endtype);
3361 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
3362 endtype);
3363 /* If either term is absolute, use the other terms relocation. */
3364 if (valid0 == null_pointer_node)
3365 return valid1;
3366 if (valid1 == null_pointer_node)
3367 return valid0;
3369 break;
3371 case MINUS_EXPR:
3372 if (! INTEGRAL_TYPE_P (endtype)
3373 || TYPE_PRECISION (endtype) >= POINTER_SIZE)
3375 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
3376 endtype);
3377 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
3378 endtype);
3379 /* Win if second argument is absolute. */
3380 if (valid1 == null_pointer_node)
3381 return valid0;
3382 /* Win if both arguments have the same relocation.
3383 Then the value is absolute. */
3384 if (valid0 == valid1 && valid0 != 0)
3385 return null_pointer_node;
3387 /* Since GCC guarantees that string constants are unique in the
3388 generated code, a subtraction between two copies of the same
3389 constant string is absolute. */
3390 if (valid0 && TREE_CODE (valid0) == STRING_CST &&
3391 valid1 && TREE_CODE (valid1) == STRING_CST &&
3392 TREE_STRING_POINTER (valid0) == TREE_STRING_POINTER (valid1))
3393 return null_pointer_node;
3396 /* Support differences between labels. */
3397 if (INTEGRAL_TYPE_P (endtype))
3399 tree op0, op1;
3400 op0 = TREE_OPERAND (value, 0);
3401 op1 = TREE_OPERAND (value, 1);
3403 /* Like STRIP_NOPS except allow the operand mode to widen.
3404 This works around a feature of fold that simplifies
3405 (int)(p1 - p2) to ((int)p1 - (int)p2) under the theory
3406 that the narrower operation is cheaper. */
3408 while (TREE_CODE (op0) == NOP_EXPR
3409 || TREE_CODE (op0) == CONVERT_EXPR
3410 || TREE_CODE (op0) == NON_LVALUE_EXPR)
3412 tree inner = TREE_OPERAND (op0, 0);
3413 if (inner == error_mark_node
3414 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
3415 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))
3416 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
3417 break;
3418 op0 = inner;
3421 while (TREE_CODE (op1) == NOP_EXPR
3422 || TREE_CODE (op1) == CONVERT_EXPR
3423 || TREE_CODE (op1) == NON_LVALUE_EXPR)
3425 tree inner = TREE_OPERAND (op1, 0);
3426 if (inner == error_mark_node
3427 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
3428 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op1)))
3429 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
3430 break;
3431 op1 = inner;
3434 if (TREE_CODE (op0) == ADDR_EXPR
3435 && TREE_CODE (TREE_OPERAND (op0, 0)) == LABEL_DECL
3436 && TREE_CODE (op1) == ADDR_EXPR
3437 && TREE_CODE (TREE_OPERAND (op1, 0)) == LABEL_DECL)
3438 return null_pointer_node;
3440 break;
3442 default:
3443 break;
3446 return 0;
3449 /* Output assembler code for constant EXP to FILE, with no label.
3450 This includes the pseudo-op such as ".int" or ".byte", and a newline.
3451 Assumes output_addressed_constants has been done on EXP already.
3453 Generate exactly SIZE bytes of assembler data, padding at the end
3454 with zeros if necessary. SIZE must always be specified.
3456 SIZE is important for structure constructors,
3457 since trailing members may have been omitted from the constructor.
3458 It is also important for initialization of arrays from string constants
3459 since the full length of the string constant might not be wanted.
3460 It is also needed for initialization of unions, where the initializer's
3461 type is just one member, and that may not be as long as the union.
3463 There a case in which we would fail to output exactly SIZE bytes:
3464 for a structure constructor that wants to produce more than SIZE bytes.
3465 But such constructors will never be generated for any possible input.
3467 ALIGN is the alignment of the data in bits. */
3469 void
3470 output_constant (tree exp, unsigned HOST_WIDE_INT size, unsigned int align)
3472 enum tree_code code;
3473 unsigned HOST_WIDE_INT thissize;
3475 /* Some front-ends use constants other than the standard language-independent
3476 varieties, but which may still be output directly. Give the front-end a
3477 chance to convert EXP to a language-independent representation. */
3478 exp = (*lang_hooks.expand_constant) (exp);
3480 if (size == 0 || flag_syntax_only)
3481 return;
3483 /* Eliminate any conversions since we'll be outputting the underlying
3484 constant. */
3485 while (TREE_CODE (exp) == NOP_EXPR || TREE_CODE (exp) == CONVERT_EXPR
3486 || TREE_CODE (exp) == NON_LVALUE_EXPR
3487 || TREE_CODE (exp) == VIEW_CONVERT_EXPR)
3488 exp = TREE_OPERAND (exp, 0);
3490 code = TREE_CODE (TREE_TYPE (exp));
3491 thissize = int_size_in_bytes (TREE_TYPE (exp));
3493 /* Allow a constructor with no elements for any data type.
3494 This means to fill the space with zeros. */
3495 if (TREE_CODE (exp) == CONSTRUCTOR && CONSTRUCTOR_ELTS (exp) == 0)
3497 assemble_zeros (size);
3498 return;
3501 if (TREE_CODE (exp) == FDESC_EXPR)
3503 #ifdef ASM_OUTPUT_FDESC
3504 HOST_WIDE_INT part = tree_low_cst (TREE_OPERAND (exp, 1), 0);
3505 tree decl = TREE_OPERAND (exp, 0);
3506 ASM_OUTPUT_FDESC (asm_out_file, decl, part);
3507 #else
3508 abort ();
3509 #endif
3510 return;
3513 /* Now output the underlying data. If we've handling the padding, return.
3514 Otherwise, break and ensure THISSIZE is the size written. */
3515 switch (code)
3517 case CHAR_TYPE:
3518 case BOOLEAN_TYPE:
3519 case INTEGER_TYPE:
3520 case ENUMERAL_TYPE:
3521 case POINTER_TYPE:
3522 case REFERENCE_TYPE:
3523 case OFFSET_TYPE:
3524 if (! assemble_integer (expand_expr (exp, NULL_RTX, VOIDmode,
3525 EXPAND_INITIALIZER),
3526 size, align, 0))
3527 error ("initializer for integer value is too complicated");
3528 break;
3530 case REAL_TYPE:
3531 if (TREE_CODE (exp) != REAL_CST)
3532 error ("initializer for floating value is not a floating constant");
3534 assemble_real (TREE_REAL_CST (exp), TYPE_MODE (TREE_TYPE (exp)), align);
3535 break;
3537 case COMPLEX_TYPE:
3538 output_constant (TREE_REALPART (exp), thissize / 2, align);
3539 output_constant (TREE_IMAGPART (exp), thissize / 2,
3540 min_align (align, BITS_PER_UNIT * (thissize / 2)));
3541 break;
3543 case ARRAY_TYPE:
3544 case VECTOR_TYPE:
3545 if (TREE_CODE (exp) == CONSTRUCTOR)
3547 output_constructor (exp, size, align);
3548 return;
3550 else if (TREE_CODE (exp) == STRING_CST)
3552 thissize = MIN ((unsigned HOST_WIDE_INT)TREE_STRING_LENGTH (exp),
3553 size);
3554 assemble_string (TREE_STRING_POINTER (exp), thissize);
3556 else if (TREE_CODE (exp) == VECTOR_CST)
3558 int elt_size;
3559 tree link;
3560 unsigned int nalign;
3561 enum machine_mode inner;
3563 inner = GET_MODE_INNER (TYPE_MODE (TREE_TYPE (exp)));
3564 nalign = MIN (align, GET_MODE_ALIGNMENT (inner));
3566 elt_size = GET_MODE_UNIT_SIZE (TYPE_MODE (TREE_TYPE (exp)));
3568 link = TREE_VECTOR_CST_ELTS (exp);
3569 output_constant (TREE_VALUE (link), elt_size, align);
3570 while ((link = TREE_CHAIN (link)) != NULL)
3571 output_constant (TREE_VALUE (link), elt_size, nalign);
3573 else
3574 abort ();
3575 break;
3577 case RECORD_TYPE:
3578 case UNION_TYPE:
3579 if (TREE_CODE (exp) == CONSTRUCTOR)
3580 output_constructor (exp, size, align);
3581 else
3582 abort ();
3583 return;
3585 case SET_TYPE:
3586 if (TREE_CODE (exp) == INTEGER_CST)
3587 assemble_integer (expand_expr (exp, NULL_RTX,
3588 VOIDmode, EXPAND_INITIALIZER),
3589 thissize, align, 1);
3590 else if (TREE_CODE (exp) == CONSTRUCTOR)
3592 unsigned char *buffer = alloca (thissize);
3593 if (get_set_constructor_bytes (exp, buffer, thissize))
3594 abort ();
3595 assemble_string ((char *) buffer, thissize);
3597 else
3598 error ("unknown set constructor type");
3599 return;
3601 case ERROR_MARK:
3602 return;
3604 default:
3605 abort ();
3608 if (size > thissize)
3609 assemble_zeros (size - thissize);
3613 /* Subroutine of output_constructor, used for computing the size of
3614 arrays of unspecified length. VAL must be a CONSTRUCTOR of an array
3615 type with an unspecified upper bound. */
3617 static unsigned HOST_WIDE_INT
3618 array_size_for_constructor (tree val)
3620 tree max_index, i;
3622 /* This code used to attempt to handle string constants that are not
3623 arrays of single-bytes, but nothing else does, so there's no point in
3624 doing it here. */
3625 if (TREE_CODE (val) == STRING_CST)
3626 return TREE_STRING_LENGTH (val);
3628 max_index = NULL_TREE;
3629 for (i = CONSTRUCTOR_ELTS (val); i; i = TREE_CHAIN (i))
3631 tree index = TREE_PURPOSE (i);
3633 if (TREE_CODE (index) == RANGE_EXPR)
3634 index = TREE_OPERAND (index, 1);
3635 if (max_index == NULL_TREE || tree_int_cst_lt (max_index, index))
3636 max_index = index;
3639 if (max_index == NULL_TREE)
3640 return 0;
3642 /* Compute the total number of array elements. */
3643 i = size_binop (MINUS_EXPR, convert (sizetype, max_index),
3644 convert (sizetype,
3645 TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (val)))));
3646 i = size_binop (PLUS_EXPR, i, convert (sizetype, integer_one_node));
3648 /* Multiply by the array element unit size to find number of bytes. */
3649 i = size_binop (MULT_EXPR, i, TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (val))));
3651 return tree_low_cst (i, 1);
3654 /* Subroutine of output_constant, used for CONSTRUCTORs (aggregate constants).
3655 Generate at least SIZE bytes, padding if necessary. */
3657 static void
3658 output_constructor (tree exp, unsigned HOST_WIDE_INT size,
3659 unsigned int align)
3661 tree type = TREE_TYPE (exp);
3662 tree link, field = 0;
3663 tree min_index = 0;
3664 /* Number of bytes output or skipped so far.
3665 In other words, current position within the constructor. */
3666 HOST_WIDE_INT total_bytes = 0;
3667 /* Nonzero means BYTE contains part of a byte, to be output. */
3668 int byte_buffer_in_use = 0;
3669 int byte = 0;
3671 if (HOST_BITS_PER_WIDE_INT < BITS_PER_UNIT)
3672 abort ();
3674 if (TREE_CODE (type) == RECORD_TYPE)
3675 field = TYPE_FIELDS (type);
3677 if (TREE_CODE (type) == ARRAY_TYPE
3678 && TYPE_DOMAIN (type) != 0)
3679 min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
3681 /* As LINK goes through the elements of the constant,
3682 FIELD goes through the structure fields, if the constant is a structure.
3683 if the constant is a union, then we override this,
3684 by getting the field from the TREE_LIST element.
3685 But the constant could also be an array. Then FIELD is zero.
3687 There is always a maximum of one element in the chain LINK for unions
3688 (even if the initializer in a source program incorrectly contains
3689 more one). */
3690 for (link = CONSTRUCTOR_ELTS (exp);
3691 link;
3692 link = TREE_CHAIN (link),
3693 field = field ? TREE_CHAIN (field) : 0)
3695 tree val = TREE_VALUE (link);
3696 tree index = 0;
3698 /* The element in a union constructor specifies the proper field
3699 or index. */
3700 if ((TREE_CODE (type) == RECORD_TYPE || TREE_CODE (type) == UNION_TYPE
3701 || TREE_CODE (type) == QUAL_UNION_TYPE)
3702 && TREE_PURPOSE (link) != 0)
3703 field = TREE_PURPOSE (link);
3705 else if (TREE_CODE (type) == ARRAY_TYPE)
3706 index = TREE_PURPOSE (link);
3708 #ifdef ASM_COMMENT_START
3709 if (field && flag_verbose_asm)
3710 fprintf (asm_out_file, "%s %s:\n",
3711 ASM_COMMENT_START,
3712 DECL_NAME (field)
3713 ? IDENTIFIER_POINTER (DECL_NAME (field))
3714 : "<anonymous>");
3715 #endif
3717 /* Eliminate the marker that makes a cast not be an lvalue. */
3718 if (val != 0)
3719 STRIP_NOPS (val);
3721 if (index && TREE_CODE (index) == RANGE_EXPR)
3723 unsigned HOST_WIDE_INT fieldsize
3724 = int_size_in_bytes (TREE_TYPE (type));
3725 HOST_WIDE_INT lo_index = tree_low_cst (TREE_OPERAND (index, 0), 0);
3726 HOST_WIDE_INT hi_index = tree_low_cst (TREE_OPERAND (index, 1), 0);
3727 HOST_WIDE_INT index;
3728 unsigned int align2 = min_align (align, fieldsize * BITS_PER_UNIT);
3730 for (index = lo_index; index <= hi_index; index++)
3732 /* Output the element's initial value. */
3733 if (val == 0)
3734 assemble_zeros (fieldsize);
3735 else
3736 output_constant (val, fieldsize, align2);
3738 /* Count its size. */
3739 total_bytes += fieldsize;
3742 else if (field == 0 || !DECL_BIT_FIELD (field))
3744 /* An element that is not a bit-field. */
3746 unsigned HOST_WIDE_INT fieldsize;
3747 /* Since this structure is static,
3748 we know the positions are constant. */
3749 HOST_WIDE_INT pos = field ? int_byte_position (field) : 0;
3750 unsigned int align2;
3752 if (index != 0)
3753 pos = (tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (val)), 1)
3754 * (tree_low_cst (index, 0) - tree_low_cst (min_index, 0)));
3756 /* Output any buffered-up bit-fields preceding this element. */
3757 if (byte_buffer_in_use)
3759 assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
3760 total_bytes++;
3761 byte_buffer_in_use = 0;
3764 /* Advance to offset of this element.
3765 Note no alignment needed in an array, since that is guaranteed
3766 if each element has the proper size. */
3767 if ((field != 0 || index != 0) && pos != total_bytes)
3769 assemble_zeros (pos - total_bytes);
3770 total_bytes = pos;
3773 /* Find the alignment of this element. */
3774 align2 = min_align (align, BITS_PER_UNIT * pos);
3776 /* Determine size this element should occupy. */
3777 if (field)
3779 fieldsize = 0;
3781 /* If this is an array with an unspecified upper bound,
3782 the initializer determines the size. */
3783 /* ??? This ought to only checked if DECL_SIZE_UNIT is NULL,
3784 but we cannot do this until the deprecated support for
3785 initializing zero-length array members is removed. */
3786 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
3787 && TYPE_DOMAIN (TREE_TYPE (field))
3788 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
3790 fieldsize = array_size_for_constructor (val);
3791 /* Given a non-empty initialization, this field had
3792 better be last. */
3793 if (fieldsize != 0 && TREE_CHAIN (field) != NULL_TREE)
3794 abort ();
3796 else if (DECL_SIZE_UNIT (field))
3798 /* ??? This can't be right. If the decl size overflows
3799 a host integer we will silently emit no data. */
3800 if (host_integerp (DECL_SIZE_UNIT (field), 1))
3801 fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 1);
3804 else
3805 fieldsize = int_size_in_bytes (TREE_TYPE (type));
3807 /* Output the element's initial value. */
3808 if (val == 0)
3809 assemble_zeros (fieldsize);
3810 else
3811 output_constant (val, fieldsize, align2);
3813 /* Count its size. */
3814 total_bytes += fieldsize;
3816 else if (val != 0 && TREE_CODE (val) != INTEGER_CST)
3817 error ("invalid initial value for member `%s'",
3818 IDENTIFIER_POINTER (DECL_NAME (field)));
3819 else
3821 /* Element that is a bit-field. */
3823 HOST_WIDE_INT next_offset = int_bit_position (field);
3824 HOST_WIDE_INT end_offset
3825 = (next_offset + tree_low_cst (DECL_SIZE (field), 1));
3827 if (val == 0)
3828 val = integer_zero_node;
3830 /* If this field does not start in this (or, next) byte,
3831 skip some bytes. */
3832 if (next_offset / BITS_PER_UNIT != total_bytes)
3834 /* Output remnant of any bit field in previous bytes. */
3835 if (byte_buffer_in_use)
3837 assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
3838 total_bytes++;
3839 byte_buffer_in_use = 0;
3842 /* If still not at proper byte, advance to there. */
3843 if (next_offset / BITS_PER_UNIT != total_bytes)
3845 assemble_zeros (next_offset / BITS_PER_UNIT - total_bytes);
3846 total_bytes = next_offset / BITS_PER_UNIT;
3850 if (! byte_buffer_in_use)
3851 byte = 0;
3853 /* We must split the element into pieces that fall within
3854 separate bytes, and combine each byte with previous or
3855 following bit-fields. */
3857 /* next_offset is the offset n fbits from the beginning of
3858 the structure to the next bit of this element to be processed.
3859 end_offset is the offset of the first bit past the end of
3860 this element. */
3861 while (next_offset < end_offset)
3863 int this_time;
3864 int shift;
3865 HOST_WIDE_INT value;
3866 HOST_WIDE_INT next_byte = next_offset / BITS_PER_UNIT;
3867 HOST_WIDE_INT next_bit = next_offset % BITS_PER_UNIT;
3869 /* Advance from byte to byte
3870 within this element when necessary. */
3871 while (next_byte != total_bytes)
3873 assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
3874 total_bytes++;
3875 byte = 0;
3878 /* Number of bits we can process at once
3879 (all part of the same byte). */
3880 this_time = MIN (end_offset - next_offset,
3881 BITS_PER_UNIT - next_bit);
3882 if (BYTES_BIG_ENDIAN)
3884 /* On big-endian machine, take the most significant bits
3885 first (of the bits that are significant)
3886 and put them into bytes from the most significant end. */
3887 shift = end_offset - next_offset - this_time;
3889 /* Don't try to take a bunch of bits that cross
3890 the word boundary in the INTEGER_CST. We can
3891 only select bits from the LOW or HIGH part
3892 not from both. */
3893 if (shift < HOST_BITS_PER_WIDE_INT
3894 && shift + this_time > HOST_BITS_PER_WIDE_INT)
3896 this_time = shift + this_time - HOST_BITS_PER_WIDE_INT;
3897 shift = HOST_BITS_PER_WIDE_INT;
3900 /* Now get the bits from the appropriate constant word. */
3901 if (shift < HOST_BITS_PER_WIDE_INT)
3902 value = TREE_INT_CST_LOW (val);
3903 else if (shift < 2 * HOST_BITS_PER_WIDE_INT)
3905 value = TREE_INT_CST_HIGH (val);
3906 shift -= HOST_BITS_PER_WIDE_INT;
3908 else
3909 abort ();
3911 /* Get the result. This works only when:
3912 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
3913 byte |= (((value >> shift)
3914 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
3915 << (BITS_PER_UNIT - this_time - next_bit));
3917 else
3919 /* On little-endian machines,
3920 take first the least significant bits of the value
3921 and pack them starting at the least significant
3922 bits of the bytes. */
3923 shift = next_offset - int_bit_position (field);
3925 /* Don't try to take a bunch of bits that cross
3926 the word boundary in the INTEGER_CST. We can
3927 only select bits from the LOW or HIGH part
3928 not from both. */
3929 if (shift < HOST_BITS_PER_WIDE_INT
3930 && shift + this_time > HOST_BITS_PER_WIDE_INT)
3931 this_time = (HOST_BITS_PER_WIDE_INT - shift);
3933 /* Now get the bits from the appropriate constant word. */
3934 if (shift < HOST_BITS_PER_WIDE_INT)
3935 value = TREE_INT_CST_LOW (val);
3936 else if (shift < 2 * HOST_BITS_PER_WIDE_INT)
3938 value = TREE_INT_CST_HIGH (val);
3939 shift -= HOST_BITS_PER_WIDE_INT;
3941 else
3942 abort ();
3944 /* Get the result. This works only when:
3945 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
3946 byte |= (((value >> shift)
3947 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
3948 << next_bit);
3951 next_offset += this_time;
3952 byte_buffer_in_use = 1;
3957 if (byte_buffer_in_use)
3959 assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
3960 total_bytes++;
3963 if ((unsigned HOST_WIDE_INT)total_bytes < size)
3964 assemble_zeros (size - total_bytes);
3967 /* This TREE_LIST contains any weak symbol declarations waiting
3968 to be emitted. */
3969 static GTY(()) tree weak_decls;
3971 /* Mark DECL as weak. */
3973 static void
3974 mark_weak (tree decl)
3976 DECL_WEAK (decl) = 1;
3978 if (DECL_RTL_SET_P (decl)
3979 && GET_CODE (DECL_RTL (decl)) == MEM
3980 && XEXP (DECL_RTL (decl), 0)
3981 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == SYMBOL_REF)
3982 SYMBOL_REF_WEAK (XEXP (DECL_RTL (decl), 0)) = 1;
3985 /* Merge weak status between NEWDECL and OLDDECL. */
3987 void
3988 merge_weak (tree newdecl, tree olddecl)
3990 if (DECL_WEAK (newdecl) == DECL_WEAK (olddecl))
3991 return;
3993 if (DECL_WEAK (newdecl))
3995 tree wd;
3997 /* NEWDECL is weak, but OLDDECL is not. */
3999 /* If we already output the OLDDECL, we're in trouble; we can't
4000 go back and make it weak. This error cannot caught in
4001 declare_weak because the NEWDECL and OLDDECL was not yet
4002 been merged; therefore, TREE_ASM_WRITTEN was not set. */
4003 if (TREE_ASM_WRITTEN (olddecl))
4004 error ("%Jweak declaration of '%D' must precede definition",
4005 newdecl, newdecl);
4007 /* If we've already generated rtl referencing OLDDECL, we may
4008 have done so in a way that will not function properly with
4009 a weak symbol. */
4010 else if (TREE_USED (olddecl)
4011 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (olddecl)))
4012 warning ("%Jweak declaration of '%D' after first use results "
4013 "in unspecified behavior", newdecl, newdecl);
4015 if (SUPPORTS_WEAK)
4017 /* We put the NEWDECL on the weak_decls list at some point.
4018 Replace it with the OLDDECL. */
4019 for (wd = weak_decls; wd; wd = TREE_CHAIN (wd))
4020 if (TREE_VALUE (wd) == newdecl)
4022 TREE_VALUE (wd) = olddecl;
4023 break;
4025 /* We may not find the entry on the list. If NEWDECL is a
4026 weak alias, then we will have already called
4027 globalize_decl to remove the entry; in that case, we do
4028 not need to do anything. */
4031 /* Make the OLDDECL weak; it's OLDDECL that we'll be keeping. */
4032 mark_weak (olddecl);
4034 else
4035 /* OLDDECL was weak, but NEWDECL was not explicitly marked as
4036 weak. Just update NEWDECL to indicate that it's weak too. */
4037 mark_weak (newdecl);
4040 /* Declare DECL to be a weak symbol. */
4042 void
4043 declare_weak (tree decl)
4045 if (! TREE_PUBLIC (decl))
4046 error ("%Jweak declaration of '%D' must be public", decl, decl);
4047 else if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
4048 error ("%Jweak declaration of '%D' must precede definition", decl, decl);
4049 else if (SUPPORTS_WEAK)
4051 if (! DECL_WEAK (decl))
4052 weak_decls = tree_cons (NULL, decl, weak_decls);
4054 else
4055 warning ("%Jweak declaration of '%D' not supported", decl, decl);
4057 mark_weak (decl);
4060 /* Emit any pending weak declarations. */
4062 void
4063 weak_finish (void)
4065 tree t;
4067 for (t = weak_decls; t; t = TREE_CHAIN (t))
4069 tree decl = TREE_VALUE (t);
4070 #if defined (ASM_WEAKEN_DECL) || defined (ASM_WEAKEN_LABEL)
4071 const char *const name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
4072 #endif
4074 if (! TREE_USED (decl))
4075 continue;
4077 #ifdef ASM_WEAKEN_DECL
4078 ASM_WEAKEN_DECL (asm_out_file, decl, name, NULL);
4079 #else
4080 #ifdef ASM_WEAKEN_LABEL
4081 ASM_WEAKEN_LABEL (asm_out_file, name);
4082 #else
4083 #ifdef ASM_OUTPUT_WEAK_ALIAS
4084 warning ("only weak aliases are supported in this configuration");
4085 return;
4086 #endif
4087 #endif
4088 #endif
4092 /* Emit the assembly bits to indicate that DECL is globally visible. */
4094 static void
4095 globalize_decl (tree decl)
4097 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
4099 #if defined (ASM_WEAKEN_LABEL) || defined (ASM_WEAKEN_DECL)
4100 if (DECL_WEAK (decl))
4102 tree *p, t;
4104 #ifdef ASM_WEAKEN_DECL
4105 ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
4106 #else
4107 ASM_WEAKEN_LABEL (asm_out_file, name);
4108 #endif
4110 /* Remove this function from the pending weak list so that
4111 we do not emit multiple .weak directives for it. */
4112 for (p = &weak_decls; (t = *p) ; )
4114 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
4115 *p = TREE_CHAIN (t);
4116 else
4117 p = &TREE_CHAIN (t);
4119 return;
4121 #endif
4123 (*targetm.asm_out.globalize_label) (asm_out_file, name);
4126 /* Emit an assembler directive to make the symbol for DECL an alias to
4127 the symbol for TARGET. */
4129 void
4130 assemble_alias (tree decl, tree target ATTRIBUTE_UNUSED)
4132 const char *name;
4134 /* We must force creation of DECL_RTL for debug info generation, even though
4135 we don't use it here. */
4136 make_decl_rtl (decl, NULL);
4138 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
4140 #ifdef ASM_OUTPUT_DEF
4141 /* Make name accessible from other files, if appropriate. */
4143 if (TREE_PUBLIC (decl))
4145 globalize_decl (decl);
4146 maybe_assemble_visibility (decl);
4149 #ifdef ASM_OUTPUT_DEF_FROM_DECLS
4150 ASM_OUTPUT_DEF_FROM_DECLS (asm_out_file, decl, target);
4151 #else
4152 ASM_OUTPUT_DEF (asm_out_file, name, IDENTIFIER_POINTER (target));
4153 #endif
4154 #else /* !ASM_OUTPUT_DEF */
4155 #if defined (ASM_OUTPUT_WEAK_ALIAS) || defined (ASM_WEAKEN_DECL)
4156 if (DECL_WEAK (decl))
4158 tree *p, t;
4159 #ifdef ASM_WEAKEN_DECL
4160 ASM_WEAKEN_DECL (asm_out_file, decl, name, IDENTIFIER_POINTER (target));
4161 #else
4162 ASM_OUTPUT_WEAK_ALIAS (asm_out_file, name, IDENTIFIER_POINTER (target));
4163 #endif
4164 /* Remove this function from the pending weak list so that
4165 we do not emit multiple .weak directives for it. */
4166 for (p = &weak_decls; (t = *p) ; )
4167 if (DECL_ASSEMBLER_NAME (decl)
4168 == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
4169 *p = TREE_CHAIN (t);
4170 else
4171 p = &TREE_CHAIN (t);
4173 else
4174 warning ("only weak aliases are supported in this configuration");
4176 #else
4177 warning ("alias definitions not supported in this configuration; ignored");
4178 #endif
4179 #endif
4181 TREE_USED (decl) = 1;
4182 TREE_ASM_WRITTEN (decl) = 1;
4183 TREE_ASM_WRITTEN (DECL_ASSEMBLER_NAME (decl)) = 1;
4186 /* Emit an assembler directive to set symbol for DECL visibility to
4187 the visibility type VIS, which must not be VISIBILITY_DEFAULT. */
4189 void
4190 default_assemble_visibility (tree decl, int vis)
4192 static const char * const visibility_types[] = {
4193 NULL, "internal", "hidden", "protected"
4196 const char *name, *type;
4198 name = (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
4199 type = visibility_types[vis];
4201 #ifdef HAVE_GAS_HIDDEN
4202 fprintf (asm_out_file, "\t.%s\t", type);
4203 assemble_name (asm_out_file, name);
4204 fprintf (asm_out_file, "\n");
4205 #else
4206 warning ("visibility attribute not supported in this configuration; ignored");
4207 #endif
4210 /* A helper function to call assemble_visibility when needed for a decl. */
4212 static void
4213 maybe_assemble_visibility (tree decl)
4215 enum symbol_visibility vis = DECL_VISIBILITY (decl);
4217 if (vis != VISIBILITY_DEFAULT)
4218 (* targetm.asm_out.visibility) (decl, vis);
4221 /* Returns 1 if the target configuration supports defining public symbols
4222 so that one of them will be chosen at link time instead of generating a
4223 multiply-defined symbol error, whether through the use of weak symbols or
4224 a target-specific mechanism for having duplicates discarded. */
4227 supports_one_only (void)
4229 if (SUPPORTS_ONE_ONLY)
4230 return 1;
4231 return SUPPORTS_WEAK;
4234 /* Set up DECL as a public symbol that can be defined in multiple
4235 translation units without generating a linker error. */
4237 void
4238 make_decl_one_only (tree decl)
4240 if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != FUNCTION_DECL)
4241 abort ();
4243 TREE_PUBLIC (decl) = 1;
4245 if (TREE_CODE (decl) == VAR_DECL
4246 && (DECL_INITIAL (decl) == 0 || DECL_INITIAL (decl) == error_mark_node))
4247 DECL_COMMON (decl) = 1;
4248 else if (SUPPORTS_ONE_ONLY)
4250 #ifdef MAKE_DECL_ONE_ONLY
4251 MAKE_DECL_ONE_ONLY (decl);
4252 #endif
4253 DECL_ONE_ONLY (decl) = 1;
4255 else if (SUPPORTS_WEAK)
4256 DECL_WEAK (decl) = 1;
4257 else
4258 abort ();
4261 void
4262 init_varasm_once (void)
4264 in_named_htab = htab_create_ggc (31, in_named_entry_hash,
4265 in_named_entry_eq, NULL);
4266 const_desc_htab = htab_create_ggc (1009, const_desc_hash,
4267 const_desc_eq, NULL);
4269 const_alias_set = new_alias_set ();
4272 enum tls_model
4273 decl_tls_model (tree decl)
4275 enum tls_model kind;
4276 tree attr = lookup_attribute ("tls_model", DECL_ATTRIBUTES (decl));
4277 bool is_local;
4279 if (attr)
4281 attr = TREE_VALUE (TREE_VALUE (attr));
4282 if (TREE_CODE (attr) != STRING_CST)
4283 abort ();
4284 if (!strcmp (TREE_STRING_POINTER (attr), "local-exec"))
4285 kind = TLS_MODEL_LOCAL_EXEC;
4286 else if (!strcmp (TREE_STRING_POINTER (attr), "initial-exec"))
4287 kind = TLS_MODEL_INITIAL_EXEC;
4288 else if (!strcmp (TREE_STRING_POINTER (attr), "local-dynamic"))
4289 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
4290 else if (!strcmp (TREE_STRING_POINTER (attr), "global-dynamic"))
4291 kind = TLS_MODEL_GLOBAL_DYNAMIC;
4292 else
4293 abort ();
4294 return kind;
4297 is_local = (*targetm.binds_local_p) (decl);
4298 if (!flag_pic)
4300 if (is_local)
4301 kind = TLS_MODEL_LOCAL_EXEC;
4302 else
4303 kind = TLS_MODEL_INITIAL_EXEC;
4305 /* Local dynamic is inefficient when we're not combining the
4306 parts of the address. */
4307 else if (optimize && is_local)
4308 kind = TLS_MODEL_LOCAL_DYNAMIC;
4309 else
4310 kind = TLS_MODEL_GLOBAL_DYNAMIC;
4311 if (kind < flag_tls_default)
4312 kind = flag_tls_default;
4314 return kind;
4317 /* Select a set of attributes for section NAME based on the properties
4318 of DECL and whether or not RELOC indicates that DECL's initializer
4319 might contain runtime relocations.
4321 We make the section read-only and executable for a function decl,
4322 read-only for a const data decl, and writable for a non-const data decl. */
4324 unsigned int
4325 default_section_type_flags (tree decl, const char *name, int reloc)
4327 return default_section_type_flags_1 (decl, name, reloc, flag_pic);
4330 unsigned int
4331 default_section_type_flags_1 (tree decl, const char *name, int reloc,
4332 int shlib)
4334 unsigned int flags;
4336 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
4337 flags = SECTION_CODE;
4338 else if (decl && decl_readonly_section_1 (decl, reloc, shlib))
4339 flags = 0;
4340 else
4341 flags = SECTION_WRITE;
4343 if (decl && DECL_ONE_ONLY (decl))
4344 flags |= SECTION_LINKONCE;
4346 if (decl && TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL (decl))
4347 flags |= SECTION_TLS | SECTION_WRITE;
4349 if (strcmp (name, ".bss") == 0
4350 || strncmp (name, ".bss.", 5) == 0
4351 || strncmp (name, ".gnu.linkonce.b.", 16) == 0
4352 || strcmp (name, ".sbss") == 0
4353 || strncmp (name, ".sbss.", 6) == 0
4354 || strncmp (name, ".gnu.linkonce.sb.", 17) == 0
4355 || strcmp (name, ".tbss") == 0
4356 || strncmp (name, ".gnu.linkonce.tb.", 17) == 0)
4357 flags |= SECTION_BSS;
4359 if (strcmp (name, ".tdata") == 0
4360 || strcmp (name, ".tbss") == 0
4361 || strncmp (name, ".gnu.linkonce.td.", 17) == 0
4362 || strncmp (name, ".gnu.linkonce.tb.", 17) == 0)
4363 flags |= SECTION_TLS;
4365 /* These three sections have special ELF types. They are neither
4366 SHT_PROGBITS nor SHT_NOBITS, so when changing sections we don't
4367 want to print a section type (@progbits or @nobits). If someone
4368 is silly enough to emit code or TLS variables to one of these
4369 sections, then don't handle them specially. */
4370 if (!(flags & (SECTION_CODE | SECTION_BSS | SECTION_TLS))
4371 && (strcmp (name, ".init_array") == 0
4372 || strcmp (name, ".fini_array") == 0
4373 || strcmp (name, ".preinit_array") == 0))
4374 flags |= SECTION_NOTYPE;
4376 return flags;
4379 /* Output assembly to switch to section NAME with attribute FLAGS.
4380 Four variants for common object file formats. */
4382 void
4383 default_no_named_section (const char *name ATTRIBUTE_UNUSED,
4384 unsigned int flags ATTRIBUTE_UNUSED)
4386 /* Some object formats don't support named sections at all. The
4387 front-end should already have flagged this as an error. */
4388 abort ();
4391 void
4392 default_elf_asm_named_section (const char *name, unsigned int flags)
4394 char flagchars[10], *f = flagchars;
4396 if (! named_section_first_declaration (name))
4398 fprintf (asm_out_file, "\t.section\t%s\n", name);
4399 return;
4402 if (!(flags & SECTION_DEBUG))
4403 *f++ = 'a';
4404 if (flags & SECTION_WRITE)
4405 *f++ = 'w';
4406 if (flags & SECTION_CODE)
4407 *f++ = 'x';
4408 if (flags & SECTION_SMALL)
4409 *f++ = 's';
4410 if (flags & SECTION_MERGE)
4411 *f++ = 'M';
4412 if (flags & SECTION_STRINGS)
4413 *f++ = 'S';
4414 if (flags & SECTION_TLS)
4415 *f++ = 'T';
4416 *f = '\0';
4418 fprintf (asm_out_file, "\t.section\t%s,\"%s\"", name, flagchars);
4420 if (!(flags & SECTION_NOTYPE))
4422 const char *type;
4424 if (flags & SECTION_BSS)
4425 type = "nobits";
4426 else
4427 type = "progbits";
4429 fprintf (asm_out_file, ",@%s", type);
4431 if (flags & SECTION_ENTSIZE)
4432 fprintf (asm_out_file, ",%d", flags & SECTION_ENTSIZE);
4435 putc ('\n', asm_out_file);
4438 void
4439 default_coff_asm_named_section (const char *name, unsigned int flags)
4441 char flagchars[8], *f = flagchars;
4443 if (flags & SECTION_WRITE)
4444 *f++ = 'w';
4445 if (flags & SECTION_CODE)
4446 *f++ = 'x';
4447 *f = '\0';
4449 fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
4452 void
4453 default_pe_asm_named_section (const char *name, unsigned int flags)
4455 default_coff_asm_named_section (name, flags);
4457 if (flags & SECTION_LINKONCE)
4459 /* Functions may have been compiled at various levels of
4460 optimization so we can't use `same_size' here.
4461 Instead, have the linker pick one. */
4462 fprintf (asm_out_file, "\t.linkonce %s\n",
4463 (flags & SECTION_CODE ? "discard" : "same_size"));
4467 /* The lame default section selector. */
4469 void
4470 default_select_section (tree decl, int reloc,
4471 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
4473 bool readonly = false;
4475 if (DECL_P (decl))
4477 if (decl_readonly_section (decl, reloc))
4478 readonly = true;
4480 else if (TREE_CODE (decl) == CONSTRUCTOR)
4482 if (! ((flag_pic && reloc)
4483 || !TREE_READONLY (decl)
4484 || TREE_SIDE_EFFECTS (decl)
4485 || !TREE_CONSTANT (decl)))
4486 readonly = true;
4488 else if (TREE_CODE (decl) == STRING_CST)
4489 readonly = !flag_writable_strings;
4490 else if (! (flag_pic && reloc))
4491 readonly = true;
4493 if (readonly)
4494 readonly_data_section ();
4495 else
4496 data_section ();
4499 /* A helper function for default_elf_select_section and
4500 default_elf_unique_section. Categorizes the DECL. */
4502 enum section_category
4504 SECCAT_TEXT,
4506 SECCAT_RODATA,
4507 SECCAT_RODATA_MERGE_STR,
4508 SECCAT_RODATA_MERGE_STR_INIT,
4509 SECCAT_RODATA_MERGE_CONST,
4510 SECCAT_SRODATA,
4512 SECCAT_DATA,
4514 /* To optimize loading of shared programs, define following subsections
4515 of data section:
4516 _REL Contains data that has relocations, so they get grouped
4517 together and dynamic linker will visit fewer pages in memory.
4518 _RO Contains data that is otherwise read-only. This is useful
4519 with prelinking as most relocations won't be dynamically
4520 linked and thus stay read only.
4521 _LOCAL Marks data containing relocations only to local objects.
4522 These relocations will get fully resolved by prelinking. */
4523 SECCAT_DATA_REL,
4524 SECCAT_DATA_REL_LOCAL,
4525 SECCAT_DATA_REL_RO,
4526 SECCAT_DATA_REL_RO_LOCAL,
4528 SECCAT_SDATA,
4529 SECCAT_TDATA,
4531 SECCAT_BSS,
4532 SECCAT_SBSS,
4533 SECCAT_TBSS
4536 static enum section_category
4537 categorize_decl_for_section (tree, int, int);
4539 static enum section_category
4540 categorize_decl_for_section (tree decl, int reloc, int shlib)
4542 enum section_category ret;
4544 if (TREE_CODE (decl) == FUNCTION_DECL)
4545 return SECCAT_TEXT;
4546 else if (TREE_CODE (decl) == STRING_CST)
4548 if (flag_writable_strings)
4549 return SECCAT_DATA;
4550 else
4551 return SECCAT_RODATA_MERGE_STR;
4553 else if (TREE_CODE (decl) == VAR_DECL)
4555 if (DECL_INITIAL (decl) == NULL
4556 || DECL_INITIAL (decl) == error_mark_node)
4557 ret = SECCAT_BSS;
4558 else if (! TREE_READONLY (decl)
4559 || TREE_SIDE_EFFECTS (decl)
4560 || ! TREE_CONSTANT (DECL_INITIAL (decl)))
4562 if (shlib && (reloc & 2))
4563 ret = SECCAT_DATA_REL;
4564 else if (shlib && reloc)
4565 ret = SECCAT_DATA_REL_LOCAL;
4566 else
4567 ret = SECCAT_DATA;
4569 else if (shlib && (reloc & 2))
4570 ret = SECCAT_DATA_REL_RO;
4571 else if (shlib && reloc)
4572 ret = SECCAT_DATA_REL_RO_LOCAL;
4573 else if (reloc || flag_merge_constants < 2)
4574 /* C and C++ don't allow different variables to share the same
4575 location. -fmerge-all-constants allows even that (at the
4576 expense of not conforming). */
4577 ret = SECCAT_RODATA;
4578 else if (TREE_CODE (DECL_INITIAL (decl)) == STRING_CST)
4579 ret = SECCAT_RODATA_MERGE_STR_INIT;
4580 else
4581 ret = SECCAT_RODATA_MERGE_CONST;
4583 else if (TREE_CODE (decl) == CONSTRUCTOR)
4585 if ((shlib && reloc)
4586 || TREE_SIDE_EFFECTS (decl)
4587 || ! TREE_CONSTANT (decl))
4588 ret = SECCAT_DATA;
4589 else
4590 ret = SECCAT_RODATA;
4592 else
4593 ret = SECCAT_RODATA;
4595 /* There are no read-only thread-local sections. */
4596 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL (decl))
4598 if (ret == SECCAT_BSS)
4599 ret = SECCAT_TBSS;
4600 else
4601 ret = SECCAT_TDATA;
4604 /* If the target uses small data sections, select it. */
4605 else if ((*targetm.in_small_data_p) (decl))
4607 if (ret == SECCAT_BSS)
4608 ret = SECCAT_SBSS;
4609 else if (targetm.have_srodata_section && ret == SECCAT_RODATA)
4610 ret = SECCAT_SRODATA;
4611 else
4612 ret = SECCAT_SDATA;
4615 return ret;
4618 bool
4619 decl_readonly_section (tree decl, int reloc)
4621 return decl_readonly_section_1 (decl, reloc, flag_pic);
4624 bool
4625 decl_readonly_section_1 (tree decl, int reloc, int shlib)
4627 switch (categorize_decl_for_section (decl, reloc, shlib))
4629 case SECCAT_RODATA:
4630 case SECCAT_RODATA_MERGE_STR:
4631 case SECCAT_RODATA_MERGE_STR_INIT:
4632 case SECCAT_RODATA_MERGE_CONST:
4633 case SECCAT_SRODATA:
4634 return true;
4635 break;
4636 default:
4637 return false;
4638 break;
4642 /* Select a section based on the above categorization. */
4644 void
4645 default_elf_select_section (tree decl, int reloc,
4646 unsigned HOST_WIDE_INT align)
4648 default_elf_select_section_1 (decl, reloc, align, flag_pic);
4651 void
4652 default_elf_select_section_1 (tree decl, int reloc,
4653 unsigned HOST_WIDE_INT align, int shlib)
4655 switch (categorize_decl_for_section (decl, reloc, shlib))
4657 case SECCAT_TEXT:
4658 /* We're not supposed to be called on FUNCTION_DECLs. */
4659 abort ();
4660 case SECCAT_RODATA:
4661 readonly_data_section ();
4662 break;
4663 case SECCAT_RODATA_MERGE_STR:
4664 mergeable_string_section (decl, align, 0);
4665 break;
4666 case SECCAT_RODATA_MERGE_STR_INIT:
4667 mergeable_string_section (DECL_INITIAL (decl), align, 0);
4668 break;
4669 case SECCAT_RODATA_MERGE_CONST:
4670 mergeable_constant_section (DECL_MODE (decl), align, 0);
4671 break;
4672 case SECCAT_SRODATA:
4673 named_section (NULL_TREE, ".sdata2", reloc);
4674 break;
4675 case SECCAT_DATA:
4676 data_section ();
4677 break;
4678 case SECCAT_DATA_REL:
4679 named_section (NULL_TREE, ".data.rel", reloc);
4680 break;
4681 case SECCAT_DATA_REL_LOCAL:
4682 named_section (NULL_TREE, ".data.rel.local", reloc);
4683 break;
4684 case SECCAT_DATA_REL_RO:
4685 named_section (NULL_TREE, ".data.rel.ro", reloc);
4686 break;
4687 case SECCAT_DATA_REL_RO_LOCAL:
4688 named_section (NULL_TREE, ".data.rel.ro.local", reloc);
4689 break;
4690 case SECCAT_SDATA:
4691 named_section (NULL_TREE, ".sdata", reloc);
4692 break;
4693 case SECCAT_TDATA:
4694 named_section (NULL_TREE, ".tdata", reloc);
4695 break;
4696 case SECCAT_BSS:
4697 #ifdef BSS_SECTION_ASM_OP
4698 bss_section ();
4699 #else
4700 named_section (NULL_TREE, ".bss", reloc);
4701 #endif
4702 break;
4703 case SECCAT_SBSS:
4704 named_section (NULL_TREE, ".sbss", reloc);
4705 break;
4706 case SECCAT_TBSS:
4707 named_section (NULL_TREE, ".tbss", reloc);
4708 break;
4709 default:
4710 abort ();
4714 /* Construct a unique section name based on the decl name and the
4715 categorization performed above. */
4717 void
4718 default_unique_section (tree decl, int reloc)
4720 default_unique_section_1 (decl, reloc, flag_pic);
4723 void
4724 default_unique_section_1 (tree decl, int reloc, int shlib)
4726 bool one_only = DECL_ONE_ONLY (decl);
4727 const char *prefix, *name;
4728 size_t nlen, plen;
4729 char *string;
4731 switch (categorize_decl_for_section (decl, reloc, shlib))
4733 case SECCAT_TEXT:
4734 prefix = one_only ? ".gnu.linkonce.t." : ".text.";
4735 break;
4736 case SECCAT_RODATA:
4737 case SECCAT_RODATA_MERGE_STR:
4738 case SECCAT_RODATA_MERGE_STR_INIT:
4739 case SECCAT_RODATA_MERGE_CONST:
4740 prefix = one_only ? ".gnu.linkonce.r." : ".rodata.";
4741 break;
4742 case SECCAT_SRODATA:
4743 prefix = one_only ? ".gnu.linkonce.s2." : ".sdata2.";
4744 break;
4745 case SECCAT_DATA:
4746 case SECCAT_DATA_REL:
4747 case SECCAT_DATA_REL_LOCAL:
4748 case SECCAT_DATA_REL_RO:
4749 case SECCAT_DATA_REL_RO_LOCAL:
4750 prefix = one_only ? ".gnu.linkonce.d." : ".data.";
4751 break;
4752 case SECCAT_SDATA:
4753 prefix = one_only ? ".gnu.linkonce.s." : ".sdata.";
4754 break;
4755 case SECCAT_BSS:
4756 prefix = one_only ? ".gnu.linkonce.b." : ".bss.";
4757 break;
4758 case SECCAT_SBSS:
4759 prefix = one_only ? ".gnu.linkonce.sb." : ".sbss.";
4760 break;
4761 case SECCAT_TDATA:
4762 prefix = one_only ? ".gnu.linkonce.td." : ".tdata.";
4763 break;
4764 case SECCAT_TBSS:
4765 prefix = one_only ? ".gnu.linkonce.tb." : ".tbss.";
4766 break;
4767 default:
4768 abort ();
4770 plen = strlen (prefix);
4772 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
4773 name = (* targetm.strip_name_encoding) (name);
4774 nlen = strlen (name);
4776 string = alloca (nlen + plen + 1);
4777 memcpy (string, prefix, plen);
4778 memcpy (string + plen, name, nlen + 1);
4780 DECL_SECTION_NAME (decl) = build_string (nlen + plen, string);
4783 void
4784 default_select_rtx_section (enum machine_mode mode ATTRIBUTE_UNUSED,
4785 rtx x,
4786 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
4788 if (flag_pic)
4789 switch (GET_CODE (x))
4791 case CONST:
4792 case SYMBOL_REF:
4793 case LABEL_REF:
4794 data_section ();
4795 return;
4797 default:
4798 break;
4801 readonly_data_section ();
4804 void
4805 default_elf_select_rtx_section (enum machine_mode mode, rtx x,
4806 unsigned HOST_WIDE_INT align)
4808 /* ??? Handle small data here somehow. */
4810 if (flag_pic)
4811 switch (GET_CODE (x))
4813 case CONST:
4814 case SYMBOL_REF:
4815 named_section (NULL_TREE, ".data.rel.ro", 3);
4816 return;
4818 case LABEL_REF:
4819 named_section (NULL_TREE, ".data.rel.ro.local", 1);
4820 return;
4822 default:
4823 break;
4826 mergeable_constant_section (mode, align, 0);
4829 /* Set the generally applicable flags on the SYMBOL_REF for EXP. */
4831 void
4832 default_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
4834 rtx symbol;
4835 int flags;
4837 /* Careful not to prod global register variables. */
4838 if (GET_CODE (rtl) != MEM)
4839 return;
4840 symbol = XEXP (rtl, 0);
4841 if (GET_CODE (symbol) != SYMBOL_REF)
4842 return;
4844 flags = 0;
4845 if (TREE_CODE (decl) == FUNCTION_DECL)
4846 flags |= SYMBOL_FLAG_FUNCTION;
4847 if ((*targetm.binds_local_p) (decl))
4848 flags |= SYMBOL_FLAG_LOCAL;
4849 if ((*targetm.in_small_data_p) (decl))
4850 flags |= SYMBOL_FLAG_SMALL;
4851 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL (decl))
4852 flags |= decl_tls_model (decl) << SYMBOL_FLAG_TLS_SHIFT;
4853 /* ??? Why is DECL_EXTERNAL ever set for non-PUBLIC names? Without
4854 being PUBLIC, the thing *must* be defined in this translation unit.
4855 Prevent this buglet from being propagated into rtl code as well. */
4856 if (DECL_P (decl) && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl))
4857 flags |= SYMBOL_FLAG_EXTERNAL;
4859 SYMBOL_REF_FLAGS (symbol) = flags;
4862 /* By default, we do nothing for encode_section_info, so we need not
4863 do anything but discard the '*' marker. */
4865 const char *
4866 default_strip_name_encoding (const char *str)
4868 return str + (*str == '*');
4871 /* Assume ELF-ish defaults, since that's pretty much the most liberal
4872 wrt cross-module name binding. */
4874 bool
4875 default_binds_local_p (tree exp)
4877 return default_binds_local_p_1 (exp, flag_shlib);
4880 bool
4881 default_binds_local_p_1 (tree exp, int shlib)
4883 bool local_p;
4885 /* A non-decl is an entry in the constant pool. */
4886 if (!DECL_P (exp))
4887 local_p = true;
4888 /* Static variables are always local. */
4889 else if (! TREE_PUBLIC (exp))
4890 local_p = true;
4891 /* A variable is local if the user tells us so. */
4892 else if (DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
4893 local_p = true;
4894 /* Otherwise, variables defined outside this object may not be local. */
4895 else if (DECL_EXTERNAL (exp))
4896 local_p = false;
4897 /* Linkonce and weak data are never local. */
4898 else if (DECL_ONE_ONLY (exp) || DECL_WEAK (exp))
4899 local_p = false;
4900 /* If PIC, then assume that any global name can be overridden by
4901 symbols resolved from other modules. */
4902 else if (shlib)
4903 local_p = false;
4904 /* Uninitialized COMMON variable may be unified with symbols
4905 resolved from other modules. */
4906 else if (DECL_COMMON (exp)
4907 && (DECL_INITIAL (exp) == NULL
4908 || DECL_INITIAL (exp) == error_mark_node))
4909 local_p = false;
4910 /* Otherwise we're left with initialized (or non-common) global data
4911 which is of necessity defined locally. */
4912 else
4913 local_p = true;
4915 return local_p;
4918 /* Determine whether or not a pointer mode is valid. Assume defaults
4919 of ptr_mode or Pmode - can be overridden. */
4920 bool
4921 default_valid_pointer_mode (enum machine_mode mode)
4923 return (mode == ptr_mode || mode == Pmode);
4926 /* Default function to output code that will globalize a label. A
4927 target must define GLOBAL_ASM_OP or provide it's own function to
4928 globalize a label. */
4929 #ifdef GLOBAL_ASM_OP
4930 void
4931 default_globalize_label (FILE * stream, const char *name)
4933 fputs (GLOBAL_ASM_OP, stream);
4934 assemble_name (stream, name);
4935 putc ('\n', stream);
4937 #endif /* GLOBAL_ASM_OP */
4939 /* This is how to output an internal numbered label where PREFIX is
4940 the class of label and LABELNO is the number within the class. */
4942 void
4943 default_internal_label (FILE *stream, const char *prefix,
4944 unsigned long labelno)
4946 char *const buf = alloca (40 + strlen (prefix));
4947 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
4948 ASM_OUTPUT_LABEL (stream, buf);
4951 /* This is the default behavior at the beginning of a file. It's
4952 controlled by two other target-hook toggles. */
4953 void
4954 default_file_start (void)
4956 if (targetm.file_start_app_off && !flag_verbose_asm)
4957 fputs (ASM_APP_OFF, asm_out_file);
4959 if (targetm.file_start_file_directive)
4960 output_file_directive (asm_out_file, main_input_filename);
4963 /* This is a generic routine suitable for use as TARGET_ASM_FILE_END
4964 which emits a special section directive used to indicate whether or
4965 not this object file needs an executable stack. This is primarily
4966 a GNU extension to ELF but could be used on other targets. */
4967 void
4968 file_end_indicate_exec_stack (void)
4970 unsigned int flags = SECTION_DEBUG;
4971 if (trampolines_created)
4972 flags |= SECTION_CODE;
4974 named_section_flags (".note.GNU-stack", flags);
4977 #include "gt-varasm.h"