OSDN Git Service

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