OSDN Git Service

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