./:
[official-gcc.git] / gcc / varasm.c
blobc85cf9ddf2791d52622797edc6eaae4bab2f8cce
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 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 "real.h"
42 #include "output.h"
43 #include "toplev.h"
44 #include "hashtab.h"
45 #include "ggc.h"
46 #include "langhooks.h"
47 #include "tm_p.h"
48 #include "debug.h"
49 #include "target.h"
50 #include "targhooks.h"
51 #include "tree-mudflap.h"
52 #include "cgraph.h"
53 #include "cfglayout.h"
54 #include "basic-block.h"
55 #include "tree-iterator.h"
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 static const char *strip_reg_name (const char *);
103 static int contains_pointers_p (tree);
104 #ifdef ASM_OUTPUT_EXTERNAL
105 static bool incorporeal_function_p (tree);
106 #endif
107 static void decode_addr_const (tree, struct addr_const *);
108 static hashval_t const_desc_hash (const void *);
109 static int const_desc_eq (const void *, const void *);
110 static hashval_t const_hash_1 (const tree);
111 static int compare_constant (const tree, const tree);
112 static tree copy_constant (tree);
113 static void output_constant_def_contents (rtx);
114 static void output_addressed_constants (tree);
115 static unsigned HOST_WIDE_INT array_size_for_constructor (tree);
116 static unsigned min_align (unsigned, unsigned);
117 static void output_constructor (tree, unsigned HOST_WIDE_INT, unsigned int);
118 static void globalize_decl (tree);
119 #ifdef BSS_SECTION_ASM_OP
120 #ifdef ASM_OUTPUT_BSS
121 static void asm_output_bss (FILE *, tree, const char *,
122 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
123 #endif
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 #ifndef IN_NAMED_SECTION
175 #define IN_NAMED_SECTION(DECL) \
176 ((TREE_CODE (DECL) == FUNCTION_DECL || TREE_CODE (DECL) == VAR_DECL) \
177 && DECL_SECTION_NAME (DECL) != NULL_TREE)
178 #endif
180 /* Hash table of named sections. */
181 static GTY((param_is (section))) htab_t section_htab;
183 /* A table of object_blocks, indexed by section. */
184 static GTY((param_is (struct object_block))) htab_t object_block_htab;
186 /* The next number to use for internal anchor labels. */
187 static GTY(()) int anchor_labelno;
189 /* A pool of constants that can be shared between functions. */
190 static GTY(()) struct rtx_constant_pool *shared_constant_pool;
192 /* TLS emulation. */
194 static GTY ((if_marked ("tree_map_marked_p"), param_is (struct tree_map)))
195 htab_t emutls_htab;
196 static GTY (()) tree emutls_object_type;
197 /* Emulated TLS objects have the TLS model TLS_MODEL_EMULATED. This
198 macro can be used on them to distinguish the control variable from
199 the initialization template. */
200 #define DECL_EMUTLS_VAR_P(D) (TREE_TYPE (D) == emutls_object_type)
202 #if !defined (NO_DOT_IN_LABEL)
203 # define EMUTLS_SEPARATOR "."
204 #elif !defined (NO_DOLLAR_IN_LABEL)
205 # define EMUTLS_SEPARATOR "$"
206 #else
207 # define EMUTLS_SEPARATOR "_"
208 #endif
210 /* Create an IDENTIFIER_NODE by prefixing PREFIX to the
211 IDENTIFIER_NODE NAME's name. */
213 static tree
214 prefix_name (const char *prefix, tree name)
216 unsigned plen = strlen (prefix);
217 unsigned nlen = strlen (IDENTIFIER_POINTER (name));
218 char *toname = (char *) alloca (plen + nlen + 1);
220 memcpy (toname, prefix, plen);
221 memcpy (toname + plen, IDENTIFIER_POINTER (name), nlen + 1);
223 return get_identifier (toname);
226 /* Create an identifier for the struct __emutls_object, given an identifier
227 of the DECL_ASSEMBLY_NAME of the original object. */
229 static tree
230 get_emutls_object_name (tree name)
232 const char *prefix = (targetm.emutls.var_prefix
233 ? targetm.emutls.var_prefix
234 : "__emutls_v" EMUTLS_SEPARATOR);
235 return prefix_name (prefix, name);
238 tree
239 default_emutls_var_fields (tree type, tree *name ATTRIBUTE_UNUSED)
241 tree word_type_node, field, next_field;
243 field = build_decl (FIELD_DECL, get_identifier ("__templ"), ptr_type_node);
244 DECL_CONTEXT (field) = type;
245 next_field = field;
247 field = build_decl (FIELD_DECL, get_identifier ("__offset"),
248 ptr_type_node);
249 DECL_CONTEXT (field) = type;
250 TREE_CHAIN (field) = next_field;
251 next_field = field;
253 word_type_node = lang_hooks.types.type_for_mode (word_mode, 1);
254 field = build_decl (FIELD_DECL, get_identifier ("__align"),
255 word_type_node);
256 DECL_CONTEXT (field) = type;
257 TREE_CHAIN (field) = next_field;
258 next_field = field;
260 field = build_decl (FIELD_DECL, get_identifier ("__size"), word_type_node);
261 DECL_CONTEXT (field) = type;
262 TREE_CHAIN (field) = next_field;
264 return field;
267 /* Create the structure for struct __emutls_object. This should match the
268 structure at the top of emutls.c, modulo the union there. */
270 static tree
271 get_emutls_object_type (void)
273 tree type, type_name, field;
275 type = emutls_object_type;
276 if (type)
277 return type;
279 emutls_object_type = type = lang_hooks.types.make_type (RECORD_TYPE);
280 type_name = NULL;
281 field = targetm.emutls.var_fields (type, &type_name);
282 if (!type_name)
283 type_name = get_identifier ("__emutls_object");
284 type_name = build_decl (TYPE_DECL, type_name, type);
285 TYPE_NAME (type) = type_name;
286 TYPE_FIELDS (type) = field;
287 layout_type (type);
289 return type;
292 /* Create a read-only variable like DECL, with the same DECL_INITIAL.
293 This will be used for initializing the emulated tls data area. */
295 static tree
296 get_emutls_init_templ_addr (tree decl)
298 tree name, to;
300 if (targetm.emutls.register_common && !DECL_INITIAL (decl)
301 && !DECL_SECTION_NAME (decl))
302 return null_pointer_node;
304 name = DECL_ASSEMBLER_NAME (decl);
305 if (!targetm.emutls.tmpl_prefix || targetm.emutls.tmpl_prefix[0])
307 const char *prefix = (targetm.emutls.tmpl_prefix
308 ? targetm.emutls.tmpl_prefix
309 : "__emutls_t" EMUTLS_SEPARATOR);
310 name = prefix_name (prefix, name);
313 to = build_decl (VAR_DECL, name, TREE_TYPE (decl));
314 SET_DECL_ASSEMBLER_NAME (to, DECL_NAME (to));
315 DECL_TLS_MODEL (to) = TLS_MODEL_EMULATED;
316 DECL_ARTIFICIAL (to) = 1;
317 TREE_USED (to) = TREE_USED (decl);
318 TREE_READONLY (to) = 1;
319 DECL_IGNORED_P (to) = 1;
320 DECL_CONTEXT (to) = DECL_CONTEXT (decl);
321 DECL_SECTION_NAME (to) = DECL_SECTION_NAME (decl);
323 DECL_WEAK (to) = DECL_WEAK (decl);
324 if (DECL_ONE_ONLY (decl))
326 make_decl_one_only (to);
327 TREE_STATIC (to) = TREE_STATIC (decl);
328 TREE_PUBLIC (to) = TREE_PUBLIC (decl);
329 DECL_VISIBILITY (to) = DECL_VISIBILITY (decl);
331 else
332 TREE_STATIC (to) = 1;
334 DECL_INITIAL (to) = DECL_INITIAL (decl);
335 DECL_INITIAL (decl) = NULL;
337 varpool_finalize_decl (to);
338 return build_fold_addr_expr (to);
341 /* When emulating tls, we use a control structure for use by the runtime.
342 Create and return this structure. */
344 tree
345 emutls_decl (tree decl)
347 tree name, to;
348 struct tree_map *h, in;
349 void **loc;
351 if (targetm.have_tls || decl == NULL || decl == error_mark_node
352 || TREE_CODE (decl) != VAR_DECL || ! DECL_THREAD_LOCAL_P (decl))
353 return decl;
355 /* Look up the object in the hash; return the control structure if
356 it has already been created. */
357 if (! emutls_htab)
358 emutls_htab = htab_create_ggc (512, tree_map_hash, tree_map_eq, 0);
360 name = DECL_ASSEMBLER_NAME (decl);
362 /* Note that we use the hash of the decl's name, rather than a hash
363 of the decl's pointer. In emutls_finish we iterate through the
364 hash table, and we want this traversal to be predictable. */
365 in.hash = htab_hash_string (IDENTIFIER_POINTER (name));
366 in.base.from = decl;
367 loc = htab_find_slot_with_hash (emutls_htab, &in, in.hash, INSERT);
368 h = (struct tree_map *) *loc;
369 if (h != NULL)
370 to = h->to;
371 else
373 to = build_decl (VAR_DECL, get_emutls_object_name (name),
374 get_emutls_object_type ());
376 h = GGC_NEW (struct tree_map);
377 h->hash = in.hash;
378 h->base.from = decl;
379 h->to = to;
380 *(struct tree_map **) loc = h;
382 DECL_TLS_MODEL (to) = TLS_MODEL_EMULATED;
383 DECL_ARTIFICIAL (to) = 1;
384 DECL_IGNORED_P (to) = 1;
385 TREE_READONLY (to) = 0;
386 SET_DECL_ASSEMBLER_NAME (to, DECL_NAME (to));
387 if (DECL_ONE_ONLY (decl))
388 make_decl_one_only (to);
389 DECL_CONTEXT (to) = DECL_CONTEXT (decl);
390 if (targetm.emutls.var_align_fixed)
391 /* If we're not allowed to change the proxy object's
392 alignment, pretend it's been set by the user. */
393 DECL_USER_ALIGN (to) = 1;
396 /* Note that these fields may need to be updated from time to time from
397 the original decl. Consider:
398 extern __thread int i;
399 int foo() { return i; }
400 __thread int i = 1;
401 in which I goes from external to locally defined and initialized. */
403 TREE_STATIC (to) = TREE_STATIC (decl);
404 TREE_USED (to) = TREE_USED (decl);
405 TREE_PUBLIC (to) = TREE_PUBLIC (decl);
406 DECL_EXTERNAL (to) = DECL_EXTERNAL (decl);
407 DECL_COMMON (to) = DECL_COMMON (decl);
408 DECL_WEAK (to) = DECL_WEAK (decl);
409 DECL_VISIBILITY (to) = DECL_VISIBILITY (decl);
411 return to;
414 static int
415 emutls_common_1 (void **loc, void *xstmts)
417 struct tree_map *h = *(struct tree_map **) loc;
418 tree args, x, *pstmts = (tree *) xstmts;
419 tree word_type_node;
421 if (! DECL_COMMON (h->base.from)
422 || (DECL_INITIAL (h->base.from)
423 && DECL_INITIAL (h->base.from) != error_mark_node))
424 return 1;
426 word_type_node = lang_hooks.types.type_for_mode (word_mode, 1);
428 /* The idea was to call get_emutls_init_templ_addr here, but if we
429 do this and there is an initializer, -fanchor_section loses,
430 because it would be too late to ensure the template is
431 output. */
432 x = null_pointer_node;
433 args = tree_cons (NULL, x, NULL);
434 x = build_int_cst (word_type_node, DECL_ALIGN_UNIT (h->base.from));
435 args = tree_cons (NULL, x, args);
436 x = fold_convert (word_type_node, DECL_SIZE_UNIT (h->base.from));
437 args = tree_cons (NULL, x, args);
438 x = build_fold_addr_expr (h->to);
439 args = tree_cons (NULL, x, args);
441 x = built_in_decls[BUILT_IN_EMUTLS_REGISTER_COMMON];
442 x = build_function_call_expr (x, args);
444 append_to_statement_list (x, pstmts);
445 return 1;
448 void
449 emutls_finish (void)
451 if (targetm.emutls.register_common)
453 tree body = NULL_TREE;
455 if (emutls_htab == NULL)
456 return;
458 htab_traverse_noresize (emutls_htab, emutls_common_1, &body);
459 if (body == NULL_TREE)
460 return;
462 cgraph_build_static_cdtor ('I', body, DEFAULT_INIT_PRIORITY);
466 /* Helper routines for maintaining section_htab. */
468 static int
469 section_entry_eq (const void *p1, const void *p2)
471 const section *old = (const section *) p1;
472 const char *new_name = (const char *) p2;
474 return strcmp (old->named.name, new_name) == 0;
477 static hashval_t
478 section_entry_hash (const void *p)
480 const section *old = (const section *) p;
481 return htab_hash_string (old->named.name);
484 /* Return a hash value for section SECT. */
486 static hashval_t
487 hash_section (section *sect)
489 if (sect->common.flags & SECTION_NAMED)
490 return htab_hash_string (sect->named.name);
491 return sect->common.flags;
494 /* Helper routines for maintaining object_block_htab. */
496 static int
497 object_block_entry_eq (const void *p1, const void *p2)
499 const struct object_block *old = (const struct object_block *) p1;
500 const section *new_section = (const section *) p2;
502 return old->sect == new_section;
505 static hashval_t
506 object_block_entry_hash (const void *p)
508 const struct object_block *old = (const struct object_block *) p;
509 return hash_section (old->sect);
512 /* Return a new unnamed section with the given fields. */
514 section *
515 get_unnamed_section (unsigned int flags, void (*callback) (const void *),
516 const void *data)
518 section *sect;
520 sect = GGC_NEW (section);
521 sect->unnamed.common.flags = flags | SECTION_UNNAMED;
522 sect->unnamed.callback = callback;
523 sect->unnamed.data = data;
524 sect->unnamed.next = unnamed_sections;
526 unnamed_sections = sect;
527 return sect;
530 /* Return a SECTION_NOSWITCH section with the given fields. */
532 static section *
533 get_noswitch_section (unsigned int flags, noswitch_section_callback callback)
535 section *sect;
537 sect = GGC_NEW (section);
538 sect->noswitch.common.flags = flags | SECTION_NOSWITCH;
539 sect->noswitch.callback = callback;
541 return sect;
544 /* Return the named section structure associated with NAME. Create
545 a new section with the given fields if no such structure exists. */
547 section *
548 get_section (const char *name, unsigned int flags, tree decl)
550 section *sect, **slot;
552 slot = (section **)
553 htab_find_slot_with_hash (section_htab, name,
554 htab_hash_string (name), INSERT);
555 flags |= SECTION_NAMED;
556 if (*slot == NULL)
558 sect = GGC_NEW (section);
559 sect->named.common.flags = flags;
560 sect->named.name = ggc_strdup (name);
561 sect->named.decl = decl;
562 *slot = sect;
564 else
566 sect = *slot;
567 if ((sect->common.flags & ~SECTION_DECLARED) != flags
568 && ((sect->common.flags | flags) & SECTION_OVERRIDE) == 0)
570 /* Sanity check user variables for flag changes. */
571 if (decl == 0)
572 decl = sect->named.decl;
573 gcc_assert (decl);
574 error ("%+D causes a section type conflict", decl);
577 return sect;
580 /* Return true if the current compilation mode benefits from having
581 objects grouped into blocks. */
583 static bool
584 use_object_blocks_p (void)
586 return flag_section_anchors;
589 /* Return the object_block structure for section SECT. Create a new
590 structure if we haven't created one already. Return null if SECT
591 itself is null. */
593 static struct object_block *
594 get_block_for_section (section *sect)
596 struct object_block *block;
597 void **slot;
599 if (sect == NULL)
600 return NULL;
602 slot = htab_find_slot_with_hash (object_block_htab, sect,
603 hash_section (sect), INSERT);
604 block = (struct object_block *) *slot;
605 if (block == NULL)
607 block = (struct object_block *)
608 ggc_alloc_cleared (sizeof (struct object_block));
609 block->sect = sect;
610 *slot = block;
612 return block;
615 /* Create a symbol with label LABEL and place it at byte offset
616 OFFSET in BLOCK. OFFSET can be negative if the symbol's offset
617 is not yet known. LABEL must be a garbage-collected string. */
619 static rtx
620 create_block_symbol (const char *label, struct object_block *block,
621 HOST_WIDE_INT offset)
623 rtx symbol;
624 unsigned int size;
626 /* Create the extended SYMBOL_REF. */
627 size = RTX_HDR_SIZE + sizeof (struct block_symbol);
628 symbol = (rtx) ggc_alloc_zone (size, &rtl_zone);
630 /* Initialize the normal SYMBOL_REF fields. */
631 memset (symbol, 0, size);
632 PUT_CODE (symbol, SYMBOL_REF);
633 PUT_MODE (symbol, Pmode);
634 XSTR (symbol, 0) = label;
635 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_HAS_BLOCK_INFO;
637 /* Initialize the block_symbol stuff. */
638 SYMBOL_REF_BLOCK (symbol) = block;
639 SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
641 return symbol;
644 static void
645 initialize_cold_section_name (void)
647 const char *stripped_name;
648 char *name, *buffer;
649 tree dsn;
651 gcc_assert (cfun && current_function_decl);
652 if (crtl->subsections.unlikely_text_section_name)
653 return;
655 dsn = DECL_SECTION_NAME (current_function_decl);
656 if (flag_function_sections && dsn)
658 name = (char *) alloca (TREE_STRING_LENGTH (dsn) + 1);
659 memcpy (name, TREE_STRING_POINTER (dsn), TREE_STRING_LENGTH (dsn) + 1);
661 stripped_name = targetm.strip_name_encoding (name);
663 buffer = ACONCAT ((stripped_name, "_unlikely", NULL));
664 crtl->subsections.unlikely_text_section_name = ggc_strdup (buffer);
666 else
667 crtl->subsections.unlikely_text_section_name = UNLIKELY_EXECUTED_TEXT_SECTION_NAME;
670 /* Tell assembler to switch to unlikely-to-be-executed text section. */
672 section *
673 unlikely_text_section (void)
675 if (cfun)
677 if (!crtl->subsections.unlikely_text_section_name)
678 initialize_cold_section_name ();
680 return get_named_section (NULL, crtl->subsections.unlikely_text_section_name, 0);
682 else
683 return get_named_section (NULL, UNLIKELY_EXECUTED_TEXT_SECTION_NAME, 0);
686 /* When called within a function context, return true if the function
687 has been assigned a cold text section and if SECT is that section.
688 When called outside a function context, return true if SECT is the
689 default cold section. */
691 bool
692 unlikely_text_section_p (section *sect)
694 const char *name;
696 if (cfun)
697 name = crtl->subsections.unlikely_text_section_name;
698 else
699 name = UNLIKELY_EXECUTED_TEXT_SECTION_NAME;
701 return (name
702 && sect
703 && SECTION_STYLE (sect) == SECTION_NAMED
704 && strcmp (name, sect->named.name) == 0);
707 /* Return a section with a particular name and with whatever SECTION_*
708 flags section_type_flags deems appropriate. The name of the section
709 is taken from NAME if nonnull, otherwise it is taken from DECL's
710 DECL_SECTION_NAME. DECL is the decl associated with the section
711 (see the section comment for details) and RELOC is as for
712 section_type_flags. */
714 section *
715 get_named_section (tree decl, const char *name, int reloc)
717 unsigned int flags;
719 gcc_assert (!decl || DECL_P (decl));
720 if (name == NULL)
721 name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
723 flags = targetm.section_type_flags (decl, name, reloc);
725 return get_section (name, flags, decl);
728 /* If required, set DECL_SECTION_NAME to a unique name. */
730 void
731 resolve_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED,
732 int flag_function_or_data_sections)
734 if (DECL_SECTION_NAME (decl) == NULL_TREE
735 && targetm.have_named_sections
736 && (flag_function_or_data_sections
737 || DECL_ONE_ONLY (decl)))
738 targetm.asm_out.unique_section (decl, reloc);
741 #ifdef BSS_SECTION_ASM_OP
743 #ifdef ASM_OUTPUT_BSS
745 /* Utility function for ASM_OUTPUT_BSS for targets to use if
746 they don't support alignments in .bss.
747 ??? It is believed that this function will work in most cases so such
748 support is localized here. */
750 static void
751 asm_output_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
752 const char *name,
753 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
754 unsigned HOST_WIDE_INT rounded)
756 gcc_assert (strcmp (XSTR (XEXP (DECL_RTL (decl), 0), 0), name) == 0);
757 targetm.asm_out.globalize_decl_name (file, decl);
758 switch_to_section (bss_section);
759 #ifdef ASM_DECLARE_OBJECT_NAME
760 last_assemble_variable_decl = decl;
761 ASM_DECLARE_OBJECT_NAME (file, name, decl);
762 #else
763 /* Standard thing is just output label for the object. */
764 ASM_OUTPUT_LABEL (file, name);
765 #endif /* ASM_DECLARE_OBJECT_NAME */
766 ASM_OUTPUT_SKIP (file, rounded ? rounded : 1);
769 #endif
771 #ifdef ASM_OUTPUT_ALIGNED_BSS
773 /* Utility function for targets to use in implementing
774 ASM_OUTPUT_ALIGNED_BSS.
775 ??? It is believed that this function will work in most cases so such
776 support is localized here. */
778 static void
779 asm_output_aligned_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
780 const char *name, unsigned HOST_WIDE_INT size,
781 int align)
783 switch_to_section (bss_section);
784 ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
785 #ifdef ASM_DECLARE_OBJECT_NAME
786 last_assemble_variable_decl = decl;
787 ASM_DECLARE_OBJECT_NAME (file, name, decl);
788 #else
789 /* Standard thing is just output label for the object. */
790 ASM_OUTPUT_LABEL (file, name);
791 #endif /* ASM_DECLARE_OBJECT_NAME */
792 ASM_OUTPUT_SKIP (file, size ? size : 1);
795 #endif
797 #endif /* BSS_SECTION_ASM_OP */
799 #ifndef USE_SELECT_SECTION_FOR_FUNCTIONS
800 /* Return the hot section for function DECL. Return text_section for
801 null DECLs. */
803 static section *
804 hot_function_section (tree decl)
806 if (decl != NULL_TREE
807 && DECL_SECTION_NAME (decl) != NULL_TREE
808 && targetm.have_named_sections)
809 return get_named_section (decl, NULL, 0);
810 else
811 return text_section;
813 #endif
815 /* Return the section for function DECL.
817 If DECL is NULL_TREE, return the text section. We can be passed
818 NULL_TREE under some circumstances by dbxout.c at least. */
820 section *
821 function_section (tree decl)
823 int reloc = 0;
825 if (first_function_block_is_cold)
826 reloc = 1;
828 #ifdef USE_SELECT_SECTION_FOR_FUNCTIONS
829 if (decl != NULL_TREE
830 && DECL_SECTION_NAME (decl) != NULL_TREE)
831 return reloc ? unlikely_text_section ()
832 : get_named_section (decl, NULL, 0);
833 else
834 return targetm.asm_out.select_section (decl, reloc, DECL_ALIGN (decl));
835 #else
836 return reloc ? unlikely_text_section () : hot_function_section (decl);
837 #endif
840 section *
841 current_function_section (void)
843 #ifdef USE_SELECT_SECTION_FOR_FUNCTIONS
844 if (current_function_decl != NULL_TREE
845 && DECL_SECTION_NAME (current_function_decl) != NULL_TREE)
846 return in_cold_section_p ? unlikely_text_section ()
847 : get_named_section (current_function_decl,
848 NULL, 0);
849 else
850 return targetm.asm_out.select_section (current_function_decl,
851 in_cold_section_p,
852 DECL_ALIGN (current_function_decl));
853 #else
854 return (in_cold_section_p
855 ? unlikely_text_section ()
856 : hot_function_section (current_function_decl));
857 #endif
860 /* Return the read-only data section associated with function DECL. */
862 section *
863 default_function_rodata_section (tree decl)
865 if (decl != NULL_TREE && DECL_SECTION_NAME (decl))
867 const char *name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
869 if (DECL_ONE_ONLY (decl) && HAVE_COMDAT_GROUP)
871 const char *dot;
872 size_t len;
873 char* rname;
875 dot = strchr (name + 1, '.');
876 if (!dot)
877 dot = name;
878 len = strlen (dot) + 8;
879 rname = (char *) alloca (len);
881 strcpy (rname, ".rodata");
882 strcat (rname, dot);
883 return get_section (rname, SECTION_LINKONCE, decl);
885 /* For .gnu.linkonce.t.foo we want to use .gnu.linkonce.r.foo. */
886 else if (DECL_ONE_ONLY (decl)
887 && strncmp (name, ".gnu.linkonce.t.", 16) == 0)
889 size_t len = strlen (name) + 1;
890 char *rname = (char *) alloca (len);
892 memcpy (rname, name, len);
893 rname[14] = 'r';
894 return get_section (rname, SECTION_LINKONCE, decl);
896 /* For .text.foo we want to use .rodata.foo. */
897 else if (flag_function_sections && flag_data_sections
898 && strncmp (name, ".text.", 6) == 0)
900 size_t len = strlen (name) + 1;
901 char *rname = (char *) alloca (len + 2);
903 memcpy (rname, ".rodata", 7);
904 memcpy (rname + 7, name + 5, len - 5);
905 return get_section (rname, 0, decl);
909 return readonly_data_section;
912 /* Return the read-only data section associated with function DECL
913 for targets where that section should be always the single
914 readonly data section. */
916 section *
917 default_no_function_rodata_section (tree decl ATTRIBUTE_UNUSED)
919 return readonly_data_section;
922 /* Return the section to use for string merging. */
924 static section *
925 mergeable_string_section (tree decl ATTRIBUTE_UNUSED,
926 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
927 unsigned int flags ATTRIBUTE_UNUSED)
929 HOST_WIDE_INT len;
931 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
932 && TREE_CODE (decl) == STRING_CST
933 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
934 && align <= 256
935 && (len = int_size_in_bytes (TREE_TYPE (decl))) > 0
936 && TREE_STRING_LENGTH (decl) >= len)
938 enum machine_mode mode;
939 unsigned int modesize;
940 const char *str;
941 HOST_WIDE_INT i;
942 int j, unit;
943 char name[30];
945 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (decl)));
946 modesize = GET_MODE_BITSIZE (mode);
947 if (modesize >= 8 && modesize <= 256
948 && (modesize & (modesize - 1)) == 0)
950 if (align < modesize)
951 align = modesize;
953 str = TREE_STRING_POINTER (decl);
954 unit = GET_MODE_SIZE (mode);
956 /* Check for embedded NUL characters. */
957 for (i = 0; i < len; i += unit)
959 for (j = 0; j < unit; j++)
960 if (str[i + j] != '\0')
961 break;
962 if (j == unit)
963 break;
965 if (i == len - unit)
967 sprintf (name, ".rodata.str%d.%d", modesize / 8,
968 (int) (align / 8));
969 flags |= (modesize / 8) | SECTION_MERGE | SECTION_STRINGS;
970 return get_section (name, flags, NULL);
975 return readonly_data_section;
978 /* Return the section to use for constant merging. */
980 section *
981 mergeable_constant_section (enum machine_mode mode ATTRIBUTE_UNUSED,
982 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
983 unsigned int flags ATTRIBUTE_UNUSED)
985 unsigned int modesize = GET_MODE_BITSIZE (mode);
987 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
988 && mode != VOIDmode
989 && mode != BLKmode
990 && modesize <= align
991 && align >= 8
992 && align <= 256
993 && (align & (align - 1)) == 0)
995 char name[24];
997 sprintf (name, ".rodata.cst%d", (int) (align / 8));
998 flags |= (align / 8) | SECTION_MERGE;
999 return get_section (name, flags, NULL);
1001 return readonly_data_section;
1004 /* Given NAME, a putative register name, discard any customary prefixes. */
1006 static const char *
1007 strip_reg_name (const char *name)
1009 #ifdef REGISTER_PREFIX
1010 if (!strncmp (name, REGISTER_PREFIX, strlen (REGISTER_PREFIX)))
1011 name += strlen (REGISTER_PREFIX);
1012 #endif
1013 if (name[0] == '%' || name[0] == '#')
1014 name++;
1015 return name;
1018 /* The user has asked for a DECL to have a particular name. Set (or
1019 change) it in such a way that we don't prefix an underscore to
1020 it. */
1021 void
1022 set_user_assembler_name (tree decl, const char *name)
1024 char *starred = (char *) alloca (strlen (name) + 2);
1025 starred[0] = '*';
1026 strcpy (starred + 1, name);
1027 change_decl_assembler_name (decl, get_identifier (starred));
1028 SET_DECL_RTL (decl, NULL_RTX);
1031 /* Decode an `asm' spec for a declaration as a register name.
1032 Return the register number, or -1 if nothing specified,
1033 or -2 if the ASMSPEC is not `cc' or `memory' and is not recognized,
1034 or -3 if ASMSPEC is `cc' and is not recognized,
1035 or -4 if ASMSPEC is `memory' and is not recognized.
1036 Accept an exact spelling or a decimal number.
1037 Prefixes such as % are optional. */
1040 decode_reg_name (const char *asmspec)
1042 if (asmspec != 0)
1044 int i;
1046 /* Get rid of confusing prefixes. */
1047 asmspec = strip_reg_name (asmspec);
1049 /* Allow a decimal number as a "register name". */
1050 for (i = strlen (asmspec) - 1; i >= 0; i--)
1051 if (! ISDIGIT (asmspec[i]))
1052 break;
1053 if (asmspec[0] != 0 && i < 0)
1055 i = atoi (asmspec);
1056 if (i < FIRST_PSEUDO_REGISTER && i >= 0)
1057 return i;
1058 else
1059 return -2;
1062 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1063 if (reg_names[i][0]
1064 && ! strcmp (asmspec, strip_reg_name (reg_names[i])))
1065 return i;
1067 #ifdef ADDITIONAL_REGISTER_NAMES
1069 static const struct { const char *const name; const int number; } table[]
1070 = ADDITIONAL_REGISTER_NAMES;
1072 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
1073 if (table[i].name[0]
1074 && ! strcmp (asmspec, table[i].name))
1075 return table[i].number;
1077 #endif /* ADDITIONAL_REGISTER_NAMES */
1079 if (!strcmp (asmspec, "memory"))
1080 return -4;
1082 if (!strcmp (asmspec, "cc"))
1083 return -3;
1085 return -2;
1088 return -1;
1091 /* Return true if DECL's initializer is suitable for a BSS section. */
1093 static bool
1094 bss_initializer_p (const_tree decl)
1096 return (DECL_INITIAL (decl) == NULL
1097 || DECL_INITIAL (decl) == error_mark_node
1098 || (flag_zero_initialized_in_bss
1099 /* Leave constant zeroes in .rodata so they
1100 can be shared. */
1101 && !TREE_READONLY (decl)
1102 && initializer_zerop (DECL_INITIAL (decl))));
1105 /* Compute the alignment of variable specified by DECL.
1106 DONT_OUTPUT_DATA is from assemble_variable. */
1108 void
1109 align_variable (tree decl, bool dont_output_data)
1111 unsigned int align = DECL_ALIGN (decl);
1113 /* In the case for initialing an array whose length isn't specified,
1114 where we have not yet been able to do the layout,
1115 figure out the proper alignment now. */
1116 if (dont_output_data && DECL_SIZE (decl) == 0
1117 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
1118 align = MAX (align, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (decl))));
1120 /* Some object file formats have a maximum alignment which they support.
1121 In particular, a.out format supports a maximum alignment of 4. */
1122 if (align > MAX_OFILE_ALIGNMENT)
1124 warning (0, "alignment of %q+D is greater than maximum object "
1125 "file alignment. Using %d", decl,
1126 MAX_OFILE_ALIGNMENT/BITS_PER_UNIT);
1127 align = MAX_OFILE_ALIGNMENT;
1130 /* On some machines, it is good to increase alignment sometimes. */
1131 if (! DECL_USER_ALIGN (decl))
1133 #ifdef DATA_ALIGNMENT
1134 unsigned int data_align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
1135 /* Don't increase alignment too much for TLS variables - TLS space
1136 is too precious. */
1137 if (! DECL_THREAD_LOCAL_P (decl) || data_align <= BITS_PER_WORD)
1138 align = data_align;
1139 #endif
1140 #ifdef CONSTANT_ALIGNMENT
1141 if (DECL_INITIAL (decl) != 0 && DECL_INITIAL (decl) != error_mark_node)
1143 unsigned int const_align = CONSTANT_ALIGNMENT (DECL_INITIAL (decl),
1144 align);
1145 /* Don't increase alignment too much for TLS variables - TLS space
1146 is too precious. */
1147 if (! DECL_THREAD_LOCAL_P (decl) || const_align <= BITS_PER_WORD)
1148 align = const_align;
1150 #endif
1153 /* Reset the alignment in case we have made it tighter, so we can benefit
1154 from it in get_pointer_alignment. */
1155 DECL_ALIGN (decl) = align;
1158 /* Return the section into which the given VAR_DECL or CONST_DECL
1159 should be placed. PREFER_NOSWITCH_P is true if a noswitch
1160 section should be used wherever possible. */
1162 static section *
1163 get_variable_section (tree decl, bool prefer_noswitch_p)
1165 int reloc;
1167 /* If the decl has been given an explicit section name, then it
1168 isn't common, and shouldn't be handled as such. */
1169 if (DECL_COMMON (decl) && DECL_SECTION_NAME (decl) == NULL)
1171 if (DECL_THREAD_LOCAL_P (decl))
1172 return tls_comm_section;
1173 /* This cannot be common bss for an emulated TLS object without
1174 a register_common hook. */
1175 else if (DECL_TLS_MODEL (decl) == TLS_MODEL_EMULATED
1176 && !targetm.emutls.register_common)
1178 else if (TREE_PUBLIC (decl) && bss_initializer_p (decl))
1179 return comm_section;
1182 if (DECL_INITIAL (decl) == error_mark_node)
1183 reloc = contains_pointers_p (TREE_TYPE (decl)) ? 3 : 0;
1184 else if (DECL_INITIAL (decl))
1185 reloc = compute_reloc_for_constant (DECL_INITIAL (decl));
1186 else
1187 reloc = 0;
1189 resolve_unique_section (decl, reloc, flag_data_sections);
1190 if (IN_NAMED_SECTION (decl))
1191 return get_named_section (decl, NULL, reloc);
1193 if (!DECL_THREAD_LOCAL_P (decl)
1194 && !(prefer_noswitch_p && targetm.have_switchable_bss_sections)
1195 && bss_initializer_p (decl))
1197 if (!TREE_PUBLIC (decl))
1198 return lcomm_section;
1199 if (bss_noswitch_section)
1200 return bss_noswitch_section;
1203 return targetm.asm_out.select_section (decl, reloc, DECL_ALIGN (decl));
1206 /* Return the block into which object_block DECL should be placed. */
1208 static struct object_block *
1209 get_block_for_decl (tree decl)
1211 section *sect;
1213 if (TREE_CODE (decl) == VAR_DECL)
1215 /* The object must be defined in this translation unit. */
1216 if (DECL_EXTERNAL (decl))
1217 return NULL;
1219 /* There's no point using object blocks for something that is
1220 isolated by definition. */
1221 if (DECL_ONE_ONLY (decl))
1222 return NULL;
1225 /* We can only calculate block offsets if the decl has a known
1226 constant size. */
1227 if (DECL_SIZE_UNIT (decl) == NULL)
1228 return NULL;
1229 if (!host_integerp (DECL_SIZE_UNIT (decl), 1))
1230 return NULL;
1232 /* Find out which section should contain DECL. We cannot put it into
1233 an object block if it requires a standalone definition. */
1234 if (TREE_CODE (decl) == VAR_DECL)
1235 align_variable (decl, 0);
1236 sect = get_variable_section (decl, true);
1237 if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
1238 return NULL;
1240 return get_block_for_section (sect);
1243 /* Make sure block symbol SYMBOL is in block BLOCK. */
1245 static void
1246 change_symbol_block (rtx symbol, struct object_block *block)
1248 if (block != SYMBOL_REF_BLOCK (symbol))
1250 gcc_assert (SYMBOL_REF_BLOCK_OFFSET (symbol) < 0);
1251 SYMBOL_REF_BLOCK (symbol) = block;
1255 /* Return true if it is possible to put DECL in an object_block. */
1257 static bool
1258 use_blocks_for_decl_p (tree decl)
1260 /* Only data DECLs can be placed into object blocks. */
1261 if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != CONST_DECL)
1262 return false;
1264 /* Detect decls created by dw2_force_const_mem. Such decls are
1265 special because DECL_INITIAL doesn't specify the decl's true value.
1266 dw2_output_indirect_constants will instead call assemble_variable
1267 with dont_output_data set to 1 and then print the contents itself. */
1268 if (DECL_INITIAL (decl) == decl)
1269 return false;
1271 /* If this decl is an alias, then we don't want to emit a definition. */
1272 if (lookup_attribute ("alias", DECL_ATTRIBUTES (decl)))
1273 return false;
1275 return true;
1278 /* Create the DECL_RTL for a VAR_DECL or FUNCTION_DECL. DECL should
1279 have static storage duration. In other words, it should not be an
1280 automatic variable, including PARM_DECLs.
1282 There is, however, one exception: this function handles variables
1283 explicitly placed in a particular register by the user.
1285 This is never called for PARM_DECL nodes. */
1287 void
1288 make_decl_rtl (tree decl)
1290 const char *name = 0;
1291 int reg_number;
1292 rtx x;
1294 /* Check that we are not being given an automatic variable. */
1295 gcc_assert (TREE_CODE (decl) != PARM_DECL
1296 && TREE_CODE (decl) != RESULT_DECL);
1298 /* A weak alias has TREE_PUBLIC set but not the other bits. */
1299 gcc_assert (TREE_CODE (decl) != VAR_DECL
1300 || TREE_STATIC (decl)
1301 || TREE_PUBLIC (decl)
1302 || DECL_EXTERNAL (decl)
1303 || DECL_REGISTER (decl));
1305 /* And that we were not given a type or a label. */
1306 gcc_assert (TREE_CODE (decl) != TYPE_DECL
1307 && TREE_CODE (decl) != LABEL_DECL);
1309 /* For a duplicate declaration, we can be called twice on the
1310 same DECL node. Don't discard the RTL already made. */
1311 if (DECL_RTL_SET_P (decl))
1313 /* If the old RTL had the wrong mode, fix the mode. */
1314 x = DECL_RTL (decl);
1315 if (GET_MODE (x) != DECL_MODE (decl))
1316 SET_DECL_RTL (decl, adjust_address_nv (x, DECL_MODE (decl), 0));
1318 if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1319 return;
1321 /* ??? Another way to do this would be to maintain a hashed
1322 table of such critters. Instead of adding stuff to a DECL
1323 to give certain attributes to it, we could use an external
1324 hash map from DECL to set of attributes. */
1326 /* Let the target reassign the RTL if it wants.
1327 This is necessary, for example, when one machine specific
1328 decl attribute overrides another. */
1329 targetm.encode_section_info (decl, DECL_RTL (decl), false);
1331 /* If the symbol has a SYMBOL_REF_BLOCK field, update it based
1332 on the new decl information. */
1333 if (MEM_P (x)
1334 && GET_CODE (XEXP (x, 0)) == SYMBOL_REF
1335 && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (x, 0)))
1336 change_symbol_block (XEXP (x, 0), get_block_for_decl (decl));
1338 /* Make this function static known to the mudflap runtime. */
1339 if (flag_mudflap && TREE_CODE (decl) == VAR_DECL)
1340 mudflap_enqueue_decl (decl);
1342 return;
1345 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
1347 if (name[0] != '*' && TREE_CODE (decl) != FUNCTION_DECL
1348 && DECL_REGISTER (decl))
1350 error ("register name not specified for %q+D", decl);
1352 else if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1354 const char *asmspec = name+1;
1355 reg_number = decode_reg_name (asmspec);
1356 /* First detect errors in declaring global registers. */
1357 if (reg_number == -1)
1358 error ("register name not specified for %q+D", decl);
1359 else if (reg_number < 0)
1360 error ("invalid register name for %q+D", decl);
1361 else if (TYPE_MODE (TREE_TYPE (decl)) == BLKmode)
1362 error ("data type of %q+D isn%'t suitable for a register",
1363 decl);
1364 else if (! HARD_REGNO_MODE_OK (reg_number, TYPE_MODE (TREE_TYPE (decl))))
1365 error ("register specified for %q+D isn%'t suitable for data type",
1366 decl);
1367 /* Now handle properly declared static register variables. */
1368 else
1370 int nregs;
1372 if (DECL_INITIAL (decl) != 0 && TREE_STATIC (decl))
1374 DECL_INITIAL (decl) = 0;
1375 error ("global register variable has initial value");
1377 if (TREE_THIS_VOLATILE (decl))
1378 warning (OPT_Wvolatile_register_var,
1379 "optimization may eliminate reads and/or "
1380 "writes to register variables");
1382 /* If the user specified one of the eliminables registers here,
1383 e.g., FRAME_POINTER_REGNUM, we don't want to get this variable
1384 confused with that register and be eliminated. This usage is
1385 somewhat suspect... */
1387 SET_DECL_RTL (decl, gen_rtx_raw_REG (DECL_MODE (decl), reg_number));
1388 ORIGINAL_REGNO (DECL_RTL (decl)) = reg_number;
1389 REG_USERVAR_P (DECL_RTL (decl)) = 1;
1391 if (TREE_STATIC (decl))
1393 /* Make this register global, so not usable for anything
1394 else. */
1395 #ifdef ASM_DECLARE_REGISTER_GLOBAL
1396 name = IDENTIFIER_POINTER (DECL_NAME (decl));
1397 ASM_DECLARE_REGISTER_GLOBAL (asm_out_file, decl, reg_number, name);
1398 #endif
1399 nregs = hard_regno_nregs[reg_number][DECL_MODE (decl)];
1400 while (nregs > 0)
1401 globalize_reg (reg_number + --nregs);
1404 /* As a register variable, it has no section. */
1405 return;
1408 /* Now handle ordinary static variables and functions (in memory).
1409 Also handle vars declared register invalidly. */
1410 else if (name[0] == '*')
1412 #ifdef REGISTER_PREFIX
1413 if (strlen (REGISTER_PREFIX) != 0)
1415 reg_number = decode_reg_name (name);
1416 if (reg_number >= 0 || reg_number == -3)
1417 error ("register name given for non-register variable %q+D", decl);
1419 #endif
1422 /* Specifying a section attribute on a variable forces it into a
1423 non-.bss section, and thus it cannot be common. */
1424 if (TREE_CODE (decl) == VAR_DECL
1425 && DECL_SECTION_NAME (decl) != NULL_TREE
1426 && DECL_INITIAL (decl) == NULL_TREE
1427 && DECL_COMMON (decl))
1428 DECL_COMMON (decl) = 0;
1430 /* Variables can't be both common and weak. */
1431 if (TREE_CODE (decl) == VAR_DECL && DECL_WEAK (decl))
1432 DECL_COMMON (decl) = 0;
1434 if (use_object_blocks_p () && use_blocks_for_decl_p (decl))
1435 x = create_block_symbol (name, get_block_for_decl (decl), -1);
1436 else
1437 x = gen_rtx_SYMBOL_REF (Pmode, name);
1438 SYMBOL_REF_WEAK (x) = DECL_WEAK (decl);
1439 SET_SYMBOL_REF_DECL (x, decl);
1441 x = gen_rtx_MEM (DECL_MODE (decl), x);
1442 if (TREE_CODE (decl) != FUNCTION_DECL)
1443 set_mem_attributes (x, decl, 1);
1444 SET_DECL_RTL (decl, x);
1446 /* Optionally set flags or add text to the name to record information
1447 such as that it is a function name.
1448 If the name is changed, the macro ASM_OUTPUT_LABELREF
1449 will have to know how to strip this information. */
1450 targetm.encode_section_info (decl, DECL_RTL (decl), true);
1452 /* Make this function static known to the mudflap runtime. */
1453 if (flag_mudflap && TREE_CODE (decl) == VAR_DECL)
1454 mudflap_enqueue_decl (decl);
1457 /* Output a string of literal assembler code
1458 for an `asm' keyword used between functions. */
1460 void
1461 assemble_asm (tree string)
1463 app_enable ();
1465 if (TREE_CODE (string) == ADDR_EXPR)
1466 string = TREE_OPERAND (string, 0);
1468 fprintf (asm_out_file, "\t%s\n", TREE_STRING_POINTER (string));
1471 /* Record an element in the table of global destructors. SYMBOL is
1472 a SYMBOL_REF of the function to be called; PRIORITY is a number
1473 between 0 and MAX_INIT_PRIORITY. */
1475 void
1476 default_stabs_asm_out_destructor (rtx symbol ATTRIBUTE_UNUSED,
1477 int priority ATTRIBUTE_UNUSED)
1479 #if defined DBX_DEBUGGING_INFO || defined XCOFF_DEBUGGING_INFO
1480 /* Tell GNU LD that this is part of the static destructor set.
1481 This will work for any system that uses stabs, most usefully
1482 aout systems. */
1483 dbxout_begin_simple_stabs ("___DTOR_LIST__", 22 /* N_SETT */);
1484 dbxout_stab_value_label (XSTR (symbol, 0));
1485 #else
1486 sorry ("global destructors not supported on this target");
1487 #endif
1490 /* Write the address of the entity given by SYMBOL to SEC. */
1491 void
1492 assemble_addr_to_section (rtx symbol, section *sec)
1494 switch_to_section (sec);
1495 assemble_align (POINTER_SIZE);
1496 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1499 /* Return the numbered .ctors.N (if CONSTRUCTOR_P) or .dtors.N (if
1500 not) section for PRIORITY. */
1501 section *
1502 get_cdtor_priority_section (int priority, bool constructor_p)
1504 char buf[16];
1506 /* ??? This only works reliably with the GNU linker. */
1507 sprintf (buf, "%s.%.5u",
1508 constructor_p ? ".ctors" : ".dtors",
1509 /* Invert the numbering so the linker puts us in the proper
1510 order; constructors are run from right to left, and the
1511 linker sorts in increasing order. */
1512 MAX_INIT_PRIORITY - priority);
1513 return get_section (buf, SECTION_WRITE, NULL);
1516 void
1517 default_named_section_asm_out_destructor (rtx symbol, int priority)
1519 section *sec;
1521 if (priority != DEFAULT_INIT_PRIORITY)
1522 sec = get_cdtor_priority_section (priority,
1523 /*constructor_p=*/false);
1524 else
1525 sec = get_section (".dtors", SECTION_WRITE, NULL);
1527 assemble_addr_to_section (symbol, sec);
1530 #ifdef DTORS_SECTION_ASM_OP
1531 void
1532 default_dtor_section_asm_out_destructor (rtx symbol,
1533 int priority ATTRIBUTE_UNUSED)
1535 assemble_addr_to_section (symbol, dtors_section);
1537 #endif
1539 /* Likewise for global constructors. */
1541 void
1542 default_stabs_asm_out_constructor (rtx symbol ATTRIBUTE_UNUSED,
1543 int priority ATTRIBUTE_UNUSED)
1545 #if defined DBX_DEBUGGING_INFO || defined XCOFF_DEBUGGING_INFO
1546 /* Tell GNU LD that this is part of the static destructor set.
1547 This will work for any system that uses stabs, most usefully
1548 aout systems. */
1549 dbxout_begin_simple_stabs ("___CTOR_LIST__", 22 /* N_SETT */);
1550 dbxout_stab_value_label (XSTR (symbol, 0));
1551 #else
1552 sorry ("global constructors not supported on this target");
1553 #endif
1556 void
1557 default_named_section_asm_out_constructor (rtx symbol, int priority)
1559 section *sec;
1561 if (priority != DEFAULT_INIT_PRIORITY)
1562 sec = get_cdtor_priority_section (priority,
1563 /*constructor_p=*/true);
1564 else
1565 sec = get_section (".ctors", SECTION_WRITE, NULL);
1567 assemble_addr_to_section (symbol, sec);
1570 #ifdef CTORS_SECTION_ASM_OP
1571 void
1572 default_ctor_section_asm_out_constructor (rtx symbol,
1573 int priority ATTRIBUTE_UNUSED)
1575 assemble_addr_to_section (symbol, ctors_section);
1577 #endif
1579 /* CONSTANT_POOL_BEFORE_FUNCTION may be defined as an expression with
1580 a nonzero value if the constant pool should be output before the
1581 start of the function, or a zero value if the pool should output
1582 after the end of the function. The default is to put it before the
1583 start. */
1585 #ifndef CONSTANT_POOL_BEFORE_FUNCTION
1586 #define CONSTANT_POOL_BEFORE_FUNCTION 1
1587 #endif
1589 /* DECL is an object (either VAR_DECL or FUNCTION_DECL) which is going
1590 to be output to assembler.
1591 Set first_global_object_name and weak_global_object_name as appropriate. */
1593 void
1594 notice_global_symbol (tree decl)
1596 const char **type = &first_global_object_name;
1598 if (first_global_object_name
1599 || !TREE_PUBLIC (decl)
1600 || DECL_EXTERNAL (decl)
1601 || !DECL_NAME (decl)
1602 || (TREE_CODE (decl) != FUNCTION_DECL
1603 && (TREE_CODE (decl) != VAR_DECL
1604 || (DECL_COMMON (decl)
1605 && (DECL_INITIAL (decl) == 0
1606 || DECL_INITIAL (decl) == error_mark_node))))
1607 || !MEM_P (DECL_RTL (decl)))
1608 return;
1610 /* We win when global object is found, but it is useful to know about weak
1611 symbol as well so we can produce nicer unique names. */
1612 if (DECL_WEAK (decl) || DECL_ONE_ONLY (decl) || flag_shlib)
1613 type = &weak_global_object_name;
1615 if (!*type)
1617 const char *p;
1618 const char *name;
1619 rtx decl_rtl = DECL_RTL (decl);
1621 p = targetm.strip_name_encoding (XSTR (XEXP (decl_rtl, 0), 0));
1622 name = ggc_strdup (p);
1624 *type = name;
1628 /* Output assembler code for the constant pool of a function and associated
1629 with defining the name of the function. DECL describes the function.
1630 NAME is the function's name. For the constant pool, we use the current
1631 constant pool data. */
1633 void
1634 assemble_start_function (tree decl, const char *fnname)
1636 int align;
1637 char tmp_label[100];
1638 bool hot_label_written = false;
1640 crtl->subsections.unlikely_text_section_name = NULL;
1642 first_function_block_is_cold = false;
1643 if (flag_reorder_blocks_and_partition)
1645 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTB", const_labelno);
1646 crtl->subsections.hot_section_label = ggc_strdup (tmp_label);
1647 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDB", const_labelno);
1648 crtl->subsections.cold_section_label = ggc_strdup (tmp_label);
1649 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTE", const_labelno);
1650 crtl->subsections.hot_section_end_label = ggc_strdup (tmp_label);
1651 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDE", const_labelno);
1652 crtl->subsections.cold_section_end_label = ggc_strdup (tmp_label);
1653 const_labelno++;
1655 else
1657 crtl->subsections.hot_section_label = NULL;
1658 crtl->subsections.cold_section_label = NULL;
1659 crtl->subsections.hot_section_end_label = NULL;
1660 crtl->subsections.cold_section_end_label = NULL;
1663 /* The following code does not need preprocessing in the assembler. */
1665 app_disable ();
1667 if (CONSTANT_POOL_BEFORE_FUNCTION)
1668 output_constant_pool (fnname, decl);
1670 resolve_unique_section (decl, 0, flag_function_sections);
1672 /* Make sure the not and cold text (code) sections are properly
1673 aligned. This is necessary here in the case where the function
1674 has both hot and cold sections, because we don't want to re-set
1675 the alignment when the section switch happens mid-function. */
1677 if (flag_reorder_blocks_and_partition)
1679 switch_to_section (unlikely_text_section ());
1680 assemble_align (DECL_ALIGN (decl));
1681 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_label);
1683 /* When the function starts with a cold section, we need to explicitly
1684 align the hot section and write out the hot section label.
1685 But if the current function is a thunk, we do not have a CFG. */
1686 if (!cfun->is_thunk
1687 && BB_PARTITION (ENTRY_BLOCK_PTR->next_bb) == BB_COLD_PARTITION)
1689 switch_to_section (text_section);
1690 assemble_align (DECL_ALIGN (decl));
1691 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
1692 hot_label_written = true;
1693 first_function_block_is_cold = true;
1696 else if (DECL_SECTION_NAME (decl))
1698 /* Calls to function_section rely on first_function_block_is_cold
1699 being accurate. The first block may be cold even if we aren't
1700 doing partitioning, if the entire function was decided by
1701 choose_function_section (predict.c) to be cold. */
1703 initialize_cold_section_name ();
1705 if (crtl->subsections.unlikely_text_section_name
1706 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
1707 crtl->subsections.unlikely_text_section_name) == 0)
1708 first_function_block_is_cold = true;
1711 in_cold_section_p = first_function_block_is_cold;
1713 /* Switch to the correct text section for the start of the function. */
1715 switch_to_section (function_section (decl));
1716 if (flag_reorder_blocks_and_partition
1717 && !hot_label_written)
1718 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
1720 /* Tell assembler to move to target machine's alignment for functions. */
1721 align = floor_log2 (DECL_ALIGN (decl) / BITS_PER_UNIT);
1722 if (align > 0)
1724 ASM_OUTPUT_ALIGN (asm_out_file, align);
1727 /* Handle a user-specified function alignment.
1728 Note that we still need to align to DECL_ALIGN, as above,
1729 because ASM_OUTPUT_MAX_SKIP_ALIGN might not do any alignment at all. */
1730 if (! DECL_USER_ALIGN (decl)
1731 && align_functions_log > align
1732 && optimize_function_for_speed_p (cfun))
1734 #ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
1735 ASM_OUTPUT_MAX_SKIP_ALIGN (asm_out_file,
1736 align_functions_log, align_functions - 1);
1737 #else
1738 ASM_OUTPUT_ALIGN (asm_out_file, align_functions_log);
1739 #endif
1742 #ifdef ASM_OUTPUT_FUNCTION_PREFIX
1743 ASM_OUTPUT_FUNCTION_PREFIX (asm_out_file, fnname);
1744 #endif
1746 (*debug_hooks->begin_function) (decl);
1748 /* Make function name accessible from other files, if appropriate. */
1750 if (TREE_PUBLIC (decl))
1752 notice_global_symbol (decl);
1754 globalize_decl (decl);
1756 maybe_assemble_visibility (decl);
1759 if (DECL_PRESERVE_P (decl))
1760 targetm.asm_out.mark_decl_preserved (fnname);
1762 /* Do any machine/system dependent processing of the function name. */
1763 #ifdef ASM_DECLARE_FUNCTION_NAME
1764 ASM_DECLARE_FUNCTION_NAME (asm_out_file, fnname, current_function_decl);
1765 #else
1766 /* Standard thing is just output label for the function. */
1767 ASM_OUTPUT_LABEL (asm_out_file, fnname);
1768 #endif /* ASM_DECLARE_FUNCTION_NAME */
1771 /* Output assembler code associated with defining the size of the
1772 function. DECL describes the function. NAME is the function's name. */
1774 void
1775 assemble_end_function (tree decl, const char *fnname ATTRIBUTE_UNUSED)
1777 #ifdef ASM_DECLARE_FUNCTION_SIZE
1778 /* We could have switched section in the middle of the function. */
1779 if (flag_reorder_blocks_and_partition)
1780 switch_to_section (function_section (decl));
1781 ASM_DECLARE_FUNCTION_SIZE (asm_out_file, fnname, decl);
1782 #endif
1783 if (! CONSTANT_POOL_BEFORE_FUNCTION)
1785 output_constant_pool (fnname, decl);
1786 switch_to_section (function_section (decl)); /* need to switch back */
1788 /* Output labels for end of hot/cold text sections (to be used by
1789 debug info.) */
1790 if (flag_reorder_blocks_and_partition)
1792 section *save_text_section;
1794 save_text_section = in_section;
1795 switch_to_section (unlikely_text_section ());
1796 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_end_label);
1797 if (first_function_block_is_cold)
1798 switch_to_section (text_section);
1799 else
1800 switch_to_section (function_section (decl));
1801 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_end_label);
1802 switch_to_section (save_text_section);
1806 /* Assemble code to leave SIZE bytes of zeros. */
1808 void
1809 assemble_zeros (unsigned HOST_WIDE_INT size)
1811 /* Do no output if -fsyntax-only. */
1812 if (flag_syntax_only)
1813 return;
1815 #ifdef ASM_NO_SKIP_IN_TEXT
1816 /* The `space' pseudo in the text section outputs nop insns rather than 0s,
1817 so we must output 0s explicitly in the text section. */
1818 if (ASM_NO_SKIP_IN_TEXT && (in_section->common.flags & SECTION_CODE) != 0)
1820 unsigned HOST_WIDE_INT i;
1821 for (i = 0; i < size; i++)
1822 assemble_integer (const0_rtx, 1, BITS_PER_UNIT, 1);
1824 else
1825 #endif
1826 if (size > 0)
1827 ASM_OUTPUT_SKIP (asm_out_file, size);
1830 /* Assemble an alignment pseudo op for an ALIGN-bit boundary. */
1832 void
1833 assemble_align (int align)
1835 if (align > BITS_PER_UNIT)
1837 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
1841 /* Assemble a string constant with the specified C string as contents. */
1843 void
1844 assemble_string (const char *p, int size)
1846 int pos = 0;
1847 int maximum = 2000;
1849 /* If the string is very long, split it up. */
1851 while (pos < size)
1853 int thissize = size - pos;
1854 if (thissize > maximum)
1855 thissize = maximum;
1857 ASM_OUTPUT_ASCII (asm_out_file, p, thissize);
1859 pos += thissize;
1860 p += thissize;
1865 /* A noswitch_section_callback for lcomm_section. */
1867 static bool
1868 emit_local (tree decl ATTRIBUTE_UNUSED,
1869 const char *name ATTRIBUTE_UNUSED,
1870 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1871 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1873 #if defined ASM_OUTPUT_ALIGNED_DECL_LOCAL
1874 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, decl, name,
1875 size, DECL_ALIGN (decl));
1876 return true;
1877 #elif defined ASM_OUTPUT_ALIGNED_LOCAL
1878 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, DECL_ALIGN (decl));
1879 return true;
1880 #else
1881 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
1882 return false;
1883 #endif
1886 /* A noswitch_section_callback for bss_noswitch_section. */
1888 #if defined ASM_OUTPUT_ALIGNED_BSS || defined ASM_OUTPUT_BSS
1889 static bool
1890 emit_bss (tree decl ATTRIBUTE_UNUSED,
1891 const char *name ATTRIBUTE_UNUSED,
1892 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1893 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1895 #if defined ASM_OUTPUT_ALIGNED_BSS
1896 ASM_OUTPUT_ALIGNED_BSS (asm_out_file, decl, name, size, DECL_ALIGN (decl));
1897 return true;
1898 #else
1899 ASM_OUTPUT_BSS (asm_out_file, decl, name, size, rounded);
1900 return false;
1901 #endif
1903 #endif
1905 /* A noswitch_section_callback for comm_section. */
1907 static bool
1908 emit_common (tree decl ATTRIBUTE_UNUSED,
1909 const char *name ATTRIBUTE_UNUSED,
1910 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1911 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1913 #if defined ASM_OUTPUT_ALIGNED_DECL_COMMON
1914 ASM_OUTPUT_ALIGNED_DECL_COMMON (asm_out_file, decl, name,
1915 size, DECL_ALIGN (decl));
1916 return true;
1917 #elif defined ASM_OUTPUT_ALIGNED_COMMON
1918 ASM_OUTPUT_ALIGNED_COMMON (asm_out_file, name, size, DECL_ALIGN (decl));
1919 return true;
1920 #else
1921 ASM_OUTPUT_COMMON (asm_out_file, name, size, rounded);
1922 return false;
1923 #endif
1926 /* A noswitch_section_callback for tls_comm_section. */
1928 static bool
1929 emit_tls_common (tree decl ATTRIBUTE_UNUSED,
1930 const char *name ATTRIBUTE_UNUSED,
1931 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1932 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1934 #ifdef ASM_OUTPUT_TLS_COMMON
1935 ASM_OUTPUT_TLS_COMMON (asm_out_file, decl, name, size);
1936 return true;
1937 #else
1938 sorry ("thread-local COMMON data not implemented");
1939 return true;
1940 #endif
1943 /* Assemble DECL given that it belongs in SECTION_NOSWITCH section SECT.
1944 NAME is the name of DECL's SYMBOL_REF. */
1946 static void
1947 assemble_noswitch_variable (tree decl, const char *name, section *sect)
1949 unsigned HOST_WIDE_INT size, rounded;
1951 size = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
1952 rounded = size;
1954 /* Don't allocate zero bytes of common,
1955 since that means "undefined external" in the linker. */
1956 if (size == 0)
1957 rounded = 1;
1959 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
1960 so that each uninitialized object starts on such a boundary. */
1961 rounded += (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1;
1962 rounded = (rounded / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
1963 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
1965 if (!sect->noswitch.callback (decl, name, size, rounded)
1966 && (unsigned HOST_WIDE_INT) DECL_ALIGN_UNIT (decl) > rounded)
1967 warning (0, "requested alignment for %q+D is greater than "
1968 "implemented alignment of %wu", decl, rounded);
1971 /* A subroutine of assemble_variable. Output the label and contents of
1972 DECL, whose address is a SYMBOL_REF with name NAME. DONT_OUTPUT_DATA
1973 is as for assemble_variable. */
1975 static void
1976 assemble_variable_contents (tree decl, const char *name,
1977 bool dont_output_data)
1979 /* Do any machine/system dependent processing of the object. */
1980 #ifdef ASM_DECLARE_OBJECT_NAME
1981 last_assemble_variable_decl = decl;
1982 ASM_DECLARE_OBJECT_NAME (asm_out_file, name, decl);
1983 #else
1984 /* Standard thing is just output label for the object. */
1985 ASM_OUTPUT_LABEL (asm_out_file, name);
1986 #endif /* ASM_DECLARE_OBJECT_NAME */
1988 if (!dont_output_data)
1990 if (DECL_INITIAL (decl)
1991 && DECL_INITIAL (decl) != error_mark_node
1992 && !initializer_zerop (DECL_INITIAL (decl)))
1993 /* Output the actual data. */
1994 output_constant (DECL_INITIAL (decl),
1995 tree_low_cst (DECL_SIZE_UNIT (decl), 1),
1996 DECL_ALIGN (decl));
1997 else
1998 /* Leave space for it. */
1999 assemble_zeros (tree_low_cst (DECL_SIZE_UNIT (decl), 1));
2003 /* Initialize emulated tls object TO, which refers to TLS variable
2004 DECL and is initialized by PROXY. */
2006 tree
2007 default_emutls_var_init (tree to, tree decl, tree proxy)
2009 VEC(constructor_elt,gc) *v = VEC_alloc (constructor_elt, gc, 4);
2010 constructor_elt *elt;
2011 tree type = TREE_TYPE (to);
2012 tree field = TYPE_FIELDS (type);
2014 elt = VEC_quick_push (constructor_elt, v, NULL);
2015 elt->index = field;
2016 elt->value = fold_convert (TREE_TYPE (field), DECL_SIZE_UNIT (decl));
2018 elt = VEC_quick_push (constructor_elt, v, NULL);
2019 field = TREE_CHAIN (field);
2020 elt->index = field;
2021 elt->value = build_int_cst (TREE_TYPE (field),
2022 DECL_ALIGN_UNIT (decl));
2024 elt = VEC_quick_push (constructor_elt, v, NULL);
2025 field = TREE_CHAIN (field);
2026 elt->index = field;
2027 elt->value = null_pointer_node;
2029 elt = VEC_quick_push (constructor_elt, v, NULL);
2030 field = TREE_CHAIN (field);
2031 elt->index = field;
2032 elt->value = proxy;
2034 return build_constructor (type, v);
2037 /* Assemble everything that is needed for a variable or function declaration.
2038 Not used for automatic variables, and not used for function definitions.
2039 Should not be called for variables of incomplete structure type.
2041 TOP_LEVEL is nonzero if this variable has file scope.
2042 AT_END is nonzero if this is the special handling, at end of compilation,
2043 to define things that have had only tentative definitions.
2044 DONT_OUTPUT_DATA if nonzero means don't actually output the
2045 initial value (that will be done by the caller). */
2047 void
2048 assemble_variable (tree decl, int top_level ATTRIBUTE_UNUSED,
2049 int at_end ATTRIBUTE_UNUSED, int dont_output_data)
2051 const char *name;
2052 rtx decl_rtl, symbol;
2053 section *sect;
2055 if (! targetm.have_tls
2056 && TREE_CODE (decl) == VAR_DECL
2057 && DECL_THREAD_LOCAL_P (decl))
2059 tree to = emutls_decl (decl);
2061 /* If this variable is defined locally, then we need to initialize the
2062 control structure with size and alignment information. We do this
2063 at the last moment because tentative definitions can take a locally
2064 defined but uninitialized variable and initialize it later, which
2065 would result in incorrect contents. */
2066 if (! DECL_EXTERNAL (to)
2067 && (! DECL_COMMON (to)
2068 || (DECL_INITIAL (decl)
2069 && DECL_INITIAL (decl) != error_mark_node)))
2071 DECL_INITIAL (to) = targetm.emutls.var_init
2072 (to, decl, get_emutls_init_templ_addr (decl));
2074 /* Make sure the template is marked as needed early enough.
2075 Without this, if the variable is placed in a
2076 section-anchored block, the template will only be marked
2077 when it's too late. */
2078 record_references_in_initializer (to);
2081 decl = to;
2084 last_assemble_variable_decl = 0;
2086 /* Normally no need to say anything here for external references,
2087 since assemble_external is called by the language-specific code
2088 when a declaration is first seen. */
2090 if (DECL_EXTERNAL (decl))
2091 return;
2093 /* Output no assembler code for a function declaration.
2094 Only definitions of functions output anything. */
2096 if (TREE_CODE (decl) == FUNCTION_DECL)
2097 return;
2099 /* Do nothing for global register variables. */
2100 if (DECL_RTL_SET_P (decl) && REG_P (DECL_RTL (decl)))
2102 TREE_ASM_WRITTEN (decl) = 1;
2103 return;
2106 /* If type was incomplete when the variable was declared,
2107 see if it is complete now. */
2109 if (DECL_SIZE (decl) == 0)
2110 layout_decl (decl, 0);
2112 /* Still incomplete => don't allocate it; treat the tentative defn
2113 (which is what it must have been) as an `extern' reference. */
2115 if (!dont_output_data && DECL_SIZE (decl) == 0)
2117 error ("storage size of %q+D isn%'t known", decl);
2118 TREE_ASM_WRITTEN (decl) = 1;
2119 return;
2122 /* The first declaration of a variable that comes through this function
2123 decides whether it is global (in C, has external linkage)
2124 or local (in C, has internal linkage). So do nothing more
2125 if this function has already run. */
2127 if (TREE_ASM_WRITTEN (decl))
2128 return;
2130 /* Make sure targetm.encode_section_info is invoked before we set
2131 ASM_WRITTEN. */
2132 decl_rtl = DECL_RTL (decl);
2134 TREE_ASM_WRITTEN (decl) = 1;
2136 /* Do no output if -fsyntax-only. */
2137 if (flag_syntax_only)
2138 return;
2140 app_disable ();
2142 if (! dont_output_data
2143 && ! host_integerp (DECL_SIZE_UNIT (decl), 1))
2145 error ("size of variable %q+D is too large", decl);
2146 return;
2149 gcc_assert (MEM_P (decl_rtl));
2150 gcc_assert (GET_CODE (XEXP (decl_rtl, 0)) == SYMBOL_REF);
2151 symbol = XEXP (decl_rtl, 0);
2152 name = XSTR (symbol, 0);
2153 if (TREE_PUBLIC (decl) && DECL_NAME (decl))
2154 notice_global_symbol (decl);
2156 /* Compute the alignment of this data. */
2158 align_variable (decl, dont_output_data);
2159 set_mem_align (decl_rtl, DECL_ALIGN (decl));
2161 if (TREE_PUBLIC (decl))
2162 maybe_assemble_visibility (decl);
2164 if (DECL_PRESERVE_P (decl))
2165 targetm.asm_out.mark_decl_preserved (name);
2167 /* First make the assembler name(s) global if appropriate. */
2168 sect = get_variable_section (decl, false);
2169 if (TREE_PUBLIC (decl)
2170 && (sect->common.flags & SECTION_COMMON) == 0)
2171 globalize_decl (decl);
2173 /* Output any data that we will need to use the address of. */
2174 if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node)
2175 output_addressed_constants (DECL_INITIAL (decl));
2177 /* dbxout.c needs to know this. */
2178 if (sect && (sect->common.flags & SECTION_CODE) != 0)
2179 DECL_IN_TEXT_SECTION (decl) = 1;
2181 /* If the decl is part of an object_block, make sure that the decl
2182 has been positioned within its block, but do not write out its
2183 definition yet. output_object_blocks will do that later. */
2184 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
2186 gcc_assert (!dont_output_data);
2187 place_block_symbol (symbol);
2189 else if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
2190 assemble_noswitch_variable (decl, name, sect);
2191 else
2193 switch_to_section (sect);
2194 if (DECL_ALIGN (decl) > BITS_PER_UNIT)
2195 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (DECL_ALIGN_UNIT (decl)));
2196 assemble_variable_contents (decl, name, dont_output_data);
2200 /* Return 1 if type TYPE contains any pointers. */
2202 static int
2203 contains_pointers_p (tree type)
2205 switch (TREE_CODE (type))
2207 case POINTER_TYPE:
2208 case REFERENCE_TYPE:
2209 /* I'm not sure whether OFFSET_TYPE needs this treatment,
2210 so I'll play safe and return 1. */
2211 case OFFSET_TYPE:
2212 return 1;
2214 case RECORD_TYPE:
2215 case UNION_TYPE:
2216 case QUAL_UNION_TYPE:
2218 tree fields;
2219 /* For a type that has fields, see if the fields have pointers. */
2220 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
2221 if (TREE_CODE (fields) == FIELD_DECL
2222 && contains_pointers_p (TREE_TYPE (fields)))
2223 return 1;
2224 return 0;
2227 case ARRAY_TYPE:
2228 /* An array type contains pointers if its element type does. */
2229 return contains_pointers_p (TREE_TYPE (type));
2231 default:
2232 return 0;
2236 /* We delay assemble_external processing until
2237 the compilation unit is finalized. This is the best we can do for
2238 right now (i.e. stage 3 of GCC 4.0) - the right thing is to delay
2239 it all the way to final. See PR 17982 for further discussion. */
2240 static GTY(()) tree pending_assemble_externals;
2242 #ifdef ASM_OUTPUT_EXTERNAL
2243 /* True if DECL is a function decl for which no out-of-line copy exists.
2244 It is assumed that DECL's assembler name has been set. */
2246 static bool
2247 incorporeal_function_p (tree decl)
2249 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
2251 const char *name;
2253 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
2254 && DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA)
2255 return true;
2257 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2258 if (is_builtin_name (name))
2259 return true;
2261 return false;
2264 /* Actually do the tests to determine if this is necessary, and invoke
2265 ASM_OUTPUT_EXTERNAL. */
2266 static void
2267 assemble_external_real (tree decl)
2269 rtx rtl = DECL_RTL (decl);
2271 if (MEM_P (rtl) && GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF
2272 && !SYMBOL_REF_USED (XEXP (rtl, 0))
2273 && !incorporeal_function_p (decl))
2275 /* Some systems do require some output. */
2276 SYMBOL_REF_USED (XEXP (rtl, 0)) = 1;
2277 ASM_OUTPUT_EXTERNAL (asm_out_file, decl, XSTR (XEXP (rtl, 0), 0));
2280 #endif
2282 void
2283 process_pending_assemble_externals (void)
2285 #ifdef ASM_OUTPUT_EXTERNAL
2286 tree list;
2287 for (list = pending_assemble_externals; list; list = TREE_CHAIN (list))
2288 assemble_external_real (TREE_VALUE (list));
2290 pending_assemble_externals = 0;
2291 #endif
2294 /* This TREE_LIST contains any weak symbol declarations waiting
2295 to be emitted. */
2296 static GTY(()) tree weak_decls;
2298 /* Output something to declare an external symbol to the assembler,
2299 and qualifiers such as weakness. (Most assemblers don't need
2300 extern declaration, so we normally output nothing.) Do nothing if
2301 DECL is not external. */
2303 void
2304 assemble_external (tree decl ATTRIBUTE_UNUSED)
2306 /* Because most platforms do not define ASM_OUTPUT_EXTERNAL, the
2307 main body of this code is only rarely exercised. To provide some
2308 testing, on all platforms, we make sure that the ASM_OUT_FILE is
2309 open. If it's not, we should not be calling this function. */
2310 gcc_assert (asm_out_file);
2312 if (!DECL_P (decl) || !DECL_EXTERNAL (decl) || !TREE_PUBLIC (decl))
2313 return;
2315 /* We want to output annotation for weak and external symbols at
2316 very last to check if they are references or not. */
2318 if (SUPPORTS_WEAK && DECL_WEAK (decl)
2319 /* TREE_STATIC is a weird and abused creature which is not
2320 generally the right test for whether an entity has been
2321 locally emitted, inlined or otherwise not-really-extern, but
2322 for declarations that can be weak, it happens to be
2323 match. */
2324 && !TREE_STATIC (decl)
2325 && tree_find_value (weak_decls, decl) == NULL_TREE)
2326 weak_decls = tree_cons (NULL, decl, weak_decls);
2328 #ifdef ASM_OUTPUT_EXTERNAL
2329 if (tree_find_value (pending_assemble_externals, decl) == NULL_TREE)
2330 pending_assemble_externals = tree_cons (NULL, decl,
2331 pending_assemble_externals);
2332 #endif
2335 /* Similar, for calling a library function FUN. */
2337 void
2338 assemble_external_libcall (rtx fun)
2340 /* Declare library function name external when first used, if nec. */
2341 if (! SYMBOL_REF_USED (fun))
2343 SYMBOL_REF_USED (fun) = 1;
2344 targetm.asm_out.external_libcall (fun);
2348 /* Assemble a label named NAME. */
2350 void
2351 assemble_label (const char *name)
2353 ASM_OUTPUT_LABEL (asm_out_file, name);
2356 /* Set the symbol_referenced flag for ID. */
2357 void
2358 mark_referenced (tree id)
2360 TREE_SYMBOL_REFERENCED (id) = 1;
2363 /* Set the symbol_referenced flag for DECL and notify callgraph. */
2364 void
2365 mark_decl_referenced (tree decl)
2367 if (TREE_CODE (decl) == FUNCTION_DECL)
2369 /* Extern inline functions don't become needed when referenced.
2370 If we know a method will be emitted in other TU and no new
2371 functions can be marked reachable, just use the external
2372 definition. */
2373 struct cgraph_node *node = cgraph_node (decl);
2374 if (!DECL_EXTERNAL (decl)
2375 && (!node->local.vtable_method || !cgraph_global_info_ready
2376 || !node->local.finalized))
2377 cgraph_mark_needed_node (node);
2379 else if (TREE_CODE (decl) == VAR_DECL)
2381 struct varpool_node *node = varpool_node (decl);
2382 varpool_mark_needed_node (node);
2383 /* C++ frontend use mark_decl_references to force COMDAT variables
2384 to be output that might appear dead otherwise. */
2385 node->force_output = true;
2387 /* else do nothing - we can get various sorts of CST nodes here,
2388 which do not need to be marked. */
2392 /* Follow the IDENTIFIER_TRANSPARENT_ALIAS chain starting at *ALIAS
2393 until we find an identifier that is not itself a transparent alias.
2394 Modify the alias passed to it by reference (and all aliases on the
2395 way to the ultimate target), such that they do not have to be
2396 followed again, and return the ultimate target of the alias
2397 chain. */
2399 static inline tree
2400 ultimate_transparent_alias_target (tree *alias)
2402 tree target = *alias;
2404 if (IDENTIFIER_TRANSPARENT_ALIAS (target))
2406 gcc_assert (TREE_CHAIN (target));
2407 target = ultimate_transparent_alias_target (&TREE_CHAIN (target));
2408 gcc_assert (! IDENTIFIER_TRANSPARENT_ALIAS (target)
2409 && ! TREE_CHAIN (target));
2410 *alias = target;
2413 return target;
2416 /* Output to FILE (an assembly file) a reference to NAME. If NAME
2417 starts with a *, the rest of NAME is output verbatim. Otherwise
2418 NAME is transformed in a target-specific way (usually by the
2419 addition of an underscore). */
2421 void
2422 assemble_name_raw (FILE *file, const char *name)
2424 if (name[0] == '*')
2425 fputs (&name[1], file);
2426 else
2427 ASM_OUTPUT_LABELREF (file, name);
2430 /* Like assemble_name_raw, but should be used when NAME might refer to
2431 an entity that is also represented as a tree (like a function or
2432 variable). If NAME does refer to such an entity, that entity will
2433 be marked as referenced. */
2435 void
2436 assemble_name (FILE *file, const char *name)
2438 const char *real_name;
2439 tree id;
2441 real_name = targetm.strip_name_encoding (name);
2443 id = maybe_get_identifier (real_name);
2444 if (id)
2446 tree id_orig = id;
2448 mark_referenced (id);
2449 ultimate_transparent_alias_target (&id);
2450 if (id != id_orig)
2451 name = IDENTIFIER_POINTER (id);
2452 gcc_assert (! TREE_CHAIN (id));
2455 assemble_name_raw (file, name);
2458 /* Allocate SIZE bytes writable static space with a gensym name
2459 and return an RTX to refer to its address. */
2462 assemble_static_space (unsigned HOST_WIDE_INT size)
2464 char name[12];
2465 const char *namestring;
2466 rtx x;
2468 ASM_GENERATE_INTERNAL_LABEL (name, "LF", const_labelno);
2469 ++const_labelno;
2470 namestring = ggc_strdup (name);
2472 x = gen_rtx_SYMBOL_REF (Pmode, namestring);
2473 SYMBOL_REF_FLAGS (x) = SYMBOL_FLAG_LOCAL;
2475 #ifdef ASM_OUTPUT_ALIGNED_DECL_LOCAL
2476 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, NULL_TREE, name, size,
2477 BIGGEST_ALIGNMENT);
2478 #else
2479 #ifdef ASM_OUTPUT_ALIGNED_LOCAL
2480 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, BIGGEST_ALIGNMENT);
2481 #else
2483 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
2484 so that each uninitialized object starts on such a boundary. */
2485 /* Variable `rounded' might or might not be used in ASM_OUTPUT_LOCAL. */
2486 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED
2487 = ((size + (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1)
2488 / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
2489 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
2490 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
2492 #endif
2493 #endif
2494 return x;
2497 /* Assemble the static constant template for function entry trampolines.
2498 This is done at most once per compilation.
2499 Returns an RTX for the address of the template. */
2501 static GTY(()) rtx initial_trampoline;
2503 #ifdef TRAMPOLINE_TEMPLATE
2505 assemble_trampoline_template (void)
2507 char label[256];
2508 const char *name;
2509 int align;
2510 rtx symbol;
2512 if (initial_trampoline)
2513 return initial_trampoline;
2515 /* By default, put trampoline templates in read-only data section. */
2517 #ifdef TRAMPOLINE_SECTION
2518 switch_to_section (TRAMPOLINE_SECTION);
2519 #else
2520 switch_to_section (readonly_data_section);
2521 #endif
2523 /* Write the assembler code to define one. */
2524 align = floor_log2 (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
2525 if (align > 0)
2527 ASM_OUTPUT_ALIGN (asm_out_file, align);
2530 targetm.asm_out.internal_label (asm_out_file, "LTRAMP", 0);
2531 TRAMPOLINE_TEMPLATE (asm_out_file);
2533 /* Record the rtl to refer to it. */
2534 ASM_GENERATE_INTERNAL_LABEL (label, "LTRAMP", 0);
2535 name = ggc_strdup (label);
2536 symbol = gen_rtx_SYMBOL_REF (Pmode, name);
2537 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_LOCAL;
2539 initial_trampoline = gen_rtx_MEM (BLKmode, symbol);
2540 set_mem_align (initial_trampoline, TRAMPOLINE_ALIGNMENT);
2542 return initial_trampoline;
2544 #endif
2546 /* A and B are either alignments or offsets. Return the minimum alignment
2547 that may be assumed after adding the two together. */
2549 static inline unsigned
2550 min_align (unsigned int a, unsigned int b)
2552 return (a | b) & -(a | b);
2555 /* Return the assembler directive for creating a given kind of integer
2556 object. SIZE is the number of bytes in the object and ALIGNED_P
2557 indicates whether it is known to be aligned. Return NULL if the
2558 assembly dialect has no such directive.
2560 The returned string should be printed at the start of a new line and
2561 be followed immediately by the object's initial value. */
2563 const char *
2564 integer_asm_op (int size, int aligned_p)
2566 struct asm_int_op *ops;
2568 if (aligned_p)
2569 ops = &targetm.asm_out.aligned_op;
2570 else
2571 ops = &targetm.asm_out.unaligned_op;
2573 switch (size)
2575 case 1:
2576 return targetm.asm_out.byte_op;
2577 case 2:
2578 return ops->hi;
2579 case 4:
2580 return ops->si;
2581 case 8:
2582 return ops->di;
2583 case 16:
2584 return ops->ti;
2585 default:
2586 return NULL;
2590 /* Use directive OP to assemble an integer object X. Print OP at the
2591 start of the line, followed immediately by the value of X. */
2593 void
2594 assemble_integer_with_op (const char *op, rtx x)
2596 fputs (op, asm_out_file);
2597 output_addr_const (asm_out_file, x);
2598 fputc ('\n', asm_out_file);
2601 /* The default implementation of the asm_out.integer target hook. */
2603 bool
2604 default_assemble_integer (rtx x ATTRIBUTE_UNUSED,
2605 unsigned int size ATTRIBUTE_UNUSED,
2606 int aligned_p ATTRIBUTE_UNUSED)
2608 const char *op = integer_asm_op (size, aligned_p);
2609 /* Avoid GAS bugs for large values. Specifically negative values whose
2610 absolute value fits in a bfd_vma, but not in a bfd_signed_vma. */
2611 if (size > UNITS_PER_WORD && size > POINTER_SIZE / BITS_PER_UNIT)
2612 return false;
2613 return op && (assemble_integer_with_op (op, x), true);
2616 /* Assemble the integer constant X into an object of SIZE bytes. ALIGN is
2617 the alignment of the integer in bits. Return 1 if we were able to output
2618 the constant, otherwise 0. We must be able to output the constant,
2619 if FORCE is nonzero. */
2621 bool
2622 assemble_integer (rtx x, unsigned int size, unsigned int align, int force)
2624 int aligned_p;
2626 aligned_p = (align >= MIN (size * BITS_PER_UNIT, BIGGEST_ALIGNMENT));
2628 /* See if the target hook can handle this kind of object. */
2629 if (targetm.asm_out.integer (x, size, aligned_p))
2630 return true;
2632 /* If the object is a multi-byte one, try splitting it up. Split
2633 it into words it if is multi-word, otherwise split it into bytes. */
2634 if (size > 1)
2636 enum machine_mode omode, imode;
2637 unsigned int subalign;
2638 unsigned int subsize, i;
2639 enum mode_class mclass;
2641 subsize = size > UNITS_PER_WORD? UNITS_PER_WORD : 1;
2642 subalign = MIN (align, subsize * BITS_PER_UNIT);
2643 if (GET_CODE (x) == CONST_FIXED)
2644 mclass = GET_MODE_CLASS (GET_MODE (x));
2645 else
2646 mclass = MODE_INT;
2648 omode = mode_for_size (subsize * BITS_PER_UNIT, mclass, 0);
2649 imode = mode_for_size (size * BITS_PER_UNIT, mclass, 0);
2651 for (i = 0; i < size; i += subsize)
2653 rtx partial = simplify_subreg (omode, x, imode, i);
2654 if (!partial || !assemble_integer (partial, subsize, subalign, 0))
2655 break;
2657 if (i == size)
2658 return true;
2660 /* If we've printed some of it, but not all of it, there's no going
2661 back now. */
2662 gcc_assert (!i);
2665 gcc_assert (!force);
2667 return false;
2670 void
2671 assemble_real (REAL_VALUE_TYPE d, enum machine_mode mode, unsigned int align)
2673 long data[4] = {0, 0, 0, 0};
2674 int i;
2675 int bitsize, nelts, nunits, units_per;
2677 /* This is hairy. We have a quantity of known size. real_to_target
2678 will put it into an array of *host* longs, 32 bits per element
2679 (even if long is more than 32 bits). We need to determine the
2680 number of array elements that are occupied (nelts) and the number
2681 of *target* min-addressable units that will be occupied in the
2682 object file (nunits). We cannot assume that 32 divides the
2683 mode's bitsize (size * BITS_PER_UNIT) evenly.
2685 size * BITS_PER_UNIT is used here to make sure that padding bits
2686 (which might appear at either end of the value; real_to_target
2687 will include the padding bits in its output array) are included. */
2689 nunits = GET_MODE_SIZE (mode);
2690 bitsize = nunits * BITS_PER_UNIT;
2691 nelts = CEIL (bitsize, 32);
2692 units_per = 32 / BITS_PER_UNIT;
2694 real_to_target (data, &d, mode);
2696 /* Put out the first word with the specified alignment. */
2697 assemble_integer (GEN_INT (data[0]), MIN (nunits, units_per), align, 1);
2698 nunits -= units_per;
2700 /* Subsequent words need only 32-bit alignment. */
2701 align = min_align (align, 32);
2703 for (i = 1; i < nelts; i++)
2705 assemble_integer (GEN_INT (data[i]), MIN (nunits, units_per), align, 1);
2706 nunits -= units_per;
2710 /* Given an expression EXP with a constant value,
2711 reduce it to the sum of an assembler symbol and an integer.
2712 Store them both in the structure *VALUE.
2713 EXP must be reducible. */
2715 struct GTY(()) addr_const {
2716 rtx base;
2717 HOST_WIDE_INT offset;
2720 static void
2721 decode_addr_const (tree exp, struct addr_const *value)
2723 tree target = TREE_OPERAND (exp, 0);
2724 int offset = 0;
2725 rtx x;
2727 while (1)
2729 if (TREE_CODE (target) == COMPONENT_REF
2730 && host_integerp (byte_position (TREE_OPERAND (target, 1)), 0))
2733 offset += int_byte_position (TREE_OPERAND (target, 1));
2734 target = TREE_OPERAND (target, 0);
2736 else if (TREE_CODE (target) == ARRAY_REF
2737 || TREE_CODE (target) == ARRAY_RANGE_REF)
2739 offset += (tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (target)), 1)
2740 * tree_low_cst (TREE_OPERAND (target, 1), 0));
2741 target = TREE_OPERAND (target, 0);
2743 else
2744 break;
2747 switch (TREE_CODE (target))
2749 case VAR_DECL:
2750 case FUNCTION_DECL:
2751 x = DECL_RTL (target);
2752 break;
2754 case LABEL_DECL:
2755 x = gen_rtx_MEM (FUNCTION_MODE,
2756 gen_rtx_LABEL_REF (Pmode, force_label_rtx (target)));
2757 break;
2759 case REAL_CST:
2760 case FIXED_CST:
2761 case STRING_CST:
2762 case COMPLEX_CST:
2763 case CONSTRUCTOR:
2764 case INTEGER_CST:
2765 x = output_constant_def (target, 1);
2766 break;
2768 default:
2769 gcc_unreachable ();
2772 gcc_assert (MEM_P (x));
2773 x = XEXP (x, 0);
2775 value->base = x;
2776 value->offset = offset;
2779 /* Uniquize all constants that appear in memory.
2780 Each constant in memory thus far output is recorded
2781 in `const_desc_table'. */
2783 struct GTY(()) constant_descriptor_tree {
2784 /* A MEM for the constant. */
2785 rtx rtl;
2787 /* The value of the constant. */
2788 tree value;
2790 /* Hash of value. Computing the hash from value each time
2791 hashfn is called can't work properly, as that means recursive
2792 use of the hash table during hash table expansion. */
2793 hashval_t hash;
2796 static GTY((param_is (struct constant_descriptor_tree)))
2797 htab_t const_desc_htab;
2799 static struct constant_descriptor_tree * build_constant_desc (tree);
2800 static void maybe_output_constant_def_contents (struct constant_descriptor_tree *, int);
2802 /* Compute a hash code for a constant expression. */
2804 static hashval_t
2805 const_desc_hash (const void *ptr)
2807 return ((const struct constant_descriptor_tree *)ptr)->hash;
2810 static hashval_t
2811 const_hash_1 (const tree exp)
2813 const char *p;
2814 hashval_t hi;
2815 int len, i;
2816 enum tree_code code = TREE_CODE (exp);
2818 /* Either set P and LEN to the address and len of something to hash and
2819 exit the switch or return a value. */
2821 switch (code)
2823 case INTEGER_CST:
2824 p = (char *) &TREE_INT_CST (exp);
2825 len = sizeof TREE_INT_CST (exp);
2826 break;
2828 case REAL_CST:
2829 return real_hash (TREE_REAL_CST_PTR (exp));
2831 case FIXED_CST:
2832 return fixed_hash (TREE_FIXED_CST_PTR (exp));
2834 case STRING_CST:
2835 p = TREE_STRING_POINTER (exp);
2836 len = TREE_STRING_LENGTH (exp);
2837 break;
2839 case COMPLEX_CST:
2840 return (const_hash_1 (TREE_REALPART (exp)) * 5
2841 + const_hash_1 (TREE_IMAGPART (exp)));
2843 case CONSTRUCTOR:
2845 unsigned HOST_WIDE_INT idx;
2846 tree value;
2848 hi = 5 + int_size_in_bytes (TREE_TYPE (exp));
2850 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
2851 if (value)
2852 hi = hi * 603 + const_hash_1 (value);
2854 return hi;
2857 case ADDR_EXPR:
2858 case FDESC_EXPR:
2860 struct addr_const value;
2862 decode_addr_const (exp, &value);
2863 switch (GET_CODE (value.base))
2865 case SYMBOL_REF:
2866 /* Don't hash the address of the SYMBOL_REF;
2867 only use the offset and the symbol name. */
2868 hi = value.offset;
2869 p = XSTR (value.base, 0);
2870 for (i = 0; p[i] != 0; i++)
2871 hi = ((hi * 613) + (unsigned) (p[i]));
2872 break;
2874 case LABEL_REF:
2875 hi = value.offset + CODE_LABEL_NUMBER (XEXP (value.base, 0)) * 13;
2876 break;
2878 default:
2879 gcc_unreachable ();
2882 return hi;
2884 case PLUS_EXPR:
2885 case POINTER_PLUS_EXPR:
2886 case MINUS_EXPR:
2887 return (const_hash_1 (TREE_OPERAND (exp, 0)) * 9
2888 + const_hash_1 (TREE_OPERAND (exp, 1)));
2890 CASE_CONVERT:
2891 return const_hash_1 (TREE_OPERAND (exp, 0)) * 7 + 2;
2893 default:
2894 /* A language specific constant. Just hash the code. */
2895 return code;
2898 /* Compute hashing function. */
2899 hi = len;
2900 for (i = 0; i < len; i++)
2901 hi = ((hi * 613) + (unsigned) (p[i]));
2903 return hi;
2906 /* Wrapper of compare_constant, for the htab interface. */
2907 static int
2908 const_desc_eq (const void *p1, const void *p2)
2910 const struct constant_descriptor_tree *const c1
2911 = (const struct constant_descriptor_tree *) p1;
2912 const struct constant_descriptor_tree *const c2
2913 = (const struct constant_descriptor_tree *) p2;
2914 if (c1->hash != c2->hash)
2915 return 0;
2916 return compare_constant (c1->value, c2->value);
2919 /* Compare t1 and t2, and return 1 only if they are known to result in
2920 the same bit pattern on output. */
2922 static int
2923 compare_constant (const tree t1, const tree t2)
2925 enum tree_code typecode;
2927 if (t1 == NULL_TREE)
2928 return t2 == NULL_TREE;
2929 if (t2 == NULL_TREE)
2930 return 0;
2932 if (TREE_CODE (t1) != TREE_CODE (t2))
2933 return 0;
2935 switch (TREE_CODE (t1))
2937 case INTEGER_CST:
2938 /* Integer constants are the same only if the same width of type. */
2939 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
2940 return 0;
2941 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
2942 return 0;
2943 return tree_int_cst_equal (t1, t2);
2945 case REAL_CST:
2946 /* Real constants are the same only if the same width of type. */
2947 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
2948 return 0;
2950 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
2952 case FIXED_CST:
2953 /* Fixed constants are the same only if the same width of type. */
2954 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
2955 return 0;
2957 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
2959 case STRING_CST:
2960 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
2961 return 0;
2963 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
2964 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
2965 TREE_STRING_LENGTH (t1)));
2967 case COMPLEX_CST:
2968 return (compare_constant (TREE_REALPART (t1), TREE_REALPART (t2))
2969 && compare_constant (TREE_IMAGPART (t1), TREE_IMAGPART (t2)));
2971 case CONSTRUCTOR:
2973 VEC(constructor_elt, gc) *v1, *v2;
2974 unsigned HOST_WIDE_INT idx;
2976 typecode = TREE_CODE (TREE_TYPE (t1));
2977 if (typecode != TREE_CODE (TREE_TYPE (t2)))
2978 return 0;
2980 if (typecode == ARRAY_TYPE)
2982 HOST_WIDE_INT size_1 = int_size_in_bytes (TREE_TYPE (t1));
2983 /* For arrays, check that the sizes all match. */
2984 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))
2985 || size_1 == -1
2986 || size_1 != int_size_in_bytes (TREE_TYPE (t2)))
2987 return 0;
2989 else
2991 /* For record and union constructors, require exact type
2992 equality. */
2993 if (TREE_TYPE (t1) != TREE_TYPE (t2))
2994 return 0;
2997 v1 = CONSTRUCTOR_ELTS (t1);
2998 v2 = CONSTRUCTOR_ELTS (t2);
2999 if (VEC_length (constructor_elt, v1)
3000 != VEC_length (constructor_elt, v2))
3001 return 0;
3003 for (idx = 0; idx < VEC_length (constructor_elt, v1); ++idx)
3005 constructor_elt *c1 = VEC_index (constructor_elt, v1, idx);
3006 constructor_elt *c2 = VEC_index (constructor_elt, v2, idx);
3008 /* Check that each value is the same... */
3009 if (!compare_constant (c1->value, c2->value))
3010 return 0;
3011 /* ... and that they apply to the same fields! */
3012 if (typecode == ARRAY_TYPE)
3014 if (!compare_constant (c1->index, c2->index))
3015 return 0;
3017 else
3019 if (c1->index != c2->index)
3020 return 0;
3024 return 1;
3027 case ADDR_EXPR:
3028 case FDESC_EXPR:
3030 struct addr_const value1, value2;
3032 decode_addr_const (t1, &value1);
3033 decode_addr_const (t2, &value2);
3034 return (value1.offset == value2.offset
3035 && strcmp (XSTR (value1.base, 0), XSTR (value2.base, 0)) == 0);
3038 case PLUS_EXPR:
3039 case POINTER_PLUS_EXPR:
3040 case MINUS_EXPR:
3041 case RANGE_EXPR:
3042 return (compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
3043 && compare_constant(TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
3045 CASE_CONVERT:
3046 case VIEW_CONVERT_EXPR:
3047 return compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3049 default:
3050 return 0;
3053 gcc_unreachable ();
3056 /* Make a copy of the whole tree structure for a constant. This
3057 handles the same types of nodes that compare_constant handles. */
3059 static tree
3060 copy_constant (tree exp)
3062 switch (TREE_CODE (exp))
3064 case ADDR_EXPR:
3065 /* For ADDR_EXPR, we do not want to copy the decl whose address
3066 is requested. We do want to copy constants though. */
3067 if (CONSTANT_CLASS_P (TREE_OPERAND (exp, 0)))
3068 return build1 (TREE_CODE (exp), TREE_TYPE (exp),
3069 copy_constant (TREE_OPERAND (exp, 0)));
3070 else
3071 return copy_node (exp);
3073 case INTEGER_CST:
3074 case REAL_CST:
3075 case FIXED_CST:
3076 case STRING_CST:
3077 return copy_node (exp);
3079 case COMPLEX_CST:
3080 return build_complex (TREE_TYPE (exp),
3081 copy_constant (TREE_REALPART (exp)),
3082 copy_constant (TREE_IMAGPART (exp)));
3084 case PLUS_EXPR:
3085 case POINTER_PLUS_EXPR:
3086 case MINUS_EXPR:
3087 return build2 (TREE_CODE (exp), TREE_TYPE (exp),
3088 copy_constant (TREE_OPERAND (exp, 0)),
3089 copy_constant (TREE_OPERAND (exp, 1)));
3091 CASE_CONVERT:
3092 case VIEW_CONVERT_EXPR:
3093 return build1 (TREE_CODE (exp), TREE_TYPE (exp),
3094 copy_constant (TREE_OPERAND (exp, 0)));
3096 case CONSTRUCTOR:
3098 tree copy = copy_node (exp);
3099 VEC(constructor_elt, gc) *v;
3100 unsigned HOST_WIDE_INT idx;
3101 tree purpose, value;
3103 v = VEC_alloc(constructor_elt, gc, VEC_length(constructor_elt,
3104 CONSTRUCTOR_ELTS (exp)));
3105 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, purpose, value)
3107 constructor_elt *ce = VEC_quick_push (constructor_elt, v, NULL);
3108 ce->index = purpose;
3109 ce->value = copy_constant (value);
3111 CONSTRUCTOR_ELTS (copy) = v;
3112 return copy;
3115 default:
3116 gcc_unreachable ();
3120 /* Return the alignment of constant EXP in bits. */
3122 static unsigned int
3123 get_constant_alignment (tree exp)
3125 unsigned int align;
3127 align = TYPE_ALIGN (TREE_TYPE (exp));
3128 #ifdef CONSTANT_ALIGNMENT
3129 align = CONSTANT_ALIGNMENT (exp, align);
3130 #endif
3131 return align;
3134 /* Return the section into which constant EXP should be placed. */
3136 static section *
3137 get_constant_section (tree exp)
3139 if (IN_NAMED_SECTION (exp))
3140 return get_named_section (exp, NULL, compute_reloc_for_constant (exp));
3141 else
3142 return targetm.asm_out.select_section (exp,
3143 compute_reloc_for_constant (exp),
3144 get_constant_alignment (exp));
3147 /* Return the size of constant EXP in bytes. */
3149 static HOST_WIDE_INT
3150 get_constant_size (tree exp)
3152 HOST_WIDE_INT size;
3154 size = int_size_in_bytes (TREE_TYPE (exp));
3155 if (TREE_CODE (exp) == STRING_CST)
3156 size = MAX (TREE_STRING_LENGTH (exp), size);
3157 return size;
3160 /* Subroutine of output_constant_def:
3161 No constant equal to EXP is known to have been output.
3162 Make a constant descriptor to enter EXP in the hash table.
3163 Assign the label number and construct RTL to refer to the
3164 constant's location in memory.
3165 Caller is responsible for updating the hash table. */
3167 static struct constant_descriptor_tree *
3168 build_constant_desc (tree exp)
3170 rtx symbol;
3171 rtx rtl;
3172 char label[256];
3173 int labelno;
3174 struct constant_descriptor_tree *desc;
3176 desc = GGC_NEW (struct constant_descriptor_tree);
3177 desc->value = copy_constant (exp);
3179 /* Propagate marked-ness to copied constant. */
3180 if (flag_mudflap && mf_marked_p (exp))
3181 mf_mark (desc->value);
3183 /* Create a string containing the label name, in LABEL. */
3184 labelno = const_labelno++;
3185 ASM_GENERATE_INTERNAL_LABEL (label, "LC", labelno);
3187 /* We have a symbol name; construct the SYMBOL_REF and the MEM. */
3188 if (use_object_blocks_p ())
3190 section *sect = get_constant_section (exp);
3191 symbol = create_block_symbol (ggc_strdup (label),
3192 get_block_for_section (sect), -1);
3194 else
3195 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3196 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3197 SET_SYMBOL_REF_DECL (symbol, desc->value);
3198 TREE_CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3200 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (exp)), symbol);
3201 set_mem_attributes (rtl, exp, 1);
3202 set_mem_alias_set (rtl, 0);
3203 set_mem_alias_set (rtl, const_alias_set);
3205 /* Set flags or add text to the name to record information, such as
3206 that it is a local symbol. If the name is changed, the macro
3207 ASM_OUTPUT_LABELREF will have to know how to strip this
3208 information. This call might invalidate our local variable
3209 SYMBOL; we can't use it afterward. */
3211 targetm.encode_section_info (exp, rtl, true);
3213 desc->rtl = rtl;
3215 return desc;
3218 /* Return an rtx representing a reference to constant data in memory
3219 for the constant expression EXP.
3221 If assembler code for such a constant has already been output,
3222 return an rtx to refer to it.
3223 Otherwise, output such a constant in memory
3224 and generate an rtx for it.
3226 If DEFER is nonzero, this constant can be deferred and output only
3227 if referenced in the function after all optimizations.
3229 `const_desc_table' records which constants already have label strings. */
3232 output_constant_def (tree exp, int defer)
3234 struct constant_descriptor_tree *desc;
3235 struct constant_descriptor_tree key;
3236 void **loc;
3238 /* Look up EXP in the table of constant descriptors. If we didn't find
3239 it, create a new one. */
3240 key.value = exp;
3241 key.hash = const_hash_1 (exp);
3242 loc = htab_find_slot_with_hash (const_desc_htab, &key, key.hash, INSERT);
3244 desc = (struct constant_descriptor_tree *) *loc;
3245 if (desc == 0)
3247 desc = build_constant_desc (exp);
3248 desc->hash = key.hash;
3249 *loc = desc;
3252 maybe_output_constant_def_contents (desc, defer);
3253 return desc->rtl;
3256 /* Subroutine of output_constant_def: Decide whether or not we need to
3257 output the constant DESC now, and if so, do it. */
3258 static void
3259 maybe_output_constant_def_contents (struct constant_descriptor_tree *desc,
3260 int defer)
3262 rtx symbol = XEXP (desc->rtl, 0);
3263 tree exp = desc->value;
3265 if (flag_syntax_only)
3266 return;
3268 if (TREE_ASM_WRITTEN (exp))
3269 /* Already output; don't do it again. */
3270 return;
3272 /* We can always defer constants as long as the context allows
3273 doing so. */
3274 if (defer)
3276 /* Increment n_deferred_constants if it exists. It needs to be at
3277 least as large as the number of constants actually referred to
3278 by the function. If it's too small we'll stop looking too early
3279 and fail to emit constants; if it's too large we'll only look
3280 through the entire function when we could have stopped earlier. */
3281 if (cfun)
3282 n_deferred_constants++;
3283 return;
3286 output_constant_def_contents (symbol);
3289 /* Subroutine of output_constant_def_contents. Output the definition
3290 of constant EXP, which is pointed to by label LABEL. ALIGN is the
3291 constant's alignment in bits. */
3293 static void
3294 assemble_constant_contents (tree exp, const char *label, unsigned int align)
3296 HOST_WIDE_INT size;
3298 size = get_constant_size (exp);
3300 /* Do any machine/system dependent processing of the constant. */
3301 #ifdef ASM_DECLARE_CONSTANT_NAME
3302 ASM_DECLARE_CONSTANT_NAME (asm_out_file, label, exp, size);
3303 #else
3304 /* Standard thing is just output label for the constant. */
3305 ASM_OUTPUT_LABEL (asm_out_file, label);
3306 #endif /* ASM_DECLARE_CONSTANT_NAME */
3308 /* Output the value of EXP. */
3309 output_constant (exp, size, align);
3312 /* We must output the constant data referred to by SYMBOL; do so. */
3314 static void
3315 output_constant_def_contents (rtx symbol)
3317 tree exp = SYMBOL_REF_DECL (symbol);
3318 unsigned int align;
3320 /* Make sure any other constants whose addresses appear in EXP
3321 are assigned label numbers. */
3322 output_addressed_constants (exp);
3324 /* We are no longer deferring this constant. */
3325 TREE_ASM_WRITTEN (exp) = 1;
3327 /* If the constant is part of an object block, make sure that the
3328 decl has been positioned within its block, but do not write out
3329 its definition yet. output_object_blocks will do that later. */
3330 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
3331 place_block_symbol (symbol);
3332 else
3334 switch_to_section (get_constant_section (exp));
3335 align = get_constant_alignment (exp);
3336 if (align > BITS_PER_UNIT)
3337 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
3338 assemble_constant_contents (exp, XSTR (symbol, 0), align);
3340 if (flag_mudflap)
3341 mudflap_enqueue_constant (exp);
3344 /* Look up EXP in the table of constant descriptors. Return the rtl
3345 if it has been emitted, else null. */
3348 lookup_constant_def (tree exp)
3350 struct constant_descriptor_tree *desc;
3351 struct constant_descriptor_tree key;
3353 key.value = exp;
3354 key.hash = const_hash_1 (exp);
3355 desc = (struct constant_descriptor_tree *)
3356 htab_find_with_hash (const_desc_htab, &key, key.hash);
3358 return (desc ? desc->rtl : NULL_RTX);
3361 /* Used in the hash tables to avoid outputting the same constant
3362 twice. Unlike 'struct constant_descriptor_tree', RTX constants
3363 are output once per function, not once per file. */
3364 /* ??? Only a few targets need per-function constant pools. Most
3365 can use one per-file pool. Should add a targetm bit to tell the
3366 difference. */
3368 struct GTY(()) rtx_constant_pool {
3369 /* Pointers to first and last constant in pool, as ordered by offset. */
3370 struct constant_descriptor_rtx *first;
3371 struct constant_descriptor_rtx *last;
3373 /* Hash facility for making memory-constants from constant rtl-expressions.
3374 It is used on RISC machines where immediate integer arguments and
3375 constant addresses are restricted so that such constants must be stored
3376 in memory. */
3377 htab_t GTY((param_is (struct constant_descriptor_rtx))) const_rtx_htab;
3379 /* Current offset in constant pool (does not include any
3380 machine-specific header). */
3381 HOST_WIDE_INT offset;
3384 struct GTY((chain_next ("%h.next"))) constant_descriptor_rtx {
3385 struct constant_descriptor_rtx *next;
3386 rtx mem;
3387 rtx sym;
3388 rtx constant;
3389 HOST_WIDE_INT offset;
3390 hashval_t hash;
3391 enum machine_mode mode;
3392 unsigned int align;
3393 int labelno;
3394 int mark;
3397 /* Hash and compare functions for const_rtx_htab. */
3399 static hashval_t
3400 const_desc_rtx_hash (const void *ptr)
3402 const struct constant_descriptor_rtx *const desc
3403 = (const struct constant_descriptor_rtx *) ptr;
3404 return desc->hash;
3407 static int
3408 const_desc_rtx_eq (const void *a, const void *b)
3410 const struct constant_descriptor_rtx *const x
3411 = (const struct constant_descriptor_rtx *) a;
3412 const struct constant_descriptor_rtx *const y
3413 = (const struct constant_descriptor_rtx *) b;
3415 if (x->mode != y->mode)
3416 return 0;
3417 return rtx_equal_p (x->constant, y->constant);
3420 /* This is the worker function for const_rtx_hash, called via for_each_rtx. */
3422 static int
3423 const_rtx_hash_1 (rtx *xp, void *data)
3425 unsigned HOST_WIDE_INT hwi;
3426 enum machine_mode mode;
3427 enum rtx_code code;
3428 hashval_t h, *hp;
3429 rtx x;
3431 x = *xp;
3432 code = GET_CODE (x);
3433 mode = GET_MODE (x);
3434 h = (hashval_t) code * 1048573 + mode;
3436 switch (code)
3438 case CONST_INT:
3439 hwi = INTVAL (x);
3440 fold_hwi:
3442 const int shift = sizeof (hashval_t) * CHAR_BIT;
3443 const int n = sizeof (HOST_WIDE_INT) / sizeof (hashval_t);
3444 int i;
3446 h ^= (hashval_t) hwi;
3447 for (i = 1; i < n; ++i)
3449 hwi >>= shift;
3450 h ^= (hashval_t) hwi;
3453 break;
3455 case CONST_DOUBLE:
3456 if (mode == VOIDmode)
3458 hwi = CONST_DOUBLE_LOW (x) ^ CONST_DOUBLE_HIGH (x);
3459 goto fold_hwi;
3461 else
3462 h ^= real_hash (CONST_DOUBLE_REAL_VALUE (x));
3463 break;
3465 case CONST_FIXED:
3466 h ^= fixed_hash (CONST_FIXED_VALUE (x));
3467 break;
3469 case CONST_VECTOR:
3471 int i;
3472 for (i = XVECLEN (x, 0); i-- > 0; )
3473 h = h * 251 + const_rtx_hash_1 (&XVECEXP (x, 0, i), data);
3475 break;
3477 case SYMBOL_REF:
3478 h ^= htab_hash_string (XSTR (x, 0));
3479 break;
3481 case LABEL_REF:
3482 h = h * 251 + CODE_LABEL_NUMBER (XEXP (x, 0));
3483 break;
3485 case UNSPEC:
3486 case UNSPEC_VOLATILE:
3487 h = h * 251 + XINT (x, 1);
3488 break;
3490 default:
3491 break;
3494 hp = (hashval_t *) data;
3495 *hp = *hp * 509 + h;
3496 return 0;
3499 /* Compute a hash value for X, which should be a constant. */
3501 static hashval_t
3502 const_rtx_hash (rtx x)
3504 hashval_t h = 0;
3505 for_each_rtx (&x, const_rtx_hash_1, &h);
3506 return h;
3510 /* Create and return a new rtx constant pool. */
3512 static struct rtx_constant_pool *
3513 create_constant_pool (void)
3515 struct rtx_constant_pool *pool;
3517 pool = GGC_NEW (struct rtx_constant_pool);
3518 pool->const_rtx_htab = htab_create_ggc (31, const_desc_rtx_hash,
3519 const_desc_rtx_eq, NULL);
3520 pool->first = NULL;
3521 pool->last = NULL;
3522 pool->offset = 0;
3523 return pool;
3526 /* Initialize constant pool hashing for a new function. */
3528 void
3529 init_varasm_status (void)
3531 crtl->varasm.pool = create_constant_pool ();
3532 crtl->varasm.deferred_constants = 0;
3535 /* Given a MINUS expression, simplify it if both sides
3536 include the same symbol. */
3539 simplify_subtraction (rtx x)
3541 rtx r = simplify_rtx (x);
3542 return r ? r : x;
3545 /* Given a constant rtx X, make (or find) a memory constant for its value
3546 and return a MEM rtx to refer to it in memory. */
3549 force_const_mem (enum machine_mode mode, rtx x)
3551 struct constant_descriptor_rtx *desc, tmp;
3552 struct rtx_constant_pool *pool;
3553 char label[256];
3554 rtx def, symbol;
3555 hashval_t hash;
3556 unsigned int align;
3557 void **slot;
3559 /* If we're not allowed to drop X into the constant pool, don't. */
3560 if (targetm.cannot_force_const_mem (x))
3561 return NULL_RTX;
3563 /* Record that this function has used a constant pool entry. */
3564 crtl->uses_const_pool = 1;
3566 /* Decide which pool to use. */
3567 pool = (targetm.use_blocks_for_constant_p (mode, x)
3568 ? shared_constant_pool
3569 : crtl->varasm.pool);
3571 /* Lookup the value in the hashtable. */
3572 tmp.constant = x;
3573 tmp.mode = mode;
3574 hash = const_rtx_hash (x);
3575 slot = htab_find_slot_with_hash (pool->const_rtx_htab, &tmp, hash, INSERT);
3576 desc = (struct constant_descriptor_rtx *) *slot;
3578 /* If the constant was already present, return its memory. */
3579 if (desc)
3580 return copy_rtx (desc->mem);
3582 /* Otherwise, create a new descriptor. */
3583 desc = GGC_NEW (struct constant_descriptor_rtx);
3584 *slot = desc;
3586 /* Align the location counter as required by EXP's data type. */
3587 align = GET_MODE_ALIGNMENT (mode == VOIDmode ? word_mode : mode);
3588 #ifdef CONSTANT_ALIGNMENT
3590 tree type = lang_hooks.types.type_for_mode (mode, 0);
3591 if (type != NULL_TREE)
3592 align = CONSTANT_ALIGNMENT (make_tree (type, x), align);
3594 #endif
3596 pool->offset += (align / BITS_PER_UNIT) - 1;
3597 pool->offset &= ~ ((align / BITS_PER_UNIT) - 1);
3599 desc->next = NULL;
3600 desc->constant = tmp.constant;
3601 desc->offset = pool->offset;
3602 desc->hash = hash;
3603 desc->mode = mode;
3604 desc->align = align;
3605 desc->labelno = const_labelno;
3606 desc->mark = 0;
3608 pool->offset += GET_MODE_SIZE (mode);
3609 if (pool->last)
3610 pool->last->next = desc;
3611 else
3612 pool->first = pool->last = desc;
3613 pool->last = desc;
3615 /* Create a string containing the label name, in LABEL. */
3616 ASM_GENERATE_INTERNAL_LABEL (label, "LC", const_labelno);
3617 ++const_labelno;
3619 /* Construct the SYMBOL_REF. Make sure to mark it as belonging to
3620 the constants pool. */
3621 if (use_object_blocks_p () && targetm.use_blocks_for_constant_p (mode, x))
3623 section *sect = targetm.asm_out.select_rtx_section (mode, x, align);
3624 symbol = create_block_symbol (ggc_strdup (label),
3625 get_block_for_section (sect), -1);
3627 else
3628 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3629 desc->sym = symbol;
3630 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3631 CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3632 SET_SYMBOL_REF_CONSTANT (symbol, desc);
3634 /* Construct the MEM. */
3635 desc->mem = def = gen_const_mem (mode, symbol);
3636 set_mem_attributes (def, lang_hooks.types.type_for_mode (mode, 0), 1);
3637 set_mem_align (def, align);
3639 /* If we're dropping a label to the constant pool, make sure we
3640 don't delete it. */
3641 if (GET_CODE (x) == LABEL_REF)
3642 LABEL_PRESERVE_P (XEXP (x, 0)) = 1;
3644 return copy_rtx (def);
3647 /* Given a constant pool SYMBOL_REF, return the corresponding constant. */
3650 get_pool_constant (rtx addr)
3652 return SYMBOL_REF_CONSTANT (addr)->constant;
3655 /* Given a constant pool SYMBOL_REF, return the corresponding constant
3656 and whether it has been output or not. */
3659 get_pool_constant_mark (rtx addr, bool *pmarked)
3661 struct constant_descriptor_rtx *desc;
3663 desc = SYMBOL_REF_CONSTANT (addr);
3664 *pmarked = (desc->mark != 0);
3665 return desc->constant;
3668 /* Similar, return the mode. */
3670 enum machine_mode
3671 get_pool_mode (const_rtx addr)
3673 return SYMBOL_REF_CONSTANT (addr)->mode;
3676 /* Return the size of the constant pool. */
3679 get_pool_size (void)
3681 return crtl->varasm.pool->offset;
3684 /* Worker function for output_constant_pool_1. Emit assembly for X
3685 in MODE with known alignment ALIGN. */
3687 static void
3688 output_constant_pool_2 (enum machine_mode mode, rtx x, unsigned int align)
3690 switch (GET_MODE_CLASS (mode))
3692 case MODE_FLOAT:
3693 case MODE_DECIMAL_FLOAT:
3695 REAL_VALUE_TYPE r;
3697 gcc_assert (GET_CODE (x) == CONST_DOUBLE);
3698 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
3699 assemble_real (r, mode, align);
3700 break;
3703 case MODE_INT:
3704 case MODE_PARTIAL_INT:
3705 case MODE_FRACT:
3706 case MODE_UFRACT:
3707 case MODE_ACCUM:
3708 case MODE_UACCUM:
3709 assemble_integer (x, GET_MODE_SIZE (mode), align, 1);
3710 break;
3712 case MODE_VECTOR_FLOAT:
3713 case MODE_VECTOR_INT:
3714 case MODE_VECTOR_FRACT:
3715 case MODE_VECTOR_UFRACT:
3716 case MODE_VECTOR_ACCUM:
3717 case MODE_VECTOR_UACCUM:
3719 int i, units;
3720 enum machine_mode submode = GET_MODE_INNER (mode);
3721 unsigned int subalign = MIN (align, GET_MODE_BITSIZE (submode));
3723 gcc_assert (GET_CODE (x) == CONST_VECTOR);
3724 units = CONST_VECTOR_NUNITS (x);
3726 for (i = 0; i < units; i++)
3728 rtx elt = CONST_VECTOR_ELT (x, i);
3729 output_constant_pool_2 (submode, elt, i ? subalign : align);
3732 break;
3734 default:
3735 gcc_unreachable ();
3739 /* Worker function for output_constant_pool. Emit constant DESC,
3740 giving it ALIGN bits of alignment. */
3742 static void
3743 output_constant_pool_1 (struct constant_descriptor_rtx *desc,
3744 unsigned int align)
3746 rtx x, tmp;
3748 x = desc->constant;
3750 /* See if X is a LABEL_REF (or a CONST referring to a LABEL_REF)
3751 whose CODE_LABEL has been deleted. This can occur if a jump table
3752 is eliminated by optimization. If so, write a constant of zero
3753 instead. Note that this can also happen by turning the
3754 CODE_LABEL into a NOTE. */
3755 /* ??? This seems completely and utterly wrong. Certainly it's
3756 not true for NOTE_INSN_DELETED_LABEL, but I disbelieve proper
3757 functioning even with INSN_DELETED_P and friends. */
3759 tmp = x;
3760 switch (GET_CODE (tmp))
3762 case CONST:
3763 if (GET_CODE (XEXP (tmp, 0)) != PLUS
3764 || GET_CODE (XEXP (XEXP (tmp, 0), 0)) != LABEL_REF)
3765 break;
3766 tmp = XEXP (XEXP (tmp, 0), 0);
3767 /* FALLTHRU */
3769 case LABEL_REF:
3770 tmp = XEXP (tmp, 0);
3771 gcc_assert (!INSN_DELETED_P (tmp));
3772 gcc_assert (!NOTE_P (tmp)
3773 || NOTE_KIND (tmp) != NOTE_INSN_DELETED);
3774 break;
3776 default:
3777 break;
3780 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3781 ASM_OUTPUT_SPECIAL_POOL_ENTRY (asm_out_file, x, desc->mode,
3782 align, desc->labelno, done);
3783 #endif
3785 assemble_align (align);
3787 /* Output the label. */
3788 targetm.asm_out.internal_label (asm_out_file, "LC", desc->labelno);
3790 /* Output the data. */
3791 output_constant_pool_2 (desc->mode, x, align);
3793 /* Make sure all constants in SECTION_MERGE and not SECTION_STRINGS
3794 sections have proper size. */
3795 if (align > GET_MODE_BITSIZE (desc->mode)
3796 && in_section
3797 && (in_section->common.flags & SECTION_MERGE))
3798 assemble_align (align);
3800 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3801 done:
3802 #endif
3803 return;
3806 /* Given a SYMBOL_REF CURRENT_RTX, mark it and all constants it refers
3807 to as used. Emit referenced deferred strings. This function can
3808 be used with for_each_rtx to mark all SYMBOL_REFs in an rtx. */
3810 static int
3811 mark_constant (rtx *current_rtx, void *data ATTRIBUTE_UNUSED)
3813 rtx x = *current_rtx;
3815 if (x == NULL_RTX || GET_CODE (x) != SYMBOL_REF)
3816 return 0;
3818 if (CONSTANT_POOL_ADDRESS_P (x))
3820 struct constant_descriptor_rtx *desc = SYMBOL_REF_CONSTANT (x);
3821 if (desc->mark == 0)
3823 desc->mark = 1;
3824 for_each_rtx (&desc->constant, mark_constant, NULL);
3827 else if (TREE_CONSTANT_POOL_ADDRESS_P (x))
3829 tree exp = SYMBOL_REF_DECL (x);
3830 if (!TREE_ASM_WRITTEN (exp))
3832 n_deferred_constants--;
3833 output_constant_def_contents (x);
3837 return -1;
3840 /* Look through appropriate parts of INSN, marking all entries in the
3841 constant pool which are actually being used. Entries that are only
3842 referenced by other constants are also marked as used. Emit
3843 deferred strings that are used. */
3845 static void
3846 mark_constants (rtx insn)
3848 if (!INSN_P (insn))
3849 return;
3851 /* Insns may appear inside a SEQUENCE. Only check the patterns of
3852 insns, not any notes that may be attached. We don't want to mark
3853 a constant just because it happens to appear in a REG_EQUIV note. */
3854 if (GET_CODE (PATTERN (insn)) == SEQUENCE)
3856 rtx seq = PATTERN (insn);
3857 int i, n = XVECLEN (seq, 0);
3858 for (i = 0; i < n; ++i)
3860 rtx subinsn = XVECEXP (seq, 0, i);
3861 if (INSN_P (subinsn))
3862 for_each_rtx (&PATTERN (subinsn), mark_constant, NULL);
3865 else
3866 for_each_rtx (&PATTERN (insn), mark_constant, NULL);
3869 /* Look through the instructions for this function, and mark all the
3870 entries in POOL which are actually being used. Emit deferred constants
3871 which have indeed been used. */
3873 static void
3874 mark_constant_pool (void)
3876 rtx insn, link;
3878 if (!crtl->uses_const_pool && n_deferred_constants == 0)
3879 return;
3881 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3882 mark_constants (insn);
3884 for (link = crtl->epilogue_delay_list;
3885 link;
3886 link = XEXP (link, 1))
3887 mark_constants (XEXP (link, 0));
3890 /* Write all the constants in POOL. */
3892 static void
3893 output_constant_pool_contents (struct rtx_constant_pool *pool)
3895 struct constant_descriptor_rtx *desc;
3897 for (desc = pool->first; desc ; desc = desc->next)
3898 if (desc->mark)
3900 /* If the constant is part of an object_block, make sure that
3901 the constant has been positioned within its block, but do not
3902 write out its definition yet. output_object_blocks will do
3903 that later. */
3904 if (SYMBOL_REF_HAS_BLOCK_INFO_P (desc->sym)
3905 && SYMBOL_REF_BLOCK (desc->sym))
3906 place_block_symbol (desc->sym);
3907 else
3909 switch_to_section (targetm.asm_out.select_rtx_section
3910 (desc->mode, desc->constant, desc->align));
3911 output_constant_pool_1 (desc, desc->align);
3916 /* Mark all constants that are used in the current function, then write
3917 out the function's private constant pool. */
3919 static void
3920 output_constant_pool (const char *fnname ATTRIBUTE_UNUSED,
3921 tree fndecl ATTRIBUTE_UNUSED)
3923 struct rtx_constant_pool *pool = crtl->varasm.pool;
3925 /* It is possible for gcc to call force_const_mem and then to later
3926 discard the instructions which refer to the constant. In such a
3927 case we do not need to output the constant. */
3928 mark_constant_pool ();
3930 #ifdef ASM_OUTPUT_POOL_PROLOGUE
3931 ASM_OUTPUT_POOL_PROLOGUE (asm_out_file, fnname, fndecl, pool->offset);
3932 #endif
3934 output_constant_pool_contents (pool);
3936 #ifdef ASM_OUTPUT_POOL_EPILOGUE
3937 ASM_OUTPUT_POOL_EPILOGUE (asm_out_file, fnname, fndecl, pool->offset);
3938 #endif
3941 /* Write the contents of the shared constant pool. */
3943 void
3944 output_shared_constant_pool (void)
3946 output_constant_pool_contents (shared_constant_pool);
3949 /* Determine what kind of relocations EXP may need. */
3952 compute_reloc_for_constant (tree exp)
3954 int reloc = 0, reloc2;
3955 tree tem;
3957 switch (TREE_CODE (exp))
3959 case ADDR_EXPR:
3960 case FDESC_EXPR:
3961 /* Go inside any operations that get_inner_reference can handle and see
3962 if what's inside is a constant: no need to do anything here for
3963 addresses of variables or functions. */
3964 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
3965 tem = TREE_OPERAND (tem, 0))
3968 if (TREE_PUBLIC (tem))
3969 reloc |= 2;
3970 else
3971 reloc |= 1;
3972 break;
3974 case PLUS_EXPR:
3975 case POINTER_PLUS_EXPR:
3976 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3977 reloc |= compute_reloc_for_constant (TREE_OPERAND (exp, 1));
3978 break;
3980 case MINUS_EXPR:
3981 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3982 reloc2 = compute_reloc_for_constant (TREE_OPERAND (exp, 1));
3983 /* The difference of two local labels is computable at link time. */
3984 if (reloc == 1 && reloc2 == 1)
3985 reloc = 0;
3986 else
3987 reloc |= reloc2;
3988 break;
3990 CASE_CONVERT:
3991 case VIEW_CONVERT_EXPR:
3992 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3993 break;
3995 case CONSTRUCTOR:
3997 unsigned HOST_WIDE_INT idx;
3998 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
3999 if (tem != 0)
4000 reloc |= compute_reloc_for_constant (tem);
4002 break;
4004 default:
4005 break;
4007 return reloc;
4010 /* Find all the constants whose addresses are referenced inside of EXP,
4011 and make sure assembler code with a label has been output for each one.
4012 Indicate whether an ADDR_EXPR has been encountered. */
4014 static void
4015 output_addressed_constants (tree exp)
4017 tree tem;
4019 switch (TREE_CODE (exp))
4021 case ADDR_EXPR:
4022 case FDESC_EXPR:
4023 /* Go inside any operations that get_inner_reference can handle and see
4024 if what's inside is a constant: no need to do anything here for
4025 addresses of variables or functions. */
4026 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
4027 tem = TREE_OPERAND (tem, 0))
4030 /* If we have an initialized CONST_DECL, retrieve the initializer. */
4031 if (TREE_CODE (tem) == CONST_DECL && DECL_INITIAL (tem))
4032 tem = DECL_INITIAL (tem);
4034 if (CONSTANT_CLASS_P (tem) || TREE_CODE (tem) == CONSTRUCTOR)
4035 output_constant_def (tem, 0);
4036 break;
4038 case PLUS_EXPR:
4039 case POINTER_PLUS_EXPR:
4040 case MINUS_EXPR:
4041 output_addressed_constants (TREE_OPERAND (exp, 1));
4042 /* Fall through. */
4044 CASE_CONVERT:
4045 case VIEW_CONVERT_EXPR:
4046 output_addressed_constants (TREE_OPERAND (exp, 0));
4047 break;
4049 case CONSTRUCTOR:
4051 unsigned HOST_WIDE_INT idx;
4052 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
4053 if (tem != 0)
4054 output_addressed_constants (tem);
4056 break;
4058 default:
4059 break;
4063 /* Whether a constructor CTOR is a valid static constant initializer if all
4064 its elements are. This used to be internal to initializer_constant_valid_p
4065 and has been exposed to let other functions like categorize_ctor_elements
4066 evaluate the property while walking a constructor for other purposes. */
4068 bool
4069 constructor_static_from_elts_p (const_tree ctor)
4071 return (TREE_CONSTANT (ctor)
4072 && (TREE_CODE (TREE_TYPE (ctor)) == UNION_TYPE
4073 || TREE_CODE (TREE_TYPE (ctor)) == RECORD_TYPE)
4074 && !VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (ctor)));
4077 /* A subroutine of initializer_constant_valid_p. VALUE is a MINUS_EXPR,
4078 PLUS_EXPR or POINTER_PLUS_EXPR. This looks for cases of VALUE
4079 which are valid when ENDTYPE is an integer of any size; in
4080 particular, this does not accept a pointer minus a constant. This
4081 returns null_pointer_node if the VALUE is an absolute constant
4082 which can be used to initialize a static variable. Otherwise it
4083 returns NULL. */
4085 static tree
4086 narrowing_initializer_constant_valid_p (tree value, tree endtype)
4088 tree op0, op1;
4090 if (!INTEGRAL_TYPE_P (endtype))
4091 return NULL_TREE;
4093 op0 = TREE_OPERAND (value, 0);
4094 op1 = TREE_OPERAND (value, 1);
4096 /* Like STRIP_NOPS except allow the operand mode to widen. This
4097 works around a feature of fold that simplifies (int)(p1 - p2) to
4098 ((int)p1 - (int)p2) under the theory that the narrower operation
4099 is cheaper. */
4101 while (CONVERT_EXPR_P (op0)
4102 || TREE_CODE (op0) == NON_LVALUE_EXPR)
4104 tree inner = TREE_OPERAND (op0, 0);
4105 if (inner == error_mark_node
4106 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4107 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))
4108 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
4109 break;
4110 op0 = inner;
4113 while (CONVERT_EXPR_P (op1)
4114 || TREE_CODE (op1) == NON_LVALUE_EXPR)
4116 tree inner = TREE_OPERAND (op1, 0);
4117 if (inner == error_mark_node
4118 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4119 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op1)))
4120 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
4121 break;
4122 op1 = inner;
4125 op0 = initializer_constant_valid_p (op0, endtype);
4126 op1 = initializer_constant_valid_p (op1, endtype);
4128 /* Both initializers must be known. */
4129 if (op0 && op1)
4131 if (op0 == op1
4132 && (op0 == null_pointer_node
4133 || TREE_CODE (value) == MINUS_EXPR))
4134 return null_pointer_node;
4136 /* Support differences between labels. */
4137 if (TREE_CODE (op0) == LABEL_DECL
4138 && TREE_CODE (op1) == LABEL_DECL)
4139 return null_pointer_node;
4141 if (TREE_CODE (op0) == STRING_CST
4142 && TREE_CODE (op1) == STRING_CST
4143 && operand_equal_p (op0, op1, 1))
4144 return null_pointer_node;
4147 return NULL_TREE;
4150 /* Return nonzero if VALUE is a valid constant-valued expression
4151 for use in initializing a static variable; one that can be an
4152 element of a "constant" initializer.
4154 Return null_pointer_node if the value is absolute;
4155 if it is relocatable, return the variable that determines the relocation.
4156 We assume that VALUE has been folded as much as possible;
4157 therefore, we do not need to check for such things as
4158 arithmetic-combinations of integers. */
4160 tree
4161 initializer_constant_valid_p (tree value, tree endtype)
4163 tree ret;
4165 switch (TREE_CODE (value))
4167 case CONSTRUCTOR:
4168 if (constructor_static_from_elts_p (value))
4170 unsigned HOST_WIDE_INT idx;
4171 tree elt;
4172 bool absolute = true;
4174 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
4176 tree reloc;
4177 reloc = initializer_constant_valid_p (elt, TREE_TYPE (elt));
4178 if (!reloc)
4179 return NULL_TREE;
4180 if (reloc != null_pointer_node)
4181 absolute = false;
4183 /* For a non-absolute relocation, there is no single
4184 variable that can be "the variable that determines the
4185 relocation." */
4186 return absolute ? null_pointer_node : error_mark_node;
4189 return TREE_STATIC (value) ? null_pointer_node : NULL_TREE;
4191 case INTEGER_CST:
4192 case VECTOR_CST:
4193 case REAL_CST:
4194 case FIXED_CST:
4195 case STRING_CST:
4196 case COMPLEX_CST:
4197 return null_pointer_node;
4199 case ADDR_EXPR:
4200 case FDESC_EXPR:
4202 tree op0 = staticp (TREE_OPERAND (value, 0));
4203 if (op0)
4205 /* "&(*a).f" is like unto pointer arithmetic. If "a" turns out
4206 to be a constant, this is old-skool offsetof-like nonsense. */
4207 if (TREE_CODE (op0) == INDIRECT_REF
4208 && TREE_CONSTANT (TREE_OPERAND (op0, 0)))
4209 return null_pointer_node;
4210 /* Taking the address of a nested function involves a trampoline,
4211 unless we don't need or want one. */
4212 if (TREE_CODE (op0) == FUNCTION_DECL
4213 && decl_function_context (op0)
4214 && !DECL_NO_STATIC_CHAIN (op0)
4215 && !TREE_NO_TRAMPOLINE (value))
4216 return NULL_TREE;
4217 /* "&{...}" requires a temporary to hold the constructed
4218 object. */
4219 if (TREE_CODE (op0) == CONSTRUCTOR)
4220 return NULL_TREE;
4222 return op0;
4225 case NON_LVALUE_EXPR:
4226 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4228 case VIEW_CONVERT_EXPR:
4230 tree src = TREE_OPERAND (value, 0);
4231 tree src_type = TREE_TYPE (src);
4232 tree dest_type = TREE_TYPE (value);
4234 /* Allow view-conversions from aggregate to non-aggregate type only
4235 if the bit pattern is fully preserved afterwards; otherwise, the
4236 RTL expander won't be able to apply a subsequent transformation
4237 to the underlying constructor. */
4238 if (AGGREGATE_TYPE_P (src_type) && !AGGREGATE_TYPE_P (dest_type))
4240 if (TYPE_MODE (endtype) == TYPE_MODE (dest_type))
4241 return initializer_constant_valid_p (src, endtype);
4242 else
4243 return NULL_TREE;
4246 /* Allow all other kinds of view-conversion. */
4247 return initializer_constant_valid_p (src, endtype);
4250 CASE_CONVERT:
4252 tree src = TREE_OPERAND (value, 0);
4253 tree src_type = TREE_TYPE (src);
4254 tree dest_type = TREE_TYPE (value);
4256 /* Allow conversions between pointer types, floating-point
4257 types, and offset types. */
4258 if ((POINTER_TYPE_P (dest_type) && POINTER_TYPE_P (src_type))
4259 || (FLOAT_TYPE_P (dest_type) && FLOAT_TYPE_P (src_type))
4260 || (TREE_CODE (dest_type) == OFFSET_TYPE
4261 && TREE_CODE (src_type) == OFFSET_TYPE))
4262 return initializer_constant_valid_p (src, endtype);
4264 /* Allow length-preserving conversions between integer types. */
4265 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type)
4266 && (TYPE_PRECISION (dest_type) == TYPE_PRECISION (src_type)))
4267 return initializer_constant_valid_p (src, endtype);
4269 /* Allow conversions between other integer types only if
4270 explicit value. */
4271 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type))
4273 tree inner = initializer_constant_valid_p (src, endtype);
4274 if (inner == null_pointer_node)
4275 return null_pointer_node;
4276 break;
4279 /* Allow (int) &foo provided int is as wide as a pointer. */
4280 if (INTEGRAL_TYPE_P (dest_type) && POINTER_TYPE_P (src_type)
4281 && (TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type)))
4282 return initializer_constant_valid_p (src, endtype);
4284 /* Likewise conversions from int to pointers, but also allow
4285 conversions from 0. */
4286 if ((POINTER_TYPE_P (dest_type)
4287 || TREE_CODE (dest_type) == OFFSET_TYPE)
4288 && INTEGRAL_TYPE_P (src_type))
4290 if (TREE_CODE (src) == INTEGER_CST
4291 && TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type))
4292 return null_pointer_node;
4293 if (integer_zerop (src))
4294 return null_pointer_node;
4295 else if (TYPE_PRECISION (dest_type) <= TYPE_PRECISION (src_type))
4296 return initializer_constant_valid_p (src, endtype);
4299 /* Allow conversions to struct or union types if the value
4300 inside is okay. */
4301 if (TREE_CODE (dest_type) == RECORD_TYPE
4302 || TREE_CODE (dest_type) == UNION_TYPE)
4303 return initializer_constant_valid_p (src, endtype);
4305 break;
4307 case POINTER_PLUS_EXPR:
4308 case PLUS_EXPR:
4309 if (! INTEGRAL_TYPE_P (endtype)
4310 || TYPE_PRECISION (endtype) >= POINTER_SIZE)
4312 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
4313 endtype);
4314 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
4315 endtype);
4316 /* If either term is absolute, use the other term's relocation. */
4317 if (valid0 == null_pointer_node)
4318 return valid1;
4319 if (valid1 == null_pointer_node)
4320 return valid0;
4323 /* Support narrowing pointer differences. */
4324 ret = narrowing_initializer_constant_valid_p (value, endtype);
4325 if (ret != NULL_TREE)
4326 return ret;
4328 break;
4330 case MINUS_EXPR:
4331 if (! INTEGRAL_TYPE_P (endtype)
4332 || TYPE_PRECISION (endtype) >= POINTER_SIZE)
4334 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
4335 endtype);
4336 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
4337 endtype);
4338 /* Win if second argument is absolute. */
4339 if (valid1 == null_pointer_node)
4340 return valid0;
4341 /* Win if both arguments have the same relocation.
4342 Then the value is absolute. */
4343 if (valid0 == valid1 && valid0 != 0)
4344 return null_pointer_node;
4346 /* Since GCC guarantees that string constants are unique in the
4347 generated code, a subtraction between two copies of the same
4348 constant string is absolute. */
4349 if (valid0 && TREE_CODE (valid0) == STRING_CST
4350 && valid1 && TREE_CODE (valid1) == STRING_CST
4351 && operand_equal_p (valid0, valid1, 1))
4352 return null_pointer_node;
4355 /* Support narrowing differences. */
4356 ret = narrowing_initializer_constant_valid_p (value, endtype);
4357 if (ret != NULL_TREE)
4358 return ret;
4360 break;
4362 default:
4363 break;
4366 return 0;
4369 /* Output assembler code for constant EXP to FILE, with no label.
4370 This includes the pseudo-op such as ".int" or ".byte", and a newline.
4371 Assumes output_addressed_constants has been done on EXP already.
4373 Generate exactly SIZE bytes of assembler data, padding at the end
4374 with zeros if necessary. SIZE must always be specified.
4376 SIZE is important for structure constructors,
4377 since trailing members may have been omitted from the constructor.
4378 It is also important for initialization of arrays from string constants
4379 since the full length of the string constant might not be wanted.
4380 It is also needed for initialization of unions, where the initializer's
4381 type is just one member, and that may not be as long as the union.
4383 There a case in which we would fail to output exactly SIZE bytes:
4384 for a structure constructor that wants to produce more than SIZE bytes.
4385 But such constructors will never be generated for any possible input.
4387 ALIGN is the alignment of the data in bits. */
4389 void
4390 output_constant (tree exp, unsigned HOST_WIDE_INT size, unsigned int align)
4392 enum tree_code code;
4393 unsigned HOST_WIDE_INT thissize;
4395 if (size == 0 || flag_syntax_only)
4396 return;
4398 /* See if we're trying to initialize a pointer in a non-default mode
4399 to the address of some declaration somewhere. If the target says
4400 the mode is valid for pointers, assume the target has a way of
4401 resolving it. */
4402 if (TREE_CODE (exp) == NOP_EXPR
4403 && POINTER_TYPE_P (TREE_TYPE (exp))
4404 && targetm.valid_pointer_mode (TYPE_MODE (TREE_TYPE (exp))))
4406 tree saved_type = TREE_TYPE (exp);
4408 /* Peel off any intermediate conversions-to-pointer for valid
4409 pointer modes. */
4410 while (TREE_CODE (exp) == NOP_EXPR
4411 && POINTER_TYPE_P (TREE_TYPE (exp))
4412 && targetm.valid_pointer_mode (TYPE_MODE (TREE_TYPE (exp))))
4413 exp = TREE_OPERAND (exp, 0);
4415 /* If what we're left with is the address of something, we can
4416 convert the address to the final type and output it that
4417 way. */
4418 if (TREE_CODE (exp) == ADDR_EXPR)
4419 exp = build1 (ADDR_EXPR, saved_type, TREE_OPERAND (exp, 0));
4420 /* Likewise for constant ints. */
4421 else if (TREE_CODE (exp) == INTEGER_CST)
4422 exp = build_int_cst_wide (saved_type, TREE_INT_CST_LOW (exp),
4423 TREE_INT_CST_HIGH (exp));
4427 /* Eliminate any conversions since we'll be outputting the underlying
4428 constant. */
4429 while (CONVERT_EXPR_P (exp)
4430 || TREE_CODE (exp) == NON_LVALUE_EXPR
4431 || TREE_CODE (exp) == VIEW_CONVERT_EXPR)
4433 HOST_WIDE_INT type_size = int_size_in_bytes (TREE_TYPE (exp));
4434 HOST_WIDE_INT op_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (exp, 0)));
4436 /* Make sure eliminating the conversion is really a no-op, except with
4437 VIEW_CONVERT_EXPRs to allow for wild Ada unchecked conversions and
4438 union types to allow for Ada unchecked unions. */
4439 if (type_size > op_size
4440 && TREE_CODE (exp) != VIEW_CONVERT_EXPR
4441 && TREE_CODE (TREE_TYPE (exp)) != UNION_TYPE)
4442 /* Keep the conversion. */
4443 break;
4444 else
4445 exp = TREE_OPERAND (exp, 0);
4448 code = TREE_CODE (TREE_TYPE (exp));
4449 thissize = int_size_in_bytes (TREE_TYPE (exp));
4451 /* Allow a constructor with no elements for any data type.
4452 This means to fill the space with zeros. */
4453 if (TREE_CODE (exp) == CONSTRUCTOR
4454 && VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (exp)))
4456 assemble_zeros (size);
4457 return;
4460 if (TREE_CODE (exp) == FDESC_EXPR)
4462 #ifdef ASM_OUTPUT_FDESC
4463 HOST_WIDE_INT part = tree_low_cst (TREE_OPERAND (exp, 1), 0);
4464 tree decl = TREE_OPERAND (exp, 0);
4465 ASM_OUTPUT_FDESC (asm_out_file, decl, part);
4466 #else
4467 gcc_unreachable ();
4468 #endif
4469 return;
4472 /* Now output the underlying data. If we've handling the padding, return.
4473 Otherwise, break and ensure SIZE is the size written. */
4474 switch (code)
4476 case BOOLEAN_TYPE:
4477 case INTEGER_TYPE:
4478 case ENUMERAL_TYPE:
4479 case POINTER_TYPE:
4480 case REFERENCE_TYPE:
4481 case OFFSET_TYPE:
4482 case FIXED_POINT_TYPE:
4483 if (! assemble_integer (expand_expr (exp, NULL_RTX, VOIDmode,
4484 EXPAND_INITIALIZER),
4485 MIN (size, thissize), align, 0))
4486 error ("initializer for integer/fixed-point value is too complicated");
4487 break;
4489 case REAL_TYPE:
4490 if (TREE_CODE (exp) != REAL_CST)
4491 error ("initializer for floating value is not a floating constant");
4493 assemble_real (TREE_REAL_CST (exp), TYPE_MODE (TREE_TYPE (exp)), align);
4494 break;
4496 case COMPLEX_TYPE:
4497 output_constant (TREE_REALPART (exp), thissize / 2, align);
4498 output_constant (TREE_IMAGPART (exp), thissize / 2,
4499 min_align (align, BITS_PER_UNIT * (thissize / 2)));
4500 break;
4502 case ARRAY_TYPE:
4503 case VECTOR_TYPE:
4504 switch (TREE_CODE (exp))
4506 case CONSTRUCTOR:
4507 output_constructor (exp, size, align);
4508 return;
4509 case STRING_CST:
4510 thissize = MIN ((unsigned HOST_WIDE_INT)TREE_STRING_LENGTH (exp),
4511 size);
4512 assemble_string (TREE_STRING_POINTER (exp), thissize);
4513 break;
4515 case VECTOR_CST:
4517 int elt_size;
4518 tree link;
4519 unsigned int nalign;
4520 enum machine_mode inner;
4522 inner = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
4523 nalign = MIN (align, GET_MODE_ALIGNMENT (inner));
4525 elt_size = GET_MODE_SIZE (inner);
4527 link = TREE_VECTOR_CST_ELTS (exp);
4528 output_constant (TREE_VALUE (link), elt_size, align);
4529 thissize = elt_size;
4530 while ((link = TREE_CHAIN (link)) != NULL)
4532 output_constant (TREE_VALUE (link), elt_size, nalign);
4533 thissize += elt_size;
4535 break;
4537 default:
4538 gcc_unreachable ();
4540 break;
4542 case RECORD_TYPE:
4543 case UNION_TYPE:
4544 gcc_assert (TREE_CODE (exp) == CONSTRUCTOR);
4545 output_constructor (exp, size, align);
4546 return;
4548 case ERROR_MARK:
4549 return;
4551 default:
4552 gcc_unreachable ();
4555 if (size > thissize)
4556 assemble_zeros (size - thissize);
4560 /* Subroutine of output_constructor, used for computing the size of
4561 arrays of unspecified length. VAL must be a CONSTRUCTOR of an array
4562 type with an unspecified upper bound. */
4564 static unsigned HOST_WIDE_INT
4565 array_size_for_constructor (tree val)
4567 tree max_index, i;
4568 unsigned HOST_WIDE_INT cnt;
4569 tree index, value, tmp;
4571 /* This code used to attempt to handle string constants that are not
4572 arrays of single-bytes, but nothing else does, so there's no point in
4573 doing it here. */
4574 if (TREE_CODE (val) == STRING_CST)
4575 return TREE_STRING_LENGTH (val);
4577 max_index = NULL_TREE;
4578 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (val), cnt, index, value)
4580 if (TREE_CODE (index) == RANGE_EXPR)
4581 index = TREE_OPERAND (index, 1);
4582 if (max_index == NULL_TREE || tree_int_cst_lt (max_index, index))
4583 max_index = index;
4586 if (max_index == NULL_TREE)
4587 return 0;
4589 /* Compute the total number of array elements. */
4590 tmp = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (val)));
4591 i = size_binop (MINUS_EXPR, fold_convert (sizetype, max_index),
4592 fold_convert (sizetype, tmp));
4593 i = size_binop (PLUS_EXPR, i, build_int_cst (sizetype, 1));
4595 /* Multiply by the array element unit size to find number of bytes. */
4596 i = size_binop (MULT_EXPR, i, TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (val))));
4598 return tree_low_cst (i, 1);
4601 /* Subroutine of output_constant, used for CONSTRUCTORs (aggregate constants).
4602 Generate at least SIZE bytes, padding if necessary. */
4604 static void
4605 output_constructor (tree exp, unsigned HOST_WIDE_INT size,
4606 unsigned int align)
4608 tree type = TREE_TYPE (exp);
4609 tree field = 0;
4610 tree min_index = 0;
4611 /* Number of bytes output or skipped so far.
4612 In other words, current position within the constructor. */
4613 HOST_WIDE_INT total_bytes = 0;
4614 /* Nonzero means BYTE contains part of a byte, to be output. */
4615 int byte_buffer_in_use = 0;
4616 int byte = 0;
4617 unsigned HOST_WIDE_INT cnt;
4618 constructor_elt *ce;
4620 gcc_assert (HOST_BITS_PER_WIDE_INT >= BITS_PER_UNIT);
4622 if (TREE_CODE (type) == RECORD_TYPE)
4623 field = TYPE_FIELDS (type);
4625 if (TREE_CODE (type) == ARRAY_TYPE
4626 && TYPE_DOMAIN (type) != 0)
4627 min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
4629 /* As LINK goes through the elements of the constant,
4630 FIELD goes through the structure fields, if the constant is a structure.
4631 if the constant is a union, then we override this,
4632 by getting the field from the TREE_LIST element.
4633 But the constant could also be an array. Then FIELD is zero.
4635 There is always a maximum of one element in the chain LINK for unions
4636 (even if the initializer in a source program incorrectly contains
4637 more one). */
4638 for (cnt = 0;
4639 VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (exp), cnt, ce);
4640 cnt++, field = field ? TREE_CHAIN (field) : 0)
4642 tree val = ce->value;
4643 tree index = 0;
4645 /* The element in a union constructor specifies the proper field
4646 or index. */
4647 if ((TREE_CODE (type) == RECORD_TYPE || TREE_CODE (type) == UNION_TYPE
4648 || TREE_CODE (type) == QUAL_UNION_TYPE)
4649 && ce->index != 0)
4650 field = ce->index;
4652 else if (TREE_CODE (type) == ARRAY_TYPE)
4653 index = ce->index;
4655 #ifdef ASM_COMMENT_START
4656 if (field && flag_verbose_asm)
4657 fprintf (asm_out_file, "%s %s:\n",
4658 ASM_COMMENT_START,
4659 DECL_NAME (field)
4660 ? IDENTIFIER_POINTER (DECL_NAME (field))
4661 : "<anonymous>");
4662 #endif
4664 /* Eliminate the marker that makes a cast not be an lvalue. */
4665 if (val != 0)
4666 STRIP_NOPS (val);
4668 if (index && TREE_CODE (index) == RANGE_EXPR)
4670 unsigned HOST_WIDE_INT fieldsize
4671 = int_size_in_bytes (TREE_TYPE (type));
4672 HOST_WIDE_INT lo_index = tree_low_cst (TREE_OPERAND (index, 0), 0);
4673 HOST_WIDE_INT hi_index = tree_low_cst (TREE_OPERAND (index, 1), 0);
4674 HOST_WIDE_INT index;
4675 unsigned int align2 = min_align (align, fieldsize * BITS_PER_UNIT);
4677 for (index = lo_index; index <= hi_index; index++)
4679 /* Output the element's initial value. */
4680 if (val == 0)
4681 assemble_zeros (fieldsize);
4682 else
4683 output_constant (val, fieldsize, align2);
4685 /* Count its size. */
4686 total_bytes += fieldsize;
4689 else if (field == 0 || !DECL_BIT_FIELD (field))
4691 /* An element that is not a bit-field. */
4693 unsigned HOST_WIDE_INT fieldsize;
4694 /* Since this structure is static,
4695 we know the positions are constant. */
4696 HOST_WIDE_INT pos = field ? int_byte_position (field) : 0;
4697 unsigned int align2;
4699 if (index != 0)
4700 pos = (tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (val)), 1)
4701 * (tree_low_cst (index, 0) - tree_low_cst (min_index, 0)));
4703 /* Output any buffered-up bit-fields preceding this element. */
4704 if (byte_buffer_in_use)
4706 assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
4707 total_bytes++;
4708 byte_buffer_in_use = 0;
4711 /* Advance to offset of this element.
4712 Note no alignment needed in an array, since that is guaranteed
4713 if each element has the proper size. */
4714 if ((field != 0 || index != 0) && pos != total_bytes)
4716 gcc_assert (pos >= total_bytes);
4717 assemble_zeros (pos - total_bytes);
4718 total_bytes = pos;
4721 /* Find the alignment of this element. */
4722 align2 = min_align (align, BITS_PER_UNIT * pos);
4724 /* Determine size this element should occupy. */
4725 if (field)
4727 fieldsize = 0;
4729 /* If this is an array with an unspecified upper bound,
4730 the initializer determines the size. */
4731 /* ??? This ought to only checked if DECL_SIZE_UNIT is NULL,
4732 but we cannot do this until the deprecated support for
4733 initializing zero-length array members is removed. */
4734 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
4735 && TYPE_DOMAIN (TREE_TYPE (field))
4736 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
4738 fieldsize = array_size_for_constructor (val);
4739 /* Given a non-empty initialization, this field had
4740 better be last. */
4741 gcc_assert (!fieldsize || !TREE_CHAIN (field));
4743 else if (DECL_SIZE_UNIT (field))
4745 /* ??? This can't be right. If the decl size overflows
4746 a host integer we will silently emit no data. */
4747 if (host_integerp (DECL_SIZE_UNIT (field), 1))
4748 fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 1);
4751 else
4752 fieldsize = int_size_in_bytes (TREE_TYPE (type));
4754 /* Output the element's initial value. */
4755 if (val == 0)
4756 assemble_zeros (fieldsize);
4757 else
4758 output_constant (val, fieldsize, align2);
4760 /* Count its size. */
4761 total_bytes += fieldsize;
4763 else if (val != 0 && TREE_CODE (val) != INTEGER_CST)
4764 error ("invalid initial value for member %qE",
4765 DECL_NAME (field));
4766 else
4768 /* Element that is a bit-field. */
4770 HOST_WIDE_INT next_offset = int_bit_position (field);
4771 HOST_WIDE_INT end_offset
4772 = (next_offset + tree_low_cst (DECL_SIZE (field), 1));
4774 if (val == 0)
4775 val = integer_zero_node;
4777 /* If this field does not start in this (or, next) byte,
4778 skip some bytes. */
4779 if (next_offset / BITS_PER_UNIT != total_bytes)
4781 /* Output remnant of any bit field in previous bytes. */
4782 if (byte_buffer_in_use)
4784 assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
4785 total_bytes++;
4786 byte_buffer_in_use = 0;
4789 /* If still not at proper byte, advance to there. */
4790 if (next_offset / BITS_PER_UNIT != total_bytes)
4792 gcc_assert (next_offset / BITS_PER_UNIT >= total_bytes);
4793 assemble_zeros (next_offset / BITS_PER_UNIT - total_bytes);
4794 total_bytes = next_offset / BITS_PER_UNIT;
4798 if (! byte_buffer_in_use)
4799 byte = 0;
4801 /* We must split the element into pieces that fall within
4802 separate bytes, and combine each byte with previous or
4803 following bit-fields. */
4805 /* next_offset is the offset n fbits from the beginning of
4806 the structure to the next bit of this element to be processed.
4807 end_offset is the offset of the first bit past the end of
4808 this element. */
4809 while (next_offset < end_offset)
4811 int this_time;
4812 int shift;
4813 HOST_WIDE_INT value;
4814 HOST_WIDE_INT next_byte = next_offset / BITS_PER_UNIT;
4815 HOST_WIDE_INT next_bit = next_offset % BITS_PER_UNIT;
4817 /* Advance from byte to byte
4818 within this element when necessary. */
4819 while (next_byte != total_bytes)
4821 assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
4822 total_bytes++;
4823 byte = 0;
4826 /* Number of bits we can process at once
4827 (all part of the same byte). */
4828 this_time = MIN (end_offset - next_offset,
4829 BITS_PER_UNIT - next_bit);
4830 if (BYTES_BIG_ENDIAN)
4832 /* On big-endian machine, take the most significant bits
4833 first (of the bits that are significant)
4834 and put them into bytes from the most significant end. */
4835 shift = end_offset - next_offset - this_time;
4837 /* Don't try to take a bunch of bits that cross
4838 the word boundary in the INTEGER_CST. We can
4839 only select bits from the LOW or HIGH part
4840 not from both. */
4841 if (shift < HOST_BITS_PER_WIDE_INT
4842 && shift + this_time > HOST_BITS_PER_WIDE_INT)
4844 this_time = shift + this_time - HOST_BITS_PER_WIDE_INT;
4845 shift = HOST_BITS_PER_WIDE_INT;
4848 /* Now get the bits from the appropriate constant word. */
4849 if (shift < HOST_BITS_PER_WIDE_INT)
4850 value = TREE_INT_CST_LOW (val);
4851 else
4853 gcc_assert (shift < 2 * HOST_BITS_PER_WIDE_INT);
4854 value = TREE_INT_CST_HIGH (val);
4855 shift -= HOST_BITS_PER_WIDE_INT;
4858 /* Get the result. This works only when:
4859 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
4860 byte |= (((value >> shift)
4861 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
4862 << (BITS_PER_UNIT - this_time - next_bit));
4864 else
4866 /* On little-endian machines,
4867 take first the least significant bits of the value
4868 and pack them starting at the least significant
4869 bits of the bytes. */
4870 shift = next_offset - int_bit_position (field);
4872 /* Don't try to take a bunch of bits that cross
4873 the word boundary in the INTEGER_CST. We can
4874 only select bits from the LOW or HIGH part
4875 not from both. */
4876 if (shift < HOST_BITS_PER_WIDE_INT
4877 && shift + this_time > HOST_BITS_PER_WIDE_INT)
4878 this_time = (HOST_BITS_PER_WIDE_INT - shift);
4880 /* Now get the bits from the appropriate constant word. */
4881 if (shift < HOST_BITS_PER_WIDE_INT)
4882 value = TREE_INT_CST_LOW (val);
4883 else
4885 gcc_assert (shift < 2 * HOST_BITS_PER_WIDE_INT);
4886 value = TREE_INT_CST_HIGH (val);
4887 shift -= HOST_BITS_PER_WIDE_INT;
4890 /* Get the result. This works only when:
4891 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
4892 byte |= (((value >> shift)
4893 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
4894 << next_bit);
4897 next_offset += this_time;
4898 byte_buffer_in_use = 1;
4903 if (byte_buffer_in_use)
4905 assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
4906 total_bytes++;
4909 if ((unsigned HOST_WIDE_INT)total_bytes < size)
4910 assemble_zeros (size - total_bytes);
4913 /* Mark DECL as weak. */
4915 static void
4916 mark_weak (tree decl)
4918 DECL_WEAK (decl) = 1;
4920 if (DECL_RTL_SET_P (decl)
4921 && MEM_P (DECL_RTL (decl))
4922 && XEXP (DECL_RTL (decl), 0)
4923 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == SYMBOL_REF)
4924 SYMBOL_REF_WEAK (XEXP (DECL_RTL (decl), 0)) = 1;
4927 /* Merge weak status between NEWDECL and OLDDECL. */
4929 void
4930 merge_weak (tree newdecl, tree olddecl)
4932 if (DECL_WEAK (newdecl) == DECL_WEAK (olddecl))
4934 if (DECL_WEAK (newdecl) && SUPPORTS_WEAK)
4936 tree *pwd;
4937 /* We put the NEWDECL on the weak_decls list at some point
4938 and OLDDECL as well. Keep just OLDDECL on the list. */
4939 for (pwd = &weak_decls; *pwd; pwd = &TREE_CHAIN (*pwd))
4940 if (TREE_VALUE (*pwd) == newdecl)
4942 *pwd = TREE_CHAIN (*pwd);
4943 break;
4946 return;
4949 if (DECL_WEAK (newdecl))
4951 tree wd;
4953 /* NEWDECL is weak, but OLDDECL is not. */
4955 /* If we already output the OLDDECL, we're in trouble; we can't
4956 go back and make it weak. This error cannot be caught in
4957 declare_weak because the NEWDECL and OLDDECL was not yet
4958 been merged; therefore, TREE_ASM_WRITTEN was not set. */
4959 if (TREE_ASM_WRITTEN (olddecl))
4960 error ("weak declaration of %q+D must precede definition",
4961 newdecl);
4963 /* If we've already generated rtl referencing OLDDECL, we may
4964 have done so in a way that will not function properly with
4965 a weak symbol. */
4966 else if (TREE_USED (olddecl)
4967 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (olddecl)))
4968 warning (0, "weak declaration of %q+D after first use results "
4969 "in unspecified behavior", newdecl);
4971 if (SUPPORTS_WEAK)
4973 /* We put the NEWDECL on the weak_decls list at some point.
4974 Replace it with the OLDDECL. */
4975 for (wd = weak_decls; wd; wd = TREE_CHAIN (wd))
4976 if (TREE_VALUE (wd) == newdecl)
4978 TREE_VALUE (wd) = olddecl;
4979 break;
4981 /* We may not find the entry on the list. If NEWDECL is a
4982 weak alias, then we will have already called
4983 globalize_decl to remove the entry; in that case, we do
4984 not need to do anything. */
4987 /* Make the OLDDECL weak; it's OLDDECL that we'll be keeping. */
4988 mark_weak (olddecl);
4990 else
4991 /* OLDDECL was weak, but NEWDECL was not explicitly marked as
4992 weak. Just update NEWDECL to indicate that it's weak too. */
4993 mark_weak (newdecl);
4996 /* Declare DECL to be a weak symbol. */
4998 void
4999 declare_weak (tree decl)
5001 if (! TREE_PUBLIC (decl))
5002 error ("weak declaration of %q+D must be public", decl);
5003 else if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
5004 error ("weak declaration of %q+D must precede definition", decl);
5005 else if (!SUPPORTS_WEAK)
5006 warning (0, "weak declaration of %q+D not supported", decl);
5008 mark_weak (decl);
5011 static void
5012 weak_finish_1 (tree decl)
5014 #if defined (ASM_WEAKEN_DECL) || defined (ASM_WEAKEN_LABEL)
5015 const char *const name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5016 #endif
5018 if (! TREE_USED (decl))
5019 return;
5021 #ifdef ASM_WEAKEN_DECL
5022 ASM_WEAKEN_DECL (asm_out_file, decl, name, NULL);
5023 #else
5024 #ifdef ASM_WEAKEN_LABEL
5025 ASM_WEAKEN_LABEL (asm_out_file, name);
5026 #else
5027 #ifdef ASM_OUTPUT_WEAK_ALIAS
5029 static bool warn_once = 0;
5030 if (! warn_once)
5032 warning (0, "only weak aliases are supported in this configuration");
5033 warn_once = 1;
5035 return;
5037 #endif
5038 #endif
5039 #endif
5042 /* This TREE_LIST contains weakref targets. */
5044 static GTY(()) tree weakref_targets;
5046 /* Forward declaration. */
5047 static tree find_decl_and_mark_needed (tree decl, tree target);
5049 /* Emit any pending weak declarations. */
5051 void
5052 weak_finish (void)
5054 tree t;
5056 for (t = weakref_targets; t; t = TREE_CHAIN (t))
5058 tree alias_decl = TREE_PURPOSE (t);
5059 tree target = ultimate_transparent_alias_target (&TREE_VALUE (t));
5061 if (! TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (alias_decl)))
5062 /* Remove alias_decl from the weak list, but leave entries for
5063 the target alone. */
5064 target = NULL_TREE;
5065 #ifndef ASM_OUTPUT_WEAKREF
5066 else if (! TREE_SYMBOL_REFERENCED (target))
5068 /* Use ASM_WEAKEN_LABEL only if ASM_WEAKEN_DECL is not
5069 defined, otherwise we and weak_finish_1 would use
5070 different macros. */
5071 # if defined ASM_WEAKEN_LABEL && ! defined ASM_WEAKEN_DECL
5072 ASM_WEAKEN_LABEL (asm_out_file, IDENTIFIER_POINTER (target));
5073 # else
5074 tree decl = find_decl_and_mark_needed (alias_decl, target);
5076 if (! decl)
5078 decl = build_decl (TREE_CODE (alias_decl), target,
5079 TREE_TYPE (alias_decl));
5081 DECL_EXTERNAL (decl) = 1;
5082 TREE_PUBLIC (decl) = 1;
5083 DECL_ARTIFICIAL (decl) = 1;
5084 TREE_NOTHROW (decl) = TREE_NOTHROW (alias_decl);
5085 TREE_USED (decl) = 1;
5088 weak_finish_1 (decl);
5089 # endif
5091 #endif
5094 tree *p;
5095 tree t2;
5097 /* Remove the alias and the target from the pending weak list
5098 so that we do not emit any .weak directives for the former,
5099 nor multiple .weak directives for the latter. */
5100 for (p = &weak_decls; (t2 = *p) ; )
5102 if (TREE_VALUE (t2) == alias_decl
5103 || target == DECL_ASSEMBLER_NAME (TREE_VALUE (t2)))
5104 *p = TREE_CHAIN (t2);
5105 else
5106 p = &TREE_CHAIN (t2);
5109 /* Remove other weakrefs to the same target, to speed things up. */
5110 for (p = &TREE_CHAIN (t); (t2 = *p) ; )
5112 if (target == ultimate_transparent_alias_target (&TREE_VALUE (t2)))
5113 *p = TREE_CHAIN (t2);
5114 else
5115 p = &TREE_CHAIN (t2);
5120 for (t = weak_decls; t; t = TREE_CHAIN (t))
5122 tree decl = TREE_VALUE (t);
5124 weak_finish_1 (decl);
5128 /* Emit the assembly bits to indicate that DECL is globally visible. */
5130 static void
5131 globalize_decl (tree decl)
5134 #if defined (ASM_WEAKEN_LABEL) || defined (ASM_WEAKEN_DECL)
5135 if (DECL_WEAK (decl))
5137 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
5138 tree *p, t;
5140 #ifdef ASM_WEAKEN_DECL
5141 ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
5142 #else
5143 ASM_WEAKEN_LABEL (asm_out_file, name);
5144 #endif
5146 /* Remove this function from the pending weak list so that
5147 we do not emit multiple .weak directives for it. */
5148 for (p = &weak_decls; (t = *p) ; )
5150 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
5151 *p = TREE_CHAIN (t);
5152 else
5153 p = &TREE_CHAIN (t);
5156 /* Remove weakrefs to the same target from the pending weakref
5157 list, for the same reason. */
5158 for (p = &weakref_targets; (t = *p) ; )
5160 if (DECL_ASSEMBLER_NAME (decl)
5161 == ultimate_transparent_alias_target (&TREE_VALUE (t)))
5162 *p = TREE_CHAIN (t);
5163 else
5164 p = &TREE_CHAIN (t);
5167 return;
5169 #endif
5171 targetm.asm_out.globalize_decl_name (asm_out_file, decl);
5174 /* We have to be able to tell cgraph about the needed-ness of the target
5175 of an alias. This requires that the decl have been defined. Aliases
5176 that precede their definition have to be queued for later processing. */
5178 typedef struct GTY(()) alias_pair {
5179 tree decl;
5180 tree target;
5181 } alias_pair;
5183 /* Define gc'd vector type. */
5184 DEF_VEC_O(alias_pair);
5185 DEF_VEC_ALLOC_O(alias_pair,gc);
5187 static GTY(()) VEC(alias_pair,gc) *alias_pairs;
5189 /* Given an assembly name, find the decl it is associated with. At the
5190 same time, mark it needed for cgraph. */
5192 static tree
5193 find_decl_and_mark_needed (tree decl, tree target)
5195 struct cgraph_node *fnode = NULL;
5196 struct varpool_node *vnode = NULL;
5198 if (TREE_CODE (decl) == FUNCTION_DECL)
5200 fnode = cgraph_node_for_asm (target);
5201 if (fnode == NULL)
5202 vnode = varpool_node_for_asm (target);
5204 else
5206 vnode = varpool_node_for_asm (target);
5207 if (vnode == NULL)
5208 fnode = cgraph_node_for_asm (target);
5211 if (fnode)
5213 /* We can't mark function nodes as used after cgraph global info
5214 is finished. This wouldn't generally be necessary, but C++
5215 virtual table thunks are introduced late in the game and
5216 might seem like they need marking, although in fact they
5217 don't. */
5218 if (! cgraph_global_info_ready)
5219 cgraph_mark_needed_node (fnode);
5220 return fnode->decl;
5222 else if (vnode)
5224 varpool_mark_needed_node (vnode);
5225 return vnode->decl;
5227 else
5228 return NULL_TREE;
5231 /* Output the assembler code for a define (equate) using ASM_OUTPUT_DEF
5232 or ASM_OUTPUT_DEF_FROM_DECLS. The function defines the symbol whose
5233 tree node is DECL to have the value of the tree node TARGET. */
5235 static void
5236 do_assemble_alias (tree decl, tree target)
5238 if (TREE_ASM_WRITTEN (decl))
5239 return;
5241 TREE_ASM_WRITTEN (decl) = 1;
5242 TREE_ASM_WRITTEN (DECL_ASSEMBLER_NAME (decl)) = 1;
5244 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
5246 ultimate_transparent_alias_target (&target);
5248 if (!targetm.have_tls
5249 && TREE_CODE (decl) == VAR_DECL
5250 && DECL_THREAD_LOCAL_P (decl))
5252 decl = emutls_decl (decl);
5253 target = get_emutls_object_name (target);
5256 if (!TREE_SYMBOL_REFERENCED (target))
5257 weakref_targets = tree_cons (decl, target, weakref_targets);
5259 #ifdef ASM_OUTPUT_WEAKREF
5260 ASM_OUTPUT_WEAKREF (asm_out_file, decl,
5261 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
5262 IDENTIFIER_POINTER (target));
5263 #else
5264 if (!SUPPORTS_WEAK)
5266 error ("%Jweakref is not supported in this configuration", decl);
5267 return;
5269 #endif
5270 return;
5273 if (!targetm.have_tls
5274 && TREE_CODE (decl) == VAR_DECL
5275 && DECL_THREAD_LOCAL_P (decl))
5277 decl = emutls_decl (decl);
5278 target = get_emutls_object_name (target);
5281 #ifdef ASM_OUTPUT_DEF
5282 /* Make name accessible from other files, if appropriate. */
5284 if (TREE_PUBLIC (decl))
5286 globalize_decl (decl);
5287 maybe_assemble_visibility (decl);
5290 # ifdef ASM_OUTPUT_DEF_FROM_DECLS
5291 ASM_OUTPUT_DEF_FROM_DECLS (asm_out_file, decl, target);
5292 # else
5293 ASM_OUTPUT_DEF (asm_out_file,
5294 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
5295 IDENTIFIER_POINTER (target));
5296 # endif
5297 #elif defined (ASM_OUTPUT_WEAK_ALIAS) || defined (ASM_WEAKEN_DECL)
5299 const char *name;
5300 tree *p, t;
5302 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5303 # ifdef ASM_WEAKEN_DECL
5304 ASM_WEAKEN_DECL (asm_out_file, decl, name, IDENTIFIER_POINTER (target));
5305 # else
5306 ASM_OUTPUT_WEAK_ALIAS (asm_out_file, name, IDENTIFIER_POINTER (target));
5307 # endif
5308 /* Remove this function from the pending weak list so that
5309 we do not emit multiple .weak directives for it. */
5310 for (p = &weak_decls; (t = *p) ; )
5311 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
5312 *p = TREE_CHAIN (t);
5313 else
5314 p = &TREE_CHAIN (t);
5316 /* Remove weakrefs to the same target from the pending weakref
5317 list, for the same reason. */
5318 for (p = &weakref_targets; (t = *p) ; )
5320 if (DECL_ASSEMBLER_NAME (decl)
5321 == ultimate_transparent_alias_target (&TREE_VALUE (t)))
5322 *p = TREE_CHAIN (t);
5323 else
5324 p = &TREE_CHAIN (t);
5327 #endif
5330 /* First pass of completing pending aliases. Make sure that cgraph knows
5331 which symbols will be required. */
5333 void
5334 finish_aliases_1 (void)
5336 unsigned i;
5337 alias_pair *p;
5339 for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); i++)
5341 tree target_decl;
5343 target_decl = find_decl_and_mark_needed (p->decl, p->target);
5344 if (target_decl == NULL)
5346 if (! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
5347 error ("%q+D aliased to undefined symbol %qE",
5348 p->decl, p->target);
5350 else if (DECL_EXTERNAL (target_decl)
5351 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
5352 error ("%q+D aliased to external symbol %qE",
5353 p->decl, p->target);
5357 /* Second pass of completing pending aliases. Emit the actual assembly.
5358 This happens at the end of compilation and thus it is assured that the
5359 target symbol has been emitted. */
5361 void
5362 finish_aliases_2 (void)
5364 unsigned i;
5365 alias_pair *p;
5367 for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); i++)
5368 do_assemble_alias (p->decl, p->target);
5370 VEC_truncate (alias_pair, alias_pairs, 0);
5373 /* Emit an assembler directive to make the symbol for DECL an alias to
5374 the symbol for TARGET. */
5376 void
5377 assemble_alias (tree decl, tree target)
5379 tree target_decl;
5380 bool is_weakref = false;
5382 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
5384 tree alias = DECL_ASSEMBLER_NAME (decl);
5386 is_weakref = true;
5388 ultimate_transparent_alias_target (&target);
5390 if (alias == target)
5391 error ("weakref %q+D ultimately targets itself", decl);
5392 else
5394 #ifndef ASM_OUTPUT_WEAKREF
5395 IDENTIFIER_TRANSPARENT_ALIAS (alias) = 1;
5396 TREE_CHAIN (alias) = target;
5397 #endif
5399 if (TREE_PUBLIC (decl))
5400 error ("weakref %q+D must have static linkage", decl);
5402 else
5404 #if !defined (ASM_OUTPUT_DEF)
5405 # if !defined(ASM_OUTPUT_WEAK_ALIAS) && !defined (ASM_WEAKEN_DECL)
5406 error ("%Jalias definitions not supported in this configuration", decl);
5407 return;
5408 # else
5409 if (!DECL_WEAK (decl))
5411 error ("%Jonly weak aliases are supported in this configuration", decl);
5412 return;
5414 # endif
5415 #endif
5418 /* We must force creation of DECL_RTL for debug info generation, even though
5419 we don't use it here. */
5420 make_decl_rtl (decl);
5421 TREE_USED (decl) = 1;
5423 /* A quirk of the initial implementation of aliases required that the user
5424 add "extern" to all of them. Which is silly, but now historical. Do
5425 note that the symbol is in fact locally defined. */
5426 if (! is_weakref)
5427 DECL_EXTERNAL (decl) = 0;
5429 /* Allow aliases to aliases. */
5430 if (TREE_CODE (decl) == FUNCTION_DECL)
5431 cgraph_node (decl)->alias = true;
5432 else
5433 varpool_node (decl)->alias = true;
5435 /* If the target has already been emitted, we don't have to queue the
5436 alias. This saves a tad of memory. */
5437 if (cgraph_global_info_ready)
5438 target_decl = find_decl_and_mark_needed (decl, target);
5439 else
5440 target_decl= NULL;
5441 if (target_decl && TREE_ASM_WRITTEN (target_decl))
5442 do_assemble_alias (decl, target);
5443 else
5445 alias_pair *p = VEC_safe_push (alias_pair, gc, alias_pairs, NULL);
5446 p->decl = decl;
5447 p->target = target;
5451 /* Emit an assembler directive to set symbol for DECL visibility to
5452 the visibility type VIS, which must not be VISIBILITY_DEFAULT. */
5454 void
5455 default_assemble_visibility (tree decl, int vis)
5457 static const char * const visibility_types[] = {
5458 NULL, "protected", "hidden", "internal"
5461 const char *name, *type;
5463 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5464 type = visibility_types[vis];
5466 #ifdef HAVE_GAS_HIDDEN
5467 fprintf (asm_out_file, "\t.%s\t", type);
5468 assemble_name (asm_out_file, name);
5469 fprintf (asm_out_file, "\n");
5470 #else
5471 warning (OPT_Wattributes, "visibility attribute not supported "
5472 "in this configuration; ignored");
5473 #endif
5476 /* A helper function to call assemble_visibility when needed for a decl. */
5479 maybe_assemble_visibility (tree decl)
5481 enum symbol_visibility vis = DECL_VISIBILITY (decl);
5483 if (vis != VISIBILITY_DEFAULT)
5485 targetm.asm_out.visibility (decl, vis);
5486 return 1;
5488 else
5489 return 0;
5492 /* Returns 1 if the target configuration supports defining public symbols
5493 so that one of them will be chosen at link time instead of generating a
5494 multiply-defined symbol error, whether through the use of weak symbols or
5495 a target-specific mechanism for having duplicates discarded. */
5498 supports_one_only (void)
5500 if (SUPPORTS_ONE_ONLY)
5501 return 1;
5502 return SUPPORTS_WEAK;
5505 /* Set up DECL as a public symbol that can be defined in multiple
5506 translation units without generating a linker error. */
5508 void
5509 make_decl_one_only (tree decl)
5511 gcc_assert (TREE_CODE (decl) == VAR_DECL
5512 || TREE_CODE (decl) == FUNCTION_DECL);
5514 TREE_PUBLIC (decl) = 1;
5516 if (SUPPORTS_ONE_ONLY)
5518 #ifdef MAKE_DECL_ONE_ONLY
5519 MAKE_DECL_ONE_ONLY (decl);
5520 #endif
5521 DECL_ONE_ONLY (decl) = 1;
5523 else if (TREE_CODE (decl) == VAR_DECL
5524 && (DECL_INITIAL (decl) == 0 || DECL_INITIAL (decl) == error_mark_node))
5525 DECL_COMMON (decl) = 1;
5526 else
5528 gcc_assert (SUPPORTS_WEAK);
5529 DECL_WEAK (decl) = 1;
5533 void
5534 init_varasm_once (void)
5536 section_htab = htab_create_ggc (31, section_entry_hash,
5537 section_entry_eq, NULL);
5538 object_block_htab = htab_create_ggc (31, object_block_entry_hash,
5539 object_block_entry_eq, NULL);
5540 const_desc_htab = htab_create_ggc (1009, const_desc_hash,
5541 const_desc_eq, NULL);
5543 const_alias_set = new_alias_set ();
5544 shared_constant_pool = create_constant_pool ();
5546 #ifdef TEXT_SECTION_ASM_OP
5547 text_section = get_unnamed_section (SECTION_CODE, output_section_asm_op,
5548 TEXT_SECTION_ASM_OP);
5549 #endif
5551 #ifdef DATA_SECTION_ASM_OP
5552 data_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
5553 DATA_SECTION_ASM_OP);
5554 #endif
5556 #ifdef SDATA_SECTION_ASM_OP
5557 sdata_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
5558 SDATA_SECTION_ASM_OP);
5559 #endif
5561 #ifdef READONLY_DATA_SECTION_ASM_OP
5562 readonly_data_section = get_unnamed_section (0, output_section_asm_op,
5563 READONLY_DATA_SECTION_ASM_OP);
5564 #endif
5566 #ifdef CTORS_SECTION_ASM_OP
5567 ctors_section = get_unnamed_section (0, output_section_asm_op,
5568 CTORS_SECTION_ASM_OP);
5569 #endif
5571 #ifdef DTORS_SECTION_ASM_OP
5572 dtors_section = get_unnamed_section (0, output_section_asm_op,
5573 DTORS_SECTION_ASM_OP);
5574 #endif
5576 #ifdef BSS_SECTION_ASM_OP
5577 bss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
5578 output_section_asm_op,
5579 BSS_SECTION_ASM_OP);
5580 #endif
5582 #ifdef SBSS_SECTION_ASM_OP
5583 sbss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
5584 output_section_asm_op,
5585 SBSS_SECTION_ASM_OP);
5586 #endif
5588 tls_comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
5589 | SECTION_COMMON, emit_tls_common);
5590 lcomm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
5591 | SECTION_COMMON, emit_local);
5592 comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
5593 | SECTION_COMMON, emit_common);
5595 #if defined ASM_OUTPUT_ALIGNED_BSS || defined ASM_OUTPUT_BSS
5596 bss_noswitch_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS,
5597 emit_bss);
5598 #endif
5600 targetm.asm_out.init_sections ();
5602 if (readonly_data_section == NULL)
5603 readonly_data_section = text_section;
5606 enum tls_model
5607 decl_default_tls_model (const_tree decl)
5609 enum tls_model kind;
5610 bool is_local;
5612 is_local = targetm.binds_local_p (decl);
5613 if (!flag_shlib)
5615 if (is_local)
5616 kind = TLS_MODEL_LOCAL_EXEC;
5617 else
5618 kind = TLS_MODEL_INITIAL_EXEC;
5621 /* Local dynamic is inefficient when we're not combining the
5622 parts of the address. */
5623 else if (optimize && is_local)
5624 kind = TLS_MODEL_LOCAL_DYNAMIC;
5625 else
5626 kind = TLS_MODEL_GLOBAL_DYNAMIC;
5627 if (kind < flag_tls_default)
5628 kind = flag_tls_default;
5630 return kind;
5633 /* Select a set of attributes for section NAME based on the properties
5634 of DECL and whether or not RELOC indicates that DECL's initializer
5635 might contain runtime relocations.
5637 We make the section read-only and executable for a function decl,
5638 read-only for a const data decl, and writable for a non-const data decl. */
5640 unsigned int
5641 default_section_type_flags (tree decl, const char *name, int reloc)
5643 unsigned int flags;
5645 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
5646 flags = SECTION_CODE;
5647 else if (decl && decl_readonly_section (decl, reloc))
5648 flags = 0;
5649 else if (current_function_decl
5650 && cfun
5651 && crtl->subsections.unlikely_text_section_name
5652 && strcmp (name, crtl->subsections.unlikely_text_section_name) == 0)
5653 flags = SECTION_CODE;
5654 else if (!decl
5655 && (!current_function_decl || !cfun)
5656 && strcmp (name, UNLIKELY_EXECUTED_TEXT_SECTION_NAME) == 0)
5657 flags = SECTION_CODE;
5658 else
5659 flags = SECTION_WRITE;
5661 if (decl && DECL_ONE_ONLY (decl))
5662 flags |= SECTION_LINKONCE;
5664 if (decl && TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
5665 flags |= SECTION_TLS | SECTION_WRITE;
5667 if (strcmp (name, ".bss") == 0
5668 || strncmp (name, ".bss.", 5) == 0
5669 || strncmp (name, ".gnu.linkonce.b.", 16) == 0
5670 || strcmp (name, ".sbss") == 0
5671 || strncmp (name, ".sbss.", 6) == 0
5672 || strncmp (name, ".gnu.linkonce.sb.", 17) == 0)
5673 flags |= SECTION_BSS;
5675 if (strcmp (name, ".tdata") == 0
5676 || strncmp (name, ".tdata.", 7) == 0
5677 || strncmp (name, ".gnu.linkonce.td.", 17) == 0)
5678 flags |= SECTION_TLS;
5680 if (strcmp (name, ".tbss") == 0
5681 || strncmp (name, ".tbss.", 6) == 0
5682 || strncmp (name, ".gnu.linkonce.tb.", 17) == 0)
5683 flags |= SECTION_TLS | SECTION_BSS;
5685 /* These three sections have special ELF types. They are neither
5686 SHT_PROGBITS nor SHT_NOBITS, so when changing sections we don't
5687 want to print a section type (@progbits or @nobits). If someone
5688 is silly enough to emit code or TLS variables to one of these
5689 sections, then don't handle them specially. */
5690 if (!(flags & (SECTION_CODE | SECTION_BSS | SECTION_TLS))
5691 && (strcmp (name, ".init_array") == 0
5692 || strcmp (name, ".fini_array") == 0
5693 || strcmp (name, ".preinit_array") == 0))
5694 flags |= SECTION_NOTYPE;
5696 return flags;
5699 /* Return true if the target supports some form of global BSS,
5700 either through bss_noswitch_section, or by selecting a BSS
5701 section in TARGET_ASM_SELECT_SECTION. */
5703 bool
5704 have_global_bss_p (void)
5706 return bss_noswitch_section || targetm.have_switchable_bss_sections;
5709 /* Output assembly to switch to section NAME with attribute FLAGS.
5710 Four variants for common object file formats. */
5712 void
5713 default_no_named_section (const char *name ATTRIBUTE_UNUSED,
5714 unsigned int flags ATTRIBUTE_UNUSED,
5715 tree decl ATTRIBUTE_UNUSED)
5717 /* Some object formats don't support named sections at all. The
5718 front-end should already have flagged this as an error. */
5719 gcc_unreachable ();
5722 void
5723 default_elf_asm_named_section (const char *name, unsigned int flags,
5724 tree decl ATTRIBUTE_UNUSED)
5726 char flagchars[10], *f = flagchars;
5728 /* If we have already declared this section, we can use an
5729 abbreviated form to switch back to it -- unless this section is
5730 part of a COMDAT groups, in which case GAS requires the full
5731 declaration every time. */
5732 if (!(HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
5733 && (flags & SECTION_DECLARED))
5735 fprintf (asm_out_file, "\t.section\t%s\n", name);
5736 return;
5739 if (!(flags & SECTION_DEBUG))
5740 *f++ = 'a';
5741 if (flags & SECTION_WRITE)
5742 *f++ = 'w';
5743 if (flags & SECTION_CODE)
5744 *f++ = 'x';
5745 if (flags & SECTION_SMALL)
5746 *f++ = 's';
5747 if (flags & SECTION_MERGE)
5748 *f++ = 'M';
5749 if (flags & SECTION_STRINGS)
5750 *f++ = 'S';
5751 if (flags & SECTION_TLS)
5752 *f++ = 'T';
5753 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
5754 *f++ = 'G';
5755 *f = '\0';
5757 fprintf (asm_out_file, "\t.section\t%s,\"%s\"", name, flagchars);
5759 if (!(flags & SECTION_NOTYPE))
5761 const char *type;
5762 const char *format;
5764 if (flags & SECTION_BSS)
5765 type = "nobits";
5766 else
5767 type = "progbits";
5769 format = ",@%s";
5770 #ifdef ASM_COMMENT_START
5771 /* On platforms that use "@" as the assembly comment character,
5772 use "%" instead. */
5773 if (strcmp (ASM_COMMENT_START, "@") == 0)
5774 format = ",%%%s";
5775 #endif
5776 fprintf (asm_out_file, format, type);
5778 if (flags & SECTION_ENTSIZE)
5779 fprintf (asm_out_file, ",%d", flags & SECTION_ENTSIZE);
5780 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
5781 fprintf (asm_out_file, ",%s,comdat",
5782 lang_hooks.decls.comdat_group (decl));
5785 putc ('\n', asm_out_file);
5788 void
5789 default_coff_asm_named_section (const char *name, unsigned int flags,
5790 tree decl ATTRIBUTE_UNUSED)
5792 char flagchars[8], *f = flagchars;
5794 if (flags & SECTION_WRITE)
5795 *f++ = 'w';
5796 if (flags & SECTION_CODE)
5797 *f++ = 'x';
5798 *f = '\0';
5800 fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
5803 void
5804 default_pe_asm_named_section (const char *name, unsigned int flags,
5805 tree decl)
5807 default_coff_asm_named_section (name, flags, decl);
5809 if (flags & SECTION_LINKONCE)
5811 /* Functions may have been compiled at various levels of
5812 optimization so we can't use `same_size' here.
5813 Instead, have the linker pick one. */
5814 fprintf (asm_out_file, "\t.linkonce %s\n",
5815 (flags & SECTION_CODE ? "discard" : "same_size"));
5819 /* The lame default section selector. */
5821 section *
5822 default_select_section (tree decl, int reloc,
5823 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
5825 if (DECL_P (decl))
5827 if (decl_readonly_section (decl, reloc))
5828 return readonly_data_section;
5830 else if (TREE_CODE (decl) == CONSTRUCTOR)
5832 if (! ((flag_pic && reloc)
5833 || !TREE_READONLY (decl)
5834 || TREE_SIDE_EFFECTS (decl)
5835 || !TREE_CONSTANT (decl)))
5836 return readonly_data_section;
5838 else if (TREE_CODE (decl) == STRING_CST)
5839 return readonly_data_section;
5840 else if (! (flag_pic && reloc))
5841 return readonly_data_section;
5843 return data_section;
5846 enum section_category
5847 categorize_decl_for_section (const_tree decl, int reloc)
5849 enum section_category ret;
5851 if (TREE_CODE (decl) == FUNCTION_DECL)
5852 return SECCAT_TEXT;
5853 else if (TREE_CODE (decl) == STRING_CST)
5855 if (flag_mudflap) /* or !flag_merge_constants */
5856 return SECCAT_RODATA;
5857 else
5858 return SECCAT_RODATA_MERGE_STR;
5860 else if (TREE_CODE (decl) == VAR_DECL)
5862 if (bss_initializer_p (decl))
5863 ret = SECCAT_BSS;
5864 else if (! TREE_READONLY (decl)
5865 || TREE_SIDE_EFFECTS (decl)
5866 || ! TREE_CONSTANT (DECL_INITIAL (decl)))
5868 /* Here the reloc_rw_mask is not testing whether the section should
5869 be read-only or not, but whether the dynamic link will have to
5870 do something. If so, we wish to segregate the data in order to
5871 minimize cache misses inside the dynamic linker. */
5872 if (reloc & targetm.asm_out.reloc_rw_mask ())
5873 ret = reloc == 1 ? SECCAT_DATA_REL_LOCAL : SECCAT_DATA_REL;
5874 else
5875 ret = SECCAT_DATA;
5877 else if (reloc & targetm.asm_out.reloc_rw_mask ())
5878 ret = reloc == 1 ? SECCAT_DATA_REL_RO_LOCAL : SECCAT_DATA_REL_RO;
5879 else if (reloc || flag_merge_constants < 2)
5880 /* C and C++ don't allow different variables to share the same
5881 location. -fmerge-all-constants allows even that (at the
5882 expense of not conforming). */
5883 ret = SECCAT_RODATA;
5884 else if (TREE_CODE (DECL_INITIAL (decl)) == STRING_CST)
5885 ret = SECCAT_RODATA_MERGE_STR_INIT;
5886 else
5887 ret = SECCAT_RODATA_MERGE_CONST;
5889 else if (TREE_CODE (decl) == CONSTRUCTOR)
5891 if ((reloc & targetm.asm_out.reloc_rw_mask ())
5892 || TREE_SIDE_EFFECTS (decl)
5893 || ! TREE_CONSTANT (decl))
5894 ret = SECCAT_DATA;
5895 else
5896 ret = SECCAT_RODATA;
5898 else
5899 ret = SECCAT_RODATA;
5901 /* There are no read-only thread-local sections. */
5902 if (TREE_CODE (decl) == VAR_DECL && DECL_TLS_MODEL (decl))
5904 if (DECL_TLS_MODEL (decl) == TLS_MODEL_EMULATED)
5906 if (DECL_EMUTLS_VAR_P (decl))
5908 if (targetm.emutls.var_section)
5909 ret = SECCAT_EMUTLS_VAR;
5911 else
5913 if (targetm.emutls.tmpl_prefix)
5914 ret = SECCAT_EMUTLS_TMPL;
5917 /* Note that this would be *just* SECCAT_BSS, except that there's
5918 no concept of a read-only thread-local-data section. */
5919 else if (ret == SECCAT_BSS
5920 || (flag_zero_initialized_in_bss
5921 && initializer_zerop (DECL_INITIAL (decl))))
5922 ret = SECCAT_TBSS;
5923 else
5924 ret = SECCAT_TDATA;
5927 /* If the target uses small data sections, select it. */
5928 else if (targetm.in_small_data_p (decl))
5930 if (ret == SECCAT_BSS)
5931 ret = SECCAT_SBSS;
5932 else if (targetm.have_srodata_section && ret == SECCAT_RODATA)
5933 ret = SECCAT_SRODATA;
5934 else
5935 ret = SECCAT_SDATA;
5938 return ret;
5941 bool
5942 decl_readonly_section (const_tree decl, int reloc)
5944 switch (categorize_decl_for_section (decl, reloc))
5946 case SECCAT_RODATA:
5947 case SECCAT_RODATA_MERGE_STR:
5948 case SECCAT_RODATA_MERGE_STR_INIT:
5949 case SECCAT_RODATA_MERGE_CONST:
5950 case SECCAT_SRODATA:
5951 return true;
5952 break;
5953 default:
5954 return false;
5955 break;
5959 /* Select a section based on the above categorization. */
5961 section *
5962 default_elf_select_section (tree decl, int reloc,
5963 unsigned HOST_WIDE_INT align)
5965 const char *sname;
5966 switch (categorize_decl_for_section (decl, reloc))
5968 case SECCAT_TEXT:
5969 /* We're not supposed to be called on FUNCTION_DECLs. */
5970 gcc_unreachable ();
5971 case SECCAT_RODATA:
5972 return readonly_data_section;
5973 case SECCAT_RODATA_MERGE_STR:
5974 return mergeable_string_section (decl, align, 0);
5975 case SECCAT_RODATA_MERGE_STR_INIT:
5976 return mergeable_string_section (DECL_INITIAL (decl), align, 0);
5977 case SECCAT_RODATA_MERGE_CONST:
5978 return mergeable_constant_section (DECL_MODE (decl), align, 0);
5979 case SECCAT_SRODATA:
5980 sname = ".sdata2";
5981 break;
5982 case SECCAT_DATA:
5983 return data_section;
5984 case SECCAT_DATA_REL:
5985 sname = ".data.rel";
5986 break;
5987 case SECCAT_DATA_REL_LOCAL:
5988 sname = ".data.rel.local";
5989 break;
5990 case SECCAT_DATA_REL_RO:
5991 sname = ".data.rel.ro";
5992 break;
5993 case SECCAT_DATA_REL_RO_LOCAL:
5994 sname = ".data.rel.ro.local";
5995 break;
5996 case SECCAT_SDATA:
5997 sname = ".sdata";
5998 break;
5999 case SECCAT_TDATA:
6000 sname = ".tdata";
6001 break;
6002 case SECCAT_BSS:
6003 if (bss_section)
6004 return bss_section;
6005 sname = ".bss";
6006 break;
6007 case SECCAT_SBSS:
6008 sname = ".sbss";
6009 break;
6010 case SECCAT_TBSS:
6011 sname = ".tbss";
6012 break;
6013 case SECCAT_EMUTLS_VAR:
6014 sname = targetm.emutls.var_section;
6015 break;
6016 case SECCAT_EMUTLS_TMPL:
6017 sname = targetm.emutls.tmpl_section;
6018 break;
6019 default:
6020 gcc_unreachable ();
6023 if (!DECL_P (decl))
6024 decl = NULL_TREE;
6025 return get_named_section (decl, sname, reloc);
6028 /* Construct a unique section name based on the decl name and the
6029 categorization performed above. */
6031 void
6032 default_unique_section (tree decl, int reloc)
6034 /* We only need to use .gnu.linkonce if we don't have COMDAT groups. */
6035 bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
6036 const char *prefix, *name, *linkonce;
6037 char *string;
6039 switch (categorize_decl_for_section (decl, reloc))
6041 case SECCAT_TEXT:
6042 prefix = one_only ? ".t" : ".text";
6043 break;
6044 case SECCAT_RODATA:
6045 case SECCAT_RODATA_MERGE_STR:
6046 case SECCAT_RODATA_MERGE_STR_INIT:
6047 case SECCAT_RODATA_MERGE_CONST:
6048 prefix = one_only ? ".r" : ".rodata";
6049 break;
6050 case SECCAT_SRODATA:
6051 prefix = one_only ? ".s2" : ".sdata2";
6052 break;
6053 case SECCAT_DATA:
6054 prefix = one_only ? ".d" : ".data";
6055 break;
6056 case SECCAT_DATA_REL:
6057 prefix = one_only ? ".d.rel" : ".data.rel";
6058 break;
6059 case SECCAT_DATA_REL_LOCAL:
6060 prefix = one_only ? ".d.rel.local" : ".data.rel.local";
6061 break;
6062 case SECCAT_DATA_REL_RO:
6063 prefix = one_only ? ".d.rel.ro" : ".data.rel.ro";
6064 break;
6065 case SECCAT_DATA_REL_RO_LOCAL:
6066 prefix = one_only ? ".d.rel.ro.local" : ".data.rel.ro.local";
6067 break;
6068 case SECCAT_SDATA:
6069 prefix = one_only ? ".s" : ".sdata";
6070 break;
6071 case SECCAT_BSS:
6072 prefix = one_only ? ".b" : ".bss";
6073 break;
6074 case SECCAT_SBSS:
6075 prefix = one_only ? ".sb" : ".sbss";
6076 break;
6077 case SECCAT_TDATA:
6078 prefix = one_only ? ".td" : ".tdata";
6079 break;
6080 case SECCAT_TBSS:
6081 prefix = one_only ? ".tb" : ".tbss";
6082 break;
6083 case SECCAT_EMUTLS_VAR:
6084 prefix = targetm.emutls.var_section;
6085 break;
6086 case SECCAT_EMUTLS_TMPL:
6087 prefix = targetm.emutls.tmpl_section;
6088 break;
6089 default:
6090 gcc_unreachable ();
6093 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
6094 name = targetm.strip_name_encoding (name);
6096 /* If we're using one_only, then there needs to be a .gnu.linkonce
6097 prefix to the section name. */
6098 linkonce = one_only ? ".gnu.linkonce" : "";
6100 string = ACONCAT ((linkonce, prefix, ".", name, NULL));
6102 DECL_SECTION_NAME (decl) = build_string (strlen (string), string);
6105 /* Like compute_reloc_for_constant, except for an RTX. The return value
6106 is a mask for which bit 1 indicates a global relocation, and bit 0
6107 indicates a local relocation. */
6109 static int
6110 compute_reloc_for_rtx_1 (rtx *xp, void *data)
6112 int *preloc = (int *) data;
6113 rtx x = *xp;
6115 switch (GET_CODE (x))
6117 case SYMBOL_REF:
6118 *preloc |= SYMBOL_REF_LOCAL_P (x) ? 1 : 2;
6119 break;
6120 case LABEL_REF:
6121 *preloc |= 1;
6122 break;
6123 default:
6124 break;
6127 return 0;
6130 static int
6131 compute_reloc_for_rtx (rtx x)
6133 int reloc;
6135 switch (GET_CODE (x))
6137 case CONST:
6138 case SYMBOL_REF:
6139 case LABEL_REF:
6140 reloc = 0;
6141 for_each_rtx (&x, compute_reloc_for_rtx_1, &reloc);
6142 return reloc;
6144 default:
6145 return 0;
6149 section *
6150 default_select_rtx_section (enum machine_mode mode ATTRIBUTE_UNUSED,
6151 rtx x,
6152 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
6154 if (compute_reloc_for_rtx (x) & targetm.asm_out.reloc_rw_mask ())
6155 return data_section;
6156 else
6157 return readonly_data_section;
6160 section *
6161 default_elf_select_rtx_section (enum machine_mode mode, rtx x,
6162 unsigned HOST_WIDE_INT align)
6164 int reloc = compute_reloc_for_rtx (x);
6166 /* ??? Handle small data here somehow. */
6168 if (reloc & targetm.asm_out.reloc_rw_mask ())
6170 if (reloc == 1)
6171 return get_named_section (NULL, ".data.rel.ro.local", 1);
6172 else
6173 return get_named_section (NULL, ".data.rel.ro", 3);
6176 return mergeable_constant_section (mode, align, 0);
6179 /* Set the generally applicable flags on the SYMBOL_REF for EXP. */
6181 void
6182 default_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
6184 rtx symbol;
6185 int flags;
6187 /* Careful not to prod global register variables. */
6188 if (!MEM_P (rtl))
6189 return;
6190 symbol = XEXP (rtl, 0);
6191 if (GET_CODE (symbol) != SYMBOL_REF)
6192 return;
6194 flags = SYMBOL_REF_FLAGS (symbol) & SYMBOL_FLAG_HAS_BLOCK_INFO;
6195 if (TREE_CODE (decl) == FUNCTION_DECL)
6196 flags |= SYMBOL_FLAG_FUNCTION;
6197 if (targetm.binds_local_p (decl))
6198 flags |= SYMBOL_FLAG_LOCAL;
6199 if (targetm.have_tls && TREE_CODE (decl) == VAR_DECL
6200 && DECL_THREAD_LOCAL_P (decl))
6201 flags |= DECL_TLS_MODEL (decl) << SYMBOL_FLAG_TLS_SHIFT;
6202 else if (targetm.in_small_data_p (decl))
6203 flags |= SYMBOL_FLAG_SMALL;
6204 /* ??? Why is DECL_EXTERNAL ever set for non-PUBLIC names? Without
6205 being PUBLIC, the thing *must* be defined in this translation unit.
6206 Prevent this buglet from being propagated into rtl code as well. */
6207 if (DECL_P (decl) && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl))
6208 flags |= SYMBOL_FLAG_EXTERNAL;
6210 SYMBOL_REF_FLAGS (symbol) = flags;
6213 /* By default, we do nothing for encode_section_info, so we need not
6214 do anything but discard the '*' marker. */
6216 const char *
6217 default_strip_name_encoding (const char *str)
6219 return str + (*str == '*');
6222 #ifdef ASM_OUTPUT_DEF
6223 /* The default implementation of TARGET_ASM_OUTPUT_ANCHOR. Define the
6224 anchor relative to ".", the current section position. */
6226 void
6227 default_asm_output_anchor (rtx symbol)
6229 char buffer[100];
6231 sprintf (buffer, "*. + " HOST_WIDE_INT_PRINT_DEC,
6232 SYMBOL_REF_BLOCK_OFFSET (symbol));
6233 ASM_OUTPUT_DEF (asm_out_file, XSTR (symbol, 0), buffer);
6235 #endif
6237 /* The default implementation of TARGET_USE_ANCHORS_FOR_SYMBOL_P. */
6239 bool
6240 default_use_anchors_for_symbol_p (const_rtx symbol)
6242 section *sect;
6243 tree decl;
6245 /* Don't use anchors for mergeable sections. The linker might move
6246 the objects around. */
6247 sect = SYMBOL_REF_BLOCK (symbol)->sect;
6248 if (sect->common.flags & SECTION_MERGE)
6249 return false;
6251 /* Don't use anchors for small data sections. The small data register
6252 acts as an anchor for such sections. */
6253 if (sect->common.flags & SECTION_SMALL)
6254 return false;
6256 decl = SYMBOL_REF_DECL (symbol);
6257 if (decl && DECL_P (decl))
6259 /* Don't use section anchors for decls that might be defined by
6260 other modules. */
6261 if (!targetm.binds_local_p (decl))
6262 return false;
6264 /* Don't use section anchors for decls that will be placed in a
6265 small data section. */
6266 /* ??? Ideally, this check would be redundant with the SECTION_SMALL
6267 one above. The problem is that we only use SECTION_SMALL for
6268 sections that should be marked as small in the section directive. */
6269 if (targetm.in_small_data_p (decl))
6270 return false;
6272 return true;
6275 /* Assume ELF-ish defaults, since that's pretty much the most liberal
6276 wrt cross-module name binding. */
6278 bool
6279 default_binds_local_p (const_tree exp)
6281 return default_binds_local_p_1 (exp, flag_shlib);
6284 bool
6285 default_binds_local_p_1 (const_tree exp, int shlib)
6287 bool local_p;
6289 /* A non-decl is an entry in the constant pool. */
6290 if (!DECL_P (exp))
6291 local_p = true;
6292 /* Weakrefs may not bind locally, even though the weakref itself is
6293 always static and therefore local. */
6294 else if (lookup_attribute ("weakref", DECL_ATTRIBUTES (exp)))
6295 local_p = false;
6296 /* Static variables are always local. */
6297 else if (! TREE_PUBLIC (exp))
6298 local_p = true;
6299 /* A variable is local if the user has said explicitly that it will
6300 be. */
6301 else if (DECL_VISIBILITY_SPECIFIED (exp)
6302 && DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
6303 local_p = true;
6304 /* Variables defined outside this object might not be local. */
6305 else if (DECL_EXTERNAL (exp))
6306 local_p = false;
6307 /* If defined in this object and visibility is not default, must be
6308 local. */
6309 else if (DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
6310 local_p = true;
6311 /* Default visibility weak data can be overridden by a strong symbol
6312 in another module and so are not local. */
6313 else if (DECL_WEAK (exp))
6314 local_p = false;
6315 /* If PIC, then assume that any global name can be overridden by
6316 symbols resolved from other modules, unless we are compiling with
6317 -fwhole-program, which assumes that names are local. */
6318 else if (shlib)
6319 local_p = flag_whole_program;
6320 /* Uninitialized COMMON variable may be unified with symbols
6321 resolved from other modules. */
6322 else if (DECL_COMMON (exp)
6323 && (DECL_INITIAL (exp) == NULL
6324 || DECL_INITIAL (exp) == error_mark_node))
6325 local_p = false;
6326 /* Otherwise we're left with initialized (or non-common) global data
6327 which is of necessity defined locally. */
6328 else
6329 local_p = true;
6331 return local_p;
6334 /* Determine whether or not a pointer mode is valid. Assume defaults
6335 of ptr_mode or Pmode - can be overridden. */
6336 bool
6337 default_valid_pointer_mode (enum machine_mode mode)
6339 return (mode == ptr_mode || mode == Pmode);
6342 /* Default function to output code that will globalize a label. A
6343 target must define GLOBAL_ASM_OP or provide its own function to
6344 globalize a label. */
6345 #ifdef GLOBAL_ASM_OP
6346 void
6347 default_globalize_label (FILE * stream, const char *name)
6349 fputs (GLOBAL_ASM_OP, stream);
6350 assemble_name (stream, name);
6351 putc ('\n', stream);
6353 #endif /* GLOBAL_ASM_OP */
6355 /* Default function to output code that will globalize a declaration. */
6356 void
6357 default_globalize_decl_name (FILE * stream, tree decl)
6359 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
6360 targetm.asm_out.globalize_label (stream, name);
6363 /* Default function to output a label for unwind information. The
6364 default is to do nothing. A target that needs nonlocal labels for
6365 unwind information must provide its own function to do this. */
6366 void
6367 default_emit_unwind_label (FILE * stream ATTRIBUTE_UNUSED,
6368 tree decl ATTRIBUTE_UNUSED,
6369 int for_eh ATTRIBUTE_UNUSED,
6370 int empty ATTRIBUTE_UNUSED)
6374 /* Default function to output a label to divide up the exception table.
6375 The default is to do nothing. A target that needs/wants to divide
6376 up the table must provide it's own function to do this. */
6377 void
6378 default_emit_except_table_label (FILE * stream ATTRIBUTE_UNUSED)
6382 /* This is how to output an internal numbered label where PREFIX is
6383 the class of label and LABELNO is the number within the class. */
6385 void
6386 default_internal_label (FILE *stream, const char *prefix,
6387 unsigned long labelno)
6389 char *const buf = (char *) alloca (40 + strlen (prefix));
6390 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
6391 ASM_OUTPUT_INTERNAL_LABEL (stream, buf);
6394 /* This is the default behavior at the beginning of a file. It's
6395 controlled by two other target-hook toggles. */
6396 void
6397 default_file_start (void)
6399 if (targetm.file_start_app_off
6400 && !(flag_verbose_asm || flag_debug_asm || flag_dump_rtl_in_asm))
6401 fputs (ASM_APP_OFF, asm_out_file);
6403 if (targetm.file_start_file_directive)
6404 output_file_directive (asm_out_file, main_input_filename);
6407 /* This is a generic routine suitable for use as TARGET_ASM_FILE_END
6408 which emits a special section directive used to indicate whether or
6409 not this object file needs an executable stack. This is primarily
6410 a GNU extension to ELF but could be used on other targets. */
6412 int trampolines_created;
6414 void
6415 file_end_indicate_exec_stack (void)
6417 unsigned int flags = SECTION_DEBUG;
6418 if (trampolines_created)
6419 flags |= SECTION_CODE;
6421 switch_to_section (get_section (".note.GNU-stack", flags, NULL));
6424 /* Output DIRECTIVE (a C string) followed by a newline. This is used as
6425 a get_unnamed_section callback. */
6427 void
6428 output_section_asm_op (const void *directive)
6430 fprintf (asm_out_file, "%s\n", (const char *) directive);
6433 /* Emit assembly code to switch to section NEW_SECTION. Do nothing if
6434 the current section is NEW_SECTION. */
6436 void
6437 switch_to_section (section *new_section)
6439 if (in_section == new_section)
6440 return;
6442 if (new_section->common.flags & SECTION_FORGET)
6443 in_section = NULL;
6444 else
6445 in_section = new_section;
6447 switch (SECTION_STYLE (new_section))
6449 case SECTION_NAMED:
6450 if (cfun
6451 && !crtl->subsections.unlikely_text_section_name
6452 && strcmp (new_section->named.name,
6453 UNLIKELY_EXECUTED_TEXT_SECTION_NAME) == 0)
6454 crtl->subsections.unlikely_text_section_name = UNLIKELY_EXECUTED_TEXT_SECTION_NAME;
6456 targetm.asm_out.named_section (new_section->named.name,
6457 new_section->named.common.flags,
6458 new_section->named.decl);
6459 break;
6461 case SECTION_UNNAMED:
6462 new_section->unnamed.callback (new_section->unnamed.data);
6463 break;
6465 case SECTION_NOSWITCH:
6466 gcc_unreachable ();
6467 break;
6470 new_section->common.flags |= SECTION_DECLARED;
6473 /* If block symbol SYMBOL has not yet been assigned an offset, place
6474 it at the end of its block. */
6476 void
6477 place_block_symbol (rtx symbol)
6479 unsigned HOST_WIDE_INT size, mask, offset;
6480 struct constant_descriptor_rtx *desc;
6481 unsigned int alignment;
6482 struct object_block *block;
6483 tree decl;
6485 gcc_assert (SYMBOL_REF_BLOCK (symbol));
6486 if (SYMBOL_REF_BLOCK_OFFSET (symbol) >= 0)
6487 return;
6489 /* Work out the symbol's size and alignment. */
6490 if (CONSTANT_POOL_ADDRESS_P (symbol))
6492 desc = SYMBOL_REF_CONSTANT (symbol);
6493 alignment = desc->align;
6494 size = GET_MODE_SIZE (desc->mode);
6496 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
6498 decl = SYMBOL_REF_DECL (symbol);
6499 alignment = get_constant_alignment (decl);
6500 size = get_constant_size (decl);
6502 else
6504 decl = SYMBOL_REF_DECL (symbol);
6505 alignment = DECL_ALIGN (decl);
6506 size = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
6509 /* Calculate the object's offset from the start of the block. */
6510 block = SYMBOL_REF_BLOCK (symbol);
6511 mask = alignment / BITS_PER_UNIT - 1;
6512 offset = (block->size + mask) & ~mask;
6513 SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
6515 /* Record the block's new alignment and size. */
6516 block->alignment = MAX (block->alignment, alignment);
6517 block->size = offset + size;
6519 VEC_safe_push (rtx, gc, block->objects, symbol);
6522 /* Return the anchor that should be used to address byte offset OFFSET
6523 from the first object in BLOCK. MODEL is the TLS model used
6524 to access it. */
6527 get_section_anchor (struct object_block *block, HOST_WIDE_INT offset,
6528 enum tls_model model)
6530 char label[100];
6531 unsigned int begin, middle, end;
6532 unsigned HOST_WIDE_INT min_offset, max_offset, range, bias, delta;
6533 rtx anchor;
6535 /* Work out the anchor's offset. Use an offset of 0 for the first
6536 anchor so that we don't pessimize the case where we take the address
6537 of a variable at the beginning of the block. This is particularly
6538 useful when a block has only one variable assigned to it.
6540 We try to place anchors RANGE bytes apart, so there can then be
6541 anchors at +/-RANGE, +/-2 * RANGE, and so on, up to the limits of
6542 a ptr_mode offset. With some target settings, the lowest such
6543 anchor might be out of range for the lowest ptr_mode offset;
6544 likewise the highest anchor for the highest offset. Use anchors
6545 at the extreme ends of the ptr_mode range in such cases.
6547 All arithmetic uses unsigned integers in order to avoid
6548 signed overflow. */
6549 max_offset = (unsigned HOST_WIDE_INT) targetm.max_anchor_offset;
6550 min_offset = (unsigned HOST_WIDE_INT) targetm.min_anchor_offset;
6551 range = max_offset - min_offset + 1;
6552 if (range == 0)
6553 offset = 0;
6554 else
6556 bias = 1 << (GET_MODE_BITSIZE (ptr_mode) - 1);
6557 if (offset < 0)
6559 delta = -(unsigned HOST_WIDE_INT) offset + max_offset;
6560 delta -= delta % range;
6561 if (delta > bias)
6562 delta = bias;
6563 offset = (HOST_WIDE_INT) (-delta);
6565 else
6567 delta = (unsigned HOST_WIDE_INT) offset - min_offset;
6568 delta -= delta % range;
6569 if (delta > bias - 1)
6570 delta = bias - 1;
6571 offset = (HOST_WIDE_INT) delta;
6575 /* Do a binary search to see if there's already an anchor we can use.
6576 Set BEGIN to the new anchor's index if not. */
6577 begin = 0;
6578 end = VEC_length (rtx, block->anchors);
6579 while (begin != end)
6581 middle = (end + begin) / 2;
6582 anchor = VEC_index (rtx, block->anchors, middle);
6583 if (SYMBOL_REF_BLOCK_OFFSET (anchor) > offset)
6584 end = middle;
6585 else if (SYMBOL_REF_BLOCK_OFFSET (anchor) < offset)
6586 begin = middle + 1;
6587 else if (SYMBOL_REF_TLS_MODEL (anchor) > model)
6588 end = middle;
6589 else if (SYMBOL_REF_TLS_MODEL (anchor) < model)
6590 begin = middle + 1;
6591 else
6592 return anchor;
6595 /* Create a new anchor with a unique label. */
6596 ASM_GENERATE_INTERNAL_LABEL (label, "LANCHOR", anchor_labelno++);
6597 anchor = create_block_symbol (ggc_strdup (label), block, offset);
6598 SYMBOL_REF_FLAGS (anchor) |= SYMBOL_FLAG_LOCAL | SYMBOL_FLAG_ANCHOR;
6599 SYMBOL_REF_FLAGS (anchor) |= model << SYMBOL_FLAG_TLS_SHIFT;
6601 /* Insert it at index BEGIN. */
6602 VEC_safe_insert (rtx, gc, block->anchors, begin, anchor);
6603 return anchor;
6606 /* Output the objects in BLOCK. */
6608 static void
6609 output_object_block (struct object_block *block)
6611 struct constant_descriptor_rtx *desc;
6612 unsigned int i;
6613 HOST_WIDE_INT offset;
6614 tree decl;
6615 rtx symbol;
6617 if (block->objects == NULL)
6618 return;
6620 /* Switch to the section and make sure that the first byte is
6621 suitably aligned. */
6622 switch_to_section (block->sect);
6623 assemble_align (block->alignment);
6625 /* Define the values of all anchors relative to the current section
6626 position. */
6627 for (i = 0; VEC_iterate (rtx, block->anchors, i, symbol); i++)
6628 targetm.asm_out.output_anchor (symbol);
6630 /* Output the objects themselves. */
6631 offset = 0;
6632 for (i = 0; VEC_iterate (rtx, block->objects, i, symbol); i++)
6634 /* Move to the object's offset, padding with zeros if necessary. */
6635 assemble_zeros (SYMBOL_REF_BLOCK_OFFSET (symbol) - offset);
6636 offset = SYMBOL_REF_BLOCK_OFFSET (symbol);
6637 if (CONSTANT_POOL_ADDRESS_P (symbol))
6639 desc = SYMBOL_REF_CONSTANT (symbol);
6640 output_constant_pool_1 (desc, 1);
6641 offset += GET_MODE_SIZE (desc->mode);
6643 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
6645 decl = SYMBOL_REF_DECL (symbol);
6646 assemble_constant_contents (decl, XSTR (symbol, 0),
6647 get_constant_alignment (decl));
6648 offset += get_constant_size (decl);
6650 else
6652 decl = SYMBOL_REF_DECL (symbol);
6653 assemble_variable_contents (decl, XSTR (symbol, 0), false);
6654 offset += tree_low_cst (DECL_SIZE_UNIT (decl), 1);
6659 /* A htab_traverse callback used to call output_object_block for
6660 each member of object_block_htab. */
6662 static int
6663 output_object_block_htab (void **slot, void *data ATTRIBUTE_UNUSED)
6665 output_object_block ((struct object_block *) (*slot));
6666 return 1;
6669 /* Output the definitions of all object_blocks. */
6671 void
6672 output_object_blocks (void)
6674 htab_traverse (object_block_htab, output_object_block_htab, NULL);
6677 /* This function provides a possible implementation of the
6678 TARGET_ASM_RECORD_GCC_SWITCHES target hook for ELF targets. When triggered
6679 by -frecord-gcc-switches it creates a new mergeable, string section in the
6680 assembler output file called TARGET_ASM_RECORD_GCC_SWITCHES_SECTION which
6681 contains the switches in ASCII format.
6683 FIXME: This code does not correctly handle double quote characters
6684 that appear inside strings, (it strips them rather than preserving them).
6685 FIXME: ASM_OUTPUT_ASCII, as defined in config/elfos.h will not emit NUL
6686 characters - instead it treats them as sub-string separators. Since
6687 we want to emit NUL strings terminators into the object file we have to use
6688 ASM_OUTPUT_SKIP. */
6691 elf_record_gcc_switches (print_switch_type type, const char * name)
6693 static char buffer[1024];
6695 /* This variable is used as part of a simplistic heuristic to detect
6696 command line switches which take an argument:
6698 "If a command line option does not start with a dash then
6699 it is an argument for the previous command line option."
6701 This fails in the case of the command line option which is the name
6702 of the file to compile, but otherwise it is pretty reasonable. */
6703 static bool previous_name_held_back = FALSE;
6705 switch (type)
6707 case SWITCH_TYPE_PASSED:
6708 if (* name != '-')
6710 if (previous_name_held_back)
6712 unsigned int len = strlen (buffer);
6714 snprintf (buffer + len, sizeof buffer - len, " %s", name);
6715 ASM_OUTPUT_ASCII (asm_out_file, buffer, strlen (buffer));
6716 ASM_OUTPUT_SKIP (asm_out_file, (unsigned HOST_WIDE_INT) 1);
6717 previous_name_held_back = FALSE;
6719 else
6721 strncpy (buffer, name, sizeof buffer);
6722 ASM_OUTPUT_ASCII (asm_out_file, buffer, strlen (buffer));
6723 ASM_OUTPUT_SKIP (asm_out_file, (unsigned HOST_WIDE_INT) 1);
6726 else
6728 if (previous_name_held_back)
6730 ASM_OUTPUT_ASCII (asm_out_file, buffer, strlen (buffer));
6731 ASM_OUTPUT_SKIP (asm_out_file, (unsigned HOST_WIDE_INT) 1);
6734 strncpy (buffer, name, sizeof buffer);
6735 previous_name_held_back = TRUE;
6737 break;
6739 case SWITCH_TYPE_DESCRIPTIVE:
6740 if (name == NULL)
6742 /* Distinguish between invocations where name is NULL. */
6743 static bool started = false;
6745 if (started)
6747 if (previous_name_held_back)
6749 ASM_OUTPUT_ASCII (asm_out_file, buffer, strlen (buffer));
6750 ASM_OUTPUT_SKIP (asm_out_file, (unsigned HOST_WIDE_INT) 1);
6753 else
6755 section * sec;
6757 sec = get_section (targetm.asm_out.record_gcc_switches_section,
6758 SECTION_DEBUG
6759 | SECTION_MERGE
6760 | SECTION_STRINGS
6761 | (SECTION_ENTSIZE & 1),
6762 NULL);
6763 switch_to_section (sec);
6764 started = true;
6768 default:
6769 break;
6772 /* The return value is currently ignored by the caller, but must be 0.
6773 For -fverbose-asm the return value would be the number of characters
6774 emitted into the assembler file. */
6775 return 0;
6778 /* Emit text to declare externally defined symbols. It is needed to
6779 properly support non-default visibility. */
6780 void
6781 default_elf_asm_output_external (FILE *file ATTRIBUTE_UNUSED,
6782 tree decl,
6783 const char *name ATTRIBUTE_UNUSED)
6785 /* We output the name if and only if TREE_SYMBOL_REFERENCED is
6786 set in order to avoid putting out names that are never really
6787 used. */
6788 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
6789 && targetm.binds_local_p (decl))
6790 maybe_assemble_visibility (decl);
6793 #include "gt-varasm.h"