OSDN Git Service

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