OSDN Git Service

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