OSDN Git Service

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