OSDN Git Service

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