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