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