OSDN Git Service

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