OSDN Git Service

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