OSDN Git Service

* config/alpha/alpha.c (alpha_sa_size): Force procedure type to
[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   (*debug_hooks->begin_function) (decl);
1753
1754   /* Make function name accessible from other files, if appropriate.  */
1755
1756   if (TREE_PUBLIC (decl))
1757     {
1758       notice_global_symbol (decl);
1759
1760       globalize_decl (decl);
1761
1762       maybe_assemble_visibility (decl);
1763     }
1764
1765   if (DECL_PRESERVE_P (decl))
1766     targetm.asm_out.mark_decl_preserved (fnname);
1767
1768   /* Do any machine/system dependent processing of the function name.  */
1769 #ifdef ASM_DECLARE_FUNCTION_NAME
1770   ASM_DECLARE_FUNCTION_NAME (asm_out_file, fnname, current_function_decl);
1771 #else
1772   /* Standard thing is just output label for the function.  */
1773   ASM_OUTPUT_LABEL (asm_out_file, fnname);
1774 #endif /* ASM_DECLARE_FUNCTION_NAME */
1775 }
1776
1777 /* Output assembler code associated with defining the size of the
1778    function.  DECL describes the function.  NAME is the function's name.  */
1779
1780 void
1781 assemble_end_function (tree decl, const char *fnname ATTRIBUTE_UNUSED)
1782 {
1783 #ifdef ASM_DECLARE_FUNCTION_SIZE
1784   /* We could have switched section in the middle of the function.  */
1785   if (flag_reorder_blocks_and_partition)
1786     switch_to_section (function_section (decl));
1787   ASM_DECLARE_FUNCTION_SIZE (asm_out_file, fnname, decl);
1788 #endif
1789   if (! CONSTANT_POOL_BEFORE_FUNCTION)
1790     {
1791       output_constant_pool (fnname, decl);
1792       switch_to_section (function_section (decl)); /* need to switch back */
1793     }
1794   /* Output labels for end of hot/cold text sections (to be used by
1795      debug info.)  */
1796   if (flag_reorder_blocks_and_partition)
1797     {
1798       section *save_text_section;
1799
1800       save_text_section = in_section;
1801       switch_to_section (unlikely_text_section ());
1802       ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_end_label);
1803       if (first_function_block_is_cold)
1804         switch_to_section (text_section);
1805       else
1806         switch_to_section (function_section (decl));
1807       ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_end_label);
1808       switch_to_section (save_text_section);
1809     }
1810 }
1811 \f
1812 /* Assemble code to leave SIZE bytes of zeros.  */
1813
1814 void
1815 assemble_zeros (unsigned HOST_WIDE_INT size)
1816 {
1817   /* Do no output if -fsyntax-only.  */
1818   if (flag_syntax_only)
1819     return;
1820
1821 #ifdef ASM_NO_SKIP_IN_TEXT
1822   /* The `space' pseudo in the text section outputs nop insns rather than 0s,
1823      so we must output 0s explicitly in the text section.  */
1824   if (ASM_NO_SKIP_IN_TEXT && (in_section->common.flags & SECTION_CODE) != 0)
1825     {
1826       unsigned HOST_WIDE_INT i;
1827       for (i = 0; i < size; i++)
1828         assemble_integer (const0_rtx, 1, BITS_PER_UNIT, 1);
1829     }
1830   else
1831 #endif
1832     if (size > 0)
1833       ASM_OUTPUT_SKIP (asm_out_file, size);
1834 }
1835
1836 /* Assemble an alignment pseudo op for an ALIGN-bit boundary.  */
1837
1838 void
1839 assemble_align (int align)
1840 {
1841   if (align > BITS_PER_UNIT)
1842     {
1843       ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
1844     }
1845 }
1846
1847 /* Assemble a string constant with the specified C string as contents.  */
1848
1849 void
1850 assemble_string (const char *p, int size)
1851 {
1852   int pos = 0;
1853   int maximum = 2000;
1854
1855   /* If the string is very long, split it up.  */
1856
1857   while (pos < size)
1858     {
1859       int thissize = size - pos;
1860       if (thissize > maximum)
1861         thissize = maximum;
1862
1863       ASM_OUTPUT_ASCII (asm_out_file, p, thissize);
1864
1865       pos += thissize;
1866       p += thissize;
1867     }
1868 }
1869
1870 \f
1871 /* A noswitch_section_callback for lcomm_section.  */
1872
1873 static bool
1874 emit_local (tree decl ATTRIBUTE_UNUSED,
1875             const char *name ATTRIBUTE_UNUSED,
1876             unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1877             unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1878 {
1879 #if defined ASM_OUTPUT_ALIGNED_DECL_LOCAL
1880   ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, decl, name,
1881                                  size, DECL_ALIGN (decl));
1882   return true;
1883 #elif defined ASM_OUTPUT_ALIGNED_LOCAL
1884   ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, DECL_ALIGN (decl));
1885   return true;
1886 #else
1887   ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
1888   return false;
1889 #endif
1890 }
1891
1892 /* A noswitch_section_callback for bss_noswitch_section.  */
1893
1894 #if defined ASM_OUTPUT_ALIGNED_BSS || defined ASM_OUTPUT_BSS
1895 static bool
1896 emit_bss (tree decl ATTRIBUTE_UNUSED,
1897           const char *name ATTRIBUTE_UNUSED,
1898           unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1899           unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1900 {
1901 #if defined ASM_OUTPUT_ALIGNED_BSS
1902   ASM_OUTPUT_ALIGNED_BSS (asm_out_file, decl, name, size, DECL_ALIGN (decl));
1903   return true;
1904 #else
1905   ASM_OUTPUT_BSS (asm_out_file, decl, name, size, rounded);
1906   return false;
1907 #endif
1908 }
1909 #endif
1910
1911 /* A noswitch_section_callback for comm_section.  */
1912
1913 static bool
1914 emit_common (tree decl ATTRIBUTE_UNUSED,
1915              const char *name ATTRIBUTE_UNUSED,
1916              unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1917              unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1918 {
1919 #if defined ASM_OUTPUT_ALIGNED_DECL_COMMON
1920   ASM_OUTPUT_ALIGNED_DECL_COMMON (asm_out_file, decl, name,
1921                                   size, DECL_ALIGN (decl));
1922   return true;
1923 #elif defined ASM_OUTPUT_ALIGNED_COMMON
1924   ASM_OUTPUT_ALIGNED_COMMON (asm_out_file, name, size, DECL_ALIGN (decl));
1925   return true;
1926 #else
1927   ASM_OUTPUT_COMMON (asm_out_file, name, size, rounded);
1928   return false;
1929 #endif
1930 }
1931
1932 /* A noswitch_section_callback for tls_comm_section.  */
1933
1934 static bool
1935 emit_tls_common (tree decl ATTRIBUTE_UNUSED,
1936                  const char *name ATTRIBUTE_UNUSED,
1937                  unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1938                  unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1939 {
1940 #ifdef ASM_OUTPUT_TLS_COMMON
1941   ASM_OUTPUT_TLS_COMMON (asm_out_file, decl, name, size);
1942   return true;
1943 #else
1944   sorry ("thread-local COMMON data not implemented");
1945   return true;
1946 #endif
1947 }
1948
1949 /* Assemble DECL given that it belongs in SECTION_NOSWITCH section SECT.
1950    NAME is the name of DECL's SYMBOL_REF.  */
1951
1952 static void
1953 assemble_noswitch_variable (tree decl, const char *name, section *sect)
1954 {
1955   unsigned HOST_WIDE_INT size, rounded;
1956
1957   size = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
1958   rounded = size;
1959
1960   /* Don't allocate zero bytes of common,
1961      since that means "undefined external" in the linker.  */
1962   if (size == 0)
1963     rounded = 1;
1964
1965   /* Round size up to multiple of BIGGEST_ALIGNMENT bits
1966      so that each uninitialized object starts on such a boundary.  */
1967   rounded += (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1;
1968   rounded = (rounded / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
1969              * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
1970
1971   if (!sect->noswitch.callback (decl, name, size, rounded)
1972       && (unsigned HOST_WIDE_INT) DECL_ALIGN_UNIT (decl) > rounded)
1973     warning (0, "requested alignment for %q+D is greater than "
1974              "implemented alignment of %wu", decl, rounded);
1975 }
1976
1977 /* A subroutine of assemble_variable.  Output the label and contents of
1978    DECL, whose address is a SYMBOL_REF with name NAME.  DONT_OUTPUT_DATA
1979    is as for assemble_variable.  */
1980
1981 static void
1982 assemble_variable_contents (tree decl, const char *name,
1983                             bool dont_output_data)
1984 {
1985   /* Do any machine/system dependent processing of the object.  */
1986 #ifdef ASM_DECLARE_OBJECT_NAME
1987   last_assemble_variable_decl = decl;
1988   ASM_DECLARE_OBJECT_NAME (asm_out_file, name, decl);
1989 #else
1990   /* Standard thing is just output label for the object.  */
1991   ASM_OUTPUT_LABEL (asm_out_file, name);
1992 #endif /* ASM_DECLARE_OBJECT_NAME */
1993
1994   if (!dont_output_data)
1995     {
1996       if (DECL_INITIAL (decl)
1997           && DECL_INITIAL (decl) != error_mark_node
1998           && !initializer_zerop (DECL_INITIAL (decl)))
1999         /* Output the actual data.  */
2000         output_constant (DECL_INITIAL (decl),
2001                          tree_low_cst (DECL_SIZE_UNIT (decl), 1),
2002                          DECL_ALIGN (decl));
2003       else
2004         /* Leave space for it.  */
2005         assemble_zeros (tree_low_cst (DECL_SIZE_UNIT (decl), 1));
2006     }
2007 }
2008
2009 /* Initialize emulated tls object TO, which refers to TLS variable
2010    DECL and is initialized by PROXY.  */
2011
2012 tree
2013 default_emutls_var_init (tree to, tree decl, tree proxy)
2014 {
2015   VEC(constructor_elt,gc) *v = VEC_alloc (constructor_elt, gc, 4);
2016   constructor_elt *elt;
2017   tree type = TREE_TYPE (to);
2018   tree field = TYPE_FIELDS (type);
2019   
2020   elt = VEC_quick_push (constructor_elt, v, NULL);
2021   elt->index = field;
2022   elt->value = fold_convert (TREE_TYPE (field), DECL_SIZE_UNIT (decl));
2023   
2024   elt = VEC_quick_push (constructor_elt, v, NULL);
2025   field = TREE_CHAIN (field);
2026   elt->index = field;
2027   elt->value = build_int_cst (TREE_TYPE (field),
2028                               DECL_ALIGN_UNIT (decl));
2029   
2030   elt = VEC_quick_push (constructor_elt, v, NULL);
2031   field = TREE_CHAIN (field);
2032   elt->index = field;
2033   elt->value = null_pointer_node;
2034   
2035   elt = VEC_quick_push (constructor_elt, v, NULL);
2036   field = TREE_CHAIN (field);
2037   elt->index = field;
2038   elt->value = proxy;
2039   
2040   return build_constructor (type, v);
2041 }
2042
2043 /* Assemble everything that is needed for a variable or function declaration.
2044    Not used for automatic variables, and not used for function definitions.
2045    Should not be called for variables of incomplete structure type.
2046
2047    TOP_LEVEL is nonzero if this variable has file scope.
2048    AT_END is nonzero if this is the special handling, at end of compilation,
2049    to define things that have had only tentative definitions.
2050    DONT_OUTPUT_DATA if nonzero means don't actually output the
2051    initial value (that will be done by the caller).  */
2052
2053 void
2054 assemble_variable (tree decl, int top_level ATTRIBUTE_UNUSED,
2055                    int at_end ATTRIBUTE_UNUSED, int dont_output_data)
2056 {
2057   const char *name;
2058   rtx decl_rtl, symbol;
2059   section *sect;
2060
2061   if (! targetm.have_tls
2062       && TREE_CODE (decl) == VAR_DECL
2063       && DECL_THREAD_LOCAL_P (decl))
2064     {
2065       tree to = emutls_decl (decl);
2066
2067       /* If this variable is defined locally, then we need to initialize the
2068          control structure with size and alignment information.  We do this
2069          at the last moment because tentative definitions can take a locally
2070          defined but uninitialized variable and initialize it later, which
2071          would result in incorrect contents.  */
2072       if (! DECL_EXTERNAL (to)
2073           && (! DECL_COMMON (to)
2074               || (DECL_INITIAL (decl)
2075                   && DECL_INITIAL (decl) != error_mark_node)))
2076         {
2077           DECL_INITIAL (to) = targetm.emutls.var_init
2078             (to, decl, get_emutls_init_templ_addr (decl));
2079
2080           /* Make sure the template is marked as needed early enough.
2081              Without this, if the variable is placed in a
2082              section-anchored block, the template will only be marked
2083              when it's too late.  */
2084           record_references_in_initializer (to);
2085         }
2086
2087       decl = to;
2088     }
2089
2090   last_assemble_variable_decl = 0;
2091
2092   /* Normally no need to say anything here for external references,
2093      since assemble_external is called by the language-specific code
2094      when a declaration is first seen.  */
2095
2096   if (DECL_EXTERNAL (decl))
2097     return;
2098
2099   /* Output no assembler code for a function declaration.
2100      Only definitions of functions output anything.  */
2101
2102   if (TREE_CODE (decl) == FUNCTION_DECL)
2103     return;
2104
2105   /* Do nothing for global register variables.  */
2106   if (DECL_RTL_SET_P (decl) && REG_P (DECL_RTL (decl)))
2107     {
2108       TREE_ASM_WRITTEN (decl) = 1;
2109       return;
2110     }
2111
2112   /* If type was incomplete when the variable was declared,
2113      see if it is complete now.  */
2114
2115   if (DECL_SIZE (decl) == 0)
2116     layout_decl (decl, 0);
2117
2118   /* Still incomplete => don't allocate it; treat the tentative defn
2119      (which is what it must have been) as an `extern' reference.  */
2120
2121   if (!dont_output_data && DECL_SIZE (decl) == 0)
2122     {
2123       error ("storage size of %q+D isn%'t known", decl);
2124       TREE_ASM_WRITTEN (decl) = 1;
2125       return;
2126     }
2127
2128   /* The first declaration of a variable that comes through this function
2129      decides whether it is global (in C, has external linkage)
2130      or local (in C, has internal linkage).  So do nothing more
2131      if this function has already run.  */
2132
2133   if (TREE_ASM_WRITTEN (decl))
2134     return;
2135
2136   /* Make sure targetm.encode_section_info is invoked before we set
2137      ASM_WRITTEN.  */
2138   decl_rtl = DECL_RTL (decl);
2139
2140   TREE_ASM_WRITTEN (decl) = 1;
2141
2142   /* Do no output if -fsyntax-only.  */
2143   if (flag_syntax_only)
2144     return;
2145
2146   app_disable ();
2147
2148   if (! dont_output_data
2149       && ! host_integerp (DECL_SIZE_UNIT (decl), 1))
2150     {
2151       error ("size of variable %q+D is too large", decl);
2152       return;
2153     }
2154
2155   gcc_assert (MEM_P (decl_rtl));
2156   gcc_assert (GET_CODE (XEXP (decl_rtl, 0)) == SYMBOL_REF);
2157   symbol = XEXP (decl_rtl, 0);
2158   name = XSTR (symbol, 0);
2159   if (TREE_PUBLIC (decl) && DECL_NAME (decl))
2160     notice_global_symbol (decl);
2161
2162   /* Compute the alignment of this data.  */
2163
2164   align_variable (decl, dont_output_data);
2165   set_mem_align (decl_rtl, DECL_ALIGN (decl));
2166
2167   if (TREE_PUBLIC (decl))
2168     maybe_assemble_visibility (decl);
2169
2170   if (DECL_PRESERVE_P (decl))
2171     targetm.asm_out.mark_decl_preserved (name);
2172
2173   /* First make the assembler name(s) global if appropriate.  */
2174   sect = get_variable_section (decl, false);
2175   if (TREE_PUBLIC (decl)
2176       && (sect->common.flags & SECTION_COMMON) == 0)
2177     globalize_decl (decl);
2178
2179   /* Output any data that we will need to use the address of.  */
2180   if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node)
2181     output_addressed_constants (DECL_INITIAL (decl));
2182
2183   /* dbxout.c needs to know this.  */
2184   if (sect && (sect->common.flags & SECTION_CODE) != 0)
2185     DECL_IN_TEXT_SECTION (decl) = 1;
2186
2187   /* If the decl is part of an object_block, make sure that the decl
2188      has been positioned within its block, but do not write out its
2189      definition yet.  output_object_blocks will do that later.  */
2190   if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
2191     {
2192       gcc_assert (!dont_output_data);
2193       place_block_symbol (symbol);
2194     }
2195   else if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
2196     assemble_noswitch_variable (decl, name, sect);
2197   else
2198     {
2199       switch_to_section (sect);
2200       if (DECL_ALIGN (decl) > BITS_PER_UNIT)
2201         ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (DECL_ALIGN_UNIT (decl)));
2202       assemble_variable_contents (decl, name, dont_output_data);
2203     }
2204 }
2205
2206 /* Return 1 if type TYPE contains any pointers.  */
2207
2208 static int
2209 contains_pointers_p (tree type)
2210 {
2211   switch (TREE_CODE (type))
2212     {
2213     case POINTER_TYPE:
2214     case REFERENCE_TYPE:
2215       /* I'm not sure whether OFFSET_TYPE needs this treatment,
2216          so I'll play safe and return 1.  */
2217     case OFFSET_TYPE:
2218       return 1;
2219
2220     case RECORD_TYPE:
2221     case UNION_TYPE:
2222     case QUAL_UNION_TYPE:
2223       {
2224         tree fields;
2225         /* For a type that has fields, see if the fields have pointers.  */
2226         for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
2227           if (TREE_CODE (fields) == FIELD_DECL
2228               && contains_pointers_p (TREE_TYPE (fields)))
2229             return 1;
2230         return 0;
2231       }
2232
2233     case ARRAY_TYPE:
2234       /* An array type contains pointers if its element type does.  */
2235       return contains_pointers_p (TREE_TYPE (type));
2236
2237     default:
2238       return 0;
2239     }
2240 }
2241
2242 /* We delay assemble_external processing until
2243    the compilation unit is finalized.  This is the best we can do for
2244    right now (i.e. stage 3 of GCC 4.0) - the right thing is to delay
2245    it all the way to final.  See PR 17982 for further discussion.  */
2246 static GTY(()) tree pending_assemble_externals;
2247
2248 #ifdef ASM_OUTPUT_EXTERNAL
2249 /* True if DECL is a function decl for which no out-of-line copy exists.
2250    It is assumed that DECL's assembler name has been set.  */
2251
2252 static bool
2253 incorporeal_function_p (tree decl)
2254 {
2255   if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
2256     {
2257       const char *name;
2258
2259       if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
2260           && DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA)
2261         return true;
2262
2263       name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2264       if (is_builtin_name (name))
2265         return true;
2266     }
2267   return false;
2268 }
2269
2270 /* Actually do the tests to determine if this is necessary, and invoke
2271    ASM_OUTPUT_EXTERNAL.  */
2272 static void
2273 assemble_external_real (tree decl)
2274 {
2275   rtx rtl = DECL_RTL (decl);
2276
2277   if (MEM_P (rtl) && GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF
2278       && !SYMBOL_REF_USED (XEXP (rtl, 0))
2279       && !incorporeal_function_p (decl))
2280     {
2281       /* Some systems do require some output.  */
2282       SYMBOL_REF_USED (XEXP (rtl, 0)) = 1;
2283       ASM_OUTPUT_EXTERNAL (asm_out_file, decl, XSTR (XEXP (rtl, 0), 0));
2284     }
2285 }
2286 #endif
2287
2288 void
2289 process_pending_assemble_externals (void)
2290 {
2291 #ifdef ASM_OUTPUT_EXTERNAL
2292   tree list;
2293   for (list = pending_assemble_externals; list; list = TREE_CHAIN (list))
2294     assemble_external_real (TREE_VALUE (list));
2295
2296   pending_assemble_externals = 0;
2297 #endif
2298 }
2299
2300 /* This TREE_LIST contains any weak symbol declarations waiting
2301    to be emitted.  */
2302 static GTY(()) tree weak_decls;
2303
2304 /* Output something to declare an external symbol to the assembler,
2305    and qualifiers such as weakness.  (Most assemblers don't need
2306    extern declaration, so we normally output nothing.)  Do nothing if
2307    DECL is not external.  */
2308
2309 void
2310 assemble_external (tree decl ATTRIBUTE_UNUSED)
2311 {
2312   /* Because most platforms do not define ASM_OUTPUT_EXTERNAL, the
2313      main body of this code is only rarely exercised.  To provide some
2314      testing, on all platforms, we make sure that the ASM_OUT_FILE is
2315      open.  If it's not, we should not be calling this function.  */
2316   gcc_assert (asm_out_file);
2317
2318   if (!DECL_P (decl) || !DECL_EXTERNAL (decl) || !TREE_PUBLIC (decl))
2319     return;
2320
2321   /* We want to output annotation for weak and external symbols at
2322      very last to check if they are references or not.  */
2323
2324   if (SUPPORTS_WEAK && DECL_WEAK (decl)
2325       /* TREE_STATIC is a weird and abused creature which is not
2326          generally the right test for whether an entity has been
2327          locally emitted, inlined or otherwise not-really-extern, but
2328          for declarations that can be weak, it happens to be
2329          match.  */
2330       && !TREE_STATIC (decl)
2331       && tree_find_value (weak_decls, decl) == NULL_TREE)
2332       weak_decls = tree_cons (NULL, decl, weak_decls);
2333
2334 #ifdef ASM_OUTPUT_EXTERNAL
2335   if (tree_find_value (pending_assemble_externals, decl) == NULL_TREE)
2336     pending_assemble_externals = tree_cons (NULL, decl,
2337                                             pending_assemble_externals);
2338 #endif
2339 }
2340
2341 /* Similar, for calling a library function FUN.  */
2342
2343 void
2344 assemble_external_libcall (rtx fun)
2345 {
2346   /* Declare library function name external when first used, if nec.  */
2347   if (! SYMBOL_REF_USED (fun))
2348     {
2349       SYMBOL_REF_USED (fun) = 1;
2350       targetm.asm_out.external_libcall (fun);
2351     }
2352 }
2353
2354 /* Assemble a label named NAME.  */
2355
2356 void
2357 assemble_label (const char *name)
2358 {
2359   ASM_OUTPUT_LABEL (asm_out_file, name);
2360 }
2361
2362 /* Set the symbol_referenced flag for ID.  */
2363 void
2364 mark_referenced (tree id)
2365 {
2366   TREE_SYMBOL_REFERENCED (id) = 1;
2367 }
2368
2369 /* Set the symbol_referenced flag for DECL and notify callgraph.  */
2370 void
2371 mark_decl_referenced (tree decl)
2372 {
2373   if (TREE_CODE (decl) == FUNCTION_DECL)
2374     {
2375       /* Extern inline functions don't become needed when referenced.
2376          If we know a method will be emitted in other TU and no new
2377          functions can be marked reachable, just use the external
2378          definition.  */
2379       struct cgraph_node *node = cgraph_node (decl);
2380       if (!DECL_EXTERNAL (decl)
2381           && (!node->local.vtable_method || !cgraph_global_info_ready
2382               || !node->local.finalized))
2383         cgraph_mark_needed_node (node);
2384     }
2385   else if (TREE_CODE (decl) == VAR_DECL)
2386     {
2387       struct varpool_node *node = varpool_node (decl);
2388       varpool_mark_needed_node (node);
2389       /* C++ frontend use mark_decl_references to force COMDAT variables
2390          to be output that might appear dead otherwise.  */
2391       node->force_output = true;
2392     }
2393   /* else do nothing - we can get various sorts of CST nodes here,
2394      which do not need to be marked.  */
2395 }
2396
2397
2398 /* Follow the IDENTIFIER_TRANSPARENT_ALIAS chain starting at *ALIAS
2399    until we find an identifier that is not itself a transparent alias.
2400    Modify the alias passed to it by reference (and all aliases on the
2401    way to the ultimate target), such that they do not have to be
2402    followed again, and return the ultimate target of the alias
2403    chain.  */
2404
2405 static inline tree
2406 ultimate_transparent_alias_target (tree *alias)
2407 {
2408   tree target = *alias;
2409
2410   if (IDENTIFIER_TRANSPARENT_ALIAS (target))
2411     {
2412       gcc_assert (TREE_CHAIN (target));
2413       target = ultimate_transparent_alias_target (&TREE_CHAIN (target));
2414       gcc_assert (! IDENTIFIER_TRANSPARENT_ALIAS (target)
2415                   && ! TREE_CHAIN (target));
2416       *alias = target;
2417     }
2418
2419   return target;
2420 }
2421
2422 /* Output to FILE (an assembly file) a reference to NAME.  If NAME
2423    starts with a *, the rest of NAME is output verbatim.  Otherwise
2424    NAME is transformed in a target-specific way (usually by the
2425    addition of an underscore).  */
2426
2427 void
2428 assemble_name_raw (FILE *file, const char *name)
2429 {
2430   if (name[0] == '*')
2431     fputs (&name[1], file);
2432   else
2433     ASM_OUTPUT_LABELREF (file, name);
2434 }
2435
2436 /* Like assemble_name_raw, but should be used when NAME might refer to
2437    an entity that is also represented as a tree (like a function or
2438    variable).  If NAME does refer to such an entity, that entity will
2439    be marked as referenced.  */
2440
2441 void
2442 assemble_name (FILE *file, const char *name)
2443 {
2444   const char *real_name;
2445   tree id;
2446
2447   real_name = targetm.strip_name_encoding (name);
2448
2449   id = maybe_get_identifier (real_name);
2450   if (id)
2451     {
2452       tree id_orig = id;
2453
2454       mark_referenced (id);
2455       ultimate_transparent_alias_target (&id);
2456       if (id != id_orig)
2457         name = IDENTIFIER_POINTER (id);
2458       gcc_assert (! TREE_CHAIN (id));
2459     }
2460
2461   assemble_name_raw (file, name);
2462 }
2463
2464 /* Allocate SIZE bytes writable static space with a gensym name
2465    and return an RTX to refer to its address.  */
2466
2467 rtx
2468 assemble_static_space (unsigned HOST_WIDE_INT size)
2469 {
2470   char name[12];
2471   const char *namestring;
2472   rtx x;
2473
2474   ASM_GENERATE_INTERNAL_LABEL (name, "LF", const_labelno);
2475   ++const_labelno;
2476   namestring = ggc_strdup (name);
2477
2478   x = gen_rtx_SYMBOL_REF (Pmode, namestring);
2479   SYMBOL_REF_FLAGS (x) = SYMBOL_FLAG_LOCAL;
2480
2481 #ifdef ASM_OUTPUT_ALIGNED_DECL_LOCAL
2482   ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, NULL_TREE, name, size,
2483                                  BIGGEST_ALIGNMENT);
2484 #else
2485 #ifdef ASM_OUTPUT_ALIGNED_LOCAL
2486   ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, BIGGEST_ALIGNMENT);
2487 #else
2488   {
2489     /* Round size up to multiple of BIGGEST_ALIGNMENT bits
2490        so that each uninitialized object starts on such a boundary.  */
2491     /* Variable `rounded' might or might not be used in ASM_OUTPUT_LOCAL.  */
2492     unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED
2493       = ((size + (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1)
2494          / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
2495          * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
2496     ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
2497   }
2498 #endif
2499 #endif
2500   return x;
2501 }
2502
2503 /* Assemble the static constant template for function entry trampolines.
2504    This is done at most once per compilation.
2505    Returns an RTX for the address of the template.  */
2506
2507 static GTY(()) rtx initial_trampoline;
2508
2509 #ifdef TRAMPOLINE_TEMPLATE
2510 rtx
2511 assemble_trampoline_template (void)
2512 {
2513   char label[256];
2514   const char *name;
2515   int align;
2516   rtx symbol;
2517
2518   if (initial_trampoline)
2519     return initial_trampoline;
2520
2521   /* By default, put trampoline templates in read-only data section.  */
2522
2523 #ifdef TRAMPOLINE_SECTION
2524   switch_to_section (TRAMPOLINE_SECTION);
2525 #else
2526   switch_to_section (readonly_data_section);
2527 #endif
2528
2529   /* Write the assembler code to define one.  */
2530   align = floor_log2 (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
2531   if (align > 0)
2532     {
2533       ASM_OUTPUT_ALIGN (asm_out_file, align);
2534     }
2535
2536   targetm.asm_out.internal_label (asm_out_file, "LTRAMP", 0);
2537   TRAMPOLINE_TEMPLATE (asm_out_file);
2538
2539   /* Record the rtl to refer to it.  */
2540   ASM_GENERATE_INTERNAL_LABEL (label, "LTRAMP", 0);
2541   name = ggc_strdup (label);
2542   symbol = gen_rtx_SYMBOL_REF (Pmode, name);
2543   SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_LOCAL;
2544
2545   initial_trampoline = gen_rtx_MEM (BLKmode, symbol);
2546   set_mem_align (initial_trampoline, TRAMPOLINE_ALIGNMENT);
2547
2548   return initial_trampoline;
2549 }
2550 #endif
2551 \f
2552 /* A and B are either alignments or offsets.  Return the minimum alignment
2553    that may be assumed after adding the two together.  */
2554
2555 static inline unsigned
2556 min_align (unsigned int a, unsigned int b)
2557 {
2558   return (a | b) & -(a | b);
2559 }
2560
2561 /* Return the assembler directive for creating a given kind of integer
2562    object.  SIZE is the number of bytes in the object and ALIGNED_P
2563    indicates whether it is known to be aligned.  Return NULL if the
2564    assembly dialect has no such directive.
2565
2566    The returned string should be printed at the start of a new line and
2567    be followed immediately by the object's initial value.  */
2568
2569 const char *
2570 integer_asm_op (int size, int aligned_p)
2571 {
2572   struct asm_int_op *ops;
2573
2574   if (aligned_p)
2575     ops = &targetm.asm_out.aligned_op;
2576   else
2577     ops = &targetm.asm_out.unaligned_op;
2578
2579   switch (size)
2580     {
2581     case 1:
2582       return targetm.asm_out.byte_op;
2583     case 2:
2584       return ops->hi;
2585     case 4:
2586       return ops->si;
2587     case 8:
2588       return ops->di;
2589     case 16:
2590       return ops->ti;
2591     default:
2592       return NULL;
2593     }
2594 }
2595
2596 /* Use directive OP to assemble an integer object X.  Print OP at the
2597    start of the line, followed immediately by the value of X.  */
2598
2599 void
2600 assemble_integer_with_op (const char *op, rtx x)
2601 {
2602   fputs (op, asm_out_file);
2603   output_addr_const (asm_out_file, x);
2604   fputc ('\n', asm_out_file);
2605 }
2606
2607 /* The default implementation of the asm_out.integer target hook.  */
2608
2609 bool
2610 default_assemble_integer (rtx x ATTRIBUTE_UNUSED,
2611                           unsigned int size ATTRIBUTE_UNUSED,
2612                           int aligned_p ATTRIBUTE_UNUSED)
2613 {
2614   const char *op = integer_asm_op (size, aligned_p);
2615   /* Avoid GAS bugs for large values.  Specifically negative values whose
2616      absolute value fits in a bfd_vma, but not in a bfd_signed_vma.  */
2617   if (size > UNITS_PER_WORD && size > POINTER_SIZE / BITS_PER_UNIT)
2618     return false;
2619   return op && (assemble_integer_with_op (op, x), true);
2620 }
2621
2622 /* Assemble the integer constant X into an object of SIZE bytes.  ALIGN is
2623    the alignment of the integer in bits.  Return 1 if we were able to output
2624    the constant, otherwise 0.  We must be able to output the constant,
2625    if FORCE is nonzero.  */
2626
2627 bool
2628 assemble_integer (rtx x, unsigned int size, unsigned int align, int force)
2629 {
2630   int aligned_p;
2631
2632   aligned_p = (align >= MIN (size * BITS_PER_UNIT, BIGGEST_ALIGNMENT));
2633
2634   /* See if the target hook can handle this kind of object.  */
2635   if (targetm.asm_out.integer (x, size, aligned_p))
2636     return true;
2637
2638   /* If the object is a multi-byte one, try splitting it up.  Split
2639      it into words it if is multi-word, otherwise split it into bytes.  */
2640   if (size > 1)
2641     {
2642       enum machine_mode omode, imode;
2643       unsigned int subalign;
2644       unsigned int subsize, i;
2645       enum mode_class mclass;
2646
2647       subsize = size > UNITS_PER_WORD? UNITS_PER_WORD : 1;
2648       subalign = MIN (align, subsize * BITS_PER_UNIT);
2649       if (GET_CODE (x) == CONST_FIXED)
2650         mclass = GET_MODE_CLASS (GET_MODE (x));
2651       else
2652         mclass = MODE_INT;
2653
2654       omode = mode_for_size (subsize * BITS_PER_UNIT, mclass, 0);
2655       imode = mode_for_size (size * BITS_PER_UNIT, mclass, 0);
2656
2657       for (i = 0; i < size; i += subsize)
2658         {
2659           rtx partial = simplify_subreg (omode, x, imode, i);
2660           if (!partial || !assemble_integer (partial, subsize, subalign, 0))
2661             break;
2662         }
2663       if (i == size)
2664         return true;
2665
2666       /* If we've printed some of it, but not all of it, there's no going
2667          back now.  */
2668       gcc_assert (!i);
2669     }
2670
2671   gcc_assert (!force);
2672
2673   return false;
2674 }
2675 \f
2676 void
2677 assemble_real (REAL_VALUE_TYPE d, enum machine_mode mode, unsigned int align)
2678 {
2679   long data[4] = {0, 0, 0, 0};
2680   int i;
2681   int bitsize, nelts, nunits, units_per;
2682
2683   /* This is hairy.  We have a quantity of known size.  real_to_target
2684      will put it into an array of *host* longs, 32 bits per element
2685      (even if long is more than 32 bits).  We need to determine the
2686      number of array elements that are occupied (nelts) and the number
2687      of *target* min-addressable units that will be occupied in the
2688      object file (nunits).  We cannot assume that 32 divides the
2689      mode's bitsize (size * BITS_PER_UNIT) evenly.
2690
2691      size * BITS_PER_UNIT is used here to make sure that padding bits
2692      (which might appear at either end of the value; real_to_target
2693      will include the padding bits in its output array) are included.  */
2694
2695   nunits = GET_MODE_SIZE (mode);
2696   bitsize = nunits * BITS_PER_UNIT;
2697   nelts = CEIL (bitsize, 32);
2698   units_per = 32 / BITS_PER_UNIT;
2699
2700   real_to_target (data, &d, mode);
2701
2702   /* Put out the first word with the specified alignment.  */
2703   assemble_integer (GEN_INT (data[0]), MIN (nunits, units_per), align, 1);
2704   nunits -= units_per;
2705
2706   /* Subsequent words need only 32-bit alignment.  */
2707   align = min_align (align, 32);
2708
2709   for (i = 1; i < nelts; i++)
2710     {
2711       assemble_integer (GEN_INT (data[i]), MIN (nunits, units_per), align, 1);
2712       nunits -= units_per;
2713     }
2714 }
2715 \f
2716 /* Given an expression EXP with a constant value,
2717    reduce it to the sum of an assembler symbol and an integer.
2718    Store them both in the structure *VALUE.
2719    EXP must be reducible.  */
2720
2721 struct GTY(()) addr_const {
2722   rtx base;
2723   HOST_WIDE_INT offset;
2724 };
2725
2726 static void
2727 decode_addr_const (tree exp, struct addr_const *value)
2728 {
2729   tree target = TREE_OPERAND (exp, 0);
2730   int offset = 0;
2731   rtx x;
2732
2733   while (1)
2734     {
2735       if (TREE_CODE (target) == COMPONENT_REF
2736           && host_integerp (byte_position (TREE_OPERAND (target, 1)), 0))
2737
2738         {
2739           offset += int_byte_position (TREE_OPERAND (target, 1));
2740           target = TREE_OPERAND (target, 0);
2741         }
2742       else if (TREE_CODE (target) == ARRAY_REF
2743                || TREE_CODE (target) == ARRAY_RANGE_REF)
2744         {
2745           offset += (tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (target)), 1)
2746                      * tree_low_cst (TREE_OPERAND (target, 1), 0));
2747           target = TREE_OPERAND (target, 0);
2748         }
2749       else
2750         break;
2751     }
2752
2753   switch (TREE_CODE (target))
2754     {
2755     case VAR_DECL:
2756     case FUNCTION_DECL:
2757       x = DECL_RTL (target);
2758       break;
2759
2760     case LABEL_DECL:
2761       x = gen_rtx_MEM (FUNCTION_MODE,
2762                        gen_rtx_LABEL_REF (Pmode, force_label_rtx (target)));
2763       break;
2764
2765     case REAL_CST:
2766     case FIXED_CST:
2767     case STRING_CST:
2768     case COMPLEX_CST:
2769     case CONSTRUCTOR:
2770     case INTEGER_CST:
2771       x = output_constant_def (target, 1);
2772       break;
2773
2774     default:
2775       gcc_unreachable ();
2776     }
2777
2778   gcc_assert (MEM_P (x));
2779   x = XEXP (x, 0);
2780
2781   value->base = x;
2782   value->offset = offset;
2783 }
2784 \f
2785
2786 static GTY((param_is (struct constant_descriptor_tree)))
2787      htab_t const_desc_htab;
2788
2789 static struct constant_descriptor_tree * build_constant_desc (tree);
2790 static void maybe_output_constant_def_contents (struct constant_descriptor_tree *, int);
2791
2792 /* Constant pool accessor function.  */
2793
2794 htab_t 
2795 constant_pool_htab (void)
2796 {
2797   return const_desc_htab;
2798 }
2799
2800 /* Compute a hash code for a constant expression.  */
2801
2802 static hashval_t
2803 const_desc_hash (const void *ptr)
2804 {
2805   return ((const struct constant_descriptor_tree *)ptr)->hash;
2806 }
2807
2808 static hashval_t
2809 const_hash_1 (const tree exp)
2810 {
2811   const char *p;
2812   hashval_t hi;
2813   int len, i;
2814   enum tree_code code = TREE_CODE (exp);
2815
2816   /* Either set P and LEN to the address and len of something to hash and
2817      exit the switch or return a value.  */
2818
2819   switch (code)
2820     {
2821     case INTEGER_CST:
2822       p = (char *) &TREE_INT_CST (exp);
2823       len = sizeof TREE_INT_CST (exp);
2824       break;
2825
2826     case REAL_CST:
2827       return real_hash (TREE_REAL_CST_PTR (exp));
2828
2829     case FIXED_CST:
2830       return fixed_hash (TREE_FIXED_CST_PTR (exp));
2831
2832     case STRING_CST:
2833       p = TREE_STRING_POINTER (exp);
2834       len = TREE_STRING_LENGTH (exp);
2835       break;
2836
2837     case COMPLEX_CST:
2838       return (const_hash_1 (TREE_REALPART (exp)) * 5
2839               + const_hash_1 (TREE_IMAGPART (exp)));
2840
2841     case CONSTRUCTOR:
2842       {
2843         unsigned HOST_WIDE_INT idx;
2844         tree value;
2845
2846         hi = 5 + int_size_in_bytes (TREE_TYPE (exp));
2847
2848         FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
2849           if (value)
2850             hi = hi * 603 + const_hash_1 (value);
2851
2852         return hi;
2853       }
2854
2855     case ADDR_EXPR:
2856     case FDESC_EXPR:
2857       {
2858         struct addr_const value;
2859
2860         decode_addr_const (exp, &value);
2861         switch (GET_CODE (value.base))
2862           {
2863           case SYMBOL_REF:
2864             /* Don't hash the address of the SYMBOL_REF;
2865                only use the offset and the symbol name.  */
2866             hi = value.offset;
2867             p = XSTR (value.base, 0);
2868             for (i = 0; p[i] != 0; i++)
2869               hi = ((hi * 613) + (unsigned) (p[i]));
2870             break;
2871
2872           case LABEL_REF:
2873             hi = value.offset + CODE_LABEL_NUMBER (XEXP (value.base, 0)) * 13;
2874             break;
2875
2876           default:
2877             gcc_unreachable ();
2878           }
2879       }
2880       return hi;
2881
2882     case PLUS_EXPR:
2883     case POINTER_PLUS_EXPR:
2884     case MINUS_EXPR:
2885       return (const_hash_1 (TREE_OPERAND (exp, 0)) * 9
2886               + const_hash_1 (TREE_OPERAND (exp, 1)));
2887
2888     CASE_CONVERT:
2889       return const_hash_1 (TREE_OPERAND (exp, 0)) * 7 + 2;
2890
2891     default:
2892       /* A language specific constant. Just hash the code.  */
2893       return code;
2894     }
2895
2896   /* Compute hashing function.  */
2897   hi = len;
2898   for (i = 0; i < len; i++)
2899     hi = ((hi * 613) + (unsigned) (p[i]));
2900
2901   return hi;
2902 }
2903
2904 /* Wrapper of compare_constant, for the htab interface.  */
2905 static int
2906 const_desc_eq (const void *p1, const void *p2)
2907 {
2908   const struct constant_descriptor_tree *const c1
2909     = (const struct constant_descriptor_tree *) p1;
2910   const struct constant_descriptor_tree *const c2
2911     = (const struct constant_descriptor_tree *) p2;
2912   if (c1->hash != c2->hash)
2913     return 0;
2914   return compare_constant (c1->value, c2->value);
2915 }
2916
2917 /* Compare t1 and t2, and return 1 only if they are known to result in
2918    the same bit pattern on output.  */
2919
2920 static int
2921 compare_constant (const tree t1, const tree t2)
2922 {
2923   enum tree_code typecode;
2924
2925   if (t1 == NULL_TREE)
2926     return t2 == NULL_TREE;
2927   if (t2 == NULL_TREE)
2928     return 0;
2929
2930   if (TREE_CODE (t1) != TREE_CODE (t2))
2931     return 0;
2932
2933   switch (TREE_CODE (t1))
2934     {
2935     case INTEGER_CST:
2936       /* Integer constants are the same only if the same width of type.  */
2937       if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
2938         return 0;
2939       if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
2940         return 0;
2941       return tree_int_cst_equal (t1, t2);
2942
2943     case REAL_CST:
2944       /* Real constants are the same only if the same width of type.  */
2945       if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
2946         return 0;
2947
2948       return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
2949
2950     case FIXED_CST:
2951       /* Fixed constants are the same only if the same width of type.  */
2952       if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
2953         return 0;
2954
2955       return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
2956
2957     case STRING_CST:
2958       if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
2959         return 0;
2960
2961       return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
2962               && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
2963                          TREE_STRING_LENGTH (t1)));
2964
2965     case COMPLEX_CST:
2966       return (compare_constant (TREE_REALPART (t1), TREE_REALPART (t2))
2967               && compare_constant (TREE_IMAGPART (t1), TREE_IMAGPART (t2)));
2968
2969     case CONSTRUCTOR:
2970       {
2971         VEC(constructor_elt, gc) *v1, *v2;
2972         unsigned HOST_WIDE_INT idx;
2973
2974         typecode = TREE_CODE (TREE_TYPE (t1));
2975         if (typecode != TREE_CODE (TREE_TYPE (t2)))
2976           return 0;
2977
2978         if (typecode == ARRAY_TYPE)
2979           {
2980             HOST_WIDE_INT size_1 = int_size_in_bytes (TREE_TYPE (t1));
2981             /* For arrays, check that the sizes all match.  */
2982             if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))
2983                 || size_1 == -1
2984                 || size_1 != int_size_in_bytes (TREE_TYPE (t2)))
2985               return 0;
2986           }
2987         else
2988           {
2989             /* For record and union constructors, require exact type
2990                equality.  */
2991             if (TREE_TYPE (t1) != TREE_TYPE (t2))
2992               return 0;
2993           }
2994
2995         v1 = CONSTRUCTOR_ELTS (t1);
2996         v2 = CONSTRUCTOR_ELTS (t2);
2997         if (VEC_length (constructor_elt, v1)
2998             != VEC_length (constructor_elt, v2))
2999             return 0;
3000
3001         for (idx = 0; idx < VEC_length (constructor_elt, v1); ++idx)
3002           {
3003             constructor_elt *c1 = VEC_index (constructor_elt, v1, idx);
3004             constructor_elt *c2 = VEC_index (constructor_elt, v2, idx);
3005
3006             /* Check that each value is the same...  */
3007             if (!compare_constant (c1->value, c2->value))
3008               return 0;
3009             /* ... and that they apply to the same fields!  */
3010             if (typecode == ARRAY_TYPE)
3011               {
3012                 if (!compare_constant (c1->index, c2->index))
3013                   return 0;
3014               }
3015             else
3016               {
3017                 if (c1->index != c2->index)
3018                   return 0;
3019               }
3020           }
3021
3022         return 1;
3023       }
3024
3025     case ADDR_EXPR:
3026     case FDESC_EXPR:
3027       {
3028         struct addr_const value1, value2;
3029
3030         decode_addr_const (t1, &value1);
3031         decode_addr_const (t2, &value2);
3032         return (value1.offset == value2.offset
3033                 && strcmp (XSTR (value1.base, 0), XSTR (value2.base, 0)) == 0);
3034       }
3035
3036     case PLUS_EXPR:
3037     case POINTER_PLUS_EXPR:
3038     case MINUS_EXPR:
3039     case RANGE_EXPR:
3040       return (compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
3041               && compare_constant(TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
3042
3043     CASE_CONVERT:
3044     case VIEW_CONVERT_EXPR:
3045       return compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3046
3047     default:
3048       return 0;
3049     }
3050
3051   gcc_unreachable ();
3052 }
3053 \f
3054 /* Make a copy of the whole tree structure for a constant.  This
3055    handles the same types of nodes that compare_constant handles.  */
3056
3057 static tree
3058 copy_constant (tree exp)
3059 {
3060   switch (TREE_CODE (exp))
3061     {
3062     case ADDR_EXPR:
3063       /* For ADDR_EXPR, we do not want to copy the decl whose address
3064          is requested.  We do want to copy constants though.  */
3065       if (CONSTANT_CLASS_P (TREE_OPERAND (exp, 0)))
3066         return build1 (TREE_CODE (exp), TREE_TYPE (exp),
3067                        copy_constant (TREE_OPERAND (exp, 0)));
3068       else
3069         return copy_node (exp);
3070
3071     case INTEGER_CST:
3072     case REAL_CST:
3073     case FIXED_CST:
3074     case STRING_CST:
3075       return copy_node (exp);
3076
3077     case COMPLEX_CST:
3078       return build_complex (TREE_TYPE (exp),
3079                             copy_constant (TREE_REALPART (exp)),
3080                             copy_constant (TREE_IMAGPART (exp)));
3081
3082     case PLUS_EXPR:
3083     case POINTER_PLUS_EXPR:
3084     case MINUS_EXPR:
3085       return build2 (TREE_CODE (exp), TREE_TYPE (exp),
3086                      copy_constant (TREE_OPERAND (exp, 0)),
3087                      copy_constant (TREE_OPERAND (exp, 1)));
3088
3089     CASE_CONVERT:
3090     case VIEW_CONVERT_EXPR:
3091       return build1 (TREE_CODE (exp), TREE_TYPE (exp),
3092                      copy_constant (TREE_OPERAND (exp, 0)));
3093
3094     case CONSTRUCTOR:
3095       {
3096         tree copy = copy_node (exp);
3097         VEC(constructor_elt, gc) *v;
3098         unsigned HOST_WIDE_INT idx;
3099         tree purpose, value;
3100
3101         v = VEC_alloc(constructor_elt, gc, VEC_length(constructor_elt,
3102                                                       CONSTRUCTOR_ELTS (exp)));
3103         FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, purpose, value)
3104           {
3105             constructor_elt *ce = VEC_quick_push (constructor_elt, v, NULL);
3106             ce->index = purpose;
3107             ce->value = copy_constant (value);
3108           }
3109         CONSTRUCTOR_ELTS (copy) = v;
3110         return copy;
3111       }
3112
3113     default:
3114       gcc_unreachable ();
3115     }
3116 }
3117 \f
3118 /* Return the alignment of constant EXP in bits.  */
3119
3120 static unsigned int
3121 get_constant_alignment (tree exp)
3122 {
3123   unsigned int align;
3124
3125   align = TYPE_ALIGN (TREE_TYPE (exp));
3126 #ifdef CONSTANT_ALIGNMENT
3127   align = CONSTANT_ALIGNMENT (exp, align);
3128 #endif
3129   return align;
3130 }
3131
3132 /* Return the section into which constant EXP should be placed.  */
3133
3134 static section *
3135 get_constant_section (tree exp)
3136 {
3137   if (IN_NAMED_SECTION (exp))
3138     return get_named_section (exp, NULL, compute_reloc_for_constant (exp));
3139   else
3140     return targetm.asm_out.select_section (exp,
3141                                            compute_reloc_for_constant (exp),
3142                                            get_constant_alignment (exp));
3143 }
3144
3145 /* Return the size of constant EXP in bytes.  */
3146
3147 static HOST_WIDE_INT
3148 get_constant_size (tree exp)
3149 {
3150   HOST_WIDE_INT size;
3151
3152   size = int_size_in_bytes (TREE_TYPE (exp));
3153   if (TREE_CODE (exp) == STRING_CST)
3154     size = MAX (TREE_STRING_LENGTH (exp), size);
3155   return size;
3156 }
3157
3158 /* Subroutine of output_constant_def:
3159    No constant equal to EXP is known to have been output.
3160    Make a constant descriptor to enter EXP in the hash table.
3161    Assign the label number and construct RTL to refer to the
3162    constant's location in memory.
3163    Caller is responsible for updating the hash table.  */
3164
3165 static struct constant_descriptor_tree *
3166 build_constant_desc (tree exp)
3167 {
3168   rtx symbol;
3169   rtx rtl;
3170   char label[256];
3171   int labelno;
3172   struct constant_descriptor_tree *desc;
3173
3174   desc = GGC_NEW (struct constant_descriptor_tree);
3175   desc->value = copy_constant (exp);
3176
3177   /* Propagate marked-ness to copied constant.  */
3178   if (flag_mudflap && mf_marked_p (exp))
3179     mf_mark (desc->value);
3180
3181   /* Create a string containing the label name, in LABEL.  */
3182   labelno = const_labelno++;
3183   ASM_GENERATE_INTERNAL_LABEL (label, "LC", labelno);
3184
3185   /* We have a symbol name; construct the SYMBOL_REF and the MEM.  */
3186   if (use_object_blocks_p ())
3187     {
3188       section *sect = get_constant_section (exp);
3189       symbol = create_block_symbol (ggc_strdup (label),
3190                                     get_block_for_section (sect), -1);
3191     }
3192   else
3193     symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3194   SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3195   SET_SYMBOL_REF_DECL (symbol, desc->value);
3196   TREE_CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3197
3198   rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (exp)), symbol);
3199   set_mem_attributes (rtl, exp, 1);
3200   set_mem_alias_set (rtl, 0);
3201   set_mem_alias_set (rtl, const_alias_set);
3202
3203   /* We cannot share RTX'es in pool entries.
3204      Mark this piece of RTL as required for unsharing.  */
3205   RTX_FLAG (rtl, used) = 1;
3206
3207   /* Set flags or add text to the name to record information, such as
3208      that it is a local symbol.  If the name is changed, the macro
3209      ASM_OUTPUT_LABELREF will have to know how to strip this
3210      information.  This call might invalidate our local variable
3211      SYMBOL; we can't use it afterward.  */
3212
3213   targetm.encode_section_info (exp, rtl, true);
3214
3215   desc->rtl = rtl;
3216
3217   return desc;
3218 }
3219
3220 /* Return an rtx representing a reference to constant data in memory
3221    for the constant expression EXP.
3222
3223    If assembler code for such a constant has already been output,
3224    return an rtx to refer to it.
3225    Otherwise, output such a constant in memory
3226    and generate an rtx for it.
3227
3228    If DEFER is nonzero, this constant can be deferred and output only
3229    if referenced in the function after all optimizations.
3230
3231    `const_desc_table' records which constants already have label strings.  */
3232
3233 rtx
3234 output_constant_def (tree exp, int defer)
3235 {
3236   struct constant_descriptor_tree *desc;
3237   struct constant_descriptor_tree key;
3238   void **loc;
3239
3240   /* Look up EXP in the table of constant descriptors.  If we didn't find
3241      it, create a new one.  */
3242   key.value = exp;
3243   key.hash = const_hash_1 (exp);
3244   loc = htab_find_slot_with_hash (const_desc_htab, &key, key.hash, INSERT);
3245
3246   desc = (struct constant_descriptor_tree *) *loc;
3247   if (desc == 0)
3248     {
3249       desc = build_constant_desc (exp);
3250       desc->hash = key.hash;
3251       *loc = desc;
3252     }
3253
3254   maybe_output_constant_def_contents (desc, defer);
3255   return desc->rtl;
3256 }
3257
3258 /* Subroutine of output_constant_def: Decide whether or not we need to
3259    output the constant DESC now, and if so, do it.  */
3260 static void
3261 maybe_output_constant_def_contents (struct constant_descriptor_tree *desc,
3262                                     int defer)
3263 {
3264   rtx symbol = XEXP (desc->rtl, 0);
3265   tree exp = desc->value;
3266
3267   if (flag_syntax_only)
3268     return;
3269
3270   if (TREE_ASM_WRITTEN (exp))
3271     /* Already output; don't do it again.  */
3272     return;
3273
3274   /* We can always defer constants as long as the context allows
3275      doing so.  */
3276   if (defer)
3277     {
3278       /* Increment n_deferred_constants if it exists.  It needs to be at
3279          least as large as the number of constants actually referred to
3280          by the function.  If it's too small we'll stop looking too early
3281          and fail to emit constants; if it's too large we'll only look
3282          through the entire function when we could have stopped earlier.  */
3283       if (cfun)
3284         n_deferred_constants++;
3285       return;
3286     }
3287
3288   output_constant_def_contents (symbol);
3289 }
3290
3291 /* Subroutine of output_constant_def_contents.  Output the definition
3292    of constant EXP, which is pointed to by label LABEL.  ALIGN is the
3293    constant's alignment in bits.  */
3294
3295 static void
3296 assemble_constant_contents (tree exp, const char *label, unsigned int align)
3297 {
3298   HOST_WIDE_INT size;
3299
3300   size = get_constant_size (exp);
3301
3302   /* Do any machine/system dependent processing of the constant.  */
3303 #ifdef ASM_DECLARE_CONSTANT_NAME
3304   ASM_DECLARE_CONSTANT_NAME (asm_out_file, label, exp, size);
3305 #else
3306   /* Standard thing is just output label for the constant.  */
3307   ASM_OUTPUT_LABEL (asm_out_file, label);
3308 #endif /* ASM_DECLARE_CONSTANT_NAME */
3309
3310   /* Output the value of EXP.  */
3311   output_constant (exp, size, align);
3312 }
3313
3314 /* We must output the constant data referred to by SYMBOL; do so.  */
3315
3316 static void
3317 output_constant_def_contents (rtx symbol)
3318 {
3319   tree exp = SYMBOL_REF_DECL (symbol);
3320   unsigned int align;
3321
3322   /* Make sure any other constants whose addresses appear in EXP
3323      are assigned label numbers.  */
3324   output_addressed_constants (exp);
3325
3326   /* We are no longer deferring this constant.  */
3327   TREE_ASM_WRITTEN (exp) = 1;
3328
3329   /* If the constant is part of an object block, make sure that the
3330      decl has been positioned within its block, but do not write out
3331      its definition yet.  output_object_blocks will do that later.  */
3332   if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
3333     place_block_symbol (symbol);
3334   else
3335     {
3336       switch_to_section (get_constant_section (exp));
3337       align = get_constant_alignment (exp);
3338       if (align > BITS_PER_UNIT)
3339         ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
3340       assemble_constant_contents (exp, XSTR (symbol, 0), align);
3341     }
3342   if (flag_mudflap)
3343     mudflap_enqueue_constant (exp);
3344 }
3345
3346 /* Look up EXP in the table of constant descriptors.  Return the rtl
3347    if it has been emitted, else null.  */
3348
3349 rtx
3350 lookup_constant_def (tree exp)
3351 {
3352   struct constant_descriptor_tree *desc;
3353   struct constant_descriptor_tree key;
3354
3355   key.value = exp;
3356   key.hash = const_hash_1 (exp);
3357   desc = (struct constant_descriptor_tree *)
3358     htab_find_with_hash (const_desc_htab, &key, key.hash);
3359
3360   return (desc ? desc->rtl : NULL_RTX);
3361 }
3362 \f
3363 /* Used in the hash tables to avoid outputting the same constant
3364    twice.  Unlike 'struct constant_descriptor_tree', RTX constants
3365    are output once per function, not once per file.  */
3366 /* ??? Only a few targets need per-function constant pools.  Most
3367    can use one per-file pool.  Should add a targetm bit to tell the
3368    difference.  */
3369
3370 struct GTY(()) rtx_constant_pool {
3371   /* Pointers to first and last constant in pool, as ordered by offset.  */
3372   struct constant_descriptor_rtx *first;
3373   struct constant_descriptor_rtx *last;
3374
3375   /* Hash facility for making memory-constants from constant rtl-expressions.
3376      It is used on RISC machines where immediate integer arguments and
3377      constant addresses are restricted so that such constants must be stored
3378      in memory.  */
3379   htab_t GTY((param_is (struct constant_descriptor_rtx))) const_rtx_htab;
3380
3381   /* Current offset in constant pool (does not include any
3382      machine-specific header).  */
3383   HOST_WIDE_INT offset;
3384 };
3385
3386 struct GTY((chain_next ("%h.next"))) constant_descriptor_rtx {
3387   struct constant_descriptor_rtx *next;
3388   rtx mem;
3389   rtx sym;
3390   rtx constant;
3391   HOST_WIDE_INT offset;
3392   hashval_t hash;
3393   enum machine_mode mode;
3394   unsigned int align;
3395   int labelno;
3396   int mark;
3397 };
3398
3399 /* Hash and compare functions for const_rtx_htab.  */
3400
3401 static hashval_t
3402 const_desc_rtx_hash (const void *ptr)
3403 {
3404   const struct constant_descriptor_rtx *const desc
3405     = (const struct constant_descriptor_rtx *) ptr;
3406   return desc->hash;
3407 }
3408
3409 static int
3410 const_desc_rtx_eq (const void *a, const void *b)
3411 {
3412   const struct constant_descriptor_rtx *const x
3413     = (const struct constant_descriptor_rtx *) a;
3414   const struct constant_descriptor_rtx *const y
3415     = (const struct constant_descriptor_rtx *) b;
3416
3417   if (x->mode != y->mode)
3418     return 0;
3419   return rtx_equal_p (x->constant, y->constant);
3420 }
3421
3422 /* This is the worker function for const_rtx_hash, called via for_each_rtx.  */
3423
3424 static int
3425 const_rtx_hash_1 (rtx *xp, void *data)
3426 {
3427   unsigned HOST_WIDE_INT hwi;
3428   enum machine_mode mode;
3429   enum rtx_code code;
3430   hashval_t h, *hp;
3431   rtx x;
3432
3433   x = *xp;
3434   code = GET_CODE (x);
3435   mode = GET_MODE (x);
3436   h = (hashval_t) code * 1048573 + mode;
3437
3438   switch (code)
3439     {
3440     case CONST_INT:
3441       hwi = INTVAL (x);
3442     fold_hwi:
3443       {
3444         int shift = sizeof (hashval_t) * CHAR_BIT;
3445         const int n = sizeof (HOST_WIDE_INT) / sizeof (hashval_t);
3446         int i;
3447
3448         h ^= (hashval_t) hwi;
3449         for (i = 1; i < n; ++i)
3450           {
3451             hwi >>= shift;
3452             h ^= (hashval_t) hwi;
3453           }
3454       }
3455       break;
3456
3457     case CONST_DOUBLE:
3458       if (mode == VOIDmode)
3459         {
3460           hwi = CONST_DOUBLE_LOW (x) ^ CONST_DOUBLE_HIGH (x);
3461           goto fold_hwi;
3462         }
3463       else
3464         h ^= real_hash (CONST_DOUBLE_REAL_VALUE (x));
3465       break;
3466
3467     case CONST_FIXED:
3468       h ^= fixed_hash (CONST_FIXED_VALUE (x));
3469       break;
3470
3471     case CONST_VECTOR:
3472       {
3473         int i;
3474         for (i = XVECLEN (x, 0); i-- > 0; )
3475           h = h * 251 + const_rtx_hash_1 (&XVECEXP (x, 0, i), data);
3476       }
3477       break;
3478
3479     case SYMBOL_REF:
3480       h ^= htab_hash_string (XSTR (x, 0));
3481       break;
3482
3483     case LABEL_REF:
3484       h = h * 251 + CODE_LABEL_NUMBER (XEXP (x, 0));
3485       break;
3486
3487     case UNSPEC:
3488     case UNSPEC_VOLATILE:
3489       h = h * 251 + XINT (x, 1);
3490       break;
3491
3492     default:
3493       break;
3494     }
3495
3496   hp = (hashval_t *) data;
3497   *hp = *hp * 509 + h;
3498   return 0;
3499 }
3500
3501 /* Compute a hash value for X, which should be a constant.  */
3502
3503 static hashval_t
3504 const_rtx_hash (rtx x)
3505 {
3506   hashval_t h = 0;
3507   for_each_rtx (&x, const_rtx_hash_1, &h);
3508   return h;
3509 }
3510
3511 \f
3512 /* Create and return a new rtx constant pool.  */
3513
3514 static struct rtx_constant_pool *
3515 create_constant_pool (void)
3516 {
3517   struct rtx_constant_pool *pool;
3518
3519   pool = GGC_NEW (struct rtx_constant_pool);
3520   pool->const_rtx_htab = htab_create_ggc (31, const_desc_rtx_hash,
3521                                           const_desc_rtx_eq, NULL);
3522   pool->first = NULL;
3523   pool->last = NULL;
3524   pool->offset = 0;
3525   return pool;
3526 }
3527
3528 /* Initialize constant pool hashing for a new function.  */
3529
3530 void
3531 init_varasm_status (void)
3532 {
3533   crtl->varasm.pool = create_constant_pool ();
3534   crtl->varasm.deferred_constants = 0;
3535 }
3536 \f
3537 /* Given a MINUS expression, simplify it if both sides
3538    include the same symbol.  */
3539
3540 rtx
3541 simplify_subtraction (rtx x)
3542 {
3543   rtx r = simplify_rtx (x);
3544   return r ? r : x;
3545 }
3546 \f
3547 /* Given a constant rtx X, make (or find) a memory constant for its value
3548    and return a MEM rtx to refer to it in memory.  */
3549
3550 rtx
3551 force_const_mem (enum machine_mode mode, rtx x)
3552 {
3553   struct constant_descriptor_rtx *desc, tmp;
3554   struct rtx_constant_pool *pool;
3555   char label[256];
3556   rtx def, symbol;
3557   hashval_t hash;
3558   unsigned int align;
3559   void **slot;
3560
3561   /* If we're not allowed to drop X into the constant pool, don't.  */
3562   if (targetm.cannot_force_const_mem (x))
3563     return NULL_RTX;
3564
3565   /* Record that this function has used a constant pool entry.  */
3566   crtl->uses_const_pool = 1;
3567
3568   /* Decide which pool to use.  */
3569   pool = (targetm.use_blocks_for_constant_p (mode, x)
3570           ? shared_constant_pool
3571           : crtl->varasm.pool);
3572
3573   /* Lookup the value in the hashtable.  */
3574   tmp.constant = x;
3575   tmp.mode = mode;
3576   hash = const_rtx_hash (x);
3577   slot = htab_find_slot_with_hash (pool->const_rtx_htab, &tmp, hash, INSERT);
3578   desc = (struct constant_descriptor_rtx *) *slot;
3579
3580   /* If the constant was already present, return its memory.  */
3581   if (desc)
3582     return copy_rtx (desc->mem);
3583
3584   /* Otherwise, create a new descriptor.  */
3585   desc = GGC_NEW (struct constant_descriptor_rtx);
3586   *slot = desc;
3587
3588   /* Align the location counter as required by EXP's data type.  */
3589   align = GET_MODE_ALIGNMENT (mode == VOIDmode ? word_mode : mode);
3590 #ifdef CONSTANT_ALIGNMENT
3591   {
3592     tree type = lang_hooks.types.type_for_mode (mode, 0);
3593     if (type != NULL_TREE)
3594       align = CONSTANT_ALIGNMENT (make_tree (type, x), align);
3595   }
3596 #endif
3597
3598   pool->offset += (align / BITS_PER_UNIT) - 1;
3599   pool->offset &= ~ ((align / BITS_PER_UNIT) - 1);
3600
3601   desc->next = NULL;
3602   desc->constant = tmp.constant;
3603   desc->offset = pool->offset;
3604   desc->hash = hash;
3605   desc->mode = mode;
3606   desc->align = align;
3607   desc->labelno = const_labelno;
3608   desc->mark = 0;
3609
3610   pool->offset += GET_MODE_SIZE (mode);
3611   if (pool->last)
3612     pool->last->next = desc;
3613   else
3614     pool->first = pool->last = desc;
3615   pool->last = desc;
3616
3617   /* Create a string containing the label name, in LABEL.  */
3618   ASM_GENERATE_INTERNAL_LABEL (label, "LC", const_labelno);
3619   ++const_labelno;
3620
3621   /* Construct the SYMBOL_REF.  Make sure to mark it as belonging to
3622      the constants pool.  */
3623   if (use_object_blocks_p () && targetm.use_blocks_for_constant_p (mode, x))
3624     {
3625       section *sect = targetm.asm_out.select_rtx_section (mode, x, align);
3626       symbol = create_block_symbol (ggc_strdup (label),
3627                                     get_block_for_section (sect), -1);
3628     }
3629   else
3630     symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3631   desc->sym = symbol;
3632   SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3633   CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3634   SET_SYMBOL_REF_CONSTANT (symbol, desc);
3635
3636   /* Construct the MEM.  */
3637   desc->mem = def = gen_const_mem (mode, symbol);
3638   set_mem_attributes (def, lang_hooks.types.type_for_mode (mode, 0), 1);
3639   set_mem_align (def, align);
3640
3641   /* If we're dropping a label to the constant pool, make sure we
3642      don't delete it.  */
3643   if (GET_CODE (x) == LABEL_REF)
3644     LABEL_PRESERVE_P (XEXP (x, 0)) = 1;
3645
3646   return copy_rtx (def);
3647 }
3648 \f
3649 /* Given a constant pool SYMBOL_REF, return the corresponding constant.  */
3650
3651 rtx
3652 get_pool_constant (rtx addr)
3653 {
3654   return SYMBOL_REF_CONSTANT (addr)->constant;
3655 }
3656
3657 /* Given a constant pool SYMBOL_REF, return the corresponding constant
3658    and whether it has been output or not.  */
3659
3660 rtx
3661 get_pool_constant_mark (rtx addr, bool *pmarked)
3662 {
3663   struct constant_descriptor_rtx *desc;
3664
3665   desc = SYMBOL_REF_CONSTANT (addr);
3666   *pmarked = (desc->mark != 0);
3667   return desc->constant;
3668 }
3669
3670 /* Similar, return the mode.  */
3671
3672 enum machine_mode
3673 get_pool_mode (const_rtx addr)
3674 {
3675   return SYMBOL_REF_CONSTANT (addr)->mode;
3676 }
3677
3678 /* Return the size of the constant pool.  */
3679
3680 int
3681 get_pool_size (void)
3682 {
3683   return crtl->varasm.pool->offset;
3684 }
3685 \f
3686 /* Worker function for output_constant_pool_1.  Emit assembly for X
3687    in MODE with known alignment ALIGN.  */
3688
3689 static void
3690 output_constant_pool_2 (enum machine_mode mode, rtx x, unsigned int align)
3691 {
3692   switch (GET_MODE_CLASS (mode))
3693     {
3694     case MODE_FLOAT:
3695     case MODE_DECIMAL_FLOAT:
3696       {
3697         REAL_VALUE_TYPE r;
3698
3699         gcc_assert (GET_CODE (x) == CONST_DOUBLE);
3700         REAL_VALUE_FROM_CONST_DOUBLE (r, x);
3701         assemble_real (r, mode, align);
3702         break;
3703       }
3704
3705     case MODE_INT:
3706     case MODE_PARTIAL_INT:
3707     case MODE_FRACT:
3708     case MODE_UFRACT:
3709     case MODE_ACCUM:
3710     case MODE_UACCUM:
3711       assemble_integer (x, GET_MODE_SIZE (mode), align, 1);
3712       break;
3713
3714     case MODE_VECTOR_FLOAT:
3715     case MODE_VECTOR_INT:
3716     case MODE_VECTOR_FRACT:
3717     case MODE_VECTOR_UFRACT:
3718     case MODE_VECTOR_ACCUM:
3719     case MODE_VECTOR_UACCUM:
3720       {
3721         int i, units;
3722         enum machine_mode submode = GET_MODE_INNER (mode);
3723         unsigned int subalign = MIN (align, GET_MODE_BITSIZE (submode));
3724
3725         gcc_assert (GET_CODE (x) == CONST_VECTOR);
3726         units = CONST_VECTOR_NUNITS (x);
3727
3728         for (i = 0; i < units; i++)
3729           {
3730             rtx elt = CONST_VECTOR_ELT (x, i);
3731             output_constant_pool_2 (submode, elt, i ? subalign : align);
3732           }
3733       }
3734       break;
3735
3736     default:
3737       gcc_unreachable ();
3738     }
3739 }
3740
3741 /* Worker function for output_constant_pool.  Emit constant DESC,
3742    giving it ALIGN bits of alignment.  */
3743
3744 static void
3745 output_constant_pool_1 (struct constant_descriptor_rtx *desc,
3746                         unsigned int align)
3747 {
3748   rtx x, tmp;
3749
3750   x = desc->constant;
3751
3752   /* See if X is a LABEL_REF (or a CONST referring to a LABEL_REF)
3753      whose CODE_LABEL has been deleted.  This can occur if a jump table
3754      is eliminated by optimization.  If so, write a constant of zero
3755      instead.  Note that this can also happen by turning the
3756      CODE_LABEL into a NOTE.  */
3757   /* ??? This seems completely and utterly wrong.  Certainly it's
3758      not true for NOTE_INSN_DELETED_LABEL, but I disbelieve proper
3759      functioning even with INSN_DELETED_P and friends.  */
3760
3761   tmp = x;
3762   switch (GET_CODE (tmp))
3763     {
3764     case CONST:
3765       if (GET_CODE (XEXP (tmp, 0)) != PLUS
3766           || GET_CODE (XEXP (XEXP (tmp, 0), 0)) != LABEL_REF)
3767         break;
3768       tmp = XEXP (XEXP (tmp, 0), 0);
3769       /* FALLTHRU  */
3770
3771     case LABEL_REF:
3772       tmp = XEXP (tmp, 0);
3773       gcc_assert (!INSN_DELETED_P (tmp));
3774       gcc_assert (!NOTE_P (tmp)
3775                   || NOTE_KIND (tmp) != NOTE_INSN_DELETED);
3776       break;
3777
3778     default:
3779       break;
3780     }
3781
3782 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3783   ASM_OUTPUT_SPECIAL_POOL_ENTRY (asm_out_file, x, desc->mode,
3784                                  align, desc->labelno, done);
3785 #endif
3786
3787   assemble_align (align);
3788
3789   /* Output the label.  */
3790   targetm.asm_out.internal_label (asm_out_file, "LC", desc->labelno);
3791
3792   /* Output the data.  */
3793   output_constant_pool_2 (desc->mode, x, align);
3794
3795   /* Make sure all constants in SECTION_MERGE and not SECTION_STRINGS
3796      sections have proper size.  */
3797   if (align > GET_MODE_BITSIZE (desc->mode)
3798       && in_section
3799       && (in_section->common.flags & SECTION_MERGE))
3800     assemble_align (align);
3801
3802 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3803  done:
3804 #endif
3805   return;
3806 }
3807
3808 /* Given a SYMBOL_REF CURRENT_RTX, mark it and all constants it refers
3809    to as used.  Emit referenced deferred strings.  This function can
3810    be used with for_each_rtx to mark all SYMBOL_REFs in an rtx.  */
3811
3812 static int
3813 mark_constant (rtx *current_rtx, void *data ATTRIBUTE_UNUSED)
3814 {
3815   rtx x = *current_rtx;
3816
3817   if (x == NULL_RTX || GET_CODE (x) != SYMBOL_REF)
3818     return 0;
3819
3820   if (CONSTANT_POOL_ADDRESS_P (x))
3821     {
3822       struct constant_descriptor_rtx *desc = SYMBOL_REF_CONSTANT (x);
3823       if (desc->mark == 0)
3824         {
3825           desc->mark = 1;
3826           for_each_rtx (&desc->constant, mark_constant, NULL);
3827         }
3828     }
3829   else if (TREE_CONSTANT_POOL_ADDRESS_P (x))
3830     {
3831       tree exp = SYMBOL_REF_DECL (x);
3832       if (!TREE_ASM_WRITTEN (exp))
3833         {
3834           n_deferred_constants--;
3835           output_constant_def_contents (x);
3836         }
3837     }
3838
3839   return -1;
3840 }
3841
3842 /* Look through appropriate parts of INSN, marking all entries in the
3843    constant pool which are actually being used.  Entries that are only
3844    referenced by other constants are also marked as used.  Emit
3845    deferred strings that are used.  */
3846
3847 static void
3848 mark_constants (rtx insn)
3849 {
3850   if (!INSN_P (insn))
3851     return;
3852
3853   /* Insns may appear inside a SEQUENCE.  Only check the patterns of
3854      insns, not any notes that may be attached.  We don't want to mark
3855      a constant just because it happens to appear in a REG_EQUIV note.  */
3856   if (GET_CODE (PATTERN (insn)) == SEQUENCE)
3857     {
3858       rtx seq = PATTERN (insn);
3859       int i, n = XVECLEN (seq, 0);
3860       for (i = 0; i < n; ++i)
3861         {
3862           rtx subinsn = XVECEXP (seq, 0, i);
3863           if (INSN_P (subinsn))
3864             for_each_rtx (&PATTERN (subinsn), mark_constant, NULL);
3865         }
3866     }
3867   else
3868     for_each_rtx (&PATTERN (insn), mark_constant, NULL);
3869 }
3870
3871 /* Look through the instructions for this function, and mark all the
3872    entries in POOL which are actually being used.  Emit deferred constants
3873    which have indeed been used.  */
3874
3875 static void
3876 mark_constant_pool (void)
3877 {
3878   rtx insn, link;
3879
3880   if (!crtl->uses_const_pool && n_deferred_constants == 0)
3881     return;
3882
3883   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3884     mark_constants (insn);
3885
3886   for (link = crtl->epilogue_delay_list;
3887        link;
3888        link = XEXP (link, 1))
3889     mark_constants (XEXP (link, 0));
3890 }
3891
3892 /* Write all the constants in POOL.  */
3893
3894 static void
3895 output_constant_pool_contents (struct rtx_constant_pool *pool)
3896 {
3897   struct constant_descriptor_rtx *desc;
3898
3899   for (desc = pool->first; desc ; desc = desc->next)
3900     if (desc->mark)
3901       {
3902         /* If the constant is part of an object_block, make sure that
3903            the constant has been positioned within its block, but do not
3904            write out its definition yet.  output_object_blocks will do
3905            that later.  */
3906         if (SYMBOL_REF_HAS_BLOCK_INFO_P (desc->sym)
3907             && SYMBOL_REF_BLOCK (desc->sym))
3908           place_block_symbol (desc->sym);
3909         else
3910           {
3911             switch_to_section (targetm.asm_out.select_rtx_section
3912                                (desc->mode, desc->constant, desc->align));
3913             output_constant_pool_1 (desc, desc->align);
3914           }
3915       }
3916 }
3917
3918 /* Mark all constants that are used in the current function, then write
3919    out the function's private constant pool.  */
3920
3921 static void
3922 output_constant_pool (const char *fnname ATTRIBUTE_UNUSED,
3923                       tree fndecl ATTRIBUTE_UNUSED)
3924 {
3925   struct rtx_constant_pool *pool = crtl->varasm.pool;
3926
3927   /* It is possible for gcc to call force_const_mem and then to later
3928      discard the instructions which refer to the constant.  In such a
3929      case we do not need to output the constant.  */
3930   mark_constant_pool ();
3931
3932 #ifdef ASM_OUTPUT_POOL_PROLOGUE
3933   ASM_OUTPUT_POOL_PROLOGUE (asm_out_file, fnname, fndecl, pool->offset);
3934 #endif
3935
3936   output_constant_pool_contents (pool);
3937
3938 #ifdef ASM_OUTPUT_POOL_EPILOGUE
3939   ASM_OUTPUT_POOL_EPILOGUE (asm_out_file, fnname, fndecl, pool->offset);
3940 #endif
3941 }
3942 \f
3943 /* Write the contents of the shared constant pool.  */
3944
3945 void
3946 output_shared_constant_pool (void)
3947 {
3948   output_constant_pool_contents (shared_constant_pool);
3949 }
3950 \f
3951 /* Determine what kind of relocations EXP may need.  */
3952
3953 int
3954 compute_reloc_for_constant (tree exp)
3955 {
3956   int reloc = 0, reloc2;
3957   tree tem;
3958
3959   switch (TREE_CODE (exp))
3960     {
3961     case ADDR_EXPR:
3962     case FDESC_EXPR:
3963       /* Go inside any operations that get_inner_reference can handle and see
3964          if what's inside is a constant: no need to do anything here for
3965          addresses of variables or functions.  */
3966       for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
3967            tem = TREE_OPERAND (tem, 0))
3968         ;
3969
3970       if (TREE_PUBLIC (tem))
3971         reloc |= 2;
3972       else
3973         reloc |= 1;
3974       break;
3975
3976     case PLUS_EXPR:
3977     case POINTER_PLUS_EXPR:
3978       reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3979       reloc |= compute_reloc_for_constant (TREE_OPERAND (exp, 1));
3980       break;
3981
3982     case MINUS_EXPR:
3983       reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3984       reloc2 = compute_reloc_for_constant (TREE_OPERAND (exp, 1));
3985       /* The difference of two local labels is computable at link time.  */
3986       if (reloc == 1 && reloc2 == 1)
3987         reloc = 0;
3988       else
3989         reloc |= reloc2;
3990       break;
3991
3992     CASE_CONVERT:
3993     case VIEW_CONVERT_EXPR:
3994       reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3995       break;
3996
3997     case CONSTRUCTOR:
3998       {
3999         unsigned HOST_WIDE_INT idx;
4000         FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
4001           if (tem != 0)
4002             reloc |= compute_reloc_for_constant (tem);
4003       }
4004       break;
4005
4006     default:
4007       break;
4008     }
4009   return reloc;
4010 }
4011
4012 /* Find all the constants whose addresses are referenced inside of EXP,
4013    and make sure assembler code with a label has been output for each one.
4014    Indicate whether an ADDR_EXPR has been encountered.  */
4015
4016 static void
4017 output_addressed_constants (tree exp)
4018 {
4019   tree tem;
4020
4021   switch (TREE_CODE (exp))
4022     {
4023     case ADDR_EXPR:
4024     case FDESC_EXPR:
4025       /* Go inside any operations that get_inner_reference can handle and see
4026          if what's inside is a constant: no need to do anything here for
4027          addresses of variables or functions.  */
4028       for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
4029            tem = TREE_OPERAND (tem, 0))
4030         ;
4031
4032       /* If we have an initialized CONST_DECL, retrieve the initializer.  */
4033       if (TREE_CODE (tem) == CONST_DECL && DECL_INITIAL (tem))
4034         tem = DECL_INITIAL (tem);
4035
4036       if (CONSTANT_CLASS_P (tem) || TREE_CODE (tem) == CONSTRUCTOR)
4037         output_constant_def (tem, 0);
4038       break;
4039
4040     case PLUS_EXPR:
4041     case POINTER_PLUS_EXPR:
4042     case MINUS_EXPR:
4043       output_addressed_constants (TREE_OPERAND (exp, 1));
4044       /* Fall through.  */
4045
4046     CASE_CONVERT:
4047     case VIEW_CONVERT_EXPR:
4048       output_addressed_constants (TREE_OPERAND (exp, 0));
4049       break;
4050
4051     case CONSTRUCTOR:
4052       {
4053         unsigned HOST_WIDE_INT idx;
4054         FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
4055           if (tem != 0)
4056             output_addressed_constants (tem);
4057       }
4058       break;
4059
4060     default:
4061       break;
4062     }
4063 }
4064 \f
4065 /* Whether a constructor CTOR is a valid static constant initializer if all
4066    its elements are.  This used to be internal to initializer_constant_valid_p
4067    and has been exposed to let other functions like categorize_ctor_elements
4068    evaluate the property while walking a constructor for other purposes.  */
4069
4070 bool
4071 constructor_static_from_elts_p (const_tree ctor)
4072 {
4073   return (TREE_CONSTANT (ctor)
4074           && (TREE_CODE (TREE_TYPE (ctor)) == UNION_TYPE
4075               || TREE_CODE (TREE_TYPE (ctor)) == RECORD_TYPE)
4076           && !VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (ctor)));
4077 }
4078
4079 /* A subroutine of initializer_constant_valid_p.  VALUE is a MINUS_EXPR,
4080    PLUS_EXPR or POINTER_PLUS_EXPR.  This looks for cases of VALUE
4081    which are valid when ENDTYPE is an integer of any size; in
4082    particular, this does not accept a pointer minus a constant.  This
4083    returns null_pointer_node if the VALUE is an absolute constant
4084    which can be used to initialize a static variable.  Otherwise it
4085    returns NULL.  */
4086
4087 static tree
4088 narrowing_initializer_constant_valid_p (tree value, tree endtype)
4089 {
4090   tree op0, op1;
4091
4092   if (!INTEGRAL_TYPE_P (endtype))
4093     return NULL_TREE;
4094
4095   op0 = TREE_OPERAND (value, 0);
4096   op1 = TREE_OPERAND (value, 1);
4097
4098   /* Like STRIP_NOPS except allow the operand mode to widen.  This
4099      works around a feature of fold that simplifies (int)(p1 - p2) to
4100      ((int)p1 - (int)p2) under the theory that the narrower operation
4101      is cheaper.  */
4102
4103   while (CONVERT_EXPR_P (op0)
4104          || TREE_CODE (op0) == NON_LVALUE_EXPR)
4105     {
4106       tree inner = TREE_OPERAND (op0, 0);
4107       if (inner == error_mark_node
4108           || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4109           || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))
4110               > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
4111         break;
4112       op0 = inner;
4113     }
4114
4115   while (CONVERT_EXPR_P (op1)
4116          || TREE_CODE (op1) == NON_LVALUE_EXPR)
4117     {
4118       tree inner = TREE_OPERAND (op1, 0);
4119       if (inner == error_mark_node
4120           || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4121           || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op1)))
4122               > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
4123         break;
4124       op1 = inner;
4125     }
4126
4127   op0 = initializer_constant_valid_p (op0, endtype);
4128   op1 = initializer_constant_valid_p (op1, endtype);
4129
4130   /* Both initializers must be known.  */
4131   if (op0 && op1)
4132     {
4133       if (op0 == op1
4134           && (op0 == null_pointer_node
4135               || TREE_CODE (value) == MINUS_EXPR))
4136         return null_pointer_node;
4137
4138       /* Support differences between labels.  */
4139       if (TREE_CODE (op0) == LABEL_DECL
4140           && TREE_CODE (op1) == LABEL_DECL)
4141         return null_pointer_node;
4142
4143       if (TREE_CODE (op0) == STRING_CST
4144           && TREE_CODE (op1) == STRING_CST
4145           && operand_equal_p (op0, op1, 1))
4146         return null_pointer_node;
4147     }
4148
4149   return NULL_TREE;
4150 }
4151
4152 /* Return nonzero if VALUE is a valid constant-valued expression
4153    for use in initializing a static variable; one that can be an
4154    element of a "constant" initializer.
4155
4156    Return null_pointer_node if the value is absolute;
4157    if it is relocatable, return the variable that determines the relocation.
4158    We assume that VALUE has been folded as much as possible;
4159    therefore, we do not need to check for such things as
4160    arithmetic-combinations of integers.  */
4161
4162 tree
4163 initializer_constant_valid_p (tree value, tree endtype)
4164 {
4165   tree ret;
4166
4167   switch (TREE_CODE (value))
4168     {
4169     case CONSTRUCTOR:
4170       if (constructor_static_from_elts_p (value))
4171         {
4172           unsigned HOST_WIDE_INT idx;
4173           tree elt;
4174           bool absolute = true;
4175
4176           FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
4177             {
4178               tree reloc;
4179               reloc = initializer_constant_valid_p (elt, TREE_TYPE (elt));
4180               if (!reloc)
4181                 return NULL_TREE;
4182               if (reloc != null_pointer_node)
4183                 absolute = false;
4184             }
4185           /* For a non-absolute relocation, there is no single
4186              variable that can be "the variable that determines the
4187              relocation."  */
4188           return absolute ? null_pointer_node : error_mark_node;
4189         }
4190
4191       return TREE_STATIC (value) ? null_pointer_node : NULL_TREE;
4192
4193     case INTEGER_CST:
4194     case VECTOR_CST:
4195     case REAL_CST:
4196     case FIXED_CST:
4197     case STRING_CST:
4198     case COMPLEX_CST:
4199       return null_pointer_node;
4200
4201     case ADDR_EXPR:
4202     case FDESC_EXPR:
4203       {
4204         tree op0 = staticp (TREE_OPERAND (value, 0));
4205         if (op0)
4206           {
4207             /* "&(*a).f" is like unto pointer arithmetic.  If "a" turns out
4208                to be a constant, this is old-skool offsetof-like nonsense.  */
4209             if (TREE_CODE (op0) == INDIRECT_REF
4210                 && TREE_CONSTANT (TREE_OPERAND (op0, 0)))
4211               return null_pointer_node;
4212             /* Taking the address of a nested function involves a trampoline,
4213                unless we don't need or want one.  */
4214             if (TREE_CODE (op0) == FUNCTION_DECL
4215                 && decl_function_context (op0)
4216                 && !DECL_NO_STATIC_CHAIN (op0)
4217                 && !TREE_NO_TRAMPOLINE (value))
4218               return NULL_TREE;
4219             /* "&{...}" requires a temporary to hold the constructed
4220                object.  */
4221             if (TREE_CODE (op0) == CONSTRUCTOR)
4222               return NULL_TREE;
4223           }
4224         return op0;
4225       }
4226
4227     case NON_LVALUE_EXPR:
4228       return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4229
4230     case VIEW_CONVERT_EXPR:
4231       {
4232         tree src = TREE_OPERAND (value, 0);
4233         tree src_type = TREE_TYPE (src);
4234         tree dest_type = TREE_TYPE (value);
4235
4236         /* Allow view-conversions from aggregate to non-aggregate type only
4237            if the bit pattern is fully preserved afterwards; otherwise, the
4238            RTL expander won't be able to apply a subsequent transformation
4239            to the underlying constructor.  */
4240         if (AGGREGATE_TYPE_P (src_type) && !AGGREGATE_TYPE_P (dest_type))
4241           {
4242             if (TYPE_MODE (endtype) == TYPE_MODE (dest_type))
4243               return initializer_constant_valid_p (src, endtype);
4244             else
4245               return NULL_TREE;
4246           }
4247
4248         /* Allow all other kinds of view-conversion.  */
4249         return initializer_constant_valid_p (src, endtype);
4250       }
4251
4252     CASE_CONVERT:
4253       {
4254         tree src = TREE_OPERAND (value, 0);
4255         tree src_type = TREE_TYPE (src);
4256         tree dest_type = TREE_TYPE (value);
4257
4258         /* Allow conversions between pointer types, floating-point
4259            types, and offset types.  */
4260         if ((POINTER_TYPE_P (dest_type) && POINTER_TYPE_P (src_type))
4261             || (FLOAT_TYPE_P (dest_type) && FLOAT_TYPE_P (src_type))
4262             || (TREE_CODE (dest_type) == OFFSET_TYPE
4263                 && TREE_CODE (src_type) == OFFSET_TYPE))
4264           return initializer_constant_valid_p (src, endtype);
4265
4266         /* Allow length-preserving conversions between integer types.  */
4267         if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type)
4268             && (TYPE_PRECISION (dest_type) == TYPE_PRECISION (src_type)))
4269           return initializer_constant_valid_p (src, endtype);
4270
4271         /* Allow conversions between other integer types only if
4272            explicit value.  */
4273         if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type))
4274           {
4275             tree inner = initializer_constant_valid_p (src, endtype);
4276             if (inner == null_pointer_node)
4277               return null_pointer_node;
4278             break;
4279           }
4280
4281         /* Allow (int) &foo provided int is as wide as a pointer.  */
4282         if (INTEGRAL_TYPE_P (dest_type) && POINTER_TYPE_P (src_type)
4283             && (TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type)))
4284           return initializer_constant_valid_p (src, endtype);
4285
4286         /* Likewise conversions from int to pointers, but also allow
4287            conversions from 0.  */
4288         if ((POINTER_TYPE_P (dest_type)
4289              || TREE_CODE (dest_type) == OFFSET_TYPE)
4290             && INTEGRAL_TYPE_P (src_type))
4291           {
4292             if (TREE_CODE (src) == INTEGER_CST
4293                 && TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type))
4294               return null_pointer_node;
4295             if (integer_zerop (src))
4296               return null_pointer_node;
4297             else if (TYPE_PRECISION (dest_type) <= TYPE_PRECISION (src_type))
4298               return initializer_constant_valid_p (src, endtype);
4299           }
4300
4301         /* Allow conversions to struct or union types if the value
4302            inside is okay.  */
4303         if (TREE_CODE (dest_type) == RECORD_TYPE
4304             || TREE_CODE (dest_type) == UNION_TYPE)
4305           return initializer_constant_valid_p (src, endtype);
4306       }
4307       break;
4308
4309     case POINTER_PLUS_EXPR:
4310     case PLUS_EXPR:
4311       if (! INTEGRAL_TYPE_P (endtype)
4312           || TYPE_PRECISION (endtype) >= POINTER_SIZE)
4313         {
4314           tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
4315                                                       endtype);
4316           tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
4317                                                       endtype);
4318           /* If either term is absolute, use the other term's relocation.  */
4319           if (valid0 == null_pointer_node)
4320             return valid1;
4321           if (valid1 == null_pointer_node)
4322             return valid0;
4323         }
4324
4325       /* Support narrowing pointer differences.  */
4326       ret = narrowing_initializer_constant_valid_p (value, endtype);
4327       if (ret != NULL_TREE)
4328         return ret;
4329
4330       break;
4331
4332     case MINUS_EXPR:
4333       if (! INTEGRAL_TYPE_P (endtype)
4334           || TYPE_PRECISION (endtype) >= POINTER_SIZE)
4335         {
4336           tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
4337                                                       endtype);
4338           tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
4339                                                       endtype);
4340           /* Win if second argument is absolute.  */
4341           if (valid1 == null_pointer_node)
4342             return valid0;
4343           /* Win if both arguments have the same relocation.
4344              Then the value is absolute.  */
4345           if (valid0 == valid1 && valid0 != 0)
4346             return null_pointer_node;
4347
4348           /* Since GCC guarantees that string constants are unique in the
4349              generated code, a subtraction between two copies of the same
4350              constant string is absolute.  */
4351           if (valid0 && TREE_CODE (valid0) == STRING_CST
4352               && valid1 && TREE_CODE (valid1) == STRING_CST
4353               && operand_equal_p (valid0, valid1, 1))
4354             return null_pointer_node;
4355         }
4356
4357       /* Support narrowing differences.  */
4358       ret = narrowing_initializer_constant_valid_p (value, endtype);
4359       if (ret != NULL_TREE)
4360         return ret;
4361
4362       break;
4363
4364     default:
4365       break;
4366     }
4367
4368   return 0;
4369 }
4370 \f
4371 /* Return true if VALUE is a valid constant-valued expression
4372    for use in initializing a static bit-field; one that can be
4373    an element of a "constant" initializer.  */
4374
4375 bool
4376 initializer_constant_valid_for_bitfield_p (tree value)
4377 {
4378   /* For bitfields we support integer constants or possibly nested aggregates
4379      of such.  */
4380   switch (TREE_CODE (value))
4381     {
4382     case CONSTRUCTOR:
4383       {
4384         unsigned HOST_WIDE_INT idx;
4385         tree elt;
4386
4387         FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
4388           if (!initializer_constant_valid_for_bitfield_p (elt))
4389             return false;
4390         return true;
4391       }
4392
4393     case INTEGER_CST:
4394       return true;
4395
4396     case VIEW_CONVERT_EXPR:
4397     case NON_LVALUE_EXPR:
4398       return
4399         initializer_constant_valid_for_bitfield_p (TREE_OPERAND (value, 0));
4400
4401     default:
4402       break;
4403     }
4404
4405   return false;
4406 }
4407
4408 /* output_constructor outer state of relevance in recursive calls, typically
4409    for nested aggregate bitfields.  */
4410
4411 typedef struct {
4412   unsigned int bit_offset;  /* current position in ...  */
4413   int byte;                 /* ... the outer byte buffer.  */
4414 } oc_outer_state;
4415
4416 static unsigned HOST_WIDE_INT
4417   output_constructor (tree, unsigned HOST_WIDE_INT, unsigned int,
4418                       oc_outer_state *);
4419
4420 /* Output assembler code for constant EXP to FILE, with no label.
4421    This includes the pseudo-op such as ".int" or ".byte", and a newline.
4422    Assumes output_addressed_constants has been done on EXP already.
4423
4424    Generate exactly SIZE bytes of assembler data, padding at the end
4425    with zeros if necessary.  SIZE must always be specified.
4426
4427    SIZE is important for structure constructors,
4428    since trailing members may have been omitted from the constructor.
4429    It is also important for initialization of arrays from string constants
4430    since the full length of the string constant might not be wanted.
4431    It is also needed for initialization of unions, where the initializer's
4432    type is just one member, and that may not be as long as the union.
4433
4434    There a case in which we would fail to output exactly SIZE bytes:
4435    for a structure constructor that wants to produce more than SIZE bytes.
4436    But such constructors will never be generated for any possible input.
4437
4438    ALIGN is the alignment of the data in bits.  */
4439
4440 void
4441 output_constant (tree exp, unsigned HOST_WIDE_INT size, unsigned int align)
4442 {
4443   enum tree_code code;
4444   unsigned HOST_WIDE_INT thissize;
4445
4446   if (size == 0 || flag_syntax_only)
4447     return;
4448
4449   /* See if we're trying to initialize a pointer in a non-default mode
4450      to the address of some declaration somewhere.  If the target says
4451      the mode is valid for pointers, assume the target has a way of
4452      resolving it.  */
4453   if (TREE_CODE (exp) == NOP_EXPR
4454       && POINTER_TYPE_P (TREE_TYPE (exp))
4455       && targetm.valid_pointer_mode (TYPE_MODE (TREE_TYPE (exp))))
4456     {
4457       tree saved_type = TREE_TYPE (exp);
4458
4459       /* Peel off any intermediate conversions-to-pointer for valid
4460          pointer modes.  */
4461       while (TREE_CODE (exp) == NOP_EXPR
4462              && POINTER_TYPE_P (TREE_TYPE (exp))
4463              && targetm.valid_pointer_mode (TYPE_MODE (TREE_TYPE (exp))))
4464         exp = TREE_OPERAND (exp, 0);
4465
4466       /* If what we're left with is the address of something, we can
4467          convert the address to the final type and output it that
4468          way.  */
4469       if (TREE_CODE (exp) == ADDR_EXPR)
4470         exp = build1 (ADDR_EXPR, saved_type, TREE_OPERAND (exp, 0));
4471       /* Likewise for constant ints.  */
4472       else if (TREE_CODE (exp) == INTEGER_CST)
4473         exp = build_int_cst_wide (saved_type, TREE_INT_CST_LOW (exp),
4474                                   TREE_INT_CST_HIGH (exp));
4475       
4476     }
4477
4478   /* Eliminate any conversions since we'll be outputting the underlying
4479      constant.  */
4480   while (CONVERT_EXPR_P (exp)
4481          || TREE_CODE (exp) == NON_LVALUE_EXPR
4482          || TREE_CODE (exp) == VIEW_CONVERT_EXPR)
4483     {
4484       HOST_WIDE_INT type_size = int_size_in_bytes (TREE_TYPE (exp));
4485       HOST_WIDE_INT op_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (exp, 0)));
4486
4487       /* Make sure eliminating the conversion is really a no-op, except with
4488          VIEW_CONVERT_EXPRs to allow for wild Ada unchecked conversions and
4489          union types to allow for Ada unchecked unions.  */
4490       if (type_size > op_size
4491           && TREE_CODE (exp) != VIEW_CONVERT_EXPR
4492           && TREE_CODE (TREE_TYPE (exp)) != UNION_TYPE)
4493         /* Keep the conversion. */
4494         break;
4495       else
4496         exp = TREE_OPERAND (exp, 0);
4497     }
4498
4499   code = TREE_CODE (TREE_TYPE (exp));
4500   thissize = int_size_in_bytes (TREE_TYPE (exp));
4501
4502   /* Allow a constructor with no elements for any data type.
4503      This means to fill the space with zeros.  */
4504   if (TREE_CODE (exp) == CONSTRUCTOR
4505       && VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (exp)))
4506     {
4507       assemble_zeros (size);
4508       return;
4509     }
4510
4511   if (TREE_CODE (exp) == FDESC_EXPR)
4512     {
4513 #ifdef ASM_OUTPUT_FDESC
4514       HOST_WIDE_INT part = tree_low_cst (TREE_OPERAND (exp, 1), 0);
4515       tree decl = TREE_OPERAND (exp, 0);
4516       ASM_OUTPUT_FDESC (asm_out_file, decl, part);
4517 #else
4518       gcc_unreachable ();
4519 #endif
4520       return;
4521     }
4522
4523   /* Now output the underlying data.  If we've handling the padding, return.
4524      Otherwise, break and ensure SIZE is the size written.  */
4525   switch (code)
4526     {
4527     case BOOLEAN_TYPE:
4528     case INTEGER_TYPE:
4529     case ENUMERAL_TYPE:
4530     case POINTER_TYPE:
4531     case REFERENCE_TYPE:
4532     case OFFSET_TYPE:
4533     case FIXED_POINT_TYPE:
4534       if (! assemble_integer (expand_expr (exp, NULL_RTX, VOIDmode,
4535                                            EXPAND_INITIALIZER),
4536                               MIN (size, thissize), align, 0))
4537         error ("initializer for integer/fixed-point value is too complicated");
4538       break;
4539
4540     case REAL_TYPE:
4541       if (TREE_CODE (exp) != REAL_CST)
4542         error ("initializer for floating value is not a floating constant");
4543
4544       assemble_real (TREE_REAL_CST (exp), TYPE_MODE (TREE_TYPE (exp)), align);
4545       break;
4546
4547     case COMPLEX_TYPE:
4548       output_constant (TREE_REALPART (exp), thissize / 2, align);
4549       output_constant (TREE_IMAGPART (exp), thissize / 2,
4550                        min_align (align, BITS_PER_UNIT * (thissize / 2)));
4551       break;
4552
4553     case ARRAY_TYPE:
4554     case VECTOR_TYPE:
4555       switch (TREE_CODE (exp))
4556         {
4557         case CONSTRUCTOR:
4558             output_constructor (exp, size, align, NULL);
4559           return;
4560         case STRING_CST:
4561           thissize = MIN ((unsigned HOST_WIDE_INT)TREE_STRING_LENGTH (exp),
4562                           size);
4563           assemble_string (TREE_STRING_POINTER (exp), thissize);
4564           break;
4565
4566         case VECTOR_CST:
4567           {
4568             int elt_size;
4569             tree link;
4570             unsigned int nalign;
4571             enum machine_mode inner;
4572
4573             inner = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
4574             nalign = MIN (align, GET_MODE_ALIGNMENT (inner));
4575
4576             elt_size = GET_MODE_SIZE (inner);
4577
4578             link = TREE_VECTOR_CST_ELTS (exp);
4579             output_constant (TREE_VALUE (link), elt_size, align);
4580             thissize = elt_size;
4581             while ((link = TREE_CHAIN (link)) != NULL)
4582               {
4583                 output_constant (TREE_VALUE (link), elt_size, nalign);
4584                 thissize += elt_size;
4585               }
4586             break;
4587           }
4588         default:
4589           gcc_unreachable ();
4590         }
4591       break;
4592
4593     case RECORD_TYPE:
4594     case UNION_TYPE:
4595       gcc_assert (TREE_CODE (exp) == CONSTRUCTOR);
4596       output_constructor (exp, size, align, NULL);
4597       return;
4598
4599     case ERROR_MARK:
4600       return;
4601
4602     default:
4603       gcc_unreachable ();
4604     }
4605
4606   if (size > thissize)
4607     assemble_zeros (size - thissize);
4608 }
4609
4610 \f
4611 /* Subroutine of output_constructor, used for computing the size of
4612    arrays of unspecified length.  VAL must be a CONSTRUCTOR of an array
4613    type with an unspecified upper bound.  */
4614
4615 static unsigned HOST_WIDE_INT
4616 array_size_for_constructor (tree val)
4617 {
4618   tree max_index, i;
4619   unsigned HOST_WIDE_INT cnt;
4620   tree index, value, tmp;
4621
4622   /* This code used to attempt to handle string constants that are not
4623      arrays of single-bytes, but nothing else does, so there's no point in
4624      doing it here.  */
4625   if (TREE_CODE (val) == STRING_CST)
4626     return TREE_STRING_LENGTH (val);
4627
4628   max_index = NULL_TREE;
4629   FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (val), cnt, index, value)
4630     {
4631       if (TREE_CODE (index) == RANGE_EXPR)
4632         index = TREE_OPERAND (index, 1);
4633       if (max_index == NULL_TREE || tree_int_cst_lt (max_index, index))
4634         max_index = index;
4635     }
4636
4637   if (max_index == NULL_TREE)
4638     return 0;
4639
4640   /* Compute the total number of array elements.  */
4641   tmp = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (val)));
4642   i = size_binop (MINUS_EXPR, fold_convert (sizetype, max_index),
4643                   fold_convert (sizetype, tmp));
4644   i = size_binop (PLUS_EXPR, i, build_int_cst (sizetype, 1));
4645
4646   /* Multiply by the array element unit size to find number of bytes.  */
4647   i = size_binop (MULT_EXPR, i, TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (val))));
4648
4649   return tree_low_cst (i, 1);
4650 }
4651
4652 /* Other datastructures + helpers for output_constructor.  */
4653
4654 /* output_constructor local state to support interaction with helpers.  */
4655
4656 typedef struct {
4657
4658   /* Received arguments.  */
4659   tree exp;                     /* Constructor expression.  */
4660   unsigned HOST_WIDE_INT size;  /* # bytes to output - pad if necessary.  */
4661   unsigned int align;           /* Known initial alignment.  */
4662
4663   /* Constructor expression data.  */
4664   tree type;       /* Expression type.  */
4665   tree field;      /* Current field decl in a record.  */
4666   tree min_index;  /* Lower bound if specified for an array.  */
4667
4668   /* Output processing state.  */
4669   HOST_WIDE_INT total_bytes;  /* # bytes output so far / current position.  */
4670   bool byte_buffer_in_use;    /* Whether byte ...  */
4671   int byte;                   /* ... contains part of a bitfield byte yet to
4672                                  be output.  */
4673
4674   int last_relative_index;    /* Implicit or explicit index of the last
4675                                  array element output within a bitfield.  */
4676   /* Current element.  */
4677   tree val;    /* Current element value.  */
4678   tree index;  /* Current element index.  */
4679
4680 } oc_local_state;
4681
4682 /* Helper for output_constructor.  From the current LOCAL state, output a
4683    RANGE_EXPR element.  */
4684
4685 static void
4686 output_constructor_array_range (oc_local_state *local)
4687 {
4688   unsigned HOST_WIDE_INT fieldsize
4689     = int_size_in_bytes (TREE_TYPE (local->type));
4690
4691   HOST_WIDE_INT lo_index
4692     = tree_low_cst (TREE_OPERAND (local->index, 0), 0);
4693   HOST_WIDE_INT hi_index
4694     = tree_low_cst (TREE_OPERAND (local->index, 1), 0);
4695   HOST_WIDE_INT index;
4696
4697   unsigned int align2
4698     = min_align (local->align, fieldsize * BITS_PER_UNIT);
4699   
4700   for (index = lo_index; index <= hi_index; index++)
4701     {
4702       /* Output the element's initial value.  */
4703       if (local->val == NULL_TREE)
4704         assemble_zeros (fieldsize);
4705       else
4706         output_constant (local->val, fieldsize, align2);
4707       
4708       /* Count its size.  */
4709       local->total_bytes += fieldsize;
4710     }
4711 }
4712
4713 /* Helper for output_constructor.  From the current LOCAL state, output a
4714    field element that is not true bitfield or part of an outer one.  */
4715
4716 static void
4717 output_constructor_regular_field (oc_local_state *local)
4718 {
4719   /* Field size and position.  Since this structure is static, we know the
4720      positions are constant.  */
4721   unsigned HOST_WIDE_INT fieldsize;
4722   HOST_WIDE_INT fieldpos;
4723
4724   unsigned int align2;
4725
4726   if (local->index != NULL_TREE)
4727     fieldpos = (tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (local->val)), 1)
4728                 * ((tree_low_cst (local->index, 0) 
4729                     - tree_low_cst (local->min_index, 0))));
4730   else if (local->field != NULL_TREE) 
4731     fieldpos = int_byte_position (local->field);
4732   else
4733     fieldpos = 0; 
4734
4735   /* Output any buffered-up bit-fields preceding this element.  */
4736   if (local->byte_buffer_in_use)
4737     {
4738       assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
4739       local->total_bytes++;
4740       local->byte_buffer_in_use = false;
4741     }
4742   
4743   /* Advance to offset of this element.
4744      Note no alignment needed in an array, since that is guaranteed
4745      if each element has the proper size.  */
4746   if ((local->field != NULL_TREE || local->index != NULL_TREE)
4747       && fieldpos != local->total_bytes)
4748     {
4749       gcc_assert (fieldpos >= local->total_bytes);
4750       assemble_zeros (fieldpos - local->total_bytes);
4751       local->total_bytes = fieldpos;
4752     }
4753   
4754   /* Find the alignment of this element.  */
4755   align2 = min_align (local->align, BITS_PER_UNIT * fieldpos);
4756
4757   /* Determine size this element should occupy.  */
4758   if (local->field)
4759     {
4760       fieldsize = 0;
4761       
4762       /* If this is an array with an unspecified upper bound,
4763          the initializer determines the size.  */
4764       /* ??? This ought to only checked if DECL_SIZE_UNIT is NULL,
4765          but we cannot do this until the deprecated support for
4766          initializing zero-length array members is removed.  */
4767       if (TREE_CODE (TREE_TYPE (local->field)) == ARRAY_TYPE
4768           && TYPE_DOMAIN (TREE_TYPE (local->field))
4769           && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (local->field))))
4770         {
4771           fieldsize = array_size_for_constructor (local->val);
4772           /* Given a non-empty initialization, this field had
4773              better be last.  */
4774           gcc_assert (!fieldsize || !TREE_CHAIN (local->field));
4775         }
4776       else if (DECL_SIZE_UNIT (local->field))
4777         {
4778           /* ??? This can't be right.  If the decl size overflows
4779              a host integer we will silently emit no data.  */
4780           if (host_integerp (DECL_SIZE_UNIT (local->field), 1))
4781             fieldsize = tree_low_cst (DECL_SIZE_UNIT (local->field), 1);
4782         }
4783     }
4784   else
4785     fieldsize = int_size_in_bytes (TREE_TYPE (local->type));
4786   
4787   /* Output the element's initial value.  */
4788   if (local->val == NULL_TREE)
4789     assemble_zeros (fieldsize);
4790   else
4791     output_constant (local->val, fieldsize, align2);
4792
4793   /* Count its size.  */
4794   local->total_bytes += fieldsize;
4795 }
4796
4797 /* Helper for output_constructor.  From the current LOCAL and OUTER states,
4798    output an element that is a true bitfield or part of an outer one.  */
4799
4800 static void
4801 output_constructor_bitfield (oc_local_state *local, oc_outer_state *outer)
4802 {
4803   /* Bit size of this element.  */
4804   HOST_WIDE_INT ebitsize
4805     = (local->field
4806        ? tree_low_cst (DECL_SIZE (local->field), 1)
4807        : tree_low_cst (TYPE_SIZE (TREE_TYPE (local->type)), 1));
4808
4809   /* Relative index of this element if this is an array component.  */
4810   HOST_WIDE_INT relative_index
4811     = (!local->field
4812        ? (local->index
4813           ? (tree_low_cst (local->index, 0) 
4814              - tree_low_cst (local->min_index, 0))
4815           : local->last_relative_index + 1)
4816        : 0);
4817   
4818   /* Bit position of this element from the start of the containing
4819      constructor.  */
4820   HOST_WIDE_INT constructor_relative_ebitpos
4821       = (local->field
4822          ? int_bit_position (local->field) 
4823          : ebitsize * relative_index);
4824   
4825   /* Bit position of this element from the start of a possibly ongoing
4826      outer byte buffer.  */
4827   HOST_WIDE_INT byte_relative_ebitpos
4828       = ((outer ? outer->bit_offset : 0) + constructor_relative_ebitpos);
4829
4830   /* From the start of a possibly ongoing outer byte buffer, offsets to 
4831      the first bit of this element and to the first bit past the end of
4832      this element.  */
4833   HOST_WIDE_INT next_offset = byte_relative_ebitpos;
4834   HOST_WIDE_INT end_offset = byte_relative_ebitpos + ebitsize;
4835   
4836   local->last_relative_index = relative_index;
4837   
4838   if (local->val == NULL_TREE)
4839     local->val = integer_zero_node;
4840   
4841   while (TREE_CODE (local->val) == VIEW_CONVERT_EXPR
4842          || TREE_CODE (local->val) == NON_LVALUE_EXPR)
4843     local->val = TREE_OPERAND (local->val, 0);
4844     
4845   if (TREE_CODE (local->val) != INTEGER_CST
4846       && TREE_CODE (local->val) != CONSTRUCTOR)
4847     {
4848       error ("invalid initial value for member %qE", DECL_NAME (local->field));
4849       return;
4850     }
4851
4852   /* If this field does not start in this (or, next) byte,
4853      skip some bytes.  */
4854   if (next_offset / BITS_PER_UNIT != local->total_bytes)
4855     {
4856       /* Output remnant of any bit field in previous bytes.  */
4857       if (local->byte_buffer_in_use)
4858         {
4859           assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
4860           local->total_bytes++;
4861           local->byte_buffer_in_use = false;
4862         }
4863       
4864       /* If still not at proper byte, advance to there.  */
4865       if (next_offset / BITS_PER_UNIT != local->total_bytes)
4866         {
4867           gcc_assert (next_offset / BITS_PER_UNIT >= local->total_bytes);
4868           assemble_zeros (next_offset / BITS_PER_UNIT - local->total_bytes);
4869           local->total_bytes = next_offset / BITS_PER_UNIT;
4870         }
4871     }
4872   
4873   /* Set up the buffer if necessary.  */
4874   if (!local->byte_buffer_in_use)
4875     {
4876       local->byte = 0;
4877       if (ebitsize > 0)
4878         local->byte_buffer_in_use = true;
4879     }
4880   
4881   /* If this is nested constructor, recurse passing the bit offset and the
4882      pending data, then retrieve the new pending data afterwards.  */
4883   if (TREE_CODE (local->val) == CONSTRUCTOR)
4884     {
4885       oc_outer_state output_state;
4886
4887       output_state.bit_offset = next_offset % BITS_PER_UNIT;
4888       output_state.byte = local->byte;
4889       local->total_bytes
4890           += output_constructor (local->val, 0, 0, &output_state);
4891       local->byte = output_state.byte;
4892       return;
4893     }
4894   
4895   /* Otherwise, we must split the element into pieces that fall within
4896      separate bytes, and combine each byte with previous or following
4897      bit-fields.  */  
4898   while (next_offset < end_offset)
4899     {
4900       int this_time;
4901       int shift;
4902       HOST_WIDE_INT value;
4903       HOST_WIDE_INT next_byte = next_offset / BITS_PER_UNIT;
4904       HOST_WIDE_INT next_bit = next_offset % BITS_PER_UNIT;
4905       
4906       /* Advance from byte to byte
4907          within this element when necessary.  */
4908       while (next_byte != local->total_bytes)
4909         {
4910           assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
4911           local->total_bytes++;
4912           local->byte = 0;
4913         }
4914       
4915       /* Number of bits we can process at once
4916          (all part of the same byte).  */
4917       this_time = MIN (end_offset - next_offset,
4918                        BITS_PER_UNIT - next_bit);
4919       if (BYTES_BIG_ENDIAN)
4920         {
4921           /* On big-endian machine, take the most significant bits
4922              first (of the bits that are significant)
4923              and put them into bytes from the most significant end.  */
4924           shift = end_offset - next_offset - this_time;
4925           
4926           /* Don't try to take a bunch of bits that cross
4927              the word boundary in the INTEGER_CST. We can
4928              only select bits from the LOW or HIGH part
4929              not from both.  */
4930           if (shift < HOST_BITS_PER_WIDE_INT
4931               && shift + this_time > HOST_BITS_PER_WIDE_INT)
4932             {
4933               this_time = shift + this_time - HOST_BITS_PER_WIDE_INT;
4934               shift = HOST_BITS_PER_WIDE_INT;
4935             }
4936           
4937           /* Now get the bits from the appropriate constant word.  */
4938           if (shift < HOST_BITS_PER_WIDE_INT)
4939             value = TREE_INT_CST_LOW (local->val);
4940           else
4941             {
4942               gcc_assert (shift < 2 * HOST_BITS_PER_WIDE_INT);
4943               value = TREE_INT_CST_HIGH (local->val);
4944               shift -= HOST_BITS_PER_WIDE_INT;
4945             }
4946           
4947           /* Get the result. This works only when:
4948              1 <= this_time <= HOST_BITS_PER_WIDE_INT.  */
4949           local->byte |= (((value >> shift)
4950                            & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
4951                           << (BITS_PER_UNIT - this_time - next_bit));
4952         }
4953       else
4954         {
4955           /* On little-endian machines,
4956              take first the least significant bits of the value
4957              and pack them starting at the least significant
4958              bits of the bytes.  */
4959           shift = next_offset - byte_relative_ebitpos;
4960           
4961           /* Don't try to take a bunch of bits that cross
4962              the word boundary in the INTEGER_CST. We can
4963              only select bits from the LOW or HIGH part
4964              not from both.  */
4965           if (shift < HOST_BITS_PER_WIDE_INT
4966               && shift + this_time > HOST_BITS_PER_WIDE_INT)
4967             this_time = (HOST_BITS_PER_WIDE_INT - shift);
4968           
4969           /* Now get the bits from the appropriate constant word.  */
4970           if (shift < HOST_BITS_PER_WIDE_INT)
4971             value = TREE_INT_CST_LOW (local->val);
4972           else
4973             {
4974               gcc_assert (shift < 2 * HOST_BITS_PER_WIDE_INT);
4975               value = TREE_INT_CST_HIGH (local->val);
4976               shift -= HOST_BITS_PER_WIDE_INT;
4977             }
4978           
4979           /* Get the result. This works only when:
4980              1 <= this_time <= HOST_BITS_PER_WIDE_INT.  */
4981           local->byte |= (((value >> shift)
4982                            & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
4983                           << next_bit);
4984         }
4985       
4986       next_offset += this_time;
4987       local->byte_buffer_in_use = true;
4988     }
4989 }
4990
4991 /* Subroutine of output_constant, used for CONSTRUCTORs (aggregate constants).
4992    Generate at least SIZE bytes, padding if necessary.  OUTER designates the
4993    caller output state of relevance in recursive invocations.  */
4994
4995 static unsigned HOST_WIDE_INT
4996 output_constructor (tree exp, unsigned HOST_WIDE_INT size,
4997                     unsigned int align, oc_outer_state * outer)
4998 {
4999   unsigned HOST_WIDE_INT cnt;
5000   constructor_elt *ce;
5001
5002   oc_local_state local;
5003
5004   /* Setup our local state to communicate with helpers.  */
5005   local.exp = exp;
5006   local.size = size;
5007   local.align = align;
5008
5009   local.total_bytes = 0;
5010   local.byte_buffer_in_use = outer != NULL;
5011   local.byte = outer ? outer->byte : 0;
5012
5013   local.type = TREE_TYPE (exp);
5014
5015   local.last_relative_index = -1;
5016
5017   local.min_index = NULL_TREE;
5018   if (TREE_CODE (local.type) == ARRAY_TYPE
5019       && TYPE_DOMAIN (local.type) != NULL_TREE)
5020     local.min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (local.type));
5021   
5022   gcc_assert (HOST_BITS_PER_WIDE_INT >= BITS_PER_UNIT);
5023
5024   /* As CE goes through the elements of the constant, FIELD goes through the
5025      structure fields if the constant is a structure.  If the constant is a
5026      union, we override this by getting the field from the TREE_LIST element.
5027      But the constant could also be an array.  Then FIELD is zero.
5028
5029      There is always a maximum of one element in the chain LINK for unions
5030      (even if the initializer in a source program incorrectly contains
5031      more one).  */
5032
5033   local.field = NULL_TREE;
5034   if (TREE_CODE (local.type) == RECORD_TYPE)
5035     local.field = TYPE_FIELDS (local.type);
5036
5037   for (cnt = 0;
5038        VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (exp), cnt, ce);
5039        cnt++, local.field = local.field ? TREE_CHAIN (local.field) : 0)
5040     {
5041       local.val = ce->value;
5042       local.index = NULL_TREE;
5043
5044       /* The element in a union constructor specifies the proper field
5045          or index.  */
5046       if ((TREE_CODE (local.type) == RECORD_TYPE
5047            || TREE_CODE (local.type) == UNION_TYPE
5048            || TREE_CODE (local.type) == QUAL_UNION_TYPE)
5049           && ce->index != NULL_TREE)
5050         local.field = ce->index;
5051
5052       else if (TREE_CODE (local.type) == ARRAY_TYPE)
5053         local.index = ce->index;
5054
5055 #ifdef ASM_COMMENT_START
5056       if (local.field && flag_verbose_asm)
5057         fprintf (asm_out_file, "%s %s:\n",
5058                  ASM_COMMENT_START,
5059                  DECL_NAME (local.field)
5060                  ? IDENTIFIER_POINTER (DECL_NAME (local.field))
5061                  : "<anonymous>");
5062 #endif
5063
5064       /* Eliminate the marker that makes a cast not be an lvalue.  */
5065       if (local.val != NULL_TREE)
5066         STRIP_NOPS (local.val);
5067
5068       /* Output the current element, using the appropriate helper ...  */
5069
5070       /* For an array slice not part of an outer bitfield.  */
5071       if (!outer
5072           && local.index != NULL_TREE
5073           && TREE_CODE (local.index) == RANGE_EXPR)
5074         output_constructor_array_range (&local);
5075
5076       /* For a field that is neither a true bitfield nor part of an outer one,
5077          known to be at least byte aligned and multiple-of-bytes long.  */
5078       else if (!outer
5079                && (local.field == NULL_TREE
5080                    || !CONSTRUCTOR_BITFIELD_P (local.field)))
5081         output_constructor_regular_field (&local);
5082       
5083       /* For a true bitfield or part of an outer one.  */
5084       else
5085         output_constructor_bitfield (&local, outer);
5086     }
5087
5088   /* If we are not at toplevel, save the pending data for our caller.
5089      Otherwise output the pending data and padding zeros as needed. */
5090   if (outer)
5091     outer->byte = local.byte;
5092   else
5093     {
5094       if (local.byte_buffer_in_use)
5095         {
5096           assemble_integer (GEN_INT (local.byte), 1, BITS_PER_UNIT, 1);
5097           local.total_bytes++;
5098         }
5099
5100       if ((unsigned HOST_WIDE_INT)local.total_bytes < local.size)
5101         {
5102           assemble_zeros (local.size - local.total_bytes);
5103           local.total_bytes = local.size;
5104         }
5105     }
5106       
5107   return local.total_bytes;
5108 }
5109
5110 /* Mark DECL as weak.  */
5111
5112 static void
5113 mark_weak (tree decl)
5114 {
5115   DECL_WEAK (decl) = 1;
5116
5117   if (DECL_RTL_SET_P (decl)
5118       && MEM_P (DECL_RTL (decl))
5119       && XEXP (DECL_RTL (decl), 0)
5120       && GET_CODE (XEXP (DECL_RTL (decl), 0)) == SYMBOL_REF)
5121     SYMBOL_REF_WEAK (XEXP (DECL_RTL (decl), 0)) = 1;
5122 }
5123
5124 /* Merge weak status between NEWDECL and OLDDECL.  */
5125
5126 void
5127 merge_weak (tree newdecl, tree olddecl)
5128 {
5129   if (DECL_WEAK (newdecl) == DECL_WEAK (olddecl))
5130     {
5131       if (DECL_WEAK (newdecl) && SUPPORTS_WEAK)
5132         {
5133           tree *pwd;
5134           /* We put the NEWDECL on the weak_decls list at some point
5135              and OLDDECL as well.  Keep just OLDDECL on the list.  */
5136           for (pwd = &weak_decls; *pwd; pwd = &TREE_CHAIN (*pwd))
5137             if (TREE_VALUE (*pwd) == newdecl)
5138               {
5139                 *pwd = TREE_CHAIN (*pwd);
5140                 break;
5141               }
5142         }
5143       return;
5144     }
5145
5146   if (DECL_WEAK (newdecl))
5147     {
5148       tree wd;
5149
5150       /* NEWDECL is weak, but OLDDECL is not.  */
5151
5152       /* If we already output the OLDDECL, we're in trouble; we can't
5153          go back and make it weak.  This error cannot be caught in
5154          declare_weak because the NEWDECL and OLDDECL was not yet
5155          been merged; therefore, TREE_ASM_WRITTEN was not set.  */
5156       if (TREE_ASM_WRITTEN (olddecl))
5157         error ("weak declaration of %q+D must precede definition",
5158                newdecl);
5159
5160       /* If we've already generated rtl referencing OLDDECL, we may
5161          have done so in a way that will not function properly with
5162          a weak symbol.  */
5163       else if (TREE_USED (olddecl)
5164                && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (olddecl)))
5165         warning (0, "weak declaration of %q+D after first use results "
5166                  "in unspecified behavior", newdecl);
5167
5168       if (SUPPORTS_WEAK)
5169         {
5170           /* We put the NEWDECL on the weak_decls list at some point.
5171              Replace it with the OLDDECL.  */
5172           for (wd = weak_decls; wd; wd = TREE_CHAIN (wd))
5173             if (TREE_VALUE (wd) == newdecl)
5174               {
5175                 TREE_VALUE (wd) = olddecl;
5176                 break;
5177               }
5178           /* We may not find the entry on the list.  If NEWDECL is a
5179              weak alias, then we will have already called
5180              globalize_decl to remove the entry; in that case, we do
5181              not need to do anything.  */
5182         }
5183
5184       /* Make the OLDDECL weak; it's OLDDECL that we'll be keeping.  */
5185       mark_weak (olddecl);
5186     }
5187   else
5188     /* OLDDECL was weak, but NEWDECL was not explicitly marked as
5189        weak.  Just update NEWDECL to indicate that it's weak too.  */
5190     mark_weak (newdecl);
5191 }
5192
5193 /* Declare DECL to be a weak symbol.  */
5194
5195 void
5196 declare_weak (tree decl)
5197 {
5198   if (! TREE_PUBLIC (decl))
5199     error ("weak declaration of %q+D must be public", decl);
5200   else if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
5201     error ("weak declaration of %q+D must precede definition", decl);
5202   else if (!SUPPORTS_WEAK)
5203     warning (0, "weak declaration of %q+D not supported", decl);
5204
5205   mark_weak (decl);
5206 }
5207
5208 static void
5209 weak_finish_1 (tree decl)
5210 {
5211 #if defined (ASM_WEAKEN_DECL) || defined (ASM_WEAKEN_LABEL)
5212   const char *const name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5213 #endif
5214
5215   if (! TREE_USED (decl))
5216     return;
5217
5218 #ifdef ASM_WEAKEN_DECL
5219   ASM_WEAKEN_DECL (asm_out_file, decl, name, NULL);
5220 #else
5221 #ifdef ASM_WEAKEN_LABEL
5222   ASM_WEAKEN_LABEL (asm_out_file, name);
5223 #else
5224 #ifdef ASM_OUTPUT_WEAK_ALIAS
5225   {
5226     static bool warn_once = 0;
5227     if (! warn_once)
5228       {
5229         warning (0, "only weak aliases are supported in this configuration");
5230         warn_once = 1;
5231       }
5232     return;
5233   }
5234 #endif
5235 #endif
5236 #endif
5237 }
5238
5239 /* This TREE_LIST contains weakref targets.  */
5240
5241 static GTY(()) tree weakref_targets;
5242
5243 /* Forward declaration.  */
5244 static tree find_decl_and_mark_needed (tree decl, tree target);
5245
5246 /* Emit any pending weak declarations.  */
5247
5248 void
5249 weak_finish (void)
5250 {
5251   tree t;
5252
5253   for (t = weakref_targets; t; t = TREE_CHAIN (t))
5254     {
5255       tree alias_decl = TREE_PURPOSE (t);
5256       tree target = ultimate_transparent_alias_target (&TREE_VALUE (t));
5257
5258       if (! TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (alias_decl)))
5259         /* Remove alias_decl from the weak list, but leave entries for
5260            the target alone.  */
5261         target = NULL_TREE;
5262 #ifndef ASM_OUTPUT_WEAKREF
5263       else if (! TREE_SYMBOL_REFERENCED (target))
5264         {
5265           /* Use ASM_WEAKEN_LABEL only if ASM_WEAKEN_DECL is not
5266              defined, otherwise we and weak_finish_1 would use
5267              different macros.  */
5268 # if defined ASM_WEAKEN_LABEL && ! defined ASM_WEAKEN_DECL
5269           ASM_WEAKEN_LABEL (asm_out_file, IDENTIFIER_POINTER (target));
5270 # else
5271           tree decl = find_decl_and_mark_needed (alias_decl, target);
5272
5273           if (! decl)
5274             {
5275               decl = build_decl (DECL_SOURCE_LOCATION (alias_decl),
5276                                  TREE_CODE (alias_decl), target,
5277                                  TREE_TYPE (alias_decl));
5278
5279               DECL_EXTERNAL (decl) = 1;
5280               TREE_PUBLIC (decl) = 1;
5281               DECL_ARTIFICIAL (decl) = 1;
5282               TREE_NOTHROW (decl) = TREE_NOTHROW (alias_decl);
5283               TREE_USED (decl) = 1;
5284             }
5285
5286           weak_finish_1 (decl);
5287 # endif
5288         }
5289 #endif
5290
5291       {
5292         tree *p;
5293         tree t2;
5294
5295         /* Remove the alias and the target from the pending weak list
5296            so that we do not emit any .weak directives for the former,
5297            nor multiple .weak directives for the latter.  */
5298         for (p = &weak_decls; (t2 = *p) ; )
5299           {
5300             if (TREE_VALUE (t2) == alias_decl
5301                 || target == DECL_ASSEMBLER_NAME (TREE_VALUE (t2)))
5302               *p = TREE_CHAIN (t2);
5303             else
5304               p = &TREE_CHAIN (t2);
5305           }
5306
5307         /* Remove other weakrefs to the same target, to speed things up.  */
5308         for (p = &TREE_CHAIN (t); (t2 = *p) ; )
5309           {
5310             if (target == ultimate_transparent_alias_target (&TREE_VALUE (t2)))
5311               *p = TREE_CHAIN (t2);
5312             else
5313               p = &TREE_CHAIN (t2);
5314           }
5315       }
5316     }
5317
5318   for (t = weak_decls; t; t = TREE_CHAIN (t))
5319     {
5320       tree decl = TREE_VALUE (t);
5321
5322       weak_finish_1 (decl);
5323     }
5324 }
5325
5326 /* Emit the assembly bits to indicate that DECL is globally visible.  */
5327
5328 static void
5329 globalize_decl (tree decl)
5330 {
5331
5332 #if defined (ASM_WEAKEN_LABEL) || defined (ASM_WEAKEN_DECL)
5333   if (DECL_WEAK (decl))
5334     {
5335       const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
5336       tree *p, t;
5337
5338 #ifdef ASM_WEAKEN_DECL
5339       ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
5340 #else
5341       ASM_WEAKEN_LABEL (asm_out_file, name);
5342 #endif
5343
5344       /* Remove this function from the pending weak list so that
5345          we do not emit multiple .weak directives for it.  */
5346       for (p = &weak_decls; (t = *p) ; )
5347         {
5348           if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
5349             *p = TREE_CHAIN (t);
5350           else
5351             p = &TREE_CHAIN (t);
5352         }
5353
5354       /* Remove weakrefs to the same target from the pending weakref
5355          list, for the same reason.  */
5356       for (p = &weakref_targets; (t = *p) ; )
5357         {
5358           if (DECL_ASSEMBLER_NAME (decl)
5359               == ultimate_transparent_alias_target (&TREE_VALUE (t)))
5360             *p = TREE_CHAIN (t);
5361           else
5362             p = &TREE_CHAIN (t);
5363         }
5364
5365       return;
5366     }
5367 #endif
5368
5369   targetm.asm_out.globalize_decl_name (asm_out_file, decl);
5370 }
5371
5372 /* We have to be able to tell cgraph about the needed-ness of the target
5373    of an alias.  This requires that the decl have been defined.  Aliases
5374    that precede their definition have to be queued for later processing.  */
5375
5376 typedef struct GTY(()) alias_pair {
5377   tree decl;
5378   tree target;
5379 } alias_pair;
5380
5381 /* Define gc'd vector type.  */
5382 DEF_VEC_O(alias_pair);
5383 DEF_VEC_ALLOC_O(alias_pair,gc);
5384
5385 static GTY(()) VEC(alias_pair,gc) *alias_pairs;
5386
5387 /* Given an assembly name, find the decl it is associated with.  At the
5388    same time, mark it needed for cgraph.  */
5389
5390 static tree
5391 find_decl_and_mark_needed (tree decl, tree target)
5392 {
5393   struct cgraph_node *fnode = NULL;
5394   struct varpool_node *vnode = NULL;
5395
5396   if (TREE_CODE (decl) == FUNCTION_DECL)
5397     {
5398       fnode = cgraph_node_for_asm (target);
5399       if (fnode == NULL)
5400         vnode = varpool_node_for_asm (target);
5401     }
5402   else
5403     {
5404       vnode = varpool_node_for_asm (target);
5405       if (vnode == NULL)
5406         fnode = cgraph_node_for_asm (target);
5407     }
5408
5409   if (fnode)
5410     {
5411       /* We can't mark function nodes as used after cgraph global info
5412          is finished.  This wouldn't generally be necessary, but C++
5413          virtual table thunks are introduced late in the game and
5414          might seem like they need marking, although in fact they
5415          don't.  */
5416       if (! cgraph_global_info_ready)
5417         cgraph_mark_needed_node (fnode);
5418       return fnode->decl;
5419     }
5420   else if (vnode)
5421     {
5422       varpool_mark_needed_node (vnode);
5423       return vnode->decl;
5424     }
5425   else
5426     return NULL_TREE;
5427 }
5428
5429 /* Output the assembler code for a define (equate) using ASM_OUTPUT_DEF
5430    or ASM_OUTPUT_DEF_FROM_DECLS.  The function defines the symbol whose
5431    tree node is DECL to have the value of the tree node TARGET.  */
5432
5433 static void
5434 do_assemble_alias (tree decl, tree target)
5435 {
5436   if (TREE_ASM_WRITTEN (decl))
5437     return;
5438
5439   TREE_ASM_WRITTEN (decl) = 1;
5440   TREE_ASM_WRITTEN (DECL_ASSEMBLER_NAME (decl)) = 1;
5441
5442   if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
5443     {
5444       ultimate_transparent_alias_target (&target);
5445
5446       if (!targetm.have_tls
5447           && TREE_CODE (decl) == VAR_DECL
5448           && DECL_THREAD_LOCAL_P (decl))
5449         {
5450           decl = emutls_decl (decl);
5451           target = get_emutls_object_name (target);
5452         }
5453
5454       if (!TREE_SYMBOL_REFERENCED (target))
5455         weakref_targets = tree_cons (decl, target, weakref_targets);
5456
5457 #ifdef ASM_OUTPUT_WEAKREF
5458       ASM_OUTPUT_WEAKREF (asm_out_file, decl,
5459                           IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
5460                           IDENTIFIER_POINTER (target));
5461 #else
5462       if (!SUPPORTS_WEAK)
5463         {
5464           error_at (DECL_SOURCE_LOCATION (decl),
5465                     "weakref is not supported in this configuration");
5466           return;
5467         }
5468 #endif
5469       return;
5470     }
5471
5472   if (!targetm.have_tls
5473       && TREE_CODE (decl) == VAR_DECL
5474       && DECL_THREAD_LOCAL_P (decl))
5475     {
5476       decl = emutls_decl (decl);
5477       target = get_emutls_object_name (target);
5478     }
5479
5480 #ifdef ASM_OUTPUT_DEF
5481   /* Make name accessible from other files, if appropriate.  */
5482
5483   if (TREE_PUBLIC (decl))
5484     {
5485       globalize_decl (decl);
5486       maybe_assemble_visibility (decl);
5487     }
5488
5489 # ifdef ASM_OUTPUT_DEF_FROM_DECLS
5490   ASM_OUTPUT_DEF_FROM_DECLS (asm_out_file, decl, target);
5491 # else
5492   ASM_OUTPUT_DEF (asm_out_file,
5493                   IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
5494                   IDENTIFIER_POINTER (target));
5495 # endif
5496 #elif defined (ASM_OUTPUT_WEAK_ALIAS) || defined (ASM_WEAKEN_DECL)
5497   {
5498     const char *name;
5499     tree *p, t;
5500
5501     name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5502 # ifdef ASM_WEAKEN_DECL
5503     ASM_WEAKEN_DECL (asm_out_file, decl, name, IDENTIFIER_POINTER (target));
5504 # else
5505     ASM_OUTPUT_WEAK_ALIAS (asm_out_file, name, IDENTIFIER_POINTER (target));
5506 # endif
5507     /* Remove this function from the pending weak list so that
5508        we do not emit multiple .weak directives for it.  */
5509     for (p = &weak_decls; (t = *p) ; )
5510       if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
5511         *p = TREE_CHAIN (t);
5512       else
5513         p = &TREE_CHAIN (t);
5514
5515     /* Remove weakrefs to the same target from the pending weakref
5516        list, for the same reason.  */
5517     for (p = &weakref_targets; (t = *p) ; )
5518       {
5519         if (DECL_ASSEMBLER_NAME (decl)
5520             == ultimate_transparent_alias_target (&TREE_VALUE (t)))
5521           *p = TREE_CHAIN (t);
5522         else
5523           p = &TREE_CHAIN (t);
5524       }
5525   }
5526 #endif
5527 }
5528
5529 /* First pass of completing pending aliases.  Make sure that cgraph knows
5530    which symbols will be required.  */
5531
5532 void
5533 finish_aliases_1 (void)
5534 {
5535   unsigned i;
5536   alias_pair *p;
5537
5538   for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); i++)
5539     {
5540       tree target_decl;
5541
5542       target_decl = find_decl_and_mark_needed (p->decl, p->target);
5543       if (target_decl == NULL)
5544         {
5545           if (! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
5546             error ("%q+D aliased to undefined symbol %qE",
5547                    p->decl, p->target);
5548         }
5549       else if (DECL_EXTERNAL (target_decl)
5550                && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
5551         error ("%q+D aliased to external symbol %qE",
5552                p->decl, p->target);
5553     }
5554 }
5555
5556 /* Second pass of completing pending aliases.  Emit the actual assembly.
5557    This happens at the end of compilation and thus it is assured that the
5558    target symbol has been emitted.  */
5559
5560 void
5561 finish_aliases_2 (void)
5562 {
5563   unsigned i;
5564   alias_pair *p;
5565
5566   for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); i++)
5567     do_assemble_alias (p->decl, p->target);
5568
5569   VEC_truncate (alias_pair, alias_pairs, 0);
5570 }
5571
5572 /* Emit an assembler directive to make the symbol for DECL an alias to
5573    the symbol for TARGET.  */
5574
5575 void
5576 assemble_alias (tree decl, tree target)
5577 {
5578   tree target_decl;
5579   bool is_weakref = false;
5580
5581   if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
5582     {
5583       tree alias = DECL_ASSEMBLER_NAME (decl);
5584
5585       is_weakref = true;
5586
5587       ultimate_transparent_alias_target (&target);
5588
5589       if (alias == target)
5590         error ("weakref %q+D ultimately targets itself", decl);
5591       else
5592         {
5593 #ifndef ASM_OUTPUT_WEAKREF
5594           IDENTIFIER_TRANSPARENT_ALIAS (alias) = 1;
5595           TREE_CHAIN (alias) = target;
5596 #endif
5597         }
5598       if (TREE_PUBLIC (decl))
5599         error ("weakref %q+D must have static linkage", decl);
5600     }
5601   else
5602     {
5603 #if !defined (ASM_OUTPUT_DEF)
5604 # if !defined(ASM_OUTPUT_WEAK_ALIAS) && !defined (ASM_WEAKEN_DECL)
5605       error_at (DECL_SOURCE_LOCATION (decl),
5606                 "alias definitions not supported in this configuration");
5607       return;
5608 # else
5609       if (!DECL_WEAK (decl))
5610         {
5611           error_at (DECL_SOURCE_LOCATION (decl),
5612                     "only weak aliases are supported in this configuration");
5613           return;
5614         }
5615 # endif
5616 #endif
5617     }
5618
5619   /* We must force creation of DECL_RTL for debug info generation, even though
5620      we don't use it here.  */
5621   make_decl_rtl (decl);
5622   TREE_USED (decl) = 1;
5623
5624   /* A quirk of the initial implementation of aliases required that the user
5625      add "extern" to all of them.  Which is silly, but now historical.  Do
5626      note that the symbol is in fact locally defined.  */
5627   if (! is_weakref)
5628     DECL_EXTERNAL (decl) = 0;
5629
5630   /* Allow aliases to aliases.  */
5631   if (TREE_CODE (decl) == FUNCTION_DECL)
5632     cgraph_node (decl)->alias = true;
5633   else
5634     varpool_node (decl)->alias = true;
5635
5636   /* If the target has already been emitted, we don't have to queue the
5637      alias.  This saves a tad of memory.  */
5638   if (cgraph_global_info_ready)
5639     target_decl = find_decl_and_mark_needed (decl, target);
5640   else
5641     target_decl= NULL;
5642   if (target_decl && TREE_ASM_WRITTEN (target_decl))
5643     do_assemble_alias (decl, target);
5644   else
5645     {
5646       alias_pair *p = VEC_safe_push (alias_pair, gc, alias_pairs, NULL);
5647       p->decl = decl;
5648       p->target = target;
5649     }
5650 }
5651
5652 /* Emit an assembler directive to set symbol for DECL visibility to
5653    the visibility type VIS, which must not be VISIBILITY_DEFAULT.  */
5654
5655 void
5656 default_assemble_visibility (tree decl, int vis)
5657 {
5658   static const char * const visibility_types[] = {
5659     NULL, "protected", "hidden", "internal"
5660   };
5661
5662   const char *name, *type;
5663
5664   name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5665   type = visibility_types[vis];
5666
5667 #ifdef HAVE_GAS_HIDDEN
5668   fprintf (asm_out_file, "\t.%s\t", type);
5669   assemble_name (asm_out_file, name);
5670   fprintf (asm_out_file, "\n");
5671 #else
5672   warning (OPT_Wattributes, "visibility attribute not supported "
5673            "in this configuration; ignored");
5674 #endif
5675 }
5676
5677 /* A helper function to call assemble_visibility when needed for a decl.  */
5678
5679 int
5680 maybe_assemble_visibility (tree decl)
5681 {
5682   enum symbol_visibility vis = DECL_VISIBILITY (decl);
5683
5684   if (vis != VISIBILITY_DEFAULT)
5685     {
5686       targetm.asm_out.visibility (decl, vis);
5687       return 1;
5688     }
5689   else
5690     return 0;
5691 }
5692
5693 /* Returns 1 if the target configuration supports defining public symbols
5694    so that one of them will be chosen at link time instead of generating a
5695    multiply-defined symbol error, whether through the use of weak symbols or
5696    a target-specific mechanism for having duplicates discarded.  */
5697
5698 int
5699 supports_one_only (void)
5700 {
5701   if (SUPPORTS_ONE_ONLY)
5702     return 1;
5703   return SUPPORTS_WEAK;
5704 }
5705
5706 /* Set up DECL as a public symbol that can be defined in multiple
5707    translation units without generating a linker error.  */
5708
5709 void
5710 make_decl_one_only (tree decl, tree comdat_group)
5711 {
5712   gcc_assert (TREE_CODE (decl) == VAR_DECL
5713               || TREE_CODE (decl) == FUNCTION_DECL);
5714
5715   TREE_PUBLIC (decl) = 1;
5716
5717   if (SUPPORTS_ONE_ONLY)
5718     {
5719 #ifdef MAKE_DECL_ONE_ONLY
5720       MAKE_DECL_ONE_ONLY (decl);
5721 #endif
5722       DECL_COMDAT_GROUP (decl) = comdat_group;
5723     }
5724   else if (TREE_CODE (decl) == VAR_DECL
5725       && (DECL_INITIAL (decl) == 0 || DECL_INITIAL (decl) == error_mark_node))
5726     DECL_COMMON (decl) = 1;
5727   else
5728     {
5729       gcc_assert (SUPPORTS_WEAK);
5730       DECL_WEAK (decl) = 1;
5731     }
5732 }
5733
5734 void
5735 init_varasm_once (void)
5736 {
5737   section_htab = htab_create_ggc (31, section_entry_hash,
5738                                   section_entry_eq, NULL);
5739   object_block_htab = htab_create_ggc (31, object_block_entry_hash,
5740                                        object_block_entry_eq, NULL);
5741   const_desc_htab = htab_create_ggc (1009, const_desc_hash,
5742                                      const_desc_eq, NULL);
5743
5744   const_alias_set = new_alias_set ();
5745   shared_constant_pool = create_constant_pool ();
5746
5747 #ifdef TEXT_SECTION_ASM_OP
5748   text_section = get_unnamed_section (SECTION_CODE, output_section_asm_op,
5749                                       TEXT_SECTION_ASM_OP);
5750 #endif
5751
5752 #ifdef DATA_SECTION_ASM_OP
5753   data_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
5754                                       DATA_SECTION_ASM_OP);
5755 #endif
5756
5757 #ifdef SDATA_SECTION_ASM_OP
5758   sdata_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
5759                                        SDATA_SECTION_ASM_OP);
5760 #endif
5761
5762 #ifdef READONLY_DATA_SECTION_ASM_OP
5763   readonly_data_section = get_unnamed_section (0, output_section_asm_op,
5764                                                READONLY_DATA_SECTION_ASM_OP);
5765 #endif
5766
5767 #ifdef CTORS_SECTION_ASM_OP
5768   ctors_section = get_unnamed_section (0, output_section_asm_op,
5769                                        CTORS_SECTION_ASM_OP);
5770 #endif
5771
5772 #ifdef DTORS_SECTION_ASM_OP
5773   dtors_section = get_unnamed_section (0, output_section_asm_op,
5774                                        DTORS_SECTION_ASM_OP);
5775 #endif
5776
5777 #ifdef BSS_SECTION_ASM_OP
5778   bss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
5779                                      output_section_asm_op,
5780                                      BSS_SECTION_ASM_OP);
5781 #endif
5782
5783 #ifdef SBSS_SECTION_ASM_OP
5784   sbss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
5785                                       output_section_asm_op,
5786                                       SBSS_SECTION_ASM_OP);
5787 #endif
5788
5789   tls_comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
5790                                            | SECTION_COMMON, emit_tls_common);
5791   lcomm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
5792                                         | SECTION_COMMON, emit_local);
5793   comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
5794                                        | SECTION_COMMON, emit_common);
5795
5796 #if defined ASM_OUTPUT_ALIGNED_BSS || defined ASM_OUTPUT_BSS
5797   bss_noswitch_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS,
5798                                                emit_bss);
5799 #endif
5800
5801   targetm.asm_out.init_sections ();
5802
5803   if (readonly_data_section == NULL)
5804     readonly_data_section = text_section;
5805 }
5806
5807 enum tls_model
5808 decl_default_tls_model (const_tree decl)
5809 {
5810   enum tls_model kind;
5811   bool is_local;
5812
5813   is_local = targetm.binds_local_p (decl);
5814   if (!flag_shlib)
5815     {
5816       if (is_local)
5817         kind = TLS_MODEL_LOCAL_EXEC;
5818       else
5819         kind = TLS_MODEL_INITIAL_EXEC;
5820     }
5821
5822   /* Local dynamic is inefficient when we're not combining the
5823      parts of the address.  */
5824   else if (optimize && is_local)
5825     kind = TLS_MODEL_LOCAL_DYNAMIC;
5826   else
5827     kind = TLS_MODEL_GLOBAL_DYNAMIC;
5828   if (kind < flag_tls_default)
5829     kind = flag_tls_default;
5830
5831   return kind;
5832 }
5833
5834 /* Select a set of attributes for section NAME based on the properties
5835    of DECL and whether or not RELOC indicates that DECL's initializer
5836    might contain runtime relocations.
5837
5838    We make the section read-only and executable for a function decl,
5839    read-only for a const data decl, and writable for a non-const data decl.  */
5840
5841 unsigned int
5842 default_section_type_flags (tree decl, const char *name, int reloc)
5843 {
5844   unsigned int flags;
5845
5846   if (decl && TREE_CODE (decl) == FUNCTION_DECL)
5847     flags = SECTION_CODE;
5848   else if (decl && decl_readonly_section (decl, reloc))
5849     flags = 0;
5850   else if (current_function_decl
5851            && cfun
5852            && crtl->subsections.unlikely_text_section_name
5853            && strcmp (name, crtl->subsections.unlikely_text_section_name) == 0)
5854     flags = SECTION_CODE;
5855   else if (!decl
5856            && (!current_function_decl || !cfun)
5857            && strcmp (name, UNLIKELY_EXECUTED_TEXT_SECTION_NAME) == 0)
5858     flags = SECTION_CODE;
5859   else
5860     flags = SECTION_WRITE;
5861
5862   if (decl && DECL_ONE_ONLY (decl))
5863     flags |= SECTION_LINKONCE;
5864
5865   if (decl && TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
5866     flags |= SECTION_TLS | SECTION_WRITE;
5867
5868   if (strcmp (name, ".bss") == 0
5869       || strncmp (name, ".bss.", 5) == 0
5870       || strncmp (name, ".gnu.linkonce.b.", 16) == 0
5871       || strcmp (name, ".sbss") == 0
5872       || strncmp (name, ".sbss.", 6) == 0
5873       || strncmp (name, ".gnu.linkonce.sb.", 17) == 0)
5874     flags |= SECTION_BSS;
5875
5876   if (strcmp (name, ".tdata") == 0
5877       || strncmp (name, ".tdata.", 7) == 0
5878       || strncmp (name, ".gnu.linkonce.td.", 17) == 0)
5879     flags |= SECTION_TLS;
5880
5881   if (strcmp (name, ".tbss") == 0
5882       || strncmp (name, ".tbss.", 6) == 0
5883       || strncmp (name, ".gnu.linkonce.tb.", 17) == 0)
5884     flags |= SECTION_TLS | SECTION_BSS;
5885
5886   /* These three sections have special ELF types.  They are neither
5887      SHT_PROGBITS nor SHT_NOBITS, so when changing sections we don't
5888      want to print a section type (@progbits or @nobits).  If someone
5889      is silly enough to emit code or TLS variables to one of these
5890      sections, then don't handle them specially.  */
5891   if (!(flags & (SECTION_CODE | SECTION_BSS | SECTION_TLS))
5892       && (strcmp (name, ".init_array") == 0
5893           || strcmp (name, ".fini_array") == 0
5894           || strcmp (name, ".preinit_array") == 0))
5895     flags |= SECTION_NOTYPE;
5896
5897   return flags;
5898 }
5899
5900 /* Return true if the target supports some form of global BSS,
5901    either through bss_noswitch_section, or by selecting a BSS
5902    section in TARGET_ASM_SELECT_SECTION.  */
5903
5904 bool
5905 have_global_bss_p (void)
5906 {
5907   return bss_noswitch_section || targetm.have_switchable_bss_sections;
5908 }
5909
5910 /* Output assembly to switch to section NAME with attribute FLAGS.
5911    Four variants for common object file formats.  */
5912
5913 void
5914 default_no_named_section (const char *name ATTRIBUTE_UNUSED,
5915                           unsigned int flags ATTRIBUTE_UNUSED,
5916                           tree decl ATTRIBUTE_UNUSED)
5917 {
5918   /* Some object formats don't support named sections at all.  The
5919      front-end should already have flagged this as an error.  */
5920   gcc_unreachable ();
5921 }
5922
5923 void
5924 default_elf_asm_named_section (const char *name, unsigned int flags,
5925                                tree decl ATTRIBUTE_UNUSED)
5926 {
5927   char flagchars[10], *f = flagchars;
5928
5929   /* If we have already declared this section, we can use an
5930      abbreviated form to switch back to it -- unless this section is
5931      part of a COMDAT groups, in which case GAS requires the full
5932      declaration every time.  */
5933   if (!(HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
5934       && (flags & SECTION_DECLARED))
5935     {
5936       fprintf (asm_out_file, "\t.section\t%s\n", name);
5937       return;
5938     }
5939
5940   if (!(flags & SECTION_DEBUG))
5941     *f++ = 'a';
5942   if (flags & SECTION_WRITE)
5943     *f++ = 'w';
5944   if (flags & SECTION_CODE)
5945     *f++ = 'x';
5946   if (flags & SECTION_SMALL)
5947     *f++ = 's';
5948   if (flags & SECTION_MERGE)
5949     *f++ = 'M';
5950   if (flags & SECTION_STRINGS)
5951     *f++ = 'S';
5952   if (flags & SECTION_TLS)
5953     *f++ = 'T';
5954   if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
5955     *f++ = 'G';
5956   *f = '\0';
5957
5958   fprintf (asm_out_file, "\t.section\t%s,\"%s\"", name, flagchars);
5959
5960   if (!(flags & SECTION_NOTYPE))
5961     {
5962       const char *type;
5963       const char *format;
5964
5965       if (flags & SECTION_BSS)
5966         type = "nobits";
5967       else
5968         type = "progbits";
5969
5970       format = ",@%s";
5971 #ifdef ASM_COMMENT_START
5972       /* On platforms that use "@" as the assembly comment character,
5973          use "%" instead.  */
5974       if (strcmp (ASM_COMMENT_START, "@") == 0)
5975         format = ",%%%s";
5976 #endif
5977       fprintf (asm_out_file, format, type);
5978
5979       if (flags & SECTION_ENTSIZE)
5980         fprintf (asm_out_file, ",%d", flags & SECTION_ENTSIZE);
5981       if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
5982         fprintf (asm_out_file, ",%s,comdat",
5983                  IDENTIFIER_POINTER (DECL_COMDAT_GROUP (decl)));
5984     }
5985
5986   putc ('\n', asm_out_file);
5987 }
5988
5989 void
5990 default_coff_asm_named_section (const char *name, unsigned int flags,
5991                                 tree decl ATTRIBUTE_UNUSED)
5992 {
5993   char flagchars[8], *f = flagchars;
5994
5995   if (flags & SECTION_WRITE)
5996     *f++ = 'w';
5997   if (flags & SECTION_CODE)
5998     *f++ = 'x';
5999   *f = '\0';
6000
6001   fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
6002 }
6003
6004 void
6005 default_pe_asm_named_section (const char *name, unsigned int flags,
6006                               tree decl)
6007 {
6008   default_coff_asm_named_section (name, flags, decl);
6009
6010   if (flags & SECTION_LINKONCE)
6011     {
6012       /* Functions may have been compiled at various levels of
6013          optimization so we can't use `same_size' here.
6014          Instead, have the linker pick one.  */
6015       fprintf (asm_out_file, "\t.linkonce %s\n",
6016                (flags & SECTION_CODE ? "discard" : "same_size"));
6017     }
6018 }
6019 \f
6020 /* The lame default section selector.  */
6021
6022 section *
6023 default_select_section (tree decl, int reloc,
6024                         unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
6025 {
6026   if (DECL_P (decl))
6027     {
6028       if (decl_readonly_section (decl, reloc))
6029         return readonly_data_section;
6030     }
6031   else if (TREE_CODE (decl) == CONSTRUCTOR)
6032     {
6033       if (! ((flag_pic && reloc)
6034              || !TREE_READONLY (decl)
6035              || TREE_SIDE_EFFECTS (decl)
6036              || !TREE_CONSTANT (decl)))
6037         return readonly_data_section;
6038     }
6039   else if (TREE_CODE (decl) == STRING_CST)
6040     return readonly_data_section;
6041   else if (! (flag_pic && reloc))
6042     return readonly_data_section;
6043
6044   return data_section;
6045 }
6046
6047 enum section_category
6048 categorize_decl_for_section (const_tree decl, int reloc)
6049 {
6050   enum section_category ret;
6051
6052   if (TREE_CODE (decl) == FUNCTION_DECL)
6053     return SECCAT_TEXT;
6054   else if (TREE_CODE (decl) == STRING_CST)
6055     {
6056       if (flag_mudflap) /* or !flag_merge_constants */
6057         return SECCAT_RODATA;
6058       else
6059         return SECCAT_RODATA_MERGE_STR;
6060     }
6061   else if (TREE_CODE (decl) == VAR_DECL)
6062     {
6063       if (bss_initializer_p (decl))
6064         ret = SECCAT_BSS;
6065       else if (! TREE_READONLY (decl)
6066                || TREE_SIDE_EFFECTS (decl)
6067                || ! TREE_CONSTANT (DECL_INITIAL (decl)))
6068         {
6069           /* Here the reloc_rw_mask is not testing whether the section should
6070              be read-only or not, but whether the dynamic link will have to
6071              do something.  If so, we wish to segregate the data in order to
6072              minimize cache misses inside the dynamic linker.  */
6073           if (reloc & targetm.asm_out.reloc_rw_mask ())
6074             ret = reloc == 1 ? SECCAT_DATA_REL_LOCAL : SECCAT_DATA_REL;
6075           else
6076             ret = SECCAT_DATA;
6077         }
6078       else if (reloc & targetm.asm_out.reloc_rw_mask ())
6079         ret = reloc == 1 ? SECCAT_DATA_REL_RO_LOCAL : SECCAT_DATA_REL_RO;
6080       else if (reloc || flag_merge_constants < 2)
6081         /* C and C++ don't allow different variables to share the same
6082            location.  -fmerge-all-constants allows even that (at the
6083            expense of not conforming).  */
6084         ret = SECCAT_RODATA;
6085       else if (TREE_CODE (DECL_INITIAL (decl)) == STRING_CST)
6086         ret = SECCAT_RODATA_MERGE_STR_INIT;
6087       else
6088         ret = SECCAT_RODATA_MERGE_CONST;
6089     }
6090   else if (TREE_CODE (decl) == CONSTRUCTOR)
6091     {
6092       if ((reloc & targetm.asm_out.reloc_rw_mask ())
6093           || TREE_SIDE_EFFECTS (decl)
6094           || ! TREE_CONSTANT (decl))
6095         ret = SECCAT_DATA;
6096       else
6097         ret = SECCAT_RODATA;
6098     }
6099   else
6100     ret = SECCAT_RODATA;
6101
6102   /* There are no read-only thread-local sections.  */
6103   if (TREE_CODE (decl) == VAR_DECL && DECL_TLS_MODEL (decl))
6104     {
6105       if (DECL_TLS_MODEL (decl) == TLS_MODEL_EMULATED)
6106         {
6107           if (DECL_EMUTLS_VAR_P (decl))
6108             {
6109               if (targetm.emutls.var_section)
6110                 ret = SECCAT_EMUTLS_VAR;
6111             }
6112           else
6113             {
6114               if (targetm.emutls.tmpl_prefix)
6115                 ret = SECCAT_EMUTLS_TMPL;
6116             }
6117         }
6118       /* Note that this would be *just* SECCAT_BSS, except that there's
6119          no concept of a read-only thread-local-data section.  */
6120       else if (ret == SECCAT_BSS
6121                || (flag_zero_initialized_in_bss
6122                    && initializer_zerop (DECL_INITIAL (decl))))
6123         ret = SECCAT_TBSS;
6124       else
6125         ret = SECCAT_TDATA;
6126     }
6127
6128   /* If the target uses small data sections, select it.  */
6129   else if (targetm.in_small_data_p (decl))
6130     {
6131       if (ret == SECCAT_BSS)
6132         ret = SECCAT_SBSS;
6133       else if (targetm.have_srodata_section && ret == SECCAT_RODATA)
6134         ret = SECCAT_SRODATA;
6135       else
6136         ret = SECCAT_SDATA;
6137     }
6138
6139   return ret;
6140 }
6141
6142 bool
6143 decl_readonly_section (const_tree decl, int reloc)
6144 {
6145   switch (categorize_decl_for_section (decl, reloc))
6146     {
6147     case SECCAT_RODATA:
6148     case SECCAT_RODATA_MERGE_STR:
6149     case SECCAT_RODATA_MERGE_STR_INIT:
6150     case SECCAT_RODATA_MERGE_CONST:
6151     case SECCAT_SRODATA:
6152       return true;
6153       break;
6154     default:
6155       return false;
6156       break;
6157     }
6158 }
6159
6160 /* Select a section based on the above categorization.  */
6161
6162 section *
6163 default_elf_select_section (tree decl, int reloc,
6164                             unsigned HOST_WIDE_INT align)
6165 {
6166   const char *sname;
6167   switch (categorize_decl_for_section (decl, reloc))
6168     {
6169     case SECCAT_TEXT:
6170       /* We're not supposed to be called on FUNCTION_DECLs.  */
6171       gcc_unreachable ();
6172     case SECCAT_RODATA:
6173       return readonly_data_section;
6174     case SECCAT_RODATA_MERGE_STR:
6175       return mergeable_string_section (decl, align, 0);
6176     case SECCAT_RODATA_MERGE_STR_INIT:
6177       return mergeable_string_section (DECL_INITIAL (decl), align, 0);
6178     case SECCAT_RODATA_MERGE_CONST:
6179       return mergeable_constant_section (DECL_MODE (decl), align, 0);
6180     case SECCAT_SRODATA:
6181       sname = ".sdata2";
6182       break;
6183     case SECCAT_DATA:
6184       return data_section;
6185     case SECCAT_DATA_REL:
6186       sname = ".data.rel";
6187       break;
6188     case SECCAT_DATA_REL_LOCAL:
6189       sname = ".data.rel.local";
6190       break;
6191     case SECCAT_DATA_REL_RO:
6192       sname = ".data.rel.ro";
6193       break;
6194     case SECCAT_DATA_REL_RO_LOCAL:
6195       sname = ".data.rel.ro.local";
6196       break;
6197     case SECCAT_SDATA:
6198       sname = ".sdata";
6199       break;
6200     case SECCAT_TDATA:
6201       sname = ".tdata";
6202       break;
6203     case SECCAT_BSS:
6204       if (bss_section)
6205         return bss_section;
6206       sname = ".bss";
6207       break;
6208     case SECCAT_SBSS:
6209       sname = ".sbss";
6210       break;
6211     case SECCAT_TBSS:
6212       sname = ".tbss";
6213       break;
6214     case SECCAT_EMUTLS_VAR:
6215       sname = targetm.emutls.var_section;
6216       break;
6217     case SECCAT_EMUTLS_TMPL:
6218       sname = targetm.emutls.tmpl_section;
6219       break;
6220     default:
6221       gcc_unreachable ();
6222     }
6223
6224   if (!DECL_P (decl))
6225     decl = NULL_TREE;
6226   return get_named_section (decl, sname, reloc);
6227 }
6228
6229 /* Construct a unique section name based on the decl name and the
6230    categorization performed above.  */
6231
6232 void
6233 default_unique_section (tree decl, int reloc)
6234 {
6235   /* We only need to use .gnu.linkonce if we don't have COMDAT groups.  */
6236   bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
6237   const char *prefix, *name, *linkonce;
6238   char *string;
6239
6240   switch (categorize_decl_for_section (decl, reloc))
6241     {
6242     case SECCAT_TEXT:
6243       prefix = one_only ? ".t" : ".text";
6244       break;
6245     case SECCAT_RODATA:
6246     case SECCAT_RODATA_MERGE_STR:
6247     case SECCAT_RODATA_MERGE_STR_INIT:
6248     case SECCAT_RODATA_MERGE_CONST:
6249       prefix = one_only ? ".r" : ".rodata";
6250       break;
6251     case SECCAT_SRODATA:
6252       prefix = one_only ? ".s2" : ".sdata2";
6253       break;
6254     case SECCAT_DATA:
6255       prefix = one_only ? ".d" : ".data";
6256       break;
6257     case SECCAT_DATA_REL:
6258       prefix = one_only ? ".d.rel" : ".data.rel";
6259       break;
6260     case SECCAT_DATA_REL_LOCAL:
6261       prefix = one_only ? ".d.rel.local" : ".data.rel.local";
6262       break;
6263     case SECCAT_DATA_REL_RO:
6264       prefix = one_only ? ".d.rel.ro" : ".data.rel.ro";
6265       break;
6266     case SECCAT_DATA_REL_RO_LOCAL:
6267       prefix = one_only ? ".d.rel.ro.local" : ".data.rel.ro.local";
6268       break;
6269     case SECCAT_SDATA:
6270       prefix = one_only ? ".s" : ".sdata";
6271       break;
6272     case SECCAT_BSS:
6273       prefix = one_only ? ".b" : ".bss";
6274       break;
6275     case SECCAT_SBSS:
6276       prefix = one_only ? ".sb" : ".sbss";
6277       break;
6278     case SECCAT_TDATA:
6279       prefix = one_only ? ".td" : ".tdata";
6280       break;
6281     case SECCAT_TBSS:
6282       prefix = one_only ? ".tb" : ".tbss";
6283       break;
6284     case SECCAT_EMUTLS_VAR:
6285       prefix = targetm.emutls.var_section;
6286       break;
6287     case SECCAT_EMUTLS_TMPL:
6288       prefix = targetm.emutls.tmpl_section;
6289       break;
6290     default:
6291       gcc_unreachable ();
6292     }
6293
6294   name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
6295   name = targetm.strip_name_encoding (name);
6296
6297   /* If we're using one_only, then there needs to be a .gnu.linkonce
6298      prefix to the section name.  */
6299   linkonce = one_only ? ".gnu.linkonce" : "";
6300   
6301   string = ACONCAT ((linkonce, prefix, ".", name, NULL));
6302
6303   DECL_SECTION_NAME (decl) = build_string (strlen (string), string);
6304 }
6305
6306 /* Like compute_reloc_for_constant, except for an RTX.  The return value
6307    is a mask for which bit 1 indicates a global relocation, and bit 0
6308    indicates a local relocation.  */
6309
6310 static int
6311 compute_reloc_for_rtx_1 (rtx *xp, void *data)
6312 {
6313   int *preloc = (int *) data;
6314   rtx x = *xp;
6315
6316   switch (GET_CODE (x))
6317     {
6318     case SYMBOL_REF:
6319       *preloc |= SYMBOL_REF_LOCAL_P (x) ? 1 : 2;
6320       break;
6321     case LABEL_REF:
6322       *preloc |= 1;
6323       break;
6324     default:
6325       break;
6326     }
6327
6328   return 0;
6329 }
6330
6331 static int
6332 compute_reloc_for_rtx (rtx x)
6333 {
6334   int reloc;
6335
6336   switch (GET_CODE (x))
6337     {
6338     case CONST:
6339     case SYMBOL_REF:
6340     case LABEL_REF:
6341       reloc = 0;
6342       for_each_rtx (&x, compute_reloc_for_rtx_1, &reloc);
6343       return reloc;
6344
6345     default:
6346       return 0;
6347     }
6348 }
6349
6350 section *
6351 default_select_rtx_section (enum machine_mode mode ATTRIBUTE_UNUSED,
6352                             rtx x,
6353                             unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
6354 {
6355   if (compute_reloc_for_rtx (x) & targetm.asm_out.reloc_rw_mask ())
6356     return data_section;
6357   else
6358     return readonly_data_section;
6359 }
6360
6361 section *
6362 default_elf_select_rtx_section (enum machine_mode mode, rtx x,
6363                                 unsigned HOST_WIDE_INT align)
6364 {
6365   int reloc = compute_reloc_for_rtx (x);
6366
6367   /* ??? Handle small data here somehow.  */
6368
6369   if (reloc & targetm.asm_out.reloc_rw_mask ())
6370     {
6371       if (reloc == 1)
6372         return get_named_section (NULL, ".data.rel.ro.local", 1);
6373       else
6374         return get_named_section (NULL, ".data.rel.ro", 3);
6375     }
6376
6377   return mergeable_constant_section (mode, align, 0);
6378 }
6379
6380 /* Set the generally applicable flags on the SYMBOL_REF for EXP.  */
6381
6382 void
6383 default_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
6384 {
6385   rtx symbol;
6386   int flags;
6387
6388   /* Careful not to prod global register variables.  */
6389   if (!MEM_P (rtl))
6390     return;
6391   symbol = XEXP (rtl, 0);
6392   if (GET_CODE (symbol) != SYMBOL_REF)
6393     return;
6394
6395   flags = SYMBOL_REF_FLAGS (symbol) & SYMBOL_FLAG_HAS_BLOCK_INFO;
6396   if (TREE_CODE (decl) == FUNCTION_DECL)
6397     flags |= SYMBOL_FLAG_FUNCTION;
6398   if (targetm.binds_local_p (decl))
6399     flags |= SYMBOL_FLAG_LOCAL;
6400   if (targetm.have_tls && TREE_CODE (decl) == VAR_DECL
6401       && DECL_THREAD_LOCAL_P (decl))
6402     flags |= DECL_TLS_MODEL (decl) << SYMBOL_FLAG_TLS_SHIFT;
6403   else if (targetm.in_small_data_p (decl))
6404     flags |= SYMBOL_FLAG_SMALL;
6405   /* ??? Why is DECL_EXTERNAL ever set for non-PUBLIC names?  Without
6406      being PUBLIC, the thing *must* be defined in this translation unit.
6407      Prevent this buglet from being propagated into rtl code as well.  */
6408   if (DECL_P (decl) && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl))
6409     flags |= SYMBOL_FLAG_EXTERNAL;
6410
6411   SYMBOL_REF_FLAGS (symbol) = flags;
6412 }
6413
6414 /* By default, we do nothing for encode_section_info, so we need not
6415    do anything but discard the '*' marker.  */
6416
6417 const char *
6418 default_strip_name_encoding (const char *str)
6419 {
6420   return str + (*str == '*');
6421 }
6422
6423 #ifdef ASM_OUTPUT_DEF
6424 /* The default implementation of TARGET_ASM_OUTPUT_ANCHOR.  Define the
6425    anchor relative to ".", the current section position.  */
6426
6427 void
6428 default_asm_output_anchor (rtx symbol)
6429 {
6430   char buffer[100];
6431
6432   sprintf (buffer, "*. + " HOST_WIDE_INT_PRINT_DEC,
6433            SYMBOL_REF_BLOCK_OFFSET (symbol));
6434   ASM_OUTPUT_DEF (asm_out_file, XSTR (symbol, 0), buffer);
6435 }
6436 #endif
6437
6438 /* The default implementation of TARGET_USE_ANCHORS_FOR_SYMBOL_P.  */
6439
6440 bool
6441 default_use_anchors_for_symbol_p (const_rtx symbol)
6442 {
6443   section *sect;
6444   tree decl;
6445
6446   /* Don't use anchors for mergeable sections.  The linker might move
6447      the objects around.  */
6448   sect = SYMBOL_REF_BLOCK (symbol)->sect;
6449   if (sect->common.flags & SECTION_MERGE)
6450     return false;
6451
6452   /* Don't use anchors for small data sections.  The small data register
6453      acts as an anchor for such sections.  */
6454   if (sect->common.flags & SECTION_SMALL)
6455     return false;
6456
6457   decl = SYMBOL_REF_DECL (symbol);
6458   if (decl && DECL_P (decl))
6459     {
6460       /* Don't use section anchors for decls that might be defined by
6461          other modules.  */
6462       if (!targetm.binds_local_p (decl))
6463         return false;
6464
6465       /* Don't use section anchors for decls that will be placed in a
6466          small data section.  */
6467       /* ??? Ideally, this check would be redundant with the SECTION_SMALL
6468          one above.  The problem is that we only use SECTION_SMALL for
6469          sections that should be marked as small in the section directive.  */
6470       if (targetm.in_small_data_p (decl))
6471         return false;
6472     }
6473   return true;
6474 }
6475
6476 /* Assume ELF-ish defaults, since that's pretty much the most liberal
6477    wrt cross-module name binding.  */
6478
6479 bool
6480 default_binds_local_p (const_tree exp)
6481 {
6482   return default_binds_local_p_1 (exp, flag_shlib);
6483 }
6484
6485 bool
6486 default_binds_local_p_1 (const_tree exp, int shlib)
6487 {
6488   bool local_p;
6489
6490   /* A non-decl is an entry in the constant pool.  */
6491   if (!DECL_P (exp))
6492     local_p = true;
6493   /* Weakrefs may not bind locally, even though the weakref itself is
6494      always static and therefore local.  */
6495   else if (lookup_attribute ("weakref", DECL_ATTRIBUTES (exp)))
6496     local_p = false;
6497   /* Static variables are always local.  */
6498   else if (! TREE_PUBLIC (exp))
6499     local_p = true;
6500   /* A variable is local if the user has said explicitly that it will
6501      be.  */
6502   else if (DECL_VISIBILITY_SPECIFIED (exp)
6503            && DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
6504     local_p = true;
6505   /* Variables defined outside this object might not be local.  */
6506   else if (DECL_EXTERNAL (exp))
6507     local_p = false;
6508   /* If defined in this object and visibility is not default, must be
6509      local.  */
6510   else if (DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
6511     local_p = true;
6512   /* Default visibility weak data can be overridden by a strong symbol
6513      in another module and so are not local.  */
6514   else if (DECL_WEAK (exp))
6515     local_p = false;
6516   /* If PIC, then assume that any global name can be overridden by
6517      symbols resolved from other modules, unless we are compiling with
6518      -fwhole-program, which assumes that names are local.  */
6519   else if (shlib)
6520     local_p = flag_whole_program;
6521   /* Uninitialized COMMON variable may be unified with symbols
6522      resolved from other modules.  */
6523   else if (DECL_COMMON (exp)
6524            && (DECL_INITIAL (exp) == NULL
6525                || DECL_INITIAL (exp) == error_mark_node))
6526     local_p = false;
6527   /* Otherwise we're left with initialized (or non-common) global data
6528      which is of necessity defined locally.  */
6529   else
6530     local_p = true;
6531
6532   return local_p;
6533 }
6534
6535 /* Determine whether or not a pointer mode is valid. Assume defaults
6536    of ptr_mode or Pmode - can be overridden.  */
6537 bool
6538 default_valid_pointer_mode (enum machine_mode mode)
6539 {
6540   return (mode == ptr_mode || mode == Pmode);
6541 }
6542
6543 /* Default function to output code that will globalize a label.  A
6544    target must define GLOBAL_ASM_OP or provide its own function to
6545    globalize a label.  */
6546 #ifdef GLOBAL_ASM_OP
6547 void
6548 default_globalize_label (FILE * stream, const char *name)
6549 {
6550   fputs (GLOBAL_ASM_OP, stream);
6551   assemble_name (stream, name);
6552   putc ('\n', stream);
6553 }
6554 #endif /* GLOBAL_ASM_OP */
6555
6556 /* Default function to output code that will globalize a declaration.  */
6557 void
6558 default_globalize_decl_name (FILE * stream, tree decl)
6559 {
6560   const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
6561   targetm.asm_out.globalize_label (stream, name);
6562 }
6563
6564 /* Default function to output a label for unwind information.  The
6565    default is to do nothing.  A target that needs nonlocal labels for
6566    unwind information must provide its own function to do this.  */
6567 void
6568 default_emit_unwind_label (FILE * stream ATTRIBUTE_UNUSED,
6569                            tree decl ATTRIBUTE_UNUSED,
6570                            int for_eh ATTRIBUTE_UNUSED,
6571                            int empty ATTRIBUTE_UNUSED)
6572 {
6573 }
6574
6575 /* Default function to output a label to divide up the exception table.
6576    The default is to do nothing.  A target that needs/wants to divide
6577    up the table must provide it's own function to do this.  */
6578 void
6579 default_emit_except_table_label (FILE * stream ATTRIBUTE_UNUSED)
6580 {
6581 }
6582
6583 /* This is how to output an internal numbered label where PREFIX is
6584    the class of label and LABELNO is the number within the class.  */
6585
6586 void
6587 default_internal_label (FILE *stream, const char *prefix,
6588                         unsigned long labelno)
6589 {
6590   char *const buf = (char *) alloca (40 + strlen (prefix));
6591   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
6592   ASM_OUTPUT_INTERNAL_LABEL (stream, buf);
6593 }
6594
6595 /* This is the default behavior at the beginning of a file.  It's
6596    controlled by two other target-hook toggles.  */
6597 void
6598 default_file_start (void)
6599 {
6600   if (targetm.file_start_app_off
6601       && !(flag_verbose_asm || flag_debug_asm || flag_dump_rtl_in_asm))
6602     fputs (ASM_APP_OFF, asm_out_file);
6603
6604   if (targetm.file_start_file_directive)
6605     output_file_directive (asm_out_file, main_input_filename);
6606 }
6607
6608 /* This is a generic routine suitable for use as TARGET_ASM_FILE_END
6609    which emits a special section directive used to indicate whether or
6610    not this object file needs an executable stack.  This is primarily
6611    a GNU extension to ELF but could be used on other targets.  */
6612
6613 int trampolines_created;
6614
6615 void
6616 file_end_indicate_exec_stack (void)
6617 {
6618   unsigned int flags = SECTION_DEBUG;
6619   if (trampolines_created)
6620     flags |= SECTION_CODE;
6621
6622   switch_to_section (get_section (".note.GNU-stack", flags, NULL));
6623 }
6624
6625 /* Output DIRECTIVE (a C string) followed by a newline.  This is used as
6626    a get_unnamed_section callback.  */
6627
6628 void
6629 output_section_asm_op (const void *directive)
6630 {
6631   fprintf (asm_out_file, "%s\n", (const char *) directive);
6632 }
6633
6634 /* Emit assembly code to switch to section NEW_SECTION.  Do nothing if
6635    the current section is NEW_SECTION.  */
6636
6637 void
6638 switch_to_section (section *new_section)
6639 {
6640   if (in_section == new_section)
6641     return;
6642
6643   if (new_section->common.flags & SECTION_FORGET)
6644     in_section = NULL;
6645   else
6646     in_section = new_section;
6647
6648   switch (SECTION_STYLE (new_section))
6649     {
6650     case SECTION_NAMED:
6651       if (cfun
6652           && !crtl->subsections.unlikely_text_section_name
6653           && strcmp (new_section->named.name,
6654                      UNLIKELY_EXECUTED_TEXT_SECTION_NAME) == 0)
6655         crtl->subsections.unlikely_text_section_name = UNLIKELY_EXECUTED_TEXT_SECTION_NAME;
6656
6657       targetm.asm_out.named_section (new_section->named.name,
6658                                      new_section->named.common.flags,
6659                                      new_section->named.decl);
6660       break;
6661
6662     case SECTION_UNNAMED:
6663       new_section->unnamed.callback (new_section->unnamed.data);
6664       break;
6665
6666     case SECTION_NOSWITCH:
6667       gcc_unreachable ();
6668       break;
6669     }
6670
6671   new_section->common.flags |= SECTION_DECLARED;
6672 }
6673
6674 /* If block symbol SYMBOL has not yet been assigned an offset, place
6675    it at the end of its block.  */
6676
6677 void
6678 place_block_symbol (rtx symbol)
6679 {
6680   unsigned HOST_WIDE_INT size, mask, offset;
6681   struct constant_descriptor_rtx *desc;
6682   unsigned int alignment;
6683   struct object_block *block;
6684   tree decl;
6685
6686   gcc_assert (SYMBOL_REF_BLOCK (symbol));
6687   if (SYMBOL_REF_BLOCK_OFFSET (symbol) >= 0)
6688     return;
6689
6690   /* Work out the symbol's size and alignment.  */
6691   if (CONSTANT_POOL_ADDRESS_P (symbol))
6692     {
6693       desc = SYMBOL_REF_CONSTANT (symbol);
6694       alignment = desc->align;
6695       size = GET_MODE_SIZE (desc->mode);
6696     }
6697   else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
6698     {
6699       decl = SYMBOL_REF_DECL (symbol);
6700       alignment = get_constant_alignment (decl);
6701       size = get_constant_size (decl);
6702     }
6703   else
6704     {
6705       decl = SYMBOL_REF_DECL (symbol);
6706       alignment = DECL_ALIGN (decl);
6707       size = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
6708     }
6709
6710   /* Calculate the object's offset from the start of the block.  */
6711   block = SYMBOL_REF_BLOCK (symbol);
6712   mask = alignment / BITS_PER_UNIT - 1;
6713   offset = (block->size + mask) & ~mask;
6714   SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
6715
6716   /* Record the block's new alignment and size.  */
6717   block->alignment = MAX (block->alignment, alignment);
6718   block->size = offset + size;
6719
6720   VEC_safe_push (rtx, gc, block->objects, symbol);
6721 }
6722
6723 /* Return the anchor that should be used to address byte offset OFFSET
6724    from the first object in BLOCK.  MODEL is the TLS model used
6725    to access it.  */
6726
6727 rtx
6728 get_section_anchor (struct object_block *block, HOST_WIDE_INT offset,
6729                     enum tls_model model)
6730 {
6731   char label[100];
6732   unsigned int begin, middle, end;
6733   unsigned HOST_WIDE_INT min_offset, max_offset, range, bias, delta;
6734   rtx anchor;
6735
6736   /* Work out the anchor's offset.  Use an offset of 0 for the first
6737      anchor so that we don't pessimize the case where we take the address
6738      of a variable at the beginning of the block.  This is particularly
6739      useful when a block has only one variable assigned to it.
6740
6741      We try to place anchors RANGE bytes apart, so there can then be
6742      anchors at +/-RANGE, +/-2 * RANGE, and so on, up to the limits of
6743      a ptr_mode offset.  With some target settings, the lowest such
6744      anchor might be out of range for the lowest ptr_mode offset;
6745      likewise the highest anchor for the highest offset.  Use anchors
6746      at the extreme ends of the ptr_mode range in such cases.
6747
6748      All arithmetic uses unsigned integers in order to avoid
6749      signed overflow.  */
6750   max_offset = (unsigned HOST_WIDE_INT) targetm.max_anchor_offset;
6751   min_offset = (unsigned HOST_WIDE_INT) targetm.min_anchor_offset;
6752   range = max_offset - min_offset + 1;
6753   if (range == 0)
6754     offset = 0;
6755   else
6756     {
6757       bias = 1 << (GET_MODE_BITSIZE (ptr_mode) - 1);
6758       if (offset < 0)
6759         {
6760           delta = -(unsigned HOST_WIDE_INT) offset + max_offset;
6761           delta -= delta % range;
6762           if (delta > bias)
6763             delta = bias;
6764           offset = (HOST_WIDE_INT) (-delta);
6765         }
6766       else
6767         {
6768           delta = (unsigned HOST_WIDE_INT) offset - min_offset;
6769           delta -= delta % range;
6770           if (delta > bias - 1)
6771             delta = bias - 1;
6772           offset = (HOST_WIDE_INT) delta;
6773         }
6774     }
6775
6776   /* Do a binary search to see if there's already an anchor we can use.
6777      Set BEGIN to the new anchor's index if not.  */
6778   begin = 0;
6779   end = VEC_length (rtx, block->anchors);
6780   while (begin != end)
6781     {
6782       middle = (end + begin) / 2;
6783       anchor = VEC_index (rtx, block->anchors, middle);
6784       if (SYMBOL_REF_BLOCK_OFFSET (anchor) > offset)
6785         end = middle;
6786       else if (SYMBOL_REF_BLOCK_OFFSET (anchor) < offset)
6787         begin = middle + 1;
6788       else if (SYMBOL_REF_TLS_MODEL (anchor) > model)
6789         end = middle;
6790       else if (SYMBOL_REF_TLS_MODEL (anchor) < model)
6791         begin = middle + 1;
6792       else
6793         return anchor;
6794     }
6795
6796   /* Create a new anchor with a unique label.  */
6797   ASM_GENERATE_INTERNAL_LABEL (label, "LANCHOR", anchor_labelno++);
6798   anchor = create_block_symbol (ggc_strdup (label), block, offset);
6799   SYMBOL_REF_FLAGS (anchor) |= SYMBOL_FLAG_LOCAL | SYMBOL_FLAG_ANCHOR;
6800   SYMBOL_REF_FLAGS (anchor) |= model << SYMBOL_FLAG_TLS_SHIFT;
6801
6802   /* Insert it at index BEGIN.  */
6803   VEC_safe_insert (rtx, gc, block->anchors, begin, anchor);
6804   return anchor;
6805 }
6806
6807 /* Output the objects in BLOCK.  */
6808
6809 static void
6810 output_object_block (struct object_block *block)
6811 {
6812   struct constant_descriptor_rtx *desc;
6813   unsigned int i;
6814   HOST_WIDE_INT offset;
6815   tree decl;
6816   rtx symbol;
6817
6818   if (block->objects == NULL)
6819     return;
6820
6821   /* Switch to the section and make sure that the first byte is
6822      suitably aligned.  */
6823   switch_to_section (block->sect);
6824   assemble_align (block->alignment);
6825
6826   /* Define the values of all anchors relative to the current section
6827      position.  */
6828   for (i = 0; VEC_iterate (rtx, block->anchors, i, symbol); i++)
6829     targetm.asm_out.output_anchor (symbol);
6830
6831   /* Output the objects themselves.  */
6832   offset = 0;
6833   for (i = 0; VEC_iterate (rtx, block->objects, i, symbol); i++)
6834     {
6835       /* Move to the object's offset, padding with zeros if necessary.  */
6836       assemble_zeros (SYMBOL_REF_BLOCK_OFFSET (symbol) - offset);
6837       offset = SYMBOL_REF_BLOCK_OFFSET (symbol);
6838       if (CONSTANT_POOL_ADDRESS_P (symbol))
6839         {
6840           desc = SYMBOL_REF_CONSTANT (symbol);
6841           output_constant_pool_1 (desc, 1);
6842           offset += GET_MODE_SIZE (desc->mode);
6843         }
6844       else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
6845         {
6846           decl = SYMBOL_REF_DECL (symbol);
6847           assemble_constant_contents (decl, XSTR (symbol, 0),
6848                                       get_constant_alignment (decl));
6849           offset += get_constant_size (decl);
6850         }
6851       else
6852         {
6853           decl = SYMBOL_REF_DECL (symbol);
6854           assemble_variable_contents (decl, XSTR (symbol, 0), false);
6855           offset += tree_low_cst (DECL_SIZE_UNIT (decl), 1);
6856         }
6857     }
6858 }
6859
6860 /* A htab_traverse callback used to call output_object_block for
6861    each member of object_block_htab.  */
6862
6863 static int
6864 output_object_block_htab (void **slot, void *data ATTRIBUTE_UNUSED)
6865 {
6866   output_object_block ((struct object_block *) (*slot));
6867   return 1;
6868 }
6869
6870 /* Output the definitions of all object_blocks.  */
6871
6872 void
6873 output_object_blocks (void)
6874 {
6875   htab_traverse (object_block_htab, output_object_block_htab, NULL);
6876 }
6877
6878 /* This function provides a possible implementation of the
6879    TARGET_ASM_RECORD_GCC_SWITCHES target hook for ELF targets.  When triggered
6880    by -frecord-gcc-switches it creates a new mergeable, string section in the
6881    assembler output file called TARGET_ASM_RECORD_GCC_SWITCHES_SECTION which
6882    contains the switches in ASCII format.
6883
6884    FIXME: This code does not correctly handle double quote characters
6885    that appear inside strings, (it strips them rather than preserving them).
6886    FIXME: ASM_OUTPUT_ASCII, as defined in config/elfos.h will not emit NUL
6887    characters - instead it treats them as sub-string separators.  Since
6888    we want to emit NUL strings terminators into the object file we have to use
6889    ASM_OUTPUT_SKIP.  */
6890
6891 int
6892 elf_record_gcc_switches (print_switch_type type, const char * name)
6893 {
6894   static char buffer[1024];
6895
6896   /* This variable is used as part of a simplistic heuristic to detect
6897      command line switches which take an argument:
6898
6899        "If a command line option does not start with a dash then
6900         it is an argument for the previous command line option."
6901
6902      This fails in the case of the command line option which is the name
6903      of the file to compile, but otherwise it is pretty reasonable.  */
6904   static bool previous_name_held_back = FALSE;
6905
6906   switch (type)
6907     {
6908     case SWITCH_TYPE_PASSED:
6909       if (* name != '-')
6910         {
6911           if (previous_name_held_back)
6912             {
6913               unsigned int len = strlen (buffer);
6914
6915               snprintf (buffer + len, sizeof buffer - len, " %s", name);
6916               ASM_OUTPUT_ASCII (asm_out_file, buffer, strlen (buffer));
6917               ASM_OUTPUT_SKIP (asm_out_file, (unsigned HOST_WIDE_INT) 1);
6918               previous_name_held_back = FALSE;
6919             }
6920           else
6921             {
6922               strncpy (buffer, name, sizeof buffer);
6923               ASM_OUTPUT_ASCII (asm_out_file, buffer, strlen (buffer));
6924               ASM_OUTPUT_SKIP (asm_out_file, (unsigned HOST_WIDE_INT) 1);
6925             }
6926         }
6927       else
6928         {
6929           if (previous_name_held_back)
6930             {
6931               ASM_OUTPUT_ASCII (asm_out_file, buffer, strlen (buffer));
6932               ASM_OUTPUT_SKIP (asm_out_file, (unsigned HOST_WIDE_INT) 1);
6933             }
6934
6935           strncpy (buffer, name, sizeof buffer);
6936           previous_name_held_back = TRUE;
6937         }
6938       break;
6939
6940     case SWITCH_TYPE_DESCRIPTIVE:
6941       if (name == NULL)
6942         {
6943           /* Distinguish between invocations where name is NULL.  */
6944           static bool started = false;
6945
6946           if (started)
6947             {
6948               if (previous_name_held_back)
6949                 {
6950                   ASM_OUTPUT_ASCII (asm_out_file, buffer, strlen (buffer));
6951                   ASM_OUTPUT_SKIP (asm_out_file, (unsigned HOST_WIDE_INT) 1);
6952                 }
6953             }
6954           else
6955             {
6956               section * sec;
6957
6958               sec = get_section (targetm.asm_out.record_gcc_switches_section,
6959                                  SECTION_DEBUG
6960                                  | SECTION_MERGE
6961                                  | SECTION_STRINGS
6962                                  | (SECTION_ENTSIZE & 1),
6963                                  NULL);
6964               switch_to_section (sec);
6965               started = true;
6966             }
6967         }
6968
6969     default:
6970       break;
6971     }
6972
6973   /* The return value is currently ignored by the caller, but must be 0.
6974      For -fverbose-asm the return value would be the number of characters
6975      emitted into the assembler file.  */
6976   return 0;
6977 }
6978
6979 /* Emit text to declare externally defined symbols. It is needed to
6980    properly support non-default visibility.  */
6981 void
6982 default_elf_asm_output_external (FILE *file ATTRIBUTE_UNUSED,
6983                                  tree decl,
6984                                  const char *name ATTRIBUTE_UNUSED)
6985 {
6986   /* We output the name if and only if TREE_SYMBOL_REFERENCED is
6987      set in order to avoid putting out names that are never really
6988      used. */
6989   if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
6990       && targetm.binds_local_p (decl))
6991     maybe_assemble_visibility (decl);
6992 }
6993
6994 #include "gt-varasm.h"