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