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