OSDN Git Service

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