OSDN Git Service

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