2002-05-17 David S. Miller <davem@redhat.com>
[official-gcc.git] / gcc / varasm.c
blobe13efe0914cbdaecd5f2f21494c2f9f44da714a6
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 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 "rtl.h"
33 #include "tree.h"
34 #include "flags.h"
35 #include "function.h"
36 #include "expr.h"
37 #include "hard-reg-set.h"
38 #include "regs.h"
39 #include "output.h"
40 #include "real.h"
41 #include "toplev.h"
42 #include "obstack.h"
43 #include "hashtab.h"
44 #include "c-pragma.h"
45 #include "c-tree.h"
46 #include "ggc.h"
47 #include "langhooks.h"
48 #include "tm_p.h"
49 #include "debug.h"
50 #include "target.h"
52 #ifdef XCOFF_DEBUGGING_INFO
53 #include "xcoffout.h" /* Needed for external data
54 declarations for e.g. AIX 4.x. */
55 #endif
57 #ifndef TRAMPOLINE_ALIGNMENT
58 #define TRAMPOLINE_ALIGNMENT FUNCTION_BOUNDARY
59 #endif
61 #ifndef ASM_STABS_OP
62 #define ASM_STABS_OP "\t.stabs\t"
63 #endif
65 /* The (assembler) name of the first globally-visible object output. */
66 const char *first_global_object_name;
67 const char *weak_global_object_name;
69 extern struct obstack permanent_obstack;
70 #define obstack_chunk_alloc xmalloc
72 struct addr_const;
73 struct constant_descriptor;
74 struct rtx_const;
75 struct pool_constant;
77 #define MAX_RTX_HASH_TABLE 61
79 struct varasm_status
81 /* Hash facility for making memory-constants
82 from constant rtl-expressions. It is used on RISC machines
83 where immediate integer arguments and constant addresses are restricted
84 so that such constants must be stored in memory.
86 This pool of constants is reinitialized for each function
87 so each function gets its own constants-pool that comes right before
88 it. */
89 struct constant_descriptor **x_const_rtx_hash_table;
90 struct pool_constant **x_const_rtx_sym_hash_table;
92 /* Pointers to first and last constant in pool. */
93 struct pool_constant *x_first_pool, *x_last_pool;
95 /* Current offset in constant pool (does not include any machine-specific
96 header). */
97 HOST_WIDE_INT x_pool_offset;
100 #define const_rtx_hash_table (cfun->varasm->x_const_rtx_hash_table)
101 #define const_rtx_sym_hash_table (cfun->varasm->x_const_rtx_sym_hash_table)
102 #define first_pool (cfun->varasm->x_first_pool)
103 #define last_pool (cfun->varasm->x_last_pool)
104 #define pool_offset (cfun->varasm->x_pool_offset)
106 /* Number for making the label on the next
107 constant that is stored in memory. */
109 int const_labelno;
111 /* Number for making the label on the next
112 static variable internal to a function. */
114 int var_labelno;
116 /* Carry information from ASM_DECLARE_OBJECT_NAME
117 to ASM_FINISH_DECLARE_OBJECT. */
119 int size_directive_output;
121 /* The last decl for which assemble_variable was called,
122 if it did ASM_DECLARE_OBJECT_NAME.
123 If the last call to assemble_variable didn't do that,
124 this holds 0. */
126 tree last_assemble_variable_decl;
128 /* RTX_UNCHANGING_P in a MEM can mean it is stored into, for initialization.
129 So giving constant the alias set for the type will allow such
130 initializations to appear to conflict with the load of the constant. We
131 avoid this by giving all constants an alias set for just constants.
132 Since there will be no stores to that alias set, nothing will ever
133 conflict with them. */
135 static HOST_WIDE_INT const_alias_set;
137 static const char *strip_reg_name PARAMS ((const char *));
138 static int contains_pointers_p PARAMS ((tree));
139 static void decode_addr_const PARAMS ((tree, struct addr_const *));
140 static int const_hash PARAMS ((tree));
141 static int compare_constant PARAMS ((tree,
142 struct constant_descriptor *));
143 static const unsigned char *compare_constant_1 PARAMS ((tree, const unsigned char *));
144 static struct constant_descriptor *record_constant PARAMS ((tree));
145 static void record_constant_1 PARAMS ((tree));
146 static tree copy_constant PARAMS ((tree));
147 static void output_constant_def_contents PARAMS ((tree, int, int));
148 static void decode_rtx_const PARAMS ((enum machine_mode, rtx,
149 struct rtx_const *));
150 static int const_hash_rtx PARAMS ((enum machine_mode, rtx));
151 static int compare_constant_rtx PARAMS ((enum machine_mode, rtx,
152 struct constant_descriptor *));
153 static struct constant_descriptor *record_constant_rtx PARAMS ((enum machine_mode,
154 rtx));
155 static struct pool_constant *find_pool_constant PARAMS ((struct function *, rtx));
156 static void mark_constant_pool PARAMS ((void));
157 static void mark_constants PARAMS ((rtx));
158 static int mark_constant PARAMS ((rtx *current_rtx, void *data));
159 static int output_addressed_constants PARAMS ((tree));
160 static void output_after_function_constants PARAMS ((void));
161 static unsigned HOST_WIDE_INT array_size_for_constructor PARAMS ((tree));
162 static unsigned min_align PARAMS ((unsigned, unsigned));
163 static void output_constructor PARAMS ((tree, HOST_WIDE_INT,
164 unsigned int));
165 static void globalize_decl PARAMS ((tree));
166 static void maybe_assemble_visibility PARAMS ((tree));
167 static int in_named_entry_eq PARAMS ((const PTR, const PTR));
168 static hashval_t in_named_entry_hash PARAMS ((const PTR));
169 #ifdef ASM_OUTPUT_BSS
170 static void asm_output_bss PARAMS ((FILE *, tree, const char *, int, int));
171 #endif
172 #ifdef BSS_SECTION_ASM_OP
173 #ifdef ASM_OUTPUT_ALIGNED_BSS
174 static void asm_output_aligned_bss PARAMS ((FILE *, tree, const char *,
175 int, int));
176 #endif
177 #endif /* BSS_SECTION_ASM_OP */
178 static void mark_pool_constant PARAMS ((struct pool_constant *));
179 static void mark_const_hash_entry PARAMS ((void *));
180 static int mark_const_str_htab_1 PARAMS ((void **, void *));
181 static void mark_const_str_htab PARAMS ((void *));
182 static hashval_t const_str_htab_hash PARAMS ((const void *x));
183 static int const_str_htab_eq PARAMS ((const void *x, const void *y));
184 static void const_str_htab_del PARAMS ((void *));
185 static void asm_emit_uninitialised PARAMS ((tree, const char*, int, int));
186 static void resolve_unique_section PARAMS ((tree, int, int));
187 static void mark_weak PARAMS ((tree));
189 static enum in_section { no_section, in_text, in_data, in_named
190 #ifdef BSS_SECTION_ASM_OP
191 , in_bss
192 #endif
193 #ifdef CTORS_SECTION_ASM_OP
194 , in_ctors
195 #endif
196 #ifdef DTORS_SECTION_ASM_OP
197 , in_dtors
198 #endif
199 #ifdef EXTRA_SECTIONS
200 , EXTRA_SECTIONS
201 #endif
202 } in_section = no_section;
204 /* Return a non-zero value if DECL has a section attribute. */
205 #ifndef IN_NAMED_SECTION
206 #define IN_NAMED_SECTION(DECL) \
207 ((TREE_CODE (DECL) == FUNCTION_DECL || TREE_CODE (DECL) == VAR_DECL) \
208 && DECL_SECTION_NAME (DECL) != NULL_TREE)
209 #endif
211 /* Text of section name when in_section == in_named. */
212 static const char *in_named_name;
214 /* Hash table of flags that have been used for a particular named section. */
216 struct in_named_entry
218 const char *name;
219 unsigned int flags;
220 bool declared;
223 static htab_t in_named_htab;
225 /* Define functions like text_section for any extra sections. */
226 #ifdef EXTRA_SECTION_FUNCTIONS
227 EXTRA_SECTION_FUNCTIONS
228 #endif
230 /* Tell assembler to switch to text section. */
232 void
233 text_section ()
235 if (in_section != in_text)
237 #ifdef TEXT_SECTION
238 TEXT_SECTION ();
239 #else
240 fprintf (asm_out_file, "%s\n", TEXT_SECTION_ASM_OP);
241 #endif
242 in_section = in_text;
246 /* Tell assembler to switch to data section. */
248 void
249 data_section ()
251 if (in_section != in_data)
253 if (flag_shared_data)
255 #ifdef SHARED_SECTION_ASM_OP
256 fprintf (asm_out_file, "%s\n", SHARED_SECTION_ASM_OP);
257 #else
258 fprintf (asm_out_file, "%s\n", DATA_SECTION_ASM_OP);
259 #endif
261 else
262 fprintf (asm_out_file, "%s\n", DATA_SECTION_ASM_OP);
264 in_section = in_data;
268 /* Tell assembler to ALWAYS switch to data section, in case
269 it's not sure where it is. */
271 void
272 force_data_section ()
274 in_section = no_section;
275 data_section ();
278 /* Tell assembler to switch to read-only data section. This is normally
279 the text section. */
281 void
282 readonly_data_section ()
284 #ifdef READONLY_DATA_SECTION
285 READONLY_DATA_SECTION (); /* Note this can call data_section. */
286 #else
287 text_section ();
288 #endif
291 /* Determine if we're in the text section. */
294 in_text_section ()
296 return in_section == in_text;
299 /* Determine if we're in the data section. */
302 in_data_section ()
304 return in_section == in_data;
307 /* Helper routines for maintaining in_named_htab. */
309 static int
310 in_named_entry_eq (p1, p2)
311 const PTR p1;
312 const PTR p2;
314 const struct in_named_entry *old = p1;
315 const char *new = p2;
317 return strcmp (old->name, new) == 0;
320 static hashval_t
321 in_named_entry_hash (p)
322 const PTR p;
324 const struct in_named_entry *old = p;
325 return htab_hash_string (old->name);
328 /* If SECTION has been seen before as a named section, return the flags
329 that were used. Otherwise, return 0. Note, that 0 is a perfectly valid
330 set of flags for a section to have, so 0 does not mean that the section
331 has not been seen. */
333 unsigned int
334 get_named_section_flags (section)
335 const char *section;
337 struct in_named_entry **slot;
339 slot = (struct in_named_entry **)
340 htab_find_slot_with_hash (in_named_htab, section,
341 htab_hash_string (section), NO_INSERT);
343 return slot ? (*slot)->flags : 0;
346 /* Returns true if the section has been declared before. Sets internal
347 flag on this section in in_named_hash so subsequent calls on this
348 section will return false. */
350 bool
351 named_section_first_declaration (name)
352 const char *name;
354 struct in_named_entry **slot;
356 slot = (struct in_named_entry **)
357 htab_find_slot_with_hash (in_named_htab, name,
358 htab_hash_string (name), NO_INSERT);
359 if (! (*slot)->declared)
361 (*slot)->declared = true;
362 return true;
364 else
366 return false;
371 /* Record FLAGS for SECTION. If SECTION was previously recorded with a
372 different set of flags, return false. */
374 bool
375 set_named_section_flags (section, flags)
376 const char *section;
377 unsigned int flags;
379 struct in_named_entry **slot, *entry;
381 slot = (struct in_named_entry **)
382 htab_find_slot_with_hash (in_named_htab, section,
383 htab_hash_string (section), INSERT);
384 entry = *slot;
386 if (!entry)
388 entry = (struct in_named_entry *) xmalloc (sizeof (*entry));
389 *slot = entry;
390 entry->name = ggc_strdup (section);
391 entry->flags = flags;
392 entry->declared = false;
394 else if (entry->flags != flags)
395 return false;
397 return true;
400 /* Tell assembler to change to section NAME with attributes FLAGS. */
402 void
403 named_section_flags (name, flags)
404 const char *name;
405 unsigned int flags;
407 if (in_section != in_named || strcmp (name, in_named_name) != 0)
409 if (! set_named_section_flags (name, flags))
410 abort ();
412 (*targetm.asm_out.named_section) (name, flags);
414 if (flags & SECTION_FORGET)
415 in_section = no_section;
416 else
418 in_named_name = ggc_strdup (name);
419 in_section = in_named;
424 /* Tell assembler to change to section NAME for DECL.
425 If DECL is NULL, just switch to section NAME.
426 If NAME is NULL, get the name from DECL.
427 If RELOC is 1, the initializer for DECL contains relocs. */
429 void
430 named_section (decl, name, reloc)
431 tree decl;
432 const char *name;
433 int reloc;
435 unsigned int flags;
437 if (decl != NULL_TREE && !DECL_P (decl))
438 abort ();
439 if (name == NULL)
440 name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
442 flags = (* targetm.section_type_flags) (decl, name, reloc);
444 /* Sanity check user variables for flag changes. Non-user
445 section flag changes will abort in named_section_flags.
446 However, don't complain if SECTION_OVERRIDE is set.
447 We trust that the setter knows that it is safe to ignore
448 the default flags for this decl. */
449 if (decl && ! set_named_section_flags (name, flags))
451 flags = get_named_section_flags (name);
452 if ((flags & SECTION_OVERRIDE) == 0)
453 error_with_decl (decl, "%s causes a section type conflict");
456 named_section_flags (name, flags);
459 /* If required, set DECL_SECTION_NAME to a unique name. */
461 static void
462 resolve_unique_section (decl, reloc, flag_function_or_data_sections)
463 tree decl;
464 int reloc ATTRIBUTE_UNUSED;
465 int flag_function_or_data_sections;
467 if (DECL_SECTION_NAME (decl) == NULL_TREE
468 && targetm.have_named_sections
469 && (flag_function_or_data_sections
470 || DECL_ONE_ONLY (decl)))
471 (*targetm.asm_out.unique_section) (decl, reloc);
474 #ifdef BSS_SECTION_ASM_OP
476 /* Tell the assembler to switch to the bss section. */
478 void
479 bss_section ()
481 if (in_section != in_bss)
483 #ifdef SHARED_BSS_SECTION_ASM_OP
484 if (flag_shared_data)
485 fprintf (asm_out_file, "%s\n", SHARED_BSS_SECTION_ASM_OP);
486 else
487 #endif
488 fprintf (asm_out_file, "%s\n", BSS_SECTION_ASM_OP);
490 in_section = in_bss;
494 #ifdef ASM_OUTPUT_BSS
496 /* Utility function for ASM_OUTPUT_BSS for targets to use if
497 they don't support alignments in .bss.
498 ??? It is believed that this function will work in most cases so such
499 support is localized here. */
501 static void
502 asm_output_bss (file, decl, name, size, rounded)
503 FILE *file;
504 tree decl ATTRIBUTE_UNUSED;
505 const char *name;
506 int size ATTRIBUTE_UNUSED, rounded;
508 ASM_GLOBALIZE_LABEL (file, name);
509 bss_section ();
510 #ifdef ASM_DECLARE_OBJECT_NAME
511 last_assemble_variable_decl = decl;
512 ASM_DECLARE_OBJECT_NAME (file, name, decl);
513 #else
514 /* Standard thing is just output label for the object. */
515 ASM_OUTPUT_LABEL (file, name);
516 #endif /* ASM_DECLARE_OBJECT_NAME */
517 ASM_OUTPUT_SKIP (file, rounded);
520 #endif
522 #ifdef ASM_OUTPUT_ALIGNED_BSS
524 /* Utility function for targets to use in implementing
525 ASM_OUTPUT_ALIGNED_BSS.
526 ??? It is believed that this function will work in most cases so such
527 support is localized here. */
529 static void
530 asm_output_aligned_bss (file, decl, name, size, align)
531 FILE *file;
532 tree decl ATTRIBUTE_UNUSED;
533 const char *name;
534 int size, align;
536 ASM_GLOBALIZE_LABEL (file, name);
537 bss_section ();
538 ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
539 #ifdef ASM_DECLARE_OBJECT_NAME
540 last_assemble_variable_decl = decl;
541 ASM_DECLARE_OBJECT_NAME (file, name, decl);
542 #else
543 /* Standard thing is just output label for the object. */
544 ASM_OUTPUT_LABEL (file, name);
545 #endif /* ASM_DECLARE_OBJECT_NAME */
546 ASM_OUTPUT_SKIP (file, size ? size : 1);
549 #endif
551 #endif /* BSS_SECTION_ASM_OP */
553 /* Switch to the section for function DECL.
555 If DECL is NULL_TREE, switch to the text section.
556 ??? It's not clear that we will ever be passed NULL_TREE, but it's
557 safer to handle it. */
559 void
560 function_section (decl)
561 tree decl;
563 if (decl != NULL_TREE
564 && DECL_SECTION_NAME (decl) != NULL_TREE)
565 named_section (decl, (char *) 0, 0);
566 else
567 text_section ();
570 /* Switch to section for variable DECL. RELOC is the same as the
571 argument to SELECT_SECTION. */
573 void
574 variable_section (decl, reloc)
575 tree decl;
576 int reloc;
578 if (IN_NAMED_SECTION (decl))
579 named_section (decl, NULL, reloc);
580 else
581 (*targetm.asm_out.select_section) (decl, reloc, DECL_ALIGN (decl));
584 /* Tell assembler to switch to the section for the exception handling
585 table. */
587 void
588 default_exception_section ()
590 if (targetm.have_named_sections)
591 named_section (NULL_TREE, ".gcc_except_table", 0);
592 else if (flag_pic)
593 data_section ();
594 else
595 readonly_data_section ();
598 /* Tell assembler to switch to the section for string merging. */
600 void
601 mergeable_string_section (decl, align, flags)
602 tree decl ATTRIBUTE_UNUSED;
603 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED;
604 unsigned int flags ATTRIBUTE_UNUSED;
606 #ifdef HAVE_GAS_SHF_MERGE
607 if (flag_merge_constants
608 && TREE_CODE (decl) == STRING_CST
609 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
610 && align <= 256
611 && TREE_STRING_LENGTH (decl) >= int_size_in_bytes (TREE_TYPE (decl)))
613 enum machine_mode mode;
614 unsigned int modesize;
615 const char *str;
616 int i, j, len, unit;
617 char name[30];
619 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (decl)));
620 modesize = GET_MODE_BITSIZE (mode);
621 if (modesize >= 8 && modesize <= 256
622 && (modesize & (modesize - 1)) == 0)
624 if (align < modesize)
625 align = modesize;
627 str = TREE_STRING_POINTER (decl);
628 len = TREE_STRING_LENGTH (decl);
629 unit = GET_MODE_SIZE (mode);
631 /* Check for embedded NUL characters. */
632 for (i = 0; i < len; i += unit)
634 for (j = 0; j < unit; j++)
635 if (str[i + j] != '\0')
636 break;
637 if (j == unit)
638 break;
640 if (i == len - unit)
642 sprintf (name, ".rodata.str%d.%d", modesize / 8,
643 (int) (align / 8));
644 flags |= (modesize / 8) | SECTION_MERGE | SECTION_STRINGS;
645 if (!i && modesize < align)
647 /* A "" string with requested alignment greater than
648 character size might cause a problem:
649 if some other string required even bigger
650 alignment than "", then linker might think the
651 "" is just part of padding after some other string
652 and not put it into the hash table initially.
653 But this means "" could have smaller alignment
654 than requested. */
655 #ifdef ASM_OUTPUT_SECTION_START
656 named_section_flags (name, flags);
657 ASM_OUTPUT_SECTION_START (asm_out_file);
658 #else
659 readonly_data_section ();
660 #endif
661 return;
664 named_section_flags (name, flags);
665 return;
669 #endif
670 readonly_data_section ();
673 /* Tell assembler to switch to the section for constant merging. */
675 void
676 mergeable_constant_section (mode, align, flags)
677 enum machine_mode mode ATTRIBUTE_UNUSED;
678 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED;
679 unsigned int flags ATTRIBUTE_UNUSED;
681 #ifdef HAVE_GAS_SHF_MERGE
682 unsigned int modesize = GET_MODE_BITSIZE (mode);
684 if (flag_merge_constants
685 && mode != VOIDmode
686 && mode != BLKmode
687 && modesize <= align
688 && align >= 8
689 && align <= 256
690 && (align & (align - 1)) == 0)
692 char name[24];
694 sprintf (name, ".rodata.cst%d", (int) (align / 8));
695 flags |= (align / 8) | SECTION_MERGE;
696 named_section_flags (name, flags);
697 return;
699 #endif
700 readonly_data_section ();
703 /* Given NAME, a putative register name, discard any customary prefixes. */
705 static const char *
706 strip_reg_name (name)
707 const char *name;
709 #ifdef REGISTER_PREFIX
710 if (!strncmp (name, REGISTER_PREFIX, strlen (REGISTER_PREFIX)))
711 name += strlen (REGISTER_PREFIX);
712 #endif
713 if (name[0] == '%' || name[0] == '#')
714 name++;
715 return name;
718 /* Decode an `asm' spec for a declaration as a register name.
719 Return the register number, or -1 if nothing specified,
720 or -2 if the ASMSPEC is not `cc' or `memory' and is not recognized,
721 or -3 if ASMSPEC is `cc' and is not recognized,
722 or -4 if ASMSPEC is `memory' and is not recognized.
723 Accept an exact spelling or a decimal number.
724 Prefixes such as % are optional. */
727 decode_reg_name (asmspec)
728 const char *asmspec;
730 if (asmspec != 0)
732 int i;
734 /* Get rid of confusing prefixes. */
735 asmspec = strip_reg_name (asmspec);
737 /* Allow a decimal number as a "register name". */
738 for (i = strlen (asmspec) - 1; i >= 0; i--)
739 if (! ISDIGIT (asmspec[i]))
740 break;
741 if (asmspec[0] != 0 && i < 0)
743 i = atoi (asmspec);
744 if (i < FIRST_PSEUDO_REGISTER && i >= 0)
745 return i;
746 else
747 return -2;
750 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
751 if (reg_names[i][0]
752 && ! strcmp (asmspec, strip_reg_name (reg_names[i])))
753 return i;
755 #ifdef ADDITIONAL_REGISTER_NAMES
757 static const struct { const char *const name; const int number; } table[]
758 = ADDITIONAL_REGISTER_NAMES;
760 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
761 if (! strcmp (asmspec, table[i].name))
762 return table[i].number;
764 #endif /* ADDITIONAL_REGISTER_NAMES */
766 if (!strcmp (asmspec, "memory"))
767 return -4;
769 if (!strcmp (asmspec, "cc"))
770 return -3;
772 return -2;
775 return -1;
778 /* Create the DECL_RTL for a VAR_DECL or FUNCTION_DECL. DECL should
779 have static storage duration. In other words, it should not be an
780 automatic variable, including PARM_DECLs.
782 There is, however, one exception: this function handles variables
783 explicitly placed in a particular register by the user.
785 ASMSPEC, if not 0, is the string which the user specified as the
786 assembler symbol name.
788 This is never called for PARM_DECL nodes. */
790 void
791 make_decl_rtl (decl, asmspec)
792 tree decl;
793 const char *asmspec;
795 int top_level = (DECL_CONTEXT (decl) == NULL_TREE);
796 const char *name = 0;
797 const char *new_name = 0;
798 int reg_number;
799 rtx x;
801 /* Check that we are not being given an automatic variable. */
802 /* A weak alias has TREE_PUBLIC set but not the other bits. */
803 if (TREE_CODE (decl) == PARM_DECL
804 || TREE_CODE (decl) == RESULT_DECL
805 || (TREE_CODE (decl) == VAR_DECL
806 && !TREE_STATIC (decl)
807 && !TREE_PUBLIC (decl)
808 && !DECL_EXTERNAL (decl)
809 && !DECL_REGISTER (decl)))
810 abort ();
811 /* And that we were not given a type or a label. */
812 else if (TREE_CODE (decl) == TYPE_DECL
813 || TREE_CODE (decl) == LABEL_DECL)
814 abort ();
816 /* For a duplicate declaration, we can be called twice on the
817 same DECL node. Don't discard the RTL already made. */
818 if (DECL_RTL_SET_P (decl))
820 /* If the old RTL had the wrong mode, fix the mode. */
821 if (GET_MODE (DECL_RTL (decl)) != DECL_MODE (decl))
822 SET_DECL_RTL (decl, adjust_address_nv (DECL_RTL (decl),
823 DECL_MODE (decl), 0));
825 /* ??? Another way to do this would be to do what halfpic.c does
826 and maintain a hashed table of such critters. */
827 /* Let the target reassign the RTL if it wants.
828 This is necessary, for example, when one machine specific
829 decl attribute overrides another. */
830 #ifdef ENCODE_SECTION_INFO
831 ENCODE_SECTION_INFO (decl, false);
832 #endif
833 return;
836 new_name = name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
838 reg_number = decode_reg_name (asmspec);
839 if (reg_number == -2)
841 /* ASMSPEC is given, and not the name of a register. Mark the
842 name with a star so assemble_name won't munge it. */
843 char *starred = alloca (strlen (asmspec) + 2);
844 starred[0] = '*';
845 strcpy (starred + 1, asmspec);
846 new_name = starred;
849 if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
851 /* First detect errors in declaring global registers. */
852 if (reg_number == -1)
853 error_with_decl (decl, "register name not specified for `%s'");
854 else if (reg_number < 0)
855 error_with_decl (decl, "invalid register name for `%s'");
856 else if (TYPE_MODE (TREE_TYPE (decl)) == BLKmode)
857 error_with_decl (decl,
858 "data type of `%s' isn't suitable for a register");
859 else if (! HARD_REGNO_MODE_OK (reg_number, TYPE_MODE (TREE_TYPE (decl))))
860 error_with_decl (decl,
861 "register specified for `%s' isn't suitable for data type");
862 /* Now handle properly declared static register variables. */
863 else
865 int nregs;
867 if (DECL_INITIAL (decl) != 0 && TREE_STATIC (decl))
869 DECL_INITIAL (decl) = 0;
870 error ("global register variable has initial value");
872 if (TREE_THIS_VOLATILE (decl))
873 warning ("volatile register variables don't work as you might wish");
875 /* If the user specified one of the eliminables registers here,
876 e.g., FRAME_POINTER_REGNUM, we don't want to get this variable
877 confused with that register and be eliminated. This usage is
878 somewhat suspect... */
880 SET_DECL_RTL (decl, gen_rtx_raw_REG (DECL_MODE (decl), reg_number));
881 ORIGINAL_REGNO (DECL_RTL (decl)) = reg_number;
882 REG_USERVAR_P (DECL_RTL (decl)) = 1;
884 if (TREE_STATIC (decl))
886 /* Make this register global, so not usable for anything
887 else. */
888 #ifdef ASM_DECLARE_REGISTER_GLOBAL
889 ASM_DECLARE_REGISTER_GLOBAL (asm_out_file, decl, reg_number, name);
890 #endif
891 nregs = HARD_REGNO_NREGS (reg_number, DECL_MODE (decl));
892 while (nregs > 0)
893 globalize_reg (reg_number + --nregs);
896 /* As a register variable, it has no section. */
897 return;
901 /* Now handle ordinary static variables and functions (in memory).
902 Also handle vars declared register invalidly. */
904 if (reg_number >= 0 || reg_number == -3)
905 error_with_decl (decl,
906 "register name given for non-register variable `%s'");
908 /* Specifying a section attribute on a variable forces it into a
909 non-.bss section, and thus it cannot be common. */
910 if (TREE_CODE (decl) == VAR_DECL
911 && DECL_SECTION_NAME (decl) != NULL_TREE
912 && DECL_INITIAL (decl) == NULL_TREE
913 && DECL_COMMON (decl))
914 DECL_COMMON (decl) = 0;
916 /* Can't use just the variable's own name for a variable
917 whose scope is less than the whole file, unless it's a member
918 of a local class (which will already be unambiguous).
919 Concatenate a distinguishing number. */
920 if (!top_level && !TREE_PUBLIC (decl)
921 && ! (DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl)))
922 && asmspec == 0
923 && name == IDENTIFIER_POINTER (DECL_NAME (decl)))
925 char *label;
927 ASM_FORMAT_PRIVATE_NAME (label, name, var_labelno);
928 var_labelno++;
929 new_name = label;
932 if (name != new_name)
934 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (new_name));
935 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
938 /* If this variable is to be treated as volatile, show its
939 tree node has side effects. */
940 if ((flag_volatile_global && TREE_CODE (decl) == VAR_DECL
941 && TREE_PUBLIC (decl))
942 || ((flag_volatile_static && TREE_CODE (decl) == VAR_DECL
943 && (TREE_PUBLIC (decl) || TREE_STATIC (decl)))))
944 TREE_SIDE_EFFECTS (decl) = 1;
946 x = gen_rtx_MEM (DECL_MODE (decl), gen_rtx_SYMBOL_REF (Pmode, name));
947 SYMBOL_REF_WEAK (XEXP (x, 0)) = DECL_WEAK (decl);
948 if (TREE_CODE (decl) != FUNCTION_DECL)
949 set_mem_attributes (x, decl, 1);
950 SET_DECL_RTL (decl, x);
952 /* Optionally set flags or add text to the name to record information
953 such as that it is a function name.
954 If the name is changed, the macro ASM_OUTPUT_LABELREF
955 will have to know how to strip this information. */
956 #ifdef ENCODE_SECTION_INFO
957 ENCODE_SECTION_INFO (decl, true);
958 #endif
961 /* Make the rtl for variable VAR be volatile.
962 Use this only for static variables. */
964 void
965 make_var_volatile (var)
966 tree var;
968 if (GET_CODE (DECL_RTL (var)) != MEM)
969 abort ();
971 MEM_VOLATILE_P (DECL_RTL (var)) = 1;
974 /* Output alignment directive to align for constant expression EXP. */
976 void
977 assemble_constant_align (exp)
978 tree exp;
980 int align;
982 /* Align the location counter as required by EXP's data type. */
983 align = TYPE_ALIGN (TREE_TYPE (exp));
984 #ifdef CONSTANT_ALIGNMENT
985 align = CONSTANT_ALIGNMENT (exp, align);
986 #endif
988 if (align > BITS_PER_UNIT)
990 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
994 /* Output a string of literal assembler code
995 for an `asm' keyword used between functions. */
997 void
998 assemble_asm (string)
999 tree string;
1001 app_enable ();
1003 if (TREE_CODE (string) == ADDR_EXPR)
1004 string = TREE_OPERAND (string, 0);
1006 fprintf (asm_out_file, "\t%s\n", TREE_STRING_POINTER (string));
1009 /* Record an element in the table of global destructors. SYMBOL is
1010 a SYMBOL_REF of the function to be called; PRIORITY is a number
1011 between 0 and MAX_INIT_PRIORITY. */
1013 void
1014 default_stabs_asm_out_destructor (symbol, priority)
1015 rtx symbol;
1016 int priority ATTRIBUTE_UNUSED;
1018 /* Tell GNU LD that this is part of the static destructor set.
1019 This will work for any system that uses stabs, most usefully
1020 aout systems. */
1021 fprintf (asm_out_file, "%s\"___DTOR_LIST__\",22,0,0,", ASM_STABS_OP);
1022 assemble_name (asm_out_file, XSTR (symbol, 0));
1023 fputc ('\n', asm_out_file);
1026 void
1027 default_named_section_asm_out_destructor (symbol, priority)
1028 rtx symbol;
1029 int priority;
1031 const char *section = ".dtors";
1032 char buf[16];
1034 /* ??? This only works reliably with the GNU linker. */
1035 if (priority != DEFAULT_INIT_PRIORITY)
1037 sprintf (buf, ".dtors.%.5u",
1038 /* Invert the numbering so the linker puts us in the proper
1039 order; constructors are run from right to left, and the
1040 linker sorts in increasing order. */
1041 MAX_INIT_PRIORITY - priority);
1042 section = buf;
1045 named_section_flags (section, SECTION_WRITE);
1046 assemble_align (POINTER_SIZE);
1047 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1050 #ifdef DTORS_SECTION_ASM_OP
1051 void
1052 dtors_section ()
1054 if (in_section != in_dtors)
1056 in_section = in_dtors;
1057 fputs (DTORS_SECTION_ASM_OP, asm_out_file);
1058 fputc ('\n', asm_out_file);
1062 void
1063 default_dtor_section_asm_out_destructor (symbol, priority)
1064 rtx symbol;
1065 int priority ATTRIBUTE_UNUSED;
1067 dtors_section ();
1068 assemble_align (POINTER_SIZE);
1069 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1071 #endif
1073 /* Likewise for global constructors. */
1075 void
1076 default_stabs_asm_out_constructor (symbol, priority)
1077 rtx symbol;
1078 int priority ATTRIBUTE_UNUSED;
1080 /* Tell GNU LD that this is part of the static destructor set.
1081 This will work for any system that uses stabs, most usefully
1082 aout systems. */
1083 fprintf (asm_out_file, "%s\"___CTOR_LIST__\",22,0,0,", ASM_STABS_OP);
1084 assemble_name (asm_out_file, XSTR (symbol, 0));
1085 fputc ('\n', asm_out_file);
1088 void
1089 default_named_section_asm_out_constructor (symbol, priority)
1090 rtx symbol;
1091 int priority;
1093 const char *section = ".ctors";
1094 char buf[16];
1096 /* ??? This only works reliably with the GNU linker. */
1097 if (priority != DEFAULT_INIT_PRIORITY)
1099 sprintf (buf, ".ctors.%.5u",
1100 /* Invert the numbering so the linker puts us in the proper
1101 order; constructors are run from right to left, and the
1102 linker sorts in increasing order. */
1103 MAX_INIT_PRIORITY - priority);
1104 section = buf;
1107 named_section_flags (section, SECTION_WRITE);
1108 assemble_align (POINTER_SIZE);
1109 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1112 #ifdef CTORS_SECTION_ASM_OP
1113 void
1114 ctors_section ()
1116 if (in_section != in_ctors)
1118 in_section = in_ctors;
1119 fputs (CTORS_SECTION_ASM_OP, asm_out_file);
1120 fputc ('\n', asm_out_file);
1124 void
1125 default_ctor_section_asm_out_constructor (symbol, priority)
1126 rtx symbol;
1127 int priority ATTRIBUTE_UNUSED;
1129 ctors_section ();
1130 assemble_align (POINTER_SIZE);
1131 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1133 #endif
1135 /* CONSTANT_POOL_BEFORE_FUNCTION may be defined as an expression with
1136 a non-zero value if the constant pool should be output before the
1137 start of the function, or a zero value if the pool should output
1138 after the end of the function. The default is to put it before the
1139 start. */
1141 #ifndef CONSTANT_POOL_BEFORE_FUNCTION
1142 #define CONSTANT_POOL_BEFORE_FUNCTION 1
1143 #endif
1145 /* Output assembler code for the constant pool of a function and associated
1146 with defining the name of the function. DECL describes the function.
1147 NAME is the function's name. For the constant pool, we use the current
1148 constant pool data. */
1150 void
1151 assemble_start_function (decl, fnname)
1152 tree decl;
1153 const char *fnname;
1155 int align;
1157 /* The following code does not need preprocessing in the assembler. */
1159 app_disable ();
1161 if (CONSTANT_POOL_BEFORE_FUNCTION)
1162 output_constant_pool (fnname, decl);
1164 resolve_unique_section (decl, 0, flag_function_sections);
1165 function_section (decl);
1167 /* Tell assembler to move to target machine's alignment for functions. */
1168 align = floor_log2 (FUNCTION_BOUNDARY / BITS_PER_UNIT);
1169 if (align > 0)
1171 ASM_OUTPUT_ALIGN (asm_out_file, align);
1174 /* Handle a user-specified function alignment.
1175 Note that we still need to align to FUNCTION_BOUNDARY, as above,
1176 because ASM_OUTPUT_MAX_SKIP_ALIGN might not do any alignment at all. */
1177 if (align_functions_log > align
1178 && cfun->function_frequency != FUNCTION_FREQUENCY_UNLIKELY_EXECUTED)
1180 #ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
1181 ASM_OUTPUT_MAX_SKIP_ALIGN (asm_out_file,
1182 align_functions_log, align_functions - 1);
1183 #else
1184 ASM_OUTPUT_ALIGN (asm_out_file, align_functions_log);
1185 #endif
1188 #ifdef ASM_OUTPUT_FUNCTION_PREFIX
1189 ASM_OUTPUT_FUNCTION_PREFIX (asm_out_file, fnname);
1190 #endif
1192 (*debug_hooks->begin_function) (decl);
1194 /* Make function name accessible from other files, if appropriate. */
1196 if (TREE_PUBLIC (decl))
1198 if (! first_global_object_name)
1200 const char *p;
1201 char *name;
1203 STRIP_NAME_ENCODING (p, fnname);
1204 name = permalloc (strlen (p) + 1);
1205 strcpy (name, p);
1207 if (! DECL_WEAK (decl) && ! DECL_ONE_ONLY (decl))
1208 first_global_object_name = name;
1209 else
1210 weak_global_object_name = name;
1213 globalize_decl (decl);
1215 maybe_assemble_visibility (decl);
1218 /* Do any machine/system dependent processing of the function name */
1219 #ifdef ASM_DECLARE_FUNCTION_NAME
1220 ASM_DECLARE_FUNCTION_NAME (asm_out_file, fnname, current_function_decl);
1221 #else
1222 /* Standard thing is just output label for the function. */
1223 ASM_OUTPUT_LABEL (asm_out_file, fnname);
1224 #endif /* ASM_DECLARE_FUNCTION_NAME */
1227 /* Output assembler code associated with defining the size of the
1228 function. DECL describes the function. NAME is the function's name. */
1230 void
1231 assemble_end_function (decl, fnname)
1232 tree decl;
1233 const char *fnname;
1235 #ifdef ASM_DECLARE_FUNCTION_SIZE
1236 ASM_DECLARE_FUNCTION_SIZE (asm_out_file, fnname, decl);
1237 #endif
1238 if (! CONSTANT_POOL_BEFORE_FUNCTION)
1240 output_constant_pool (fnname, decl);
1241 function_section (decl); /* need to switch back */
1244 /* Output any constants which should appear after the function. */
1245 output_after_function_constants ();
1248 /* Assemble code to leave SIZE bytes of zeros. */
1250 void
1251 assemble_zeros (size)
1252 int size;
1254 /* Do no output if -fsyntax-only. */
1255 if (flag_syntax_only)
1256 return;
1258 #ifdef ASM_NO_SKIP_IN_TEXT
1259 /* The `space' pseudo in the text section outputs nop insns rather than 0s,
1260 so we must output 0s explicitly in the text section. */
1261 if (ASM_NO_SKIP_IN_TEXT && in_text_section ())
1263 int i;
1264 for (i = 0; i < size; i++)
1265 assemble_integer (const0_rtx, 1, BITS_PER_UNIT, 1);
1267 else
1268 #endif
1269 if (size > 0)
1270 ASM_OUTPUT_SKIP (asm_out_file, size);
1273 /* Assemble an alignment pseudo op for an ALIGN-bit boundary. */
1275 void
1276 assemble_align (align)
1277 int align;
1279 if (align > BITS_PER_UNIT)
1281 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
1285 /* Assemble a string constant with the specified C string as contents. */
1287 void
1288 assemble_string (p, size)
1289 const char *p;
1290 int size;
1292 int pos = 0;
1293 int maximum = 2000;
1295 /* If the string is very long, split it up. */
1297 while (pos < size)
1299 int thissize = size - pos;
1300 if (thissize > maximum)
1301 thissize = maximum;
1303 ASM_OUTPUT_ASCII (asm_out_file, p, thissize);
1305 pos += thissize;
1306 p += thissize;
1311 #if defined ASM_OUTPUT_ALIGNED_DECL_LOCAL
1312 #define ASM_EMIT_LOCAL(decl, name, size, rounded) \
1313 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, decl, name, size, DECL_ALIGN (decl))
1314 #else
1315 #if defined ASM_OUTPUT_ALIGNED_LOCAL
1316 #define ASM_EMIT_LOCAL(decl, name, size, rounded) \
1317 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, DECL_ALIGN (decl))
1318 #else
1319 #define ASM_EMIT_LOCAL(decl, name, size, rounded) \
1320 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded)
1321 #endif
1322 #endif
1324 #if defined ASM_OUTPUT_ALIGNED_BSS
1325 #define ASM_EMIT_BSS(decl, name, size, rounded) \
1326 ASM_OUTPUT_ALIGNED_BSS (asm_out_file, decl, name, size, DECL_ALIGN (decl))
1327 #else
1328 #if defined ASM_OUTPUT_BSS
1329 #define ASM_EMIT_BSS(decl, name, size, rounded) \
1330 ASM_OUTPUT_BSS (asm_out_file, decl, name, size, rounded)
1331 #else
1332 #undef ASM_EMIT_BSS
1333 #endif
1334 #endif
1336 #if defined ASM_OUTPUT_ALIGNED_DECL_COMMON
1337 #define ASM_EMIT_COMMON(decl, name, size, rounded) \
1338 ASM_OUTPUT_ALIGNED_DECL_COMMON (asm_out_file, decl, name, size, DECL_ALIGN (decl))
1339 #else
1340 #if defined ASM_OUTPUT_ALIGNED_COMMON
1341 #define ASM_EMIT_COMMON(decl, name, size, rounded) \
1342 ASM_OUTPUT_ALIGNED_COMMON (asm_out_file, name, size, DECL_ALIGN (decl))
1343 #else
1344 #define ASM_EMIT_COMMON(decl, name, size, rounded) \
1345 ASM_OUTPUT_COMMON (asm_out_file, name, size, rounded)
1346 #endif
1347 #endif
1349 static void
1350 asm_emit_uninitialised (decl, name, size, rounded)
1351 tree decl;
1352 const char *name;
1353 int size ATTRIBUTE_UNUSED;
1354 int rounded ATTRIBUTE_UNUSED;
1356 enum
1358 asm_dest_common,
1359 asm_dest_bss,
1360 asm_dest_local
1362 destination = asm_dest_local;
1364 if (TREE_PUBLIC (decl))
1366 #if defined ASM_EMIT_BSS
1367 if (! DECL_COMMON (decl))
1368 destination = asm_dest_bss;
1369 else
1370 #endif
1371 destination = asm_dest_common;
1374 if (destination == asm_dest_bss)
1375 globalize_decl (decl);
1376 resolve_unique_section (decl, 0, flag_data_sections);
1378 if (flag_shared_data)
1380 switch (destination)
1382 #ifdef ASM_OUTPUT_SHARED_BSS
1383 case asm_dest_bss:
1384 ASM_OUTPUT_SHARED_BSS (asm_out_file, decl, name, size, rounded);
1385 return;
1386 #endif
1387 #ifdef ASM_OUTPUT_SHARED_COMMON
1388 case asm_dest_common:
1389 ASM_OUTPUT_SHARED_COMMON (asm_out_file, name, size, rounded);
1390 return;
1391 #endif
1392 #ifdef ASM_OUTPUT_SHARED_LOCAL
1393 case asm_dest_local:
1394 ASM_OUTPUT_SHARED_LOCAL (asm_out_file, name, size, rounded);
1395 return;
1396 #endif
1397 default:
1398 break;
1402 switch (destination)
1404 #ifdef ASM_EMIT_BSS
1405 case asm_dest_bss:
1406 ASM_EMIT_BSS (decl, name, size, rounded);
1407 break;
1408 #endif
1409 case asm_dest_common:
1410 ASM_EMIT_COMMON (decl, name, size, rounded);
1411 break;
1412 case asm_dest_local:
1413 ASM_EMIT_LOCAL (decl, name, size, rounded);
1414 break;
1415 default:
1416 abort ();
1419 return;
1422 /* Assemble everything that is needed for a variable or function declaration.
1423 Not used for automatic variables, and not used for function definitions.
1424 Should not be called for variables of incomplete structure type.
1426 TOP_LEVEL is nonzero if this variable has file scope.
1427 AT_END is nonzero if this is the special handling, at end of compilation,
1428 to define things that have had only tentative definitions.
1429 DONT_OUTPUT_DATA if nonzero means don't actually output the
1430 initial value (that will be done by the caller). */
1432 void
1433 assemble_variable (decl, top_level, at_end, dont_output_data)
1434 tree decl;
1435 int top_level ATTRIBUTE_UNUSED;
1436 int at_end ATTRIBUTE_UNUSED;
1437 int dont_output_data;
1439 const char *name;
1440 unsigned int align;
1441 int reloc = 0;
1442 rtx decl_rtl;
1444 last_assemble_variable_decl = 0;
1446 /* Normally no need to say anything here for external references,
1447 since assemble_external is called by the language-specific code
1448 when a declaration is first seen. */
1450 if (DECL_EXTERNAL (decl))
1451 return;
1453 /* Output no assembler code for a function declaration.
1454 Only definitions of functions output anything. */
1456 if (TREE_CODE (decl) == FUNCTION_DECL)
1457 return;
1459 /* Do nothing for global register variables. */
1460 if (DECL_RTL_SET_P (decl) && GET_CODE (DECL_RTL (decl)) == REG)
1462 TREE_ASM_WRITTEN (decl) = 1;
1463 return;
1466 /* If type was incomplete when the variable was declared,
1467 see if it is complete now. */
1469 if (DECL_SIZE (decl) == 0)
1470 layout_decl (decl, 0);
1472 /* Still incomplete => don't allocate it; treat the tentative defn
1473 (which is what it must have been) as an `extern' reference. */
1475 if (!dont_output_data && DECL_SIZE (decl) == 0)
1477 error_with_file_and_line (DECL_SOURCE_FILE (decl),
1478 DECL_SOURCE_LINE (decl),
1479 "storage size of `%s' isn't known",
1480 IDENTIFIER_POINTER (DECL_NAME (decl)));
1481 TREE_ASM_WRITTEN (decl) = 1;
1482 return;
1485 /* The first declaration of a variable that comes through this function
1486 decides whether it is global (in C, has external linkage)
1487 or local (in C, has internal linkage). So do nothing more
1488 if this function has already run. */
1490 if (TREE_ASM_WRITTEN (decl))
1491 return;
1493 /* Make sure ENCODE_SECTION_INFO is invoked before we set ASM_WRITTEN. */
1494 decl_rtl = DECL_RTL (decl);
1496 TREE_ASM_WRITTEN (decl) = 1;
1498 /* Do no output if -fsyntax-only. */
1499 if (flag_syntax_only)
1500 return;
1502 app_disable ();
1504 if (! dont_output_data
1505 && ! host_integerp (DECL_SIZE_UNIT (decl), 1))
1507 error_with_decl (decl, "size of variable `%s' is too large");
1508 return;
1511 name = XSTR (XEXP (decl_rtl, 0), 0);
1512 if (TREE_PUBLIC (decl) && DECL_NAME (decl)
1513 && ! first_global_object_name
1514 && ! (DECL_COMMON (decl) && (DECL_INITIAL (decl) == 0
1515 || DECL_INITIAL (decl) == error_mark_node))
1516 && ! DECL_WEAK (decl)
1517 && ! DECL_ONE_ONLY (decl))
1519 const char *p;
1520 char *xname;
1522 STRIP_NAME_ENCODING (p, name);
1523 xname = permalloc (strlen (p) + 1);
1524 strcpy (xname, p);
1525 first_global_object_name = xname;
1528 /* Compute the alignment of this data. */
1530 align = DECL_ALIGN (decl);
1532 /* In the case for initialing an array whose length isn't specified,
1533 where we have not yet been able to do the layout,
1534 figure out the proper alignment now. */
1535 if (dont_output_data && DECL_SIZE (decl) == 0
1536 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
1537 align = MAX (align, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (decl))));
1539 /* Some object file formats have a maximum alignment which they support.
1540 In particular, a.out format supports a maximum alignment of 4. */
1541 #ifndef MAX_OFILE_ALIGNMENT
1542 #define MAX_OFILE_ALIGNMENT BIGGEST_ALIGNMENT
1543 #endif
1544 if (align > MAX_OFILE_ALIGNMENT)
1546 warning_with_decl (decl,
1547 "alignment of `%s' is greater than maximum object file alignment. Using %d",
1548 MAX_OFILE_ALIGNMENT/BITS_PER_UNIT);
1549 align = MAX_OFILE_ALIGNMENT;
1552 /* On some machines, it is good to increase alignment sometimes. */
1553 if (! DECL_USER_ALIGN (decl))
1555 #ifdef DATA_ALIGNMENT
1556 align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
1557 #endif
1558 #ifdef CONSTANT_ALIGNMENT
1559 if (DECL_INITIAL (decl) != 0 && DECL_INITIAL (decl) != error_mark_node)
1560 align = CONSTANT_ALIGNMENT (DECL_INITIAL (decl), align);
1561 #endif
1564 /* Reset the alignment in case we have made it tighter, so we can benefit
1565 from it in get_pointer_alignment. */
1566 DECL_ALIGN (decl) = align;
1567 set_mem_align (decl_rtl, align);
1569 if (TREE_PUBLIC (decl))
1570 maybe_assemble_visibility (decl);
1572 /* Output any data that we will need to use the address of. */
1573 if (DECL_INITIAL (decl) == error_mark_node)
1574 reloc = contains_pointers_p (TREE_TYPE (decl)) ? 3 : 0;
1575 else if (DECL_INITIAL (decl))
1576 reloc = output_addressed_constants (DECL_INITIAL (decl));
1577 resolve_unique_section (decl, reloc, flag_data_sections);
1579 /* Handle uninitialized definitions. */
1581 if ((DECL_INITIAL (decl) == 0 || DECL_INITIAL (decl) == error_mark_node
1582 #if defined ASM_EMIT_BSS
1583 || (flag_zero_initialized_in_bss
1584 && initializer_zerop (DECL_INITIAL (decl)))
1585 #endif
1587 /* If the target can't output uninitialized but not common global data
1588 in .bss, then we have to use .data. */
1589 #if ! defined ASM_EMIT_BSS
1590 && DECL_COMMON (decl)
1591 #endif
1592 && DECL_SECTION_NAME (decl) == NULL_TREE
1593 && ! dont_output_data)
1595 unsigned HOST_WIDE_INT size = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
1596 unsigned HOST_WIDE_INT rounded = size;
1598 /* Don't allocate zero bytes of common,
1599 since that means "undefined external" in the linker. */
1600 if (size == 0)
1601 rounded = 1;
1603 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
1604 so that each uninitialized object starts on such a boundary. */
1605 rounded += (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1;
1606 rounded = (rounded / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
1607 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
1609 /* Don't continue this line--convex cc version 4.1 would lose. */
1610 #if !defined(ASM_OUTPUT_ALIGNED_COMMON) && !defined(ASM_OUTPUT_ALIGNED_DECL_COMMON) && !defined(ASM_OUTPUT_ALIGNED_BSS)
1611 if ((unsigned HOST_WIDE_INT) DECL_ALIGN (decl) / BITS_PER_UNIT > rounded)
1612 warning_with_decl
1613 (decl, "requested alignment for %s is greater than implemented alignment of %d",rounded);
1614 #endif
1616 asm_emit_uninitialised (decl, name, size, rounded);
1618 return;
1621 /* Handle initialized definitions.
1622 Also handle uninitialized global definitions if -fno-common and the
1623 target doesn't support ASM_OUTPUT_BSS. */
1625 /* First make the assembler name(s) global if appropriate. */
1626 if (TREE_PUBLIC (decl) && DECL_NAME (decl))
1627 globalize_decl (decl);
1629 /* Switch to the appropriate section. */
1630 variable_section (decl, reloc);
1632 /* dbxout.c needs to know this. */
1633 if (in_text_section ())
1634 DECL_IN_TEXT_SECTION (decl) = 1;
1636 /* Output the alignment of this data. */
1637 if (align > BITS_PER_UNIT)
1639 ASM_OUTPUT_ALIGN (asm_out_file,
1640 floor_log2 (DECL_ALIGN (decl) / BITS_PER_UNIT));
1643 /* Do any machine/system dependent processing of the object. */
1644 #ifdef ASM_DECLARE_OBJECT_NAME
1645 last_assemble_variable_decl = decl;
1646 ASM_DECLARE_OBJECT_NAME (asm_out_file, name, decl);
1647 #else
1648 /* Standard thing is just output label for the object. */
1649 ASM_OUTPUT_LABEL (asm_out_file, name);
1650 #endif /* ASM_DECLARE_OBJECT_NAME */
1652 if (!dont_output_data)
1654 if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node)
1655 /* Output the actual data. */
1656 output_constant (DECL_INITIAL (decl),
1657 tree_low_cst (DECL_SIZE_UNIT (decl), 1),
1658 align);
1659 else
1660 /* Leave space for it. */
1661 assemble_zeros (tree_low_cst (DECL_SIZE_UNIT (decl), 1));
1665 /* Return 1 if type TYPE contains any pointers. */
1667 static int
1668 contains_pointers_p (type)
1669 tree type;
1671 switch (TREE_CODE (type))
1673 case POINTER_TYPE:
1674 case REFERENCE_TYPE:
1675 /* I'm not sure whether OFFSET_TYPE needs this treatment,
1676 so I'll play safe and return 1. */
1677 case OFFSET_TYPE:
1678 return 1;
1680 case RECORD_TYPE:
1681 case UNION_TYPE:
1682 case QUAL_UNION_TYPE:
1684 tree fields;
1685 /* For a type that has fields, see if the fields have pointers. */
1686 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
1687 if (TREE_CODE (fields) == FIELD_DECL
1688 && contains_pointers_p (TREE_TYPE (fields)))
1689 return 1;
1690 return 0;
1693 case ARRAY_TYPE:
1694 /* An array type contains pointers if its element type does. */
1695 return contains_pointers_p (TREE_TYPE (type));
1697 default:
1698 return 0;
1702 /* Output something to declare an external symbol to the assembler.
1703 (Most assemblers don't need this, so we normally output nothing.)
1704 Do nothing if DECL is not external. */
1706 void
1707 assemble_external (decl)
1708 tree decl ATTRIBUTE_UNUSED;
1710 /* Because most platforms do not define ASM_OUTPUT_EXTERNAL, the
1711 main body of this code is only rarely exercised. To provide some
1712 testing, on all platforms, we make sure that the ASM_OUT_FILE is
1713 open. If it's not, we should not be calling this function. */
1714 if (!asm_out_file)
1715 abort ();
1717 #ifdef ASM_OUTPUT_EXTERNAL
1718 if (DECL_P (decl) && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl))
1720 rtx rtl = DECL_RTL (decl);
1722 if (GET_CODE (rtl) == MEM && GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF
1723 && ! SYMBOL_REF_USED (XEXP (rtl, 0)))
1725 /* Some systems do require some output. */
1726 SYMBOL_REF_USED (XEXP (rtl, 0)) = 1;
1727 ASM_OUTPUT_EXTERNAL (asm_out_file, decl, XSTR (XEXP (rtl, 0), 0));
1730 #endif
1733 /* Similar, for calling a library function FUN. */
1735 void
1736 assemble_external_libcall (fun)
1737 rtx fun ATTRIBUTE_UNUSED;
1739 #ifdef ASM_OUTPUT_EXTERNAL_LIBCALL
1740 /* Declare library function name external when first used, if nec. */
1741 if (! SYMBOL_REF_USED (fun))
1743 SYMBOL_REF_USED (fun) = 1;
1744 ASM_OUTPUT_EXTERNAL_LIBCALL (asm_out_file, fun);
1746 #endif
1749 /* Declare the label NAME global. */
1751 void
1752 assemble_global (name)
1753 const char *name ATTRIBUTE_UNUSED;
1755 ASM_GLOBALIZE_LABEL (asm_out_file, name);
1758 /* Assemble a label named NAME. */
1760 void
1761 assemble_label (name)
1762 const char *name;
1764 ASM_OUTPUT_LABEL (asm_out_file, name);
1767 /* Output to FILE a reference to the assembler name of a C-level name NAME.
1768 If NAME starts with a *, the rest of NAME is output verbatim.
1769 Otherwise NAME is transformed in an implementation-defined way
1770 (usually by the addition of an underscore).
1771 Many macros in the tm file are defined to call this function. */
1773 void
1774 assemble_name (file, name)
1775 FILE *file;
1776 const char *name;
1778 const char *real_name;
1779 tree id;
1781 STRIP_NAME_ENCODING (real_name, name);
1783 id = maybe_get_identifier (real_name);
1784 if (id)
1785 TREE_SYMBOL_REFERENCED (id) = 1;
1787 if (name[0] == '*')
1788 fputs (&name[1], file);
1789 else
1790 ASM_OUTPUT_LABELREF (file, name);
1793 /* Allocate SIZE bytes writable static space with a gensym name
1794 and return an RTX to refer to its address. */
1797 assemble_static_space (size)
1798 int size;
1800 char name[12];
1801 const char *namestring;
1802 rtx x;
1804 #if 0
1805 if (flag_shared_data)
1806 data_section ();
1807 #endif
1809 ASM_GENERATE_INTERNAL_LABEL (name, "LF", const_labelno);
1810 ++const_labelno;
1811 namestring = ggc_strdup (name);
1813 x = gen_rtx_SYMBOL_REF (Pmode, namestring);
1815 #ifdef ASM_OUTPUT_ALIGNED_DECL_LOCAL
1816 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, NULL_TREE, name, size,
1817 BIGGEST_ALIGNMENT);
1818 #else
1819 #ifdef ASM_OUTPUT_ALIGNED_LOCAL
1820 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, BIGGEST_ALIGNMENT);
1821 #else
1823 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
1824 so that each uninitialized object starts on such a boundary. */
1825 /* Variable `rounded' might or might not be used in ASM_OUTPUT_LOCAL. */
1826 int rounded ATTRIBUTE_UNUSED
1827 = ((size + (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1)
1828 / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
1829 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
1830 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
1832 #endif
1833 #endif
1834 return x;
1837 /* Assemble the static constant template for function entry trampolines.
1838 This is done at most once per compilation.
1839 Returns an RTX for the address of the template. */
1841 #ifdef TRAMPOLINE_TEMPLATE
1843 assemble_trampoline_template ()
1845 char label[256];
1846 const char *name;
1847 int align;
1849 /* By default, put trampoline templates in read-only data section. */
1851 #ifdef TRAMPOLINE_SECTION
1852 TRAMPOLINE_SECTION ();
1853 #else
1854 readonly_data_section ();
1855 #endif
1857 /* Write the assembler code to define one. */
1858 align = floor_log2 (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
1859 if (align > 0)
1861 ASM_OUTPUT_ALIGN (asm_out_file, align);
1864 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, "LTRAMP", 0);
1865 TRAMPOLINE_TEMPLATE (asm_out_file);
1867 /* Record the rtl to refer to it. */
1868 ASM_GENERATE_INTERNAL_LABEL (label, "LTRAMP", 0);
1869 name = ggc_strdup (label);
1870 return gen_rtx_SYMBOL_REF (Pmode, name);
1872 #endif
1874 /* A and B are either alignments or offsets. Return the minimum alignment
1875 that may be assumed after adding the two together. */
1877 static inline unsigned
1878 min_align (a, b)
1879 unsigned int a, b;
1881 return (a | b) & -(a | b);
1884 /* Return the assembler directive for creating a given kind of integer
1885 object. SIZE is the number of bytes in the object and ALIGNED_P
1886 indicates whether it is known to be aligned. Return NULL if the
1887 assembly dialect has no such directive.
1889 The returned string should be printed at the start of a new line and
1890 be followed immediately by the object's initial value. */
1892 const char *
1893 integer_asm_op (size, aligned_p)
1894 int size;
1895 int aligned_p;
1897 struct asm_int_op *ops;
1899 if (aligned_p)
1900 ops = &targetm.asm_out.aligned_op;
1901 else
1902 ops = &targetm.asm_out.unaligned_op;
1904 switch (size)
1906 case 1:
1907 return targetm.asm_out.byte_op;
1908 case 2:
1909 return ops->hi;
1910 case 4:
1911 return ops->si;
1912 case 8:
1913 return ops->di;
1914 case 16:
1915 return ops->ti;
1916 default:
1917 return NULL;
1921 /* Use directive OP to assemble an integer object X. Print OP at the
1922 start of the line, followed immediately by the value of X. */
1924 void
1925 assemble_integer_with_op (op, x)
1926 const char *op;
1927 rtx x;
1929 fputs (op, asm_out_file);
1930 output_addr_const (asm_out_file, x);
1931 fputc ('\n', asm_out_file);
1934 /* The default implementation of the asm_out.integer target hook. */
1936 bool
1937 default_assemble_integer (x, size, aligned_p)
1938 rtx x ATTRIBUTE_UNUSED;
1939 unsigned int size ATTRIBUTE_UNUSED;
1940 int aligned_p ATTRIBUTE_UNUSED;
1942 const char *op = integer_asm_op (size, aligned_p);
1943 return op && (assemble_integer_with_op (op, x), true);
1946 /* Assemble the integer constant X into an object of SIZE bytes. ALIGN is
1947 the alignment of the integer in bits. Return 1 if we were able to output
1948 the constant, otherwise 0. If FORCE is non-zero, abort if we can't output
1949 the constant. */
1951 bool
1952 assemble_integer (x, size, align, force)
1953 rtx x;
1954 unsigned int size;
1955 unsigned int align;
1956 int force;
1958 int aligned_p;
1960 aligned_p = (align >= MIN (size * BITS_PER_UNIT, BIGGEST_ALIGNMENT));
1962 /* See if the target hook can handle this kind of object. */
1963 if ((*targetm.asm_out.integer) (x, size, aligned_p))
1964 return true;
1966 /* If the object is a multi-byte one, try splitting it up. Split
1967 it into words it if is multi-word, otherwise split it into bytes. */
1968 if (size > 1)
1970 enum machine_mode omode, imode;
1971 unsigned int subalign;
1972 unsigned int subsize, i;
1974 subsize = size > UNITS_PER_WORD? UNITS_PER_WORD : 1;
1975 subalign = MIN (align, subsize * BITS_PER_UNIT);
1976 omode = mode_for_size (subsize * BITS_PER_UNIT, MODE_INT, 0);
1977 imode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
1979 for (i = 0; i < size; i += subsize)
1981 rtx partial = simplify_subreg (omode, x, imode, i);
1982 if (!partial || !assemble_integer (partial, subsize, subalign, 0))
1983 break;
1985 if (i == size)
1986 return true;
1988 /* If we've printed some of it, but not all of it, there's no going
1989 back now. */
1990 if (i > 0)
1991 abort ();
1994 if (force)
1995 abort ();
1997 return false;
2000 void
2001 assemble_real (d, mode, align)
2002 REAL_VALUE_TYPE d;
2003 enum machine_mode mode;
2004 unsigned int align;
2006 long data[4];
2007 long l;
2008 unsigned int nalign = min_align (align, 32);
2010 switch (BITS_PER_UNIT)
2012 case 8:
2013 switch (mode)
2015 case SFmode:
2016 REAL_VALUE_TO_TARGET_SINGLE (d, l);
2017 assemble_integer (GEN_INT (l), 4, align, 1);
2018 break;
2019 case DFmode:
2020 REAL_VALUE_TO_TARGET_DOUBLE (d, data);
2021 assemble_integer (GEN_INT (data[0]), 4, align, 1);
2022 assemble_integer (GEN_INT (data[1]), 4, nalign, 1);
2023 break;
2024 case XFmode:
2025 REAL_VALUE_TO_TARGET_LONG_DOUBLE (d, data);
2026 assemble_integer (GEN_INT (data[0]), 4, align, 1);
2027 assemble_integer (GEN_INT (data[1]), 4, nalign, 1);
2028 assemble_integer (GEN_INT (data[2]), 4, nalign, 1);
2029 break;
2030 case TFmode:
2031 REAL_VALUE_TO_TARGET_LONG_DOUBLE (d, data);
2032 assemble_integer (GEN_INT (data[0]), 4, align, 1);
2033 assemble_integer (GEN_INT (data[1]), 4, nalign, 1);
2034 assemble_integer (GEN_INT (data[2]), 4, nalign, 1);
2035 assemble_integer (GEN_INT (data[3]), 4, nalign, 1);
2036 break;
2037 default:
2038 abort ();
2040 break;
2042 case 16:
2043 switch (mode)
2045 case HFmode:
2046 REAL_VALUE_TO_TARGET_SINGLE (d, l);
2047 assemble_integer (GEN_INT (l), 2, align, 1);
2048 break;
2049 case TQFmode:
2050 REAL_VALUE_TO_TARGET_DOUBLE (d, data);
2051 assemble_integer (GEN_INT (data[0]), 2, align, 1);
2052 assemble_integer (GEN_INT (data[1]), 1, nalign, 1);
2053 break;
2054 default:
2055 abort ();
2057 break;
2059 case 32:
2060 switch (mode)
2062 case QFmode:
2063 REAL_VALUE_TO_TARGET_SINGLE (d, l);
2064 assemble_integer (GEN_INT (l), 1, align, 1);
2065 break;
2066 case HFmode:
2067 REAL_VALUE_TO_TARGET_DOUBLE (d, data);
2068 assemble_integer (GEN_INT (data[0]), 1, align, 1);
2069 assemble_integer (GEN_INT (data[1]), 1, nalign, 1);
2070 break;
2071 default:
2072 abort ();
2074 break;
2076 default:
2077 abort ();
2081 /* Given an expression EXP with a constant value,
2082 reduce it to the sum of an assembler symbol and an integer.
2083 Store them both in the structure *VALUE.
2084 Abort if EXP does not reduce. */
2086 struct addr_const
2088 rtx base;
2089 HOST_WIDE_INT offset;
2092 static void
2093 decode_addr_const (exp, value)
2094 tree exp;
2095 struct addr_const *value;
2097 tree target = TREE_OPERAND (exp, 0);
2098 int offset = 0;
2099 rtx x;
2101 while (1)
2103 if (TREE_CODE (target) == COMPONENT_REF
2104 && host_integerp (byte_position (TREE_OPERAND (target, 1)), 0))
2107 offset += int_byte_position (TREE_OPERAND (target, 1));
2108 target = TREE_OPERAND (target, 0);
2110 else if (TREE_CODE (target) == ARRAY_REF
2111 || TREE_CODE (target) == ARRAY_RANGE_REF)
2113 offset += (tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (target)), 1)
2114 * tree_low_cst (TREE_OPERAND (target, 1), 0));
2115 target = TREE_OPERAND (target, 0);
2117 else
2118 break;
2121 switch (TREE_CODE (target))
2123 case VAR_DECL:
2124 case FUNCTION_DECL:
2125 x = DECL_RTL (target);
2126 break;
2128 case LABEL_DECL:
2129 x = gen_rtx_MEM (FUNCTION_MODE,
2130 gen_rtx_LABEL_REF (VOIDmode,
2131 label_rtx (TREE_OPERAND (exp, 0))));
2132 break;
2134 case REAL_CST:
2135 case STRING_CST:
2136 case COMPLEX_CST:
2137 case CONSTRUCTOR:
2138 case INTEGER_CST:
2139 /* This constant should have been output already, but we can't simply
2140 use TREE_CST_RTL since INTEGER_CST doesn't have one. */
2141 x = output_constant_def (target, 1);
2142 break;
2144 default:
2145 abort ();
2148 if (GET_CODE (x) != MEM)
2149 abort ();
2150 x = XEXP (x, 0);
2152 value->base = x;
2153 value->offset = offset;
2156 /* We do RTX_UNSPEC + XINT (blah), so nothing can go after RTX_UNSPEC. */
2157 enum kind { RTX_UNKNOWN, RTX_DOUBLE, RTX_INT, RTX_VECTOR, RTX_UNSPEC };
2158 struct rtx_const
2160 ENUM_BITFIELD(kind) kind : 16;
2161 ENUM_BITFIELD(machine_mode) mode : 16;
2162 union {
2163 REAL_VALUE_TYPE du;
2164 struct addr_const addr;
2165 struct {HOST_WIDE_INT high, low;} di;
2167 /* The max vector size we have is 8 wide. This should be enough. */
2168 HOST_WIDE_INT veclo[16];
2169 HOST_WIDE_INT vechi[16];
2170 } un;
2173 /* Uniquize all constants that appear in memory.
2174 Each constant in memory thus far output is recorded
2175 in `const_hash_table' with a `struct constant_descriptor'
2176 that contains a polish representation of the value of
2177 the constant.
2179 We cannot store the trees in the hash table
2180 because the trees may be temporary. */
2182 struct constant_descriptor
2184 struct constant_descriptor *next;
2185 const char *label;
2186 rtx rtl;
2187 /* Make sure the data is reasonably aligned. */
2188 union
2190 unsigned char contents[1];
2191 #ifdef HAVE_LONG_DOUBLE
2192 long double d;
2193 #else
2194 double d;
2195 #endif
2196 } u;
2199 #define HASHBITS 30
2200 #define MAX_HASH_TABLE 1009
2201 static struct constant_descriptor *const_hash_table[MAX_HASH_TABLE];
2203 /* We maintain a hash table of STRING_CST values. Unless we are asked to force
2204 out a string constant, we defer output of the constants until we know
2205 they are actually used. This will be if something takes its address or if
2206 there is a usage of the string in the RTL of a function. */
2208 #define STRHASH(x) ((hashval_t) ((long) (x) >> 3))
2210 struct deferred_string
2212 const char *label;
2213 tree exp;
2214 int labelno;
2217 static htab_t const_str_htab;
2219 /* Mark a const_hash_table descriptor for GC. */
2221 static void
2222 mark_const_hash_entry (ptr)
2223 void *ptr;
2225 struct constant_descriptor *desc = * (struct constant_descriptor **) ptr;
2227 while (desc)
2229 ggc_mark_rtx (desc->rtl);
2230 desc = desc->next;
2234 /* Mark the hash-table element X (which is really a pointer to an
2235 struct deferred_string *). */
2237 static int
2238 mark_const_str_htab_1 (x, data)
2239 void **x;
2240 void *data ATTRIBUTE_UNUSED;
2242 ggc_mark_tree (((struct deferred_string *) *x)->exp);
2243 return 1;
2246 /* Mark a const_str_htab for GC. */
2248 static void
2249 mark_const_str_htab (htab)
2250 void *htab;
2252 htab_traverse (*((htab_t *) htab), mark_const_str_htab_1, NULL);
2255 /* Returns a hash code for X (which is a really a
2256 struct deferred_string *). */
2258 static hashval_t
2259 const_str_htab_hash (x)
2260 const void *x;
2262 return STRHASH (((const struct deferred_string *) x)->label);
2265 /* Returns non-zero if the value represented by X (which is really a
2266 struct deferred_string *) is the same as that given by Y
2267 (which is really a char *). */
2269 static int
2270 const_str_htab_eq (x, y)
2271 const void *x;
2272 const void *y;
2274 return (((const struct deferred_string *) x)->label == (const char *) y);
2277 /* Delete the hash table entry dfsp. */
2279 static void
2280 const_str_htab_del (dfsp)
2281 void *dfsp;
2283 free (dfsp);
2286 /* Compute a hash code for a constant expression. */
2288 static int
2289 const_hash (exp)
2290 tree exp;
2292 const char *p;
2293 int len, hi, i;
2294 enum tree_code code = TREE_CODE (exp);
2296 /* Either set P and LEN to the address and len of something to hash and
2297 exit the switch or return a value. */
2299 switch (code)
2301 case INTEGER_CST:
2302 p = (char *) &TREE_INT_CST (exp);
2303 len = sizeof TREE_INT_CST (exp);
2304 break;
2306 case REAL_CST:
2307 p = (char *) &TREE_REAL_CST (exp);
2308 len = sizeof TREE_REAL_CST (exp);
2309 break;
2311 case STRING_CST:
2312 p = TREE_STRING_POINTER (exp);
2313 len = TREE_STRING_LENGTH (exp);
2314 break;
2316 case COMPLEX_CST:
2317 return (const_hash (TREE_REALPART (exp)) * 5
2318 + const_hash (TREE_IMAGPART (exp)));
2320 case CONSTRUCTOR:
2321 if (TREE_CODE (TREE_TYPE (exp)) == SET_TYPE)
2323 char *tmp;
2325 len = int_size_in_bytes (TREE_TYPE (exp));
2326 tmp = (char *) alloca (len);
2327 get_set_constructor_bytes (exp, (unsigned char *) tmp, len);
2328 p = tmp;
2329 break;
2331 else
2333 tree link;
2335 /* For record type, include the type in the hashing.
2336 We do not do so for array types
2337 because (1) the sizes of the elements are sufficient
2338 and (2) distinct array types can have the same constructor.
2339 Instead, we include the array size because the constructor could
2340 be shorter. */
2341 if (TREE_CODE (TREE_TYPE (exp)) == RECORD_TYPE)
2342 hi = ((unsigned long) TREE_TYPE (exp) & ((1 << HASHBITS) - 1))
2343 % MAX_HASH_TABLE;
2344 else
2345 hi = ((5 + int_size_in_bytes (TREE_TYPE (exp)))
2346 & ((1 << HASHBITS) - 1)) % MAX_HASH_TABLE;
2348 for (link = CONSTRUCTOR_ELTS (exp); link; link = TREE_CHAIN (link))
2349 if (TREE_VALUE (link))
2351 = (hi * 603 + const_hash (TREE_VALUE (link))) % MAX_HASH_TABLE;
2353 return hi;
2356 case ADDR_EXPR:
2358 struct addr_const value;
2360 decode_addr_const (exp, &value);
2361 if (GET_CODE (value.base) == SYMBOL_REF)
2363 /* Don't hash the address of the SYMBOL_REF;
2364 only use the offset and the symbol name. */
2365 hi = value.offset;
2366 p = XSTR (value.base, 0);
2367 for (i = 0; p[i] != 0; i++)
2368 hi = ((hi * 613) + (unsigned) (p[i]));
2370 else if (GET_CODE (value.base) == LABEL_REF)
2371 hi = value.offset + CODE_LABEL_NUMBER (XEXP (value.base, 0)) * 13;
2372 else
2373 abort ();
2375 hi &= (1 << HASHBITS) - 1;
2376 hi %= MAX_HASH_TABLE;
2378 return hi;
2380 case PLUS_EXPR:
2381 case MINUS_EXPR:
2382 return (const_hash (TREE_OPERAND (exp, 0)) * 9
2383 + const_hash (TREE_OPERAND (exp, 1)));
2385 case NOP_EXPR:
2386 case CONVERT_EXPR:
2387 case NON_LVALUE_EXPR:
2388 return const_hash (TREE_OPERAND (exp, 0)) * 7 + 2;
2390 default:
2391 /* A language specific constant. Just hash the code. */
2392 return (int) code % MAX_HASH_TABLE;
2395 /* Compute hashing function */
2396 hi = len;
2397 for (i = 0; i < len; i++)
2398 hi = ((hi * 613) + (unsigned) (p[i]));
2400 hi &= (1 << HASHBITS) - 1;
2401 hi %= MAX_HASH_TABLE;
2402 return hi;
2405 /* Compare a constant expression EXP with a constant-descriptor DESC.
2406 Return 1 if DESC describes a constant with the same value as EXP. */
2408 static int
2409 compare_constant (exp, desc)
2410 tree exp;
2411 struct constant_descriptor *desc;
2413 return 0 != compare_constant_1 (exp, desc->u.contents);
2416 /* Compare constant expression EXP with a substring P of a constant descriptor.
2417 If they match, return a pointer to the end of the substring matched.
2418 If they do not match, return 0.
2420 Since descriptors are written in polish prefix notation,
2421 this function can be used recursively to test one operand of EXP
2422 against a subdescriptor, and if it succeeds it returns the
2423 address of the subdescriptor for the next operand. */
2425 static const unsigned char *
2426 compare_constant_1 (exp, p)
2427 tree exp;
2428 const unsigned char *p;
2430 const unsigned char *strp;
2431 int len;
2432 enum tree_code code = TREE_CODE (exp);
2434 if (code != (enum tree_code) *p++)
2435 return 0;
2437 /* Either set STRP, P and LEN to pointers and length to compare and exit the
2438 switch, or return the result of the comparison. */
2440 switch (code)
2442 case INTEGER_CST:
2443 /* Integer constants are the same only if the same width of type. */
2444 if (*p++ != TYPE_PRECISION (TREE_TYPE (exp)))
2445 return 0;
2447 strp = (unsigned char *) &TREE_INT_CST (exp);
2448 len = sizeof TREE_INT_CST (exp);
2449 break;
2451 case REAL_CST:
2452 /* Real constants are the same only if the same width of type. */
2453 if (*p++ != TYPE_PRECISION (TREE_TYPE (exp)))
2454 return 0;
2456 strp = (unsigned char *) &TREE_REAL_CST (exp);
2457 len = sizeof TREE_REAL_CST (exp);
2458 break;
2460 case STRING_CST:
2461 if (flag_writable_strings)
2462 return 0;
2464 if ((enum machine_mode) *p++ != TYPE_MODE (TREE_TYPE (exp)))
2465 return 0;
2467 strp = (const unsigned char *) TREE_STRING_POINTER (exp);
2468 len = TREE_STRING_LENGTH (exp);
2469 if (memcmp ((char *) &TREE_STRING_LENGTH (exp), p,
2470 sizeof TREE_STRING_LENGTH (exp)))
2471 return 0;
2473 p += sizeof TREE_STRING_LENGTH (exp);
2474 break;
2476 case COMPLEX_CST:
2477 p = compare_constant_1 (TREE_REALPART (exp), p);
2478 if (p == 0)
2479 return 0;
2481 return compare_constant_1 (TREE_IMAGPART (exp), p);
2483 case CONSTRUCTOR:
2484 if (TREE_CODE (TREE_TYPE (exp)) == SET_TYPE)
2486 int xlen = len = int_size_in_bytes (TREE_TYPE (exp));
2487 unsigned char *tmp = (unsigned char *) alloca (len);
2489 get_set_constructor_bytes (exp, tmp, len);
2490 strp = (unsigned char *) tmp;
2491 if (memcmp ((char *) &xlen, p, sizeof xlen))
2492 return 0;
2494 p += sizeof xlen;
2495 break;
2497 else
2499 tree link;
2500 int length = list_length (CONSTRUCTOR_ELTS (exp));
2501 tree type;
2502 enum machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
2503 int have_purpose = 0;
2505 for (link = CONSTRUCTOR_ELTS (exp); link; link = TREE_CHAIN (link))
2506 if (TREE_PURPOSE (link))
2507 have_purpose = 1;
2509 if (memcmp ((char *) &length, p, sizeof length))
2510 return 0;
2512 p += sizeof length;
2514 /* For record constructors, insist that the types match.
2515 For arrays, just verify both constructors are for arrays.
2516 Then insist that either both or none have any TREE_PURPOSE
2517 values. */
2518 if (TREE_CODE (TREE_TYPE (exp)) == RECORD_TYPE)
2519 type = TREE_TYPE (exp);
2520 else
2521 type = 0;
2523 if (memcmp ((char *) &type, p, sizeof type))
2524 return 0;
2526 if (TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE)
2528 if (memcmp ((char *) &mode, p, sizeof mode))
2529 return 0;
2531 p += sizeof mode;
2534 p += sizeof type;
2536 if (memcmp ((char *) &have_purpose, p, sizeof have_purpose))
2537 return 0;
2539 p += sizeof have_purpose;
2541 /* For arrays, insist that the size in bytes match. */
2542 if (TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE)
2544 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
2546 if (memcmp ((char *) &size, p, sizeof size))
2547 return 0;
2549 p += sizeof size;
2552 for (link = CONSTRUCTOR_ELTS (exp); link; link = TREE_CHAIN (link))
2554 if (TREE_VALUE (link))
2556 if ((p = compare_constant_1 (TREE_VALUE (link), p)) == 0)
2557 return 0;
2559 else
2561 tree zero = 0;
2563 if (memcmp ((char *) &zero, p, sizeof zero))
2564 return 0;
2566 p += sizeof zero;
2569 if (TREE_PURPOSE (link)
2570 && TREE_CODE (TREE_PURPOSE (link)) == FIELD_DECL)
2572 if (memcmp ((char *) &TREE_PURPOSE (link), p,
2573 sizeof TREE_PURPOSE (link)))
2574 return 0;
2576 p += sizeof TREE_PURPOSE (link);
2578 else if (TREE_PURPOSE (link))
2580 if ((p = compare_constant_1 (TREE_PURPOSE (link), p)) == 0)
2581 return 0;
2583 else if (have_purpose)
2585 int zero = 0;
2587 if (memcmp ((char *) &zero, p, sizeof zero))
2588 return 0;
2590 p += sizeof zero;
2594 return p;
2597 case ADDR_EXPR:
2599 struct addr_const value;
2601 decode_addr_const (exp, &value);
2602 strp = (unsigned char *) &value.offset;
2603 len = sizeof value.offset;
2604 /* Compare the offset. */
2605 while (--len >= 0)
2606 if (*p++ != *strp++)
2607 return 0;
2609 /* Compare symbol name. */
2610 strp = (const unsigned char *) XSTR (value.base, 0);
2611 len = strlen ((const char *) strp) + 1;
2613 break;
2615 case PLUS_EXPR:
2616 case MINUS_EXPR:
2617 case RANGE_EXPR:
2618 p = compare_constant_1 (TREE_OPERAND (exp, 0), p);
2619 if (p == 0)
2620 return 0;
2622 return compare_constant_1 (TREE_OPERAND (exp, 1), p);
2624 case NOP_EXPR:
2625 case CONVERT_EXPR:
2626 case NON_LVALUE_EXPR:
2627 return compare_constant_1 (TREE_OPERAND (exp, 0), p);
2629 default:
2631 tree new = (*lang_hooks.expand_constant) (exp);
2633 if (new != exp)
2634 return compare_constant_1 (new, p);
2635 else
2636 return 0;
2640 /* Compare constant contents. */
2641 while (--len >= 0)
2642 if (*p++ != *strp++)
2643 return 0;
2645 return p;
2648 /* Construct a constant descriptor for the expression EXP.
2649 It is up to the caller to enter the descriptor in the hash table. */
2651 static struct constant_descriptor *
2652 record_constant (exp)
2653 tree exp;
2655 struct constant_descriptor *next = 0;
2656 char *label = 0;
2657 rtx rtl = 0;
2658 int pad;
2660 /* Make a struct constant_descriptor. The first three pointers will
2661 be filled in later. Here we just leave space for them. */
2663 obstack_grow (&permanent_obstack, (char *) &next, sizeof next);
2664 obstack_grow (&permanent_obstack, (char *) &label, sizeof label);
2665 obstack_grow (&permanent_obstack, (char *) &rtl, sizeof rtl);
2667 /* Align the descriptor for the data payload. */
2668 pad = (offsetof (struct constant_descriptor, u)
2669 - offsetof(struct constant_descriptor, rtl)
2670 - sizeof(next->rtl));
2671 if (pad > 0)
2672 obstack_blank (&permanent_obstack, pad);
2674 record_constant_1 (exp);
2675 return (struct constant_descriptor *) obstack_finish (&permanent_obstack);
2678 /* Add a description of constant expression EXP
2679 to the object growing in `permanent_obstack'.
2680 No need to return its address; the caller will get that
2681 from the obstack when the object is complete. */
2683 static void
2684 record_constant_1 (exp)
2685 tree exp;
2687 const unsigned char *strp;
2688 int len;
2689 enum tree_code code = TREE_CODE (exp);
2691 obstack_1grow (&permanent_obstack, (unsigned int) code);
2693 switch (code)
2695 case INTEGER_CST:
2696 obstack_1grow (&permanent_obstack, TYPE_PRECISION (TREE_TYPE (exp)));
2697 strp = (unsigned char *) &TREE_INT_CST (exp);
2698 len = sizeof TREE_INT_CST (exp);
2699 break;
2701 case REAL_CST:
2702 obstack_1grow (&permanent_obstack, TYPE_PRECISION (TREE_TYPE (exp)));
2703 strp = (unsigned char *) &TREE_REAL_CST (exp);
2704 len = sizeof TREE_REAL_CST (exp);
2705 break;
2707 case STRING_CST:
2708 if (flag_writable_strings)
2709 return;
2711 obstack_1grow (&permanent_obstack, TYPE_MODE (TREE_TYPE (exp)));
2712 strp = (const unsigned char *) TREE_STRING_POINTER (exp);
2713 len = TREE_STRING_LENGTH (exp);
2714 obstack_grow (&permanent_obstack, (char *) &TREE_STRING_LENGTH (exp),
2715 sizeof TREE_STRING_LENGTH (exp));
2716 break;
2718 case COMPLEX_CST:
2719 record_constant_1 (TREE_REALPART (exp));
2720 record_constant_1 (TREE_IMAGPART (exp));
2721 return;
2723 case CONSTRUCTOR:
2724 if (TREE_CODE (TREE_TYPE (exp)) == SET_TYPE)
2726 int nbytes = int_size_in_bytes (TREE_TYPE (exp));
2727 obstack_grow (&permanent_obstack, &nbytes, sizeof (nbytes));
2728 obstack_blank (&permanent_obstack, nbytes);
2729 get_set_constructor_bytes
2730 (exp, (unsigned char *) permanent_obstack.next_free - nbytes,
2731 nbytes);
2732 return;
2734 else
2736 tree link;
2737 int length = list_length (CONSTRUCTOR_ELTS (exp));
2738 enum machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
2739 tree type;
2740 int have_purpose = 0;
2742 for (link = CONSTRUCTOR_ELTS (exp); link; link = TREE_CHAIN (link))
2743 if (TREE_PURPOSE (link))
2744 have_purpose = 1;
2746 obstack_grow (&permanent_obstack, (char *) &length, sizeof length);
2748 /* For record constructors, insist that the types match.
2749 For arrays, just verify both constructors are for arrays
2750 of the same mode. Then insist that either both or none
2751 have any TREE_PURPOSE values. */
2752 if (TREE_CODE (TREE_TYPE (exp)) == RECORD_TYPE)
2753 type = TREE_TYPE (exp);
2754 else
2755 type = 0;
2757 obstack_grow (&permanent_obstack, (char *) &type, sizeof type);
2758 if (TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE)
2759 obstack_grow (&permanent_obstack, &mode, sizeof mode);
2761 obstack_grow (&permanent_obstack, (char *) &have_purpose,
2762 sizeof have_purpose);
2764 /* For arrays, insist that the size in bytes match. */
2765 if (TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE)
2767 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
2768 obstack_grow (&permanent_obstack, (char *) &size, sizeof size);
2771 for (link = CONSTRUCTOR_ELTS (exp); link; link = TREE_CHAIN (link))
2773 if (TREE_VALUE (link))
2774 record_constant_1 (TREE_VALUE (link));
2775 else
2777 tree zero = 0;
2779 obstack_grow (&permanent_obstack,
2780 (char *) &zero, sizeof zero);
2783 if (TREE_PURPOSE (link)
2784 && TREE_CODE (TREE_PURPOSE (link)) == FIELD_DECL)
2785 obstack_grow (&permanent_obstack,
2786 (char *) &TREE_PURPOSE (link),
2787 sizeof TREE_PURPOSE (link));
2788 else if (TREE_PURPOSE (link))
2789 record_constant_1 (TREE_PURPOSE (link));
2790 else if (have_purpose)
2792 int zero = 0;
2794 obstack_grow (&permanent_obstack,
2795 (char *) &zero, sizeof zero);
2799 return;
2801 case ADDR_EXPR:
2803 struct addr_const value;
2805 decode_addr_const (exp, &value);
2806 /* Record the offset. */
2807 obstack_grow (&permanent_obstack,
2808 (char *) &value.offset, sizeof value.offset);
2810 switch (GET_CODE (value.base))
2812 case SYMBOL_REF:
2813 /* Record the symbol name. */
2814 obstack_grow (&permanent_obstack, XSTR (value.base, 0),
2815 strlen (XSTR (value.base, 0)) + 1);
2816 break;
2817 case LABEL_REF:
2818 /* Record the address of the CODE_LABEL. It may not have
2819 been emitted yet, so it's UID may be zero. But pointer
2820 identity is good enough. */
2821 obstack_grow (&permanent_obstack, &XEXP (value.base, 0),
2822 sizeof (rtx));
2823 break;
2824 default:
2825 abort ();
2828 return;
2830 case PLUS_EXPR:
2831 case MINUS_EXPR:
2832 case RANGE_EXPR:
2833 record_constant_1 (TREE_OPERAND (exp, 0));
2834 record_constant_1 (TREE_OPERAND (exp, 1));
2835 return;
2837 case NOP_EXPR:
2838 case CONVERT_EXPR:
2839 case NON_LVALUE_EXPR:
2840 record_constant_1 (TREE_OPERAND (exp, 0));
2841 return;
2843 default:
2845 tree new = (*lang_hooks.expand_constant) (exp);
2847 if (new != exp)
2848 record_constant_1 (new);
2849 return;
2853 /* Record constant contents. */
2854 obstack_grow (&permanent_obstack, strp, len);
2857 /* Record a list of constant expressions that were passed to
2858 output_constant_def but that could not be output right away. */
2860 struct deferred_constant
2862 struct deferred_constant *next;
2863 tree exp;
2864 int reloc;
2865 int labelno;
2868 static struct deferred_constant *deferred_constants;
2870 /* Another list of constants which should be output after the
2871 function. */
2872 static struct deferred_constant *after_function_constants;
2874 /* Nonzero means defer output of addressed subconstants
2875 (i.e., those for which output_constant_def is called.) */
2876 static int defer_addressed_constants_flag;
2878 /* Start deferring output of subconstants. */
2880 void
2881 defer_addressed_constants ()
2883 defer_addressed_constants_flag++;
2886 /* Stop deferring output of subconstants,
2887 and output now all those that have been deferred. */
2889 void
2890 output_deferred_addressed_constants ()
2892 struct deferred_constant *p, *next;
2894 defer_addressed_constants_flag--;
2896 if (defer_addressed_constants_flag > 0)
2897 return;
2899 for (p = deferred_constants; p; p = next)
2901 output_constant_def_contents (p->exp, p->reloc, p->labelno);
2902 next = p->next;
2903 free (p);
2906 deferred_constants = 0;
2909 /* Output any constants which should appear after a function. */
2911 static void
2912 output_after_function_constants ()
2914 struct deferred_constant *p, *next;
2916 for (p = after_function_constants; p; p = next)
2918 output_constant_def_contents (p->exp, p->reloc, p->labelno);
2919 next = p->next;
2920 free (p);
2923 after_function_constants = 0;
2926 /* Make a copy of the whole tree structure for a constant.
2927 This handles the same types of nodes that compare_constant
2928 and record_constant handle. */
2930 static tree
2931 copy_constant (exp)
2932 tree exp;
2934 switch (TREE_CODE (exp))
2936 case ADDR_EXPR:
2937 /* For ADDR_EXPR, we do not want to copy the decl whose address
2938 is requested. We do want to copy constants though. */
2939 if (TREE_CODE_CLASS (TREE_CODE (TREE_OPERAND (exp, 0))) == 'c')
2940 return build1 (TREE_CODE (exp), TREE_TYPE (exp),
2941 copy_constant (TREE_OPERAND (exp, 0)));
2942 else
2943 return copy_node (exp);
2945 case INTEGER_CST:
2946 case REAL_CST:
2947 case STRING_CST:
2948 return copy_node (exp);
2950 case COMPLEX_CST:
2951 return build_complex (TREE_TYPE (exp),
2952 copy_constant (TREE_REALPART (exp)),
2953 copy_constant (TREE_IMAGPART (exp)));
2955 case PLUS_EXPR:
2956 case MINUS_EXPR:
2957 return build (TREE_CODE (exp), TREE_TYPE (exp),
2958 copy_constant (TREE_OPERAND (exp, 0)),
2959 copy_constant (TREE_OPERAND (exp, 1)));
2961 case NOP_EXPR:
2962 case CONVERT_EXPR:
2963 case NON_LVALUE_EXPR:
2964 return build1 (TREE_CODE (exp), TREE_TYPE (exp),
2965 copy_constant (TREE_OPERAND (exp, 0)));
2967 case CONSTRUCTOR:
2969 tree copy = copy_node (exp);
2970 tree list = copy_list (CONSTRUCTOR_ELTS (exp));
2971 tree tail;
2973 CONSTRUCTOR_ELTS (copy) = list;
2974 for (tail = list; tail; tail = TREE_CHAIN (tail))
2975 TREE_VALUE (tail) = copy_constant (TREE_VALUE (tail));
2976 if (TREE_CODE (TREE_TYPE (exp)) == SET_TYPE)
2977 for (tail = list; tail; tail = TREE_CHAIN (tail))
2978 TREE_PURPOSE (tail) = copy_constant (TREE_PURPOSE (tail));
2980 return copy;
2983 default:
2984 abort ();
2988 /* Return an rtx representing a reference to constant data in memory
2989 for the constant expression EXP.
2991 If assembler code for such a constant has already been output,
2992 return an rtx to refer to it.
2993 Otherwise, output such a constant in memory (or defer it for later)
2994 and generate an rtx for it.
2996 If DEFER is non-zero, the output of string constants can be deferred
2997 and output only if referenced in the function after all optimizations.
2999 The TREE_CST_RTL of EXP is set up to point to that rtx.
3000 The const_hash_table records which constants already have label strings. */
3003 output_constant_def (exp, defer)
3004 tree exp;
3005 int defer;
3007 int hash;
3008 struct constant_descriptor *desc;
3009 struct deferred_string **defstr;
3010 char label[256];
3011 int reloc;
3012 int found = 1;
3013 int after_function = 0;
3014 int labelno = -1;
3015 rtx rtl;
3017 /* We can't just use the saved RTL if this is a defererred string constant
3018 and we are not to defer anymode. */
3019 if (TREE_CODE (exp) != INTEGER_CST && TREE_CST_RTL (exp)
3020 && (defer || !STRING_POOL_ADDRESS_P (XEXP (TREE_CST_RTL (exp), 0))))
3021 return TREE_CST_RTL (exp);
3023 /* Make sure any other constants whose addresses appear in EXP
3024 are assigned label numbers. */
3026 reloc = output_addressed_constants (exp);
3028 /* Compute hash code of EXP. Search the descriptors for that hash code
3029 to see if any of them describes EXP. If yes, the descriptor records
3030 the label number already assigned. */
3032 hash = const_hash (exp) % MAX_HASH_TABLE;
3034 for (desc = const_hash_table[hash]; desc; desc = desc->next)
3035 if (compare_constant (exp, desc))
3036 break;
3038 if (desc == 0)
3040 /* No constant equal to EXP is known to have been output.
3041 Make a constant descriptor to enter EXP in the hash table.
3042 Assign the label number and record it in the descriptor for
3043 future calls to this function to find. */
3045 /* Create a string containing the label name, in LABEL. */
3046 labelno = const_labelno++;
3047 ASM_GENERATE_INTERNAL_LABEL (label, "LC", labelno);
3049 desc = record_constant (exp);
3050 desc->next = const_hash_table[hash];
3051 desc->label = ggc_strdup (label);
3052 const_hash_table[hash] = desc;
3054 /* We have a symbol name; construct the SYMBOL_REF and the MEM. */
3055 rtl = desc->rtl
3056 = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (exp)),
3057 gen_rtx_SYMBOL_REF (Pmode, desc->label));
3059 set_mem_attributes (rtl, exp, 1);
3060 set_mem_alias_set (rtl, 0);
3061 set_mem_alias_set (rtl, const_alias_set);
3063 found = 0;
3065 else
3066 rtl = desc->rtl;
3068 if (TREE_CODE (exp) != INTEGER_CST)
3069 TREE_CST_RTL (exp) = rtl;
3071 /* Optionally set flags or add text to the name to record information
3072 such as that it is a function name. If the name is changed, the macro
3073 ASM_OUTPUT_LABELREF will have to know how to strip this information. */
3074 #ifdef ENCODE_SECTION_INFO
3075 /* A previously-processed constant would already have section info
3076 encoded in it. */
3077 if (! found)
3079 /* Take care not to invoke ENCODE_SECTION_INFO for constants
3080 which don't have a TREE_CST_RTL. */
3081 if (TREE_CODE (exp) != INTEGER_CST)
3082 ENCODE_SECTION_INFO (exp, true);
3084 desc->rtl = rtl;
3085 desc->label = XSTR (XEXP (desc->rtl, 0), 0);
3087 #endif
3089 #ifdef CONSTANT_AFTER_FUNCTION_P
3090 if (current_function_decl != 0
3091 && CONSTANT_AFTER_FUNCTION_P (exp))
3092 after_function = 1;
3093 #endif
3095 if (found
3096 && STRING_POOL_ADDRESS_P (XEXP (rtl, 0))
3097 && (!defer || defer_addressed_constants_flag || after_function))
3099 defstr = (struct deferred_string **)
3100 htab_find_slot_with_hash (const_str_htab, desc->label,
3101 STRHASH (desc->label), NO_INSERT);
3102 if (defstr)
3104 /* If the string is currently deferred but we need to output it now,
3105 remove it from deferred string hash table. */
3106 found = 0;
3107 labelno = (*defstr)->labelno;
3108 STRING_POOL_ADDRESS_P (XEXP (rtl, 0)) = 0;
3109 htab_clear_slot (const_str_htab, (void **) defstr);
3113 /* If this is the first time we've seen this particular constant,
3114 output it (or defer its output for later). */
3115 if (! found)
3117 if (defer_addressed_constants_flag || after_function)
3119 struct deferred_constant *p
3120 = (struct deferred_constant *)
3121 xmalloc (sizeof (struct deferred_constant));
3123 p->exp = copy_constant (exp);
3124 p->reloc = reloc;
3125 p->labelno = labelno;
3126 if (after_function)
3128 p->next = after_function_constants;
3129 after_function_constants = p;
3131 else
3133 p->next = deferred_constants;
3134 deferred_constants = p;
3137 else
3139 /* Do no output if -fsyntax-only. */
3140 if (! flag_syntax_only)
3142 if (TREE_CODE (exp) != STRING_CST
3143 || !defer
3144 || flag_writable_strings
3145 || (defstr = (struct deferred_string **)
3146 htab_find_slot_with_hash (const_str_htab,
3147 desc->label,
3148 STRHASH (desc->label),
3149 INSERT)) == NULL)
3150 output_constant_def_contents (exp, reloc, labelno);
3151 else
3153 struct deferred_string *p;
3155 p = (struct deferred_string *)
3156 xmalloc (sizeof (struct deferred_string));
3158 p->exp = copy_constant (exp);
3159 p->label = desc->label;
3160 p->labelno = labelno;
3161 *defstr = p;
3162 STRING_POOL_ADDRESS_P (XEXP (rtl, 0)) = 1;
3168 return rtl;
3171 /* Now output assembler code to define the label for EXP,
3172 and follow it with the data of EXP. */
3174 static void
3175 output_constant_def_contents (exp, reloc, labelno)
3176 tree exp;
3177 int reloc;
3178 int labelno;
3180 int align;
3182 /* Align the location counter as required by EXP's data type. */
3183 align = TYPE_ALIGN (TREE_TYPE (exp));
3184 #ifdef CONSTANT_ALIGNMENT
3185 align = CONSTANT_ALIGNMENT (exp, align);
3186 #endif
3188 if (IN_NAMED_SECTION (exp))
3189 named_section (exp, NULL, reloc);
3190 else
3191 (*targetm.asm_out.select_section) (exp, reloc, align);
3193 if (align > BITS_PER_UNIT)
3195 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
3198 /* Output the label itself. */
3199 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, "LC", labelno);
3201 /* Output the value of EXP. */
3202 output_constant (exp,
3203 (TREE_CODE (exp) == STRING_CST
3204 ? MAX (TREE_STRING_LENGTH (exp),
3205 int_size_in_bytes (TREE_TYPE (exp)))
3206 : int_size_in_bytes (TREE_TYPE (exp))),
3207 align);
3211 /* Structure to represent sufficient information about a constant so that
3212 it can be output when the constant pool is output, so that function
3213 integration can be done, and to simplify handling on machines that reference
3214 constant pool as base+displacement. */
3216 struct pool_constant
3218 struct constant_descriptor *desc;
3219 struct pool_constant *next, *next_sym;
3220 rtx constant;
3221 enum machine_mode mode;
3222 int labelno;
3223 unsigned int align;
3224 HOST_WIDE_INT offset;
3225 int mark;
3228 /* Hash code for a SYMBOL_REF with CONSTANT_POOL_ADDRESS_P true.
3229 The argument is XSTR (... , 0) */
3231 #define SYMHASH(LABEL) \
3232 ((((unsigned long) (LABEL)) & ((1 << HASHBITS) - 1)) % MAX_RTX_HASH_TABLE)
3234 /* Initialize constant pool hashing for a new function. */
3236 void
3237 init_varasm_status (f)
3238 struct function *f;
3240 struct varasm_status *p;
3241 p = (struct varasm_status *) xmalloc (sizeof (struct varasm_status));
3242 f->varasm = p;
3243 p->x_const_rtx_hash_table
3244 = ((struct constant_descriptor **)
3245 xcalloc (MAX_RTX_HASH_TABLE, sizeof (struct constant_descriptor *)));
3246 p->x_const_rtx_sym_hash_table
3247 = ((struct pool_constant **)
3248 xcalloc (MAX_RTX_HASH_TABLE, sizeof (struct pool_constant *)));
3250 p->x_first_pool = p->x_last_pool = 0;
3251 p->x_pool_offset = 0;
3254 /* Mark PC for GC. */
3256 static void
3257 mark_pool_constant (pc)
3258 struct pool_constant *pc;
3260 while (pc)
3262 ggc_mark (pc);
3263 ggc_mark_rtx (pc->constant);
3264 ggc_mark_rtx (pc->desc->rtl);
3265 pc = pc->next;
3269 /* Mark P for GC. */
3271 void
3272 mark_varasm_status (p)
3273 struct varasm_status *p;
3275 if (p == NULL)
3276 return;
3278 mark_pool_constant (p->x_first_pool);
3281 /* Clear out all parts of the state in F that can safely be discarded
3282 after the function has been compiled, to let garbage collection
3283 reclaim the memory. */
3285 void
3286 free_varasm_status (f)
3287 struct function *f;
3289 struct varasm_status *p;
3290 int i;
3292 p = f->varasm;
3294 /* Clear out the hash tables. */
3295 for (i = 0; i < MAX_RTX_HASH_TABLE; ++i)
3297 struct constant_descriptor *cd;
3299 cd = p->x_const_rtx_hash_table[i];
3300 while (cd)
3302 struct constant_descriptor *next = cd->next;
3304 free (cd);
3305 cd = next;
3309 free (p->x_const_rtx_hash_table);
3310 free (p->x_const_rtx_sym_hash_table);
3311 free (p);
3313 f->varasm = NULL;
3317 /* Express an rtx for a constant integer (perhaps symbolic)
3318 as the sum of a symbol or label plus an explicit integer.
3319 They are stored into VALUE. */
3321 static void
3322 decode_rtx_const (mode, x, value)
3323 enum machine_mode mode;
3324 rtx x;
3325 struct rtx_const *value;
3327 /* Clear the whole structure, including any gaps. */
3328 memset (value, 0, sizeof (struct rtx_const));
3330 value->kind = RTX_INT; /* Most usual kind. */
3331 value->mode = mode;
3333 switch (GET_CODE (x))
3335 case CONST_DOUBLE:
3336 value->kind = RTX_DOUBLE;
3337 if (GET_MODE (x) != VOIDmode)
3339 value->mode = GET_MODE (x);
3340 REAL_VALUE_FROM_CONST_DOUBLE (value->un.du, x);
3342 else
3344 value->un.di.low = CONST_DOUBLE_LOW (x);
3345 value->un.di.high = CONST_DOUBLE_HIGH (x);
3347 break;
3349 case CONST_VECTOR:
3351 int units, i;
3352 rtx elt;
3354 units = CONST_VECTOR_NUNITS (x);
3355 value->kind = RTX_VECTOR;
3356 value->mode = mode;
3358 for (i = 0; i < units; ++i)
3360 elt = CONST_VECTOR_ELT (x, i);
3361 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
3363 value->un.veclo[i] = (HOST_WIDE_INT) INTVAL (elt);
3364 value->un.vechi[i] = 0;
3366 else if (GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
3368 value->un.veclo[i] = (HOST_WIDE_INT) CONST_DOUBLE_LOW (elt);
3369 value->un.vechi[i] = (HOST_WIDE_INT) CONST_DOUBLE_HIGH (elt);
3371 else
3372 abort ();
3375 break;
3377 case CONST_INT:
3378 value->un.addr.offset = INTVAL (x);
3379 break;
3381 case SYMBOL_REF:
3382 case LABEL_REF:
3383 case PC:
3384 value->un.addr.base = x;
3385 break;
3387 case CONST:
3388 x = XEXP (x, 0);
3389 if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 1)) == CONST_INT)
3391 value->un.addr.base = XEXP (x, 0);
3392 value->un.addr.offset = INTVAL (XEXP (x, 1));
3394 else if (GET_CODE (x) == MINUS && GET_CODE (XEXP (x, 1)) == CONST_INT)
3396 value->un.addr.base = XEXP (x, 0);
3397 value->un.addr.offset = - INTVAL (XEXP (x, 1));
3399 else
3401 value->un.addr.base = x;
3402 value->un.addr.offset = 0;
3404 break;
3406 default:
3407 value->kind = RTX_UNKNOWN;
3408 break;
3411 if (value->kind == RTX_INT && value->un.addr.base != 0
3412 && GET_CODE (value->un.addr.base) == UNSPEC)
3414 /* For a simple UNSPEC, the base is set to the
3415 operand, the kind field is set to the index of
3416 the unspec expression.
3417 Together with the code below, in case that
3418 the operand is a SYMBOL_REF or LABEL_REF,
3419 the address of the string or the code_label
3420 is taken as base. */
3421 if (XVECLEN (value->un.addr.base, 0) == 1)
3423 value->kind = RTX_UNSPEC + XINT (value->un.addr.base, 1);
3424 value->un.addr.base = XVECEXP (value->un.addr.base, 0, 0);
3428 if (value->kind > RTX_DOUBLE && value->un.addr.base != 0)
3429 switch (GET_CODE (value->un.addr.base))
3431 case SYMBOL_REF:
3432 /* Use the string's address, not the SYMBOL_REF's address,
3433 for the sake of addresses of library routines. */
3434 value->un.addr.base = (rtx) XSTR (value->un.addr.base, 0);
3435 break;
3437 case LABEL_REF:
3438 /* For a LABEL_REF, compare labels. */
3439 value->un.addr.base = XEXP (value->un.addr.base, 0);
3441 default:
3442 break;
3446 /* Given a MINUS expression, simplify it if both sides
3447 include the same symbol. */
3450 simplify_subtraction (x)
3451 rtx x;
3453 struct rtx_const val0, val1;
3455 decode_rtx_const (GET_MODE (x), XEXP (x, 0), &val0);
3456 decode_rtx_const (GET_MODE (x), XEXP (x, 1), &val1);
3458 if (val0.kind > RTX_DOUBLE
3459 && val0.kind == val1.kind
3460 && val0.un.addr.base == val1.un.addr.base)
3461 return GEN_INT (val0.un.addr.offset - val1.un.addr.offset);
3463 return x;
3466 /* Compute a hash code for a constant RTL expression. */
3468 static int
3469 const_hash_rtx (mode, x)
3470 enum machine_mode mode;
3471 rtx x;
3473 int hi;
3474 size_t i;
3476 struct rtx_const value;
3477 decode_rtx_const (mode, x, &value);
3479 /* Compute hashing function */
3480 hi = 0;
3481 for (i = 0; i < sizeof value / sizeof (int); i++)
3482 hi += ((int *) &value)[i];
3484 hi &= (1 << HASHBITS) - 1;
3485 hi %= MAX_RTX_HASH_TABLE;
3486 return hi;
3489 /* Compare a constant rtl object X with a constant-descriptor DESC.
3490 Return 1 if DESC describes a constant with the same value as X. */
3492 static int
3493 compare_constant_rtx (mode, x, desc)
3494 enum machine_mode mode;
3495 rtx x;
3496 struct constant_descriptor *desc;
3498 int *p = (int *) desc->u.contents;
3499 int *strp;
3500 int len;
3501 struct rtx_const value;
3503 decode_rtx_const (mode, x, &value);
3504 strp = (int *) &value;
3505 len = sizeof value / sizeof (int);
3507 /* Compare constant contents. */
3508 while (--len >= 0)
3509 if (*p++ != *strp++)
3510 return 0;
3512 return 1;
3515 /* Construct a constant descriptor for the rtl-expression X.
3516 It is up to the caller to enter the descriptor in the hash table. */
3518 static struct constant_descriptor *
3519 record_constant_rtx (mode, x)
3520 enum machine_mode mode;
3521 rtx x;
3523 struct constant_descriptor *ptr;
3525 ptr = ((struct constant_descriptor *)
3526 xcalloc (1, (offsetof (struct constant_descriptor, u)
3527 + sizeof (struct rtx_const))));
3528 decode_rtx_const (mode, x, (struct rtx_const *) ptr->u.contents);
3530 return ptr;
3533 /* Given a constant rtx X, return a MEM for the location in memory at which
3534 this constant has been placed. Return 0 if it not has been placed yet. */
3537 mem_for_const_double (x)
3538 rtx x;
3540 enum machine_mode mode = GET_MODE (x);
3541 struct constant_descriptor *desc;
3543 for (desc = const_rtx_hash_table[const_hash_rtx (mode, x)]; desc;
3544 desc = desc->next)
3545 if (compare_constant_rtx (mode, x, desc))
3546 return desc->rtl;
3548 return 0;
3551 /* Given a constant rtx X, make (or find) a memory constant for its value
3552 and return a MEM rtx to refer to it in memory. */
3555 force_const_mem (mode, x)
3556 enum machine_mode mode;
3557 rtx x;
3559 int hash;
3560 struct constant_descriptor *desc;
3561 char label[256];
3562 rtx def;
3563 struct pool_constant *pool;
3564 unsigned int align;
3566 /* Compute hash code of X. Search the descriptors for that hash code
3567 to see if any of them describes X. If yes, we have an rtx to use. */
3568 hash = const_hash_rtx (mode, x);
3569 for (desc = const_rtx_hash_table[hash]; desc; desc = desc->next)
3570 if (compare_constant_rtx (mode, x, desc))
3571 return desc->rtl;
3573 /* No constant equal to X is known to have been output.
3574 Make a constant descriptor to enter X in the hash table
3575 and make a MEM for it. */
3576 desc = record_constant_rtx (mode, x);
3577 desc->next = const_rtx_hash_table[hash];
3578 const_rtx_hash_table[hash] = desc;
3580 /* Align the location counter as required by EXP's data type. */
3581 align = GET_MODE_ALIGNMENT (mode == VOIDmode ? word_mode : mode);
3582 #ifdef CONSTANT_ALIGNMENT
3583 align = CONSTANT_ALIGNMENT (make_tree ((*lang_hooks.types.type_for_mode)
3584 (mode, 0), x), align);
3585 #endif
3587 pool_offset += (align / BITS_PER_UNIT) - 1;
3588 pool_offset &= ~ ((align / BITS_PER_UNIT) - 1);
3590 if (GET_CODE (x) == LABEL_REF)
3591 LABEL_PRESERVE_P (XEXP (x, 0)) = 1;
3593 /* Allocate a pool constant descriptor, fill it in, and chain it in. */
3594 pool = (struct pool_constant *) ggc_alloc (sizeof (struct pool_constant));
3595 pool->desc = desc;
3596 pool->constant = x;
3597 pool->mode = mode;
3598 pool->labelno = const_labelno;
3599 pool->align = align;
3600 pool->offset = pool_offset;
3601 pool->mark = 1;
3602 pool->next = 0;
3604 if (last_pool == 0)
3605 first_pool = pool;
3606 else
3607 last_pool->next = pool;
3609 last_pool = pool;
3610 pool_offset += GET_MODE_SIZE (mode);
3612 /* Create a string containing the label name, in LABEL. */
3613 ASM_GENERATE_INTERNAL_LABEL (label, "LC", const_labelno);
3615 ++const_labelno;
3617 /* Construct the SYMBOL_REF and the MEM. */
3619 pool->desc->rtl = def
3620 = gen_rtx_MEM (mode, gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label)));
3621 set_mem_alias_set (def, const_alias_set);
3622 set_mem_attributes (def, (*lang_hooks.types.type_for_mode) (mode, 0), 1);
3623 RTX_UNCHANGING_P (def) = 1;
3625 /* Add label to symbol hash table. */
3626 hash = SYMHASH (XSTR (XEXP (def, 0), 0));
3627 pool->next_sym = const_rtx_sym_hash_table[hash];
3628 const_rtx_sym_hash_table[hash] = pool;
3630 /* Mark the symbol_ref as belonging to this constants pool. */
3631 CONSTANT_POOL_ADDRESS_P (XEXP (def, 0)) = 1;
3632 current_function_uses_const_pool = 1;
3634 return def;
3637 /* Given a SYMBOL_REF with CONSTANT_POOL_ADDRESS_P true, return a pointer to
3638 the corresponding pool_constant structure. */
3640 static struct pool_constant *
3641 find_pool_constant (f, addr)
3642 struct function *f;
3643 rtx addr;
3645 struct pool_constant *pool;
3646 const char *label = XSTR (addr, 0);
3648 for (pool = f->varasm->x_const_rtx_sym_hash_table[SYMHASH (label)]; pool;
3649 pool = pool->next_sym)
3650 if (XSTR (XEXP (pool->desc->rtl, 0), 0) == label)
3651 return pool;
3653 abort ();
3656 /* Given a constant pool SYMBOL_REF, return the corresponding constant. */
3659 get_pool_constant (addr)
3660 rtx addr;
3662 return (find_pool_constant (cfun, addr))->constant;
3665 /* Given a constant pool SYMBOL_REF, return the corresponding constant
3666 and whether it has been output or not. */
3669 get_pool_constant_mark (addr, pmarked)
3670 rtx addr;
3671 bool *pmarked;
3673 struct pool_constant *pool = find_pool_constant (cfun, addr);
3674 *pmarked = (pool->mark != 0);
3675 return pool->constant;
3678 /* Likewise, but for the constant pool of a specific function. */
3681 get_pool_constant_for_function (f, addr)
3682 struct function *f;
3683 rtx addr;
3685 return (find_pool_constant (f, addr))->constant;
3688 /* Similar, return the mode. */
3690 enum machine_mode
3691 get_pool_mode (addr)
3692 rtx addr;
3694 return (find_pool_constant (cfun, addr))->mode;
3697 enum machine_mode
3698 get_pool_mode_for_function (f, addr)
3699 struct function *f;
3700 rtx addr;
3702 return (find_pool_constant (f, addr))->mode;
3705 /* Similar, return the offset in the constant pool. */
3708 get_pool_offset (addr)
3709 rtx addr;
3711 return (find_pool_constant (cfun, addr))->offset;
3714 /* Return the size of the constant pool. */
3717 get_pool_size ()
3719 return pool_offset;
3722 /* Write all the constants in the constant pool. */
3724 void
3725 output_constant_pool (fnname, fndecl)
3726 const char *fnname ATTRIBUTE_UNUSED;
3727 tree fndecl ATTRIBUTE_UNUSED;
3729 struct pool_constant *pool;
3730 rtx x;
3731 REAL_VALUE_TYPE r;
3733 /* It is possible for gcc to call force_const_mem and then to later
3734 discard the instructions which refer to the constant. In such a
3735 case we do not need to output the constant. */
3736 mark_constant_pool ();
3738 #ifdef ASM_OUTPUT_POOL_PROLOGUE
3739 ASM_OUTPUT_POOL_PROLOGUE (asm_out_file, fnname, fndecl, pool_offset);
3740 #endif
3742 for (pool = first_pool; pool; pool = pool->next)
3744 rtx tmp;
3746 x = pool->constant;
3748 if (! pool->mark)
3749 continue;
3751 /* See if X is a LABEL_REF (or a CONST referring to a LABEL_REF)
3752 whose CODE_LABEL has been deleted. This can occur if a jump table
3753 is eliminated by optimization. If so, write a constant of zero
3754 instead. Note that this can also happen by turning the
3755 CODE_LABEL into a NOTE. */
3756 /* ??? This seems completely and utterly wrong. Certainly it's
3757 not true for NOTE_INSN_DELETED_LABEL, but I disbelieve proper
3758 functioning even with INSN_DELETED_P and friends. */
3760 tmp = x;
3761 switch (GET_CODE (x))
3763 case CONST:
3764 if (GET_CODE (XEXP (x, 0)) != PLUS
3765 || GET_CODE (XEXP (XEXP (x, 0), 0)) != LABEL_REF)
3766 break;
3767 tmp = XEXP (XEXP (x, 0), 0);
3768 /* FALLTHRU */
3770 case LABEL_REF:
3771 tmp = XEXP (x, 0);
3772 if (INSN_DELETED_P (tmp)
3773 || (GET_CODE (tmp) == NOTE
3774 && NOTE_LINE_NUMBER (tmp) == NOTE_INSN_DELETED))
3776 abort ();
3777 x = const0_rtx;
3779 break;
3781 default:
3782 break;
3785 /* First switch to correct section. */
3786 #ifdef SELECT_RTX_SECTION
3787 SELECT_RTX_SECTION (pool->mode, x, pool->align);
3788 #else
3789 readonly_data_section ();
3790 #endif
3792 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3793 ASM_OUTPUT_SPECIAL_POOL_ENTRY (asm_out_file, x, pool->mode,
3794 pool->align, pool->labelno, done);
3795 #endif
3797 assemble_align (pool->align);
3799 /* Output the label. */
3800 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, "LC", pool->labelno);
3802 /* Output the value of the constant itself. */
3803 switch (GET_MODE_CLASS (pool->mode))
3805 case MODE_FLOAT:
3806 if (GET_CODE (x) != CONST_DOUBLE)
3807 abort ();
3809 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
3810 assemble_real (r, pool->mode, pool->align);
3811 break;
3813 case MODE_INT:
3814 case MODE_PARTIAL_INT:
3815 assemble_integer (x, GET_MODE_SIZE (pool->mode), pool->align, 1);
3816 break;
3818 case MODE_VECTOR_FLOAT:
3820 int i, units;
3821 rtx elt;
3823 if (GET_CODE (x) != CONST_VECTOR)
3824 abort ();
3826 units = CONST_VECTOR_NUNITS (x);
3828 for (i = 0; i < units; i++)
3830 elt = CONST_VECTOR_ELT (x, i);
3831 REAL_VALUE_FROM_CONST_DOUBLE (r, elt);
3832 assemble_real (r, GET_MODE_INNER (pool->mode), pool->align);
3835 break;
3837 case MODE_VECTOR_INT:
3839 int i, units;
3840 rtx elt;
3842 if (GET_CODE (x) != CONST_VECTOR)
3843 abort ();
3845 units = CONST_VECTOR_NUNITS (x);
3847 for (i = 0; i < units; i++)
3849 elt = CONST_VECTOR_ELT (x, i);
3850 assemble_integer (elt, GET_MODE_UNIT_SIZE (pool->mode),
3851 pool->align, 1);
3854 break;
3856 default:
3857 abort ();
3860 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3861 done: ;
3862 #endif
3865 #ifdef ASM_OUTPUT_POOL_EPILOGUE
3866 ASM_OUTPUT_POOL_EPILOGUE (asm_out_file, fnname, fndecl, pool_offset);
3867 #endif
3869 /* Done with this pool. */
3870 first_pool = last_pool = 0;
3873 /* Look through the instructions for this function, and mark all the
3874 entries in the constant pool which are actually being used.
3875 Emit used deferred strings. */
3877 static void
3878 mark_constant_pool ()
3880 rtx insn;
3881 struct pool_constant *pool;
3883 if (first_pool == 0 && htab_elements (const_str_htab) == 0)
3884 return;
3886 for (pool = first_pool; pool; pool = pool->next)
3887 pool->mark = 0;
3889 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3890 if (INSN_P (insn))
3891 mark_constants (PATTERN (insn));
3893 for (insn = current_function_epilogue_delay_list;
3894 insn;
3895 insn = XEXP (insn, 1))
3896 if (INSN_P (insn))
3897 mark_constants (PATTERN (insn));
3900 /* Look through appropriate parts of X, marking all entries in the
3901 constant pool which are actually being used. Entries that are only
3902 referenced by other constants are also marked as used. Emit
3903 deferred strings that are used. */
3905 static void
3906 mark_constants (x)
3907 rtx x;
3909 int i;
3910 const char *format_ptr;
3912 if (x == 0)
3913 return;
3915 if (GET_CODE (x) == SYMBOL_REF)
3917 mark_constant (&x, NULL);
3918 return;
3921 /* Insns may appear inside a SEQUENCE. Only check the patterns of
3922 insns, not any notes that may be attached. We don't want to mark
3923 a constant just because it happens to appear in a REG_EQUIV note. */
3924 if (INSN_P (x))
3926 mark_constants (PATTERN (x));
3927 return;
3930 format_ptr = GET_RTX_FORMAT (GET_CODE (x));
3932 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
3934 switch (*format_ptr++)
3936 case 'e':
3937 mark_constants (XEXP (x, i));
3938 break;
3940 case 'E':
3941 if (XVEC (x, i) != 0)
3943 int j;
3945 for (j = 0; j < XVECLEN (x, i); j++)
3946 mark_constants (XVECEXP (x, i, j));
3948 break;
3950 case 'S':
3951 case 's':
3952 case '0':
3953 case 'i':
3954 case 'w':
3955 case 'n':
3956 case 'u':
3957 break;
3959 default:
3960 abort ();
3965 /* Given a SYMBOL_REF CURRENT_RTX, mark it and all constants it refers
3966 to as used. Emit referenced deferred strings. This function can
3967 be used with for_each_rtx to mark all SYMBOL_REFs in an rtx. */
3969 static int
3970 mark_constant (current_rtx, data)
3971 rtx *current_rtx;
3972 void *data ATTRIBUTE_UNUSED;
3974 rtx x = *current_rtx;
3976 if (x == NULL_RTX)
3977 return 0;
3979 else if (GET_CODE (x) == SYMBOL_REF)
3981 if (CONSTANT_POOL_ADDRESS_P (x))
3983 struct pool_constant *pool = find_pool_constant (cfun, x);
3984 if (pool->mark == 0)
3986 pool->mark = 1;
3987 for_each_rtx (&(pool->constant), &mark_constant, NULL);
3989 else
3990 return -1;
3992 else if (STRING_POOL_ADDRESS_P (x))
3994 struct deferred_string **defstr;
3996 defstr = (struct deferred_string **)
3997 htab_find_slot_with_hash (const_str_htab, XSTR (x, 0),
3998 STRHASH (XSTR (x, 0)), NO_INSERT);
3999 if (defstr)
4001 struct deferred_string *p = *defstr;
4003 STRING_POOL_ADDRESS_P (x) = 0;
4004 output_constant_def_contents (p->exp, 0, p->labelno);
4005 htab_clear_slot (const_str_htab, (void **) defstr);
4009 return 0;
4012 /* Find all the constants whose addresses are referenced inside of EXP,
4013 and make sure assembler code with a label has been output for each one.
4014 Indicate whether an ADDR_EXPR has been encountered. */
4016 static int
4017 output_addressed_constants (exp)
4018 tree exp;
4020 int reloc = 0;
4021 tree tem;
4023 /* Give the front-end a chance to convert VALUE to something that
4024 looks more like a constant to the back-end. */
4025 exp = (*lang_hooks.expand_constant) (exp);
4027 switch (TREE_CODE (exp))
4029 case ADDR_EXPR:
4030 /* Go inside any operations that get_inner_reference can handle and see
4031 if what's inside is a constant: no need to do anything here for
4032 addresses of variables or functions. */
4033 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
4034 tem = TREE_OPERAND (tem, 0))
4037 if (TREE_CODE_CLASS (TREE_CODE (tem)) == 'c'
4038 || TREE_CODE (tem) == CONSTRUCTOR)
4039 output_constant_def (tem, 0);
4041 if (TREE_PUBLIC (tem))
4042 reloc |= 2;
4043 else
4044 reloc |= 1;
4045 break;
4047 case PLUS_EXPR:
4048 case MINUS_EXPR:
4049 reloc = output_addressed_constants (TREE_OPERAND (exp, 0));
4050 reloc |= output_addressed_constants (TREE_OPERAND (exp, 1));
4051 break;
4053 case NOP_EXPR:
4054 case CONVERT_EXPR:
4055 case NON_LVALUE_EXPR:
4056 reloc = output_addressed_constants (TREE_OPERAND (exp, 0));
4057 break;
4059 case CONSTRUCTOR:
4060 for (tem = CONSTRUCTOR_ELTS (exp); tem; tem = TREE_CHAIN (tem))
4061 if (TREE_VALUE (tem) != 0)
4062 reloc |= output_addressed_constants (TREE_VALUE (tem));
4064 break;
4066 default:
4067 break;
4069 return reloc;
4072 /* Return nonzero if VALUE is a valid constant-valued expression
4073 for use in initializing a static variable; one that can be an
4074 element of a "constant" initializer.
4076 Return null_pointer_node if the value is absolute;
4077 if it is relocatable, return the variable that determines the relocation.
4078 We assume that VALUE has been folded as much as possible;
4079 therefore, we do not need to check for such things as
4080 arithmetic-combinations of integers. */
4082 tree
4083 initializer_constant_valid_p (value, endtype)
4084 tree value;
4085 tree endtype;
4087 /* Give the front-end a chance to convert VALUE to something that
4088 looks more like a constant to the back-end. */
4089 value = (*lang_hooks.expand_constant) (value);
4091 switch (TREE_CODE (value))
4093 case CONSTRUCTOR:
4094 if ((TREE_CODE (TREE_TYPE (value)) == UNION_TYPE
4095 || TREE_CODE (TREE_TYPE (value)) == RECORD_TYPE)
4096 && TREE_CONSTANT (value)
4097 && CONSTRUCTOR_ELTS (value))
4098 return
4099 initializer_constant_valid_p (TREE_VALUE (CONSTRUCTOR_ELTS (value)),
4100 endtype);
4102 return TREE_STATIC (value) ? null_pointer_node : 0;
4104 case INTEGER_CST:
4105 case VECTOR_CST:
4106 case REAL_CST:
4107 case STRING_CST:
4108 case COMPLEX_CST:
4109 return null_pointer_node;
4111 case ADDR_EXPR:
4112 case FDESC_EXPR:
4113 return staticp (TREE_OPERAND (value, 0)) ? TREE_OPERAND (value, 0) : 0;
4115 case VIEW_CONVERT_EXPR:
4116 case NON_LVALUE_EXPR:
4117 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4119 case CONVERT_EXPR:
4120 case NOP_EXPR:
4121 /* Allow conversions between pointer types. */
4122 if (POINTER_TYPE_P (TREE_TYPE (value))
4123 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (value, 0))))
4124 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4126 /* Allow conversions between real types. */
4127 if (FLOAT_TYPE_P (TREE_TYPE (value))
4128 && FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (value, 0))))
4129 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4131 /* Allow length-preserving conversions between integer types. */
4132 if (INTEGRAL_TYPE_P (TREE_TYPE (value))
4133 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (value, 0)))
4134 && (TYPE_PRECISION (TREE_TYPE (value))
4135 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0)))))
4136 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4138 /* Allow conversions between other integer types only if
4139 explicit value. */
4140 if (INTEGRAL_TYPE_P (TREE_TYPE (value))
4141 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (value, 0))))
4143 tree inner = initializer_constant_valid_p (TREE_OPERAND (value, 0),
4144 endtype);
4145 if (inner == null_pointer_node)
4146 return null_pointer_node;
4147 break;
4150 /* Allow (int) &foo provided int is as wide as a pointer. */
4151 if (INTEGRAL_TYPE_P (TREE_TYPE (value))
4152 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (value, 0)))
4153 && (TYPE_PRECISION (TREE_TYPE (value))
4154 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0)))))
4155 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
4156 endtype);
4158 /* Likewise conversions from int to pointers, but also allow
4159 conversions from 0. */
4160 if (POINTER_TYPE_P (TREE_TYPE (value))
4161 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (value, 0))))
4163 if (integer_zerop (TREE_OPERAND (value, 0)))
4164 return null_pointer_node;
4165 else if (TYPE_PRECISION (TREE_TYPE (value))
4166 <= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0))))
4167 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
4168 endtype);
4171 /* Allow conversions to union types if the value inside is okay. */
4172 if (TREE_CODE (TREE_TYPE (value)) == UNION_TYPE)
4173 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
4174 endtype);
4175 break;
4177 case PLUS_EXPR:
4178 if (! INTEGRAL_TYPE_P (endtype)
4179 || TYPE_PRECISION (endtype) >= POINTER_SIZE)
4181 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
4182 endtype);
4183 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
4184 endtype);
4185 /* If either term is absolute, use the other terms relocation. */
4186 if (valid0 == null_pointer_node)
4187 return valid1;
4188 if (valid1 == null_pointer_node)
4189 return valid0;
4191 break;
4193 case MINUS_EXPR:
4194 if (! INTEGRAL_TYPE_P (endtype)
4195 || TYPE_PRECISION (endtype) >= POINTER_SIZE)
4197 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
4198 endtype);
4199 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
4200 endtype);
4201 /* Win if second argument is absolute. */
4202 if (valid1 == null_pointer_node)
4203 return valid0;
4204 /* Win if both arguments have the same relocation.
4205 Then the value is absolute. */
4206 if (valid0 == valid1 && valid0 != 0)
4207 return null_pointer_node;
4209 /* Since GCC guarantees that string constants are unique in the
4210 generated code, a subtraction between two copies of the same
4211 constant string is absolute. */
4212 if (valid0 && TREE_CODE (valid0) == STRING_CST &&
4213 valid1 && TREE_CODE (valid1) == STRING_CST &&
4214 TREE_STRING_POINTER (valid0) == TREE_STRING_POINTER (valid1))
4215 return null_pointer_node;
4218 /* Support differences between labels. */
4219 if (INTEGRAL_TYPE_P (endtype))
4221 tree op0, op1;
4222 op0 = TREE_OPERAND (value, 0);
4223 op1 = TREE_OPERAND (value, 1);
4225 /* Like STRIP_NOPS except allow the operand mode to widen.
4226 This works around a feature of fold that simplfies
4227 (int)(p1 - p2) to ((int)p1 - (int)p2) under the theory
4228 that the narrower operation is cheaper. */
4230 while (TREE_CODE (op0) == NOP_EXPR
4231 || TREE_CODE (op0) == CONVERT_EXPR
4232 || TREE_CODE (op0) == NON_LVALUE_EXPR)
4234 tree inner = TREE_OPERAND (op0, 0);
4235 if (inner == error_mark_node
4236 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4237 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))
4238 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
4239 break;
4240 op0 = inner;
4243 while (TREE_CODE (op1) == NOP_EXPR
4244 || TREE_CODE (op1) == CONVERT_EXPR
4245 || TREE_CODE (op1) == NON_LVALUE_EXPR)
4247 tree inner = TREE_OPERAND (op1, 0);
4248 if (inner == error_mark_node
4249 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4250 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op1)))
4251 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
4252 break;
4253 op1 = inner;
4256 if (TREE_CODE (op0) == ADDR_EXPR
4257 && TREE_CODE (TREE_OPERAND (op0, 0)) == LABEL_DECL
4258 && TREE_CODE (op1) == ADDR_EXPR
4259 && TREE_CODE (TREE_OPERAND (op1, 0)) == LABEL_DECL)
4260 return null_pointer_node;
4262 break;
4264 default:
4265 break;
4268 return 0;
4271 /* Output assembler code for constant EXP to FILE, with no label.
4272 This includes the pseudo-op such as ".int" or ".byte", and a newline.
4273 Assumes output_addressed_constants has been done on EXP already.
4275 Generate exactly SIZE bytes of assembler data, padding at the end
4276 with zeros if necessary. SIZE must always be specified.
4278 SIZE is important for structure constructors,
4279 since trailing members may have been omitted from the constructor.
4280 It is also important for initialization of arrays from string constants
4281 since the full length of the string constant might not be wanted.
4282 It is also needed for initialization of unions, where the initializer's
4283 type is just one member, and that may not be as long as the union.
4285 There a case in which we would fail to output exactly SIZE bytes:
4286 for a structure constructor that wants to produce more than SIZE bytes.
4287 But such constructors will never be generated for any possible input.
4289 ALIGN is the alignment of the data in bits. */
4291 void
4292 output_constant (exp, size, align)
4293 tree exp;
4294 HOST_WIDE_INT size;
4295 unsigned int align;
4297 enum tree_code code;
4298 HOST_WIDE_INT thissize;
4300 /* Some front-ends use constants other than the standard language-indepdent
4301 varieties, but which may still be output directly. Give the front-end a
4302 chance to convert EXP to a language-independent representation. */
4303 exp = (*lang_hooks.expand_constant) (exp);
4305 if (size == 0 || flag_syntax_only)
4306 return;
4308 /* Eliminate any conversions since we'll be outputting the underlying
4309 constant. */
4310 while (TREE_CODE (exp) == NOP_EXPR || TREE_CODE (exp) == CONVERT_EXPR
4311 || TREE_CODE (exp) == NON_LVALUE_EXPR
4312 || TREE_CODE (exp) == VIEW_CONVERT_EXPR)
4313 exp = TREE_OPERAND (exp, 0);
4315 code = TREE_CODE (TREE_TYPE (exp));
4316 thissize = int_size_in_bytes (TREE_TYPE (exp));
4318 /* Allow a constructor with no elements for any data type.
4319 This means to fill the space with zeros. */
4320 if (TREE_CODE (exp) == CONSTRUCTOR && CONSTRUCTOR_ELTS (exp) == 0)
4322 assemble_zeros (size);
4323 return;
4326 if (TREE_CODE (exp) == FDESC_EXPR)
4328 #ifdef ASM_OUTPUT_FDESC
4329 HOST_WIDE_INT part = tree_low_cst (TREE_OPERAND (exp, 1), 0);
4330 tree decl = TREE_OPERAND (exp, 0);
4331 ASM_OUTPUT_FDESC (asm_out_file, decl, part);
4332 #else
4333 abort ();
4334 #endif
4335 return;
4338 /* Now output the underlying data. If we've handling the padding, return.
4339 Otherwise, break and ensure THISSIZE is the size written. */
4340 switch (code)
4342 case CHAR_TYPE:
4343 case BOOLEAN_TYPE:
4344 case INTEGER_TYPE:
4345 case ENUMERAL_TYPE:
4346 case POINTER_TYPE:
4347 case REFERENCE_TYPE:
4348 if (! assemble_integer (expand_expr (exp, NULL_RTX, VOIDmode,
4349 EXPAND_INITIALIZER),
4350 size, align, 0))
4351 error ("initializer for integer value is too complicated");
4352 break;
4354 case REAL_TYPE:
4355 if (TREE_CODE (exp) != REAL_CST)
4356 error ("initializer for floating value is not a floating constant");
4358 assemble_real (TREE_REAL_CST (exp),
4359 mode_for_size (size * BITS_PER_UNIT, MODE_FLOAT, 0),
4360 align);
4361 break;
4363 case COMPLEX_TYPE:
4364 output_constant (TREE_REALPART (exp), thissize / 2, align);
4365 output_constant (TREE_IMAGPART (exp), thissize / 2,
4366 min_align (align, BITS_PER_UNIT * (thissize / 2)));
4367 break;
4369 case ARRAY_TYPE:
4370 case VECTOR_TYPE:
4371 if (TREE_CODE (exp) == CONSTRUCTOR)
4373 output_constructor (exp, size, align);
4374 return;
4376 else if (TREE_CODE (exp) == STRING_CST)
4378 thissize = MIN (TREE_STRING_LENGTH (exp), size);
4379 assemble_string (TREE_STRING_POINTER (exp), thissize);
4381 else
4382 abort ();
4383 break;
4385 case RECORD_TYPE:
4386 case UNION_TYPE:
4387 if (TREE_CODE (exp) == CONSTRUCTOR)
4388 output_constructor (exp, size, align);
4389 else
4390 abort ();
4391 return;
4393 case SET_TYPE:
4394 if (TREE_CODE (exp) == INTEGER_CST)
4395 assemble_integer (expand_expr (exp, NULL_RTX,
4396 VOIDmode, EXPAND_INITIALIZER),
4397 thissize, align, 1);
4398 else if (TREE_CODE (exp) == CONSTRUCTOR)
4400 unsigned char *buffer = (unsigned char *) alloca (thissize);
4401 if (get_set_constructor_bytes (exp, buffer, thissize))
4402 abort ();
4403 assemble_string ((char *) buffer, thissize);
4405 else
4406 error ("unknown set constructor type");
4407 return;
4409 case ERROR_MARK:
4410 return;
4412 default:
4413 abort ();
4416 size -= thissize;
4417 if (size > 0)
4418 assemble_zeros (size);
4422 /* Subroutine of output_constructor, used for computing the size of
4423 arrays of unspecified length. VAL must be a CONSTRUCTOR of an array
4424 type with an unspecified upper bound. */
4426 static unsigned HOST_WIDE_INT
4427 array_size_for_constructor (val)
4428 tree val;
4430 tree max_index, i;
4432 /* This code used to attempt to handle string constants that are not
4433 arrays of single-bytes, but nothing else does, so there's no point in
4434 doing it here. */
4435 if (TREE_CODE (val) == STRING_CST)
4436 return TREE_STRING_LENGTH (val);
4438 max_index = NULL_TREE;
4439 for (i = CONSTRUCTOR_ELTS (val); i; i = TREE_CHAIN (i))
4441 tree index = TREE_PURPOSE (i);
4443 if (TREE_CODE (index) == RANGE_EXPR)
4444 index = TREE_OPERAND (index, 1);
4445 if (max_index == NULL_TREE || tree_int_cst_lt (max_index, index))
4446 max_index = index;
4449 if (max_index == NULL_TREE)
4450 return 0;
4452 /* Compute the total number of array elements. */
4453 i = size_binop (MINUS_EXPR, convert (sizetype, max_index),
4454 convert (sizetype,
4455 TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (val)))));
4456 i = size_binop (PLUS_EXPR, i, convert (sizetype, integer_one_node));
4458 /* Multiply by the array element unit size to find number of bytes. */
4459 i = size_binop (MULT_EXPR, i, TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (val))));
4461 return tree_low_cst (i, 1);
4464 /* Subroutine of output_constant, used for CONSTRUCTORs (aggregate constants).
4465 Generate at least SIZE bytes, padding if necessary. */
4467 static void
4468 output_constructor (exp, size, align)
4469 tree exp;
4470 HOST_WIDE_INT size;
4471 unsigned int align;
4473 tree type = TREE_TYPE (exp);
4474 tree link, field = 0;
4475 tree min_index = 0;
4476 /* Number of bytes output or skipped so far.
4477 In other words, current position within the constructor. */
4478 HOST_WIDE_INT total_bytes = 0;
4479 /* Non-zero means BYTE contains part of a byte, to be output. */
4480 int byte_buffer_in_use = 0;
4481 int byte = 0;
4483 if (HOST_BITS_PER_WIDE_INT < BITS_PER_UNIT)
4484 abort ();
4486 if (TREE_CODE (type) == RECORD_TYPE)
4487 field = TYPE_FIELDS (type);
4489 if (TREE_CODE (type) == ARRAY_TYPE
4490 && TYPE_DOMAIN (type) != 0)
4491 min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
4493 /* As LINK goes through the elements of the constant,
4494 FIELD goes through the structure fields, if the constant is a structure.
4495 if the constant is a union, then we override this,
4496 by getting the field from the TREE_LIST element.
4497 But the constant could also be an array. Then FIELD is zero.
4499 There is always a maximum of one element in the chain LINK for unions
4500 (even if the initializer in a source program incorrectly contains
4501 more one). */
4502 for (link = CONSTRUCTOR_ELTS (exp);
4503 link;
4504 link = TREE_CHAIN (link),
4505 field = field ? TREE_CHAIN (field) : 0)
4507 tree val = TREE_VALUE (link);
4508 tree index = 0;
4510 /* The element in a union constructor specifies the proper field
4511 or index. */
4512 if ((TREE_CODE (type) == RECORD_TYPE || TREE_CODE (type) == UNION_TYPE
4513 || TREE_CODE (type) == QUAL_UNION_TYPE)
4514 && TREE_PURPOSE (link) != 0)
4515 field = TREE_PURPOSE (link);
4517 else if (TREE_CODE (type) == ARRAY_TYPE)
4518 index = TREE_PURPOSE (link);
4520 /* Eliminate the marker that makes a cast not be an lvalue. */
4521 if (val != 0)
4522 STRIP_NOPS (val);
4524 if (index && TREE_CODE (index) == RANGE_EXPR)
4526 unsigned HOST_WIDE_INT fieldsize
4527 = int_size_in_bytes (TREE_TYPE (type));
4528 HOST_WIDE_INT lo_index = tree_low_cst (TREE_OPERAND (index, 0), 0);
4529 HOST_WIDE_INT hi_index = tree_low_cst (TREE_OPERAND (index, 1), 0);
4530 HOST_WIDE_INT index;
4531 unsigned int align2 = min_align (align, fieldsize * BITS_PER_UNIT);
4533 for (index = lo_index; index <= hi_index; index++)
4535 /* Output the element's initial value. */
4536 if (val == 0)
4537 assemble_zeros (fieldsize);
4538 else
4539 output_constant (val, fieldsize, align2);
4541 /* Count its size. */
4542 total_bytes += fieldsize;
4545 else if (field == 0 || !DECL_BIT_FIELD (field))
4547 /* An element that is not a bit-field. */
4549 unsigned HOST_WIDE_INT fieldsize;
4550 /* Since this structure is static,
4551 we know the positions are constant. */
4552 HOST_WIDE_INT pos = field ? int_byte_position (field) : 0;
4553 unsigned int align2;
4555 if (index != 0)
4556 pos = (tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (val)), 1)
4557 * (tree_low_cst (index, 0) - tree_low_cst (min_index, 0)));
4559 /* Output any buffered-up bit-fields preceding this element. */
4560 if (byte_buffer_in_use)
4562 assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
4563 total_bytes++;
4564 byte_buffer_in_use = 0;
4567 /* Advance to offset of this element.
4568 Note no alignment needed in an array, since that is guaranteed
4569 if each element has the proper size. */
4570 if ((field != 0 || index != 0) && pos != total_bytes)
4572 assemble_zeros (pos - total_bytes);
4573 total_bytes = pos;
4576 /* Find the alignment of this element. */
4577 align2 = min_align (align, BITS_PER_UNIT * pos);
4579 /* Determine size this element should occupy. */
4580 if (field)
4582 fieldsize = 0;
4584 /* If this is an array with an unspecified upper bound,
4585 the initializer determines the size. */
4586 /* ??? This ought to only checked if DECL_SIZE_UNIT is NULL,
4587 but we cannot do this until the deprecated support for
4588 initializing zero-length array members is removed. */
4589 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
4590 && TYPE_DOMAIN (TREE_TYPE (field))
4591 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
4593 fieldsize = array_size_for_constructor (val);
4594 /* Given a non-empty initialization, this field had
4595 better be last. */
4596 if (fieldsize != 0 && TREE_CHAIN (field) != NULL_TREE)
4597 abort ();
4599 else if (DECL_SIZE_UNIT (field))
4601 /* ??? This can't be right. If the decl size overflows
4602 a host integer we will silently emit no data. */
4603 if (host_integerp (DECL_SIZE_UNIT (field), 1))
4604 fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 1);
4607 else
4608 fieldsize = int_size_in_bytes (TREE_TYPE (type));
4610 /* Output the element's initial value. */
4611 if (val == 0)
4612 assemble_zeros (fieldsize);
4613 else
4614 output_constant (val, fieldsize, align2);
4616 /* Count its size. */
4617 total_bytes += fieldsize;
4619 else if (val != 0 && TREE_CODE (val) != INTEGER_CST)
4620 error ("invalid initial value for member `%s'",
4621 IDENTIFIER_POINTER (DECL_NAME (field)));
4622 else
4624 /* Element that is a bit-field. */
4626 HOST_WIDE_INT next_offset = int_bit_position (field);
4627 HOST_WIDE_INT end_offset
4628 = (next_offset + tree_low_cst (DECL_SIZE (field), 1));
4630 if (val == 0)
4631 val = integer_zero_node;
4633 /* If this field does not start in this (or, next) byte,
4634 skip some bytes. */
4635 if (next_offset / BITS_PER_UNIT != total_bytes)
4637 /* Output remnant of any bit field in previous bytes. */
4638 if (byte_buffer_in_use)
4640 assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
4641 total_bytes++;
4642 byte_buffer_in_use = 0;
4645 /* If still not at proper byte, advance to there. */
4646 if (next_offset / BITS_PER_UNIT != total_bytes)
4648 assemble_zeros (next_offset / BITS_PER_UNIT - total_bytes);
4649 total_bytes = next_offset / BITS_PER_UNIT;
4653 if (! byte_buffer_in_use)
4654 byte = 0;
4656 /* We must split the element into pieces that fall within
4657 separate bytes, and combine each byte with previous or
4658 following bit-fields. */
4660 /* next_offset is the offset n fbits from the beginning of
4661 the structure to the next bit of this element to be processed.
4662 end_offset is the offset of the first bit past the end of
4663 this element. */
4664 while (next_offset < end_offset)
4666 int this_time;
4667 int shift;
4668 HOST_WIDE_INT value;
4669 HOST_WIDE_INT next_byte = next_offset / BITS_PER_UNIT;
4670 HOST_WIDE_INT next_bit = next_offset % BITS_PER_UNIT;
4672 /* Advance from byte to byte
4673 within this element when necessary. */
4674 while (next_byte != total_bytes)
4676 assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
4677 total_bytes++;
4678 byte = 0;
4681 /* Number of bits we can process at once
4682 (all part of the same byte). */
4683 this_time = MIN (end_offset - next_offset,
4684 BITS_PER_UNIT - next_bit);
4685 if (BYTES_BIG_ENDIAN)
4687 /* On big-endian machine, take the most significant bits
4688 first (of the bits that are significant)
4689 and put them into bytes from the most significant end. */
4690 shift = end_offset - next_offset - this_time;
4692 /* Don't try to take a bunch of bits that cross
4693 the word boundary in the INTEGER_CST. We can
4694 only select bits from the LOW or HIGH part
4695 not from both. */
4696 if (shift < HOST_BITS_PER_WIDE_INT
4697 && shift + this_time > HOST_BITS_PER_WIDE_INT)
4699 this_time = shift + this_time - HOST_BITS_PER_WIDE_INT;
4700 shift = HOST_BITS_PER_WIDE_INT;
4703 /* Now get the bits from the appropriate constant word. */
4704 if (shift < HOST_BITS_PER_WIDE_INT)
4705 value = TREE_INT_CST_LOW (val);
4706 else if (shift < 2 * HOST_BITS_PER_WIDE_INT)
4708 value = TREE_INT_CST_HIGH (val);
4709 shift -= HOST_BITS_PER_WIDE_INT;
4711 else
4712 abort ();
4714 /* Get the result. This works only when:
4715 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
4716 byte |= (((value >> shift)
4717 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
4718 << (BITS_PER_UNIT - this_time - next_bit));
4720 else
4722 /* On little-endian machines,
4723 take first the least significant bits of the value
4724 and pack them starting at the least significant
4725 bits of the bytes. */
4726 shift = next_offset - int_bit_position (field);
4728 /* Don't try to take a bunch of bits that cross
4729 the word boundary in the INTEGER_CST. We can
4730 only select bits from the LOW or HIGH part
4731 not from both. */
4732 if (shift < HOST_BITS_PER_WIDE_INT
4733 && shift + this_time > HOST_BITS_PER_WIDE_INT)
4734 this_time = (HOST_BITS_PER_WIDE_INT - shift);
4736 /* Now get the bits from the appropriate constant word. */
4737 if (shift < HOST_BITS_PER_WIDE_INT)
4738 value = TREE_INT_CST_LOW (val);
4739 else if (shift < 2 * HOST_BITS_PER_WIDE_INT)
4741 value = TREE_INT_CST_HIGH (val);
4742 shift -= HOST_BITS_PER_WIDE_INT;
4744 else
4745 abort ();
4747 /* Get the result. This works only when:
4748 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
4749 byte |= (((value >> shift)
4750 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
4751 << next_bit);
4754 next_offset += this_time;
4755 byte_buffer_in_use = 1;
4760 if (byte_buffer_in_use)
4762 assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
4763 total_bytes++;
4766 if (total_bytes < size)
4767 assemble_zeros (size - total_bytes);
4770 /* This TREE_LIST contains any weak symbol declarations waiting
4771 to be emitted. */
4772 static tree weak_decls;
4774 /* Mark DECL as weak. */
4776 static void
4777 mark_weak (decl)
4778 tree decl;
4780 DECL_WEAK (decl) = 1;
4782 if (DECL_RTL_SET_P (decl)
4783 && GET_CODE (DECL_RTL (decl)) == MEM
4784 && XEXP (DECL_RTL (decl), 0)
4785 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == SYMBOL_REF)
4786 SYMBOL_REF_WEAK (XEXP (DECL_RTL (decl), 0)) = 1;
4789 /* Merge weak status between NEWDECL and OLDDECL. */
4791 void
4792 merge_weak (newdecl, olddecl)
4793 tree newdecl;
4794 tree olddecl;
4796 if (DECL_WEAK (newdecl) == DECL_WEAK (olddecl))
4797 return;
4799 if (DECL_WEAK (newdecl))
4801 tree wd;
4803 /* NEWDECL is weak, but OLDDECL is not. */
4805 /* If we already output the OLDDECL, we're in trouble; we can't
4806 go back and make it weak. This error cannot caught in
4807 declare_weak because the NEWDECL and OLDDECL was not yet
4808 been merged; therefore, TREE_ASM_WRITTEN was not set. */
4809 if (TREE_ASM_WRITTEN (olddecl))
4810 error_with_decl (newdecl,
4811 "weak declaration of `%s' must precede definition");
4813 /* If we've already generated rtl referencing OLDDECL, we may
4814 have done so in a way that will not function properly with
4815 a weak symbol. */
4816 else if (TREE_USED (olddecl)
4817 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (olddecl)))
4818 warning_with_decl (newdecl, "weak declaration of `%s' after first use results in unspecified behavior");
4820 if (SUPPORTS_WEAK)
4822 /* We put the NEWDECL on the weak_decls list at some point.
4823 Replace it with the OLDDECL. */
4824 for (wd = weak_decls; wd; wd = TREE_CHAIN (wd))
4825 if (TREE_VALUE (wd) == newdecl)
4827 TREE_VALUE (wd) = olddecl;
4828 break;
4830 /* We may not find the entry on the list. If NEWDECL is a
4831 weak alias, then we will have already called
4832 globalize_decl to remove the entry; in that case, we do
4833 not need to do anything. */
4836 /* Make the OLDDECL weak; it's OLDDECL that we'll be keeping. */
4837 mark_weak (olddecl);
4839 else
4840 /* OLDDECL was weak, but NEWDECL was not explicitly marked as
4841 weak. Just update NEWDECL to indicate that it's weak too. */
4842 mark_weak (newdecl);
4845 /* Declare DECL to be a weak symbol. */
4847 void
4848 declare_weak (decl)
4849 tree decl;
4851 if (! TREE_PUBLIC (decl))
4852 error_with_decl (decl, "weak declaration of `%s' must be public");
4853 else if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
4854 error_with_decl (decl, "weak declaration of `%s' must precede definition");
4855 else if (SUPPORTS_WEAK)
4857 if (! DECL_WEAK (decl))
4858 weak_decls = tree_cons (NULL, decl, weak_decls);
4860 else
4861 warning_with_decl (decl, "weak declaration of `%s' not supported");
4863 mark_weak (decl);
4866 /* Emit any pending weak declarations. */
4868 void
4869 weak_finish ()
4871 tree t;
4873 for (t = weak_decls; t; t = TREE_CHAIN (t))
4875 tree decl = TREE_VALUE (t);
4876 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
4878 if (! TREE_USED (decl))
4879 continue;
4881 #ifdef ASM_WEAKEN_DECL
4882 ASM_WEAKEN_DECL (asm_out_file, decl, name, NULL);
4883 #else
4884 #ifdef ASM_WEAKEN_LABEL
4885 ASM_WEAKEN_LABEL (asm_out_file, name);
4886 #else
4887 #ifdef ASM_OUTPUT_WEAK_ALIAS
4888 warning ("only weak aliases are supported in this configuration");
4889 return;
4890 #endif
4891 #endif
4892 #endif
4896 /* Emit the assembly bits to indicate that DECL is globally visible. */
4898 static void
4899 globalize_decl (decl)
4900 tree decl;
4902 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
4904 #if defined (ASM_WEAKEN_LABEL) || defined (ASM_WEAKEN_DECL)
4905 if (DECL_WEAK (decl))
4907 tree *p, t;
4909 #ifdef ASM_WEAKEN_DECL
4910 ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
4911 #else
4912 ASM_WEAKEN_LABEL (asm_out_file, name);
4913 #endif
4915 /* Remove this function from the pending weak list so that
4916 we do not emit multiple .weak directives for it. */
4917 for (p = &weak_decls; (t = *p) ; )
4919 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
4920 *p = TREE_CHAIN (t);
4921 else
4922 p = &TREE_CHAIN (t);
4924 return;
4926 #endif
4928 ASM_GLOBALIZE_LABEL (asm_out_file, name);
4931 /* Emit an assembler directive to make the symbol for DECL an alias to
4932 the symbol for TARGET. */
4934 void
4935 assemble_alias (decl, target)
4936 tree decl, target ATTRIBUTE_UNUSED;
4938 const char *name;
4940 /* We must force creation of DECL_RTL for debug info generation, even though
4941 we don't use it here. */
4942 make_decl_rtl (decl, NULL);
4944 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
4946 #ifdef ASM_OUTPUT_DEF
4947 /* Make name accessible from other files, if appropriate. */
4949 if (TREE_PUBLIC (decl))
4951 globalize_decl (decl);
4952 maybe_assemble_visibility (decl);
4955 #ifdef ASM_OUTPUT_DEF_FROM_DECLS
4956 ASM_OUTPUT_DEF_FROM_DECLS (asm_out_file, decl, target);
4957 #else
4958 ASM_OUTPUT_DEF (asm_out_file, name, IDENTIFIER_POINTER (target));
4959 #endif
4960 #else /* !ASM_OUTPUT_DEF */
4961 #if defined (ASM_OUTPUT_WEAK_ALIAS) || defined (ASM_WEAKEN_DECL)
4962 if (! DECL_WEAK (decl))
4963 warning ("only weak aliases are supported in this configuration");
4965 #ifdef ASM_WEAKEN_DECL
4966 ASM_WEAKEN_DECL (asm_out_file, decl, name, IDENTIFIER_POINTER (target));
4967 #else
4968 ASM_OUTPUT_WEAK_ALIAS (asm_out_file, name, IDENTIFIER_POINTER (target));
4969 #endif
4970 #else
4971 warning ("alias definitions not supported in this configuration; ignored");
4972 #endif
4973 #endif
4975 TREE_USED (decl) = 1;
4976 TREE_ASM_WRITTEN (decl) = 1;
4977 TREE_ASM_WRITTEN (DECL_ASSEMBLER_NAME (decl)) = 1;
4980 /* Emit an assembler directive to set symbol for DECL visibility to
4981 VISIBILITY_TYPE. */
4983 void
4984 assemble_visibility (decl, visibility_type)
4985 tree decl;
4986 const char *visibility_type ATTRIBUTE_UNUSED;
4988 const char *name;
4990 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
4992 #ifdef HAVE_GAS_HIDDEN
4993 fprintf (asm_out_file, "\t.%s\t%s\n", visibility_type, name);
4994 #else
4995 warning ("visibility attribute not supported in this configuration; ignored");
4996 #endif
4999 /* A helper function to call assemble_visibility when needed for a decl. */
5001 static void
5002 maybe_assemble_visibility (decl)
5003 tree decl;
5005 tree visibility = lookup_attribute ("visibility", DECL_ATTRIBUTES (decl));
5006 if (visibility)
5008 const char *type
5009 = TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (visibility)));
5010 assemble_visibility (decl, type);
5014 /* Returns 1 if the target configuration supports defining public symbols
5015 so that one of them will be chosen at link time instead of generating a
5016 multiply-defined symbol error, whether through the use of weak symbols or
5017 a target-specific mechanism for having duplicates discarded. */
5020 supports_one_only ()
5022 if (SUPPORTS_ONE_ONLY)
5023 return 1;
5024 return SUPPORTS_WEAK;
5027 /* Set up DECL as a public symbol that can be defined in multiple
5028 translation units without generating a linker error. */
5030 void
5031 make_decl_one_only (decl)
5032 tree decl;
5034 if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != FUNCTION_DECL)
5035 abort ();
5037 TREE_PUBLIC (decl) = 1;
5039 if (TREE_CODE (decl) == VAR_DECL
5040 && (DECL_INITIAL (decl) == 0 || DECL_INITIAL (decl) == error_mark_node))
5041 DECL_COMMON (decl) = 1;
5042 else if (SUPPORTS_ONE_ONLY)
5044 #ifdef MAKE_DECL_ONE_ONLY
5045 MAKE_DECL_ONE_ONLY (decl);
5046 #endif
5047 DECL_ONE_ONLY (decl) = 1;
5049 else if (SUPPORTS_WEAK)
5050 DECL_WEAK (decl) = 1;
5051 else
5052 abort ();
5055 void
5056 init_varasm_once ()
5058 const_str_htab = htab_create (128, const_str_htab_hash, const_str_htab_eq,
5059 const_str_htab_del);
5060 in_named_htab = htab_create (31, in_named_entry_hash,
5061 in_named_entry_eq, NULL);
5063 ggc_add_root (const_hash_table, MAX_HASH_TABLE, sizeof const_hash_table[0],
5064 mark_const_hash_entry);
5065 ggc_add_root (&const_str_htab, 1, sizeof const_str_htab,
5066 mark_const_str_htab);
5067 ggc_add_tree_root (&weak_decls, 1);
5069 const_alias_set = new_alias_set ();
5072 /* Select a set of attributes for section NAME based on the properties
5073 of DECL and whether or not RELOC indicates that DECL's initializer
5074 might contain runtime relocations.
5076 We make the section read-only and executable for a function decl,
5077 read-only for a const data decl, and writable for a non-const data decl. */
5079 unsigned int
5080 default_section_type_flags (decl, name, reloc)
5081 tree decl;
5082 const char *name;
5083 int reloc;
5085 unsigned int flags;
5087 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
5088 flags = SECTION_CODE;
5089 else if (decl && DECL_READONLY_SECTION (decl, reloc))
5090 flags = 0;
5091 else
5092 flags = SECTION_WRITE;
5094 if (decl && DECL_ONE_ONLY (decl))
5095 flags |= SECTION_LINKONCE;
5097 if (strcmp (name, ".bss") == 0
5098 || strncmp (name, ".bss.", 5) == 0
5099 || strncmp (name, ".gnu.linkonce.b.", 16) == 0
5100 || strcmp (name, ".sbss") == 0
5101 || strncmp (name, ".sbss.", 6) == 0
5102 || strncmp (name, ".gnu.linkonce.sb.", 17) == 0)
5103 flags |= SECTION_BSS;
5105 return flags;
5108 /* Output assembly to switch to section NAME with attribute FLAGS.
5109 Four variants for common object file formats. */
5111 void
5112 default_no_named_section (name, flags)
5113 const char *name ATTRIBUTE_UNUSED;
5114 unsigned int flags ATTRIBUTE_UNUSED;
5116 /* Some object formats don't support named sections at all. The
5117 front-end should already have flagged this as an error. */
5118 abort ();
5121 void
5122 default_elf_asm_named_section (name, flags)
5123 const char *name;
5124 unsigned int flags;
5126 char flagchars[10], *f = flagchars;
5127 const char *type;
5129 if (! named_section_first_declaration (name))
5131 fprintf (asm_out_file, "\t.section\t%s\n", name);
5132 return;
5135 if (!(flags & SECTION_DEBUG))
5136 *f++ = 'a';
5137 if (flags & SECTION_WRITE)
5138 *f++ = 'w';
5139 if (flags & SECTION_CODE)
5140 *f++ = 'x';
5141 if (flags & SECTION_SMALL)
5142 *f++ = 's';
5143 if (flags & SECTION_MERGE)
5144 *f++ = 'M';
5145 if (flags & SECTION_STRINGS)
5146 *f++ = 'S';
5147 *f = '\0';
5149 if (flags & SECTION_BSS)
5150 type = "nobits";
5151 else
5152 type = "progbits";
5154 if (flags & SECTION_ENTSIZE)
5155 fprintf (asm_out_file, "\t.section\t%s,\"%s\",@%s,%d\n",
5156 name, flagchars, type, flags & SECTION_ENTSIZE);
5157 else
5158 fprintf (asm_out_file, "\t.section\t%s,\"%s\",@%s\n",
5159 name, flagchars, type);
5162 void
5163 default_coff_asm_named_section (name, flags)
5164 const char *name;
5165 unsigned int flags;
5167 char flagchars[8], *f = flagchars;
5169 if (flags & SECTION_WRITE)
5170 *f++ = 'w';
5171 if (flags & SECTION_CODE)
5172 *f++ = 'x';
5173 *f = '\0';
5175 fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
5178 void
5179 default_pe_asm_named_section (name, flags)
5180 const char *name;
5181 unsigned int flags;
5183 default_coff_asm_named_section (name, flags);
5185 if (flags & SECTION_LINKONCE)
5187 /* Functions may have been compiled at various levels of
5188 optimization so we can't use `same_size' here.
5189 Instead, have the linker pick one. */
5190 fprintf (asm_out_file, "\t.linkonce %s\n",
5191 (flags & SECTION_CODE ? "discard" : "same_size"));
5195 /* Used for vtable gc in GNU binutils. Record that the pointer at OFFSET
5196 from SYMBOL is used in all classes derived from SYMBOL. */
5198 void
5199 assemble_vtable_entry (symbol, offset)
5200 rtx symbol;
5201 HOST_WIDE_INT offset;
5203 fputs ("\t.vtable_entry ", asm_out_file);
5204 output_addr_const (asm_out_file, symbol);
5205 fputs (", ", asm_out_file);
5206 fprintf (asm_out_file, HOST_WIDE_INT_PRINT_DEC, offset);
5207 fputc ('\n', asm_out_file);
5210 /* Used for vtable gc in GNU binutils. Record the class hierarchy by noting
5211 that the vtable symbol CHILD is derived from the vtable symbol PARENT. */
5213 void
5214 assemble_vtable_inherit (child, parent)
5215 rtx child, parent;
5217 fputs ("\t.vtable_inherit ", asm_out_file);
5218 output_addr_const (asm_out_file, child);
5219 fputs (", ", asm_out_file);
5220 output_addr_const (asm_out_file, parent);
5221 fputc ('\n', asm_out_file);
5224 /* The lame default section selector. */
5226 void
5227 default_select_section (decl, reloc, align)
5228 tree decl;
5229 int reloc;
5230 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED;
5232 bool readonly = false;
5234 if (DECL_P (decl))
5236 if (DECL_READONLY_SECTION (decl, reloc))
5237 readonly = true;
5239 else if (TREE_CODE (decl) == CONSTRUCTOR)
5241 if (! ((flag_pic && reloc)
5242 || !TREE_READONLY (decl)
5243 || TREE_SIDE_EFFECTS (decl)
5244 || !TREE_CONSTANT (decl)))
5245 readonly = true;
5247 else if (TREE_CODE (decl) == STRING_CST)
5248 readonly = !flag_writable_strings;
5249 else if (! (flag_pic && reloc))
5250 readonly = true;
5252 if (readonly)
5253 readonly_data_section ();
5254 else
5255 data_section ();
5258 /* A helper function for default_elf_select_section and
5259 default_elf_unique_section. Categorizes the DECL. */
5261 enum section_category
5263 SECCAT_TEXT,
5265 SECCAT_RODATA,
5266 SECCAT_RODATA_MERGE_STR,
5267 SECCAT_RODATA_MERGE_STR_INIT,
5268 SECCAT_RODATA_MERGE_CONST,
5270 SECCAT_DATA,
5272 /* To optimize loading of shared programs, define following subsections
5273 of data section:
5274 _REL Contains data that has relocations, so they get grouped
5275 together and dynamic linker will visit fewer pages in memory.
5276 _RO Contains data that is otherwise read-only. This is useful
5277 with prelinking as most relocations won't be dynamically
5278 linked and thus stay read only.
5279 _LOCAL Marks data containing relocations only to local objects.
5280 These relocations will get fully resolved by prelinking. */
5281 SECCAT_DATA_REL,
5282 SECCAT_DATA_REL_LOCAL,
5283 SECCAT_DATA_REL_RO,
5284 SECCAT_DATA_REL_RO_LOCAL,
5286 SECCAT_SDATA,
5287 SECCAT_TDATA,
5289 SECCAT_BSS,
5290 SECCAT_SBSS,
5291 SECCAT_TBSS
5294 static enum section_category categorize_decl_for_section PARAMS ((tree, int));
5296 static enum section_category
5297 categorize_decl_for_section (decl, reloc)
5298 tree decl;
5299 int reloc;
5301 enum section_category ret;
5303 if (TREE_CODE (decl) == FUNCTION_DECL)
5304 return SECCAT_TEXT;
5305 else if (TREE_CODE (decl) == STRING_CST)
5307 if (flag_writable_strings)
5308 return SECCAT_DATA;
5309 else
5310 return SECCAT_RODATA_MERGE_STR;
5312 else if (TREE_CODE (decl) == VAR_DECL)
5314 if (DECL_INITIAL (decl) == NULL
5315 || DECL_INITIAL (decl) == error_mark_node)
5316 ret = SECCAT_BSS;
5317 else if (! TREE_READONLY (decl)
5318 || TREE_SIDE_EFFECTS (decl)
5319 || ! TREE_CONSTANT (DECL_INITIAL (decl)))
5321 if (flag_pic && (reloc & 2))
5322 ret = SECCAT_DATA_REL;
5323 else if (flag_pic && reloc)
5324 ret = SECCAT_DATA_REL_LOCAL;
5325 else
5326 ret = SECCAT_DATA;
5328 else if (flag_pic && (reloc & 2))
5329 ret = SECCAT_DATA_REL_RO;
5330 else if (flag_pic && reloc)
5331 ret = SECCAT_DATA_REL_RO_LOCAL;
5332 else if (flag_merge_constants < 2)
5333 /* C and C++ don't allow different variables to share the same
5334 location. -fmerge-all-constants allows even that (at the
5335 expense of not conforming). */
5336 ret = SECCAT_RODATA;
5337 else if (TREE_CODE (DECL_INITIAL (decl)) == STRING_CST)
5338 ret = SECCAT_RODATA_MERGE_STR_INIT;
5339 else
5340 ret = SECCAT_RODATA_MERGE_CONST;
5342 else if (TREE_CODE (decl) == CONSTRUCTOR)
5344 if ((flag_pic && reloc)
5345 || TREE_SIDE_EFFECTS (decl)
5346 || ! TREE_CONSTANT (decl))
5347 ret = SECCAT_DATA;
5348 else
5349 ret = SECCAT_RODATA;
5351 else
5352 ret = SECCAT_RODATA;
5354 /* If the target uses small data sections, select it. */
5355 if ((*targetm.in_small_data_p) (decl))
5357 if (ret == SECCAT_BSS)
5358 ret = SECCAT_SBSS;
5359 else
5360 ret = SECCAT_SDATA;
5363 return ret;
5366 /* Select a section based on the above categorization. */
5368 void
5369 default_elf_select_section (decl, reloc, align)
5370 tree decl;
5371 int reloc;
5372 unsigned HOST_WIDE_INT align;
5374 switch (categorize_decl_for_section (decl, reloc))
5376 case SECCAT_TEXT:
5377 /* We're not supposed to be called on FUNCTION_DECLs. */
5378 abort ();
5379 case SECCAT_RODATA:
5380 readonly_data_section ();
5381 break;
5382 case SECCAT_RODATA_MERGE_STR:
5383 mergeable_string_section (decl, align, 0);
5384 break;
5385 case SECCAT_RODATA_MERGE_STR_INIT:
5386 mergeable_string_section (DECL_INITIAL (decl), align, 0);
5387 break;
5388 case SECCAT_RODATA_MERGE_CONST:
5389 mergeable_constant_section (DECL_MODE (decl), align, 0);
5390 break;
5391 case SECCAT_DATA:
5392 data_section ();
5393 break;
5394 case SECCAT_DATA_REL:
5395 named_section (NULL_TREE, ".data.rel", reloc);
5396 break;
5397 case SECCAT_DATA_REL_LOCAL:
5398 named_section (NULL_TREE, ".data.rel.local", reloc);
5399 break;
5400 case SECCAT_DATA_REL_RO:
5401 named_section (NULL_TREE, ".data.rel.ro", reloc);
5402 break;
5403 case SECCAT_DATA_REL_RO_LOCAL:
5404 named_section (NULL_TREE, ".data.rel.ro.local", reloc);
5405 break;
5406 case SECCAT_SDATA:
5407 named_section (NULL_TREE, ".sdata", reloc);
5408 break;
5409 case SECCAT_TDATA:
5410 named_section (NULL_TREE, ".tdata", reloc);
5411 break;
5412 case SECCAT_BSS:
5413 #ifdef BSS_SECTION_ASM_OP
5414 bss_section ();
5415 #else
5416 named_section (NULL_TREE, ".bss", reloc);
5417 #endif
5418 break;
5419 case SECCAT_SBSS:
5420 named_section (NULL_TREE, ".sbss", reloc);
5421 break;
5422 case SECCAT_TBSS:
5423 named_section (NULL_TREE, ".tbss", reloc);
5424 break;
5425 default:
5426 abort ();
5430 /* Construct a unique section name based on the decl name and the
5431 categorization performed above. */
5433 void
5434 default_unique_section (decl, reloc)
5435 tree decl;
5436 int reloc;
5438 bool one_only = DECL_ONE_ONLY (decl);
5439 const char *prefix, *name;
5440 size_t nlen, plen;
5441 char *string;
5443 switch (categorize_decl_for_section (decl, reloc))
5445 case SECCAT_TEXT:
5446 prefix = one_only ? ".gnu.linkonce.t." : ".text.";
5447 break;
5448 case SECCAT_RODATA:
5449 case SECCAT_RODATA_MERGE_STR:
5450 case SECCAT_RODATA_MERGE_STR_INIT:
5451 case SECCAT_RODATA_MERGE_CONST:
5452 prefix = one_only ? ".gnu.linkonce.r." : ".rodata.";
5453 break;
5454 case SECCAT_DATA:
5455 case SECCAT_DATA_REL:
5456 case SECCAT_DATA_REL_LOCAL:
5457 case SECCAT_DATA_REL_RO:
5458 case SECCAT_DATA_REL_RO_LOCAL:
5459 prefix = one_only ? ".gnu.linkonce.d." : ".data.";
5460 break;
5461 case SECCAT_SDATA:
5462 prefix = one_only ? ".gnu.linkonce.s." : ".sdata.";
5463 break;
5464 case SECCAT_BSS:
5465 prefix = one_only ? ".gnu.linkonce.b." : ".bss.";
5466 break;
5467 case SECCAT_SBSS:
5468 prefix = one_only ? ".gnu.linkonce.sb." : ".sbss.";
5469 break;
5470 case SECCAT_TDATA:
5471 case SECCAT_TBSS:
5472 default:
5473 abort ();
5475 plen = strlen (prefix);
5477 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5478 STRIP_NAME_ENCODING (name, name);
5479 nlen = strlen (name);
5481 string = alloca (nlen + plen + 1);
5482 memcpy (string, prefix, plen);
5483 memcpy (string + plen, name, nlen + 1);
5485 DECL_SECTION_NAME (decl) = build_string (nlen + plen, string);