OSDN Git Service

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