OSDN Git Service

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