OSDN Git Service

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