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