OSDN Git Service

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