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