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