OSDN Git Service

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