OSDN Git Service

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