OSDN Git Service

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