OSDN Git Service

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