OSDN Git Service

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