OSDN Git Service

PR c++/44703
[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 (FILE *file, const char *name)
2465 {
2466   ASM_OUTPUT_LABEL (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   targetm.asm_out.declare_constant_name (asm_out_file, label, exp, size);
3479
3480   /* Output the value of EXP.  */
3481   output_constant (exp, size, align);
3482 }
3483
3484 /* We must output the constant data referred to by SYMBOL; do so.  */
3485
3486 static void
3487 output_constant_def_contents (rtx symbol)
3488 {
3489   tree decl = SYMBOL_REF_DECL (symbol);
3490   tree exp = DECL_INITIAL (decl);
3491   unsigned int align;
3492
3493   /* Make sure any other constants whose addresses appear in EXP
3494      are assigned label numbers.  */
3495   output_addressed_constants (exp);
3496
3497   /* We are no longer deferring this constant.  */
3498   TREE_ASM_WRITTEN (exp) = 1;
3499
3500   /* If the constant is part of an object block, make sure that the
3501      decl has been positioned within its block, but do not write out
3502      its definition yet.  output_object_blocks will do that later.  */
3503   if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
3504     place_block_symbol (symbol);
3505   else
3506     {
3507       align = DECL_ALIGN (decl);
3508       switch_to_section (get_constant_section (exp, align));
3509       if (align > BITS_PER_UNIT)
3510         ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
3511       assemble_constant_contents (exp, XSTR (symbol, 0), align);
3512     }
3513   if (flag_mudflap)
3514     mudflap_enqueue_constant (exp);
3515 }
3516
3517 /* Look up EXP in the table of constant descriptors.  Return the rtl
3518    if it has been emitted, else null.  */
3519
3520 rtx
3521 lookup_constant_def (tree exp)
3522 {
3523   struct constant_descriptor_tree *desc;
3524   struct constant_descriptor_tree key;
3525
3526   key.value = exp;
3527   key.hash = const_hash_1 (exp);
3528   desc = (struct constant_descriptor_tree *)
3529     htab_find_with_hash (const_desc_htab, &key, key.hash);
3530
3531   return (desc ? desc->rtl : NULL_RTX);
3532 }
3533
3534 /* Return a tree representing a reference to constant data in memory
3535    for the constant expression EXP.
3536
3537    This is the counterpart of output_constant_def at the Tree level.  */
3538
3539 tree
3540 tree_output_constant_def (tree exp)
3541 {
3542   struct constant_descriptor_tree *desc, key;
3543   void **loc;
3544   tree decl;
3545
3546   /* Look up EXP in the table of constant descriptors.  If we didn't find
3547      it, create a new one.  */
3548   key.value = exp;
3549   key.hash = const_hash_1 (exp);
3550   loc = htab_find_slot_with_hash (const_desc_htab, &key, key.hash, INSERT);
3551
3552   desc = (struct constant_descriptor_tree *) *loc;
3553   if (desc == 0)
3554     {
3555       desc = build_constant_desc (exp);
3556       desc->hash = key.hash;
3557       *loc = desc;
3558     }
3559
3560   decl = SYMBOL_REF_DECL (XEXP (desc->rtl, 0));
3561   varpool_finalize_decl (decl);
3562   return decl;
3563 }
3564 \f
3565 /* Used in the hash tables to avoid outputting the same constant
3566    twice.  Unlike 'struct constant_descriptor_tree', RTX constants
3567    are output once per function, not once per file.  */
3568 /* ??? Only a few targets need per-function constant pools.  Most
3569    can use one per-file pool.  Should add a targetm bit to tell the
3570    difference.  */
3571
3572 struct GTY(()) rtx_constant_pool {
3573   /* Pointers to first and last constant in pool, as ordered by offset.  */
3574   struct constant_descriptor_rtx *first;
3575   struct constant_descriptor_rtx *last;
3576
3577   /* Hash facility for making memory-constants from constant rtl-expressions.
3578      It is used on RISC machines where immediate integer arguments and
3579      constant addresses are restricted so that such constants must be stored
3580      in memory.  */
3581   htab_t GTY((param_is (struct constant_descriptor_rtx))) const_rtx_htab;
3582
3583   /* Current offset in constant pool (does not include any
3584      machine-specific header).  */
3585   HOST_WIDE_INT offset;
3586 };
3587
3588 struct GTY((chain_next ("%h.next"))) constant_descriptor_rtx {
3589   struct constant_descriptor_rtx *next;
3590   rtx mem;
3591   rtx sym;
3592   rtx constant;
3593   HOST_WIDE_INT offset;
3594   hashval_t hash;
3595   enum machine_mode mode;
3596   unsigned int align;
3597   int labelno;
3598   int mark;
3599 };
3600
3601 /* Hash and compare functions for const_rtx_htab.  */
3602
3603 static hashval_t
3604 const_desc_rtx_hash (const void *ptr)
3605 {
3606   const struct constant_descriptor_rtx *const desc
3607     = (const struct constant_descriptor_rtx *) ptr;
3608   return desc->hash;
3609 }
3610
3611 static int
3612 const_desc_rtx_eq (const void *a, const void *b)
3613 {
3614   const struct constant_descriptor_rtx *const x
3615     = (const struct constant_descriptor_rtx *) a;
3616   const struct constant_descriptor_rtx *const y
3617     = (const struct constant_descriptor_rtx *) b;
3618
3619   if (x->mode != y->mode)
3620     return 0;
3621   return rtx_equal_p (x->constant, y->constant);
3622 }
3623
3624 /* This is the worker function for const_rtx_hash, called via for_each_rtx.  */
3625
3626 static int
3627 const_rtx_hash_1 (rtx *xp, void *data)
3628 {
3629   unsigned HOST_WIDE_INT hwi;
3630   enum machine_mode mode;
3631   enum rtx_code code;
3632   hashval_t h, *hp;
3633   rtx x;
3634
3635   x = *xp;
3636   code = GET_CODE (x);
3637   mode = GET_MODE (x);
3638   h = (hashval_t) code * 1048573 + mode;
3639
3640   switch (code)
3641     {
3642     case CONST_INT:
3643       hwi = INTVAL (x);
3644     fold_hwi:
3645       {
3646         int shift = sizeof (hashval_t) * CHAR_BIT;
3647         const int n = sizeof (HOST_WIDE_INT) / sizeof (hashval_t);
3648         int i;
3649
3650         h ^= (hashval_t) hwi;
3651         for (i = 1; i < n; ++i)
3652           {
3653             hwi >>= shift;
3654             h ^= (hashval_t) hwi;
3655           }
3656       }
3657       break;
3658
3659     case CONST_DOUBLE:
3660       if (mode == VOIDmode)
3661         {
3662           hwi = CONST_DOUBLE_LOW (x) ^ CONST_DOUBLE_HIGH (x);
3663           goto fold_hwi;
3664         }
3665       else
3666         h ^= real_hash (CONST_DOUBLE_REAL_VALUE (x));
3667       break;
3668
3669     case CONST_FIXED:
3670       h ^= fixed_hash (CONST_FIXED_VALUE (x));
3671       break;
3672
3673     case CONST_VECTOR:
3674       {
3675         int i;
3676         for (i = XVECLEN (x, 0); i-- > 0; )
3677           h = h * 251 + const_rtx_hash_1 (&XVECEXP (x, 0, i), data);
3678       }
3679       break;
3680
3681     case SYMBOL_REF:
3682       h ^= htab_hash_string (XSTR (x, 0));
3683       break;
3684
3685     case LABEL_REF:
3686       h = h * 251 + CODE_LABEL_NUMBER (XEXP (x, 0));
3687       break;
3688
3689     case UNSPEC:
3690     case UNSPEC_VOLATILE:
3691       h = h * 251 + XINT (x, 1);
3692       break;
3693
3694     default:
3695       break;
3696     }
3697
3698   hp = (hashval_t *) data;
3699   *hp = *hp * 509 + h;
3700   return 0;
3701 }
3702
3703 /* Compute a hash value for X, which should be a constant.  */
3704
3705 static hashval_t
3706 const_rtx_hash (rtx x)
3707 {
3708   hashval_t h = 0;
3709   for_each_rtx (&x, const_rtx_hash_1, &h);
3710   return h;
3711 }
3712
3713 \f
3714 /* Create and return a new rtx constant pool.  */
3715
3716 static struct rtx_constant_pool *
3717 create_constant_pool (void)
3718 {
3719   struct rtx_constant_pool *pool;
3720
3721   pool = ggc_alloc_rtx_constant_pool ();
3722   pool->const_rtx_htab = htab_create_ggc (31, const_desc_rtx_hash,
3723                                           const_desc_rtx_eq, NULL);
3724   pool->first = NULL;
3725   pool->last = NULL;
3726   pool->offset = 0;
3727   return pool;
3728 }
3729
3730 /* Initialize constant pool hashing for a new function.  */
3731
3732 void
3733 init_varasm_status (void)
3734 {
3735   crtl->varasm.pool = create_constant_pool ();
3736   crtl->varasm.deferred_constants = 0;
3737 }
3738 \f
3739 /* Given a MINUS expression, simplify it if both sides
3740    include the same symbol.  */
3741
3742 rtx
3743 simplify_subtraction (rtx x)
3744 {
3745   rtx r = simplify_rtx (x);
3746   return r ? r : x;
3747 }
3748 \f
3749 /* Given a constant rtx X, make (or find) a memory constant for its value
3750    and return a MEM rtx to refer to it in memory.  */
3751
3752 rtx
3753 force_const_mem (enum machine_mode mode, rtx x)
3754 {
3755   struct constant_descriptor_rtx *desc, tmp;
3756   struct rtx_constant_pool *pool;
3757   char label[256];
3758   rtx def, symbol;
3759   hashval_t hash;
3760   unsigned int align;
3761   void **slot;
3762
3763   /* If we're not allowed to drop X into the constant pool, don't.  */
3764   if (targetm.cannot_force_const_mem (x))
3765     return NULL_RTX;
3766
3767   /* Record that this function has used a constant pool entry.  */
3768   crtl->uses_const_pool = 1;
3769
3770   /* Decide which pool to use.  */
3771   pool = (targetm.use_blocks_for_constant_p (mode, x)
3772           ? shared_constant_pool
3773           : crtl->varasm.pool);
3774
3775   /* Lookup the value in the hashtable.  */
3776   tmp.constant = x;
3777   tmp.mode = mode;
3778   hash = const_rtx_hash (x);
3779   slot = htab_find_slot_with_hash (pool->const_rtx_htab, &tmp, hash, INSERT);
3780   desc = (struct constant_descriptor_rtx *) *slot;
3781
3782   /* If the constant was already present, return its memory.  */
3783   if (desc)
3784     return copy_rtx (desc->mem);
3785
3786   /* Otherwise, create a new descriptor.  */
3787   desc = ggc_alloc_constant_descriptor_rtx ();
3788   *slot = desc;
3789
3790   /* Align the location counter as required by EXP's data type.  */
3791   align = GET_MODE_ALIGNMENT (mode == VOIDmode ? word_mode : mode);
3792 #ifdef CONSTANT_ALIGNMENT
3793   {
3794     tree type = lang_hooks.types.type_for_mode (mode, 0);
3795     if (type != NULL_TREE)
3796       align = CONSTANT_ALIGNMENT (make_tree (type, x), align);
3797   }
3798 #endif
3799
3800   pool->offset += (align / BITS_PER_UNIT) - 1;
3801   pool->offset &= ~ ((align / BITS_PER_UNIT) - 1);
3802
3803   desc->next = NULL;
3804   desc->constant = tmp.constant;
3805   desc->offset = pool->offset;
3806   desc->hash = hash;
3807   desc->mode = mode;
3808   desc->align = align;
3809   desc->labelno = const_labelno;
3810   desc->mark = 0;
3811
3812   pool->offset += GET_MODE_SIZE (mode);
3813   if (pool->last)
3814     pool->last->next = desc;
3815   else
3816     pool->first = pool->last = desc;
3817   pool->last = desc;
3818
3819   /* Create a string containing the label name, in LABEL.  */
3820   ASM_GENERATE_INTERNAL_LABEL (label, "LC", const_labelno);
3821   ++const_labelno;
3822
3823   /* Construct the SYMBOL_REF.  Make sure to mark it as belonging to
3824      the constants pool.  */
3825   if (use_object_blocks_p () && targetm.use_blocks_for_constant_p (mode, x))
3826     {
3827       section *sect = targetm.asm_out.select_rtx_section (mode, x, align);
3828       symbol = create_block_symbol (ggc_strdup (label),
3829                                     get_block_for_section (sect), -1);
3830     }
3831   else
3832     symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3833   desc->sym = symbol;
3834   SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3835   CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3836   SET_SYMBOL_REF_CONSTANT (symbol, desc);
3837
3838   /* Construct the MEM.  */
3839   desc->mem = def = gen_const_mem (mode, symbol);
3840   set_mem_attributes (def, lang_hooks.types.type_for_mode (mode, 0), 1);
3841   set_mem_align (def, align);
3842
3843   /* If we're dropping a label to the constant pool, make sure we
3844      don't delete it.  */
3845   if (GET_CODE (x) == LABEL_REF)
3846     LABEL_PRESERVE_P (XEXP (x, 0)) = 1;
3847
3848   return copy_rtx (def);
3849 }
3850 \f
3851 /* Given a constant pool SYMBOL_REF, return the corresponding constant.  */
3852
3853 rtx
3854 get_pool_constant (rtx addr)
3855 {
3856   return SYMBOL_REF_CONSTANT (addr)->constant;
3857 }
3858
3859 /* Given a constant pool SYMBOL_REF, return the corresponding constant
3860    and whether it has been output or not.  */
3861
3862 rtx
3863 get_pool_constant_mark (rtx addr, bool *pmarked)
3864 {
3865   struct constant_descriptor_rtx *desc;
3866
3867   desc = SYMBOL_REF_CONSTANT (addr);
3868   *pmarked = (desc->mark != 0);
3869   return desc->constant;
3870 }
3871
3872 /* Similar, return the mode.  */
3873
3874 enum machine_mode
3875 get_pool_mode (const_rtx addr)
3876 {
3877   return SYMBOL_REF_CONSTANT (addr)->mode;
3878 }
3879
3880 /* Return the size of the constant pool.  */
3881
3882 int
3883 get_pool_size (void)
3884 {
3885   return crtl->varasm.pool->offset;
3886 }
3887 \f
3888 /* Worker function for output_constant_pool_1.  Emit assembly for X
3889    in MODE with known alignment ALIGN.  */
3890
3891 static void
3892 output_constant_pool_2 (enum machine_mode mode, rtx x, unsigned int align)
3893 {
3894   switch (GET_MODE_CLASS (mode))
3895     {
3896     case MODE_FLOAT:
3897     case MODE_DECIMAL_FLOAT:
3898       {
3899         REAL_VALUE_TYPE r;
3900
3901         gcc_assert (GET_CODE (x) == CONST_DOUBLE);
3902         REAL_VALUE_FROM_CONST_DOUBLE (r, x);
3903         assemble_real (r, mode, align);
3904         break;
3905       }
3906
3907     case MODE_INT:
3908     case MODE_PARTIAL_INT:
3909     case MODE_FRACT:
3910     case MODE_UFRACT:
3911     case MODE_ACCUM:
3912     case MODE_UACCUM:
3913       assemble_integer (x, GET_MODE_SIZE (mode), align, 1);
3914       break;
3915
3916     case MODE_VECTOR_FLOAT:
3917     case MODE_VECTOR_INT:
3918     case MODE_VECTOR_FRACT:
3919     case MODE_VECTOR_UFRACT:
3920     case MODE_VECTOR_ACCUM:
3921     case MODE_VECTOR_UACCUM:
3922       {
3923         int i, units;
3924         enum machine_mode submode = GET_MODE_INNER (mode);
3925         unsigned int subalign = MIN (align, GET_MODE_BITSIZE (submode));
3926
3927         gcc_assert (GET_CODE (x) == CONST_VECTOR);
3928         units = CONST_VECTOR_NUNITS (x);
3929
3930         for (i = 0; i < units; i++)
3931           {
3932             rtx elt = CONST_VECTOR_ELT (x, i);
3933             output_constant_pool_2 (submode, elt, i ? subalign : align);
3934           }
3935       }
3936       break;
3937
3938     default:
3939       gcc_unreachable ();
3940     }
3941 }
3942
3943 /* Worker function for output_constant_pool.  Emit constant DESC,
3944    giving it ALIGN bits of alignment.  */
3945
3946 static void
3947 output_constant_pool_1 (struct constant_descriptor_rtx *desc,
3948                         unsigned int align)
3949 {
3950   rtx x, tmp;
3951
3952   x = desc->constant;
3953
3954   /* See if X is a LABEL_REF (or a CONST referring to a LABEL_REF)
3955      whose CODE_LABEL has been deleted.  This can occur if a jump table
3956      is eliminated by optimization.  If so, write a constant of zero
3957      instead.  Note that this can also happen by turning the
3958      CODE_LABEL into a NOTE.  */
3959   /* ??? This seems completely and utterly wrong.  Certainly it's
3960      not true for NOTE_INSN_DELETED_LABEL, but I disbelieve proper
3961      functioning even with INSN_DELETED_P and friends.  */
3962
3963   tmp = x;
3964   switch (GET_CODE (tmp))
3965     {
3966     case CONST:
3967       if (GET_CODE (XEXP (tmp, 0)) != PLUS
3968           || GET_CODE (XEXP (XEXP (tmp, 0), 0)) != LABEL_REF)
3969         break;
3970       tmp = XEXP (XEXP (tmp, 0), 0);
3971       /* FALLTHRU  */
3972
3973     case LABEL_REF:
3974       tmp = XEXP (tmp, 0);
3975       gcc_assert (!INSN_DELETED_P (tmp));
3976       gcc_assert (!NOTE_P (tmp)
3977                   || NOTE_KIND (tmp) != NOTE_INSN_DELETED);
3978       break;
3979
3980     default:
3981       break;
3982     }
3983
3984 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3985   ASM_OUTPUT_SPECIAL_POOL_ENTRY (asm_out_file, x, desc->mode,
3986                                  align, desc->labelno, done);
3987 #endif
3988
3989   assemble_align (align);
3990
3991   /* Output the label.  */
3992   targetm.asm_out.internal_label (asm_out_file, "LC", desc->labelno);
3993
3994   /* Output the data.  */
3995   output_constant_pool_2 (desc->mode, x, align);
3996
3997   /* Make sure all constants in SECTION_MERGE and not SECTION_STRINGS
3998      sections have proper size.  */
3999   if (align > GET_MODE_BITSIZE (desc->mode)
4000       && in_section
4001       && (in_section->common.flags & SECTION_MERGE))
4002     assemble_align (align);
4003
4004 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
4005  done:
4006 #endif
4007   return;
4008 }
4009
4010 /* Given a SYMBOL_REF CURRENT_RTX, mark it and all constants it refers
4011    to as used.  Emit referenced deferred strings.  This function can
4012    be used with for_each_rtx to mark all SYMBOL_REFs in an rtx.  */
4013
4014 static int
4015 mark_constant (rtx *current_rtx, void *data ATTRIBUTE_UNUSED)
4016 {
4017   rtx x = *current_rtx;
4018
4019   if (x == NULL_RTX || GET_CODE (x) != SYMBOL_REF)
4020     return 0;
4021
4022   if (CONSTANT_POOL_ADDRESS_P (x))
4023     {
4024       struct constant_descriptor_rtx *desc = SYMBOL_REF_CONSTANT (x);
4025       if (desc->mark == 0)
4026         {
4027           desc->mark = 1;
4028           for_each_rtx (&desc->constant, mark_constant, NULL);
4029         }
4030     }
4031   else if (TREE_CONSTANT_POOL_ADDRESS_P (x))
4032     {
4033       tree decl = SYMBOL_REF_DECL (x);
4034       if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
4035         {
4036           n_deferred_constants--;
4037           output_constant_def_contents (x);
4038         }
4039     }
4040
4041   return -1;
4042 }
4043
4044 /* Look through appropriate parts of INSN, marking all entries in the
4045    constant pool which are actually being used.  Entries that are only
4046    referenced by other constants are also marked as used.  Emit
4047    deferred strings that are used.  */
4048
4049 static void
4050 mark_constants (rtx insn)
4051 {
4052   if (!INSN_P (insn))
4053     return;
4054
4055   /* Insns may appear inside a SEQUENCE.  Only check the patterns of
4056      insns, not any notes that may be attached.  We don't want to mark
4057      a constant just because it happens to appear in a REG_EQUIV note.  */
4058   if (GET_CODE (PATTERN (insn)) == SEQUENCE)
4059     {
4060       rtx seq = PATTERN (insn);
4061       int i, n = XVECLEN (seq, 0);
4062       for (i = 0; i < n; ++i)
4063         {
4064           rtx subinsn = XVECEXP (seq, 0, i);
4065           if (INSN_P (subinsn))
4066             for_each_rtx (&PATTERN (subinsn), mark_constant, NULL);
4067         }
4068     }
4069   else
4070     for_each_rtx (&PATTERN (insn), mark_constant, NULL);
4071 }
4072
4073 /* Look through the instructions for this function, and mark all the
4074    entries in POOL which are actually being used.  Emit deferred constants
4075    which have indeed been used.  */
4076
4077 static void
4078 mark_constant_pool (void)
4079 {
4080   rtx insn, link;
4081
4082   if (!crtl->uses_const_pool && n_deferred_constants == 0)
4083     return;
4084
4085   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4086     mark_constants (insn);
4087
4088   for (link = crtl->epilogue_delay_list;
4089        link;
4090        link = XEXP (link, 1))
4091     mark_constants (XEXP (link, 0));
4092 }
4093
4094 /* Write all the constants in POOL.  */
4095
4096 static void
4097 output_constant_pool_contents (struct rtx_constant_pool *pool)
4098 {
4099   struct constant_descriptor_rtx *desc;
4100
4101   for (desc = pool->first; desc ; desc = desc->next)
4102     if (desc->mark)
4103       {
4104         /* If the constant is part of an object_block, make sure that
4105            the constant has been positioned within its block, but do not
4106            write out its definition yet.  output_object_blocks will do
4107            that later.  */
4108         if (SYMBOL_REF_HAS_BLOCK_INFO_P (desc->sym)
4109             && SYMBOL_REF_BLOCK (desc->sym))
4110           place_block_symbol (desc->sym);
4111         else
4112           {
4113             switch_to_section (targetm.asm_out.select_rtx_section
4114                                (desc->mode, desc->constant, desc->align));
4115             output_constant_pool_1 (desc, desc->align);
4116           }
4117       }
4118 }
4119
4120 /* Mark all constants that are used in the current function, then write
4121    out the function's private constant pool.  */
4122
4123 static void
4124 output_constant_pool (const char *fnname ATTRIBUTE_UNUSED,
4125                       tree fndecl ATTRIBUTE_UNUSED)
4126 {
4127   struct rtx_constant_pool *pool = crtl->varasm.pool;
4128
4129   /* It is possible for gcc to call force_const_mem and then to later
4130      discard the instructions which refer to the constant.  In such a
4131      case we do not need to output the constant.  */
4132   mark_constant_pool ();
4133
4134 #ifdef ASM_OUTPUT_POOL_PROLOGUE
4135   ASM_OUTPUT_POOL_PROLOGUE (asm_out_file, fnname, fndecl, pool->offset);
4136 #endif
4137
4138   output_constant_pool_contents (pool);
4139
4140 #ifdef ASM_OUTPUT_POOL_EPILOGUE
4141   ASM_OUTPUT_POOL_EPILOGUE (asm_out_file, fnname, fndecl, pool->offset);
4142 #endif
4143 }
4144 \f
4145 /* Write the contents of the shared constant pool.  */
4146
4147 void
4148 output_shared_constant_pool (void)
4149 {
4150   output_constant_pool_contents (shared_constant_pool);
4151 }
4152 \f
4153 /* Determine what kind of relocations EXP may need.  */
4154
4155 int
4156 compute_reloc_for_constant (tree exp)
4157 {
4158   int reloc = 0, reloc2;
4159   tree tem;
4160
4161   switch (TREE_CODE (exp))
4162     {
4163     case ADDR_EXPR:
4164     case FDESC_EXPR:
4165       /* Go inside any operations that get_inner_reference can handle and see
4166          if what's inside is a constant: no need to do anything here for
4167          addresses of variables or functions.  */
4168       for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
4169            tem = TREE_OPERAND (tem, 0))
4170         ;
4171
4172       if (TREE_PUBLIC (tem))
4173         reloc |= 2;
4174       else
4175         reloc |= 1;
4176       break;
4177
4178     case PLUS_EXPR:
4179     case POINTER_PLUS_EXPR:
4180       reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4181       reloc |= compute_reloc_for_constant (TREE_OPERAND (exp, 1));
4182       break;
4183
4184     case MINUS_EXPR:
4185       reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4186       reloc2 = compute_reloc_for_constant (TREE_OPERAND (exp, 1));
4187       /* The difference of two local labels is computable at link time.  */
4188       if (reloc == 1 && reloc2 == 1)
4189         reloc = 0;
4190       else
4191         reloc |= reloc2;
4192       break;
4193
4194     CASE_CONVERT:
4195     case VIEW_CONVERT_EXPR:
4196       reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4197       break;
4198
4199     case CONSTRUCTOR:
4200       {
4201         unsigned HOST_WIDE_INT idx;
4202         FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
4203           if (tem != 0)
4204             reloc |= compute_reloc_for_constant (tem);
4205       }
4206       break;
4207
4208     default:
4209       break;
4210     }
4211   return reloc;
4212 }
4213
4214 /* Find all the constants whose addresses are referenced inside of EXP,
4215    and make sure assembler code with a label has been output for each one.
4216    Indicate whether an ADDR_EXPR has been encountered.  */
4217
4218 static void
4219 output_addressed_constants (tree exp)
4220 {
4221   tree tem;
4222
4223   switch (TREE_CODE (exp))
4224     {
4225     case ADDR_EXPR:
4226     case FDESC_EXPR:
4227       /* Go inside any operations that get_inner_reference can handle and see
4228          if what's inside is a constant: no need to do anything here for
4229          addresses of variables or functions.  */
4230       for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
4231            tem = TREE_OPERAND (tem, 0))
4232         ;
4233
4234       /* If we have an initialized CONST_DECL, retrieve the initializer.  */
4235       if (TREE_CODE (tem) == CONST_DECL && DECL_INITIAL (tem))
4236         tem = DECL_INITIAL (tem);
4237
4238       if (CONSTANT_CLASS_P (tem) || TREE_CODE (tem) == CONSTRUCTOR)
4239         output_constant_def (tem, 0);
4240       break;
4241
4242     case PLUS_EXPR:
4243     case POINTER_PLUS_EXPR:
4244     case MINUS_EXPR:
4245       output_addressed_constants (TREE_OPERAND (exp, 1));
4246       /* Fall through.  */
4247
4248     CASE_CONVERT:
4249     case VIEW_CONVERT_EXPR:
4250       output_addressed_constants (TREE_OPERAND (exp, 0));
4251       break;
4252
4253     case CONSTRUCTOR:
4254       {
4255         unsigned HOST_WIDE_INT idx;
4256         FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
4257           if (tem != 0)
4258             output_addressed_constants (tem);
4259       }
4260       break;
4261
4262     default:
4263       break;
4264     }
4265 }
4266 \f
4267 /* Whether a constructor CTOR is a valid static constant initializer if all
4268    its elements are.  This used to be internal to initializer_constant_valid_p
4269    and has been exposed to let other functions like categorize_ctor_elements
4270    evaluate the property while walking a constructor for other purposes.  */
4271
4272 bool
4273 constructor_static_from_elts_p (const_tree ctor)
4274 {
4275   return (TREE_CONSTANT (ctor)
4276           && (TREE_CODE (TREE_TYPE (ctor)) == UNION_TYPE
4277               || TREE_CODE (TREE_TYPE (ctor)) == RECORD_TYPE)
4278           && !VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (ctor)));
4279 }
4280
4281 static tree initializer_constant_valid_p_1 (tree value, tree endtype,
4282                                             tree *cache);
4283
4284 /* A subroutine of initializer_constant_valid_p.  VALUE is a MINUS_EXPR,
4285    PLUS_EXPR or POINTER_PLUS_EXPR.  This looks for cases of VALUE
4286    which are valid when ENDTYPE is an integer of any size; in
4287    particular, this does not accept a pointer minus a constant.  This
4288    returns null_pointer_node if the VALUE is an absolute constant
4289    which can be used to initialize a static variable.  Otherwise it
4290    returns NULL.  */
4291
4292 static tree
4293 narrowing_initializer_constant_valid_p (tree value, tree endtype, tree *cache)
4294 {
4295   tree op0, op1;
4296
4297   if (!INTEGRAL_TYPE_P (endtype))
4298     return NULL_TREE;
4299
4300   op0 = TREE_OPERAND (value, 0);
4301   op1 = TREE_OPERAND (value, 1);
4302
4303   /* Like STRIP_NOPS except allow the operand mode to widen.  This
4304      works around a feature of fold that simplifies (int)(p1 - p2) to
4305      ((int)p1 - (int)p2) under the theory that the narrower operation
4306      is cheaper.  */
4307
4308   while (CONVERT_EXPR_P (op0)
4309          || TREE_CODE (op0) == NON_LVALUE_EXPR)
4310     {
4311       tree inner = TREE_OPERAND (op0, 0);
4312       if (inner == error_mark_node
4313           || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4314           || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))
4315               > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
4316         break;
4317       op0 = inner;
4318     }
4319
4320   while (CONVERT_EXPR_P (op1)
4321          || TREE_CODE (op1) == NON_LVALUE_EXPR)
4322     {
4323       tree inner = TREE_OPERAND (op1, 0);
4324       if (inner == error_mark_node
4325           || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4326           || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op1)))
4327               > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
4328         break;
4329       op1 = inner;
4330     }
4331
4332   op0 = initializer_constant_valid_p_1 (op0, endtype, cache);
4333   if (!op0)
4334     return NULL_TREE;
4335
4336   op1 = initializer_constant_valid_p_1 (op1, endtype,
4337                                         cache ? cache + 2 : NULL);
4338   /* Both initializers must be known.  */
4339   if (op1)
4340     {
4341       if (op0 == op1
4342           && (op0 == null_pointer_node
4343               || TREE_CODE (value) == MINUS_EXPR))
4344         return null_pointer_node;
4345
4346       /* Support differences between labels.  */
4347       if (TREE_CODE (op0) == LABEL_DECL
4348           && TREE_CODE (op1) == LABEL_DECL)
4349         return null_pointer_node;
4350
4351       if (TREE_CODE (op0) == STRING_CST
4352           && TREE_CODE (op1) == STRING_CST
4353           && operand_equal_p (op0, op1, 1))
4354         return null_pointer_node;
4355     }
4356
4357   return NULL_TREE;
4358 }
4359
4360 /* Helper function of initializer_constant_valid_p.
4361    Return nonzero if VALUE is a valid constant-valued expression
4362    for use in initializing a static variable; one that can be an
4363    element of a "constant" initializer.
4364
4365    Return null_pointer_node if the value is absolute;
4366    if it is relocatable, return the variable that determines the relocation.
4367    We assume that VALUE has been folded as much as possible;
4368    therefore, we do not need to check for such things as
4369    arithmetic-combinations of integers.
4370
4371    Use CACHE (pointer to 2 tree values) for caching if non-NULL.  */
4372
4373 static tree
4374 initializer_constant_valid_p_1 (tree value, tree endtype, tree *cache)
4375 {
4376   tree ret;
4377
4378   switch (TREE_CODE (value))
4379     {
4380     case CONSTRUCTOR:
4381       if (constructor_static_from_elts_p (value))
4382         {
4383           unsigned HOST_WIDE_INT idx;
4384           tree elt;
4385           bool absolute = true;
4386
4387           if (cache && cache[0] == value)
4388             return cache[1];
4389           FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
4390             {
4391               tree reloc;
4392               reloc = initializer_constant_valid_p_1 (elt, TREE_TYPE (elt),
4393                                                       NULL);
4394               if (!reloc)
4395                 {
4396                   if (cache)
4397                     {
4398                       cache[0] = value;
4399                       cache[1] = NULL_TREE;
4400                     }
4401                   return NULL_TREE;
4402                 }
4403               if (reloc != null_pointer_node)
4404                 absolute = false;
4405             }
4406           /* For a non-absolute relocation, there is no single
4407              variable that can be "the variable that determines the
4408              relocation."  */
4409           if (cache)
4410             {
4411               cache[0] = value;
4412               cache[1] = absolute ? null_pointer_node : error_mark_node;
4413             }
4414           return absolute ? null_pointer_node : error_mark_node;
4415         }
4416
4417       return TREE_STATIC (value) ? null_pointer_node : NULL_TREE;
4418
4419     case INTEGER_CST:
4420     case VECTOR_CST:
4421     case REAL_CST:
4422     case FIXED_CST:
4423     case STRING_CST:
4424     case COMPLEX_CST:
4425       return null_pointer_node;
4426
4427     case ADDR_EXPR:
4428     case FDESC_EXPR:
4429       {
4430         tree op0 = staticp (TREE_OPERAND (value, 0));
4431         if (op0)
4432           {
4433             /* "&(*a).f" is like unto pointer arithmetic.  If "a" turns out
4434                to be a constant, this is old-skool offsetof-like nonsense.  */
4435             if (TREE_CODE (op0) == INDIRECT_REF
4436                 && TREE_CONSTANT (TREE_OPERAND (op0, 0)))
4437               return null_pointer_node;
4438             /* Taking the address of a nested function involves a trampoline,
4439                unless we don't need or want one.  */
4440             if (TREE_CODE (op0) == FUNCTION_DECL
4441                 && DECL_STATIC_CHAIN (op0)
4442                 && !TREE_NO_TRAMPOLINE (value))
4443               return NULL_TREE;
4444             /* "&{...}" requires a temporary to hold the constructed
4445                object.  */
4446             if (TREE_CODE (op0) == CONSTRUCTOR)
4447               return NULL_TREE;
4448           }
4449         return op0;
4450       }
4451
4452     case NON_LVALUE_EXPR:
4453       return initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4454                                              endtype, cache);
4455
4456     case VIEW_CONVERT_EXPR:
4457       {
4458         tree src = TREE_OPERAND (value, 0);
4459         tree src_type = TREE_TYPE (src);
4460         tree dest_type = TREE_TYPE (value);
4461
4462         /* Allow view-conversions from aggregate to non-aggregate type only
4463            if the bit pattern is fully preserved afterwards; otherwise, the
4464            RTL expander won't be able to apply a subsequent transformation
4465            to the underlying constructor.  */
4466         if (AGGREGATE_TYPE_P (src_type) && !AGGREGATE_TYPE_P (dest_type))
4467           {
4468             if (TYPE_MODE (endtype) == TYPE_MODE (dest_type))
4469               return initializer_constant_valid_p_1 (src, endtype, cache);
4470             else
4471               return NULL_TREE;
4472           }
4473
4474         /* Allow all other kinds of view-conversion.  */
4475         return initializer_constant_valid_p_1 (src, endtype, cache);
4476       }
4477
4478     CASE_CONVERT:
4479       {
4480         tree src = TREE_OPERAND (value, 0);
4481         tree src_type = TREE_TYPE (src);
4482         tree dest_type = TREE_TYPE (value);
4483
4484         /* Allow conversions between pointer types, floating-point
4485            types, and offset types.  */
4486         if ((POINTER_TYPE_P (dest_type) && POINTER_TYPE_P (src_type))
4487             || (FLOAT_TYPE_P (dest_type) && FLOAT_TYPE_P (src_type))
4488             || (TREE_CODE (dest_type) == OFFSET_TYPE
4489                 && TREE_CODE (src_type) == OFFSET_TYPE))
4490           return initializer_constant_valid_p_1 (src, endtype, cache);
4491
4492         /* Allow length-preserving conversions between integer types.  */
4493         if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type)
4494             && (TYPE_PRECISION (dest_type) == TYPE_PRECISION (src_type)))
4495           return initializer_constant_valid_p_1 (src, endtype, cache);
4496
4497         /* Allow conversions between other integer types only if
4498            explicit value.  */
4499         if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type))
4500           {
4501             tree inner = initializer_constant_valid_p_1 (src, endtype, cache);
4502             if (inner == null_pointer_node)
4503               return null_pointer_node;
4504             break;
4505           }
4506
4507         /* Allow (int) &foo provided int is as wide as a pointer.  */
4508         if (INTEGRAL_TYPE_P (dest_type) && POINTER_TYPE_P (src_type)
4509             && (TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type)))
4510           return initializer_constant_valid_p_1 (src, endtype, cache);
4511
4512         /* Likewise conversions from int to pointers, but also allow
4513            conversions from 0.  */
4514         if ((POINTER_TYPE_P (dest_type)
4515              || TREE_CODE (dest_type) == OFFSET_TYPE)
4516             && INTEGRAL_TYPE_P (src_type))
4517           {
4518             if (TREE_CODE (src) == INTEGER_CST
4519                 && TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type))
4520               return null_pointer_node;
4521             if (integer_zerop (src))
4522               return null_pointer_node;
4523             else if (TYPE_PRECISION (dest_type) <= TYPE_PRECISION (src_type))
4524               return initializer_constant_valid_p_1 (src, endtype, cache);
4525           }
4526
4527         /* Allow conversions to struct or union types if the value
4528            inside is okay.  */
4529         if (TREE_CODE (dest_type) == RECORD_TYPE
4530             || TREE_CODE (dest_type) == UNION_TYPE)
4531           return initializer_constant_valid_p_1 (src, endtype, cache);
4532       }
4533       break;
4534
4535     case POINTER_PLUS_EXPR:
4536     case PLUS_EXPR:
4537       /* Any valid floating-point constants will have been folded by now;
4538          with -frounding-math we hit this with addition of two constants.  */
4539       if (TREE_CODE (endtype) == REAL_TYPE)
4540         return NULL_TREE;
4541       if (cache && cache[0] == value)
4542         return cache[1];
4543       if (! INTEGRAL_TYPE_P (endtype)
4544           || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
4545         {
4546           tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
4547           tree valid0
4548             = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4549                                               endtype, ncache);
4550           tree valid1
4551             = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
4552                                               endtype, ncache + 2);
4553           /* If either term is absolute, use the other term's relocation.  */
4554           if (valid0 == null_pointer_node)
4555             ret = valid1;
4556           else if (valid1 == null_pointer_node)
4557             ret = valid0;
4558           /* Support narrowing pointer differences.  */
4559           else
4560             ret = narrowing_initializer_constant_valid_p (value, endtype,
4561                                                           ncache);
4562         }
4563       else
4564       /* Support narrowing pointer differences.  */
4565         ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
4566       if (cache)
4567         {
4568           cache[0] = value;
4569           cache[1] = ret;
4570         }
4571       return ret;
4572
4573     case MINUS_EXPR:
4574       if (TREE_CODE (endtype) == REAL_TYPE)
4575         return NULL_TREE;
4576       if (cache && cache[0] == value)
4577         return cache[1];
4578       if (! INTEGRAL_TYPE_P (endtype)
4579           || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
4580         {
4581           tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
4582           tree valid0
4583             = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4584                                               endtype, ncache);
4585           tree valid1
4586             = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
4587                                               endtype, ncache + 2);
4588           /* Win if second argument is absolute.  */
4589           if (valid1 == null_pointer_node)
4590             ret = valid0;
4591           /* Win if both arguments have the same relocation.
4592              Then the value is absolute.  */
4593           else if (valid0 == valid1 && valid0 != 0)
4594             ret = null_pointer_node;
4595           /* Since GCC guarantees that string constants are unique in the
4596              generated code, a subtraction between two copies of the same
4597              constant string is absolute.  */
4598           else if (valid0 && TREE_CODE (valid0) == STRING_CST
4599                    && valid1 && TREE_CODE (valid1) == STRING_CST
4600                    && operand_equal_p (valid0, valid1, 1))
4601             ret = null_pointer_node;
4602           /* Support narrowing differences.  */
4603           else
4604             ret = narrowing_initializer_constant_valid_p (value, endtype,
4605                                                           ncache);
4606         }
4607       else
4608         /* Support narrowing differences.  */
4609         ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
4610       if (cache)
4611         {
4612           cache[0] = value;
4613           cache[1] = ret;
4614         }
4615       return ret;
4616
4617     default:
4618       break;
4619     }
4620
4621   return NULL_TREE;
4622 }
4623
4624 /* Return nonzero if VALUE is a valid constant-valued expression
4625    for use in initializing a static variable; one that can be an
4626    element of a "constant" initializer.
4627
4628    Return null_pointer_node if the value is absolute;
4629    if it is relocatable, return the variable that determines the relocation.
4630    We assume that VALUE has been folded as much as possible;
4631    therefore, we do not need to check for such things as
4632    arithmetic-combinations of integers.  */
4633 tree
4634 initializer_constant_valid_p (tree value, tree endtype)
4635 {
4636   return initializer_constant_valid_p_1 (value, endtype, NULL);
4637 }
4638 \f
4639 /* Return true if VALUE is a valid constant-valued expression
4640    for use in initializing a static bit-field; one that can be
4641    an element of a "constant" initializer.  */
4642
4643 bool
4644 initializer_constant_valid_for_bitfield_p (tree value)
4645 {
4646   /* For bitfields we support integer constants or possibly nested aggregates
4647      of such.  */
4648   switch (TREE_CODE (value))
4649     {
4650     case CONSTRUCTOR:
4651       {
4652         unsigned HOST_WIDE_INT idx;
4653         tree elt;
4654
4655         FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
4656           if (!initializer_constant_valid_for_bitfield_p (elt))
4657             return false;
4658         return true;
4659       }
4660
4661     case INTEGER_CST:
4662       return true;
4663
4664     case VIEW_CONVERT_EXPR:
4665     case NON_LVALUE_EXPR:
4666       return
4667         initializer_constant_valid_for_bitfield_p (TREE_OPERAND (value, 0));
4668
4669     default:
4670       break;
4671     }
4672
4673   return false;
4674 }
4675
4676 /* output_constructor outer state of relevance in recursive calls, typically
4677    for nested aggregate bitfields.  */
4678
4679 typedef struct {
4680   unsigned int bit_offset;  /* current position in ...  */
4681   int byte;                 /* ... the outer byte buffer.  */
4682 } oc_outer_state;
4683
4684 static unsigned HOST_WIDE_INT
4685   output_constructor (tree, unsigned HOST_WIDE_INT, unsigned int,
4686                       oc_outer_state *);
4687
4688 /* Output assembler code for constant EXP to FILE, with no label.
4689    This includes the pseudo-op such as ".int" or ".byte", and a newline.
4690    Assumes output_addressed_constants has been done on EXP already.
4691
4692    Generate exactly SIZE bytes of assembler data, padding at the end
4693    with zeros if necessary.  SIZE must always be specified.
4694
4695    SIZE is important for structure constructors,
4696    since trailing members may have been omitted from the constructor.
4697    It is also important for initialization of arrays from string constants
4698    since the full length of the string constant might not be wanted.
4699    It is also needed for initialization of unions, where the initializer's
4700    type is just one member, and that may not be as long as the union.
4701
4702    There a case in which we would fail to output exactly SIZE bytes:
4703    for a structure constructor that wants to produce more than SIZE bytes.
4704    But such constructors will never be generated for any possible input.
4705
4706    ALIGN is the alignment of the data in bits.  */
4707
4708 void
4709 output_constant (tree exp, unsigned HOST_WIDE_INT size, unsigned int align)
4710 {
4711   enum tree_code code;
4712   unsigned HOST_WIDE_INT thissize;
4713
4714   if (size == 0 || flag_syntax_only)
4715     return;
4716
4717   /* See if we're trying to initialize a pointer in a non-default mode
4718      to the address of some declaration somewhere.  If the target says
4719      the mode is valid for pointers, assume the target has a way of
4720      resolving it.  */
4721   if (TREE_CODE (exp) == NOP_EXPR
4722       && POINTER_TYPE_P (TREE_TYPE (exp))
4723       && targetm.addr_space.valid_pointer_mode
4724            (TYPE_MODE (TREE_TYPE (exp)),
4725             TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
4726     {
4727       tree saved_type = TREE_TYPE (exp);
4728
4729       /* Peel off any intermediate conversions-to-pointer for valid
4730          pointer modes.  */
4731       while (TREE_CODE (exp) == NOP_EXPR
4732              && POINTER_TYPE_P (TREE_TYPE (exp))
4733              && targetm.addr_space.valid_pointer_mode
4734                   (TYPE_MODE (TREE_TYPE (exp)),
4735                    TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
4736         exp = TREE_OPERAND (exp, 0);
4737
4738       /* If what we're left with is the address of something, we can
4739          convert the address to the final type and output it that
4740          way.  */
4741       if (TREE_CODE (exp) == ADDR_EXPR)
4742         exp = build1 (ADDR_EXPR, saved_type, TREE_OPERAND (exp, 0));
4743       /* Likewise for constant ints.  */
4744       else if (TREE_CODE (exp) == INTEGER_CST)
4745         exp = build_int_cst_wide (saved_type, TREE_INT_CST_LOW (exp),
4746                                   TREE_INT_CST_HIGH (exp));
4747
4748     }
4749
4750   /* Eliminate any conversions since we'll be outputting the underlying
4751      constant.  */
4752   while (CONVERT_EXPR_P (exp)
4753          || TREE_CODE (exp) == NON_LVALUE_EXPR
4754          || TREE_CODE (exp) == VIEW_CONVERT_EXPR)
4755     {
4756       HOST_WIDE_INT type_size = int_size_in_bytes (TREE_TYPE (exp));
4757       HOST_WIDE_INT op_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (exp, 0)));
4758
4759       /* Make sure eliminating the conversion is really a no-op, except with
4760          VIEW_CONVERT_EXPRs to allow for wild Ada unchecked conversions and
4761          union types to allow for Ada unchecked unions.  */
4762       if (type_size > op_size
4763           && TREE_CODE (exp) != VIEW_CONVERT_EXPR
4764           && TREE_CODE (TREE_TYPE (exp)) != UNION_TYPE)
4765         /* Keep the conversion. */
4766         break;
4767       else
4768         exp = TREE_OPERAND (exp, 0);
4769     }
4770
4771   code = TREE_CODE (TREE_TYPE (exp));
4772   thissize = int_size_in_bytes (TREE_TYPE (exp));
4773
4774   /* Allow a constructor with no elements for any data type.
4775      This means to fill the space with zeros.  */
4776   if (TREE_CODE (exp) == CONSTRUCTOR
4777       && VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (exp)))
4778     {
4779       assemble_zeros (size);
4780       return;
4781     }
4782
4783   if (TREE_CODE (exp) == FDESC_EXPR)
4784     {
4785 #ifdef ASM_OUTPUT_FDESC
4786       HOST_WIDE_INT part = tree_low_cst (TREE_OPERAND (exp, 1), 0);
4787       tree decl = TREE_OPERAND (exp, 0);
4788       ASM_OUTPUT_FDESC (asm_out_file, decl, part);
4789 #else
4790       gcc_unreachable ();
4791 #endif
4792       return;
4793     }
4794
4795   /* Now output the underlying data.  If we've handling the padding, return.
4796      Otherwise, break and ensure SIZE is the size written.  */
4797   switch (code)
4798     {
4799     case BOOLEAN_TYPE:
4800     case INTEGER_TYPE:
4801     case ENUMERAL_TYPE:
4802     case POINTER_TYPE:
4803     case REFERENCE_TYPE:
4804     case OFFSET_TYPE:
4805     case FIXED_POINT_TYPE:
4806       if (! assemble_integer (expand_expr (exp, NULL_RTX, VOIDmode,
4807                                            EXPAND_INITIALIZER),
4808                               MIN (size, thissize), align, 0))
4809         error ("initializer for integer/fixed-point value is too complicated");
4810       break;
4811
4812     case REAL_TYPE:
4813       if (TREE_CODE (exp) != REAL_CST)
4814         error ("initializer for floating value is not a floating constant");
4815       else
4816         assemble_real (TREE_REAL_CST (exp), TYPE_MODE (TREE_TYPE (exp)), align);
4817       break;
4818
4819     case COMPLEX_TYPE:
4820       output_constant (TREE_REALPART (exp), thissize / 2, align);
4821       output_constant (TREE_IMAGPART (exp), thissize / 2,
4822                        min_align (align, BITS_PER_UNIT * (thissize / 2)));
4823       break;
4824
4825     case ARRAY_TYPE:
4826     case VECTOR_TYPE:
4827       switch (TREE_CODE (exp))
4828         {
4829         case CONSTRUCTOR:
4830             output_constructor (exp, size, align, NULL);
4831           return;
4832         case STRING_CST:
4833           thissize = MIN ((unsigned HOST_WIDE_INT)TREE_STRING_LENGTH (exp),
4834                           size);
4835           assemble_string (TREE_STRING_POINTER (exp), thissize);
4836           break;
4837
4838         case VECTOR_CST:
4839           {
4840             int elt_size;
4841             tree link;
4842             unsigned int nalign;
4843             enum machine_mode inner;
4844
4845             inner = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
4846             nalign = MIN (align, GET_MODE_ALIGNMENT (inner));
4847
4848             elt_size = GET_MODE_SIZE (inner);
4849
4850             link = TREE_VECTOR_CST_ELTS (exp);
4851             output_constant (TREE_VALUE (link), elt_size, align);
4852             thissize = elt_size;
4853             while ((link = TREE_CHAIN (link)) != NULL)
4854               {
4855                 output_constant (TREE_VALUE (link), elt_size, nalign);
4856                 thissize += elt_size;
4857               }
4858             break;
4859           }
4860         default:
4861           gcc_unreachable ();
4862         }
4863       break;
4864
4865     case RECORD_TYPE:
4866     case UNION_TYPE:
4867       gcc_assert (TREE_CODE (exp) == CONSTRUCTOR);
4868       output_constructor (exp, size, align, NULL);
4869       return;
4870
4871     case ERROR_MARK:
4872       return;
4873
4874     default:
4875       gcc_unreachable ();
4876     }
4877
4878   if (size > thissize)
4879     assemble_zeros (size - thissize);
4880 }
4881
4882 \f
4883 /* Subroutine of output_constructor, used for computing the size of
4884    arrays of unspecified length.  VAL must be a CONSTRUCTOR of an array
4885    type with an unspecified upper bound.  */
4886
4887 static unsigned HOST_WIDE_INT
4888 array_size_for_constructor (tree val)
4889 {
4890   tree max_index, i;
4891   unsigned HOST_WIDE_INT cnt;
4892   tree index, value, tmp;
4893
4894   /* This code used to attempt to handle string constants that are not
4895      arrays of single-bytes, but nothing else does, so there's no point in
4896      doing it here.  */
4897   if (TREE_CODE (val) == STRING_CST)
4898     return TREE_STRING_LENGTH (val);
4899
4900   max_index = NULL_TREE;
4901   FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (val), cnt, index, value)
4902     {
4903       if (TREE_CODE (index) == RANGE_EXPR)
4904         index = TREE_OPERAND (index, 1);
4905       if (max_index == NULL_TREE || tree_int_cst_lt (max_index, index))
4906         max_index = index;
4907     }
4908
4909   if (max_index == NULL_TREE)
4910     return 0;
4911
4912   /* Compute the total number of array elements.  */
4913   tmp = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (val)));
4914   i = size_binop (MINUS_EXPR, fold_convert (sizetype, max_index),
4915                   fold_convert (sizetype, tmp));
4916   i = size_binop (PLUS_EXPR, i, build_int_cst (sizetype, 1));
4917
4918   /* Multiply by the array element unit size to find number of bytes.  */
4919   i = size_binop (MULT_EXPR, i, TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (val))));
4920
4921   return tree_low_cst (i, 1);
4922 }
4923
4924 /* Other datastructures + helpers for output_constructor.  */
4925
4926 /* output_constructor local state to support interaction with helpers.  */
4927
4928 typedef struct {
4929
4930   /* Received arguments.  */
4931   tree exp;                     /* Constructor expression.  */
4932   unsigned HOST_WIDE_INT size;  /* # bytes to output - pad if necessary.  */
4933   unsigned int align;           /* Known initial alignment.  */
4934
4935   /* Constructor expression data.  */
4936   tree type;       /* Expression type.  */
4937   tree field;      /* Current field decl in a record.  */
4938   tree min_index;  /* Lower bound if specified for an array.  */
4939
4940   /* Output processing state.  */
4941   HOST_WIDE_INT total_bytes;  /* # bytes output so far / current position.  */
4942   bool byte_buffer_in_use;    /* Whether byte ...  */
4943   int byte;                   /* ... contains part of a bitfield byte yet to
4944                                  be output.  */
4945
4946   int last_relative_index;    /* Implicit or explicit index of the last
4947                                  array element output within a bitfield.  */
4948   /* Current element.  */
4949   tree val;    /* Current element value.  */
4950   tree index;  /* Current element index.  */
4951
4952 } oc_local_state;
4953
4954 /* Helper for output_constructor.  From the current LOCAL state, output a
4955    RANGE_EXPR element.  */
4956
4957 static void
4958 output_constructor_array_range (oc_local_state *local)
4959 {
4960   unsigned HOST_WIDE_INT fieldsize
4961     = int_size_in_bytes (TREE_TYPE (local->type));
4962
4963   HOST_WIDE_INT lo_index
4964     = tree_low_cst (TREE_OPERAND (local->index, 0), 0);
4965   HOST_WIDE_INT hi_index
4966     = tree_low_cst (TREE_OPERAND (local->index, 1), 0);
4967   HOST_WIDE_INT index;
4968
4969   unsigned int align2
4970     = min_align (local->align, fieldsize * BITS_PER_UNIT);
4971
4972   for (index = lo_index; index <= hi_index; index++)
4973     {
4974       /* Output the element's initial value.  */
4975       if (local->val == NULL_TREE)
4976         assemble_zeros (fieldsize);
4977       else
4978         output_constant (local->val, fieldsize, align2);
4979
4980       /* Count its size.  */
4981       local->total_bytes += fieldsize;
4982     }
4983 }
4984
4985 /* Helper for output_constructor.  From the current LOCAL state, output a
4986    field element that is not true bitfield or part of an outer one.  */
4987
4988 static void
4989 output_constructor_regular_field (oc_local_state *local)
4990 {
4991   /* Field size and position.  Since this structure is static, we know the
4992      positions are constant.  */
4993   unsigned HOST_WIDE_INT fieldsize;
4994   HOST_WIDE_INT fieldpos;
4995
4996   unsigned int align2;
4997
4998   if (local->index != NULL_TREE)
4999     fieldpos = (tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (local->val)), 1)
5000                 * ((tree_low_cst (local->index, 0)
5001                     - tree_low_cst (local->min_index, 0))));
5002   else if (local->field != NULL_TREE)
5003     fieldpos = int_byte_position (local->field);
5004   else
5005     fieldpos = 0;
5006
5007   /* Output any buffered-up bit-fields preceding this element.  */
5008   if (local->byte_buffer_in_use)
5009     {
5010       assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5011       local->total_bytes++;
5012       local->byte_buffer_in_use = false;
5013     }
5014
5015   /* Advance to offset of this element.
5016      Note no alignment needed in an array, since that is guaranteed
5017      if each element has the proper size.  */
5018   if ((local->field != NULL_TREE || local->index != NULL_TREE)
5019       && fieldpos != local->total_bytes)
5020     {
5021       gcc_assert (fieldpos >= local->total_bytes);
5022       assemble_zeros (fieldpos - local->total_bytes);
5023       local->total_bytes = fieldpos;
5024     }
5025
5026   /* Find the alignment of this element.  */
5027   align2 = min_align (local->align, BITS_PER_UNIT * fieldpos);
5028
5029   /* Determine size this element should occupy.  */
5030   if (local->field)
5031     {
5032       fieldsize = 0;
5033
5034       /* If this is an array with an unspecified upper bound,
5035          the initializer determines the size.  */
5036       /* ??? This ought to only checked if DECL_SIZE_UNIT is NULL,
5037          but we cannot do this until the deprecated support for
5038          initializing zero-length array members is removed.  */
5039       if (TREE_CODE (TREE_TYPE (local->field)) == ARRAY_TYPE
5040           && TYPE_DOMAIN (TREE_TYPE (local->field))
5041           && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (local->field))))
5042         {
5043           fieldsize = array_size_for_constructor (local->val);
5044           /* Given a non-empty initialization, this field had
5045              better be last.  */
5046           gcc_assert (!fieldsize || !TREE_CHAIN (local->field));
5047         }
5048       else if (DECL_SIZE_UNIT (local->field))
5049         {
5050           /* ??? This can't be right.  If the decl size overflows
5051              a host integer we will silently emit no data.  */
5052           if (host_integerp (DECL_SIZE_UNIT (local->field), 1))
5053             fieldsize = tree_low_cst (DECL_SIZE_UNIT (local->field), 1);
5054         }
5055     }
5056   else
5057     fieldsize = int_size_in_bytes (TREE_TYPE (local->type));
5058
5059   /* Output the element's initial value.  */
5060   if (local->val == NULL_TREE)
5061     assemble_zeros (fieldsize);
5062   else
5063     output_constant (local->val, fieldsize, align2);
5064
5065   /* Count its size.  */
5066   local->total_bytes += fieldsize;
5067 }
5068
5069 /* Helper for output_constructor.  From the current LOCAL and OUTER states,
5070    output an element that is a true bitfield or part of an outer one.  */
5071
5072 static void
5073 output_constructor_bitfield (oc_local_state *local, oc_outer_state *outer)
5074 {
5075   /* Bit size of this element.  */
5076   HOST_WIDE_INT ebitsize
5077     = (local->field
5078        ? tree_low_cst (DECL_SIZE (local->field), 1)
5079        : tree_low_cst (TYPE_SIZE (TREE_TYPE (local->type)), 1));
5080
5081   /* Relative index of this element if this is an array component.  */
5082   HOST_WIDE_INT relative_index
5083     = (!local->field
5084        ? (local->index
5085           ? (tree_low_cst (local->index, 0)
5086              - tree_low_cst (local->min_index, 0))
5087           : local->last_relative_index + 1)
5088        : 0);
5089
5090   /* Bit position of this element from the start of the containing
5091      constructor.  */
5092   HOST_WIDE_INT constructor_relative_ebitpos
5093       = (local->field
5094          ? int_bit_position (local->field)
5095          : ebitsize * relative_index);
5096
5097   /* Bit position of this element from the start of a possibly ongoing
5098      outer byte buffer.  */
5099   HOST_WIDE_INT byte_relative_ebitpos
5100       = ((outer ? outer->bit_offset : 0) + constructor_relative_ebitpos);
5101
5102   /* From the start of a possibly ongoing outer byte buffer, offsets to
5103      the first bit of this element and to the first bit past the end of
5104      this element.  */
5105   HOST_WIDE_INT next_offset = byte_relative_ebitpos;
5106   HOST_WIDE_INT end_offset = byte_relative_ebitpos + ebitsize;
5107
5108   local->last_relative_index = relative_index;
5109
5110   if (local->val == NULL_TREE)
5111     local->val = integer_zero_node;
5112
5113   while (TREE_CODE (local->val) == VIEW_CONVERT_EXPR
5114          || TREE_CODE (local->val) == NON_LVALUE_EXPR)
5115     local->val = TREE_OPERAND (local->val, 0);
5116
5117   if (TREE_CODE (local->val) != INTEGER_CST
5118       && TREE_CODE (local->val) != CONSTRUCTOR)
5119     {
5120       error ("invalid initial value for member %qE", DECL_NAME (local->field));
5121       return;
5122     }
5123
5124   /* If this field does not start in this (or, next) byte,
5125      skip some bytes.  */
5126   if (next_offset / BITS_PER_UNIT != local->total_bytes)
5127     {
5128       /* Output remnant of any bit field in previous bytes.  */
5129       if (local->byte_buffer_in_use)
5130         {
5131           assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5132           local->total_bytes++;
5133           local->byte_buffer_in_use = false;
5134         }
5135
5136       /* If still not at proper byte, advance to there.  */
5137       if (next_offset / BITS_PER_UNIT != local->total_bytes)
5138         {
5139           gcc_assert (next_offset / BITS_PER_UNIT >= local->total_bytes);
5140           assemble_zeros (next_offset / BITS_PER_UNIT - local->total_bytes);
5141           local->total_bytes = next_offset / BITS_PER_UNIT;
5142         }
5143     }
5144
5145   /* Set up the buffer if necessary.  */
5146   if (!local->byte_buffer_in_use)
5147     {
5148       local->byte = 0;
5149       if (ebitsize > 0)
5150         local->byte_buffer_in_use = true;
5151     }
5152
5153   /* If this is nested constructor, recurse passing the bit offset and the
5154      pending data, then retrieve the new pending data afterwards.  */
5155   if (TREE_CODE (local->val) == CONSTRUCTOR)
5156     {
5157       oc_outer_state output_state;
5158
5159       output_state.bit_offset = next_offset % BITS_PER_UNIT;
5160       output_state.byte = local->byte;
5161       local->total_bytes
5162           += output_constructor (local->val, 0, 0, &output_state);
5163       local->byte = output_state.byte;
5164       return;
5165     }
5166
5167   /* Otherwise, we must split the element into pieces that fall within
5168      separate bytes, and combine each byte with previous or following
5169      bit-fields.  */
5170   while (next_offset < end_offset)
5171     {
5172       int this_time;
5173       int shift;
5174       HOST_WIDE_INT value;
5175       HOST_WIDE_INT next_byte = next_offset / BITS_PER_UNIT;
5176       HOST_WIDE_INT next_bit = next_offset % BITS_PER_UNIT;
5177
5178       /* Advance from byte to byte
5179          within this element when necessary.  */
5180       while (next_byte != local->total_bytes)
5181         {
5182           assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5183           local->total_bytes++;
5184           local->byte = 0;
5185         }
5186
5187       /* Number of bits we can process at once
5188          (all part of the same byte).  */
5189       this_time = MIN (end_offset - next_offset,
5190                        BITS_PER_UNIT - next_bit);
5191       if (BYTES_BIG_ENDIAN)
5192         {
5193           /* On big-endian machine, take the most significant bits
5194              first (of the bits that are significant)
5195              and put them into bytes from the most significant end.  */
5196           shift = end_offset - next_offset - this_time;
5197
5198           /* Don't try to take a bunch of bits that cross
5199              the word boundary in the INTEGER_CST. We can
5200              only select bits from the LOW or HIGH part
5201              not from both.  */
5202           if (shift < HOST_BITS_PER_WIDE_INT
5203               && shift + this_time > HOST_BITS_PER_WIDE_INT)
5204             {
5205               this_time = shift + this_time - HOST_BITS_PER_WIDE_INT;
5206               shift = HOST_BITS_PER_WIDE_INT;
5207             }
5208
5209           /* Now get the bits from the appropriate constant word.  */
5210           if (shift < HOST_BITS_PER_WIDE_INT)
5211             value = TREE_INT_CST_LOW (local->val);
5212           else
5213             {
5214               gcc_assert (shift < 2 * HOST_BITS_PER_WIDE_INT);
5215               value = TREE_INT_CST_HIGH (local->val);
5216               shift -= HOST_BITS_PER_WIDE_INT;
5217             }
5218
5219           /* Get the result. This works only when:
5220              1 <= this_time <= HOST_BITS_PER_WIDE_INT.  */
5221           local->byte |= (((value >> shift)
5222                            & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
5223                           << (BITS_PER_UNIT - this_time - next_bit));
5224         }
5225       else
5226         {
5227           /* On little-endian machines,
5228              take first the least significant bits of the value
5229              and pack them starting at the least significant
5230              bits of the bytes.  */
5231           shift = next_offset - byte_relative_ebitpos;
5232
5233           /* Don't try to take a bunch of bits that cross
5234              the word boundary in the INTEGER_CST. We can
5235              only select bits from the LOW or HIGH part
5236              not from both.  */
5237           if (shift < HOST_BITS_PER_WIDE_INT
5238               && shift + this_time > HOST_BITS_PER_WIDE_INT)
5239             this_time = (HOST_BITS_PER_WIDE_INT - shift);
5240
5241           /* Now get the bits from the appropriate constant word.  */
5242           if (shift < HOST_BITS_PER_WIDE_INT)
5243             value = TREE_INT_CST_LOW (local->val);
5244           else
5245             {
5246               gcc_assert (shift < 2 * HOST_BITS_PER_WIDE_INT);
5247               value = TREE_INT_CST_HIGH (local->val);
5248               shift -= HOST_BITS_PER_WIDE_INT;
5249             }
5250
5251           /* Get the result. This works only when:
5252              1 <= this_time <= HOST_BITS_PER_WIDE_INT.  */
5253           local->byte |= (((value >> shift)
5254                            & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
5255                           << next_bit);
5256         }
5257
5258       next_offset += this_time;
5259       local->byte_buffer_in_use = true;
5260     }
5261 }
5262
5263 /* Subroutine of output_constant, used for CONSTRUCTORs (aggregate constants).
5264    Generate at least SIZE bytes, padding if necessary.  OUTER designates the
5265    caller output state of relevance in recursive invocations.  */
5266
5267 static unsigned HOST_WIDE_INT
5268 output_constructor (tree exp, unsigned HOST_WIDE_INT size,
5269                     unsigned int align, oc_outer_state * outer)
5270 {
5271   unsigned HOST_WIDE_INT cnt;
5272   constructor_elt *ce;
5273
5274   oc_local_state local;
5275
5276   /* Setup our local state to communicate with helpers.  */
5277   local.exp = exp;
5278   local.size = size;
5279   local.align = align;
5280
5281   local.total_bytes = 0;
5282   local.byte_buffer_in_use = outer != NULL;
5283   local.byte = outer ? outer->byte : 0;
5284
5285   local.type = TREE_TYPE (exp);
5286
5287   local.last_relative_index = -1;
5288
5289   local.min_index = NULL_TREE;
5290   if (TREE_CODE (local.type) == ARRAY_TYPE
5291       && TYPE_DOMAIN (local.type) != NULL_TREE)
5292     local.min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (local.type));
5293
5294   gcc_assert (HOST_BITS_PER_WIDE_INT >= BITS_PER_UNIT);
5295
5296   /* As CE goes through the elements of the constant, FIELD goes through the
5297      structure fields if the constant is a structure.  If the constant is a
5298      union, we override this by getting the field from the TREE_LIST element.
5299      But the constant could also be an array.  Then FIELD is zero.
5300
5301      There is always a maximum of one element in the chain LINK for unions
5302      (even if the initializer in a source program incorrectly contains
5303      more one).  */
5304
5305   local.field = NULL_TREE;
5306   if (TREE_CODE (local.type) == RECORD_TYPE)
5307     local.field = TYPE_FIELDS (local.type);
5308
5309   for (cnt = 0;
5310        VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (exp), cnt, ce);
5311        cnt++, local.field = local.field ? TREE_CHAIN (local.field) : 0)
5312     {
5313       local.val = ce->value;
5314       local.index = NULL_TREE;
5315
5316       /* The element in a union constructor specifies the proper field
5317          or index.  */
5318       if ((TREE_CODE (local.type) == RECORD_TYPE
5319            || TREE_CODE (local.type) == UNION_TYPE
5320            || TREE_CODE (local.type) == QUAL_UNION_TYPE)
5321           && ce->index != NULL_TREE)
5322         local.field = ce->index;
5323
5324       else if (TREE_CODE (local.type) == ARRAY_TYPE)
5325         local.index = ce->index;
5326
5327 #ifdef ASM_COMMENT_START
5328       if (local.field && flag_verbose_asm)
5329         fprintf (asm_out_file, "%s %s:\n",
5330                  ASM_COMMENT_START,
5331                  DECL_NAME (local.field)
5332                  ? IDENTIFIER_POINTER (DECL_NAME (local.field))
5333                  : "<anonymous>");
5334 #endif
5335
5336       /* Eliminate the marker that makes a cast not be an lvalue.  */
5337       if (local.val != NULL_TREE)
5338         STRIP_NOPS (local.val);
5339
5340       /* Output the current element, using the appropriate helper ...  */
5341
5342       /* For an array slice not part of an outer bitfield.  */
5343       if (!outer
5344           && local.index != NULL_TREE
5345           && TREE_CODE (local.index) == RANGE_EXPR)
5346         output_constructor_array_range (&local);
5347
5348       /* For a field that is neither a true bitfield nor part of an outer one,
5349          known to be at least byte aligned and multiple-of-bytes long.  */
5350       else if (!outer
5351                && (local.field == NULL_TREE
5352                    || !CONSTRUCTOR_BITFIELD_P (local.field)))
5353         output_constructor_regular_field (&local);
5354
5355       /* For a true bitfield or part of an outer one.  */
5356       else
5357         output_constructor_bitfield (&local, outer);
5358     }
5359
5360   /* If we are not at toplevel, save the pending data for our caller.
5361      Otherwise output the pending data and padding zeros as needed. */
5362   if (outer)
5363     outer->byte = local.byte;
5364   else
5365     {
5366       if (local.byte_buffer_in_use)
5367         {
5368           assemble_integer (GEN_INT (local.byte), 1, BITS_PER_UNIT, 1);
5369           local.total_bytes++;
5370         }
5371
5372       if ((unsigned HOST_WIDE_INT)local.total_bytes < local.size)
5373         {
5374           assemble_zeros (local.size - local.total_bytes);
5375           local.total_bytes = local.size;
5376         }
5377     }
5378
5379   return local.total_bytes;
5380 }
5381
5382 /* Mark DECL as weak.  */
5383
5384 static void
5385 mark_weak (tree decl)
5386 {
5387   DECL_WEAK (decl) = 1;
5388
5389   if (DECL_RTL_SET_P (decl)
5390       && MEM_P (DECL_RTL (decl))
5391       && XEXP (DECL_RTL (decl), 0)
5392       && GET_CODE (XEXP (DECL_RTL (decl), 0)) == SYMBOL_REF)
5393     SYMBOL_REF_WEAK (XEXP (DECL_RTL (decl), 0)) = 1;
5394 }
5395
5396 /* Merge weak status between NEWDECL and OLDDECL.  */
5397
5398 void
5399 merge_weak (tree newdecl, tree olddecl)
5400 {
5401   if (DECL_WEAK (newdecl) == DECL_WEAK (olddecl))
5402     {
5403       if (DECL_WEAK (newdecl) && SUPPORTS_WEAK)
5404         {
5405           tree *pwd;
5406           /* We put the NEWDECL on the weak_decls list at some point
5407              and OLDDECL as well.  Keep just OLDDECL on the list.  */
5408           for (pwd = &weak_decls; *pwd; pwd = &TREE_CHAIN (*pwd))
5409             if (TREE_VALUE (*pwd) == newdecl)
5410               {
5411                 *pwd = TREE_CHAIN (*pwd);
5412                 break;
5413               }
5414         }
5415       return;
5416     }
5417
5418   if (DECL_WEAK (newdecl))
5419     {
5420       tree wd;
5421
5422       /* NEWDECL is weak, but OLDDECL is not.  */
5423
5424       /* If we already output the OLDDECL, we're in trouble; we can't
5425          go back and make it weak.  This error cannot be caught in
5426          declare_weak because the NEWDECL and OLDDECL was not yet
5427          been merged; therefore, TREE_ASM_WRITTEN was not set.  */
5428       if (TREE_ASM_WRITTEN (olddecl))
5429         error ("weak declaration of %q+D must precede definition",
5430                newdecl);
5431
5432       /* If we've already generated rtl referencing OLDDECL, we may
5433          have done so in a way that will not function properly with
5434          a weak symbol.  */
5435       else if (TREE_USED (olddecl)
5436                && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (olddecl)))
5437         warning (0, "weak declaration of %q+D after first use results "
5438                  "in unspecified behavior", newdecl);
5439
5440       if (SUPPORTS_WEAK)
5441         {
5442           /* We put the NEWDECL on the weak_decls list at some point.
5443              Replace it with the OLDDECL.  */
5444           for (wd = weak_decls; wd; wd = TREE_CHAIN (wd))
5445             if (TREE_VALUE (wd) == newdecl)
5446               {
5447                 TREE_VALUE (wd) = olddecl;
5448                 break;
5449               }
5450           /* We may not find the entry on the list.  If NEWDECL is a
5451              weak alias, then we will have already called
5452              globalize_decl to remove the entry; in that case, we do
5453              not need to do anything.  */
5454         }
5455
5456       /* Make the OLDDECL weak; it's OLDDECL that we'll be keeping.  */
5457       mark_weak (olddecl);
5458     }
5459   else
5460     /* OLDDECL was weak, but NEWDECL was not explicitly marked as
5461        weak.  Just update NEWDECL to indicate that it's weak too.  */
5462     mark_weak (newdecl);
5463 }
5464
5465 /* Declare DECL to be a weak symbol.  */
5466
5467 void
5468 declare_weak (tree decl)
5469 {
5470   if (! TREE_PUBLIC (decl))
5471     error ("weak declaration of %q+D must be public", decl);
5472   else if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
5473     error ("weak declaration of %q+D must precede definition", decl);
5474   else if (!SUPPORTS_WEAK)
5475     warning (0, "weak declaration of %q+D not supported", decl);
5476
5477   mark_weak (decl);
5478   if (!lookup_attribute ("weak", DECL_ATTRIBUTES (decl)))
5479     DECL_ATTRIBUTES (decl)
5480       = tree_cons (get_identifier ("weak"), NULL, DECL_ATTRIBUTES (decl));
5481 }
5482
5483 static void
5484 weak_finish_1 (tree decl)
5485 {
5486 #if defined (ASM_WEAKEN_DECL) || defined (ASM_WEAKEN_LABEL)
5487   const char *const name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5488 #endif
5489
5490   if (! TREE_USED (decl))
5491     return;
5492
5493 #ifdef ASM_WEAKEN_DECL
5494   ASM_WEAKEN_DECL (asm_out_file, decl, name, NULL);
5495 #else
5496 #ifdef ASM_WEAKEN_LABEL
5497   ASM_WEAKEN_LABEL (asm_out_file, name);
5498 #else
5499 #ifdef ASM_OUTPUT_WEAK_ALIAS
5500   {
5501     static bool warn_once = 0;
5502     if (! warn_once)
5503       {
5504         warning (0, "only weak aliases are supported in this configuration");
5505         warn_once = 1;
5506       }
5507     return;
5508   }
5509 #endif
5510 #endif
5511 #endif
5512 }
5513
5514 /* This TREE_LIST contains weakref targets.  */
5515
5516 static GTY(()) tree weakref_targets;
5517
5518 /* Forward declaration.  */
5519 static tree find_decl_and_mark_needed (tree decl, tree target);
5520
5521 /* Emit any pending weak declarations.  */
5522
5523 void
5524 weak_finish (void)
5525 {
5526   tree t;
5527
5528   for (t = weakref_targets; t; t = TREE_CHAIN (t))
5529     {
5530       tree alias_decl = TREE_PURPOSE (t);
5531       tree target = ultimate_transparent_alias_target (&TREE_VALUE (t));
5532
5533       if (! TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (alias_decl)))
5534         /* Remove alias_decl from the weak list, but leave entries for
5535            the target alone.  */
5536         target = NULL_TREE;
5537 #ifndef ASM_OUTPUT_WEAKREF
5538       else if (! TREE_SYMBOL_REFERENCED (target))
5539         {
5540           /* Use ASM_WEAKEN_LABEL only if ASM_WEAKEN_DECL is not
5541              defined, otherwise we and weak_finish_1 would use
5542              different macros.  */
5543 # if defined ASM_WEAKEN_LABEL && ! defined ASM_WEAKEN_DECL
5544           ASM_WEAKEN_LABEL (asm_out_file, IDENTIFIER_POINTER (target));
5545 # else
5546           tree decl = find_decl_and_mark_needed (alias_decl, target);
5547
5548           if (! decl)
5549             {
5550               decl = build_decl (DECL_SOURCE_LOCATION (alias_decl),
5551                                  TREE_CODE (alias_decl), target,
5552                                  TREE_TYPE (alias_decl));
5553
5554               DECL_EXTERNAL (decl) = 1;
5555               TREE_PUBLIC (decl) = 1;
5556               DECL_ARTIFICIAL (decl) = 1;
5557               TREE_NOTHROW (decl) = TREE_NOTHROW (alias_decl);
5558               TREE_USED (decl) = 1;
5559             }
5560
5561           weak_finish_1 (decl);
5562 # endif
5563         }
5564 #endif
5565
5566       {
5567         tree *p;
5568         tree t2;
5569
5570         /* Remove the alias and the target from the pending weak list
5571            so that we do not emit any .weak directives for the former,
5572            nor multiple .weak directives for the latter.  */
5573         for (p = &weak_decls; (t2 = *p) ; )
5574           {
5575             if (TREE_VALUE (t2) == alias_decl
5576                 || target == DECL_ASSEMBLER_NAME (TREE_VALUE (t2)))
5577               *p = TREE_CHAIN (t2);
5578             else
5579               p = &TREE_CHAIN (t2);
5580           }
5581
5582         /* Remove other weakrefs to the same target, to speed things up.  */
5583         for (p = &TREE_CHAIN (t); (t2 = *p) ; )
5584           {
5585             if (target == ultimate_transparent_alias_target (&TREE_VALUE (t2)))
5586               *p = TREE_CHAIN (t2);
5587             else
5588               p = &TREE_CHAIN (t2);
5589           }
5590       }
5591     }
5592
5593   for (t = weak_decls; t; t = TREE_CHAIN (t))
5594     {
5595       tree decl = TREE_VALUE (t);
5596
5597       weak_finish_1 (decl);
5598     }
5599 }
5600
5601 /* Emit the assembly bits to indicate that DECL is globally visible.  */
5602
5603 static void
5604 globalize_decl (tree decl)
5605 {
5606
5607 #if defined (ASM_WEAKEN_LABEL) || defined (ASM_WEAKEN_DECL)
5608   if (DECL_WEAK (decl))
5609     {
5610       const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
5611       tree *p, t;
5612
5613 #ifdef ASM_WEAKEN_DECL
5614       ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
5615 #else
5616       ASM_WEAKEN_LABEL (asm_out_file, name);
5617 #endif
5618
5619       /* Remove this function from the pending weak list so that
5620          we do not emit multiple .weak directives for it.  */
5621       for (p = &weak_decls; (t = *p) ; )
5622         {
5623           if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
5624             *p = TREE_CHAIN (t);
5625           else
5626             p = &TREE_CHAIN (t);
5627         }
5628
5629       /* Remove weakrefs to the same target from the pending weakref
5630          list, for the same reason.  */
5631       for (p = &weakref_targets; (t = *p) ; )
5632         {
5633           if (DECL_ASSEMBLER_NAME (decl)
5634               == ultimate_transparent_alias_target (&TREE_VALUE (t)))
5635             *p = TREE_CHAIN (t);
5636           else
5637             p = &TREE_CHAIN (t);
5638         }
5639
5640       return;
5641     }
5642 #endif
5643
5644   targetm.asm_out.globalize_decl_name (asm_out_file, decl);
5645 }
5646
5647 VEC(alias_pair,gc) *alias_pairs;
5648
5649 /* Given an assembly name, find the decl it is associated with.  At the
5650    same time, mark it needed for cgraph.  */
5651
5652 static tree
5653 find_decl_and_mark_needed (tree decl, tree target)
5654 {
5655   struct cgraph_node *fnode = NULL;
5656   struct varpool_node *vnode = NULL;
5657
5658   if (TREE_CODE (decl) == FUNCTION_DECL)
5659     {
5660       fnode = cgraph_node_for_asm (target);
5661       if (fnode == NULL)
5662         vnode = varpool_node_for_asm (target);
5663     }
5664   else
5665     {
5666       vnode = varpool_node_for_asm (target);
5667       if (vnode == NULL)
5668         fnode = cgraph_node_for_asm (target);
5669     }
5670
5671   if (fnode)
5672     {
5673       cgraph_mark_needed_node (fnode);
5674       return fnode->decl;
5675     }
5676   else if (vnode)
5677     {
5678       varpool_mark_needed_node (vnode);
5679       vnode->force_output = 1;
5680       return vnode->decl;
5681     }
5682   else
5683     return NULL_TREE;
5684 }
5685
5686 /* Output the assembler code for a define (equate) using ASM_OUTPUT_DEF
5687    or ASM_OUTPUT_DEF_FROM_DECLS.  The function defines the symbol whose
5688    tree node is DECL to have the value of the tree node TARGET.  */
5689
5690 static void
5691 do_assemble_alias (tree decl, tree target)
5692 {
5693   if (TREE_ASM_WRITTEN (decl))
5694     return;
5695
5696   /* We must force creation of DECL_RTL for debug info generation, even though
5697      we don't use it here.  */
5698   make_decl_rtl (decl);
5699
5700   TREE_ASM_WRITTEN (decl) = 1;
5701   TREE_ASM_WRITTEN (DECL_ASSEMBLER_NAME (decl)) = 1;
5702
5703   if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
5704     {
5705       ultimate_transparent_alias_target (&target);
5706
5707       if (!targetm.have_tls
5708           && TREE_CODE (decl) == VAR_DECL
5709           && DECL_THREAD_LOCAL_P (decl))
5710         {
5711           decl = emutls_decl (decl);
5712           target = get_emutls_object_name (target);
5713         }
5714
5715       if (!TREE_SYMBOL_REFERENCED (target))
5716         weakref_targets = tree_cons (decl, target, weakref_targets);
5717
5718 #ifdef ASM_OUTPUT_WEAKREF
5719       ASM_OUTPUT_WEAKREF (asm_out_file, decl,
5720                           IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
5721                           IDENTIFIER_POINTER (target));
5722 #else
5723       if (!SUPPORTS_WEAK)
5724         {
5725           error_at (DECL_SOURCE_LOCATION (decl),
5726                     "weakref is not supported in this configuration");
5727           return;
5728         }
5729 #endif
5730       return;
5731     }
5732
5733   if (!targetm.have_tls
5734       && TREE_CODE (decl) == VAR_DECL
5735       && DECL_THREAD_LOCAL_P (decl))
5736     {
5737       decl = emutls_decl (decl);
5738       target = get_emutls_object_name (target);
5739     }
5740
5741 #ifdef ASM_OUTPUT_DEF
5742   /* Make name accessible from other files, if appropriate.  */
5743
5744   if (TREE_PUBLIC (decl))
5745     {
5746       globalize_decl (decl);
5747       maybe_assemble_visibility (decl);
5748     }
5749
5750 # ifdef ASM_OUTPUT_DEF_FROM_DECLS
5751   ASM_OUTPUT_DEF_FROM_DECLS (asm_out_file, decl, target);
5752 # else
5753   ASM_OUTPUT_DEF (asm_out_file,
5754                   IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
5755                   IDENTIFIER_POINTER (target));
5756 # endif
5757 #elif defined (ASM_OUTPUT_WEAK_ALIAS) || defined (ASM_WEAKEN_DECL)
5758   {
5759     const char *name;
5760     tree *p, t;
5761
5762     name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5763 # ifdef ASM_WEAKEN_DECL
5764     ASM_WEAKEN_DECL (asm_out_file, decl, name, IDENTIFIER_POINTER (target));
5765 # else
5766     ASM_OUTPUT_WEAK_ALIAS (asm_out_file, name, IDENTIFIER_POINTER (target));
5767 # endif
5768     /* Remove this function from the pending weak list so that
5769        we do not emit multiple .weak directives for it.  */
5770     for (p = &weak_decls; (t = *p) ; )
5771       if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
5772         *p = TREE_CHAIN (t);
5773       else
5774         p = &TREE_CHAIN (t);
5775
5776     /* Remove weakrefs to the same target from the pending weakref
5777        list, for the same reason.  */
5778     for (p = &weakref_targets; (t = *p) ; )
5779       {
5780         if (DECL_ASSEMBLER_NAME (decl)
5781             == ultimate_transparent_alias_target (&TREE_VALUE (t)))
5782           *p = TREE_CHAIN (t);
5783         else
5784           p = &TREE_CHAIN (t);
5785       }
5786   }
5787 #endif
5788 }
5789
5790
5791 /* Remove the alias pairing for functions that are no longer in the call
5792    graph.  */
5793
5794 void
5795 remove_unreachable_alias_pairs (void)
5796 {
5797   unsigned i;
5798   alias_pair *p;
5799
5800   if (alias_pairs == NULL)
5801     return;
5802
5803   for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); )
5804     {
5805       if (!DECL_EXTERNAL (p->decl))
5806         {
5807           struct cgraph_node *fnode = NULL;
5808           struct varpool_node *vnode = NULL;
5809           fnode = cgraph_node_for_asm (p->target);
5810           vnode = (fnode == NULL) ? varpool_node_for_asm (p->target) : NULL;
5811           if (fnode == NULL && vnode == NULL)
5812             {
5813               VEC_unordered_remove (alias_pair, alias_pairs, i);
5814               continue;
5815             }
5816         }
5817
5818       i++;
5819     }
5820 }
5821
5822
5823 /* First pass of completing pending aliases.  Make sure that cgraph knows
5824    which symbols will be required.  */
5825
5826 void
5827 finish_aliases_1 (void)
5828 {
5829   unsigned i;
5830   alias_pair *p;
5831
5832   for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); i++)
5833     {
5834       tree target_decl;
5835
5836       target_decl = find_decl_and_mark_needed (p->decl, p->target);
5837       if (target_decl == NULL)
5838         {
5839           if (! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
5840             error ("%q+D aliased to undefined symbol %qE",
5841                    p->decl, p->target);
5842         }
5843       else if (DECL_EXTERNAL (target_decl)
5844                /* We use local aliases for C++ thunks to force the tailcall
5845                   to bind locally.  Of course this is a hack - to keep it
5846                   working do the following (which is not strictly correct).  */
5847                && (! TREE_CODE (target_decl) == FUNCTION_DECL
5848                    || ! DECL_VIRTUAL_P (target_decl))
5849                && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
5850         error ("%q+D aliased to external symbol %qE",
5851                p->decl, p->target);
5852     }
5853 }
5854
5855 /* Second pass of completing pending aliases.  Emit the actual assembly.
5856    This happens at the end of compilation and thus it is assured that the
5857    target symbol has been emitted.  */
5858
5859 void
5860 finish_aliases_2 (void)
5861 {
5862   unsigned i;
5863   alias_pair *p;
5864
5865   for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); i++)
5866     do_assemble_alias (p->decl, p->target);
5867
5868   VEC_truncate (alias_pair, alias_pairs, 0);
5869 }
5870
5871 /* Emit an assembler directive to make the symbol for DECL an alias to
5872    the symbol for TARGET.  */
5873
5874 void
5875 assemble_alias (tree decl, tree target)
5876 {
5877   tree target_decl;
5878   bool is_weakref = false;
5879
5880   if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
5881     {
5882       tree alias = DECL_ASSEMBLER_NAME (decl);
5883
5884       is_weakref = true;
5885
5886       ultimate_transparent_alias_target (&target);
5887
5888       if (alias == target)
5889         error ("weakref %q+D ultimately targets itself", decl);
5890       else
5891         {
5892 #ifndef ASM_OUTPUT_WEAKREF
5893           IDENTIFIER_TRANSPARENT_ALIAS (alias) = 1;
5894           TREE_CHAIN (alias) = target;
5895 #endif
5896         }
5897       if (TREE_PUBLIC (decl))
5898         error ("weakref %q+D must have static linkage", decl);
5899     }
5900   else
5901     {
5902 #if !defined (ASM_OUTPUT_DEF)
5903 # if !defined(ASM_OUTPUT_WEAK_ALIAS) && !defined (ASM_WEAKEN_DECL)
5904       error_at (DECL_SOURCE_LOCATION (decl),
5905                 "alias definitions not supported in this configuration");
5906       return;
5907 # else
5908       if (!DECL_WEAK (decl))
5909         {
5910           error_at (DECL_SOURCE_LOCATION (decl),
5911                     "only weak aliases are supported in this configuration");
5912           return;
5913         }
5914 # endif
5915 #endif
5916     }
5917   TREE_USED (decl) = 1;
5918
5919   /* A quirk of the initial implementation of aliases required that the user
5920      add "extern" to all of them.  Which is silly, but now historical.  Do
5921      note that the symbol is in fact locally defined.  */
5922   if (! is_weakref)
5923     DECL_EXTERNAL (decl) = 0;
5924
5925   /* Allow aliases to aliases.  */
5926   if (TREE_CODE (decl) == FUNCTION_DECL)
5927     cgraph_node (decl)->alias = true;
5928   else
5929     varpool_node (decl)->alias = true;
5930
5931   /* If the target has already been emitted, we don't have to queue the
5932      alias.  This saves a tad of memory.  */
5933   if (cgraph_global_info_ready)
5934     target_decl = find_decl_and_mark_needed (decl, target);
5935   else
5936     target_decl= NULL;
5937   if (target_decl && TREE_ASM_WRITTEN (target_decl))
5938     do_assemble_alias (decl, target);
5939   else
5940     {
5941       alias_pair *p = VEC_safe_push (alias_pair, gc, alias_pairs, NULL);
5942       p->decl = decl;
5943       p->target = target;
5944     }
5945 }
5946
5947 /* Emit an assembler directive to set symbol for DECL visibility to
5948    the visibility type VIS, which must not be VISIBILITY_DEFAULT.  */
5949
5950 void
5951 default_assemble_visibility (tree decl ATTRIBUTE_UNUSED, 
5952                              int vis ATTRIBUTE_UNUSED)
5953 {
5954 #ifdef HAVE_GAS_HIDDEN
5955   static const char * const visibility_types[] = {
5956     NULL, "protected", "hidden", "internal"
5957   };
5958
5959   const char *name, *type;
5960
5961   name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5962   type = visibility_types[vis];
5963
5964   fprintf (asm_out_file, "\t.%s\t", type);
5965   assemble_name (asm_out_file, name);
5966   fprintf (asm_out_file, "\n");
5967 #else
5968   warning (OPT_Wattributes, "visibility attribute not supported "
5969            "in this configuration; ignored");
5970 #endif
5971 }
5972
5973 /* A helper function to call assemble_visibility when needed for a decl.  */
5974
5975 int
5976 maybe_assemble_visibility (tree decl)
5977 {
5978   enum symbol_visibility vis = DECL_VISIBILITY (decl);
5979
5980   if (vis != VISIBILITY_DEFAULT)
5981     {
5982       targetm.asm_out.assemble_visibility (decl, vis);
5983       return 1;
5984     }
5985   else
5986     return 0;
5987 }
5988
5989 /* Returns 1 if the target configuration supports defining public symbols
5990    so that one of them will be chosen at link time instead of generating a
5991    multiply-defined symbol error, whether through the use of weak symbols or
5992    a target-specific mechanism for having duplicates discarded.  */
5993
5994 int
5995 supports_one_only (void)
5996 {
5997   if (SUPPORTS_ONE_ONLY)
5998     return 1;
5999   return SUPPORTS_WEAK;
6000 }
6001
6002 /* Set up DECL as a public symbol that can be defined in multiple
6003    translation units without generating a linker error.  */
6004
6005 void
6006 make_decl_one_only (tree decl, tree comdat_group)
6007 {
6008   gcc_assert (TREE_CODE (decl) == VAR_DECL
6009               || TREE_CODE (decl) == FUNCTION_DECL);
6010
6011   TREE_PUBLIC (decl) = 1;
6012
6013   if (SUPPORTS_ONE_ONLY)
6014     {
6015 #ifdef MAKE_DECL_ONE_ONLY
6016       MAKE_DECL_ONE_ONLY (decl);
6017 #endif
6018       DECL_COMDAT_GROUP (decl) = comdat_group;
6019     }
6020   else if (TREE_CODE (decl) == VAR_DECL
6021       && (DECL_INITIAL (decl) == 0 || DECL_INITIAL (decl) == error_mark_node))
6022     DECL_COMMON (decl) = 1;
6023   else
6024     {
6025       gcc_assert (SUPPORTS_WEAK);
6026       DECL_WEAK (decl) = 1;
6027     }
6028 }
6029
6030 void
6031 init_varasm_once (void)
6032 {
6033   section_htab = htab_create_ggc (31, section_entry_hash,
6034                                   section_entry_eq, NULL);
6035   object_block_htab = htab_create_ggc (31, object_block_entry_hash,
6036                                        object_block_entry_eq, NULL);
6037   const_desc_htab = htab_create_ggc (1009, const_desc_hash,
6038                                      const_desc_eq, NULL);
6039
6040   const_alias_set = new_alias_set ();
6041   shared_constant_pool = create_constant_pool ();
6042
6043 #ifdef TEXT_SECTION_ASM_OP
6044   text_section = get_unnamed_section (SECTION_CODE, output_section_asm_op,
6045                                       TEXT_SECTION_ASM_OP);
6046 #endif
6047
6048 #ifdef DATA_SECTION_ASM_OP
6049   data_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
6050                                       DATA_SECTION_ASM_OP);
6051 #endif
6052
6053 #ifdef SDATA_SECTION_ASM_OP
6054   sdata_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
6055                                        SDATA_SECTION_ASM_OP);
6056 #endif
6057
6058 #ifdef READONLY_DATA_SECTION_ASM_OP
6059   readonly_data_section = get_unnamed_section (0, output_section_asm_op,
6060                                                READONLY_DATA_SECTION_ASM_OP);
6061 #endif
6062
6063 #ifdef CTORS_SECTION_ASM_OP
6064   ctors_section = get_unnamed_section (0, output_section_asm_op,
6065                                        CTORS_SECTION_ASM_OP);
6066 #endif
6067
6068 #ifdef DTORS_SECTION_ASM_OP
6069   dtors_section = get_unnamed_section (0, output_section_asm_op,
6070                                        DTORS_SECTION_ASM_OP);
6071 #endif
6072
6073 #ifdef BSS_SECTION_ASM_OP
6074   bss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
6075                                      output_section_asm_op,
6076                                      BSS_SECTION_ASM_OP);
6077 #endif
6078
6079 #ifdef SBSS_SECTION_ASM_OP
6080   sbss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
6081                                       output_section_asm_op,
6082                                       SBSS_SECTION_ASM_OP);
6083 #endif
6084
6085   tls_comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6086                                            | SECTION_COMMON, emit_tls_common);
6087   lcomm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6088                                         | SECTION_COMMON, emit_local);
6089   comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6090                                        | SECTION_COMMON, emit_common);
6091
6092 #if defined ASM_OUTPUT_ALIGNED_BSS || defined ASM_OUTPUT_BSS
6093   bss_noswitch_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS,
6094                                                emit_bss);
6095 #endif
6096
6097   targetm.asm_out.init_sections ();
6098
6099   if (readonly_data_section == NULL)
6100     readonly_data_section = text_section;
6101 }
6102
6103 enum tls_model
6104 decl_default_tls_model (const_tree decl)
6105 {
6106   enum tls_model kind;
6107   bool is_local;
6108
6109   is_local = targetm.binds_local_p (decl);
6110   if (!flag_shlib)
6111     {
6112       if (is_local)
6113         kind = TLS_MODEL_LOCAL_EXEC;
6114       else
6115         kind = TLS_MODEL_INITIAL_EXEC;
6116     }
6117
6118   /* Local dynamic is inefficient when we're not combining the
6119      parts of the address.  */
6120   else if (optimize && is_local)
6121     kind = TLS_MODEL_LOCAL_DYNAMIC;
6122   else
6123     kind = TLS_MODEL_GLOBAL_DYNAMIC;
6124   if (kind < flag_tls_default)
6125     kind = flag_tls_default;
6126
6127   return kind;
6128 }
6129
6130 /* Select a set of attributes for section NAME based on the properties
6131    of DECL and whether or not RELOC indicates that DECL's initializer
6132    might contain runtime relocations.
6133
6134    We make the section read-only and executable for a function decl,
6135    read-only for a const data decl, and writable for a non-const data decl.  */
6136
6137 unsigned int
6138 default_section_type_flags (tree decl, const char *name, int reloc)
6139 {
6140   unsigned int flags;
6141
6142   if (decl && TREE_CODE (decl) == FUNCTION_DECL)
6143     flags = SECTION_CODE;
6144   else if (decl && decl_readonly_section (decl, reloc))
6145     flags = 0;
6146   else if (current_function_decl
6147            && cfun
6148            && crtl->subsections.unlikely_text_section_name
6149            && strcmp (name, crtl->subsections.unlikely_text_section_name) == 0)
6150     flags = SECTION_CODE;
6151   else if (!decl
6152            && (!current_function_decl || !cfun)
6153            && strcmp (name, UNLIKELY_EXECUTED_TEXT_SECTION_NAME) == 0)
6154     flags = SECTION_CODE;
6155   else
6156     flags = SECTION_WRITE;
6157
6158   if (decl && DECL_ONE_ONLY (decl))
6159     flags |= SECTION_LINKONCE;
6160
6161   if (decl && TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
6162     flags |= SECTION_TLS | SECTION_WRITE;
6163
6164   if (strcmp (name, ".bss") == 0
6165       || strncmp (name, ".bss.", 5) == 0
6166       || strncmp (name, ".gnu.linkonce.b.", 16) == 0
6167       || strcmp (name, ".sbss") == 0
6168       || strncmp (name, ".sbss.", 6) == 0
6169       || strncmp (name, ".gnu.linkonce.sb.", 17) == 0)
6170     flags |= SECTION_BSS;
6171
6172   if (strcmp (name, ".tdata") == 0
6173       || strncmp (name, ".tdata.", 7) == 0
6174       || strncmp (name, ".gnu.linkonce.td.", 17) == 0)
6175     flags |= SECTION_TLS;
6176
6177   if (strcmp (name, ".tbss") == 0
6178       || strncmp (name, ".tbss.", 6) == 0
6179       || strncmp (name, ".gnu.linkonce.tb.", 17) == 0)
6180     flags |= SECTION_TLS | SECTION_BSS;
6181
6182   /* These three sections have special ELF types.  They are neither
6183      SHT_PROGBITS nor SHT_NOBITS, so when changing sections we don't
6184      want to print a section type (@progbits or @nobits).  If someone
6185      is silly enough to emit code or TLS variables to one of these
6186      sections, then don't handle them specially.  */
6187   if (!(flags & (SECTION_CODE | SECTION_BSS | SECTION_TLS))
6188       && (strcmp (name, ".init_array") == 0
6189           || strcmp (name, ".fini_array") == 0
6190           || strcmp (name, ".preinit_array") == 0))
6191     flags |= SECTION_NOTYPE;
6192
6193   return flags;
6194 }
6195
6196 /* Return true if the target supports some form of global BSS,
6197    either through bss_noswitch_section, or by selecting a BSS
6198    section in TARGET_ASM_SELECT_SECTION.  */
6199
6200 bool
6201 have_global_bss_p (void)
6202 {
6203   return bss_noswitch_section || targetm.have_switchable_bss_sections;
6204 }
6205
6206 /* Output assembly to switch to section NAME with attribute FLAGS.
6207    Four variants for common object file formats.  */
6208
6209 void
6210 default_no_named_section (const char *name ATTRIBUTE_UNUSED,
6211                           unsigned int flags ATTRIBUTE_UNUSED,
6212                           tree decl ATTRIBUTE_UNUSED)
6213 {
6214   /* Some object formats don't support named sections at all.  The
6215      front-end should already have flagged this as an error.  */
6216   gcc_unreachable ();
6217 }
6218
6219 #ifndef TLS_SECTION_ASM_FLAG
6220 #define TLS_SECTION_ASM_FLAG 'T'
6221 #endif
6222
6223 void
6224 default_elf_asm_named_section (const char *name, unsigned int flags,
6225                                tree decl ATTRIBUTE_UNUSED)
6226 {
6227   char flagchars[10], *f = flagchars;
6228
6229   /* If we have already declared this section, we can use an
6230      abbreviated form to switch back to it -- unless this section is
6231      part of a COMDAT groups, in which case GAS requires the full
6232      declaration every time.  */
6233   if (!(HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6234       && (flags & SECTION_DECLARED))
6235     {
6236       fprintf (asm_out_file, "\t.section\t%s\n", name);
6237       return;
6238     }
6239
6240   if (!(flags & SECTION_DEBUG))
6241     *f++ = 'a';
6242   if (flags & SECTION_WRITE)
6243     *f++ = 'w';
6244   if (flags & SECTION_CODE)
6245     *f++ = 'x';
6246   if (flags & SECTION_SMALL)
6247     *f++ = 's';
6248   if (flags & SECTION_MERGE)
6249     *f++ = 'M';
6250   if (flags & SECTION_STRINGS)
6251     *f++ = 'S';
6252   if (flags & SECTION_TLS)
6253     *f++ = TLS_SECTION_ASM_FLAG;
6254   if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6255     *f++ = 'G';
6256   *f = '\0';
6257
6258   fprintf (asm_out_file, "\t.section\t%s,\"%s\"", name, flagchars);
6259
6260   if (!(flags & SECTION_NOTYPE))
6261     {
6262       const char *type;
6263       const char *format;
6264
6265       if (flags & SECTION_BSS)
6266         type = "nobits";
6267       else
6268         type = "progbits";
6269
6270       format = ",@%s";
6271 #ifdef ASM_COMMENT_START
6272       /* On platforms that use "@" as the assembly comment character,
6273          use "%" instead.  */
6274       if (strcmp (ASM_COMMENT_START, "@") == 0)
6275         format = ",%%%s";
6276 #endif
6277       fprintf (asm_out_file, format, type);
6278
6279       if (flags & SECTION_ENTSIZE)
6280         fprintf (asm_out_file, ",%d", flags & SECTION_ENTSIZE);
6281       if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6282         {
6283           if (TREE_CODE (decl) == IDENTIFIER_NODE)
6284             fprintf (asm_out_file, ",%s,comdat", IDENTIFIER_POINTER (decl));
6285           else
6286             fprintf (asm_out_file, ",%s,comdat",
6287                      IDENTIFIER_POINTER (DECL_COMDAT_GROUP (decl)));
6288         }
6289     }
6290
6291   putc ('\n', asm_out_file);
6292 }
6293
6294 void
6295 default_coff_asm_named_section (const char *name, unsigned int flags,
6296                                 tree decl ATTRIBUTE_UNUSED)
6297 {
6298   char flagchars[8], *f = flagchars;
6299
6300   if (flags & SECTION_WRITE)
6301     *f++ = 'w';
6302   if (flags & SECTION_CODE)
6303     *f++ = 'x';
6304   *f = '\0';
6305
6306   fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
6307 }
6308
6309 void
6310 default_pe_asm_named_section (const char *name, unsigned int flags,
6311                               tree decl)
6312 {
6313   default_coff_asm_named_section (name, flags, decl);
6314
6315   if (flags & SECTION_LINKONCE)
6316     {
6317       /* Functions may have been compiled at various levels of
6318          optimization so we can't use `same_size' here.
6319          Instead, have the linker pick one.  */
6320       fprintf (asm_out_file, "\t.linkonce %s\n",
6321                (flags & SECTION_CODE ? "discard" : "same_size"));
6322     }
6323 }
6324 \f
6325 /* The lame default section selector.  */
6326
6327 section *
6328 default_select_section (tree decl, int reloc,
6329                         unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
6330 {
6331   if (DECL_P (decl))
6332     {
6333       if (decl_readonly_section (decl, reloc))
6334         return readonly_data_section;
6335     }
6336   else if (TREE_CODE (decl) == CONSTRUCTOR)
6337     {
6338       if (! ((flag_pic && reloc)
6339              || !TREE_READONLY (decl)
6340              || TREE_SIDE_EFFECTS (decl)
6341              || !TREE_CONSTANT (decl)))
6342         return readonly_data_section;
6343     }
6344   else if (TREE_CODE (decl) == STRING_CST)
6345     return readonly_data_section;
6346   else if (! (flag_pic && reloc))
6347     return readonly_data_section;
6348
6349   return data_section;
6350 }
6351
6352 enum section_category
6353 categorize_decl_for_section (const_tree decl, int reloc)
6354 {
6355   enum section_category ret;
6356
6357   if (TREE_CODE (decl) == FUNCTION_DECL)
6358     return SECCAT_TEXT;
6359   else if (TREE_CODE (decl) == STRING_CST)
6360     {
6361       if (flag_mudflap) /* or !flag_merge_constants */
6362         return SECCAT_RODATA;
6363       else
6364         return SECCAT_RODATA_MERGE_STR;
6365     }
6366   else if (TREE_CODE (decl) == VAR_DECL)
6367     {
6368       if (bss_initializer_p (decl))
6369         ret = SECCAT_BSS;
6370       else if (! TREE_READONLY (decl)
6371                || TREE_SIDE_EFFECTS (decl)
6372                || ! TREE_CONSTANT (DECL_INITIAL (decl)))
6373         {
6374           /* Here the reloc_rw_mask is not testing whether the section should
6375              be read-only or not, but whether the dynamic link will have to
6376              do something.  If so, we wish to segregate the data in order to
6377              minimize cache misses inside the dynamic linker.  */
6378           if (reloc & targetm.asm_out.reloc_rw_mask ())
6379             ret = reloc == 1 ? SECCAT_DATA_REL_LOCAL : SECCAT_DATA_REL;
6380           else
6381             ret = SECCAT_DATA;
6382         }
6383       else if (reloc & targetm.asm_out.reloc_rw_mask ())
6384         ret = reloc == 1 ? SECCAT_DATA_REL_RO_LOCAL : SECCAT_DATA_REL_RO;
6385       else if (reloc || flag_merge_constants < 2)
6386         /* C and C++ don't allow different variables to share the same
6387            location.  -fmerge-all-constants allows even that (at the
6388            expense of not conforming).  */
6389         ret = SECCAT_RODATA;
6390       else if (TREE_CODE (DECL_INITIAL (decl)) == STRING_CST)
6391         ret = SECCAT_RODATA_MERGE_STR_INIT;
6392       else
6393         ret = SECCAT_RODATA_MERGE_CONST;
6394     }
6395   else if (TREE_CODE (decl) == CONSTRUCTOR)
6396     {
6397       if ((reloc & targetm.asm_out.reloc_rw_mask ())
6398           || TREE_SIDE_EFFECTS (decl)
6399           || ! TREE_CONSTANT (decl))
6400         ret = SECCAT_DATA;
6401       else
6402         ret = SECCAT_RODATA;
6403     }
6404   else
6405     ret = SECCAT_RODATA;
6406
6407   /* There are no read-only thread-local sections.  */
6408   if (TREE_CODE (decl) == VAR_DECL && DECL_TLS_MODEL (decl))
6409     {
6410       if (DECL_TLS_MODEL (decl) == TLS_MODEL_EMULATED)
6411         {
6412           if (DECL_EMUTLS_VAR_P (decl))
6413             {
6414               if (targetm.emutls.var_section)
6415                 ret = SECCAT_EMUTLS_VAR;
6416             }
6417           else
6418             {
6419               if (targetm.emutls.tmpl_prefix)
6420                 ret = SECCAT_EMUTLS_TMPL;
6421             }
6422         }
6423       /* Note that this would be *just* SECCAT_BSS, except that there's
6424          no concept of a read-only thread-local-data section.  */
6425       else if (ret == SECCAT_BSS
6426                || (flag_zero_initialized_in_bss
6427                    && initializer_zerop (DECL_INITIAL (decl))))
6428         ret = SECCAT_TBSS;
6429       else
6430         ret = SECCAT_TDATA;
6431     }
6432
6433   /* If the target uses small data sections, select it.  */
6434   else if (targetm.in_small_data_p (decl))
6435     {
6436       if (ret == SECCAT_BSS)
6437         ret = SECCAT_SBSS;
6438       else if (targetm.have_srodata_section && ret == SECCAT_RODATA)
6439         ret = SECCAT_SRODATA;
6440       else
6441         ret = SECCAT_SDATA;
6442     }
6443
6444   return ret;
6445 }
6446
6447 bool
6448 decl_readonly_section (const_tree decl, int reloc)
6449 {
6450   switch (categorize_decl_for_section (decl, reloc))
6451     {
6452     case SECCAT_RODATA:
6453     case SECCAT_RODATA_MERGE_STR:
6454     case SECCAT_RODATA_MERGE_STR_INIT:
6455     case SECCAT_RODATA_MERGE_CONST:
6456     case SECCAT_SRODATA:
6457       return true;
6458       break;
6459     default:
6460       return false;
6461       break;
6462     }
6463 }
6464
6465 /* Select a section based on the above categorization.  */
6466
6467 section *
6468 default_elf_select_section (tree decl, int reloc,
6469                             unsigned HOST_WIDE_INT align)
6470 {
6471   const char *sname;
6472   switch (categorize_decl_for_section (decl, reloc))
6473     {
6474     case SECCAT_TEXT:
6475       /* We're not supposed to be called on FUNCTION_DECLs.  */
6476       gcc_unreachable ();
6477     case SECCAT_RODATA:
6478       return readonly_data_section;
6479     case SECCAT_RODATA_MERGE_STR:
6480       return mergeable_string_section (decl, align, 0);
6481     case SECCAT_RODATA_MERGE_STR_INIT:
6482       return mergeable_string_section (DECL_INITIAL (decl), align, 0);
6483     case SECCAT_RODATA_MERGE_CONST:
6484       return mergeable_constant_section (DECL_MODE (decl), align, 0);
6485     case SECCAT_SRODATA:
6486       sname = ".sdata2";
6487       break;
6488     case SECCAT_DATA:
6489       return data_section;
6490     case SECCAT_DATA_REL:
6491       sname = ".data.rel";
6492       break;
6493     case SECCAT_DATA_REL_LOCAL:
6494       sname = ".data.rel.local";
6495       break;
6496     case SECCAT_DATA_REL_RO:
6497       sname = ".data.rel.ro";
6498       break;
6499     case SECCAT_DATA_REL_RO_LOCAL:
6500       sname = ".data.rel.ro.local";
6501       break;
6502     case SECCAT_SDATA:
6503       sname = ".sdata";
6504       break;
6505     case SECCAT_TDATA:
6506       sname = ".tdata";
6507       break;
6508     case SECCAT_BSS:
6509       if (bss_section)
6510         return bss_section;
6511       sname = ".bss";
6512       break;
6513     case SECCAT_SBSS:
6514       sname = ".sbss";
6515       break;
6516     case SECCAT_TBSS:
6517       sname = ".tbss";
6518       break;
6519     case SECCAT_EMUTLS_VAR:
6520       sname = targetm.emutls.var_section;
6521       break;
6522     case SECCAT_EMUTLS_TMPL:
6523       sname = targetm.emutls.tmpl_section;
6524       break;
6525     default:
6526       gcc_unreachable ();
6527     }
6528
6529   if (!DECL_P (decl))
6530     decl = NULL_TREE;
6531   return get_named_section (decl, sname, reloc);
6532 }
6533
6534 /* Construct a unique section name based on the decl name and the
6535    categorization performed above.  */
6536
6537 void
6538 default_unique_section (tree decl, int reloc)
6539 {
6540   /* We only need to use .gnu.linkonce if we don't have COMDAT groups.  */
6541   bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
6542   const char *prefix, *name, *linkonce;
6543   char *string;
6544
6545   switch (categorize_decl_for_section (decl, reloc))
6546     {
6547     case SECCAT_TEXT:
6548       prefix = one_only ? ".t" : ".text";
6549       break;
6550     case SECCAT_RODATA:
6551     case SECCAT_RODATA_MERGE_STR:
6552     case SECCAT_RODATA_MERGE_STR_INIT:
6553     case SECCAT_RODATA_MERGE_CONST:
6554       prefix = one_only ? ".r" : ".rodata";
6555       break;
6556     case SECCAT_SRODATA:
6557       prefix = one_only ? ".s2" : ".sdata2";
6558       break;
6559     case SECCAT_DATA:
6560       prefix = one_only ? ".d" : ".data";
6561       break;
6562     case SECCAT_DATA_REL:
6563       prefix = one_only ? ".d.rel" : ".data.rel";
6564       break;
6565     case SECCAT_DATA_REL_LOCAL:
6566       prefix = one_only ? ".d.rel.local" : ".data.rel.local";
6567       break;
6568     case SECCAT_DATA_REL_RO:
6569       prefix = one_only ? ".d.rel.ro" : ".data.rel.ro";
6570       break;
6571     case SECCAT_DATA_REL_RO_LOCAL:
6572       prefix = one_only ? ".d.rel.ro.local" : ".data.rel.ro.local";
6573       break;
6574     case SECCAT_SDATA:
6575       prefix = one_only ? ".s" : ".sdata";
6576       break;
6577     case SECCAT_BSS:
6578       prefix = one_only ? ".b" : ".bss";
6579       break;
6580     case SECCAT_SBSS:
6581       prefix = one_only ? ".sb" : ".sbss";
6582       break;
6583     case SECCAT_TDATA:
6584       prefix = one_only ? ".td" : ".tdata";
6585       break;
6586     case SECCAT_TBSS:
6587       prefix = one_only ? ".tb" : ".tbss";
6588       break;
6589     case SECCAT_EMUTLS_VAR:
6590       prefix = targetm.emutls.var_section;
6591       break;
6592     case SECCAT_EMUTLS_TMPL:
6593       prefix = targetm.emutls.tmpl_section;
6594       break;
6595     default:
6596       gcc_unreachable ();
6597     }
6598
6599   name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
6600   name = targetm.strip_name_encoding (name);
6601
6602   /* If we're using one_only, then there needs to be a .gnu.linkonce
6603      prefix to the section name.  */
6604   linkonce = one_only ? ".gnu.linkonce" : "";
6605
6606   string = ACONCAT ((linkonce, prefix, ".", name, NULL));
6607
6608   DECL_SECTION_NAME (decl) = build_string (strlen (string), string);
6609 }
6610
6611 /* Like compute_reloc_for_constant, except for an RTX.  The return value
6612    is a mask for which bit 1 indicates a global relocation, and bit 0
6613    indicates a local relocation.  */
6614
6615 static int
6616 compute_reloc_for_rtx_1 (rtx *xp, void *data)
6617 {
6618   int *preloc = (int *) data;
6619   rtx x = *xp;
6620
6621   switch (GET_CODE (x))
6622     {
6623     case SYMBOL_REF:
6624       *preloc |= SYMBOL_REF_LOCAL_P (x) ? 1 : 2;
6625       break;
6626     case LABEL_REF:
6627       *preloc |= 1;
6628       break;
6629     default:
6630       break;
6631     }
6632
6633   return 0;
6634 }
6635
6636 static int
6637 compute_reloc_for_rtx (rtx x)
6638 {
6639   int reloc;
6640
6641   switch (GET_CODE (x))
6642     {
6643     case CONST:
6644     case SYMBOL_REF:
6645     case LABEL_REF:
6646       reloc = 0;
6647       for_each_rtx (&x, compute_reloc_for_rtx_1, &reloc);
6648       return reloc;
6649
6650     default:
6651       return 0;
6652     }
6653 }
6654
6655 section *
6656 default_select_rtx_section (enum machine_mode mode ATTRIBUTE_UNUSED,
6657                             rtx x,
6658                             unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
6659 {
6660   if (compute_reloc_for_rtx (x) & targetm.asm_out.reloc_rw_mask ())
6661     return data_section;
6662   else
6663     return readonly_data_section;
6664 }
6665
6666 section *
6667 default_elf_select_rtx_section (enum machine_mode mode, rtx x,
6668                                 unsigned HOST_WIDE_INT align)
6669 {
6670   int reloc = compute_reloc_for_rtx (x);
6671
6672   /* ??? Handle small data here somehow.  */
6673
6674   if (reloc & targetm.asm_out.reloc_rw_mask ())
6675     {
6676       if (reloc == 1)
6677         return get_named_section (NULL, ".data.rel.ro.local", 1);
6678       else
6679         return get_named_section (NULL, ".data.rel.ro", 3);
6680     }
6681
6682   return mergeable_constant_section (mode, align, 0);
6683 }
6684
6685 /* Set the generally applicable flags on the SYMBOL_REF for EXP.  */
6686
6687 void
6688 default_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
6689 {
6690   rtx symbol;
6691   int flags;
6692
6693   /* Careful not to prod global register variables.  */
6694   if (!MEM_P (rtl))
6695     return;
6696   symbol = XEXP (rtl, 0);
6697   if (GET_CODE (symbol) != SYMBOL_REF)
6698     return;
6699
6700   flags = SYMBOL_REF_FLAGS (symbol) & SYMBOL_FLAG_HAS_BLOCK_INFO;
6701   if (TREE_CODE (decl) == FUNCTION_DECL)
6702     flags |= SYMBOL_FLAG_FUNCTION;
6703   if (targetm.binds_local_p (decl))
6704     flags |= SYMBOL_FLAG_LOCAL;
6705   if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl)
6706       && DECL_TLS_MODEL (decl) != TLS_MODEL_EMULATED)
6707     flags |= DECL_TLS_MODEL (decl) << SYMBOL_FLAG_TLS_SHIFT;
6708   else if (targetm.in_small_data_p (decl))
6709     flags |= SYMBOL_FLAG_SMALL;
6710   /* ??? Why is DECL_EXTERNAL ever set for non-PUBLIC names?  Without
6711      being PUBLIC, the thing *must* be defined in this translation unit.
6712      Prevent this buglet from being propagated into rtl code as well.  */
6713   if (DECL_P (decl) && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl))
6714     flags |= SYMBOL_FLAG_EXTERNAL;
6715
6716   SYMBOL_REF_FLAGS (symbol) = flags;
6717 }
6718
6719 /* By default, we do nothing for encode_section_info, so we need not
6720    do anything but discard the '*' marker.  */
6721
6722 const char *
6723 default_strip_name_encoding (const char *str)
6724 {
6725   return str + (*str == '*');
6726 }
6727
6728 #ifdef ASM_OUTPUT_DEF
6729 /* The default implementation of TARGET_ASM_OUTPUT_ANCHOR.  Define the
6730    anchor relative to ".", the current section position.  */
6731
6732 void
6733 default_asm_output_anchor (rtx symbol)
6734 {
6735   char buffer[100];
6736
6737   sprintf (buffer, "*. + " HOST_WIDE_INT_PRINT_DEC,
6738            SYMBOL_REF_BLOCK_OFFSET (symbol));
6739   ASM_OUTPUT_DEF (asm_out_file, XSTR (symbol, 0), buffer);
6740 }
6741 #endif
6742
6743 /* The default implementation of TARGET_USE_ANCHORS_FOR_SYMBOL_P.  */
6744
6745 bool
6746 default_use_anchors_for_symbol_p (const_rtx symbol)
6747 {
6748   section *sect;
6749   tree decl;
6750
6751   /* Don't use anchors for mergeable sections.  The linker might move
6752      the objects around.  */
6753   sect = SYMBOL_REF_BLOCK (symbol)->sect;
6754   if (sect->common.flags & SECTION_MERGE)
6755     return false;
6756
6757   /* Don't use anchors for small data sections.  The small data register
6758      acts as an anchor for such sections.  */
6759   if (sect->common.flags & SECTION_SMALL)
6760     return false;
6761
6762   decl = SYMBOL_REF_DECL (symbol);
6763   if (decl && DECL_P (decl))
6764     {
6765       /* Don't use section anchors for decls that might be defined by
6766          other modules.  */
6767       if (!targetm.binds_local_p (decl))
6768         return false;
6769
6770       /* Don't use section anchors for decls that will be placed in a
6771          small data section.  */
6772       /* ??? Ideally, this check would be redundant with the SECTION_SMALL
6773          one above.  The problem is that we only use SECTION_SMALL for
6774          sections that should be marked as small in the section directive.  */
6775       if (targetm.in_small_data_p (decl))
6776         return false;
6777     }
6778   return true;
6779 }
6780
6781 /* Assume ELF-ish defaults, since that's pretty much the most liberal
6782    wrt cross-module name binding.  */
6783
6784 bool
6785 default_binds_local_p (const_tree exp)
6786 {
6787   return default_binds_local_p_1 (exp, flag_shlib);
6788 }
6789
6790 bool
6791 default_binds_local_p_1 (const_tree exp, int shlib)
6792 {
6793   bool local_p;
6794
6795   /* A non-decl is an entry in the constant pool.  */
6796   if (!DECL_P (exp))
6797     local_p = true;
6798   /* Weakrefs may not bind locally, even though the weakref itself is
6799      always static and therefore local.  */
6800   else if (lookup_attribute ("weakref", DECL_ATTRIBUTES (exp)))
6801     local_p = false;
6802   /* Static variables are always local.  */
6803   else if (! TREE_PUBLIC (exp))
6804     local_p = true;
6805   /* A variable is local if the user has said explicitly that it will
6806      be.  */
6807   else if (DECL_VISIBILITY_SPECIFIED (exp)
6808            && DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
6809     local_p = true;
6810   /* Variables defined outside this object might not be local.  */
6811   else if (DECL_EXTERNAL (exp))
6812     local_p = false;
6813   /* If defined in this object and visibility is not default, must be
6814      local.  */
6815   else if (DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
6816     local_p = true;
6817   /* Default visibility weak data can be overridden by a strong symbol
6818      in another module and so are not local.  */
6819   else if (DECL_WEAK (exp))
6820     local_p = false;
6821   /* If PIC, then assume that any global name can be overridden by
6822      symbols resolved from other modules, unless we are compiling with
6823      -fwhole-program, which assumes that names are local.  */
6824   else if (shlib)
6825     local_p = flag_whole_program;
6826   /* Uninitialized COMMON variable may be unified with symbols
6827      resolved from other modules.  */
6828   else if (DECL_COMMON (exp)
6829            && (DECL_INITIAL (exp) == NULL
6830                || DECL_INITIAL (exp) == error_mark_node))
6831     local_p = false;
6832   /* Otherwise we're left with initialized (or non-common) global data
6833      which is of necessity defined locally.  */
6834   else
6835     local_p = true;
6836
6837   return local_p;
6838 }
6839
6840 /* Default function to output code that will globalize a label.  A
6841    target must define GLOBAL_ASM_OP or provide its own function to
6842    globalize a label.  */
6843 #ifdef GLOBAL_ASM_OP
6844 void
6845 default_globalize_label (FILE * stream, const char *name)
6846 {
6847   fputs (GLOBAL_ASM_OP, stream);
6848   assemble_name (stream, name);
6849   putc ('\n', stream);
6850 }
6851 #endif /* GLOBAL_ASM_OP */
6852
6853 /* Default function to output code that will globalize a declaration.  */
6854 void
6855 default_globalize_decl_name (FILE * stream, tree decl)
6856 {
6857   const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
6858   targetm.asm_out.globalize_label (stream, name);
6859 }
6860
6861 /* Default function to output a label for unwind information.  The
6862    default is to do nothing.  A target that needs nonlocal labels for
6863    unwind information must provide its own function to do this.  */
6864 void
6865 default_emit_unwind_label (FILE * stream ATTRIBUTE_UNUSED,
6866                            tree decl ATTRIBUTE_UNUSED,
6867                            int for_eh ATTRIBUTE_UNUSED,
6868                            int empty ATTRIBUTE_UNUSED)
6869 {
6870 }
6871
6872 /* Default function to output a label to divide up the exception table.
6873    The default is to do nothing.  A target that needs/wants to divide
6874    up the table must provide it's own function to do this.  */
6875 void
6876 default_emit_except_table_label (FILE * stream ATTRIBUTE_UNUSED)
6877 {
6878 }
6879
6880 /* This is how to output an internal numbered label where PREFIX is
6881    the class of label and LABELNO is the number within the class.  */
6882
6883 void
6884 default_internal_label (FILE *stream, const char *prefix,
6885                         unsigned long labelno)
6886 {
6887   char *const buf = (char *) alloca (40 + strlen (prefix));
6888   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
6889   ASM_OUTPUT_INTERNAL_LABEL (stream, buf);
6890 }
6891
6892
6893 /* The default implementation of ASM_DECLARE_CONSTANT_NAME.  */
6894
6895 void
6896 default_asm_declare_constant_name (FILE *file, const char *name,
6897                                    const_tree exp ATTRIBUTE_UNUSED,
6898                                    HOST_WIDE_INT size ATTRIBUTE_UNUSED)
6899 {
6900   assemble_label (file, name);
6901 }
6902
6903 /* This is the default behavior at the beginning of a file.  It's
6904    controlled by two other target-hook toggles.  */
6905 void
6906 default_file_start (void)
6907 {
6908   if (targetm.asm_file_start_app_off
6909       && !(flag_verbose_asm || flag_debug_asm || flag_dump_rtl_in_asm))
6910     fputs (ASM_APP_OFF, asm_out_file);
6911
6912   if (targetm.asm_file_start_file_directive)
6913     output_file_directive (asm_out_file, main_input_filename);
6914 }
6915
6916 /* This is a generic routine suitable for use as TARGET_ASM_FILE_END
6917    which emits a special section directive used to indicate whether or
6918    not this object file needs an executable stack.  This is primarily
6919    a GNU extension to ELF but could be used on other targets.  */
6920
6921 int trampolines_created;
6922
6923 void
6924 file_end_indicate_exec_stack (void)
6925 {
6926   unsigned int flags = SECTION_DEBUG;
6927   if (trampolines_created)
6928     flags |= SECTION_CODE;
6929
6930   switch_to_section (get_section (".note.GNU-stack", flags, NULL));
6931 }
6932
6933 /* Output DIRECTIVE (a C string) followed by a newline.  This is used as
6934    a get_unnamed_section callback.  */
6935
6936 void
6937 output_section_asm_op (const void *directive)
6938 {
6939   fprintf (asm_out_file, "%s\n", (const char *) directive);
6940 }
6941
6942 /* Emit assembly code to switch to section NEW_SECTION.  Do nothing if
6943    the current section is NEW_SECTION.  */
6944
6945 void
6946 switch_to_section (section *new_section)
6947 {
6948   if (in_section == new_section)
6949     return;
6950
6951   if (new_section->common.flags & SECTION_FORGET)
6952     in_section = NULL;
6953   else
6954     in_section = new_section;
6955
6956   switch (SECTION_STYLE (new_section))
6957     {
6958     case SECTION_NAMED:
6959       if (cfun
6960           && !crtl->subsections.unlikely_text_section_name
6961           && strcmp (new_section->named.name,
6962                      UNLIKELY_EXECUTED_TEXT_SECTION_NAME) == 0)
6963         crtl->subsections.unlikely_text_section_name = UNLIKELY_EXECUTED_TEXT_SECTION_NAME;
6964
6965       targetm.asm_out.named_section (new_section->named.name,
6966                                      new_section->named.common.flags,
6967                                      new_section->named.decl);
6968       break;
6969
6970     case SECTION_UNNAMED:
6971       new_section->unnamed.callback (new_section->unnamed.data);
6972       break;
6973
6974     case SECTION_NOSWITCH:
6975       gcc_unreachable ();
6976       break;
6977     }
6978
6979   new_section->common.flags |= SECTION_DECLARED;
6980 }
6981
6982 /* If block symbol SYMBOL has not yet been assigned an offset, place
6983    it at the end of its block.  */
6984
6985 void
6986 place_block_symbol (rtx symbol)
6987 {
6988   unsigned HOST_WIDE_INT size, mask, offset;
6989   struct constant_descriptor_rtx *desc;
6990   unsigned int alignment;
6991   struct object_block *block;
6992   tree decl;
6993
6994   gcc_assert (SYMBOL_REF_BLOCK (symbol));
6995   if (SYMBOL_REF_BLOCK_OFFSET (symbol) >= 0)
6996     return;
6997
6998   /* Work out the symbol's size and alignment.  */
6999   if (CONSTANT_POOL_ADDRESS_P (symbol))
7000     {
7001       desc = SYMBOL_REF_CONSTANT (symbol);
7002       alignment = desc->align;
7003       size = GET_MODE_SIZE (desc->mode);
7004     }
7005   else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
7006     {
7007       decl = SYMBOL_REF_DECL (symbol);
7008       alignment = DECL_ALIGN (decl);
7009       size = get_constant_size (DECL_INITIAL (decl));
7010     }
7011   else
7012     {
7013       decl = SYMBOL_REF_DECL (symbol);
7014       alignment = DECL_ALIGN (decl);
7015       size = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
7016     }
7017
7018   /* Calculate the object's offset from the start of the block.  */
7019   block = SYMBOL_REF_BLOCK (symbol);
7020   mask = alignment / BITS_PER_UNIT - 1;
7021   offset = (block->size + mask) & ~mask;
7022   SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
7023
7024   /* Record the block's new alignment and size.  */
7025   block->alignment = MAX (block->alignment, alignment);
7026   block->size = offset + size;
7027
7028   VEC_safe_push (rtx, gc, block->objects, symbol);
7029 }
7030
7031 /* Return the anchor that should be used to address byte offset OFFSET
7032    from the first object in BLOCK.  MODEL is the TLS model used
7033    to access it.  */
7034
7035 rtx
7036 get_section_anchor (struct object_block *block, HOST_WIDE_INT offset,
7037                     enum tls_model model)
7038 {
7039   char label[100];
7040   unsigned int begin, middle, end;
7041   unsigned HOST_WIDE_INT min_offset, max_offset, range, bias, delta;
7042   rtx anchor;
7043
7044   /* Work out the anchor's offset.  Use an offset of 0 for the first
7045      anchor so that we don't pessimize the case where we take the address
7046      of a variable at the beginning of the block.  This is particularly
7047      useful when a block has only one variable assigned to it.
7048
7049      We try to place anchors RANGE bytes apart, so there can then be
7050      anchors at +/-RANGE, +/-2 * RANGE, and so on, up to the limits of
7051      a ptr_mode offset.  With some target settings, the lowest such
7052      anchor might be out of range for the lowest ptr_mode offset;
7053      likewise the highest anchor for the highest offset.  Use anchors
7054      at the extreme ends of the ptr_mode range in such cases.
7055
7056      All arithmetic uses unsigned integers in order to avoid
7057      signed overflow.  */
7058   max_offset = (unsigned HOST_WIDE_INT) targetm.max_anchor_offset;
7059   min_offset = (unsigned HOST_WIDE_INT) targetm.min_anchor_offset;
7060   range = max_offset - min_offset + 1;
7061   if (range == 0)
7062     offset = 0;
7063   else
7064     {
7065       bias = 1 << (GET_MODE_BITSIZE (ptr_mode) - 1);
7066       if (offset < 0)
7067         {
7068           delta = -(unsigned HOST_WIDE_INT) offset + max_offset;
7069           delta -= delta % range;
7070           if (delta > bias)
7071             delta = bias;
7072           offset = (HOST_WIDE_INT) (-delta);
7073         }
7074       else
7075         {
7076           delta = (unsigned HOST_WIDE_INT) offset - min_offset;
7077           delta -= delta % range;
7078           if (delta > bias - 1)
7079             delta = bias - 1;
7080           offset = (HOST_WIDE_INT) delta;
7081         }
7082     }
7083
7084   /* Do a binary search to see if there's already an anchor we can use.
7085      Set BEGIN to the new anchor's index if not.  */
7086   begin = 0;
7087   end = VEC_length (rtx, block->anchors);
7088   while (begin != end)
7089     {
7090       middle = (end + begin) / 2;
7091       anchor = VEC_index (rtx, block->anchors, middle);
7092       if (SYMBOL_REF_BLOCK_OFFSET (anchor) > offset)
7093         end = middle;
7094       else if (SYMBOL_REF_BLOCK_OFFSET (anchor) < offset)
7095         begin = middle + 1;
7096       else if (SYMBOL_REF_TLS_MODEL (anchor) > model)
7097         end = middle;
7098       else if (SYMBOL_REF_TLS_MODEL (anchor) < model)
7099         begin = middle + 1;
7100       else
7101         return anchor;
7102     }
7103
7104   /* Create a new anchor with a unique label.  */
7105   ASM_GENERATE_INTERNAL_LABEL (label, "LANCHOR", anchor_labelno++);
7106   anchor = create_block_symbol (ggc_strdup (label), block, offset);
7107   SYMBOL_REF_FLAGS (anchor) |= SYMBOL_FLAG_LOCAL | SYMBOL_FLAG_ANCHOR;
7108   SYMBOL_REF_FLAGS (anchor) |= model << SYMBOL_FLAG_TLS_SHIFT;
7109
7110   /* Insert it at index BEGIN.  */
7111   VEC_safe_insert (rtx, gc, block->anchors, begin, anchor);
7112   return anchor;
7113 }
7114
7115 /* Output the objects in BLOCK.  */
7116
7117 static void
7118 output_object_block (struct object_block *block)
7119 {
7120   struct constant_descriptor_rtx *desc;
7121   unsigned int i;
7122   HOST_WIDE_INT offset;
7123   tree decl;
7124   rtx symbol;
7125
7126   if (block->objects == NULL)
7127     return;
7128
7129   /* Switch to the section and make sure that the first byte is
7130      suitably aligned.  */
7131   switch_to_section (block->sect);
7132   assemble_align (block->alignment);
7133
7134   /* Define the values of all anchors relative to the current section
7135      position.  */
7136   for (i = 0; VEC_iterate (rtx, block->anchors, i, symbol); i++)
7137     targetm.asm_out.output_anchor (symbol);
7138
7139   /* Output the objects themselves.  */
7140   offset = 0;
7141   for (i = 0; VEC_iterate (rtx, block->objects, i, symbol); i++)
7142     {
7143       /* Move to the object's offset, padding with zeros if necessary.  */
7144       assemble_zeros (SYMBOL_REF_BLOCK_OFFSET (symbol) - offset);
7145       offset = SYMBOL_REF_BLOCK_OFFSET (symbol);
7146       if (CONSTANT_POOL_ADDRESS_P (symbol))
7147         {
7148           desc = SYMBOL_REF_CONSTANT (symbol);
7149           output_constant_pool_1 (desc, 1);
7150           offset += GET_MODE_SIZE (desc->mode);
7151         }
7152       else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
7153         {
7154           decl = SYMBOL_REF_DECL (symbol);
7155           assemble_constant_contents (DECL_INITIAL (decl), XSTR (symbol, 0),
7156                                       DECL_ALIGN (decl));
7157           offset += get_constant_size (DECL_INITIAL (decl));
7158         }
7159       else
7160         {
7161           decl = SYMBOL_REF_DECL (symbol);
7162           assemble_variable_contents (decl, XSTR (symbol, 0), false);
7163           offset += tree_low_cst (DECL_SIZE_UNIT (decl), 1);
7164         }
7165     }
7166 }
7167
7168 /* A htab_traverse callback used to call output_object_block for
7169    each member of object_block_htab.  */
7170
7171 static int
7172 output_object_block_htab (void **slot, void *data ATTRIBUTE_UNUSED)
7173 {
7174   output_object_block ((struct object_block *) (*slot));
7175   return 1;
7176 }
7177
7178 /* Output the definitions of all object_blocks.  */
7179
7180 void
7181 output_object_blocks (void)
7182 {
7183   htab_traverse (object_block_htab, output_object_block_htab, NULL);
7184 }
7185
7186 /* This function provides a possible implementation of the
7187    TARGET_ASM_RECORD_GCC_SWITCHES target hook for ELF targets.  When triggered
7188    by -frecord-gcc-switches it creates a new mergeable, string section in the
7189    assembler output file called TARGET_ASM_RECORD_GCC_SWITCHES_SECTION which
7190    contains the switches in ASCII format.
7191
7192    FIXME: This code does not correctly handle double quote characters
7193    that appear inside strings, (it strips them rather than preserving them).
7194    FIXME: ASM_OUTPUT_ASCII, as defined in config/elfos.h will not emit NUL
7195    characters - instead it treats them as sub-string separators.  Since
7196    we want to emit NUL strings terminators into the object file we have to use
7197    ASM_OUTPUT_SKIP.  */
7198
7199 int
7200 elf_record_gcc_switches (print_switch_type type, const char * name)
7201 {
7202   switch (type)
7203     {
7204     case SWITCH_TYPE_PASSED:
7205       ASM_OUTPUT_ASCII (asm_out_file, name, strlen (name));
7206       ASM_OUTPUT_SKIP (asm_out_file, (unsigned HOST_WIDE_INT) 1);
7207       break;
7208
7209     case SWITCH_TYPE_DESCRIPTIVE:
7210       if (name == NULL)
7211         {
7212           /* Distinguish between invocations where name is NULL.  */
7213           static bool started = false;
7214
7215           if (!started)
7216             {
7217               section * sec;
7218
7219               sec = get_section (targetm.asm_out.record_gcc_switches_section,
7220                                  SECTION_DEBUG
7221                                  | SECTION_MERGE
7222                                  | SECTION_STRINGS
7223                                  | (SECTION_ENTSIZE & 1),
7224                                  NULL);
7225               switch_to_section (sec);
7226               started = true;
7227             }
7228         }
7229
7230     default:
7231       break;
7232     }
7233
7234   /* The return value is currently ignored by the caller, but must be 0.
7235      For -fverbose-asm the return value would be the number of characters
7236      emitted into the assembler file.  */
7237   return 0;
7238 }
7239
7240 /* Emit text to declare externally defined symbols. It is needed to
7241    properly support non-default visibility.  */
7242 void
7243 default_elf_asm_output_external (FILE *file ATTRIBUTE_UNUSED,
7244                                  tree decl,
7245                                  const char *name ATTRIBUTE_UNUSED)
7246 {
7247   /* We output the name if and only if TREE_SYMBOL_REFERENCED is
7248      set in order to avoid putting out names that are never really
7249      used. */
7250   if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
7251       && targetm.binds_local_p (decl))
7252     maybe_assemble_visibility (decl);
7253 }
7254
7255 /* Create a DEBUG_EXPR_DECL / DEBUG_EXPR pair from RTL expression
7256    EXP.  */
7257 rtx
7258 make_debug_expr_from_rtl (const_rtx exp)
7259 {
7260   tree ddecl = make_node (DEBUG_EXPR_DECL), type;
7261   enum machine_mode mode = GET_MODE (exp);
7262   rtx dval;
7263
7264   DECL_ARTIFICIAL (ddecl) = 1;
7265   if (REG_P (exp) && REG_EXPR (exp))
7266     type = TREE_TYPE (REG_EXPR (exp));
7267   else if (MEM_P (exp) && MEM_EXPR (exp))
7268     type = TREE_TYPE (MEM_EXPR (exp));
7269   else
7270     type = NULL_TREE;
7271   if (type && TYPE_MODE (type) == mode)
7272     TREE_TYPE (ddecl) = type;
7273   else
7274     TREE_TYPE (ddecl) = lang_hooks.types.type_for_mode (mode, 1);
7275   DECL_MODE (ddecl) = mode;
7276   dval = gen_rtx_DEBUG_EXPR (mode);
7277   DEBUG_EXPR_TREE_DECL (dval) = ddecl;
7278   SET_DECL_RTL (ddecl, dval);
7279   return dval;
7280 }
7281
7282 #include "gt-varasm.h"