OSDN Git Service

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