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