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