OSDN Git Service

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