OSDN Git Service

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