OSDN Git Service

* varasm.c (initialize_constant_valid_p): Allow pointers
[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 (TREE_CODE (src) == INTEGER_CST
3557                 && TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type))
3558               return null_pointer_node;
3559             if (integer_zerop (src))
3560               return null_pointer_node;
3561             else if (TYPE_PRECISION (dest_type) <= TYPE_PRECISION (src_type))
3562               return initializer_constant_valid_p (src, endtype);
3563           }
3564
3565         /* Allow conversions to struct or union types if the value
3566            inside is okay.  */
3567         if (TREE_CODE (dest_type) == RECORD_TYPE
3568             || TREE_CODE (dest_type) == UNION_TYPE)
3569           return initializer_constant_valid_p (src, endtype);
3570       }
3571       break;
3572
3573     case PLUS_EXPR:
3574       if (! INTEGRAL_TYPE_P (endtype)
3575           || TYPE_PRECISION (endtype) >= POINTER_SIZE)
3576         {
3577           tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
3578                                                       endtype);
3579           tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
3580                                                       endtype);
3581           /* If either term is absolute, use the other terms relocation.  */
3582           if (valid0 == null_pointer_node)
3583             return valid1;
3584           if (valid1 == null_pointer_node)
3585             return valid0;
3586         }
3587       break;
3588
3589     case MINUS_EXPR:
3590       if (! INTEGRAL_TYPE_P (endtype)
3591           || TYPE_PRECISION (endtype) >= POINTER_SIZE)
3592         {
3593           tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
3594                                                       endtype);
3595           tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
3596                                                       endtype);
3597           /* Win if second argument is absolute.  */
3598           if (valid1 == null_pointer_node)
3599             return valid0;
3600           /* Win if both arguments have the same relocation.
3601              Then the value is absolute.  */
3602           if (valid0 == valid1 && valid0 != 0)
3603             return null_pointer_node;
3604
3605           /* Since GCC guarantees that string constants are unique in the
3606              generated code, a subtraction between two copies of the same
3607              constant string is absolute.  */
3608           if (valid0 && TREE_CODE (valid0) == STRING_CST
3609               && valid1 && TREE_CODE (valid1) == STRING_CST
3610               && operand_equal_p (valid0, valid1, 1))
3611             return null_pointer_node;
3612         }
3613
3614       /* Support narrowing differences.  */
3615       if (INTEGRAL_TYPE_P (endtype))
3616         {
3617           tree op0, op1;
3618
3619           op0 = TREE_OPERAND (value, 0);
3620           op1 = TREE_OPERAND (value, 1);
3621
3622           /* Like STRIP_NOPS except allow the operand mode to widen.
3623              This works around a feature of fold that simplifies
3624              (int)(p1 - p2) to ((int)p1 - (int)p2) under the theory
3625              that the narrower operation is cheaper.  */
3626
3627           while (TREE_CODE (op0) == NOP_EXPR
3628                  || TREE_CODE (op0) == CONVERT_EXPR
3629                  || TREE_CODE (op0) == NON_LVALUE_EXPR)
3630             {
3631               tree inner = TREE_OPERAND (op0, 0);
3632               if (inner == error_mark_node
3633                   || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
3634                   || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))
3635                       > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
3636                 break;
3637               op0 = inner;
3638             }
3639
3640           while (TREE_CODE (op1) == NOP_EXPR
3641                  || TREE_CODE (op1) == CONVERT_EXPR
3642                  || TREE_CODE (op1) == NON_LVALUE_EXPR)
3643             {
3644               tree inner = TREE_OPERAND (op1, 0);
3645               if (inner == error_mark_node
3646                   || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
3647                   || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op1)))
3648                       > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
3649                 break;
3650               op1 = inner;
3651             }
3652
3653           op0 = initializer_constant_valid_p (op0, endtype);
3654           op1 = initializer_constant_valid_p (op1, endtype);
3655
3656           /* Both initializers must be known.  */
3657           if (op0 && op1)
3658             {
3659               if (op0 == op1)
3660                 return null_pointer_node;
3661
3662               /* Support differences between labels.  */
3663               if (TREE_CODE (op0) == LABEL_DECL
3664                   && TREE_CODE (op1) == LABEL_DECL)
3665                 return null_pointer_node;
3666
3667               if (TREE_CODE (op0) == STRING_CST
3668                   && TREE_CODE (op1) == STRING_CST
3669                   && operand_equal_p (op0, op1, 1))
3670                 return null_pointer_node;
3671             }
3672         }
3673       break;
3674
3675     default:
3676       break;
3677     }
3678
3679   return 0;
3680 }
3681 \f
3682 /* Output assembler code for constant EXP to FILE, with no label.
3683    This includes the pseudo-op such as ".int" or ".byte", and a newline.
3684    Assumes output_addressed_constants has been done on EXP already.
3685
3686    Generate exactly SIZE bytes of assembler data, padding at the end
3687    with zeros if necessary.  SIZE must always be specified.
3688
3689    SIZE is important for structure constructors,
3690    since trailing members may have been omitted from the constructor.
3691    It is also important for initialization of arrays from string constants
3692    since the full length of the string constant might not be wanted.
3693    It is also needed for initialization of unions, where the initializer's
3694    type is just one member, and that may not be as long as the union.
3695
3696    There a case in which we would fail to output exactly SIZE bytes:
3697    for a structure constructor that wants to produce more than SIZE bytes.
3698    But such constructors will never be generated for any possible input.
3699
3700    ALIGN is the alignment of the data in bits.  */
3701
3702 void
3703 output_constant (tree exp, unsigned HOST_WIDE_INT size, unsigned int align)
3704 {
3705   enum tree_code code;
3706   unsigned HOST_WIDE_INT thissize;
3707
3708   /* Some front-ends use constants other than the standard language-independent
3709      varieties, but which may still be output directly.  Give the front-end a
3710      chance to convert EXP to a language-independent representation.  */
3711   exp = lang_hooks.expand_constant (exp);
3712
3713   if (size == 0 || flag_syntax_only)
3714     return;
3715
3716   /* See if we're trying to initialize a pointer in a non-default mode
3717      to the address of some declaration somewhere.  If the target says
3718      the mode is valid for pointers, assume the target has a way of
3719      resolving it.  */
3720   if (TREE_CODE (exp) == NOP_EXPR
3721       && POINTER_TYPE_P (TREE_TYPE (exp))
3722       && targetm.valid_pointer_mode (TYPE_MODE (TREE_TYPE (exp))))
3723     {
3724       tree saved_type = TREE_TYPE (exp);
3725
3726       /* Peel off any intermediate conversions-to-pointer for valid
3727          pointer modes.  */
3728       while (TREE_CODE (exp) == NOP_EXPR
3729              && POINTER_TYPE_P (TREE_TYPE (exp))
3730              && targetm.valid_pointer_mode (TYPE_MODE (TREE_TYPE (exp))))
3731         exp = TREE_OPERAND (exp, 0);
3732
3733       /* If what we're left with is the address of something, we can
3734          convert the address to the final type and output it that
3735          way.  */
3736       if (TREE_CODE (exp) == ADDR_EXPR)
3737         exp = build1 (ADDR_EXPR, saved_type, TREE_OPERAND (exp, 0));
3738       /* Likewise for constant ints.  */
3739       else if (TREE_CODE (exp) == INTEGER_CST)
3740         exp = build_int_cst_wide (saved_type, TREE_INT_CST_LOW (exp),
3741                                   TREE_INT_CST_HIGH (exp));
3742       
3743     }
3744
3745   /* Eliminate any conversions since we'll be outputting the underlying
3746      constant.  */
3747   while (TREE_CODE (exp) == NOP_EXPR || TREE_CODE (exp) == CONVERT_EXPR
3748          || TREE_CODE (exp) == NON_LVALUE_EXPR
3749          || TREE_CODE (exp) == VIEW_CONVERT_EXPR)
3750     {
3751       HOST_WIDE_INT type_size = int_size_in_bytes (TREE_TYPE (exp));
3752       HOST_WIDE_INT op_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (exp, 0)));
3753
3754       /* Make sure eliminating the conversion is really a no-op, except with
3755          VIEW_CONVERT_EXPRs to allow for wild Ada unchecked conversions and
3756          union types to allow for Ada unchecked unions.  */
3757       if (type_size > op_size
3758           && TREE_CODE (exp) != VIEW_CONVERT_EXPR
3759           && TREE_CODE (TREE_TYPE (exp)) != UNION_TYPE)
3760         internal_error ("no-op convert from %wd to %wd bytes in initializer",
3761                         op_size, type_size);
3762
3763       exp = TREE_OPERAND (exp, 0);
3764     }
3765
3766   code = TREE_CODE (TREE_TYPE (exp));
3767   thissize = int_size_in_bytes (TREE_TYPE (exp));
3768
3769   /* Allow a constructor with no elements for any data type.
3770      This means to fill the space with zeros.  */
3771   if (TREE_CODE (exp) == CONSTRUCTOR
3772       && VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (exp)))
3773     {
3774       assemble_zeros (size);
3775       return;
3776     }
3777
3778   if (TREE_CODE (exp) == FDESC_EXPR)
3779     {
3780 #ifdef ASM_OUTPUT_FDESC
3781       HOST_WIDE_INT part = tree_low_cst (TREE_OPERAND (exp, 1), 0);
3782       tree decl = TREE_OPERAND (exp, 0);
3783       ASM_OUTPUT_FDESC (asm_out_file, decl, part);
3784 #else
3785       gcc_unreachable ();
3786 #endif
3787       return;
3788     }
3789
3790   /* Now output the underlying data.  If we've handling the padding, return.
3791      Otherwise, break and ensure SIZE is the size written.  */
3792   switch (code)
3793     {
3794     case CHAR_TYPE:
3795     case BOOLEAN_TYPE:
3796     case INTEGER_TYPE:
3797     case ENUMERAL_TYPE:
3798     case POINTER_TYPE:
3799     case REFERENCE_TYPE:
3800     case OFFSET_TYPE:
3801       if (! assemble_integer (expand_expr (exp, NULL_RTX, VOIDmode,
3802                                            EXPAND_INITIALIZER),
3803                               MIN (size, thissize), align, 0))
3804         error ("initializer for integer value is too complicated");
3805       break;
3806
3807     case REAL_TYPE:
3808       if (TREE_CODE (exp) != REAL_CST)
3809         error ("initializer for floating value is not a floating constant");
3810
3811       assemble_real (TREE_REAL_CST (exp), TYPE_MODE (TREE_TYPE (exp)), align);
3812       break;
3813
3814     case COMPLEX_TYPE:
3815       output_constant (TREE_REALPART (exp), thissize / 2, align);
3816       output_constant (TREE_IMAGPART (exp), thissize / 2,
3817                        min_align (align, BITS_PER_UNIT * (thissize / 2)));
3818       break;
3819
3820     case ARRAY_TYPE:
3821     case VECTOR_TYPE:
3822       switch (TREE_CODE (exp))
3823         {
3824         case CONSTRUCTOR:
3825           output_constructor (exp, size, align);
3826           return;
3827         case STRING_CST:
3828           thissize = MIN ((unsigned HOST_WIDE_INT)TREE_STRING_LENGTH (exp),
3829                           size);
3830           assemble_string (TREE_STRING_POINTER (exp), thissize);
3831           break;
3832
3833         case VECTOR_CST:
3834           {
3835             int elt_size;
3836             tree link;
3837             unsigned int nalign;
3838             enum machine_mode inner;
3839
3840             inner = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
3841             nalign = MIN (align, GET_MODE_ALIGNMENT (inner));
3842
3843             elt_size = GET_MODE_SIZE (inner);
3844
3845             link = TREE_VECTOR_CST_ELTS (exp);
3846             output_constant (TREE_VALUE (link), elt_size, align);
3847             while ((link = TREE_CHAIN (link)) != NULL)
3848               output_constant (TREE_VALUE (link), elt_size, nalign);
3849             break;
3850           }
3851         default:
3852           gcc_unreachable ();
3853         }
3854       break;
3855
3856     case RECORD_TYPE:
3857     case UNION_TYPE:
3858       gcc_assert (TREE_CODE (exp) == CONSTRUCTOR);
3859       output_constructor (exp, size, align);
3860       return;
3861
3862     case ERROR_MARK:
3863       return;
3864
3865     default:
3866       gcc_unreachable ();
3867     }
3868
3869   if (size > thissize)
3870     assemble_zeros (size - thissize);
3871 }
3872
3873 \f
3874 /* Subroutine of output_constructor, used for computing the size of
3875    arrays of unspecified length.  VAL must be a CONSTRUCTOR of an array
3876    type with an unspecified upper bound.  */
3877
3878 static unsigned HOST_WIDE_INT
3879 array_size_for_constructor (tree val)
3880 {
3881   tree max_index, i;
3882   unsigned HOST_WIDE_INT cnt;
3883   tree index, value;
3884
3885   /* This code used to attempt to handle string constants that are not
3886      arrays of single-bytes, but nothing else does, so there's no point in
3887      doing it here.  */
3888   if (TREE_CODE (val) == STRING_CST)
3889     return TREE_STRING_LENGTH (val);
3890
3891   max_index = NULL_TREE;
3892   FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (val), cnt, index, value)
3893     {
3894       if (TREE_CODE (index) == RANGE_EXPR)
3895         index = TREE_OPERAND (index, 1);
3896       if (max_index == NULL_TREE || tree_int_cst_lt (max_index, index))
3897         max_index = index;
3898     }
3899
3900   if (max_index == NULL_TREE)
3901     return 0;
3902
3903   /* Compute the total number of array elements.  */
3904   i = size_binop (MINUS_EXPR, convert (sizetype, max_index),
3905                   convert (sizetype,
3906                            TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (val)))));
3907   i = size_binop (PLUS_EXPR, i, build_int_cst (sizetype, 1));
3908
3909   /* Multiply by the array element unit size to find number of bytes.  */
3910   i = size_binop (MULT_EXPR, i, TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (val))));
3911
3912   return tree_low_cst (i, 1);
3913 }
3914
3915 /* Subroutine of output_constant, used for CONSTRUCTORs (aggregate constants).
3916    Generate at least SIZE bytes, padding if necessary.  */
3917
3918 static void
3919 output_constructor (tree exp, unsigned HOST_WIDE_INT size,
3920                     unsigned int align)
3921 {
3922   tree type = TREE_TYPE (exp);
3923   tree field = 0;
3924   tree min_index = 0;
3925   /* Number of bytes output or skipped so far.
3926      In other words, current position within the constructor.  */
3927   HOST_WIDE_INT total_bytes = 0;
3928   /* Nonzero means BYTE contains part of a byte, to be output.  */
3929   int byte_buffer_in_use = 0;
3930   int byte = 0;
3931   unsigned HOST_WIDE_INT cnt;
3932   constructor_elt *ce;
3933
3934   gcc_assert (HOST_BITS_PER_WIDE_INT >= BITS_PER_UNIT);
3935
3936   if (TREE_CODE (type) == RECORD_TYPE)
3937     field = TYPE_FIELDS (type);
3938
3939   if (TREE_CODE (type) == ARRAY_TYPE
3940       && TYPE_DOMAIN (type) != 0)
3941     min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
3942
3943   /* As LINK goes through the elements of the constant,
3944      FIELD goes through the structure fields, if the constant is a structure.
3945      if the constant is a union, then we override this,
3946      by getting the field from the TREE_LIST element.
3947      But the constant could also be an array.  Then FIELD is zero.
3948
3949      There is always a maximum of one element in the chain LINK for unions
3950      (even if the initializer in a source program incorrectly contains
3951      more one).  */
3952   for (cnt = 0;
3953        VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (exp), cnt, ce);
3954        cnt++, field = field ? TREE_CHAIN (field) : 0)
3955     {
3956       tree val = ce->value;
3957       tree index = 0;
3958
3959       /* The element in a union constructor specifies the proper field
3960          or index.  */
3961       if ((TREE_CODE (type) == RECORD_TYPE || TREE_CODE (type) == UNION_TYPE
3962            || TREE_CODE (type) == QUAL_UNION_TYPE)
3963           && ce->index != 0)
3964         field = ce->index;
3965
3966       else if (TREE_CODE (type) == ARRAY_TYPE)
3967         index = ce->index;
3968
3969 #ifdef ASM_COMMENT_START
3970       if (field && flag_verbose_asm)
3971         fprintf (asm_out_file, "%s %s:\n",
3972                  ASM_COMMENT_START,
3973                  DECL_NAME (field)
3974                  ? IDENTIFIER_POINTER (DECL_NAME (field))
3975                  : "<anonymous>");
3976 #endif
3977
3978       /* Eliminate the marker that makes a cast not be an lvalue.  */
3979       if (val != 0)
3980         STRIP_NOPS (val);
3981
3982       if (index && TREE_CODE (index) == RANGE_EXPR)
3983         {
3984           unsigned HOST_WIDE_INT fieldsize
3985             = int_size_in_bytes (TREE_TYPE (type));
3986           HOST_WIDE_INT lo_index = tree_low_cst (TREE_OPERAND (index, 0), 0);
3987           HOST_WIDE_INT hi_index = tree_low_cst (TREE_OPERAND (index, 1), 0);
3988           HOST_WIDE_INT index;
3989           unsigned int align2 = min_align (align, fieldsize * BITS_PER_UNIT);
3990
3991           for (index = lo_index; index <= hi_index; index++)
3992             {
3993               /* Output the element's initial value.  */
3994               if (val == 0)
3995                 assemble_zeros (fieldsize);
3996               else
3997                 output_constant (val, fieldsize, align2);
3998
3999               /* Count its size.  */
4000               total_bytes += fieldsize;
4001             }
4002         }
4003       else if (field == 0 || !DECL_BIT_FIELD (field))
4004         {
4005           /* An element that is not a bit-field.  */
4006
4007           unsigned HOST_WIDE_INT fieldsize;
4008           /* Since this structure is static,
4009              we know the positions are constant.  */
4010           HOST_WIDE_INT pos = field ? int_byte_position (field) : 0;
4011           unsigned int align2;
4012
4013           if (index != 0)
4014             pos = (tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (val)), 1)
4015                    * (tree_low_cst (index, 0) - tree_low_cst (min_index, 0)));
4016
4017           /* Output any buffered-up bit-fields preceding this element.  */
4018           if (byte_buffer_in_use)
4019             {
4020               assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
4021               total_bytes++;
4022               byte_buffer_in_use = 0;
4023             }
4024
4025           /* Advance to offset of this element.
4026              Note no alignment needed in an array, since that is guaranteed
4027              if each element has the proper size.  */
4028           if ((field != 0 || index != 0) && pos != total_bytes)
4029             {
4030               gcc_assert (pos >= total_bytes);
4031               assemble_zeros (pos - total_bytes);
4032               total_bytes = pos;
4033             }
4034
4035           /* Find the alignment of this element.  */
4036           align2 = min_align (align, BITS_PER_UNIT * pos);
4037
4038           /* Determine size this element should occupy.  */
4039           if (field)
4040             {
4041               fieldsize = 0;
4042
4043               /* If this is an array with an unspecified upper bound,
4044                  the initializer determines the size.  */
4045               /* ??? This ought to only checked if DECL_SIZE_UNIT is NULL,
4046                  but we cannot do this until the deprecated support for
4047                  initializing zero-length array members is removed.  */
4048               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
4049                   && TYPE_DOMAIN (TREE_TYPE (field))
4050                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
4051                 {
4052                   fieldsize = array_size_for_constructor (val);
4053                   /* Given a non-empty initialization, this field had
4054                      better be last.  */
4055                   gcc_assert (!fieldsize || !TREE_CHAIN (field));
4056                 }
4057               else if (DECL_SIZE_UNIT (field))
4058                 {
4059                   /* ??? This can't be right.  If the decl size overflows
4060                      a host integer we will silently emit no data.  */
4061                   if (host_integerp (DECL_SIZE_UNIT (field), 1))
4062                     fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 1);
4063                 }
4064             }
4065           else
4066             fieldsize = int_size_in_bytes (TREE_TYPE (type));
4067
4068           /* Output the element's initial value.  */
4069           if (val == 0)
4070             assemble_zeros (fieldsize);
4071           else
4072             output_constant (val, fieldsize, align2);
4073
4074           /* Count its size.  */
4075           total_bytes += fieldsize;
4076         }
4077       else if (val != 0 && TREE_CODE (val) != INTEGER_CST)
4078         error ("invalid initial value for member %qs",
4079                IDENTIFIER_POINTER (DECL_NAME (field)));
4080       else
4081         {
4082           /* Element that is a bit-field.  */
4083
4084           HOST_WIDE_INT next_offset = int_bit_position (field);
4085           HOST_WIDE_INT end_offset
4086             = (next_offset + tree_low_cst (DECL_SIZE (field), 1));
4087
4088           if (val == 0)
4089             val = integer_zero_node;
4090
4091           /* If this field does not start in this (or, next) byte,
4092              skip some bytes.  */
4093           if (next_offset / BITS_PER_UNIT != total_bytes)
4094             {
4095               /* Output remnant of any bit field in previous bytes.  */
4096               if (byte_buffer_in_use)
4097                 {
4098                   assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
4099                   total_bytes++;
4100                   byte_buffer_in_use = 0;
4101                 }
4102
4103               /* If still not at proper byte, advance to there.  */
4104               if (next_offset / BITS_PER_UNIT != total_bytes)
4105                 {
4106                   gcc_assert (next_offset / BITS_PER_UNIT >= total_bytes);
4107                   assemble_zeros (next_offset / BITS_PER_UNIT - total_bytes);
4108                   total_bytes = next_offset / BITS_PER_UNIT;
4109                 }
4110             }
4111
4112           if (! byte_buffer_in_use)
4113             byte = 0;
4114
4115           /* We must split the element into pieces that fall within
4116              separate bytes, and combine each byte with previous or
4117              following bit-fields.  */
4118
4119           /* next_offset is the offset n fbits from the beginning of
4120              the structure to the next bit of this element to be processed.
4121              end_offset is the offset of the first bit past the end of
4122              this element.  */
4123           while (next_offset < end_offset)
4124             {
4125               int this_time;
4126               int shift;
4127               HOST_WIDE_INT value;
4128               HOST_WIDE_INT next_byte = next_offset / BITS_PER_UNIT;
4129               HOST_WIDE_INT next_bit = next_offset % BITS_PER_UNIT;
4130
4131               /* Advance from byte to byte
4132                  within this element when necessary.  */
4133               while (next_byte != total_bytes)
4134                 {
4135                   assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
4136                   total_bytes++;
4137                   byte = 0;
4138                 }
4139
4140               /* Number of bits we can process at once
4141                  (all part of the same byte).  */
4142               this_time = MIN (end_offset - next_offset,
4143                                BITS_PER_UNIT - next_bit);
4144               if (BYTES_BIG_ENDIAN)
4145                 {
4146                   /* On big-endian machine, take the most significant bits
4147                      first (of the bits that are significant)
4148                      and put them into bytes from the most significant end.  */
4149                   shift = end_offset - next_offset - this_time;
4150
4151                   /* Don't try to take a bunch of bits that cross
4152                      the word boundary in the INTEGER_CST. We can
4153                      only select bits from the LOW or HIGH part
4154                      not from both.  */
4155                   if (shift < HOST_BITS_PER_WIDE_INT
4156                       && shift + this_time > HOST_BITS_PER_WIDE_INT)
4157                     {
4158                       this_time = shift + this_time - HOST_BITS_PER_WIDE_INT;
4159                       shift = HOST_BITS_PER_WIDE_INT;
4160                     }
4161
4162                   /* Now get the bits from the appropriate constant word.  */
4163                   if (shift < HOST_BITS_PER_WIDE_INT)
4164                     value = TREE_INT_CST_LOW (val);
4165                   else
4166                     {
4167                       gcc_assert (shift < 2 * HOST_BITS_PER_WIDE_INT);
4168                       value = TREE_INT_CST_HIGH (val);
4169                       shift -= HOST_BITS_PER_WIDE_INT;
4170                     }
4171
4172                   /* Get the result. This works only when:
4173                      1 <= this_time <= HOST_BITS_PER_WIDE_INT.  */
4174                   byte |= (((value >> shift)
4175                             & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
4176                            << (BITS_PER_UNIT - this_time - next_bit));
4177                 }
4178               else
4179                 {
4180                   /* On little-endian machines,
4181                      take first the least significant bits of the value
4182                      and pack them starting at the least significant
4183                      bits of the bytes.  */
4184                   shift = next_offset - int_bit_position (field);
4185
4186                   /* Don't try to take a bunch of bits that cross
4187                      the word boundary in the INTEGER_CST. We can
4188                      only select bits from the LOW or HIGH part
4189                      not from both.  */
4190                   if (shift < HOST_BITS_PER_WIDE_INT
4191                       && shift + this_time > HOST_BITS_PER_WIDE_INT)
4192                     this_time = (HOST_BITS_PER_WIDE_INT - shift);
4193
4194                   /* Now get the bits from the appropriate constant word.  */
4195                   if (shift < HOST_BITS_PER_WIDE_INT)
4196                     value = TREE_INT_CST_LOW (val);
4197                   else
4198                     {
4199                       gcc_assert (shift < 2 * HOST_BITS_PER_WIDE_INT);
4200                       value = TREE_INT_CST_HIGH (val);
4201                       shift -= HOST_BITS_PER_WIDE_INT;
4202                     }
4203
4204                   /* Get the result. This works only when:
4205                      1 <= this_time <= HOST_BITS_PER_WIDE_INT.  */
4206                   byte |= (((value >> shift)
4207                             & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
4208                            << next_bit);
4209                 }
4210
4211               next_offset += this_time;
4212               byte_buffer_in_use = 1;
4213             }
4214         }
4215     }
4216
4217   if (byte_buffer_in_use)
4218     {
4219       assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
4220       total_bytes++;
4221     }
4222
4223   if ((unsigned HOST_WIDE_INT)total_bytes < size)
4224     assemble_zeros (size - total_bytes);
4225 }
4226
4227 /* This TREE_LIST contains any weak symbol declarations waiting
4228    to be emitted.  */
4229 static GTY(()) tree weak_decls;
4230
4231 /* Mark DECL as weak.  */
4232
4233 static void
4234 mark_weak (tree decl)
4235 {
4236   DECL_WEAK (decl) = 1;
4237
4238   if (DECL_RTL_SET_P (decl)
4239       && MEM_P (DECL_RTL (decl))
4240       && XEXP (DECL_RTL (decl), 0)
4241       && GET_CODE (XEXP (DECL_RTL (decl), 0)) == SYMBOL_REF)
4242     SYMBOL_REF_WEAK (XEXP (DECL_RTL (decl), 0)) = 1;
4243 }
4244
4245 /* Merge weak status between NEWDECL and OLDDECL.  */
4246
4247 void
4248 merge_weak (tree newdecl, tree olddecl)
4249 {
4250   if (DECL_WEAK (newdecl) == DECL_WEAK (olddecl))
4251     {
4252       if (DECL_WEAK (newdecl) && SUPPORTS_WEAK)
4253         {
4254           tree *pwd;
4255           /* We put the NEWDECL on the weak_decls list at some point
4256              and OLDDECL as well.  Keep just OLDDECL on the list.  */
4257           for (pwd = &weak_decls; *pwd; pwd = &TREE_CHAIN (*pwd))
4258             if (TREE_VALUE (*pwd) == newdecl)
4259               {
4260                 *pwd = TREE_CHAIN (*pwd);
4261                 break;
4262               }
4263         }
4264       return;
4265     }
4266
4267   if (DECL_WEAK (newdecl))
4268     {
4269       tree wd;
4270
4271       /* NEWDECL is weak, but OLDDECL is not.  */
4272
4273       /* If we already output the OLDDECL, we're in trouble; we can't
4274          go back and make it weak.  This error cannot caught in
4275          declare_weak because the NEWDECL and OLDDECL was not yet
4276          been merged; therefore, TREE_ASM_WRITTEN was not set.  */
4277       if (TREE_ASM_WRITTEN (olddecl))
4278         error ("weak declaration of %q+D must precede definition",
4279                newdecl);
4280
4281       /* If we've already generated rtl referencing OLDDECL, we may
4282          have done so in a way that will not function properly with
4283          a weak symbol.  */
4284       else if (TREE_USED (olddecl)
4285                && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (olddecl)))
4286         warning (0, "weak declaration of %q+D after first use results "
4287                  "in unspecified behavior", newdecl);
4288
4289       if (SUPPORTS_WEAK)
4290         {
4291           /* We put the NEWDECL on the weak_decls list at some point.
4292              Replace it with the OLDDECL.  */
4293           for (wd = weak_decls; wd; wd = TREE_CHAIN (wd))
4294             if (TREE_VALUE (wd) == newdecl)
4295               {
4296                 TREE_VALUE (wd) = olddecl;
4297                 break;
4298               }
4299           /* We may not find the entry on the list.  If NEWDECL is a
4300              weak alias, then we will have already called
4301              globalize_decl to remove the entry; in that case, we do
4302              not need to do anything.  */
4303         }
4304
4305       /* Make the OLDDECL weak; it's OLDDECL that we'll be keeping.  */
4306       mark_weak (olddecl);
4307     }
4308   else
4309     /* OLDDECL was weak, but NEWDECL was not explicitly marked as
4310        weak.  Just update NEWDECL to indicate that it's weak too.  */
4311     mark_weak (newdecl);
4312 }
4313
4314 /* Declare DECL to be a weak symbol.  */
4315
4316 void
4317 declare_weak (tree decl)
4318 {
4319   if (! TREE_PUBLIC (decl))
4320     error ("weak declaration of %q+D must be public", decl);
4321   else if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
4322     error ("weak declaration of %q+D must precede definition", decl);
4323   else if (SUPPORTS_WEAK)
4324     {
4325       if (! DECL_WEAK (decl))
4326         weak_decls = tree_cons (NULL, decl, weak_decls);
4327     }
4328   else
4329     warning (0, "weak declaration of %q+D not supported", decl);
4330
4331   mark_weak (decl);
4332 }
4333
4334 static void
4335 weak_finish_1 (tree decl)
4336 {
4337 #if defined (ASM_WEAKEN_DECL) || defined (ASM_WEAKEN_LABEL)
4338   const char *const name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
4339 #endif
4340
4341   if (! TREE_USED (decl))
4342     return;
4343
4344 #ifdef ASM_WEAKEN_DECL
4345   ASM_WEAKEN_DECL (asm_out_file, decl, name, NULL);
4346 #else
4347 #ifdef ASM_WEAKEN_LABEL
4348   ASM_WEAKEN_LABEL (asm_out_file, name);
4349 #else
4350 #ifdef ASM_OUTPUT_WEAK_ALIAS
4351   {
4352     static bool warn_once = 0;
4353     if (! warn_once)
4354       {
4355         warning (0, "only weak aliases are supported in this configuration");
4356         warn_once = 1;
4357       }
4358     return;
4359   }
4360 #endif
4361 #endif
4362 #endif
4363 }
4364
4365 /* This TREE_LIST contains weakref targets.  */
4366
4367 static GTY(()) tree weakref_targets;
4368
4369 /* Forward declaration.  */
4370 static tree find_decl_and_mark_needed (tree decl, tree target);
4371
4372 /* Emit any pending weak declarations.  */
4373
4374 void
4375 weak_finish (void)
4376 {
4377   tree t;
4378
4379   for (t = weakref_targets; t; t = TREE_CHAIN (t))
4380     {
4381       tree alias_decl = TREE_PURPOSE (t);
4382       tree target = ultimate_transparent_alias_target (&TREE_VALUE (t));
4383
4384       if (! TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (alias_decl)))
4385         /* Remove alias_decl from the weak list, but leave entries for
4386            the target alone.  */
4387         target = NULL_TREE;
4388 #ifndef ASM_OUTPUT_WEAKREF
4389       else if (! TREE_SYMBOL_REFERENCED (target))
4390         {
4391           /* Use ASM_WEAKEN_LABEL only if ASM_WEAKEN_DECL is not
4392              defined, otherwise we and weak_finish_1 would use a
4393              different macros.  */
4394 # if defined ASM_WEAKEN_LABEL && ! defined ASM_WEAKEN_DECL
4395           ASM_WEAKEN_LABEL (asm_out_file, IDENTIFIER_POINTER (target));
4396 # else
4397           tree decl = find_decl_and_mark_needed (alias_decl, target);
4398
4399           if (! decl)
4400             {
4401               decl = build_decl (TREE_CODE (alias_decl), target,
4402                                  TREE_TYPE (alias_decl));
4403
4404               DECL_EXTERNAL (decl) = 1;
4405               TREE_PUBLIC (decl) = 1;
4406               DECL_ARTIFICIAL (decl) = 1;
4407               TREE_NOTHROW (decl) = TREE_NOTHROW (alias_decl);
4408               TREE_USED (decl) = 1;
4409             }
4410
4411           weak_finish_1 (decl);
4412 # endif
4413         }
4414 #endif
4415
4416       {
4417         tree *p;
4418         tree t2;
4419
4420         /* Remove the alias and the target from the pending weak list
4421            so that we do not emit any .weak directives for the former,
4422            nor multiple .weak directives for the latter.  */
4423         for (p = &weak_decls; (t2 = *p) ; )
4424           {
4425             if (TREE_VALUE (t2) == alias_decl
4426                 || target == DECL_ASSEMBLER_NAME (TREE_VALUE (t2)))
4427               *p = TREE_CHAIN (t2);
4428             else
4429               p = &TREE_CHAIN (t2);
4430           }
4431
4432         /* Remove other weakrefs to the same target, to speed things up.  */
4433         for (p = &TREE_CHAIN (t); (t2 = *p) ; )
4434           {
4435             if (target == ultimate_transparent_alias_target (&TREE_VALUE (t2)))
4436               *p = TREE_CHAIN (t2);
4437             else
4438               p = &TREE_CHAIN (t2);
4439           }
4440       }
4441     }
4442
4443   for (t = weak_decls; t; t = TREE_CHAIN (t))
4444     {
4445       tree decl = TREE_VALUE (t);
4446
4447       weak_finish_1 (decl);
4448     }
4449 }
4450
4451 /* Emit the assembly bits to indicate that DECL is globally visible.  */
4452
4453 static void
4454 globalize_decl (tree decl)
4455 {
4456   const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
4457
4458 #if defined (ASM_WEAKEN_LABEL) || defined (ASM_WEAKEN_DECL)
4459   if (DECL_WEAK (decl))
4460     {
4461       tree *p, t;
4462
4463 #ifdef ASM_WEAKEN_DECL
4464       ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
4465 #else
4466       ASM_WEAKEN_LABEL (asm_out_file, name);
4467 #endif
4468
4469       /* Remove this function from the pending weak list so that
4470          we do not emit multiple .weak directives for it.  */
4471       for (p = &weak_decls; (t = *p) ; )
4472         {
4473           if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
4474             *p = TREE_CHAIN (t);
4475           else
4476             p = &TREE_CHAIN (t);
4477         }
4478
4479         /* Remove weakrefs to the same target from the pending weakref
4480            list, for the same reason.  */
4481         for (p = &weakref_targets; (t = *p) ; )
4482           {
4483             if (DECL_ASSEMBLER_NAME (decl)
4484                 == ultimate_transparent_alias_target (&TREE_VALUE (t)))
4485               *p = TREE_CHAIN (t);
4486             else
4487               p = &TREE_CHAIN (t);
4488           }
4489
4490       return;
4491     }
4492 #elif defined(ASM_MAKE_LABEL_LINKONCE)
4493   if (DECL_ONE_ONLY (decl))
4494     ASM_MAKE_LABEL_LINKONCE (asm_out_file, name);
4495 #endif
4496
4497   targetm.asm_out.globalize_label (asm_out_file, name);
4498 }
4499
4500 /* We have to be able to tell cgraph about the needed-ness of the target
4501    of an alias.  This requires that the decl have been defined.  Aliases
4502    that precede their definition have to be queued for later processing.  */
4503
4504 typedef struct alias_pair GTY(())
4505 {
4506   tree decl;
4507   tree target;
4508 } alias_pair;
4509
4510 /* Define gc'd vector type.  */
4511 DEF_VEC_O(alias_pair);
4512 DEF_VEC_ALLOC_O(alias_pair,gc);
4513
4514 static GTY(()) VEC(alias_pair,gc) *alias_pairs;
4515
4516 /* Given an assembly name, find the decl it is associated with.  At the
4517    same time, mark it needed for cgraph.  */
4518
4519 static tree
4520 find_decl_and_mark_needed (tree decl, tree target)
4521 {
4522   struct cgraph_node *fnode = NULL;
4523   struct cgraph_varpool_node *vnode = NULL;
4524
4525   if (TREE_CODE (decl) == FUNCTION_DECL)
4526     {
4527       fnode = cgraph_node_for_asm (target);
4528       if (fnode == NULL)
4529         vnode = cgraph_varpool_node_for_asm (target);
4530     }
4531   else
4532     {
4533       vnode = cgraph_varpool_node_for_asm (target);
4534       if (vnode == NULL)
4535         fnode = cgraph_node_for_asm (target);
4536     }
4537
4538   if (fnode)
4539     {
4540       /* We can't mark function nodes as used after cgraph global info
4541          is finished.  This wouldn't generally be necessary, but C++
4542          virtual table thunks are introduced late in the game and
4543          might seem like they need marking, although in fact they
4544          don't.  */
4545       if (! cgraph_global_info_ready)
4546         cgraph_mark_needed_node (fnode);
4547       return fnode->decl;
4548     }
4549   else if (vnode)
4550     {
4551       cgraph_varpool_mark_needed_node (vnode);
4552       return vnode->decl;
4553     }
4554   else
4555     return NULL_TREE;
4556 }
4557
4558 /* Output the assembler code for a define (equate) using ASM_OUTPUT_DEF
4559    or ASM_OUTPUT_DEF_FROM_DECLS.  The function defines the symbol whose
4560    tree node is DECL to have the value of the tree node TARGET.  */
4561
4562 static void
4563 do_assemble_alias (tree decl, tree target)
4564 {
4565   if (TREE_ASM_WRITTEN (decl))
4566     return;
4567
4568   TREE_ASM_WRITTEN (decl) = 1;
4569   TREE_ASM_WRITTEN (DECL_ASSEMBLER_NAME (decl)) = 1;
4570
4571   if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
4572     {
4573       ultimate_transparent_alias_target (&target);
4574
4575       if (!TREE_SYMBOL_REFERENCED (target))
4576         weakref_targets = tree_cons (decl, target, weakref_targets);
4577
4578 #ifdef ASM_OUTPUT_WEAKREF
4579       ASM_OUTPUT_WEAKREF (asm_out_file, decl,
4580                           IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
4581                           IDENTIFIER_POINTER (target));
4582 #else
4583       if (!SUPPORTS_WEAK)
4584         {
4585           error ("%Jweakref is not supported in this configuration", decl);
4586           return;
4587         }
4588 #endif
4589       return;
4590     }
4591
4592 #ifdef ASM_OUTPUT_DEF
4593   /* Make name accessible from other files, if appropriate.  */
4594
4595   if (TREE_PUBLIC (decl))
4596     {
4597       globalize_decl (decl);
4598       maybe_assemble_visibility (decl);
4599     }
4600
4601 # ifdef ASM_OUTPUT_DEF_FROM_DECLS
4602   ASM_OUTPUT_DEF_FROM_DECLS (asm_out_file, decl, target);
4603 # else
4604   ASM_OUTPUT_DEF (asm_out_file,
4605                   IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
4606                   IDENTIFIER_POINTER (target));
4607 # endif
4608 #elif defined (ASM_OUTPUT_WEAK_ALIAS) || defined (ASM_WEAKEN_DECL)
4609   {
4610     const char *name;
4611     tree *p, t;
4612
4613     name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
4614 # ifdef ASM_WEAKEN_DECL
4615     ASM_WEAKEN_DECL (asm_out_file, decl, name, IDENTIFIER_POINTER (target));
4616 # else
4617     ASM_OUTPUT_WEAK_ALIAS (asm_out_file, name, IDENTIFIER_POINTER (target));
4618 # endif
4619     /* Remove this function from the pending weak list so that
4620        we do not emit multiple .weak directives for it.  */
4621     for (p = &weak_decls; (t = *p) ; )
4622       if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
4623         *p = TREE_CHAIN (t);
4624       else
4625         p = &TREE_CHAIN (t);
4626
4627     /* Remove weakrefs to the same target from the pending weakref
4628        list, for the same reason.  */
4629     for (p = &weakref_targets; (t = *p) ; )
4630       {
4631         if (DECL_ASSEMBLER_NAME (decl)
4632             == ultimate_transparent_alias_target (&TREE_VALUE (t)))
4633           *p = TREE_CHAIN (t);
4634         else
4635           p = &TREE_CHAIN (t);
4636       }
4637   }
4638 #endif
4639 }
4640
4641 /* First pass of completing pending aliases.  Make sure that cgraph knows
4642    which symbols will be required.  */
4643
4644 void
4645 finish_aliases_1 (void)
4646 {
4647   unsigned i;
4648   alias_pair *p;
4649
4650   for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); i++)
4651     {
4652       tree target_decl;
4653
4654       target_decl = find_decl_and_mark_needed (p->decl, p->target);
4655       if (target_decl == NULL)
4656         {
4657           if (! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
4658             error ("%q+D aliased to undefined symbol %qs",
4659                    p->decl, IDENTIFIER_POINTER (p->target));
4660         }
4661       else if (DECL_EXTERNAL (target_decl)
4662                && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
4663         error ("%q+D aliased to external symbol %qs",
4664                p->decl, IDENTIFIER_POINTER (p->target));
4665     }
4666 }
4667
4668 /* Second pass of completing pending aliases.  Emit the actual assembly.
4669    This happens at the end of compilation and thus it is assured that the
4670    target symbol has been emitted.  */
4671
4672 void
4673 finish_aliases_2 (void)
4674 {
4675   unsigned i;
4676   alias_pair *p;
4677
4678   for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); i++)
4679     do_assemble_alias (p->decl, p->target);
4680
4681   VEC_truncate (alias_pair, alias_pairs, 0);
4682 }
4683
4684 /* Emit an assembler directive to make the symbol for DECL an alias to
4685    the symbol for TARGET.  */
4686
4687 void
4688 assemble_alias (tree decl, tree target)
4689 {
4690   tree target_decl;
4691   bool is_weakref = false;
4692
4693   if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
4694     {
4695       tree alias = DECL_ASSEMBLER_NAME (decl);
4696
4697       is_weakref = true;
4698
4699       ultimate_transparent_alias_target (&target);
4700
4701       if (alias == target)
4702         error ("weakref %q+D ultimately targets itself", decl);
4703       else
4704         {
4705 #ifndef ASM_OUTPUT_WEAKREF
4706           IDENTIFIER_TRANSPARENT_ALIAS (alias) = 1;
4707           TREE_CHAIN (alias) = target;
4708 #endif
4709         }
4710       if (TREE_PUBLIC (decl))
4711         error ("weakref %q+D must have static linkage", decl);
4712     }
4713   else
4714     {
4715 #if !defined (ASM_OUTPUT_DEF)
4716 # if !defined(ASM_OUTPUT_WEAK_ALIAS) && !defined (ASM_WEAKEN_DECL)
4717       error ("%Jalias definitions not supported in this configuration", decl);
4718       return;
4719 # else
4720       if (!DECL_WEAK (decl))
4721         {
4722           error ("%Jonly weak aliases are supported in this configuration", decl);
4723           return;
4724         }
4725 # endif
4726 #endif
4727     }
4728
4729   /* We must force creation of DECL_RTL for debug info generation, even though
4730      we don't use it here.  */
4731   make_decl_rtl (decl);
4732   TREE_USED (decl) = 1;
4733
4734   /* A quirk of the initial implementation of aliases required that the user
4735      add "extern" to all of them.  Which is silly, but now historical.  Do
4736      note that the symbol is in fact locally defined.  */
4737   if (! is_weakref)
4738     DECL_EXTERNAL (decl) = 0;
4739
4740   /* Allow aliases to aliases.  */
4741   if (TREE_CODE (decl) == FUNCTION_DECL)
4742     cgraph_node (decl)->alias = true;
4743   else
4744     cgraph_varpool_node (decl)->alias = true;
4745
4746   /* If the target has already been emitted, we don't have to queue the
4747      alias.  This saves a tad o memory.  */
4748   target_decl = find_decl_and_mark_needed (decl, target);
4749   if (target_decl && TREE_ASM_WRITTEN (target_decl))
4750     do_assemble_alias (decl, target);
4751   else
4752     {
4753       alias_pair *p = VEC_safe_push (alias_pair, gc, alias_pairs, NULL);
4754       p->decl = decl;
4755       p->target = target;
4756     }
4757 }
4758
4759 /* Emit an assembler directive to set symbol for DECL visibility to
4760    the visibility type VIS, which must not be VISIBILITY_DEFAULT.  */
4761
4762 void
4763 default_assemble_visibility (tree decl, int vis)
4764 {
4765   static const char * const visibility_types[] = {
4766     NULL, "internal", "hidden", "protected"
4767   };
4768
4769   const char *name, *type;
4770
4771   name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
4772   type = visibility_types[vis];
4773
4774 #ifdef HAVE_GAS_HIDDEN
4775   fprintf (asm_out_file, "\t.%s\t", type);
4776   assemble_name (asm_out_file, name);
4777   fprintf (asm_out_file, "\n");
4778 #else
4779   warning (OPT_Wattributes, "visibility attribute not supported "
4780            "in this configuration; ignored");
4781 #endif
4782 }
4783
4784 /* A helper function to call assemble_visibility when needed for a decl.  */
4785
4786 static void
4787 maybe_assemble_visibility (tree decl)
4788 {
4789   enum symbol_visibility vis = DECL_VISIBILITY (decl);
4790
4791   if (vis != VISIBILITY_DEFAULT)
4792     targetm.asm_out.visibility (decl, vis);
4793 }
4794
4795 /* Returns 1 if the target configuration supports defining public symbols
4796    so that one of them will be chosen at link time instead of generating a
4797    multiply-defined symbol error, whether through the use of weak symbols or
4798    a target-specific mechanism for having duplicates discarded.  */
4799
4800 int
4801 supports_one_only (void)
4802 {
4803   if (SUPPORTS_ONE_ONLY)
4804     return 1;
4805   return SUPPORTS_WEAK;
4806 }
4807
4808 /* Set up DECL as a public symbol that can be defined in multiple
4809    translation units without generating a linker error.  */
4810
4811 void
4812 make_decl_one_only (tree decl)
4813 {
4814   gcc_assert (TREE_CODE (decl) == VAR_DECL
4815               || TREE_CODE (decl) == FUNCTION_DECL);
4816
4817   TREE_PUBLIC (decl) = 1;
4818
4819   if (SUPPORTS_ONE_ONLY)
4820     {
4821 #ifdef MAKE_DECL_ONE_ONLY
4822       MAKE_DECL_ONE_ONLY (decl);
4823 #endif
4824       DECL_ONE_ONLY (decl) = 1;
4825     }
4826   else if (TREE_CODE (decl) == VAR_DECL
4827       && (DECL_INITIAL (decl) == 0 || DECL_INITIAL (decl) == error_mark_node))
4828     DECL_COMMON (decl) = 1;
4829   else
4830     {
4831       gcc_assert (SUPPORTS_WEAK);
4832       DECL_WEAK (decl) = 1;
4833     }
4834 }
4835
4836 void
4837 init_varasm_once (void)
4838 {
4839   section_htab = htab_create_ggc (31, section_entry_hash,
4840                                   section_entry_eq, NULL);
4841   const_desc_htab = htab_create_ggc (1009, const_desc_hash,
4842                                      const_desc_eq, NULL);
4843
4844   const_alias_set = new_alias_set ();
4845
4846 #ifdef TEXT_SECTION_ASM_OP
4847   text_section = get_unnamed_section (SECTION_CODE, output_section_asm_op,
4848                                       TEXT_SECTION_ASM_OP);
4849 #endif
4850
4851 #ifdef DATA_SECTION_ASM_OP
4852   data_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
4853                                       DATA_SECTION_ASM_OP);
4854 #endif
4855
4856 #ifdef SDATA_SECTION_ASM_OP
4857   sdata_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
4858                                        SDATA_SECTION_ASM_OP);
4859 #endif
4860
4861 #ifdef READONLY_DATA_SECTION_ASM_OP
4862   readonly_data_section = get_unnamed_section (0, output_section_asm_op,
4863                                                READONLY_DATA_SECTION_ASM_OP);
4864 #endif
4865
4866 #ifdef CTORS_SECTION_ASM_OP
4867   ctors_section = get_unnamed_section (0, output_section_asm_op,
4868                                        CTORS_SECTION_ASM_OP);
4869 #endif
4870
4871 #ifdef DTORS_SECTION_ASM_OP
4872   dtors_section = get_unnamed_section (0, output_section_asm_op,
4873                                        DTORS_SECTION_ASM_OP);
4874 #endif
4875
4876 #ifdef BSS_SECTION_ASM_OP
4877   bss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
4878                                      output_section_asm_op,
4879                                      BSS_SECTION_ASM_OP);
4880 #endif
4881
4882 #ifdef SBSS_SECTION_ASM_OP
4883   sbss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
4884                                       output_section_asm_op,
4885                                       SBSS_SECTION_ASM_OP);
4886 #endif
4887
4888   targetm.asm_out.init_sections ();
4889
4890   if (readonly_data_section == NULL)
4891     readonly_data_section = text_section;
4892 }
4893
4894 enum tls_model
4895 decl_default_tls_model (tree decl)
4896 {
4897   enum tls_model kind;
4898   bool is_local;
4899
4900   is_local = targetm.binds_local_p (decl);
4901   if (!flag_shlib)
4902     {
4903       if (is_local)
4904         kind = TLS_MODEL_LOCAL_EXEC;
4905       else
4906         kind = TLS_MODEL_INITIAL_EXEC;
4907     }
4908
4909   /* Local dynamic is inefficient when we're not combining the
4910      parts of the address.  */
4911   else if (optimize && is_local)
4912     kind = TLS_MODEL_LOCAL_DYNAMIC;
4913   else
4914     kind = TLS_MODEL_GLOBAL_DYNAMIC;
4915   if (kind < flag_tls_default)
4916     kind = flag_tls_default;
4917
4918   return kind;
4919 }
4920
4921 /* Select a set of attributes for section NAME based on the properties
4922    of DECL and whether or not RELOC indicates that DECL's initializer
4923    might contain runtime relocations.
4924
4925    We make the section read-only and executable for a function decl,
4926    read-only for a const data decl, and writable for a non-const data decl.  */
4927
4928 unsigned int
4929 default_section_type_flags (tree decl, const char *name, int reloc)
4930 {
4931   return default_section_type_flags_1 (decl, name, reloc, flag_pic);
4932 }
4933
4934 unsigned int
4935 default_section_type_flags_1 (tree decl, const char *name, int reloc,
4936                               int shlib)
4937 {
4938   unsigned int flags;
4939
4940   if (decl && TREE_CODE (decl) == FUNCTION_DECL)
4941     flags = SECTION_CODE;
4942   else if (decl && decl_readonly_section_1 (decl, reloc, shlib))
4943     flags = 0;
4944   else if (current_function_decl
4945            && cfun
4946            && cfun->unlikely_text_section_name
4947            && strcmp (name, cfun->unlikely_text_section_name) == 0)
4948     flags = SECTION_CODE;
4949   else if (!decl
4950            && (!current_function_decl || !cfun)
4951            && strcmp (name, UNLIKELY_EXECUTED_TEXT_SECTION_NAME) == 0)
4952     flags = SECTION_CODE;
4953   else
4954     flags = SECTION_WRITE;
4955
4956   if (decl && DECL_ONE_ONLY (decl))
4957     flags |= SECTION_LINKONCE;
4958
4959   if (decl && TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
4960     flags |= SECTION_TLS | SECTION_WRITE;
4961
4962   if (strcmp (name, ".bss") == 0
4963       || strncmp (name, ".bss.", 5) == 0
4964       || strncmp (name, ".gnu.linkonce.b.", 16) == 0
4965       || strcmp (name, ".sbss") == 0
4966       || strncmp (name, ".sbss.", 6) == 0
4967       || strncmp (name, ".gnu.linkonce.sb.", 17) == 0)
4968     flags |= SECTION_BSS;
4969
4970   if (strcmp (name, ".tdata") == 0
4971       || strncmp (name, ".tdata.", 7) == 0
4972       || strncmp (name, ".gnu.linkonce.td.", 17) == 0)
4973     flags |= SECTION_TLS;
4974
4975   if (strcmp (name, ".tbss") == 0
4976       || strncmp (name, ".tbss.", 6) == 0
4977       || strncmp (name, ".gnu.linkonce.tb.", 17) == 0)
4978     flags |= SECTION_TLS | SECTION_BSS;
4979
4980   /* These three sections have special ELF types.  They are neither
4981      SHT_PROGBITS nor SHT_NOBITS, so when changing sections we don't
4982      want to print a section type (@progbits or @nobits).  If someone
4983      is silly enough to emit code or TLS variables to one of these
4984      sections, then don't handle them specially.  */
4985   if (!(flags & (SECTION_CODE | SECTION_BSS | SECTION_TLS))
4986       && (strcmp (name, ".init_array") == 0
4987           || strcmp (name, ".fini_array") == 0
4988           || strcmp (name, ".preinit_array") == 0))
4989     flags |= SECTION_NOTYPE;
4990
4991   return flags;
4992 }
4993
4994 /* Output assembly to switch to section NAME with attribute FLAGS.
4995    Four variants for common object file formats.  */
4996
4997 void
4998 default_no_named_section (const char *name ATTRIBUTE_UNUSED,
4999                           unsigned int flags ATTRIBUTE_UNUSED,
5000                           tree decl ATTRIBUTE_UNUSED)
5001 {
5002   /* Some object formats don't support named sections at all.  The
5003      front-end should already have flagged this as an error.  */
5004   gcc_unreachable ();
5005 }
5006
5007 void
5008 default_elf_asm_named_section (const char *name, unsigned int flags,
5009                                tree decl ATTRIBUTE_UNUSED)
5010 {
5011   char flagchars[10], *f = flagchars;
5012
5013   /* If we have already declared this section, we can use an
5014      abbreviated form to switch back to it -- unless this section is
5015      part of a COMDAT groups, in which case GAS requires the full
5016      declaration every time.  */
5017   if (!(HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
5018       && (flags & SECTION_DECLARED))
5019     {
5020       fprintf (asm_out_file, "\t.section\t%s\n", name);
5021       return;
5022     }
5023
5024   if (!(flags & SECTION_DEBUG))
5025     *f++ = 'a';
5026   if (flags & SECTION_WRITE)
5027     *f++ = 'w';
5028   if (flags & SECTION_CODE)
5029     *f++ = 'x';
5030   if (flags & SECTION_SMALL)
5031     *f++ = 's';
5032   if (flags & SECTION_MERGE)
5033     *f++ = 'M';
5034   if (flags & SECTION_STRINGS)
5035     *f++ = 'S';
5036   if (flags & SECTION_TLS)
5037     *f++ = 'T';
5038   if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
5039     *f++ = 'G';
5040   *f = '\0';
5041
5042   fprintf (asm_out_file, "\t.section\t%s,\"%s\"", name, flagchars);
5043
5044   if (!(flags & SECTION_NOTYPE))
5045     {
5046       const char *type;
5047       const char *format;
5048
5049       if (flags & SECTION_BSS)
5050         type = "nobits";
5051       else
5052         type = "progbits";
5053
5054       format = ",@%s";
5055 #ifdef ASM_COMMENT_START
5056       /* On platforms that use "@" as the assembly comment character,
5057          use "%" instead.  */
5058       if (strcmp (ASM_COMMENT_START, "@") == 0)
5059         format = ",%%%s";
5060 #endif
5061       fprintf (asm_out_file, format, type);
5062
5063       if (flags & SECTION_ENTSIZE)
5064         fprintf (asm_out_file, ",%d", flags & SECTION_ENTSIZE);
5065       if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
5066         fprintf (asm_out_file, ",%s,comdat",
5067                  lang_hooks.decls.comdat_group (decl));
5068     }
5069
5070   putc ('\n', asm_out_file);
5071 }
5072
5073 void
5074 default_coff_asm_named_section (const char *name, unsigned int flags,
5075                                 tree decl ATTRIBUTE_UNUSED)
5076 {
5077   char flagchars[8], *f = flagchars;
5078
5079   if (flags & SECTION_WRITE)
5080     *f++ = 'w';
5081   if (flags & SECTION_CODE)
5082     *f++ = 'x';
5083   *f = '\0';
5084
5085   fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
5086 }
5087
5088 void
5089 default_pe_asm_named_section (const char *name, unsigned int flags,
5090                               tree decl)
5091 {
5092   default_coff_asm_named_section (name, flags, decl);
5093
5094   if (flags & SECTION_LINKONCE)
5095     {
5096       /* Functions may have been compiled at various levels of
5097          optimization so we can't use `same_size' here.
5098          Instead, have the linker pick one.  */
5099       fprintf (asm_out_file, "\t.linkonce %s\n",
5100                (flags & SECTION_CODE ? "discard" : "same_size"));
5101     }
5102 }
5103 \f
5104 /* The lame default section selector.  */
5105
5106 section *
5107 default_select_section (tree decl, int reloc,
5108                         unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
5109 {
5110   if (DECL_P (decl))
5111     {
5112       if (decl_readonly_section (decl, reloc))
5113         return readonly_data_section;
5114     }
5115   else if (TREE_CODE (decl) == CONSTRUCTOR)
5116     {
5117       if (! ((flag_pic && reloc)
5118              || !TREE_READONLY (decl)
5119              || TREE_SIDE_EFFECTS (decl)
5120              || !TREE_CONSTANT (decl)))
5121         return readonly_data_section;
5122     }
5123   else if (TREE_CODE (decl) == STRING_CST)
5124     return readonly_data_section;
5125   else if (! (flag_pic && reloc))
5126     return readonly_data_section;
5127
5128   return data_section;
5129 }
5130
5131 enum section_category
5132 categorize_decl_for_section (tree decl, int reloc, int shlib)
5133 {
5134   enum section_category ret;
5135
5136   if (TREE_CODE (decl) == FUNCTION_DECL)
5137     return SECCAT_TEXT;
5138   else if (TREE_CODE (decl) == STRING_CST)
5139     {
5140       if (flag_mudflap) /* or !flag_merge_constants */
5141         return SECCAT_RODATA;
5142       else
5143         return SECCAT_RODATA_MERGE_STR;
5144     }
5145   else if (TREE_CODE (decl) == VAR_DECL)
5146     {
5147       if (DECL_INITIAL (decl) == NULL
5148           || DECL_INITIAL (decl) == error_mark_node
5149           || (flag_zero_initialized_in_bss
5150               /* Leave constant zeroes in .rodata so they can be shared.  */
5151               && !TREE_READONLY (decl)
5152               && initializer_zerop (DECL_INITIAL (decl))))
5153         ret = SECCAT_BSS;
5154       else if (! TREE_READONLY (decl)
5155                || TREE_SIDE_EFFECTS (decl)
5156                || ! TREE_CONSTANT (DECL_INITIAL (decl)))
5157         {
5158           if (shlib && (reloc & 2))
5159             ret = SECCAT_DATA_REL;
5160           else if (shlib && reloc)
5161             ret = SECCAT_DATA_REL_LOCAL;
5162           else
5163             ret = SECCAT_DATA;
5164         }
5165       else if (shlib && (reloc & 2))
5166         ret = SECCAT_DATA_REL_RO;
5167       else if (shlib && reloc)
5168         ret = SECCAT_DATA_REL_RO_LOCAL;
5169       else if (reloc || flag_merge_constants < 2)
5170         /* C and C++ don't allow different variables to share the same
5171            location.  -fmerge-all-constants allows even that (at the
5172            expense of not conforming).  */
5173         ret = SECCAT_RODATA;
5174       else if (TREE_CODE (DECL_INITIAL (decl)) == STRING_CST)
5175         ret = SECCAT_RODATA_MERGE_STR_INIT;
5176       else
5177         ret = SECCAT_RODATA_MERGE_CONST;
5178     }
5179   else if (TREE_CODE (decl) == CONSTRUCTOR)
5180     {
5181       if ((shlib && reloc)
5182           || TREE_SIDE_EFFECTS (decl)
5183           || ! TREE_CONSTANT (decl))
5184         ret = SECCAT_DATA;
5185       else
5186         ret = SECCAT_RODATA;
5187     }
5188   else
5189     ret = SECCAT_RODATA;
5190
5191   /* There are no read-only thread-local sections.  */
5192   if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
5193     {
5194       /* Note that this would be *just* SECCAT_BSS, except that there's
5195          no concept of a read-only thread-local-data section.  */
5196       if (ret == SECCAT_BSS
5197           || (flag_zero_initialized_in_bss
5198               && initializer_zerop (DECL_INITIAL (decl))))
5199         ret = SECCAT_TBSS;
5200       else
5201         ret = SECCAT_TDATA;
5202     }
5203
5204   /* If the target uses small data sections, select it.  */
5205   else if (targetm.in_small_data_p (decl))
5206     {
5207       if (ret == SECCAT_BSS)
5208         ret = SECCAT_SBSS;
5209       else if (targetm.have_srodata_section && ret == SECCAT_RODATA)
5210         ret = SECCAT_SRODATA;
5211       else
5212         ret = SECCAT_SDATA;
5213     }
5214
5215   return ret;
5216 }
5217
5218 bool
5219 decl_readonly_section (tree decl, int reloc)
5220 {
5221   return decl_readonly_section_1 (decl, reloc, flag_pic);
5222 }
5223
5224 bool
5225 decl_readonly_section_1 (tree decl, int reloc, int shlib)
5226 {
5227   switch (categorize_decl_for_section (decl, reloc, shlib))
5228     {
5229     case SECCAT_RODATA:
5230     case SECCAT_RODATA_MERGE_STR:
5231     case SECCAT_RODATA_MERGE_STR_INIT:
5232     case SECCAT_RODATA_MERGE_CONST:
5233     case SECCAT_SRODATA:
5234       return true;
5235       break;
5236     default:
5237       return false;
5238       break;
5239     }
5240 }
5241
5242 /* Select a section based on the above categorization.  */
5243
5244 section *
5245 default_elf_select_section (tree decl, int reloc,
5246                             unsigned HOST_WIDE_INT align)
5247 {
5248   return default_elf_select_section_1 (decl, reloc, align, flag_pic);
5249 }
5250
5251 section *
5252 default_elf_select_section_1 (tree decl, int reloc,
5253                               unsigned HOST_WIDE_INT align, int shlib)
5254 {
5255   const char *sname;
5256   switch (categorize_decl_for_section (decl, reloc, shlib))
5257     {
5258     case SECCAT_TEXT:
5259       /* We're not supposed to be called on FUNCTION_DECLs.  */
5260       gcc_unreachable ();
5261     case SECCAT_RODATA:
5262       return readonly_data_section;
5263     case SECCAT_RODATA_MERGE_STR:
5264       return mergeable_string_section (decl, align, 0);
5265     case SECCAT_RODATA_MERGE_STR_INIT:
5266       return mergeable_string_section (DECL_INITIAL (decl), align, 0);
5267     case SECCAT_RODATA_MERGE_CONST:
5268       return mergeable_constant_section (DECL_MODE (decl), align, 0);
5269     case SECCAT_SRODATA:
5270       sname = ".sdata2";
5271       break;
5272     case SECCAT_DATA:
5273       return data_section;
5274     case SECCAT_DATA_REL:
5275       sname = ".data.rel";
5276       break;
5277     case SECCAT_DATA_REL_LOCAL:
5278       sname = ".data.rel.local";
5279       break;
5280     case SECCAT_DATA_REL_RO:
5281       sname = ".data.rel.ro";
5282       break;
5283     case SECCAT_DATA_REL_RO_LOCAL:
5284       sname = ".data.rel.ro.local";
5285       break;
5286     case SECCAT_SDATA:
5287       sname = ".sdata";
5288       break;
5289     case SECCAT_TDATA:
5290       sname = ".tdata";
5291       break;
5292     case SECCAT_BSS:
5293       if (bss_section)
5294         return bss_section;
5295       sname = ".bss";
5296       break;
5297     case SECCAT_SBSS:
5298       sname = ".sbss";
5299       break;
5300     case SECCAT_TBSS:
5301       sname = ".tbss";
5302       break;
5303     default:
5304       gcc_unreachable ();
5305     }
5306
5307   if (!DECL_P (decl))
5308     decl = NULL_TREE;
5309   return get_named_section (decl, sname, reloc);
5310 }
5311
5312 /* Construct a unique section name based on the decl name and the
5313    categorization performed above.  */
5314
5315 void
5316 default_unique_section (tree decl, int reloc)
5317 {
5318   default_unique_section_1 (decl, reloc, flag_pic);
5319 }
5320
5321 void
5322 default_unique_section_1 (tree decl, int reloc, int shlib)
5323 {
5324   /* We only need to use .gnu.linkonce if we don't have COMDAT groups.  */
5325   bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
5326   const char *prefix, *name;
5327   size_t nlen, plen;
5328   char *string;
5329
5330   switch (categorize_decl_for_section (decl, reloc, shlib))
5331     {
5332     case SECCAT_TEXT:
5333       prefix = one_only ? ".gnu.linkonce.t." : ".text.";
5334       break;
5335     case SECCAT_RODATA:
5336     case SECCAT_RODATA_MERGE_STR:
5337     case SECCAT_RODATA_MERGE_STR_INIT:
5338     case SECCAT_RODATA_MERGE_CONST:
5339       prefix = one_only ? ".gnu.linkonce.r." : ".rodata.";
5340       break;
5341     case SECCAT_SRODATA:
5342       prefix = one_only ? ".gnu.linkonce.s2." : ".sdata2.";
5343       break;
5344     case SECCAT_DATA:
5345       prefix = one_only ? ".gnu.linkonce.d." : ".data.";
5346       break;
5347     case SECCAT_DATA_REL:
5348       prefix = one_only ? ".gnu.linkonce.d.rel." : ".data.rel.";
5349       break;
5350     case SECCAT_DATA_REL_LOCAL:
5351       prefix = one_only ? ".gnu.linkonce.d.rel.local." : ".data.rel.local.";
5352       break;
5353     case SECCAT_DATA_REL_RO:
5354       prefix = one_only ? ".gnu.linkonce.d.rel.ro." : ".data.rel.ro.";
5355       break;
5356     case SECCAT_DATA_REL_RO_LOCAL:
5357       prefix = one_only ? ".gnu.linkonce.d.rel.ro.local."
5358                : ".data.rel.ro.local.";
5359       break;
5360     case SECCAT_SDATA:
5361       prefix = one_only ? ".gnu.linkonce.s." : ".sdata.";
5362       break;
5363     case SECCAT_BSS:
5364       prefix = one_only ? ".gnu.linkonce.b." : ".bss.";
5365       break;
5366     case SECCAT_SBSS:
5367       prefix = one_only ? ".gnu.linkonce.sb." : ".sbss.";
5368       break;
5369     case SECCAT_TDATA:
5370       prefix = one_only ? ".gnu.linkonce.td." : ".tdata.";
5371       break;
5372     case SECCAT_TBSS:
5373       prefix = one_only ? ".gnu.linkonce.tb." : ".tbss.";
5374       break;
5375     default:
5376       gcc_unreachable ();
5377     }
5378   plen = strlen (prefix);
5379
5380   name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5381   name = targetm.strip_name_encoding (name);
5382   nlen = strlen (name);
5383
5384   string = alloca (nlen + plen + 1);
5385   memcpy (string, prefix, plen);
5386   memcpy (string + plen, name, nlen + 1);
5387
5388   DECL_SECTION_NAME (decl) = build_string (nlen + plen, string);
5389 }
5390
5391 section *
5392 default_select_rtx_section (enum machine_mode mode ATTRIBUTE_UNUSED,
5393                             rtx x,
5394                             unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
5395 {
5396   if (flag_pic)
5397     switch (GET_CODE (x))
5398       {
5399       case CONST:
5400       case SYMBOL_REF:
5401       case LABEL_REF:
5402         return data_section;
5403
5404       default:
5405         break;
5406       }
5407
5408   return readonly_data_section;
5409 }
5410
5411 section *
5412 default_elf_select_rtx_section (enum machine_mode mode, rtx x,
5413                                 unsigned HOST_WIDE_INT align)
5414 {
5415   /* ??? Handle small data here somehow.  */
5416
5417   if (flag_pic)
5418     switch (GET_CODE (x))
5419       {
5420       case CONST:
5421       case SYMBOL_REF:
5422         return get_named_section (NULL, ".data.rel.ro", 3);
5423
5424       case LABEL_REF:
5425         return get_named_section (NULL, ".data.rel.ro.local", 1);
5426
5427       default:
5428         break;
5429       }
5430
5431   return mergeable_constant_section (mode, align, 0);
5432 }
5433
5434 /* Set the generally applicable flags on the SYMBOL_REF for EXP.  */
5435
5436 void
5437 default_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
5438 {
5439   rtx symbol;
5440   int flags;
5441
5442   /* Careful not to prod global register variables.  */
5443   if (!MEM_P (rtl))
5444     return;
5445   symbol = XEXP (rtl, 0);
5446   if (GET_CODE (symbol) != SYMBOL_REF)
5447     return;
5448
5449   flags = 0;
5450   if (TREE_CODE (decl) == FUNCTION_DECL)
5451     flags |= SYMBOL_FLAG_FUNCTION;
5452   if (targetm.binds_local_p (decl))
5453     flags |= SYMBOL_FLAG_LOCAL;
5454   if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
5455     flags |= DECL_TLS_MODEL (decl) << SYMBOL_FLAG_TLS_SHIFT;
5456   else if (targetm.in_small_data_p (decl))
5457     flags |= SYMBOL_FLAG_SMALL;
5458   /* ??? Why is DECL_EXTERNAL ever set for non-PUBLIC names?  Without
5459      being PUBLIC, the thing *must* be defined in this translation unit.
5460      Prevent this buglet from being propagated into rtl code as well.  */
5461   if (DECL_P (decl) && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl))
5462     flags |= SYMBOL_FLAG_EXTERNAL;
5463
5464   SYMBOL_REF_FLAGS (symbol) = flags;
5465 }
5466
5467 /* By default, we do nothing for encode_section_info, so we need not
5468    do anything but discard the '*' marker.  */
5469
5470 const char *
5471 default_strip_name_encoding (const char *str)
5472 {
5473   return str + (*str == '*');
5474 }
5475
5476 /* Assume ELF-ish defaults, since that's pretty much the most liberal
5477    wrt cross-module name binding.  */
5478
5479 bool
5480 default_binds_local_p (tree exp)
5481 {
5482   return default_binds_local_p_1 (exp, flag_shlib);
5483 }
5484
5485 bool
5486 default_binds_local_p_1 (tree exp, int shlib)
5487 {
5488   bool local_p;
5489
5490   /* A non-decl is an entry in the constant pool.  */
5491   if (!DECL_P (exp))
5492     local_p = true;
5493   /* Weakrefs may not bind locally, even though the weakref itself is
5494      always static and therefore local.  */
5495   else if (lookup_attribute ("weakref", DECL_ATTRIBUTES (exp)))
5496     local_p = false;
5497   /* Static variables are always local.  */
5498   else if (! TREE_PUBLIC (exp))
5499     local_p = true;
5500   /* A variable is local if the user has said explicitly that it will
5501      be.  */
5502   else if (DECL_VISIBILITY_SPECIFIED (exp)
5503            && DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
5504     local_p = true;
5505   /* Variables defined outside this object might not be local.  */
5506   else if (DECL_EXTERNAL (exp))
5507     local_p = false;
5508   /* If defined in this object and visibility is not default, must be
5509      local.  */
5510   else if (DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
5511     local_p = true;
5512   /* Default visibility weak data can be overridden by a strong symbol
5513      in another module and so are not local.  */
5514   else if (DECL_WEAK (exp))
5515     local_p = false;
5516   /* If PIC, then assume that any global name can be overridden by
5517      symbols resolved from other modules.  */
5518   else if (shlib)
5519     local_p = false;
5520   /* Uninitialized COMMON variable may be unified with symbols
5521      resolved from other modules.  */
5522   else if (DECL_COMMON (exp)
5523            && (DECL_INITIAL (exp) == NULL
5524                || DECL_INITIAL (exp) == error_mark_node))
5525     local_p = false;
5526   /* Otherwise we're left with initialized (or non-common) global data
5527      which is of necessity defined locally.  */
5528   else
5529     local_p = true;
5530
5531   return local_p;
5532 }
5533
5534 /* Determine whether or not a pointer mode is valid. Assume defaults
5535    of ptr_mode or Pmode - can be overridden.  */
5536 bool
5537 default_valid_pointer_mode (enum machine_mode mode)
5538 {
5539   return (mode == ptr_mode || mode == Pmode);
5540 }
5541
5542 /* Default function to output code that will globalize a label.  A
5543    target must define GLOBAL_ASM_OP or provide its own function to
5544    globalize a label.  */
5545 #ifdef GLOBAL_ASM_OP
5546 void
5547 default_globalize_label (FILE * stream, const char *name)
5548 {
5549   fputs (GLOBAL_ASM_OP, stream);
5550   assemble_name (stream, name);
5551   putc ('\n', stream);
5552 }
5553 #endif /* GLOBAL_ASM_OP */
5554
5555 /* Default function to output a label for unwind information.  The
5556    default is to do nothing.  A target that needs nonlocal labels for
5557    unwind information must provide its own function to do this.  */
5558 void
5559 default_emit_unwind_label (FILE * stream ATTRIBUTE_UNUSED,
5560                            tree decl ATTRIBUTE_UNUSED,
5561                            int for_eh ATTRIBUTE_UNUSED,
5562                            int empty ATTRIBUTE_UNUSED)
5563 {
5564 }
5565
5566 /* Default function to output a label to divide up the exception table.
5567    The default is to do nothing.  A target that needs/wants to divide
5568    up the table must provide it's own function to do this.  */
5569 void
5570 default_emit_except_table_label (FILE * stream ATTRIBUTE_UNUSED)
5571 {
5572 }
5573
5574 /* This is how to output an internal numbered label where PREFIX is
5575    the class of label and LABELNO is the number within the class.  */
5576
5577 void
5578 default_internal_label (FILE *stream, const char *prefix,
5579                         unsigned long labelno)
5580 {
5581   char *const buf = alloca (40 + strlen (prefix));
5582   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
5583   ASM_OUTPUT_INTERNAL_LABEL (stream, buf);
5584 }
5585
5586 /* This is the default behavior at the beginning of a file.  It's
5587    controlled by two other target-hook toggles.  */
5588 void
5589 default_file_start (void)
5590 {
5591   if (targetm.file_start_app_off && !flag_verbose_asm)
5592     fputs (ASM_APP_OFF, asm_out_file);
5593
5594   if (targetm.file_start_file_directive)
5595     output_file_directive (asm_out_file, main_input_filename);
5596 }
5597
5598 /* This is a generic routine suitable for use as TARGET_ASM_FILE_END
5599    which emits a special section directive used to indicate whether or
5600    not this object file needs an executable stack.  This is primarily
5601    a GNU extension to ELF but could be used on other targets.  */
5602
5603 int trampolines_created;
5604
5605 void
5606 file_end_indicate_exec_stack (void)
5607 {
5608   unsigned int flags = SECTION_DEBUG;
5609   if (trampolines_created)
5610     flags |= SECTION_CODE;
5611
5612   switch_to_section (get_section (".note.GNU-stack", flags, NULL));
5613 }
5614
5615 /* Output DIRECTIVE (a C string) followed by a newline.  This is used as
5616    a get_unnamed_section callback.  */
5617
5618 void
5619 output_section_asm_op (const void *directive)
5620 {
5621   fprintf (asm_out_file, "%s\n", (const char *) directive);
5622 }
5623
5624 /* Emit assembly code to switch to section NEW_SECTION.  Do nothing if
5625    the current section is NEW_SECTION.  */
5626
5627 void
5628 switch_to_section (section *new_section)
5629 {
5630   if (in_section == new_section)
5631     return;
5632
5633   if (new_section->common.flags & SECTION_FORGET)
5634     in_section = NULL;
5635   else
5636     in_section = new_section;
5637
5638   if (new_section->common.flags & SECTION_NAMED)
5639     {
5640       if (cfun
5641           && !cfun->unlikely_text_section_name
5642           && strcmp (new_section->named.name,
5643                      UNLIKELY_EXECUTED_TEXT_SECTION_NAME) == 0)
5644         cfun->unlikely_text_section_name = UNLIKELY_EXECUTED_TEXT_SECTION_NAME;
5645
5646       targetm.asm_out.named_section (new_section->named.name,
5647                                      new_section->named.common.flags,
5648                                      new_section->named.decl);
5649     }
5650   else
5651     new_section->unnamed.callback (new_section->unnamed.data);
5652
5653   new_section->common.flags |= SECTION_DECLARED;
5654 }
5655
5656 #include "gt-varasm.h"