OSDN Git Service

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