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