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