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