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