OSDN Git Service

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