2011-04-23 Tobias Burnus <burnus@net-b.de>
[official-gcc.git] / gcc / varasm.c
blob8755dded36e4398f1a4a4283a04a89f592e346e2
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.
6 This file is part of GCC.
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.
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.
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/>. */
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.
27 We also output the assembler code for constants stored in memory
28 and are responsible for combining constants with the same value. */
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"
57 #ifdef XCOFF_DEBUGGING_INFO
58 #include "xcoffout.h" /* Needed for external data
59 declarations for e.g. AIX 4.x. */
60 #endif
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;
66 const char *first_global_object_name;
67 const char *weak_global_object_name;
69 struct addr_const;
70 struct constant_descriptor_rtx;
71 struct rtx_constant_pool;
73 #define n_deferred_constants (crtl->varasm.deferred_constants)
75 /* Number for making the label on the next
76 constant that is stored in memory. */
78 static GTY(()) int const_labelno;
80 /* Carry information from ASM_DECLARE_OBJECT_NAME
81 to ASM_FINISH_DECLARE_OBJECT. */
83 int size_directive_output;
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. */
90 tree last_assemble_variable_decl;
92 /* The following global variable indicates if the first basic block
93 in a function belongs to the cold partition or not. */
95 bool first_function_block_is_cold;
97 /* We give all constants their own alias set. Perhaps redundant with
98 MEM_READONLY_P, but pre-dates it. */
100 static alias_set_type const_alias_set;
102 /* Whether we saw any functions with no_split_stack. */
104 static bool saw_no_split_stack;
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);
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;
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;
148 /* A SECTION_NOSWITCH section used for declaring global BSS variables.
149 May be null. */
150 section *bss_noswitch_section;
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;
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;
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;
166 /* True if code for the current function is currently being directed
167 at the cold section. */
168 bool in_cold_section_p;
170 /* A linked list of all the unnamed sections. */
171 static GTY(()) section *unnamed_sections;
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)
178 /* Hash table of named sections. */
179 static GTY((param_is (section))) htab_t section_htab;
181 /* A table of object_blocks, indexed by section. */
182 static GTY((param_is (struct object_block))) htab_t object_block_htab;
184 /* The next number to use for internal anchor labels. */
185 static GTY(()) int anchor_labelno;
187 /* A pool of constants that can be shared between functions. */
188 static GTY(()) struct rtx_constant_pool *shared_constant_pool;
190 /* Helper routines for maintaining section_htab. */
192 static int
193 section_entry_eq (const void *p1, const void *p2)
195 const section *old = (const section *) p1;
196 const char *new_name = (const char *) p2;
198 return strcmp (old->named.name, new_name) == 0;
201 static hashval_t
202 section_entry_hash (const void *p)
204 const section *old = (const section *) p;
205 return htab_hash_string (old->named.name);
208 /* Return a hash value for section SECT. */
210 static hashval_t
211 hash_section (section *sect)
213 if (sect->common.flags & SECTION_NAMED)
214 return htab_hash_string (sect->named.name);
215 return sect->common.flags;
218 /* Helper routines for maintaining object_block_htab. */
220 static int
221 object_block_entry_eq (const void *p1, const void *p2)
223 const struct object_block *old = (const struct object_block *) p1;
224 const section *new_section = (const section *) p2;
226 return old->sect == new_section;
229 static hashval_t
230 object_block_entry_hash (const void *p)
232 const struct object_block *old = (const struct object_block *) p;
233 return hash_section (old->sect);
236 /* Return a new unnamed section with the given fields. */
238 section *
239 get_unnamed_section (unsigned int flags, void (*callback) (const void *),
240 const void *data)
242 section *sect;
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;
250 unnamed_sections = sect;
251 return sect;
254 /* Return a SECTION_NOSWITCH section with the given fields. */
256 static section *
257 get_noswitch_section (unsigned int flags, noswitch_section_callback callback)
259 section *sect;
261 sect = ggc_alloc_section ();
262 sect->noswitch.common.flags = flags | SECTION_NOSWITCH;
263 sect->noswitch.callback = callback;
265 return sect;
268 /* Return the named section structure associated with NAME. Create
269 a new section with the given fields if no such structure exists. */
271 section *
272 get_section (const char *name, unsigned int flags, tree decl)
274 section *sect, **slot;
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)
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;
288 else
290 sect = *slot;
291 if ((sect->common.flags & ~SECTION_DECLARED) != flags
292 && ((sect->common.flags | flags) & SECTION_OVERRIDE) == 0)
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)))
309 sect->common.flags |= (SECTION_WRITE | SECTION_RELRO);
310 return sect;
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;
321 return sect;
324 /* Return true if the current compilation mode benefits from having
325 objects grouped into blocks. */
327 static bool
328 use_object_blocks_p (void)
330 return flag_section_anchors;
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. */
337 static struct object_block *
338 get_block_for_section (section *sect)
340 struct object_block *block;
341 void **slot;
343 if (sect == NULL)
344 return NULL;
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)
351 block = ggc_alloc_cleared_object_block ();
352 block->sect = sect;
353 *slot = block;
355 return block;
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. */
362 static rtx
363 create_block_symbol (const char *label, struct object_block *block,
364 HOST_WIDE_INT offset)
366 rtx symbol;
367 unsigned int size;
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);
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;
380 /* Initialize the block_symbol stuff. */
381 SYMBOL_REF_BLOCK (symbol) = block;
382 SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
384 return symbol;
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. */
394 section *
395 get_named_section (tree decl, const char *name, int reloc)
397 unsigned int flags;
399 gcc_assert (!decl || DECL_P (decl));
400 if (name == NULL)
401 name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
403 flags = targetm.section_type_flags (decl, name, reloc);
405 return get_section (name, flags, decl);
408 /* If required, set DECL_SECTION_NAME to a unique name. */
410 void
411 resolve_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED,
412 int flag_function_or_data_sections)
414 if (DECL_SECTION_NAME (decl) == NULL_TREE
415 && targetm.have_named_sections
416 && (flag_function_or_data_sections
417 || DECL_ONE_ONLY (decl)))
419 targetm.asm_out.unique_section (decl, reloc);
420 DECL_HAS_IMPLICIT_SECTION_NAME_P (decl) = true;
424 #ifdef BSS_SECTION_ASM_OP
426 #ifdef ASM_OUTPUT_ALIGNED_BSS
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. */
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)
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);
450 #endif
452 #endif /* BSS_SECTION_ASM_OP */
454 #ifndef USE_SELECT_SECTION_FOR_FUNCTIONS
455 /* Return the hot section for function DECL. Return text_section for
456 null DECLs. */
458 static section *
459 hot_function_section (tree decl)
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;
468 #endif
470 /* Return section for TEXT_SECTION_NAME if DECL or DECL_SECTION_NAME (DECL)
471 is NULL.
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". */
478 section *
479 get_named_text_section (tree decl,
480 const char *text_section_name,
481 const char *named_section_suffix)
483 if (decl && DECL_SECTION_NAME (decl))
485 if (named_section_suffix)
487 tree dsn = DECL_SECTION_NAME (decl);
488 const char *stripped_name;
489 char *name, *buffer;
491 name = (char *) alloca (TREE_STRING_LENGTH (dsn) + 1);
492 memcpy (name, TREE_STRING_POINTER (dsn),
493 TREE_STRING_LENGTH (dsn) + 1);
495 stripped_name = targetm.strip_name_encoding (name);
497 buffer = ACONCAT ((stripped_name, named_section_suffix, NULL));
498 return get_named_section (decl, buffer, 0);
500 else if (DECL_HAS_IMPLICIT_SECTION_NAME_P (decl))
502 const char *name;
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);
513 else
514 return NULL;
516 return get_named_section (decl, text_section_name, 0);
519 /* Choose named function section based on its frequency. */
521 section *
522 default_function_section (tree decl, enum node_frequency freq,
523 bool startup, bool exit)
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
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);
543 /* Similarly for exit. */
544 if (exit && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
545 return get_named_text_section (decl, ".text.exit", NULL);
547 /* Group cold functions together, similarly for hot code. */
548 switch (freq)
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;
559 /* Return the section for function DECL.
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.
564 If FORCE_COLD is true, return cold function section ignoring
565 the frequency info of cgraph_node. */
567 static section *
568 function_section_1 (tree decl, bool force_cold)
570 section *section = NULL;
571 enum node_frequency freq = NODE_FREQUENCY_NORMAL;
572 bool startup = false, exit = false;
574 if (decl)
576 struct cgraph_node *node = cgraph_get_node (decl);
578 if (node)
580 freq = node->frequency;
581 startup = node->only_called_at_startup;
582 exit = node->only_called_at_exit;
585 if (force_cold)
586 freq = NODE_FREQUENCY_UNLIKELY_EXECUTED;
588 #ifdef USE_SELECT_SECTION_FOR_FUNCTIONS
589 if (decl != NULL_TREE
590 && DECL_SECTION_NAME (decl) != NULL_TREE)
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);
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
612 /* Return the section for function DECL.
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. */
617 section *
618 function_section (tree decl)
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);
629 /* Return the section for the current function, take IN_COLD_SECTION_P
630 into account. */
632 section *
633 current_function_section (void)
635 return function_section_1 (current_function_decl, in_cold_section_p);
638 /* Tell assembler to switch to unlikely-to-be-executed text section. */
640 section *
641 unlikely_text_section (void)
643 return function_section_1 (current_function_decl, true);
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. */
651 bool
652 unlikely_text_section_p (section *sect)
654 return sect == function_section_1 (current_function_decl, true);
657 /* Return the read-only data section associated with function DECL. */
659 section *
660 default_function_rodata_section (tree decl)
662 if (decl != NULL_TREE && DECL_SECTION_NAME (decl))
664 const char *name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
666 if (DECL_ONE_ONLY (decl) && HAVE_COMDAT_GROUP)
668 const char *dot;
669 size_t len;
670 char* rname;
672 dot = strchr (name + 1, '.');
673 if (!dot)
674 dot = name;
675 len = strlen (dot) + 8;
676 rname = (char *) alloca (len);
678 strcpy (rname, ".rodata");
679 strcat (rname, dot);
680 return get_section (rname, SECTION_LINKONCE, decl);
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)
686 size_t len = strlen (name) + 1;
687 char *rname = (char *) alloca (len);
689 memcpy (rname, name, len);
690 rname[14] = 'r';
691 return get_section (rname, SECTION_LINKONCE, decl);
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)
697 size_t len = strlen (name) + 1;
698 char *rname = (char *) alloca (len + 2);
700 memcpy (rname, ".rodata", 7);
701 memcpy (rname + 7, name + 5, len - 5);
702 return get_section (rname, 0, decl);
706 return readonly_data_section;
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. */
713 section *
714 default_no_function_rodata_section (tree decl ATTRIBUTE_UNUSED)
716 return readonly_data_section;
719 /* Return the section to use for string merging. */
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)
726 HOST_WIDE_INT len;
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)
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];
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)
747 if (align < modesize)
748 align = modesize;
750 str = TREE_STRING_POINTER (decl);
751 unit = GET_MODE_SIZE (mode);
753 /* Check for embedded NUL characters. */
754 for (i = 0; i < len; i += unit)
756 for (j = 0; j < unit; j++)
757 if (str[i + j] != '\0')
758 break;
759 if (j == unit)
760 break;
762 if (i == len - unit)
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);
772 return readonly_data_section;
775 /* Return the section to use for constant merging. */
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)
782 unsigned int modesize = GET_MODE_BITSIZE (mode);
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)
792 char name[24];
794 sprintf (name, ".rodata.cst%d", (int) (align / 8));
795 flags |= (align / 8) | SECTION_MERGE;
796 return get_section (name, flags, NULL);
798 return readonly_data_section;
801 /* Given NAME, a putative register name, discard any customary prefixes. */
803 static const char *
804 strip_reg_name (const char *name)
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;
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)
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);
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. */
837 decode_reg_name_and_count (const char *asmspec, int *pnregs)
839 /* Presume just one register is clobbered. */
840 *pnregs = 1;
842 if (asmspec != 0)
844 int i;
846 /* Get rid of confusing prefixes. */
847 asmspec = strip_reg_name (asmspec);
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)
855 i = atoi (asmspec);
856 if (i < FIRST_PSEUDO_REGISTER && i >= 0)
857 return i;
858 else
859 return -2;
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;
867 #ifdef OVERLAPPING_REGISTER_NAMES
869 static const struct
871 const char *const name;
872 const int number;
873 const int nregs;
874 } table[] = OVERLAPPING_REGISTER_NAMES;
876 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
877 if (table[i].name[0]
878 && ! strcmp (asmspec, table[i].name))
880 *pnregs = table[i].nregs;
881 return table[i].number;
884 #endif /* OVERLAPPING_REGISTER_NAMES */
886 #ifdef ADDITIONAL_REGISTER_NAMES
888 static const struct { const char *const name; const int number; } table[]
889 = ADDITIONAL_REGISTER_NAMES;
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;
896 #endif /* ADDITIONAL_REGISTER_NAMES */
898 if (!strcmp (asmspec, "memory"))
899 return -4;
901 if (!strcmp (asmspec, "cc"))
902 return -3;
904 return -2;
907 return -1;
911 decode_reg_name (const char *name)
913 int count;
914 return decode_reg_name_and_count (name, &count);
918 /* Return true if DECL's initializer is suitable for a BSS section. */
920 static bool
921 bss_initializer_p (const_tree decl)
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))));
932 /* Compute the alignment of variable specified by DECL.
933 DONT_OUTPUT_DATA is from assemble_variable. */
935 void
936 align_variable (tree decl, bool dont_output_data)
938 unsigned int align = DECL_ALIGN (decl);
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))));
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)
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;
957 /* On some machines, it is good to increase alignment sometimes. */
958 if (! DECL_USER_ALIGN (decl))
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)
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;
977 #endif
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;
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. */
989 section *
990 get_variable_section (tree decl, bool prefer_noswitch_p)
992 addr_space_t as = ADDR_SPACE_GENERIC;
993 int reloc;
995 if (TREE_TYPE (decl) != error_mark_node)
996 as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
998 if (DECL_COMMON (decl))
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;
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;
1018 resolve_unique_section (decl, reloc, flag_data_sections);
1019 if (IN_NAMED_SECTION (decl))
1020 return get_named_section (decl, NULL, reloc);
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))
1027 if (!TREE_PUBLIC (decl))
1028 return lcomm_section;
1029 if (bss_noswitch_section)
1030 return bss_noswitch_section;
1033 return targetm.asm_out.select_section (decl, reloc, DECL_ALIGN (decl));
1036 /* Return the block into which object_block DECL should be placed. */
1038 static struct object_block *
1039 get_block_for_decl (tree decl)
1041 section *sect;
1043 if (TREE_CODE (decl) == VAR_DECL)
1045 /* The object must be defined in this translation unit. */
1046 if (DECL_EXTERNAL (decl))
1047 return NULL;
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;
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;
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;
1070 return get_block_for_section (sect);
1073 /* Make sure block symbol SYMBOL is in block BLOCK. */
1075 static void
1076 change_symbol_block (rtx symbol, struct object_block *block)
1078 if (block != SYMBOL_REF_BLOCK (symbol))
1080 gcc_assert (SYMBOL_REF_BLOCK_OFFSET (symbol) < 0);
1081 SYMBOL_REF_BLOCK (symbol) = block;
1085 /* Return true if it is possible to put DECL in an object_block. */
1087 static bool
1088 use_blocks_for_decl_p (tree decl)
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;
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;
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;
1106 return true;
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.
1113 There is, however, one exception: this function handles variables
1114 explicitly placed in a particular register by the user.
1116 This is never called for PARM_DECL nodes. */
1118 void
1119 make_decl_rtl (tree decl)
1121 const char *name = 0;
1122 int reg_number;
1123 rtx x;
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);
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));
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);
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))
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));
1149 if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1150 return;
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. */
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);
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));
1169 /* Make this function static known to the mudflap runtime. */
1170 if (flag_mudflap && TREE_CODE (decl) == VAR_DECL)
1171 mudflap_enqueue_decl (decl);
1173 return;
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))
1180 SET_DECL_RTL (decl, output_constant_def (DECL_INITIAL (decl), 1));
1181 return;
1184 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
1186 if (name[0] != '*' && TREE_CODE (decl) != FUNCTION_DECL
1187 && DECL_REGISTER (decl))
1189 error ("register name not specified for %q+D", decl);
1191 else if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
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
1209 int nregs;
1211 if (DECL_INITIAL (decl) != 0 && TREE_STATIC (decl))
1213 DECL_INITIAL (decl) = 0;
1214 error ("global register variable has initial value");
1216 if (TREE_THIS_VOLATILE (decl))
1217 warning (OPT_Wvolatile_register_var,
1218 "optimization may eliminate reads and/or "
1219 "writes to register variables");
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... */
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;
1230 if (TREE_STATIC (decl))
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);
1243 /* As a register variable, it has no section. */
1244 return;
1247 /* Now handle ordinary static variables and functions (in memory).
1248 Also handle vars declared register invalidly. */
1249 else if (name[0] == '*')
1251 #ifdef REGISTER_PREFIX
1252 if (strlen (REGISTER_PREFIX) != 0)
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);
1258 #endif
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;
1273 /* Variables can't be both common and weak. */
1274 if (TREE_CODE (decl) == VAR_DECL && DECL_WEAK (decl))
1275 DECL_COMMON (decl) = 0;
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
1281 enum machine_mode address_mode = Pmode;
1282 if (TREE_TYPE (decl) != error_mark_node)
1284 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
1285 address_mode = targetm.addr_space.address_mode (as);
1287 x = gen_rtx_SYMBOL_REF (address_mode, name);
1289 SYMBOL_REF_WEAK (x) = DECL_WEAK (decl);
1290 SET_SYMBOL_REF_DECL (x, decl);
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);
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);
1303 /* Make this function static known to the mudflap runtime. */
1304 if (flag_mudflap && TREE_CODE (decl) == VAR_DECL)
1305 mudflap_enqueue_decl (decl);
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. */
1313 make_decl_rtl_for_debug (tree decl)
1315 unsigned int save_aliasing_flag, save_mudflap_flag;
1316 rtx rtl;
1318 if (DECL_RTL_SET_P (decl))
1319 return DECL_RTL (decl);
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;
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);
1338 flag_strict_aliasing = save_aliasing_flag;
1339 flag_mudflap = save_mudflap_flag;
1341 return rtl;
1344 /* Output a string of literal assembler code
1345 for an `asm' keyword used between functions. */
1347 void
1348 assemble_asm (tree string)
1350 app_enable ();
1352 if (TREE_CODE (string) == ADDR_EXPR)
1353 string = TREE_OPERAND (string, 0);
1355 fprintf (asm_out_file, "\t%s\n", TREE_STRING_POINTER (string));
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. */
1362 void
1363 default_stabs_asm_out_destructor (rtx symbol ATTRIBUTE_UNUSED,
1364 int priority ATTRIBUTE_UNUSED)
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
1377 /* Write the address of the entity given by SYMBOL to SEC. */
1378 void
1379 assemble_addr_to_section (rtx symbol, section *sec)
1381 switch_to_section (sec);
1382 assemble_align (POINTER_SIZE);
1383 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
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)
1391 char buf[16];
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);
1403 void
1404 default_named_section_asm_out_destructor (rtx symbol, int priority)
1406 section *sec;
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);
1414 assemble_addr_to_section (symbol, sec);
1417 #ifdef DTORS_SECTION_ASM_OP
1418 void
1419 default_dtor_section_asm_out_destructor (rtx symbol,
1420 int priority ATTRIBUTE_UNUSED)
1422 assemble_addr_to_section (symbol, dtors_section);
1424 #endif
1426 /* Likewise for global constructors. */
1428 void
1429 default_stabs_asm_out_constructor (rtx symbol ATTRIBUTE_UNUSED,
1430 int priority ATTRIBUTE_UNUSED)
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
1443 void
1444 default_named_section_asm_out_constructor (rtx symbol, int priority)
1446 section *sec;
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);
1454 assemble_addr_to_section (symbol, sec);
1457 #ifdef CTORS_SECTION_ASM_OP
1458 void
1459 default_ctor_section_asm_out_constructor (rtx symbol,
1460 int priority ATTRIBUTE_UNUSED)
1462 assemble_addr_to_section (symbol, ctors_section);
1464 #endif
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. */
1472 #ifndef CONSTANT_POOL_BEFORE_FUNCTION
1473 #define CONSTANT_POOL_BEFORE_FUNCTION 1
1474 #endif
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. */
1480 void
1481 notice_global_symbol (tree decl)
1483 const char **type = &first_global_object_name;
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;
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;
1502 if (!*type)
1504 const char *p;
1505 const char *name;
1506 rtx decl_rtl = DECL_RTL (decl);
1508 p = targetm.strip_name_encoding (XSTR (XEXP (decl_rtl, 0), 0));
1509 name = ggc_strdup (p);
1511 *type = name;
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. */
1520 void
1521 assemble_start_function (tree decl, const char *fnname)
1523 int align;
1524 char tmp_label[100];
1525 bool hot_label_written = false;
1527 first_function_block_is_cold = false;
1528 if (flag_reorder_blocks_and_partition)
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++;
1540 else
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;
1548 /* The following code does not need preprocessing in the assembler. */
1550 app_disable ();
1552 if (CONSTANT_POOL_BEFORE_FUNCTION)
1553 output_constant_pool (fnname, decl);
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. */
1560 if (flag_reorder_blocks_and_partition)
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);
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)
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;
1579 else if (DECL_SECTION_NAME (decl))
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);
1589 in_cold_section_p = first_function_block_is_cold;
1591 /* Switch to the correct text section for the start of the function. */
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);
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)
1602 ASM_OUTPUT_ALIGN (asm_out_file, align);
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))
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
1620 #ifdef ASM_OUTPUT_FUNCTION_PREFIX
1621 ASM_OUTPUT_FUNCTION_PREFIX (asm_out_file, fnname);
1622 #endif
1624 if (!DECL_IGNORED_P (decl))
1625 (*debug_hooks->begin_function) (decl);
1627 /* Make function name accessible from other files, if appropriate. */
1629 if (TREE_PUBLIC (decl))
1631 notice_global_symbol (decl);
1633 globalize_decl (decl);
1635 maybe_assemble_visibility (decl);
1638 if (DECL_PRESERVE_P (decl))
1639 targetm.asm_out.mark_decl_preserved (fnname);
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 */
1649 if (lookup_attribute ("no_split_stack", DECL_ATTRIBUTES (decl)))
1650 saw_no_split_stack = true;
1653 /* Output assembler code associated with defining the size of the
1654 function. DECL describes the function. NAME is the function's name. */
1656 void
1657 assemble_end_function (tree decl, const char *fnname ATTRIBUTE_UNUSED)
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)
1667 output_constant_pool (fnname, decl);
1668 switch_to_section (function_section (decl)); /* need to switch back */
1670 /* Output labels for end of hot/cold text sections (to be used by
1671 debug info.) */
1672 if (flag_reorder_blocks_and_partition)
1674 section *save_text_section;
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);
1688 /* Assemble code to leave SIZE bytes of zeros. */
1690 void
1691 assemble_zeros (unsigned HOST_WIDE_INT size)
1693 /* Do no output if -fsyntax-only. */
1694 if (flag_syntax_only)
1695 return;
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)
1702 unsigned HOST_WIDE_INT i;
1703 for (i = 0; i < size; i++)
1704 assemble_integer (const0_rtx, 1, BITS_PER_UNIT, 1);
1706 else
1707 #endif
1708 if (size > 0)
1709 ASM_OUTPUT_SKIP (asm_out_file, size);
1712 /* Assemble an alignment pseudo op for an ALIGN-bit boundary. */
1714 void
1715 assemble_align (int align)
1717 if (align > BITS_PER_UNIT)
1719 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
1723 /* Assemble a string constant with the specified C string as contents. */
1725 void
1726 assemble_string (const char *p, int size)
1728 int pos = 0;
1729 int maximum = 2000;
1731 /* If the string is very long, split it up. */
1733 while (pos < size)
1735 int thissize = size - pos;
1736 if (thissize > maximum)
1737 thissize = maximum;
1739 ASM_OUTPUT_ASCII (asm_out_file, p, thissize);
1741 pos += thissize;
1742 p += thissize;
1747 /* A noswitch_section_callback for lcomm_section. */
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)
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
1768 /* A noswitch_section_callback for bss_noswitch_section. */
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)
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
1782 #endif
1784 /* A noswitch_section_callback for comm_section. */
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)
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
1805 /* A noswitch_section_callback for tls_comm_section. */
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)
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
1822 /* Assemble DECL given that it belongs in SECTION_NOSWITCH section SECT.
1823 NAME is the name of DECL's SYMBOL_REF. */
1825 static void
1826 assemble_noswitch_variable (tree decl, const char *name, section *sect)
1828 unsigned HOST_WIDE_INT size, rounded;
1830 size = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
1831 rounded = size;
1833 /* Don't allocate zero bytes of common,
1834 since that means "undefined external" in the linker. */
1835 if (size == 0)
1836 rounded = 1;
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));
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);
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. */
1854 static void
1855 assemble_variable_contents (tree decl, const char *name,
1856 bool dont_output_data)
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 */
1867 if (!dont_output_data)
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));
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.
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). */
1892 void
1893 assemble_variable (tree decl, int top_level ATTRIBUTE_UNUSED,
1894 int at_end ATTRIBUTE_UNUSED, int dont_output_data)
1896 const char *name;
1897 rtx decl_rtl, symbol;
1898 section *sect;
1900 /* This function is supposed to handle VARIABLES. Ensure we have one. */
1901 gcc_assert (TREE_CODE (decl) == VAR_DECL);
1903 /* Emulated TLS had better not get this far. */
1904 gcc_checking_assert (targetm.have_tls || !DECL_THREAD_LOCAL_P (decl));
1906 last_assemble_variable_decl = 0;
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. */
1912 if (DECL_EXTERNAL (decl))
1913 return;
1915 /* Do nothing for global register variables. */
1916 if (DECL_RTL_SET_P (decl) && REG_P (DECL_RTL (decl)))
1918 TREE_ASM_WRITTEN (decl) = 1;
1919 return;
1922 /* If type was incomplete when the variable was declared,
1923 see if it is complete now. */
1925 if (DECL_SIZE (decl) == 0)
1926 layout_decl (decl, 0);
1928 /* Still incomplete => don't allocate it; treat the tentative defn
1929 (which is what it must have been) as an `extern' reference. */
1931 if (!dont_output_data && DECL_SIZE (decl) == 0)
1933 error ("storage size of %q+D isn%'t known", decl);
1934 TREE_ASM_WRITTEN (decl) = 1;
1935 return;
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. */
1943 if (TREE_ASM_WRITTEN (decl))
1944 return;
1946 /* Make sure targetm.encode_section_info is invoked before we set
1947 ASM_WRITTEN. */
1948 decl_rtl = DECL_RTL (decl);
1950 TREE_ASM_WRITTEN (decl) = 1;
1952 /* Do no output if -fsyntax-only. */
1953 if (flag_syntax_only)
1954 return;
1956 if (! dont_output_data
1957 && ! host_integerp (DECL_SIZE_UNIT (decl), 1))
1959 error ("size of variable %q+D is too large", decl);
1960 return;
1963 gcc_assert (MEM_P (decl_rtl));
1964 gcc_assert (GET_CODE (XEXP (decl_rtl, 0)) == SYMBOL_REF);
1965 symbol = XEXP (decl_rtl, 0);
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))
1971 tree decl = SYMBOL_REF_DECL (symbol);
1972 if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
1973 output_constant_def_contents (symbol);
1974 return;
1977 app_disable ();
1979 name = XSTR (symbol, 0);
1980 if (TREE_PUBLIC (decl) && DECL_NAME (decl))
1981 notice_global_symbol (decl);
1983 /* Compute the alignment of this data. */
1985 align_variable (decl, dont_output_data);
1986 set_mem_align (decl_rtl, DECL_ALIGN (decl));
1988 if (TREE_PUBLIC (decl))
1989 maybe_assemble_visibility (decl);
1991 if (DECL_PRESERVE_P (decl))
1992 targetm.asm_out.mark_decl_preserved (name);
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);
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));
2004 /* dbxout.c needs to know this. */
2005 if (sect && (sect->common.flags & SECTION_CODE) != 0)
2006 DECL_IN_TEXT_SECTION (decl) = 1;
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))
2013 gcc_assert (!dont_output_data);
2014 place_block_symbol (symbol);
2016 else if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
2017 assemble_noswitch_variable (decl, name, sect);
2018 else
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);
2027 /* Return 1 if type TYPE contains any pointers. */
2029 static int
2030 contains_pointers_p (tree type)
2032 switch (TREE_CODE (type))
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;
2041 case RECORD_TYPE:
2042 case UNION_TYPE:
2043 case QUAL_UNION_TYPE:
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;
2054 case ARRAY_TYPE:
2055 /* An array type contains pointers if its element type does. */
2056 return contains_pointers_p (TREE_TYPE (type));
2058 default:
2059 return 0;
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;
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. */
2073 static bool
2074 incorporeal_function_p (tree decl)
2076 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
2078 const char *name;
2080 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
2081 && DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA)
2082 return true;
2084 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2085 if (is_builtin_name (name))
2086 return true;
2088 return false;
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)
2096 rtx rtl = DECL_RTL (decl);
2098 if (MEM_P (rtl) && GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF
2099 && !SYMBOL_REF_USED (XEXP (rtl, 0))
2100 && !incorporeal_function_p (decl))
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));
2107 #endif
2109 void
2110 process_pending_assemble_externals (void)
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));
2117 pending_assemble_externals = 0;
2118 #endif
2121 /* This TREE_LIST contains any weak symbol declarations waiting
2122 to be emitted. */
2123 static GTY(()) tree weak_decls;
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. */
2130 void
2131 assemble_external (tree decl ATTRIBUTE_UNUSED)
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);
2139 if (!DECL_P (decl) || !DECL_EXTERNAL (decl) || !TREE_PUBLIC (decl))
2140 return;
2142 /* We want to output annotation for weak and external symbols at
2143 very last to check if they are references or not. */
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);
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
2164 /* Similar, for calling a library function FUN. */
2166 void
2167 assemble_external_libcall (rtx fun)
2169 /* Declare library function name external when first used, if nec. */
2170 if (! SYMBOL_REF_USED (fun))
2172 SYMBOL_REF_USED (fun) = 1;
2173 targetm.asm_out.external_libcall (fun);
2177 /* Assemble a label named NAME. */
2179 void
2180 assemble_label (FILE *file, const char *name)
2182 ASM_OUTPUT_LABEL (file, name);
2185 /* Set the symbol_referenced flag for ID. */
2186 void
2187 mark_referenced (tree id)
2189 TREE_SYMBOL_REFERENCED (id) = 1;
2192 /* Set the symbol_referenced flag for DECL and notify callgraph. */
2193 void
2194 mark_decl_referenced (tree decl)
2196 if (TREE_CODE (decl) == FUNCTION_DECL)
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);
2207 else if (TREE_CODE (decl) == VAR_DECL)
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;
2215 /* else do nothing - we can get various sorts of CST nodes here,
2216 which do not need to be marked. */
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. */
2227 static inline tree
2228 ultimate_transparent_alias_target (tree *alias)
2230 tree target = *alias;
2232 if (IDENTIFIER_TRANSPARENT_ALIAS (target))
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;
2241 return target;
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). */
2249 void
2250 assemble_name_raw (FILE *file, const char *name)
2252 if (name[0] == '*')
2253 fputs (&name[1], file);
2254 else
2255 ASM_OUTPUT_LABELREF (file, name);
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. */
2263 void
2264 assemble_name (FILE *file, const char *name)
2266 const char *real_name;
2267 tree id;
2269 real_name = targetm.strip_name_encoding (name);
2271 id = maybe_get_identifier (real_name);
2272 if (id)
2274 tree id_orig = id;
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));
2283 assemble_name_raw (file, name);
2286 /* Allocate SIZE bytes writable static space with a gensym name
2287 and return an RTX to refer to its address. */
2290 assemble_static_space (unsigned HOST_WIDE_INT size)
2292 char name[12];
2293 const char *namestring;
2294 rtx x;
2296 ASM_GENERATE_INTERNAL_LABEL (name, "LF", const_labelno);
2297 ++const_labelno;
2298 namestring = ggc_strdup (name);
2300 x = gen_rtx_SYMBOL_REF (Pmode, namestring);
2301 SYMBOL_REF_FLAGS (x) = SYMBOL_FLAG_LOCAL;
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
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);
2320 #endif
2321 #endif
2322 return x;
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. */
2329 static GTY(()) rtx initial_trampoline;
2332 assemble_trampoline_template (void)
2334 char label[256];
2335 const char *name;
2336 int align;
2337 rtx symbol;
2339 gcc_assert (targetm.asm_out.trampoline_template != NULL);
2341 if (initial_trampoline)
2342 return initial_trampoline;
2344 /* By default, put trampoline templates in read-only data section. */
2346 #ifdef TRAMPOLINE_SECTION
2347 switch_to_section (TRAMPOLINE_SECTION);
2348 #else
2349 switch_to_section (readonly_data_section);
2350 #endif
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);
2357 targetm.asm_out.internal_label (asm_out_file, "LTRAMP", 0);
2358 targetm.asm_out.trampoline_template (asm_out_file);
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;
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));
2370 return initial_trampoline;
2373 /* A and B are either alignments or offsets. Return the minimum alignment
2374 that may be assumed after adding the two together. */
2376 static inline unsigned
2377 min_align (unsigned int a, unsigned int b)
2379 return (a | b) & -(a | b);
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.
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. */
2390 const char *
2391 integer_asm_op (int size, int aligned_p)
2393 struct asm_int_op *ops;
2395 if (aligned_p)
2396 ops = &targetm.asm_out.aligned_op;
2397 else
2398 ops = &targetm.asm_out.unaligned_op;
2400 switch (size)
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;
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. */
2420 void
2421 assemble_integer_with_op (const char *op, rtx x)
2423 fputs (op, asm_out_file);
2424 output_addr_const (asm_out_file, x);
2425 fputc ('\n', asm_out_file);
2428 /* The default implementation of the asm_out.integer target hook. */
2430 bool
2431 default_assemble_integer (rtx x ATTRIBUTE_UNUSED,
2432 unsigned int size ATTRIBUTE_UNUSED,
2433 int aligned_p ATTRIBUTE_UNUSED)
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);
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. */
2448 bool
2449 assemble_integer (rtx x, unsigned int size, unsigned int align, int force)
2451 int aligned_p;
2453 aligned_p = (align >= MIN (size * BITS_PER_UNIT, BIGGEST_ALIGNMENT));
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;
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)
2463 enum machine_mode omode, imode;
2464 unsigned int subalign;
2465 unsigned int subsize, i;
2466 enum mode_class mclass;
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;
2475 omode = mode_for_size (subsize * BITS_PER_UNIT, mclass, 0);
2476 imode = mode_for_size (size * BITS_PER_UNIT, mclass, 0);
2478 for (i = 0; i < size; i += subsize)
2480 rtx partial = simplify_subreg (omode, x, imode, i);
2481 if (!partial || !assemble_integer (partial, subsize, subalign, 0))
2482 break;
2484 if (i == size)
2485 return true;
2487 /* If we've printed some of it, but not all of it, there's no going
2488 back now. */
2489 gcc_assert (!i);
2492 gcc_assert (!force);
2494 return false;
2497 void
2498 assemble_real (REAL_VALUE_TYPE d, enum machine_mode mode, unsigned int align)
2500 long data[4] = {0, 0, 0, 0};
2501 int i;
2502 int bitsize, nelts, nunits, units_per;
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.
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. */
2516 nunits = GET_MODE_SIZE (mode);
2517 bitsize = nunits * BITS_PER_UNIT;
2518 nelts = CEIL (bitsize, 32);
2519 units_per = 32 / BITS_PER_UNIT;
2521 real_to_target (data, &d, mode);
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;
2527 /* Subsequent words need only 32-bit alignment. */
2528 align = min_align (align, 32);
2530 for (i = 1; i < nelts; i++)
2532 assemble_integer (GEN_INT (data[i]), MIN (nunits, units_per), align, 1);
2533 nunits -= units_per;
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. */
2542 struct addr_const {
2543 rtx base;
2544 HOST_WIDE_INT offset;
2547 static void
2548 decode_addr_const (tree exp, struct addr_const *value)
2550 tree target = TREE_OPERAND (exp, 0);
2551 int offset = 0;
2552 rtx x;
2554 while (1)
2556 if (TREE_CODE (target) == COMPONENT_REF
2557 && host_integerp (byte_position (TREE_OPERAND (target, 1)), 0))
2559 offset += int_byte_position (TREE_OPERAND (target, 1));
2560 target = TREE_OPERAND (target, 0);
2562 else if (TREE_CODE (target) == ARRAY_REF
2563 || TREE_CODE (target) == ARRAY_RANGE_REF)
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);
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;
2578 switch (TREE_CODE (target))
2580 case VAR_DECL:
2581 case FUNCTION_DECL:
2582 x = DECL_RTL (target);
2583 break;
2585 case LABEL_DECL:
2586 x = gen_rtx_MEM (FUNCTION_MODE,
2587 gen_rtx_LABEL_REF (Pmode, force_label_rtx (target)));
2588 break;
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;
2599 default:
2600 gcc_unreachable ();
2603 gcc_assert (MEM_P (x));
2604 x = XEXP (x, 0);
2606 value->base = x;
2607 value->offset = offset;
2611 static GTY((param_is (struct constant_descriptor_tree)))
2612 htab_t const_desc_htab;
2614 static void maybe_output_constant_def_contents (struct constant_descriptor_tree *, int);
2616 /* Constant pool accessor function. */
2618 htab_t
2619 constant_pool_htab (void)
2621 return const_desc_htab;
2624 /* Compute a hash code for a constant expression. */
2626 static hashval_t
2627 const_desc_hash (const void *ptr)
2629 return ((const struct constant_descriptor_tree *)ptr)->hash;
2632 static hashval_t
2633 const_hash_1 (const tree exp)
2635 const char *p;
2636 hashval_t hi;
2637 int len, i;
2638 enum tree_code code = TREE_CODE (exp);
2640 /* Either set P and LEN to the address and len of something to hash and
2641 exit the switch or return a value. */
2643 switch (code)
2645 case INTEGER_CST:
2646 p = (char *) &TREE_INT_CST (exp);
2647 len = sizeof TREE_INT_CST (exp);
2648 break;
2650 case REAL_CST:
2651 return real_hash (TREE_REAL_CST_PTR (exp));
2653 case FIXED_CST:
2654 return fixed_hash (TREE_FIXED_CST_PTR (exp));
2656 case STRING_CST:
2657 p = TREE_STRING_POINTER (exp);
2658 len = TREE_STRING_LENGTH (exp);
2659 break;
2661 case COMPLEX_CST:
2662 return (const_hash_1 (TREE_REALPART (exp)) * 5
2663 + const_hash_1 (TREE_IMAGPART (exp)));
2665 case VECTOR_CST:
2667 tree link;
2669 hi = 7 + TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp));
2671 for (link = TREE_VECTOR_CST_ELTS (exp); link; link = TREE_CHAIN (link))
2672 hi = hi * 563 + const_hash_1 (TREE_VALUE (link));
2674 return hi;
2677 case CONSTRUCTOR:
2679 unsigned HOST_WIDE_INT idx;
2680 tree value;
2682 hi = 5 + int_size_in_bytes (TREE_TYPE (exp));
2684 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
2685 if (value)
2686 hi = hi * 603 + const_hash_1 (value);
2688 return hi;
2691 case ADDR_EXPR:
2692 case FDESC_EXPR:
2694 struct addr_const value;
2696 decode_addr_const (exp, &value);
2697 switch (GET_CODE (value.base))
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;
2708 case LABEL_REF:
2709 hi = value.offset + CODE_LABEL_NUMBER (XEXP (value.base, 0)) * 13;
2710 break;
2712 default:
2713 gcc_unreachable ();
2716 return hi;
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)));
2724 CASE_CONVERT:
2725 return const_hash_1 (TREE_OPERAND (exp, 0)) * 7 + 2;
2727 default:
2728 /* A language specific constant. Just hash the code. */
2729 return code;
2732 /* Compute hashing function. */
2733 hi = len;
2734 for (i = 0; i < len; i++)
2735 hi = ((hi * 613) + (unsigned) (p[i]));
2737 return hi;
2740 /* Wrapper of compare_constant, for the htab interface. */
2741 static int
2742 const_desc_eq (const void *p1, const void *p2)
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);
2753 /* Compare t1 and t2, and return 1 only if they are known to result in
2754 the same bit pattern on output. */
2756 static int
2757 compare_constant (const tree t1, const tree t2)
2759 enum tree_code typecode;
2761 if (t1 == NULL_TREE)
2762 return t2 == NULL_TREE;
2763 if (t2 == NULL_TREE)
2764 return 0;
2766 if (TREE_CODE (t1) != TREE_CODE (t2))
2767 return 0;
2769 switch (TREE_CODE (t1))
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);
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;
2784 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
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;
2791 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
2793 case STRING_CST:
2794 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
2795 return 0;
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)));
2801 case COMPLEX_CST:
2802 return (compare_constant (TREE_REALPART (t1), TREE_REALPART (t2))
2803 && compare_constant (TREE_IMAGPART (t1), TREE_IMAGPART (t2)));
2805 case VECTOR_CST:
2807 tree link1, link2;
2809 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (t1))
2810 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (t2)))
2811 return 0;
2813 link2 = TREE_VECTOR_CST_ELTS (t2);
2814 for (link1 = TREE_VECTOR_CST_ELTS (t1);
2815 link1;
2816 link1 = TREE_CHAIN (link1))
2818 if (!compare_constant (TREE_VALUE (link1), TREE_VALUE (link2)))
2819 return 0;
2820 link2 = TREE_CHAIN (link2);
2823 return 1;
2826 case CONSTRUCTOR:
2828 VEC(constructor_elt, gc) *v1, *v2;
2829 unsigned HOST_WIDE_INT idx;
2831 typecode = TREE_CODE (TREE_TYPE (t1));
2832 if (typecode != TREE_CODE (TREE_TYPE (t2)))
2833 return 0;
2835 if (typecode == ARRAY_TYPE)
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;
2844 else
2846 /* For record and union constructors, require exact type
2847 equality. */
2848 if (TREE_TYPE (t1) != TREE_TYPE (t2))
2849 return 0;
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;
2858 for (idx = 0; idx < VEC_length (constructor_elt, v1); ++idx)
2860 constructor_elt *c1 = VEC_index (constructor_elt, v1, idx);
2861 constructor_elt *c2 = VEC_index (constructor_elt, v2, idx);
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)
2869 if (!compare_constant (c1->index, c2->index))
2870 return 0;
2872 else
2874 if (c1->index != c2->index)
2875 return 0;
2879 return 1;
2882 case ADDR_EXPR:
2883 case FDESC_EXPR:
2885 struct addr_const value1, value2;
2886 enum rtx_code code;
2887 int ret;
2889 decode_addr_const (t1, &value1);
2890 decode_addr_const (t2, &value2);
2892 if (value1.offset != value2.offset)
2893 return 0;
2895 code = GET_CODE (value1.base);
2896 if (code != GET_CODE (value2.base))
2897 return 0;
2899 switch (code)
2901 case SYMBOL_REF:
2902 ret = (strcmp (XSTR (value1.base, 0), XSTR (value2.base, 0)) == 0);
2903 break;
2905 case LABEL_REF:
2906 ret = (CODE_LABEL_NUMBER (XEXP (value1.base, 0))
2907 == CODE_LABEL_NUMBER (XEXP (value2.base, 0)));
2908 break;
2910 default:
2911 gcc_unreachable ();
2913 return ret;
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)));
2923 CASE_CONVERT:
2924 case VIEW_CONVERT_EXPR:
2925 return compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2927 default:
2928 return 0;
2931 gcc_unreachable ();
2934 /* Make a copy of the whole tree structure for a constant. This
2935 handles the same types of nodes that compare_constant handles. */
2937 static tree
2938 copy_constant (tree exp)
2940 switch (TREE_CODE (exp))
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);
2951 case INTEGER_CST:
2952 case REAL_CST:
2953 case FIXED_CST:
2954 case STRING_CST:
2955 return copy_node (exp);
2957 case COMPLEX_CST:
2958 return build_complex (TREE_TYPE (exp),
2959 copy_constant (TREE_REALPART (exp)),
2960 copy_constant (TREE_IMAGPART (exp)));
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)));
2969 CASE_CONVERT:
2970 case VIEW_CONVERT_EXPR:
2971 return build1 (TREE_CODE (exp), TREE_TYPE (exp),
2972 copy_constant (TREE_OPERAND (exp, 0)));
2974 case VECTOR_CST:
2975 return build_vector (TREE_TYPE (exp),
2976 copy_list (TREE_VECTOR_CST_ELTS (exp)));
2978 case CONSTRUCTOR:
2980 tree copy = copy_node (exp);
2981 VEC(constructor_elt, gc) *v;
2982 unsigned HOST_WIDE_INT idx;
2983 tree purpose, value;
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)
2989 constructor_elt *ce = VEC_quick_push (constructor_elt, v, NULL);
2990 ce->index = purpose;
2991 ce->value = copy_constant (value);
2993 CONSTRUCTOR_ELTS (copy) = v;
2994 return copy;
2997 default:
2998 gcc_unreachable ();
3002 /* Return the section into which constant EXP should be placed. */
3004 static section *
3005 get_constant_section (tree exp, unsigned int align)
3007 return targetm.asm_out.select_section (exp,
3008 compute_reloc_for_constant (exp),
3009 align);
3012 /* Return the size of constant EXP in bytes. */
3014 static HOST_WIDE_INT
3015 get_constant_size (tree exp)
3017 HOST_WIDE_INT size;
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;
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. */
3032 static struct constant_descriptor_tree *
3033 build_constant_desc (tree exp)
3035 struct constant_descriptor_tree *desc;
3036 rtx symbol, rtl;
3037 char label[256];
3038 int labelno;
3039 tree decl;
3041 desc = ggc_alloc_constant_descriptor_tree ();
3042 desc->value = copy_constant (exp);
3044 /* Propagate marked-ness to copied constant. */
3045 if (flag_mudflap && mf_marked_p (exp))
3046 mf_mark (desc->value);
3048 /* Create a string containing the label name, in LABEL. */
3049 labelno = const_labelno++;
3050 ASM_GENERATE_INTERNAL_LABEL (label, "LC", labelno);
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)
3069 #ifdef CONSTANT_ALIGNMENT
3070 DECL_ALIGN (decl) = CONSTANT_ALIGNMENT (exp, DECL_ALIGN (decl));
3071 #endif
3073 else
3074 align_variable (decl, 0);
3076 /* Now construct the SYMBOL_REF and the MEM. */
3077 if (use_object_blocks_p ())
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);
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;
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);
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;
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);
3105 desc->rtl = rtl;
3107 return desc;
3110 /* Return an rtx representing a reference to constant data in memory
3111 for the constant expression EXP.
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.
3118 If DEFER is nonzero, this constant can be deferred and output only
3119 if referenced in the function after all optimizations.
3121 `const_desc_table' records which constants already have label strings. */
3124 output_constant_def (tree exp, int defer)
3126 struct constant_descriptor_tree *desc;
3127 struct constant_descriptor_tree key;
3128 void **loc;
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);
3136 desc = (struct constant_descriptor_tree *) *loc;
3137 if (desc == 0)
3139 desc = build_constant_desc (exp);
3140 desc->hash = key.hash;
3141 *loc = desc;
3144 maybe_output_constant_def_contents (desc, defer);
3145 return desc->rtl;
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)
3154 rtx symbol = XEXP (desc->rtl, 0);
3155 tree exp = desc->value;
3157 if (flag_syntax_only)
3158 return;
3160 if (TREE_ASM_WRITTEN (exp))
3161 /* Already output; don't do it again. */
3162 return;
3164 /* We can always defer constants as long as the context allows
3165 doing so. */
3166 if (defer)
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;
3178 output_constant_def_contents (symbol);
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. */
3185 static void
3186 assemble_constant_contents (tree exp, const char *label, unsigned int align)
3188 HOST_WIDE_INT size;
3190 size = get_constant_size (exp);
3192 /* Do any machine/system dependent processing of the constant. */
3193 targetm.asm_out.declare_constant_name (asm_out_file, label, exp, size);
3195 /* Output the value of EXP. */
3196 output_constant (exp, size, align);
3199 /* We must output the constant data referred to by SYMBOL; do so. */
3201 static void
3202 output_constant_def_contents (rtx symbol)
3204 tree decl = SYMBOL_REF_DECL (symbol);
3205 tree exp = DECL_INITIAL (decl);
3206 unsigned int align;
3208 /* Make sure any other constants whose addresses appear in EXP
3209 are assigned label numbers. */
3210 output_addressed_constants (exp);
3212 /* We are no longer deferring this constant. */
3213 TREE_ASM_WRITTEN (decl) = TREE_ASM_WRITTEN (exp) = 1;
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
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);
3228 if (flag_mudflap)
3229 mudflap_enqueue_constant (exp);
3232 /* Look up EXP in the table of constant descriptors. Return the rtl
3233 if it has been emitted, else null. */
3236 lookup_constant_def (tree exp)
3238 struct constant_descriptor_tree *desc;
3239 struct constant_descriptor_tree key;
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);
3246 return (desc ? desc->rtl : NULL_RTX);
3249 /* Return a tree representing a reference to constant data in memory
3250 for the constant expression EXP.
3252 This is the counterpart of output_constant_def at the Tree level. */
3254 tree
3255 tree_output_constant_def (tree exp)
3257 struct constant_descriptor_tree *desc, key;
3258 void **loc;
3259 tree decl;
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);
3267 desc = (struct constant_descriptor_tree *) *loc;
3268 if (desc == 0)
3270 desc = build_constant_desc (exp);
3271 desc->hash = key.hash;
3272 *loc = desc;
3275 decl = SYMBOL_REF_DECL (XEXP (desc->rtl, 0));
3276 varpool_finalize_decl (decl);
3277 return decl;
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. */
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;
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;
3298 /* Current offset in constant pool (does not include any
3299 machine-specific header). */
3300 HOST_WIDE_INT offset;
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;
3316 /* Hash and compare functions for const_rtx_htab. */
3318 static hashval_t
3319 const_desc_rtx_hash (const void *ptr)
3321 const struct constant_descriptor_rtx *const desc
3322 = (const struct constant_descriptor_rtx *) ptr;
3323 return desc->hash;
3326 static int
3327 const_desc_rtx_eq (const void *a, const void *b)
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;
3334 if (x->mode != y->mode)
3335 return 0;
3336 return rtx_equal_p (x->constant, y->constant);
3339 /* This is the worker function for const_rtx_hash, called via for_each_rtx. */
3341 static int
3342 const_rtx_hash_1 (rtx *xp, void *data)
3344 unsigned HOST_WIDE_INT hwi;
3345 enum machine_mode mode;
3346 enum rtx_code code;
3347 hashval_t h, *hp;
3348 rtx x;
3350 x = *xp;
3351 code = GET_CODE (x);
3352 mode = GET_MODE (x);
3353 h = (hashval_t) code * 1048573 + mode;
3355 switch (code)
3357 case CONST_INT:
3358 hwi = INTVAL (x);
3359 fold_hwi:
3361 int shift = sizeof (hashval_t) * CHAR_BIT;
3362 const int n = sizeof (HOST_WIDE_INT) / sizeof (hashval_t);
3363 int i;
3365 h ^= (hashval_t) hwi;
3366 for (i = 1; i < n; ++i)
3368 hwi >>= shift;
3369 h ^= (hashval_t) hwi;
3372 break;
3374 case CONST_DOUBLE:
3375 if (mode == VOIDmode)
3377 hwi = CONST_DOUBLE_LOW (x) ^ CONST_DOUBLE_HIGH (x);
3378 goto fold_hwi;
3380 else
3381 h ^= real_hash (CONST_DOUBLE_REAL_VALUE (x));
3382 break;
3384 case CONST_FIXED:
3385 h ^= fixed_hash (CONST_FIXED_VALUE (x));
3386 break;
3388 case CONST_VECTOR:
3390 int i;
3391 for (i = XVECLEN (x, 0); i-- > 0; )
3392 h = h * 251 + const_rtx_hash_1 (&XVECEXP (x, 0, i), data);
3394 break;
3396 case SYMBOL_REF:
3397 h ^= htab_hash_string (XSTR (x, 0));
3398 break;
3400 case LABEL_REF:
3401 h = h * 251 + CODE_LABEL_NUMBER (XEXP (x, 0));
3402 break;
3404 case UNSPEC:
3405 case UNSPEC_VOLATILE:
3406 h = h * 251 + XINT (x, 1);
3407 break;
3409 default:
3410 break;
3413 hp = (hashval_t *) data;
3414 *hp = *hp * 509 + h;
3415 return 0;
3418 /* Compute a hash value for X, which should be a constant. */
3420 static hashval_t
3421 const_rtx_hash (rtx x)
3423 hashval_t h = 0;
3424 for_each_rtx (&x, const_rtx_hash_1, &h);
3425 return h;
3429 /* Create and return a new rtx constant pool. */
3431 static struct rtx_constant_pool *
3432 create_constant_pool (void)
3434 struct rtx_constant_pool *pool;
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;
3445 /* Initialize constant pool hashing for a new function. */
3447 void
3448 init_varasm_status (void)
3450 crtl->varasm.pool = create_constant_pool ();
3451 crtl->varasm.deferred_constants = 0;
3454 /* Given a MINUS expression, simplify it if both sides
3455 include the same symbol. */
3458 simplify_subtraction (rtx x)
3460 rtx r = simplify_rtx (x);
3461 return r ? r : x;
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. */
3468 force_const_mem (enum machine_mode mode, rtx x)
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;
3478 /* If we're not allowed to drop X into the constant pool, don't. */
3479 if (targetm.cannot_force_const_mem (mode, x))
3480 return NULL_RTX;
3482 /* Record that this function has used a constant pool entry. */
3483 crtl->uses_const_pool = 1;
3485 /* Decide which pool to use. */
3486 pool = (targetm.use_blocks_for_constant_p (mode, x)
3487 ? shared_constant_pool
3488 : crtl->varasm.pool);
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;
3497 /* If the constant was already present, return its memory. */
3498 if (desc)
3499 return copy_rtx (desc->mem);
3501 /* Otherwise, create a new descriptor. */
3502 desc = ggc_alloc_constant_descriptor_rtx ();
3503 *slot = desc;
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
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);
3513 #endif
3515 pool->offset += (align / BITS_PER_UNIT) - 1;
3516 pool->offset &= ~ ((align / BITS_PER_UNIT) - 1);
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;
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;
3534 /* Create a string containing the label name, in LABEL. */
3535 ASM_GENERATE_INTERNAL_LABEL (label, "LC", const_labelno);
3536 ++const_labelno;
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))
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);
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);
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);
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;
3563 return copy_rtx (def);
3566 /* Given a constant pool SYMBOL_REF, return the corresponding constant. */
3569 get_pool_constant (rtx addr)
3571 return SYMBOL_REF_CONSTANT (addr)->constant;
3574 /* Given a constant pool SYMBOL_REF, return the corresponding constant
3575 and whether it has been output or not. */
3578 get_pool_constant_mark (rtx addr, bool *pmarked)
3580 struct constant_descriptor_rtx *desc;
3582 desc = SYMBOL_REF_CONSTANT (addr);
3583 *pmarked = (desc->mark != 0);
3584 return desc->constant;
3587 /* Similar, return the mode. */
3589 enum machine_mode
3590 get_pool_mode (const_rtx addr)
3592 return SYMBOL_REF_CONSTANT (addr)->mode;
3595 /* Return the size of the constant pool. */
3598 get_pool_size (void)
3600 return crtl->varasm.pool->offset;
3603 /* Worker function for output_constant_pool_1. Emit assembly for X
3604 in MODE with known alignment ALIGN. */
3606 static void
3607 output_constant_pool_2 (enum machine_mode mode, rtx x, unsigned int align)
3609 switch (GET_MODE_CLASS (mode))
3611 case MODE_FLOAT:
3612 case MODE_DECIMAL_FLOAT:
3614 REAL_VALUE_TYPE r;
3616 gcc_assert (GET_CODE (x) == CONST_DOUBLE);
3617 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
3618 assemble_real (r, mode, align);
3619 break;
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;
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:
3638 int i, units;
3639 enum machine_mode submode = GET_MODE_INNER (mode);
3640 unsigned int subalign = MIN (align, GET_MODE_BITSIZE (submode));
3642 gcc_assert (GET_CODE (x) == CONST_VECTOR);
3643 units = CONST_VECTOR_NUNITS (x);
3645 for (i = 0; i < units; i++)
3647 rtx elt = CONST_VECTOR_ELT (x, i);
3648 output_constant_pool_2 (submode, elt, i ? subalign : align);
3651 break;
3653 default:
3654 gcc_unreachable ();
3658 /* Worker function for output_constant_pool. Emit constant DESC,
3659 giving it ALIGN bits of alignment. */
3661 static void
3662 output_constant_pool_1 (struct constant_descriptor_rtx *desc,
3663 unsigned int align)
3665 rtx x, tmp;
3667 x = desc->constant;
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. */
3678 tmp = x;
3679 switch (GET_CODE (tmp))
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 */
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;
3695 default:
3696 break;
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
3704 assemble_align (align);
3706 /* Output the label. */
3707 targetm.asm_out.internal_label (asm_out_file, "LC", desc->labelno);
3709 /* Output the data. */
3710 output_constant_pool_2 (desc->mode, x, align);
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);
3719 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3720 done:
3721 #endif
3722 return;
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. */
3729 static int
3730 mark_constant (rtx *current_rtx, void *data ATTRIBUTE_UNUSED)
3732 rtx x = *current_rtx;
3734 if (x == NULL_RTX || GET_CODE (x) != SYMBOL_REF)
3735 return 0;
3737 if (CONSTANT_POOL_ADDRESS_P (x))
3739 struct constant_descriptor_rtx *desc = SYMBOL_REF_CONSTANT (x);
3740 if (desc->mark == 0)
3742 desc->mark = 1;
3743 for_each_rtx (&desc->constant, mark_constant, NULL);
3746 else if (TREE_CONSTANT_POOL_ADDRESS_P (x))
3748 tree decl = SYMBOL_REF_DECL (x);
3749 if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
3751 n_deferred_constants--;
3752 output_constant_def_contents (x);
3756 return -1;
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. */
3764 static void
3765 mark_constants (rtx insn)
3767 if (!INSN_P (insn))
3768 return;
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)
3775 rtx seq = PATTERN (insn);
3776 int i, n = XVECLEN (seq, 0);
3777 for (i = 0; i < n; ++i)
3779 rtx subinsn = XVECEXP (seq, 0, i);
3780 if (INSN_P (subinsn))
3781 for_each_rtx (&PATTERN (subinsn), mark_constant, NULL);
3784 else
3785 for_each_rtx (&PATTERN (insn), mark_constant, NULL);
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. */
3792 static void
3793 mark_constant_pool (void)
3795 rtx insn, link;
3797 if (!crtl->uses_const_pool && n_deferred_constants == 0)
3798 return;
3800 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3801 mark_constants (insn);
3803 for (link = crtl->epilogue_delay_list;
3804 link;
3805 link = XEXP (link, 1))
3806 mark_constants (XEXP (link, 0));
3809 /* Write all the constants in POOL. */
3811 static void
3812 output_constant_pool_contents (struct rtx_constant_pool *pool)
3814 struct constant_descriptor_rtx *desc;
3816 for (desc = pool->first; desc ; desc = desc->next)
3817 if (desc->mark)
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
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);
3835 /* Mark all constants that are used in the current function, then write
3836 out the function's private constant pool. */
3838 static void
3839 output_constant_pool (const char *fnname ATTRIBUTE_UNUSED,
3840 tree fndecl ATTRIBUTE_UNUSED)
3842 struct rtx_constant_pool *pool = crtl->varasm.pool;
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 ();
3849 #ifdef ASM_OUTPUT_POOL_PROLOGUE
3850 ASM_OUTPUT_POOL_PROLOGUE (asm_out_file, fnname, fndecl, pool->offset);
3851 #endif
3853 output_constant_pool_contents (pool);
3855 #ifdef ASM_OUTPUT_POOL_EPILOGUE
3856 ASM_OUTPUT_POOL_EPILOGUE (asm_out_file, fnname, fndecl, pool->offset);
3857 #endif
3860 /* Write the contents of the shared constant pool. */
3862 void
3863 output_shared_constant_pool (void)
3865 output_constant_pool_contents (shared_constant_pool);
3868 /* Determine what kind of relocations EXP may need. */
3871 compute_reloc_for_constant (tree exp)
3873 int reloc = 0, reloc2;
3874 tree tem;
3876 switch (TREE_CODE (exp))
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))
3887 if (TREE_PUBLIC (tem))
3888 reloc |= 2;
3889 else
3890 reloc |= 1;
3891 break;
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;
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;
3909 CASE_CONVERT:
3910 case VIEW_CONVERT_EXPR:
3911 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3912 break;
3914 case CONSTRUCTOR:
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);
3921 break;
3923 default:
3924 break;
3926 return reloc;
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. */
3933 static void
3934 output_addressed_constants (tree exp)
3936 tree tem;
3938 switch (TREE_CODE (exp))
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))
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);
3953 if (CONSTANT_CLASS_P (tem) || TREE_CODE (tem) == CONSTRUCTOR)
3954 output_constant_def (tem, 0);
3955 break;
3957 case PLUS_EXPR:
3958 case POINTER_PLUS_EXPR:
3959 case MINUS_EXPR:
3960 output_addressed_constants (TREE_OPERAND (exp, 1));
3961 /* Fall through. */
3963 CASE_CONVERT:
3964 case VIEW_CONVERT_EXPR:
3965 output_addressed_constants (TREE_OPERAND (exp, 0));
3966 break;
3968 case CONSTRUCTOR:
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);
3975 break;
3977 default:
3978 break;
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. */
3987 bool
3988 constructor_static_from_elts_p (const_tree ctor)
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));
3996 static tree initializer_constant_valid_p_1 (tree value, tree endtype,
3997 tree *cache);
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. */
4007 static tree
4008 narrowing_initializer_constant_valid_p (tree value, tree endtype, tree *cache)
4010 tree op0, op1;
4012 if (!INTEGRAL_TYPE_P (endtype))
4013 return NULL_TREE;
4015 op0 = TREE_OPERAND (value, 0);
4016 op1 = TREE_OPERAND (value, 1);
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. */
4023 while (CONVERT_EXPR_P (op0)
4024 || TREE_CODE (op0) == NON_LVALUE_EXPR)
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;
4035 while (CONVERT_EXPR_P (op1)
4036 || TREE_CODE (op1) == NON_LVALUE_EXPR)
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;
4047 op0 = initializer_constant_valid_p_1 (op0, endtype, cache);
4048 if (!op0)
4049 return NULL_TREE;
4051 op1 = initializer_constant_valid_p_1 (op1, endtype,
4052 cache ? cache + 2 : NULL);
4053 /* Both initializers must be known. */
4054 if (op1)
4056 if (op0 == op1
4057 && (op0 == null_pointer_node
4058 || TREE_CODE (value) == MINUS_EXPR))
4059 return null_pointer_node;
4061 /* Support differences between labels. */
4062 if (TREE_CODE (op0) == LABEL_DECL
4063 && TREE_CODE (op1) == LABEL_DECL)
4064 return null_pointer_node;
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;
4072 return NULL_TREE;
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.
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.
4086 Use CACHE (pointer to 2 tree values) for caching if non-NULL. */
4088 static tree
4089 initializer_constant_valid_p_1 (tree value, tree endtype, tree *cache)
4091 tree ret;
4093 switch (TREE_CODE (value))
4095 case CONSTRUCTOR:
4096 if (constructor_static_from_elts_p (value))
4098 unsigned HOST_WIDE_INT idx;
4099 tree elt;
4100 bool absolute = true;
4102 if (cache && cache[0] == value)
4103 return cache[1];
4104 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
4106 tree reloc;
4107 reloc = initializer_constant_valid_p_1 (elt, TREE_TYPE (elt),
4108 NULL);
4109 if (!reloc)
4111 if (cache)
4113 cache[0] = value;
4114 cache[1] = NULL_TREE;
4116 return NULL_TREE;
4118 if (reloc != null_pointer_node)
4119 absolute = false;
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)
4126 cache[0] = value;
4127 cache[1] = absolute ? null_pointer_node : error_mark_node;
4129 return absolute ? null_pointer_node : error_mark_node;
4132 return TREE_STATIC (value) ? null_pointer_node : NULL_TREE;
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;
4142 case ADDR_EXPR:
4143 case FDESC_EXPR:
4145 tree op0 = staticp (TREE_OPERAND (value, 0));
4146 if (op0)
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;
4164 return op0;
4167 case NON_LVALUE_EXPR:
4168 return initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4169 endtype, cache);
4171 case VIEW_CONVERT_EXPR:
4173 tree src = TREE_OPERAND (value, 0);
4174 tree src_type = TREE_TYPE (src);
4175 tree dest_type = TREE_TYPE (value);
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))
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;
4189 /* Allow all other kinds of view-conversion. */
4190 return initializer_constant_valid_p_1 (src, endtype, cache);
4193 CASE_CONVERT:
4195 tree src = TREE_OPERAND (value, 0);
4196 tree src_type = TREE_TYPE (src);
4197 tree dest_type = TREE_TYPE (value);
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);
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);
4212 /* Allow conversions between other integer types only if
4213 explicit value. */
4214 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type))
4216 tree inner = initializer_constant_valid_p_1 (src, endtype, cache);
4217 if (inner == null_pointer_node)
4218 return null_pointer_node;
4219 break;
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);
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))
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);
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);
4248 break;
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)))
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);
4278 else
4279 /* Support narrowing pointer differences. */
4280 ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
4281 if (cache)
4283 cache[0] = value;
4284 cache[1] = ret;
4286 return ret;
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)))
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);
4322 else
4323 /* Support narrowing differences. */
4324 ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
4325 if (cache)
4327 cache[0] = value;
4328 cache[1] = ret;
4330 return ret;
4332 default:
4333 break;
4336 return NULL_TREE;
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.
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)
4351 return initializer_constant_valid_p_1 (value, endtype, NULL);
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. */
4358 bool
4359 initializer_constant_valid_for_bitfield_p (tree value)
4361 /* For bitfields we support integer constants or possibly nested aggregates
4362 of such. */
4363 switch (TREE_CODE (value))
4365 case CONSTRUCTOR:
4367 unsigned HOST_WIDE_INT idx;
4368 tree elt;
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;
4376 case INTEGER_CST:
4377 return true;
4379 case VIEW_CONVERT_EXPR:
4380 case NON_LVALUE_EXPR:
4381 return
4382 initializer_constant_valid_for_bitfield_p (TREE_OPERAND (value, 0));
4384 default:
4385 break;
4388 return false;
4391 /* output_constructor outer state of relevance in recursive calls, typically
4392 for nested aggregate bitfields. */
4394 typedef struct {
4395 unsigned int bit_offset; /* current position in ... */
4396 int byte; /* ... the outer byte buffer. */
4397 } oc_outer_state;
4399 static unsigned HOST_WIDE_INT
4400 output_constructor (tree, unsigned HOST_WIDE_INT, unsigned int,
4401 oc_outer_state *);
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.
4407 Generate exactly SIZE bytes of assembler data, padding at the end
4408 with zeros if necessary. SIZE must always be specified.
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.
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.
4421 ALIGN is the alignment of the data in bits. */
4423 void
4424 output_constant (tree exp, unsigned HOST_WIDE_INT size, unsigned int align)
4426 enum tree_code code;
4427 unsigned HOST_WIDE_INT thissize;
4429 if (size == 0 || flag_syntax_only)
4430 return;
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)))))
4442 tree saved_type = TREE_TYPE (exp);
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);
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));
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)
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)));
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);
4486 code = TREE_CODE (TREE_TYPE (exp));
4487 thissize = int_size_in_bytes (TREE_TYPE (exp));
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)))
4494 assemble_zeros (size);
4495 return;
4498 if (TREE_CODE (exp) == FDESC_EXPR)
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;
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)
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;
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;
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;
4540 case ARRAY_TYPE:
4541 case VECTOR_TYPE:
4542 switch (TREE_CODE (exp))
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;
4553 case VECTOR_CST:
4555 int elt_size;
4556 tree link;
4557 unsigned int nalign;
4558 enum machine_mode inner;
4560 inner = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
4561 nalign = MIN (align, GET_MODE_ALIGNMENT (inner));
4563 elt_size = GET_MODE_SIZE (inner);
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)
4570 output_constant (TREE_VALUE (link), elt_size, nalign);
4571 thissize += elt_size;
4573 break;
4575 default:
4576 gcc_unreachable ();
4578 break;
4580 case RECORD_TYPE:
4581 case UNION_TYPE:
4582 gcc_assert (TREE_CODE (exp) == CONSTRUCTOR);
4583 output_constructor (exp, size, align, NULL);
4584 return;
4586 case ERROR_MARK:
4587 return;
4589 default:
4590 gcc_unreachable ();
4593 if (size > thissize)
4594 assemble_zeros (size - thissize);
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. */
4602 static unsigned HOST_WIDE_INT
4603 array_size_for_constructor (tree val)
4605 tree max_index, i;
4606 unsigned HOST_WIDE_INT cnt;
4607 tree index, value, tmp;
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);
4615 max_index = NULL_TREE;
4616 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (val), cnt, index, value)
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;
4624 if (max_index == NULL_TREE)
4625 return 0;
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);
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))));
4636 return tree_low_cst (i, 1);
4639 /* Other datastructures + helpers for output_constructor. */
4641 /* output_constructor local state to support interaction with helpers. */
4643 typedef struct {
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. */
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. */
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. */
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. */
4667 } oc_local_state;
4669 /* Helper for output_constructor. From the current LOCAL state, output a
4670 RANGE_EXPR element. */
4672 static void
4673 output_constructor_array_range (oc_local_state *local)
4675 unsigned HOST_WIDE_INT fieldsize
4676 = int_size_in_bytes (TREE_TYPE (local->type));
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;
4684 unsigned int align2
4685 = min_align (local->align, fieldsize * BITS_PER_UNIT);
4687 for (index = lo_index; index <= hi_index; index++)
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);
4695 /* Count its size. */
4696 local->total_bytes += fieldsize;
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. */
4703 static void
4704 output_constructor_regular_field (oc_local_state *local)
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;
4711 unsigned int align2;
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;
4722 /* Output any buffered-up bit-fields preceding this element. */
4723 if (local->byte_buffer_in_use)
4725 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
4726 local->total_bytes++;
4727 local->byte_buffer_in_use = false;
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)
4736 gcc_assert (fieldpos >= local->total_bytes);
4737 assemble_zeros (fieldpos - local->total_bytes);
4738 local->total_bytes = fieldpos;
4741 /* Find the alignment of this element. */
4742 align2 = min_align (local->align, BITS_PER_UNIT * fieldpos);
4744 /* Determine size this element should occupy. */
4745 if (local->field)
4747 fieldsize = 0;
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))))
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));
4763 else if (DECL_SIZE_UNIT (local->field))
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);
4771 else
4772 fieldsize = int_size_in_bytes (TREE_TYPE (local->type));
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);
4780 /* Count its size. */
4781 local->total_bytes += fieldsize;
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. */
4787 static void
4788 output_constructor_bitfield (oc_local_state *local, oc_outer_state *outer)
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));
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);
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);
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);
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;
4823 local->last_relative_index = relative_index;
4825 if (local->val == NULL_TREE)
4826 local->val = integer_zero_node;
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);
4832 if (TREE_CODE (local->val) != INTEGER_CST
4833 && TREE_CODE (local->val) != CONSTRUCTOR)
4835 error ("invalid initial value for member %qE", DECL_NAME (local->field));
4836 return;
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)
4843 /* Output remnant of any bit field in previous bytes. */
4844 if (local->byte_buffer_in_use)
4846 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
4847 local->total_bytes++;
4848 local->byte_buffer_in_use = false;
4851 /* If still not at proper byte, advance to there. */
4852 if (next_offset / BITS_PER_UNIT != local->total_bytes)
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;
4860 /* Set up the buffer if necessary. */
4861 if (!local->byte_buffer_in_use)
4863 local->byte = 0;
4864 if (ebitsize > 0)
4865 local->byte_buffer_in_use = true;
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)
4872 oc_outer_state output_state;
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;
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)
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;
4893 /* Advance from byte to byte
4894 within this element when necessary. */
4895 while (next_byte != local->total_bytes)
4897 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
4898 local->total_bytes++;
4899 local->byte = 0;
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)
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;
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)
4920 this_time = shift + this_time - HOST_BITS_PER_WIDE_INT;
4921 shift = HOST_BITS_PER_WIDE_INT;
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
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;
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));
4940 else
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;
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);
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
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;
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);
4973 next_offset += this_time;
4974 local->byte_buffer_in_use = true;
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. */
4982 static unsigned HOST_WIDE_INT
4983 output_constructor (tree exp, unsigned HOST_WIDE_INT size,
4984 unsigned int align, oc_outer_state * outer)
4986 unsigned HOST_WIDE_INT cnt;
4987 constructor_elt *ce;
4989 oc_local_state local;
4991 /* Setup our local state to communicate with helpers. */
4992 local.exp = exp;
4993 local.size = size;
4994 local.align = align;
4996 local.total_bytes = 0;
4997 local.byte_buffer_in_use = outer != NULL;
4998 local.byte = outer ? outer->byte : 0;
5000 local.type = TREE_TYPE (exp);
5002 local.last_relative_index = -1;
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));
5009 gcc_assert (HOST_BITS_PER_WIDE_INT >= BITS_PER_UNIT);
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.
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). */
5020 local.field = NULL_TREE;
5021 if (TREE_CODE (local.type) == RECORD_TYPE)
5022 local.field = TYPE_FIELDS (local.type);
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)
5028 local.val = ce->value;
5029 local.index = NULL_TREE;
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;
5039 else if (TREE_CODE (local.type) == ARRAY_TYPE)
5040 local.index = ce->index;
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
5051 /* Eliminate the marker that makes a cast not be an lvalue. */
5052 if (local.val != NULL_TREE)
5053 STRIP_NOPS (local.val);
5055 /* Output the current element, using the appropriate helper ... */
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);
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);
5070 /* For a true bitfield or part of an outer one. */
5071 else
5072 output_constructor_bitfield (&local, outer);
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
5081 if (local.byte_buffer_in_use)
5083 assemble_integer (GEN_INT (local.byte), 1, BITS_PER_UNIT, 1);
5084 local.total_bytes++;
5087 if ((unsigned HOST_WIDE_INT)local.total_bytes < local.size)
5089 assemble_zeros (local.size - local.total_bytes);
5090 local.total_bytes = local.size;
5094 return local.total_bytes;
5097 /* Mark DECL as weak. */
5099 static void
5100 mark_weak (tree decl)
5102 DECL_WEAK (decl) = 1;
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;
5111 /* Merge weak status between NEWDECL and OLDDECL. */
5113 void
5114 merge_weak (tree newdecl, tree olddecl)
5116 if (DECL_WEAK (newdecl) == DECL_WEAK (olddecl))
5118 if (DECL_WEAK (newdecl) && TARGET_SUPPORTS_WEAK)
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)
5126 *pwd = TREE_CHAIN (*pwd);
5127 break;
5130 return;
5133 if (DECL_WEAK (newdecl))
5135 tree wd;
5137 /* NEWDECL is weak, but OLDDECL is not. */
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));
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)));
5151 if (TARGET_SUPPORTS_WEAK)
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)
5158 TREE_VALUE (wd) = olddecl;
5159 break;
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. */
5167 /* Make the OLDDECL weak; it's OLDDECL that we'll be keeping. */
5168 mark_weak (olddecl);
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);
5176 /* Declare DECL to be a weak symbol. */
5178 void
5179 declare_weak (tree decl)
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);
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));
5193 static void
5194 weak_finish_1 (tree decl)
5196 #if defined (ASM_WEAKEN_DECL) || defined (ASM_WEAKEN_LABEL)
5197 const char *const name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5198 #endif
5200 if (! TREE_USED (decl))
5201 return;
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
5211 static bool warn_once = 0;
5212 if (! warn_once)
5214 warning (0, "only weak aliases are supported in this configuration");
5215 warn_once = 1;
5217 return;
5219 #endif
5220 #endif
5221 #endif
5224 /* This TREE_LIST contains weakref targets. */
5226 static GTY(()) tree weakref_targets;
5228 /* Forward declaration. */
5229 static tree find_decl_and_mark_needed (tree decl, tree target);
5231 /* Emit any pending weak declarations. */
5233 void
5234 weak_finish (void)
5236 tree t;
5238 for (t = weakref_targets; t; t = TREE_CHAIN (t))
5240 tree alias_decl = TREE_PURPOSE (t);
5241 tree target = ultimate_transparent_alias_target (&TREE_VALUE (t));
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))
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);
5258 if (! decl)
5260 decl = build_decl (DECL_SOURCE_LOCATION (alias_decl),
5261 TREE_CODE (alias_decl), target,
5262 TREE_TYPE (alias_decl));
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;
5271 weak_finish_1 (decl);
5272 # endif
5274 #endif
5277 tree *p;
5278 tree t2;
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) ; )
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);
5292 /* Remove other weakrefs to the same target, to speed things up. */
5293 for (p = &TREE_CHAIN (t); (t2 = *p) ; )
5295 if (target == ultimate_transparent_alias_target (&TREE_VALUE (t2)))
5296 *p = TREE_CHAIN (t2);
5297 else
5298 p = &TREE_CHAIN (t2);
5303 for (t = weak_decls; t; t = TREE_CHAIN (t))
5305 tree decl = TREE_VALUE (t);
5307 weak_finish_1 (decl);
5311 /* Emit the assembly bits to indicate that DECL is globally visible. */
5313 static void
5314 globalize_decl (tree decl)
5317 #if defined (ASM_WEAKEN_LABEL) || defined (ASM_WEAKEN_DECL)
5318 if (DECL_WEAK (decl))
5320 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
5321 tree *p, t;
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
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) ; )
5333 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
5334 *p = TREE_CHAIN (t);
5335 else
5336 p = &TREE_CHAIN (t);
5339 /* Remove weakrefs to the same target from the pending weakref
5340 list, for the same reason. */
5341 for (p = &weakref_targets; (t = *p) ; )
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);
5350 return;
5352 #endif
5354 targetm.asm_out.globalize_decl_name (asm_out_file, decl);
5357 VEC(alias_pair,gc) *alias_pairs;
5359 /* Given an assembly name, find the decl it is associated with. At the
5360 same time, mark it needed for cgraph. */
5362 static tree
5363 find_decl_and_mark_needed (tree decl, tree target)
5365 struct cgraph_node *fnode = NULL;
5366 struct varpool_node *vnode = NULL;
5368 if (TREE_CODE (decl) == FUNCTION_DECL)
5370 fnode = cgraph_node_for_asm (target);
5371 if (fnode == NULL)
5372 vnode = varpool_node_for_asm (target);
5374 else
5376 vnode = varpool_node_for_asm (target);
5377 if (vnode == NULL)
5378 fnode = cgraph_node_for_asm (target);
5381 if (fnode)
5383 cgraph_mark_needed_node (fnode);
5384 return fnode->decl;
5386 else if (vnode)
5388 varpool_mark_needed_node (vnode);
5389 vnode->force_output = 1;
5390 return vnode->decl;
5392 else
5393 return NULL_TREE;
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. */
5400 static void
5401 do_assemble_alias (tree decl, tree target)
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)));
5408 if (TREE_ASM_WRITTEN (decl))
5409 return;
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);
5415 TREE_ASM_WRITTEN (decl) = 1;
5416 TREE_ASM_WRITTEN (DECL_ASSEMBLER_NAME (decl)) = 1;
5418 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
5420 ultimate_transparent_alias_target (&target);
5422 if (!TREE_SYMBOL_REFERENCED (target))
5423 weakref_targets = tree_cons (decl, target, weakref_targets);
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)
5432 error_at (DECL_SOURCE_LOCATION (decl),
5433 "weakref is not supported in this configuration");
5434 return;
5436 #endif
5437 return;
5440 #ifdef ASM_OUTPUT_DEF
5441 /* Make name accessible from other files, if appropriate. */
5443 if (TREE_PUBLIC (decl))
5445 globalize_decl (decl);
5446 maybe_assemble_visibility (decl);
5448 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
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
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)
5469 const char *name;
5470 tree *p, t;
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);
5486 /* Remove weakrefs to the same target from the pending weakref
5487 list, for the same reason. */
5488 for (p = &weakref_targets; (t = *p) ; )
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);
5497 #endif
5501 /* Allocate and construct a symbol alias set. */
5503 static symbol_alias_set_t *
5504 symbol_alias_set_create (void)
5506 return pointer_set_create ();
5509 /* Destruct and free a symbol alias set. */
5511 void
5512 symbol_alias_set_destroy (symbol_alias_set_t *aset)
5514 pointer_set_destroy (aset);
5517 /* Test if a symbol alias set contains a given name. */
5520 symbol_alias_set_contains (const symbol_alias_set_t *aset, tree t)
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);
5529 /* Enter a new name into a symbol alias set. */
5531 static int
5532 symbol_alias_set_insert (symbol_alias_set_t *aset, tree t)
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);
5541 /* IN_SET_P is a predicate function assuming to be taken
5542 alias_pair->decl, alias_pair->target and DATA arguments.
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. */
5548 static symbol_alias_set_t *
5549 propagate_aliases_forward (bool (*in_set_p)
5550 (tree decl, tree target, void *data),
5551 void *data)
5553 symbol_alias_set_t *set;
5554 unsigned i;
5555 alias_pair *p;
5556 bool changed;
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);
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;
5570 while (changed);
5572 return set;
5575 /* Like propagate_aliases_forward but do backward propagation. */
5577 symbol_alias_set_t *
5578 propagate_aliases_backward (bool (*in_set_p)
5579 (tree decl, tree target, void *data),
5580 void *data)
5582 symbol_alias_set_t *set;
5583 unsigned i;
5584 alias_pair *p;
5585 bool changed;
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);
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;
5601 while (changed);
5603 return set;
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. */
5612 static bool
5613 trivially_visible_alias (tree decl, tree target ATTRIBUTE_UNUSED,
5614 void *data ATTRIBUTE_UNUSED)
5616 struct cgraph_node *fnode = NULL;
5617 struct varpool_node *vnode = NULL;
5619 if (!TREE_PUBLIC (decl))
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;
5627 else
5628 return true;
5631 /* See if the target of alias is defined in this unit.
5632 Used as callback for propagate_aliases. */
5634 static bool
5635 trivially_defined_alias (tree decl ATTRIBUTE_UNUSED,
5636 tree target,
5637 void *data ATTRIBUTE_UNUSED)
5639 struct cgraph_node *fnode = NULL;
5640 struct varpool_node *vnode = NULL;
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);
5647 /* Remove the alias pairing for functions that are no longer in the call
5648 graph. */
5650 void
5651 remove_unreachable_alias_pairs (void)
5653 symbol_alias_set_t *visible;
5654 unsigned i;
5655 alias_pair *p;
5657 if (alias_pairs == NULL)
5658 return;
5660 /* We have to compute the set of visible nodes including aliases
5661 themselves. */
5662 visible = propagate_aliases_forward (trivially_visible_alias, NULL);
5664 for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); )
5666 if (!DECL_EXTERNAL (p->decl)
5667 && !symbol_alias_set_contains (visible, p->decl))
5669 VEC_unordered_remove (alias_pair, alias_pairs, i);
5670 continue;
5673 i++;
5676 symbol_alias_set_destroy (visible);
5680 /* First pass of completing pending aliases. Make sure that cgraph knows
5681 which symbols will be required. */
5683 void
5684 finish_aliases_1 (void)
5686 symbol_alias_set_t *defined;
5687 unsigned i;
5688 alias_pair *p;
5690 if (alias_pairs == NULL)
5691 return;
5693 /* We have to compute the set of defined nodes including aliases
5694 themselves. */
5695 defined = propagate_aliases_backward (trivially_defined_alias, NULL);
5697 FOR_EACH_VEC_ELT (alias_pair, alias_pairs, i, p)
5699 tree target_decl;
5701 target_decl = find_decl_and_mark_needed (p->decl, p->target);
5702 if (target_decl == NULL)
5704 if (symbol_alias_set_contains (defined, p->target))
5705 continue;
5707 if (! (p->emitted_diags & ALIAS_DIAG_TO_UNDEF)
5708 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
5710 error ("%q+D aliased to undefined symbol %qE",
5711 p->decl, p->target);
5712 p->emitted_diags |= ALIAS_DIAG_TO_UNDEF;
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)))
5724 error ("%q+D aliased to external symbol %qE",
5725 p->decl, p->target);
5726 p->emitted_diags |= ALIAS_DIAG_TO_EXTERN;
5730 symbol_alias_set_destroy (defined);
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. */
5737 void
5738 finish_aliases_2 (void)
5740 unsigned i;
5741 alias_pair *p;
5743 FOR_EACH_VEC_ELT (alias_pair, alias_pairs, i, p)
5744 do_assemble_alias (p->decl, p->target);
5746 VEC_truncate (alias_pair, alias_pairs, 0);
5749 /* Emit an assembler directive to make the symbol for DECL an alias to
5750 the symbol for TARGET. */
5752 void
5753 assemble_alias (tree decl, tree target)
5755 tree target_decl;
5756 bool is_weakref = false;
5758 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
5760 tree alias = DECL_ASSEMBLER_NAME (decl);
5762 is_weakref = true;
5764 ultimate_transparent_alias_target (&target);
5766 if (alias == target)
5767 error ("weakref %q+D ultimately targets itself", decl);
5768 else
5770 #ifndef ASM_OUTPUT_WEAKREF
5771 IDENTIFIER_TRANSPARENT_ALIAS (alias) = 1;
5772 TREE_CHAIN (alias) = target;
5773 #endif
5775 if (TREE_PUBLIC (decl))
5776 error ("weakref %q+D must have static linkage", decl);
5778 else
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))
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;
5796 # endif
5797 #endif
5799 TREE_USED (decl) = 1;
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;
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;
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
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;
5830 /* Emit an assembler directive to set symbol for DECL visibility to
5831 the visibility type VIS, which must not be VISIBILITY_DEFAULT. */
5833 void
5834 default_assemble_visibility (tree decl ATTRIBUTE_UNUSED,
5835 int vis ATTRIBUTE_UNUSED)
5837 #ifdef HAVE_GAS_HIDDEN
5838 static const char * const visibility_types[] = {
5839 NULL, "protected", "hidden", "internal"
5842 const char *name, *type;
5844 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5845 type = visibility_types[vis];
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
5856 /* A helper function to call assemble_visibility when needed for a decl. */
5859 maybe_assemble_visibility (tree decl)
5861 enum symbol_visibility vis = DECL_VISIBILITY (decl);
5863 if (vis != VISIBILITY_DEFAULT)
5865 targetm.asm_out.assemble_visibility (decl, vis);
5866 return 1;
5868 else
5869 return 0;
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. */
5878 supports_one_only (void)
5880 if (SUPPORTS_ONE_ONLY)
5881 return 1;
5882 return TARGET_SUPPORTS_WEAK;
5885 /* Set up DECL as a public symbol that can be defined in multiple
5886 translation units without generating a linker error. */
5888 void
5889 make_decl_one_only (tree decl, tree comdat_group)
5891 gcc_assert (TREE_CODE (decl) == VAR_DECL
5892 || TREE_CODE (decl) == FUNCTION_DECL);
5894 TREE_PUBLIC (decl) = 1;
5896 if (SUPPORTS_ONE_ONLY)
5898 #ifdef MAKE_DECL_ONE_ONLY
5899 MAKE_DECL_ONE_ONLY (decl);
5900 #endif
5901 DECL_COMDAT_GROUP (decl) = comdat_group;
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
5908 gcc_assert (TARGET_SUPPORTS_WEAK);
5909 DECL_WEAK (decl) = 1;
5913 void
5914 init_varasm_once (void)
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);
5923 const_alias_set = new_alias_set ();
5924 shared_constant_pool = create_constant_pool ();
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
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
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
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
5946 #ifdef CTORS_SECTION_ASM_OP
5947 ctors_section = get_unnamed_section (0, output_section_asm_op,
5948 CTORS_SECTION_ASM_OP);
5949 #endif
5951 #ifdef DTORS_SECTION_ASM_OP
5952 dtors_section = get_unnamed_section (0, output_section_asm_op,
5953 DTORS_SECTION_ASM_OP);
5954 #endif
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
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
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);
5975 #if defined ASM_OUTPUT_ALIGNED_BSS
5976 bss_noswitch_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS,
5977 emit_bss);
5978 #endif
5980 targetm.asm_out.init_sections ();
5982 if (readonly_data_section == NULL)
5983 readonly_data_section = text_section;
5986 enum tls_model
5987 decl_default_tls_model (const_tree decl)
5989 enum tls_model kind;
5990 bool is_local;
5992 is_local = targetm.binds_local_p (decl);
5993 if (!flag_shlib)
5995 if (is_local)
5996 kind = TLS_MODEL_LOCAL_EXEC;
5997 else
5998 kind = TLS_MODEL_INITIAL_EXEC;
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;
6010 return kind;
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.
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. */
6020 unsigned int
6021 default_section_type_flags (tree decl, const char *name, int reloc)
6023 unsigned int flags;
6025 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
6026 flags = SECTION_CODE;
6027 else if (decl)
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;
6039 else
6041 flags = SECTION_WRITE;
6042 if (strcmp (name, ".data.rel.ro") == 0
6043 || strcmp (name, ".data.rel.ro.local") == 0)
6044 flags |= SECTION_RELRO;
6047 if (decl && DECL_ONE_ONLY (decl))
6048 flags |= SECTION_LINKONCE;
6050 if (decl && TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
6051 flags |= SECTION_TLS | SECTION_WRITE;
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;
6061 if (strcmp (name, ".tdata") == 0
6062 || strncmp (name, ".tdata.", 7) == 0
6063 || strncmp (name, ".gnu.linkonce.td.", 17) == 0)
6064 flags |= SECTION_TLS;
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;
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;
6082 return flags;
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. */
6089 bool
6090 have_global_bss_p (void)
6092 return bss_noswitch_section || targetm.have_switchable_bss_sections;
6095 /* Output assembly to switch to section NAME with attribute FLAGS.
6096 Four variants for common object file formats. */
6098 void
6099 default_no_named_section (const char *name ATTRIBUTE_UNUSED,
6100 unsigned int flags ATTRIBUTE_UNUSED,
6101 tree decl ATTRIBUTE_UNUSED)
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 ();
6108 #ifndef TLS_SECTION_ASM_FLAG
6109 #define TLS_SECTION_ASM_FLAG 'T'
6110 #endif
6112 void
6113 default_elf_asm_named_section (const char *name, unsigned int flags,
6114 tree decl ATTRIBUTE_UNUSED)
6116 char flagchars[10], *f = flagchars;
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))
6125 fprintf (asm_out_file, "\t.section\t%s\n", name);
6126 return;
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';
6147 fprintf (asm_out_file, "\t.section\t%s,\"%s\"", name, flagchars);
6149 if (!(flags & SECTION_NOTYPE))
6151 const char *type;
6152 const char *format;
6154 if (flags & SECTION_BSS)
6155 type = "nobits";
6156 else
6157 type = "progbits";
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);
6168 if (flags & SECTION_ENTSIZE)
6169 fprintf (asm_out_file, ",%d", flags & SECTION_ENTSIZE);
6170 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
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)));
6180 putc ('\n', asm_out_file);
6183 void
6184 default_coff_asm_named_section (const char *name, unsigned int flags,
6185 tree decl ATTRIBUTE_UNUSED)
6187 char flagchars[8], *f = flagchars;
6189 if (flags & SECTION_WRITE)
6190 *f++ = 'w';
6191 if (flags & SECTION_CODE)
6192 *f++ = 'x';
6193 *f = '\0';
6195 fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
6198 void
6199 default_pe_asm_named_section (const char *name, unsigned int flags,
6200 tree decl)
6202 default_coff_asm_named_section (name, flags, decl);
6204 if (flags & SECTION_LINKONCE)
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"));
6214 /* The lame default section selector. */
6216 section *
6217 default_select_section (tree decl, int reloc,
6218 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
6220 if (DECL_P (decl))
6222 if (decl_readonly_section (decl, reloc))
6223 return readonly_data_section;
6225 else if (TREE_CODE (decl) == CONSTRUCTOR)
6227 if (! ((flag_pic && reloc)
6228 || !TREE_READONLY (decl)
6229 || TREE_SIDE_EFFECTS (decl)
6230 || !TREE_CONSTANT (decl)))
6231 return readonly_data_section;
6233 else if (TREE_CODE (decl) == STRING_CST)
6234 return readonly_data_section;
6235 else if (! (flag_pic && reloc))
6236 return readonly_data_section;
6238 return data_section;
6241 enum section_category
6242 categorize_decl_for_section (const_tree decl, int reloc)
6244 enum section_category ret;
6246 if (TREE_CODE (decl) == FUNCTION_DECL)
6247 return SECCAT_TEXT;
6248 else if (TREE_CODE (decl) == STRING_CST)
6250 if (flag_mudflap) /* or !flag_merge_constants */
6251 return SECCAT_RODATA;
6252 else
6253 return SECCAT_RODATA_MERGE_STR;
6255 else if (TREE_CODE (decl) == VAR_DECL)
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)))
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;
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;
6284 else if (TREE_CODE (decl) == CONSTRUCTOR)
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;
6293 else
6294 ret = SECCAT_RODATA;
6296 /* There are no read-only thread-local sections. */
6297 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
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;
6309 /* If the target uses small data sections, select it. */
6310 else if (targetm.in_small_data_p (decl))
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;
6320 return ret;
6323 static bool
6324 decl_readonly_section_1 (enum section_category category)
6326 switch (category)
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;
6339 bool
6340 decl_readonly_section (const_tree decl, int reloc)
6342 return decl_readonly_section_1 (categorize_decl_for_section (decl, reloc));
6345 /* Select a section based on the above categorization. */
6347 section *
6348 default_elf_select_section (tree decl, int reloc,
6349 unsigned HOST_WIDE_INT align)
6351 const char *sname;
6352 switch (categorize_decl_for_section (decl, reloc))
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 ();
6403 if (!DECL_P (decl))
6404 decl = NULL_TREE;
6405 return get_named_section (decl, sname, reloc);
6408 /* Construct a unique section name based on the decl name and the
6409 categorization performed above. */
6411 void
6412 default_unique_section (tree decl, int reloc)
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;
6419 switch (categorize_decl_for_section (decl, reloc))
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 ();
6467 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
6468 name = targetm.strip_name_encoding (name);
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" : "";
6474 string = ACONCAT ((linkonce, prefix, ".", name, NULL));
6476 DECL_SECTION_NAME (decl) = build_string (strlen (string), string);
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. */
6483 static int
6484 compute_reloc_for_rtx_1 (rtx *xp, void *data)
6486 int *preloc = (int *) data;
6487 rtx x = *xp;
6489 switch (GET_CODE (x))
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;
6501 return 0;
6504 static int
6505 compute_reloc_for_rtx (rtx x)
6507 int reloc;
6509 switch (GET_CODE (x))
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;
6518 default:
6519 return 0;
6523 section *
6524 default_select_rtx_section (enum machine_mode mode ATTRIBUTE_UNUSED,
6525 rtx x,
6526 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
6528 if (compute_reloc_for_rtx (x) & targetm.asm_out.reloc_rw_mask ())
6529 return data_section;
6530 else
6531 return readonly_data_section;
6534 section *
6535 default_elf_select_rtx_section (enum machine_mode mode, rtx x,
6536 unsigned HOST_WIDE_INT align)
6538 int reloc = compute_reloc_for_rtx (x);
6540 /* ??? Handle small data here somehow. */
6542 if (reloc & targetm.asm_out.reloc_rw_mask ())
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);
6550 return mergeable_constant_section (mode, align, 0);
6553 /* Set the generally applicable flags on the SYMBOL_REF for EXP. */
6555 void
6556 default_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
6558 rtx symbol;
6559 int flags;
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;
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;
6583 SYMBOL_REF_FLAGS (symbol) = flags;
6586 /* By default, we do nothing for encode_section_info, so we need not
6587 do anything but discard the '*' marker. */
6589 const char *
6590 default_strip_name_encoding (const char *str)
6592 return str + (*str == '*');
6595 #ifdef ASM_OUTPUT_DEF
6596 /* The default implementation of TARGET_ASM_OUTPUT_ANCHOR. Define the
6597 anchor relative to ".", the current section position. */
6599 void
6600 default_asm_output_anchor (rtx symbol)
6602 char buffer[100];
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);
6608 #endif
6610 /* The default implementation of TARGET_USE_ANCHORS_FOR_SYMBOL_P. */
6612 bool
6613 default_use_anchors_for_symbol_p (const_rtx symbol)
6615 section *sect;
6616 tree decl;
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;
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;
6629 decl = SYMBOL_REF_DECL (symbol);
6630 if (decl && DECL_P (decl))
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;
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;
6645 return true;
6648 /* Return true when RESOLUTION indicate that symbol will be bound to the
6649 definition provided by current .o file. */
6651 static bool
6652 resolution_to_local_definition_p (enum ld_plugin_symbol_resolution resolution)
6654 return (resolution == LDPR_PREVAILING_DEF
6655 || resolution == LDPR_PREVAILING_DEF_IRONLY);
6658 /* Return true when RESOLUTION indicate that symbol will be bound locally
6659 within current executable or DSO. */
6661 static bool
6662 resolution_local_p (enum ld_plugin_symbol_resolution resolution)
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);
6672 /* Assume ELF-ish defaults, since that's pretty much the most liberal
6673 wrt cross-module name binding. */
6675 bool
6676 default_binds_local_p (const_tree exp)
6678 return default_binds_local_p_1 (exp, flag_shlib);
6681 bool
6682 default_binds_local_p_1 (const_tree exp, int shlib)
6684 bool local_p;
6685 bool resolved_locally = false;
6686 bool resolved_to_local_def = false;
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)))
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;
6702 else if (TREE_CODE (exp) == FUNCTION_DECL && TREE_PUBLIC (exp))
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;
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;
6759 return local_p;
6762 /* Return true when references to DECL must bind to current definition in
6763 final executable.
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)
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)))
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);
6790 else if (TREE_CODE (decl) == FUNCTION_DECL)
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);
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);
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.
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. */
6814 bool
6815 decl_replaceable_p (tree decl)
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);
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)
6830 fputs (GLOBAL_ASM_OP, stream);
6831 assemble_name (stream, name);
6832 putc ('\n', stream);
6834 #endif /* GLOBAL_ASM_OP */
6836 /* Default function to output code that will globalize a declaration. */
6837 void
6838 default_globalize_decl_name (FILE * stream, tree decl)
6840 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
6841 targetm.asm_out.globalize_label (stream, name);
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)
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)
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. */
6866 void
6867 default_generate_internal_label (char *buf, const char *prefix,
6868 unsigned long labelno)
6870 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
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. */
6876 void
6877 default_internal_label (FILE *stream, const char *prefix,
6878 unsigned long labelno)
6880 char *const buf = (char *) alloca (40 + strlen (prefix));
6881 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
6882 ASM_OUTPUT_INTERNAL_LABEL (stream, buf);
6886 /* The default implementation of ASM_DECLARE_CONSTANT_NAME. */
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)
6893 assemble_label (file, name);
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)
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);
6905 if (targetm.asm_file_start_file_directive)
6906 output_file_directive (asm_out_file, main_input_filename);
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. */
6914 int trampolines_created;
6916 void
6917 file_end_indicate_exec_stack (void)
6919 unsigned int flags = SECTION_DEBUG;
6920 if (trampolines_created)
6921 flags |= SECTION_CODE;
6923 switch_to_section (get_section (".note.GNU-stack", flags, NULL));
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. */
6935 void
6936 file_end_indicate_split_stack (void)
6938 if (flag_split_stack)
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));
6948 /* Output DIRECTIVE (a C string) followed by a newline. This is used as
6949 a get_unnamed_section callback. */
6951 void
6952 output_section_asm_op (const void *directive)
6954 fprintf (asm_out_file, "%s\n", (const char *) directive);
6957 /* Emit assembly code to switch to section NEW_SECTION. Do nothing if
6958 the current section is NEW_SECTION. */
6960 void
6961 switch_to_section (section *new_section)
6963 if (in_section == new_section)
6964 return;
6966 if (new_section->common.flags & SECTION_FORGET)
6967 in_section = NULL;
6968 else
6969 in_section = new_section;
6971 switch (SECTION_STYLE (new_section))
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;
6979 case SECTION_UNNAMED:
6980 new_section->unnamed.callback (new_section->unnamed.data);
6981 break;
6983 case SECTION_NOSWITCH:
6984 gcc_unreachable ();
6985 break;
6988 new_section->common.flags |= SECTION_DECLARED;
6991 /* If block symbol SYMBOL has not yet been assigned an offset, place
6992 it at the end of its block. */
6994 void
6995 place_block_symbol (rtx symbol)
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;
7003 gcc_assert (SYMBOL_REF_BLOCK (symbol));
7004 if (SYMBOL_REF_BLOCK_OFFSET (symbol) >= 0)
7005 return;
7007 /* Work out the symbol's size and alignment. */
7008 if (CONSTANT_POOL_ADDRESS_P (symbol))
7010 desc = SYMBOL_REF_CONSTANT (symbol);
7011 alignment = desc->align;
7012 size = GET_MODE_SIZE (desc->mode);
7014 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
7016 decl = SYMBOL_REF_DECL (symbol);
7017 alignment = DECL_ALIGN (decl);
7018 size = get_constant_size (DECL_INITIAL (decl));
7020 else
7022 decl = SYMBOL_REF_DECL (symbol);
7023 alignment = DECL_ALIGN (decl);
7024 size = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
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;
7033 /* Record the block's new alignment and size. */
7034 block->alignment = MAX (block->alignment, alignment);
7035 block->size = offset + size;
7037 VEC_safe_push (rtx, gc, block->objects, symbol);
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. */
7045 get_section_anchor (struct object_block *block, HOST_WIDE_INT offset,
7046 enum tls_model model)
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;
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.
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.
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
7074 bias = 1 << (GET_MODE_BITSIZE (ptr_mode) - 1);
7075 if (offset < 0)
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);
7083 else
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;
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)
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;
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;
7119 /* Insert it at index BEGIN. */
7120 VEC_safe_insert (rtx, gc, block->anchors, begin, anchor);
7121 return anchor;
7124 /* Output the objects in BLOCK. */
7126 static void
7127 output_object_block (struct object_block *block)
7129 struct constant_descriptor_rtx *desc;
7130 unsigned int i;
7131 HOST_WIDE_INT offset;
7132 tree decl;
7133 rtx symbol;
7135 if (block->objects == NULL)
7136 return;
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);
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);
7148 /* Output the objects themselves. */
7149 offset = 0;
7150 FOR_EACH_VEC_ELT (rtx, block->objects, i, symbol)
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))
7157 desc = SYMBOL_REF_CONSTANT (symbol);
7158 output_constant_pool_1 (desc, 1);
7159 offset += GET_MODE_SIZE (desc->mode);
7161 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
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));
7168 else
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);
7177 /* A htab_traverse callback used to call output_object_block for
7178 each member of object_block_htab. */
7180 static int
7181 output_object_block_htab (void **slot, void *data ATTRIBUTE_UNUSED)
7183 output_object_block ((struct object_block *) (*slot));
7184 return 1;
7187 /* Output the definitions of all object_blocks. */
7189 void
7190 output_object_blocks (void)
7192 htab_traverse (object_block_htab, output_object_block_htab, NULL);
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.
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. */
7209 elf_record_gcc_switches (print_switch_type type, const char * name)
7211 switch (type)
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;
7218 case SWITCH_TYPE_DESCRIPTIVE:
7219 if (name == NULL)
7221 /* Distinguish between invocations where name is NULL. */
7222 static bool started = false;
7224 if (!started)
7226 section * sec;
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;
7239 default:
7240 break;
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;
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)
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);
7264 /* The default hook for TARGET_ASM_OUTPUT_SOURCE_FILENAME. */
7266 void
7267 default_asm_output_source_filename (FILE *file, const char *name)
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
7278 /* Output a file name in the form wanted by System V. */
7280 void
7281 output_file_directive (FILE *asm_file, const char *input_name)
7283 int len;
7284 const char *na;
7286 if (input_name == NULL)
7287 input_name = "<stdin>";
7288 else
7289 input_name = remap_debug_filename (input_name);
7291 len = strlen (input_name);
7292 na = input_name + len;
7294 /* NA gets INPUT_NAME sans directory names. */
7295 while (na > input_name)
7297 if (IS_DIR_SEPARATOR (na[-1]))
7298 break;
7299 na--;
7302 targetm.asm_out.output_source_filename (asm_file, na);
7305 /* Create a DEBUG_EXPR_DECL / DEBUG_EXPR pair from RTL expression
7306 EXP. */
7308 make_debug_expr_from_rtl (const_rtx exp)
7310 tree ddecl = make_node (DEBUG_EXPR_DECL), type;
7311 enum machine_mode mode = GET_MODE (exp);
7312 rtx dval;
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;
7332 #include "gt-varasm.h"