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