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