OSDN Git Service

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