OSDN Git Service

disable scan-assembler tests in g++.dg/abi/mangle60.C
[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) == MEM_REF
2602                && TREE_CODE (TREE_OPERAND (target, 0)) == ADDR_EXPR)
2603         {
2604           offset += mem_ref_offset (target).low;
2605           target = TREE_OPERAND (TREE_OPERAND (target, 0), 0);
2606         }
2607       else if (TREE_CODE (target) == INDIRECT_REF
2608                && TREE_CODE (TREE_OPERAND (target, 0)) == NOP_EXPR
2609                && TREE_CODE (TREE_OPERAND (TREE_OPERAND (target, 0), 0))
2610                   == ADDR_EXPR)
2611         target = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (target, 0), 0), 0);
2612       else
2613         break;
2614     }
2615
2616   switch (TREE_CODE (target))
2617     {
2618     case VAR_DECL:
2619     case FUNCTION_DECL:
2620       x = DECL_RTL (target);
2621       break;
2622
2623     case LABEL_DECL:
2624       x = gen_rtx_MEM (FUNCTION_MODE,
2625                        gen_rtx_LABEL_REF (Pmode, force_label_rtx (target)));
2626       break;
2627
2628     case REAL_CST:
2629     case FIXED_CST:
2630     case STRING_CST:
2631     case COMPLEX_CST:
2632     case CONSTRUCTOR:
2633     case INTEGER_CST:
2634       x = output_constant_def (target, 1);
2635       break;
2636
2637     default:
2638       gcc_unreachable ();
2639     }
2640
2641   gcc_assert (MEM_P (x));
2642   x = XEXP (x, 0);
2643
2644   value->base = x;
2645   value->offset = offset;
2646 }
2647 \f
2648
2649 static GTY((param_is (struct constant_descriptor_tree)))
2650      htab_t const_desc_htab;
2651
2652 static void maybe_output_constant_def_contents (struct constant_descriptor_tree *, int);
2653
2654 /* Constant pool accessor function.  */
2655
2656 htab_t
2657 constant_pool_htab (void)
2658 {
2659   return const_desc_htab;
2660 }
2661
2662 /* Compute a hash code for a constant expression.  */
2663
2664 static hashval_t
2665 const_desc_hash (const void *ptr)
2666 {
2667   return ((const struct constant_descriptor_tree *)ptr)->hash;
2668 }
2669
2670 static hashval_t
2671 const_hash_1 (const tree exp)
2672 {
2673   const char *p;
2674   hashval_t hi;
2675   int len, i;
2676   enum tree_code code = TREE_CODE (exp);
2677
2678   /* Either set P and LEN to the address and len of something to hash and
2679      exit the switch or return a value.  */
2680
2681   switch (code)
2682     {
2683     case INTEGER_CST:
2684       p = (char *) &TREE_INT_CST (exp);
2685       len = sizeof TREE_INT_CST (exp);
2686       break;
2687
2688     case REAL_CST:
2689       return real_hash (TREE_REAL_CST_PTR (exp));
2690
2691     case FIXED_CST:
2692       return fixed_hash (TREE_FIXED_CST_PTR (exp));
2693
2694     case STRING_CST:
2695       p = TREE_STRING_POINTER (exp);
2696       len = TREE_STRING_LENGTH (exp);
2697       break;
2698
2699     case COMPLEX_CST:
2700       return (const_hash_1 (TREE_REALPART (exp)) * 5
2701               + const_hash_1 (TREE_IMAGPART (exp)));
2702
2703     case VECTOR_CST:
2704       {
2705         tree link;
2706
2707         hi = 7 + TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp));
2708
2709         for (link = TREE_VECTOR_CST_ELTS (exp); link; link = TREE_CHAIN (link))
2710             hi = hi * 563 + const_hash_1 (TREE_VALUE (link));
2711
2712         return hi;
2713       }
2714
2715     case CONSTRUCTOR:
2716       {
2717         unsigned HOST_WIDE_INT idx;
2718         tree value;
2719
2720         hi = 5 + int_size_in_bytes (TREE_TYPE (exp));
2721
2722         FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
2723           if (value)
2724             hi = hi * 603 + const_hash_1 (value);
2725
2726         return hi;
2727       }
2728
2729     case ADDR_EXPR:
2730     case FDESC_EXPR:
2731       {
2732         struct addr_const value;
2733
2734         decode_addr_const (exp, &value);
2735         switch (GET_CODE (value.base))
2736           {
2737           case SYMBOL_REF:
2738             /* Don't hash the address of the SYMBOL_REF;
2739                only use the offset and the symbol name.  */
2740             hi = value.offset;
2741             p = XSTR (value.base, 0);
2742             for (i = 0; p[i] != 0; i++)
2743               hi = ((hi * 613) + (unsigned) (p[i]));
2744             break;
2745
2746           case LABEL_REF:
2747             hi = value.offset + CODE_LABEL_NUMBER (XEXP (value.base, 0)) * 13;
2748             break;
2749
2750           default:
2751             gcc_unreachable ();
2752           }
2753       }
2754       return hi;
2755
2756     case PLUS_EXPR:
2757     case POINTER_PLUS_EXPR:
2758     case MINUS_EXPR:
2759       return (const_hash_1 (TREE_OPERAND (exp, 0)) * 9
2760               + const_hash_1 (TREE_OPERAND (exp, 1)));
2761
2762     CASE_CONVERT:
2763       return const_hash_1 (TREE_OPERAND (exp, 0)) * 7 + 2;
2764
2765     default:
2766       /* A language specific constant. Just hash the code.  */
2767       return code;
2768     }
2769
2770   /* Compute hashing function.  */
2771   hi = len;
2772   for (i = 0; i < len; i++)
2773     hi = ((hi * 613) + (unsigned) (p[i]));
2774
2775   return hi;
2776 }
2777
2778 /* Wrapper of compare_constant, for the htab interface.  */
2779 static int
2780 const_desc_eq (const void *p1, const void *p2)
2781 {
2782   const struct constant_descriptor_tree *const c1
2783     = (const struct constant_descriptor_tree *) p1;
2784   const struct constant_descriptor_tree *const c2
2785     = (const struct constant_descriptor_tree *) p2;
2786   if (c1->hash != c2->hash)
2787     return 0;
2788   return compare_constant (c1->value, c2->value);
2789 }
2790
2791 /* Compare t1 and t2, and return 1 only if they are known to result in
2792    the same bit pattern on output.  */
2793
2794 static int
2795 compare_constant (const tree t1, const tree t2)
2796 {
2797   enum tree_code typecode;
2798
2799   if (t1 == NULL_TREE)
2800     return t2 == NULL_TREE;
2801   if (t2 == NULL_TREE)
2802     return 0;
2803
2804   if (TREE_CODE (t1) != TREE_CODE (t2))
2805     return 0;
2806
2807   switch (TREE_CODE (t1))
2808     {
2809     case INTEGER_CST:
2810       /* Integer constants are the same only if the same width of type.  */
2811       if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
2812         return 0;
2813       if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
2814         return 0;
2815       return tree_int_cst_equal (t1, t2);
2816
2817     case REAL_CST:
2818       /* Real constants are the same only if the same width of type.  */
2819       if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
2820         return 0;
2821
2822       return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
2823
2824     case FIXED_CST:
2825       /* Fixed constants are the same only if the same width of type.  */
2826       if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
2827         return 0;
2828
2829       return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
2830
2831     case STRING_CST:
2832       if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
2833         return 0;
2834
2835       return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
2836               && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
2837                          TREE_STRING_LENGTH (t1)));
2838
2839     case COMPLEX_CST:
2840       return (compare_constant (TREE_REALPART (t1), TREE_REALPART (t2))
2841               && compare_constant (TREE_IMAGPART (t1), TREE_IMAGPART (t2)));
2842
2843     case VECTOR_CST:
2844       {
2845         tree link1, link2;
2846
2847         if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (t1))
2848             != TYPE_VECTOR_SUBPARTS (TREE_TYPE (t2)))
2849           return 0;
2850
2851         link2 = TREE_VECTOR_CST_ELTS (t2);
2852         for (link1 = TREE_VECTOR_CST_ELTS (t1);
2853              link1;
2854              link1 = TREE_CHAIN (link1))
2855           {
2856             if (!compare_constant (TREE_VALUE (link1), TREE_VALUE (link2)))
2857               return 0;
2858             link2 = TREE_CHAIN (link2);
2859           }
2860         
2861         return 1;
2862       }
2863
2864     case CONSTRUCTOR:
2865       {
2866         VEC(constructor_elt, gc) *v1, *v2;
2867         unsigned HOST_WIDE_INT idx;
2868
2869         typecode = TREE_CODE (TREE_TYPE (t1));
2870         if (typecode != TREE_CODE (TREE_TYPE (t2)))
2871           return 0;
2872
2873         if (typecode == ARRAY_TYPE)
2874           {
2875             HOST_WIDE_INT size_1 = int_size_in_bytes (TREE_TYPE (t1));
2876             /* For arrays, check that the sizes all match.  */
2877             if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))
2878                 || size_1 == -1
2879                 || size_1 != int_size_in_bytes (TREE_TYPE (t2)))
2880               return 0;
2881           }
2882         else
2883           {
2884             /* For record and union constructors, require exact type
2885                equality.  */
2886             if (TREE_TYPE (t1) != TREE_TYPE (t2))
2887               return 0;
2888           }
2889
2890         v1 = CONSTRUCTOR_ELTS (t1);
2891         v2 = CONSTRUCTOR_ELTS (t2);
2892         if (VEC_length (constructor_elt, v1)
2893             != VEC_length (constructor_elt, v2))
2894             return 0;
2895
2896         for (idx = 0; idx < VEC_length (constructor_elt, v1); ++idx)
2897           {
2898             constructor_elt *c1 = VEC_index (constructor_elt, v1, idx);
2899             constructor_elt *c2 = VEC_index (constructor_elt, v2, idx);
2900
2901             /* Check that each value is the same...  */
2902             if (!compare_constant (c1->value, c2->value))
2903               return 0;
2904             /* ... and that they apply to the same fields!  */
2905             if (typecode == ARRAY_TYPE)
2906               {
2907                 if (!compare_constant (c1->index, c2->index))
2908                   return 0;
2909               }
2910             else
2911               {
2912                 if (c1->index != c2->index)
2913                   return 0;
2914               }
2915           }
2916
2917         return 1;
2918       }
2919
2920     case ADDR_EXPR:
2921     case FDESC_EXPR:
2922       {
2923         struct addr_const value1, value2;
2924         enum rtx_code code;
2925         int ret;
2926
2927         decode_addr_const (t1, &value1);
2928         decode_addr_const (t2, &value2);
2929
2930         if (value1.offset != value2.offset)
2931           return 0;
2932
2933         code = GET_CODE (value1.base);
2934         if (code != GET_CODE (value2.base))
2935           return 0;
2936
2937         switch (code)
2938           {
2939           case SYMBOL_REF:
2940             ret = (strcmp (XSTR (value1.base, 0), XSTR (value2.base, 0)) == 0);
2941             break;
2942
2943           case LABEL_REF:
2944             ret = (CODE_LABEL_NUMBER (XEXP (value1.base, 0))
2945                    == CODE_LABEL_NUMBER (XEXP (value2.base, 0)));
2946             break;
2947
2948           default:
2949             gcc_unreachable ();
2950           }
2951         return ret;
2952       }
2953
2954     case PLUS_EXPR:
2955     case POINTER_PLUS_EXPR:
2956     case MINUS_EXPR:
2957     case RANGE_EXPR:
2958       return (compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
2959               && compare_constant(TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
2960
2961     CASE_CONVERT:
2962     case VIEW_CONVERT_EXPR:
2963       return compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2964
2965     default:
2966       return 0;
2967     }
2968
2969   gcc_unreachable ();
2970 }
2971 \f
2972 /* Make a copy of the whole tree structure for a constant.  This
2973    handles the same types of nodes that compare_constant handles.  */
2974
2975 static tree
2976 copy_constant (tree exp)
2977 {
2978   switch (TREE_CODE (exp))
2979     {
2980     case ADDR_EXPR:
2981       /* For ADDR_EXPR, we do not want to copy the decl whose address
2982          is requested.  We do want to copy constants though.  */
2983       if (CONSTANT_CLASS_P (TREE_OPERAND (exp, 0)))
2984         return build1 (TREE_CODE (exp), TREE_TYPE (exp),
2985                        copy_constant (TREE_OPERAND (exp, 0)));
2986       else
2987         return copy_node (exp);
2988
2989     case INTEGER_CST:
2990     case REAL_CST:
2991     case FIXED_CST:
2992     case STRING_CST:
2993       return copy_node (exp);
2994
2995     case COMPLEX_CST:
2996       return build_complex (TREE_TYPE (exp),
2997                             copy_constant (TREE_REALPART (exp)),
2998                             copy_constant (TREE_IMAGPART (exp)));
2999
3000     case PLUS_EXPR:
3001     case POINTER_PLUS_EXPR:
3002     case MINUS_EXPR:
3003       return build2 (TREE_CODE (exp), TREE_TYPE (exp),
3004                      copy_constant (TREE_OPERAND (exp, 0)),
3005                      copy_constant (TREE_OPERAND (exp, 1)));
3006
3007     CASE_CONVERT:
3008     case VIEW_CONVERT_EXPR:
3009       return build1 (TREE_CODE (exp), TREE_TYPE (exp),
3010                      copy_constant (TREE_OPERAND (exp, 0)));
3011
3012     case VECTOR_CST:
3013       return build_vector (TREE_TYPE (exp),
3014                            copy_list (TREE_VECTOR_CST_ELTS (exp)));
3015
3016     case CONSTRUCTOR:
3017       {
3018         tree copy = copy_node (exp);
3019         VEC(constructor_elt, gc) *v;
3020         unsigned HOST_WIDE_INT idx;
3021         tree purpose, value;
3022
3023         v = VEC_alloc(constructor_elt, gc, VEC_length(constructor_elt,
3024                                                       CONSTRUCTOR_ELTS (exp)));
3025         FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, purpose, value)
3026           {
3027             constructor_elt *ce = VEC_quick_push (constructor_elt, v, NULL);
3028             ce->index = purpose;
3029             ce->value = copy_constant (value);
3030           }
3031         CONSTRUCTOR_ELTS (copy) = v;
3032         return copy;
3033       }
3034
3035     default:
3036       gcc_unreachable ();
3037     }
3038 }
3039 \f
3040 /* Return the section into which constant EXP should be placed.  */
3041
3042 static section *
3043 get_constant_section (tree exp, unsigned int align)
3044 {
3045   return targetm.asm_out.select_section (exp,
3046                                          compute_reloc_for_constant (exp),
3047                                          align);
3048 }
3049
3050 /* Return the size of constant EXP in bytes.  */
3051
3052 static HOST_WIDE_INT
3053 get_constant_size (tree exp)
3054 {
3055   HOST_WIDE_INT size;
3056
3057   size = int_size_in_bytes (TREE_TYPE (exp));
3058   if (TREE_CODE (exp) == STRING_CST)
3059     size = MAX (TREE_STRING_LENGTH (exp), size);
3060   return size;
3061 }
3062
3063 /* Subroutine of output_constant_def:
3064    No constant equal to EXP is known to have been output.
3065    Make a constant descriptor to enter EXP in the hash table.
3066    Assign the label number and construct RTL to refer to the
3067    constant's location in memory.
3068    Caller is responsible for updating the hash table.  */
3069
3070 static struct constant_descriptor_tree *
3071 build_constant_desc (tree exp)
3072 {
3073   struct constant_descriptor_tree *desc;
3074   rtx symbol, rtl;
3075   char label[256];
3076   int labelno;
3077   tree decl;
3078
3079   desc = ggc_alloc_constant_descriptor_tree ();
3080   desc->value = copy_constant (exp);
3081
3082   /* Propagate marked-ness to copied constant.  */
3083   if (flag_mudflap && mf_marked_p (exp))
3084     mf_mark (desc->value);
3085
3086   /* Create a string containing the label name, in LABEL.  */
3087   labelno = const_labelno++;
3088   ASM_GENERATE_INTERNAL_LABEL (label, "LC", labelno);
3089
3090   /* Construct the VAR_DECL associated with the constant.  */
3091   decl = build_decl (UNKNOWN_LOCATION, VAR_DECL, get_identifier (label),
3092                      TREE_TYPE (exp));
3093   DECL_ARTIFICIAL (decl) = 1;
3094   DECL_IGNORED_P (decl) = 1;
3095   TREE_READONLY (decl) = 1;
3096   TREE_STATIC (decl) = 1;
3097   TREE_ADDRESSABLE (decl) = 1;
3098   /* We don't set the RTL yet as this would cause varpool to assume that the
3099      variable is referenced.  Moreover, it would just be dropped in LTO mode.
3100      Instead we set the flag that will be recognized in make_decl_rtl.  */
3101   DECL_IN_CONSTANT_POOL (decl) = 1;
3102   DECL_INITIAL (decl) = desc->value;
3103   /* ??? CONSTANT_ALIGNMENT hasn't been updated for vector types on most
3104      architectures so use DATA_ALIGNMENT as well, except for strings.  */
3105   if (TREE_CODE (exp) == STRING_CST)
3106     {
3107 #ifdef CONSTANT_ALIGNMENT
3108       DECL_ALIGN (decl) = CONSTANT_ALIGNMENT (exp, DECL_ALIGN (decl));
3109 #endif
3110     }
3111   else
3112     align_variable (decl, 0);
3113
3114   /* Now construct the SYMBOL_REF and the MEM.  */
3115   if (use_object_blocks_p ())
3116     {
3117       section *sect = get_constant_section (exp, DECL_ALIGN (decl));
3118       symbol = create_block_symbol (ggc_strdup (label),
3119                                     get_block_for_section (sect), -1);
3120     }
3121   else
3122     symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3123   SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3124   SET_SYMBOL_REF_DECL (symbol, decl);
3125   TREE_CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3126
3127   rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (exp)), symbol);
3128   set_mem_attributes (rtl, exp, 1);
3129   set_mem_alias_set (rtl, 0);
3130   set_mem_alias_set (rtl, const_alias_set);
3131
3132   /* We cannot share RTX'es in pool entries.
3133      Mark this piece of RTL as required for unsharing.  */
3134   RTX_FLAG (rtl, used) = 1;
3135
3136   /* Set flags or add text to the name to record information, such as
3137      that it is a local symbol.  If the name is changed, the macro
3138      ASM_OUTPUT_LABELREF will have to know how to strip this
3139      information.  This call might invalidate our local variable
3140      SYMBOL; we can't use it afterward.  */
3141   targetm.encode_section_info (exp, rtl, true);
3142
3143   desc->rtl = rtl;
3144
3145   return desc;
3146 }
3147
3148 /* Return an rtx representing a reference to constant data in memory
3149    for the constant expression EXP.
3150
3151    If assembler code for such a constant has already been output,
3152    return an rtx to refer to it.
3153    Otherwise, output such a constant in memory
3154    and generate an rtx for it.
3155
3156    If DEFER is nonzero, this constant can be deferred and output only
3157    if referenced in the function after all optimizations.
3158
3159    `const_desc_table' records which constants already have label strings.  */
3160
3161 rtx
3162 output_constant_def (tree exp, int defer)
3163 {
3164   struct constant_descriptor_tree *desc;
3165   struct constant_descriptor_tree key;
3166   void **loc;
3167
3168   /* Look up EXP in the table of constant descriptors.  If we didn't find
3169      it, create a new one.  */
3170   key.value = exp;
3171   key.hash = const_hash_1 (exp);
3172   loc = htab_find_slot_with_hash (const_desc_htab, &key, key.hash, INSERT);
3173
3174   desc = (struct constant_descriptor_tree *) *loc;
3175   if (desc == 0)
3176     {
3177       desc = build_constant_desc (exp);
3178       desc->hash = key.hash;
3179       *loc = desc;
3180     }
3181
3182   maybe_output_constant_def_contents (desc, defer);
3183   return desc->rtl;
3184 }
3185
3186 /* Subroutine of output_constant_def: Decide whether or not we need to
3187    output the constant DESC now, and if so, do it.  */
3188 static void
3189 maybe_output_constant_def_contents (struct constant_descriptor_tree *desc,
3190                                     int defer)
3191 {
3192   rtx symbol = XEXP (desc->rtl, 0);
3193   tree exp = desc->value;
3194
3195   if (flag_syntax_only)
3196     return;
3197
3198   if (TREE_ASM_WRITTEN (exp))
3199     /* Already output; don't do it again.  */
3200     return;
3201
3202   /* We can always defer constants as long as the context allows
3203      doing so.  */
3204   if (defer)
3205     {
3206       /* Increment n_deferred_constants if it exists.  It needs to be at
3207          least as large as the number of constants actually referred to
3208          by the function.  If it's too small we'll stop looking too early
3209          and fail to emit constants; if it's too large we'll only look
3210          through the entire function when we could have stopped earlier.  */
3211       if (cfun)
3212         n_deferred_constants++;
3213       return;
3214     }
3215
3216   output_constant_def_contents (symbol);
3217 }
3218
3219 /* Subroutine of output_constant_def_contents.  Output the definition
3220    of constant EXP, which is pointed to by label LABEL.  ALIGN is the
3221    constant's alignment in bits.  */
3222
3223 static void
3224 assemble_constant_contents (tree exp, const char *label, unsigned int align)
3225 {
3226   HOST_WIDE_INT size;
3227
3228   size = get_constant_size (exp);
3229
3230   /* Do any machine/system dependent processing of the constant.  */
3231   targetm.asm_out.declare_constant_name (asm_out_file, label, exp, size);
3232
3233   /* Output the value of EXP.  */
3234   output_constant (exp, size, align);
3235 }
3236
3237 /* We must output the constant data referred to by SYMBOL; do so.  */
3238
3239 static void
3240 output_constant_def_contents (rtx symbol)
3241 {
3242   tree decl = SYMBOL_REF_DECL (symbol);
3243   tree exp = DECL_INITIAL (decl);
3244   unsigned int align;
3245
3246   /* Make sure any other constants whose addresses appear in EXP
3247      are assigned label numbers.  */
3248   output_addressed_constants (exp);
3249
3250   /* We are no longer deferring this constant.  */
3251   TREE_ASM_WRITTEN (decl) = TREE_ASM_WRITTEN (exp) = 1;
3252
3253   /* If the constant is part of an object block, make sure that the
3254      decl has been positioned within its block, but do not write out
3255      its definition yet.  output_object_blocks will do that later.  */
3256   if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
3257     place_block_symbol (symbol);
3258   else
3259     {
3260       align = DECL_ALIGN (decl);
3261       switch_to_section (get_constant_section (exp, align));
3262       if (align > BITS_PER_UNIT)
3263         ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
3264       assemble_constant_contents (exp, XSTR (symbol, 0), align);
3265     }
3266   if (flag_mudflap)
3267     mudflap_enqueue_constant (exp);
3268 }
3269
3270 /* Look up EXP in the table of constant descriptors.  Return the rtl
3271    if it has been emitted, else null.  */
3272
3273 rtx
3274 lookup_constant_def (tree exp)
3275 {
3276   struct constant_descriptor_tree *desc;
3277   struct constant_descriptor_tree key;
3278
3279   key.value = exp;
3280   key.hash = const_hash_1 (exp);
3281   desc = (struct constant_descriptor_tree *)
3282     htab_find_with_hash (const_desc_htab, &key, key.hash);
3283
3284   return (desc ? desc->rtl : NULL_RTX);
3285 }
3286
3287 /* Return a tree representing a reference to constant data in memory
3288    for the constant expression EXP.
3289
3290    This is the counterpart of output_constant_def at the Tree level.  */
3291
3292 tree
3293 tree_output_constant_def (tree exp)
3294 {
3295   struct constant_descriptor_tree *desc, key;
3296   void **loc;
3297   tree decl;
3298
3299   /* Look up EXP in the table of constant descriptors.  If we didn't find
3300      it, create a new one.  */
3301   key.value = exp;
3302   key.hash = const_hash_1 (exp);
3303   loc = htab_find_slot_with_hash (const_desc_htab, &key, key.hash, INSERT);
3304
3305   desc = (struct constant_descriptor_tree *) *loc;
3306   if (desc == 0)
3307     {
3308       desc = build_constant_desc (exp);
3309       desc->hash = key.hash;
3310       *loc = desc;
3311     }
3312
3313   decl = SYMBOL_REF_DECL (XEXP (desc->rtl, 0));
3314   varpool_finalize_decl (decl);
3315   return decl;
3316 }
3317 \f
3318 /* Used in the hash tables to avoid outputting the same constant
3319    twice.  Unlike 'struct constant_descriptor_tree', RTX constants
3320    are output once per function, not once per file.  */
3321 /* ??? Only a few targets need per-function constant pools.  Most
3322    can use one per-file pool.  Should add a targetm bit to tell the
3323    difference.  */
3324
3325 struct GTY(()) rtx_constant_pool {
3326   /* Pointers to first and last constant in pool, as ordered by offset.  */
3327   struct constant_descriptor_rtx *first;
3328   struct constant_descriptor_rtx *last;
3329
3330   /* Hash facility for making memory-constants from constant rtl-expressions.
3331      It is used on RISC machines where immediate integer arguments and
3332      constant addresses are restricted so that such constants must be stored
3333      in memory.  */
3334   htab_t GTY((param_is (struct constant_descriptor_rtx))) const_rtx_htab;
3335
3336   /* Current offset in constant pool (does not include any
3337      machine-specific header).  */
3338   HOST_WIDE_INT offset;
3339 };
3340
3341 struct GTY((chain_next ("%h.next"))) constant_descriptor_rtx {
3342   struct constant_descriptor_rtx *next;
3343   rtx mem;
3344   rtx sym;
3345   rtx constant;
3346   HOST_WIDE_INT offset;
3347   hashval_t hash;
3348   enum machine_mode mode;
3349   unsigned int align;
3350   int labelno;
3351   int mark;
3352 };
3353
3354 /* Hash and compare functions for const_rtx_htab.  */
3355
3356 static hashval_t
3357 const_desc_rtx_hash (const void *ptr)
3358 {
3359   const struct constant_descriptor_rtx *const desc
3360     = (const struct constant_descriptor_rtx *) ptr;
3361   return desc->hash;
3362 }
3363
3364 static int
3365 const_desc_rtx_eq (const void *a, const void *b)
3366 {
3367   const struct constant_descriptor_rtx *const x
3368     = (const struct constant_descriptor_rtx *) a;
3369   const struct constant_descriptor_rtx *const y
3370     = (const struct constant_descriptor_rtx *) b;
3371
3372   if (x->mode != y->mode)
3373     return 0;
3374   return rtx_equal_p (x->constant, y->constant);
3375 }
3376
3377 /* This is the worker function for const_rtx_hash, called via for_each_rtx.  */
3378
3379 static int
3380 const_rtx_hash_1 (rtx *xp, void *data)
3381 {
3382   unsigned HOST_WIDE_INT hwi;
3383   enum machine_mode mode;
3384   enum rtx_code code;
3385   hashval_t h, *hp;
3386   rtx x;
3387
3388   x = *xp;
3389   code = GET_CODE (x);
3390   mode = GET_MODE (x);
3391   h = (hashval_t) code * 1048573 + mode;
3392
3393   switch (code)
3394     {
3395     case CONST_INT:
3396       hwi = INTVAL (x);
3397     fold_hwi:
3398       {
3399         int shift = sizeof (hashval_t) * CHAR_BIT;
3400         const int n = sizeof (HOST_WIDE_INT) / sizeof (hashval_t);
3401         int i;
3402
3403         h ^= (hashval_t) hwi;
3404         for (i = 1; i < n; ++i)
3405           {
3406             hwi >>= shift;
3407             h ^= (hashval_t) hwi;
3408           }
3409       }
3410       break;
3411
3412     case CONST_DOUBLE:
3413       if (mode == VOIDmode)
3414         {
3415           hwi = CONST_DOUBLE_LOW (x) ^ CONST_DOUBLE_HIGH (x);
3416           goto fold_hwi;
3417         }
3418       else
3419         h ^= real_hash (CONST_DOUBLE_REAL_VALUE (x));
3420       break;
3421
3422     case CONST_FIXED:
3423       h ^= fixed_hash (CONST_FIXED_VALUE (x));
3424       break;
3425
3426     case CONST_VECTOR:
3427       {
3428         int i;
3429         for (i = XVECLEN (x, 0); i-- > 0; )
3430           h = h * 251 + const_rtx_hash_1 (&XVECEXP (x, 0, i), data);
3431       }
3432       break;
3433
3434     case SYMBOL_REF:
3435       h ^= htab_hash_string (XSTR (x, 0));
3436       break;
3437
3438     case LABEL_REF:
3439       h = h * 251 + CODE_LABEL_NUMBER (XEXP (x, 0));
3440       break;
3441
3442     case UNSPEC:
3443     case UNSPEC_VOLATILE:
3444       h = h * 251 + XINT (x, 1);
3445       break;
3446
3447     default:
3448       break;
3449     }
3450
3451   hp = (hashval_t *) data;
3452   *hp = *hp * 509 + h;
3453   return 0;
3454 }
3455
3456 /* Compute a hash value for X, which should be a constant.  */
3457
3458 static hashval_t
3459 const_rtx_hash (rtx x)
3460 {
3461   hashval_t h = 0;
3462   for_each_rtx (&x, const_rtx_hash_1, &h);
3463   return h;
3464 }
3465
3466 \f
3467 /* Create and return a new rtx constant pool.  */
3468
3469 static struct rtx_constant_pool *
3470 create_constant_pool (void)
3471 {
3472   struct rtx_constant_pool *pool;
3473
3474   pool = ggc_alloc_rtx_constant_pool ();
3475   pool->const_rtx_htab = htab_create_ggc (31, const_desc_rtx_hash,
3476                                           const_desc_rtx_eq, NULL);
3477   pool->first = NULL;
3478   pool->last = NULL;
3479   pool->offset = 0;
3480   return pool;
3481 }
3482
3483 /* Initialize constant pool hashing for a new function.  */
3484
3485 void
3486 init_varasm_status (void)
3487 {
3488   crtl->varasm.pool = create_constant_pool ();
3489   crtl->varasm.deferred_constants = 0;
3490 }
3491 \f
3492 /* Given a MINUS expression, simplify it if both sides
3493    include the same symbol.  */
3494
3495 rtx
3496 simplify_subtraction (rtx x)
3497 {
3498   rtx r = simplify_rtx (x);
3499   return r ? r : x;
3500 }
3501 \f
3502 /* Given a constant rtx X, make (or find) a memory constant for its value
3503    and return a MEM rtx to refer to it in memory.  */
3504
3505 rtx
3506 force_const_mem (enum machine_mode mode, rtx x)
3507 {
3508   struct constant_descriptor_rtx *desc, tmp;
3509   struct rtx_constant_pool *pool;
3510   char label[256];
3511   rtx def, symbol;
3512   hashval_t hash;
3513   unsigned int align;
3514   void **slot;
3515
3516   /* If we're not allowed to drop X into the constant pool, don't.  */
3517   if (targetm.cannot_force_const_mem (x))
3518     return NULL_RTX;
3519
3520   /* Record that this function has used a constant pool entry.  */
3521   crtl->uses_const_pool = 1;
3522
3523   /* Decide which pool to use.  */
3524   pool = (targetm.use_blocks_for_constant_p (mode, x)
3525           ? shared_constant_pool
3526           : crtl->varasm.pool);
3527
3528   /* Lookup the value in the hashtable.  */
3529   tmp.constant = x;
3530   tmp.mode = mode;
3531   hash = const_rtx_hash (x);
3532   slot = htab_find_slot_with_hash (pool->const_rtx_htab, &tmp, hash, INSERT);
3533   desc = (struct constant_descriptor_rtx *) *slot;
3534
3535   /* If the constant was already present, return its memory.  */
3536   if (desc)
3537     return copy_rtx (desc->mem);
3538
3539   /* Otherwise, create a new descriptor.  */
3540   desc = ggc_alloc_constant_descriptor_rtx ();
3541   *slot = desc;
3542
3543   /* Align the location counter as required by EXP's data type.  */
3544   align = GET_MODE_ALIGNMENT (mode == VOIDmode ? word_mode : mode);
3545 #ifdef CONSTANT_ALIGNMENT
3546   {
3547     tree type = lang_hooks.types.type_for_mode (mode, 0);
3548     if (type != NULL_TREE)
3549       align = CONSTANT_ALIGNMENT (make_tree (type, x), align);
3550   }
3551 #endif
3552
3553   pool->offset += (align / BITS_PER_UNIT) - 1;
3554   pool->offset &= ~ ((align / BITS_PER_UNIT) - 1);
3555
3556   desc->next = NULL;
3557   desc->constant = copy_rtx (tmp.constant);
3558   desc->offset = pool->offset;
3559   desc->hash = hash;
3560   desc->mode = mode;
3561   desc->align = align;
3562   desc->labelno = const_labelno;
3563   desc->mark = 0;
3564
3565   pool->offset += GET_MODE_SIZE (mode);
3566   if (pool->last)
3567     pool->last->next = desc;
3568   else
3569     pool->first = pool->last = desc;
3570   pool->last = desc;
3571
3572   /* Create a string containing the label name, in LABEL.  */
3573   ASM_GENERATE_INTERNAL_LABEL (label, "LC", const_labelno);
3574   ++const_labelno;
3575
3576   /* Construct the SYMBOL_REF.  Make sure to mark it as belonging to
3577      the constants pool.  */
3578   if (use_object_blocks_p () && targetm.use_blocks_for_constant_p (mode, x))
3579     {
3580       section *sect = targetm.asm_out.select_rtx_section (mode, x, align);
3581       symbol = create_block_symbol (ggc_strdup (label),
3582                                     get_block_for_section (sect), -1);
3583     }
3584   else
3585     symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3586   desc->sym = symbol;
3587   SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3588   CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3589   SET_SYMBOL_REF_CONSTANT (symbol, desc);
3590
3591   /* Construct the MEM.  */
3592   desc->mem = def = gen_const_mem (mode, symbol);
3593   set_mem_attributes (def, lang_hooks.types.type_for_mode (mode, 0), 1);
3594   set_mem_align (def, align);
3595
3596   /* If we're dropping a label to the constant pool, make sure we
3597      don't delete it.  */
3598   if (GET_CODE (x) == LABEL_REF)
3599     LABEL_PRESERVE_P (XEXP (x, 0)) = 1;
3600
3601   return copy_rtx (def);
3602 }
3603 \f
3604 /* Given a constant pool SYMBOL_REF, return the corresponding constant.  */
3605
3606 rtx
3607 get_pool_constant (rtx addr)
3608 {
3609   return SYMBOL_REF_CONSTANT (addr)->constant;
3610 }
3611
3612 /* Given a constant pool SYMBOL_REF, return the corresponding constant
3613    and whether it has been output or not.  */
3614
3615 rtx
3616 get_pool_constant_mark (rtx addr, bool *pmarked)
3617 {
3618   struct constant_descriptor_rtx *desc;
3619
3620   desc = SYMBOL_REF_CONSTANT (addr);
3621   *pmarked = (desc->mark != 0);
3622   return desc->constant;
3623 }
3624
3625 /* Similar, return the mode.  */
3626
3627 enum machine_mode
3628 get_pool_mode (const_rtx addr)
3629 {
3630   return SYMBOL_REF_CONSTANT (addr)->mode;
3631 }
3632
3633 /* Return the size of the constant pool.  */
3634
3635 int
3636 get_pool_size (void)
3637 {
3638   return crtl->varasm.pool->offset;
3639 }
3640 \f
3641 /* Worker function for output_constant_pool_1.  Emit assembly for X
3642    in MODE with known alignment ALIGN.  */
3643
3644 static void
3645 output_constant_pool_2 (enum machine_mode mode, rtx x, unsigned int align)
3646 {
3647   switch (GET_MODE_CLASS (mode))
3648     {
3649     case MODE_FLOAT:
3650     case MODE_DECIMAL_FLOAT:
3651       {
3652         REAL_VALUE_TYPE r;
3653
3654         gcc_assert (GET_CODE (x) == CONST_DOUBLE);
3655         REAL_VALUE_FROM_CONST_DOUBLE (r, x);
3656         assemble_real (r, mode, align);
3657         break;
3658       }
3659
3660     case MODE_INT:
3661     case MODE_PARTIAL_INT:
3662     case MODE_FRACT:
3663     case MODE_UFRACT:
3664     case MODE_ACCUM:
3665     case MODE_UACCUM:
3666       assemble_integer (x, GET_MODE_SIZE (mode), align, 1);
3667       break;
3668
3669     case MODE_VECTOR_FLOAT:
3670     case MODE_VECTOR_INT:
3671     case MODE_VECTOR_FRACT:
3672     case MODE_VECTOR_UFRACT:
3673     case MODE_VECTOR_ACCUM:
3674     case MODE_VECTOR_UACCUM:
3675       {
3676         int i, units;
3677         enum machine_mode submode = GET_MODE_INNER (mode);
3678         unsigned int subalign = MIN (align, GET_MODE_BITSIZE (submode));
3679
3680         gcc_assert (GET_CODE (x) == CONST_VECTOR);
3681         units = CONST_VECTOR_NUNITS (x);
3682
3683         for (i = 0; i < units; i++)
3684           {
3685             rtx elt = CONST_VECTOR_ELT (x, i);
3686             output_constant_pool_2 (submode, elt, i ? subalign : align);
3687           }
3688       }
3689       break;
3690
3691     default:
3692       gcc_unreachable ();
3693     }
3694 }
3695
3696 /* Worker function for output_constant_pool.  Emit constant DESC,
3697    giving it ALIGN bits of alignment.  */
3698
3699 static void
3700 output_constant_pool_1 (struct constant_descriptor_rtx *desc,
3701                         unsigned int align)
3702 {
3703   rtx x, tmp;
3704
3705   x = desc->constant;
3706
3707   /* See if X is a LABEL_REF (or a CONST referring to a LABEL_REF)
3708      whose CODE_LABEL has been deleted.  This can occur if a jump table
3709      is eliminated by optimization.  If so, write a constant of zero
3710      instead.  Note that this can also happen by turning the
3711      CODE_LABEL into a NOTE.  */
3712   /* ??? This seems completely and utterly wrong.  Certainly it's
3713      not true for NOTE_INSN_DELETED_LABEL, but I disbelieve proper
3714      functioning even with INSN_DELETED_P and friends.  */
3715
3716   tmp = x;
3717   switch (GET_CODE (tmp))
3718     {
3719     case CONST:
3720       if (GET_CODE (XEXP (tmp, 0)) != PLUS
3721           || GET_CODE (XEXP (XEXP (tmp, 0), 0)) != LABEL_REF)
3722         break;
3723       tmp = XEXP (XEXP (tmp, 0), 0);
3724       /* FALLTHRU  */
3725
3726     case LABEL_REF:
3727       tmp = XEXP (tmp, 0);
3728       gcc_assert (!INSN_DELETED_P (tmp));
3729       gcc_assert (!NOTE_P (tmp)
3730                   || NOTE_KIND (tmp) != NOTE_INSN_DELETED);
3731       break;
3732
3733     default:
3734       break;
3735     }
3736
3737 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3738   ASM_OUTPUT_SPECIAL_POOL_ENTRY (asm_out_file, x, desc->mode,
3739                                  align, desc->labelno, done);
3740 #endif
3741
3742   assemble_align (align);
3743
3744   /* Output the label.  */
3745   targetm.asm_out.internal_label (asm_out_file, "LC", desc->labelno);
3746
3747   /* Output the data.  */
3748   output_constant_pool_2 (desc->mode, x, align);
3749
3750   /* Make sure all constants in SECTION_MERGE and not SECTION_STRINGS
3751      sections have proper size.  */
3752   if (align > GET_MODE_BITSIZE (desc->mode)
3753       && in_section
3754       && (in_section->common.flags & SECTION_MERGE))
3755     assemble_align (align);
3756
3757 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3758  done:
3759 #endif
3760   return;
3761 }
3762
3763 /* Given a SYMBOL_REF CURRENT_RTX, mark it and all constants it refers
3764    to as used.  Emit referenced deferred strings.  This function can
3765    be used with for_each_rtx to mark all SYMBOL_REFs in an rtx.  */
3766
3767 static int
3768 mark_constant (rtx *current_rtx, void *data ATTRIBUTE_UNUSED)
3769 {
3770   rtx x = *current_rtx;
3771
3772   if (x == NULL_RTX || GET_CODE (x) != SYMBOL_REF)
3773     return 0;
3774
3775   if (CONSTANT_POOL_ADDRESS_P (x))
3776     {
3777       struct constant_descriptor_rtx *desc = SYMBOL_REF_CONSTANT (x);
3778       if (desc->mark == 0)
3779         {
3780           desc->mark = 1;
3781           for_each_rtx (&desc->constant, mark_constant, NULL);
3782         }
3783     }
3784   else if (TREE_CONSTANT_POOL_ADDRESS_P (x))
3785     {
3786       tree decl = SYMBOL_REF_DECL (x);
3787       if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
3788         {
3789           n_deferred_constants--;
3790           output_constant_def_contents (x);
3791         }
3792     }
3793
3794   return -1;
3795 }
3796
3797 /* Look through appropriate parts of INSN, marking all entries in the
3798    constant pool which are actually being used.  Entries that are only
3799    referenced by other constants are also marked as used.  Emit
3800    deferred strings that are used.  */
3801
3802 static void
3803 mark_constants (rtx insn)
3804 {
3805   if (!INSN_P (insn))
3806     return;
3807
3808   /* Insns may appear inside a SEQUENCE.  Only check the patterns of
3809      insns, not any notes that may be attached.  We don't want to mark
3810      a constant just because it happens to appear in a REG_EQUIV note.  */
3811   if (GET_CODE (PATTERN (insn)) == SEQUENCE)
3812     {
3813       rtx seq = PATTERN (insn);
3814       int i, n = XVECLEN (seq, 0);
3815       for (i = 0; i < n; ++i)
3816         {
3817           rtx subinsn = XVECEXP (seq, 0, i);
3818           if (INSN_P (subinsn))
3819             for_each_rtx (&PATTERN (subinsn), mark_constant, NULL);
3820         }
3821     }
3822   else
3823     for_each_rtx (&PATTERN (insn), mark_constant, NULL);
3824 }
3825
3826 /* Look through the instructions for this function, and mark all the
3827    entries in POOL which are actually being used.  Emit deferred constants
3828    which have indeed been used.  */
3829
3830 static void
3831 mark_constant_pool (void)
3832 {
3833   rtx insn, link;
3834
3835   if (!crtl->uses_const_pool && n_deferred_constants == 0)
3836     return;
3837
3838   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3839     mark_constants (insn);
3840
3841   for (link = crtl->epilogue_delay_list;
3842        link;
3843        link = XEXP (link, 1))
3844     mark_constants (XEXP (link, 0));
3845 }
3846
3847 /* Write all the constants in POOL.  */
3848
3849 static void
3850 output_constant_pool_contents (struct rtx_constant_pool *pool)
3851 {
3852   struct constant_descriptor_rtx *desc;
3853
3854   for (desc = pool->first; desc ; desc = desc->next)
3855     if (desc->mark)
3856       {
3857         /* If the constant is part of an object_block, make sure that
3858            the constant has been positioned within its block, but do not
3859            write out its definition yet.  output_object_blocks will do
3860            that later.  */
3861         if (SYMBOL_REF_HAS_BLOCK_INFO_P (desc->sym)
3862             && SYMBOL_REF_BLOCK (desc->sym))
3863           place_block_symbol (desc->sym);
3864         else
3865           {
3866             switch_to_section (targetm.asm_out.select_rtx_section
3867                                (desc->mode, desc->constant, desc->align));
3868             output_constant_pool_1 (desc, desc->align);
3869           }
3870       }
3871 }
3872
3873 /* Mark all constants that are used in the current function, then write
3874    out the function's private constant pool.  */
3875
3876 static void
3877 output_constant_pool (const char *fnname ATTRIBUTE_UNUSED,
3878                       tree fndecl ATTRIBUTE_UNUSED)
3879 {
3880   struct rtx_constant_pool *pool = crtl->varasm.pool;
3881
3882   /* It is possible for gcc to call force_const_mem and then to later
3883      discard the instructions which refer to the constant.  In such a
3884      case we do not need to output the constant.  */
3885   mark_constant_pool ();
3886
3887 #ifdef ASM_OUTPUT_POOL_PROLOGUE
3888   ASM_OUTPUT_POOL_PROLOGUE (asm_out_file, fnname, fndecl, pool->offset);
3889 #endif
3890
3891   output_constant_pool_contents (pool);
3892
3893 #ifdef ASM_OUTPUT_POOL_EPILOGUE
3894   ASM_OUTPUT_POOL_EPILOGUE (asm_out_file, fnname, fndecl, pool->offset);
3895 #endif
3896 }
3897 \f
3898 /* Write the contents of the shared constant pool.  */
3899
3900 void
3901 output_shared_constant_pool (void)
3902 {
3903   output_constant_pool_contents (shared_constant_pool);
3904 }
3905 \f
3906 /* Determine what kind of relocations EXP may need.  */
3907
3908 int
3909 compute_reloc_for_constant (tree exp)
3910 {
3911   int reloc = 0, reloc2;
3912   tree tem;
3913
3914   switch (TREE_CODE (exp))
3915     {
3916     case ADDR_EXPR:
3917     case FDESC_EXPR:
3918       /* Go inside any operations that get_inner_reference can handle and see
3919          if what's inside is a constant: no need to do anything here for
3920          addresses of variables or functions.  */
3921       for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
3922            tem = TREE_OPERAND (tem, 0))
3923         ;
3924
3925       if (TREE_PUBLIC (tem))
3926         reloc |= 2;
3927       else
3928         reloc |= 1;
3929       break;
3930
3931     case PLUS_EXPR:
3932     case POINTER_PLUS_EXPR:
3933       reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3934       reloc |= compute_reloc_for_constant (TREE_OPERAND (exp, 1));
3935       break;
3936
3937     case MINUS_EXPR:
3938       reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3939       reloc2 = compute_reloc_for_constant (TREE_OPERAND (exp, 1));
3940       /* The difference of two local labels is computable at link time.  */
3941       if (reloc == 1 && reloc2 == 1)
3942         reloc = 0;
3943       else
3944         reloc |= reloc2;
3945       break;
3946
3947     CASE_CONVERT:
3948     case VIEW_CONVERT_EXPR:
3949       reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3950       break;
3951
3952     case CONSTRUCTOR:
3953       {
3954         unsigned HOST_WIDE_INT idx;
3955         FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
3956           if (tem != 0)
3957             reloc |= compute_reloc_for_constant (tem);
3958       }
3959       break;
3960
3961     default:
3962       break;
3963     }
3964   return reloc;
3965 }
3966
3967 /* Find all the constants whose addresses are referenced inside of EXP,
3968    and make sure assembler code with a label has been output for each one.
3969    Indicate whether an ADDR_EXPR has been encountered.  */
3970
3971 static void
3972 output_addressed_constants (tree exp)
3973 {
3974   tree tem;
3975
3976   switch (TREE_CODE (exp))
3977     {
3978     case ADDR_EXPR:
3979     case FDESC_EXPR:
3980       /* Go inside any operations that get_inner_reference can handle and see
3981          if what's inside is a constant: no need to do anything here for
3982          addresses of variables or functions.  */
3983       for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
3984            tem = TREE_OPERAND (tem, 0))
3985         ;
3986
3987       /* If we have an initialized CONST_DECL, retrieve the initializer.  */
3988       if (TREE_CODE (tem) == CONST_DECL && DECL_INITIAL (tem))
3989         tem = DECL_INITIAL (tem);
3990
3991       if (CONSTANT_CLASS_P (tem) || TREE_CODE (tem) == CONSTRUCTOR)
3992         output_constant_def (tem, 0);
3993       break;
3994
3995     case PLUS_EXPR:
3996     case POINTER_PLUS_EXPR:
3997     case MINUS_EXPR:
3998       output_addressed_constants (TREE_OPERAND (exp, 1));
3999       /* Fall through.  */
4000
4001     CASE_CONVERT:
4002     case VIEW_CONVERT_EXPR:
4003       output_addressed_constants (TREE_OPERAND (exp, 0));
4004       break;
4005
4006     case CONSTRUCTOR:
4007       {
4008         unsigned HOST_WIDE_INT idx;
4009         FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
4010           if (tem != 0)
4011             output_addressed_constants (tem);
4012       }
4013       break;
4014
4015     default:
4016       break;
4017     }
4018 }
4019 \f
4020 /* Whether a constructor CTOR is a valid static constant initializer if all
4021    its elements are.  This used to be internal to initializer_constant_valid_p
4022    and has been exposed to let other functions like categorize_ctor_elements
4023    evaluate the property while walking a constructor for other purposes.  */
4024
4025 bool
4026 constructor_static_from_elts_p (const_tree ctor)
4027 {
4028   return (TREE_CONSTANT (ctor)
4029           && (TREE_CODE (TREE_TYPE (ctor)) == UNION_TYPE
4030               || TREE_CODE (TREE_TYPE (ctor)) == RECORD_TYPE
4031               || TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE));
4032 }
4033
4034 static tree initializer_constant_valid_p_1 (tree value, tree endtype,
4035                                             tree *cache);
4036
4037 /* A subroutine of initializer_constant_valid_p.  VALUE is a MINUS_EXPR,
4038    PLUS_EXPR or POINTER_PLUS_EXPR.  This looks for cases of VALUE
4039    which are valid when ENDTYPE is an integer of any size; in
4040    particular, this does not accept a pointer minus a constant.  This
4041    returns null_pointer_node if the VALUE is an absolute constant
4042    which can be used to initialize a static variable.  Otherwise it
4043    returns NULL.  */
4044
4045 static tree
4046 narrowing_initializer_constant_valid_p (tree value, tree endtype, tree *cache)
4047 {
4048   tree op0, op1;
4049
4050   if (!INTEGRAL_TYPE_P (endtype))
4051     return NULL_TREE;
4052
4053   op0 = TREE_OPERAND (value, 0);
4054   op1 = TREE_OPERAND (value, 1);
4055
4056   /* Like STRIP_NOPS except allow the operand mode to widen.  This
4057      works around a feature of fold that simplifies (int)(p1 - p2) to
4058      ((int)p1 - (int)p2) under the theory that the narrower operation
4059      is cheaper.  */
4060
4061   while (CONVERT_EXPR_P (op0)
4062          || TREE_CODE (op0) == NON_LVALUE_EXPR)
4063     {
4064       tree inner = TREE_OPERAND (op0, 0);
4065       if (inner == error_mark_node
4066           || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4067           || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))
4068               > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
4069         break;
4070       op0 = inner;
4071     }
4072
4073   while (CONVERT_EXPR_P (op1)
4074          || TREE_CODE (op1) == NON_LVALUE_EXPR)
4075     {
4076       tree inner = TREE_OPERAND (op1, 0);
4077       if (inner == error_mark_node
4078           || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4079           || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op1)))
4080               > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
4081         break;
4082       op1 = inner;
4083     }
4084
4085   op0 = initializer_constant_valid_p_1 (op0, endtype, cache);
4086   if (!op0)
4087     return NULL_TREE;
4088
4089   op1 = initializer_constant_valid_p_1 (op1, endtype,
4090                                         cache ? cache + 2 : NULL);
4091   /* Both initializers must be known.  */
4092   if (op1)
4093     {
4094       if (op0 == op1
4095           && (op0 == null_pointer_node
4096               || TREE_CODE (value) == MINUS_EXPR))
4097         return null_pointer_node;
4098
4099       /* Support differences between labels.  */
4100       if (TREE_CODE (op0) == LABEL_DECL
4101           && TREE_CODE (op1) == LABEL_DECL)
4102         return null_pointer_node;
4103
4104       if (TREE_CODE (op0) == STRING_CST
4105           && TREE_CODE (op1) == STRING_CST
4106           && operand_equal_p (op0, op1, 1))
4107         return null_pointer_node;
4108     }
4109
4110   return NULL_TREE;
4111 }
4112
4113 /* Helper function of initializer_constant_valid_p.
4114    Return nonzero if VALUE is a valid constant-valued expression
4115    for use in initializing a static variable; one that can be an
4116    element of a "constant" initializer.
4117
4118    Return null_pointer_node if the value is absolute;
4119    if it is relocatable, return the variable that determines the relocation.
4120    We assume that VALUE has been folded as much as possible;
4121    therefore, we do not need to check for such things as
4122    arithmetic-combinations of integers.
4123
4124    Use CACHE (pointer to 2 tree values) for caching if non-NULL.  */
4125
4126 static tree
4127 initializer_constant_valid_p_1 (tree value, tree endtype, tree *cache)
4128 {
4129   tree ret;
4130
4131   switch (TREE_CODE (value))
4132     {
4133     case CONSTRUCTOR:
4134       if (constructor_static_from_elts_p (value))
4135         {
4136           unsigned HOST_WIDE_INT idx;
4137           tree elt;
4138           bool absolute = true;
4139
4140           if (cache && cache[0] == value)
4141             return cache[1];
4142           FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
4143             {
4144               tree reloc;
4145               reloc = initializer_constant_valid_p_1 (elt, TREE_TYPE (elt),
4146                                                       NULL);
4147               if (!reloc)
4148                 {
4149                   if (cache)
4150                     {
4151                       cache[0] = value;
4152                       cache[1] = NULL_TREE;
4153                     }
4154                   return NULL_TREE;
4155                 }
4156               if (reloc != null_pointer_node)
4157                 absolute = false;
4158             }
4159           /* For a non-absolute relocation, there is no single
4160              variable that can be "the variable that determines the
4161              relocation."  */
4162           if (cache)
4163             {
4164               cache[0] = value;
4165               cache[1] = absolute ? null_pointer_node : error_mark_node;
4166             }
4167           return absolute ? null_pointer_node : error_mark_node;
4168         }
4169
4170       return TREE_STATIC (value) ? null_pointer_node : NULL_TREE;
4171
4172     case INTEGER_CST:
4173     case VECTOR_CST:
4174     case REAL_CST:
4175     case FIXED_CST:
4176     case STRING_CST:
4177     case COMPLEX_CST:
4178       return null_pointer_node;
4179
4180     case ADDR_EXPR:
4181     case FDESC_EXPR:
4182       {
4183         tree op0 = staticp (TREE_OPERAND (value, 0));
4184         if (op0)
4185           {
4186             /* "&(*a).f" is like unto pointer arithmetic.  If "a" turns out
4187                to be a constant, this is old-skool offsetof-like nonsense.  */
4188             if (TREE_CODE (op0) == INDIRECT_REF
4189                 && TREE_CONSTANT (TREE_OPERAND (op0, 0)))
4190               return null_pointer_node;
4191             /* Taking the address of a nested function involves a trampoline,
4192                unless we don't need or want one.  */
4193             if (TREE_CODE (op0) == FUNCTION_DECL
4194                 && DECL_STATIC_CHAIN (op0)
4195                 && !TREE_NO_TRAMPOLINE (value))
4196               return NULL_TREE;
4197             /* "&{...}" requires a temporary to hold the constructed
4198                object.  */
4199             if (TREE_CODE (op0) == CONSTRUCTOR)
4200               return NULL_TREE;
4201           }
4202         return op0;
4203       }
4204
4205     case NON_LVALUE_EXPR:
4206       return initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4207                                              endtype, cache);
4208
4209     case VIEW_CONVERT_EXPR:
4210       {
4211         tree src = TREE_OPERAND (value, 0);
4212         tree src_type = TREE_TYPE (src);
4213         tree dest_type = TREE_TYPE (value);
4214
4215         /* Allow view-conversions from aggregate to non-aggregate type only
4216            if the bit pattern is fully preserved afterwards; otherwise, the
4217            RTL expander won't be able to apply a subsequent transformation
4218            to the underlying constructor.  */
4219         if (AGGREGATE_TYPE_P (src_type) && !AGGREGATE_TYPE_P (dest_type))
4220           {
4221             if (TYPE_MODE (endtype) == TYPE_MODE (dest_type))
4222               return initializer_constant_valid_p_1 (src, endtype, cache);
4223             else
4224               return NULL_TREE;
4225           }
4226
4227         /* Allow all other kinds of view-conversion.  */
4228         return initializer_constant_valid_p_1 (src, endtype, cache);
4229       }
4230
4231     CASE_CONVERT:
4232       {
4233         tree src = TREE_OPERAND (value, 0);
4234         tree src_type = TREE_TYPE (src);
4235         tree dest_type = TREE_TYPE (value);
4236
4237         /* Allow conversions between pointer types, floating-point
4238            types, and offset types.  */
4239         if ((POINTER_TYPE_P (dest_type) && POINTER_TYPE_P (src_type))
4240             || (FLOAT_TYPE_P (dest_type) && FLOAT_TYPE_P (src_type))
4241             || (TREE_CODE (dest_type) == OFFSET_TYPE
4242                 && TREE_CODE (src_type) == OFFSET_TYPE))
4243           return initializer_constant_valid_p_1 (src, endtype, cache);
4244
4245         /* Allow length-preserving conversions between integer types.  */
4246         if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type)
4247             && (TYPE_PRECISION (dest_type) == TYPE_PRECISION (src_type)))
4248           return initializer_constant_valid_p_1 (src, endtype, cache);
4249
4250         /* Allow conversions between other integer types only if
4251            explicit value.  */
4252         if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type))
4253           {
4254             tree inner = initializer_constant_valid_p_1 (src, endtype, cache);
4255             if (inner == null_pointer_node)
4256               return null_pointer_node;
4257             break;
4258           }
4259
4260         /* Allow (int) &foo provided int is as wide as a pointer.  */
4261         if (INTEGRAL_TYPE_P (dest_type) && POINTER_TYPE_P (src_type)
4262             && (TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type)))
4263           return initializer_constant_valid_p_1 (src, endtype, cache);
4264
4265         /* Likewise conversions from int to pointers, but also allow
4266            conversions from 0.  */
4267         if ((POINTER_TYPE_P (dest_type)
4268              || TREE_CODE (dest_type) == OFFSET_TYPE)
4269             && INTEGRAL_TYPE_P (src_type))
4270           {
4271             if (TREE_CODE (src) == INTEGER_CST
4272                 && TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type))
4273               return null_pointer_node;
4274             if (integer_zerop (src))
4275               return null_pointer_node;
4276             else if (TYPE_PRECISION (dest_type) <= TYPE_PRECISION (src_type))
4277               return initializer_constant_valid_p_1 (src, endtype, cache);
4278           }
4279
4280         /* Allow conversions to struct or union types if the value
4281            inside is okay.  */
4282         if (TREE_CODE (dest_type) == RECORD_TYPE
4283             || TREE_CODE (dest_type) == UNION_TYPE)
4284           return initializer_constant_valid_p_1 (src, endtype, cache);
4285       }
4286       break;
4287
4288     case POINTER_PLUS_EXPR:
4289     case PLUS_EXPR:
4290       /* Any valid floating-point constants will have been folded by now;
4291          with -frounding-math we hit this with addition of two constants.  */
4292       if (TREE_CODE (endtype) == REAL_TYPE)
4293         return NULL_TREE;
4294       if (cache && cache[0] == value)
4295         return cache[1];
4296       if (! INTEGRAL_TYPE_P (endtype)
4297           || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
4298         {
4299           tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
4300           tree valid0
4301             = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4302                                               endtype, ncache);
4303           tree valid1
4304             = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
4305                                               endtype, ncache + 2);
4306           /* If either term is absolute, use the other term's relocation.  */
4307           if (valid0 == null_pointer_node)
4308             ret = valid1;
4309           else if (valid1 == null_pointer_node)
4310             ret = valid0;
4311           /* Support narrowing pointer differences.  */
4312           else
4313             ret = narrowing_initializer_constant_valid_p (value, endtype,
4314                                                           ncache);
4315         }
4316       else
4317       /* Support narrowing pointer differences.  */
4318         ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
4319       if (cache)
4320         {
4321           cache[0] = value;
4322           cache[1] = ret;
4323         }
4324       return ret;
4325
4326     case MINUS_EXPR:
4327       if (TREE_CODE (endtype) == REAL_TYPE)
4328         return NULL_TREE;
4329       if (cache && cache[0] == value)
4330         return cache[1];
4331       if (! INTEGRAL_TYPE_P (endtype)
4332           || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
4333         {
4334           tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
4335           tree valid0
4336             = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4337                                               endtype, ncache);
4338           tree valid1
4339             = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
4340                                               endtype, ncache + 2);
4341           /* Win if second argument is absolute.  */
4342           if (valid1 == null_pointer_node)
4343             ret = valid0;
4344           /* Win if both arguments have the same relocation.
4345              Then the value is absolute.  */
4346           else if (valid0 == valid1 && valid0 != 0)
4347             ret = null_pointer_node;
4348           /* Since GCC guarantees that string constants are unique in the
4349              generated code, a subtraction between two copies of the same
4350              constant string is absolute.  */
4351           else if (valid0 && TREE_CODE (valid0) == STRING_CST
4352                    && valid1 && TREE_CODE (valid1) == STRING_CST
4353                    && operand_equal_p (valid0, valid1, 1))
4354             ret = null_pointer_node;
4355           /* Support narrowing differences.  */
4356           else
4357             ret = narrowing_initializer_constant_valid_p (value, endtype,
4358                                                           ncache);
4359         }
4360       else
4361         /* Support narrowing differences.  */
4362         ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
4363       if (cache)
4364         {
4365           cache[0] = value;
4366           cache[1] = ret;
4367         }
4368       return ret;
4369
4370     default:
4371       break;
4372     }
4373
4374   return NULL_TREE;
4375 }
4376
4377 /* Return nonzero if VALUE is a valid constant-valued expression
4378    for use in initializing a static variable; one that can be an
4379    element of a "constant" initializer.
4380
4381    Return null_pointer_node if the value is absolute;
4382    if it is relocatable, return the variable that determines the relocation.
4383    We assume that VALUE has been folded as much as possible;
4384    therefore, we do not need to check for such things as
4385    arithmetic-combinations of integers.  */
4386 tree
4387 initializer_constant_valid_p (tree value, tree endtype)
4388 {
4389   return initializer_constant_valid_p_1 (value, endtype, NULL);
4390 }
4391 \f
4392 /* Return true if VALUE is a valid constant-valued expression
4393    for use in initializing a static bit-field; one that can be
4394    an element of a "constant" initializer.  */
4395
4396 bool
4397 initializer_constant_valid_for_bitfield_p (tree value)
4398 {
4399   /* For bitfields we support integer constants or possibly nested aggregates
4400      of such.  */
4401   switch (TREE_CODE (value))
4402     {
4403     case CONSTRUCTOR:
4404       {
4405         unsigned HOST_WIDE_INT idx;
4406         tree elt;
4407
4408         FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
4409           if (!initializer_constant_valid_for_bitfield_p (elt))
4410             return false;
4411         return true;
4412       }
4413
4414     case INTEGER_CST:
4415       return true;
4416
4417     case VIEW_CONVERT_EXPR:
4418     case NON_LVALUE_EXPR:
4419       return
4420         initializer_constant_valid_for_bitfield_p (TREE_OPERAND (value, 0));
4421
4422     default:
4423       break;
4424     }
4425
4426   return false;
4427 }
4428
4429 /* output_constructor outer state of relevance in recursive calls, typically
4430    for nested aggregate bitfields.  */
4431
4432 typedef struct {
4433   unsigned int bit_offset;  /* current position in ...  */
4434   int byte;                 /* ... the outer byte buffer.  */
4435 } oc_outer_state;
4436
4437 static unsigned HOST_WIDE_INT
4438   output_constructor (tree, unsigned HOST_WIDE_INT, unsigned int,
4439                       oc_outer_state *);
4440
4441 /* Output assembler code for constant EXP to FILE, with no label.
4442    This includes the pseudo-op such as ".int" or ".byte", and a newline.
4443    Assumes output_addressed_constants has been done on EXP already.
4444
4445    Generate exactly SIZE bytes of assembler data, padding at the end
4446    with zeros if necessary.  SIZE must always be specified.
4447
4448    SIZE is important for structure constructors,
4449    since trailing members may have been omitted from the constructor.
4450    It is also important for initialization of arrays from string constants
4451    since the full length of the string constant might not be wanted.
4452    It is also needed for initialization of unions, where the initializer's
4453    type is just one member, and that may not be as long as the union.
4454
4455    There a case in which we would fail to output exactly SIZE bytes:
4456    for a structure constructor that wants to produce more than SIZE bytes.
4457    But such constructors will never be generated for any possible input.
4458
4459    ALIGN is the alignment of the data in bits.  */
4460
4461 void
4462 output_constant (tree exp, unsigned HOST_WIDE_INT size, unsigned int align)
4463 {
4464   enum tree_code code;
4465   unsigned HOST_WIDE_INT thissize;
4466
4467   if (size == 0 || flag_syntax_only)
4468     return;
4469
4470   /* See if we're trying to initialize a pointer in a non-default mode
4471      to the address of some declaration somewhere.  If the target says
4472      the mode is valid for pointers, assume the target has a way of
4473      resolving it.  */
4474   if (TREE_CODE (exp) == NOP_EXPR
4475       && POINTER_TYPE_P (TREE_TYPE (exp))
4476       && targetm.addr_space.valid_pointer_mode
4477            (TYPE_MODE (TREE_TYPE (exp)),
4478             TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
4479     {
4480       tree saved_type = TREE_TYPE (exp);
4481
4482       /* Peel off any intermediate conversions-to-pointer for valid
4483          pointer modes.  */
4484       while (TREE_CODE (exp) == NOP_EXPR
4485              && POINTER_TYPE_P (TREE_TYPE (exp))
4486              && targetm.addr_space.valid_pointer_mode
4487                   (TYPE_MODE (TREE_TYPE (exp)),
4488                    TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
4489         exp = TREE_OPERAND (exp, 0);
4490
4491       /* If what we're left with is the address of something, we can
4492          convert the address to the final type and output it that
4493          way.  */
4494       if (TREE_CODE (exp) == ADDR_EXPR)
4495         exp = build1 (ADDR_EXPR, saved_type, TREE_OPERAND (exp, 0));
4496       /* Likewise for constant ints.  */
4497       else if (TREE_CODE (exp) == INTEGER_CST)
4498         exp = build_int_cst_wide (saved_type, TREE_INT_CST_LOW (exp),
4499                                   TREE_INT_CST_HIGH (exp));
4500
4501     }
4502
4503   /* Eliminate any conversions since we'll be outputting the underlying
4504      constant.  */
4505   while (CONVERT_EXPR_P (exp)
4506          || TREE_CODE (exp) == NON_LVALUE_EXPR
4507          || TREE_CODE (exp) == VIEW_CONVERT_EXPR)
4508     {
4509       HOST_WIDE_INT type_size = int_size_in_bytes (TREE_TYPE (exp));
4510       HOST_WIDE_INT op_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (exp, 0)));
4511
4512       /* Make sure eliminating the conversion is really a no-op, except with
4513          VIEW_CONVERT_EXPRs to allow for wild Ada unchecked conversions and
4514          union types to allow for Ada unchecked unions.  */
4515       if (type_size > op_size
4516           && TREE_CODE (exp) != VIEW_CONVERT_EXPR
4517           && TREE_CODE (TREE_TYPE (exp)) != UNION_TYPE)
4518         /* Keep the conversion. */
4519         break;
4520       else
4521         exp = TREE_OPERAND (exp, 0);
4522     }
4523
4524   code = TREE_CODE (TREE_TYPE (exp));
4525   thissize = int_size_in_bytes (TREE_TYPE (exp));
4526
4527   /* Allow a constructor with no elements for any data type.
4528      This means to fill the space with zeros.  */
4529   if (TREE_CODE (exp) == CONSTRUCTOR
4530       && VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (exp)))
4531     {
4532       assemble_zeros (size);
4533       return;
4534     }
4535
4536   if (TREE_CODE (exp) == FDESC_EXPR)
4537     {
4538 #ifdef ASM_OUTPUT_FDESC
4539       HOST_WIDE_INT part = tree_low_cst (TREE_OPERAND (exp, 1), 0);
4540       tree decl = TREE_OPERAND (exp, 0);
4541       ASM_OUTPUT_FDESC (asm_out_file, decl, part);
4542 #else
4543       gcc_unreachable ();
4544 #endif
4545       return;
4546     }
4547
4548   /* Now output the underlying data.  If we've handling the padding, return.
4549      Otherwise, break and ensure SIZE is the size written.  */
4550   switch (code)
4551     {
4552     case BOOLEAN_TYPE:
4553     case INTEGER_TYPE:
4554     case ENUMERAL_TYPE:
4555     case POINTER_TYPE:
4556     case REFERENCE_TYPE:
4557     case OFFSET_TYPE:
4558     case FIXED_POINT_TYPE:
4559       if (! assemble_integer (expand_expr (exp, NULL_RTX, VOIDmode,
4560                                            EXPAND_INITIALIZER),
4561                               MIN (size, thissize), align, 0))
4562         error ("initializer for integer/fixed-point value is too complicated");
4563       break;
4564
4565     case REAL_TYPE:
4566       if (TREE_CODE (exp) != REAL_CST)
4567         error ("initializer for floating value is not a floating constant");
4568       else
4569         assemble_real (TREE_REAL_CST (exp), TYPE_MODE (TREE_TYPE (exp)), align);
4570       break;
4571
4572     case COMPLEX_TYPE:
4573       output_constant (TREE_REALPART (exp), thissize / 2, align);
4574       output_constant (TREE_IMAGPART (exp), thissize / 2,
4575                        min_align (align, BITS_PER_UNIT * (thissize / 2)));
4576       break;
4577
4578     case ARRAY_TYPE:
4579     case VECTOR_TYPE:
4580       switch (TREE_CODE (exp))
4581         {
4582         case CONSTRUCTOR:
4583             output_constructor (exp, size, align, NULL);
4584           return;
4585         case STRING_CST:
4586           thissize = MIN ((unsigned HOST_WIDE_INT)TREE_STRING_LENGTH (exp),
4587                           size);
4588           assemble_string (TREE_STRING_POINTER (exp), thissize);
4589           break;
4590
4591         case VECTOR_CST:
4592           {
4593             int elt_size;
4594             tree link;
4595             unsigned int nalign;
4596             enum machine_mode inner;
4597
4598             inner = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
4599             nalign = MIN (align, GET_MODE_ALIGNMENT (inner));
4600
4601             elt_size = GET_MODE_SIZE (inner);
4602
4603             link = TREE_VECTOR_CST_ELTS (exp);
4604             output_constant (TREE_VALUE (link), elt_size, align);
4605             thissize = elt_size;
4606             while ((link = TREE_CHAIN (link)) != NULL)
4607               {
4608                 output_constant (TREE_VALUE (link), elt_size, nalign);
4609                 thissize += elt_size;
4610               }
4611             break;
4612           }
4613         default:
4614           gcc_unreachable ();
4615         }
4616       break;
4617
4618     case RECORD_TYPE:
4619     case UNION_TYPE:
4620       gcc_assert (TREE_CODE (exp) == CONSTRUCTOR);
4621       output_constructor (exp, size, align, NULL);
4622       return;
4623
4624     case ERROR_MARK:
4625       return;
4626
4627     default:
4628       gcc_unreachable ();
4629     }
4630
4631   if (size > thissize)
4632     assemble_zeros (size - thissize);
4633 }
4634
4635 \f
4636 /* Subroutine of output_constructor, used for computing the size of
4637    arrays of unspecified length.  VAL must be a CONSTRUCTOR of an array
4638    type with an unspecified upper bound.  */
4639
4640 static unsigned HOST_WIDE_INT
4641 array_size_for_constructor (tree val)
4642 {
4643   tree max_index, i;
4644   unsigned HOST_WIDE_INT cnt;
4645   tree index, value, tmp;
4646
4647   /* This code used to attempt to handle string constants that are not
4648      arrays of single-bytes, but nothing else does, so there's no point in
4649      doing it here.  */
4650   if (TREE_CODE (val) == STRING_CST)
4651     return TREE_STRING_LENGTH (val);
4652
4653   max_index = NULL_TREE;
4654   FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (val), cnt, index, value)
4655     {
4656       if (TREE_CODE (index) == RANGE_EXPR)
4657         index = TREE_OPERAND (index, 1);
4658       if (max_index == NULL_TREE || tree_int_cst_lt (max_index, index))
4659         max_index = index;
4660     }
4661
4662   if (max_index == NULL_TREE)
4663     return 0;
4664
4665   /* Compute the total number of array elements.  */
4666   tmp = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (val)));
4667   i = size_binop (MINUS_EXPR, fold_convert (sizetype, max_index),
4668                   fold_convert (sizetype, tmp));
4669   i = size_binop (PLUS_EXPR, i, size_one_node);
4670
4671   /* Multiply by the array element unit size to find number of bytes.  */
4672   i = size_binop (MULT_EXPR, i, TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (val))));
4673
4674   return tree_low_cst (i, 1);
4675 }
4676
4677 /* Other datastructures + helpers for output_constructor.  */
4678
4679 /* output_constructor local state to support interaction with helpers.  */
4680
4681 typedef struct {
4682
4683   /* Received arguments.  */
4684   tree exp;                     /* Constructor expression.  */
4685   unsigned HOST_WIDE_INT size;  /* # bytes to output - pad if necessary.  */
4686   unsigned int align;           /* Known initial alignment.  */
4687
4688   /* Constructor expression data.  */
4689   tree type;       /* Expression type.  */
4690   tree field;      /* Current field decl in a record.  */
4691   tree min_index;  /* Lower bound if specified for an array.  */
4692
4693   /* Output processing state.  */
4694   HOST_WIDE_INT total_bytes;  /* # bytes output so far / current position.  */
4695   bool byte_buffer_in_use;    /* Whether byte ...  */
4696   int byte;                   /* ... contains part of a bitfield byte yet to
4697                                  be output.  */
4698
4699   int last_relative_index;    /* Implicit or explicit index of the last
4700                                  array element output within a bitfield.  */
4701   /* Current element.  */
4702   tree val;    /* Current element value.  */
4703   tree index;  /* Current element index.  */
4704
4705 } oc_local_state;
4706
4707 /* Helper for output_constructor.  From the current LOCAL state, output a
4708    RANGE_EXPR element.  */
4709
4710 static void
4711 output_constructor_array_range (oc_local_state *local)
4712 {
4713   unsigned HOST_WIDE_INT fieldsize
4714     = int_size_in_bytes (TREE_TYPE (local->type));
4715
4716   HOST_WIDE_INT lo_index
4717     = tree_low_cst (TREE_OPERAND (local->index, 0), 0);
4718   HOST_WIDE_INT hi_index
4719     = tree_low_cst (TREE_OPERAND (local->index, 1), 0);
4720   HOST_WIDE_INT index;
4721
4722   unsigned int align2
4723     = min_align (local->align, fieldsize * BITS_PER_UNIT);
4724
4725   for (index = lo_index; index <= hi_index; index++)
4726     {
4727       /* Output the element's initial value.  */
4728       if (local->val == NULL_TREE)
4729         assemble_zeros (fieldsize);
4730       else
4731         output_constant (local->val, fieldsize, align2);
4732
4733       /* Count its size.  */
4734       local->total_bytes += fieldsize;
4735     }
4736 }
4737
4738 /* Helper for output_constructor.  From the current LOCAL state, output a
4739    field element that is not true bitfield or part of an outer one.  */
4740
4741 static void
4742 output_constructor_regular_field (oc_local_state *local)
4743 {
4744   /* Field size and position.  Since this structure is static, we know the
4745      positions are constant.  */
4746   unsigned HOST_WIDE_INT fieldsize;
4747   HOST_WIDE_INT fieldpos;
4748
4749   unsigned int align2;
4750
4751   if (local->index != NULL_TREE)
4752     fieldpos = (tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (local->val)), 1)
4753                 * ((tree_low_cst (local->index, 0)
4754                     - tree_low_cst (local->min_index, 0))));
4755   else if (local->field != NULL_TREE)
4756     fieldpos = int_byte_position (local->field);
4757   else
4758     fieldpos = 0;
4759
4760   /* Output any buffered-up bit-fields preceding this element.  */
4761   if (local->byte_buffer_in_use)
4762     {
4763       assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
4764       local->total_bytes++;
4765       local->byte_buffer_in_use = false;
4766     }
4767
4768   /* Advance to offset of this element.
4769      Note no alignment needed in an array, since that is guaranteed
4770      if each element has the proper size.  */
4771   if ((local->field != NULL_TREE || local->index != NULL_TREE)
4772       && fieldpos != local->total_bytes)
4773     {
4774       gcc_assert (fieldpos >= local->total_bytes);
4775       assemble_zeros (fieldpos - local->total_bytes);
4776       local->total_bytes = fieldpos;
4777     }
4778
4779   /* Find the alignment of this element.  */
4780   align2 = min_align (local->align, BITS_PER_UNIT * fieldpos);
4781
4782   /* Determine size this element should occupy.  */
4783   if (local->field)
4784     {
4785       fieldsize = 0;
4786
4787       /* If this is an array with an unspecified upper bound,
4788          the initializer determines the size.  */
4789       /* ??? This ought to only checked if DECL_SIZE_UNIT is NULL,
4790          but we cannot do this until the deprecated support for
4791          initializing zero-length array members is removed.  */
4792       if (TREE_CODE (TREE_TYPE (local->field)) == ARRAY_TYPE
4793           && TYPE_DOMAIN (TREE_TYPE (local->field))
4794           && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (local->field))))
4795         {
4796           fieldsize = array_size_for_constructor (local->val);
4797           /* Given a non-empty initialization, this field had
4798              better be last.  */
4799           gcc_assert (!fieldsize || !DECL_CHAIN (local->field));
4800         }
4801       else if (DECL_SIZE_UNIT (local->field))
4802         {
4803           /* ??? This can't be right.  If the decl size overflows
4804              a host integer we will silently emit no data.  */
4805           if (host_integerp (DECL_SIZE_UNIT (local->field), 1))
4806             fieldsize = tree_low_cst (DECL_SIZE_UNIT (local->field), 1);
4807         }
4808     }
4809   else
4810     fieldsize = int_size_in_bytes (TREE_TYPE (local->type));
4811
4812   /* Output the element's initial value.  */
4813   if (local->val == NULL_TREE)
4814     assemble_zeros (fieldsize);
4815   else
4816     output_constant (local->val, fieldsize, align2);
4817
4818   /* Count its size.  */
4819   local->total_bytes += fieldsize;
4820 }
4821
4822 /* Helper for output_constructor.  From the current LOCAL and OUTER states,
4823    output an element that is a true bitfield or part of an outer one.  */
4824
4825 static void
4826 output_constructor_bitfield (oc_local_state *local, oc_outer_state *outer)
4827 {
4828   /* Bit size of this element.  */
4829   HOST_WIDE_INT ebitsize
4830     = (local->field
4831        ? tree_low_cst (DECL_SIZE (local->field), 1)
4832        : tree_low_cst (TYPE_SIZE (TREE_TYPE (local->type)), 1));
4833
4834   /* Relative index of this element if this is an array component.  */
4835   HOST_WIDE_INT relative_index
4836     = (!local->field
4837        ? (local->index
4838           ? (tree_low_cst (local->index, 0)
4839              - tree_low_cst (local->min_index, 0))
4840           : local->last_relative_index + 1)
4841        : 0);
4842
4843   /* Bit position of this element from the start of the containing
4844      constructor.  */
4845   HOST_WIDE_INT constructor_relative_ebitpos
4846       = (local->field
4847          ? int_bit_position (local->field)
4848          : ebitsize * relative_index);
4849
4850   /* Bit position of this element from the start of a possibly ongoing
4851      outer byte buffer.  */
4852   HOST_WIDE_INT byte_relative_ebitpos
4853       = ((outer ? outer->bit_offset : 0) + constructor_relative_ebitpos);
4854
4855   /* From the start of a possibly ongoing outer byte buffer, offsets to
4856      the first bit of this element and to the first bit past the end of
4857      this element.  */
4858   HOST_WIDE_INT next_offset = byte_relative_ebitpos;
4859   HOST_WIDE_INT end_offset = byte_relative_ebitpos + ebitsize;
4860
4861   local->last_relative_index = relative_index;
4862
4863   if (local->val == NULL_TREE)
4864     local->val = integer_zero_node;
4865
4866   while (TREE_CODE (local->val) == VIEW_CONVERT_EXPR
4867          || TREE_CODE (local->val) == NON_LVALUE_EXPR)
4868     local->val = TREE_OPERAND (local->val, 0);
4869
4870   if (TREE_CODE (local->val) != INTEGER_CST
4871       && TREE_CODE (local->val) != CONSTRUCTOR)
4872     {
4873       error ("invalid initial value for member %qE", DECL_NAME (local->field));
4874       return;
4875     }
4876
4877   /* If this field does not start in this (or, next) byte,
4878      skip some bytes.  */
4879   if (next_offset / BITS_PER_UNIT != local->total_bytes)
4880     {
4881       /* Output remnant of any bit field in previous bytes.  */
4882       if (local->byte_buffer_in_use)
4883         {
4884           assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
4885           local->total_bytes++;
4886           local->byte_buffer_in_use = false;
4887         }
4888
4889       /* If still not at proper byte, advance to there.  */
4890       if (next_offset / BITS_PER_UNIT != local->total_bytes)
4891         {
4892           gcc_assert (next_offset / BITS_PER_UNIT >= local->total_bytes);
4893           assemble_zeros (next_offset / BITS_PER_UNIT - local->total_bytes);
4894           local->total_bytes = next_offset / BITS_PER_UNIT;
4895         }
4896     }
4897
4898   /* Set up the buffer if necessary.  */
4899   if (!local->byte_buffer_in_use)
4900     {
4901       local->byte = 0;
4902       if (ebitsize > 0)
4903         local->byte_buffer_in_use = true;
4904     }
4905
4906   /* If this is nested constructor, recurse passing the bit offset and the
4907      pending data, then retrieve the new pending data afterwards.  */
4908   if (TREE_CODE (local->val) == CONSTRUCTOR)
4909     {
4910       oc_outer_state output_state;
4911
4912       output_state.bit_offset = next_offset % BITS_PER_UNIT;
4913       output_state.byte = local->byte;
4914       local->total_bytes
4915           += output_constructor (local->val, 0, 0, &output_state);
4916       local->byte = output_state.byte;
4917       return;
4918     }
4919
4920   /* Otherwise, we must split the element into pieces that fall within
4921      separate bytes, and combine each byte with previous or following
4922      bit-fields.  */
4923   while (next_offset < end_offset)
4924     {
4925       int this_time;
4926       int shift;
4927       HOST_WIDE_INT value;
4928       HOST_WIDE_INT next_byte = next_offset / BITS_PER_UNIT;
4929       HOST_WIDE_INT next_bit = next_offset % BITS_PER_UNIT;
4930
4931       /* Advance from byte to byte
4932          within this element when necessary.  */
4933       while (next_byte != local->total_bytes)
4934         {
4935           assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
4936           local->total_bytes++;
4937           local->byte = 0;
4938         }
4939
4940       /* Number of bits we can process at once
4941          (all part of the same byte).  */
4942       this_time = MIN (end_offset - next_offset,
4943                        BITS_PER_UNIT - next_bit);
4944       if (BYTES_BIG_ENDIAN)
4945         {
4946           /* On big-endian machine, take the most significant bits
4947              first (of the bits that are significant)
4948              and put them into bytes from the most significant end.  */
4949           shift = end_offset - next_offset - this_time;
4950
4951           /* Don't try to take a bunch of bits that cross
4952              the word boundary in the INTEGER_CST. We can
4953              only select bits from the LOW or HIGH part
4954              not from both.  */
4955           if (shift < HOST_BITS_PER_WIDE_INT
4956               && shift + this_time > HOST_BITS_PER_WIDE_INT)
4957             {
4958               this_time = shift + this_time - HOST_BITS_PER_WIDE_INT;
4959               shift = HOST_BITS_PER_WIDE_INT;
4960             }
4961
4962           /* Now get the bits from the appropriate constant word.  */
4963           if (shift < HOST_BITS_PER_WIDE_INT)
4964             value = TREE_INT_CST_LOW (local->val);
4965           else
4966             {
4967               gcc_assert (shift < 2 * HOST_BITS_PER_WIDE_INT);
4968               value = TREE_INT_CST_HIGH (local->val);
4969               shift -= HOST_BITS_PER_WIDE_INT;
4970             }
4971
4972           /* Get the result. This works only when:
4973              1 <= this_time <= HOST_BITS_PER_WIDE_INT.  */
4974           local->byte |= (((value >> shift)
4975                            & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
4976                           << (BITS_PER_UNIT - this_time - next_bit));
4977         }
4978       else
4979         {
4980           /* On little-endian machines,
4981              take first the least significant bits of the value
4982              and pack them starting at the least significant
4983              bits of the bytes.  */
4984           shift = next_offset - byte_relative_ebitpos;
4985
4986           /* Don't try to take a bunch of bits that cross
4987              the word boundary in the INTEGER_CST. We can
4988              only select bits from the LOW or HIGH part
4989              not from both.  */
4990           if (shift < HOST_BITS_PER_WIDE_INT
4991               && shift + this_time > HOST_BITS_PER_WIDE_INT)
4992             this_time = (HOST_BITS_PER_WIDE_INT - shift);
4993
4994           /* Now get the bits from the appropriate constant word.  */
4995           if (shift < HOST_BITS_PER_WIDE_INT)
4996             value = TREE_INT_CST_LOW (local->val);
4997           else
4998             {
4999               gcc_assert (shift < 2 * HOST_BITS_PER_WIDE_INT);
5000               value = TREE_INT_CST_HIGH (local->val);
5001               shift -= HOST_BITS_PER_WIDE_INT;
5002             }
5003
5004           /* Get the result. This works only when:
5005              1 <= this_time <= HOST_BITS_PER_WIDE_INT.  */
5006           local->byte |= (((value >> shift)
5007                            & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
5008                           << next_bit);
5009         }
5010
5011       next_offset += this_time;
5012       local->byte_buffer_in_use = true;
5013     }
5014 }
5015
5016 /* Subroutine of output_constant, used for CONSTRUCTORs (aggregate constants).
5017    Generate at least SIZE bytes, padding if necessary.  OUTER designates the
5018    caller output state of relevance in recursive invocations.  */
5019
5020 static unsigned HOST_WIDE_INT
5021 output_constructor (tree exp, unsigned HOST_WIDE_INT size,
5022                     unsigned int align, oc_outer_state * outer)
5023 {
5024   unsigned HOST_WIDE_INT cnt;
5025   constructor_elt *ce;
5026
5027   oc_local_state local;
5028
5029   /* Setup our local state to communicate with helpers.  */
5030   local.exp = exp;
5031   local.size = size;
5032   local.align = align;
5033
5034   local.total_bytes = 0;
5035   local.byte_buffer_in_use = outer != NULL;
5036   local.byte = outer ? outer->byte : 0;
5037
5038   local.type = TREE_TYPE (exp);
5039
5040   local.last_relative_index = -1;
5041
5042   local.min_index = NULL_TREE;
5043   if (TREE_CODE (local.type) == ARRAY_TYPE
5044       && TYPE_DOMAIN (local.type) != NULL_TREE)
5045     local.min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (local.type));
5046
5047   gcc_assert (HOST_BITS_PER_WIDE_INT >= BITS_PER_UNIT);
5048
5049   /* As CE goes through the elements of the constant, FIELD goes through the
5050      structure fields if the constant is a structure.  If the constant is a
5051      union, we override this by getting the field from the TREE_LIST element.
5052      But the constant could also be an array.  Then FIELD is zero.
5053
5054      There is always a maximum of one element in the chain LINK for unions
5055      (even if the initializer in a source program incorrectly contains
5056      more one).  */
5057
5058   local.field = NULL_TREE;
5059   if (TREE_CODE (local.type) == RECORD_TYPE)
5060     local.field = TYPE_FIELDS (local.type);
5061
5062   for (cnt = 0;
5063        VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (exp), cnt, ce);
5064        cnt++, local.field = local.field ? DECL_CHAIN (local.field) : 0)
5065     {
5066       local.val = ce->value;
5067       local.index = NULL_TREE;
5068
5069       /* The element in a union constructor specifies the proper field
5070          or index.  */
5071       if ((TREE_CODE (local.type) == RECORD_TYPE
5072            || TREE_CODE (local.type) == UNION_TYPE
5073            || TREE_CODE (local.type) == QUAL_UNION_TYPE)
5074           && ce->index != NULL_TREE)
5075         local.field = ce->index;
5076
5077       else if (TREE_CODE (local.type) == ARRAY_TYPE)
5078         local.index = ce->index;
5079
5080 #ifdef ASM_COMMENT_START
5081       if (local.field && flag_verbose_asm)
5082         fprintf (asm_out_file, "%s %s:\n",
5083                  ASM_COMMENT_START,
5084                  DECL_NAME (local.field)
5085                  ? IDENTIFIER_POINTER (DECL_NAME (local.field))
5086                  : "<anonymous>");
5087 #endif
5088
5089       /* Eliminate the marker that makes a cast not be an lvalue.  */
5090       if (local.val != NULL_TREE)
5091         STRIP_NOPS (local.val);
5092
5093       /* Output the current element, using the appropriate helper ...  */
5094
5095       /* For an array slice not part of an outer bitfield.  */
5096       if (!outer
5097           && local.index != NULL_TREE
5098           && TREE_CODE (local.index) == RANGE_EXPR)
5099         output_constructor_array_range (&local);
5100
5101       /* For a field that is neither a true bitfield nor part of an outer one,
5102          known to be at least byte aligned and multiple-of-bytes long.  */
5103       else if (!outer
5104                && (local.field == NULL_TREE
5105                    || !CONSTRUCTOR_BITFIELD_P (local.field)))
5106         output_constructor_regular_field (&local);
5107
5108       /* For a true bitfield or part of an outer one.  */
5109       else
5110         output_constructor_bitfield (&local, outer);
5111     }
5112
5113   /* If we are not at toplevel, save the pending data for our caller.
5114      Otherwise output the pending data and padding zeros as needed. */
5115   if (outer)
5116     outer->byte = local.byte;
5117   else
5118     {
5119       if (local.byte_buffer_in_use)
5120         {
5121           assemble_integer (GEN_INT (local.byte), 1, BITS_PER_UNIT, 1);
5122           local.total_bytes++;
5123         }
5124
5125       if ((unsigned HOST_WIDE_INT)local.total_bytes < local.size)
5126         {
5127           assemble_zeros (local.size - local.total_bytes);
5128           local.total_bytes = local.size;
5129         }
5130     }
5131
5132   return local.total_bytes;
5133 }
5134
5135 /* Mark DECL as weak.  */
5136
5137 static void
5138 mark_weak (tree decl)
5139 {
5140   DECL_WEAK (decl) = 1;
5141
5142   if (DECL_RTL_SET_P (decl)
5143       && MEM_P (DECL_RTL (decl))
5144       && XEXP (DECL_RTL (decl), 0)
5145       && GET_CODE (XEXP (DECL_RTL (decl), 0)) == SYMBOL_REF)
5146     SYMBOL_REF_WEAK (XEXP (DECL_RTL (decl), 0)) = 1;
5147 }
5148
5149 /* Merge weak status between NEWDECL and OLDDECL.  */
5150
5151 void
5152 merge_weak (tree newdecl, tree olddecl)
5153 {
5154   if (DECL_WEAK (newdecl) == DECL_WEAK (olddecl))
5155     {
5156       if (DECL_WEAK (newdecl) && TARGET_SUPPORTS_WEAK)
5157         {
5158           tree *pwd;
5159           /* We put the NEWDECL on the weak_decls list at some point
5160              and OLDDECL as well.  Keep just OLDDECL on the list.  */
5161           for (pwd = &weak_decls; *pwd; pwd = &TREE_CHAIN (*pwd))
5162             if (TREE_VALUE (*pwd) == newdecl)
5163               {
5164                 *pwd = TREE_CHAIN (*pwd);
5165                 break;
5166               }
5167         }
5168       return;
5169     }
5170
5171   if (DECL_WEAK (newdecl))
5172     {
5173       tree wd;
5174
5175       /* NEWDECL is weak, but OLDDECL is not.  */
5176
5177       /* If we already output the OLDDECL, we're in trouble; we can't
5178          go back and make it weak.  This should never happen in
5179          unit-at-a-time compilation.  */
5180       gcc_assert (!TREE_ASM_WRITTEN (olddecl));
5181
5182       /* If we've already generated rtl referencing OLDDECL, we may
5183          have done so in a way that will not function properly with
5184          a weak symbol.  Again in unit-at-a-time this should be
5185          impossible.  */
5186       gcc_assert (!TREE_USED (olddecl)
5187                   || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (olddecl)));
5188
5189       if (TARGET_SUPPORTS_WEAK)
5190         {
5191           /* We put the NEWDECL on the weak_decls list at some point.
5192              Replace it with the OLDDECL.  */
5193           for (wd = weak_decls; wd; wd = TREE_CHAIN (wd))
5194             if (TREE_VALUE (wd) == newdecl)
5195               {
5196                 TREE_VALUE (wd) = olddecl;
5197                 break;
5198               }
5199           /* We may not find the entry on the list.  If NEWDECL is a
5200              weak alias, then we will have already called
5201              globalize_decl to remove the entry; in that case, we do
5202              not need to do anything.  */
5203         }
5204
5205       /* Make the OLDDECL weak; it's OLDDECL that we'll be keeping.  */
5206       mark_weak (olddecl);
5207     }
5208   else
5209     /* OLDDECL was weak, but NEWDECL was not explicitly marked as
5210        weak.  Just update NEWDECL to indicate that it's weak too.  */
5211     mark_weak (newdecl);
5212 }
5213
5214 /* Declare DECL to be a weak symbol.  */
5215
5216 void
5217 declare_weak (tree decl)
5218 {
5219   gcc_assert (TREE_CODE (decl) != FUNCTION_DECL || !TREE_ASM_WRITTEN (decl));
5220   if (! TREE_PUBLIC (decl))
5221     error ("weak declaration of %q+D must be public", decl);
5222   else if (!TARGET_SUPPORTS_WEAK)
5223     warning (0, "weak declaration of %q+D not supported", decl);
5224
5225   mark_weak (decl);
5226   if (!lookup_attribute ("weak", DECL_ATTRIBUTES (decl)))
5227     DECL_ATTRIBUTES (decl)
5228       = tree_cons (get_identifier ("weak"), NULL, DECL_ATTRIBUTES (decl));
5229 }
5230
5231 static void
5232 weak_finish_1 (tree decl)
5233 {
5234 #if defined (ASM_WEAKEN_DECL) || defined (ASM_WEAKEN_LABEL)
5235   const char *const name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5236 #endif
5237
5238   if (! TREE_USED (decl))
5239     return;
5240
5241 #ifdef ASM_WEAKEN_DECL
5242   ASM_WEAKEN_DECL (asm_out_file, decl, name, NULL);
5243 #else
5244 #ifdef ASM_WEAKEN_LABEL
5245   ASM_WEAKEN_LABEL (asm_out_file, name);
5246 #else
5247 #ifdef ASM_OUTPUT_WEAK_ALIAS
5248   {
5249     static bool warn_once = 0;
5250     if (! warn_once)
5251       {
5252         warning (0, "only weak aliases are supported in this configuration");
5253         warn_once = 1;
5254       }
5255     return;
5256   }
5257 #endif
5258 #endif
5259 #endif
5260 }
5261
5262 /* This TREE_LIST contains weakref targets.  */
5263
5264 static GTY(()) tree weakref_targets;
5265
5266 /* Forward declaration.  */
5267 static tree find_decl_and_mark_needed (tree decl, tree target);
5268
5269 /* Emit any pending weak declarations.  */
5270
5271 void
5272 weak_finish (void)
5273 {
5274   tree t;
5275
5276   for (t = weakref_targets; t; t = TREE_CHAIN (t))
5277     {
5278       tree alias_decl = TREE_PURPOSE (t);
5279       tree target = ultimate_transparent_alias_target (&TREE_VALUE (t));
5280
5281       if (! TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (alias_decl)))
5282         /* Remove alias_decl from the weak list, but leave entries for
5283            the target alone.  */
5284         target = NULL_TREE;
5285 #ifndef ASM_OUTPUT_WEAKREF
5286       else if (! TREE_SYMBOL_REFERENCED (target))
5287         {
5288           /* Use ASM_WEAKEN_LABEL only if ASM_WEAKEN_DECL is not
5289              defined, otherwise we and weak_finish_1 would use
5290              different macros.  */
5291 # if defined ASM_WEAKEN_LABEL && ! defined ASM_WEAKEN_DECL
5292           ASM_WEAKEN_LABEL (asm_out_file, IDENTIFIER_POINTER (target));
5293 # else
5294           tree decl = find_decl_and_mark_needed (alias_decl, target);
5295
5296           if (! decl)
5297             {
5298               decl = build_decl (DECL_SOURCE_LOCATION (alias_decl),
5299                                  TREE_CODE (alias_decl), target,
5300                                  TREE_TYPE (alias_decl));
5301
5302               DECL_EXTERNAL (decl) = 1;
5303               TREE_PUBLIC (decl) = 1;
5304               DECL_ARTIFICIAL (decl) = 1;
5305               TREE_NOTHROW (decl) = TREE_NOTHROW (alias_decl);
5306               TREE_USED (decl) = 1;
5307             }
5308
5309           weak_finish_1 (decl);
5310 # endif
5311         }
5312 #endif
5313
5314       {
5315         tree *p;
5316         tree t2;
5317
5318         /* Remove the alias and the target from the pending weak list
5319            so that we do not emit any .weak directives for the former,
5320            nor multiple .weak directives for the latter.  */
5321         for (p = &weak_decls; (t2 = *p) ; )
5322           {
5323             if (TREE_VALUE (t2) == alias_decl
5324                 || target == DECL_ASSEMBLER_NAME (TREE_VALUE (t2)))
5325               *p = TREE_CHAIN (t2);
5326             else
5327               p = &TREE_CHAIN (t2);
5328           }
5329
5330         /* Remove other weakrefs to the same target, to speed things up.  */
5331         for (p = &TREE_CHAIN (t); (t2 = *p) ; )
5332           {
5333             if (target == ultimate_transparent_alias_target (&TREE_VALUE (t2)))
5334               *p = TREE_CHAIN (t2);
5335             else
5336               p = &TREE_CHAIN (t2);
5337           }
5338       }
5339     }
5340
5341   for (t = weak_decls; t; t = TREE_CHAIN (t))
5342     {
5343       tree decl = TREE_VALUE (t);
5344
5345       weak_finish_1 (decl);
5346     }
5347 }
5348
5349 /* Emit the assembly bits to indicate that DECL is globally visible.  */
5350
5351 static void
5352 globalize_decl (tree decl)
5353 {
5354
5355 #if defined (ASM_WEAKEN_LABEL) || defined (ASM_WEAKEN_DECL)
5356   if (DECL_WEAK (decl))
5357     {
5358       const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
5359       tree *p, t;
5360
5361 #ifdef ASM_WEAKEN_DECL
5362       ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
5363 #else
5364       ASM_WEAKEN_LABEL (asm_out_file, name);
5365 #endif
5366
5367       /* Remove this function from the pending weak list so that
5368          we do not emit multiple .weak directives for it.  */
5369       for (p = &weak_decls; (t = *p) ; )
5370         {
5371           if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
5372             *p = TREE_CHAIN (t);
5373           else
5374             p = &TREE_CHAIN (t);
5375         }
5376
5377       /* Remove weakrefs to the same target from the pending weakref
5378          list, for the same reason.  */
5379       for (p = &weakref_targets; (t = *p) ; )
5380         {
5381           if (DECL_ASSEMBLER_NAME (decl)
5382               == ultimate_transparent_alias_target (&TREE_VALUE (t)))
5383             *p = TREE_CHAIN (t);
5384           else
5385             p = &TREE_CHAIN (t);
5386         }
5387
5388       return;
5389     }
5390 #endif
5391
5392   targetm.asm_out.globalize_decl_name (asm_out_file, decl);
5393 }
5394
5395 VEC(alias_pair,gc) *alias_pairs;
5396
5397 /* Given an assembly name, find the decl it is associated with.  At the
5398    same time, mark it needed for cgraph.  */
5399
5400 static tree
5401 find_decl_and_mark_needed (tree decl, tree target)
5402 {
5403   struct cgraph_node *fnode = NULL;
5404   struct varpool_node *vnode = NULL;
5405
5406   if (TREE_CODE (decl) == FUNCTION_DECL)
5407     {
5408       fnode = cgraph_node_for_asm (target);
5409       if (fnode == NULL)
5410         vnode = varpool_node_for_asm (target);
5411     }
5412   else
5413     {
5414       vnode = varpool_node_for_asm (target);
5415       if (vnode == NULL)
5416         fnode = cgraph_node_for_asm (target);
5417     }
5418
5419   if (fnode)
5420     {
5421       cgraph_mark_needed_node (fnode);
5422       return fnode->decl;
5423     }
5424   else if (vnode)
5425     {
5426       varpool_mark_needed_node (vnode);
5427       vnode->force_output = 1;
5428       return vnode->decl;
5429     }
5430   else
5431     return NULL_TREE;
5432 }
5433
5434 /* Output the assembler code for a define (equate) using ASM_OUTPUT_DEF
5435    or ASM_OUTPUT_DEF_FROM_DECLS.  The function defines the symbol whose
5436    tree node is DECL to have the value of the tree node TARGET.  */
5437
5438 static void
5439 do_assemble_alias (tree decl, tree target)
5440 {
5441   /* Emulated TLS had better not get this var.  */
5442   gcc_assert(!(!targetm.have_tls
5443                && TREE_CODE (decl) == VAR_DECL
5444                && DECL_THREAD_LOCAL_P (decl)));
5445
5446   if (TREE_ASM_WRITTEN (decl))
5447     return;
5448
5449   /* We must force creation of DECL_RTL for debug info generation, even though
5450      we don't use it here.  */
5451   make_decl_rtl (decl);
5452
5453   TREE_ASM_WRITTEN (decl) = 1;
5454   TREE_ASM_WRITTEN (DECL_ASSEMBLER_NAME (decl)) = 1;
5455
5456   if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
5457     {
5458       ultimate_transparent_alias_target (&target);
5459
5460       if (!TREE_SYMBOL_REFERENCED (target))
5461         weakref_targets = tree_cons (decl, target, weakref_targets);
5462
5463 #ifdef ASM_OUTPUT_WEAKREF
5464       ASM_OUTPUT_WEAKREF (asm_out_file, decl,
5465                           IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
5466                           IDENTIFIER_POINTER (target));
5467 #else
5468       if (!TARGET_SUPPORTS_WEAK)
5469         {
5470           error_at (DECL_SOURCE_LOCATION (decl),
5471                     "weakref is not supported in this configuration");
5472           return;
5473         }
5474 #endif
5475       return;
5476     }
5477
5478 #ifdef ASM_OUTPUT_DEF
5479   /* Make name accessible from other files, if appropriate.  */
5480
5481   if (TREE_PUBLIC (decl))
5482     {
5483       globalize_decl (decl);
5484       maybe_assemble_visibility (decl);
5485     }
5486   if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
5487     {
5488 #if defined (ASM_OUTPUT_TYPE_DIRECTIVE) && HAVE_GNU_INDIRECT_FUNCTION
5489       ASM_OUTPUT_TYPE_DIRECTIVE
5490         (asm_out_file, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
5491          IFUNC_ASM_TYPE);
5492 #else
5493       error_at (DECL_SOURCE_LOCATION (decl),
5494                 "ifunc is not supported in this configuration");
5495 #endif
5496     }
5497
5498 # ifdef ASM_OUTPUT_DEF_FROM_DECLS
5499   ASM_OUTPUT_DEF_FROM_DECLS (asm_out_file, decl, target);
5500 # else
5501   ASM_OUTPUT_DEF (asm_out_file,
5502                   IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
5503                   IDENTIFIER_POINTER (target));
5504 # endif
5505 #elif defined (ASM_OUTPUT_WEAK_ALIAS) || defined (ASM_WEAKEN_DECL)
5506   {
5507     const char *name;
5508     tree *p, t;
5509
5510     name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5511 # ifdef ASM_WEAKEN_DECL
5512     ASM_WEAKEN_DECL (asm_out_file, decl, name, IDENTIFIER_POINTER (target));
5513 # else
5514     ASM_OUTPUT_WEAK_ALIAS (asm_out_file, name, IDENTIFIER_POINTER (target));
5515 # endif
5516     /* Remove this function from the pending weak list so that
5517        we do not emit multiple .weak directives for it.  */
5518     for (p = &weak_decls; (t = *p) ; )
5519       if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
5520         *p = TREE_CHAIN (t);
5521       else
5522         p = &TREE_CHAIN (t);
5523
5524     /* Remove weakrefs to the same target from the pending weakref
5525        list, for the same reason.  */
5526     for (p = &weakref_targets; (t = *p) ; )
5527       {
5528         if (DECL_ASSEMBLER_NAME (decl)
5529             == ultimate_transparent_alias_target (&TREE_VALUE (t)))
5530           *p = TREE_CHAIN (t);
5531         else
5532           p = &TREE_CHAIN (t);
5533       }
5534   }
5535 #endif
5536 }
5537
5538
5539 /* Allocate and construct a symbol alias set.  */
5540
5541 static symbol_alias_set_t *
5542 symbol_alias_set_create (void)
5543 {
5544   return pointer_set_create ();
5545 }
5546
5547 /* Destruct and free a symbol alias set.  */
5548
5549 void
5550 symbol_alias_set_destroy (symbol_alias_set_t *aset)
5551 {
5552   pointer_set_destroy (aset);
5553 }
5554
5555 /* Test if a symbol alias set contains a given name.  */
5556
5557 int
5558 symbol_alias_set_contains (const symbol_alias_set_t *aset, tree t)
5559 {
5560   /* We accept either a DECL or an IDENTIFIER directly.  */
5561   if (TREE_CODE (t) != IDENTIFIER_NODE)
5562     t = DECL_ASSEMBLER_NAME (t);
5563   t = targetm.asm_out.mangle_assembler_name (IDENTIFIER_POINTER (t));
5564   return pointer_set_contains (aset, t);
5565 }
5566
5567 /* Enter a new name into a symbol alias set.  */
5568
5569 static int
5570 symbol_alias_set_insert (symbol_alias_set_t *aset, tree t)
5571 {
5572   /* We accept either a DECL or an IDENTIFIER directly.  */
5573   if (TREE_CODE (t) != IDENTIFIER_NODE)
5574     t = DECL_ASSEMBLER_NAME (t);
5575   t = targetm.asm_out.mangle_assembler_name (IDENTIFIER_POINTER (t));
5576   return pointer_set_insert (aset, t);
5577 }
5578
5579 /* IN_SET_P is a predicate function assuming to be taken
5580    alias_pair->decl, alias_pair->target and DATA arguments.
5581
5582    Compute set of aliases by including everything where TRIVIALLY_VISIBLE
5583    predeicate is true and propagate across aliases such that when
5584    alias DECL is included, its TARGET is included too.  */
5585
5586 static symbol_alias_set_t *
5587 propagate_aliases_forward (bool (*in_set_p)
5588                              (tree decl, tree target, void *data),
5589                            void *data)
5590 {
5591   symbol_alias_set_t *set;
5592   unsigned i;
5593   alias_pair *p;
5594   bool changed;
5595
5596   set = symbol_alias_set_create ();
5597   for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); ++i)
5598     if (in_set_p (p->decl, p->target, data))
5599       symbol_alias_set_insert (set, p->decl);
5600   do
5601     {
5602       changed = false;
5603       for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); ++i)
5604         if (symbol_alias_set_contains (set, p->decl)
5605             && !symbol_alias_set_insert (set, p->target))
5606           changed = true;
5607     }
5608   while (changed);
5609
5610   return set;
5611 }
5612
5613 /* Like propagate_aliases_forward but do backward propagation.  */
5614
5615 symbol_alias_set_t *
5616 propagate_aliases_backward (bool (*in_set_p)
5617                              (tree decl, tree target, void *data),
5618                            void *data)
5619 {
5620   symbol_alias_set_t *set;
5621   unsigned i;
5622   alias_pair *p;
5623   bool changed;
5624
5625   /* We have to compute the set of set nodes including aliases
5626      themselves.  */
5627   set = symbol_alias_set_create ();
5628   for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); ++i)
5629     if (in_set_p (p->decl, p->target, data))
5630       symbol_alias_set_insert (set, p->target);
5631   do
5632     {
5633       changed = false;
5634       for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); ++i)
5635         if (symbol_alias_set_contains (set, p->target)
5636             && !symbol_alias_set_insert (set, p->decl))
5637           changed = true;
5638     }
5639   while (changed);
5640
5641   return set;
5642 }
5643 /* See if the alias is trivially visible.  This means
5644      1) alias is expoerted from the unit or
5645      2) alias is used in the code.
5646    We assume that unused cgraph/varpool nodes has been
5647    removed.
5648    Used as callback for propagate_aliases.  */
5649
5650 static bool
5651 trivially_visible_alias (tree decl, tree target ATTRIBUTE_UNUSED,
5652                          void *data ATTRIBUTE_UNUSED)
5653 {
5654   struct cgraph_node *fnode = NULL;
5655   struct varpool_node *vnode = NULL;
5656
5657   if (!TREE_PUBLIC (decl))
5658     {
5659       if (TREE_CODE (decl) == FUNCTION_DECL)
5660         fnode = cgraph_get_node (decl);
5661       else
5662         vnode = varpool_get_node (decl);
5663       return vnode || fnode;
5664     }
5665   else
5666     return true;
5667 }
5668
5669 /* See if the target of alias is defined in this unit.
5670    Used as callback for propagate_aliases.  */
5671
5672 static bool
5673 trivially_defined_alias (tree decl ATTRIBUTE_UNUSED,
5674                          tree target,
5675                          void *data ATTRIBUTE_UNUSED)
5676 {
5677   struct cgraph_node *fnode = NULL;
5678   struct varpool_node *vnode = NULL;
5679
5680   fnode = cgraph_node_for_asm (target);
5681   vnode = (fnode == NULL) ? varpool_node_for_asm (target) : NULL;
5682   return (fnode && fnode->analyzed) || (vnode && vnode->finalized);
5683 }
5684
5685 /* Remove the alias pairing for functions that are no longer in the call
5686    graph.  */
5687
5688 void
5689 remove_unreachable_alias_pairs (void)
5690 {
5691   symbol_alias_set_t *visible;
5692   unsigned i;
5693   alias_pair *p;
5694
5695   if (alias_pairs == NULL)
5696     return;
5697
5698   /* We have to compute the set of visible nodes including aliases
5699      themselves.  */
5700   visible = propagate_aliases_forward (trivially_visible_alias, NULL);
5701
5702   for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); )
5703     {
5704       if (!DECL_EXTERNAL (p->decl)
5705           && !symbol_alias_set_contains (visible, p->decl))
5706         {
5707           VEC_unordered_remove (alias_pair, alias_pairs, i);
5708           continue;
5709         }
5710
5711       i++;
5712     }
5713
5714   symbol_alias_set_destroy (visible);
5715 }
5716
5717
5718 /* First pass of completing pending aliases.  Make sure that cgraph knows
5719    which symbols will be required.  */
5720
5721 void
5722 finish_aliases_1 (void)
5723 {
5724   symbol_alias_set_t *defined;
5725   unsigned i;
5726   alias_pair *p;
5727
5728   if (alias_pairs == NULL)
5729     return;
5730
5731   /* We have to compute the set of defined nodes including aliases
5732      themselves.  */
5733   defined = propagate_aliases_backward (trivially_defined_alias, NULL);
5734
5735   FOR_EACH_VEC_ELT (alias_pair, alias_pairs, i, p)
5736     {
5737       tree target_decl;
5738
5739       target_decl = find_decl_and_mark_needed (p->decl, p->target);
5740       if (target_decl == NULL)
5741         {
5742           if (symbol_alias_set_contains (defined, p->target))
5743             continue;
5744
5745           if (! (p->emitted_diags & ALIAS_DIAG_TO_UNDEF)
5746               && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
5747             {
5748               error ("%q+D aliased to undefined symbol %qE",
5749                      p->decl, p->target);
5750               p->emitted_diags |= ALIAS_DIAG_TO_UNDEF;
5751             }
5752         }
5753       else if (! (p->emitted_diags & ALIAS_DIAG_TO_EXTERN)
5754                && DECL_EXTERNAL (target_decl)
5755                /* We use local aliases for C++ thunks to force the tailcall
5756                   to bind locally.  This is a hack - to keep it working do
5757                   the following (which is not strictly correct).  */
5758                && (! TREE_CODE (target_decl) == FUNCTION_DECL
5759                    || ! DECL_VIRTUAL_P (target_decl))
5760                && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
5761         {
5762           error ("%q+D aliased to external symbol %qE",
5763                  p->decl, p->target);     
5764           p->emitted_diags |= ALIAS_DIAG_TO_EXTERN;
5765         }
5766     }
5767
5768   symbol_alias_set_destroy (defined);
5769 }
5770
5771 /* Second pass of completing pending aliases.  Emit the actual assembly.
5772    This happens at the end of compilation and thus it is assured that the
5773    target symbol has been emitted.  */
5774
5775 void
5776 finish_aliases_2 (void)
5777 {
5778   unsigned i;
5779   alias_pair *p;
5780
5781   FOR_EACH_VEC_ELT (alias_pair, alias_pairs, i, p)
5782     do_assemble_alias (p->decl, p->target);
5783
5784   VEC_truncate (alias_pair, alias_pairs, 0);
5785 }
5786
5787 /* Emit an assembler directive to make the symbol for DECL an alias to
5788    the symbol for TARGET.  */
5789
5790 void
5791 assemble_alias (tree decl, tree target)
5792 {
5793   tree target_decl;
5794   bool is_weakref = false;
5795
5796   if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
5797     {
5798       tree alias = DECL_ASSEMBLER_NAME (decl);
5799
5800       is_weakref = true;
5801
5802       ultimate_transparent_alias_target (&target);
5803
5804       if (alias == target)
5805         error ("weakref %q+D ultimately targets itself", decl);
5806       else
5807         {
5808 #ifndef ASM_OUTPUT_WEAKREF
5809           IDENTIFIER_TRANSPARENT_ALIAS (alias) = 1;
5810           TREE_CHAIN (alias) = target;
5811 #endif
5812         }
5813       if (TREE_PUBLIC (decl))
5814         error ("weakref %q+D must have static linkage", decl);
5815     }
5816   else
5817     {
5818 #if !defined (ASM_OUTPUT_DEF)
5819 # if !defined(ASM_OUTPUT_WEAK_ALIAS) && !defined (ASM_WEAKEN_DECL)
5820       error_at (DECL_SOURCE_LOCATION (decl),
5821                 "alias definitions not supported in this configuration");
5822       return;
5823 # else
5824       if (!DECL_WEAK (decl))
5825         {
5826           if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
5827             error_at (DECL_SOURCE_LOCATION (decl),
5828                       "ifunc is not supported in this configuration");
5829           else  
5830             error_at (DECL_SOURCE_LOCATION (decl),
5831                       "only weak aliases are supported in this configuration");
5832           return;
5833         }
5834 # endif
5835 #endif
5836     }
5837   TREE_USED (decl) = 1;
5838
5839   /* A quirk of the initial implementation of aliases required that the user
5840      add "extern" to all of them.  Which is silly, but now historical.  Do
5841      note that the symbol is in fact locally defined.  */
5842   if (! is_weakref)
5843     DECL_EXTERNAL (decl) = 0;
5844
5845   /* Allow aliases to aliases.  */
5846   if (TREE_CODE (decl) == FUNCTION_DECL)
5847     cgraph_node (decl)->alias = true;
5848   else
5849     varpool_node (decl)->alias = true;
5850
5851   /* If the target has already been emitted, we don't have to queue the
5852      alias.  This saves a tad of memory.  */
5853   if (cgraph_global_info_ready)
5854     target_decl = find_decl_and_mark_needed (decl, target);
5855   else
5856     target_decl= NULL;
5857   if (target_decl && TREE_ASM_WRITTEN (target_decl))
5858     do_assemble_alias (decl, target);
5859   else
5860     {
5861       alias_pair *p = VEC_safe_push (alias_pair, gc, alias_pairs, NULL);
5862       p->decl = decl;
5863       p->target = target;
5864       p->emitted_diags = ALIAS_DIAG_NONE;
5865     }
5866 }
5867
5868 /* Emit an assembler directive to set symbol for DECL visibility to
5869    the visibility type VIS, which must not be VISIBILITY_DEFAULT.  */
5870
5871 void
5872 default_assemble_visibility (tree decl ATTRIBUTE_UNUSED, 
5873                              int vis ATTRIBUTE_UNUSED)
5874 {
5875 #ifdef HAVE_GAS_HIDDEN
5876   static const char * const visibility_types[] = {
5877     NULL, "protected", "hidden", "internal"
5878   };
5879
5880   const char *name, *type;
5881
5882   name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5883   type = visibility_types[vis];
5884
5885   fprintf (asm_out_file, "\t.%s\t", type);
5886   assemble_name (asm_out_file, name);
5887   fprintf (asm_out_file, "\n");
5888 #else
5889   warning (OPT_Wattributes, "visibility attribute not supported "
5890            "in this configuration; ignored");
5891 #endif
5892 }
5893
5894 /* A helper function to call assemble_visibility when needed for a decl.  */
5895
5896 int
5897 maybe_assemble_visibility (tree decl)
5898 {
5899   enum symbol_visibility vis = DECL_VISIBILITY (decl);
5900
5901   if (vis != VISIBILITY_DEFAULT)
5902     {
5903       targetm.asm_out.assemble_visibility (decl, vis);
5904       return 1;
5905     }
5906   else
5907     return 0;
5908 }
5909
5910 /* Returns 1 if the target configuration supports defining public symbols
5911    so that one of them will be chosen at link time instead of generating a
5912    multiply-defined symbol error, whether through the use of weak symbols or
5913    a target-specific mechanism for having duplicates discarded.  */
5914
5915 int
5916 supports_one_only (void)
5917 {
5918   if (SUPPORTS_ONE_ONLY)
5919     return 1;
5920   return TARGET_SUPPORTS_WEAK;
5921 }
5922
5923 /* Set up DECL as a public symbol that can be defined in multiple
5924    translation units without generating a linker error.  */
5925
5926 void
5927 make_decl_one_only (tree decl, tree comdat_group)
5928 {
5929   gcc_assert (TREE_CODE (decl) == VAR_DECL
5930               || TREE_CODE (decl) == FUNCTION_DECL);
5931
5932   TREE_PUBLIC (decl) = 1;
5933
5934   if (SUPPORTS_ONE_ONLY)
5935     {
5936 #ifdef MAKE_DECL_ONE_ONLY
5937       MAKE_DECL_ONE_ONLY (decl);
5938 #endif
5939       DECL_COMDAT_GROUP (decl) = comdat_group;
5940     }
5941   else if (TREE_CODE (decl) == VAR_DECL
5942       && (DECL_INITIAL (decl) == 0 || DECL_INITIAL (decl) == error_mark_node))
5943     DECL_COMMON (decl) = 1;
5944   else
5945     {
5946       gcc_assert (TARGET_SUPPORTS_WEAK);
5947       DECL_WEAK (decl) = 1;
5948     }
5949 }
5950
5951 void
5952 init_varasm_once (void)
5953 {
5954   section_htab = htab_create_ggc (31, section_entry_hash,
5955                                   section_entry_eq, NULL);
5956   object_block_htab = htab_create_ggc (31, object_block_entry_hash,
5957                                        object_block_entry_eq, NULL);
5958   const_desc_htab = htab_create_ggc (1009, const_desc_hash,
5959                                      const_desc_eq, NULL);
5960
5961   const_alias_set = new_alias_set ();
5962   shared_constant_pool = create_constant_pool ();
5963
5964 #ifdef TEXT_SECTION_ASM_OP
5965   text_section = get_unnamed_section (SECTION_CODE, output_section_asm_op,
5966                                       TEXT_SECTION_ASM_OP);
5967 #endif
5968
5969 #ifdef DATA_SECTION_ASM_OP
5970   data_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
5971                                       DATA_SECTION_ASM_OP);
5972 #endif
5973
5974 #ifdef SDATA_SECTION_ASM_OP
5975   sdata_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
5976                                        SDATA_SECTION_ASM_OP);
5977 #endif
5978
5979 #ifdef READONLY_DATA_SECTION_ASM_OP
5980   readonly_data_section = get_unnamed_section (0, output_section_asm_op,
5981                                                READONLY_DATA_SECTION_ASM_OP);
5982 #endif
5983
5984 #ifdef CTORS_SECTION_ASM_OP
5985   ctors_section = get_unnamed_section (0, output_section_asm_op,
5986                                        CTORS_SECTION_ASM_OP);
5987 #endif
5988
5989 #ifdef DTORS_SECTION_ASM_OP
5990   dtors_section = get_unnamed_section (0, output_section_asm_op,
5991                                        DTORS_SECTION_ASM_OP);
5992 #endif
5993
5994 #ifdef BSS_SECTION_ASM_OP
5995   bss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
5996                                      output_section_asm_op,
5997                                      BSS_SECTION_ASM_OP);
5998 #endif
5999
6000 #ifdef SBSS_SECTION_ASM_OP
6001   sbss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
6002                                       output_section_asm_op,
6003                                       SBSS_SECTION_ASM_OP);
6004 #endif
6005
6006   tls_comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6007                                            | SECTION_COMMON, emit_tls_common);
6008   lcomm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6009                                         | SECTION_COMMON, emit_local);
6010   comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6011                                        | SECTION_COMMON, emit_common);
6012
6013 #if defined ASM_OUTPUT_ALIGNED_BSS || defined ASM_OUTPUT_BSS
6014   bss_noswitch_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS,
6015                                                emit_bss);
6016 #endif
6017
6018   targetm.asm_out.init_sections ();
6019
6020   if (readonly_data_section == NULL)
6021     readonly_data_section = text_section;
6022 }
6023
6024 enum tls_model
6025 decl_default_tls_model (const_tree decl)
6026 {
6027   enum tls_model kind;
6028   bool is_local;
6029
6030   is_local = targetm.binds_local_p (decl);
6031   if (!flag_shlib)
6032     {
6033       if (is_local)
6034         kind = TLS_MODEL_LOCAL_EXEC;
6035       else
6036         kind = TLS_MODEL_INITIAL_EXEC;
6037     }
6038
6039   /* Local dynamic is inefficient when we're not combining the
6040      parts of the address.  */
6041   else if (optimize && is_local)
6042     kind = TLS_MODEL_LOCAL_DYNAMIC;
6043   else
6044     kind = TLS_MODEL_GLOBAL_DYNAMIC;
6045   if (kind < flag_tls_default)
6046     kind = flag_tls_default;
6047
6048   return kind;
6049 }
6050
6051 /* Select a set of attributes for section NAME based on the properties
6052    of DECL and whether or not RELOC indicates that DECL's initializer
6053    might contain runtime relocations.
6054
6055    We make the section read-only and executable for a function decl,
6056    read-only for a const data decl, and writable for a non-const data decl.  */
6057
6058 unsigned int
6059 default_section_type_flags (tree decl, const char *name, int reloc)
6060 {
6061   unsigned int flags;
6062
6063   if (decl && TREE_CODE (decl) == FUNCTION_DECL)
6064     flags = SECTION_CODE;
6065   else if (decl)
6066     {
6067       enum section_category category
6068         = categorize_decl_for_section (decl, reloc);
6069       if (decl_readonly_section_1 (category))
6070         flags = 0;
6071       else if (category == SECCAT_DATA_REL_RO
6072                || category == SECCAT_DATA_REL_RO_LOCAL)
6073         flags = SECTION_WRITE | SECTION_RELRO;
6074       else
6075         flags = SECTION_WRITE;
6076     }
6077   else
6078     {
6079       flags = SECTION_WRITE;
6080       if (strcmp (name, ".data.rel.ro") == 0
6081           || strcmp (name, ".data.rel.ro.local") == 0)
6082         flags |= SECTION_RELRO;
6083     }
6084
6085   if (decl && DECL_ONE_ONLY (decl))
6086     flags |= SECTION_LINKONCE;
6087
6088   if (decl && TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
6089     flags |= SECTION_TLS | SECTION_WRITE;
6090
6091   if (strcmp (name, ".bss") == 0
6092       || strncmp (name, ".bss.", 5) == 0
6093       || strncmp (name, ".gnu.linkonce.b.", 16) == 0
6094       || strcmp (name, ".sbss") == 0
6095       || strncmp (name, ".sbss.", 6) == 0
6096       || strncmp (name, ".gnu.linkonce.sb.", 17) == 0)
6097     flags |= SECTION_BSS;
6098
6099   if (strcmp (name, ".tdata") == 0
6100       || strncmp (name, ".tdata.", 7) == 0
6101       || strncmp (name, ".gnu.linkonce.td.", 17) == 0)
6102     flags |= SECTION_TLS;
6103
6104   if (strcmp (name, ".tbss") == 0
6105       || strncmp (name, ".tbss.", 6) == 0
6106       || strncmp (name, ".gnu.linkonce.tb.", 17) == 0)
6107     flags |= SECTION_TLS | SECTION_BSS;
6108
6109   /* These three sections have special ELF types.  They are neither
6110      SHT_PROGBITS nor SHT_NOBITS, so when changing sections we don't
6111      want to print a section type (@progbits or @nobits).  If someone
6112      is silly enough to emit code or TLS variables to one of these
6113      sections, then don't handle them specially.  */
6114   if (!(flags & (SECTION_CODE | SECTION_BSS | SECTION_TLS))
6115       && (strcmp (name, ".init_array") == 0
6116           || strcmp (name, ".fini_array") == 0
6117           || strcmp (name, ".preinit_array") == 0))
6118     flags |= SECTION_NOTYPE;
6119
6120   return flags;
6121 }
6122
6123 /* Return true if the target supports some form of global BSS,
6124    either through bss_noswitch_section, or by selecting a BSS
6125    section in TARGET_ASM_SELECT_SECTION.  */
6126
6127 bool
6128 have_global_bss_p (void)
6129 {
6130   return bss_noswitch_section || targetm.have_switchable_bss_sections;
6131 }
6132
6133 /* Output assembly to switch to section NAME with attribute FLAGS.
6134    Four variants for common object file formats.  */
6135
6136 void
6137 default_no_named_section (const char *name ATTRIBUTE_UNUSED,
6138                           unsigned int flags ATTRIBUTE_UNUSED,
6139                           tree decl ATTRIBUTE_UNUSED)
6140 {
6141   /* Some object formats don't support named sections at all.  The
6142      front-end should already have flagged this as an error.  */
6143   gcc_unreachable ();
6144 }
6145
6146 #ifndef TLS_SECTION_ASM_FLAG
6147 #define TLS_SECTION_ASM_FLAG 'T'
6148 #endif
6149
6150 void
6151 default_elf_asm_named_section (const char *name, unsigned int flags,
6152                                tree decl ATTRIBUTE_UNUSED)
6153 {
6154   char flagchars[10], *f = flagchars;
6155
6156   /* If we have already declared this section, we can use an
6157      abbreviated form to switch back to it -- unless this section is
6158      part of a COMDAT groups, in which case GAS requires the full
6159      declaration every time.  */
6160   if (!(HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6161       && (flags & SECTION_DECLARED))
6162     {
6163       fprintf (asm_out_file, "\t.section\t%s\n", name);
6164       return;
6165     }
6166
6167   if (!(flags & SECTION_DEBUG))
6168     *f++ = 'a';
6169   if (flags & SECTION_WRITE)
6170     *f++ = 'w';
6171   if (flags & SECTION_CODE)
6172     *f++ = 'x';
6173   if (flags & SECTION_SMALL)
6174     *f++ = 's';
6175   if (flags & SECTION_MERGE)
6176     *f++ = 'M';
6177   if (flags & SECTION_STRINGS)
6178     *f++ = 'S';
6179   if (flags & SECTION_TLS)
6180     *f++ = TLS_SECTION_ASM_FLAG;
6181   if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6182     *f++ = 'G';
6183   *f = '\0';
6184
6185   fprintf (asm_out_file, "\t.section\t%s,\"%s\"", name, flagchars);
6186
6187   if (!(flags & SECTION_NOTYPE))
6188     {
6189       const char *type;
6190       const char *format;
6191
6192       if (flags & SECTION_BSS)
6193         type = "nobits";
6194       else
6195         type = "progbits";
6196
6197       format = ",@%s";
6198 #ifdef ASM_COMMENT_START
6199       /* On platforms that use "@" as the assembly comment character,
6200          use "%" instead.  */
6201       if (strcmp (ASM_COMMENT_START, "@") == 0)
6202         format = ",%%%s";
6203 #endif
6204       fprintf (asm_out_file, format, type);
6205
6206       if (flags & SECTION_ENTSIZE)
6207         fprintf (asm_out_file, ",%d", flags & SECTION_ENTSIZE);
6208       if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6209         {
6210           if (TREE_CODE (decl) == IDENTIFIER_NODE)
6211             fprintf (asm_out_file, ",%s,comdat", IDENTIFIER_POINTER (decl));
6212           else
6213             fprintf (asm_out_file, ",%s,comdat",
6214                      IDENTIFIER_POINTER (DECL_COMDAT_GROUP (decl)));
6215         }
6216     }
6217
6218   putc ('\n', asm_out_file);
6219 }
6220
6221 void
6222 default_coff_asm_named_section (const char *name, unsigned int flags,
6223                                 tree decl ATTRIBUTE_UNUSED)
6224 {
6225   char flagchars[8], *f = flagchars;
6226
6227   if (flags & SECTION_WRITE)
6228     *f++ = 'w';
6229   if (flags & SECTION_CODE)
6230     *f++ = 'x';
6231   *f = '\0';
6232
6233   fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
6234 }
6235
6236 void
6237 default_pe_asm_named_section (const char *name, unsigned int flags,
6238                               tree decl)
6239 {
6240   default_coff_asm_named_section (name, flags, decl);
6241
6242   if (flags & SECTION_LINKONCE)
6243     {
6244       /* Functions may have been compiled at various levels of
6245          optimization so we can't use `same_size' here.
6246          Instead, have the linker pick one.  */
6247       fprintf (asm_out_file, "\t.linkonce %s\n",
6248                (flags & SECTION_CODE ? "discard" : "same_size"));
6249     }
6250 }
6251 \f
6252 /* The lame default section selector.  */
6253
6254 section *
6255 default_select_section (tree decl, int reloc,
6256                         unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
6257 {
6258   if (DECL_P (decl))
6259     {
6260       if (decl_readonly_section (decl, reloc))
6261         return readonly_data_section;
6262     }
6263   else if (TREE_CODE (decl) == CONSTRUCTOR)
6264     {
6265       if (! ((flag_pic && reloc)
6266              || !TREE_READONLY (decl)
6267              || TREE_SIDE_EFFECTS (decl)
6268              || !TREE_CONSTANT (decl)))
6269         return readonly_data_section;
6270     }
6271   else if (TREE_CODE (decl) == STRING_CST)
6272     return readonly_data_section;
6273   else if (! (flag_pic && reloc))
6274     return readonly_data_section;
6275
6276   return data_section;
6277 }
6278
6279 enum section_category
6280 categorize_decl_for_section (const_tree decl, int reloc)
6281 {
6282   enum section_category ret;
6283
6284   if (TREE_CODE (decl) == FUNCTION_DECL)
6285     return SECCAT_TEXT;
6286   else if (TREE_CODE (decl) == STRING_CST)
6287     {
6288       if (flag_mudflap) /* or !flag_merge_constants */
6289         return SECCAT_RODATA;
6290       else
6291         return SECCAT_RODATA_MERGE_STR;
6292     }
6293   else if (TREE_CODE (decl) == VAR_DECL)
6294     {
6295       if (bss_initializer_p (decl))
6296         ret = SECCAT_BSS;
6297       else if (! TREE_READONLY (decl)
6298                || TREE_SIDE_EFFECTS (decl)
6299                || ! TREE_CONSTANT (DECL_INITIAL (decl)))
6300         {
6301           /* Here the reloc_rw_mask is not testing whether the section should
6302              be read-only or not, but whether the dynamic link will have to
6303              do something.  If so, we wish to segregate the data in order to
6304              minimize cache misses inside the dynamic linker.  */
6305           if (reloc & targetm.asm_out.reloc_rw_mask ())
6306             ret = reloc == 1 ? SECCAT_DATA_REL_LOCAL : SECCAT_DATA_REL;
6307           else
6308             ret = SECCAT_DATA;
6309         }
6310       else if (reloc & targetm.asm_out.reloc_rw_mask ())
6311         ret = reloc == 1 ? SECCAT_DATA_REL_RO_LOCAL : SECCAT_DATA_REL_RO;
6312       else if (reloc || flag_merge_constants < 2)
6313         /* C and C++ don't allow different variables to share the same
6314            location.  -fmerge-all-constants allows even that (at the
6315            expense of not conforming).  */
6316         ret = SECCAT_RODATA;
6317       else if (TREE_CODE (DECL_INITIAL (decl)) == STRING_CST)
6318         ret = SECCAT_RODATA_MERGE_STR_INIT;
6319       else
6320         ret = SECCAT_RODATA_MERGE_CONST;
6321     }
6322   else if (TREE_CODE (decl) == CONSTRUCTOR)
6323     {
6324       if ((reloc & targetm.asm_out.reloc_rw_mask ())
6325           || TREE_SIDE_EFFECTS (decl)
6326           || ! TREE_CONSTANT (decl))
6327         ret = SECCAT_DATA;
6328       else
6329         ret = SECCAT_RODATA;
6330     }
6331   else
6332     ret = SECCAT_RODATA;
6333
6334   /* There are no read-only thread-local sections.  */
6335   if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
6336     {
6337       /* Note that this would be *just* SECCAT_BSS, except that there's
6338          no concept of a read-only thread-local-data section.  */
6339       if (ret == SECCAT_BSS
6340                || (flag_zero_initialized_in_bss
6341                    && initializer_zerop (DECL_INITIAL (decl))))
6342         ret = SECCAT_TBSS;
6343       else
6344         ret = SECCAT_TDATA;
6345     }
6346
6347   /* If the target uses small data sections, select it.  */
6348   else if (targetm.in_small_data_p (decl))
6349     {
6350       if (ret == SECCAT_BSS)
6351         ret = SECCAT_SBSS;
6352       else if (targetm.have_srodata_section && ret == SECCAT_RODATA)
6353         ret = SECCAT_SRODATA;
6354       else
6355         ret = SECCAT_SDATA;
6356     }
6357
6358   return ret;
6359 }
6360
6361 static bool
6362 decl_readonly_section_1 (enum section_category category)
6363 {
6364   switch (category)
6365     {
6366     case SECCAT_RODATA:
6367     case SECCAT_RODATA_MERGE_STR:
6368     case SECCAT_RODATA_MERGE_STR_INIT:
6369     case SECCAT_RODATA_MERGE_CONST:
6370     case SECCAT_SRODATA:
6371       return true;
6372     default:
6373       return false;
6374     }
6375 }
6376
6377 bool
6378 decl_readonly_section (const_tree decl, int reloc)
6379 {
6380   return decl_readonly_section_1 (categorize_decl_for_section (decl, reloc));
6381 }
6382
6383 /* Select a section based on the above categorization.  */
6384
6385 section *
6386 default_elf_select_section (tree decl, int reloc,
6387                             unsigned HOST_WIDE_INT align)
6388 {
6389   const char *sname;
6390   switch (categorize_decl_for_section (decl, reloc))
6391     {
6392     case SECCAT_TEXT:
6393       /* We're not supposed to be called on FUNCTION_DECLs.  */
6394       gcc_unreachable ();
6395     case SECCAT_RODATA:
6396       return readonly_data_section;
6397     case SECCAT_RODATA_MERGE_STR:
6398       return mergeable_string_section (decl, align, 0);
6399     case SECCAT_RODATA_MERGE_STR_INIT:
6400       return mergeable_string_section (DECL_INITIAL (decl), align, 0);
6401     case SECCAT_RODATA_MERGE_CONST:
6402       return mergeable_constant_section (DECL_MODE (decl), align, 0);
6403     case SECCAT_SRODATA:
6404       sname = ".sdata2";
6405       break;
6406     case SECCAT_DATA:
6407       return data_section;
6408     case SECCAT_DATA_REL:
6409       sname = ".data.rel";
6410       break;
6411     case SECCAT_DATA_REL_LOCAL:
6412       sname = ".data.rel.local";
6413       break;
6414     case SECCAT_DATA_REL_RO:
6415       sname = ".data.rel.ro";
6416       break;
6417     case SECCAT_DATA_REL_RO_LOCAL:
6418       sname = ".data.rel.ro.local";
6419       break;
6420     case SECCAT_SDATA:
6421       sname = ".sdata";
6422       break;
6423     case SECCAT_TDATA:
6424       sname = ".tdata";
6425       break;
6426     case SECCAT_BSS:
6427       if (bss_section)
6428         return bss_section;
6429       sname = ".bss";
6430       break;
6431     case SECCAT_SBSS:
6432       sname = ".sbss";
6433       break;
6434     case SECCAT_TBSS:
6435       sname = ".tbss";
6436       break;
6437     default:
6438       gcc_unreachable ();
6439     }
6440
6441   if (!DECL_P (decl))
6442     decl = NULL_TREE;
6443   return get_named_section (decl, sname, reloc);
6444 }
6445
6446 /* Construct a unique section name based on the decl name and the
6447    categorization performed above.  */
6448
6449 void
6450 default_unique_section (tree decl, int reloc)
6451 {
6452   /* We only need to use .gnu.linkonce if we don't have COMDAT groups.  */
6453   bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
6454   const char *prefix, *name, *linkonce;
6455   char *string;
6456
6457   switch (categorize_decl_for_section (decl, reloc))
6458     {
6459     case SECCAT_TEXT:
6460       prefix = one_only ? ".t" : ".text";
6461       break;
6462     case SECCAT_RODATA:
6463     case SECCAT_RODATA_MERGE_STR:
6464     case SECCAT_RODATA_MERGE_STR_INIT:
6465     case SECCAT_RODATA_MERGE_CONST:
6466       prefix = one_only ? ".r" : ".rodata";
6467       break;
6468     case SECCAT_SRODATA:
6469       prefix = one_only ? ".s2" : ".sdata2";
6470       break;
6471     case SECCAT_DATA:
6472       prefix = one_only ? ".d" : ".data";
6473       break;
6474     case SECCAT_DATA_REL:
6475       prefix = one_only ? ".d.rel" : ".data.rel";
6476       break;
6477     case SECCAT_DATA_REL_LOCAL:
6478       prefix = one_only ? ".d.rel.local" : ".data.rel.local";
6479       break;
6480     case SECCAT_DATA_REL_RO:
6481       prefix = one_only ? ".d.rel.ro" : ".data.rel.ro";
6482       break;
6483     case SECCAT_DATA_REL_RO_LOCAL:
6484       prefix = one_only ? ".d.rel.ro.local" : ".data.rel.ro.local";
6485       break;
6486     case SECCAT_SDATA:
6487       prefix = one_only ? ".s" : ".sdata";
6488       break;
6489     case SECCAT_BSS:
6490       prefix = one_only ? ".b" : ".bss";
6491       break;
6492     case SECCAT_SBSS:
6493       prefix = one_only ? ".sb" : ".sbss";
6494       break;
6495     case SECCAT_TDATA:
6496       prefix = one_only ? ".td" : ".tdata";
6497       break;
6498     case SECCAT_TBSS:
6499       prefix = one_only ? ".tb" : ".tbss";
6500       break;
6501     default:
6502       gcc_unreachable ();
6503     }
6504
6505   name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
6506   name = targetm.strip_name_encoding (name);
6507
6508   /* If we're using one_only, then there needs to be a .gnu.linkonce
6509      prefix to the section name.  */
6510   linkonce = one_only ? ".gnu.linkonce" : "";
6511
6512   string = ACONCAT ((linkonce, prefix, ".", name, NULL));
6513
6514   DECL_SECTION_NAME (decl) = build_string (strlen (string), string);
6515 }
6516
6517 /* Like compute_reloc_for_constant, except for an RTX.  The return value
6518    is a mask for which bit 1 indicates a global relocation, and bit 0
6519    indicates a local relocation.  */
6520
6521 static int
6522 compute_reloc_for_rtx_1 (rtx *xp, void *data)
6523 {
6524   int *preloc = (int *) data;
6525   rtx x = *xp;
6526
6527   switch (GET_CODE (x))
6528     {
6529     case SYMBOL_REF:
6530       *preloc |= SYMBOL_REF_LOCAL_P (x) ? 1 : 2;
6531       break;
6532     case LABEL_REF:
6533       *preloc |= 1;
6534       break;
6535     default:
6536       break;
6537     }
6538
6539   return 0;
6540 }
6541
6542 static int
6543 compute_reloc_for_rtx (rtx x)
6544 {
6545   int reloc;
6546
6547   switch (GET_CODE (x))
6548     {
6549     case CONST:
6550     case SYMBOL_REF:
6551     case LABEL_REF:
6552       reloc = 0;
6553       for_each_rtx (&x, compute_reloc_for_rtx_1, &reloc);
6554       return reloc;
6555
6556     default:
6557       return 0;
6558     }
6559 }
6560
6561 section *
6562 default_select_rtx_section (enum machine_mode mode ATTRIBUTE_UNUSED,
6563                             rtx x,
6564                             unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
6565 {
6566   if (compute_reloc_for_rtx (x) & targetm.asm_out.reloc_rw_mask ())
6567     return data_section;
6568   else
6569     return readonly_data_section;
6570 }
6571
6572 section *
6573 default_elf_select_rtx_section (enum machine_mode mode, rtx x,
6574                                 unsigned HOST_WIDE_INT align)
6575 {
6576   int reloc = compute_reloc_for_rtx (x);
6577
6578   /* ??? Handle small data here somehow.  */
6579
6580   if (reloc & targetm.asm_out.reloc_rw_mask ())
6581     {
6582       if (reloc == 1)
6583         return get_named_section (NULL, ".data.rel.ro.local", 1);
6584       else
6585         return get_named_section (NULL, ".data.rel.ro", 3);
6586     }
6587
6588   return mergeable_constant_section (mode, align, 0);
6589 }
6590
6591 /* Set the generally applicable flags on the SYMBOL_REF for EXP.  */
6592
6593 void
6594 default_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
6595 {
6596   rtx symbol;
6597   int flags;
6598
6599   /* Careful not to prod global register variables.  */
6600   if (!MEM_P (rtl))
6601     return;
6602   symbol = XEXP (rtl, 0);
6603   if (GET_CODE (symbol) != SYMBOL_REF)
6604     return;
6605
6606   flags = SYMBOL_REF_FLAGS (symbol) & SYMBOL_FLAG_HAS_BLOCK_INFO;
6607   if (TREE_CODE (decl) == FUNCTION_DECL)
6608     flags |= SYMBOL_FLAG_FUNCTION;
6609   if (targetm.binds_local_p (decl))
6610     flags |= SYMBOL_FLAG_LOCAL;
6611   if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
6612     flags |= DECL_TLS_MODEL (decl) << SYMBOL_FLAG_TLS_SHIFT;
6613   else if (targetm.in_small_data_p (decl))
6614     flags |= SYMBOL_FLAG_SMALL;
6615   /* ??? Why is DECL_EXTERNAL ever set for non-PUBLIC names?  Without
6616      being PUBLIC, the thing *must* be defined in this translation unit.
6617      Prevent this buglet from being propagated into rtl code as well.  */
6618   if (DECL_P (decl) && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl))
6619     flags |= SYMBOL_FLAG_EXTERNAL;
6620
6621   SYMBOL_REF_FLAGS (symbol) = flags;
6622 }
6623
6624 /* By default, we do nothing for encode_section_info, so we need not
6625    do anything but discard the '*' marker.  */
6626
6627 const char *
6628 default_strip_name_encoding (const char *str)
6629 {
6630   return str + (*str == '*');
6631 }
6632
6633 #ifdef ASM_OUTPUT_DEF
6634 /* The default implementation of TARGET_ASM_OUTPUT_ANCHOR.  Define the
6635    anchor relative to ".", the current section position.  */
6636
6637 void
6638 default_asm_output_anchor (rtx symbol)
6639 {
6640   char buffer[100];
6641
6642   sprintf (buffer, "*. + " HOST_WIDE_INT_PRINT_DEC,
6643            SYMBOL_REF_BLOCK_OFFSET (symbol));
6644   ASM_OUTPUT_DEF (asm_out_file, XSTR (symbol, 0), buffer);
6645 }
6646 #endif
6647
6648 /* The default implementation of TARGET_USE_ANCHORS_FOR_SYMBOL_P.  */
6649
6650 bool
6651 default_use_anchors_for_symbol_p (const_rtx symbol)
6652 {
6653   section *sect;
6654   tree decl;
6655
6656   /* Don't use anchors for mergeable sections.  The linker might move
6657      the objects around.  */
6658   sect = SYMBOL_REF_BLOCK (symbol)->sect;
6659   if (sect->common.flags & SECTION_MERGE)
6660     return false;
6661
6662   /* Don't use anchors for small data sections.  The small data register
6663      acts as an anchor for such sections.  */
6664   if (sect->common.flags & SECTION_SMALL)
6665     return false;
6666
6667   decl = SYMBOL_REF_DECL (symbol);
6668   if (decl && DECL_P (decl))
6669     {
6670       /* Don't use section anchors for decls that might be defined by
6671          other modules.  */
6672       if (!targetm.binds_local_p (decl))
6673         return false;
6674
6675       /* Don't use section anchors for decls that will be placed in a
6676          small data section.  */
6677       /* ??? Ideally, this check would be redundant with the SECTION_SMALL
6678          one above.  The problem is that we only use SECTION_SMALL for
6679          sections that should be marked as small in the section directive.  */
6680       if (targetm.in_small_data_p (decl))
6681         return false;
6682     }
6683   return true;
6684 }
6685
6686 /* Return true when RESOLUTION indicate that symbol will be bound to the
6687    definition provided by current .o file.  */
6688
6689 static bool
6690 resolution_to_local_definition_p (enum ld_plugin_symbol_resolution resolution)
6691 {
6692   return (resolution == LDPR_PREVAILING_DEF
6693           || resolution == LDPR_PREVAILING_DEF_IRONLY);
6694 }
6695
6696 /* Return true when RESOLUTION indicate that symbol will be bound locally
6697    within current executable or DSO.  */
6698
6699 static bool
6700 resolution_local_p (enum ld_plugin_symbol_resolution resolution)
6701 {
6702   return (resolution == LDPR_PREVAILING_DEF
6703           || resolution == LDPR_PREVAILING_DEF_IRONLY
6704           || resolution == LDPR_PREEMPTED_REG
6705           || resolution == LDPR_PREEMPTED_IR
6706           || resolution == LDPR_RESOLVED_IR
6707           || resolution == LDPR_RESOLVED_EXEC);
6708 }
6709
6710 /* Assume ELF-ish defaults, since that's pretty much the most liberal
6711    wrt cross-module name binding.  */
6712
6713 bool
6714 default_binds_local_p (const_tree exp)
6715 {
6716   return default_binds_local_p_1 (exp, flag_shlib);
6717 }
6718
6719 bool
6720 default_binds_local_p_1 (const_tree exp, int shlib)
6721 {
6722   bool local_p;
6723   bool resolved_locally = false;
6724   bool resolved_to_local_def = false;
6725
6726   /* With resolution file in hands, take look into resolutions.
6727      We can't just return true for resolved_localy symbols,
6728      because dynamic linking might overwrite symbols
6729      in shared libraries.  */
6730   if (TREE_CODE (exp) == VAR_DECL && TREE_PUBLIC (exp)
6731       && (TREE_STATIC (exp) || DECL_EXTERNAL (exp)))
6732     {
6733       struct varpool_node *vnode = varpool_get_node (exp);
6734       if (vnode && resolution_local_p (vnode->resolution))
6735         resolved_locally = true;
6736       if (vnode
6737           && resolution_to_local_definition_p (vnode->resolution))
6738         resolved_to_local_def = true;
6739     }
6740   else if (TREE_CODE (exp) == FUNCTION_DECL && TREE_PUBLIC (exp))
6741     {
6742       struct cgraph_node *node = cgraph_get_node_or_alias (exp);
6743       if (node
6744           && resolution_local_p (node->resolution))
6745         resolved_locally = true;
6746       if (node
6747           && resolution_to_local_definition_p (node->resolution))
6748         resolved_to_local_def = true;
6749     }
6750
6751   /* A non-decl is an entry in the constant pool.  */
6752   if (!DECL_P (exp))
6753     local_p = true;
6754   /* Weakrefs may not bind locally, even though the weakref itself is always
6755      static and therefore local.  Similarly, the resolver for ifunc functions
6756      might resolve to a non-local function.
6757      FIXME: We can resolve the weakref case more curefuly by looking at the
6758      weakref alias.  */
6759   else if (lookup_attribute ("weakref", DECL_ATTRIBUTES (exp))
6760            || (TREE_CODE (exp) == FUNCTION_DECL
6761                && lookup_attribute ("ifunc", DECL_ATTRIBUTES (exp))))
6762     local_p = false;
6763   /* Static variables are always local.  */
6764   else if (! TREE_PUBLIC (exp))
6765     local_p = true;
6766   /* A variable is local if the user has said explicitly that it will
6767      be.  */
6768   else if ((DECL_VISIBILITY_SPECIFIED (exp)
6769             || resolved_to_local_def)
6770            && DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
6771     local_p = true;
6772   /* Variables defined outside this object might not be local.  */
6773   else if (DECL_EXTERNAL (exp) && !resolved_locally)
6774     local_p = false;
6775   /* If defined in this object and visibility is not default, must be
6776      local.  */
6777   else if (DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
6778     local_p = true;
6779   /* Default visibility weak data can be overridden by a strong symbol
6780      in another module and so are not local.  */
6781   else if (DECL_WEAK (exp)
6782            && !resolved_locally)
6783     local_p = false;
6784   /* If PIC, then assume that any global name can be overridden by
6785      symbols resolved from other modules.  */
6786   else if (shlib)
6787     local_p = false;
6788   /* Uninitialized COMMON variable may be unified with symbols
6789      resolved from other modules.  */
6790   else if (DECL_COMMON (exp)
6791            && !resolved_locally
6792            && (DECL_INITIAL (exp) == NULL
6793                || DECL_INITIAL (exp) == error_mark_node))
6794     local_p = false;
6795   /* Otherwise we're left with initialized (or non-common) global data
6796      which is of necessity defined locally.  */
6797   else
6798     local_p = true;
6799
6800   return local_p;
6801 }
6802
6803 /* Return true when references to DECL must bind to current definition in
6804    final executable.
6805
6806    The condition is usually equivalent to whether the function binds to the
6807    current module (shared library or executable), that is to binds_local_p.
6808    We use this fact to avoid need for another target hook and implement
6809    the logic using binds_local_p and just special cases where
6810    decl_binds_to_current_def_p is stronger than binds local_p.  In particular
6811    the weak definitions (that can be overwritten at linktime by other
6812    definition from different object file) and when resolution info is available
6813    we simply use the knowledge passed to us by linker plugin.  */
6814 bool
6815 decl_binds_to_current_def_p (tree decl)
6816 {
6817   gcc_assert (DECL_P (decl));
6818   if (!TREE_PUBLIC (decl))
6819     return true;
6820   if (!targetm.binds_local_p (decl))
6821     return false;
6822   /* When resolution is available, just use it.  */
6823   if (TREE_CODE (decl) == VAR_DECL && TREE_PUBLIC (decl)
6824       && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
6825     {
6826       struct varpool_node *vnode = varpool_get_node (decl);
6827       if (vnode
6828           && vnode->resolution != LDPR_UNKNOWN)
6829         return resolution_to_local_definition_p (vnode->resolution);
6830     }
6831   else if (TREE_CODE (decl) == FUNCTION_DECL && TREE_PUBLIC (decl))
6832     {
6833       struct cgraph_node *node = cgraph_get_node_or_alias (decl);
6834       if (node
6835           && node->resolution != LDPR_UNKNOWN)
6836         return resolution_to_local_definition_p (node->resolution);
6837     }
6838   /* Otherwise we have to assume the worst for DECL_WEAK (hidden weaks
6839      binds localy but still can be overwritten).
6840      This rely on fact that binds_local_p behave as decl_replaceable_p
6841      for all other declaration types.  */
6842   return !DECL_WEAK (decl);
6843 }
6844
6845 /* A replaceable function or variable is one which may be replaced
6846    at link-time with an entirely different definition, provided that the
6847    replacement has the same type.  For example, functions declared
6848    with __attribute__((weak)) on most systems are replaceable.
6849
6850    COMDAT functions are not replaceable, since all definitions of the
6851    function must be equivalent.  It is important that COMDAT functions
6852    not be treated as replaceable so that use of C++ template
6853    instantiations is not penalized.  */
6854
6855 bool
6856 decl_replaceable_p (tree decl)
6857 {
6858   gcc_assert (DECL_P (decl));
6859   if (!TREE_PUBLIC (decl) || DECL_COMDAT (decl))
6860     return false;
6861   return !decl_binds_to_current_def_p (decl);
6862 }
6863
6864 /* Default function to output code that will globalize a label.  A
6865    target must define GLOBAL_ASM_OP or provide its own function to
6866    globalize a label.  */
6867 #ifdef GLOBAL_ASM_OP
6868 void
6869 default_globalize_label (FILE * stream, const char *name)
6870 {
6871   fputs (GLOBAL_ASM_OP, stream);
6872   assemble_name (stream, name);
6873   putc ('\n', stream);
6874 }
6875 #endif /* GLOBAL_ASM_OP */
6876
6877 /* Default function to output code that will globalize a declaration.  */
6878 void
6879 default_globalize_decl_name (FILE * stream, tree decl)
6880 {
6881   const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
6882   targetm.asm_out.globalize_label (stream, name);
6883 }
6884
6885 /* Default function to output a label for unwind information.  The
6886    default is to do nothing.  A target that needs nonlocal labels for
6887    unwind information must provide its own function to do this.  */
6888 void
6889 default_emit_unwind_label (FILE * stream ATTRIBUTE_UNUSED,
6890                            tree decl ATTRIBUTE_UNUSED,
6891                            int for_eh ATTRIBUTE_UNUSED,
6892                            int empty ATTRIBUTE_UNUSED)
6893 {
6894 }
6895
6896 /* Default function to output a label to divide up the exception table.
6897    The default is to do nothing.  A target that needs/wants to divide
6898    up the table must provide it's own function to do this.  */
6899 void
6900 default_emit_except_table_label (FILE * stream ATTRIBUTE_UNUSED)
6901 {
6902 }
6903
6904 /* This is how to output an internal numbered label where PREFIX is
6905    the class of label and LABELNO is the number within the class.  */
6906
6907 void
6908 default_generate_internal_label (char *buf, const char *prefix,
6909                                  unsigned long labelno)
6910 {
6911   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
6912 }
6913
6914 /* This is how to output an internal numbered label where PREFIX is
6915    the class of label and LABELNO is the number within the class.  */
6916
6917 void
6918 default_internal_label (FILE *stream, const char *prefix,
6919                         unsigned long labelno)
6920 {
6921   char *const buf = (char *) alloca (40 + strlen (prefix));
6922   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
6923   ASM_OUTPUT_INTERNAL_LABEL (stream, buf);
6924 }
6925
6926
6927 /* The default implementation of ASM_DECLARE_CONSTANT_NAME.  */
6928
6929 void
6930 default_asm_declare_constant_name (FILE *file, const char *name,
6931                                    const_tree exp ATTRIBUTE_UNUSED,
6932                                    HOST_WIDE_INT size ATTRIBUTE_UNUSED)
6933 {
6934   assemble_label (file, name);
6935 }
6936
6937 /* This is the default behavior at the beginning of a file.  It's
6938    controlled by two other target-hook toggles.  */
6939 void
6940 default_file_start (void)
6941 {
6942   if (targetm.asm_file_start_app_off
6943       && !(flag_verbose_asm || flag_debug_asm || flag_dump_rtl_in_asm))
6944     fputs (ASM_APP_OFF, asm_out_file);
6945
6946   if (targetm.asm_file_start_file_directive)
6947     output_file_directive (asm_out_file, main_input_filename);
6948 }
6949
6950 /* This is a generic routine suitable for use as TARGET_ASM_FILE_END
6951    which emits a special section directive used to indicate whether or
6952    not this object file needs an executable stack.  This is primarily
6953    a GNU extension to ELF but could be used on other targets.  */
6954
6955 int trampolines_created;
6956
6957 void
6958 file_end_indicate_exec_stack (void)
6959 {
6960   unsigned int flags = SECTION_DEBUG;
6961   if (trampolines_created)
6962     flags |= SECTION_CODE;
6963
6964   switch_to_section (get_section (".note.GNU-stack", flags, NULL));
6965 }
6966
6967 /* Emit a special section directive to indicate that this object file
6968    was compiled with -fsplit-stack.  This is used to let the linker
6969    detect calls between split-stack code and non-split-stack code, so
6970    that it can modify the split-stack code to allocate a sufficiently
6971    large stack.  We emit another special section if there are any
6972    functions in this file which have the no_split_stack attribute, to
6973    prevent the linker from warning about being unable to convert the
6974    functions if they call non-split-stack code.  */
6975
6976 void
6977 file_end_indicate_split_stack (void)
6978 {
6979   if (flag_split_stack)
6980     {
6981       switch_to_section (get_section (".note.GNU-split-stack", SECTION_DEBUG,
6982                                       NULL));
6983       if (saw_no_split_stack)
6984         switch_to_section (get_section (".note.GNU-no-split-stack",
6985                                         SECTION_DEBUG, NULL));
6986     }
6987 }
6988
6989 /* Output DIRECTIVE (a C string) followed by a newline.  This is used as
6990    a get_unnamed_section callback.  */
6991
6992 void
6993 output_section_asm_op (const void *directive)
6994 {
6995   fprintf (asm_out_file, "%s\n", (const char *) directive);
6996 }
6997
6998 /* Emit assembly code to switch to section NEW_SECTION.  Do nothing if
6999    the current section is NEW_SECTION.  */
7000
7001 void
7002 switch_to_section (section *new_section)
7003 {
7004   if (in_section == new_section)
7005     return;
7006
7007   if (new_section->common.flags & SECTION_FORGET)
7008     in_section = NULL;
7009   else
7010     in_section = new_section;
7011
7012   switch (SECTION_STYLE (new_section))
7013     {
7014     case SECTION_NAMED:
7015       targetm.asm_out.named_section (new_section->named.name,
7016                                      new_section->named.common.flags,
7017                                      new_section->named.decl);
7018       break;
7019
7020     case SECTION_UNNAMED:
7021       new_section->unnamed.callback (new_section->unnamed.data);
7022       break;
7023
7024     case SECTION_NOSWITCH:
7025       gcc_unreachable ();
7026       break;
7027     }
7028
7029   new_section->common.flags |= SECTION_DECLARED;
7030 }
7031
7032 /* If block symbol SYMBOL has not yet been assigned an offset, place
7033    it at the end of its block.  */
7034
7035 void
7036 place_block_symbol (rtx symbol)
7037 {
7038   unsigned HOST_WIDE_INT size, mask, offset;
7039   struct constant_descriptor_rtx *desc;
7040   unsigned int alignment;
7041   struct object_block *block;
7042   tree decl;
7043
7044   gcc_assert (SYMBOL_REF_BLOCK (symbol));
7045   if (SYMBOL_REF_BLOCK_OFFSET (symbol) >= 0)
7046     return;
7047
7048   /* Work out the symbol's size and alignment.  */
7049   if (CONSTANT_POOL_ADDRESS_P (symbol))
7050     {
7051       desc = SYMBOL_REF_CONSTANT (symbol);
7052       alignment = desc->align;
7053       size = GET_MODE_SIZE (desc->mode);
7054     }
7055   else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
7056     {
7057       decl = SYMBOL_REF_DECL (symbol);
7058       alignment = DECL_ALIGN (decl);
7059       size = get_constant_size (DECL_INITIAL (decl));
7060     }
7061   else
7062     {
7063       decl = SYMBOL_REF_DECL (symbol);
7064       alignment = DECL_ALIGN (decl);
7065       size = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
7066     }
7067
7068   /* Calculate the object's offset from the start of the block.  */
7069   block = SYMBOL_REF_BLOCK (symbol);
7070   mask = alignment / BITS_PER_UNIT - 1;
7071   offset = (block->size + mask) & ~mask;
7072   SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
7073
7074   /* Record the block's new alignment and size.  */
7075   block->alignment = MAX (block->alignment, alignment);
7076   block->size = offset + size;
7077
7078   VEC_safe_push (rtx, gc, block->objects, symbol);
7079 }
7080
7081 /* Return the anchor that should be used to address byte offset OFFSET
7082    from the first object in BLOCK.  MODEL is the TLS model used
7083    to access it.  */
7084
7085 rtx
7086 get_section_anchor (struct object_block *block, HOST_WIDE_INT offset,
7087                     enum tls_model model)
7088 {
7089   char label[100];
7090   unsigned int begin, middle, end;
7091   unsigned HOST_WIDE_INT min_offset, max_offset, range, bias, delta;
7092   rtx anchor;
7093
7094   /* Work out the anchor's offset.  Use an offset of 0 for the first
7095      anchor so that we don't pessimize the case where we take the address
7096      of a variable at the beginning of the block.  This is particularly
7097      useful when a block has only one variable assigned to it.
7098
7099      We try to place anchors RANGE bytes apart, so there can then be
7100      anchors at +/-RANGE, +/-2 * RANGE, and so on, up to the limits of
7101      a ptr_mode offset.  With some target settings, the lowest such
7102      anchor might be out of range for the lowest ptr_mode offset;
7103      likewise the highest anchor for the highest offset.  Use anchors
7104      at the extreme ends of the ptr_mode range in such cases.
7105
7106      All arithmetic uses unsigned integers in order to avoid
7107      signed overflow.  */
7108   max_offset = (unsigned HOST_WIDE_INT) targetm.max_anchor_offset;
7109   min_offset = (unsigned HOST_WIDE_INT) targetm.min_anchor_offset;
7110   range = max_offset - min_offset + 1;
7111   if (range == 0)
7112     offset = 0;
7113   else
7114     {
7115       bias = 1 << (GET_MODE_BITSIZE (ptr_mode) - 1);
7116       if (offset < 0)
7117         {
7118           delta = -(unsigned HOST_WIDE_INT) offset + max_offset;
7119           delta -= delta % range;
7120           if (delta > bias)
7121             delta = bias;
7122           offset = (HOST_WIDE_INT) (-delta);
7123         }
7124       else
7125         {
7126           delta = (unsigned HOST_WIDE_INT) offset - min_offset;
7127           delta -= delta % range;
7128           if (delta > bias - 1)
7129             delta = bias - 1;
7130           offset = (HOST_WIDE_INT) delta;
7131         }
7132     }
7133
7134   /* Do a binary search to see if there's already an anchor we can use.
7135      Set BEGIN to the new anchor's index if not.  */
7136   begin = 0;
7137   end = VEC_length (rtx, block->anchors);
7138   while (begin != end)
7139     {
7140       middle = (end + begin) / 2;
7141       anchor = VEC_index (rtx, block->anchors, middle);
7142       if (SYMBOL_REF_BLOCK_OFFSET (anchor) > offset)
7143         end = middle;
7144       else if (SYMBOL_REF_BLOCK_OFFSET (anchor) < offset)
7145         begin = middle + 1;
7146       else if (SYMBOL_REF_TLS_MODEL (anchor) > model)
7147         end = middle;
7148       else if (SYMBOL_REF_TLS_MODEL (anchor) < model)
7149         begin = middle + 1;
7150       else
7151         return anchor;
7152     }
7153
7154   /* Create a new anchor with a unique label.  */
7155   ASM_GENERATE_INTERNAL_LABEL (label, "LANCHOR", anchor_labelno++);
7156   anchor = create_block_symbol (ggc_strdup (label), block, offset);
7157   SYMBOL_REF_FLAGS (anchor) |= SYMBOL_FLAG_LOCAL | SYMBOL_FLAG_ANCHOR;
7158   SYMBOL_REF_FLAGS (anchor) |= model << SYMBOL_FLAG_TLS_SHIFT;
7159
7160   /* Insert it at index BEGIN.  */
7161   VEC_safe_insert (rtx, gc, block->anchors, begin, anchor);
7162   return anchor;
7163 }
7164
7165 /* Output the objects in BLOCK.  */
7166
7167 static void
7168 output_object_block (struct object_block *block)
7169 {
7170   struct constant_descriptor_rtx *desc;
7171   unsigned int i;
7172   HOST_WIDE_INT offset;
7173   tree decl;
7174   rtx symbol;
7175
7176   if (block->objects == NULL)
7177     return;
7178
7179   /* Switch to the section and make sure that the first byte is
7180      suitably aligned.  */
7181   switch_to_section (block->sect);
7182   assemble_align (block->alignment);
7183
7184   /* Define the values of all anchors relative to the current section
7185      position.  */
7186   FOR_EACH_VEC_ELT (rtx, block->anchors, i, symbol)
7187     targetm.asm_out.output_anchor (symbol);
7188
7189   /* Output the objects themselves.  */
7190   offset = 0;
7191   FOR_EACH_VEC_ELT (rtx, block->objects, i, symbol)
7192     {
7193       /* Move to the object's offset, padding with zeros if necessary.  */
7194       assemble_zeros (SYMBOL_REF_BLOCK_OFFSET (symbol) - offset);
7195       offset = SYMBOL_REF_BLOCK_OFFSET (symbol);
7196       if (CONSTANT_POOL_ADDRESS_P (symbol))
7197         {
7198           desc = SYMBOL_REF_CONSTANT (symbol);
7199           output_constant_pool_1 (desc, 1);
7200           offset += GET_MODE_SIZE (desc->mode);
7201         }
7202       else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
7203         {
7204           decl = SYMBOL_REF_DECL (symbol);
7205           assemble_constant_contents (DECL_INITIAL (decl), XSTR (symbol, 0),
7206                                       DECL_ALIGN (decl));
7207           offset += get_constant_size (DECL_INITIAL (decl));
7208         }
7209       else
7210         {
7211           decl = SYMBOL_REF_DECL (symbol);
7212           assemble_variable_contents (decl, XSTR (symbol, 0), false);
7213           offset += tree_low_cst (DECL_SIZE_UNIT (decl), 1);
7214         }
7215     }
7216 }
7217
7218 /* A htab_traverse callback used to call output_object_block for
7219    each member of object_block_htab.  */
7220
7221 static int
7222 output_object_block_htab (void **slot, void *data ATTRIBUTE_UNUSED)
7223 {
7224   output_object_block ((struct object_block *) (*slot));
7225   return 1;
7226 }
7227
7228 /* Output the definitions of all object_blocks.  */
7229
7230 void
7231 output_object_blocks (void)
7232 {
7233   htab_traverse (object_block_htab, output_object_block_htab, NULL);
7234 }
7235
7236 /* This function provides a possible implementation of the
7237    TARGET_ASM_RECORD_GCC_SWITCHES target hook for ELF targets.  When triggered
7238    by -frecord-gcc-switches it creates a new mergeable, string section in the
7239    assembler output file called TARGET_ASM_RECORD_GCC_SWITCHES_SECTION which
7240    contains the switches in ASCII format.
7241
7242    FIXME: This code does not correctly handle double quote characters
7243    that appear inside strings, (it strips them rather than preserving them).
7244    FIXME: ASM_OUTPUT_ASCII, as defined in config/elfos.h will not emit NUL
7245    characters - instead it treats them as sub-string separators.  Since
7246    we want to emit NUL strings terminators into the object file we have to use
7247    ASM_OUTPUT_SKIP.  */
7248
7249 int
7250 elf_record_gcc_switches (print_switch_type type, const char * name)
7251 {
7252   switch (type)
7253     {
7254     case SWITCH_TYPE_PASSED:
7255       ASM_OUTPUT_ASCII (asm_out_file, name, strlen (name));
7256       ASM_OUTPUT_SKIP (asm_out_file, (unsigned HOST_WIDE_INT) 1);
7257       break;
7258
7259     case SWITCH_TYPE_DESCRIPTIVE:
7260       if (name == NULL)
7261         {
7262           /* Distinguish between invocations where name is NULL.  */
7263           static bool started = false;
7264
7265           if (!started)
7266             {
7267               section * sec;
7268
7269               sec = get_section (targetm.asm_out.record_gcc_switches_section,
7270                                  SECTION_DEBUG
7271                                  | SECTION_MERGE
7272                                  | SECTION_STRINGS
7273                                  | (SECTION_ENTSIZE & 1),
7274                                  NULL);
7275               switch_to_section (sec);
7276               started = true;
7277             }
7278         }
7279
7280     default:
7281       break;
7282     }
7283
7284   /* The return value is currently ignored by the caller, but must be 0.
7285      For -fverbose-asm the return value would be the number of characters
7286      emitted into the assembler file.  */
7287   return 0;
7288 }
7289
7290 /* Emit text to declare externally defined symbols. It is needed to
7291    properly support non-default visibility.  */
7292 void
7293 default_elf_asm_output_external (FILE *file ATTRIBUTE_UNUSED,
7294                                  tree decl,
7295                                  const char *name ATTRIBUTE_UNUSED)
7296 {
7297   /* We output the name if and only if TREE_SYMBOL_REFERENCED is
7298      set in order to avoid putting out names that are never really
7299      used. */
7300   if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
7301       && targetm.binds_local_p (decl))
7302     maybe_assemble_visibility (decl);
7303 }
7304
7305 /* The default hook for TARGET_ASM_OUTPUT_SOURCE_FILENAME.  */
7306
7307 void
7308 default_asm_output_source_filename (FILE *file, const char *name)
7309 {
7310 #ifdef ASM_OUTPUT_SOURCE_FILENAME
7311   ASM_OUTPUT_SOURCE_FILENAME (file, name);
7312 #else
7313   fprintf (file, "\t.file\t");
7314   output_quoted_string (file, name);
7315   putc ('\n', file);
7316 #endif
7317 }
7318
7319 /* Output a file name in the form wanted by System V.  */
7320
7321 void
7322 output_file_directive (FILE *asm_file, const char *input_name)
7323 {
7324   int len;
7325   const char *na;
7326
7327   if (input_name == NULL)
7328     input_name = "<stdin>";
7329   else
7330     input_name = remap_debug_filename (input_name);
7331
7332   len = strlen (input_name);
7333   na = input_name + len;
7334
7335   /* NA gets INPUT_NAME sans directory names.  */
7336   while (na > input_name)
7337     {
7338       if (IS_DIR_SEPARATOR (na[-1]))
7339         break;
7340       na--;
7341     }
7342
7343   targetm.asm_out.output_source_filename (asm_file, na);
7344 }
7345
7346 /* Create a DEBUG_EXPR_DECL / DEBUG_EXPR pair from RTL expression
7347    EXP.  */
7348 rtx
7349 make_debug_expr_from_rtl (const_rtx exp)
7350 {
7351   tree ddecl = make_node (DEBUG_EXPR_DECL), type;
7352   enum machine_mode mode = GET_MODE (exp);
7353   rtx dval;
7354
7355   DECL_ARTIFICIAL (ddecl) = 1;
7356   if (REG_P (exp) && REG_EXPR (exp))
7357     type = TREE_TYPE (REG_EXPR (exp));
7358   else if (MEM_P (exp) && MEM_EXPR (exp))
7359     type = TREE_TYPE (MEM_EXPR (exp));
7360   else
7361     type = NULL_TREE;
7362   if (type && TYPE_MODE (type) == mode)
7363     TREE_TYPE (ddecl) = type;
7364   else
7365     TREE_TYPE (ddecl) = lang_hooks.types.type_for_mode (mode, 1);
7366   DECL_MODE (ddecl) = mode;
7367   dval = gen_rtx_DEBUG_EXPR (mode);
7368   DEBUG_EXPR_TREE_DECL (dval) = ddecl;
7369   SET_DECL_RTL (ddecl, dval);
7370   return dval;
7371 }
7372
7373 #include "gt-varasm.h"