OSDN Git Service

2004-04-09 Caroline Tice <ctice@apple.com>
[pf3gnuchains/gcc-fork.git] / gcc / varasm.c
1 /* Output variables, constants and external declarations, for GNU compiler.
2    Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997,
3    1998, 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
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.
11
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.
16
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.  */
21
22
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.
26
27    We also output the assembler code for constants stored in memory
28    and are responsible for combining constants with the same value.  */
29
30 #include "config.h"
31 #include "system.h"
32 #include "coretypes.h"
33 #include "tm.h"
34 #include "rtl.h"
35 #include "tree.h"
36 #include "flags.h"
37 #include "function.h"
38 #include "expr.h"
39 #include "hard-reg-set.h"
40 #include "regs.h"
41 #include "real.h"
42 #include "output.h"
43 #include "toplev.h"
44 #include "hashtab.h"
45 #include "c-pragma.h"
46 #include "ggc.h"
47 #include "langhooks.h"
48 #include "tm_p.h"
49 #include "debug.h"
50 #include "target.h"
51 #include "cgraph.h"
52 #include "cfglayout.h"
53
54 #ifdef XCOFF_DEBUGGING_INFO
55 #include "xcoffout.h"           /* Needed for external data
56                                    declarations for e.g. AIX 4.x.  */
57 #endif
58
59 #ifndef TRAMPOLINE_ALIGNMENT
60 #define TRAMPOLINE_ALIGNMENT FUNCTION_BOUNDARY
61 #endif
62
63 #ifndef ASM_STABS_OP
64 #define ASM_STABS_OP "\t.stabs\t"
65 #endif
66
67 /* The (assembler) name of the first globally-visible object output.  */
68 const char *first_global_object_name;
69 const char *weak_global_object_name;
70
71 struct addr_const;
72 struct constant_descriptor_rtx;
73 struct rtx_constant_pool;
74
75 struct varasm_status GTY(())
76 {
77   /* If we're using a per-function constant pool, this is it.  */
78   struct rtx_constant_pool *pool;
79
80   /* Number of tree-constants deferred during the expansion of this
81      function.  */
82   unsigned int deferred_constants;
83 };
84
85 #define n_deferred_constants (cfun->varasm->deferred_constants)
86
87 /* Number for making the label on the next
88    constant that is stored in memory.  */
89
90 static GTY(()) int const_labelno;
91
92 /* Carry information from ASM_DECLARE_OBJECT_NAME
93    to ASM_FINISH_DECLARE_OBJECT.  */
94
95 int size_directive_output;
96
97 /* The last decl for which assemble_variable was called,
98    if it did ASM_DECLARE_OBJECT_NAME.
99    If the last call to assemble_variable didn't do that,
100    this holds 0.  */
101
102 tree last_assemble_variable_decl;
103
104 /* The following global variable indicates if the section label for the
105    "cold" section of code has been output yet to the assembler.  The
106    label is useful when running gdb.  This is part of the optimization that
107    partitions hot and cold basic blocks into separate sections of the .o
108    file.  */
109
110 bool unlikely_section_label_printed = false;
111
112 /* RTX_UNCHANGING_P in a MEM can mean it is stored into, for initialization.
113    So giving constant the alias set for the type will allow such
114    initializations to appear to conflict with the load of the constant.  We
115    avoid this by giving all constants an alias set for just constants.
116    Since there will be no stores to that alias set, nothing will ever
117    conflict with them.  */
118
119 static HOST_WIDE_INT const_alias_set;
120
121 static const char *strip_reg_name (const char *);
122 static int contains_pointers_p (tree);
123 #ifdef ASM_OUTPUT_EXTERNAL
124 static bool incorporeal_function_p (tree);
125 #endif
126 static void decode_addr_const (tree, struct addr_const *);
127 static hashval_t const_desc_hash (const void *);
128 static int const_desc_eq (const void *, const void *);
129 static hashval_t const_hash_1 (const tree);
130 static int compare_constant (const tree, const tree);
131 static tree copy_constant (tree);
132 static void output_constant_def_contents (rtx);
133 static void output_addressed_constants (tree);
134 static unsigned HOST_WIDE_INT array_size_for_constructor (tree);
135 static unsigned min_align (unsigned, unsigned);
136 static void output_constructor (tree, unsigned HOST_WIDE_INT, unsigned int);
137 static void globalize_decl (tree);
138 static void maybe_assemble_visibility (tree);
139 static int in_named_entry_eq (const void *, const void *);
140 static hashval_t in_named_entry_hash (const void *);
141 #ifdef ASM_OUTPUT_BSS
142 static void asm_output_bss (FILE *, tree, const char *,
143                             unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
144 #endif
145 #ifdef BSS_SECTION_ASM_OP
146 #ifdef ASM_OUTPUT_ALIGNED_BSS
147 static void asm_output_aligned_bss (FILE *, tree, const char *,
148                                     unsigned HOST_WIDE_INT, int)
149      ATTRIBUTE_UNUSED;
150 #endif
151 #endif /* BSS_SECTION_ASM_OP */
152 static bool asm_emit_uninitialised (tree, const char*,
153                                     unsigned HOST_WIDE_INT,
154                                     unsigned HOST_WIDE_INT);
155 static void mark_weak (tree);
156 \f
157 enum in_section { no_section, in_text, in_unlikely_executed_text, in_data, 
158                   in_named
159 #ifdef BSS_SECTION_ASM_OP
160   , in_bss
161 #endif
162 #ifdef CTORS_SECTION_ASM_OP
163   , in_ctors
164 #endif
165 #ifdef DTORS_SECTION_ASM_OP
166   , in_dtors
167 #endif
168 #ifdef READONLY_DATA_SECTION_ASM_OP
169   , in_readonly_data
170 #endif
171 #ifdef EXTRA_SECTIONS
172   , EXTRA_SECTIONS
173 #endif
174 };
175 static GTY(()) enum in_section in_section = no_section;
176
177 /* Return a nonzero value if DECL has a section attribute.  */
178 #ifndef IN_NAMED_SECTION
179 #define IN_NAMED_SECTION(DECL) \
180   ((TREE_CODE (DECL) == FUNCTION_DECL || TREE_CODE (DECL) == VAR_DECL) \
181    && DECL_SECTION_NAME (DECL) != NULL_TREE)
182 #endif
183
184 /* Text of section name when in_section == in_named.  */
185 static GTY(()) const char *in_named_name;
186
187 /* Hash table of flags that have been used for a particular named section.  */
188
189 struct in_named_entry GTY(())
190 {
191   const char *name;
192   unsigned int flags;
193   bool declared;
194 };
195
196 static GTY((param_is (struct in_named_entry))) htab_t in_named_htab;
197
198 /* Define functions like text_section for any extra sections.  */
199 #ifdef EXTRA_SECTION_FUNCTIONS
200 EXTRA_SECTION_FUNCTIONS
201 #endif
202
203 /* Tell assembler to switch to text section.  */
204
205 void
206 text_section (void)
207 {
208   if (in_section != in_text)
209     {
210       in_section = in_text;
211       fprintf (asm_out_file, SECTION_FORMAT_STRING, NORMAL_TEXT_SECTION_NAME);
212     }
213 }
214
215 /* Tell assembler to switch to unlikely-to-be-executed text section.  */
216
217 void
218 unlikely_text_section (void)
219 {
220   if ((in_section != in_unlikely_executed_text)
221       &&  (in_section != in_named 
222            || strcmp (in_named_name, UNLIKELY_EXECUTED_TEXT_SECTION_NAME) != 0))
223     {
224 #ifdef TARGET_ASM_NAMED_SECTION
225         
226       named_section (NULL_TREE, UNLIKELY_EXECUTED_TEXT_SECTION_NAME, 0);
227       
228 #else
229       in_section = in_unlikely_executed_text;
230       fprintf (asm_out_file, SECTION_FORMAT_STRING, 
231                UNLIKELY_EXECUTED_TEXT_SECTION_NAME);
232 #endif /* ifdef TARGET_ASM_NAMED_SECTION */
233       if (!unlikely_section_label_printed)
234         {
235           fprintf (asm_out_file, "__%s_unlikely_section:\n", 
236                    current_function_name ());
237           unlikely_section_label_printed = true;
238         }
239     }
240 }
241
242 /* Tell assembler to switch to data section.  */
243
244 void
245 data_section (void)
246 {
247   if (in_section != in_data)
248     {
249       in_section = in_data;
250       fprintf (asm_out_file, "%s\n", DATA_SECTION_ASM_OP);
251     }
252 }
253
254 /* Tell assembler to switch to read-only data section.  This is normally
255    the text section.  */
256
257 void
258 readonly_data_section (void)
259 {
260 #ifdef READONLY_DATA_SECTION
261   READONLY_DATA_SECTION ();  /* Note this can call data_section.  */
262 #else
263 #ifdef READONLY_DATA_SECTION_ASM_OP
264   if (in_section != in_readonly_data)
265     {
266       in_section = in_readonly_data;
267       fputs (READONLY_DATA_SECTION_ASM_OP, asm_out_file);
268       fputc ('\n', asm_out_file);
269     }
270 #else
271   text_section ();
272 #endif
273 #endif
274 }
275
276 /* Determine if we're in the text section.  */
277
278 int
279 in_text_section (void)
280 {
281   return in_section == in_text;
282 }
283
284 /* Determine if we're in the unlikely-to-be-executed text section.  */
285
286 int
287 in_unlikely_text_section (void)
288 {
289   return in_section == in_unlikely_executed_text;
290 }
291
292 /* Determine if we're in the data section.  */
293
294 int
295 in_data_section (void)
296 {
297   return in_section == in_data;
298 }
299
300 /* Helper routines for maintaining in_named_htab.  */
301
302 static int
303 in_named_entry_eq (const void *p1, const void *p2)
304 {
305   const struct in_named_entry *old = p1;
306   const char *new = p2;
307
308   return strcmp (old->name, new) == 0;
309 }
310
311 static hashval_t
312 in_named_entry_hash (const void *p)
313 {
314   const struct in_named_entry *old = p;
315   return htab_hash_string (old->name);
316 }
317
318 /* If SECTION has been seen before as a named section, return the flags
319    that were used.  Otherwise, return 0.  Note, that 0 is a perfectly valid
320    set of flags for a section to have, so 0 does not mean that the section
321    has not been seen.  */
322
323 unsigned int
324 get_named_section_flags (const char *section)
325 {
326   struct in_named_entry **slot;
327
328   slot = (struct in_named_entry **)
329     htab_find_slot_with_hash (in_named_htab, section,
330                               htab_hash_string (section), NO_INSERT);
331
332   return slot ? (*slot)->flags : 0;
333 }
334
335 /* Returns true if the section has been declared before.   Sets internal
336    flag on this section in in_named_hash so subsequent calls on this
337    section will return false.  */
338
339 bool
340 named_section_first_declaration (const char *name)
341 {
342   struct in_named_entry **slot;
343
344   slot = (struct in_named_entry **)
345     htab_find_slot_with_hash (in_named_htab, name,
346                               htab_hash_string (name), NO_INSERT);
347   if (! (*slot)->declared)
348     {
349       (*slot)->declared = true;
350       return true;
351     }
352   else
353     {
354       return false;
355     }
356 }
357
358
359 /* Record FLAGS for SECTION.  If SECTION was previously recorded with a
360    different set of flags, return false.  */
361
362 bool
363 set_named_section_flags (const char *section, unsigned int flags)
364 {
365   struct in_named_entry **slot, *entry;
366
367   slot = (struct in_named_entry **)
368     htab_find_slot_with_hash (in_named_htab, section,
369                               htab_hash_string (section), INSERT);
370   entry = *slot;
371
372   if (!entry)
373     {
374       entry = ggc_alloc (sizeof (*entry));
375       *slot = entry;
376       entry->name = ggc_strdup (section);
377       entry->flags = flags;
378       entry->declared = false;
379     }
380   else if (entry->flags != flags)
381     return false;
382
383   return true;
384 }
385
386 /* Tell assembler to change to section NAME with attributes FLAGS.  */
387
388 void
389 named_section_flags (const char *name, unsigned int flags)
390 {
391   if (in_section != in_named || strcmp (name, in_named_name) != 0)
392     {
393       if (! set_named_section_flags (name, flags))
394         abort ();
395
396       targetm.asm_out.named_section (name, flags);
397
398       if (flags & SECTION_FORGET)
399         in_section = no_section;
400       else
401         {
402           in_named_name = ggc_strdup (name);
403           in_section = in_named;
404         }
405     }
406 }
407
408 /* Tell assembler to change to section NAME for DECL.
409    If DECL is NULL, just switch to section NAME.
410    If NAME is NULL, get the name from DECL.
411    If RELOC is 1, the initializer for DECL contains relocs.  */
412
413 void
414 named_section (tree decl, const char *name, int reloc)
415 {
416   unsigned int flags;
417
418   if (decl != NULL_TREE && !DECL_P (decl))
419     abort ();
420   if (name == NULL)
421     name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
422
423   flags = targetm.section_type_flags (decl, name, reloc);
424
425   /* Sanity check user variables for flag changes.  Non-user
426      section flag changes will abort in named_section_flags.
427      However, don't complain if SECTION_OVERRIDE is set.
428      We trust that the setter knows that it is safe to ignore
429      the default flags for this decl.  */
430   if (decl && ! set_named_section_flags (name, flags))
431     {
432       flags = get_named_section_flags (name);
433       if ((flags & SECTION_OVERRIDE) == 0)
434         error ("%J%D causes a section type conflict", decl, decl);
435     }
436
437   named_section_flags (name, flags);
438 }
439
440 /* If required, set DECL_SECTION_NAME to a unique name.  */
441
442 void
443 resolve_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED,
444                         int flag_function_or_data_sections)
445 {
446   if (DECL_SECTION_NAME (decl) == NULL_TREE
447       && targetm.have_named_sections
448       && (flag_function_or_data_sections
449           || DECL_ONE_ONLY (decl)))
450     targetm.asm_out.unique_section (decl, reloc);
451 }
452
453 #ifdef BSS_SECTION_ASM_OP
454
455 /* Tell the assembler to switch to the bss section.  */
456
457 void
458 bss_section (void)
459 {
460   if (in_section != in_bss)
461     {
462       fprintf (asm_out_file, "%s\n", BSS_SECTION_ASM_OP);
463       in_section = in_bss;
464     }
465 }
466
467 #ifdef ASM_OUTPUT_BSS
468
469 /* Utility function for ASM_OUTPUT_BSS for targets to use if
470    they don't support alignments in .bss.
471    ??? It is believed that this function will work in most cases so such
472    support is localized here.  */
473
474 static void
475 asm_output_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
476                 const char *name,
477                 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
478                 unsigned HOST_WIDE_INT rounded)
479 {
480   targetm.asm_out.globalize_label (file, name);
481   bss_section ();
482 #ifdef ASM_DECLARE_OBJECT_NAME
483   last_assemble_variable_decl = decl;
484   ASM_DECLARE_OBJECT_NAME (file, name, decl);
485 #else
486   /* Standard thing is just output label for the object.  */
487   ASM_OUTPUT_LABEL (file, name);
488 #endif /* ASM_DECLARE_OBJECT_NAME */
489   ASM_OUTPUT_SKIP (file, rounded ? rounded : 1);
490 }
491
492 #endif
493
494 #ifdef ASM_OUTPUT_ALIGNED_BSS
495
496 /* Utility function for targets to use in implementing
497    ASM_OUTPUT_ALIGNED_BSS.
498    ??? It is believed that this function will work in most cases so such
499    support is localized here.  */
500
501 static void
502 asm_output_aligned_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
503                         const char *name, unsigned HOST_WIDE_INT size,
504                         int align)
505 {
506   bss_section ();
507   ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
508 #ifdef ASM_DECLARE_OBJECT_NAME
509   last_assemble_variable_decl = decl;
510   ASM_DECLARE_OBJECT_NAME (file, name, decl);
511 #else
512   /* Standard thing is just output label for the object.  */
513   ASM_OUTPUT_LABEL (file, name);
514 #endif /* ASM_DECLARE_OBJECT_NAME */
515   ASM_OUTPUT_SKIP (file, size ? size : 1);
516 }
517
518 #endif
519
520 #endif /* BSS_SECTION_ASM_OP */
521
522 /* Switch to the section for function DECL.
523
524    If DECL is NULL_TREE, switch to the text section.
525    ??? It's not clear that we will ever be passed NULL_TREE, but it's
526    safer to handle it.  */
527
528 void
529 function_section (tree decl)
530 {
531   if (scan_ahead_for_unlikely_executed_note (get_insns()))
532     unlikely_text_section ();
533   else
534     {
535       if (decl != NULL_TREE
536           && DECL_SECTION_NAME (decl) != NULL_TREE)
537         named_section (decl, (char *) 0, 0);
538       else
539         text_section (); 
540     }
541 }
542
543 /* Switch to section for variable DECL.  RELOC is the same as the
544    argument to SELECT_SECTION.  */
545
546 void
547 variable_section (tree decl, int reloc)
548 {
549   if (IN_NAMED_SECTION (decl))
550     named_section (decl, NULL, reloc);
551   else
552     targetm.asm_out.select_section (decl, reloc, DECL_ALIGN (decl));
553 }
554
555 /* Tell assembler to switch to the section for string merging.  */
556
557 void
558 mergeable_string_section (tree decl ATTRIBUTE_UNUSED,
559                           unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
560                           unsigned int flags ATTRIBUTE_UNUSED)
561 {
562   if (HAVE_GAS_SHF_MERGE && flag_merge_constants
563       && TREE_CODE (decl) == STRING_CST
564       && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
565       && align <= 256
566       && TREE_STRING_LENGTH (decl) >= int_size_in_bytes (TREE_TYPE (decl)))
567     {
568       enum machine_mode mode;
569       unsigned int modesize;
570       const char *str;
571       int i, j, len, unit;
572       char name[30];
573
574       mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (decl)));
575       modesize = GET_MODE_BITSIZE (mode);
576       if (modesize >= 8 && modesize <= 256
577           && (modesize & (modesize - 1)) == 0)
578         {
579           if (align < modesize)
580             align = modesize;
581
582           str = TREE_STRING_POINTER (decl);
583           len = TREE_STRING_LENGTH (decl);
584           unit = GET_MODE_SIZE (mode);
585
586           /* Check for embedded NUL characters.  */
587           for (i = 0; i < len; i += unit)
588             {
589               for (j = 0; j < unit; j++)
590                 if (str[i + j] != '\0')
591                   break;
592               if (j == unit)
593                 break;
594             }
595           if (i == len - unit)
596             {
597               sprintf (name, ".rodata.str%d.%d", modesize / 8,
598                        (int) (align / 8));
599               flags |= (modesize / 8) | SECTION_MERGE | SECTION_STRINGS;
600               if (!i && modesize < align)
601                 {
602                   /* A "" string with requested alignment greater than
603                      character size might cause a problem:
604                      if some other string required even bigger
605                      alignment than "", then linker might think the
606                      "" is just part of padding after some other string
607                      and not put it into the hash table initially.
608                      But this means "" could have smaller alignment
609                      than requested.  */
610 #ifdef ASM_OUTPUT_SECTION_START
611                   named_section_flags (name, flags);
612                   ASM_OUTPUT_SECTION_START (asm_out_file);
613 #else
614                   readonly_data_section ();
615 #endif
616                   return;
617                 }
618
619               named_section_flags (name, flags);
620               return;
621             }
622         }
623     }
624
625   readonly_data_section ();
626 }
627
628 /* Tell assembler to switch to the section for constant merging.  */
629
630 void
631 mergeable_constant_section (enum machine_mode mode ATTRIBUTE_UNUSED,
632                             unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
633                             unsigned int flags ATTRIBUTE_UNUSED)
634 {
635   unsigned int modesize = GET_MODE_BITSIZE (mode);
636
637   if (HAVE_GAS_SHF_MERGE && flag_merge_constants
638       && mode != VOIDmode
639       && mode != BLKmode
640       && modesize <= align
641       && align >= 8
642       && align <= 256
643       && (align & (align - 1)) == 0)
644     {
645       char name[24];
646
647       sprintf (name, ".rodata.cst%d", (int) (align / 8));
648       flags |= (align / 8) | SECTION_MERGE;
649       named_section_flags (name, flags);
650       return;
651     }
652
653   readonly_data_section ();
654 }
655 \f
656 /* Given NAME, a putative register name, discard any customary prefixes.  */
657
658 static const char *
659 strip_reg_name (const char *name)
660 {
661 #ifdef REGISTER_PREFIX
662   if (!strncmp (name, REGISTER_PREFIX, strlen (REGISTER_PREFIX)))
663     name += strlen (REGISTER_PREFIX);
664 #endif
665   if (name[0] == '%' || name[0] == '#')
666     name++;
667   return name;
668 }
669 \f
670 /* Decode an `asm' spec for a declaration as a register name.
671    Return the register number, or -1 if nothing specified,
672    or -2 if the ASMSPEC is not `cc' or `memory' and is not recognized,
673    or -3 if ASMSPEC is `cc' and is not recognized,
674    or -4 if ASMSPEC is `memory' and is not recognized.
675    Accept an exact spelling or a decimal number.
676    Prefixes such as % are optional.  */
677
678 int
679 decode_reg_name (const char *asmspec)
680 {
681   if (asmspec != 0)
682     {
683       int i;
684
685       /* Get rid of confusing prefixes.  */
686       asmspec = strip_reg_name (asmspec);
687
688       /* Allow a decimal number as a "register name".  */
689       for (i = strlen (asmspec) - 1; i >= 0; i--)
690         if (! ISDIGIT (asmspec[i]))
691           break;
692       if (asmspec[0] != 0 && i < 0)
693         {
694           i = atoi (asmspec);
695           if (i < FIRST_PSEUDO_REGISTER && i >= 0)
696             return i;
697           else
698             return -2;
699         }
700
701       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
702         if (reg_names[i][0]
703             && ! strcmp (asmspec, strip_reg_name (reg_names[i])))
704           return i;
705
706 #ifdef ADDITIONAL_REGISTER_NAMES
707       {
708         static const struct { const char *const name; const int number; } table[]
709           = ADDITIONAL_REGISTER_NAMES;
710
711         for (i = 0; i < (int) ARRAY_SIZE (table); i++)
712           if (! strcmp (asmspec, table[i].name))
713             return table[i].number;
714       }
715 #endif /* ADDITIONAL_REGISTER_NAMES */
716
717       if (!strcmp (asmspec, "memory"))
718         return -4;
719
720       if (!strcmp (asmspec, "cc"))
721         return -3;
722
723       return -2;
724     }
725
726   return -1;
727 }
728 \f
729 /* Create the DECL_RTL for a VAR_DECL or FUNCTION_DECL.  DECL should
730    have static storage duration.  In other words, it should not be an
731    automatic variable, including PARM_DECLs.
732
733    There is, however, one exception: this function handles variables
734    explicitly placed in a particular register by the user.
735
736    ASMSPEC, if not 0, is the string which the user specified as the
737    assembler symbol name.
738
739    This is never called for PARM_DECL nodes.  */
740
741 void
742 make_decl_rtl (tree decl, const char *asmspec)
743 {
744   const char *name = 0;
745   int reg_number;
746   rtx x;
747
748   /* Check that we are not being given an automatic variable.  */
749   /* A weak alias has TREE_PUBLIC set but not the other bits.  */
750   if (TREE_CODE (decl) == PARM_DECL
751       || TREE_CODE (decl) == RESULT_DECL
752       || (TREE_CODE (decl) == VAR_DECL
753           && !TREE_STATIC (decl)
754           && !TREE_PUBLIC (decl)
755           && !DECL_EXTERNAL (decl)
756           && !DECL_REGISTER (decl)))
757     abort ();
758   /* And that we were not given a type or a label.  */
759   else if (TREE_CODE (decl) == TYPE_DECL
760            || TREE_CODE (decl) == LABEL_DECL)
761     abort ();
762
763   /* For a duplicate declaration, we can be called twice on the
764      same DECL node.  Don't discard the RTL already made.  */
765   if (DECL_RTL_SET_P (decl))
766     {
767       /* If the old RTL had the wrong mode, fix the mode.  */
768       if (GET_MODE (DECL_RTL (decl)) != DECL_MODE (decl))
769         SET_DECL_RTL (decl, adjust_address_nv (DECL_RTL (decl),
770                                                DECL_MODE (decl), 0));
771
772       /* ??? Another way to do this would be to maintain a hashed
773          table of such critters.  Instead of adding stuff to a DECL
774          to give certain attributes to it, we could use an external
775          hash map from DECL to set of attributes.  */
776
777       /* Let the target reassign the RTL if it wants.
778          This is necessary, for example, when one machine specific
779          decl attribute overrides another.  */
780       targetm.encode_section_info (decl, DECL_RTL (decl), false);
781       return;
782     }
783
784   reg_number = decode_reg_name (asmspec);
785   if (reg_number == -2)
786     {
787       /* ASMSPEC is given, and not the name of a register.  Mark the
788          name with a star so assemble_name won't munge it.  */
789       char *starred = alloca (strlen (asmspec) + 2);
790       starred[0] = '*';
791       strcpy (starred + 1, asmspec);
792       change_decl_assembler_name (decl, get_identifier (starred));
793     }
794
795   name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
796
797   if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
798     {
799       /* First detect errors in declaring global registers.  */
800       if (reg_number == -1)
801         error ("%Jregister name not specified for '%D'", decl, decl);
802       else if (reg_number < 0)
803         error ("%Jinvalid register name for '%D'", decl, decl);
804       else if (TYPE_MODE (TREE_TYPE (decl)) == BLKmode)
805         error ("%Jdata type of '%D' isn't suitable for a register",
806                decl, decl);
807       else if (! HARD_REGNO_MODE_OK (reg_number, TYPE_MODE (TREE_TYPE (decl))))
808         error ("%Jregister specified for '%D' isn't suitable for data type",
809                decl, decl);
810       /* Now handle properly declared static register variables.  */
811       else
812         {
813           int nregs;
814
815           if (DECL_INITIAL (decl) != 0 && TREE_STATIC (decl))
816             {
817               DECL_INITIAL (decl) = 0;
818               error ("global register variable has initial value");
819             }
820           if (TREE_THIS_VOLATILE (decl))
821             warning ("volatile register variables don't work as you might wish");
822
823           /* If the user specified one of the eliminables registers here,
824              e.g., FRAME_POINTER_REGNUM, we don't want to get this variable
825              confused with that register and be eliminated.  This usage is
826              somewhat suspect...  */
827
828           SET_DECL_RTL (decl, gen_rtx_raw_REG (DECL_MODE (decl), reg_number));
829           ORIGINAL_REGNO (DECL_RTL (decl)) = reg_number;
830           REG_USERVAR_P (DECL_RTL (decl)) = 1;
831
832           if (TREE_STATIC (decl))
833             {
834               /* Make this register global, so not usable for anything
835                  else.  */
836 #ifdef ASM_DECLARE_REGISTER_GLOBAL
837               ASM_DECLARE_REGISTER_GLOBAL (asm_out_file, decl, reg_number, name);
838 #endif
839               nregs = hard_regno_nregs[reg_number][DECL_MODE (decl)];
840               while (nregs > 0)
841                 globalize_reg (reg_number + --nregs);
842             }
843
844           /* As a register variable, it has no section.  */
845           return;
846         }
847     }
848
849   /* Now handle ordinary static variables and functions (in memory).
850      Also handle vars declared register invalidly.  */
851
852   if (reg_number >= 0 || reg_number == -3)
853     error ("%Jregister name given for non-register variable '%D'", decl, decl);
854
855   /* Specifying a section attribute on a variable forces it into a
856      non-.bss section, and thus it cannot be common.  */
857   if (TREE_CODE (decl) == VAR_DECL
858       && DECL_SECTION_NAME (decl) != NULL_TREE
859       && DECL_INITIAL (decl) == NULL_TREE
860       && DECL_COMMON (decl))
861     DECL_COMMON (decl) = 0;
862
863   /* Variables can't be both common and weak.  */
864   if (TREE_CODE (decl) == VAR_DECL && DECL_WEAK (decl))
865     DECL_COMMON (decl) = 0;
866
867   x = gen_rtx_SYMBOL_REF (Pmode, name);
868   SYMBOL_REF_WEAK (x) = DECL_WEAK (decl);
869   SYMBOL_REF_DECL (x) = decl;
870
871   x = gen_rtx_MEM (DECL_MODE (decl), x);
872   if (TREE_CODE (decl) != FUNCTION_DECL)
873     set_mem_attributes (x, decl, 1);
874   SET_DECL_RTL (decl, x);
875
876   /* Optionally set flags or add text to the name to record information
877      such as that it is a function name.
878      If the name is changed, the macro ASM_OUTPUT_LABELREF
879      will have to know how to strip this information.  */
880   targetm.encode_section_info (decl, DECL_RTL (decl), true);
881 }
882
883 /* Make the rtl for variable VAR be volatile.
884    Use this only for static variables.  */
885
886 void
887 make_var_volatile (tree var)
888 {
889   if (GET_CODE (DECL_RTL (var)) != MEM)
890     abort ();
891
892   MEM_VOLATILE_P (DECL_RTL (var)) = 1;
893 }
894 \f
895 /* Output a string of literal assembler code
896    for an `asm' keyword used between functions.  */
897
898 void
899 assemble_asm (tree string)
900 {
901   app_enable ();
902
903   if (TREE_CODE (string) == ADDR_EXPR)
904     string = TREE_OPERAND (string, 0);
905
906   fprintf (asm_out_file, "\t%s\n", TREE_STRING_POINTER (string));
907 }
908
909 /* Record an element in the table of global destructors.  SYMBOL is
910    a SYMBOL_REF of the function to be called; PRIORITY is a number
911    between 0 and MAX_INIT_PRIORITY.  */
912
913 void
914 default_stabs_asm_out_destructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
915 {
916   /* Tell GNU LD that this is part of the static destructor set.
917      This will work for any system that uses stabs, most usefully
918      aout systems.  */
919   fprintf (asm_out_file, "%s\"___DTOR_LIST__\",22,0,0,", ASM_STABS_OP);
920   assemble_name (asm_out_file, XSTR (symbol, 0));
921   fputc ('\n', asm_out_file);
922 }
923
924 void
925 default_named_section_asm_out_destructor (rtx symbol, int priority)
926 {
927   const char *section = ".dtors";
928   char buf[16];
929
930   /* ??? This only works reliably with the GNU linker.  */
931   if (priority != DEFAULT_INIT_PRIORITY)
932     {
933       sprintf (buf, ".dtors.%.5u",
934                /* Invert the numbering so the linker puts us in the proper
935                   order; constructors are run from right to left, and the
936                   linker sorts in increasing order.  */
937                MAX_INIT_PRIORITY - priority);
938       section = buf;
939     }
940
941   named_section_flags (section, SECTION_WRITE);
942   assemble_align (POINTER_SIZE);
943   assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
944 }
945
946 #ifdef DTORS_SECTION_ASM_OP
947 void
948 dtors_section (void)
949 {
950   if (in_section != in_dtors)
951     {
952       in_section = in_dtors;
953       fputs (DTORS_SECTION_ASM_OP, asm_out_file);
954       fputc ('\n', asm_out_file);
955     }
956 }
957
958 void
959 default_dtor_section_asm_out_destructor (rtx symbol,
960                                          int priority ATTRIBUTE_UNUSED)
961 {
962   dtors_section ();
963   assemble_align (POINTER_SIZE);
964   assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
965 }
966 #endif
967
968 /* Likewise for global constructors.  */
969
970 void
971 default_stabs_asm_out_constructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
972 {
973   /* Tell GNU LD that this is part of the static destructor set.
974      This will work for any system that uses stabs, most usefully
975      aout systems.  */
976   fprintf (asm_out_file, "%s\"___CTOR_LIST__\",22,0,0,", ASM_STABS_OP);
977   assemble_name (asm_out_file, XSTR (symbol, 0));
978   fputc ('\n', asm_out_file);
979 }
980
981 void
982 default_named_section_asm_out_constructor (rtx symbol, int priority)
983 {
984   const char *section = ".ctors";
985   char buf[16];
986
987   /* ??? This only works reliably with the GNU linker.  */
988   if (priority != DEFAULT_INIT_PRIORITY)
989     {
990       sprintf (buf, ".ctors.%.5u",
991                /* Invert the numbering so the linker puts us in the proper
992                   order; constructors are run from right to left, and the
993                   linker sorts in increasing order.  */
994                MAX_INIT_PRIORITY - priority);
995       section = buf;
996     }
997
998   named_section_flags (section, SECTION_WRITE);
999   assemble_align (POINTER_SIZE);
1000   assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1001 }
1002
1003 #ifdef CTORS_SECTION_ASM_OP
1004 void
1005 ctors_section (void)
1006 {
1007   if (in_section != in_ctors)
1008     {
1009       in_section = in_ctors;
1010       fputs (CTORS_SECTION_ASM_OP, asm_out_file);
1011       fputc ('\n', asm_out_file);
1012     }
1013 }
1014
1015 void
1016 default_ctor_section_asm_out_constructor (rtx symbol,
1017                                           int priority ATTRIBUTE_UNUSED)
1018 {
1019   ctors_section ();
1020   assemble_align (POINTER_SIZE);
1021   assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1022 }
1023 #endif
1024 \f
1025 /* CONSTANT_POOL_BEFORE_FUNCTION may be defined as an expression with
1026    a nonzero value if the constant pool should be output before the
1027    start of the function, or a zero value if the pool should output
1028    after the end of the function.  The default is to put it before the
1029    start.  */
1030
1031 #ifndef CONSTANT_POOL_BEFORE_FUNCTION
1032 #define CONSTANT_POOL_BEFORE_FUNCTION 1
1033 #endif
1034
1035 /* DECL is an object (either VAR_DECL or FUNCTION_DECL) which is going
1036    to be output to assembler.
1037    Set first_global_object_name and weak_global_object_name as appropriate.  */
1038
1039 void
1040 notice_global_symbol (tree decl)
1041 {
1042   const char **type = &first_global_object_name;
1043
1044   if (first_global_object_name
1045       || !TREE_PUBLIC (decl) || DECL_EXTERNAL (decl)
1046       || !DECL_NAME (decl)
1047       || (TREE_CODE (decl) != FUNCTION_DECL
1048           && (TREE_CODE (decl) != VAR_DECL
1049               || (DECL_COMMON (decl)
1050                   && (DECL_INITIAL (decl) == 0
1051                       || DECL_INITIAL (decl) == error_mark_node))))
1052       || GET_CODE (DECL_RTL (decl)) != MEM)
1053     return;
1054
1055   /* We win when global object is found, but it is useful to know about weak
1056      symbol as well so we can produce nicer unique names.  */
1057   if (DECL_WEAK (decl) || DECL_ONE_ONLY (decl))
1058     type = &weak_global_object_name;
1059
1060   if (!*type)
1061     {
1062       const char *p;
1063       char *name;
1064       rtx decl_rtl = DECL_RTL (decl);
1065
1066       p = targetm.strip_name_encoding (XSTR (XEXP (decl_rtl, 0), 0));
1067       name = xstrdup (p);
1068
1069       *type = name;
1070     }
1071 }
1072
1073 /* Output assembler code for the constant pool of a function and associated
1074    with defining the name of the function.  DECL describes the function.
1075    NAME is the function's name.  For the constant pool, we use the current
1076    constant pool data.  */
1077
1078 void
1079 assemble_start_function (tree decl, const char *fnname)
1080 {
1081   int align;
1082
1083   unlikely_section_label_printed = false;
1084
1085   /* The following code does not need preprocessing in the assembler.  */
1086
1087   app_disable ();
1088
1089   if (CONSTANT_POOL_BEFORE_FUNCTION)
1090     output_constant_pool (fnname, decl);
1091
1092   resolve_unique_section (decl, 0, flag_function_sections);
1093   function_section (decl);
1094
1095   /* Tell assembler to move to target machine's alignment for functions.  */
1096   align = floor_log2 (FUNCTION_BOUNDARY / BITS_PER_UNIT);
1097   if (align < force_align_functions_log)
1098     align = force_align_functions_log;
1099   if (align > 0)
1100     {
1101       ASM_OUTPUT_ALIGN (asm_out_file, align);
1102     }
1103
1104   /* Handle a user-specified function alignment.
1105      Note that we still need to align to FUNCTION_BOUNDARY, as above,
1106      because ASM_OUTPUT_MAX_SKIP_ALIGN might not do any alignment at all.  */
1107   if (align_functions_log > align
1108       && cfun->function_frequency != FUNCTION_FREQUENCY_UNLIKELY_EXECUTED)
1109     {
1110 #ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
1111       ASM_OUTPUT_MAX_SKIP_ALIGN (asm_out_file,
1112                                  align_functions_log, align_functions - 1);
1113 #else
1114       ASM_OUTPUT_ALIGN (asm_out_file, align_functions_log);
1115 #endif
1116     }
1117
1118 #ifdef ASM_OUTPUT_FUNCTION_PREFIX
1119   ASM_OUTPUT_FUNCTION_PREFIX (asm_out_file, fnname);
1120 #endif
1121
1122   (*debug_hooks->begin_function) (decl);
1123
1124   /* Make function name accessible from other files, if appropriate.  */
1125
1126   if (TREE_PUBLIC (decl))
1127     {
1128       notice_global_symbol (decl);
1129
1130       globalize_decl (decl);
1131
1132       maybe_assemble_visibility (decl);
1133     }
1134
1135   /* Do any machine/system dependent processing of the function name.  */
1136 #ifdef ASM_DECLARE_FUNCTION_NAME
1137   ASM_DECLARE_FUNCTION_NAME (asm_out_file, fnname, current_function_decl);
1138 #else
1139   /* Standard thing is just output label for the function.  */
1140   ASM_OUTPUT_LABEL (asm_out_file, fnname);
1141 #endif /* ASM_DECLARE_FUNCTION_NAME */
1142 }
1143
1144 /* Output assembler code associated with defining the size of the
1145    function.  DECL describes the function.  NAME is the function's name.  */
1146
1147 void
1148 assemble_end_function (tree decl, const char *fnname)
1149 {
1150 #ifdef ASM_DECLARE_FUNCTION_SIZE
1151   ASM_DECLARE_FUNCTION_SIZE (asm_out_file, fnname, decl);
1152 #endif
1153   if (! CONSTANT_POOL_BEFORE_FUNCTION)
1154     {
1155       output_constant_pool (fnname, decl);
1156       function_section (decl);  /* need to switch back */
1157     }
1158 }
1159 \f
1160 /* Assemble code to leave SIZE bytes of zeros.  */
1161
1162 void
1163 assemble_zeros (unsigned HOST_WIDE_INT size)
1164 {
1165   /* Do no output if -fsyntax-only.  */
1166   if (flag_syntax_only)
1167     return;
1168
1169 #ifdef ASM_NO_SKIP_IN_TEXT
1170   /* The `space' pseudo in the text section outputs nop insns rather than 0s,
1171      so we must output 0s explicitly in the text section.  */
1172   if ((ASM_NO_SKIP_IN_TEXT && in_text_section ())
1173       || (ASM_NO_SKIP_IN_TEXT && in_unlikely_text_section ()))
1174     {
1175       unsigned HOST_WIDE_INT i;
1176       for (i = 0; i < size; i++)
1177         assemble_integer (const0_rtx, 1, BITS_PER_UNIT, 1);
1178     }
1179   else
1180 #endif
1181     if (size > 0)
1182       ASM_OUTPUT_SKIP (asm_out_file, size);
1183 }
1184
1185 /* Assemble an alignment pseudo op for an ALIGN-bit boundary.  */
1186
1187 void
1188 assemble_align (int align)
1189 {
1190   if (align > BITS_PER_UNIT)
1191     {
1192       ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
1193     }
1194 }
1195
1196 /* Assemble a string constant with the specified C string as contents.  */
1197
1198 void
1199 assemble_string (const char *p, int size)
1200 {
1201   int pos = 0;
1202   int maximum = 2000;
1203
1204   /* If the string is very long, split it up.  */
1205
1206   while (pos < size)
1207     {
1208       int thissize = size - pos;
1209       if (thissize > maximum)
1210         thissize = maximum;
1211
1212       ASM_OUTPUT_ASCII (asm_out_file, p, thissize);
1213
1214       pos += thissize;
1215       p += thissize;
1216     }
1217 }
1218
1219 \f
1220 #if defined  ASM_OUTPUT_ALIGNED_DECL_LOCAL
1221 #define ASM_EMIT_LOCAL(decl, name, size, rounded) \
1222   ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, decl, name, size, DECL_ALIGN (decl))
1223 #else
1224 #if defined  ASM_OUTPUT_ALIGNED_LOCAL
1225 #define ASM_EMIT_LOCAL(decl, name, size, rounded) \
1226   ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, DECL_ALIGN (decl))
1227 #else
1228 #define ASM_EMIT_LOCAL(decl, name, size, rounded) \
1229   ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded)
1230 #endif
1231 #endif
1232
1233 #if defined ASM_OUTPUT_ALIGNED_BSS
1234 #define ASM_EMIT_BSS(decl, name, size, rounded) \
1235   ASM_OUTPUT_ALIGNED_BSS (asm_out_file, decl, name, size, DECL_ALIGN (decl))
1236 #else
1237 #if defined ASM_OUTPUT_BSS
1238 #define ASM_EMIT_BSS(decl, name, size, rounded) \
1239   ASM_OUTPUT_BSS (asm_out_file, decl, name, size, rounded)
1240 #else
1241 #undef  ASM_EMIT_BSS
1242 #endif
1243 #endif
1244
1245 #if defined ASM_OUTPUT_ALIGNED_DECL_COMMON
1246 #define ASM_EMIT_COMMON(decl, name, size, rounded) \
1247   ASM_OUTPUT_ALIGNED_DECL_COMMON (asm_out_file, decl, name, size, DECL_ALIGN (decl))
1248 #else
1249 #if defined ASM_OUTPUT_ALIGNED_COMMON
1250 #define ASM_EMIT_COMMON(decl, name, size, rounded) \
1251   ASM_OUTPUT_ALIGNED_COMMON (asm_out_file, name, size, DECL_ALIGN (decl))
1252 #else
1253 #define ASM_EMIT_COMMON(decl, name, size, rounded) \
1254   ASM_OUTPUT_COMMON (asm_out_file, name, size, rounded)
1255 #endif
1256 #endif
1257
1258 static bool
1259 asm_emit_uninitialised (tree decl, const char *name,
1260                         unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1261                         unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1262 {
1263   enum
1264   {
1265     asm_dest_common,
1266     asm_dest_bss,
1267     asm_dest_local
1268   }
1269   destination = asm_dest_local;
1270
1271   /* ??? We should handle .bss via select_section mechanisms rather than
1272      via special target hooks.  That would eliminate this special case.  */
1273   if (TREE_PUBLIC (decl))
1274     {
1275       if (!DECL_COMMON (decl))
1276 #ifdef ASM_EMIT_BSS
1277         destination = asm_dest_bss;
1278 #else
1279         return false;
1280 #endif
1281       else
1282         destination = asm_dest_common;
1283     }
1284
1285   if (destination == asm_dest_bss)
1286     globalize_decl (decl);
1287   resolve_unique_section (decl, 0, flag_data_sections);
1288
1289   if (flag_shared_data)
1290     {
1291       switch (destination)
1292         {
1293 #ifdef ASM_OUTPUT_SHARED_BSS
1294         case asm_dest_bss:
1295           ASM_OUTPUT_SHARED_BSS (asm_out_file, decl, name, size, rounded);
1296           return;
1297 #endif
1298 #ifdef ASM_OUTPUT_SHARED_COMMON
1299         case asm_dest_common:
1300           ASM_OUTPUT_SHARED_COMMON (asm_out_file, name, size, rounded);
1301           return;
1302 #endif
1303 #ifdef ASM_OUTPUT_SHARED_LOCAL
1304         case asm_dest_local:
1305           ASM_OUTPUT_SHARED_LOCAL (asm_out_file, name, size, rounded);
1306           return;
1307 #endif
1308         default:
1309           break;
1310         }
1311     }
1312
1313   switch (destination)
1314     {
1315 #ifdef ASM_EMIT_BSS
1316     case asm_dest_bss:
1317       ASM_EMIT_BSS (decl, name, size, rounded);
1318       break;
1319 #endif
1320     case asm_dest_common:
1321       ASM_EMIT_COMMON (decl, name, size, rounded);
1322       break;
1323     case asm_dest_local:
1324       ASM_EMIT_LOCAL (decl, name, size, rounded);
1325       break;
1326     default:
1327       abort ();
1328     }
1329
1330   return true;
1331 }
1332
1333 /* Assemble everything that is needed for a variable or function declaration.
1334    Not used for automatic variables, and not used for function definitions.
1335    Should not be called for variables of incomplete structure type.
1336
1337    TOP_LEVEL is nonzero if this variable has file scope.
1338    AT_END is nonzero if this is the special handling, at end of compilation,
1339    to define things that have had only tentative definitions.
1340    DONT_OUTPUT_DATA if nonzero means don't actually output the
1341    initial value (that will be done by the caller).  */
1342
1343 void
1344 assemble_variable (tree decl, int top_level ATTRIBUTE_UNUSED,
1345                    int at_end ATTRIBUTE_UNUSED, int dont_output_data)
1346 {
1347   const char *name;
1348   unsigned int align;
1349   int reloc = 0;
1350   rtx decl_rtl;
1351
1352   if (lang_hooks.decls.prepare_assemble_variable)
1353     lang_hooks.decls.prepare_assemble_variable (decl);
1354
1355   last_assemble_variable_decl = 0;
1356
1357   /* Normally no need to say anything here for external references,
1358      since assemble_external is called by the language-specific code
1359      when a declaration is first seen.  */
1360
1361   if (DECL_EXTERNAL (decl))
1362     return;
1363
1364   /* Output no assembler code for a function declaration.
1365      Only definitions of functions output anything.  */
1366
1367   if (TREE_CODE (decl) == FUNCTION_DECL)
1368     return;
1369
1370   /* Do nothing for global register variables.  */
1371   if (DECL_RTL_SET_P (decl) && GET_CODE (DECL_RTL (decl)) == REG)
1372     {
1373       TREE_ASM_WRITTEN (decl) = 1;
1374       return;
1375     }
1376
1377   /* If type was incomplete when the variable was declared,
1378      see if it is complete now.  */
1379
1380   if (DECL_SIZE (decl) == 0)
1381     layout_decl (decl, 0);
1382
1383   /* Still incomplete => don't allocate it; treat the tentative defn
1384      (which is what it must have been) as an `extern' reference.  */
1385
1386   if (!dont_output_data && DECL_SIZE (decl) == 0)
1387     {
1388       error ("%Jstorage size of `%D' isn't known", decl, decl);
1389       TREE_ASM_WRITTEN (decl) = 1;
1390       return;
1391     }
1392
1393   /* The first declaration of a variable that comes through this function
1394      decides whether it is global (in C, has external linkage)
1395      or local (in C, has internal linkage).  So do nothing more
1396      if this function has already run.  */
1397
1398   if (TREE_ASM_WRITTEN (decl))
1399     return;
1400
1401   /* Make sure targetm.encode_section_info is invoked before we set
1402      ASM_WRITTEN.  */
1403   decl_rtl = DECL_RTL (decl);
1404
1405   TREE_ASM_WRITTEN (decl) = 1;
1406
1407   /* Do no output if -fsyntax-only.  */
1408   if (flag_syntax_only)
1409     return;
1410
1411   app_disable ();
1412
1413   if (! dont_output_data
1414       && ! host_integerp (DECL_SIZE_UNIT (decl), 1))
1415     {
1416       error ("%Jsize of variable '%D' is too large", decl, decl);
1417       return;
1418     }
1419
1420   name = XSTR (XEXP (decl_rtl, 0), 0);
1421   if (TREE_PUBLIC (decl) && DECL_NAME (decl))
1422     notice_global_symbol (decl);
1423
1424   /* Compute the alignment of this data.  */
1425
1426   align = DECL_ALIGN (decl);
1427
1428   /* In the case for initialing an array whose length isn't specified,
1429      where we have not yet been able to do the layout,
1430      figure out the proper alignment now.  */
1431   if (dont_output_data && DECL_SIZE (decl) == 0
1432       && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
1433     align = MAX (align, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (decl))));
1434
1435   /* Some object file formats have a maximum alignment which they support.
1436      In particular, a.out format supports a maximum alignment of 4.  */
1437 #ifndef MAX_OFILE_ALIGNMENT
1438 #define MAX_OFILE_ALIGNMENT BIGGEST_ALIGNMENT
1439 #endif
1440   if (align > MAX_OFILE_ALIGNMENT)
1441     {
1442       warning ("%Jalignment of '%D' is greater than maximum object "
1443                "file alignment.  Using %d", decl, decl,
1444                MAX_OFILE_ALIGNMENT/BITS_PER_UNIT);
1445       align = MAX_OFILE_ALIGNMENT;
1446     }
1447
1448   /* On some machines, it is good to increase alignment sometimes.  */
1449   if (! DECL_USER_ALIGN (decl))
1450     {
1451 #ifdef DATA_ALIGNMENT
1452       align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
1453 #endif
1454 #ifdef CONSTANT_ALIGNMENT
1455       if (DECL_INITIAL (decl) != 0 && DECL_INITIAL (decl) != error_mark_node)
1456         align = CONSTANT_ALIGNMENT (DECL_INITIAL (decl), align);
1457 #endif
1458     }
1459
1460   /* Reset the alignment in case we have made it tighter, so we can benefit
1461      from it in get_pointer_alignment.  */
1462   DECL_ALIGN (decl) = align;
1463   set_mem_align (decl_rtl, align);
1464
1465   if (TREE_PUBLIC (decl))
1466     maybe_assemble_visibility (decl);
1467
1468   /* Output any data that we will need to use the address of.  */
1469   if (DECL_INITIAL (decl) == error_mark_node)
1470     reloc = contains_pointers_p (TREE_TYPE (decl)) ? 3 : 0;
1471   else if (DECL_INITIAL (decl))
1472     {
1473       reloc = compute_reloc_for_constant (DECL_INITIAL (decl));
1474       output_addressed_constants (DECL_INITIAL (decl));
1475     }
1476   resolve_unique_section (decl, reloc, flag_data_sections);
1477
1478   /* Handle uninitialized definitions.  */
1479
1480   /* If the decl has been given an explicit section name, then it
1481      isn't common, and shouldn't be handled as such.  */
1482   if (DECL_SECTION_NAME (decl) || dont_output_data)
1483     ;
1484   /* We don't implement common thread-local data at present.  */
1485   else if (DECL_THREAD_LOCAL (decl))
1486     {
1487       if (DECL_COMMON (decl))
1488         sorry ("thread-local COMMON data not implemented");
1489     }
1490   else if (DECL_INITIAL (decl) == 0
1491            || DECL_INITIAL (decl) == error_mark_node
1492            || (flag_zero_initialized_in_bss
1493                /* Leave constant zeroes in .rodata so they can be shared.  */
1494                && !TREE_READONLY (decl)
1495                && initializer_zerop (DECL_INITIAL (decl))))
1496     {
1497       unsigned HOST_WIDE_INT size = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
1498       unsigned HOST_WIDE_INT rounded = size;
1499
1500       /* Don't allocate zero bytes of common,
1501          since that means "undefined external" in the linker.  */
1502       if (size == 0)
1503         rounded = 1;
1504
1505       /* Round size up to multiple of BIGGEST_ALIGNMENT bits
1506          so that each uninitialized object starts on such a boundary.  */
1507       rounded += (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1;
1508       rounded = (rounded / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
1509                  * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
1510
1511 #if !defined(ASM_OUTPUT_ALIGNED_COMMON) && !defined(ASM_OUTPUT_ALIGNED_DECL_COMMON) && !defined(ASM_OUTPUT_ALIGNED_BSS)
1512       if ((unsigned HOST_WIDE_INT) DECL_ALIGN (decl) / BITS_PER_UNIT > rounded)
1513         warning ("%Jrequested alignment for '%D' is greater than "
1514                  "implemented alignment of %d", decl, decl, rounded);
1515 #endif
1516
1517       /* If the target cannot output uninitialized but not common global data
1518          in .bss, then we have to use .data, so fall through.  */
1519       if (asm_emit_uninitialised (decl, name, size, rounded))
1520         return;
1521     }
1522
1523   /* Handle initialized definitions.
1524      Also handle uninitialized global definitions if -fno-common and the
1525      target doesn't support ASM_OUTPUT_BSS.  */
1526
1527   /* First make the assembler name(s) global if appropriate.  */
1528   if (TREE_PUBLIC (decl) && DECL_NAME (decl))
1529     globalize_decl (decl);
1530
1531   /* Switch to the appropriate section.  */
1532   variable_section (decl, reloc);
1533
1534   /* dbxout.c needs to know this.  */
1535   if (in_text_section () || in_unlikely_text_section ())
1536     DECL_IN_TEXT_SECTION (decl) = 1;
1537
1538   /* Output the alignment of this data.  */
1539   if (align > BITS_PER_UNIT)
1540     {
1541       ASM_OUTPUT_ALIGN (asm_out_file,
1542                         floor_log2 (DECL_ALIGN (decl) / BITS_PER_UNIT));
1543     }
1544
1545   /* Do any machine/system dependent processing of the object.  */
1546 #ifdef ASM_DECLARE_OBJECT_NAME
1547   last_assemble_variable_decl = decl;
1548   ASM_DECLARE_OBJECT_NAME (asm_out_file, name, decl);
1549 #else
1550   /* Standard thing is just output label for the object.  */
1551   ASM_OUTPUT_LABEL (asm_out_file, name);
1552 #endif /* ASM_DECLARE_OBJECT_NAME */
1553
1554   if (!dont_output_data)
1555     {
1556       if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node)
1557         /* Output the actual data.  */
1558         output_constant (DECL_INITIAL (decl),
1559                          tree_low_cst (DECL_SIZE_UNIT (decl), 1),
1560                          align);
1561       else
1562         /* Leave space for it.  */
1563         assemble_zeros (tree_low_cst (DECL_SIZE_UNIT (decl), 1));
1564     }
1565 }
1566
1567 /* Return 1 if type TYPE contains any pointers.  */
1568
1569 static int
1570 contains_pointers_p (tree type)
1571 {
1572   switch (TREE_CODE (type))
1573     {
1574     case POINTER_TYPE:
1575     case REFERENCE_TYPE:
1576       /* I'm not sure whether OFFSET_TYPE needs this treatment,
1577          so I'll play safe and return 1.  */
1578     case OFFSET_TYPE:
1579       return 1;
1580
1581     case RECORD_TYPE:
1582     case UNION_TYPE:
1583     case QUAL_UNION_TYPE:
1584       {
1585         tree fields;
1586         /* For a type that has fields, see if the fields have pointers.  */
1587         for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
1588           if (TREE_CODE (fields) == FIELD_DECL
1589               && contains_pointers_p (TREE_TYPE (fields)))
1590             return 1;
1591         return 0;
1592       }
1593
1594     case ARRAY_TYPE:
1595       /* An array type contains pointers if its element type does.  */
1596       return contains_pointers_p (TREE_TYPE (type));
1597
1598     default:
1599       return 0;
1600     }
1601 }
1602
1603 #ifdef ASM_OUTPUT_EXTERNAL
1604 /* True if DECL is a function decl for which no out-of-line copy exists.
1605    It is assumed that DECL's assembler name has been set.  */
1606
1607 static bool
1608 incorporeal_function_p (tree decl)
1609 {
1610   if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1611     {
1612       const char *name;
1613
1614       if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
1615           && DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA)
1616         return true;
1617
1618       name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
1619       if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
1620         return true;
1621     }
1622   return false;
1623 }
1624 #endif
1625
1626 /* Output something to declare an external symbol to the assembler.
1627    (Most assemblers don't need this, so we normally output nothing.)
1628    Do nothing if DECL is not external.  */
1629
1630 void
1631 assemble_external (tree decl ATTRIBUTE_UNUSED)
1632 {
1633   /* Because most platforms do not define ASM_OUTPUT_EXTERNAL, the
1634      main body of this code is only rarely exercised.  To provide some
1635      testing, on all platforms, we make sure that the ASM_OUT_FILE is
1636      open.  If it's not, we should not be calling this function.  */
1637   if (!asm_out_file)
1638     abort ();
1639
1640 #ifdef ASM_OUTPUT_EXTERNAL
1641   if (DECL_P (decl) && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl))
1642     {
1643       rtx rtl = DECL_RTL (decl);
1644
1645       if (GET_CODE (rtl) == MEM && GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF
1646           && !SYMBOL_REF_USED (XEXP (rtl, 0))
1647           && !incorporeal_function_p (decl))
1648         {
1649           /* Some systems do require some output.  */
1650           SYMBOL_REF_USED (XEXP (rtl, 0)) = 1;
1651           ASM_OUTPUT_EXTERNAL (asm_out_file, decl, XSTR (XEXP (rtl, 0), 0));
1652         }
1653     }
1654 #endif
1655 }
1656
1657 /* Similar, for calling a library function FUN.  */
1658
1659 void
1660 assemble_external_libcall (rtx fun)
1661 {
1662   /* Declare library function name external when first used, if nec.  */
1663   if (! SYMBOL_REF_USED (fun))
1664     {
1665       SYMBOL_REF_USED (fun) = 1;
1666       targetm.asm_out.external_libcall (fun);
1667     }
1668 }
1669
1670 /* Assemble a label named NAME.  */
1671
1672 void
1673 assemble_label (const char *name)
1674 {
1675   ASM_OUTPUT_LABEL (asm_out_file, name);
1676 }
1677
1678 /* Set the symbol_referenced flag for ID and notify callgraph code.  */
1679 void
1680 mark_referenced (tree id)
1681 {
1682   if (!TREE_SYMBOL_REFERENCED (id))
1683     {
1684       struct cgraph_node *node;
1685       struct cgraph_varpool_node *vnode;
1686
1687       if (!cgraph_global_info_ready)
1688         {
1689           node = cgraph_node_for_identifier (id);
1690           if (node)
1691             cgraph_mark_needed_node (node);
1692         }
1693
1694       vnode = cgraph_varpool_node_for_identifier (id);
1695       if (vnode)
1696         cgraph_varpool_mark_needed_node (vnode);
1697     }
1698   TREE_SYMBOL_REFERENCED (id) = 1;
1699 }
1700
1701 /* Output to FILE a reference to the assembler name of a C-level name NAME.
1702    If NAME starts with a *, the rest of NAME is output verbatim.
1703    Otherwise NAME is transformed in an implementation-defined way
1704    (usually by the addition of an underscore).
1705    Many macros in the tm file are defined to call this function.  */
1706
1707 void
1708 assemble_name (FILE *file, const char *name)
1709 {
1710   const char *real_name;
1711   tree id;
1712
1713   real_name = targetm.strip_name_encoding (name);
1714
1715   id = maybe_get_identifier (real_name);
1716   if (id)
1717     mark_referenced (id);
1718
1719   if (name[0] == '*')
1720     fputs (&name[1], file);
1721   else
1722     ASM_OUTPUT_LABELREF (file, name);
1723 }
1724
1725 /* Allocate SIZE bytes writable static space with a gensym name
1726    and return an RTX to refer to its address.  */
1727
1728 rtx
1729 assemble_static_space (unsigned HOST_WIDE_INT size)
1730 {
1731   char name[12];
1732   const char *namestring;
1733   rtx x;
1734
1735 #if 0
1736   if (flag_shared_data)
1737     data_section ();
1738 #endif
1739
1740   ASM_GENERATE_INTERNAL_LABEL (name, "LF", const_labelno);
1741   ++const_labelno;
1742   namestring = ggc_strdup (name);
1743
1744   x = gen_rtx_SYMBOL_REF (Pmode, namestring);
1745   SYMBOL_REF_FLAGS (x) = SYMBOL_FLAG_LOCAL;
1746
1747 #ifdef ASM_OUTPUT_ALIGNED_DECL_LOCAL
1748   ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, NULL_TREE, name, size,
1749                                  BIGGEST_ALIGNMENT);
1750 #else
1751 #ifdef ASM_OUTPUT_ALIGNED_LOCAL
1752   ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, BIGGEST_ALIGNMENT);
1753 #else
1754   {
1755     /* Round size up to multiple of BIGGEST_ALIGNMENT bits
1756        so that each uninitialized object starts on such a boundary.  */
1757     /* Variable `rounded' might or might not be used in ASM_OUTPUT_LOCAL.  */
1758     unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED
1759       = ((size + (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1)
1760          / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
1761          * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
1762     ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
1763   }
1764 #endif
1765 #endif
1766   return x;
1767 }
1768
1769 /* Assemble the static constant template for function entry trampolines.
1770    This is done at most once per compilation.
1771    Returns an RTX for the address of the template.  */
1772
1773 #ifdef TRAMPOLINE_TEMPLATE
1774 rtx
1775 assemble_trampoline_template (void)
1776 {
1777   char label[256];
1778   const char *name;
1779   int align;
1780   rtx symbol;
1781
1782   /* By default, put trampoline templates in read-only data section.  */
1783
1784 #ifdef TRAMPOLINE_SECTION
1785   TRAMPOLINE_SECTION ();
1786 #else
1787   readonly_data_section ();
1788 #endif
1789
1790   /* Write the assembler code to define one.  */
1791   align = floor_log2 (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
1792   if (align > 0)
1793     {
1794       ASM_OUTPUT_ALIGN (asm_out_file, align);
1795     }
1796
1797   targetm.asm_out.internal_label (asm_out_file, "LTRAMP", 0);
1798   TRAMPOLINE_TEMPLATE (asm_out_file);
1799
1800   /* Record the rtl to refer to it.  */
1801   ASM_GENERATE_INTERNAL_LABEL (label, "LTRAMP", 0);
1802   name = ggc_strdup (label);
1803   symbol = gen_rtx_SYMBOL_REF (Pmode, name);
1804   SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_LOCAL;
1805
1806   return symbol;
1807 }
1808 #endif
1809 \f
1810 /* A and B are either alignments or offsets.  Return the minimum alignment
1811    that may be assumed after adding the two together.  */
1812
1813 static inline unsigned
1814 min_align (unsigned int a, unsigned int b)
1815 {
1816   return (a | b) & -(a | b);
1817 }
1818
1819 /* Return the assembler directive for creating a given kind of integer
1820    object.  SIZE is the number of bytes in the object and ALIGNED_P
1821    indicates whether it is known to be aligned.  Return NULL if the
1822    assembly dialect has no such directive.
1823
1824    The returned string should be printed at the start of a new line and
1825    be followed immediately by the object's initial value.  */
1826
1827 const char *
1828 integer_asm_op (int size, int aligned_p)
1829 {
1830   struct asm_int_op *ops;
1831
1832   if (aligned_p)
1833     ops = &targetm.asm_out.aligned_op;
1834   else
1835     ops = &targetm.asm_out.unaligned_op;
1836
1837   switch (size)
1838     {
1839     case 1:
1840       return targetm.asm_out.byte_op;
1841     case 2:
1842       return ops->hi;
1843     case 4:
1844       return ops->si;
1845     case 8:
1846       return ops->di;
1847     case 16:
1848       return ops->ti;
1849     default:
1850       return NULL;
1851     }
1852 }
1853
1854 /* Use directive OP to assemble an integer object X.  Print OP at the
1855    start of the line, followed immediately by the value of X.  */
1856
1857 void
1858 assemble_integer_with_op (const char *op, rtx x)
1859 {
1860   fputs (op, asm_out_file);
1861   output_addr_const (asm_out_file, x);
1862   fputc ('\n', asm_out_file);
1863 }
1864
1865 /* The default implementation of the asm_out.integer target hook.  */
1866
1867 bool
1868 default_assemble_integer (rtx x ATTRIBUTE_UNUSED,
1869                           unsigned int size ATTRIBUTE_UNUSED,
1870                           int aligned_p ATTRIBUTE_UNUSED)
1871 {
1872   const char *op = integer_asm_op (size, aligned_p);
1873   return op && (assemble_integer_with_op (op, x), true);
1874 }
1875
1876 /* Assemble the integer constant X into an object of SIZE bytes.  ALIGN is
1877    the alignment of the integer in bits.  Return 1 if we were able to output
1878    the constant, otherwise 0.  If FORCE is nonzero, abort if we can't output
1879    the constant.  */
1880
1881 bool
1882 assemble_integer (rtx x, unsigned int size, unsigned int align, int force)
1883 {
1884   int aligned_p;
1885
1886   aligned_p = (align >= MIN (size * BITS_PER_UNIT, BIGGEST_ALIGNMENT));
1887
1888   /* See if the target hook can handle this kind of object.  */
1889   if (targetm.asm_out.integer (x, size, aligned_p))
1890     return true;
1891
1892   /* If the object is a multi-byte one, try splitting it up.  Split
1893      it into words it if is multi-word, otherwise split it into bytes.  */
1894   if (size > 1)
1895     {
1896       enum machine_mode omode, imode;
1897       unsigned int subalign;
1898       unsigned int subsize, i;
1899
1900       subsize = size > UNITS_PER_WORD? UNITS_PER_WORD : 1;
1901       subalign = MIN (align, subsize * BITS_PER_UNIT);
1902       omode = mode_for_size (subsize * BITS_PER_UNIT, MODE_INT, 0);
1903       imode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
1904
1905       for (i = 0; i < size; i += subsize)
1906         {
1907           rtx partial = simplify_subreg (omode, x, imode, i);
1908           if (!partial || !assemble_integer (partial, subsize, subalign, 0))
1909             break;
1910         }
1911       if (i == size)
1912         return true;
1913
1914       /* If we've printed some of it, but not all of it, there's no going
1915          back now.  */
1916       if (i > 0)
1917         abort ();
1918     }
1919
1920   if (force)
1921     abort ();
1922
1923   return false;
1924 }
1925 \f
1926 void
1927 assemble_real (REAL_VALUE_TYPE d, enum machine_mode mode, unsigned int align)
1928 {
1929   long data[4];
1930   int i;
1931   int bitsize, nelts, nunits, units_per;
1932
1933   /* This is hairy.  We have a quantity of known size.  real_to_target
1934      will put it into an array of *host* longs, 32 bits per element
1935      (even if long is more than 32 bits).  We need to determine the
1936      number of array elements that are occupied (nelts) and the number
1937      of *target* min-addressable units that will be occupied in the
1938      object file (nunits).  We cannot assume that 32 divides the
1939      mode's bitsize (size * BITS_PER_UNIT) evenly.
1940
1941      size * BITS_PER_UNIT is used here to make sure that padding bits
1942      (which might appear at either end of the value; real_to_target
1943      will include the padding bits in its output array) are included.  */
1944
1945   nunits = GET_MODE_SIZE (mode);
1946   bitsize = nunits * BITS_PER_UNIT;
1947   nelts = CEIL (bitsize, 32);
1948   units_per = 32 / BITS_PER_UNIT;
1949
1950   real_to_target (data, &d, mode);
1951
1952   /* Put out the first word with the specified alignment.  */
1953   assemble_integer (GEN_INT (data[0]), MIN (nunits, units_per), align, 1);
1954   nunits -= units_per;
1955
1956   /* Subsequent words need only 32-bit alignment.  */
1957   align = min_align (align, 32);
1958
1959   for (i = 1; i < nelts; i++)
1960     {
1961       assemble_integer (GEN_INT (data[i]), MIN (nunits, units_per), align, 1);
1962       nunits -= units_per;
1963     }
1964 }
1965 \f
1966 /* Given an expression EXP with a constant value,
1967    reduce it to the sum of an assembler symbol and an integer.
1968    Store them both in the structure *VALUE.
1969    Abort if EXP does not reduce.  */
1970
1971 struct addr_const GTY(())
1972 {
1973   rtx base;
1974   HOST_WIDE_INT offset;
1975 };
1976
1977 static void
1978 decode_addr_const (tree exp, struct addr_const *value)
1979 {
1980   tree target = TREE_OPERAND (exp, 0);
1981   int offset = 0;
1982   rtx x;
1983
1984   while (1)
1985     {
1986       if (TREE_CODE (target) == COMPONENT_REF
1987           && host_integerp (byte_position (TREE_OPERAND (target, 1)), 0))
1988
1989         {
1990           offset += int_byte_position (TREE_OPERAND (target, 1));
1991           target = TREE_OPERAND (target, 0);
1992         }
1993       else if (TREE_CODE (target) == ARRAY_REF
1994                || TREE_CODE (target) == ARRAY_RANGE_REF)
1995         {
1996           offset += (tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (target)), 1)
1997                      * tree_low_cst (TREE_OPERAND (target, 1), 0));
1998           target = TREE_OPERAND (target, 0);
1999         }
2000       else
2001         break;
2002     }
2003
2004   switch (TREE_CODE (target))
2005     {
2006     case VAR_DECL:
2007     case FUNCTION_DECL:
2008       x = DECL_RTL (target);
2009       break;
2010
2011     case LABEL_DECL:
2012       x = gen_rtx_MEM (FUNCTION_MODE,
2013                        gen_rtx_LABEL_REF (VOIDmode, force_label_rtx (target)));
2014       break;
2015
2016     case REAL_CST:
2017     case STRING_CST:
2018     case COMPLEX_CST:
2019     case CONSTRUCTOR:
2020     case INTEGER_CST:
2021       x = output_constant_def (target, 1);
2022       break;
2023
2024     default:
2025       abort ();
2026     }
2027
2028   if (GET_CODE (x) != MEM)
2029     abort ();
2030   x = XEXP (x, 0);
2031
2032   value->base = x;
2033   value->offset = offset;
2034 }
2035 \f
2036 /* Uniquize all constants that appear in memory.
2037    Each constant in memory thus far output is recorded
2038    in `const_desc_table'.  */
2039
2040 struct constant_descriptor_tree GTY(())
2041 {
2042   /* A MEM for the constant.  */
2043   rtx rtl;
2044
2045   /* The value of the constant.  */
2046   tree value;
2047 };
2048
2049 static GTY((param_is (struct constant_descriptor_tree)))
2050      htab_t const_desc_htab;
2051
2052 static struct constant_descriptor_tree * build_constant_desc (tree);
2053 static void maybe_output_constant_def_contents (struct constant_descriptor_tree *, int);
2054
2055 /* Compute a hash code for a constant expression.  */
2056
2057 static hashval_t
2058 const_desc_hash (const void *ptr)
2059 {
2060   return const_hash_1 (((struct constant_descriptor_tree *)ptr)->value);
2061 }
2062
2063 static hashval_t
2064 const_hash_1 (const tree exp)
2065 {
2066   const char *p;
2067   hashval_t hi;
2068   int len, i;
2069   enum tree_code code = TREE_CODE (exp);
2070
2071   /* Either set P and LEN to the address and len of something to hash and
2072      exit the switch or return a value.  */
2073
2074   switch (code)
2075     {
2076     case INTEGER_CST:
2077       p = (char *) &TREE_INT_CST (exp);
2078       len = sizeof TREE_INT_CST (exp);
2079       break;
2080
2081     case REAL_CST:
2082       return real_hash (TREE_REAL_CST_PTR (exp));
2083
2084     case STRING_CST:
2085       p = TREE_STRING_POINTER (exp);
2086       len = TREE_STRING_LENGTH (exp);
2087       break;
2088
2089     case COMPLEX_CST:
2090       return (const_hash_1 (TREE_REALPART (exp)) * 5
2091               + const_hash_1 (TREE_IMAGPART (exp)));
2092
2093     case CONSTRUCTOR:
2094       if (TREE_CODE (TREE_TYPE (exp)) == SET_TYPE)
2095         {
2096           char *tmp;
2097
2098           len = int_size_in_bytes (TREE_TYPE (exp));
2099           tmp = alloca (len);
2100           get_set_constructor_bytes (exp, (unsigned char *) tmp, len);
2101           p = tmp;
2102           break;
2103         }
2104       else
2105         {
2106           tree link;
2107
2108           hi = 5 + int_size_in_bytes (TREE_TYPE (exp));
2109
2110           for (link = CONSTRUCTOR_ELTS (exp); link; link = TREE_CHAIN (link))
2111             if (TREE_VALUE (link))
2112               hi = hi * 603 + const_hash_1 (TREE_VALUE (link));
2113
2114           return hi;
2115         }
2116
2117     case ADDR_EXPR:
2118     case FDESC_EXPR:
2119       {
2120         struct addr_const value;
2121
2122         decode_addr_const (exp, &value);
2123         if (GET_CODE (value.base) == SYMBOL_REF)
2124           {
2125             /* Don't hash the address of the SYMBOL_REF;
2126                only use the offset and the symbol name.  */
2127             hi = value.offset;
2128             p = XSTR (value.base, 0);
2129             for (i = 0; p[i] != 0; i++)
2130               hi = ((hi * 613) + (unsigned) (p[i]));
2131           }
2132         else if (GET_CODE (value.base) == LABEL_REF)
2133           hi = value.offset + CODE_LABEL_NUMBER (XEXP (value.base, 0)) * 13;
2134         else
2135           abort ();
2136       }
2137       return hi;
2138
2139     case PLUS_EXPR:
2140     case MINUS_EXPR:
2141       return (const_hash_1 (TREE_OPERAND (exp, 0)) * 9
2142               + const_hash_1 (TREE_OPERAND (exp, 1)));
2143
2144     case NOP_EXPR:
2145     case CONVERT_EXPR:
2146     case NON_LVALUE_EXPR:
2147       return const_hash_1 (TREE_OPERAND (exp, 0)) * 7 + 2;
2148
2149     default:
2150       /* A language specific constant. Just hash the code.  */
2151       return code;
2152     }
2153
2154   /* Compute hashing function.  */
2155   hi = len;
2156   for (i = 0; i < len; i++)
2157     hi = ((hi * 613) + (unsigned) (p[i]));
2158
2159   return hi;
2160 }
2161
2162 /* Wrapper of compare_constant, for the htab interface.  */
2163 static int
2164 const_desc_eq (const void *p1, const void *p2)
2165 {
2166   return compare_constant (((struct constant_descriptor_tree *)p1)->value,
2167                            ((struct constant_descriptor_tree *)p2)->value);
2168 }
2169
2170 /* Compare t1 and t2, and return 1 only if they are known to result in
2171    the same bit pattern on output.  */
2172
2173 static int
2174 compare_constant (const tree t1, const tree t2)
2175 {
2176   enum tree_code typecode;
2177
2178   if (t1 == NULL_TREE)
2179     return t2 == NULL_TREE;
2180   if (t2 == NULL_TREE)
2181     return 0;
2182
2183   if (TREE_CODE (t1) != TREE_CODE (t2))
2184     return 0;
2185
2186   switch (TREE_CODE (t1))
2187     {
2188     case INTEGER_CST:
2189       /* Integer constants are the same only if the same width of type.  */
2190       if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
2191         return 0;
2192       return tree_int_cst_equal (t1, t2);
2193
2194     case REAL_CST:
2195       /* Real constants are the same only if the same width of type.  */
2196       if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
2197         return 0;
2198
2199       return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
2200
2201     case STRING_CST:
2202       if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
2203         return 0;
2204
2205       return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
2206               && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
2207                          TREE_STRING_LENGTH (t1)));
2208
2209     case COMPLEX_CST:
2210       return (compare_constant (TREE_REALPART (t1), TREE_REALPART (t2))
2211               && compare_constant (TREE_IMAGPART (t1), TREE_IMAGPART (t2)));
2212
2213     case CONSTRUCTOR:
2214       typecode = TREE_CODE (TREE_TYPE (t1));
2215       if (typecode != TREE_CODE (TREE_TYPE (t2)))
2216         return 0;
2217
2218       if (typecode == SET_TYPE)
2219         {
2220           int len = int_size_in_bytes (TREE_TYPE (t2));
2221           unsigned char *tmp1, *tmp2;
2222
2223           if (int_size_in_bytes (TREE_TYPE (t1)) != len)
2224             return 0;
2225
2226           tmp1 = alloca (len);
2227           tmp2 = alloca (len);
2228
2229           if (get_set_constructor_bytes (t1, tmp1, len) != NULL_TREE)
2230             return 0;
2231           if (get_set_constructor_bytes (t2, tmp2, len) != NULL_TREE)
2232             return 0;
2233
2234           return memcmp (tmp1, tmp2, len) == 0;
2235         }
2236       else
2237         {
2238           tree l1, l2;
2239
2240           if (typecode == ARRAY_TYPE)
2241             {
2242               HOST_WIDE_INT size_1 = int_size_in_bytes (TREE_TYPE (t1));
2243               /* For arrays, check that the sizes all match.  */
2244               if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))
2245                   || size_1 == -1
2246                   || size_1 != int_size_in_bytes (TREE_TYPE (t2)))
2247                 return 0;
2248             }
2249           else
2250             {
2251               /* For record and union constructors, require exact type
2252                  equality.  */
2253               if (TREE_TYPE (t1) != TREE_TYPE (t2))
2254                 return 0;
2255             }
2256
2257           for (l1 = CONSTRUCTOR_ELTS (t1), l2 = CONSTRUCTOR_ELTS (t2);
2258                l1 && l2;
2259                l1 = TREE_CHAIN (l1), l2 = TREE_CHAIN (l2))
2260             {
2261               /* Check that each value is the same...  */
2262               if (! compare_constant (TREE_VALUE (l1), TREE_VALUE (l2)))
2263                 return 0;
2264               /* ... and that they apply to the same fields!  */
2265               if (typecode == ARRAY_TYPE)
2266                 {
2267                   if (! compare_constant (TREE_PURPOSE (l1),
2268                                           TREE_PURPOSE (l2)))
2269                     return 0;
2270                 }
2271               else
2272                 {
2273                   if (TREE_PURPOSE (l1) != TREE_PURPOSE (l2))
2274                     return 0;
2275                 }
2276             }
2277
2278           return l1 == NULL_TREE && l2 == NULL_TREE;
2279         }
2280
2281     case ADDR_EXPR:
2282     case FDESC_EXPR:
2283       {
2284         struct addr_const value1, value2;
2285
2286         decode_addr_const (t1, &value1);
2287         decode_addr_const (t2, &value2);
2288         return (value1.offset == value2.offset
2289                 && strcmp (XSTR (value1.base, 0), XSTR (value2.base, 0)) == 0);
2290       }
2291
2292     case PLUS_EXPR:
2293     case MINUS_EXPR:
2294     case RANGE_EXPR:
2295       return (compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
2296               && compare_constant(TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
2297
2298     case NOP_EXPR:
2299     case CONVERT_EXPR:
2300     case NON_LVALUE_EXPR:
2301       return compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2302
2303     default:
2304       {
2305         tree nt1, nt2;
2306         nt1 = lang_hooks.expand_constant (t1);
2307         nt2 = lang_hooks.expand_constant (t2);
2308         if (nt1 != t1 || nt2 != t2)
2309           return compare_constant (nt1, nt2);
2310         else
2311           return 0;
2312       }
2313     }
2314
2315   /* Should not get here.  */
2316   abort ();
2317 }
2318 \f
2319 /* Make a copy of the whole tree structure for a constant.  This
2320    handles the same types of nodes that compare_constant handles.  */
2321
2322 static tree
2323 copy_constant (tree exp)
2324 {
2325   switch (TREE_CODE (exp))
2326     {
2327     case ADDR_EXPR:
2328       /* For ADDR_EXPR, we do not want to copy the decl whose address
2329          is requested.  We do want to copy constants though.  */
2330       if (TREE_CODE_CLASS (TREE_CODE (TREE_OPERAND (exp, 0))) == 'c')
2331         return build1 (TREE_CODE (exp), TREE_TYPE (exp),
2332                        copy_constant (TREE_OPERAND (exp, 0)));
2333       else
2334         return copy_node (exp);
2335
2336     case INTEGER_CST:
2337     case REAL_CST:
2338     case STRING_CST:
2339       return copy_node (exp);
2340
2341     case COMPLEX_CST:
2342       return build_complex (TREE_TYPE (exp),
2343                             copy_constant (TREE_REALPART (exp)),
2344                             copy_constant (TREE_IMAGPART (exp)));
2345
2346     case PLUS_EXPR:
2347     case MINUS_EXPR:
2348       return build (TREE_CODE (exp), TREE_TYPE (exp),
2349                     copy_constant (TREE_OPERAND (exp, 0)),
2350                     copy_constant (TREE_OPERAND (exp, 1)));
2351
2352     case NOP_EXPR:
2353     case CONVERT_EXPR:
2354     case NON_LVALUE_EXPR:
2355     case VIEW_CONVERT_EXPR:
2356       return build1 (TREE_CODE (exp), TREE_TYPE (exp),
2357                      copy_constant (TREE_OPERAND (exp, 0)));
2358
2359     case CONSTRUCTOR:
2360       {
2361         tree copy = copy_node (exp);
2362         tree list = copy_list (CONSTRUCTOR_ELTS (exp));
2363         tree tail;
2364
2365         CONSTRUCTOR_ELTS (copy) = list;
2366         for (tail = list; tail; tail = TREE_CHAIN (tail))
2367           TREE_VALUE (tail) = copy_constant (TREE_VALUE (tail));
2368         if (TREE_CODE (TREE_TYPE (exp)) == SET_TYPE)
2369           for (tail = list; tail; tail = TREE_CHAIN (tail))
2370             TREE_PURPOSE (tail) = copy_constant (TREE_PURPOSE (tail));
2371
2372         return copy;
2373       }
2374
2375     default:
2376       {
2377         tree t;
2378         t = lang_hooks.expand_constant (exp);
2379         if (t != exp)
2380           return copy_constant (t);
2381         else
2382           abort ();
2383       }
2384     }
2385 }
2386 \f
2387 /* Subroutine of output_constant_def:
2388    No constant equal to EXP is known to have been output.
2389    Make a constant descriptor to enter EXP in the hash table.
2390    Assign the label number and construct RTL to refer to the
2391    constant's location in memory.
2392    Caller is responsible for updating the hash table.  */
2393
2394 static struct constant_descriptor_tree *
2395 build_constant_desc (tree exp)
2396 {
2397   rtx symbol;
2398   rtx rtl;
2399   char label[256];
2400   int labelno;
2401   struct constant_descriptor_tree *desc;
2402
2403   desc = ggc_alloc (sizeof (*desc));
2404   desc->value = copy_constant (exp);
2405
2406   /* Create a string containing the label name, in LABEL.  */
2407   labelno = const_labelno++;
2408   ASM_GENERATE_INTERNAL_LABEL (label, "LC", labelno);
2409
2410   /* We have a symbol name; construct the SYMBOL_REF and the MEM.  */
2411   symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
2412   SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_LOCAL;
2413   SYMBOL_REF_DECL (symbol) = desc->value;
2414   TREE_CONSTANT_POOL_ADDRESS_P (symbol) = 1;
2415
2416   rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (exp)), symbol);
2417   set_mem_attributes (rtl, exp, 1);
2418   set_mem_alias_set (rtl, 0);
2419   set_mem_alias_set (rtl, const_alias_set);
2420
2421   /* Set flags or add text to the name to record information, such as
2422      that it is a local symbol.  If the name is changed, the macro
2423      ASM_OUTPUT_LABELREF will have to know how to strip this
2424      information.  This call might invalidate our local variable
2425      SYMBOL; we can't use it afterward.  */
2426
2427   targetm.encode_section_info (exp, rtl, true);
2428
2429   desc->rtl = rtl;
2430
2431   return desc;
2432 }
2433
2434 /* Return an rtx representing a reference to constant data in memory
2435    for the constant expression EXP.
2436
2437    If assembler code for such a constant has already been output,
2438    return an rtx to refer to it.
2439    Otherwise, output such a constant in memory
2440    and generate an rtx for it.
2441
2442    If DEFER is nonzero, this constant can be deferred and output only
2443    if referenced in the function after all optimizations.
2444
2445    `const_desc_table' records which constants already have label strings.  */
2446
2447 rtx
2448 output_constant_def (tree exp, int defer)
2449 {
2450   struct constant_descriptor_tree *desc;
2451   struct constant_descriptor_tree key;
2452   void **loc;
2453
2454   /* Look up EXP in the table of constant descriptors.  If we didn't find
2455      it, create a new one.  */
2456   key.value = exp;
2457   loc = htab_find_slot (const_desc_htab, &key, INSERT);
2458
2459   desc = *loc;
2460   if (desc == 0)
2461     {
2462       desc = build_constant_desc (exp);
2463       *loc = desc;
2464     }
2465
2466   maybe_output_constant_def_contents (desc, defer);
2467   return desc->rtl;
2468 }
2469
2470 /* Subroutine of output_constant_def: Decide whether or not we need to
2471    output the constant DESC now, and if so, do it.  */
2472 static void
2473 maybe_output_constant_def_contents (struct constant_descriptor_tree *desc,
2474                                     int defer)
2475 {
2476   rtx symbol = XEXP (desc->rtl, 0);
2477   tree exp = desc->value;
2478
2479   if (flag_syntax_only)
2480     return;
2481
2482   if (TREE_ASM_WRITTEN (exp))
2483     /* Already output; don't do it again.  */
2484     return;
2485
2486   /* We can always defer constants as long as the context allows
2487      doing so.  */
2488   if (defer)
2489     {
2490       /* Increment n_deferred_constants if it exists.  It needs to be at
2491          least as large as the number of constants actually referred to
2492          by the function.  If it's too small we'll stop looking too early
2493          and fail to emit constants; if it's too large we'll only look
2494          through the entire function when we could have stopped earlier.  */
2495       if (cfun)
2496         n_deferred_constants++;
2497       return;
2498     }
2499
2500   output_constant_def_contents (symbol);
2501 }
2502
2503 /* We must output the constant data referred to by SYMBOL; do so.  */
2504
2505 static void
2506 output_constant_def_contents (rtx symbol)
2507 {
2508   tree exp = SYMBOL_REF_DECL (symbol);
2509   const char *label = XSTR (symbol, 0);
2510   HOST_WIDE_INT size;
2511
2512   /* Make sure any other constants whose addresses appear in EXP
2513      are assigned label numbers.  */
2514   int reloc = compute_reloc_for_constant (exp);
2515
2516   /* Align the location counter as required by EXP's data type.  */
2517   int align = TYPE_ALIGN (TREE_TYPE (exp));
2518 #ifdef CONSTANT_ALIGNMENT
2519   align = CONSTANT_ALIGNMENT (exp, align);
2520 #endif
2521
2522   output_addressed_constants (exp);
2523
2524   /* We are no longer deferring this constant.  */
2525   TREE_ASM_WRITTEN (exp) = 1;
2526
2527   if (IN_NAMED_SECTION (exp))
2528     named_section (exp, NULL, reloc);
2529   else
2530     targetm.asm_out.select_section (exp, reloc, align);
2531
2532   if (align > BITS_PER_UNIT)
2533     {
2534       ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
2535     }
2536
2537   size = int_size_in_bytes (TREE_TYPE (exp));
2538   if (TREE_CODE (exp) == STRING_CST)
2539     size = MAX (TREE_STRING_LENGTH (exp), size);
2540
2541   /* Do any machine/system dependent processing of the constant.  */
2542 #ifdef ASM_DECLARE_CONSTANT_NAME
2543   ASM_DECLARE_CONSTANT_NAME (asm_out_file, label, exp, size);
2544 #else
2545   /* Standard thing is just output label for the constant.  */
2546   ASM_OUTPUT_LABEL (asm_out_file, label);
2547 #endif /* ASM_DECLARE_CONSTANT_NAME */
2548
2549   /* Output the value of EXP.  */
2550   output_constant (exp, size, align);
2551 }
2552
2553 /* A constant which was deferred in its original location has been
2554    inserted by the RTL inliner into a different function.  The
2555    current function's deferred constant count must be incremented.  */
2556 void
2557 notice_rtl_inlining_of_deferred_constant (void)
2558 {
2559   n_deferred_constants++;
2560 }
2561
2562 /* Look up EXP in the table of constant descriptors.  Return the rtl
2563    if it has been emitted, else null.  */
2564
2565 rtx
2566 lookup_constant_def (tree exp)
2567 {
2568   struct constant_descriptor_tree *desc;
2569   struct constant_descriptor_tree key;
2570
2571   key.value = exp;
2572   desc = htab_find (const_desc_htab, &key);
2573
2574   return (desc ? desc->rtl : NULL_RTX);
2575 }
2576 \f
2577 /* Used in the hash tables to avoid outputting the same constant
2578    twice.  Unlike 'struct constant_descriptor_tree', RTX constants
2579    are output once per function, not once per file.  */
2580 /* ??? Only a few targets need per-function constant pools.  Most
2581    can use one per-file pool.  Should add a targetm bit to tell the
2582    difference.  */
2583
2584 struct rtx_constant_pool GTY(())
2585 {
2586   /* Pointers to first and last constant in pool, as ordered by offset.  */
2587   struct constant_descriptor_rtx *first;
2588   struct constant_descriptor_rtx *last;
2589
2590   /* Hash facility for making memory-constants from constant rtl-expressions.
2591      It is used on RISC machines where immediate integer arguments and
2592      constant addresses are restricted so that such constants must be stored
2593      in memory.  */
2594   htab_t GTY((param_is (struct constant_descriptor_rtx))) const_rtx_htab;
2595   htab_t GTY((param_is (struct constant_descriptor_rtx))) const_rtx_sym_htab;
2596
2597   /* Current offset in constant pool (does not include any
2598      machine-specific header).  */
2599   HOST_WIDE_INT offset;
2600 };
2601
2602 struct constant_descriptor_rtx GTY((chain_next ("%h.next")))
2603 {
2604   struct constant_descriptor_rtx *next;
2605   rtx mem;
2606   rtx sym;
2607   rtx constant;
2608   HOST_WIDE_INT offset;
2609   hashval_t hash;
2610   enum machine_mode mode;
2611   unsigned int align;
2612   int labelno;
2613   int mark;
2614 };
2615
2616 /* Hash and compare functions for const_rtx_htab.  */
2617
2618 static hashval_t
2619 const_desc_rtx_hash (const void *ptr)
2620 {
2621   const struct constant_descriptor_rtx *desc = ptr;
2622   return desc->hash;
2623 }
2624
2625 static int
2626 const_desc_rtx_eq (const void *a, const void *b)
2627 {
2628   const struct constant_descriptor_rtx *x = a;
2629   const struct constant_descriptor_rtx *y = b;
2630
2631   if (x->mode != y->mode)
2632     return 0;
2633   return rtx_equal_p (x->constant, y->constant);
2634 }
2635
2636 /* Hash and compare functions for const_rtx_sym_htab.  */
2637
2638 static hashval_t
2639 const_desc_rtx_sym_hash (const void *ptr)
2640 {
2641   const struct constant_descriptor_rtx *desc = ptr;
2642   return htab_hash_string (XSTR (desc->sym, 0));
2643 }
2644
2645 static int
2646 const_desc_rtx_sym_eq (const void *a, const void *b)
2647 {
2648   const struct constant_descriptor_rtx *x = a;
2649   const struct constant_descriptor_rtx *y = b;
2650   return XSTR (x->sym, 0) == XSTR (y->sym, 0);
2651 }
2652
2653 /* This is the worker function for const_rtx_hash, called via for_each_rtx.  */
2654
2655 static int
2656 const_rtx_hash_1 (rtx *xp, void *data)
2657 {
2658   unsigned HOST_WIDE_INT hwi;
2659   enum machine_mode mode;
2660   enum rtx_code code;
2661   hashval_t h, *hp;
2662   rtx x;
2663
2664   x = *xp;
2665   code = GET_CODE (x);
2666   mode = GET_MODE (x);
2667   h = (hashval_t) code * 1048573 + mode;
2668
2669   switch (code)
2670     {
2671     case CONST_INT:
2672       hwi = INTVAL (x);
2673     fold_hwi:
2674       {
2675         const int shift = sizeof (hashval_t) * CHAR_BIT;
2676         const int n = sizeof (HOST_WIDE_INT) / sizeof (hashval_t);
2677         int i;
2678
2679         h ^= (hashval_t) hwi;
2680         for (i = 1; i < n; ++i)
2681           {
2682             hwi >>= shift;
2683             h ^= (hashval_t) hwi;
2684           }
2685       }
2686       break;
2687
2688     case CONST_DOUBLE:
2689       if (mode == VOIDmode)
2690         {
2691           hwi = CONST_DOUBLE_LOW (x) ^ CONST_DOUBLE_HIGH (x);
2692           goto fold_hwi;
2693         }
2694       else
2695         h ^= real_hash (CONST_DOUBLE_REAL_VALUE (x));
2696       break;
2697
2698     case SYMBOL_REF:
2699       h ^= htab_hash_string (XSTR (x, 0));
2700       break;
2701
2702     case LABEL_REF:
2703       h = h * 251 + CODE_LABEL_NUMBER (XEXP (x, 0));
2704       break;
2705
2706     case UNSPEC:
2707     case UNSPEC_VOLATILE:
2708       h = h * 251 + XINT (x, 1);
2709       break;
2710
2711     default:
2712       break;
2713     }
2714
2715   hp = data;
2716   *hp = *hp * 509 + h;
2717   return 0;
2718 }
2719
2720 /* Compute a hash value for X, which should be a constant.  */
2721
2722 static hashval_t
2723 const_rtx_hash (rtx x)
2724 {
2725   hashval_t h = 0;
2726   for_each_rtx (&x, const_rtx_hash_1, &h);
2727   return h;
2728 }
2729
2730 \f
2731 /* Initialize constant pool hashing for a new function.  */
2732
2733 void
2734 init_varasm_status (struct function *f)
2735 {
2736   struct varasm_status *p;
2737   struct rtx_constant_pool *pool;
2738
2739   p = ggc_alloc (sizeof (struct varasm_status));
2740   f->varasm = p;
2741
2742   pool = ggc_alloc (sizeof (struct rtx_constant_pool));
2743   p->pool = pool;
2744   p->deferred_constants = 0;
2745
2746   pool->const_rtx_htab = htab_create_ggc (31, const_desc_rtx_hash,
2747                                           const_desc_rtx_eq, NULL);
2748   pool->const_rtx_sym_htab = htab_create_ggc (31, const_desc_rtx_sym_hash,
2749                                               const_desc_rtx_sym_eq, NULL);
2750   pool->first = pool->last = NULL;
2751   pool->offset = 0;
2752 }
2753 \f
2754 /* Given a MINUS expression, simplify it if both sides
2755    include the same symbol.  */
2756
2757 rtx
2758 simplify_subtraction (rtx x)
2759 {
2760   rtx r = simplify_rtx (x);
2761   return r ? r : x;
2762 }
2763 \f
2764 /* Given a constant rtx X, make (or find) a memory constant for its value
2765    and return a MEM rtx to refer to it in memory.  */
2766
2767 rtx
2768 force_const_mem (enum machine_mode mode, rtx x)
2769 {
2770   struct constant_descriptor_rtx *desc, tmp;
2771   struct rtx_constant_pool *pool = cfun->varasm->pool;
2772   char label[256];
2773   rtx def, symbol;
2774   hashval_t hash;
2775   unsigned int align;
2776   void **slot;
2777
2778   /* If we're not allowed to drop X into the constant pool, don't.  */
2779   if (targetm.cannot_force_const_mem (x))
2780     return NULL_RTX;
2781
2782   /* Lookup the value in the hashtable.  */
2783   tmp.constant = x;
2784   tmp.mode = mode;
2785   hash = const_rtx_hash (x);
2786   slot = htab_find_slot_with_hash (pool->const_rtx_htab, &tmp, hash, INSERT);
2787   desc = *slot;
2788   
2789   /* If the constant was already present, return its memory.  */
2790   if (desc)
2791     return copy_rtx (desc->mem);
2792
2793   /* Otherwise, create a new descriptor.  */
2794   desc = ggc_alloc (sizeof (*desc));
2795   *slot = desc;
2796
2797   /* Align the location counter as required by EXP's data type.  */
2798   align = GET_MODE_ALIGNMENT (mode == VOIDmode ? word_mode : mode);
2799 #ifdef CONSTANT_ALIGNMENT
2800   {
2801     tree type = lang_hooks.types.type_for_mode (mode, 0);
2802     if (type != NULL_TREE)
2803       align = CONSTANT_ALIGNMENT (make_tree (type, x), align);
2804   }
2805 #endif
2806
2807   pool->offset += (align / BITS_PER_UNIT) - 1;
2808   pool->offset &= ~ ((align / BITS_PER_UNIT) - 1);
2809
2810   desc->next = NULL;
2811   desc->constant = tmp.constant;
2812   desc->offset = pool->offset;
2813   desc->hash = hash;
2814   desc->mode = mode;
2815   desc->align = align;
2816   desc->labelno = const_labelno;
2817   desc->mark = 0;
2818
2819   pool->offset += GET_MODE_SIZE (mode);
2820   if (pool->last)
2821     pool->last->next = desc;
2822   else
2823     pool->first = pool->last = desc;
2824   pool->last = desc;
2825
2826   /* Create a string containing the label name, in LABEL.  */
2827   ASM_GENERATE_INTERNAL_LABEL (label, "LC", const_labelno);
2828   ++const_labelno;
2829
2830   /* Construct the SYMBOL_REF.  Make sure to mark it as belonging to
2831      the constants pool.  */
2832   desc->sym = symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
2833   SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_LOCAL;
2834   CONSTANT_POOL_ADDRESS_P (symbol) = 1;
2835   current_function_uses_const_pool = 1;
2836
2837   /* Insert the descriptor into the symbol cross-reference table too.  */
2838   slot = htab_find_slot (pool->const_rtx_sym_htab, desc, INSERT);
2839   if (*slot)
2840     abort ();
2841   *slot = desc;
2842
2843   /* Construct the MEM.  */
2844   desc->mem = def = gen_rtx_MEM (mode, symbol);
2845   set_mem_attributes (def, lang_hooks.types.type_for_mode (mode, 0), 1);
2846   RTX_UNCHANGING_P (def) = 1;
2847
2848   /* If we're dropping a label to the constant pool, make sure we
2849      don't delete it.  */
2850   if (GET_CODE (x) == LABEL_REF)
2851     LABEL_PRESERVE_P (XEXP (x, 0)) = 1;
2852
2853   return copy_rtx (def);
2854 }
2855 \f
2856 /* Given a SYMBOL_REF with CONSTANT_POOL_ADDRESS_P true, return a pointer to
2857    the corresponding constant_descriptor_rtx structure.  */
2858
2859 static struct constant_descriptor_rtx *
2860 find_pool_constant (struct rtx_constant_pool *pool, rtx sym)
2861 {
2862   struct constant_descriptor_rtx tmp;
2863   tmp.sym = sym;
2864   return htab_find (pool->const_rtx_sym_htab, &tmp);
2865 }
2866
2867 /* Given a constant pool SYMBOL_REF, return the corresponding constant.  */
2868
2869 rtx
2870 get_pool_constant (rtx addr)
2871 {
2872   return find_pool_constant (cfun->varasm->pool, addr)->constant;
2873 }
2874
2875 /* Given a constant pool SYMBOL_REF, return the corresponding constant
2876    and whether it has been output or not.  */
2877
2878 rtx
2879 get_pool_constant_mark (rtx addr, bool *pmarked)
2880 {
2881   struct constant_descriptor_rtx *desc;
2882
2883   desc = find_pool_constant (cfun->varasm->pool, addr);
2884   *pmarked = (desc->mark != 0);
2885   return desc->constant;
2886 }
2887
2888 /* Likewise, but for the constant pool of a specific function.  */
2889
2890 rtx
2891 get_pool_constant_for_function (struct function *f, rtx addr)
2892 {
2893   return find_pool_constant (f->varasm->pool, addr)->constant;
2894 }
2895
2896 /* Similar, return the mode.  */
2897
2898 enum machine_mode
2899 get_pool_mode (rtx addr)
2900 {
2901   return find_pool_constant (cfun->varasm->pool, addr)->mode;
2902 }
2903
2904 enum machine_mode
2905 get_pool_mode_for_function (struct function *f, rtx addr)
2906 {
2907   return find_pool_constant (f->varasm->pool, addr)->mode;
2908 }
2909
2910 /* Similar, return the offset in the constant pool.  */
2911
2912 int
2913 get_pool_offset (rtx addr)
2914 {
2915   return find_pool_constant (cfun->varasm->pool, addr)->offset;
2916 }
2917
2918 /* Return the size of the constant pool.  */
2919
2920 int
2921 get_pool_size (void)
2922 {
2923   return cfun->varasm->pool->offset;
2924 }
2925 \f
2926 /* Worker function for output_constant_pool_1.  Emit assembly for X
2927    in MODE with known alignment ALIGN.  */
2928
2929 static void
2930 output_constant_pool_2 (enum machine_mode mode, rtx x, unsigned int align)
2931 {
2932   switch (GET_MODE_CLASS (mode))
2933     {
2934     case MODE_FLOAT:
2935       if (GET_CODE (x) != CONST_DOUBLE)
2936         abort ();
2937       else
2938         {
2939           REAL_VALUE_TYPE r;
2940           REAL_VALUE_FROM_CONST_DOUBLE (r, x);
2941           assemble_real (r, mode, align);
2942         }
2943       break;
2944
2945     case MODE_INT:
2946     case MODE_PARTIAL_INT:
2947       assemble_integer (x, GET_MODE_SIZE (mode), align, 1);
2948       break;
2949
2950     case MODE_VECTOR_FLOAT:
2951     case MODE_VECTOR_INT:
2952       {
2953         int i, units;
2954         enum machine_mode submode = GET_MODE_INNER (mode);
2955         unsigned int subalign = MIN (align, GET_MODE_BITSIZE (submode));
2956
2957         if (GET_CODE (x) != CONST_VECTOR)
2958           abort ();
2959         units = CONST_VECTOR_NUNITS (x);
2960
2961         for (i = 0; i < units; i++)
2962           {
2963             rtx elt = CONST_VECTOR_ELT (x, i);
2964             output_constant_pool_2 (submode, elt, i ? subalign : align);
2965           }
2966       }
2967       break;
2968
2969     default:
2970       abort ();
2971     }
2972 }
2973
2974 /* Worker function for output_constant_pool.  Emit POOL.  */
2975
2976 static void
2977 output_constant_pool_1 (struct constant_descriptor_rtx *desc)
2978 {
2979   rtx x, tmp;
2980
2981   if (!desc->mark)
2982     return;
2983   x = desc->constant;
2984
2985   /* See if X is a LABEL_REF (or a CONST referring to a LABEL_REF)
2986      whose CODE_LABEL has been deleted.  This can occur if a jump table
2987      is eliminated by optimization.  If so, write a constant of zero
2988      instead.  Note that this can also happen by turning the
2989      CODE_LABEL into a NOTE.  */
2990   /* ??? This seems completely and utterly wrong.  Certainly it's
2991      not true for NOTE_INSN_DELETED_LABEL, but I disbelieve proper
2992      functioning even with INSN_DELETED_P and friends.  */
2993
2994   tmp = x;
2995   switch (GET_CODE (x))
2996     {
2997     case CONST:
2998       if (GET_CODE (XEXP (x, 0)) != PLUS
2999           || GET_CODE (XEXP (XEXP (x, 0), 0)) != LABEL_REF)
3000         break;
3001       tmp = XEXP (XEXP (x, 0), 0);
3002       /* FALLTHRU  */
3003
3004     case LABEL_REF:
3005       tmp = XEXP (x, 0);
3006       if (INSN_DELETED_P (tmp)
3007           || (GET_CODE (tmp) == NOTE
3008               && NOTE_LINE_NUMBER (tmp) == NOTE_INSN_DELETED))
3009         {
3010           abort ();
3011           x = const0_rtx;
3012         }
3013       break;
3014
3015     default:
3016       break;
3017     }
3018
3019   /* First switch to correct section.  */
3020   targetm.asm_out.select_rtx_section (desc->mode, x, desc->align);
3021
3022 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3023   ASM_OUTPUT_SPECIAL_POOL_ENTRY (asm_out_file, x, desc->mode,
3024                                  desc->align, desc->labelno, done);
3025 #endif
3026
3027   assemble_align (desc->align);
3028
3029   /* Output the label.  */
3030   targetm.asm_out.internal_label (asm_out_file, "LC", desc->labelno);
3031
3032   /* Output the data.  */
3033   output_constant_pool_2 (desc->mode, x, desc->align);
3034
3035   /* Make sure all constants in SECTION_MERGE and not SECTION_STRINGS
3036      sections have proper size.  */
3037   if (desc->align > GET_MODE_BITSIZE (desc->mode)
3038       && in_section == in_named
3039       && get_named_section_flags (in_named_name) & SECTION_MERGE)
3040     assemble_align (desc->align);
3041
3042 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3043  done:
3044 #endif
3045   return;
3046 }
3047
3048 /* Given a SYMBOL_REF CURRENT_RTX, mark it and all constants it refers
3049    to as used.  Emit referenced deferred strings.  This function can
3050    be used with for_each_rtx to mark all SYMBOL_REFs in an rtx.  */
3051
3052 static int
3053 mark_constant (rtx *current_rtx, void *data)
3054 {
3055   struct rtx_constant_pool *pool = data;
3056   rtx x = *current_rtx;
3057
3058   if (x == NULL_RTX || GET_CODE (x) != SYMBOL_REF)
3059     return 0;
3060
3061   if (CONSTANT_POOL_ADDRESS_P (x))
3062     {
3063       struct constant_descriptor_rtx *desc = find_pool_constant (pool, x);
3064       if (desc->mark == 0)
3065         {
3066           desc->mark = 1;
3067           for_each_rtx (&desc->constant, mark_constant, pool);
3068         }
3069     }
3070   else if (TREE_CONSTANT_POOL_ADDRESS_P (x))
3071     {
3072       tree exp = SYMBOL_REF_DECL (x);
3073       if (!TREE_ASM_WRITTEN (exp))
3074         {
3075           n_deferred_constants--;
3076           output_constant_def_contents (x);
3077         }
3078     }
3079
3080   return -1;
3081
3082
3083 /* Look through appropriate parts of INSN, marking all entries in the
3084    constant pool which are actually being used.  Entries that are only
3085    referenced by other constants are also marked as used.  Emit
3086    deferred strings that are used.  */
3087
3088 static void
3089 mark_constants (struct rtx_constant_pool *pool, rtx insn)
3090 {
3091   if (!INSN_P (insn))
3092     return;
3093
3094   /* Insns may appear inside a SEQUENCE.  Only check the patterns of
3095      insns, not any notes that may be attached.  We don't want to mark
3096      a constant just because it happens to appear in a REG_EQUIV note.  */
3097   if (GET_CODE (PATTERN (insn)) == SEQUENCE)
3098     {
3099       rtx seq = PATTERN (insn);
3100       int i, n = XVECLEN (seq, 0);
3101       for (i = 0; i < n; ++i)
3102         {
3103           rtx subinsn = XVECEXP (seq, 0, i);
3104           if (INSN_P (subinsn))
3105             for_each_rtx (&PATTERN (subinsn), mark_constant, pool);
3106         }
3107     }
3108   else
3109     for_each_rtx (&PATTERN (insn), mark_constant, pool);
3110 }
3111
3112 /* Look through the instructions for this function, and mark all the
3113    entries in POOL which are actually being used.  Emit deferred constants
3114    which have indeed been used.  */
3115
3116 static void
3117 mark_constant_pool (struct rtx_constant_pool *pool)
3118 {
3119   rtx insn, link;
3120
3121   if (pool->first == 0 && n_deferred_constants == 0)
3122     return;
3123
3124   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3125     mark_constants (pool, insn);
3126
3127   for (link = current_function_epilogue_delay_list;
3128        link;
3129        link = XEXP (link, 1))
3130     mark_constants (pool, XEXP (link, 0));
3131 }
3132
3133 /* Write all the constants in the constant pool.  */
3134
3135 void
3136 output_constant_pool (const char *fnname ATTRIBUTE_UNUSED,
3137                       tree fndecl ATTRIBUTE_UNUSED)
3138 {
3139   struct rtx_constant_pool *pool = cfun->varasm->pool;
3140   struct constant_descriptor_rtx *desc;
3141
3142   /* It is possible for gcc to call force_const_mem and then to later
3143      discard the instructions which refer to the constant.  In such a
3144      case we do not need to output the constant.  */
3145   mark_constant_pool (pool);
3146
3147 #ifdef ASM_OUTPUT_POOL_PROLOGUE
3148   ASM_OUTPUT_POOL_PROLOGUE (asm_out_file, fnname, fndecl, pool->offset);
3149 #endif
3150
3151   for (desc = pool->first; desc ; desc = desc->next)
3152     output_constant_pool_1 (desc);
3153
3154 #ifdef ASM_OUTPUT_POOL_EPILOGUE
3155   ASM_OUTPUT_POOL_EPILOGUE (asm_out_file, fnname, fndecl, pool->offset);
3156 #endif
3157 }
3158 \f
3159 /* Determine what kind of relocations EXP may need.  */
3160
3161 int
3162 compute_reloc_for_constant (tree exp)
3163 {
3164   int reloc = 0, reloc2;
3165   tree tem;
3166
3167   /* Give the front-end a chance to convert VALUE to something that
3168      looks more like a constant to the back-end.  */
3169   exp = lang_hooks.expand_constant (exp);
3170
3171   switch (TREE_CODE (exp))
3172     {
3173     case ADDR_EXPR:
3174     case FDESC_EXPR:
3175       /* Go inside any operations that get_inner_reference can handle and see
3176          if what's inside is a constant: no need to do anything here for
3177          addresses of variables or functions.  */
3178       for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
3179            tem = TREE_OPERAND (tem, 0))
3180         ;
3181
3182       if (TREE_PUBLIC (tem))
3183         reloc |= 2;
3184       else
3185         reloc |= 1;
3186       break;
3187
3188     case PLUS_EXPR:
3189       reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3190       reloc |= compute_reloc_for_constant (TREE_OPERAND (exp, 1));
3191       break;
3192
3193     case MINUS_EXPR:
3194       reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3195       reloc2 = compute_reloc_for_constant (TREE_OPERAND (exp, 1));
3196       /* The difference of two local labels is computable at link time.  */
3197       if (reloc == 1 && reloc2 == 1)
3198         reloc = 0;
3199       else
3200         reloc |= reloc2;
3201       break;
3202
3203     case NOP_EXPR:
3204     case CONVERT_EXPR:
3205     case NON_LVALUE_EXPR:
3206       reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3207       break;
3208
3209     case CONSTRUCTOR:
3210       for (tem = CONSTRUCTOR_ELTS (exp); tem; tem = TREE_CHAIN (tem))
3211         if (TREE_VALUE (tem) != 0)
3212           reloc |= compute_reloc_for_constant (TREE_VALUE (tem));
3213
3214       break;
3215
3216     default:
3217       break;
3218     }
3219   return reloc;
3220 }
3221
3222 /* Find all the constants whose addresses are referenced inside of EXP,
3223    and make sure assembler code with a label has been output for each one.
3224    Indicate whether an ADDR_EXPR has been encountered.  */
3225
3226 static void
3227 output_addressed_constants (tree exp)
3228 {
3229   tree tem;
3230
3231   /* Give the front-end a chance to convert VALUE to something that
3232      looks more like a constant to the back-end.  */
3233   exp = lang_hooks.expand_constant (exp);
3234
3235   switch (TREE_CODE (exp))
3236     {
3237     case ADDR_EXPR:
3238     case FDESC_EXPR:
3239       /* Go inside any operations that get_inner_reference can handle and see
3240          if what's inside is a constant: no need to do anything here for
3241          addresses of variables or functions.  */
3242       for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
3243            tem = TREE_OPERAND (tem, 0))
3244         ;
3245
3246       if (TREE_CODE_CLASS (TREE_CODE (tem)) == 'c'
3247           || TREE_CODE (tem) == CONSTRUCTOR)
3248         output_constant_def (tem, 0);
3249       break;
3250
3251     case PLUS_EXPR:
3252     case MINUS_EXPR:
3253       output_addressed_constants (TREE_OPERAND (exp, 1));
3254       /* Fall through.  */
3255
3256     case NOP_EXPR:
3257     case CONVERT_EXPR:
3258     case NON_LVALUE_EXPR:
3259       output_addressed_constants (TREE_OPERAND (exp, 0));
3260       break;
3261
3262     case CONSTRUCTOR:
3263       for (tem = CONSTRUCTOR_ELTS (exp); tem; tem = TREE_CHAIN (tem))
3264         if (TREE_VALUE (tem) != 0)
3265           output_addressed_constants (TREE_VALUE (tem));
3266
3267       break;
3268
3269     default:
3270       break;
3271     }
3272 }
3273 \f
3274 /* Return nonzero if VALUE is a valid constant-valued expression
3275    for use in initializing a static variable; one that can be an
3276    element of a "constant" initializer.
3277
3278    Return null_pointer_node if the value is absolute;
3279    if it is relocatable, return the variable that determines the relocation.
3280    We assume that VALUE has been folded as much as possible;
3281    therefore, we do not need to check for such things as
3282    arithmetic-combinations of integers.  */
3283
3284 tree
3285 initializer_constant_valid_p (tree value, tree endtype)
3286 {
3287   /* Give the front-end a chance to convert VALUE to something that
3288      looks more like a constant to the back-end.  */
3289   value = lang_hooks.expand_constant (value);
3290
3291   switch (TREE_CODE (value))
3292     {
3293     case CONSTRUCTOR:
3294       if ((TREE_CODE (TREE_TYPE (value)) == UNION_TYPE
3295            || TREE_CODE (TREE_TYPE (value)) == RECORD_TYPE)
3296           && TREE_CONSTANT (value)
3297           && CONSTRUCTOR_ELTS (value))
3298         {
3299           tree elt;
3300           bool absolute = true;
3301
3302           for (elt = CONSTRUCTOR_ELTS (value); elt; elt = TREE_CHAIN (elt))
3303             {
3304               tree reloc;
3305               value = TREE_VALUE (elt);
3306               reloc = initializer_constant_valid_p (value, TREE_TYPE (value));
3307               if (!reloc)
3308                 return NULL_TREE;
3309               if (reloc != null_pointer_node)
3310                 absolute = false;
3311             }
3312           /* For a non-absolute relocation, there is no single
3313              variable that can be "the variable that determines the
3314              relocation."  */
3315           return absolute ? null_pointer_node : error_mark_node;
3316         }
3317
3318       return TREE_STATIC (value) ? null_pointer_node : NULL_TREE;
3319
3320     case INTEGER_CST:
3321     case VECTOR_CST:
3322     case REAL_CST:
3323     case STRING_CST:
3324     case COMPLEX_CST:
3325       return null_pointer_node;
3326
3327     case ADDR_EXPR:
3328     case FDESC_EXPR:
3329       return staticp (TREE_OPERAND (value, 0)) ? TREE_OPERAND (value, 0) : 0;
3330
3331     case VIEW_CONVERT_EXPR:
3332     case NON_LVALUE_EXPR:
3333       return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
3334
3335     case CONVERT_EXPR:
3336     case NOP_EXPR:
3337       /* Allow conversions between pointer types.  */
3338       if (POINTER_TYPE_P (TREE_TYPE (value))
3339           && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (value, 0))))
3340         return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
3341
3342       /* Allow conversions between real types.  */
3343       if (FLOAT_TYPE_P (TREE_TYPE (value))
3344           && FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (value, 0))))
3345         return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
3346
3347       /* Allow length-preserving conversions between integer types.  */
3348       if (INTEGRAL_TYPE_P (TREE_TYPE (value))
3349           && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (value, 0)))
3350           && (TYPE_PRECISION (TREE_TYPE (value))
3351               == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0)))))
3352         return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
3353
3354       /* Allow conversions between other integer types only if
3355          explicit value.  */
3356       if (INTEGRAL_TYPE_P (TREE_TYPE (value))
3357           && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (value, 0))))
3358         {
3359           tree inner = initializer_constant_valid_p (TREE_OPERAND (value, 0),
3360                                                      endtype);
3361           if (inner == null_pointer_node)
3362             return null_pointer_node;
3363           break;
3364         }
3365
3366       /* Allow (int) &foo provided int is as wide as a pointer.  */
3367       if (INTEGRAL_TYPE_P (TREE_TYPE (value))
3368           && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (value, 0)))
3369           && (TYPE_PRECISION (TREE_TYPE (value))
3370               >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0)))))
3371         return initializer_constant_valid_p (TREE_OPERAND (value, 0),
3372                                              endtype);
3373
3374       /* Likewise conversions from int to pointers, but also allow
3375          conversions from 0.  */
3376       if ((POINTER_TYPE_P (TREE_TYPE (value))
3377            || TREE_CODE (TREE_TYPE (value)) == OFFSET_TYPE)
3378           && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (value, 0))))
3379         {
3380           if (integer_zerop (TREE_OPERAND (value, 0)))
3381             return null_pointer_node;
3382           else if (TYPE_PRECISION (TREE_TYPE (value))
3383                    <= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0))))
3384             return initializer_constant_valid_p (TREE_OPERAND (value, 0),
3385                                                  endtype);
3386         }
3387
3388       /* Allow conversions to struct or union types if the value
3389          inside is okay.  */
3390       if (TREE_CODE (TREE_TYPE (value)) == RECORD_TYPE
3391           || TREE_CODE (TREE_TYPE (value)) == UNION_TYPE)
3392         return initializer_constant_valid_p (TREE_OPERAND (value, 0),
3393                                              endtype);
3394       break;
3395
3396     case PLUS_EXPR:
3397       if (! INTEGRAL_TYPE_P (endtype)
3398           || TYPE_PRECISION (endtype) >= POINTER_SIZE)
3399         {
3400           tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
3401                                                       endtype);
3402           tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
3403                                                       endtype);
3404           /* If either term is absolute, use the other terms relocation.  */
3405           if (valid0 == null_pointer_node)
3406             return valid1;
3407           if (valid1 == null_pointer_node)
3408             return valid0;
3409         }
3410       break;
3411
3412     case MINUS_EXPR:
3413       if (! INTEGRAL_TYPE_P (endtype)
3414           || TYPE_PRECISION (endtype) >= POINTER_SIZE)
3415         {
3416           tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
3417                                                       endtype);
3418           tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
3419                                                       endtype);
3420           /* Win if second argument is absolute.  */
3421           if (valid1 == null_pointer_node)
3422             return valid0;
3423           /* Win if both arguments have the same relocation.
3424              Then the value is absolute.  */
3425           if (valid0 == valid1 && valid0 != 0)
3426             return null_pointer_node;
3427
3428           /* Since GCC guarantees that string constants are unique in the
3429              generated code, a subtraction between two copies of the same
3430              constant string is absolute.  */
3431           if (valid0 && TREE_CODE (valid0) == STRING_CST &&
3432               valid1 && TREE_CODE (valid1) == STRING_CST &&
3433               TREE_STRING_POINTER (valid0) == TREE_STRING_POINTER (valid1))
3434             return null_pointer_node;
3435         }
3436
3437       /* Support differences between labels.  */
3438       if (INTEGRAL_TYPE_P (endtype))
3439         {
3440           tree op0, op1;
3441           op0 = TREE_OPERAND (value, 0);
3442           op1 = TREE_OPERAND (value, 1);
3443
3444           /* Like STRIP_NOPS except allow the operand mode to widen.
3445              This works around a feature of fold that simplifies
3446              (int)(p1 - p2) to ((int)p1 - (int)p2) under the theory
3447              that the narrower operation is cheaper.  */
3448
3449           while (TREE_CODE (op0) == NOP_EXPR
3450                  || TREE_CODE (op0) == CONVERT_EXPR
3451                  || TREE_CODE (op0) == NON_LVALUE_EXPR)
3452             {
3453               tree inner = TREE_OPERAND (op0, 0);
3454               if (inner == error_mark_node
3455                   || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
3456                   || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))
3457                       > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
3458                 break;
3459               op0 = inner;
3460             }
3461
3462           while (TREE_CODE (op1) == NOP_EXPR
3463                  || TREE_CODE (op1) == CONVERT_EXPR
3464                  || TREE_CODE (op1) == NON_LVALUE_EXPR)
3465             {
3466               tree inner = TREE_OPERAND (op1, 0);
3467               if (inner == error_mark_node
3468                   || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
3469                   || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op1)))
3470                       > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
3471                 break;
3472               op1 = inner;
3473             }
3474
3475           if (TREE_CODE (op0) == ADDR_EXPR
3476               && TREE_CODE (TREE_OPERAND (op0, 0)) == LABEL_DECL
3477               && TREE_CODE (op1) == ADDR_EXPR
3478               && TREE_CODE (TREE_OPERAND (op1, 0)) == LABEL_DECL)
3479             return null_pointer_node;
3480         }
3481       break;
3482
3483     default:
3484       break;
3485     }
3486
3487   return 0;
3488 }
3489 \f
3490 /* Output assembler code for constant EXP to FILE, with no label.
3491    This includes the pseudo-op such as ".int" or ".byte", and a newline.
3492    Assumes output_addressed_constants has been done on EXP already.
3493
3494    Generate exactly SIZE bytes of assembler data, padding at the end
3495    with zeros if necessary.  SIZE must always be specified.
3496
3497    SIZE is important for structure constructors,
3498    since trailing members may have been omitted from the constructor.
3499    It is also important for initialization of arrays from string constants
3500    since the full length of the string constant might not be wanted.
3501    It is also needed for initialization of unions, where the initializer's
3502    type is just one member, and that may not be as long as the union.
3503
3504    There a case in which we would fail to output exactly SIZE bytes:
3505    for a structure constructor that wants to produce more than SIZE bytes.
3506    But such constructors will never be generated for any possible input.
3507
3508    ALIGN is the alignment of the data in bits.  */
3509
3510 void
3511 output_constant (tree exp, unsigned HOST_WIDE_INT size, unsigned int align)
3512 {
3513   enum tree_code code;
3514   unsigned HOST_WIDE_INT thissize;
3515
3516   /* Some front-ends use constants other than the standard language-independent
3517      varieties, but which may still be output directly.  Give the front-end a
3518      chance to convert EXP to a language-independent representation.  */
3519   exp = lang_hooks.expand_constant (exp);
3520
3521   if (size == 0 || flag_syntax_only)
3522     return;
3523
3524   /* Eliminate any conversions since we'll be outputting the underlying
3525      constant.  */
3526   while (TREE_CODE (exp) == NOP_EXPR || TREE_CODE (exp) == CONVERT_EXPR
3527          || TREE_CODE (exp) == NON_LVALUE_EXPR
3528          || TREE_CODE (exp) == VIEW_CONVERT_EXPR)
3529     exp = TREE_OPERAND (exp, 0);
3530
3531   code = TREE_CODE (TREE_TYPE (exp));
3532   thissize = int_size_in_bytes (TREE_TYPE (exp));
3533
3534   /* Allow a constructor with no elements for any data type.
3535      This means to fill the space with zeros.  */
3536   if (TREE_CODE (exp) == CONSTRUCTOR && CONSTRUCTOR_ELTS (exp) == 0)
3537     {
3538       assemble_zeros (size);
3539       return;
3540     }
3541
3542   if (TREE_CODE (exp) == FDESC_EXPR)
3543     {
3544 #ifdef ASM_OUTPUT_FDESC
3545       HOST_WIDE_INT part = tree_low_cst (TREE_OPERAND (exp, 1), 0);
3546       tree decl = TREE_OPERAND (exp, 0);
3547       ASM_OUTPUT_FDESC (asm_out_file, decl, part);
3548 #else
3549       abort ();
3550 #endif
3551       return;
3552     }
3553
3554   /* Now output the underlying data.  If we've handling the padding, return.
3555      Otherwise, break and ensure THISSIZE is the size written.  */
3556   switch (code)
3557     {
3558     case CHAR_TYPE:
3559     case BOOLEAN_TYPE:
3560     case INTEGER_TYPE:
3561     case ENUMERAL_TYPE:
3562     case POINTER_TYPE:
3563     case REFERENCE_TYPE:
3564     case OFFSET_TYPE:
3565       if (! assemble_integer (expand_expr (exp, NULL_RTX, VOIDmode,
3566                                            EXPAND_INITIALIZER),
3567                               size, align, 0))
3568         error ("initializer for integer value is too complicated");
3569       break;
3570
3571     case REAL_TYPE:
3572       if (TREE_CODE (exp) != REAL_CST)
3573         error ("initializer for floating value is not a floating constant");
3574
3575       assemble_real (TREE_REAL_CST (exp), TYPE_MODE (TREE_TYPE (exp)), align);
3576       break;
3577
3578     case COMPLEX_TYPE:
3579       output_constant (TREE_REALPART (exp), thissize / 2, align);
3580       output_constant (TREE_IMAGPART (exp), thissize / 2,
3581                        min_align (align, BITS_PER_UNIT * (thissize / 2)));
3582       break;
3583
3584     case ARRAY_TYPE:
3585     case VECTOR_TYPE:
3586       if (TREE_CODE (exp) == CONSTRUCTOR)
3587         {
3588           output_constructor (exp, size, align);
3589           return;
3590         }
3591       else if (TREE_CODE (exp) == STRING_CST)
3592         {
3593           thissize = MIN ((unsigned HOST_WIDE_INT)TREE_STRING_LENGTH (exp),
3594                           size);
3595           assemble_string (TREE_STRING_POINTER (exp), thissize);
3596         }
3597       else if (TREE_CODE (exp) == VECTOR_CST)
3598         {
3599           int elt_size;
3600           tree link;
3601           unsigned int nalign;
3602           enum machine_mode inner;
3603
3604           inner = GET_MODE_INNER (TYPE_MODE (TREE_TYPE (exp)));
3605           nalign = MIN (align, GET_MODE_ALIGNMENT (inner));
3606
3607           elt_size = GET_MODE_UNIT_SIZE (TYPE_MODE (TREE_TYPE (exp)));
3608
3609           link = TREE_VECTOR_CST_ELTS (exp);
3610           output_constant (TREE_VALUE (link), elt_size, align);
3611           while ((link = TREE_CHAIN (link)) != NULL)
3612             output_constant (TREE_VALUE (link), elt_size, nalign);
3613         }
3614       else
3615         abort ();
3616       break;
3617
3618     case RECORD_TYPE:
3619     case UNION_TYPE:
3620       if (TREE_CODE (exp) == CONSTRUCTOR)
3621         output_constructor (exp, size, align);
3622       else
3623         abort ();
3624       return;
3625
3626     case SET_TYPE:
3627       if (TREE_CODE (exp) == INTEGER_CST)
3628         assemble_integer (expand_expr (exp, NULL_RTX,
3629                                        VOIDmode, EXPAND_INITIALIZER),
3630                           thissize, align, 1);
3631       else if (TREE_CODE (exp) == CONSTRUCTOR)
3632         {
3633           unsigned char *buffer = alloca (thissize);
3634           if (get_set_constructor_bytes (exp, buffer, thissize))
3635             abort ();
3636           assemble_string ((char *) buffer, thissize);
3637         }
3638       else
3639         error ("unknown set constructor type");
3640       return;
3641
3642     case ERROR_MARK:
3643       return;
3644
3645     default:
3646       abort ();
3647     }
3648
3649   if (size > thissize)
3650     assemble_zeros (size - thissize);
3651 }
3652
3653 \f
3654 /* Subroutine of output_constructor, used for computing the size of
3655    arrays of unspecified length.  VAL must be a CONSTRUCTOR of an array
3656    type with an unspecified upper bound.  */
3657
3658 static unsigned HOST_WIDE_INT
3659 array_size_for_constructor (tree val)
3660 {
3661   tree max_index, i;
3662
3663   /* This code used to attempt to handle string constants that are not
3664      arrays of single-bytes, but nothing else does, so there's no point in
3665      doing it here.  */
3666   if (TREE_CODE (val) == STRING_CST)
3667     return TREE_STRING_LENGTH (val);
3668
3669   max_index = NULL_TREE;
3670   for (i = CONSTRUCTOR_ELTS (val); i; i = TREE_CHAIN (i))
3671     {
3672       tree index = TREE_PURPOSE (i);
3673
3674       if (TREE_CODE (index) == RANGE_EXPR)
3675         index = TREE_OPERAND (index, 1);
3676       if (max_index == NULL_TREE || tree_int_cst_lt (max_index, index))
3677         max_index = index;
3678     }
3679
3680   if (max_index == NULL_TREE)
3681     return 0;
3682
3683   /* Compute the total number of array elements.  */
3684   i = size_binop (MINUS_EXPR, convert (sizetype, max_index),
3685                   convert (sizetype,
3686                            TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (val)))));
3687   i = size_binop (PLUS_EXPR, i, convert (sizetype, integer_one_node));
3688
3689   /* Multiply by the array element unit size to find number of bytes.  */
3690   i = size_binop (MULT_EXPR, i, TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (val))));
3691
3692   return tree_low_cst (i, 1);
3693 }
3694
3695 /* Subroutine of output_constant, used for CONSTRUCTORs (aggregate constants).
3696    Generate at least SIZE bytes, padding if necessary.  */
3697
3698 static void
3699 output_constructor (tree exp, unsigned HOST_WIDE_INT size,
3700                     unsigned int align)
3701 {
3702   tree type = TREE_TYPE (exp);
3703   tree link, field = 0;
3704   tree min_index = 0;
3705   /* Number of bytes output or skipped so far.
3706      In other words, current position within the constructor.  */
3707   HOST_WIDE_INT total_bytes = 0;
3708   /* Nonzero means BYTE contains part of a byte, to be output.  */
3709   int byte_buffer_in_use = 0;
3710   int byte = 0;
3711
3712   if (HOST_BITS_PER_WIDE_INT < BITS_PER_UNIT)
3713     abort ();
3714
3715   if (TREE_CODE (type) == RECORD_TYPE)
3716     field = TYPE_FIELDS (type);
3717
3718   if (TREE_CODE (type) == ARRAY_TYPE
3719       && TYPE_DOMAIN (type) != 0)
3720     min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
3721
3722   /* As LINK goes through the elements of the constant,
3723      FIELD goes through the structure fields, if the constant is a structure.
3724      if the constant is a union, then we override this,
3725      by getting the field from the TREE_LIST element.
3726      But the constant could also be an array.  Then FIELD is zero.
3727
3728      There is always a maximum of one element in the chain LINK for unions
3729      (even if the initializer in a source program incorrectly contains
3730      more one).  */
3731   for (link = CONSTRUCTOR_ELTS (exp);
3732        link;
3733        link = TREE_CHAIN (link),
3734        field = field ? TREE_CHAIN (field) : 0)
3735     {
3736       tree val = TREE_VALUE (link);
3737       tree index = 0;
3738
3739       /* The element in a union constructor specifies the proper field
3740          or index.  */
3741       if ((TREE_CODE (type) == RECORD_TYPE || TREE_CODE (type) == UNION_TYPE
3742            || TREE_CODE (type) == QUAL_UNION_TYPE)
3743           && TREE_PURPOSE (link) != 0)
3744         field = TREE_PURPOSE (link);
3745
3746       else if (TREE_CODE (type) == ARRAY_TYPE)
3747         index = TREE_PURPOSE (link);
3748
3749 #ifdef ASM_COMMENT_START
3750       if (field && flag_verbose_asm)
3751         fprintf (asm_out_file, "%s %s:\n",
3752                  ASM_COMMENT_START, 
3753                  DECL_NAME (field) 
3754                  ? IDENTIFIER_POINTER (DECL_NAME (field))
3755                  : "<anonymous>");
3756 #endif
3757
3758       /* Eliminate the marker that makes a cast not be an lvalue.  */
3759       if (val != 0)
3760         STRIP_NOPS (val);
3761
3762       if (index && TREE_CODE (index) == RANGE_EXPR)
3763         {
3764           unsigned HOST_WIDE_INT fieldsize
3765             = int_size_in_bytes (TREE_TYPE (type));
3766           HOST_WIDE_INT lo_index = tree_low_cst (TREE_OPERAND (index, 0), 0);
3767           HOST_WIDE_INT hi_index = tree_low_cst (TREE_OPERAND (index, 1), 0);
3768           HOST_WIDE_INT index;
3769           unsigned int align2 = min_align (align, fieldsize * BITS_PER_UNIT);
3770
3771           for (index = lo_index; index <= hi_index; index++)
3772             {
3773               /* Output the element's initial value.  */
3774               if (val == 0)
3775                 assemble_zeros (fieldsize);
3776               else
3777                 output_constant (val, fieldsize, align2);
3778
3779               /* Count its size.  */
3780               total_bytes += fieldsize;
3781             }
3782         }
3783       else if (field == 0 || !DECL_BIT_FIELD (field))
3784         {
3785           /* An element that is not a bit-field.  */
3786
3787           unsigned HOST_WIDE_INT fieldsize;
3788           /* Since this structure is static,
3789              we know the positions are constant.  */
3790           HOST_WIDE_INT pos = field ? int_byte_position (field) : 0;
3791           unsigned int align2;
3792
3793           if (index != 0)
3794             pos = (tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (val)), 1)
3795                    * (tree_low_cst (index, 0) - tree_low_cst (min_index, 0)));
3796
3797           /* Output any buffered-up bit-fields preceding this element.  */
3798           if (byte_buffer_in_use)
3799             {
3800               assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
3801               total_bytes++;
3802               byte_buffer_in_use = 0;
3803             }
3804
3805           /* Advance to offset of this element.
3806              Note no alignment needed in an array, since that is guaranteed
3807              if each element has the proper size.  */
3808           if ((field != 0 || index != 0) && pos != total_bytes)
3809             {
3810               assemble_zeros (pos - total_bytes);
3811               total_bytes = pos;
3812             }
3813
3814           /* Find the alignment of this element.  */
3815           align2 = min_align (align, BITS_PER_UNIT * pos);
3816
3817           /* Determine size this element should occupy.  */
3818           if (field)
3819             {
3820               fieldsize = 0;
3821
3822               /* If this is an array with an unspecified upper bound,
3823                  the initializer determines the size.  */
3824               /* ??? This ought to only checked if DECL_SIZE_UNIT is NULL,
3825                  but we cannot do this until the deprecated support for
3826                  initializing zero-length array members is removed.  */
3827               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
3828                   && TYPE_DOMAIN (TREE_TYPE (field))
3829                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
3830                 {
3831                   fieldsize = array_size_for_constructor (val);
3832                   /* Given a non-empty initialization, this field had
3833                      better be last.  */
3834                   if (fieldsize != 0 && TREE_CHAIN (field) != NULL_TREE)
3835                     abort ();
3836                 }
3837               else if (DECL_SIZE_UNIT (field))
3838                 {
3839                   /* ??? This can't be right.  If the decl size overflows
3840                      a host integer we will silently emit no data.  */
3841                   if (host_integerp (DECL_SIZE_UNIT (field), 1))
3842                     fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 1);
3843                 }
3844             }
3845           else
3846             fieldsize = int_size_in_bytes (TREE_TYPE (type));
3847
3848           /* Output the element's initial value.  */
3849           if (val == 0)
3850             assemble_zeros (fieldsize);
3851           else
3852             output_constant (val, fieldsize, align2);
3853
3854           /* Count its size.  */
3855           total_bytes += fieldsize;
3856         }
3857       else if (val != 0 && TREE_CODE (val) != INTEGER_CST)
3858         error ("invalid initial value for member `%s'",
3859                IDENTIFIER_POINTER (DECL_NAME (field)));
3860       else
3861         {
3862           /* Element that is a bit-field.  */
3863
3864           HOST_WIDE_INT next_offset = int_bit_position (field);
3865           HOST_WIDE_INT end_offset
3866             = (next_offset + tree_low_cst (DECL_SIZE (field), 1));
3867
3868           if (val == 0)
3869             val = integer_zero_node;
3870
3871           /* If this field does not start in this (or, next) byte,
3872              skip some bytes.  */
3873           if (next_offset / BITS_PER_UNIT != total_bytes)
3874             {
3875               /* Output remnant of any bit field in previous bytes.  */
3876               if (byte_buffer_in_use)
3877                 {
3878                   assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
3879                   total_bytes++;
3880                   byte_buffer_in_use = 0;
3881                 }
3882
3883               /* If still not at proper byte, advance to there.  */
3884               if (next_offset / BITS_PER_UNIT != total_bytes)
3885                 {
3886                   assemble_zeros (next_offset / BITS_PER_UNIT - total_bytes);
3887                   total_bytes = next_offset / BITS_PER_UNIT;
3888                 }
3889             }
3890
3891           if (! byte_buffer_in_use)
3892             byte = 0;
3893
3894           /* We must split the element into pieces that fall within
3895              separate bytes, and combine each byte with previous or
3896              following bit-fields.  */
3897
3898           /* next_offset is the offset n fbits from the beginning of
3899              the structure to the next bit of this element to be processed.
3900              end_offset is the offset of the first bit past the end of
3901              this element.  */
3902           while (next_offset < end_offset)
3903             {
3904               int this_time;
3905               int shift;
3906               HOST_WIDE_INT value;
3907               HOST_WIDE_INT next_byte = next_offset / BITS_PER_UNIT;
3908               HOST_WIDE_INT next_bit = next_offset % BITS_PER_UNIT;
3909
3910               /* Advance from byte to byte
3911                  within this element when necessary.  */
3912               while (next_byte != total_bytes)
3913                 {
3914                   assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
3915                   total_bytes++;
3916                   byte = 0;
3917                 }
3918
3919               /* Number of bits we can process at once
3920                  (all part of the same byte).  */
3921               this_time = MIN (end_offset - next_offset,
3922                                BITS_PER_UNIT - next_bit);
3923               if (BYTES_BIG_ENDIAN)
3924                 {
3925                   /* On big-endian machine, take the most significant bits
3926                      first (of the bits that are significant)
3927                      and put them into bytes from the most significant end.  */
3928                   shift = end_offset - next_offset - this_time;
3929
3930                   /* Don't try to take a bunch of bits that cross
3931                      the word boundary in the INTEGER_CST. We can
3932                      only select bits from the LOW or HIGH part
3933                      not from both.  */
3934                   if (shift < HOST_BITS_PER_WIDE_INT
3935                       && shift + this_time > HOST_BITS_PER_WIDE_INT)
3936                     {
3937                       this_time = shift + this_time - HOST_BITS_PER_WIDE_INT;
3938                       shift = HOST_BITS_PER_WIDE_INT;
3939                     }
3940
3941                   /* Now get the bits from the appropriate constant word.  */
3942                   if (shift < HOST_BITS_PER_WIDE_INT)
3943                     value = TREE_INT_CST_LOW (val);
3944                   else if (shift < 2 * HOST_BITS_PER_WIDE_INT)
3945                     {
3946                       value = TREE_INT_CST_HIGH (val);
3947                       shift -= HOST_BITS_PER_WIDE_INT;
3948                     }
3949                   else
3950                     abort ();
3951
3952                   /* Get the result. This works only when:
3953                      1 <= this_time <= HOST_BITS_PER_WIDE_INT.  */
3954                   byte |= (((value >> shift)
3955                             & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
3956                            << (BITS_PER_UNIT - this_time - next_bit));
3957                 }
3958               else
3959                 {
3960                   /* On little-endian machines,
3961                      take first the least significant bits of the value
3962                      and pack them starting at the least significant
3963                      bits of the bytes.  */
3964                   shift = next_offset - int_bit_position (field);
3965
3966                   /* Don't try to take a bunch of bits that cross
3967                      the word boundary in the INTEGER_CST. We can
3968                      only select bits from the LOW or HIGH part
3969                      not from both.  */
3970                   if (shift < HOST_BITS_PER_WIDE_INT
3971                       && shift + this_time > HOST_BITS_PER_WIDE_INT)
3972                     this_time = (HOST_BITS_PER_WIDE_INT - shift);
3973
3974                   /* Now get the bits from the appropriate constant word.  */
3975                   if (shift < HOST_BITS_PER_WIDE_INT)
3976                     value = TREE_INT_CST_LOW (val);
3977                   else if (shift < 2 * HOST_BITS_PER_WIDE_INT)
3978                     {
3979                       value = TREE_INT_CST_HIGH (val);
3980                       shift -= HOST_BITS_PER_WIDE_INT;
3981                     }
3982                   else
3983                     abort ();
3984
3985                   /* Get the result. This works only when:
3986                      1 <= this_time <= HOST_BITS_PER_WIDE_INT.  */
3987                   byte |= (((value >> shift)
3988                             & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
3989                            << next_bit);
3990                 }
3991
3992               next_offset += this_time;
3993               byte_buffer_in_use = 1;
3994             }
3995         }
3996     }
3997
3998   if (byte_buffer_in_use)
3999     {
4000       assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
4001       total_bytes++;
4002     }
4003
4004   if ((unsigned HOST_WIDE_INT)total_bytes < size)
4005     assemble_zeros (size - total_bytes);
4006 }
4007
4008 /* This TREE_LIST contains any weak symbol declarations waiting
4009    to be emitted.  */
4010 static GTY(()) tree weak_decls;
4011
4012 /* Mark DECL as weak.  */
4013
4014 static void
4015 mark_weak (tree decl)
4016 {
4017   DECL_WEAK (decl) = 1;
4018
4019   if (DECL_RTL_SET_P (decl)
4020       && GET_CODE (DECL_RTL (decl)) == MEM
4021       && XEXP (DECL_RTL (decl), 0)
4022       && GET_CODE (XEXP (DECL_RTL (decl), 0)) == SYMBOL_REF)
4023     SYMBOL_REF_WEAK (XEXP (DECL_RTL (decl), 0)) = 1;
4024 }
4025
4026 /* Merge weak status between NEWDECL and OLDDECL.  */
4027
4028 void
4029 merge_weak (tree newdecl, tree olddecl)
4030 {
4031   if (DECL_WEAK (newdecl) == DECL_WEAK (olddecl))
4032     return;
4033
4034   if (DECL_WEAK (newdecl))
4035     {
4036       tree wd;
4037
4038       /* NEWDECL is weak, but OLDDECL is not.  */
4039
4040       /* If we already output the OLDDECL, we're in trouble; we can't
4041          go back and make it weak.  This error cannot caught in
4042          declare_weak because the NEWDECL and OLDDECL was not yet
4043          been merged; therefore, TREE_ASM_WRITTEN was not set.  */
4044       if (TREE_ASM_WRITTEN (olddecl))
4045         error ("%Jweak declaration of '%D' must precede definition",
4046                newdecl, newdecl);
4047
4048       /* If we've already generated rtl referencing OLDDECL, we may
4049          have done so in a way that will not function properly with
4050          a weak symbol.  */
4051       else if (TREE_USED (olddecl)
4052                && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (olddecl)))
4053         warning ("%Jweak declaration of '%D' after first use results "
4054                  "in unspecified behavior", newdecl, newdecl);
4055
4056       if (SUPPORTS_WEAK)
4057         {
4058           /* We put the NEWDECL on the weak_decls list at some point.
4059              Replace it with the OLDDECL.  */
4060           for (wd = weak_decls; wd; wd = TREE_CHAIN (wd))
4061             if (TREE_VALUE (wd) == newdecl)
4062               {
4063                 TREE_VALUE (wd) = olddecl;
4064                 break;
4065               }
4066           /* We may not find the entry on the list.  If NEWDECL is a
4067              weak alias, then we will have already called
4068              globalize_decl to remove the entry; in that case, we do
4069              not need to do anything.  */
4070         }
4071
4072       /* Make the OLDDECL weak; it's OLDDECL that we'll be keeping.  */
4073       mark_weak (olddecl);
4074     }
4075   else
4076     /* OLDDECL was weak, but NEWDECL was not explicitly marked as
4077        weak.  Just update NEWDECL to indicate that it's weak too.  */
4078     mark_weak (newdecl);
4079 }
4080
4081 /* Declare DECL to be a weak symbol.  */
4082
4083 void
4084 declare_weak (tree decl)
4085 {
4086   if (! TREE_PUBLIC (decl))
4087     error ("%Jweak declaration of '%D' must be public", decl, decl);
4088   else if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
4089     error ("%Jweak declaration of '%D' must precede definition", decl, decl);
4090   else if (SUPPORTS_WEAK)
4091     {
4092       if (! DECL_WEAK (decl))
4093         weak_decls = tree_cons (NULL, decl, weak_decls);
4094     }
4095   else
4096     warning ("%Jweak declaration of '%D' not supported", decl, decl);
4097
4098   mark_weak (decl);
4099 }
4100
4101 /* Emit any pending weak declarations.  */
4102
4103 void
4104 weak_finish (void)
4105 {
4106   tree t;
4107
4108   for (t = weak_decls; t; t = TREE_CHAIN (t))
4109     {
4110       tree decl = TREE_VALUE (t);
4111 #if defined (ASM_WEAKEN_DECL) || defined (ASM_WEAKEN_LABEL)
4112       const char *const name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
4113 #endif
4114
4115       if (! TREE_USED (decl))
4116         continue;
4117
4118 #ifdef ASM_WEAKEN_DECL
4119       ASM_WEAKEN_DECL (asm_out_file, decl, name, NULL);
4120 #else
4121 #ifdef ASM_WEAKEN_LABEL
4122       ASM_WEAKEN_LABEL (asm_out_file, name);
4123 #else
4124 #ifdef ASM_OUTPUT_WEAK_ALIAS
4125       warning ("only weak aliases are supported in this configuration");
4126       return;
4127 #endif
4128 #endif
4129 #endif
4130     }
4131 }
4132
4133 /* Emit the assembly bits to indicate that DECL is globally visible.  */
4134
4135 static void
4136 globalize_decl (tree decl)
4137 {
4138   const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
4139
4140 #if defined (ASM_WEAKEN_LABEL) || defined (ASM_WEAKEN_DECL)
4141   if (DECL_WEAK (decl))
4142     {
4143       tree *p, t;
4144
4145 #ifdef ASM_WEAKEN_DECL
4146       ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
4147 #else
4148       ASM_WEAKEN_LABEL (asm_out_file, name);
4149 #endif
4150
4151       /* Remove this function from the pending weak list so that
4152          we do not emit multiple .weak directives for it.  */
4153       for (p = &weak_decls; (t = *p) ; )
4154         {
4155           if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
4156             *p = TREE_CHAIN (t);
4157           else
4158             p = &TREE_CHAIN (t);
4159         }
4160       return;
4161     }
4162 #elif defined(ASM_MAKE_LABEL_LINKONCE)
4163   if (DECL_ONE_ONLY (decl))
4164     ASM_MAKE_LABEL_LINKONCE (asm_out_file, name);
4165 #endif
4166
4167   targetm.asm_out.globalize_label (asm_out_file, name);
4168 }
4169
4170 /* Emit an assembler directive to make the symbol for DECL an alias to
4171    the symbol for TARGET.  */
4172
4173 void
4174 assemble_alias (tree decl, tree target ATTRIBUTE_UNUSED)
4175 {
4176   const char *name;
4177
4178   /* We must force creation of DECL_RTL for debug info generation, even though
4179      we don't use it here.  */
4180   make_decl_rtl (decl, NULL);
4181
4182   name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
4183
4184 #ifdef ASM_OUTPUT_DEF
4185   /* Make name accessible from other files, if appropriate.  */
4186
4187   if (TREE_PUBLIC (decl))
4188     {
4189       globalize_decl (decl);
4190       maybe_assemble_visibility (decl);
4191     }
4192
4193 #ifdef ASM_OUTPUT_DEF_FROM_DECLS
4194   ASM_OUTPUT_DEF_FROM_DECLS (asm_out_file, decl, target);
4195 #else
4196   ASM_OUTPUT_DEF (asm_out_file, name, IDENTIFIER_POINTER (target));
4197 #endif
4198 #else /* !ASM_OUTPUT_DEF */
4199 #if defined (ASM_OUTPUT_WEAK_ALIAS) || defined (ASM_WEAKEN_DECL)
4200   if (DECL_WEAK (decl))
4201     {
4202       tree *p, t;
4203 #ifdef ASM_WEAKEN_DECL
4204       ASM_WEAKEN_DECL (asm_out_file, decl, name, IDENTIFIER_POINTER (target));
4205 #else
4206       ASM_OUTPUT_WEAK_ALIAS (asm_out_file, name, IDENTIFIER_POINTER (target));
4207 #endif
4208       /* Remove this function from the pending weak list so that
4209          we do not emit multiple .weak directives for it.  */
4210       for (p = &weak_decls; (t = *p) ; )
4211         if (DECL_ASSEMBLER_NAME (decl)
4212             == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
4213           *p = TREE_CHAIN (t);
4214         else
4215           p = &TREE_CHAIN (t);
4216     }
4217   else
4218     warning ("only weak aliases are supported in this configuration");
4219
4220 #else
4221   warning ("alias definitions not supported in this configuration; ignored");
4222 #endif
4223 #endif
4224
4225   TREE_USED (decl) = 1;
4226   TREE_ASM_WRITTEN (decl) = 1;
4227   TREE_ASM_WRITTEN (DECL_ASSEMBLER_NAME (decl)) = 1;
4228 }
4229
4230 /* Emit an assembler directive to set symbol for DECL visibility to
4231    the visibility type VIS, which must not be VISIBILITY_DEFAULT.  */
4232
4233 void
4234 default_assemble_visibility (tree decl, int vis)
4235 {
4236   static const char * const visibility_types[] = {
4237     NULL, "internal", "hidden", "protected"
4238   };
4239
4240   const char *name, *type;
4241
4242   name = (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
4243   type = visibility_types[vis];
4244
4245 #ifdef HAVE_GAS_HIDDEN
4246   fprintf (asm_out_file, "\t.%s\t", type);
4247   assemble_name (asm_out_file, name);
4248   fprintf (asm_out_file, "\n");
4249 #else
4250   warning ("visibility attribute not supported in this configuration; ignored");
4251 #endif
4252 }
4253
4254 /* A helper function to call assemble_visibility when needed for a decl.  */
4255
4256 static void
4257 maybe_assemble_visibility (tree decl)
4258 {
4259   enum symbol_visibility vis = DECL_VISIBILITY (decl);
4260
4261   if (vis != VISIBILITY_DEFAULT)
4262     targetm.asm_out.visibility (decl, vis);
4263 }
4264
4265 /* Returns 1 if the target configuration supports defining public symbols
4266    so that one of them will be chosen at link time instead of generating a
4267    multiply-defined symbol error, whether through the use of weak symbols or
4268    a target-specific mechanism for having duplicates discarded.  */
4269
4270 int
4271 supports_one_only (void)
4272 {
4273   if (SUPPORTS_ONE_ONLY)
4274     return 1;
4275   return SUPPORTS_WEAK;
4276 }
4277
4278 /* Set up DECL as a public symbol that can be defined in multiple
4279    translation units without generating a linker error.  */
4280
4281 void
4282 make_decl_one_only (tree decl)
4283 {
4284   if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != FUNCTION_DECL)
4285     abort ();
4286
4287   TREE_PUBLIC (decl) = 1;
4288
4289   if (SUPPORTS_ONE_ONLY)
4290     {
4291 #ifdef MAKE_DECL_ONE_ONLY
4292       MAKE_DECL_ONE_ONLY (decl);
4293 #endif
4294       DECL_ONE_ONLY (decl) = 1;
4295     }
4296   else if (TREE_CODE (decl) == VAR_DECL
4297       && (DECL_INITIAL (decl) == 0 || DECL_INITIAL (decl) == error_mark_node))
4298     DECL_COMMON (decl) = 1;
4299   else if (SUPPORTS_WEAK)
4300     DECL_WEAK (decl) = 1;
4301   else
4302     abort ();
4303 }
4304
4305 void
4306 init_varasm_once (void)
4307 {
4308   in_named_htab = htab_create_ggc (31, in_named_entry_hash,
4309                                    in_named_entry_eq, NULL);
4310   const_desc_htab = htab_create_ggc (1009, const_desc_hash,
4311                                      const_desc_eq, NULL);
4312
4313   const_alias_set = new_alias_set ();
4314 }
4315
4316 enum tls_model
4317 decl_tls_model (tree decl)
4318 {
4319   enum tls_model kind;
4320   tree attr = lookup_attribute ("tls_model", DECL_ATTRIBUTES (decl));
4321   bool is_local;
4322
4323   if (attr)
4324     {
4325       attr = TREE_VALUE (TREE_VALUE (attr));
4326       if (TREE_CODE (attr) != STRING_CST)
4327         abort ();
4328       if (!strcmp (TREE_STRING_POINTER (attr), "local-exec"))
4329         kind = TLS_MODEL_LOCAL_EXEC;
4330       else if (!strcmp (TREE_STRING_POINTER (attr), "initial-exec"))
4331         kind = TLS_MODEL_INITIAL_EXEC;
4332       else if (!strcmp (TREE_STRING_POINTER (attr), "local-dynamic"))
4333         kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
4334       else if (!strcmp (TREE_STRING_POINTER (attr), "global-dynamic"))
4335         kind = TLS_MODEL_GLOBAL_DYNAMIC;
4336       else
4337         abort ();
4338       return kind;
4339     }
4340
4341   is_local = targetm.binds_local_p (decl);
4342   if (!flag_pic)
4343     {
4344       if (is_local)
4345         kind = TLS_MODEL_LOCAL_EXEC;
4346       else
4347         kind = TLS_MODEL_INITIAL_EXEC;
4348     }
4349   /* Local dynamic is inefficient when we're not combining the
4350      parts of the address.  */
4351   else if (optimize && is_local)
4352     kind = TLS_MODEL_LOCAL_DYNAMIC;
4353   else
4354     kind = TLS_MODEL_GLOBAL_DYNAMIC;
4355   if (kind < flag_tls_default)
4356     kind = flag_tls_default;
4357
4358   return kind;
4359 }
4360
4361 /* Select a set of attributes for section NAME based on the properties
4362    of DECL and whether or not RELOC indicates that DECL's initializer
4363    might contain runtime relocations.
4364
4365    We make the section read-only and executable for a function decl,
4366    read-only for a const data decl, and writable for a non-const data decl.  */
4367
4368 unsigned int
4369 default_section_type_flags (tree decl, const char *name, int reloc)
4370 {
4371   return default_section_type_flags_1 (decl, name, reloc, flag_pic);
4372 }
4373
4374 unsigned int
4375 default_section_type_flags_1 (tree decl, const char *name, int reloc,
4376                               int shlib)
4377 {
4378   unsigned int flags;
4379
4380   if (decl && TREE_CODE (decl) == FUNCTION_DECL)
4381     flags = SECTION_CODE;
4382   else if (decl && decl_readonly_section_1 (decl, reloc, shlib))
4383     flags = 0;
4384   else if (strcmp (name, UNLIKELY_EXECUTED_TEXT_SECTION_NAME) == 0)
4385     flags = SECTION_CODE;
4386   else
4387     flags = SECTION_WRITE;
4388
4389   if (decl && DECL_ONE_ONLY (decl))
4390     flags |= SECTION_LINKONCE;
4391
4392   if (decl && TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL (decl))
4393     flags |= SECTION_TLS | SECTION_WRITE;
4394
4395   if (strcmp (name, ".bss") == 0
4396       || strncmp (name, ".bss.", 5) == 0
4397       || strncmp (name, ".gnu.linkonce.b.", 16) == 0
4398       || strcmp (name, ".sbss") == 0
4399       || strncmp (name, ".sbss.", 6) == 0
4400       || strncmp (name, ".gnu.linkonce.sb.", 17) == 0
4401       || strcmp (name, ".tbss") == 0
4402       || strncmp (name, ".gnu.linkonce.tb.", 17) == 0)
4403     flags |= SECTION_BSS;
4404
4405   if (strcmp (name, ".tdata") == 0
4406       || strcmp (name, ".tbss") == 0
4407       || strncmp (name, ".gnu.linkonce.td.", 17) == 0
4408       || strncmp (name, ".gnu.linkonce.tb.", 17) == 0)
4409     flags |= SECTION_TLS;
4410
4411   /* These three sections have special ELF types.  They are neither
4412      SHT_PROGBITS nor SHT_NOBITS, so when changing sections we don't
4413      want to print a section type (@progbits or @nobits).  If someone
4414      is silly enough to emit code or TLS variables to one of these
4415      sections, then don't handle them specially.  */
4416   if (!(flags & (SECTION_CODE | SECTION_BSS | SECTION_TLS))
4417       && (strcmp (name, ".init_array") == 0
4418           || strcmp (name, ".fini_array") == 0
4419           || strcmp (name, ".preinit_array") == 0))
4420     flags |= SECTION_NOTYPE;
4421
4422   return flags;
4423 }
4424
4425 /* Output assembly to switch to section NAME with attribute FLAGS.
4426    Four variants for common object file formats.  */
4427
4428 void
4429 default_no_named_section (const char *name ATTRIBUTE_UNUSED,
4430                           unsigned int flags ATTRIBUTE_UNUSED)
4431 {
4432   /* Some object formats don't support named sections at all.  The
4433      front-end should already have flagged this as an error.  */
4434   abort ();
4435 }
4436
4437 void
4438 default_elf_asm_named_section (const char *name, unsigned int flags)
4439 {
4440   char flagchars[10], *f = flagchars;
4441
4442   if (! named_section_first_declaration (name))
4443     {
4444       fprintf (asm_out_file, "\t.section\t%s\n", name);
4445       return;
4446     }
4447
4448   if (!(flags & SECTION_DEBUG))
4449     *f++ = 'a';
4450   if (flags & SECTION_WRITE)
4451     *f++ = 'w';
4452   if (flags & SECTION_CODE)
4453     *f++ = 'x';
4454   if (flags & SECTION_SMALL)
4455     *f++ = 's';
4456   if (flags & SECTION_MERGE)
4457     *f++ = 'M';
4458   if (flags & SECTION_STRINGS)
4459     *f++ = 'S';
4460   if (flags & SECTION_TLS)
4461     *f++ = 'T';
4462   *f = '\0';
4463
4464   fprintf (asm_out_file, "\t.section\t%s,\"%s\"", name, flagchars);
4465
4466   if (!(flags & SECTION_NOTYPE))
4467     {
4468       const char *type;
4469
4470       if (flags & SECTION_BSS)
4471         type = "nobits";
4472       else
4473         type = "progbits";
4474
4475       fprintf (asm_out_file, ",@%s", type);
4476
4477       if (flags & SECTION_ENTSIZE)
4478         fprintf (asm_out_file, ",%d", flags & SECTION_ENTSIZE);
4479     }
4480
4481   putc ('\n', asm_out_file);
4482 }
4483
4484 void
4485 default_coff_asm_named_section (const char *name, unsigned int flags)
4486 {
4487   char flagchars[8], *f = flagchars;
4488
4489   if (flags & SECTION_WRITE)
4490     *f++ = 'w';
4491   if (flags & SECTION_CODE)
4492     *f++ = 'x';
4493   *f = '\0';
4494
4495   fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
4496 }
4497
4498 void
4499 default_pe_asm_named_section (const char *name, unsigned int flags)
4500 {
4501   default_coff_asm_named_section (name, flags);
4502
4503   if (flags & SECTION_LINKONCE)
4504     {
4505       /* Functions may have been compiled at various levels of
4506          optimization so we can't use `same_size' here.
4507          Instead, have the linker pick one.  */
4508       fprintf (asm_out_file, "\t.linkonce %s\n",
4509                (flags & SECTION_CODE ? "discard" : "same_size"));
4510     }
4511 }
4512 \f
4513 /* The lame default section selector.  */
4514
4515 void
4516 default_select_section (tree decl, int reloc,
4517                         unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
4518 {
4519   bool readonly = false;
4520
4521   if (DECL_P (decl))
4522     {
4523       if (decl_readonly_section (decl, reloc))
4524         readonly = true;
4525     }
4526   else if (TREE_CODE (decl) == CONSTRUCTOR)
4527     {
4528       if (! ((flag_pic && reloc)
4529              || !TREE_READONLY (decl)
4530              || TREE_SIDE_EFFECTS (decl)
4531              || !TREE_CONSTANT (decl)))
4532         readonly = true;
4533     }
4534   else if (TREE_CODE (decl) == STRING_CST)
4535     readonly = true;
4536   else if (! (flag_pic && reloc))
4537     readonly = true;
4538
4539   if (readonly)
4540     readonly_data_section ();
4541   else
4542     data_section ();
4543 }
4544
4545 /* A helper function for default_elf_select_section and
4546    default_elf_unique_section.  Categorizes the DECL.  */
4547
4548 enum section_category
4549 {
4550   SECCAT_TEXT,
4551
4552   SECCAT_RODATA,
4553   SECCAT_RODATA_MERGE_STR,
4554   SECCAT_RODATA_MERGE_STR_INIT,
4555   SECCAT_RODATA_MERGE_CONST,
4556   SECCAT_SRODATA,
4557
4558   SECCAT_DATA,
4559
4560   /* To optimize loading of shared programs, define following subsections
4561      of data section:
4562         _REL    Contains data that has relocations, so they get grouped
4563                 together and dynamic linker will visit fewer pages in memory.
4564         _RO     Contains data that is otherwise read-only.  This is useful
4565                 with prelinking as most relocations won't be dynamically
4566                 linked and thus stay read only.
4567         _LOCAL  Marks data containing relocations only to local objects.
4568                 These relocations will get fully resolved by prelinking.  */
4569   SECCAT_DATA_REL,
4570   SECCAT_DATA_REL_LOCAL,
4571   SECCAT_DATA_REL_RO,
4572   SECCAT_DATA_REL_RO_LOCAL,
4573
4574   SECCAT_SDATA,
4575   SECCAT_TDATA,
4576
4577   SECCAT_BSS,
4578   SECCAT_SBSS,
4579   SECCAT_TBSS
4580 };
4581
4582 static enum section_category
4583 categorize_decl_for_section (tree, int, int);
4584
4585 static enum section_category
4586 categorize_decl_for_section (tree decl, int reloc, int shlib)
4587 {
4588   enum section_category ret;
4589
4590   if (TREE_CODE (decl) == FUNCTION_DECL)
4591     return SECCAT_TEXT;
4592   else if (TREE_CODE (decl) == STRING_CST)
4593     return SECCAT_RODATA_MERGE_STR;
4594   else if (TREE_CODE (decl) == VAR_DECL)
4595     {
4596       if (DECL_INITIAL (decl) == NULL
4597           || DECL_INITIAL (decl) == error_mark_node)
4598         ret = SECCAT_BSS;
4599       else if (! TREE_READONLY (decl)
4600                || TREE_SIDE_EFFECTS (decl)
4601                || ! TREE_CONSTANT (DECL_INITIAL (decl)))
4602         {
4603           if (shlib && (reloc & 2))
4604             ret = SECCAT_DATA_REL;
4605           else if (shlib && reloc)
4606             ret = SECCAT_DATA_REL_LOCAL;
4607           else
4608             ret = SECCAT_DATA;
4609         }
4610       else if (shlib && (reloc & 2))
4611         ret = SECCAT_DATA_REL_RO;
4612       else if (shlib && reloc)
4613         ret = SECCAT_DATA_REL_RO_LOCAL;
4614       else if (reloc || flag_merge_constants < 2)
4615         /* C and C++ don't allow different variables to share the same
4616            location.  -fmerge-all-constants allows even that (at the
4617            expense of not conforming).  */
4618         ret = SECCAT_RODATA;
4619       else if (TREE_CODE (DECL_INITIAL (decl)) == STRING_CST)
4620         ret = SECCAT_RODATA_MERGE_STR_INIT;
4621       else
4622         ret = SECCAT_RODATA_MERGE_CONST;
4623     }
4624   else if (TREE_CODE (decl) == CONSTRUCTOR)
4625     {
4626       if ((shlib && reloc)
4627           || TREE_SIDE_EFFECTS (decl)
4628           || ! TREE_CONSTANT (decl))
4629         ret = SECCAT_DATA;
4630       else
4631         ret = SECCAT_RODATA;
4632     }
4633   else
4634     ret = SECCAT_RODATA;
4635
4636   /* There are no read-only thread-local sections.  */
4637   if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL (decl))
4638     {
4639       if (ret == SECCAT_BSS)
4640         ret = SECCAT_TBSS;
4641       else
4642         ret = SECCAT_TDATA;
4643     }
4644
4645   /* If the target uses small data sections, select it.  */
4646   else if (targetm.in_small_data_p (decl))
4647     {
4648       if (ret == SECCAT_BSS)
4649         ret = SECCAT_SBSS;
4650       else if (targetm.have_srodata_section && ret == SECCAT_RODATA)
4651         ret = SECCAT_SRODATA;
4652       else
4653         ret = SECCAT_SDATA;
4654     }
4655
4656   return ret;
4657 }
4658
4659 bool
4660 decl_readonly_section (tree decl, int reloc)
4661 {
4662   return decl_readonly_section_1 (decl, reloc, flag_pic);
4663 }
4664
4665 bool
4666 decl_readonly_section_1 (tree decl, int reloc, int shlib)
4667 {
4668   switch (categorize_decl_for_section (decl, reloc, shlib))
4669     {
4670     case SECCAT_RODATA:
4671     case SECCAT_RODATA_MERGE_STR:
4672     case SECCAT_RODATA_MERGE_STR_INIT:
4673     case SECCAT_RODATA_MERGE_CONST:
4674     case SECCAT_SRODATA:
4675       return true;
4676       break;
4677     default:
4678       return false;
4679       break;
4680     }
4681 }
4682
4683 /* Select a section based on the above categorization.  */
4684
4685 void
4686 default_elf_select_section (tree decl, int reloc,
4687                             unsigned HOST_WIDE_INT align)
4688 {
4689   default_elf_select_section_1 (decl, reloc, align, flag_pic);
4690 }
4691
4692 void
4693 default_elf_select_section_1 (tree decl, int reloc,
4694                               unsigned HOST_WIDE_INT align, int shlib)
4695 {
4696   switch (categorize_decl_for_section (decl, reloc, shlib))
4697     {
4698     case SECCAT_TEXT:
4699       /* We're not supposed to be called on FUNCTION_DECLs.  */
4700       abort ();
4701     case SECCAT_RODATA:
4702       readonly_data_section ();
4703       break;
4704     case SECCAT_RODATA_MERGE_STR:
4705       mergeable_string_section (decl, align, 0);
4706       break;
4707     case SECCAT_RODATA_MERGE_STR_INIT:
4708       mergeable_string_section (DECL_INITIAL (decl), align, 0);
4709       break;
4710     case SECCAT_RODATA_MERGE_CONST:
4711       mergeable_constant_section (DECL_MODE (decl), align, 0);
4712       break;
4713     case SECCAT_SRODATA:
4714       named_section (NULL_TREE, ".sdata2", reloc);
4715       break;
4716     case SECCAT_DATA:
4717       data_section ();
4718       break;
4719     case SECCAT_DATA_REL:
4720       named_section (NULL_TREE, ".data.rel", reloc);
4721       break;
4722     case SECCAT_DATA_REL_LOCAL:
4723       named_section (NULL_TREE, ".data.rel.local", reloc);
4724       break;
4725     case SECCAT_DATA_REL_RO:
4726       named_section (NULL_TREE, ".data.rel.ro", reloc);
4727       break;
4728     case SECCAT_DATA_REL_RO_LOCAL:
4729       named_section (NULL_TREE, ".data.rel.ro.local", reloc);
4730       break;
4731     case SECCAT_SDATA:
4732       named_section (NULL_TREE, ".sdata", reloc);
4733       break;
4734     case SECCAT_TDATA:
4735       named_section (NULL_TREE, ".tdata", reloc);
4736       break;
4737     case SECCAT_BSS:
4738 #ifdef BSS_SECTION_ASM_OP
4739       bss_section ();
4740 #else
4741       named_section (NULL_TREE, ".bss", reloc);
4742 #endif
4743       break;
4744     case SECCAT_SBSS:
4745       named_section (NULL_TREE, ".sbss", reloc);
4746       break;
4747     case SECCAT_TBSS:
4748       named_section (NULL_TREE, ".tbss", reloc);
4749       break;
4750     default:
4751       abort ();
4752     }
4753 }
4754
4755 /* Construct a unique section name based on the decl name and the
4756    categorization performed above.  */
4757
4758 void
4759 default_unique_section (tree decl, int reloc)
4760 {
4761   default_unique_section_1 (decl, reloc, flag_pic);
4762 }
4763
4764 void
4765 default_unique_section_1 (tree decl, int reloc, int shlib)
4766 {
4767   bool one_only = DECL_ONE_ONLY (decl);
4768   const char *prefix, *name;
4769   size_t nlen, plen;
4770   char *string;
4771
4772   switch (categorize_decl_for_section (decl, reloc, shlib))
4773     {
4774     case SECCAT_TEXT:
4775       prefix = one_only ? ".gnu.linkonce.t." : ".text.";
4776       break;
4777     case SECCAT_RODATA:
4778     case SECCAT_RODATA_MERGE_STR:
4779     case SECCAT_RODATA_MERGE_STR_INIT:
4780     case SECCAT_RODATA_MERGE_CONST:
4781       prefix = one_only ? ".gnu.linkonce.r." : ".rodata.";
4782       break;
4783     case SECCAT_SRODATA:
4784       prefix = one_only ? ".gnu.linkonce.s2." : ".sdata2.";
4785       break;
4786     case SECCAT_DATA:
4787     case SECCAT_DATA_REL:
4788     case SECCAT_DATA_REL_LOCAL:
4789     case SECCAT_DATA_REL_RO:
4790     case SECCAT_DATA_REL_RO_LOCAL:
4791       prefix = one_only ? ".gnu.linkonce.d." : ".data.";
4792       break;
4793     case SECCAT_SDATA:
4794       prefix = one_only ? ".gnu.linkonce.s." : ".sdata.";
4795       break;
4796     case SECCAT_BSS:
4797       prefix = one_only ? ".gnu.linkonce.b." : ".bss.";
4798       break;
4799     case SECCAT_SBSS:
4800       prefix = one_only ? ".gnu.linkonce.sb." : ".sbss.";
4801       break;
4802     case SECCAT_TDATA:
4803       prefix = one_only ? ".gnu.linkonce.td." : ".tdata.";
4804       break;
4805     case SECCAT_TBSS:
4806       prefix = one_only ? ".gnu.linkonce.tb." : ".tbss.";
4807       break;
4808     default:
4809       abort ();
4810     }
4811   plen = strlen (prefix);
4812
4813   name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
4814   name = targetm.strip_name_encoding (name);
4815   nlen = strlen (name);
4816
4817   string = alloca (nlen + plen + 1);
4818   memcpy (string, prefix, plen);
4819   memcpy (string + plen, name, nlen + 1);
4820
4821   DECL_SECTION_NAME (decl) = build_string (nlen + plen, string);
4822 }
4823
4824 void
4825 default_select_rtx_section (enum machine_mode mode ATTRIBUTE_UNUSED,
4826                             rtx x,
4827                             unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
4828 {
4829   if (flag_pic)
4830     switch (GET_CODE (x))
4831       {
4832       case CONST:
4833       case SYMBOL_REF:
4834       case LABEL_REF:
4835         data_section ();
4836         return;
4837
4838       default:
4839         break;
4840       }
4841
4842   readonly_data_section ();
4843 }
4844
4845 void
4846 default_elf_select_rtx_section (enum machine_mode mode, rtx x,
4847                                 unsigned HOST_WIDE_INT align)
4848 {
4849   /* ??? Handle small data here somehow.  */
4850
4851   if (flag_pic)
4852     switch (GET_CODE (x))
4853       {
4854       case CONST:
4855       case SYMBOL_REF:
4856         named_section (NULL_TREE, ".data.rel.ro", 3);
4857         return;
4858
4859       case LABEL_REF:
4860         named_section (NULL_TREE, ".data.rel.ro.local", 1);
4861         return;
4862
4863       default:
4864         break;
4865       }
4866
4867   mergeable_constant_section (mode, align, 0);
4868 }
4869
4870 /* Set the generally applicable flags on the SYMBOL_REF for EXP.  */
4871
4872 void
4873 default_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
4874 {
4875   rtx symbol;
4876   int flags;
4877
4878   /* Careful not to prod global register variables.  */
4879   if (GET_CODE (rtl) != MEM)
4880     return;
4881   symbol = XEXP (rtl, 0);
4882   if (GET_CODE (symbol) != SYMBOL_REF)
4883     return;
4884
4885   flags = 0;
4886   if (TREE_CODE (decl) == FUNCTION_DECL)
4887     flags |= SYMBOL_FLAG_FUNCTION;
4888   if (targetm.binds_local_p (decl))
4889     flags |= SYMBOL_FLAG_LOCAL;
4890   if (targetm.in_small_data_p (decl))
4891     flags |= SYMBOL_FLAG_SMALL;
4892   if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL (decl))
4893     flags |= decl_tls_model (decl) << SYMBOL_FLAG_TLS_SHIFT;
4894   /* ??? Why is DECL_EXTERNAL ever set for non-PUBLIC names?  Without
4895      being PUBLIC, the thing *must* be defined in this translation unit.
4896      Prevent this buglet from being propagated into rtl code as well.  */
4897   if (DECL_P (decl) && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl))
4898     flags |= SYMBOL_FLAG_EXTERNAL;
4899
4900   SYMBOL_REF_FLAGS (symbol) = flags;
4901 }
4902
4903 /* By default, we do nothing for encode_section_info, so we need not
4904    do anything but discard the '*' marker.  */
4905
4906 const char *
4907 default_strip_name_encoding (const char *str)
4908 {
4909   return str + (*str == '*');
4910 }
4911
4912 /* Assume ELF-ish defaults, since that's pretty much the most liberal
4913    wrt cross-module name binding.  */
4914
4915 bool
4916 default_binds_local_p (tree exp)
4917 {
4918   return default_binds_local_p_1 (exp, flag_shlib);
4919 }
4920
4921 bool
4922 default_binds_local_p_1 (tree exp, int shlib)
4923 {
4924   bool local_p;
4925
4926   /* A non-decl is an entry in the constant pool.  */
4927   if (!DECL_P (exp))
4928     local_p = true;
4929   /* Static variables are always local.  */
4930   else if (! TREE_PUBLIC (exp))
4931     local_p = true;
4932   /* A variable is local if the user tells us so.  */
4933   else if (DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
4934     local_p = true;
4935   /* Otherwise, variables defined outside this object may not be local.  */
4936   else if (DECL_EXTERNAL (exp))
4937     local_p = false;
4938   /* Linkonce and weak data are never local.  */
4939   else if (DECL_ONE_ONLY (exp) || DECL_WEAK (exp))
4940     local_p = false;
4941   /* If PIC, then assume that any global name can be overridden by
4942      symbols resolved from other modules.  */
4943   else if (shlib)
4944     local_p = false;
4945   /* Uninitialized COMMON variable may be unified with symbols
4946      resolved from other modules.  */
4947   else if (DECL_COMMON (exp)
4948            && (DECL_INITIAL (exp) == NULL
4949                || DECL_INITIAL (exp) == error_mark_node))
4950     local_p = false;
4951   /* Otherwise we're left with initialized (or non-common) global data
4952      which is of necessity defined locally.  */
4953   else
4954     local_p = true;
4955
4956   return local_p;
4957 }
4958
4959 /* Determine whether or not a pointer mode is valid. Assume defaults
4960    of ptr_mode or Pmode - can be overridden.  */
4961 bool
4962 default_valid_pointer_mode (enum machine_mode mode)
4963 {
4964   return (mode == ptr_mode || mode == Pmode);
4965 }
4966
4967 /* Default function to output code that will globalize a label.  A
4968    target must define GLOBAL_ASM_OP or provide it's own function to
4969    globalize a label.  */
4970 #ifdef GLOBAL_ASM_OP
4971 void
4972 default_globalize_label (FILE * stream, const char *name)
4973 {
4974   fputs (GLOBAL_ASM_OP, stream);
4975   assemble_name (stream, name);
4976   putc ('\n', stream);
4977 }
4978 #endif /* GLOBAL_ASM_OP */
4979
4980 /* Default function to output a label for unwind information.  The
4981    default is to do nothing.  A target that needs nonlocal labels for
4982    unwind information must provide its own function to do this. */
4983 void
4984 default_emit_unwind_label (FILE * stream ATTRIBUTE_UNUSED,
4985                            tree decl ATTRIBUTE_UNUSED,
4986                            int empty ATTRIBUTE_UNUSED)
4987
4988 }
4989
4990 /* This is how to output an internal numbered label where PREFIX is
4991    the class of label and LABELNO is the number within the class.  */
4992
4993 void
4994 default_internal_label (FILE *stream, const char *prefix,
4995                         unsigned long labelno)
4996 {
4997   char *const buf = alloca (40 + strlen (prefix));
4998   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
4999   ASM_OUTPUT_LABEL (stream, buf);
5000 }
5001
5002 /* This is the default behavior at the beginning of a file.  It's
5003    controlled by two other target-hook toggles.  */
5004 void
5005 default_file_start (void)
5006 {
5007   if (targetm.file_start_app_off && !flag_verbose_asm)
5008     fputs (ASM_APP_OFF, asm_out_file);
5009
5010   if (targetm.file_start_file_directive)
5011     output_file_directive (asm_out_file, main_input_filename);
5012 }
5013
5014 /* This is a generic routine suitable for use as TARGET_ASM_FILE_END
5015    which emits a special section directive used to indicate whether or
5016    not this object file needs an executable stack.  This is primarily
5017    a GNU extension to ELF but could be used on other targets.  */
5018 void
5019 file_end_indicate_exec_stack (void)
5020 {
5021   unsigned int flags = SECTION_DEBUG;
5022   if (trampolines_created)
5023     flags |= SECTION_CODE;
5024
5025   named_section_flags (".note.GNU-stack", flags);
5026 }
5027
5028 #include "gt-varasm.h"