OSDN Git Service

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