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