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