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