OSDN Git Service

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