PR testsuite/44195
[official-gcc.git] / gcc / varasm.c
blobf9a9d976b547d23129a65e09abec98d11af8fa54
1 /* Output variables, constants and external declarations, for GNU compiler.
2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
4 2010 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
23 /* This file handles generation of all the assembler code
24 *except* the instructions of a function.
25 This includes declarations of variables and their initial values.
27 We also output the assembler code for constants stored in memory
28 and are responsible for combining constants with the same value. */
30 #include "config.h"
31 #include "system.h"
32 #include "coretypes.h"
33 #include "tm.h"
34 #include "rtl.h"
35 #include "tree.h"
36 #include "flags.h"
37 #include "function.h"
38 #include "expr.h"
39 #include "hard-reg-set.h"
40 #include "regs.h"
41 #include "output.h"
42 #include "toplev.h"
43 #include "hashtab.h"
44 #include "ggc.h"
45 #include "langhooks.h"
46 #include "tm_p.h"
47 #include "debug.h"
48 #include "target.h"
49 #include "targhooks.h"
50 #include "tree-mudflap.h"
51 #include "cgraph.h"
52 #include "cfglayout.h"
53 #include "basic-block.h"
54 #include "tree-iterator.h"
56 #ifdef XCOFF_DEBUGGING_INFO
57 #include "xcoffout.h" /* Needed for external data
58 declarations for e.g. AIX 4.x. */
59 #endif
61 /* The (assembler) name of the first globally-visible object output. */
62 extern GTY(()) const char *first_global_object_name;
63 extern GTY(()) const char *weak_global_object_name;
65 const char *first_global_object_name;
66 const char *weak_global_object_name;
68 struct addr_const;
69 struct constant_descriptor_rtx;
70 struct rtx_constant_pool;
72 #define n_deferred_constants (crtl->varasm.deferred_constants)
74 /* Number for making the label on the next
75 constant that is stored in memory. */
77 static GTY(()) int const_labelno;
79 /* Carry information from ASM_DECLARE_OBJECT_NAME
80 to ASM_FINISH_DECLARE_OBJECT. */
82 int size_directive_output;
84 /* The last decl for which assemble_variable was called,
85 if it did ASM_DECLARE_OBJECT_NAME.
86 If the last call to assemble_variable didn't do that,
87 this holds 0. */
89 tree last_assemble_variable_decl;
91 /* The following global variable indicates if the first basic block
92 in a function belongs to the cold partition or not. */
94 bool first_function_block_is_cold;
96 /* We give all constants their own alias set. Perhaps redundant with
97 MEM_READONLY_P, but pre-dates it. */
99 static alias_set_type const_alias_set;
101 static const char *strip_reg_name (const char *);
102 static int contains_pointers_p (tree);
103 #ifdef ASM_OUTPUT_EXTERNAL
104 static bool incorporeal_function_p (tree);
105 #endif
106 static void decode_addr_const (tree, struct addr_const *);
107 static hashval_t const_desc_hash (const void *);
108 static int const_desc_eq (const void *, const void *);
109 static hashval_t const_hash_1 (const tree);
110 static int compare_constant (const tree, const tree);
111 static tree copy_constant (tree);
112 static void output_constant_def_contents (rtx);
113 static void output_addressed_constants (tree);
114 static unsigned HOST_WIDE_INT array_size_for_constructor (tree);
115 static unsigned min_align (unsigned, unsigned);
116 static void globalize_decl (tree);
117 #ifdef BSS_SECTION_ASM_OP
118 #ifdef ASM_OUTPUT_BSS
119 static void asm_output_bss (FILE *, tree, const char *,
120 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
121 #endif
122 #ifdef ASM_OUTPUT_ALIGNED_BSS
123 static void asm_output_aligned_bss (FILE *, tree, const char *,
124 unsigned HOST_WIDE_INT, int)
125 ATTRIBUTE_UNUSED;
126 #endif
127 #endif /* BSS_SECTION_ASM_OP */
128 static void mark_weak (tree);
129 static void output_constant_pool (const char *, tree);
131 /* Well-known sections, each one associated with some sort of *_ASM_OP. */
132 section *text_section;
133 section *data_section;
134 section *readonly_data_section;
135 section *sdata_section;
136 section *ctors_section;
137 section *dtors_section;
138 section *bss_section;
139 section *sbss_section;
141 /* Various forms of common section. All are guaranteed to be nonnull. */
142 section *tls_comm_section;
143 section *comm_section;
144 section *lcomm_section;
146 /* A SECTION_NOSWITCH section used for declaring global BSS variables.
147 May be null. */
148 section *bss_noswitch_section;
150 /* The section that holds the main exception table, when known. The section
151 is set either by the target's init_sections hook or by the first call to
152 switch_to_exception_section. */
153 section *exception_section;
155 /* The section that holds the DWARF2 frame unwind information, when known.
156 The section is set either by the target's init_sections hook or by the
157 first call to switch_to_eh_frame_section. */
158 section *eh_frame_section;
160 /* asm_out_file's current section. This is NULL if no section has yet
161 been selected or if we lose track of what the current section is. */
162 section *in_section;
164 /* True if code for the current function is currently being directed
165 at the cold section. */
166 bool in_cold_section_p;
168 /* A linked list of all the unnamed sections. */
169 static GTY(()) section *unnamed_sections;
171 /* Return a nonzero value if DECL has a section attribute. */
172 #define IN_NAMED_SECTION(DECL) \
173 ((TREE_CODE (DECL) == FUNCTION_DECL || TREE_CODE (DECL) == VAR_DECL) \
174 && DECL_SECTION_NAME (DECL) != NULL_TREE)
176 /* Hash table of named sections. */
177 static GTY((param_is (section))) htab_t section_htab;
179 /* A table of object_blocks, indexed by section. */
180 static GTY((param_is (struct object_block))) htab_t object_block_htab;
182 /* The next number to use for internal anchor labels. */
183 static GTY(()) int anchor_labelno;
185 /* A pool of constants that can be shared between functions. */
186 static GTY(()) struct rtx_constant_pool *shared_constant_pool;
188 /* TLS emulation. */
190 static GTY ((if_marked ("tree_map_marked_p"), param_is (struct tree_map)))
191 htab_t emutls_htab;
192 static GTY (()) tree emutls_object_type;
193 /* Emulated TLS objects have the TLS model TLS_MODEL_EMULATED. This
194 macro can be used on them to distinguish the control variable from
195 the initialization template. */
196 #define DECL_EMUTLS_VAR_P(D) (TREE_TYPE (D) == emutls_object_type)
198 #if !defined (NO_DOT_IN_LABEL)
199 # define EMUTLS_SEPARATOR "."
200 #elif !defined (NO_DOLLAR_IN_LABEL)
201 # define EMUTLS_SEPARATOR "$"
202 #else
203 # define EMUTLS_SEPARATOR "_"
204 #endif
206 /* Create an IDENTIFIER_NODE by prefixing PREFIX to the
207 IDENTIFIER_NODE NAME's name. */
209 static tree
210 prefix_name (const char *prefix, tree name)
212 unsigned plen = strlen (prefix);
213 unsigned nlen = strlen (IDENTIFIER_POINTER (name));
214 char *toname = (char *) alloca (plen + nlen + 1);
216 memcpy (toname, prefix, plen);
217 memcpy (toname + plen, IDENTIFIER_POINTER (name), nlen + 1);
219 return get_identifier (toname);
222 /* Create an identifier for the struct __emutls_object, given an identifier
223 of the DECL_ASSEMBLY_NAME of the original object. */
225 static tree
226 get_emutls_object_name (tree name)
228 const char *prefix = (targetm.emutls.var_prefix
229 ? targetm.emutls.var_prefix
230 : "__emutls_v" EMUTLS_SEPARATOR);
231 return prefix_name (prefix, name);
234 tree
235 default_emutls_var_fields (tree type, tree *name ATTRIBUTE_UNUSED)
237 tree word_type_node, field, next_field;
239 field = build_decl (UNKNOWN_LOCATION,
240 FIELD_DECL, get_identifier ("__templ"), ptr_type_node);
241 DECL_CONTEXT (field) = type;
242 next_field = field;
244 field = build_decl (UNKNOWN_LOCATION,
245 FIELD_DECL, get_identifier ("__offset"),
246 ptr_type_node);
247 DECL_CONTEXT (field) = type;
248 TREE_CHAIN (field) = next_field;
249 next_field = field;
251 word_type_node = lang_hooks.types.type_for_mode (word_mode, 1);
252 field = build_decl (UNKNOWN_LOCATION,
253 FIELD_DECL, get_identifier ("__align"),
254 word_type_node);
255 DECL_CONTEXT (field) = type;
256 TREE_CHAIN (field) = next_field;
257 next_field = field;
259 field = build_decl (UNKNOWN_LOCATION,
260 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 (UNKNOWN_LOCATION,
285 TYPE_DECL, type_name, type);
286 TYPE_NAME (type) = type_name;
287 TYPE_FIELDS (type) = field;
288 layout_type (type);
290 return type;
293 /* Create a read-only variable like DECL, with the same DECL_INITIAL.
294 This will be used for initializing the emulated tls data area. */
296 static tree
297 get_emutls_init_templ_addr (tree decl)
299 tree name, to;
301 if (targetm.emutls.register_common && !DECL_INITIAL (decl)
302 && !DECL_SECTION_NAME (decl))
303 return null_pointer_node;
305 name = DECL_ASSEMBLER_NAME (decl);
306 if (!targetm.emutls.tmpl_prefix || targetm.emutls.tmpl_prefix[0])
308 const char *prefix = (targetm.emutls.tmpl_prefix
309 ? targetm.emutls.tmpl_prefix
310 : "__emutls_t" EMUTLS_SEPARATOR);
311 name = prefix_name (prefix, name);
314 to = build_decl (DECL_SOURCE_LOCATION (decl),
315 VAR_DECL, name, TREE_TYPE (decl));
316 SET_DECL_ASSEMBLER_NAME (to, DECL_NAME (to));
318 DECL_ARTIFICIAL (to) = 1;
319 TREE_USED (to) = TREE_USED (decl);
320 TREE_READONLY (to) = 1;
321 DECL_IGNORED_P (to) = 1;
322 DECL_CONTEXT (to) = DECL_CONTEXT (decl);
323 DECL_SECTION_NAME (to) = DECL_SECTION_NAME (decl);
324 DECL_PRESERVE_P (to) = DECL_PRESERVE_P (decl);
326 DECL_WEAK (to) = DECL_WEAK (decl);
327 if (DECL_ONE_ONLY (decl))
329 make_decl_one_only (to, DECL_ASSEMBLER_NAME (to));
330 TREE_STATIC (to) = TREE_STATIC (decl);
331 TREE_PUBLIC (to) = TREE_PUBLIC (decl);
332 DECL_VISIBILITY (to) = DECL_VISIBILITY (decl);
334 else
335 TREE_STATIC (to) = 1;
337 DECL_VISIBILITY_SPECIFIED (to) = DECL_VISIBILITY_SPECIFIED (decl);
338 DECL_INITIAL (to) = DECL_INITIAL (decl);
339 DECL_INITIAL (decl) = NULL;
341 varpool_finalize_decl (to);
342 return build_fold_addr_expr (to);
345 /* When emulating tls, we use a control structure for use by the runtime.
346 Create and return this structure. */
348 tree
349 emutls_decl (tree decl)
351 tree name, to;
352 struct tree_map *h, in;
353 void **loc;
355 if (targetm.have_tls || decl == NULL || decl == error_mark_node
356 || TREE_CODE (decl) != VAR_DECL || ! DECL_THREAD_LOCAL_P (decl))
357 return decl;
359 /* Look up the object in the hash; return the control structure if
360 it has already been created. */
361 if (! emutls_htab)
362 emutls_htab = htab_create_ggc (512, tree_map_hash, tree_map_eq, 0);
364 name = DECL_ASSEMBLER_NAME (decl);
366 /* Note that we use the hash of the decl's name, rather than a hash
367 of the decl's pointer. In emutls_finish we iterate through the
368 hash table, and we want this traversal to be predictable. */
369 in.hash = IDENTIFIER_HASH_VALUE (name);
370 in.base.from = decl;
371 loc = htab_find_slot_with_hash (emutls_htab, &in, in.hash, INSERT);
372 h = (struct tree_map *) *loc;
373 if (h != NULL)
374 to = h->to;
375 else
377 to = build_decl (DECL_SOURCE_LOCATION (decl),
378 VAR_DECL, get_emutls_object_name (name),
379 get_emutls_object_type ());
381 h = ggc_alloc_tree_map ();
382 h->hash = in.hash;
383 h->base.from = decl;
384 h->to = to;
385 *(struct tree_map **) loc = h;
387 DECL_TLS_MODEL (to) = TLS_MODEL_EMULATED;
388 DECL_ARTIFICIAL (to) = 1;
389 DECL_IGNORED_P (to) = 1;
390 /* FIXME: work around PR44132. */
391 DECL_PRESERVE_P (to) = 1;
392 TREE_READONLY (to) = 0;
393 SET_DECL_ASSEMBLER_NAME (to, DECL_NAME (to));
394 if (DECL_ONE_ONLY (decl))
395 make_decl_one_only (to, DECL_ASSEMBLER_NAME (to));
396 DECL_CONTEXT (to) = DECL_CONTEXT (decl);
397 if (targetm.emutls.var_align_fixed)
398 /* If we're not allowed to change the proxy object's
399 alignment, pretend it's been set by the user. */
400 DECL_USER_ALIGN (to) = 1;
403 /* Note that these fields may need to be updated from time to time from
404 the original decl. Consider:
405 extern __thread int i;
406 int foo() { return i; }
407 __thread int i = 1;
408 in which I goes from external to locally defined and initialized. */
409 DECL_DLLIMPORT_P (to) = DECL_DLLIMPORT_P (decl);
410 DECL_ATTRIBUTES (to) = targetm.merge_decl_attributes (decl, to);
412 TREE_STATIC (to) = TREE_STATIC (decl);
413 TREE_USED (to) = TREE_USED (decl);
414 TREE_PUBLIC (to) = TREE_PUBLIC (decl);
415 DECL_EXTERNAL (to) = DECL_EXTERNAL (decl);
416 DECL_COMMON (to) = DECL_COMMON (decl);
417 DECL_WEAK (to) = DECL_WEAK (decl);
418 DECL_VISIBILITY (to) = DECL_VISIBILITY (decl);
419 DECL_VISIBILITY_SPECIFIED (to) = DECL_VISIBILITY_SPECIFIED (decl);
421 /* Fortran might pass this to us. */
422 DECL_RESTRICTED_P (to) = DECL_RESTRICTED_P (decl);
424 return to;
427 static int
428 emutls_common_1 (void **loc, void *xstmts)
430 struct tree_map *h = *(struct tree_map **) loc;
431 tree args, x, *pstmts = (tree *) xstmts;
432 tree word_type_node;
434 if (! DECL_COMMON (h->base.from)
435 || (DECL_INITIAL (h->base.from)
436 && DECL_INITIAL (h->base.from) != error_mark_node))
437 return 1;
439 word_type_node = lang_hooks.types.type_for_mode (word_mode, 1);
441 /* The idea was to call get_emutls_init_templ_addr here, but if we
442 do this and there is an initializer, -fanchor_section loses,
443 because it would be too late to ensure the template is
444 output. */
445 x = null_pointer_node;
446 args = tree_cons (NULL, x, NULL);
447 x = build_int_cst (word_type_node, DECL_ALIGN_UNIT (h->base.from));
448 args = tree_cons (NULL, x, args);
449 x = fold_convert (word_type_node, DECL_SIZE_UNIT (h->base.from));
450 args = tree_cons (NULL, x, args);
451 x = build_fold_addr_expr (h->to);
452 args = tree_cons (NULL, x, args);
454 x = built_in_decls[BUILT_IN_EMUTLS_REGISTER_COMMON];
455 x = build_function_call_expr (UNKNOWN_LOCATION, x, args);
457 append_to_statement_list (x, pstmts);
458 return 1;
461 /* Callback to finalize one emutls control variable. */
463 static int
464 emutls_finalize_control_var (void **loc,
465 void *unused ATTRIBUTE_UNUSED)
467 struct tree_map *h = *(struct tree_map **) loc;
468 if (h != NULL)
470 struct varpool_node *node = varpool_node (h->to);
471 /* Because varpool_finalize_decl () has side-effects,
472 only apply to un-finalized vars. */
473 if (node && !node->finalized)
474 varpool_finalize_decl (h->to);
476 return 1;
479 /* Finalize emutls control vars and add a static constructor if
480 required. */
482 void
483 emutls_finish (void)
485 if (emutls_htab == NULL)
486 return;
487 htab_traverse_noresize (emutls_htab,
488 emutls_finalize_control_var, NULL);
490 if (targetm.emutls.register_common)
492 tree body = NULL_TREE;
494 htab_traverse_noresize (emutls_htab, emutls_common_1, &body);
495 if (body == NULL_TREE)
496 return;
498 cgraph_build_static_cdtor ('I', body, DEFAULT_INIT_PRIORITY);
502 /* Helper routines for maintaining section_htab. */
504 static int
505 section_entry_eq (const void *p1, const void *p2)
507 const section *old = (const section *) p1;
508 const char *new_name = (const char *) p2;
510 return strcmp (old->named.name, new_name) == 0;
513 static hashval_t
514 section_entry_hash (const void *p)
516 const section *old = (const section *) p;
517 return htab_hash_string (old->named.name);
520 /* Return a hash value for section SECT. */
522 static hashval_t
523 hash_section (section *sect)
525 if (sect->common.flags & SECTION_NAMED)
526 return htab_hash_string (sect->named.name);
527 return sect->common.flags;
530 /* Helper routines for maintaining object_block_htab. */
532 static int
533 object_block_entry_eq (const void *p1, const void *p2)
535 const struct object_block *old = (const struct object_block *) p1;
536 const section *new_section = (const section *) p2;
538 return old->sect == new_section;
541 static hashval_t
542 object_block_entry_hash (const void *p)
544 const struct object_block *old = (const struct object_block *) p;
545 return hash_section (old->sect);
548 /* Return a new unnamed section with the given fields. */
550 section *
551 get_unnamed_section (unsigned int flags, void (*callback) (const void *),
552 const void *data)
554 section *sect;
556 sect = ggc_alloc_section ();
557 sect->unnamed.common.flags = flags | SECTION_UNNAMED;
558 sect->unnamed.callback = callback;
559 sect->unnamed.data = data;
560 sect->unnamed.next = unnamed_sections;
562 unnamed_sections = sect;
563 return sect;
566 /* Return a SECTION_NOSWITCH section with the given fields. */
568 static section *
569 get_noswitch_section (unsigned int flags, noswitch_section_callback callback)
571 section *sect;
573 sect = ggc_alloc_section ();
574 sect->noswitch.common.flags = flags | SECTION_NOSWITCH;
575 sect->noswitch.callback = callback;
577 return sect;
580 /* Return the named section structure associated with NAME. Create
581 a new section with the given fields if no such structure exists. */
583 section *
584 get_section (const char *name, unsigned int flags, tree decl)
586 section *sect, **slot;
588 slot = (section **)
589 htab_find_slot_with_hash (section_htab, name,
590 htab_hash_string (name), INSERT);
591 flags |= SECTION_NAMED;
592 if (*slot == NULL)
594 sect = ggc_alloc_section ();
595 sect->named.common.flags = flags;
596 sect->named.name = ggc_strdup (name);
597 sect->named.decl = decl;
598 *slot = sect;
600 else
602 sect = *slot;
603 if ((sect->common.flags & ~SECTION_DECLARED) != flags
604 && ((sect->common.flags | flags) & SECTION_OVERRIDE) == 0)
606 /* Sanity check user variables for flag changes. */
607 if (decl == 0)
608 decl = sect->named.decl;
609 gcc_assert (decl);
610 error ("%+D causes a section type conflict", decl);
613 return sect;
616 /* Return true if the current compilation mode benefits from having
617 objects grouped into blocks. */
619 static bool
620 use_object_blocks_p (void)
622 return flag_section_anchors;
625 /* Return the object_block structure for section SECT. Create a new
626 structure if we haven't created one already. Return null if SECT
627 itself is null. */
629 static struct object_block *
630 get_block_for_section (section *sect)
632 struct object_block *block;
633 void **slot;
635 if (sect == NULL)
636 return NULL;
638 slot = htab_find_slot_with_hash (object_block_htab, sect,
639 hash_section (sect), INSERT);
640 block = (struct object_block *) *slot;
641 if (block == NULL)
643 block = ggc_alloc_cleared_object_block ();
644 block->sect = sect;
645 *slot = block;
647 return block;
650 /* Create a symbol with label LABEL and place it at byte offset
651 OFFSET in BLOCK. OFFSET can be negative if the symbol's offset
652 is not yet known. LABEL must be a garbage-collected string. */
654 static rtx
655 create_block_symbol (const char *label, struct object_block *block,
656 HOST_WIDE_INT offset)
658 rtx symbol;
659 unsigned int size;
661 /* Create the extended SYMBOL_REF. */
662 size = RTX_HDR_SIZE + sizeof (struct block_symbol);
663 symbol = ggc_alloc_zone_rtx_def (size, &rtl_zone);
665 /* Initialize the normal SYMBOL_REF fields. */
666 memset (symbol, 0, size);
667 PUT_CODE (symbol, SYMBOL_REF);
668 PUT_MODE (symbol, Pmode);
669 XSTR (symbol, 0) = label;
670 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_HAS_BLOCK_INFO;
672 /* Initialize the block_symbol stuff. */
673 SYMBOL_REF_BLOCK (symbol) = block;
674 SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
676 return symbol;
679 static void
680 initialize_cold_section_name (void)
682 const char *stripped_name;
683 char *name, *buffer;
684 tree dsn;
686 gcc_assert (cfun && current_function_decl);
687 if (crtl->subsections.unlikely_text_section_name)
688 return;
690 dsn = DECL_SECTION_NAME (current_function_decl);
691 if (flag_function_sections && dsn)
693 name = (char *) alloca (TREE_STRING_LENGTH (dsn) + 1);
694 memcpy (name, TREE_STRING_POINTER (dsn), TREE_STRING_LENGTH (dsn) + 1);
696 stripped_name = targetm.strip_name_encoding (name);
698 buffer = ACONCAT ((stripped_name, "_unlikely", NULL));
699 crtl->subsections.unlikely_text_section_name = ggc_strdup (buffer);
701 else
702 crtl->subsections.unlikely_text_section_name = UNLIKELY_EXECUTED_TEXT_SECTION_NAME;
705 /* Tell assembler to switch to unlikely-to-be-executed text section. */
707 section *
708 unlikely_text_section (void)
710 if (cfun)
712 if (!crtl->subsections.unlikely_text_section_name)
713 initialize_cold_section_name ();
715 return get_named_section (NULL, crtl->subsections.unlikely_text_section_name, 0);
717 else
718 return get_named_section (NULL, UNLIKELY_EXECUTED_TEXT_SECTION_NAME, 0);
721 /* When called within a function context, return true if the function
722 has been assigned a cold text section and if SECT is that section.
723 When called outside a function context, return true if SECT is the
724 default cold section. */
726 bool
727 unlikely_text_section_p (section *sect)
729 const char *name;
731 if (cfun)
732 name = crtl->subsections.unlikely_text_section_name;
733 else
734 name = UNLIKELY_EXECUTED_TEXT_SECTION_NAME;
736 return (name
737 && sect
738 && SECTION_STYLE (sect) == SECTION_NAMED
739 && strcmp (name, sect->named.name) == 0);
742 /* Return a section with a particular name and with whatever SECTION_*
743 flags section_type_flags deems appropriate. The name of the section
744 is taken from NAME if nonnull, otherwise it is taken from DECL's
745 DECL_SECTION_NAME. DECL is the decl associated with the section
746 (see the section comment for details) and RELOC is as for
747 section_type_flags. */
749 section *
750 get_named_section (tree decl, const char *name, int reloc)
752 unsigned int flags;
754 gcc_assert (!decl || DECL_P (decl));
755 if (name == NULL)
756 name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
758 flags = targetm.section_type_flags (decl, name, reloc);
760 return get_section (name, flags, decl);
763 /* If required, set DECL_SECTION_NAME to a unique name. */
765 void
766 resolve_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED,
767 int flag_function_or_data_sections)
769 if (DECL_SECTION_NAME (decl) == NULL_TREE
770 && targetm.have_named_sections
771 && (flag_function_or_data_sections
772 || DECL_ONE_ONLY (decl)))
773 targetm.asm_out.unique_section (decl, reloc);
776 #ifdef BSS_SECTION_ASM_OP
778 #ifdef ASM_OUTPUT_BSS
780 /* Utility function for ASM_OUTPUT_BSS for targets to use if
781 they don't support alignments in .bss.
782 ??? It is believed that this function will work in most cases so such
783 support is localized here. */
785 static void
786 asm_output_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
787 const char *name,
788 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
789 unsigned HOST_WIDE_INT rounded)
791 gcc_assert (strcmp (XSTR (XEXP (DECL_RTL (decl), 0), 0), name) == 0);
792 targetm.asm_out.globalize_decl_name (file, decl);
793 switch_to_section (bss_section);
794 #ifdef ASM_DECLARE_OBJECT_NAME
795 last_assemble_variable_decl = decl;
796 ASM_DECLARE_OBJECT_NAME (file, name, decl);
797 #else
798 /* Standard thing is just output label for the object. */
799 ASM_OUTPUT_LABEL (file, name);
800 #endif /* ASM_DECLARE_OBJECT_NAME */
801 ASM_OUTPUT_SKIP (file, rounded ? rounded : 1);
804 #endif
806 #ifdef ASM_OUTPUT_ALIGNED_BSS
808 /* Utility function for targets to use in implementing
809 ASM_OUTPUT_ALIGNED_BSS.
810 ??? It is believed that this function will work in most cases so such
811 support is localized here. */
813 static void
814 asm_output_aligned_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
815 const char *name, unsigned HOST_WIDE_INT size,
816 int align)
818 switch_to_section (bss_section);
819 ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
820 #ifdef ASM_DECLARE_OBJECT_NAME
821 last_assemble_variable_decl = decl;
822 ASM_DECLARE_OBJECT_NAME (file, name, decl);
823 #else
824 /* Standard thing is just output label for the object. */
825 ASM_OUTPUT_LABEL (file, name);
826 #endif /* ASM_DECLARE_OBJECT_NAME */
827 ASM_OUTPUT_SKIP (file, size ? size : 1);
830 #endif
832 #endif /* BSS_SECTION_ASM_OP */
834 #ifndef USE_SELECT_SECTION_FOR_FUNCTIONS
835 /* Return the hot section for function DECL. Return text_section for
836 null DECLs. */
838 static section *
839 hot_function_section (tree decl)
841 if (decl != NULL_TREE
842 && DECL_SECTION_NAME (decl) != NULL_TREE
843 && targetm.have_named_sections)
844 return get_named_section (decl, NULL, 0);
845 else
846 return text_section;
848 #endif
850 /* Return the section for function DECL.
852 If DECL is NULL_TREE, return the text section. We can be passed
853 NULL_TREE under some circumstances by dbxout.c at least. */
855 section *
856 function_section (tree decl)
858 int reloc = 0;
860 if (first_function_block_is_cold)
861 reloc = 1;
863 #ifdef USE_SELECT_SECTION_FOR_FUNCTIONS
864 if (decl != NULL_TREE
865 && DECL_SECTION_NAME (decl) != NULL_TREE)
866 return reloc ? unlikely_text_section ()
867 : get_named_section (decl, NULL, 0);
868 else
869 return targetm.asm_out.select_section (decl, reloc, DECL_ALIGN (decl));
870 #else
871 return reloc ? unlikely_text_section () : hot_function_section (decl);
872 #endif
875 section *
876 current_function_section (void)
878 #ifdef USE_SELECT_SECTION_FOR_FUNCTIONS
879 if (current_function_decl != NULL_TREE
880 && DECL_SECTION_NAME (current_function_decl) != NULL_TREE)
881 return in_cold_section_p ? unlikely_text_section ()
882 : get_named_section (current_function_decl,
883 NULL, 0);
884 else
885 return targetm.asm_out.select_section (current_function_decl,
886 in_cold_section_p,
887 DECL_ALIGN (current_function_decl));
888 #else
889 return (in_cold_section_p
890 ? unlikely_text_section ()
891 : hot_function_section (current_function_decl));
892 #endif
895 /* Return the read-only data section associated with function DECL. */
897 section *
898 default_function_rodata_section (tree decl)
900 if (decl != NULL_TREE && DECL_SECTION_NAME (decl))
902 const char *name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
904 if (DECL_ONE_ONLY (decl) && HAVE_COMDAT_GROUP)
906 const char *dot;
907 size_t len;
908 char* rname;
910 dot = strchr (name + 1, '.');
911 if (!dot)
912 dot = name;
913 len = strlen (dot) + 8;
914 rname = (char *) alloca (len);
916 strcpy (rname, ".rodata");
917 strcat (rname, dot);
918 return get_section (rname, SECTION_LINKONCE, decl);
920 /* For .gnu.linkonce.t.foo we want to use .gnu.linkonce.r.foo. */
921 else if (DECL_ONE_ONLY (decl)
922 && strncmp (name, ".gnu.linkonce.t.", 16) == 0)
924 size_t len = strlen (name) + 1;
925 char *rname = (char *) alloca (len);
927 memcpy (rname, name, len);
928 rname[14] = 'r';
929 return get_section (rname, SECTION_LINKONCE, decl);
931 /* For .text.foo we want to use .rodata.foo. */
932 else if (flag_function_sections && flag_data_sections
933 && strncmp (name, ".text.", 6) == 0)
935 size_t len = strlen (name) + 1;
936 char *rname = (char *) alloca (len + 2);
938 memcpy (rname, ".rodata", 7);
939 memcpy (rname + 7, name + 5, len - 5);
940 return get_section (rname, 0, decl);
944 return readonly_data_section;
947 /* Return the read-only data section associated with function DECL
948 for targets where that section should be always the single
949 readonly data section. */
951 section *
952 default_no_function_rodata_section (tree decl ATTRIBUTE_UNUSED)
954 return readonly_data_section;
957 /* Return the section to use for string merging. */
959 static section *
960 mergeable_string_section (tree decl ATTRIBUTE_UNUSED,
961 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
962 unsigned int flags ATTRIBUTE_UNUSED)
964 HOST_WIDE_INT len;
966 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
967 && TREE_CODE (decl) == STRING_CST
968 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
969 && align <= 256
970 && (len = int_size_in_bytes (TREE_TYPE (decl))) > 0
971 && TREE_STRING_LENGTH (decl) >= len)
973 enum machine_mode mode;
974 unsigned int modesize;
975 const char *str;
976 HOST_WIDE_INT i;
977 int j, unit;
978 char name[30];
980 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (decl)));
981 modesize = GET_MODE_BITSIZE (mode);
982 if (modesize >= 8 && modesize <= 256
983 && (modesize & (modesize - 1)) == 0)
985 if (align < modesize)
986 align = modesize;
988 str = TREE_STRING_POINTER (decl);
989 unit = GET_MODE_SIZE (mode);
991 /* Check for embedded NUL characters. */
992 for (i = 0; i < len; i += unit)
994 for (j = 0; j < unit; j++)
995 if (str[i + j] != '\0')
996 break;
997 if (j == unit)
998 break;
1000 if (i == len - unit)
1002 sprintf (name, ".rodata.str%d.%d", modesize / 8,
1003 (int) (align / 8));
1004 flags |= (modesize / 8) | SECTION_MERGE | SECTION_STRINGS;
1005 return get_section (name, flags, NULL);
1010 return readonly_data_section;
1013 /* Return the section to use for constant merging. */
1015 section *
1016 mergeable_constant_section (enum machine_mode mode ATTRIBUTE_UNUSED,
1017 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
1018 unsigned int flags ATTRIBUTE_UNUSED)
1020 unsigned int modesize = GET_MODE_BITSIZE (mode);
1022 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
1023 && mode != VOIDmode
1024 && mode != BLKmode
1025 && modesize <= align
1026 && align >= 8
1027 && align <= 256
1028 && (align & (align - 1)) == 0)
1030 char name[24];
1032 sprintf (name, ".rodata.cst%d", (int) (align / 8));
1033 flags |= (align / 8) | SECTION_MERGE;
1034 return get_section (name, flags, NULL);
1036 return readonly_data_section;
1039 /* Given NAME, a putative register name, discard any customary prefixes. */
1041 static const char *
1042 strip_reg_name (const char *name)
1044 #ifdef REGISTER_PREFIX
1045 if (!strncmp (name, REGISTER_PREFIX, strlen (REGISTER_PREFIX)))
1046 name += strlen (REGISTER_PREFIX);
1047 #endif
1048 if (name[0] == '%' || name[0] == '#')
1049 name++;
1050 return name;
1053 /* The user has asked for a DECL to have a particular name. Set (or
1054 change) it in such a way that we don't prefix an underscore to
1055 it. */
1056 void
1057 set_user_assembler_name (tree decl, const char *name)
1059 char *starred = (char *) alloca (strlen (name) + 2);
1060 starred[0] = '*';
1061 strcpy (starred + 1, name);
1062 change_decl_assembler_name (decl, get_identifier (starred));
1063 SET_DECL_RTL (decl, NULL_RTX);
1066 /* Decode an `asm' spec for a declaration as a register name.
1067 Return the register number, or -1 if nothing specified,
1068 or -2 if the ASMSPEC is not `cc' or `memory' and is not recognized,
1069 or -3 if ASMSPEC is `cc' and is not recognized,
1070 or -4 if ASMSPEC is `memory' and is not recognized.
1071 Accept an exact spelling or a decimal number.
1072 Prefixes such as % are optional. */
1075 decode_reg_name (const char *asmspec)
1077 if (asmspec != 0)
1079 int i;
1081 /* Get rid of confusing prefixes. */
1082 asmspec = strip_reg_name (asmspec);
1084 /* Allow a decimal number as a "register name". */
1085 for (i = strlen (asmspec) - 1; i >= 0; i--)
1086 if (! ISDIGIT (asmspec[i]))
1087 break;
1088 if (asmspec[0] != 0 && i < 0)
1090 i = atoi (asmspec);
1091 if (i < FIRST_PSEUDO_REGISTER && i >= 0)
1092 return i;
1093 else
1094 return -2;
1097 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1098 if (reg_names[i][0]
1099 && ! strcmp (asmspec, strip_reg_name (reg_names[i])))
1100 return i;
1102 #ifdef ADDITIONAL_REGISTER_NAMES
1104 static const struct { const char *const name; const int number; } table[]
1105 = ADDITIONAL_REGISTER_NAMES;
1107 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
1108 if (table[i].name[0]
1109 && ! strcmp (asmspec, table[i].name))
1110 return table[i].number;
1112 #endif /* ADDITIONAL_REGISTER_NAMES */
1114 if (!strcmp (asmspec, "memory"))
1115 return -4;
1117 if (!strcmp (asmspec, "cc"))
1118 return -3;
1120 return -2;
1123 return -1;
1126 /* Return true if DECL's initializer is suitable for a BSS section. */
1128 static bool
1129 bss_initializer_p (const_tree decl)
1131 return (DECL_INITIAL (decl) == NULL
1132 || DECL_INITIAL (decl) == error_mark_node
1133 || (flag_zero_initialized_in_bss
1134 /* Leave constant zeroes in .rodata so they
1135 can be shared. */
1136 && !TREE_READONLY (decl)
1137 && initializer_zerop (DECL_INITIAL (decl))));
1140 /* Compute the alignment of variable specified by DECL.
1141 DONT_OUTPUT_DATA is from assemble_variable. */
1143 void
1144 align_variable (tree decl, bool dont_output_data)
1146 unsigned int align = DECL_ALIGN (decl);
1148 /* In the case for initialing an array whose length isn't specified,
1149 where we have not yet been able to do the layout,
1150 figure out the proper alignment now. */
1151 if (dont_output_data && DECL_SIZE (decl) == 0
1152 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
1153 align = MAX (align, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (decl))));
1155 /* Some object file formats have a maximum alignment which they support.
1156 In particular, a.out format supports a maximum alignment of 4. */
1157 if (align > MAX_OFILE_ALIGNMENT)
1159 warning (0, "alignment of %q+D is greater than maximum object "
1160 "file alignment. Using %d", decl,
1161 MAX_OFILE_ALIGNMENT/BITS_PER_UNIT);
1162 align = MAX_OFILE_ALIGNMENT;
1165 /* On some machines, it is good to increase alignment sometimes. */
1166 if (! DECL_USER_ALIGN (decl))
1168 #ifdef DATA_ALIGNMENT
1169 unsigned int data_align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
1170 /* Don't increase alignment too much for TLS variables - TLS space
1171 is too precious. */
1172 if (! DECL_THREAD_LOCAL_P (decl) || data_align <= BITS_PER_WORD)
1173 align = data_align;
1174 #endif
1175 #ifdef CONSTANT_ALIGNMENT
1176 if (DECL_INITIAL (decl) != 0 && DECL_INITIAL (decl) != error_mark_node)
1178 unsigned int const_align = CONSTANT_ALIGNMENT (DECL_INITIAL (decl),
1179 align);
1180 /* Don't increase alignment too much for TLS variables - TLS space
1181 is too precious. */
1182 if (! DECL_THREAD_LOCAL_P (decl) || const_align <= BITS_PER_WORD)
1183 align = const_align;
1185 #endif
1188 /* Reset the alignment in case we have made it tighter, so we can benefit
1189 from it in get_pointer_alignment. */
1190 DECL_ALIGN (decl) = align;
1193 /* Return the section into which the given VAR_DECL or CONST_DECL
1194 should be placed. PREFER_NOSWITCH_P is true if a noswitch
1195 section should be used wherever possible. */
1197 static section *
1198 get_variable_section (tree decl, bool prefer_noswitch_p)
1200 addr_space_t as = ADDR_SPACE_GENERIC;
1201 int reloc;
1203 if (TREE_TYPE (decl) != error_mark_node)
1204 as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
1206 if (DECL_COMMON (decl))
1208 /* If the decl has been given an explicit section name, or it resides
1209 in a non-generic address space, then it isn't common, and shouldn't
1210 be handled as such. */
1211 gcc_assert (DECL_SECTION_NAME (decl) == NULL
1212 && ADDR_SPACE_GENERIC_P (as));
1213 if (DECL_THREAD_LOCAL_P (decl))
1214 return tls_comm_section;
1215 /* This cannot be common bss for an emulated TLS object without
1216 a register_common hook. */
1217 else if (DECL_TLS_MODEL (decl) == TLS_MODEL_EMULATED
1218 && !targetm.emutls.register_common)
1220 else if (TREE_PUBLIC (decl) && bss_initializer_p (decl))
1221 return comm_section;
1224 if (DECL_INITIAL (decl) == error_mark_node)
1225 reloc = contains_pointers_p (TREE_TYPE (decl)) ? 3 : 0;
1226 else if (DECL_INITIAL (decl))
1227 reloc = compute_reloc_for_constant (DECL_INITIAL (decl));
1228 else
1229 reloc = 0;
1231 resolve_unique_section (decl, reloc, flag_data_sections);
1232 if (IN_NAMED_SECTION (decl))
1233 return get_named_section (decl, NULL, reloc);
1235 if (ADDR_SPACE_GENERIC_P (as)
1236 && !DECL_THREAD_LOCAL_P (decl)
1237 && !(prefer_noswitch_p && targetm.have_switchable_bss_sections)
1238 && bss_initializer_p (decl))
1240 if (!TREE_PUBLIC (decl))
1241 return lcomm_section;
1242 if (bss_noswitch_section)
1243 return bss_noswitch_section;
1246 return targetm.asm_out.select_section (decl, reloc, DECL_ALIGN (decl));
1249 /* Return the block into which object_block DECL should be placed. */
1251 static struct object_block *
1252 get_block_for_decl (tree decl)
1254 section *sect;
1256 if (TREE_CODE (decl) == VAR_DECL)
1258 /* The object must be defined in this translation unit. */
1259 if (DECL_EXTERNAL (decl))
1260 return NULL;
1262 /* There's no point using object blocks for something that is
1263 isolated by definition. */
1264 if (DECL_ONE_ONLY (decl))
1265 return NULL;
1268 /* We can only calculate block offsets if the decl has a known
1269 constant size. */
1270 if (DECL_SIZE_UNIT (decl) == NULL)
1271 return NULL;
1272 if (!host_integerp (DECL_SIZE_UNIT (decl), 1))
1273 return NULL;
1275 /* Find out which section should contain DECL. We cannot put it into
1276 an object block if it requires a standalone definition. */
1277 if (TREE_CODE (decl) == VAR_DECL)
1278 align_variable (decl, 0);
1279 sect = get_variable_section (decl, true);
1280 if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
1281 return NULL;
1283 return get_block_for_section (sect);
1286 /* Make sure block symbol SYMBOL is in block BLOCK. */
1288 static void
1289 change_symbol_block (rtx symbol, struct object_block *block)
1291 if (block != SYMBOL_REF_BLOCK (symbol))
1293 gcc_assert (SYMBOL_REF_BLOCK_OFFSET (symbol) < 0);
1294 SYMBOL_REF_BLOCK (symbol) = block;
1298 /* Return true if it is possible to put DECL in an object_block. */
1300 static bool
1301 use_blocks_for_decl_p (tree decl)
1303 /* Only data DECLs can be placed into object blocks. */
1304 if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != CONST_DECL)
1305 return false;
1307 /* Detect decls created by dw2_force_const_mem. Such decls are
1308 special because DECL_INITIAL doesn't specify the decl's true value.
1309 dw2_output_indirect_constants will instead call assemble_variable
1310 with dont_output_data set to 1 and then print the contents itself. */
1311 if (DECL_INITIAL (decl) == decl)
1312 return false;
1314 /* If this decl is an alias, then we don't want to emit a definition. */
1315 if (lookup_attribute ("alias", DECL_ATTRIBUTES (decl)))
1316 return false;
1318 return true;
1321 /* Create the DECL_RTL for a VAR_DECL or FUNCTION_DECL. DECL should
1322 have static storage duration. In other words, it should not be an
1323 automatic variable, including PARM_DECLs.
1325 There is, however, one exception: this function handles variables
1326 explicitly placed in a particular register by the user.
1328 This is never called for PARM_DECL nodes. */
1330 void
1331 make_decl_rtl (tree decl)
1333 const char *name = 0;
1334 int reg_number;
1335 rtx x;
1337 /* Check that we are not being given an automatic variable. */
1338 gcc_assert (TREE_CODE (decl) != PARM_DECL
1339 && TREE_CODE (decl) != RESULT_DECL);
1341 /* A weak alias has TREE_PUBLIC set but not the other bits. */
1342 gcc_assert (TREE_CODE (decl) != VAR_DECL
1343 || TREE_STATIC (decl)
1344 || TREE_PUBLIC (decl)
1345 || DECL_EXTERNAL (decl)
1346 || DECL_REGISTER (decl));
1348 /* And that we were not given a type or a label. */
1349 gcc_assert (TREE_CODE (decl) != TYPE_DECL
1350 && TREE_CODE (decl) != LABEL_DECL);
1352 /* For a duplicate declaration, we can be called twice on the
1353 same DECL node. Don't discard the RTL already made. */
1354 if (DECL_RTL_SET_P (decl))
1356 /* If the old RTL had the wrong mode, fix the mode. */
1357 x = DECL_RTL (decl);
1358 if (GET_MODE (x) != DECL_MODE (decl))
1359 SET_DECL_RTL (decl, adjust_address_nv (x, DECL_MODE (decl), 0));
1361 if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1362 return;
1364 /* ??? Another way to do this would be to maintain a hashed
1365 table of such critters. Instead of adding stuff to a DECL
1366 to give certain attributes to it, we could use an external
1367 hash map from DECL to set of attributes. */
1369 /* Let the target reassign the RTL if it wants.
1370 This is necessary, for example, when one machine specific
1371 decl attribute overrides another. */
1372 targetm.encode_section_info (decl, DECL_RTL (decl), false);
1374 /* If the symbol has a SYMBOL_REF_BLOCK field, update it based
1375 on the new decl information. */
1376 if (MEM_P (x)
1377 && GET_CODE (XEXP (x, 0)) == SYMBOL_REF
1378 && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (x, 0)))
1379 change_symbol_block (XEXP (x, 0), get_block_for_decl (decl));
1381 /* Make this function static known to the mudflap runtime. */
1382 if (flag_mudflap && TREE_CODE (decl) == VAR_DECL)
1383 mudflap_enqueue_decl (decl);
1385 return;
1388 /* If this variable belongs to the global constant pool, retrieve the
1389 pre-computed RTL or recompute it in LTO mode. */
1390 if (TREE_CODE (decl) == VAR_DECL && DECL_IN_CONSTANT_POOL (decl))
1392 SET_DECL_RTL (decl, output_constant_def (DECL_INITIAL (decl), 1));
1393 return;
1396 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
1398 if (name[0] != '*' && TREE_CODE (decl) != FUNCTION_DECL
1399 && DECL_REGISTER (decl))
1401 error ("register name not specified for %q+D", decl);
1403 else if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1405 const char *asmspec = name+1;
1406 reg_number = decode_reg_name (asmspec);
1407 /* First detect errors in declaring global registers. */
1408 if (reg_number == -1)
1409 error ("register name not specified for %q+D", decl);
1410 else if (reg_number < 0)
1411 error ("invalid register name for %q+D", decl);
1412 else if (TYPE_MODE (TREE_TYPE (decl)) == BLKmode)
1413 error ("data type of %q+D isn%'t suitable for a register",
1414 decl);
1415 else if (! HARD_REGNO_MODE_OK (reg_number, TYPE_MODE (TREE_TYPE (decl))))
1416 error ("register specified for %q+D isn%'t suitable for data type",
1417 decl);
1418 /* Now handle properly declared static register variables. */
1419 else
1421 int nregs;
1423 if (DECL_INITIAL (decl) != 0 && TREE_STATIC (decl))
1425 DECL_INITIAL (decl) = 0;
1426 error ("global register variable has initial value");
1428 if (TREE_THIS_VOLATILE (decl))
1429 warning (OPT_Wvolatile_register_var,
1430 "optimization may eliminate reads and/or "
1431 "writes to register variables");
1433 /* If the user specified one of the eliminables registers here,
1434 e.g., FRAME_POINTER_REGNUM, we don't want to get this variable
1435 confused with that register and be eliminated. This usage is
1436 somewhat suspect... */
1438 SET_DECL_RTL (decl, gen_rtx_raw_REG (DECL_MODE (decl), reg_number));
1439 ORIGINAL_REGNO (DECL_RTL (decl)) = reg_number;
1440 REG_USERVAR_P (DECL_RTL (decl)) = 1;
1442 if (TREE_STATIC (decl))
1444 /* Make this register global, so not usable for anything
1445 else. */
1446 #ifdef ASM_DECLARE_REGISTER_GLOBAL
1447 name = IDENTIFIER_POINTER (DECL_NAME (decl));
1448 ASM_DECLARE_REGISTER_GLOBAL (asm_out_file, decl, reg_number, name);
1449 #endif
1450 nregs = hard_regno_nregs[reg_number][DECL_MODE (decl)];
1451 while (nregs > 0)
1452 globalize_reg (reg_number + --nregs);
1455 /* As a register variable, it has no section. */
1456 return;
1459 /* Now handle ordinary static variables and functions (in memory).
1460 Also handle vars declared register invalidly. */
1461 else if (name[0] == '*')
1463 #ifdef REGISTER_PREFIX
1464 if (strlen (REGISTER_PREFIX) != 0)
1466 reg_number = decode_reg_name (name);
1467 if (reg_number >= 0 || reg_number == -3)
1468 error ("register name given for non-register variable %q+D", decl);
1470 #endif
1473 /* Specifying a section attribute on a variable forces it into a
1474 non-.bss section, and thus it cannot be common. */
1475 /* FIXME: In general this code should not be necessary because
1476 visibility pass is doing the same work. But notice_global_symbol
1477 is called early and it needs to make DECL_RTL to get the name.
1478 we take care of recomputing the DECL_RTL after visibility is changed. */
1479 if (TREE_CODE (decl) == VAR_DECL
1480 && DECL_SECTION_NAME (decl) != NULL_TREE
1481 && DECL_INITIAL (decl) == NULL_TREE
1482 && DECL_COMMON (decl))
1483 DECL_COMMON (decl) = 0;
1485 /* Variables can't be both common and weak. */
1486 if (TREE_CODE (decl) == VAR_DECL && DECL_WEAK (decl))
1487 DECL_COMMON (decl) = 0;
1489 if (use_object_blocks_p () && use_blocks_for_decl_p (decl))
1490 x = create_block_symbol (name, get_block_for_decl (decl), -1);
1491 else
1493 enum machine_mode address_mode = Pmode;
1494 if (TREE_TYPE (decl) != error_mark_node)
1496 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
1497 address_mode = targetm.addr_space.address_mode (as);
1499 x = gen_rtx_SYMBOL_REF (address_mode, name);
1501 SYMBOL_REF_WEAK (x) = DECL_WEAK (decl);
1502 SET_SYMBOL_REF_DECL (x, decl);
1504 x = gen_rtx_MEM (DECL_MODE (decl), x);
1505 if (TREE_CODE (decl) != FUNCTION_DECL)
1506 set_mem_attributes (x, decl, 1);
1507 SET_DECL_RTL (decl, x);
1509 /* Optionally set flags or add text to the name to record information
1510 such as that it is a function name.
1511 If the name is changed, the macro ASM_OUTPUT_LABELREF
1512 will have to know how to strip this information. */
1513 targetm.encode_section_info (decl, DECL_RTL (decl), true);
1515 /* Make this function static known to the mudflap runtime. */
1516 if (flag_mudflap && TREE_CODE (decl) == VAR_DECL)
1517 mudflap_enqueue_decl (decl);
1520 /* Like make_decl_rtl, but inhibit creation of new alias sets when
1521 calling make_decl_rtl. Also, reset DECL_RTL before returning the
1522 rtl. */
1525 make_decl_rtl_for_debug (tree decl)
1527 unsigned int save_aliasing_flag, save_mudflap_flag;
1528 rtx rtl;
1530 if (DECL_RTL_SET_P (decl))
1531 return DECL_RTL (decl);
1533 /* Kludge alert! Somewhere down the call chain, make_decl_rtl will
1534 call new_alias_set. If running with -fcompare-debug, sometimes
1535 we do not want to create alias sets that will throw the alias
1536 numbers off in the comparison dumps. So... clearing
1537 flag_strict_aliasing will keep new_alias_set() from creating a
1538 new set. It is undesirable to register decl with mudflap
1539 in this case as well. */
1540 save_aliasing_flag = flag_strict_aliasing;
1541 flag_strict_aliasing = 0;
1542 save_mudflap_flag = flag_mudflap;
1543 flag_mudflap = 0;
1545 rtl = DECL_RTL (decl);
1546 /* Reset DECL_RTL back, as various parts of the compiler expects
1547 DECL_RTL set meaning it is actually going to be output. */
1548 SET_DECL_RTL (decl, NULL);
1550 flag_strict_aliasing = save_aliasing_flag;
1551 flag_mudflap = save_mudflap_flag;
1553 return rtl;
1556 /* Output a string of literal assembler code
1557 for an `asm' keyword used between functions. */
1559 void
1560 assemble_asm (tree string)
1562 app_enable ();
1564 if (TREE_CODE (string) == ADDR_EXPR)
1565 string = TREE_OPERAND (string, 0);
1567 fprintf (asm_out_file, "\t%s\n", TREE_STRING_POINTER (string));
1570 /* Record an element in the table of global destructors. SYMBOL is
1571 a SYMBOL_REF of the function to be called; PRIORITY is a number
1572 between 0 and MAX_INIT_PRIORITY. */
1574 void
1575 default_stabs_asm_out_destructor (rtx symbol ATTRIBUTE_UNUSED,
1576 int priority ATTRIBUTE_UNUSED)
1578 #if defined DBX_DEBUGGING_INFO || defined XCOFF_DEBUGGING_INFO
1579 /* Tell GNU LD that this is part of the static destructor set.
1580 This will work for any system that uses stabs, most usefully
1581 aout systems. */
1582 dbxout_begin_simple_stabs ("___DTOR_LIST__", 22 /* N_SETT */);
1583 dbxout_stab_value_label (XSTR (symbol, 0));
1584 #else
1585 sorry ("global destructors not supported on this target");
1586 #endif
1589 /* Write the address of the entity given by SYMBOL to SEC. */
1590 void
1591 assemble_addr_to_section (rtx symbol, section *sec)
1593 switch_to_section (sec);
1594 assemble_align (POINTER_SIZE);
1595 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1598 /* Return the numbered .ctors.N (if CONSTRUCTOR_P) or .dtors.N (if
1599 not) section for PRIORITY. */
1600 section *
1601 get_cdtor_priority_section (int priority, bool constructor_p)
1603 char buf[16];
1605 /* ??? This only works reliably with the GNU linker. */
1606 sprintf (buf, "%s.%.5u",
1607 constructor_p ? ".ctors" : ".dtors",
1608 /* Invert the numbering so the linker puts us in the proper
1609 order; constructors are run from right to left, and the
1610 linker sorts in increasing order. */
1611 MAX_INIT_PRIORITY - priority);
1612 return get_section (buf, SECTION_WRITE, NULL);
1615 void
1616 default_named_section_asm_out_destructor (rtx symbol, int priority)
1618 section *sec;
1620 if (priority != DEFAULT_INIT_PRIORITY)
1621 sec = get_cdtor_priority_section (priority,
1622 /*constructor_p=*/false);
1623 else
1624 sec = get_section (".dtors", SECTION_WRITE, NULL);
1626 assemble_addr_to_section (symbol, sec);
1629 #ifdef DTORS_SECTION_ASM_OP
1630 void
1631 default_dtor_section_asm_out_destructor (rtx symbol,
1632 int priority ATTRIBUTE_UNUSED)
1634 assemble_addr_to_section (symbol, dtors_section);
1636 #endif
1638 /* Likewise for global constructors. */
1640 void
1641 default_stabs_asm_out_constructor (rtx symbol ATTRIBUTE_UNUSED,
1642 int priority ATTRIBUTE_UNUSED)
1644 #if defined DBX_DEBUGGING_INFO || defined XCOFF_DEBUGGING_INFO
1645 /* Tell GNU LD that this is part of the static destructor set.
1646 This will work for any system that uses stabs, most usefully
1647 aout systems. */
1648 dbxout_begin_simple_stabs ("___CTOR_LIST__", 22 /* N_SETT */);
1649 dbxout_stab_value_label (XSTR (symbol, 0));
1650 #else
1651 sorry ("global constructors not supported on this target");
1652 #endif
1655 void
1656 default_named_section_asm_out_constructor (rtx symbol, int priority)
1658 section *sec;
1660 if (priority != DEFAULT_INIT_PRIORITY)
1661 sec = get_cdtor_priority_section (priority,
1662 /*constructor_p=*/true);
1663 else
1664 sec = get_section (".ctors", SECTION_WRITE, NULL);
1666 assemble_addr_to_section (symbol, sec);
1669 #ifdef CTORS_SECTION_ASM_OP
1670 void
1671 default_ctor_section_asm_out_constructor (rtx symbol,
1672 int priority ATTRIBUTE_UNUSED)
1674 assemble_addr_to_section (symbol, ctors_section);
1676 #endif
1678 /* CONSTANT_POOL_BEFORE_FUNCTION may be defined as an expression with
1679 a nonzero value if the constant pool should be output before the
1680 start of the function, or a zero value if the pool should output
1681 after the end of the function. The default is to put it before the
1682 start. */
1684 #ifndef CONSTANT_POOL_BEFORE_FUNCTION
1685 #define CONSTANT_POOL_BEFORE_FUNCTION 1
1686 #endif
1688 /* DECL is an object (either VAR_DECL or FUNCTION_DECL) which is going
1689 to be output to assembler.
1690 Set first_global_object_name and weak_global_object_name as appropriate. */
1692 void
1693 notice_global_symbol (tree decl)
1695 const char **type = &first_global_object_name;
1697 if (first_global_object_name
1698 || !TREE_PUBLIC (decl)
1699 || DECL_EXTERNAL (decl)
1700 || !DECL_NAME (decl)
1701 || (TREE_CODE (decl) != FUNCTION_DECL
1702 && (TREE_CODE (decl) != VAR_DECL
1703 || (DECL_COMMON (decl)
1704 && (DECL_INITIAL (decl) == 0
1705 || DECL_INITIAL (decl) == error_mark_node))))
1706 || !MEM_P (DECL_RTL (decl)))
1707 return;
1709 /* We win when global object is found, but it is useful to know about weak
1710 symbol as well so we can produce nicer unique names. */
1711 if (DECL_WEAK (decl) || DECL_ONE_ONLY (decl) || flag_shlib)
1712 type = &weak_global_object_name;
1714 if (!*type)
1716 const char *p;
1717 const char *name;
1718 rtx decl_rtl = DECL_RTL (decl);
1720 p = targetm.strip_name_encoding (XSTR (XEXP (decl_rtl, 0), 0));
1721 name = ggc_strdup (p);
1723 *type = name;
1727 /* Output assembler code for the constant pool of a function and associated
1728 with defining the name of the function. DECL describes the function.
1729 NAME is the function's name. For the constant pool, we use the current
1730 constant pool data. */
1732 void
1733 assemble_start_function (tree decl, const char *fnname)
1735 int align;
1736 char tmp_label[100];
1737 bool hot_label_written = false;
1739 crtl->subsections.unlikely_text_section_name = NULL;
1741 first_function_block_is_cold = false;
1742 if (flag_reorder_blocks_and_partition)
1744 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTB", const_labelno);
1745 crtl->subsections.hot_section_label = ggc_strdup (tmp_label);
1746 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDB", const_labelno);
1747 crtl->subsections.cold_section_label = ggc_strdup (tmp_label);
1748 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTE", const_labelno);
1749 crtl->subsections.hot_section_end_label = ggc_strdup (tmp_label);
1750 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDE", const_labelno);
1751 crtl->subsections.cold_section_end_label = ggc_strdup (tmp_label);
1752 const_labelno++;
1754 else
1756 crtl->subsections.hot_section_label = NULL;
1757 crtl->subsections.cold_section_label = NULL;
1758 crtl->subsections.hot_section_end_label = NULL;
1759 crtl->subsections.cold_section_end_label = NULL;
1762 /* The following code does not need preprocessing in the assembler. */
1764 app_disable ();
1766 if (CONSTANT_POOL_BEFORE_FUNCTION)
1767 output_constant_pool (fnname, decl);
1769 resolve_unique_section (decl, 0, flag_function_sections);
1771 /* Make sure the not and cold text (code) sections are properly
1772 aligned. This is necessary here in the case where the function
1773 has both hot and cold sections, because we don't want to re-set
1774 the alignment when the section switch happens mid-function. */
1776 if (flag_reorder_blocks_and_partition)
1778 switch_to_section (unlikely_text_section ());
1779 assemble_align (DECL_ALIGN (decl));
1780 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_label);
1782 /* When the function starts with a cold section, we need to explicitly
1783 align the hot section and write out the hot section label.
1784 But if the current function is a thunk, we do not have a CFG. */
1785 if (!cfun->is_thunk
1786 && BB_PARTITION (ENTRY_BLOCK_PTR->next_bb) == BB_COLD_PARTITION)
1788 switch_to_section (text_section);
1789 assemble_align (DECL_ALIGN (decl));
1790 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
1791 hot_label_written = true;
1792 first_function_block_is_cold = true;
1795 else if (DECL_SECTION_NAME (decl))
1797 /* Calls to function_section rely on first_function_block_is_cold
1798 being accurate. The first block may be cold even if we aren't
1799 doing partitioning, if the entire function was decided by
1800 choose_function_section (predict.c) to be cold. */
1802 initialize_cold_section_name ();
1804 if (crtl->subsections.unlikely_text_section_name
1805 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
1806 crtl->subsections.unlikely_text_section_name) == 0)
1807 first_function_block_is_cold = true;
1810 in_cold_section_p = first_function_block_is_cold;
1812 /* Switch to the correct text section for the start of the function. */
1814 switch_to_section (function_section (decl));
1815 if (flag_reorder_blocks_and_partition
1816 && !hot_label_written)
1817 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
1819 /* Tell assembler to move to target machine's alignment for functions. */
1820 align = floor_log2 (DECL_ALIGN (decl) / BITS_PER_UNIT);
1821 if (align > 0)
1823 ASM_OUTPUT_ALIGN (asm_out_file, align);
1826 /* Handle a user-specified function alignment.
1827 Note that we still need to align to DECL_ALIGN, as above,
1828 because ASM_OUTPUT_MAX_SKIP_ALIGN might not do any alignment at all. */
1829 if (! DECL_USER_ALIGN (decl)
1830 && align_functions_log > align
1831 && optimize_function_for_speed_p (cfun))
1833 #ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
1834 ASM_OUTPUT_MAX_SKIP_ALIGN (asm_out_file,
1835 align_functions_log, align_functions - 1);
1836 #else
1837 ASM_OUTPUT_ALIGN (asm_out_file, align_functions_log);
1838 #endif
1841 #ifdef ASM_OUTPUT_FUNCTION_PREFIX
1842 ASM_OUTPUT_FUNCTION_PREFIX (asm_out_file, fnname);
1843 #endif
1845 if (!DECL_IGNORED_P (decl))
1846 (*debug_hooks->begin_function) (decl);
1848 /* Make function name accessible from other files, if appropriate. */
1850 if (TREE_PUBLIC (decl))
1852 notice_global_symbol (decl);
1854 globalize_decl (decl);
1856 maybe_assemble_visibility (decl);
1859 if (DECL_PRESERVE_P (decl))
1860 targetm.asm_out.mark_decl_preserved (fnname);
1862 /* Do any machine/system dependent processing of the function name. */
1863 #ifdef ASM_DECLARE_FUNCTION_NAME
1864 ASM_DECLARE_FUNCTION_NAME (asm_out_file, fnname, current_function_decl);
1865 #else
1866 /* Standard thing is just output label for the function. */
1867 ASM_OUTPUT_LABEL (asm_out_file, fnname);
1868 #endif /* ASM_DECLARE_FUNCTION_NAME */
1871 /* Output assembler code associated with defining the size of the
1872 function. DECL describes the function. NAME is the function's name. */
1874 void
1875 assemble_end_function (tree decl, const char *fnname ATTRIBUTE_UNUSED)
1877 #ifdef ASM_DECLARE_FUNCTION_SIZE
1878 /* We could have switched section in the middle of the function. */
1879 if (flag_reorder_blocks_and_partition)
1880 switch_to_section (function_section (decl));
1881 ASM_DECLARE_FUNCTION_SIZE (asm_out_file, fnname, decl);
1882 #endif
1883 if (! CONSTANT_POOL_BEFORE_FUNCTION)
1885 output_constant_pool (fnname, decl);
1886 switch_to_section (function_section (decl)); /* need to switch back */
1888 /* Output labels for end of hot/cold text sections (to be used by
1889 debug info.) */
1890 if (flag_reorder_blocks_and_partition)
1892 section *save_text_section;
1894 save_text_section = in_section;
1895 switch_to_section (unlikely_text_section ());
1896 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_end_label);
1897 if (first_function_block_is_cold)
1898 switch_to_section (text_section);
1899 else
1900 switch_to_section (function_section (decl));
1901 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_end_label);
1902 switch_to_section (save_text_section);
1906 /* Assemble code to leave SIZE bytes of zeros. */
1908 void
1909 assemble_zeros (unsigned HOST_WIDE_INT size)
1911 /* Do no output if -fsyntax-only. */
1912 if (flag_syntax_only)
1913 return;
1915 #ifdef ASM_NO_SKIP_IN_TEXT
1916 /* The `space' pseudo in the text section outputs nop insns rather than 0s,
1917 so we must output 0s explicitly in the text section. */
1918 if (ASM_NO_SKIP_IN_TEXT && (in_section->common.flags & SECTION_CODE) != 0)
1920 unsigned HOST_WIDE_INT i;
1921 for (i = 0; i < size; i++)
1922 assemble_integer (const0_rtx, 1, BITS_PER_UNIT, 1);
1924 else
1925 #endif
1926 if (size > 0)
1927 ASM_OUTPUT_SKIP (asm_out_file, size);
1930 /* Assemble an alignment pseudo op for an ALIGN-bit boundary. */
1932 void
1933 assemble_align (int align)
1935 if (align > BITS_PER_UNIT)
1937 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
1941 /* Assemble a string constant with the specified C string as contents. */
1943 void
1944 assemble_string (const char *p, int size)
1946 int pos = 0;
1947 int maximum = 2000;
1949 /* If the string is very long, split it up. */
1951 while (pos < size)
1953 int thissize = size - pos;
1954 if (thissize > maximum)
1955 thissize = maximum;
1957 ASM_OUTPUT_ASCII (asm_out_file, p, thissize);
1959 pos += thissize;
1960 p += thissize;
1965 /* A noswitch_section_callback for lcomm_section. */
1967 static bool
1968 emit_local (tree decl ATTRIBUTE_UNUSED,
1969 const char *name ATTRIBUTE_UNUSED,
1970 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1971 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1973 #if defined ASM_OUTPUT_ALIGNED_DECL_LOCAL
1974 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, decl, name,
1975 size, DECL_ALIGN (decl));
1976 return true;
1977 #elif defined ASM_OUTPUT_ALIGNED_LOCAL
1978 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, DECL_ALIGN (decl));
1979 return true;
1980 #else
1981 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
1982 return false;
1983 #endif
1986 /* A noswitch_section_callback for bss_noswitch_section. */
1988 #if defined ASM_OUTPUT_ALIGNED_BSS || defined ASM_OUTPUT_BSS
1989 static bool
1990 emit_bss (tree decl ATTRIBUTE_UNUSED,
1991 const char *name ATTRIBUTE_UNUSED,
1992 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1993 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1995 #if defined ASM_OUTPUT_ALIGNED_BSS
1996 ASM_OUTPUT_ALIGNED_BSS (asm_out_file, decl, name, size, DECL_ALIGN (decl));
1997 return true;
1998 #else
1999 ASM_OUTPUT_BSS (asm_out_file, decl, name, size, rounded);
2000 return false;
2001 #endif
2003 #endif
2005 /* A noswitch_section_callback for comm_section. */
2007 static bool
2008 emit_common (tree decl ATTRIBUTE_UNUSED,
2009 const char *name ATTRIBUTE_UNUSED,
2010 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
2011 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
2013 #if defined ASM_OUTPUT_ALIGNED_DECL_COMMON
2014 ASM_OUTPUT_ALIGNED_DECL_COMMON (asm_out_file, decl, name,
2015 size, DECL_ALIGN (decl));
2016 return true;
2017 #elif defined ASM_OUTPUT_ALIGNED_COMMON
2018 ASM_OUTPUT_ALIGNED_COMMON (asm_out_file, name, size, DECL_ALIGN (decl));
2019 return true;
2020 #else
2021 ASM_OUTPUT_COMMON (asm_out_file, name, size, rounded);
2022 return false;
2023 #endif
2026 /* A noswitch_section_callback for tls_comm_section. */
2028 static bool
2029 emit_tls_common (tree decl ATTRIBUTE_UNUSED,
2030 const char *name ATTRIBUTE_UNUSED,
2031 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
2032 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
2034 #ifdef ASM_OUTPUT_TLS_COMMON
2035 ASM_OUTPUT_TLS_COMMON (asm_out_file, decl, name, size);
2036 return true;
2037 #else
2038 sorry ("thread-local COMMON data not implemented");
2039 return true;
2040 #endif
2043 /* Assemble DECL given that it belongs in SECTION_NOSWITCH section SECT.
2044 NAME is the name of DECL's SYMBOL_REF. */
2046 static void
2047 assemble_noswitch_variable (tree decl, const char *name, section *sect)
2049 unsigned HOST_WIDE_INT size, rounded;
2051 size = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
2052 rounded = size;
2054 /* Don't allocate zero bytes of common,
2055 since that means "undefined external" in the linker. */
2056 if (size == 0)
2057 rounded = 1;
2059 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
2060 so that each uninitialized object starts on such a boundary. */
2061 rounded += (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1;
2062 rounded = (rounded / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
2063 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
2065 if (!sect->noswitch.callback (decl, name, size, rounded)
2066 && (unsigned HOST_WIDE_INT) DECL_ALIGN_UNIT (decl) > rounded)
2067 warning (0, "requested alignment for %q+D is greater than "
2068 "implemented alignment of %wu", decl, rounded);
2071 /* A subroutine of assemble_variable. Output the label and contents of
2072 DECL, whose address is a SYMBOL_REF with name NAME. DONT_OUTPUT_DATA
2073 is as for assemble_variable. */
2075 static void
2076 assemble_variable_contents (tree decl, const char *name,
2077 bool dont_output_data)
2079 /* Do any machine/system dependent processing of the object. */
2080 #ifdef ASM_DECLARE_OBJECT_NAME
2081 last_assemble_variable_decl = decl;
2082 ASM_DECLARE_OBJECT_NAME (asm_out_file, name, decl);
2083 #else
2084 /* Standard thing is just output label for the object. */
2085 ASM_OUTPUT_LABEL (asm_out_file, name);
2086 #endif /* ASM_DECLARE_OBJECT_NAME */
2088 if (!dont_output_data)
2090 if (DECL_INITIAL (decl)
2091 && DECL_INITIAL (decl) != error_mark_node
2092 && !initializer_zerop (DECL_INITIAL (decl)))
2093 /* Output the actual data. */
2094 output_constant (DECL_INITIAL (decl),
2095 tree_low_cst (DECL_SIZE_UNIT (decl), 1),
2096 DECL_ALIGN (decl));
2097 else
2098 /* Leave space for it. */
2099 assemble_zeros (tree_low_cst (DECL_SIZE_UNIT (decl), 1));
2103 /* Initialize emulated tls object TO, which refers to TLS variable
2104 DECL and is initialized by PROXY. */
2106 tree
2107 default_emutls_var_init (tree to, tree decl, tree proxy)
2109 VEC(constructor_elt,gc) *v = VEC_alloc (constructor_elt, gc, 4);
2110 constructor_elt *elt;
2111 tree type = TREE_TYPE (to);
2112 tree field = TYPE_FIELDS (type);
2114 elt = VEC_quick_push (constructor_elt, v, NULL);
2115 elt->index = field;
2116 elt->value = fold_convert (TREE_TYPE (field), DECL_SIZE_UNIT (decl));
2118 elt = VEC_quick_push (constructor_elt, v, NULL);
2119 field = TREE_CHAIN (field);
2120 elt->index = field;
2121 elt->value = build_int_cst (TREE_TYPE (field),
2122 DECL_ALIGN_UNIT (decl));
2124 elt = VEC_quick_push (constructor_elt, v, NULL);
2125 field = TREE_CHAIN (field);
2126 elt->index = field;
2127 elt->value = null_pointer_node;
2129 elt = VEC_quick_push (constructor_elt, v, NULL);
2130 field = TREE_CHAIN (field);
2131 elt->index = field;
2132 elt->value = proxy;
2134 return build_constructor (type, v);
2137 /* Assemble everything that is needed for a variable or function declaration.
2138 Not used for automatic variables, and not used for function definitions.
2139 Should not be called for variables of incomplete structure type.
2141 TOP_LEVEL is nonzero if this variable has file scope.
2142 AT_END is nonzero if this is the special handling, at end of compilation,
2143 to define things that have had only tentative definitions.
2144 DONT_OUTPUT_DATA if nonzero means don't actually output the
2145 initial value (that will be done by the caller). */
2147 void
2148 assemble_variable (tree decl, int top_level ATTRIBUTE_UNUSED,
2149 int at_end ATTRIBUTE_UNUSED, int dont_output_data)
2151 const char *name;
2152 rtx decl_rtl, symbol;
2153 section *sect;
2155 if (! targetm.have_tls
2156 && TREE_CODE (decl) == VAR_DECL
2157 && DECL_THREAD_LOCAL_P (decl))
2159 tree to = emutls_decl (decl);
2161 /* If this variable is defined locally, then we need to initialize the
2162 control structure with size and alignment information. We do this
2163 at the last moment because tentative definitions can take a locally
2164 defined but uninitialized variable and initialize it later, which
2165 would result in incorrect contents. */
2166 if (! DECL_EXTERNAL (to)
2167 && (! DECL_COMMON (to)
2168 || (DECL_INITIAL (decl)
2169 && DECL_INITIAL (decl) != error_mark_node)))
2171 DECL_INITIAL (to) = targetm.emutls.var_init
2172 (to, decl, get_emutls_init_templ_addr (decl));
2174 /* Make sure the template is marked as needed early enough.
2175 Without this, if the variable is placed in a
2176 section-anchored block, the template will only be marked
2177 when it's too late. */
2178 record_references_in_initializer (to, false);
2181 decl = to;
2184 last_assemble_variable_decl = 0;
2186 /* Normally no need to say anything here for external references,
2187 since assemble_external is called by the language-specific code
2188 when a declaration is first seen. */
2190 if (DECL_EXTERNAL (decl))
2191 return;
2193 /* Output no assembler code for a function declaration.
2194 Only definitions of functions output anything. */
2196 if (TREE_CODE (decl) == FUNCTION_DECL)
2197 return;
2199 /* Do nothing for global register variables. */
2200 if (DECL_RTL_SET_P (decl) && REG_P (DECL_RTL (decl)))
2202 TREE_ASM_WRITTEN (decl) = 1;
2203 return;
2206 /* If type was incomplete when the variable was declared,
2207 see if it is complete now. */
2209 if (DECL_SIZE (decl) == 0)
2210 layout_decl (decl, 0);
2212 /* Still incomplete => don't allocate it; treat the tentative defn
2213 (which is what it must have been) as an `extern' reference. */
2215 if (!dont_output_data && DECL_SIZE (decl) == 0)
2217 error ("storage size of %q+D isn%'t known", decl);
2218 TREE_ASM_WRITTEN (decl) = 1;
2219 return;
2222 /* The first declaration of a variable that comes through this function
2223 decides whether it is global (in C, has external linkage)
2224 or local (in C, has internal linkage). So do nothing more
2225 if this function has already run. */
2227 if (TREE_ASM_WRITTEN (decl))
2228 return;
2230 /* Make sure targetm.encode_section_info is invoked before we set
2231 ASM_WRITTEN. */
2232 decl_rtl = DECL_RTL (decl);
2234 TREE_ASM_WRITTEN (decl) = 1;
2236 /* Do no output if -fsyntax-only. */
2237 if (flag_syntax_only)
2238 return;
2240 if (! dont_output_data
2241 && ! host_integerp (DECL_SIZE_UNIT (decl), 1))
2243 error ("size of variable %q+D is too large", decl);
2244 return;
2247 gcc_assert (MEM_P (decl_rtl));
2248 gcc_assert (GET_CODE (XEXP (decl_rtl, 0)) == SYMBOL_REF);
2249 symbol = XEXP (decl_rtl, 0);
2251 /* If this symbol belongs to the tree constant pool, output the constant
2252 if it hasn't already been written. */
2253 if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
2255 tree decl = SYMBOL_REF_DECL (symbol);
2256 if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
2257 output_constant_def_contents (symbol);
2258 return;
2261 app_disable ();
2263 name = XSTR (symbol, 0);
2264 if (TREE_PUBLIC (decl) && DECL_NAME (decl))
2265 notice_global_symbol (decl);
2267 /* Compute the alignment of this data. */
2269 align_variable (decl, dont_output_data);
2270 set_mem_align (decl_rtl, DECL_ALIGN (decl));
2272 if (TREE_PUBLIC (decl))
2273 maybe_assemble_visibility (decl);
2275 if (DECL_PRESERVE_P (decl))
2276 targetm.asm_out.mark_decl_preserved (name);
2278 /* First make the assembler name(s) global if appropriate. */
2279 sect = get_variable_section (decl, false);
2280 if (TREE_PUBLIC (decl)
2281 && (sect->common.flags & SECTION_COMMON) == 0)
2282 globalize_decl (decl);
2284 /* Output any data that we will need to use the address of. */
2285 if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node)
2286 output_addressed_constants (DECL_INITIAL (decl));
2288 /* dbxout.c needs to know this. */
2289 if (sect && (sect->common.flags & SECTION_CODE) != 0)
2290 DECL_IN_TEXT_SECTION (decl) = 1;
2292 /* If the decl is part of an object_block, make sure that the decl
2293 has been positioned within its block, but do not write out its
2294 definition yet. output_object_blocks will do that later. */
2295 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
2297 gcc_assert (!dont_output_data);
2298 place_block_symbol (symbol);
2300 else if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
2301 assemble_noswitch_variable (decl, name, sect);
2302 else
2304 switch_to_section (sect);
2305 if (DECL_ALIGN (decl) > BITS_PER_UNIT)
2306 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (DECL_ALIGN_UNIT (decl)));
2307 assemble_variable_contents (decl, name, dont_output_data);
2311 /* Return 1 if type TYPE contains any pointers. */
2313 static int
2314 contains_pointers_p (tree type)
2316 switch (TREE_CODE (type))
2318 case POINTER_TYPE:
2319 case REFERENCE_TYPE:
2320 /* I'm not sure whether OFFSET_TYPE needs this treatment,
2321 so I'll play safe and return 1. */
2322 case OFFSET_TYPE:
2323 return 1;
2325 case RECORD_TYPE:
2326 case UNION_TYPE:
2327 case QUAL_UNION_TYPE:
2329 tree fields;
2330 /* For a type that has fields, see if the fields have pointers. */
2331 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
2332 if (TREE_CODE (fields) == FIELD_DECL
2333 && contains_pointers_p (TREE_TYPE (fields)))
2334 return 1;
2335 return 0;
2338 case ARRAY_TYPE:
2339 /* An array type contains pointers if its element type does. */
2340 return contains_pointers_p (TREE_TYPE (type));
2342 default:
2343 return 0;
2347 /* We delay assemble_external processing until
2348 the compilation unit is finalized. This is the best we can do for
2349 right now (i.e. stage 3 of GCC 4.0) - the right thing is to delay
2350 it all the way to final. See PR 17982 for further discussion. */
2351 static GTY(()) tree pending_assemble_externals;
2353 #ifdef ASM_OUTPUT_EXTERNAL
2354 /* True if DECL is a function decl for which no out-of-line copy exists.
2355 It is assumed that DECL's assembler name has been set. */
2357 static bool
2358 incorporeal_function_p (tree decl)
2360 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
2362 const char *name;
2364 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
2365 && DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA)
2366 return true;
2368 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2369 if (is_builtin_name (name))
2370 return true;
2372 return false;
2375 /* Actually do the tests to determine if this is necessary, and invoke
2376 ASM_OUTPUT_EXTERNAL. */
2377 static void
2378 assemble_external_real (tree decl)
2380 rtx rtl = DECL_RTL (decl);
2382 if (MEM_P (rtl) && GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF
2383 && !SYMBOL_REF_USED (XEXP (rtl, 0))
2384 && !incorporeal_function_p (decl))
2386 /* Some systems do require some output. */
2387 SYMBOL_REF_USED (XEXP (rtl, 0)) = 1;
2388 ASM_OUTPUT_EXTERNAL (asm_out_file, decl, XSTR (XEXP (rtl, 0), 0));
2391 #endif
2393 void
2394 process_pending_assemble_externals (void)
2396 #ifdef ASM_OUTPUT_EXTERNAL
2397 tree list;
2398 for (list = pending_assemble_externals; list; list = TREE_CHAIN (list))
2399 assemble_external_real (TREE_VALUE (list));
2401 pending_assemble_externals = 0;
2402 #endif
2405 /* This TREE_LIST contains any weak symbol declarations waiting
2406 to be emitted. */
2407 static GTY(()) tree weak_decls;
2409 /* Output something to declare an external symbol to the assembler,
2410 and qualifiers such as weakness. (Most assemblers don't need
2411 extern declaration, so we normally output nothing.) Do nothing if
2412 DECL is not external. */
2414 void
2415 assemble_external (tree decl ATTRIBUTE_UNUSED)
2417 /* Because most platforms do not define ASM_OUTPUT_EXTERNAL, the
2418 main body of this code is only rarely exercised. To provide some
2419 testing, on all platforms, we make sure that the ASM_OUT_FILE is
2420 open. If it's not, we should not be calling this function. */
2421 gcc_assert (asm_out_file);
2423 if (!DECL_P (decl) || !DECL_EXTERNAL (decl) || !TREE_PUBLIC (decl))
2424 return;
2426 /* We want to output annotation for weak and external symbols at
2427 very last to check if they are references or not. */
2429 if (SUPPORTS_WEAK
2430 && DECL_WEAK (decl)
2431 /* TREE_STATIC is a weird and abused creature which is not
2432 generally the right test for whether an entity has been
2433 locally emitted, inlined or otherwise not-really-extern, but
2434 for declarations that can be weak, it happens to be
2435 match. */
2436 && !TREE_STATIC (decl)
2437 && lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
2438 && value_member (decl, weak_decls) == NULL_TREE)
2439 weak_decls = tree_cons (NULL, decl, weak_decls);
2441 #ifdef ASM_OUTPUT_EXTERNAL
2442 if (value_member (decl, pending_assemble_externals) == NULL_TREE)
2443 pending_assemble_externals = tree_cons (NULL, decl,
2444 pending_assemble_externals);
2445 #endif
2448 /* Similar, for calling a library function FUN. */
2450 void
2451 assemble_external_libcall (rtx fun)
2453 /* Declare library function name external when first used, if nec. */
2454 if (! SYMBOL_REF_USED (fun))
2456 SYMBOL_REF_USED (fun) = 1;
2457 targetm.asm_out.external_libcall (fun);
2461 /* Assemble a label named NAME. */
2463 void
2464 assemble_label (FILE *file, const char *name)
2466 ASM_OUTPUT_LABEL (file, name);
2469 /* Set the symbol_referenced flag for ID. */
2470 void
2471 mark_referenced (tree id)
2473 TREE_SYMBOL_REFERENCED (id) = 1;
2476 /* Set the symbol_referenced flag for DECL and notify callgraph. */
2477 void
2478 mark_decl_referenced (tree decl)
2480 if (TREE_CODE (decl) == FUNCTION_DECL)
2482 /* Extern inline functions don't become needed when referenced.
2483 If we know a method will be emitted in other TU and no new
2484 functions can be marked reachable, just use the external
2485 definition. */
2486 struct cgraph_node *node = cgraph_node (decl);
2487 if (!DECL_EXTERNAL (decl)
2488 && (!node->local.vtable_method || !cgraph_global_info_ready
2489 || !node->local.finalized))
2490 cgraph_mark_needed_node (node);
2492 else if (TREE_CODE (decl) == VAR_DECL)
2494 struct varpool_node *node = varpool_node (decl);
2495 varpool_mark_needed_node (node);
2496 /* C++ frontend use mark_decl_references to force COMDAT variables
2497 to be output that might appear dead otherwise. */
2498 node->force_output = true;
2500 /* else do nothing - we can get various sorts of CST nodes here,
2501 which do not need to be marked. */
2505 /* Follow the IDENTIFIER_TRANSPARENT_ALIAS chain starting at *ALIAS
2506 until we find an identifier that is not itself a transparent alias.
2507 Modify the alias passed to it by reference (and all aliases on the
2508 way to the ultimate target), such that they do not have to be
2509 followed again, and return the ultimate target of the alias
2510 chain. */
2512 static inline tree
2513 ultimate_transparent_alias_target (tree *alias)
2515 tree target = *alias;
2517 if (IDENTIFIER_TRANSPARENT_ALIAS (target))
2519 gcc_assert (TREE_CHAIN (target));
2520 target = ultimate_transparent_alias_target (&TREE_CHAIN (target));
2521 gcc_assert (! IDENTIFIER_TRANSPARENT_ALIAS (target)
2522 && ! TREE_CHAIN (target));
2523 *alias = target;
2526 return target;
2529 /* Output to FILE (an assembly file) a reference to NAME. If NAME
2530 starts with a *, the rest of NAME is output verbatim. Otherwise
2531 NAME is transformed in a target-specific way (usually by the
2532 addition of an underscore). */
2534 void
2535 assemble_name_raw (FILE *file, const char *name)
2537 if (name[0] == '*')
2538 fputs (&name[1], file);
2539 else
2540 ASM_OUTPUT_LABELREF (file, name);
2543 /* Like assemble_name_raw, but should be used when NAME might refer to
2544 an entity that is also represented as a tree (like a function or
2545 variable). If NAME does refer to such an entity, that entity will
2546 be marked as referenced. */
2548 void
2549 assemble_name (FILE *file, const char *name)
2551 const char *real_name;
2552 tree id;
2554 real_name = targetm.strip_name_encoding (name);
2556 id = maybe_get_identifier (real_name);
2557 if (id)
2559 tree id_orig = id;
2561 mark_referenced (id);
2562 ultimate_transparent_alias_target (&id);
2563 if (id != id_orig)
2564 name = IDENTIFIER_POINTER (id);
2565 gcc_assert (! TREE_CHAIN (id));
2568 assemble_name_raw (file, name);
2571 /* Allocate SIZE bytes writable static space with a gensym name
2572 and return an RTX to refer to its address. */
2575 assemble_static_space (unsigned HOST_WIDE_INT size)
2577 char name[12];
2578 const char *namestring;
2579 rtx x;
2581 ASM_GENERATE_INTERNAL_LABEL (name, "LF", const_labelno);
2582 ++const_labelno;
2583 namestring = ggc_strdup (name);
2585 x = gen_rtx_SYMBOL_REF (Pmode, namestring);
2586 SYMBOL_REF_FLAGS (x) = SYMBOL_FLAG_LOCAL;
2588 #ifdef ASM_OUTPUT_ALIGNED_DECL_LOCAL
2589 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, NULL_TREE, name, size,
2590 BIGGEST_ALIGNMENT);
2591 #else
2592 #ifdef ASM_OUTPUT_ALIGNED_LOCAL
2593 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, BIGGEST_ALIGNMENT);
2594 #else
2596 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
2597 so that each uninitialized object starts on such a boundary. */
2598 /* Variable `rounded' might or might not be used in ASM_OUTPUT_LOCAL. */
2599 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED
2600 = ((size + (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1)
2601 / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
2602 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
2603 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
2605 #endif
2606 #endif
2607 return x;
2610 /* Assemble the static constant template for function entry trampolines.
2611 This is done at most once per compilation.
2612 Returns an RTX for the address of the template. */
2614 static GTY(()) rtx initial_trampoline;
2617 assemble_trampoline_template (void)
2619 char label[256];
2620 const char *name;
2621 int align;
2622 rtx symbol;
2624 gcc_assert (targetm.asm_out.trampoline_template != NULL);
2626 if (initial_trampoline)
2627 return initial_trampoline;
2629 /* By default, put trampoline templates in read-only data section. */
2631 #ifdef TRAMPOLINE_SECTION
2632 switch_to_section (TRAMPOLINE_SECTION);
2633 #else
2634 switch_to_section (readonly_data_section);
2635 #endif
2637 /* Write the assembler code to define one. */
2638 align = floor_log2 (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
2639 if (align > 0)
2640 ASM_OUTPUT_ALIGN (asm_out_file, align);
2642 targetm.asm_out.internal_label (asm_out_file, "LTRAMP", 0);
2643 targetm.asm_out.trampoline_template (asm_out_file);
2645 /* Record the rtl to refer to it. */
2646 ASM_GENERATE_INTERNAL_LABEL (label, "LTRAMP", 0);
2647 name = ggc_strdup (label);
2648 symbol = gen_rtx_SYMBOL_REF (Pmode, name);
2649 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_LOCAL;
2651 initial_trampoline = gen_const_mem (BLKmode, symbol);
2652 set_mem_align (initial_trampoline, TRAMPOLINE_ALIGNMENT);
2653 set_mem_size (initial_trampoline, GEN_INT (TRAMPOLINE_SIZE));
2655 return initial_trampoline;
2658 /* A and B are either alignments or offsets. Return the minimum alignment
2659 that may be assumed after adding the two together. */
2661 static inline unsigned
2662 min_align (unsigned int a, unsigned int b)
2664 return (a | b) & -(a | b);
2667 /* Return the assembler directive for creating a given kind of integer
2668 object. SIZE is the number of bytes in the object and ALIGNED_P
2669 indicates whether it is known to be aligned. Return NULL if the
2670 assembly dialect has no such directive.
2672 The returned string should be printed at the start of a new line and
2673 be followed immediately by the object's initial value. */
2675 const char *
2676 integer_asm_op (int size, int aligned_p)
2678 struct asm_int_op *ops;
2680 if (aligned_p)
2681 ops = &targetm.asm_out.aligned_op;
2682 else
2683 ops = &targetm.asm_out.unaligned_op;
2685 switch (size)
2687 case 1:
2688 return targetm.asm_out.byte_op;
2689 case 2:
2690 return ops->hi;
2691 case 4:
2692 return ops->si;
2693 case 8:
2694 return ops->di;
2695 case 16:
2696 return ops->ti;
2697 default:
2698 return NULL;
2702 /* Use directive OP to assemble an integer object X. Print OP at the
2703 start of the line, followed immediately by the value of X. */
2705 void
2706 assemble_integer_with_op (const char *op, rtx x)
2708 fputs (op, asm_out_file);
2709 output_addr_const (asm_out_file, x);
2710 fputc ('\n', asm_out_file);
2713 /* The default implementation of the asm_out.integer target hook. */
2715 bool
2716 default_assemble_integer (rtx x ATTRIBUTE_UNUSED,
2717 unsigned int size ATTRIBUTE_UNUSED,
2718 int aligned_p ATTRIBUTE_UNUSED)
2720 const char *op = integer_asm_op (size, aligned_p);
2721 /* Avoid GAS bugs for large values. Specifically negative values whose
2722 absolute value fits in a bfd_vma, but not in a bfd_signed_vma. */
2723 if (size > UNITS_PER_WORD && size > POINTER_SIZE / BITS_PER_UNIT)
2724 return false;
2725 return op && (assemble_integer_with_op (op, x), true);
2728 /* Assemble the integer constant X into an object of SIZE bytes. ALIGN is
2729 the alignment of the integer in bits. Return 1 if we were able to output
2730 the constant, otherwise 0. We must be able to output the constant,
2731 if FORCE is nonzero. */
2733 bool
2734 assemble_integer (rtx x, unsigned int size, unsigned int align, int force)
2736 int aligned_p;
2738 aligned_p = (align >= MIN (size * BITS_PER_UNIT, BIGGEST_ALIGNMENT));
2740 /* See if the target hook can handle this kind of object. */
2741 if (targetm.asm_out.integer (x, size, aligned_p))
2742 return true;
2744 /* If the object is a multi-byte one, try splitting it up. Split
2745 it into words it if is multi-word, otherwise split it into bytes. */
2746 if (size > 1)
2748 enum machine_mode omode, imode;
2749 unsigned int subalign;
2750 unsigned int subsize, i;
2751 enum mode_class mclass;
2753 subsize = size > UNITS_PER_WORD? UNITS_PER_WORD : 1;
2754 subalign = MIN (align, subsize * BITS_PER_UNIT);
2755 if (GET_CODE (x) == CONST_FIXED)
2756 mclass = GET_MODE_CLASS (GET_MODE (x));
2757 else
2758 mclass = MODE_INT;
2760 omode = mode_for_size (subsize * BITS_PER_UNIT, mclass, 0);
2761 imode = mode_for_size (size * BITS_PER_UNIT, mclass, 0);
2763 for (i = 0; i < size; i += subsize)
2765 rtx partial = simplify_subreg (omode, x, imode, i);
2766 if (!partial || !assemble_integer (partial, subsize, subalign, 0))
2767 break;
2769 if (i == size)
2770 return true;
2772 /* If we've printed some of it, but not all of it, there's no going
2773 back now. */
2774 gcc_assert (!i);
2777 gcc_assert (!force);
2779 return false;
2782 void
2783 assemble_real (REAL_VALUE_TYPE d, enum machine_mode mode, unsigned int align)
2785 long data[4] = {0, 0, 0, 0};
2786 int i;
2787 int bitsize, nelts, nunits, units_per;
2789 /* This is hairy. We have a quantity of known size. real_to_target
2790 will put it into an array of *host* longs, 32 bits per element
2791 (even if long is more than 32 bits). We need to determine the
2792 number of array elements that are occupied (nelts) and the number
2793 of *target* min-addressable units that will be occupied in the
2794 object file (nunits). We cannot assume that 32 divides the
2795 mode's bitsize (size * BITS_PER_UNIT) evenly.
2797 size * BITS_PER_UNIT is used here to make sure that padding bits
2798 (which might appear at either end of the value; real_to_target
2799 will include the padding bits in its output array) are included. */
2801 nunits = GET_MODE_SIZE (mode);
2802 bitsize = nunits * BITS_PER_UNIT;
2803 nelts = CEIL (bitsize, 32);
2804 units_per = 32 / BITS_PER_UNIT;
2806 real_to_target (data, &d, mode);
2808 /* Put out the first word with the specified alignment. */
2809 assemble_integer (GEN_INT (data[0]), MIN (nunits, units_per), align, 1);
2810 nunits -= units_per;
2812 /* Subsequent words need only 32-bit alignment. */
2813 align = min_align (align, 32);
2815 for (i = 1; i < nelts; i++)
2817 assemble_integer (GEN_INT (data[i]), MIN (nunits, units_per), align, 1);
2818 nunits -= units_per;
2822 /* Given an expression EXP with a constant value,
2823 reduce it to the sum of an assembler symbol and an integer.
2824 Store them both in the structure *VALUE.
2825 EXP must be reducible. */
2827 struct GTY(()) addr_const {
2828 rtx base;
2829 HOST_WIDE_INT offset;
2832 static void
2833 decode_addr_const (tree exp, struct addr_const *value)
2835 tree target = TREE_OPERAND (exp, 0);
2836 int offset = 0;
2837 rtx x;
2839 while (1)
2841 if (TREE_CODE (target) == COMPONENT_REF
2842 && host_integerp (byte_position (TREE_OPERAND (target, 1)), 0))
2844 offset += int_byte_position (TREE_OPERAND (target, 1));
2845 target = TREE_OPERAND (target, 0);
2847 else if (TREE_CODE (target) == ARRAY_REF
2848 || TREE_CODE (target) == ARRAY_RANGE_REF)
2850 offset += (tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (target)), 1)
2851 * tree_low_cst (TREE_OPERAND (target, 1), 0));
2852 target = TREE_OPERAND (target, 0);
2854 else if (TREE_CODE (target) == INDIRECT_REF
2855 && TREE_CODE (TREE_OPERAND (target, 0)) == NOP_EXPR
2856 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (target, 0), 0))
2857 == ADDR_EXPR)
2858 target = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (target, 0), 0), 0);
2859 else
2860 break;
2863 switch (TREE_CODE (target))
2865 case VAR_DECL:
2866 case FUNCTION_DECL:
2867 x = DECL_RTL (target);
2868 break;
2870 case LABEL_DECL:
2871 x = gen_rtx_MEM (FUNCTION_MODE,
2872 gen_rtx_LABEL_REF (Pmode, force_label_rtx (target)));
2873 break;
2875 case REAL_CST:
2876 case FIXED_CST:
2877 case STRING_CST:
2878 case COMPLEX_CST:
2879 case CONSTRUCTOR:
2880 case INTEGER_CST:
2881 x = output_constant_def (target, 1);
2882 break;
2884 default:
2885 gcc_unreachable ();
2888 gcc_assert (MEM_P (x));
2889 x = XEXP (x, 0);
2891 value->base = x;
2892 value->offset = offset;
2896 static GTY((param_is (struct constant_descriptor_tree)))
2897 htab_t const_desc_htab;
2899 static void maybe_output_constant_def_contents (struct constant_descriptor_tree *, int);
2901 /* Constant pool accessor function. */
2903 htab_t
2904 constant_pool_htab (void)
2906 return const_desc_htab;
2909 /* Compute a hash code for a constant expression. */
2911 static hashval_t
2912 const_desc_hash (const void *ptr)
2914 return ((const struct constant_descriptor_tree *)ptr)->hash;
2917 static hashval_t
2918 const_hash_1 (const tree exp)
2920 const char *p;
2921 hashval_t hi;
2922 int len, i;
2923 enum tree_code code = TREE_CODE (exp);
2925 /* Either set P and LEN to the address and len of something to hash and
2926 exit the switch or return a value. */
2928 switch (code)
2930 case INTEGER_CST:
2931 p = (char *) &TREE_INT_CST (exp);
2932 len = sizeof TREE_INT_CST (exp);
2933 break;
2935 case REAL_CST:
2936 return real_hash (TREE_REAL_CST_PTR (exp));
2938 case FIXED_CST:
2939 return fixed_hash (TREE_FIXED_CST_PTR (exp));
2941 case STRING_CST:
2942 p = TREE_STRING_POINTER (exp);
2943 len = TREE_STRING_LENGTH (exp);
2944 break;
2946 case COMPLEX_CST:
2947 return (const_hash_1 (TREE_REALPART (exp)) * 5
2948 + const_hash_1 (TREE_IMAGPART (exp)));
2950 case VECTOR_CST:
2952 tree link;
2954 hi = 7 + TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp));
2956 for (link = TREE_VECTOR_CST_ELTS (exp); link; link = TREE_CHAIN (link))
2957 hi = hi * 563 + const_hash_1 (TREE_VALUE (link));
2959 return hi;
2962 case CONSTRUCTOR:
2964 unsigned HOST_WIDE_INT idx;
2965 tree value;
2967 hi = 5 + int_size_in_bytes (TREE_TYPE (exp));
2969 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
2970 if (value)
2971 hi = hi * 603 + const_hash_1 (value);
2973 return hi;
2976 case ADDR_EXPR:
2977 case FDESC_EXPR:
2979 struct addr_const value;
2981 decode_addr_const (exp, &value);
2982 switch (GET_CODE (value.base))
2984 case SYMBOL_REF:
2985 /* Don't hash the address of the SYMBOL_REF;
2986 only use the offset and the symbol name. */
2987 hi = value.offset;
2988 p = XSTR (value.base, 0);
2989 for (i = 0; p[i] != 0; i++)
2990 hi = ((hi * 613) + (unsigned) (p[i]));
2991 break;
2993 case LABEL_REF:
2994 hi = value.offset + CODE_LABEL_NUMBER (XEXP (value.base, 0)) * 13;
2995 break;
2997 default:
2998 gcc_unreachable ();
3001 return hi;
3003 case PLUS_EXPR:
3004 case POINTER_PLUS_EXPR:
3005 case MINUS_EXPR:
3006 return (const_hash_1 (TREE_OPERAND (exp, 0)) * 9
3007 + const_hash_1 (TREE_OPERAND (exp, 1)));
3009 CASE_CONVERT:
3010 return const_hash_1 (TREE_OPERAND (exp, 0)) * 7 + 2;
3012 default:
3013 /* A language specific constant. Just hash the code. */
3014 return code;
3017 /* Compute hashing function. */
3018 hi = len;
3019 for (i = 0; i < len; i++)
3020 hi = ((hi * 613) + (unsigned) (p[i]));
3022 return hi;
3025 /* Wrapper of compare_constant, for the htab interface. */
3026 static int
3027 const_desc_eq (const void *p1, const void *p2)
3029 const struct constant_descriptor_tree *const c1
3030 = (const struct constant_descriptor_tree *) p1;
3031 const struct constant_descriptor_tree *const c2
3032 = (const struct constant_descriptor_tree *) p2;
3033 if (c1->hash != c2->hash)
3034 return 0;
3035 return compare_constant (c1->value, c2->value);
3038 /* Compare t1 and t2, and return 1 only if they are known to result in
3039 the same bit pattern on output. */
3041 static int
3042 compare_constant (const tree t1, const tree t2)
3044 enum tree_code typecode;
3046 if (t1 == NULL_TREE)
3047 return t2 == NULL_TREE;
3048 if (t2 == NULL_TREE)
3049 return 0;
3051 if (TREE_CODE (t1) != TREE_CODE (t2))
3052 return 0;
3054 switch (TREE_CODE (t1))
3056 case INTEGER_CST:
3057 /* Integer constants are the same only if the same width of type. */
3058 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3059 return 0;
3060 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
3061 return 0;
3062 return tree_int_cst_equal (t1, t2);
3064 case REAL_CST:
3065 /* Real constants are the same only if the same width of type. */
3066 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3067 return 0;
3069 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
3071 case FIXED_CST:
3072 /* Fixed constants are the same only if the same width of type. */
3073 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3074 return 0;
3076 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
3078 case STRING_CST:
3079 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
3080 return 0;
3082 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
3083 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
3084 TREE_STRING_LENGTH (t1)));
3086 case COMPLEX_CST:
3087 return (compare_constant (TREE_REALPART (t1), TREE_REALPART (t2))
3088 && compare_constant (TREE_IMAGPART (t1), TREE_IMAGPART (t2)));
3090 case VECTOR_CST:
3092 tree link1, link2;
3094 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (t1))
3095 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (t2)))
3096 return 0;
3098 link2 = TREE_VECTOR_CST_ELTS (t2);
3099 for (link1 = TREE_VECTOR_CST_ELTS (t1);
3100 link1;
3101 link1 = TREE_CHAIN (link1))
3103 if (!compare_constant (TREE_VALUE (link1), TREE_VALUE (link2)))
3104 return 0;
3105 link2 = TREE_CHAIN (link2);
3108 return 1;
3111 case CONSTRUCTOR:
3113 VEC(constructor_elt, gc) *v1, *v2;
3114 unsigned HOST_WIDE_INT idx;
3116 typecode = TREE_CODE (TREE_TYPE (t1));
3117 if (typecode != TREE_CODE (TREE_TYPE (t2)))
3118 return 0;
3120 if (typecode == ARRAY_TYPE)
3122 HOST_WIDE_INT size_1 = int_size_in_bytes (TREE_TYPE (t1));
3123 /* For arrays, check that the sizes all match. */
3124 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))
3125 || size_1 == -1
3126 || size_1 != int_size_in_bytes (TREE_TYPE (t2)))
3127 return 0;
3129 else
3131 /* For record and union constructors, require exact type
3132 equality. */
3133 if (TREE_TYPE (t1) != TREE_TYPE (t2))
3134 return 0;
3137 v1 = CONSTRUCTOR_ELTS (t1);
3138 v2 = CONSTRUCTOR_ELTS (t2);
3139 if (VEC_length (constructor_elt, v1)
3140 != VEC_length (constructor_elt, v2))
3141 return 0;
3143 for (idx = 0; idx < VEC_length (constructor_elt, v1); ++idx)
3145 constructor_elt *c1 = VEC_index (constructor_elt, v1, idx);
3146 constructor_elt *c2 = VEC_index (constructor_elt, v2, idx);
3148 /* Check that each value is the same... */
3149 if (!compare_constant (c1->value, c2->value))
3150 return 0;
3151 /* ... and that they apply to the same fields! */
3152 if (typecode == ARRAY_TYPE)
3154 if (!compare_constant (c1->index, c2->index))
3155 return 0;
3157 else
3159 if (c1->index != c2->index)
3160 return 0;
3164 return 1;
3167 case ADDR_EXPR:
3168 case FDESC_EXPR:
3170 struct addr_const value1, value2;
3171 enum rtx_code code;
3172 int ret;
3174 decode_addr_const (t1, &value1);
3175 decode_addr_const (t2, &value2);
3177 if (value1.offset != value2.offset)
3178 return 0;
3180 code = GET_CODE (value1.base);
3181 if (code != GET_CODE (value2.base))
3182 return 0;
3184 switch (code)
3186 case SYMBOL_REF:
3187 ret = (strcmp (XSTR (value1.base, 0), XSTR (value2.base, 0)) == 0);
3188 break;
3190 case LABEL_REF:
3191 ret = (CODE_LABEL_NUMBER (XEXP (value1.base, 0))
3192 == CODE_LABEL_NUMBER (XEXP (value2.base, 0)));
3193 break;
3195 default:
3196 gcc_unreachable ();
3198 return ret;
3201 case PLUS_EXPR:
3202 case POINTER_PLUS_EXPR:
3203 case MINUS_EXPR:
3204 case RANGE_EXPR:
3205 return (compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
3206 && compare_constant(TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
3208 CASE_CONVERT:
3209 case VIEW_CONVERT_EXPR:
3210 return compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3212 default:
3213 return 0;
3216 gcc_unreachable ();
3219 /* Make a copy of the whole tree structure for a constant. This
3220 handles the same types of nodes that compare_constant handles. */
3222 static tree
3223 copy_constant (tree exp)
3225 switch (TREE_CODE (exp))
3227 case ADDR_EXPR:
3228 /* For ADDR_EXPR, we do not want to copy the decl whose address
3229 is requested. We do want to copy constants though. */
3230 if (CONSTANT_CLASS_P (TREE_OPERAND (exp, 0)))
3231 return build1 (TREE_CODE (exp), TREE_TYPE (exp),
3232 copy_constant (TREE_OPERAND (exp, 0)));
3233 else
3234 return copy_node (exp);
3236 case INTEGER_CST:
3237 case REAL_CST:
3238 case FIXED_CST:
3239 case STRING_CST:
3240 return copy_node (exp);
3242 case COMPLEX_CST:
3243 return build_complex (TREE_TYPE (exp),
3244 copy_constant (TREE_REALPART (exp)),
3245 copy_constant (TREE_IMAGPART (exp)));
3247 case PLUS_EXPR:
3248 case POINTER_PLUS_EXPR:
3249 case MINUS_EXPR:
3250 return build2 (TREE_CODE (exp), TREE_TYPE (exp),
3251 copy_constant (TREE_OPERAND (exp, 0)),
3252 copy_constant (TREE_OPERAND (exp, 1)));
3254 CASE_CONVERT:
3255 case VIEW_CONVERT_EXPR:
3256 return build1 (TREE_CODE (exp), TREE_TYPE (exp),
3257 copy_constant (TREE_OPERAND (exp, 0)));
3259 case VECTOR_CST:
3260 return build_vector (TREE_TYPE (exp),
3261 copy_list (TREE_VECTOR_CST_ELTS (exp)));
3263 case CONSTRUCTOR:
3265 tree copy = copy_node (exp);
3266 VEC(constructor_elt, gc) *v;
3267 unsigned HOST_WIDE_INT idx;
3268 tree purpose, value;
3270 v = VEC_alloc(constructor_elt, gc, VEC_length(constructor_elt,
3271 CONSTRUCTOR_ELTS (exp)));
3272 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, purpose, value)
3274 constructor_elt *ce = VEC_quick_push (constructor_elt, v, NULL);
3275 ce->index = purpose;
3276 ce->value = copy_constant (value);
3278 CONSTRUCTOR_ELTS (copy) = v;
3279 return copy;
3282 default:
3283 gcc_unreachable ();
3287 /* Return the section into which constant EXP should be placed. */
3289 static section *
3290 get_constant_section (tree exp, unsigned int align)
3292 return targetm.asm_out.select_section (exp,
3293 compute_reloc_for_constant (exp),
3294 align);
3297 /* Return the size of constant EXP in bytes. */
3299 static HOST_WIDE_INT
3300 get_constant_size (tree exp)
3302 HOST_WIDE_INT size;
3304 size = int_size_in_bytes (TREE_TYPE (exp));
3305 if (TREE_CODE (exp) == STRING_CST)
3306 size = MAX (TREE_STRING_LENGTH (exp), size);
3307 return size;
3310 /* Subroutine of output_constant_def:
3311 No constant equal to EXP is known to have been output.
3312 Make a constant descriptor to enter EXP in the hash table.
3313 Assign the label number and construct RTL to refer to the
3314 constant's location in memory.
3315 Caller is responsible for updating the hash table. */
3317 static struct constant_descriptor_tree *
3318 build_constant_desc (tree exp)
3320 struct constant_descriptor_tree *desc;
3321 rtx symbol, rtl;
3322 char label[256];
3323 int labelno;
3324 tree decl;
3326 desc = ggc_alloc_constant_descriptor_tree ();
3327 desc->value = copy_constant (exp);
3329 /* Propagate marked-ness to copied constant. */
3330 if (flag_mudflap && mf_marked_p (exp))
3331 mf_mark (desc->value);
3333 /* Create a string containing the label name, in LABEL. */
3334 labelno = const_labelno++;
3335 ASM_GENERATE_INTERNAL_LABEL (label, "LC", labelno);
3337 /* Construct the VAR_DECL associated with the constant. */
3338 decl = build_decl (UNKNOWN_LOCATION, VAR_DECL, get_identifier (label),
3339 TREE_TYPE (exp));
3340 DECL_ARTIFICIAL (decl) = 1;
3341 DECL_IGNORED_P (decl) = 1;
3342 TREE_READONLY (decl) = 1;
3343 TREE_STATIC (decl) = 1;
3344 TREE_ADDRESSABLE (decl) = 1;
3345 /* We don't set the RTL yet as this would cause varpool to assume that the
3346 variable is referenced. Moreover, it would just be dropped in LTO mode.
3347 Instead we set the flag that will be recognized in make_decl_rtl. */
3348 DECL_IN_CONSTANT_POOL (decl) = 1;
3349 DECL_INITIAL (decl) = desc->value;
3350 /* ??? CONSTANT_ALIGNMENT hasn't been updated for vector types on most
3351 architectures so use DATA_ALIGNMENT as well, except for strings. */
3352 if (TREE_CODE (exp) == STRING_CST)
3354 #ifdef CONSTANT_ALIGNMENT
3355 DECL_ALIGN (decl) = CONSTANT_ALIGNMENT (exp, DECL_ALIGN (decl));
3356 #endif
3358 else
3359 align_variable (decl, 0);
3361 /* Now construct the SYMBOL_REF and the MEM. */
3362 if (use_object_blocks_p ())
3364 section *sect = get_constant_section (exp, DECL_ALIGN (decl));
3365 symbol = create_block_symbol (ggc_strdup (label),
3366 get_block_for_section (sect), -1);
3368 else
3369 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3370 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3371 SET_SYMBOL_REF_DECL (symbol, decl);
3372 TREE_CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3374 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (exp)), symbol);
3375 set_mem_attributes (rtl, exp, 1);
3376 set_mem_alias_set (rtl, 0);
3377 set_mem_alias_set (rtl, const_alias_set);
3379 /* We cannot share RTX'es in pool entries.
3380 Mark this piece of RTL as required for unsharing. */
3381 RTX_FLAG (rtl, used) = 1;
3383 /* Set flags or add text to the name to record information, such as
3384 that it is a local symbol. If the name is changed, the macro
3385 ASM_OUTPUT_LABELREF will have to know how to strip this
3386 information. This call might invalidate our local variable
3387 SYMBOL; we can't use it afterward. */
3388 targetm.encode_section_info (exp, rtl, true);
3390 desc->rtl = rtl;
3392 return desc;
3395 /* Return an rtx representing a reference to constant data in memory
3396 for the constant expression EXP.
3398 If assembler code for such a constant has already been output,
3399 return an rtx to refer to it.
3400 Otherwise, output such a constant in memory
3401 and generate an rtx for it.
3403 If DEFER is nonzero, this constant can be deferred and output only
3404 if referenced in the function after all optimizations.
3406 `const_desc_table' records which constants already have label strings. */
3409 output_constant_def (tree exp, int defer)
3411 struct constant_descriptor_tree *desc;
3412 struct constant_descriptor_tree key;
3413 void **loc;
3415 /* Look up EXP in the table of constant descriptors. If we didn't find
3416 it, create a new one. */
3417 key.value = exp;
3418 key.hash = const_hash_1 (exp);
3419 loc = htab_find_slot_with_hash (const_desc_htab, &key, key.hash, INSERT);
3421 desc = (struct constant_descriptor_tree *) *loc;
3422 if (desc == 0)
3424 desc = build_constant_desc (exp);
3425 desc->hash = key.hash;
3426 *loc = desc;
3429 maybe_output_constant_def_contents (desc, defer);
3430 return desc->rtl;
3433 /* Subroutine of output_constant_def: Decide whether or not we need to
3434 output the constant DESC now, and if so, do it. */
3435 static void
3436 maybe_output_constant_def_contents (struct constant_descriptor_tree *desc,
3437 int defer)
3439 rtx symbol = XEXP (desc->rtl, 0);
3440 tree exp = desc->value;
3442 if (flag_syntax_only)
3443 return;
3445 if (TREE_ASM_WRITTEN (exp))
3446 /* Already output; don't do it again. */
3447 return;
3449 /* We can always defer constants as long as the context allows
3450 doing so. */
3451 if (defer)
3453 /* Increment n_deferred_constants if it exists. It needs to be at
3454 least as large as the number of constants actually referred to
3455 by the function. If it's too small we'll stop looking too early
3456 and fail to emit constants; if it's too large we'll only look
3457 through the entire function when we could have stopped earlier. */
3458 if (cfun)
3459 n_deferred_constants++;
3460 return;
3463 output_constant_def_contents (symbol);
3466 /* Subroutine of output_constant_def_contents. Output the definition
3467 of constant EXP, which is pointed to by label LABEL. ALIGN is the
3468 constant's alignment in bits. */
3470 static void
3471 assemble_constant_contents (tree exp, const char *label, unsigned int align)
3473 HOST_WIDE_INT size;
3475 size = get_constant_size (exp);
3477 /* Do any machine/system dependent processing of the constant. */
3478 targetm.asm_out.declare_constant_name (asm_out_file, label, exp, size);
3480 /* Output the value of EXP. */
3481 output_constant (exp, size, align);
3484 /* We must output the constant data referred to by SYMBOL; do so. */
3486 static void
3487 output_constant_def_contents (rtx symbol)
3489 tree decl = SYMBOL_REF_DECL (symbol);
3490 tree exp = DECL_INITIAL (decl);
3491 unsigned int align;
3493 /* Make sure any other constants whose addresses appear in EXP
3494 are assigned label numbers. */
3495 output_addressed_constants (exp);
3497 /* We are no longer deferring this constant. */
3498 TREE_ASM_WRITTEN (exp) = 1;
3500 /* If the constant is part of an object block, make sure that the
3501 decl has been positioned within its block, but do not write out
3502 its definition yet. output_object_blocks will do that later. */
3503 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
3504 place_block_symbol (symbol);
3505 else
3507 align = DECL_ALIGN (decl);
3508 switch_to_section (get_constant_section (exp, align));
3509 if (align > BITS_PER_UNIT)
3510 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
3511 assemble_constant_contents (exp, XSTR (symbol, 0), align);
3513 if (flag_mudflap)
3514 mudflap_enqueue_constant (exp);
3517 /* Look up EXP in the table of constant descriptors. Return the rtl
3518 if it has been emitted, else null. */
3521 lookup_constant_def (tree exp)
3523 struct constant_descriptor_tree *desc;
3524 struct constant_descriptor_tree key;
3526 key.value = exp;
3527 key.hash = const_hash_1 (exp);
3528 desc = (struct constant_descriptor_tree *)
3529 htab_find_with_hash (const_desc_htab, &key, key.hash);
3531 return (desc ? desc->rtl : NULL_RTX);
3534 /* Return a tree representing a reference to constant data in memory
3535 for the constant expression EXP.
3537 This is the counterpart of output_constant_def at the Tree level. */
3539 tree
3540 tree_output_constant_def (tree exp)
3542 struct constant_descriptor_tree *desc, key;
3543 void **loc;
3544 tree decl;
3546 /* Look up EXP in the table of constant descriptors. If we didn't find
3547 it, create a new one. */
3548 key.value = exp;
3549 key.hash = const_hash_1 (exp);
3550 loc = htab_find_slot_with_hash (const_desc_htab, &key, key.hash, INSERT);
3552 desc = (struct constant_descriptor_tree *) *loc;
3553 if (desc == 0)
3555 desc = build_constant_desc (exp);
3556 desc->hash = key.hash;
3557 *loc = desc;
3560 decl = SYMBOL_REF_DECL (XEXP (desc->rtl, 0));
3561 varpool_finalize_decl (decl);
3562 return decl;
3565 /* Used in the hash tables to avoid outputting the same constant
3566 twice. Unlike 'struct constant_descriptor_tree', RTX constants
3567 are output once per function, not once per file. */
3568 /* ??? Only a few targets need per-function constant pools. Most
3569 can use one per-file pool. Should add a targetm bit to tell the
3570 difference. */
3572 struct GTY(()) rtx_constant_pool {
3573 /* Pointers to first and last constant in pool, as ordered by offset. */
3574 struct constant_descriptor_rtx *first;
3575 struct constant_descriptor_rtx *last;
3577 /* Hash facility for making memory-constants from constant rtl-expressions.
3578 It is used on RISC machines where immediate integer arguments and
3579 constant addresses are restricted so that such constants must be stored
3580 in memory. */
3581 htab_t GTY((param_is (struct constant_descriptor_rtx))) const_rtx_htab;
3583 /* Current offset in constant pool (does not include any
3584 machine-specific header). */
3585 HOST_WIDE_INT offset;
3588 struct GTY((chain_next ("%h.next"))) constant_descriptor_rtx {
3589 struct constant_descriptor_rtx *next;
3590 rtx mem;
3591 rtx sym;
3592 rtx constant;
3593 HOST_WIDE_INT offset;
3594 hashval_t hash;
3595 enum machine_mode mode;
3596 unsigned int align;
3597 int labelno;
3598 int mark;
3601 /* Hash and compare functions for const_rtx_htab. */
3603 static hashval_t
3604 const_desc_rtx_hash (const void *ptr)
3606 const struct constant_descriptor_rtx *const desc
3607 = (const struct constant_descriptor_rtx *) ptr;
3608 return desc->hash;
3611 static int
3612 const_desc_rtx_eq (const void *a, const void *b)
3614 const struct constant_descriptor_rtx *const x
3615 = (const struct constant_descriptor_rtx *) a;
3616 const struct constant_descriptor_rtx *const y
3617 = (const struct constant_descriptor_rtx *) b;
3619 if (x->mode != y->mode)
3620 return 0;
3621 return rtx_equal_p (x->constant, y->constant);
3624 /* This is the worker function for const_rtx_hash, called via for_each_rtx. */
3626 static int
3627 const_rtx_hash_1 (rtx *xp, void *data)
3629 unsigned HOST_WIDE_INT hwi;
3630 enum machine_mode mode;
3631 enum rtx_code code;
3632 hashval_t h, *hp;
3633 rtx x;
3635 x = *xp;
3636 code = GET_CODE (x);
3637 mode = GET_MODE (x);
3638 h = (hashval_t) code * 1048573 + mode;
3640 switch (code)
3642 case CONST_INT:
3643 hwi = INTVAL (x);
3644 fold_hwi:
3646 int shift = sizeof (hashval_t) * CHAR_BIT;
3647 const int n = sizeof (HOST_WIDE_INT) / sizeof (hashval_t);
3648 int i;
3650 h ^= (hashval_t) hwi;
3651 for (i = 1; i < n; ++i)
3653 hwi >>= shift;
3654 h ^= (hashval_t) hwi;
3657 break;
3659 case CONST_DOUBLE:
3660 if (mode == VOIDmode)
3662 hwi = CONST_DOUBLE_LOW (x) ^ CONST_DOUBLE_HIGH (x);
3663 goto fold_hwi;
3665 else
3666 h ^= real_hash (CONST_DOUBLE_REAL_VALUE (x));
3667 break;
3669 case CONST_FIXED:
3670 h ^= fixed_hash (CONST_FIXED_VALUE (x));
3671 break;
3673 case CONST_VECTOR:
3675 int i;
3676 for (i = XVECLEN (x, 0); i-- > 0; )
3677 h = h * 251 + const_rtx_hash_1 (&XVECEXP (x, 0, i), data);
3679 break;
3681 case SYMBOL_REF:
3682 h ^= htab_hash_string (XSTR (x, 0));
3683 break;
3685 case LABEL_REF:
3686 h = h * 251 + CODE_LABEL_NUMBER (XEXP (x, 0));
3687 break;
3689 case UNSPEC:
3690 case UNSPEC_VOLATILE:
3691 h = h * 251 + XINT (x, 1);
3692 break;
3694 default:
3695 break;
3698 hp = (hashval_t *) data;
3699 *hp = *hp * 509 + h;
3700 return 0;
3703 /* Compute a hash value for X, which should be a constant. */
3705 static hashval_t
3706 const_rtx_hash (rtx x)
3708 hashval_t h = 0;
3709 for_each_rtx (&x, const_rtx_hash_1, &h);
3710 return h;
3714 /* Create and return a new rtx constant pool. */
3716 static struct rtx_constant_pool *
3717 create_constant_pool (void)
3719 struct rtx_constant_pool *pool;
3721 pool = ggc_alloc_rtx_constant_pool ();
3722 pool->const_rtx_htab = htab_create_ggc (31, const_desc_rtx_hash,
3723 const_desc_rtx_eq, NULL);
3724 pool->first = NULL;
3725 pool->last = NULL;
3726 pool->offset = 0;
3727 return pool;
3730 /* Initialize constant pool hashing for a new function. */
3732 void
3733 init_varasm_status (void)
3735 crtl->varasm.pool = create_constant_pool ();
3736 crtl->varasm.deferred_constants = 0;
3739 /* Given a MINUS expression, simplify it if both sides
3740 include the same symbol. */
3743 simplify_subtraction (rtx x)
3745 rtx r = simplify_rtx (x);
3746 return r ? r : x;
3749 /* Given a constant rtx X, make (or find) a memory constant for its value
3750 and return a MEM rtx to refer to it in memory. */
3753 force_const_mem (enum machine_mode mode, rtx x)
3755 struct constant_descriptor_rtx *desc, tmp;
3756 struct rtx_constant_pool *pool;
3757 char label[256];
3758 rtx def, symbol;
3759 hashval_t hash;
3760 unsigned int align;
3761 void **slot;
3763 /* If we're not allowed to drop X into the constant pool, don't. */
3764 if (targetm.cannot_force_const_mem (x))
3765 return NULL_RTX;
3767 /* Record that this function has used a constant pool entry. */
3768 crtl->uses_const_pool = 1;
3770 /* Decide which pool to use. */
3771 pool = (targetm.use_blocks_for_constant_p (mode, x)
3772 ? shared_constant_pool
3773 : crtl->varasm.pool);
3775 /* Lookup the value in the hashtable. */
3776 tmp.constant = x;
3777 tmp.mode = mode;
3778 hash = const_rtx_hash (x);
3779 slot = htab_find_slot_with_hash (pool->const_rtx_htab, &tmp, hash, INSERT);
3780 desc = (struct constant_descriptor_rtx *) *slot;
3782 /* If the constant was already present, return its memory. */
3783 if (desc)
3784 return copy_rtx (desc->mem);
3786 /* Otherwise, create a new descriptor. */
3787 desc = ggc_alloc_constant_descriptor_rtx ();
3788 *slot = desc;
3790 /* Align the location counter as required by EXP's data type. */
3791 align = GET_MODE_ALIGNMENT (mode == VOIDmode ? word_mode : mode);
3792 #ifdef CONSTANT_ALIGNMENT
3794 tree type = lang_hooks.types.type_for_mode (mode, 0);
3795 if (type != NULL_TREE)
3796 align = CONSTANT_ALIGNMENT (make_tree (type, x), align);
3798 #endif
3800 pool->offset += (align / BITS_PER_UNIT) - 1;
3801 pool->offset &= ~ ((align / BITS_PER_UNIT) - 1);
3803 desc->next = NULL;
3804 desc->constant = tmp.constant;
3805 desc->offset = pool->offset;
3806 desc->hash = hash;
3807 desc->mode = mode;
3808 desc->align = align;
3809 desc->labelno = const_labelno;
3810 desc->mark = 0;
3812 pool->offset += GET_MODE_SIZE (mode);
3813 if (pool->last)
3814 pool->last->next = desc;
3815 else
3816 pool->first = pool->last = desc;
3817 pool->last = desc;
3819 /* Create a string containing the label name, in LABEL. */
3820 ASM_GENERATE_INTERNAL_LABEL (label, "LC", const_labelno);
3821 ++const_labelno;
3823 /* Construct the SYMBOL_REF. Make sure to mark it as belonging to
3824 the constants pool. */
3825 if (use_object_blocks_p () && targetm.use_blocks_for_constant_p (mode, x))
3827 section *sect = targetm.asm_out.select_rtx_section (mode, x, align);
3828 symbol = create_block_symbol (ggc_strdup (label),
3829 get_block_for_section (sect), -1);
3831 else
3832 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3833 desc->sym = symbol;
3834 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3835 CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3836 SET_SYMBOL_REF_CONSTANT (symbol, desc);
3838 /* Construct the MEM. */
3839 desc->mem = def = gen_const_mem (mode, symbol);
3840 set_mem_attributes (def, lang_hooks.types.type_for_mode (mode, 0), 1);
3841 set_mem_align (def, align);
3843 /* If we're dropping a label to the constant pool, make sure we
3844 don't delete it. */
3845 if (GET_CODE (x) == LABEL_REF)
3846 LABEL_PRESERVE_P (XEXP (x, 0)) = 1;
3848 return copy_rtx (def);
3851 /* Given a constant pool SYMBOL_REF, return the corresponding constant. */
3854 get_pool_constant (rtx addr)
3856 return SYMBOL_REF_CONSTANT (addr)->constant;
3859 /* Given a constant pool SYMBOL_REF, return the corresponding constant
3860 and whether it has been output or not. */
3863 get_pool_constant_mark (rtx addr, bool *pmarked)
3865 struct constant_descriptor_rtx *desc;
3867 desc = SYMBOL_REF_CONSTANT (addr);
3868 *pmarked = (desc->mark != 0);
3869 return desc->constant;
3872 /* Similar, return the mode. */
3874 enum machine_mode
3875 get_pool_mode (const_rtx addr)
3877 return SYMBOL_REF_CONSTANT (addr)->mode;
3880 /* Return the size of the constant pool. */
3883 get_pool_size (void)
3885 return crtl->varasm.pool->offset;
3888 /* Worker function for output_constant_pool_1. Emit assembly for X
3889 in MODE with known alignment ALIGN. */
3891 static void
3892 output_constant_pool_2 (enum machine_mode mode, rtx x, unsigned int align)
3894 switch (GET_MODE_CLASS (mode))
3896 case MODE_FLOAT:
3897 case MODE_DECIMAL_FLOAT:
3899 REAL_VALUE_TYPE r;
3901 gcc_assert (GET_CODE (x) == CONST_DOUBLE);
3902 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
3903 assemble_real (r, mode, align);
3904 break;
3907 case MODE_INT:
3908 case MODE_PARTIAL_INT:
3909 case MODE_FRACT:
3910 case MODE_UFRACT:
3911 case MODE_ACCUM:
3912 case MODE_UACCUM:
3913 assemble_integer (x, GET_MODE_SIZE (mode), align, 1);
3914 break;
3916 case MODE_VECTOR_FLOAT:
3917 case MODE_VECTOR_INT:
3918 case MODE_VECTOR_FRACT:
3919 case MODE_VECTOR_UFRACT:
3920 case MODE_VECTOR_ACCUM:
3921 case MODE_VECTOR_UACCUM:
3923 int i, units;
3924 enum machine_mode submode = GET_MODE_INNER (mode);
3925 unsigned int subalign = MIN (align, GET_MODE_BITSIZE (submode));
3927 gcc_assert (GET_CODE (x) == CONST_VECTOR);
3928 units = CONST_VECTOR_NUNITS (x);
3930 for (i = 0; i < units; i++)
3932 rtx elt = CONST_VECTOR_ELT (x, i);
3933 output_constant_pool_2 (submode, elt, i ? subalign : align);
3936 break;
3938 default:
3939 gcc_unreachable ();
3943 /* Worker function for output_constant_pool. Emit constant DESC,
3944 giving it ALIGN bits of alignment. */
3946 static void
3947 output_constant_pool_1 (struct constant_descriptor_rtx *desc,
3948 unsigned int align)
3950 rtx x, tmp;
3952 x = desc->constant;
3954 /* See if X is a LABEL_REF (or a CONST referring to a LABEL_REF)
3955 whose CODE_LABEL has been deleted. This can occur if a jump table
3956 is eliminated by optimization. If so, write a constant of zero
3957 instead. Note that this can also happen by turning the
3958 CODE_LABEL into a NOTE. */
3959 /* ??? This seems completely and utterly wrong. Certainly it's
3960 not true for NOTE_INSN_DELETED_LABEL, but I disbelieve proper
3961 functioning even with INSN_DELETED_P and friends. */
3963 tmp = x;
3964 switch (GET_CODE (tmp))
3966 case CONST:
3967 if (GET_CODE (XEXP (tmp, 0)) != PLUS
3968 || GET_CODE (XEXP (XEXP (tmp, 0), 0)) != LABEL_REF)
3969 break;
3970 tmp = XEXP (XEXP (tmp, 0), 0);
3971 /* FALLTHRU */
3973 case LABEL_REF:
3974 tmp = XEXP (tmp, 0);
3975 gcc_assert (!INSN_DELETED_P (tmp));
3976 gcc_assert (!NOTE_P (tmp)
3977 || NOTE_KIND (tmp) != NOTE_INSN_DELETED);
3978 break;
3980 default:
3981 break;
3984 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3985 ASM_OUTPUT_SPECIAL_POOL_ENTRY (asm_out_file, x, desc->mode,
3986 align, desc->labelno, done);
3987 #endif
3989 assemble_align (align);
3991 /* Output the label. */
3992 targetm.asm_out.internal_label (asm_out_file, "LC", desc->labelno);
3994 /* Output the data. */
3995 output_constant_pool_2 (desc->mode, x, align);
3997 /* Make sure all constants in SECTION_MERGE and not SECTION_STRINGS
3998 sections have proper size. */
3999 if (align > GET_MODE_BITSIZE (desc->mode)
4000 && in_section
4001 && (in_section->common.flags & SECTION_MERGE))
4002 assemble_align (align);
4004 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
4005 done:
4006 #endif
4007 return;
4010 /* Given a SYMBOL_REF CURRENT_RTX, mark it and all constants it refers
4011 to as used. Emit referenced deferred strings. This function can
4012 be used with for_each_rtx to mark all SYMBOL_REFs in an rtx. */
4014 static int
4015 mark_constant (rtx *current_rtx, void *data ATTRIBUTE_UNUSED)
4017 rtx x = *current_rtx;
4019 if (x == NULL_RTX || GET_CODE (x) != SYMBOL_REF)
4020 return 0;
4022 if (CONSTANT_POOL_ADDRESS_P (x))
4024 struct constant_descriptor_rtx *desc = SYMBOL_REF_CONSTANT (x);
4025 if (desc->mark == 0)
4027 desc->mark = 1;
4028 for_each_rtx (&desc->constant, mark_constant, NULL);
4031 else if (TREE_CONSTANT_POOL_ADDRESS_P (x))
4033 tree decl = SYMBOL_REF_DECL (x);
4034 if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
4036 n_deferred_constants--;
4037 output_constant_def_contents (x);
4041 return -1;
4044 /* Look through appropriate parts of INSN, marking all entries in the
4045 constant pool which are actually being used. Entries that are only
4046 referenced by other constants are also marked as used. Emit
4047 deferred strings that are used. */
4049 static void
4050 mark_constants (rtx insn)
4052 if (!INSN_P (insn))
4053 return;
4055 /* Insns may appear inside a SEQUENCE. Only check the patterns of
4056 insns, not any notes that may be attached. We don't want to mark
4057 a constant just because it happens to appear in a REG_EQUIV note. */
4058 if (GET_CODE (PATTERN (insn)) == SEQUENCE)
4060 rtx seq = PATTERN (insn);
4061 int i, n = XVECLEN (seq, 0);
4062 for (i = 0; i < n; ++i)
4064 rtx subinsn = XVECEXP (seq, 0, i);
4065 if (INSN_P (subinsn))
4066 for_each_rtx (&PATTERN (subinsn), mark_constant, NULL);
4069 else
4070 for_each_rtx (&PATTERN (insn), mark_constant, NULL);
4073 /* Look through the instructions for this function, and mark all the
4074 entries in POOL which are actually being used. Emit deferred constants
4075 which have indeed been used. */
4077 static void
4078 mark_constant_pool (void)
4080 rtx insn, link;
4082 if (!crtl->uses_const_pool && n_deferred_constants == 0)
4083 return;
4085 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4086 mark_constants (insn);
4088 for (link = crtl->epilogue_delay_list;
4089 link;
4090 link = XEXP (link, 1))
4091 mark_constants (XEXP (link, 0));
4094 /* Write all the constants in POOL. */
4096 static void
4097 output_constant_pool_contents (struct rtx_constant_pool *pool)
4099 struct constant_descriptor_rtx *desc;
4101 for (desc = pool->first; desc ; desc = desc->next)
4102 if (desc->mark)
4104 /* If the constant is part of an object_block, make sure that
4105 the constant has been positioned within its block, but do not
4106 write out its definition yet. output_object_blocks will do
4107 that later. */
4108 if (SYMBOL_REF_HAS_BLOCK_INFO_P (desc->sym)
4109 && SYMBOL_REF_BLOCK (desc->sym))
4110 place_block_symbol (desc->sym);
4111 else
4113 switch_to_section (targetm.asm_out.select_rtx_section
4114 (desc->mode, desc->constant, desc->align));
4115 output_constant_pool_1 (desc, desc->align);
4120 /* Mark all constants that are used in the current function, then write
4121 out the function's private constant pool. */
4123 static void
4124 output_constant_pool (const char *fnname ATTRIBUTE_UNUSED,
4125 tree fndecl ATTRIBUTE_UNUSED)
4127 struct rtx_constant_pool *pool = crtl->varasm.pool;
4129 /* It is possible for gcc to call force_const_mem and then to later
4130 discard the instructions which refer to the constant. In such a
4131 case we do not need to output the constant. */
4132 mark_constant_pool ();
4134 #ifdef ASM_OUTPUT_POOL_PROLOGUE
4135 ASM_OUTPUT_POOL_PROLOGUE (asm_out_file, fnname, fndecl, pool->offset);
4136 #endif
4138 output_constant_pool_contents (pool);
4140 #ifdef ASM_OUTPUT_POOL_EPILOGUE
4141 ASM_OUTPUT_POOL_EPILOGUE (asm_out_file, fnname, fndecl, pool->offset);
4142 #endif
4145 /* Write the contents of the shared constant pool. */
4147 void
4148 output_shared_constant_pool (void)
4150 output_constant_pool_contents (shared_constant_pool);
4153 /* Determine what kind of relocations EXP may need. */
4156 compute_reloc_for_constant (tree exp)
4158 int reloc = 0, reloc2;
4159 tree tem;
4161 switch (TREE_CODE (exp))
4163 case ADDR_EXPR:
4164 case FDESC_EXPR:
4165 /* Go inside any operations that get_inner_reference can handle and see
4166 if what's inside is a constant: no need to do anything here for
4167 addresses of variables or functions. */
4168 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
4169 tem = TREE_OPERAND (tem, 0))
4172 if (TREE_PUBLIC (tem))
4173 reloc |= 2;
4174 else
4175 reloc |= 1;
4176 break;
4178 case PLUS_EXPR:
4179 case POINTER_PLUS_EXPR:
4180 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4181 reloc |= compute_reloc_for_constant (TREE_OPERAND (exp, 1));
4182 break;
4184 case MINUS_EXPR:
4185 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4186 reloc2 = compute_reloc_for_constant (TREE_OPERAND (exp, 1));
4187 /* The difference of two local labels is computable at link time. */
4188 if (reloc == 1 && reloc2 == 1)
4189 reloc = 0;
4190 else
4191 reloc |= reloc2;
4192 break;
4194 CASE_CONVERT:
4195 case VIEW_CONVERT_EXPR:
4196 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4197 break;
4199 case CONSTRUCTOR:
4201 unsigned HOST_WIDE_INT idx;
4202 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
4203 if (tem != 0)
4204 reloc |= compute_reloc_for_constant (tem);
4206 break;
4208 default:
4209 break;
4211 return reloc;
4214 /* Find all the constants whose addresses are referenced inside of EXP,
4215 and make sure assembler code with a label has been output for each one.
4216 Indicate whether an ADDR_EXPR has been encountered. */
4218 static void
4219 output_addressed_constants (tree exp)
4221 tree tem;
4223 switch (TREE_CODE (exp))
4225 case ADDR_EXPR:
4226 case FDESC_EXPR:
4227 /* Go inside any operations that get_inner_reference can handle and see
4228 if what's inside is a constant: no need to do anything here for
4229 addresses of variables or functions. */
4230 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
4231 tem = TREE_OPERAND (tem, 0))
4234 /* If we have an initialized CONST_DECL, retrieve the initializer. */
4235 if (TREE_CODE (tem) == CONST_DECL && DECL_INITIAL (tem))
4236 tem = DECL_INITIAL (tem);
4238 if (CONSTANT_CLASS_P (tem) || TREE_CODE (tem) == CONSTRUCTOR)
4239 output_constant_def (tem, 0);
4240 break;
4242 case PLUS_EXPR:
4243 case POINTER_PLUS_EXPR:
4244 case MINUS_EXPR:
4245 output_addressed_constants (TREE_OPERAND (exp, 1));
4246 /* Fall through. */
4248 CASE_CONVERT:
4249 case VIEW_CONVERT_EXPR:
4250 output_addressed_constants (TREE_OPERAND (exp, 0));
4251 break;
4253 case CONSTRUCTOR:
4255 unsigned HOST_WIDE_INT idx;
4256 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
4257 if (tem != 0)
4258 output_addressed_constants (tem);
4260 break;
4262 default:
4263 break;
4267 /* Whether a constructor CTOR is a valid static constant initializer if all
4268 its elements are. This used to be internal to initializer_constant_valid_p
4269 and has been exposed to let other functions like categorize_ctor_elements
4270 evaluate the property while walking a constructor for other purposes. */
4272 bool
4273 constructor_static_from_elts_p (const_tree ctor)
4275 return (TREE_CONSTANT (ctor)
4276 && (TREE_CODE (TREE_TYPE (ctor)) == UNION_TYPE
4277 || TREE_CODE (TREE_TYPE (ctor)) == RECORD_TYPE)
4278 && !VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (ctor)));
4281 static tree initializer_constant_valid_p_1 (tree value, tree endtype,
4282 tree *cache);
4284 /* A subroutine of initializer_constant_valid_p. VALUE is a MINUS_EXPR,
4285 PLUS_EXPR or POINTER_PLUS_EXPR. This looks for cases of VALUE
4286 which are valid when ENDTYPE is an integer of any size; in
4287 particular, this does not accept a pointer minus a constant. This
4288 returns null_pointer_node if the VALUE is an absolute constant
4289 which can be used to initialize a static variable. Otherwise it
4290 returns NULL. */
4292 static tree
4293 narrowing_initializer_constant_valid_p (tree value, tree endtype, tree *cache)
4295 tree op0, op1;
4297 if (!INTEGRAL_TYPE_P (endtype))
4298 return NULL_TREE;
4300 op0 = TREE_OPERAND (value, 0);
4301 op1 = TREE_OPERAND (value, 1);
4303 /* Like STRIP_NOPS except allow the operand mode to widen. This
4304 works around a feature of fold that simplifies (int)(p1 - p2) to
4305 ((int)p1 - (int)p2) under the theory that the narrower operation
4306 is cheaper. */
4308 while (CONVERT_EXPR_P (op0)
4309 || TREE_CODE (op0) == NON_LVALUE_EXPR)
4311 tree inner = TREE_OPERAND (op0, 0);
4312 if (inner == error_mark_node
4313 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4314 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))
4315 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
4316 break;
4317 op0 = inner;
4320 while (CONVERT_EXPR_P (op1)
4321 || TREE_CODE (op1) == NON_LVALUE_EXPR)
4323 tree inner = TREE_OPERAND (op1, 0);
4324 if (inner == error_mark_node
4325 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4326 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op1)))
4327 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
4328 break;
4329 op1 = inner;
4332 op0 = initializer_constant_valid_p_1 (op0, endtype, cache);
4333 if (!op0)
4334 return NULL_TREE;
4336 op1 = initializer_constant_valid_p_1 (op1, endtype,
4337 cache ? cache + 2 : NULL);
4338 /* Both initializers must be known. */
4339 if (op1)
4341 if (op0 == op1
4342 && (op0 == null_pointer_node
4343 || TREE_CODE (value) == MINUS_EXPR))
4344 return null_pointer_node;
4346 /* Support differences between labels. */
4347 if (TREE_CODE (op0) == LABEL_DECL
4348 && TREE_CODE (op1) == LABEL_DECL)
4349 return null_pointer_node;
4351 if (TREE_CODE (op0) == STRING_CST
4352 && TREE_CODE (op1) == STRING_CST
4353 && operand_equal_p (op0, op1, 1))
4354 return null_pointer_node;
4357 return NULL_TREE;
4360 /* Helper function of initializer_constant_valid_p.
4361 Return nonzero if VALUE is a valid constant-valued expression
4362 for use in initializing a static variable; one that can be an
4363 element of a "constant" initializer.
4365 Return null_pointer_node if the value is absolute;
4366 if it is relocatable, return the variable that determines the relocation.
4367 We assume that VALUE has been folded as much as possible;
4368 therefore, we do not need to check for such things as
4369 arithmetic-combinations of integers.
4371 Use CACHE (pointer to 2 tree values) for caching if non-NULL. */
4373 static tree
4374 initializer_constant_valid_p_1 (tree value, tree endtype, tree *cache)
4376 tree ret;
4378 switch (TREE_CODE (value))
4380 case CONSTRUCTOR:
4381 if (constructor_static_from_elts_p (value))
4383 unsigned HOST_WIDE_INT idx;
4384 tree elt;
4385 bool absolute = true;
4387 if (cache && cache[0] == value)
4388 return cache[1];
4389 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
4391 tree reloc;
4392 reloc = initializer_constant_valid_p_1 (elt, TREE_TYPE (elt),
4393 NULL);
4394 if (!reloc)
4396 if (cache)
4398 cache[0] = value;
4399 cache[1] = NULL_TREE;
4401 return NULL_TREE;
4403 if (reloc != null_pointer_node)
4404 absolute = false;
4406 /* For a non-absolute relocation, there is no single
4407 variable that can be "the variable that determines the
4408 relocation." */
4409 if (cache)
4411 cache[0] = value;
4412 cache[1] = absolute ? null_pointer_node : error_mark_node;
4414 return absolute ? null_pointer_node : error_mark_node;
4417 return TREE_STATIC (value) ? null_pointer_node : NULL_TREE;
4419 case INTEGER_CST:
4420 case VECTOR_CST:
4421 case REAL_CST:
4422 case FIXED_CST:
4423 case STRING_CST:
4424 case COMPLEX_CST:
4425 return null_pointer_node;
4427 case ADDR_EXPR:
4428 case FDESC_EXPR:
4430 tree op0 = staticp (TREE_OPERAND (value, 0));
4431 if (op0)
4433 /* "&(*a).f" is like unto pointer arithmetic. If "a" turns out
4434 to be a constant, this is old-skool offsetof-like nonsense. */
4435 if (TREE_CODE (op0) == INDIRECT_REF
4436 && TREE_CONSTANT (TREE_OPERAND (op0, 0)))
4437 return null_pointer_node;
4438 /* Taking the address of a nested function involves a trampoline,
4439 unless we don't need or want one. */
4440 if (TREE_CODE (op0) == FUNCTION_DECL
4441 && DECL_STATIC_CHAIN (op0)
4442 && !TREE_NO_TRAMPOLINE (value))
4443 return NULL_TREE;
4444 /* "&{...}" requires a temporary to hold the constructed
4445 object. */
4446 if (TREE_CODE (op0) == CONSTRUCTOR)
4447 return NULL_TREE;
4449 return op0;
4452 case NON_LVALUE_EXPR:
4453 return initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4454 endtype, cache);
4456 case VIEW_CONVERT_EXPR:
4458 tree src = TREE_OPERAND (value, 0);
4459 tree src_type = TREE_TYPE (src);
4460 tree dest_type = TREE_TYPE (value);
4462 /* Allow view-conversions from aggregate to non-aggregate type only
4463 if the bit pattern is fully preserved afterwards; otherwise, the
4464 RTL expander won't be able to apply a subsequent transformation
4465 to the underlying constructor. */
4466 if (AGGREGATE_TYPE_P (src_type) && !AGGREGATE_TYPE_P (dest_type))
4468 if (TYPE_MODE (endtype) == TYPE_MODE (dest_type))
4469 return initializer_constant_valid_p_1 (src, endtype, cache);
4470 else
4471 return NULL_TREE;
4474 /* Allow all other kinds of view-conversion. */
4475 return initializer_constant_valid_p_1 (src, endtype, cache);
4478 CASE_CONVERT:
4480 tree src = TREE_OPERAND (value, 0);
4481 tree src_type = TREE_TYPE (src);
4482 tree dest_type = TREE_TYPE (value);
4484 /* Allow conversions between pointer types, floating-point
4485 types, and offset types. */
4486 if ((POINTER_TYPE_P (dest_type) && POINTER_TYPE_P (src_type))
4487 || (FLOAT_TYPE_P (dest_type) && FLOAT_TYPE_P (src_type))
4488 || (TREE_CODE (dest_type) == OFFSET_TYPE
4489 && TREE_CODE (src_type) == OFFSET_TYPE))
4490 return initializer_constant_valid_p_1 (src, endtype, cache);
4492 /* Allow length-preserving conversions between integer types. */
4493 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type)
4494 && (TYPE_PRECISION (dest_type) == TYPE_PRECISION (src_type)))
4495 return initializer_constant_valid_p_1 (src, endtype, cache);
4497 /* Allow conversions between other integer types only if
4498 explicit value. */
4499 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type))
4501 tree inner = initializer_constant_valid_p_1 (src, endtype, cache);
4502 if (inner == null_pointer_node)
4503 return null_pointer_node;
4504 break;
4507 /* Allow (int) &foo provided int is as wide as a pointer. */
4508 if (INTEGRAL_TYPE_P (dest_type) && POINTER_TYPE_P (src_type)
4509 && (TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type)))
4510 return initializer_constant_valid_p_1 (src, endtype, cache);
4512 /* Likewise conversions from int to pointers, but also allow
4513 conversions from 0. */
4514 if ((POINTER_TYPE_P (dest_type)
4515 || TREE_CODE (dest_type) == OFFSET_TYPE)
4516 && INTEGRAL_TYPE_P (src_type))
4518 if (TREE_CODE (src) == INTEGER_CST
4519 && TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type))
4520 return null_pointer_node;
4521 if (integer_zerop (src))
4522 return null_pointer_node;
4523 else if (TYPE_PRECISION (dest_type) <= TYPE_PRECISION (src_type))
4524 return initializer_constant_valid_p_1 (src, endtype, cache);
4527 /* Allow conversions to struct or union types if the value
4528 inside is okay. */
4529 if (TREE_CODE (dest_type) == RECORD_TYPE
4530 || TREE_CODE (dest_type) == UNION_TYPE)
4531 return initializer_constant_valid_p_1 (src, endtype, cache);
4533 break;
4535 case POINTER_PLUS_EXPR:
4536 case PLUS_EXPR:
4537 /* Any valid floating-point constants will have been folded by now;
4538 with -frounding-math we hit this with addition of two constants. */
4539 if (TREE_CODE (endtype) == REAL_TYPE)
4540 return NULL_TREE;
4541 if (cache && cache[0] == value)
4542 return cache[1];
4543 if (! INTEGRAL_TYPE_P (endtype)
4544 || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
4546 tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
4547 tree valid0
4548 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4549 endtype, ncache);
4550 tree valid1
4551 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
4552 endtype, ncache + 2);
4553 /* If either term is absolute, use the other term's relocation. */
4554 if (valid0 == null_pointer_node)
4555 ret = valid1;
4556 else if (valid1 == null_pointer_node)
4557 ret = valid0;
4558 /* Support narrowing pointer differences. */
4559 else
4560 ret = narrowing_initializer_constant_valid_p (value, endtype,
4561 ncache);
4563 else
4564 /* Support narrowing pointer differences. */
4565 ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
4566 if (cache)
4568 cache[0] = value;
4569 cache[1] = ret;
4571 return ret;
4573 case MINUS_EXPR:
4574 if (TREE_CODE (endtype) == REAL_TYPE)
4575 return NULL_TREE;
4576 if (cache && cache[0] == value)
4577 return cache[1];
4578 if (! INTEGRAL_TYPE_P (endtype)
4579 || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
4581 tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
4582 tree valid0
4583 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4584 endtype, ncache);
4585 tree valid1
4586 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
4587 endtype, ncache + 2);
4588 /* Win if second argument is absolute. */
4589 if (valid1 == null_pointer_node)
4590 ret = valid0;
4591 /* Win if both arguments have the same relocation.
4592 Then the value is absolute. */
4593 else if (valid0 == valid1 && valid0 != 0)
4594 ret = null_pointer_node;
4595 /* Since GCC guarantees that string constants are unique in the
4596 generated code, a subtraction between two copies of the same
4597 constant string is absolute. */
4598 else if (valid0 && TREE_CODE (valid0) == STRING_CST
4599 && valid1 && TREE_CODE (valid1) == STRING_CST
4600 && operand_equal_p (valid0, valid1, 1))
4601 ret = null_pointer_node;
4602 /* Support narrowing differences. */
4603 else
4604 ret = narrowing_initializer_constant_valid_p (value, endtype,
4605 ncache);
4607 else
4608 /* Support narrowing differences. */
4609 ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
4610 if (cache)
4612 cache[0] = value;
4613 cache[1] = ret;
4615 return ret;
4617 default:
4618 break;
4621 return NULL_TREE;
4624 /* Return nonzero if VALUE is a valid constant-valued expression
4625 for use in initializing a static variable; one that can be an
4626 element of a "constant" initializer.
4628 Return null_pointer_node if the value is absolute;
4629 if it is relocatable, return the variable that determines the relocation.
4630 We assume that VALUE has been folded as much as possible;
4631 therefore, we do not need to check for such things as
4632 arithmetic-combinations of integers. */
4633 tree
4634 initializer_constant_valid_p (tree value, tree endtype)
4636 return initializer_constant_valid_p_1 (value, endtype, NULL);
4639 /* Return true if VALUE is a valid constant-valued expression
4640 for use in initializing a static bit-field; one that can be
4641 an element of a "constant" initializer. */
4643 bool
4644 initializer_constant_valid_for_bitfield_p (tree value)
4646 /* For bitfields we support integer constants or possibly nested aggregates
4647 of such. */
4648 switch (TREE_CODE (value))
4650 case CONSTRUCTOR:
4652 unsigned HOST_WIDE_INT idx;
4653 tree elt;
4655 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
4656 if (!initializer_constant_valid_for_bitfield_p (elt))
4657 return false;
4658 return true;
4661 case INTEGER_CST:
4662 return true;
4664 case VIEW_CONVERT_EXPR:
4665 case NON_LVALUE_EXPR:
4666 return
4667 initializer_constant_valid_for_bitfield_p (TREE_OPERAND (value, 0));
4669 default:
4670 break;
4673 return false;
4676 /* output_constructor outer state of relevance in recursive calls, typically
4677 for nested aggregate bitfields. */
4679 typedef struct {
4680 unsigned int bit_offset; /* current position in ... */
4681 int byte; /* ... the outer byte buffer. */
4682 } oc_outer_state;
4684 static unsigned HOST_WIDE_INT
4685 output_constructor (tree, unsigned HOST_WIDE_INT, unsigned int,
4686 oc_outer_state *);
4688 /* Output assembler code for constant EXP to FILE, with no label.
4689 This includes the pseudo-op such as ".int" or ".byte", and a newline.
4690 Assumes output_addressed_constants has been done on EXP already.
4692 Generate exactly SIZE bytes of assembler data, padding at the end
4693 with zeros if necessary. SIZE must always be specified.
4695 SIZE is important for structure constructors,
4696 since trailing members may have been omitted from the constructor.
4697 It is also important for initialization of arrays from string constants
4698 since the full length of the string constant might not be wanted.
4699 It is also needed for initialization of unions, where the initializer's
4700 type is just one member, and that may not be as long as the union.
4702 There a case in which we would fail to output exactly SIZE bytes:
4703 for a structure constructor that wants to produce more than SIZE bytes.
4704 But such constructors will never be generated for any possible input.
4706 ALIGN is the alignment of the data in bits. */
4708 void
4709 output_constant (tree exp, unsigned HOST_WIDE_INT size, unsigned int align)
4711 enum tree_code code;
4712 unsigned HOST_WIDE_INT thissize;
4714 if (size == 0 || flag_syntax_only)
4715 return;
4717 /* See if we're trying to initialize a pointer in a non-default mode
4718 to the address of some declaration somewhere. If the target says
4719 the mode is valid for pointers, assume the target has a way of
4720 resolving it. */
4721 if (TREE_CODE (exp) == NOP_EXPR
4722 && POINTER_TYPE_P (TREE_TYPE (exp))
4723 && targetm.addr_space.valid_pointer_mode
4724 (TYPE_MODE (TREE_TYPE (exp)),
4725 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
4727 tree saved_type = TREE_TYPE (exp);
4729 /* Peel off any intermediate conversions-to-pointer for valid
4730 pointer modes. */
4731 while (TREE_CODE (exp) == NOP_EXPR
4732 && POINTER_TYPE_P (TREE_TYPE (exp))
4733 && targetm.addr_space.valid_pointer_mode
4734 (TYPE_MODE (TREE_TYPE (exp)),
4735 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
4736 exp = TREE_OPERAND (exp, 0);
4738 /* If what we're left with is the address of something, we can
4739 convert the address to the final type and output it that
4740 way. */
4741 if (TREE_CODE (exp) == ADDR_EXPR)
4742 exp = build1 (ADDR_EXPR, saved_type, TREE_OPERAND (exp, 0));
4743 /* Likewise for constant ints. */
4744 else if (TREE_CODE (exp) == INTEGER_CST)
4745 exp = build_int_cst_wide (saved_type, TREE_INT_CST_LOW (exp),
4746 TREE_INT_CST_HIGH (exp));
4750 /* Eliminate any conversions since we'll be outputting the underlying
4751 constant. */
4752 while (CONVERT_EXPR_P (exp)
4753 || TREE_CODE (exp) == NON_LVALUE_EXPR
4754 || TREE_CODE (exp) == VIEW_CONVERT_EXPR)
4756 HOST_WIDE_INT type_size = int_size_in_bytes (TREE_TYPE (exp));
4757 HOST_WIDE_INT op_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (exp, 0)));
4759 /* Make sure eliminating the conversion is really a no-op, except with
4760 VIEW_CONVERT_EXPRs to allow for wild Ada unchecked conversions and
4761 union types to allow for Ada unchecked unions. */
4762 if (type_size > op_size
4763 && TREE_CODE (exp) != VIEW_CONVERT_EXPR
4764 && TREE_CODE (TREE_TYPE (exp)) != UNION_TYPE)
4765 /* Keep the conversion. */
4766 break;
4767 else
4768 exp = TREE_OPERAND (exp, 0);
4771 code = TREE_CODE (TREE_TYPE (exp));
4772 thissize = int_size_in_bytes (TREE_TYPE (exp));
4774 /* Allow a constructor with no elements for any data type.
4775 This means to fill the space with zeros. */
4776 if (TREE_CODE (exp) == CONSTRUCTOR
4777 && VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (exp)))
4779 assemble_zeros (size);
4780 return;
4783 if (TREE_CODE (exp) == FDESC_EXPR)
4785 #ifdef ASM_OUTPUT_FDESC
4786 HOST_WIDE_INT part = tree_low_cst (TREE_OPERAND (exp, 1), 0);
4787 tree decl = TREE_OPERAND (exp, 0);
4788 ASM_OUTPUT_FDESC (asm_out_file, decl, part);
4789 #else
4790 gcc_unreachable ();
4791 #endif
4792 return;
4795 /* Now output the underlying data. If we've handling the padding, return.
4796 Otherwise, break and ensure SIZE is the size written. */
4797 switch (code)
4799 case BOOLEAN_TYPE:
4800 case INTEGER_TYPE:
4801 case ENUMERAL_TYPE:
4802 case POINTER_TYPE:
4803 case REFERENCE_TYPE:
4804 case OFFSET_TYPE:
4805 case FIXED_POINT_TYPE:
4806 if (! assemble_integer (expand_expr (exp, NULL_RTX, VOIDmode,
4807 EXPAND_INITIALIZER),
4808 MIN (size, thissize), align, 0))
4809 error ("initializer for integer/fixed-point value is too complicated");
4810 break;
4812 case REAL_TYPE:
4813 if (TREE_CODE (exp) != REAL_CST)
4814 error ("initializer for floating value is not a floating constant");
4815 else
4816 assemble_real (TREE_REAL_CST (exp), TYPE_MODE (TREE_TYPE (exp)), align);
4817 break;
4819 case COMPLEX_TYPE:
4820 output_constant (TREE_REALPART (exp), thissize / 2, align);
4821 output_constant (TREE_IMAGPART (exp), thissize / 2,
4822 min_align (align, BITS_PER_UNIT * (thissize / 2)));
4823 break;
4825 case ARRAY_TYPE:
4826 case VECTOR_TYPE:
4827 switch (TREE_CODE (exp))
4829 case CONSTRUCTOR:
4830 output_constructor (exp, size, align, NULL);
4831 return;
4832 case STRING_CST:
4833 thissize = MIN ((unsigned HOST_WIDE_INT)TREE_STRING_LENGTH (exp),
4834 size);
4835 assemble_string (TREE_STRING_POINTER (exp), thissize);
4836 break;
4838 case VECTOR_CST:
4840 int elt_size;
4841 tree link;
4842 unsigned int nalign;
4843 enum machine_mode inner;
4845 inner = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
4846 nalign = MIN (align, GET_MODE_ALIGNMENT (inner));
4848 elt_size = GET_MODE_SIZE (inner);
4850 link = TREE_VECTOR_CST_ELTS (exp);
4851 output_constant (TREE_VALUE (link), elt_size, align);
4852 thissize = elt_size;
4853 while ((link = TREE_CHAIN (link)) != NULL)
4855 output_constant (TREE_VALUE (link), elt_size, nalign);
4856 thissize += elt_size;
4858 break;
4860 default:
4861 gcc_unreachable ();
4863 break;
4865 case RECORD_TYPE:
4866 case UNION_TYPE:
4867 gcc_assert (TREE_CODE (exp) == CONSTRUCTOR);
4868 output_constructor (exp, size, align, NULL);
4869 return;
4871 case ERROR_MARK:
4872 return;
4874 default:
4875 gcc_unreachable ();
4878 if (size > thissize)
4879 assemble_zeros (size - thissize);
4883 /* Subroutine of output_constructor, used for computing the size of
4884 arrays of unspecified length. VAL must be a CONSTRUCTOR of an array
4885 type with an unspecified upper bound. */
4887 static unsigned HOST_WIDE_INT
4888 array_size_for_constructor (tree val)
4890 tree max_index, i;
4891 unsigned HOST_WIDE_INT cnt;
4892 tree index, value, tmp;
4894 /* This code used to attempt to handle string constants that are not
4895 arrays of single-bytes, but nothing else does, so there's no point in
4896 doing it here. */
4897 if (TREE_CODE (val) == STRING_CST)
4898 return TREE_STRING_LENGTH (val);
4900 max_index = NULL_TREE;
4901 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (val), cnt, index, value)
4903 if (TREE_CODE (index) == RANGE_EXPR)
4904 index = TREE_OPERAND (index, 1);
4905 if (max_index == NULL_TREE || tree_int_cst_lt (max_index, index))
4906 max_index = index;
4909 if (max_index == NULL_TREE)
4910 return 0;
4912 /* Compute the total number of array elements. */
4913 tmp = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (val)));
4914 i = size_binop (MINUS_EXPR, fold_convert (sizetype, max_index),
4915 fold_convert (sizetype, tmp));
4916 i = size_binop (PLUS_EXPR, i, build_int_cst (sizetype, 1));
4918 /* Multiply by the array element unit size to find number of bytes. */
4919 i = size_binop (MULT_EXPR, i, TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (val))));
4921 return tree_low_cst (i, 1);
4924 /* Other datastructures + helpers for output_constructor. */
4926 /* output_constructor local state to support interaction with helpers. */
4928 typedef struct {
4930 /* Received arguments. */
4931 tree exp; /* Constructor expression. */
4932 unsigned HOST_WIDE_INT size; /* # bytes to output - pad if necessary. */
4933 unsigned int align; /* Known initial alignment. */
4935 /* Constructor expression data. */
4936 tree type; /* Expression type. */
4937 tree field; /* Current field decl in a record. */
4938 tree min_index; /* Lower bound if specified for an array. */
4940 /* Output processing state. */
4941 HOST_WIDE_INT total_bytes; /* # bytes output so far / current position. */
4942 bool byte_buffer_in_use; /* Whether byte ... */
4943 int byte; /* ... contains part of a bitfield byte yet to
4944 be output. */
4946 int last_relative_index; /* Implicit or explicit index of the last
4947 array element output within a bitfield. */
4948 /* Current element. */
4949 tree val; /* Current element value. */
4950 tree index; /* Current element index. */
4952 } oc_local_state;
4954 /* Helper for output_constructor. From the current LOCAL state, output a
4955 RANGE_EXPR element. */
4957 static void
4958 output_constructor_array_range (oc_local_state *local)
4960 unsigned HOST_WIDE_INT fieldsize
4961 = int_size_in_bytes (TREE_TYPE (local->type));
4963 HOST_WIDE_INT lo_index
4964 = tree_low_cst (TREE_OPERAND (local->index, 0), 0);
4965 HOST_WIDE_INT hi_index
4966 = tree_low_cst (TREE_OPERAND (local->index, 1), 0);
4967 HOST_WIDE_INT index;
4969 unsigned int align2
4970 = min_align (local->align, fieldsize * BITS_PER_UNIT);
4972 for (index = lo_index; index <= hi_index; index++)
4974 /* Output the element's initial value. */
4975 if (local->val == NULL_TREE)
4976 assemble_zeros (fieldsize);
4977 else
4978 output_constant (local->val, fieldsize, align2);
4980 /* Count its size. */
4981 local->total_bytes += fieldsize;
4985 /* Helper for output_constructor. From the current LOCAL state, output a
4986 field element that is not true bitfield or part of an outer one. */
4988 static void
4989 output_constructor_regular_field (oc_local_state *local)
4991 /* Field size and position. Since this structure is static, we know the
4992 positions are constant. */
4993 unsigned HOST_WIDE_INT fieldsize;
4994 HOST_WIDE_INT fieldpos;
4996 unsigned int align2;
4998 if (local->index != NULL_TREE)
4999 fieldpos = (tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (local->val)), 1)
5000 * ((tree_low_cst (local->index, 0)
5001 - tree_low_cst (local->min_index, 0))));
5002 else if (local->field != NULL_TREE)
5003 fieldpos = int_byte_position (local->field);
5004 else
5005 fieldpos = 0;
5007 /* Output any buffered-up bit-fields preceding this element. */
5008 if (local->byte_buffer_in_use)
5010 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5011 local->total_bytes++;
5012 local->byte_buffer_in_use = false;
5015 /* Advance to offset of this element.
5016 Note no alignment needed in an array, since that is guaranteed
5017 if each element has the proper size. */
5018 if ((local->field != NULL_TREE || local->index != NULL_TREE)
5019 && fieldpos != local->total_bytes)
5021 gcc_assert (fieldpos >= local->total_bytes);
5022 assemble_zeros (fieldpos - local->total_bytes);
5023 local->total_bytes = fieldpos;
5026 /* Find the alignment of this element. */
5027 align2 = min_align (local->align, BITS_PER_UNIT * fieldpos);
5029 /* Determine size this element should occupy. */
5030 if (local->field)
5032 fieldsize = 0;
5034 /* If this is an array with an unspecified upper bound,
5035 the initializer determines the size. */
5036 /* ??? This ought to only checked if DECL_SIZE_UNIT is NULL,
5037 but we cannot do this until the deprecated support for
5038 initializing zero-length array members is removed. */
5039 if (TREE_CODE (TREE_TYPE (local->field)) == ARRAY_TYPE
5040 && TYPE_DOMAIN (TREE_TYPE (local->field))
5041 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (local->field))))
5043 fieldsize = array_size_for_constructor (local->val);
5044 /* Given a non-empty initialization, this field had
5045 better be last. */
5046 gcc_assert (!fieldsize || !TREE_CHAIN (local->field));
5048 else if (DECL_SIZE_UNIT (local->field))
5050 /* ??? This can't be right. If the decl size overflows
5051 a host integer we will silently emit no data. */
5052 if (host_integerp (DECL_SIZE_UNIT (local->field), 1))
5053 fieldsize = tree_low_cst (DECL_SIZE_UNIT (local->field), 1);
5056 else
5057 fieldsize = int_size_in_bytes (TREE_TYPE (local->type));
5059 /* Output the element's initial value. */
5060 if (local->val == NULL_TREE)
5061 assemble_zeros (fieldsize);
5062 else
5063 output_constant (local->val, fieldsize, align2);
5065 /* Count its size. */
5066 local->total_bytes += fieldsize;
5069 /* Helper for output_constructor. From the current LOCAL and OUTER states,
5070 output an element that is a true bitfield or part of an outer one. */
5072 static void
5073 output_constructor_bitfield (oc_local_state *local, oc_outer_state *outer)
5075 /* Bit size of this element. */
5076 HOST_WIDE_INT ebitsize
5077 = (local->field
5078 ? tree_low_cst (DECL_SIZE (local->field), 1)
5079 : tree_low_cst (TYPE_SIZE (TREE_TYPE (local->type)), 1));
5081 /* Relative index of this element if this is an array component. */
5082 HOST_WIDE_INT relative_index
5083 = (!local->field
5084 ? (local->index
5085 ? (tree_low_cst (local->index, 0)
5086 - tree_low_cst (local->min_index, 0))
5087 : local->last_relative_index + 1)
5088 : 0);
5090 /* Bit position of this element from the start of the containing
5091 constructor. */
5092 HOST_WIDE_INT constructor_relative_ebitpos
5093 = (local->field
5094 ? int_bit_position (local->field)
5095 : ebitsize * relative_index);
5097 /* Bit position of this element from the start of a possibly ongoing
5098 outer byte buffer. */
5099 HOST_WIDE_INT byte_relative_ebitpos
5100 = ((outer ? outer->bit_offset : 0) + constructor_relative_ebitpos);
5102 /* From the start of a possibly ongoing outer byte buffer, offsets to
5103 the first bit of this element and to the first bit past the end of
5104 this element. */
5105 HOST_WIDE_INT next_offset = byte_relative_ebitpos;
5106 HOST_WIDE_INT end_offset = byte_relative_ebitpos + ebitsize;
5108 local->last_relative_index = relative_index;
5110 if (local->val == NULL_TREE)
5111 local->val = integer_zero_node;
5113 while (TREE_CODE (local->val) == VIEW_CONVERT_EXPR
5114 || TREE_CODE (local->val) == NON_LVALUE_EXPR)
5115 local->val = TREE_OPERAND (local->val, 0);
5117 if (TREE_CODE (local->val) != INTEGER_CST
5118 && TREE_CODE (local->val) != CONSTRUCTOR)
5120 error ("invalid initial value for member %qE", DECL_NAME (local->field));
5121 return;
5124 /* If this field does not start in this (or, next) byte,
5125 skip some bytes. */
5126 if (next_offset / BITS_PER_UNIT != local->total_bytes)
5128 /* Output remnant of any bit field in previous bytes. */
5129 if (local->byte_buffer_in_use)
5131 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5132 local->total_bytes++;
5133 local->byte_buffer_in_use = false;
5136 /* If still not at proper byte, advance to there. */
5137 if (next_offset / BITS_PER_UNIT != local->total_bytes)
5139 gcc_assert (next_offset / BITS_PER_UNIT >= local->total_bytes);
5140 assemble_zeros (next_offset / BITS_PER_UNIT - local->total_bytes);
5141 local->total_bytes = next_offset / BITS_PER_UNIT;
5145 /* Set up the buffer if necessary. */
5146 if (!local->byte_buffer_in_use)
5148 local->byte = 0;
5149 if (ebitsize > 0)
5150 local->byte_buffer_in_use = true;
5153 /* If this is nested constructor, recurse passing the bit offset and the
5154 pending data, then retrieve the new pending data afterwards. */
5155 if (TREE_CODE (local->val) == CONSTRUCTOR)
5157 oc_outer_state output_state;
5159 output_state.bit_offset = next_offset % BITS_PER_UNIT;
5160 output_state.byte = local->byte;
5161 local->total_bytes
5162 += output_constructor (local->val, 0, 0, &output_state);
5163 local->byte = output_state.byte;
5164 return;
5167 /* Otherwise, we must split the element into pieces that fall within
5168 separate bytes, and combine each byte with previous or following
5169 bit-fields. */
5170 while (next_offset < end_offset)
5172 int this_time;
5173 int shift;
5174 HOST_WIDE_INT value;
5175 HOST_WIDE_INT next_byte = next_offset / BITS_PER_UNIT;
5176 HOST_WIDE_INT next_bit = next_offset % BITS_PER_UNIT;
5178 /* Advance from byte to byte
5179 within this element when necessary. */
5180 while (next_byte != local->total_bytes)
5182 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5183 local->total_bytes++;
5184 local->byte = 0;
5187 /* Number of bits we can process at once
5188 (all part of the same byte). */
5189 this_time = MIN (end_offset - next_offset,
5190 BITS_PER_UNIT - next_bit);
5191 if (BYTES_BIG_ENDIAN)
5193 /* On big-endian machine, take the most significant bits
5194 first (of the bits that are significant)
5195 and put them into bytes from the most significant end. */
5196 shift = end_offset - next_offset - this_time;
5198 /* Don't try to take a bunch of bits that cross
5199 the word boundary in the INTEGER_CST. We can
5200 only select bits from the LOW or HIGH part
5201 not from both. */
5202 if (shift < HOST_BITS_PER_WIDE_INT
5203 && shift + this_time > HOST_BITS_PER_WIDE_INT)
5205 this_time = shift + this_time - HOST_BITS_PER_WIDE_INT;
5206 shift = HOST_BITS_PER_WIDE_INT;
5209 /* Now get the bits from the appropriate constant word. */
5210 if (shift < HOST_BITS_PER_WIDE_INT)
5211 value = TREE_INT_CST_LOW (local->val);
5212 else
5214 gcc_assert (shift < 2 * HOST_BITS_PER_WIDE_INT);
5215 value = TREE_INT_CST_HIGH (local->val);
5216 shift -= HOST_BITS_PER_WIDE_INT;
5219 /* Get the result. This works only when:
5220 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
5221 local->byte |= (((value >> shift)
5222 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
5223 << (BITS_PER_UNIT - this_time - next_bit));
5225 else
5227 /* On little-endian machines,
5228 take first the least significant bits of the value
5229 and pack them starting at the least significant
5230 bits of the bytes. */
5231 shift = next_offset - byte_relative_ebitpos;
5233 /* Don't try to take a bunch of bits that cross
5234 the word boundary in the INTEGER_CST. We can
5235 only select bits from the LOW or HIGH part
5236 not from both. */
5237 if (shift < HOST_BITS_PER_WIDE_INT
5238 && shift + this_time > HOST_BITS_PER_WIDE_INT)
5239 this_time = (HOST_BITS_PER_WIDE_INT - shift);
5241 /* Now get the bits from the appropriate constant word. */
5242 if (shift < HOST_BITS_PER_WIDE_INT)
5243 value = TREE_INT_CST_LOW (local->val);
5244 else
5246 gcc_assert (shift < 2 * HOST_BITS_PER_WIDE_INT);
5247 value = TREE_INT_CST_HIGH (local->val);
5248 shift -= HOST_BITS_PER_WIDE_INT;
5251 /* Get the result. This works only when:
5252 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
5253 local->byte |= (((value >> shift)
5254 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
5255 << next_bit);
5258 next_offset += this_time;
5259 local->byte_buffer_in_use = true;
5263 /* Subroutine of output_constant, used for CONSTRUCTORs (aggregate constants).
5264 Generate at least SIZE bytes, padding if necessary. OUTER designates the
5265 caller output state of relevance in recursive invocations. */
5267 static unsigned HOST_WIDE_INT
5268 output_constructor (tree exp, unsigned HOST_WIDE_INT size,
5269 unsigned int align, oc_outer_state * outer)
5271 unsigned HOST_WIDE_INT cnt;
5272 constructor_elt *ce;
5274 oc_local_state local;
5276 /* Setup our local state to communicate with helpers. */
5277 local.exp = exp;
5278 local.size = size;
5279 local.align = align;
5281 local.total_bytes = 0;
5282 local.byte_buffer_in_use = outer != NULL;
5283 local.byte = outer ? outer->byte : 0;
5285 local.type = TREE_TYPE (exp);
5287 local.last_relative_index = -1;
5289 local.min_index = NULL_TREE;
5290 if (TREE_CODE (local.type) == ARRAY_TYPE
5291 && TYPE_DOMAIN (local.type) != NULL_TREE)
5292 local.min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (local.type));
5294 gcc_assert (HOST_BITS_PER_WIDE_INT >= BITS_PER_UNIT);
5296 /* As CE goes through the elements of the constant, FIELD goes through the
5297 structure fields if the constant is a structure. If the constant is a
5298 union, we override this by getting the field from the TREE_LIST element.
5299 But the constant could also be an array. Then FIELD is zero.
5301 There is always a maximum of one element in the chain LINK for unions
5302 (even if the initializer in a source program incorrectly contains
5303 more one). */
5305 local.field = NULL_TREE;
5306 if (TREE_CODE (local.type) == RECORD_TYPE)
5307 local.field = TYPE_FIELDS (local.type);
5309 for (cnt = 0;
5310 VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (exp), cnt, ce);
5311 cnt++, local.field = local.field ? TREE_CHAIN (local.field) : 0)
5313 local.val = ce->value;
5314 local.index = NULL_TREE;
5316 /* The element in a union constructor specifies the proper field
5317 or index. */
5318 if ((TREE_CODE (local.type) == RECORD_TYPE
5319 || TREE_CODE (local.type) == UNION_TYPE
5320 || TREE_CODE (local.type) == QUAL_UNION_TYPE)
5321 && ce->index != NULL_TREE)
5322 local.field = ce->index;
5324 else if (TREE_CODE (local.type) == ARRAY_TYPE)
5325 local.index = ce->index;
5327 #ifdef ASM_COMMENT_START
5328 if (local.field && flag_verbose_asm)
5329 fprintf (asm_out_file, "%s %s:\n",
5330 ASM_COMMENT_START,
5331 DECL_NAME (local.field)
5332 ? IDENTIFIER_POINTER (DECL_NAME (local.field))
5333 : "<anonymous>");
5334 #endif
5336 /* Eliminate the marker that makes a cast not be an lvalue. */
5337 if (local.val != NULL_TREE)
5338 STRIP_NOPS (local.val);
5340 /* Output the current element, using the appropriate helper ... */
5342 /* For an array slice not part of an outer bitfield. */
5343 if (!outer
5344 && local.index != NULL_TREE
5345 && TREE_CODE (local.index) == RANGE_EXPR)
5346 output_constructor_array_range (&local);
5348 /* For a field that is neither a true bitfield nor part of an outer one,
5349 known to be at least byte aligned and multiple-of-bytes long. */
5350 else if (!outer
5351 && (local.field == NULL_TREE
5352 || !CONSTRUCTOR_BITFIELD_P (local.field)))
5353 output_constructor_regular_field (&local);
5355 /* For a true bitfield or part of an outer one. */
5356 else
5357 output_constructor_bitfield (&local, outer);
5360 /* If we are not at toplevel, save the pending data for our caller.
5361 Otherwise output the pending data and padding zeros as needed. */
5362 if (outer)
5363 outer->byte = local.byte;
5364 else
5366 if (local.byte_buffer_in_use)
5368 assemble_integer (GEN_INT (local.byte), 1, BITS_PER_UNIT, 1);
5369 local.total_bytes++;
5372 if ((unsigned HOST_WIDE_INT)local.total_bytes < local.size)
5374 assemble_zeros (local.size - local.total_bytes);
5375 local.total_bytes = local.size;
5379 return local.total_bytes;
5382 /* Mark DECL as weak. */
5384 static void
5385 mark_weak (tree decl)
5387 DECL_WEAK (decl) = 1;
5389 if (DECL_RTL_SET_P (decl)
5390 && MEM_P (DECL_RTL (decl))
5391 && XEXP (DECL_RTL (decl), 0)
5392 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == SYMBOL_REF)
5393 SYMBOL_REF_WEAK (XEXP (DECL_RTL (decl), 0)) = 1;
5396 /* Merge weak status between NEWDECL and OLDDECL. */
5398 void
5399 merge_weak (tree newdecl, tree olddecl)
5401 if (DECL_WEAK (newdecl) == DECL_WEAK (olddecl))
5403 if (DECL_WEAK (newdecl) && SUPPORTS_WEAK)
5405 tree *pwd;
5406 /* We put the NEWDECL on the weak_decls list at some point
5407 and OLDDECL as well. Keep just OLDDECL on the list. */
5408 for (pwd = &weak_decls; *pwd; pwd = &TREE_CHAIN (*pwd))
5409 if (TREE_VALUE (*pwd) == newdecl)
5411 *pwd = TREE_CHAIN (*pwd);
5412 break;
5415 return;
5418 if (DECL_WEAK (newdecl))
5420 tree wd;
5422 /* NEWDECL is weak, but OLDDECL is not. */
5424 /* If we already output the OLDDECL, we're in trouble; we can't
5425 go back and make it weak. This error cannot be caught in
5426 declare_weak because the NEWDECL and OLDDECL was not yet
5427 been merged; therefore, TREE_ASM_WRITTEN was not set. */
5428 if (TREE_ASM_WRITTEN (olddecl))
5429 error ("weak declaration of %q+D must precede definition",
5430 newdecl);
5432 /* If we've already generated rtl referencing OLDDECL, we may
5433 have done so in a way that will not function properly with
5434 a weak symbol. */
5435 else if (TREE_USED (olddecl)
5436 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (olddecl)))
5437 warning (0, "weak declaration of %q+D after first use results "
5438 "in unspecified behavior", newdecl);
5440 if (SUPPORTS_WEAK)
5442 /* We put the NEWDECL on the weak_decls list at some point.
5443 Replace it with the OLDDECL. */
5444 for (wd = weak_decls; wd; wd = TREE_CHAIN (wd))
5445 if (TREE_VALUE (wd) == newdecl)
5447 TREE_VALUE (wd) = olddecl;
5448 break;
5450 /* We may not find the entry on the list. If NEWDECL is a
5451 weak alias, then we will have already called
5452 globalize_decl to remove the entry; in that case, we do
5453 not need to do anything. */
5456 /* Make the OLDDECL weak; it's OLDDECL that we'll be keeping. */
5457 mark_weak (olddecl);
5459 else
5460 /* OLDDECL was weak, but NEWDECL was not explicitly marked as
5461 weak. Just update NEWDECL to indicate that it's weak too. */
5462 mark_weak (newdecl);
5465 /* Declare DECL to be a weak symbol. */
5467 void
5468 declare_weak (tree decl)
5470 if (! TREE_PUBLIC (decl))
5471 error ("weak declaration of %q+D must be public", decl);
5472 else if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
5473 error ("weak declaration of %q+D must precede definition", decl);
5474 else if (!SUPPORTS_WEAK)
5475 warning (0, "weak declaration of %q+D not supported", decl);
5477 mark_weak (decl);
5478 if (!lookup_attribute ("weak", DECL_ATTRIBUTES (decl)))
5479 DECL_ATTRIBUTES (decl)
5480 = tree_cons (get_identifier ("weak"), NULL, DECL_ATTRIBUTES (decl));
5483 static void
5484 weak_finish_1 (tree decl)
5486 #if defined (ASM_WEAKEN_DECL) || defined (ASM_WEAKEN_LABEL)
5487 const char *const name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5488 #endif
5490 if (! TREE_USED (decl))
5491 return;
5493 #ifdef ASM_WEAKEN_DECL
5494 ASM_WEAKEN_DECL (asm_out_file, decl, name, NULL);
5495 #else
5496 #ifdef ASM_WEAKEN_LABEL
5497 ASM_WEAKEN_LABEL (asm_out_file, name);
5498 #else
5499 #ifdef ASM_OUTPUT_WEAK_ALIAS
5501 static bool warn_once = 0;
5502 if (! warn_once)
5504 warning (0, "only weak aliases are supported in this configuration");
5505 warn_once = 1;
5507 return;
5509 #endif
5510 #endif
5511 #endif
5514 /* This TREE_LIST contains weakref targets. */
5516 static GTY(()) tree weakref_targets;
5518 /* Forward declaration. */
5519 static tree find_decl_and_mark_needed (tree decl, tree target);
5521 /* Emit any pending weak declarations. */
5523 void
5524 weak_finish (void)
5526 tree t;
5528 for (t = weakref_targets; t; t = TREE_CHAIN (t))
5530 tree alias_decl = TREE_PURPOSE (t);
5531 tree target = ultimate_transparent_alias_target (&TREE_VALUE (t));
5533 if (! TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (alias_decl)))
5534 /* Remove alias_decl from the weak list, but leave entries for
5535 the target alone. */
5536 target = NULL_TREE;
5537 #ifndef ASM_OUTPUT_WEAKREF
5538 else if (! TREE_SYMBOL_REFERENCED (target))
5540 /* Use ASM_WEAKEN_LABEL only if ASM_WEAKEN_DECL is not
5541 defined, otherwise we and weak_finish_1 would use
5542 different macros. */
5543 # if defined ASM_WEAKEN_LABEL && ! defined ASM_WEAKEN_DECL
5544 ASM_WEAKEN_LABEL (asm_out_file, IDENTIFIER_POINTER (target));
5545 # else
5546 tree decl = find_decl_and_mark_needed (alias_decl, target);
5548 if (! decl)
5550 decl = build_decl (DECL_SOURCE_LOCATION (alias_decl),
5551 TREE_CODE (alias_decl), target,
5552 TREE_TYPE (alias_decl));
5554 DECL_EXTERNAL (decl) = 1;
5555 TREE_PUBLIC (decl) = 1;
5556 DECL_ARTIFICIAL (decl) = 1;
5557 TREE_NOTHROW (decl) = TREE_NOTHROW (alias_decl);
5558 TREE_USED (decl) = 1;
5561 weak_finish_1 (decl);
5562 # endif
5564 #endif
5567 tree *p;
5568 tree t2;
5570 /* Remove the alias and the target from the pending weak list
5571 so that we do not emit any .weak directives for the former,
5572 nor multiple .weak directives for the latter. */
5573 for (p = &weak_decls; (t2 = *p) ; )
5575 if (TREE_VALUE (t2) == alias_decl
5576 || target == DECL_ASSEMBLER_NAME (TREE_VALUE (t2)))
5577 *p = TREE_CHAIN (t2);
5578 else
5579 p = &TREE_CHAIN (t2);
5582 /* Remove other weakrefs to the same target, to speed things up. */
5583 for (p = &TREE_CHAIN (t); (t2 = *p) ; )
5585 if (target == ultimate_transparent_alias_target (&TREE_VALUE (t2)))
5586 *p = TREE_CHAIN (t2);
5587 else
5588 p = &TREE_CHAIN (t2);
5593 for (t = weak_decls; t; t = TREE_CHAIN (t))
5595 tree decl = TREE_VALUE (t);
5597 weak_finish_1 (decl);
5601 /* Emit the assembly bits to indicate that DECL is globally visible. */
5603 static void
5604 globalize_decl (tree decl)
5607 #if defined (ASM_WEAKEN_LABEL) || defined (ASM_WEAKEN_DECL)
5608 if (DECL_WEAK (decl))
5610 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
5611 tree *p, t;
5613 #ifdef ASM_WEAKEN_DECL
5614 ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
5615 #else
5616 ASM_WEAKEN_LABEL (asm_out_file, name);
5617 #endif
5619 /* Remove this function from the pending weak list so that
5620 we do not emit multiple .weak directives for it. */
5621 for (p = &weak_decls; (t = *p) ; )
5623 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
5624 *p = TREE_CHAIN (t);
5625 else
5626 p = &TREE_CHAIN (t);
5629 /* Remove weakrefs to the same target from the pending weakref
5630 list, for the same reason. */
5631 for (p = &weakref_targets; (t = *p) ; )
5633 if (DECL_ASSEMBLER_NAME (decl)
5634 == ultimate_transparent_alias_target (&TREE_VALUE (t)))
5635 *p = TREE_CHAIN (t);
5636 else
5637 p = &TREE_CHAIN (t);
5640 return;
5642 #endif
5644 targetm.asm_out.globalize_decl_name (asm_out_file, decl);
5647 VEC(alias_pair,gc) *alias_pairs;
5649 /* Given an assembly name, find the decl it is associated with. At the
5650 same time, mark it needed for cgraph. */
5652 static tree
5653 find_decl_and_mark_needed (tree decl, tree target)
5655 struct cgraph_node *fnode = NULL;
5656 struct varpool_node *vnode = NULL;
5658 if (TREE_CODE (decl) == FUNCTION_DECL)
5660 fnode = cgraph_node_for_asm (target);
5661 if (fnode == NULL)
5662 vnode = varpool_node_for_asm (target);
5664 else
5666 vnode = varpool_node_for_asm (target);
5667 if (vnode == NULL)
5668 fnode = cgraph_node_for_asm (target);
5671 if (fnode)
5673 cgraph_mark_needed_node (fnode);
5674 return fnode->decl;
5676 else if (vnode)
5678 varpool_mark_needed_node (vnode);
5679 vnode->force_output = 1;
5680 return vnode->decl;
5682 else
5683 return NULL_TREE;
5686 /* Output the assembler code for a define (equate) using ASM_OUTPUT_DEF
5687 or ASM_OUTPUT_DEF_FROM_DECLS. The function defines the symbol whose
5688 tree node is DECL to have the value of the tree node TARGET. */
5690 static void
5691 do_assemble_alias (tree decl, tree target)
5693 if (TREE_ASM_WRITTEN (decl))
5694 return;
5696 /* We must force creation of DECL_RTL for debug info generation, even though
5697 we don't use it here. */
5698 make_decl_rtl (decl);
5700 TREE_ASM_WRITTEN (decl) = 1;
5701 TREE_ASM_WRITTEN (DECL_ASSEMBLER_NAME (decl)) = 1;
5703 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
5705 ultimate_transparent_alias_target (&target);
5707 if (!targetm.have_tls
5708 && TREE_CODE (decl) == VAR_DECL
5709 && DECL_THREAD_LOCAL_P (decl))
5711 decl = emutls_decl (decl);
5712 target = get_emutls_object_name (target);
5715 if (!TREE_SYMBOL_REFERENCED (target))
5716 weakref_targets = tree_cons (decl, target, weakref_targets);
5718 #ifdef ASM_OUTPUT_WEAKREF
5719 ASM_OUTPUT_WEAKREF (asm_out_file, decl,
5720 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
5721 IDENTIFIER_POINTER (target));
5722 #else
5723 if (!SUPPORTS_WEAK)
5725 error_at (DECL_SOURCE_LOCATION (decl),
5726 "weakref is not supported in this configuration");
5727 return;
5729 #endif
5730 return;
5733 if (!targetm.have_tls
5734 && TREE_CODE (decl) == VAR_DECL
5735 && DECL_THREAD_LOCAL_P (decl))
5737 decl = emutls_decl (decl);
5738 target = get_emutls_object_name (target);
5741 #ifdef ASM_OUTPUT_DEF
5742 /* Make name accessible from other files, if appropriate. */
5744 if (TREE_PUBLIC (decl))
5746 globalize_decl (decl);
5747 maybe_assemble_visibility (decl);
5750 # ifdef ASM_OUTPUT_DEF_FROM_DECLS
5751 ASM_OUTPUT_DEF_FROM_DECLS (asm_out_file, decl, target);
5752 # else
5753 ASM_OUTPUT_DEF (asm_out_file,
5754 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
5755 IDENTIFIER_POINTER (target));
5756 # endif
5757 #elif defined (ASM_OUTPUT_WEAK_ALIAS) || defined (ASM_WEAKEN_DECL)
5759 const char *name;
5760 tree *p, t;
5762 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5763 # ifdef ASM_WEAKEN_DECL
5764 ASM_WEAKEN_DECL (asm_out_file, decl, name, IDENTIFIER_POINTER (target));
5765 # else
5766 ASM_OUTPUT_WEAK_ALIAS (asm_out_file, name, IDENTIFIER_POINTER (target));
5767 # endif
5768 /* Remove this function from the pending weak list so that
5769 we do not emit multiple .weak directives for it. */
5770 for (p = &weak_decls; (t = *p) ; )
5771 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
5772 *p = TREE_CHAIN (t);
5773 else
5774 p = &TREE_CHAIN (t);
5776 /* Remove weakrefs to the same target from the pending weakref
5777 list, for the same reason. */
5778 for (p = &weakref_targets; (t = *p) ; )
5780 if (DECL_ASSEMBLER_NAME (decl)
5781 == ultimate_transparent_alias_target (&TREE_VALUE (t)))
5782 *p = TREE_CHAIN (t);
5783 else
5784 p = &TREE_CHAIN (t);
5787 #endif
5791 /* Remove the alias pairing for functions that are no longer in the call
5792 graph. */
5794 void
5795 remove_unreachable_alias_pairs (void)
5797 unsigned i;
5798 alias_pair *p;
5800 if (alias_pairs == NULL)
5801 return;
5803 for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); )
5805 if (!DECL_EXTERNAL (p->decl))
5807 struct cgraph_node *fnode = NULL;
5808 struct varpool_node *vnode = NULL;
5809 fnode = cgraph_node_for_asm (p->target);
5810 vnode = (fnode == NULL) ? varpool_node_for_asm (p->target) : NULL;
5811 if (fnode == NULL && vnode == NULL)
5813 VEC_unordered_remove (alias_pair, alias_pairs, i);
5814 continue;
5818 i++;
5823 /* First pass of completing pending aliases. Make sure that cgraph knows
5824 which symbols will be required. */
5826 void
5827 finish_aliases_1 (void)
5829 unsigned i;
5830 alias_pair *p;
5832 for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); i++)
5834 tree target_decl;
5836 target_decl = find_decl_and_mark_needed (p->decl, p->target);
5837 if (target_decl == NULL)
5839 if (! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
5840 error ("%q+D aliased to undefined symbol %qE",
5841 p->decl, p->target);
5843 else if (DECL_EXTERNAL (target_decl)
5844 /* We use local aliases for C++ thunks to force the tailcall
5845 to bind locally. Of course this is a hack - to keep it
5846 working do the following (which is not strictly correct). */
5847 && (! TREE_CODE (target_decl) == FUNCTION_DECL
5848 || ! DECL_VIRTUAL_P (target_decl))
5849 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
5850 error ("%q+D aliased to external symbol %qE",
5851 p->decl, p->target);
5855 /* Second pass of completing pending aliases. Emit the actual assembly.
5856 This happens at the end of compilation and thus it is assured that the
5857 target symbol has been emitted. */
5859 void
5860 finish_aliases_2 (void)
5862 unsigned i;
5863 alias_pair *p;
5865 for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); i++)
5866 do_assemble_alias (p->decl, p->target);
5868 VEC_truncate (alias_pair, alias_pairs, 0);
5871 /* Emit an assembler directive to make the symbol for DECL an alias to
5872 the symbol for TARGET. */
5874 void
5875 assemble_alias (tree decl, tree target)
5877 tree target_decl;
5878 bool is_weakref = false;
5880 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
5882 tree alias = DECL_ASSEMBLER_NAME (decl);
5884 is_weakref = true;
5886 ultimate_transparent_alias_target (&target);
5888 if (alias == target)
5889 error ("weakref %q+D ultimately targets itself", decl);
5890 else
5892 #ifndef ASM_OUTPUT_WEAKREF
5893 IDENTIFIER_TRANSPARENT_ALIAS (alias) = 1;
5894 TREE_CHAIN (alias) = target;
5895 #endif
5897 if (TREE_PUBLIC (decl))
5898 error ("weakref %q+D must have static linkage", decl);
5900 else
5902 #if !defined (ASM_OUTPUT_DEF)
5903 # if !defined(ASM_OUTPUT_WEAK_ALIAS) && !defined (ASM_WEAKEN_DECL)
5904 error_at (DECL_SOURCE_LOCATION (decl),
5905 "alias definitions not supported in this configuration");
5906 return;
5907 # else
5908 if (!DECL_WEAK (decl))
5910 error_at (DECL_SOURCE_LOCATION (decl),
5911 "only weak aliases are supported in this configuration");
5912 return;
5914 # endif
5915 #endif
5917 TREE_USED (decl) = 1;
5919 /* A quirk of the initial implementation of aliases required that the user
5920 add "extern" to all of them. Which is silly, but now historical. Do
5921 note that the symbol is in fact locally defined. */
5922 if (! is_weakref)
5923 DECL_EXTERNAL (decl) = 0;
5925 /* Allow aliases to aliases. */
5926 if (TREE_CODE (decl) == FUNCTION_DECL)
5927 cgraph_node (decl)->alias = true;
5928 else
5929 varpool_node (decl)->alias = true;
5931 /* If the target has already been emitted, we don't have to queue the
5932 alias. This saves a tad of memory. */
5933 if (cgraph_global_info_ready)
5934 target_decl = find_decl_and_mark_needed (decl, target);
5935 else
5936 target_decl= NULL;
5937 if (target_decl && TREE_ASM_WRITTEN (target_decl))
5938 do_assemble_alias (decl, target);
5939 else
5941 alias_pair *p = VEC_safe_push (alias_pair, gc, alias_pairs, NULL);
5942 p->decl = decl;
5943 p->target = target;
5947 /* Emit an assembler directive to set symbol for DECL visibility to
5948 the visibility type VIS, which must not be VISIBILITY_DEFAULT. */
5950 void
5951 default_assemble_visibility (tree decl ATTRIBUTE_UNUSED,
5952 int vis ATTRIBUTE_UNUSED)
5954 #ifdef HAVE_GAS_HIDDEN
5955 static const char * const visibility_types[] = {
5956 NULL, "protected", "hidden", "internal"
5959 const char *name, *type;
5961 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5962 type = visibility_types[vis];
5964 fprintf (asm_out_file, "\t.%s\t", type);
5965 assemble_name (asm_out_file, name);
5966 fprintf (asm_out_file, "\n");
5967 #else
5968 warning (OPT_Wattributes, "visibility attribute not supported "
5969 "in this configuration; ignored");
5970 #endif
5973 /* A helper function to call assemble_visibility when needed for a decl. */
5976 maybe_assemble_visibility (tree decl)
5978 enum symbol_visibility vis = DECL_VISIBILITY (decl);
5980 if (vis != VISIBILITY_DEFAULT)
5982 targetm.asm_out.assemble_visibility (decl, vis);
5983 return 1;
5985 else
5986 return 0;
5989 /* Returns 1 if the target configuration supports defining public symbols
5990 so that one of them will be chosen at link time instead of generating a
5991 multiply-defined symbol error, whether through the use of weak symbols or
5992 a target-specific mechanism for having duplicates discarded. */
5995 supports_one_only (void)
5997 if (SUPPORTS_ONE_ONLY)
5998 return 1;
5999 return SUPPORTS_WEAK;
6002 /* Set up DECL as a public symbol that can be defined in multiple
6003 translation units without generating a linker error. */
6005 void
6006 make_decl_one_only (tree decl, tree comdat_group)
6008 gcc_assert (TREE_CODE (decl) == VAR_DECL
6009 || TREE_CODE (decl) == FUNCTION_DECL);
6011 TREE_PUBLIC (decl) = 1;
6013 if (SUPPORTS_ONE_ONLY)
6015 #ifdef MAKE_DECL_ONE_ONLY
6016 MAKE_DECL_ONE_ONLY (decl);
6017 #endif
6018 DECL_COMDAT_GROUP (decl) = comdat_group;
6020 else if (TREE_CODE (decl) == VAR_DECL
6021 && (DECL_INITIAL (decl) == 0 || DECL_INITIAL (decl) == error_mark_node))
6022 DECL_COMMON (decl) = 1;
6023 else
6025 gcc_assert (SUPPORTS_WEAK);
6026 DECL_WEAK (decl) = 1;
6030 void
6031 init_varasm_once (void)
6033 section_htab = htab_create_ggc (31, section_entry_hash,
6034 section_entry_eq, NULL);
6035 object_block_htab = htab_create_ggc (31, object_block_entry_hash,
6036 object_block_entry_eq, NULL);
6037 const_desc_htab = htab_create_ggc (1009, const_desc_hash,
6038 const_desc_eq, NULL);
6040 const_alias_set = new_alias_set ();
6041 shared_constant_pool = create_constant_pool ();
6043 #ifdef TEXT_SECTION_ASM_OP
6044 text_section = get_unnamed_section (SECTION_CODE, output_section_asm_op,
6045 TEXT_SECTION_ASM_OP);
6046 #endif
6048 #ifdef DATA_SECTION_ASM_OP
6049 data_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
6050 DATA_SECTION_ASM_OP);
6051 #endif
6053 #ifdef SDATA_SECTION_ASM_OP
6054 sdata_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
6055 SDATA_SECTION_ASM_OP);
6056 #endif
6058 #ifdef READONLY_DATA_SECTION_ASM_OP
6059 readonly_data_section = get_unnamed_section (0, output_section_asm_op,
6060 READONLY_DATA_SECTION_ASM_OP);
6061 #endif
6063 #ifdef CTORS_SECTION_ASM_OP
6064 ctors_section = get_unnamed_section (0, output_section_asm_op,
6065 CTORS_SECTION_ASM_OP);
6066 #endif
6068 #ifdef DTORS_SECTION_ASM_OP
6069 dtors_section = get_unnamed_section (0, output_section_asm_op,
6070 DTORS_SECTION_ASM_OP);
6071 #endif
6073 #ifdef BSS_SECTION_ASM_OP
6074 bss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
6075 output_section_asm_op,
6076 BSS_SECTION_ASM_OP);
6077 #endif
6079 #ifdef SBSS_SECTION_ASM_OP
6080 sbss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
6081 output_section_asm_op,
6082 SBSS_SECTION_ASM_OP);
6083 #endif
6085 tls_comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6086 | SECTION_COMMON, emit_tls_common);
6087 lcomm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6088 | SECTION_COMMON, emit_local);
6089 comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6090 | SECTION_COMMON, emit_common);
6092 #if defined ASM_OUTPUT_ALIGNED_BSS || defined ASM_OUTPUT_BSS
6093 bss_noswitch_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS,
6094 emit_bss);
6095 #endif
6097 targetm.asm_out.init_sections ();
6099 if (readonly_data_section == NULL)
6100 readonly_data_section = text_section;
6103 enum tls_model
6104 decl_default_tls_model (const_tree decl)
6106 enum tls_model kind;
6107 bool is_local;
6109 is_local = targetm.binds_local_p (decl);
6110 if (!flag_shlib)
6112 if (is_local)
6113 kind = TLS_MODEL_LOCAL_EXEC;
6114 else
6115 kind = TLS_MODEL_INITIAL_EXEC;
6118 /* Local dynamic is inefficient when we're not combining the
6119 parts of the address. */
6120 else if (optimize && is_local)
6121 kind = TLS_MODEL_LOCAL_DYNAMIC;
6122 else
6123 kind = TLS_MODEL_GLOBAL_DYNAMIC;
6124 if (kind < flag_tls_default)
6125 kind = flag_tls_default;
6127 return kind;
6130 /* Select a set of attributes for section NAME based on the properties
6131 of DECL and whether or not RELOC indicates that DECL's initializer
6132 might contain runtime relocations.
6134 We make the section read-only and executable for a function decl,
6135 read-only for a const data decl, and writable for a non-const data decl. */
6137 unsigned int
6138 default_section_type_flags (tree decl, const char *name, int reloc)
6140 unsigned int flags;
6142 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
6143 flags = SECTION_CODE;
6144 else if (decl && decl_readonly_section (decl, reloc))
6145 flags = 0;
6146 else if (current_function_decl
6147 && cfun
6148 && crtl->subsections.unlikely_text_section_name
6149 && strcmp (name, crtl->subsections.unlikely_text_section_name) == 0)
6150 flags = SECTION_CODE;
6151 else if (!decl
6152 && (!current_function_decl || !cfun)
6153 && strcmp (name, UNLIKELY_EXECUTED_TEXT_SECTION_NAME) == 0)
6154 flags = SECTION_CODE;
6155 else
6156 flags = SECTION_WRITE;
6158 if (decl && DECL_ONE_ONLY (decl))
6159 flags |= SECTION_LINKONCE;
6161 if (decl && TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
6162 flags |= SECTION_TLS | SECTION_WRITE;
6164 if (strcmp (name, ".bss") == 0
6165 || strncmp (name, ".bss.", 5) == 0
6166 || strncmp (name, ".gnu.linkonce.b.", 16) == 0
6167 || strcmp (name, ".sbss") == 0
6168 || strncmp (name, ".sbss.", 6) == 0
6169 || strncmp (name, ".gnu.linkonce.sb.", 17) == 0)
6170 flags |= SECTION_BSS;
6172 if (strcmp (name, ".tdata") == 0
6173 || strncmp (name, ".tdata.", 7) == 0
6174 || strncmp (name, ".gnu.linkonce.td.", 17) == 0)
6175 flags |= SECTION_TLS;
6177 if (strcmp (name, ".tbss") == 0
6178 || strncmp (name, ".tbss.", 6) == 0
6179 || strncmp (name, ".gnu.linkonce.tb.", 17) == 0)
6180 flags |= SECTION_TLS | SECTION_BSS;
6182 /* These three sections have special ELF types. They are neither
6183 SHT_PROGBITS nor SHT_NOBITS, so when changing sections we don't
6184 want to print a section type (@progbits or @nobits). If someone
6185 is silly enough to emit code or TLS variables to one of these
6186 sections, then don't handle them specially. */
6187 if (!(flags & (SECTION_CODE | SECTION_BSS | SECTION_TLS))
6188 && (strcmp (name, ".init_array") == 0
6189 || strcmp (name, ".fini_array") == 0
6190 || strcmp (name, ".preinit_array") == 0))
6191 flags |= SECTION_NOTYPE;
6193 return flags;
6196 /* Return true if the target supports some form of global BSS,
6197 either through bss_noswitch_section, or by selecting a BSS
6198 section in TARGET_ASM_SELECT_SECTION. */
6200 bool
6201 have_global_bss_p (void)
6203 return bss_noswitch_section || targetm.have_switchable_bss_sections;
6206 /* Output assembly to switch to section NAME with attribute FLAGS.
6207 Four variants for common object file formats. */
6209 void
6210 default_no_named_section (const char *name ATTRIBUTE_UNUSED,
6211 unsigned int flags ATTRIBUTE_UNUSED,
6212 tree decl ATTRIBUTE_UNUSED)
6214 /* Some object formats don't support named sections at all. The
6215 front-end should already have flagged this as an error. */
6216 gcc_unreachable ();
6219 #ifndef TLS_SECTION_ASM_FLAG
6220 #define TLS_SECTION_ASM_FLAG 'T'
6221 #endif
6223 void
6224 default_elf_asm_named_section (const char *name, unsigned int flags,
6225 tree decl ATTRIBUTE_UNUSED)
6227 char flagchars[10], *f = flagchars;
6229 /* If we have already declared this section, we can use an
6230 abbreviated form to switch back to it -- unless this section is
6231 part of a COMDAT groups, in which case GAS requires the full
6232 declaration every time. */
6233 if (!(HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6234 && (flags & SECTION_DECLARED))
6236 fprintf (asm_out_file, "\t.section\t%s\n", name);
6237 return;
6240 if (!(flags & SECTION_DEBUG))
6241 *f++ = 'a';
6242 if (flags & SECTION_WRITE)
6243 *f++ = 'w';
6244 if (flags & SECTION_CODE)
6245 *f++ = 'x';
6246 if (flags & SECTION_SMALL)
6247 *f++ = 's';
6248 if (flags & SECTION_MERGE)
6249 *f++ = 'M';
6250 if (flags & SECTION_STRINGS)
6251 *f++ = 'S';
6252 if (flags & SECTION_TLS)
6253 *f++ = TLS_SECTION_ASM_FLAG;
6254 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6255 *f++ = 'G';
6256 *f = '\0';
6258 fprintf (asm_out_file, "\t.section\t%s,\"%s\"", name, flagchars);
6260 if (!(flags & SECTION_NOTYPE))
6262 const char *type;
6263 const char *format;
6265 if (flags & SECTION_BSS)
6266 type = "nobits";
6267 else
6268 type = "progbits";
6270 format = ",@%s";
6271 #ifdef ASM_COMMENT_START
6272 /* On platforms that use "@" as the assembly comment character,
6273 use "%" instead. */
6274 if (strcmp (ASM_COMMENT_START, "@") == 0)
6275 format = ",%%%s";
6276 #endif
6277 fprintf (asm_out_file, format, type);
6279 if (flags & SECTION_ENTSIZE)
6280 fprintf (asm_out_file, ",%d", flags & SECTION_ENTSIZE);
6281 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6283 if (TREE_CODE (decl) == IDENTIFIER_NODE)
6284 fprintf (asm_out_file, ",%s,comdat", IDENTIFIER_POINTER (decl));
6285 else
6286 fprintf (asm_out_file, ",%s,comdat",
6287 IDENTIFIER_POINTER (DECL_COMDAT_GROUP (decl)));
6291 putc ('\n', asm_out_file);
6294 void
6295 default_coff_asm_named_section (const char *name, unsigned int flags,
6296 tree decl ATTRIBUTE_UNUSED)
6298 char flagchars[8], *f = flagchars;
6300 if (flags & SECTION_WRITE)
6301 *f++ = 'w';
6302 if (flags & SECTION_CODE)
6303 *f++ = 'x';
6304 *f = '\0';
6306 fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
6309 void
6310 default_pe_asm_named_section (const char *name, unsigned int flags,
6311 tree decl)
6313 default_coff_asm_named_section (name, flags, decl);
6315 if (flags & SECTION_LINKONCE)
6317 /* Functions may have been compiled at various levels of
6318 optimization so we can't use `same_size' here.
6319 Instead, have the linker pick one. */
6320 fprintf (asm_out_file, "\t.linkonce %s\n",
6321 (flags & SECTION_CODE ? "discard" : "same_size"));
6325 /* The lame default section selector. */
6327 section *
6328 default_select_section (tree decl, int reloc,
6329 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
6331 if (DECL_P (decl))
6333 if (decl_readonly_section (decl, reloc))
6334 return readonly_data_section;
6336 else if (TREE_CODE (decl) == CONSTRUCTOR)
6338 if (! ((flag_pic && reloc)
6339 || !TREE_READONLY (decl)
6340 || TREE_SIDE_EFFECTS (decl)
6341 || !TREE_CONSTANT (decl)))
6342 return readonly_data_section;
6344 else if (TREE_CODE (decl) == STRING_CST)
6345 return readonly_data_section;
6346 else if (! (flag_pic && reloc))
6347 return readonly_data_section;
6349 return data_section;
6352 enum section_category
6353 categorize_decl_for_section (const_tree decl, int reloc)
6355 enum section_category ret;
6357 if (TREE_CODE (decl) == FUNCTION_DECL)
6358 return SECCAT_TEXT;
6359 else if (TREE_CODE (decl) == STRING_CST)
6361 if (flag_mudflap) /* or !flag_merge_constants */
6362 return SECCAT_RODATA;
6363 else
6364 return SECCAT_RODATA_MERGE_STR;
6366 else if (TREE_CODE (decl) == VAR_DECL)
6368 if (bss_initializer_p (decl))
6369 ret = SECCAT_BSS;
6370 else if (! TREE_READONLY (decl)
6371 || TREE_SIDE_EFFECTS (decl)
6372 || ! TREE_CONSTANT (DECL_INITIAL (decl)))
6374 /* Here the reloc_rw_mask is not testing whether the section should
6375 be read-only or not, but whether the dynamic link will have to
6376 do something. If so, we wish to segregate the data in order to
6377 minimize cache misses inside the dynamic linker. */
6378 if (reloc & targetm.asm_out.reloc_rw_mask ())
6379 ret = reloc == 1 ? SECCAT_DATA_REL_LOCAL : SECCAT_DATA_REL;
6380 else
6381 ret = SECCAT_DATA;
6383 else if (reloc & targetm.asm_out.reloc_rw_mask ())
6384 ret = reloc == 1 ? SECCAT_DATA_REL_RO_LOCAL : SECCAT_DATA_REL_RO;
6385 else if (reloc || flag_merge_constants < 2)
6386 /* C and C++ don't allow different variables to share the same
6387 location. -fmerge-all-constants allows even that (at the
6388 expense of not conforming). */
6389 ret = SECCAT_RODATA;
6390 else if (TREE_CODE (DECL_INITIAL (decl)) == STRING_CST)
6391 ret = SECCAT_RODATA_MERGE_STR_INIT;
6392 else
6393 ret = SECCAT_RODATA_MERGE_CONST;
6395 else if (TREE_CODE (decl) == CONSTRUCTOR)
6397 if ((reloc & targetm.asm_out.reloc_rw_mask ())
6398 || TREE_SIDE_EFFECTS (decl)
6399 || ! TREE_CONSTANT (decl))
6400 ret = SECCAT_DATA;
6401 else
6402 ret = SECCAT_RODATA;
6404 else
6405 ret = SECCAT_RODATA;
6407 /* There are no read-only thread-local sections. */
6408 if (TREE_CODE (decl) == VAR_DECL && DECL_TLS_MODEL (decl))
6410 if (DECL_TLS_MODEL (decl) == TLS_MODEL_EMULATED)
6412 if (DECL_EMUTLS_VAR_P (decl))
6414 if (targetm.emutls.var_section)
6415 ret = SECCAT_EMUTLS_VAR;
6417 else
6419 if (targetm.emutls.tmpl_prefix)
6420 ret = SECCAT_EMUTLS_TMPL;
6423 /* Note that this would be *just* SECCAT_BSS, except that there's
6424 no concept of a read-only thread-local-data section. */
6425 else if (ret == SECCAT_BSS
6426 || (flag_zero_initialized_in_bss
6427 && initializer_zerop (DECL_INITIAL (decl))))
6428 ret = SECCAT_TBSS;
6429 else
6430 ret = SECCAT_TDATA;
6433 /* If the target uses small data sections, select it. */
6434 else if (targetm.in_small_data_p (decl))
6436 if (ret == SECCAT_BSS)
6437 ret = SECCAT_SBSS;
6438 else if (targetm.have_srodata_section && ret == SECCAT_RODATA)
6439 ret = SECCAT_SRODATA;
6440 else
6441 ret = SECCAT_SDATA;
6444 return ret;
6447 bool
6448 decl_readonly_section (const_tree decl, int reloc)
6450 switch (categorize_decl_for_section (decl, reloc))
6452 case SECCAT_RODATA:
6453 case SECCAT_RODATA_MERGE_STR:
6454 case SECCAT_RODATA_MERGE_STR_INIT:
6455 case SECCAT_RODATA_MERGE_CONST:
6456 case SECCAT_SRODATA:
6457 return true;
6458 break;
6459 default:
6460 return false;
6461 break;
6465 /* Select a section based on the above categorization. */
6467 section *
6468 default_elf_select_section (tree decl, int reloc,
6469 unsigned HOST_WIDE_INT align)
6471 const char *sname;
6472 switch (categorize_decl_for_section (decl, reloc))
6474 case SECCAT_TEXT:
6475 /* We're not supposed to be called on FUNCTION_DECLs. */
6476 gcc_unreachable ();
6477 case SECCAT_RODATA:
6478 return readonly_data_section;
6479 case SECCAT_RODATA_MERGE_STR:
6480 return mergeable_string_section (decl, align, 0);
6481 case SECCAT_RODATA_MERGE_STR_INIT:
6482 return mergeable_string_section (DECL_INITIAL (decl), align, 0);
6483 case SECCAT_RODATA_MERGE_CONST:
6484 return mergeable_constant_section (DECL_MODE (decl), align, 0);
6485 case SECCAT_SRODATA:
6486 sname = ".sdata2";
6487 break;
6488 case SECCAT_DATA:
6489 return data_section;
6490 case SECCAT_DATA_REL:
6491 sname = ".data.rel";
6492 break;
6493 case SECCAT_DATA_REL_LOCAL:
6494 sname = ".data.rel.local";
6495 break;
6496 case SECCAT_DATA_REL_RO:
6497 sname = ".data.rel.ro";
6498 break;
6499 case SECCAT_DATA_REL_RO_LOCAL:
6500 sname = ".data.rel.ro.local";
6501 break;
6502 case SECCAT_SDATA:
6503 sname = ".sdata";
6504 break;
6505 case SECCAT_TDATA:
6506 sname = ".tdata";
6507 break;
6508 case SECCAT_BSS:
6509 if (bss_section)
6510 return bss_section;
6511 sname = ".bss";
6512 break;
6513 case SECCAT_SBSS:
6514 sname = ".sbss";
6515 break;
6516 case SECCAT_TBSS:
6517 sname = ".tbss";
6518 break;
6519 case SECCAT_EMUTLS_VAR:
6520 sname = targetm.emutls.var_section;
6521 break;
6522 case SECCAT_EMUTLS_TMPL:
6523 sname = targetm.emutls.tmpl_section;
6524 break;
6525 default:
6526 gcc_unreachable ();
6529 if (!DECL_P (decl))
6530 decl = NULL_TREE;
6531 return get_named_section (decl, sname, reloc);
6534 /* Construct a unique section name based on the decl name and the
6535 categorization performed above. */
6537 void
6538 default_unique_section (tree decl, int reloc)
6540 /* We only need to use .gnu.linkonce if we don't have COMDAT groups. */
6541 bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
6542 const char *prefix, *name, *linkonce;
6543 char *string;
6545 switch (categorize_decl_for_section (decl, reloc))
6547 case SECCAT_TEXT:
6548 prefix = one_only ? ".t" : ".text";
6549 break;
6550 case SECCAT_RODATA:
6551 case SECCAT_RODATA_MERGE_STR:
6552 case SECCAT_RODATA_MERGE_STR_INIT:
6553 case SECCAT_RODATA_MERGE_CONST:
6554 prefix = one_only ? ".r" : ".rodata";
6555 break;
6556 case SECCAT_SRODATA:
6557 prefix = one_only ? ".s2" : ".sdata2";
6558 break;
6559 case SECCAT_DATA:
6560 prefix = one_only ? ".d" : ".data";
6561 break;
6562 case SECCAT_DATA_REL:
6563 prefix = one_only ? ".d.rel" : ".data.rel";
6564 break;
6565 case SECCAT_DATA_REL_LOCAL:
6566 prefix = one_only ? ".d.rel.local" : ".data.rel.local";
6567 break;
6568 case SECCAT_DATA_REL_RO:
6569 prefix = one_only ? ".d.rel.ro" : ".data.rel.ro";
6570 break;
6571 case SECCAT_DATA_REL_RO_LOCAL:
6572 prefix = one_only ? ".d.rel.ro.local" : ".data.rel.ro.local";
6573 break;
6574 case SECCAT_SDATA:
6575 prefix = one_only ? ".s" : ".sdata";
6576 break;
6577 case SECCAT_BSS:
6578 prefix = one_only ? ".b" : ".bss";
6579 break;
6580 case SECCAT_SBSS:
6581 prefix = one_only ? ".sb" : ".sbss";
6582 break;
6583 case SECCAT_TDATA:
6584 prefix = one_only ? ".td" : ".tdata";
6585 break;
6586 case SECCAT_TBSS:
6587 prefix = one_only ? ".tb" : ".tbss";
6588 break;
6589 case SECCAT_EMUTLS_VAR:
6590 prefix = targetm.emutls.var_section;
6591 break;
6592 case SECCAT_EMUTLS_TMPL:
6593 prefix = targetm.emutls.tmpl_section;
6594 break;
6595 default:
6596 gcc_unreachable ();
6599 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
6600 name = targetm.strip_name_encoding (name);
6602 /* If we're using one_only, then there needs to be a .gnu.linkonce
6603 prefix to the section name. */
6604 linkonce = one_only ? ".gnu.linkonce" : "";
6606 string = ACONCAT ((linkonce, prefix, ".", name, NULL));
6608 DECL_SECTION_NAME (decl) = build_string (strlen (string), string);
6611 /* Like compute_reloc_for_constant, except for an RTX. The return value
6612 is a mask for which bit 1 indicates a global relocation, and bit 0
6613 indicates a local relocation. */
6615 static int
6616 compute_reloc_for_rtx_1 (rtx *xp, void *data)
6618 int *preloc = (int *) data;
6619 rtx x = *xp;
6621 switch (GET_CODE (x))
6623 case SYMBOL_REF:
6624 *preloc |= SYMBOL_REF_LOCAL_P (x) ? 1 : 2;
6625 break;
6626 case LABEL_REF:
6627 *preloc |= 1;
6628 break;
6629 default:
6630 break;
6633 return 0;
6636 static int
6637 compute_reloc_for_rtx (rtx x)
6639 int reloc;
6641 switch (GET_CODE (x))
6643 case CONST:
6644 case SYMBOL_REF:
6645 case LABEL_REF:
6646 reloc = 0;
6647 for_each_rtx (&x, compute_reloc_for_rtx_1, &reloc);
6648 return reloc;
6650 default:
6651 return 0;
6655 section *
6656 default_select_rtx_section (enum machine_mode mode ATTRIBUTE_UNUSED,
6657 rtx x,
6658 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
6660 if (compute_reloc_for_rtx (x) & targetm.asm_out.reloc_rw_mask ())
6661 return data_section;
6662 else
6663 return readonly_data_section;
6666 section *
6667 default_elf_select_rtx_section (enum machine_mode mode, rtx x,
6668 unsigned HOST_WIDE_INT align)
6670 int reloc = compute_reloc_for_rtx (x);
6672 /* ??? Handle small data here somehow. */
6674 if (reloc & targetm.asm_out.reloc_rw_mask ())
6676 if (reloc == 1)
6677 return get_named_section (NULL, ".data.rel.ro.local", 1);
6678 else
6679 return get_named_section (NULL, ".data.rel.ro", 3);
6682 return mergeable_constant_section (mode, align, 0);
6685 /* Set the generally applicable flags on the SYMBOL_REF for EXP. */
6687 void
6688 default_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
6690 rtx symbol;
6691 int flags;
6693 /* Careful not to prod global register variables. */
6694 if (!MEM_P (rtl))
6695 return;
6696 symbol = XEXP (rtl, 0);
6697 if (GET_CODE (symbol) != SYMBOL_REF)
6698 return;
6700 flags = SYMBOL_REF_FLAGS (symbol) & SYMBOL_FLAG_HAS_BLOCK_INFO;
6701 if (TREE_CODE (decl) == FUNCTION_DECL)
6702 flags |= SYMBOL_FLAG_FUNCTION;
6703 if (targetm.binds_local_p (decl))
6704 flags |= SYMBOL_FLAG_LOCAL;
6705 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl)
6706 && DECL_TLS_MODEL (decl) != TLS_MODEL_EMULATED)
6707 flags |= DECL_TLS_MODEL (decl) << SYMBOL_FLAG_TLS_SHIFT;
6708 else if (targetm.in_small_data_p (decl))
6709 flags |= SYMBOL_FLAG_SMALL;
6710 /* ??? Why is DECL_EXTERNAL ever set for non-PUBLIC names? Without
6711 being PUBLIC, the thing *must* be defined in this translation unit.
6712 Prevent this buglet from being propagated into rtl code as well. */
6713 if (DECL_P (decl) && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl))
6714 flags |= SYMBOL_FLAG_EXTERNAL;
6716 SYMBOL_REF_FLAGS (symbol) = flags;
6719 /* By default, we do nothing for encode_section_info, so we need not
6720 do anything but discard the '*' marker. */
6722 const char *
6723 default_strip_name_encoding (const char *str)
6725 return str + (*str == '*');
6728 #ifdef ASM_OUTPUT_DEF
6729 /* The default implementation of TARGET_ASM_OUTPUT_ANCHOR. Define the
6730 anchor relative to ".", the current section position. */
6732 void
6733 default_asm_output_anchor (rtx symbol)
6735 char buffer[100];
6737 sprintf (buffer, "*. + " HOST_WIDE_INT_PRINT_DEC,
6738 SYMBOL_REF_BLOCK_OFFSET (symbol));
6739 ASM_OUTPUT_DEF (asm_out_file, XSTR (symbol, 0), buffer);
6741 #endif
6743 /* The default implementation of TARGET_USE_ANCHORS_FOR_SYMBOL_P. */
6745 bool
6746 default_use_anchors_for_symbol_p (const_rtx symbol)
6748 section *sect;
6749 tree decl;
6751 /* Don't use anchors for mergeable sections. The linker might move
6752 the objects around. */
6753 sect = SYMBOL_REF_BLOCK (symbol)->sect;
6754 if (sect->common.flags & SECTION_MERGE)
6755 return false;
6757 /* Don't use anchors for small data sections. The small data register
6758 acts as an anchor for such sections. */
6759 if (sect->common.flags & SECTION_SMALL)
6760 return false;
6762 decl = SYMBOL_REF_DECL (symbol);
6763 if (decl && DECL_P (decl))
6765 /* Don't use section anchors for decls that might be defined by
6766 other modules. */
6767 if (!targetm.binds_local_p (decl))
6768 return false;
6770 /* Don't use section anchors for decls that will be placed in a
6771 small data section. */
6772 /* ??? Ideally, this check would be redundant with the SECTION_SMALL
6773 one above. The problem is that we only use SECTION_SMALL for
6774 sections that should be marked as small in the section directive. */
6775 if (targetm.in_small_data_p (decl))
6776 return false;
6778 return true;
6781 /* Assume ELF-ish defaults, since that's pretty much the most liberal
6782 wrt cross-module name binding. */
6784 bool
6785 default_binds_local_p (const_tree exp)
6787 return default_binds_local_p_1 (exp, flag_shlib);
6790 bool
6791 default_binds_local_p_1 (const_tree exp, int shlib)
6793 bool local_p;
6795 /* A non-decl is an entry in the constant pool. */
6796 if (!DECL_P (exp))
6797 local_p = true;
6798 /* Weakrefs may not bind locally, even though the weakref itself is
6799 always static and therefore local. */
6800 else if (lookup_attribute ("weakref", DECL_ATTRIBUTES (exp)))
6801 local_p = false;
6802 /* Static variables are always local. */
6803 else if (! TREE_PUBLIC (exp))
6804 local_p = true;
6805 /* A variable is local if the user has said explicitly that it will
6806 be. */
6807 else if (DECL_VISIBILITY_SPECIFIED (exp)
6808 && DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
6809 local_p = true;
6810 /* Variables defined outside this object might not be local. */
6811 else if (DECL_EXTERNAL (exp))
6812 local_p = false;
6813 /* If defined in this object and visibility is not default, must be
6814 local. */
6815 else if (DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
6816 local_p = true;
6817 /* Default visibility weak data can be overridden by a strong symbol
6818 in another module and so are not local. */
6819 else if (DECL_WEAK (exp))
6820 local_p = false;
6821 /* If PIC, then assume that any global name can be overridden by
6822 symbols resolved from other modules, unless we are compiling with
6823 -fwhole-program, which assumes that names are local. */
6824 else if (shlib)
6825 local_p = flag_whole_program;
6826 /* Uninitialized COMMON variable may be unified with symbols
6827 resolved from other modules. */
6828 else if (DECL_COMMON (exp)
6829 && (DECL_INITIAL (exp) == NULL
6830 || DECL_INITIAL (exp) == error_mark_node))
6831 local_p = false;
6832 /* Otherwise we're left with initialized (or non-common) global data
6833 which is of necessity defined locally. */
6834 else
6835 local_p = true;
6837 return local_p;
6840 /* Default function to output code that will globalize a label. A
6841 target must define GLOBAL_ASM_OP or provide its own function to
6842 globalize a label. */
6843 #ifdef GLOBAL_ASM_OP
6844 void
6845 default_globalize_label (FILE * stream, const char *name)
6847 fputs (GLOBAL_ASM_OP, stream);
6848 assemble_name (stream, name);
6849 putc ('\n', stream);
6851 #endif /* GLOBAL_ASM_OP */
6853 /* Default function to output code that will globalize a declaration. */
6854 void
6855 default_globalize_decl_name (FILE * stream, tree decl)
6857 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
6858 targetm.asm_out.globalize_label (stream, name);
6861 /* Default function to output a label for unwind information. The
6862 default is to do nothing. A target that needs nonlocal labels for
6863 unwind information must provide its own function to do this. */
6864 void
6865 default_emit_unwind_label (FILE * stream ATTRIBUTE_UNUSED,
6866 tree decl ATTRIBUTE_UNUSED,
6867 int for_eh ATTRIBUTE_UNUSED,
6868 int empty ATTRIBUTE_UNUSED)
6872 /* Default function to output a label to divide up the exception table.
6873 The default is to do nothing. A target that needs/wants to divide
6874 up the table must provide it's own function to do this. */
6875 void
6876 default_emit_except_table_label (FILE * stream ATTRIBUTE_UNUSED)
6880 /* This is how to output an internal numbered label where PREFIX is
6881 the class of label and LABELNO is the number within the class. */
6883 void
6884 default_internal_label (FILE *stream, const char *prefix,
6885 unsigned long labelno)
6887 char *const buf = (char *) alloca (40 + strlen (prefix));
6888 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
6889 ASM_OUTPUT_INTERNAL_LABEL (stream, buf);
6893 /* The default implementation of ASM_DECLARE_CONSTANT_NAME. */
6895 void
6896 default_asm_declare_constant_name (FILE *file, const char *name,
6897 const_tree exp ATTRIBUTE_UNUSED,
6898 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
6900 assemble_label (file, name);
6903 /* This is the default behavior at the beginning of a file. It's
6904 controlled by two other target-hook toggles. */
6905 void
6906 default_file_start (void)
6908 if (targetm.asm_file_start_app_off
6909 && !(flag_verbose_asm || flag_debug_asm || flag_dump_rtl_in_asm))
6910 fputs (ASM_APP_OFF, asm_out_file);
6912 if (targetm.asm_file_start_file_directive)
6913 output_file_directive (asm_out_file, main_input_filename);
6916 /* This is a generic routine suitable for use as TARGET_ASM_FILE_END
6917 which emits a special section directive used to indicate whether or
6918 not this object file needs an executable stack. This is primarily
6919 a GNU extension to ELF but could be used on other targets. */
6921 int trampolines_created;
6923 void
6924 file_end_indicate_exec_stack (void)
6926 unsigned int flags = SECTION_DEBUG;
6927 if (trampolines_created)
6928 flags |= SECTION_CODE;
6930 switch_to_section (get_section (".note.GNU-stack", flags, NULL));
6933 /* Output DIRECTIVE (a C string) followed by a newline. This is used as
6934 a get_unnamed_section callback. */
6936 void
6937 output_section_asm_op (const void *directive)
6939 fprintf (asm_out_file, "%s\n", (const char *) directive);
6942 /* Emit assembly code to switch to section NEW_SECTION. Do nothing if
6943 the current section is NEW_SECTION. */
6945 void
6946 switch_to_section (section *new_section)
6948 if (in_section == new_section)
6949 return;
6951 if (new_section->common.flags & SECTION_FORGET)
6952 in_section = NULL;
6953 else
6954 in_section = new_section;
6956 switch (SECTION_STYLE (new_section))
6958 case SECTION_NAMED:
6959 if (cfun
6960 && !crtl->subsections.unlikely_text_section_name
6961 && strcmp (new_section->named.name,
6962 UNLIKELY_EXECUTED_TEXT_SECTION_NAME) == 0)
6963 crtl->subsections.unlikely_text_section_name = UNLIKELY_EXECUTED_TEXT_SECTION_NAME;
6965 targetm.asm_out.named_section (new_section->named.name,
6966 new_section->named.common.flags,
6967 new_section->named.decl);
6968 break;
6970 case SECTION_UNNAMED:
6971 new_section->unnamed.callback (new_section->unnamed.data);
6972 break;
6974 case SECTION_NOSWITCH:
6975 gcc_unreachable ();
6976 break;
6979 new_section->common.flags |= SECTION_DECLARED;
6982 /* If block symbol SYMBOL has not yet been assigned an offset, place
6983 it at the end of its block. */
6985 void
6986 place_block_symbol (rtx symbol)
6988 unsigned HOST_WIDE_INT size, mask, offset;
6989 struct constant_descriptor_rtx *desc;
6990 unsigned int alignment;
6991 struct object_block *block;
6992 tree decl;
6994 gcc_assert (SYMBOL_REF_BLOCK (symbol));
6995 if (SYMBOL_REF_BLOCK_OFFSET (symbol) >= 0)
6996 return;
6998 /* Work out the symbol's size and alignment. */
6999 if (CONSTANT_POOL_ADDRESS_P (symbol))
7001 desc = SYMBOL_REF_CONSTANT (symbol);
7002 alignment = desc->align;
7003 size = GET_MODE_SIZE (desc->mode);
7005 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
7007 decl = SYMBOL_REF_DECL (symbol);
7008 alignment = DECL_ALIGN (decl);
7009 size = get_constant_size (DECL_INITIAL (decl));
7011 else
7013 decl = SYMBOL_REF_DECL (symbol);
7014 alignment = DECL_ALIGN (decl);
7015 size = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
7018 /* Calculate the object's offset from the start of the block. */
7019 block = SYMBOL_REF_BLOCK (symbol);
7020 mask = alignment / BITS_PER_UNIT - 1;
7021 offset = (block->size + mask) & ~mask;
7022 SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
7024 /* Record the block's new alignment and size. */
7025 block->alignment = MAX (block->alignment, alignment);
7026 block->size = offset + size;
7028 VEC_safe_push (rtx, gc, block->objects, symbol);
7031 /* Return the anchor that should be used to address byte offset OFFSET
7032 from the first object in BLOCK. MODEL is the TLS model used
7033 to access it. */
7036 get_section_anchor (struct object_block *block, HOST_WIDE_INT offset,
7037 enum tls_model model)
7039 char label[100];
7040 unsigned int begin, middle, end;
7041 unsigned HOST_WIDE_INT min_offset, max_offset, range, bias, delta;
7042 rtx anchor;
7044 /* Work out the anchor's offset. Use an offset of 0 for the first
7045 anchor so that we don't pessimize the case where we take the address
7046 of a variable at the beginning of the block. This is particularly
7047 useful when a block has only one variable assigned to it.
7049 We try to place anchors RANGE bytes apart, so there can then be
7050 anchors at +/-RANGE, +/-2 * RANGE, and so on, up to the limits of
7051 a ptr_mode offset. With some target settings, the lowest such
7052 anchor might be out of range for the lowest ptr_mode offset;
7053 likewise the highest anchor for the highest offset. Use anchors
7054 at the extreme ends of the ptr_mode range in such cases.
7056 All arithmetic uses unsigned integers in order to avoid
7057 signed overflow. */
7058 max_offset = (unsigned HOST_WIDE_INT) targetm.max_anchor_offset;
7059 min_offset = (unsigned HOST_WIDE_INT) targetm.min_anchor_offset;
7060 range = max_offset - min_offset + 1;
7061 if (range == 0)
7062 offset = 0;
7063 else
7065 bias = 1 << (GET_MODE_BITSIZE (ptr_mode) - 1);
7066 if (offset < 0)
7068 delta = -(unsigned HOST_WIDE_INT) offset + max_offset;
7069 delta -= delta % range;
7070 if (delta > bias)
7071 delta = bias;
7072 offset = (HOST_WIDE_INT) (-delta);
7074 else
7076 delta = (unsigned HOST_WIDE_INT) offset - min_offset;
7077 delta -= delta % range;
7078 if (delta > bias - 1)
7079 delta = bias - 1;
7080 offset = (HOST_WIDE_INT) delta;
7084 /* Do a binary search to see if there's already an anchor we can use.
7085 Set BEGIN to the new anchor's index if not. */
7086 begin = 0;
7087 end = VEC_length (rtx, block->anchors);
7088 while (begin != end)
7090 middle = (end + begin) / 2;
7091 anchor = VEC_index (rtx, block->anchors, middle);
7092 if (SYMBOL_REF_BLOCK_OFFSET (anchor) > offset)
7093 end = middle;
7094 else if (SYMBOL_REF_BLOCK_OFFSET (anchor) < offset)
7095 begin = middle + 1;
7096 else if (SYMBOL_REF_TLS_MODEL (anchor) > model)
7097 end = middle;
7098 else if (SYMBOL_REF_TLS_MODEL (anchor) < model)
7099 begin = middle + 1;
7100 else
7101 return anchor;
7104 /* Create a new anchor with a unique label. */
7105 ASM_GENERATE_INTERNAL_LABEL (label, "LANCHOR", anchor_labelno++);
7106 anchor = create_block_symbol (ggc_strdup (label), block, offset);
7107 SYMBOL_REF_FLAGS (anchor) |= SYMBOL_FLAG_LOCAL | SYMBOL_FLAG_ANCHOR;
7108 SYMBOL_REF_FLAGS (anchor) |= model << SYMBOL_FLAG_TLS_SHIFT;
7110 /* Insert it at index BEGIN. */
7111 VEC_safe_insert (rtx, gc, block->anchors, begin, anchor);
7112 return anchor;
7115 /* Output the objects in BLOCK. */
7117 static void
7118 output_object_block (struct object_block *block)
7120 struct constant_descriptor_rtx *desc;
7121 unsigned int i;
7122 HOST_WIDE_INT offset;
7123 tree decl;
7124 rtx symbol;
7126 if (block->objects == NULL)
7127 return;
7129 /* Switch to the section and make sure that the first byte is
7130 suitably aligned. */
7131 switch_to_section (block->sect);
7132 assemble_align (block->alignment);
7134 /* Define the values of all anchors relative to the current section
7135 position. */
7136 for (i = 0; VEC_iterate (rtx, block->anchors, i, symbol); i++)
7137 targetm.asm_out.output_anchor (symbol);
7139 /* Output the objects themselves. */
7140 offset = 0;
7141 for (i = 0; VEC_iterate (rtx, block->objects, i, symbol); i++)
7143 /* Move to the object's offset, padding with zeros if necessary. */
7144 assemble_zeros (SYMBOL_REF_BLOCK_OFFSET (symbol) - offset);
7145 offset = SYMBOL_REF_BLOCK_OFFSET (symbol);
7146 if (CONSTANT_POOL_ADDRESS_P (symbol))
7148 desc = SYMBOL_REF_CONSTANT (symbol);
7149 output_constant_pool_1 (desc, 1);
7150 offset += GET_MODE_SIZE (desc->mode);
7152 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
7154 decl = SYMBOL_REF_DECL (symbol);
7155 assemble_constant_contents (DECL_INITIAL (decl), XSTR (symbol, 0),
7156 DECL_ALIGN (decl));
7157 offset += get_constant_size (DECL_INITIAL (decl));
7159 else
7161 decl = SYMBOL_REF_DECL (symbol);
7162 assemble_variable_contents (decl, XSTR (symbol, 0), false);
7163 offset += tree_low_cst (DECL_SIZE_UNIT (decl), 1);
7168 /* A htab_traverse callback used to call output_object_block for
7169 each member of object_block_htab. */
7171 static int
7172 output_object_block_htab (void **slot, void *data ATTRIBUTE_UNUSED)
7174 output_object_block ((struct object_block *) (*slot));
7175 return 1;
7178 /* Output the definitions of all object_blocks. */
7180 void
7181 output_object_blocks (void)
7183 htab_traverse (object_block_htab, output_object_block_htab, NULL);
7186 /* This function provides a possible implementation of the
7187 TARGET_ASM_RECORD_GCC_SWITCHES target hook for ELF targets. When triggered
7188 by -frecord-gcc-switches it creates a new mergeable, string section in the
7189 assembler output file called TARGET_ASM_RECORD_GCC_SWITCHES_SECTION which
7190 contains the switches in ASCII format.
7192 FIXME: This code does not correctly handle double quote characters
7193 that appear inside strings, (it strips them rather than preserving them).
7194 FIXME: ASM_OUTPUT_ASCII, as defined in config/elfos.h will not emit NUL
7195 characters - instead it treats them as sub-string separators. Since
7196 we want to emit NUL strings terminators into the object file we have to use
7197 ASM_OUTPUT_SKIP. */
7200 elf_record_gcc_switches (print_switch_type type, const char * name)
7202 switch (type)
7204 case SWITCH_TYPE_PASSED:
7205 ASM_OUTPUT_ASCII (asm_out_file, name, strlen (name));
7206 ASM_OUTPUT_SKIP (asm_out_file, (unsigned HOST_WIDE_INT) 1);
7207 break;
7209 case SWITCH_TYPE_DESCRIPTIVE:
7210 if (name == NULL)
7212 /* Distinguish between invocations where name is NULL. */
7213 static bool started = false;
7215 if (!started)
7217 section * sec;
7219 sec = get_section (targetm.asm_out.record_gcc_switches_section,
7220 SECTION_DEBUG
7221 | SECTION_MERGE
7222 | SECTION_STRINGS
7223 | (SECTION_ENTSIZE & 1),
7224 NULL);
7225 switch_to_section (sec);
7226 started = true;
7230 default:
7231 break;
7234 /* The return value is currently ignored by the caller, but must be 0.
7235 For -fverbose-asm the return value would be the number of characters
7236 emitted into the assembler file. */
7237 return 0;
7240 /* Emit text to declare externally defined symbols. It is needed to
7241 properly support non-default visibility. */
7242 void
7243 default_elf_asm_output_external (FILE *file ATTRIBUTE_UNUSED,
7244 tree decl,
7245 const char *name ATTRIBUTE_UNUSED)
7247 /* We output the name if and only if TREE_SYMBOL_REFERENCED is
7248 set in order to avoid putting out names that are never really
7249 used. */
7250 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
7251 && targetm.binds_local_p (decl))
7252 maybe_assemble_visibility (decl);
7255 /* Create a DEBUG_EXPR_DECL / DEBUG_EXPR pair from RTL expression
7256 EXP. */
7258 make_debug_expr_from_rtl (const_rtx exp)
7260 tree ddecl = make_node (DEBUG_EXPR_DECL), type;
7261 enum machine_mode mode = GET_MODE (exp);
7262 rtx dval;
7264 DECL_ARTIFICIAL (ddecl) = 1;
7265 if (REG_P (exp) && REG_EXPR (exp))
7266 type = TREE_TYPE (REG_EXPR (exp));
7267 else if (MEM_P (exp) && MEM_EXPR (exp))
7268 type = TREE_TYPE (MEM_EXPR (exp));
7269 else
7270 type = NULL_TREE;
7271 if (type && TYPE_MODE (type) == mode)
7272 TREE_TYPE (ddecl) = type;
7273 else
7274 TREE_TYPE (ddecl) = lang_hooks.types.type_for_mode (mode, 1);
7275 DECL_MODE (ddecl) = mode;
7276 dval = gen_rtx_DEBUG_EXPR (mode);
7277 DEBUG_EXPR_TREE_DECL (dval) = ddecl;
7278 SET_DECL_RTL (ddecl, dval);
7279 return dval;
7282 #include "gt-varasm.h"