OSDN Git Service

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