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