OSDN Git Service

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