varasm.c revision 235623
118334Speter/* Output variables, constants and external declarations, for GNU compiler.
290075Sobrien   Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997,
3169689Skan   1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
4169689Skan   Free Software Foundation, Inc.
518334Speter
690075SobrienThis file is part of GCC.
718334Speter
890075SobrienGCC is free software; you can redistribute it and/or modify it under
990075Sobrienthe terms of the GNU General Public License as published by the Free
1090075SobrienSoftware Foundation; either version 2, or (at your option) any later
1190075Sobrienversion.
1218334Speter
1390075SobrienGCC is distributed in the hope that it will be useful, but WITHOUT ANY
1490075SobrienWARRANTY; without even the implied warranty of MERCHANTABILITY or
1590075SobrienFITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1690075Sobrienfor more details.
1718334Speter
1818334SpeterYou should have received a copy of the GNU General Public License
1990075Sobrienalong with GCC; see the file COPYING.  If not, write to the Free
20169689SkanSoftware Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21169689Skan02110-1301, USA.  */
2218334Speter
2318334Speter
2418334Speter/* This file handles generation of all the assembler code
2518334Speter   *except* the instructions of a function.
2618334Speter   This includes declarations of variables and their initial values.
2718334Speter
2818334Speter   We also output the assembler code for constants stored in memory
2918334Speter   and are responsible for combining constants with the same value.  */
3018334Speter
3150397Sobrien#include "config.h"
3250397Sobrien#include "system.h"
33132718Skan#include "coretypes.h"
34132718Skan#include "tm.h"
3518334Speter#include "rtl.h"
3618334Speter#include "tree.h"
3718334Speter#include "flags.h"
3818334Speter#include "function.h"
3918334Speter#include "expr.h"
4018334Speter#include "hard-reg-set.h"
4118334Speter#include "regs.h"
42117395Skan#include "real.h"
4390075Sobrien#include "output.h"
4450397Sobrien#include "toplev.h"
4590075Sobrien#include "hashtab.h"
4618334Speter#include "c-pragma.h"
4790075Sobrien#include "ggc.h"
4890075Sobrien#include "langhooks.h"
4990075Sobrien#include "tm_p.h"
5090075Sobrien#include "debug.h"
5190075Sobrien#include "target.h"
52169689Skan#include "tree-mudflap.h"
53132718Skan#include "cgraph.h"
54169689Skan#include "cfglayout.h"
55169689Skan#include "basic-block.h"
5618334Speter
5718334Speter#ifdef XCOFF_DEBUGGING_INFO
5890075Sobrien#include "xcoffout.h"		/* Needed for external data
5990075Sobrien				   declarations for e.g. AIX 4.x.  */
6018334Speter#endif
6118334Speter
62169689Skan/* The (assembler) name of the first globally-visible object output.  */
63169689Skanextern GTY(()) const char *first_global_object_name;
64169689Skanextern GTY(()) const char *weak_global_object_name;
6518334Speter
6690075Sobrienconst char *first_global_object_name;
6790075Sobrienconst char *weak_global_object_name;
6818334Speter
6990075Sobrienstruct addr_const;
70117395Skanstruct constant_descriptor_rtx;
71169689Skanstruct rtx_constant_pool;
7290075Sobrien
73117395Skanstruct varasm_status GTY(())
7490075Sobrien{
75169689Skan  /* If we're using a per-function constant pool, this is it.  */
76169689Skan  struct rtx_constant_pool *pool;
7790075Sobrien
78132718Skan  /* Number of tree-constants deferred during the expansion of this
79132718Skan     function.  */
80132718Skan  unsigned int deferred_constants;
8190075Sobrien};
8290075Sobrien
83132718Skan#define n_deferred_constants (cfun->varasm->deferred_constants)
8490075Sobrien
8518334Speter/* Number for making the label on the next
8618334Speter   constant that is stored in memory.  */
8718334Speter
88132718Skanstatic GTY(()) int const_labelno;
8918334Speter
9018334Speter/* Carry information from ASM_DECLARE_OBJECT_NAME
9118334Speter   to ASM_FINISH_DECLARE_OBJECT.  */
9218334Speter
9318334Speterint size_directive_output;
9418334Speter
9518334Speter/* The last decl for which assemble_variable was called,
9618334Speter   if it did ASM_DECLARE_OBJECT_NAME.
9718334Speter   If the last call to assemble_variable didn't do that,
9818334Speter   this holds 0.  */
9918334Speter
10018334Spetertree last_assemble_variable_decl;
10118334Speter
102169689Skan/* The following global variable indicates if the first basic block
103169689Skan   in a function belongs to the cold partition or not.  */
10418334Speter
105169689Skanbool first_function_block_is_cold;
106169689Skan
107169689Skan/* We give all constants their own alias set.  Perhaps redundant with
108169689Skan   MEM_READONLY_P, but pre-dates it.  */
109169689Skan
11090075Sobrienstatic HOST_WIDE_INT const_alias_set;
11118334Speter
112132718Skanstatic const char *strip_reg_name (const char *);
113132718Skanstatic int contains_pointers_p (tree);
114132718Skan#ifdef ASM_OUTPUT_EXTERNAL
115132718Skanstatic bool incorporeal_function_p (tree);
116132718Skan#endif
117132718Skanstatic void decode_addr_const (tree, struct addr_const *);
118132718Skanstatic hashval_t const_desc_hash (const void *);
119132718Skanstatic int const_desc_eq (const void *, const void *);
120132718Skanstatic hashval_t const_hash_1 (const tree);
121132718Skanstatic int compare_constant (const tree, const tree);
122132718Skanstatic tree copy_constant (tree);
123132718Skanstatic void output_constant_def_contents (rtx);
124132718Skanstatic void output_addressed_constants (tree);
125132718Skanstatic unsigned HOST_WIDE_INT array_size_for_constructor (tree);
126132718Skanstatic unsigned min_align (unsigned, unsigned);
127132718Skanstatic void output_constructor (tree, unsigned HOST_WIDE_INT, unsigned int);
128132718Skanstatic void globalize_decl (tree);
129169689Skan#ifdef BSS_SECTION_ASM_OP
13050397Sobrien#ifdef ASM_OUTPUT_BSS
131132718Skanstatic void asm_output_bss (FILE *, tree, const char *,
132132718Skan			    unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
13350397Sobrien#endif
13450397Sobrien#ifdef ASM_OUTPUT_ALIGNED_BSS
135132718Skanstatic void asm_output_aligned_bss (FILE *, tree, const char *,
136132718Skan				    unsigned HOST_WIDE_INT, int)
137132718Skan     ATTRIBUTE_UNUSED;
13850397Sobrien#endif
13950397Sobrien#endif /* BSS_SECTION_ASM_OP */
140132718Skanstatic void mark_weak (tree);
141169689Skanstatic void output_constant_pool (const char *, tree);
14218334Speter
143169689Skan/* Well-known sections, each one associated with some sort of *_ASM_OP.  */
144169689Skansection *text_section;
145169689Skansection *data_section;
146169689Skansection *readonly_data_section;
147169689Skansection *sdata_section;
148169689Skansection *ctors_section;
149169689Skansection *dtors_section;
150169689Skansection *bss_section;
151169689Skansection *sbss_section;
15218334Speter
153169689Skan/* Various forms of common section.  All are guaranteed to be nonnull.  */
154169689Skansection *tls_comm_section;
155169689Skansection *comm_section;
156169689Skansection *lcomm_section;
157169689Skan
158169689Skan/* A SECTION_NOSWITCH section used for declaring global BSS variables.
159169689Skan   May be null.  */
160169689Skansection *bss_noswitch_section;
161169689Skan
162169689Skan/* The section that holds the main exception table, when known.  The section
163169689Skan   is set either by the target's init_sections hook or by the first call to
164169689Skan   switch_to_exception_section.  */
165169689Skansection *exception_section;
166169689Skan
167169689Skan/* The section that holds the DWARF2 frame unwind information, when known.
168169689Skan   The section is set either by the target's init_sections hook or by the
169169689Skan   first call to switch_to_eh_frame_section.  */
170169689Skansection *eh_frame_section;
171169689Skan
172169689Skan/* asm_out_file's current section.  This is NULL if no section has yet
173169689Skan   been selected or if we lose track of what the current section is.  */
174169689Skansection *in_section;
175169689Skan
176169689Skan/* True if code for the current function is currently being directed
177169689Skan   at the cold section.  */
178169689Skanbool in_cold_section_p;
179169689Skan
180169689Skan/* A linked list of all the unnamed sections.  */
181169689Skanstatic GTY(()) section *unnamed_sections;
182169689Skan
183117395Skan/* Return a nonzero value if DECL has a section attribute.  */
18490075Sobrien#ifndef IN_NAMED_SECTION
18518334Speter#define IN_NAMED_SECTION(DECL) \
18618334Speter  ((TREE_CODE (DECL) == FUNCTION_DECL || TREE_CODE (DECL) == VAR_DECL) \
18718334Speter   && DECL_SECTION_NAME (DECL) != NULL_TREE)
18890075Sobrien#endif
18918334Speter
190169689Skan/* Hash table of named sections.  */
191169689Skanstatic GTY((param_is (section))) htab_t section_htab;
19218334Speter
193169689Skan/* A table of object_blocks, indexed by section.  */
194169689Skanstatic GTY((param_is (struct object_block))) htab_t object_block_htab;
19590075Sobrien
196169689Skan/* The next number to use for internal anchor labels.  */
197169689Skanstatic GTY(()) int anchor_labelno;
19890075Sobrien
199169689Skan/* A pool of constants that can be shared between functions.  */
200169689Skanstatic GTY(()) struct rtx_constant_pool *shared_constant_pool;
20190075Sobrien
202169689Skan/* Helper routines for maintaining section_htab.  */
20318334Speter
204169689Skanstatic int
205169689Skansection_entry_eq (const void *p1, const void *p2)
206169689Skan{
207169689Skan  const section *old = p1;
208169689Skan  const char *new = p2;
20918334Speter
210169689Skan  return strcmp (old->named.name, new) == 0;
21118334Speter}
21218334Speter
213169689Skanstatic hashval_t
214169689Skansection_entry_hash (const void *p)
21518334Speter{
216169689Skan  const section *old = p;
217169689Skan  return htab_hash_string (old->named.name);
21818334Speter}
219117395Skan
220169689Skan/* Return a hash value for section SECT.  */
22118334Speter
222169689Skanstatic hashval_t
223169689Skanhash_section (section *sect)
22418334Speter{
225169689Skan  if (sect->common.flags & SECTION_NAMED)
226169689Skan    return htab_hash_string (sect->named.name);
227169689Skan  return sect->common.flags;
22818334Speter}
22918334Speter
230169689Skan/* Helper routines for maintaining object_block_htab.  */
23118334Speter
232169689Skanstatic int
233169689Skanobject_block_entry_eq (const void *p1, const void *p2)
23418334Speter{
235169689Skan  const struct object_block *old = p1;
236169689Skan  const section *new = p2;
237169689Skan
238169689Skan  return old->sect == new;
23918334Speter}
24018334Speter
241169689Skanstatic hashval_t
242169689Skanobject_block_entry_hash (const void *p)
24350397Sobrien{
244169689Skan  const struct object_block *old = p;
245169689Skan  return hash_section (old->sect);
24650397Sobrien}
24750397Sobrien
248169689Skan/* Return a new unnamed section with the given fields.  */
24990075Sobrien
250169689Skansection *
251169689Skanget_unnamed_section (unsigned int flags, void (*callback) (const void *),
252169689Skan		     const void *data)
25390075Sobrien{
254169689Skan  section *sect;
25590075Sobrien
256169689Skan  sect = ggc_alloc (sizeof (struct unnamed_section));
257169689Skan  sect->unnamed.common.flags = flags | SECTION_UNNAMED;
258169689Skan  sect->unnamed.callback = callback;
259169689Skan  sect->unnamed.data = data;
260169689Skan  sect->unnamed.next = unnamed_sections;
26190075Sobrien
262169689Skan  unnamed_sections = sect;
263169689Skan  return sect;
26490075Sobrien}
26590075Sobrien
266169689Skan/* Return a SECTION_NOSWITCH section with the given fields.  */
26790075Sobrien
268169689Skanstatic section *
269169689Skanget_noswitch_section (unsigned int flags, noswitch_section_callback callback)
27090075Sobrien{
271169689Skan  section *sect;
27290075Sobrien
273169689Skan  sect = ggc_alloc (sizeof (struct unnamed_section));
274169689Skan  sect->noswitch.common.flags = flags | SECTION_NOSWITCH;
275169689Skan  sect->noswitch.callback = callback;
27690075Sobrien
277169689Skan  return sect;
27890075Sobrien}
27990075Sobrien
280169689Skan/* Return the named section structure associated with NAME.  Create
281169689Skan   a new section with the given fields if no such structure exists.  */
28290075Sobrien
283169689Skansection *
284169689Skanget_section (const char *name, unsigned int flags, tree decl)
28590075Sobrien{
286169689Skan  section *sect, **slot;
28790075Sobrien
288169689Skan  slot = (section **)
289169689Skan    htab_find_slot_with_hash (section_htab, name,
290169689Skan			      htab_hash_string (name), INSERT);
291169689Skan  flags |= SECTION_NAMED;
292169689Skan  if (*slot == NULL)
29390075Sobrien    {
294169689Skan      sect = ggc_alloc (sizeof (struct named_section));
295169689Skan      sect->named.common.flags = flags;
296169689Skan      sect->named.name = ggc_strdup (name);
297169689Skan      sect->named.decl = decl;
298169689Skan      *slot = sect;
29990075Sobrien    }
300117395Skan  else
30190075Sobrien    {
302169689Skan      sect = *slot;
303169689Skan      if ((sect->common.flags & ~SECTION_DECLARED) != flags
304169689Skan	  && ((sect->common.flags | flags) & SECTION_OVERRIDE) == 0)
305169689Skan	{
306169689Skan	  /* Sanity check user variables for flag changes.  */
307169689Skan	  if (decl == 0)
308169689Skan	    decl = sect->named.decl;
309169689Skan	  gcc_assert (decl);
310169689Skan	  error ("%+D causes a section type conflict", decl);
311169689Skan	}
31290075Sobrien    }
313169689Skan  return sect;
31490075Sobrien}
31590075Sobrien
316169689Skan/* Return true if the current compilation mode benefits from having
317169689Skan   objects grouped into blocks.  */
31890075Sobrien
319169689Skanstatic bool
320169689Skanuse_object_blocks_p (void)
321169689Skan{
322169689Skan  return flag_section_anchors;
323169689Skan}
32490075Sobrien
325169689Skan/* Return the object_block structure for section SECT.  Create a new
326169689Skan   structure if we haven't created one already.  Return null if SECT
327169689Skan   itself is null.  */
328169689Skan
329169689Skanstatic struct object_block *
330169689Skanget_block_for_section (section *sect)
33190075Sobrien{
332169689Skan  struct object_block *block;
333169689Skan  void **slot;
33490075Sobrien
335169689Skan  if (sect == NULL)
336169689Skan    return NULL;
33790075Sobrien
338169689Skan  slot = htab_find_slot_with_hash (object_block_htab, sect,
339169689Skan				   hash_section (sect), INSERT);
340169689Skan  block = (struct object_block *) *slot;
341169689Skan  if (block == NULL)
34290075Sobrien    {
343169689Skan      block = (struct object_block *)
344169689Skan	ggc_alloc_cleared (sizeof (struct object_block));
345169689Skan      block->sect = sect;
346169689Skan      *slot = block;
34790075Sobrien    }
348169689Skan  return block;
349169689Skan}
35090075Sobrien
351169689Skan/* Create a symbol with label LABEL and place it at byte offset
352169689Skan   OFFSET in BLOCK.  OFFSET can be negative if the symbol's offset
353169689Skan   is not yet known.  LABEL must be a garbage-collected string.  */
354169689Skan
355169689Skanstatic rtx
356169689Skancreate_block_symbol (const char *label, struct object_block *block,
357169689Skan		     HOST_WIDE_INT offset)
358169689Skan{
359169689Skan  rtx symbol;
360169689Skan  unsigned int size;
361169689Skan
362169689Skan  /* Create the extended SYMBOL_REF.  */
363169689Skan  size = RTX_HDR_SIZE + sizeof (struct block_symbol);
364169689Skan  symbol = ggc_alloc_zone (size, &rtl_zone);
365169689Skan
366169689Skan  /* Initialize the normal SYMBOL_REF fields.  */
367169689Skan  memset (symbol, 0, size);
368169689Skan  PUT_CODE (symbol, SYMBOL_REF);
369169689Skan  PUT_MODE (symbol, Pmode);
370169689Skan  XSTR (symbol, 0) = label;
371169689Skan  SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_HAS_BLOCK_INFO;
372169689Skan
373169689Skan  /* Initialize the block_symbol stuff.  */
374169689Skan  SYMBOL_REF_BLOCK (symbol) = block;
375169689Skan  SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
376169689Skan
377169689Skan  return symbol;
37890075Sobrien}
37990075Sobrien
380169689Skanstatic void
381169689Skaninitialize_cold_section_name (void)
382169689Skan{
383169689Skan  const char *stripped_name;
384169689Skan  char *name, *buffer;
385169689Skan  tree dsn;
38690075Sobrien
387169689Skan  gcc_assert (cfun && current_function_decl);
388169689Skan  if (cfun->unlikely_text_section_name)
389169689Skan    return;
390169689Skan
391169689Skan  dsn = DECL_SECTION_NAME (current_function_decl);
392169689Skan  if (flag_function_sections && dsn)
39390075Sobrien    {
394169689Skan      name = alloca (TREE_STRING_LENGTH (dsn) + 1);
395169689Skan      memcpy (name, TREE_STRING_POINTER (dsn), TREE_STRING_LENGTH (dsn) + 1);
39690075Sobrien
397169689Skan      stripped_name = targetm.strip_name_encoding (name);
39890075Sobrien
399169689Skan      buffer = ACONCAT ((stripped_name, "_unlikely", NULL));
400169689Skan      cfun->unlikely_text_section_name = ggc_strdup (buffer);
40190075Sobrien    }
402169689Skan  else
403169689Skan    cfun->unlikely_text_section_name =  UNLIKELY_EXECUTED_TEXT_SECTION_NAME;
40490075Sobrien}
40590075Sobrien
406169689Skan/* Tell assembler to switch to unlikely-to-be-executed text section.  */
40718334Speter
408169689Skansection *
409169689Skanunlikely_text_section (void)
41018334Speter{
411169689Skan  if (cfun)
412169689Skan    {
413169689Skan      if (!cfun->unlikely_text_section_name)
414169689Skan	initialize_cold_section_name ();
415169689Skan
416169689Skan      return get_named_section (NULL, cfun->unlikely_text_section_name, 0);
417169689Skan    }
418169689Skan  else
419169689Skan    return get_named_section (NULL, UNLIKELY_EXECUTED_TEXT_SECTION_NAME, 0);
420169689Skan}
421169689Skan
422169689Skan/* When called within a function context, return true if the function
423169689Skan   has been assigned a cold text section and if SECT is that section.
424169689Skan   When called outside a function context, return true if SECT is the
425169689Skan   default cold section.  */
426169689Skan
427169689Skanbool
428169689Skanunlikely_text_section_p (section *sect)
429169689Skan{
430169689Skan  const char *name;
431169689Skan
432169689Skan  if (cfun)
433169689Skan    name = cfun->unlikely_text_section_name;
434169689Skan  else
435169689Skan    name = UNLIKELY_EXECUTED_TEXT_SECTION_NAME;
436169689Skan
437169689Skan  return (name
438169689Skan	  && sect
439169689Skan	  && SECTION_STYLE (sect) == SECTION_NAMED
440169689Skan	  && strcmp (name, sect->named.name) == 0);
441169689Skan}
442169689Skan
443169689Skan/* Return a section with a particular name and with whatever SECTION_*
444169689Skan   flags section_type_flags deems appropriate.  The name of the section
445169689Skan   is taken from NAME if nonnull, otherwise it is taken from DECL's
446169689Skan   DECL_SECTION_NAME.  DECL is the decl associated with the section
447169689Skan   (see the section comment for details) and RELOC is as for
448169689Skan   section_type_flags.  */
449169689Skan
450169689Skansection *
451169689Skanget_named_section (tree decl, const char *name, int reloc)
452169689Skan{
45390075Sobrien  unsigned int flags;
45490075Sobrien
455169689Skan  gcc_assert (!decl || DECL_P (decl));
45618334Speter  if (name == NULL)
45718334Speter    name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
45818334Speter
459169689Skan  flags = targetm.section_type_flags (decl, name, reloc);
46090075Sobrien
461169689Skan  return get_section (name, flags, decl);
46218334Speter}
46318334Speter
46490075Sobrien/* If required, set DECL_SECTION_NAME to a unique name.  */
46550397Sobrien
466132718Skanvoid
467132718Skanresolve_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED,
468132718Skan			int flag_function_or_data_sections)
46990075Sobrien{
47090075Sobrien  if (DECL_SECTION_NAME (decl) == NULL_TREE
471117395Skan      && targetm.have_named_sections
472102780Skan      && (flag_function_or_data_sections
473117395Skan	  || DECL_ONE_ONLY (decl)))
474169689Skan    targetm.asm_out.unique_section (decl, reloc);
47590075Sobrien}
47690075Sobrien
47750397Sobrien#ifdef BSS_SECTION_ASM_OP
47850397Sobrien
47950397Sobrien#ifdef ASM_OUTPUT_BSS
48050397Sobrien
48150397Sobrien/* Utility function for ASM_OUTPUT_BSS for targets to use if
48250397Sobrien   they don't support alignments in .bss.
48350397Sobrien   ??? It is believed that this function will work in most cases so such
48450397Sobrien   support is localized here.  */
48550397Sobrien
48650397Sobrienstatic void
487132718Skanasm_output_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
488132718Skan		const char *name,
489132718Skan		unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
490132718Skan		unsigned HOST_WIDE_INT rounded)
49150397Sobrien{
492169689Skan  targetm.asm_out.globalize_label (file, name);
493169689Skan  switch_to_section (bss_section);
49450397Sobrien#ifdef ASM_DECLARE_OBJECT_NAME
49550397Sobrien  last_assemble_variable_decl = decl;
49650397Sobrien  ASM_DECLARE_OBJECT_NAME (file, name, decl);
49750397Sobrien#else
49850397Sobrien  /* Standard thing is just output label for the object.  */
49950397Sobrien  ASM_OUTPUT_LABEL (file, name);
50050397Sobrien#endif /* ASM_DECLARE_OBJECT_NAME */
501102780Skan  ASM_OUTPUT_SKIP (file, rounded ? rounded : 1);
50250397Sobrien}
50350397Sobrien
50450397Sobrien#endif
50550397Sobrien
50650397Sobrien#ifdef ASM_OUTPUT_ALIGNED_BSS
50750397Sobrien
50850397Sobrien/* Utility function for targets to use in implementing
50950397Sobrien   ASM_OUTPUT_ALIGNED_BSS.
51050397Sobrien   ??? It is believed that this function will work in most cases so such
51150397Sobrien   support is localized here.  */
51250397Sobrien
51350397Sobrienstatic void
514132718Skanasm_output_aligned_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
515132718Skan			const char *name, unsigned HOST_WIDE_INT size,
516132718Skan			int align)
51750397Sobrien{
518169689Skan  switch_to_section (bss_section);
51950397Sobrien  ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
52050397Sobrien#ifdef ASM_DECLARE_OBJECT_NAME
52150397Sobrien  last_assemble_variable_decl = decl;
52250397Sobrien  ASM_DECLARE_OBJECT_NAME (file, name, decl);
52350397Sobrien#else
52450397Sobrien  /* Standard thing is just output label for the object.  */
52550397Sobrien  ASM_OUTPUT_LABEL (file, name);
52650397Sobrien#endif /* ASM_DECLARE_OBJECT_NAME */
52750397Sobrien  ASM_OUTPUT_SKIP (file, size ? size : 1);
52850397Sobrien}
52950397Sobrien
53050397Sobrien#endif
53150397Sobrien
53250397Sobrien#endif /* BSS_SECTION_ASM_OP */
53350397Sobrien
534169689Skan#ifndef USE_SELECT_SECTION_FOR_FUNCTIONS
535169689Skan/* Return the hot section for function DECL.  Return text_section for
536169689Skan   null DECLs.  */
53718334Speter
538169689Skanstatic section *
539169689Skanhot_function_section (tree decl)
540169689Skan{
541169689Skan  if (decl != NULL_TREE
542169689Skan      && DECL_SECTION_NAME (decl) != NULL_TREE
543169689Skan      && targetm.have_named_sections)
544169689Skan    return get_named_section (decl, NULL, 0);
545169689Skan  else
546169689Skan    return text_section;
547169689Skan}
548169689Skan#endif
54918334Speter
550169689Skan/* Return the section for function DECL.
551169689Skan
552169689Skan   If DECL is NULL_TREE, return the text section.  We can be passed
553169689Skan   NULL_TREE under some circumstances by dbxout.c at least.  */
554169689Skan
555169689Skansection *
556132718Skanfunction_section (tree decl)
55718334Speter{
558169689Skan  int reloc = 0;
559169689Skan
560169689Skan  if (first_function_block_is_cold)
561169689Skan    reloc = 1;
562169689Skan
563169689Skan#ifdef USE_SELECT_SECTION_FOR_FUNCTIONS
56418334Speter  if (decl != NULL_TREE
56518334Speter      && DECL_SECTION_NAME (decl) != NULL_TREE)
566169689Skan    return reloc ? unlikely_text_section ()
567169689Skan		 : get_named_section (decl, NULL, 0);
56850397Sobrien  else
569169689Skan    return targetm.asm_out.select_section (decl, reloc, DECL_ALIGN (decl));
570169689Skan#else
571169689Skan  return reloc ? unlikely_text_section () : hot_function_section (decl);
572169689Skan#endif
57318334Speter}
57450397Sobrien
575169689Skansection *
576169689Skancurrent_function_section (void)
57750397Sobrien{
578169689Skan#ifdef USE_SELECT_SECTION_FOR_FUNCTIONS
579169689Skan  if (current_function_decl != NULL_TREE
580169689Skan      && DECL_SECTION_NAME (current_function_decl) != NULL_TREE)
581169689Skan    return in_cold_section_p ? unlikely_text_section ()
582169689Skan			     : get_named_section (current_function_decl,
583169689Skan						  NULL, 0);
58450397Sobrien  else
585169689Skan    return targetm.asm_out.select_section (current_function_decl,
586169689Skan					   in_cold_section_p,
587169689Skan					   DECL_ALIGN (current_function_decl));
588169689Skan#else
589169689Skan  return (in_cold_section_p
590169689Skan	  ? unlikely_text_section ()
591169689Skan	  : hot_function_section (current_function_decl));
592169689Skan#endif
59350397Sobrien}
59450397Sobrien
595169689Skan/* Return the read-only data section associated with function DECL.  */
59690075Sobrien
597169689Skansection *
598169689Skandefault_function_rodata_section (tree decl)
599169689Skan{
600169689Skan  if (decl != NULL_TREE && DECL_SECTION_NAME (decl))
601169689Skan    {
602169689Skan      const char *name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
603169689Skan
604169689Skan      if (DECL_ONE_ONLY (decl) && HAVE_COMDAT_GROUP)
605169689Skan        {
606169689Skan	  size_t len = strlen (name) + 3;
607169689Skan	  char* rname = alloca (len);
608169689Skan
609169689Skan	  strcpy (rname, ".rodata");
610169689Skan	  strcat (rname, name + 5);
611169689Skan	  return get_section (rname, SECTION_LINKONCE, decl);
612169689Skan	}
613169689Skan      /* For .gnu.linkonce.t.foo we want to use .gnu.linkonce.r.foo.  */
614169689Skan      else if (DECL_ONE_ONLY (decl)
615169689Skan	       && strncmp (name, ".gnu.linkonce.t.", 16) == 0)
616169689Skan	{
617169689Skan	  size_t len = strlen (name) + 1;
618169689Skan	  char *rname = alloca (len);
619169689Skan
620169689Skan	  memcpy (rname, name, len);
621169689Skan	  rname[14] = 'r';
622169689Skan	  return get_section (rname, SECTION_LINKONCE, decl);
623169689Skan	}
624169689Skan      /* For .text.foo we want to use .rodata.foo.  */
625169689Skan      else if (flag_function_sections && flag_data_sections
626169689Skan	       && strncmp (name, ".text.", 6) == 0)
627169689Skan	{
628169689Skan	  size_t len = strlen (name) + 1;
629169689Skan	  char *rname = alloca (len + 2);
630169689Skan
631169689Skan	  memcpy (rname, ".rodata", 7);
632169689Skan	  memcpy (rname + 7, name + 5, len - 5);
633169689Skan	  return get_section (rname, 0, decl);
634169689Skan	}
635169689Skan    }
636169689Skan
637169689Skan  return readonly_data_section;
638169689Skan}
639169689Skan
640169689Skan/* Return the read-only data section associated with function DECL
641169689Skan   for targets where that section should be always the single
642169689Skan   readonly data section.  */
643169689Skan
644169689Skansection *
645169689Skandefault_no_function_rodata_section (tree decl ATTRIBUTE_UNUSED)
646169689Skan{
647169689Skan  return readonly_data_section;
648169689Skan}
649169689Skan
650169689Skan/* Return the section to use for string merging.  */
651169689Skan
652169689Skanstatic section *
653132718Skanmergeable_string_section (tree decl ATTRIBUTE_UNUSED,
654132718Skan			  unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
655132718Skan			  unsigned int flags ATTRIBUTE_UNUSED)
65618334Speter{
657169689Skan  HOST_WIDE_INT len;
658169689Skan
659132718Skan  if (HAVE_GAS_SHF_MERGE && flag_merge_constants
66090075Sobrien      && TREE_CODE (decl) == STRING_CST
66190075Sobrien      && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
66290075Sobrien      && align <= 256
663169689Skan      && (len = int_size_in_bytes (TREE_TYPE (decl))) > 0
664169689Skan      && TREE_STRING_LENGTH (decl) >= len)
66518334Speter    {
66690075Sobrien      enum machine_mode mode;
66790075Sobrien      unsigned int modesize;
66890075Sobrien      const char *str;
669169689Skan      HOST_WIDE_INT i;
670169689Skan      int j, unit;
67190075Sobrien      char name[30];
67218334Speter
67390075Sobrien      mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (decl)));
67490075Sobrien      modesize = GET_MODE_BITSIZE (mode);
67590075Sobrien      if (modesize >= 8 && modesize <= 256
67690075Sobrien	  && (modesize & (modesize - 1)) == 0)
67790075Sobrien	{
67890075Sobrien	  if (align < modesize)
67990075Sobrien	    align = modesize;
68018334Speter
68190075Sobrien	  str = TREE_STRING_POINTER (decl);
68290075Sobrien	  unit = GET_MODE_SIZE (mode);
68318334Speter
68490075Sobrien	  /* Check for embedded NUL characters.  */
68590075Sobrien	  for (i = 0; i < len; i += unit)
68690075Sobrien	    {
68790075Sobrien	      for (j = 0; j < unit; j++)
688117395Skan		if (str[i + j] != '\0')
68990075Sobrien		  break;
69090075Sobrien	      if (j == unit)
69190075Sobrien		break;
69290075Sobrien	    }
69390075Sobrien	  if (i == len - unit)
69490075Sobrien	    {
69590075Sobrien	      sprintf (name, ".rodata.str%d.%d", modesize / 8,
69690075Sobrien		       (int) (align / 8));
69790075Sobrien	      flags |= (modesize / 8) | SECTION_MERGE | SECTION_STRINGS;
698169689Skan	      return get_section (name, flags, NULL);
69990075Sobrien	    }
70090075Sobrien	}
70118334Speter    }
702132718Skan
703169689Skan  return readonly_data_section;
704117395Skan}
70590075Sobrien
706169689Skan/* Return the section to use for constant merging.  */
70790075Sobrien
708169689Skansection *
709132718Skanmergeable_constant_section (enum machine_mode mode ATTRIBUTE_UNUSED,
710132718Skan			    unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
711132718Skan			    unsigned int flags ATTRIBUTE_UNUSED)
71290075Sobrien{
71390075Sobrien  unsigned int modesize = GET_MODE_BITSIZE (mode);
71490075Sobrien
715132718Skan  if (HAVE_GAS_SHF_MERGE && flag_merge_constants
71690075Sobrien      && mode != VOIDmode
71790075Sobrien      && mode != BLKmode
71890075Sobrien      && modesize <= align
71990075Sobrien      && align >= 8
72090075Sobrien      && align <= 256
72190075Sobrien      && (align & (align - 1)) == 0)
72250397Sobrien    {
72390075Sobrien      char name[24];
72490075Sobrien
72590075Sobrien      sprintf (name, ".rodata.cst%d", (int) (align / 8));
72690075Sobrien      flags |= (align / 8) | SECTION_MERGE;
727169689Skan      return get_section (name, flags, NULL);
728117395Skan    }
729169689Skan  return readonly_data_section;
73018334Speter}
73190075Sobrien
73218334Speter/* Given NAME, a putative register name, discard any customary prefixes.  */
73318334Speter
73452284Sobrienstatic const char *
735132718Skanstrip_reg_name (const char *name)
73618334Speter{
73718334Speter#ifdef REGISTER_PREFIX
73818334Speter  if (!strncmp (name, REGISTER_PREFIX, strlen (REGISTER_PREFIX)))
73918334Speter    name += strlen (REGISTER_PREFIX);
74018334Speter#endif
74118334Speter  if (name[0] == '%' || name[0] == '#')
74218334Speter    name++;
74318334Speter  return name;
74418334Speter}
74518334Speter
746169689Skan/* The user has asked for a DECL to have a particular name.  Set (or
747169689Skan   change) it in such a way that we don't prefix an underscore to
748169689Skan   it.  */
749169689Skanvoid
750169689Skanset_user_assembler_name (tree decl, const char *name)
751169689Skan{
752169689Skan  char *starred = alloca (strlen (name) + 2);
753169689Skan  starred[0] = '*';
754169689Skan  strcpy (starred + 1, name);
755169689Skan  change_decl_assembler_name (decl, get_identifier (starred));
756169689Skan  SET_DECL_RTL (decl, NULL_RTX);
757169689Skan}
758169689Skan
75918334Speter/* Decode an `asm' spec for a declaration as a register name.
76018334Speter   Return the register number, or -1 if nothing specified,
76118334Speter   or -2 if the ASMSPEC is not `cc' or `memory' and is not recognized,
76218334Speter   or -3 if ASMSPEC is `cc' and is not recognized,
76318334Speter   or -4 if ASMSPEC is `memory' and is not recognized.
76418334Speter   Accept an exact spelling or a decimal number.
76518334Speter   Prefixes such as % are optional.  */
76618334Speter
76718334Speterint
768132718Skandecode_reg_name (const char *asmspec)
76918334Speter{
77018334Speter  if (asmspec != 0)
77118334Speter    {
77218334Speter      int i;
77318334Speter
77418334Speter      /* Get rid of confusing prefixes.  */
77518334Speter      asmspec = strip_reg_name (asmspec);
77690075Sobrien
77718334Speter      /* Allow a decimal number as a "register name".  */
77818334Speter      for (i = strlen (asmspec) - 1; i >= 0; i--)
77990075Sobrien	if (! ISDIGIT (asmspec[i]))
78018334Speter	  break;
78118334Speter      if (asmspec[0] != 0 && i < 0)
78218334Speter	{
78318334Speter	  i = atoi (asmspec);
78418334Speter	  if (i < FIRST_PSEUDO_REGISTER && i >= 0)
78518334Speter	    return i;
78618334Speter	  else
78718334Speter	    return -2;
78818334Speter	}
78918334Speter
79018334Speter      for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
79118334Speter	if (reg_names[i][0]
79218334Speter	    && ! strcmp (asmspec, strip_reg_name (reg_names[i])))
79318334Speter	  return i;
79418334Speter
79518334Speter#ifdef ADDITIONAL_REGISTER_NAMES
79618334Speter      {
79790075Sobrien	static const struct { const char *const name; const int number; } table[]
79818334Speter	  = ADDITIONAL_REGISTER_NAMES;
79918334Speter
80090075Sobrien	for (i = 0; i < (int) ARRAY_SIZE (table); i++)
801169689Skan	  if (table[i].name[0]
802169689Skan	      && ! strcmp (asmspec, table[i].name))
80318334Speter	    return table[i].number;
80418334Speter      }
80518334Speter#endif /* ADDITIONAL_REGISTER_NAMES */
80618334Speter
80718334Speter      if (!strcmp (asmspec, "memory"))
80818334Speter	return -4;
80918334Speter
81018334Speter      if (!strcmp (asmspec, "cc"))
81118334Speter	return -3;
81218334Speter
81318334Speter      return -2;
81418334Speter    }
81518334Speter
81618334Speter  return -1;
81718334Speter}
81818334Speter
819169689Skan/* Return true if DECL's initializer is suitable for a BSS section.  */
820169689Skan
821169689Skanstatic bool
822169689Skanbss_initializer_p (tree decl)
823169689Skan{
824169689Skan  return (DECL_INITIAL (decl) == NULL
825169689Skan	  || DECL_INITIAL (decl) == error_mark_node
826169689Skan	  || (flag_zero_initialized_in_bss
827169689Skan	      /* Leave constant zeroes in .rodata so they
828169689Skan		 can be shared.  */
829169689Skan	      && !TREE_READONLY (decl)
830169689Skan	      && initializer_zerop (DECL_INITIAL (decl))));
831169689Skan}
832169689Skan
833169689Skan/* Compute the alignment of variable specified by DECL.
834169689Skan   DONT_OUTPUT_DATA is from assemble_variable.  */
835169689Skan
836169689Skanvoid
837169689Skanalign_variable (tree decl, bool dont_output_data)
838169689Skan{
839169689Skan  unsigned int align = DECL_ALIGN (decl);
840169689Skan
841169689Skan  /* In the case for initialing an array whose length isn't specified,
842169689Skan     where we have not yet been able to do the layout,
843169689Skan     figure out the proper alignment now.  */
844169689Skan  if (dont_output_data && DECL_SIZE (decl) == 0
845169689Skan      && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
846169689Skan    align = MAX (align, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (decl))));
847169689Skan
848169689Skan  /* Some object file formats have a maximum alignment which they support.
849169689Skan     In particular, a.out format supports a maximum alignment of 4.  */
850169689Skan  if (align > MAX_OFILE_ALIGNMENT)
851169689Skan    {
852169689Skan      warning (0, "alignment of %q+D is greater than maximum object "
853169689Skan               "file alignment.  Using %d", decl,
854169689Skan	       MAX_OFILE_ALIGNMENT/BITS_PER_UNIT);
855169689Skan      align = MAX_OFILE_ALIGNMENT;
856169689Skan    }
857169689Skan
858169689Skan  /* On some machines, it is good to increase alignment sometimes.  */
859169689Skan  if (! DECL_USER_ALIGN (decl))
860169689Skan    {
861169689Skan#ifdef DATA_ALIGNMENT
862169689Skan      align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
863169689Skan#endif
864169689Skan#ifdef CONSTANT_ALIGNMENT
865169689Skan      if (DECL_INITIAL (decl) != 0 && DECL_INITIAL (decl) != error_mark_node)
866169689Skan	align = CONSTANT_ALIGNMENT (DECL_INITIAL (decl), align);
867169689Skan#endif
868169689Skan    }
869169689Skan
870169689Skan  /* Reset the alignment in case we have made it tighter, so we can benefit
871169689Skan     from it in get_pointer_alignment.  */
872169689Skan  DECL_ALIGN (decl) = align;
873169689Skan}
874169689Skan
875169689Skan/* Return the section into which the given VAR_DECL or CONST_DECL
876169689Skan   should be placed.  PREFER_NOSWITCH_P is true if a noswitch
877169689Skan   section should be used wherever possible.  */
878169689Skan
879169689Skanstatic section *
880169689Skanget_variable_section (tree decl, bool prefer_noswitch_p)
881169689Skan{
882169689Skan  int reloc;
883169689Skan
884169689Skan  /* If the decl has been given an explicit section name, then it
885169689Skan     isn't common, and shouldn't be handled as such.  */
886169689Skan  if (DECL_COMMON (decl) && DECL_SECTION_NAME (decl) == NULL)
887169689Skan    {
888169689Skan      if (DECL_THREAD_LOCAL_P (decl))
889169689Skan	return tls_comm_section;
890169689Skan      if (TREE_PUBLIC (decl) && bss_initializer_p (decl))
891169689Skan	return comm_section;
892169689Skan    }
893169689Skan
894169689Skan  if (DECL_INITIAL (decl) == error_mark_node)
895169689Skan    reloc = contains_pointers_p (TREE_TYPE (decl)) ? 3 : 0;
896169689Skan  else if (DECL_INITIAL (decl))
897169689Skan    reloc = compute_reloc_for_constant (DECL_INITIAL (decl));
898169689Skan  else
899169689Skan    reloc = 0;
900169689Skan
901169689Skan  resolve_unique_section (decl, reloc, flag_data_sections);
902169689Skan  if (IN_NAMED_SECTION (decl))
903169689Skan    return get_named_section (decl, NULL, reloc);
904169689Skan
905169689Skan  if (!DECL_THREAD_LOCAL_P (decl)
906169689Skan      && !(prefer_noswitch_p && targetm.have_switchable_bss_sections)
907169689Skan      && bss_initializer_p (decl))
908169689Skan    {
909169689Skan      if (!TREE_PUBLIC (decl))
910169689Skan	return lcomm_section;
911169689Skan      if (bss_noswitch_section)
912169689Skan	return bss_noswitch_section;
913169689Skan    }
914169689Skan
915169689Skan  return targetm.asm_out.select_section (decl, reloc, DECL_ALIGN (decl));
916169689Skan}
917169689Skan
918169689Skan/* Return the block into which object_block DECL should be placed.  */
919169689Skan
920169689Skanstatic struct object_block *
921169689Skanget_block_for_decl (tree decl)
922169689Skan{
923169689Skan  section *sect;
924169689Skan
925169689Skan  if (TREE_CODE (decl) == VAR_DECL)
926169689Skan    {
927169689Skan      /* The object must be defined in this translation unit.  */
928169689Skan      if (DECL_EXTERNAL (decl))
929169689Skan	return NULL;
930169689Skan
931169689Skan      /* There's no point using object blocks for something that is
932169689Skan	 isolated by definition.  */
933169689Skan      if (DECL_ONE_ONLY (decl))
934169689Skan	return NULL;
935169689Skan    }
936169689Skan
937169689Skan  /* We can only calculate block offsets if the decl has a known
938169689Skan     constant size.  */
939169689Skan  if (DECL_SIZE_UNIT (decl) == NULL)
940169689Skan    return NULL;
941169689Skan  if (!host_integerp (DECL_SIZE_UNIT (decl), 1))
942169689Skan    return NULL;
943169689Skan
944169689Skan  /* Find out which section should contain DECL.  We cannot put it into
945169689Skan     an object block if it requires a standalone definition.  */
946169689Skan  if (TREE_CODE (decl) == VAR_DECL)
947169689Skan      align_variable (decl, 0);
948169689Skan  sect = get_variable_section (decl, true);
949169689Skan  if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
950169689Skan    return NULL;
951169689Skan
952169689Skan  return get_block_for_section (sect);
953169689Skan}
954169689Skan
955169689Skan/* Make sure block symbol SYMBOL is in block BLOCK.  */
956169689Skan
957169689Skanstatic void
958169689Skanchange_symbol_block (rtx symbol, struct object_block *block)
959169689Skan{
960169689Skan  if (block != SYMBOL_REF_BLOCK (symbol))
961169689Skan    {
962169689Skan      gcc_assert (SYMBOL_REF_BLOCK_OFFSET (symbol) < 0);
963169689Skan      SYMBOL_REF_BLOCK (symbol) = block;
964169689Skan    }
965169689Skan}
966169689Skan
967169689Skan/* Return true if it is possible to put DECL in an object_block.  */
968169689Skan
969169689Skanstatic bool
970169689Skanuse_blocks_for_decl_p (tree decl)
971169689Skan{
972169689Skan  /* Only data DECLs can be placed into object blocks.  */
973169689Skan  if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != CONST_DECL)
974169689Skan    return false;
975169689Skan
976169689Skan  /* Detect decls created by dw2_force_const_mem.  Such decls are
977169689Skan     special because DECL_INITIAL doesn't specify the decl's true value.
978169689Skan     dw2_output_indirect_constants will instead call assemble_variable
979169689Skan     with dont_output_data set to 1 and then print the contents itself.  */
980169689Skan  if (DECL_INITIAL (decl) == decl)
981169689Skan    return false;
982169689Skan
983169689Skan  /* If this decl is an alias, then we don't want to emit a definition.  */
984169689Skan  if (lookup_attribute ("alias", DECL_ATTRIBUTES (decl)))
985169689Skan    return false;
986169689Skan
987169689Skan  return true;
988169689Skan}
989169689Skan
99090075Sobrien/* Create the DECL_RTL for a VAR_DECL or FUNCTION_DECL.  DECL should
99190075Sobrien   have static storage duration.  In other words, it should not be an
99290075Sobrien   automatic variable, including PARM_DECLs.
99318334Speter
99490075Sobrien   There is, however, one exception: this function handles variables
99590075Sobrien   explicitly placed in a particular register by the user.
99690075Sobrien
99718334Speter   This is never called for PARM_DECL nodes.  */
99818334Speter
99918334Spetervoid
1000169689Skanmake_decl_rtl (tree decl)
100118334Speter{
100290075Sobrien  const char *name = 0;
100318334Speter  int reg_number;
100490075Sobrien  rtx x;
100518334Speter
100690075Sobrien  /* Check that we are not being given an automatic variable.  */
1007169689Skan  gcc_assert (TREE_CODE (decl) != PARM_DECL
1008169689Skan	      && TREE_CODE (decl) != RESULT_DECL);
1009169689Skan
101090075Sobrien  /* A weak alias has TREE_PUBLIC set but not the other bits.  */
1011169689Skan  gcc_assert (TREE_CODE (decl) != VAR_DECL
1012169689Skan	      || TREE_STATIC (decl)
1013169689Skan	      || TREE_PUBLIC (decl)
1014169689Skan	      || DECL_EXTERNAL (decl)
1015169689Skan	      || DECL_REGISTER (decl));
1016169689Skan
101790075Sobrien  /* And that we were not given a type or a label.  */
1018169689Skan  gcc_assert (TREE_CODE (decl) != TYPE_DECL
1019169689Skan	      && TREE_CODE (decl) != LABEL_DECL);
102018334Speter
102190075Sobrien  /* For a duplicate declaration, we can be called twice on the
102290075Sobrien     same DECL node.  Don't discard the RTL already made.  */
102390075Sobrien  if (DECL_RTL_SET_P (decl))
102490075Sobrien    {
102590075Sobrien      /* If the old RTL had the wrong mode, fix the mode.  */
1026169689Skan      x = DECL_RTL (decl);
1027169689Skan      if (GET_MODE (x) != DECL_MODE (decl))
1028169689Skan	SET_DECL_RTL (decl, adjust_address_nv (x, DECL_MODE (decl), 0));
102918334Speter
1030169689Skan      if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1031169689Skan	return;
1032169689Skan
1033117395Skan      /* ??? Another way to do this would be to maintain a hashed
1034117395Skan	 table of such critters.  Instead of adding stuff to a DECL
1035117395Skan	 to give certain attributes to it, we could use an external
1036117395Skan	 hash map from DECL to set of attributes.  */
1037117395Skan
103890075Sobrien      /* Let the target reassign the RTL if it wants.
103990075Sobrien	 This is necessary, for example, when one machine specific
104090075Sobrien	 decl attribute overrides another.  */
1041169689Skan      targetm.encode_section_info (decl, DECL_RTL (decl), false);
1042169689Skan
1043169689Skan      /* If the symbol has a SYMBOL_REF_BLOCK field, update it based
1044169689Skan	 on the new decl information.  */
1045169689Skan      if (MEM_P (x)
1046169689Skan	  && GET_CODE (XEXP (x, 0)) == SYMBOL_REF
1047169689Skan	  && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (x, 0)))
1048169689Skan	change_symbol_block (XEXP (x, 0), get_block_for_decl (decl));
1049169689Skan
1050169689Skan      /* Make this function static known to the mudflap runtime.  */
1051169689Skan      if (flag_mudflap && TREE_CODE (decl) == VAR_DECL)
1052169689Skan	mudflap_enqueue_decl (decl);
1053169689Skan
105490075Sobrien      return;
105590075Sobrien    }
105690075Sobrien
1057132718Skan  name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
1058132718Skan
1059169689Skan  if (name[0] != '*' && TREE_CODE (decl) != FUNCTION_DECL
1060169689Skan      && DECL_REGISTER (decl))
106118334Speter    {
1062169689Skan      error ("register name not specified for %q+D", decl);
1063169689Skan    }
1064169689Skan  else if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1065169689Skan    {
1066169689Skan      const char *asmspec = name+1;
1067169689Skan      reg_number = decode_reg_name (asmspec);
106818334Speter      /* First detect errors in declaring global registers.  */
106990075Sobrien      if (reg_number == -1)
1070169689Skan	error ("register name not specified for %q+D", decl);
107190075Sobrien      else if (reg_number < 0)
1072169689Skan	error ("invalid register name for %q+D", decl);
107390075Sobrien      else if (TYPE_MODE (TREE_TYPE (decl)) == BLKmode)
1074169689Skan	error ("data type of %q+D isn%'t suitable for a register",
1075169689Skan	       decl);
107690075Sobrien      else if (! HARD_REGNO_MODE_OK (reg_number, TYPE_MODE (TREE_TYPE (decl))))
1077169689Skan	error ("register specified for %q+D isn%'t suitable for data type",
1078169689Skan               decl);
107918334Speter      /* Now handle properly declared static register variables.  */
108090075Sobrien      else
108118334Speter	{
108218334Speter	  int nregs;
108350397Sobrien
108490075Sobrien	  if (DECL_INITIAL (decl) != 0 && TREE_STATIC (decl))
108518334Speter	    {
108618334Speter	      DECL_INITIAL (decl) = 0;
108718334Speter	      error ("global register variable has initial value");
108818334Speter	    }
108918334Speter	  if (TREE_THIS_VOLATILE (decl))
1090169689Skan	    warning (OPT_Wvolatile_register_var,
1091169689Skan		     "optimization may eliminate reads and/or "
1092169689Skan		     "writes to register variables");
109318334Speter
109418334Speter	  /* If the user specified one of the eliminables registers here,
109518334Speter	     e.g., FRAME_POINTER_REGNUM, we don't want to get this variable
109690075Sobrien	     confused with that register and be eliminated.  This usage is
109790075Sobrien	     somewhat suspect...  */
109818334Speter
109990075Sobrien	  SET_DECL_RTL (decl, gen_rtx_raw_REG (DECL_MODE (decl), reg_number));
110090075Sobrien	  ORIGINAL_REGNO (DECL_RTL (decl)) = reg_number;
110118334Speter	  REG_USERVAR_P (DECL_RTL (decl)) = 1;
110218334Speter
110390075Sobrien	  if (TREE_STATIC (decl))
110418334Speter	    {
110518334Speter	      /* Make this register global, so not usable for anything
110618334Speter		 else.  */
110790075Sobrien#ifdef ASM_DECLARE_REGISTER_GLOBAL
1108169689Skan	      name = IDENTIFIER_POINTER (DECL_NAME (decl));
110990075Sobrien	      ASM_DECLARE_REGISTER_GLOBAL (asm_out_file, decl, reg_number, name);
111090075Sobrien#endif
1111169689Skan	      nregs = hard_regno_nregs[reg_number][DECL_MODE (decl)];
111218334Speter	      while (nregs > 0)
111318334Speter		globalize_reg (reg_number + --nregs);
111418334Speter	    }
111590075Sobrien
111690075Sobrien	  /* As a register variable, it has no section.  */
111790075Sobrien	  return;
111818334Speter	}
111990075Sobrien    }
112090075Sobrien  /* Now handle ordinary static variables and functions (in memory).
112190075Sobrien     Also handle vars declared register invalidly.  */
1122169689Skan  else if (name[0] == '*')
1123169689Skan  {
1124169689Skan#ifdef REGISTER_PREFIX
1125169689Skan    if (strlen (REGISTER_PREFIX) != 0)
1126169689Skan      {
1127169689Skan	reg_number = decode_reg_name (name);
1128169689Skan	if (reg_number >= 0 || reg_number == -3)
1129169689Skan	  error ("register name given for non-register variable %q+D", decl);
1130169689Skan      }
1131169689Skan#endif
1132169689Skan  }
113318334Speter
113490075Sobrien  /* Specifying a section attribute on a variable forces it into a
113590075Sobrien     non-.bss section, and thus it cannot be common.  */
113690075Sobrien  if (TREE_CODE (decl) == VAR_DECL
113790075Sobrien      && DECL_SECTION_NAME (decl) != NULL_TREE
113890075Sobrien      && DECL_INITIAL (decl) == NULL_TREE
113990075Sobrien      && DECL_COMMON (decl))
114090075Sobrien    DECL_COMMON (decl) = 0;
114118334Speter
1142117395Skan  /* Variables can't be both common and weak.  */
1143117395Skan  if (TREE_CODE (decl) == VAR_DECL && DECL_WEAK (decl))
1144117395Skan    DECL_COMMON (decl) = 0;
1145117395Skan
1146169689Skan  if (use_object_blocks_p () && use_blocks_for_decl_p (decl))
1147169689Skan    x = create_block_symbol (name, get_block_for_decl (decl), -1);
1148169689Skan  else
1149169689Skan    x = gen_rtx_SYMBOL_REF (Pmode, name);
1150132718Skan  SYMBOL_REF_WEAK (x) = DECL_WEAK (decl);
1151169689Skan  SET_SYMBOL_REF_DECL (x, decl);
115218334Speter
1153132718Skan  x = gen_rtx_MEM (DECL_MODE (decl), x);
115490075Sobrien  if (TREE_CODE (decl) != FUNCTION_DECL)
115590075Sobrien    set_mem_attributes (x, decl, 1);
115690075Sobrien  SET_DECL_RTL (decl, x);
115790075Sobrien
115890075Sobrien  /* Optionally set flags or add text to the name to record information
115990075Sobrien     such as that it is a function name.
116090075Sobrien     If the name is changed, the macro ASM_OUTPUT_LABELREF
116190075Sobrien     will have to know how to strip this information.  */
1162169689Skan  targetm.encode_section_info (decl, DECL_RTL (decl), true);
116318334Speter
1164169689Skan  /* Make this function static known to the mudflap runtime.  */
1165169689Skan  if (flag_mudflap && TREE_CODE (decl) == VAR_DECL)
1166169689Skan    mudflap_enqueue_decl (decl);
116718334Speter}
116818334Speter
116918334Speter/* Output a string of literal assembler code
117018334Speter   for an `asm' keyword used between functions.  */
117118334Speter
117218334Spetervoid
1173132718Skanassemble_asm (tree string)
117418334Speter{
117518334Speter  app_enable ();
117618334Speter
117718334Speter  if (TREE_CODE (string) == ADDR_EXPR)
117818334Speter    string = TREE_OPERAND (string, 0);
117918334Speter
118018334Speter  fprintf (asm_out_file, "\t%s\n", TREE_STRING_POINTER (string));
118118334Speter}
118218334Speter
118390075Sobrien/* Record an element in the table of global destructors.  SYMBOL is
118490075Sobrien   a SYMBOL_REF of the function to be called; PRIORITY is a number
118590075Sobrien   between 0 and MAX_INIT_PRIORITY.  */
118618334Speter
118790075Sobrienvoid
1188169689Skandefault_stabs_asm_out_destructor (rtx symbol ATTRIBUTE_UNUSED,
1189169689Skan				  int priority ATTRIBUTE_UNUSED)
119090075Sobrien{
1191169689Skan#if defined DBX_DEBUGGING_INFO || defined XCOFF_DEBUGGING_INFO
119290075Sobrien  /* Tell GNU LD that this is part of the static destructor set.
119390075Sobrien     This will work for any system that uses stabs, most usefully
119490075Sobrien     aout systems.  */
1195169689Skan  dbxout_begin_simple_stabs ("___DTOR_LIST__", 22 /* N_SETT */);
1196169689Skan  dbxout_stab_value_label (XSTR (symbol, 0));
1197169689Skan#else
1198169689Skan  sorry ("global destructors not supported on this target");
1199169689Skan#endif
120090075Sobrien}
120118334Speter
120290075Sobrienvoid
1203132718Skandefault_named_section_asm_out_destructor (rtx symbol, int priority)
120490075Sobrien{
120590075Sobrien  const char *section = ".dtors";
120690075Sobrien  char buf[16];
120718334Speter
120890075Sobrien  /* ??? This only works reliably with the GNU linker.  */
120990075Sobrien  if (priority != DEFAULT_INIT_PRIORITY)
121090075Sobrien    {
121190075Sobrien      sprintf (buf, ".dtors.%.5u",
121290075Sobrien	       /* Invert the numbering so the linker puts us in the proper
121390075Sobrien		  order; constructors are run from right to left, and the
121490075Sobrien		  linker sorts in increasing order.  */
121590075Sobrien	       MAX_INIT_PRIORITY - priority);
121690075Sobrien      section = buf;
121790075Sobrien    }
121890075Sobrien
1219169689Skan  switch_to_section (get_section (section, SECTION_WRITE, NULL));
122090075Sobrien  assemble_align (POINTER_SIZE);
122190075Sobrien  assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
122290075Sobrien}
122390075Sobrien
122490075Sobrien#ifdef DTORS_SECTION_ASM_OP
122518334Spetervoid
1226132718Skandefault_dtor_section_asm_out_destructor (rtx symbol,
1227132718Skan					 int priority ATTRIBUTE_UNUSED)
122890075Sobrien{
1229169689Skan  switch_to_section (dtors_section);
123090075Sobrien  assemble_align (POINTER_SIZE);
123190075Sobrien  assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
123290075Sobrien}
123390075Sobrien#endif
123490075Sobrien
123518334Speter/* Likewise for global constructors.  */
123618334Speter
123718334Spetervoid
1238169689Skandefault_stabs_asm_out_constructor (rtx symbol ATTRIBUTE_UNUSED,
1239169689Skan				   int priority ATTRIBUTE_UNUSED)
124018334Speter{
1241169689Skan#if defined DBX_DEBUGGING_INFO || defined XCOFF_DEBUGGING_INFO
124290075Sobrien  /* Tell GNU LD that this is part of the static destructor set.
124390075Sobrien     This will work for any system that uses stabs, most usefully
124490075Sobrien     aout systems.  */
1245169689Skan  dbxout_begin_simple_stabs ("___CTOR_LIST__", 22 /* N_SETT */);
1246169689Skan  dbxout_stab_value_label (XSTR (symbol, 0));
1247169689Skan#else
1248169689Skan  sorry ("global constructors not supported on this target");
1249169689Skan#endif
125090075Sobrien}
125190075Sobrien
125290075Sobrienvoid
1253132718Skandefault_named_section_asm_out_constructor (rtx symbol, int priority)
125490075Sobrien{
125590075Sobrien  const char *section = ".ctors";
125690075Sobrien  char buf[16];
125790075Sobrien
125890075Sobrien  /* ??? This only works reliably with the GNU linker.  */
125990075Sobrien  if (priority != DEFAULT_INIT_PRIORITY)
126018334Speter    {
126190075Sobrien      sprintf (buf, ".ctors.%.5u",
126290075Sobrien	       /* Invert the numbering so the linker puts us in the proper
126390075Sobrien		  order; constructors are run from right to left, and the
126490075Sobrien		  linker sorts in increasing order.  */
126590075Sobrien	       MAX_INIT_PRIORITY - priority);
126690075Sobrien      section = buf;
126718334Speter    }
126890075Sobrien
1269169689Skan  switch_to_section (get_section (section, SECTION_WRITE, NULL));
127090075Sobrien  assemble_align (POINTER_SIZE);
127190075Sobrien  assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
127218334Speter}
127318334Speter
127490075Sobrien#ifdef CTORS_SECTION_ASM_OP
127518334Spetervoid
1276132718Skandefault_ctor_section_asm_out_constructor (rtx symbol,
1277132718Skan					  int priority ATTRIBUTE_UNUSED)
127890075Sobrien{
1279169689Skan  switch_to_section (ctors_section);
128090075Sobrien  assemble_align (POINTER_SIZE);
128190075Sobrien  assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
128290075Sobrien}
128318334Speter#endif
128418334Speter
128550397Sobrien/* CONSTANT_POOL_BEFORE_FUNCTION may be defined as an expression with
1286117395Skan   a nonzero value if the constant pool should be output before the
128750397Sobrien   start of the function, or a zero value if the pool should output
128850397Sobrien   after the end of the function.  The default is to put it before the
128950397Sobrien   start.  */
129050397Sobrien
129150397Sobrien#ifndef CONSTANT_POOL_BEFORE_FUNCTION
129250397Sobrien#define CONSTANT_POOL_BEFORE_FUNCTION 1
129350397Sobrien#endif
129450397Sobrien
1295132718Skan/* DECL is an object (either VAR_DECL or FUNCTION_DECL) which is going
1296132718Skan   to be output to assembler.
1297132718Skan   Set first_global_object_name and weak_global_object_name as appropriate.  */
1298132718Skan
1299132718Skanvoid
1300132718Skannotice_global_symbol (tree decl)
1301132718Skan{
1302132718Skan  const char **type = &first_global_object_name;
1303132718Skan
1304132718Skan  if (first_global_object_name
1305169689Skan      || !TREE_PUBLIC (decl)
1306169689Skan      || DECL_EXTERNAL (decl)
1307132718Skan      || !DECL_NAME (decl)
1308132718Skan      || (TREE_CODE (decl) != FUNCTION_DECL
1309132718Skan	  && (TREE_CODE (decl) != VAR_DECL
1310132718Skan	      || (DECL_COMMON (decl)
1311132718Skan		  && (DECL_INITIAL (decl) == 0
1312132718Skan		      || DECL_INITIAL (decl) == error_mark_node))))
1313169689Skan      || !MEM_P (DECL_RTL (decl)))
1314132718Skan    return;
1315132718Skan
1316169689Skan  /* We win when global object is found, but it is useful to know about weak
1317132718Skan     symbol as well so we can produce nicer unique names.  */
1318132718Skan  if (DECL_WEAK (decl) || DECL_ONE_ONLY (decl))
1319132718Skan    type = &weak_global_object_name;
1320132718Skan
1321132718Skan  if (!*type)
1322132718Skan    {
1323132718Skan      const char *p;
1324169689Skan      const char *name;
1325132718Skan      rtx decl_rtl = DECL_RTL (decl);
1326132718Skan
1327169689Skan      p = targetm.strip_name_encoding (XSTR (XEXP (decl_rtl, 0), 0));
1328169689Skan      name = ggc_strdup (p);
1329132718Skan
1330132718Skan      *type = name;
1331132718Skan    }
1332132718Skan}
1333132718Skan
133418334Speter/* Output assembler code for the constant pool of a function and associated
133518334Speter   with defining the name of the function.  DECL describes the function.
133618334Speter   NAME is the function's name.  For the constant pool, we use the current
133718334Speter   constant pool data.  */
133818334Speter
133918334Spetervoid
1340132718Skanassemble_start_function (tree decl, const char *fnname)
134118334Speter{
134218334Speter  int align;
1343169689Skan  char tmp_label[100];
1344169689Skan  bool hot_label_written = false;
134518334Speter
1346169689Skan  cfun->unlikely_text_section_name = NULL;
1347169689Skan
1348169689Skan  first_function_block_is_cold = false;
1349169689Skan  if (flag_reorder_blocks_and_partition)
1350169689Skan    {
1351169689Skan      ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTB", const_labelno);
1352169689Skan      cfun->hot_section_label = ggc_strdup (tmp_label);
1353169689Skan      ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDB", const_labelno);
1354169689Skan      cfun->cold_section_label = ggc_strdup (tmp_label);
1355169689Skan      ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTE", const_labelno);
1356169689Skan      cfun->hot_section_end_label = ggc_strdup (tmp_label);
1357169689Skan      ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDE", const_labelno);
1358169689Skan      cfun->cold_section_end_label = ggc_strdup (tmp_label);
1359169689Skan      const_labelno++;
1360169689Skan    }
1361169689Skan  else
1362169689Skan    {
1363169689Skan      cfun->hot_section_label = NULL;
1364169689Skan      cfun->cold_section_label = NULL;
1365169689Skan      cfun->hot_section_end_label = NULL;
1366169689Skan      cfun->cold_section_end_label = NULL;
1367169689Skan    }
1368169689Skan
136918334Speter  /* The following code does not need preprocessing in the assembler.  */
137018334Speter
137118334Speter  app_disable ();
137218334Speter
137350397Sobrien  if (CONSTANT_POOL_BEFORE_FUNCTION)
137450397Sobrien    output_constant_pool (fnname, decl);
137518334Speter
1376102780Skan  resolve_unique_section (decl, 0, flag_function_sections);
137718334Speter
1378169689Skan  /* Make sure the not and cold text (code) sections are properly
1379169689Skan     aligned.  This is necessary here in the case where the function
1380169689Skan     has both hot and cold sections, because we don't want to re-set
1381169689Skan     the alignment when the section switch happens mid-function.  */
1382169689Skan
1383169689Skan  if (flag_reorder_blocks_and_partition)
1384169689Skan    {
1385169689Skan      switch_to_section (unlikely_text_section ());
1386169689Skan      assemble_align (FUNCTION_BOUNDARY);
1387169689Skan      ASM_OUTPUT_LABEL (asm_out_file, cfun->cold_section_label);
1388169689Skan
1389169689Skan      /* When the function starts with a cold section, we need to explicitly
1390169689Skan	 align the hot section and write out the hot section label.
1391169689Skan	 But if the current function is a thunk, we do not have a CFG.  */
1392169689Skan      if (!current_function_is_thunk
1393169689Skan	  && BB_PARTITION (ENTRY_BLOCK_PTR->next_bb) == BB_COLD_PARTITION)
1394169689Skan	{
1395169689Skan	  switch_to_section (text_section);
1396169689Skan	  assemble_align (FUNCTION_BOUNDARY);
1397169689Skan	  ASM_OUTPUT_LABEL (asm_out_file, cfun->hot_section_label);
1398169689Skan	  hot_label_written = true;
1399169689Skan	  first_function_block_is_cold = true;
1400169689Skan	}
1401169689Skan    }
1402169689Skan  else if (DECL_SECTION_NAME (decl))
1403169689Skan    {
1404169689Skan      /* Calls to function_section rely on first_function_block_is_cold
1405169689Skan	 being accurate.  The first block may be cold even if we aren't
1406169689Skan	 doing partitioning, if the entire function was decided by
1407169689Skan	 choose_function_section (predict.c) to be cold.  */
1408169689Skan
1409169689Skan      initialize_cold_section_name ();
1410169689Skan
1411169689Skan      if (cfun->unlikely_text_section_name
1412169689Skan	  && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
1413169689Skan		     cfun->unlikely_text_section_name) == 0)
1414169689Skan	first_function_block_is_cold = true;
1415169689Skan    }
1416169689Skan
1417169689Skan  in_cold_section_p = first_function_block_is_cold;
1418169689Skan
1419169689Skan  /* Switch to the correct text section for the start of the function.  */
1420169689Skan
1421169689Skan  switch_to_section (function_section (decl));
1422169689Skan  if (flag_reorder_blocks_and_partition
1423169689Skan      && !hot_label_written)
1424169689Skan    ASM_OUTPUT_LABEL (asm_out_file, cfun->hot_section_label);
1425169689Skan
142618334Speter  /* Tell assembler to move to target machine's alignment for functions.  */
142718334Speter  align = floor_log2 (FUNCTION_BOUNDARY / BITS_PER_UNIT);
1428104752Skan  if (align < force_align_functions_log)
1429104752Skan    align = force_align_functions_log;
143018334Speter  if (align > 0)
143190075Sobrien    {
143290075Sobrien      ASM_OUTPUT_ALIGN (asm_out_file, align);
143390075Sobrien    }
143418334Speter
143590075Sobrien  /* Handle a user-specified function alignment.
143690075Sobrien     Note that we still need to align to FUNCTION_BOUNDARY, as above,
143790075Sobrien     because ASM_OUTPUT_MAX_SKIP_ALIGN might not do any alignment at all.  */
1438117395Skan  if (align_functions_log > align
1439117395Skan      && cfun->function_frequency != FUNCTION_FREQUENCY_UNLIKELY_EXECUTED)
144090075Sobrien    {
144190075Sobrien#ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
144290075Sobrien      ASM_OUTPUT_MAX_SKIP_ALIGN (asm_out_file,
1443117395Skan				 align_functions_log, align_functions - 1);
144490075Sobrien#else
144590075Sobrien      ASM_OUTPUT_ALIGN (asm_out_file, align_functions_log);
144690075Sobrien#endif
144790075Sobrien    }
144890075Sobrien
144918334Speter#ifdef ASM_OUTPUT_FUNCTION_PREFIX
145018334Speter  ASM_OUTPUT_FUNCTION_PREFIX (asm_out_file, fnname);
145118334Speter#endif
145218334Speter
145390075Sobrien  (*debug_hooks->begin_function) (decl);
145418334Speter
145518334Speter  /* Make function name accessible from other files, if appropriate.  */
145618334Speter
145718334Speter  if (TREE_PUBLIC (decl))
145818334Speter    {
1459132718Skan      notice_global_symbol (decl);
146018334Speter
146196263Sobrien      globalize_decl (decl);
1462117395Skan
1463117395Skan      maybe_assemble_visibility (decl);
146418334Speter    }
146518334Speter
1466169689Skan  if (DECL_PRESERVE_P (decl))
1467169689Skan    targetm.asm_out.mark_decl_preserved (fnname);
1468169689Skan
1469132718Skan  /* Do any machine/system dependent processing of the function name.  */
147018334Speter#ifdef ASM_DECLARE_FUNCTION_NAME
147118334Speter  ASM_DECLARE_FUNCTION_NAME (asm_out_file, fnname, current_function_decl);
147218334Speter#else
147318334Speter  /* Standard thing is just output label for the function.  */
147450397Sobrien  ASM_OUTPUT_LABEL (asm_out_file, fnname);
147518334Speter#endif /* ASM_DECLARE_FUNCTION_NAME */
147618334Speter}
147718334Speter
147818334Speter/* Output assembler code associated with defining the size of the
147918334Speter   function.  DECL describes the function.  NAME is the function's name.  */
148018334Speter
148118334Spetervoid
1482169689Skanassemble_end_function (tree decl, const char *fnname ATTRIBUTE_UNUSED)
148318334Speter{
148418334Speter#ifdef ASM_DECLARE_FUNCTION_SIZE
1485169689Skan  /* We could have switched section in the middle of the function.  */
1486169689Skan  if (flag_reorder_blocks_and_partition)
1487169689Skan    switch_to_section (function_section (decl));
148818334Speter  ASM_DECLARE_FUNCTION_SIZE (asm_out_file, fnname, decl);
148918334Speter#endif
149050397Sobrien  if (! CONSTANT_POOL_BEFORE_FUNCTION)
149150397Sobrien    {
149250397Sobrien      output_constant_pool (fnname, decl);
1493169689Skan      switch_to_section (function_section (decl)); /* need to switch back */
149450397Sobrien    }
1495169689Skan  /* Output labels for end of hot/cold text sections (to be used by
1496169689Skan     debug info.)  */
1497169689Skan  if (flag_reorder_blocks_and_partition)
1498169689Skan    {
1499169689Skan      section *save_text_section;
1500169689Skan
1501169689Skan      save_text_section = in_section;
1502169689Skan      switch_to_section (unlikely_text_section ());
1503169689Skan      ASM_OUTPUT_LABEL (asm_out_file, cfun->cold_section_end_label);
1504169689Skan      if (first_function_block_is_cold)
1505169689Skan	switch_to_section (text_section);
1506169689Skan      else
1507169689Skan	switch_to_section (function_section (decl));
1508169689Skan      ASM_OUTPUT_LABEL (asm_out_file, cfun->hot_section_end_label);
1509169689Skan      switch_to_section (save_text_section);
1510169689Skan    }
151118334Speter}
151218334Speter
151318334Speter/* Assemble code to leave SIZE bytes of zeros.  */
151418334Speter
151518334Spetervoid
1516132718Skanassemble_zeros (unsigned HOST_WIDE_INT size)
151718334Speter{
151852284Sobrien  /* Do no output if -fsyntax-only.  */
151952284Sobrien  if (flag_syntax_only)
152052284Sobrien    return;
152152284Sobrien
152218334Speter#ifdef ASM_NO_SKIP_IN_TEXT
152318334Speter  /* The `space' pseudo in the text section outputs nop insns rather than 0s,
152418334Speter     so we must output 0s explicitly in the text section.  */
1525169689Skan  if (ASM_NO_SKIP_IN_TEXT && (in_section->common.flags & SECTION_CODE) != 0)
152618334Speter    {
1527132718Skan      unsigned HOST_WIDE_INT i;
152890075Sobrien      for (i = 0; i < size; i++)
152990075Sobrien	assemble_integer (const0_rtx, 1, BITS_PER_UNIT, 1);
153018334Speter    }
153118334Speter  else
153218334Speter#endif
153318334Speter    if (size > 0)
153450397Sobrien      ASM_OUTPUT_SKIP (asm_out_file, size);
153518334Speter}
153618334Speter
153718334Speter/* Assemble an alignment pseudo op for an ALIGN-bit boundary.  */
153818334Speter
153918334Spetervoid
1540132718Skanassemble_align (int align)
154118334Speter{
154218334Speter  if (align > BITS_PER_UNIT)
154390075Sobrien    {
154490075Sobrien      ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
154590075Sobrien    }
154618334Speter}
154718334Speter
154818334Speter/* Assemble a string constant with the specified C string as contents.  */
154918334Speter
155018334Spetervoid
1551132718Skanassemble_string (const char *p, int size)
155218334Speter{
155318334Speter  int pos = 0;
155418334Speter  int maximum = 2000;
155518334Speter
155618334Speter  /* If the string is very long, split it up.  */
155718334Speter
155818334Speter  while (pos < size)
155918334Speter    {
156018334Speter      int thissize = size - pos;
156118334Speter      if (thissize > maximum)
156218334Speter	thissize = maximum;
156318334Speter
156450397Sobrien      ASM_OUTPUT_ASCII (asm_out_file, p, thissize);
156518334Speter
156618334Speter      pos += thissize;
156718334Speter      p += thissize;
156818334Speter    }
156918334Speter}
157018334Speter
157118334Speter
1572169689Skan/* A noswitch_section_callback for lcomm_section.  */
1573169689Skan
1574169689Skanstatic bool
1575169689Skanemit_local (tree decl ATTRIBUTE_UNUSED,
1576169689Skan	    const char *name ATTRIBUTE_UNUSED,
1577169689Skan	    unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1578169689Skan	    unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1579169689Skan{
1580169689Skan#if defined ASM_OUTPUT_ALIGNED_DECL_LOCAL
1581169689Skan  ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, decl, name,
1582169689Skan				 size, DECL_ALIGN (decl));
1583169689Skan  return true;
1584169689Skan#elif defined ASM_OUTPUT_ALIGNED_LOCAL
1585169689Skan  ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, DECL_ALIGN (decl));
1586169689Skan  return true;
158790075Sobrien#else
1588169689Skan  ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
1589169689Skan  return false;
159090075Sobrien#endif
1591169689Skan}
159290075Sobrien
1593169689Skan/* A noswitch_section_callback for bss_noswitch_section.  */
1594169689Skan
1595169689Skan#if defined ASM_OUTPUT_ALIGNED_BSS || defined ASM_OUTPUT_BSS
1596169689Skanstatic bool
1597169689Skanemit_bss (tree decl ATTRIBUTE_UNUSED,
1598169689Skan	  const char *name ATTRIBUTE_UNUSED,
1599169689Skan	  unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1600169689Skan	  unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1601169689Skan{
160290075Sobrien#if defined ASM_OUTPUT_ALIGNED_BSS
1603169689Skan  ASM_OUTPUT_ALIGNED_BSS (asm_out_file, decl, name, size, DECL_ALIGN (decl));
1604169689Skan  return true;
160590075Sobrien#else
1606169689Skan  ASM_OUTPUT_BSS (asm_out_file, decl, name, size, rounded);
1607169689Skan  return false;
160890075Sobrien#endif
1609169689Skan}
161090075Sobrien#endif
161190075Sobrien
1612169689Skan/* A noswitch_section_callback for comm_section.  */
1613169689Skan
1614169689Skanstatic bool
1615169689Skanemit_common (tree decl ATTRIBUTE_UNUSED,
1616169689Skan	     const char *name ATTRIBUTE_UNUSED,
1617169689Skan	     unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1618169689Skan	     unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1619169689Skan{
162090075Sobrien#if defined ASM_OUTPUT_ALIGNED_DECL_COMMON
1621169689Skan  ASM_OUTPUT_ALIGNED_DECL_COMMON (asm_out_file, decl, name,
1622169689Skan				  size, DECL_ALIGN (decl));
1623169689Skan  return true;
1624169689Skan#elif defined ASM_OUTPUT_ALIGNED_COMMON
1625169689Skan  ASM_OUTPUT_ALIGNED_COMMON (asm_out_file, name, size, DECL_ALIGN (decl));
1626169689Skan  return true;
162790075Sobrien#else
1628169689Skan  ASM_OUTPUT_COMMON (asm_out_file, name, size, rounded);
1629169689Skan  return false;
163090075Sobrien#endif
1631169689Skan}
163290075Sobrien
1633169689Skan/* A noswitch_section_callback for tls_comm_section.  */
1634169689Skan
1635117395Skanstatic bool
1636169689Skanemit_tls_common (tree decl ATTRIBUTE_UNUSED,
1637169689Skan		 const char *name ATTRIBUTE_UNUSED,
1638169689Skan		 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1639169689Skan		 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
164090075Sobrien{
1641169689Skan#ifdef ASM_OUTPUT_TLS_COMMON
1642169689Skan  ASM_OUTPUT_TLS_COMMON (asm_out_file, decl, name, size);
1643169689Skan  return true;
1644117395Skan#else
1645169689Skan  sorry ("thread-local COMMON data not implemented");
1646169689Skan  return true;
1647117395Skan#endif
1648169689Skan}
164990075Sobrien
1650169689Skan/* Assemble DECL given that it belongs in SECTION_NOSWITCH section SECT.
1651169689Skan   NAME is the name of DECL's SYMBOL_REF.  */
165296263Sobrien
1653169689Skanstatic void
1654169689Skanassemble_noswitch_variable (tree decl, const char *name, section *sect)
1655169689Skan{
1656169689Skan  unsigned HOST_WIDE_INT size, rounded;
165790075Sobrien
1658169689Skan  size = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
1659169689Skan  rounded = size;
1660169689Skan
1661169689Skan  /* Don't allocate zero bytes of common,
1662169689Skan     since that means "undefined external" in the linker.  */
1663169689Skan  if (size == 0)
1664169689Skan    rounded = 1;
1665169689Skan
1666169689Skan  /* Round size up to multiple of BIGGEST_ALIGNMENT bits
1667169689Skan     so that each uninitialized object starts on such a boundary.  */
1668169689Skan  rounded += (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1;
1669169689Skan  rounded = (rounded / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
1670169689Skan	     * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
1671169689Skan
1672169689Skan  if (!sect->noswitch.callback (decl, name, size, rounded)
1673169689Skan      && (unsigned HOST_WIDE_INT) DECL_ALIGN_UNIT (decl) > rounded)
1674169689Skan    warning (0, "requested alignment for %q+D is greater than "
1675169689Skan	     "implemented alignment of %wu", decl, rounded);
1676169689Skan}
1677169689Skan
1678169689Skan/* A subroutine of assemble_variable.  Output the label and contents of
1679169689Skan   DECL, whose address is a SYMBOL_REF with name NAME.  DONT_OUTPUT_DATA
1680169689Skan   is as for assemble_variable.  */
1681169689Skan
1682169689Skanstatic void
1683169689Skanassemble_variable_contents (tree decl, const char *name,
1684169689Skan			    bool dont_output_data)
1685169689Skan{
1686169689Skan  /* Do any machine/system dependent processing of the object.  */
1687169689Skan#ifdef ASM_DECLARE_OBJECT_NAME
1688169689Skan  last_assemble_variable_decl = decl;
1689169689Skan  ASM_DECLARE_OBJECT_NAME (asm_out_file, name, decl);
1690169689Skan#else
1691169689Skan  /* Standard thing is just output label for the object.  */
1692169689Skan  ASM_OUTPUT_LABEL (asm_out_file, name);
1693169689Skan#endif /* ASM_DECLARE_OBJECT_NAME */
1694169689Skan
1695169689Skan  if (!dont_output_data)
169690075Sobrien    {
1697169689Skan      if (DECL_INITIAL (decl)
1698169689Skan	  && DECL_INITIAL (decl) != error_mark_node
1699169689Skan	  && !initializer_zerop (DECL_INITIAL (decl)))
1700169689Skan	/* Output the actual data.  */
1701169689Skan	output_constant (DECL_INITIAL (decl),
1702169689Skan			 tree_low_cst (DECL_SIZE_UNIT (decl), 1),
1703169689Skan			 DECL_ALIGN (decl));
1704169689Skan      else
1705169689Skan	/* Leave space for it.  */
1706169689Skan	assemble_zeros (tree_low_cst (DECL_SIZE_UNIT (decl), 1));
170790075Sobrien    }
170890075Sobrien}
170990075Sobrien
171018334Speter/* Assemble everything that is needed for a variable or function declaration.
171118334Speter   Not used for automatic variables, and not used for function definitions.
171218334Speter   Should not be called for variables of incomplete structure type.
171318334Speter
171418334Speter   TOP_LEVEL is nonzero if this variable has file scope.
171518334Speter   AT_END is nonzero if this is the special handling, at end of compilation,
171618334Speter   to define things that have had only tentative definitions.
171718334Speter   DONT_OUTPUT_DATA if nonzero means don't actually output the
171818334Speter   initial value (that will be done by the caller).  */
171918334Speter
172018334Spetervoid
1721132718Skanassemble_variable (tree decl, int top_level ATTRIBUTE_UNUSED,
1722132718Skan		   int at_end ATTRIBUTE_UNUSED, int dont_output_data)
172318334Speter{
172490075Sobrien  const char *name;
1725169689Skan  rtx decl_rtl, symbol;
1726169689Skan  section *sect;
172718334Speter
1728132718Skan  if (lang_hooks.decls.prepare_assemble_variable)
1729169689Skan    lang_hooks.decls.prepare_assemble_variable (decl);
1730132718Skan
173118334Speter  last_assemble_variable_decl = 0;
173218334Speter
173318334Speter  /* Normally no need to say anything here for external references,
173418334Speter     since assemble_external is called by the language-specific code
173518334Speter     when a declaration is first seen.  */
173618334Speter
173718334Speter  if (DECL_EXTERNAL (decl))
173818334Speter    return;
173918334Speter
174018334Speter  /* Output no assembler code for a function declaration.
174118334Speter     Only definitions of functions output anything.  */
174218334Speter
174318334Speter  if (TREE_CODE (decl) == FUNCTION_DECL)
174418334Speter    return;
174518334Speter
174690075Sobrien  /* Do nothing for global register variables.  */
1747169689Skan  if (DECL_RTL_SET_P (decl) && REG_P (DECL_RTL (decl)))
174890075Sobrien    {
174990075Sobrien      TREE_ASM_WRITTEN (decl) = 1;
175090075Sobrien      return;
175190075Sobrien    }
175290075Sobrien
175318334Speter  /* If type was incomplete when the variable was declared,
175418334Speter     see if it is complete now.  */
175518334Speter
175618334Speter  if (DECL_SIZE (decl) == 0)
175718334Speter    layout_decl (decl, 0);
175818334Speter
175918334Speter  /* Still incomplete => don't allocate it; treat the tentative defn
176018334Speter     (which is what it must have been) as an `extern' reference.  */
176118334Speter
176218334Speter  if (!dont_output_data && DECL_SIZE (decl) == 0)
176318334Speter    {
1764169689Skan      error ("storage size of %q+D isn%'t known", decl);
176518334Speter      TREE_ASM_WRITTEN (decl) = 1;
176618334Speter      return;
176718334Speter    }
176818334Speter
176918334Speter  /* The first declaration of a variable that comes through this function
177018334Speter     decides whether it is global (in C, has external linkage)
177118334Speter     or local (in C, has internal linkage).  So do nothing more
177218334Speter     if this function has already run.  */
177318334Speter
177418334Speter  if (TREE_ASM_WRITTEN (decl))
177518334Speter    return;
177618334Speter
1777117395Skan  /* Make sure targetm.encode_section_info is invoked before we set
1778117395Skan     ASM_WRITTEN.  */
177990075Sobrien  decl_rtl = DECL_RTL (decl);
178090075Sobrien
178118334Speter  TREE_ASM_WRITTEN (decl) = 1;
178218334Speter
178352284Sobrien  /* Do no output if -fsyntax-only.  */
178452284Sobrien  if (flag_syntax_only)
178552284Sobrien    return;
178652284Sobrien
178718334Speter  app_disable ();
178818334Speter
178990075Sobrien  if (! dont_output_data
179090075Sobrien      && ! host_integerp (DECL_SIZE_UNIT (decl), 1))
179118334Speter    {
1792169689Skan      error ("size of variable %q+D is too large", decl);
179390075Sobrien      return;
179418334Speter    }
179518334Speter
1796169689Skan  gcc_assert (MEM_P (decl_rtl));
1797169689Skan  gcc_assert (GET_CODE (XEXP (decl_rtl, 0)) == SYMBOL_REF);
1798169689Skan  symbol = XEXP (decl_rtl, 0);
1799169689Skan  name = XSTR (symbol, 0);
1800132718Skan  if (TREE_PUBLIC (decl) && DECL_NAME (decl))
1801132718Skan    notice_global_symbol (decl);
180250397Sobrien
180350397Sobrien  /* Compute the alignment of this data.  */
180450397Sobrien
1805169689Skan  align_variable (decl, dont_output_data);
1806169689Skan  set_mem_align (decl_rtl, DECL_ALIGN (decl));
180750397Sobrien
1808117395Skan  if (TREE_PUBLIC (decl))
1809117395Skan    maybe_assemble_visibility (decl);
1810117395Skan
1811169689Skan  if (DECL_PRESERVE_P (decl))
1812169689Skan    targetm.asm_out.mark_decl_preserved (name);
1813117395Skan
181418334Speter  /* First make the assembler name(s) global if appropriate.  */
1815169689Skan  sect = get_variable_section (decl, false);
1816169689Skan  if (TREE_PUBLIC (decl)
1817169689Skan      && DECL_NAME (decl)
1818169689Skan      && (sect->common.flags & SECTION_COMMON) == 0)
181996263Sobrien    globalize_decl (decl);
182018334Speter
1821169689Skan  /* Output any data that we will need to use the address of.  */
1822169689Skan  if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node)
1823169689Skan    output_addressed_constants (DECL_INITIAL (decl));
182450397Sobrien
182518334Speter  /* dbxout.c needs to know this.  */
1826169689Skan  if (sect && (sect->common.flags & SECTION_CODE) != 0)
182718334Speter    DECL_IN_TEXT_SECTION (decl) = 1;
182818334Speter
1829169689Skan  /* If the decl is part of an object_block, make sure that the decl
1830169689Skan     has been positioned within its block, but do not write out its
1831169689Skan     definition yet.  output_object_blocks will do that later.  */
1832169689Skan  if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
183390075Sobrien    {
1834169689Skan      gcc_assert (!dont_output_data);
1835169689Skan      place_block_symbol (symbol);
183690075Sobrien    }
1837169689Skan  else if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
1838169689Skan    assemble_noswitch_variable (decl, name, sect);
1839169689Skan  else
184018334Speter    {
1841169689Skan      switch_to_section (sect);
1842169689Skan      if (DECL_ALIGN (decl) > BITS_PER_UNIT)
1843169689Skan	ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (DECL_ALIGN_UNIT (decl)));
1844169689Skan      assemble_variable_contents (decl, name, dont_output_data);
184518334Speter    }
184618334Speter}
184718334Speter
184818334Speter/* Return 1 if type TYPE contains any pointers.  */
184918334Speter
185018334Speterstatic int
1851132718Skancontains_pointers_p (tree type)
185218334Speter{
185318334Speter  switch (TREE_CODE (type))
185418334Speter    {
185518334Speter    case POINTER_TYPE:
185618334Speter    case REFERENCE_TYPE:
185718334Speter      /* I'm not sure whether OFFSET_TYPE needs this treatment,
185818334Speter	 so I'll play safe and return 1.  */
185918334Speter    case OFFSET_TYPE:
186018334Speter      return 1;
186118334Speter
186218334Speter    case RECORD_TYPE:
186318334Speter    case UNION_TYPE:
186418334Speter    case QUAL_UNION_TYPE:
186518334Speter      {
186618334Speter	tree fields;
186718334Speter	/* For a type that has fields, see if the fields have pointers.  */
186818334Speter	for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
186918334Speter	  if (TREE_CODE (fields) == FIELD_DECL
187018334Speter	      && contains_pointers_p (TREE_TYPE (fields)))
187118334Speter	    return 1;
187218334Speter	return 0;
187318334Speter      }
187418334Speter
187518334Speter    case ARRAY_TYPE:
187618334Speter      /* An array type contains pointers if its element type does.  */
187718334Speter      return contains_pointers_p (TREE_TYPE (type));
187818334Speter
187918334Speter    default:
188018334Speter      return 0;
188118334Speter    }
188218334Speter}
188318334Speter
1884169689Skan/* In unit-at-a-time mode, we delay assemble_external processing until
1885169689Skan   the compilation unit is finalized.  This is the best we can do for
1886169689Skan   right now (i.e. stage 3 of GCC 4.0) - the right thing is to delay
1887169689Skan   it all the way to final.  See PR 17982 for further discussion.  */
1888169689Skanstatic GTY(()) tree pending_assemble_externals;
1889169689Skan
1890132718Skan#ifdef ASM_OUTPUT_EXTERNAL
1891132718Skan/* True if DECL is a function decl for which no out-of-line copy exists.
1892132718Skan   It is assumed that DECL's assembler name has been set.  */
1893132718Skan
1894132718Skanstatic bool
1895132718Skanincorporeal_function_p (tree decl)
1896132718Skan{
1897132718Skan  if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1898132718Skan    {
1899132718Skan      const char *name;
1900132718Skan
1901132718Skan      if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
1902132718Skan	  && DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA)
1903132718Skan	return true;
1904132718Skan
1905132718Skan      name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
1906132718Skan      if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
1907132718Skan	return true;
1908132718Skan    }
1909132718Skan  return false;
1910132718Skan}
1911169689Skan
1912169689Skan/* Actually do the tests to determine if this is necessary, and invoke
1913169689Skan   ASM_OUTPUT_EXTERNAL.  */
1914169689Skanstatic void
1915169689Skanassemble_external_real (tree decl)
1916169689Skan{
1917169689Skan  rtx rtl = DECL_RTL (decl);
1918169689Skan
1919169689Skan  if (MEM_P (rtl) && GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF
1920169689Skan      && !SYMBOL_REF_USED (XEXP (rtl, 0))
1921169689Skan      && !incorporeal_function_p (decl))
1922169689Skan    {
1923169689Skan      /* Some systems do require some output.  */
1924169689Skan      SYMBOL_REF_USED (XEXP (rtl, 0)) = 1;
1925169689Skan      ASM_OUTPUT_EXTERNAL (asm_out_file, decl, XSTR (XEXP (rtl, 0), 0));
1926169689Skan    }
1927169689Skan}
1928132718Skan#endif
1929132718Skan
1930169689Skanvoid
1931169689Skanprocess_pending_assemble_externals (void)
1932169689Skan{
1933169689Skan#ifdef ASM_OUTPUT_EXTERNAL
1934169689Skan  tree list;
1935169689Skan  for (list = pending_assemble_externals; list; list = TREE_CHAIN (list))
1936169689Skan    assemble_external_real (TREE_VALUE (list));
1937169689Skan
1938169689Skan  pending_assemble_externals = 0;
1939169689Skan#endif
1940169689Skan}
1941169689Skan
194218334Speter/* Output something to declare an external symbol to the assembler.
194318334Speter   (Most assemblers don't need this, so we normally output nothing.)
194418334Speter   Do nothing if DECL is not external.  */
194518334Speter
194618334Spetervoid
1947132718Skanassemble_external (tree decl ATTRIBUTE_UNUSED)
194818334Speter{
194990075Sobrien  /* Because most platforms do not define ASM_OUTPUT_EXTERNAL, the
195090075Sobrien     main body of this code is only rarely exercised.  To provide some
195190075Sobrien     testing, on all platforms, we make sure that the ASM_OUT_FILE is
195290075Sobrien     open.  If it's not, we should not be calling this function.  */
1953169689Skan  gcc_assert (asm_out_file);
195490075Sobrien
195518334Speter#ifdef ASM_OUTPUT_EXTERNAL
1956169689Skan  if (!DECL_P (decl) || !DECL_EXTERNAL (decl) || !TREE_PUBLIC (decl))
1957169689Skan    return;
195818334Speter
1959215840Sdim  /* We want to output external symbols at very last to check if they
1960215840Sdim     are references or not.  */
1961215840Sdim  pending_assemble_externals = tree_cons (0, decl,
1962215840Sdim					  pending_assemble_externals);
196318334Speter#endif
196418334Speter}
196518334Speter
196618334Speter/* Similar, for calling a library function FUN.  */
196718334Speter
196818334Spetervoid
1969132718Skanassemble_external_libcall (rtx fun)
197018334Speter{
197150397Sobrien  /* Declare library function name external when first used, if nec.  */
197250397Sobrien  if (! SYMBOL_REF_USED (fun))
197318334Speter    {
197450397Sobrien      SYMBOL_REF_USED (fun) = 1;
1975169689Skan      targetm.asm_out.external_libcall (fun);
197618334Speter    }
197718334Speter}
197818334Speter
197918334Speter/* Assemble a label named NAME.  */
198018334Speter
198118334Spetervoid
1982132718Skanassemble_label (const char *name)
198318334Speter{
198450397Sobrien  ASM_OUTPUT_LABEL (asm_out_file, name);
198518334Speter}
198618334Speter
1987169689Skan/* Set the symbol_referenced flag for ID.  */
1988132718Skanvoid
1989132718Skanmark_referenced (tree id)
1990132718Skan{
1991169689Skan  TREE_SYMBOL_REFERENCED (id) = 1;
1992169689Skan}
1993169689Skan
1994169689Skan/* Set the symbol_referenced flag for DECL and notify callgraph.  */
1995169689Skanvoid
1996169689Skanmark_decl_referenced (tree decl)
1997169689Skan{
1998169689Skan  if (TREE_CODE (decl) == FUNCTION_DECL)
1999132718Skan    {
2000169689Skan      /* Extern inline functions don't become needed when referenced.
2001169689Skan	 If we know a method will be emitted in other TU and no new
2002169689Skan	 functions can be marked reachable, just use the external
2003169689Skan	 definition.  */
2004169689Skan      struct cgraph_node *node = cgraph_node (decl);
2005169689Skan      if (!DECL_EXTERNAL (decl)
2006169689Skan	  && (!node->local.vtable_method || !cgraph_global_info_ready
2007169689Skan	      || !node->local.finalized))
2008169689Skan	cgraph_mark_needed_node (node);
2009169689Skan    }
2010169689Skan  else if (TREE_CODE (decl) == VAR_DECL)
2011169689Skan    {
2012169689Skan      struct cgraph_varpool_node *node = cgraph_varpool_node (decl);
2013169689Skan      cgraph_varpool_mark_needed_node (node);
2014169689Skan      /* C++ frontend use mark_decl_references to force COMDAT variables
2015169689Skan         to be output that might appear dead otherwise.  */
2016169689Skan      node->force_output = true;
2017169689Skan    }
2018169689Skan  /* else do nothing - we can get various sorts of CST nodes here,
2019169689Skan     which do not need to be marked.  */
2020169689Skan}
2021132718Skan
2022132718Skan
2023169689Skan/* Follow the IDENTIFIER_TRANSPARENT_ALIAS chain starting at *ALIAS
2024169689Skan   until we find an identifier that is not itself a transparent alias.
2025169689Skan   Modify the alias passed to it by reference (and all aliases on the
2026169689Skan   way to the ultimate target), such that they do not have to be
2027169689Skan   followed again, and return the ultimate target of the alias
2028169689Skan   chain.  */
2029169689Skan
2030169689Skanstatic inline tree
2031169689Skanultimate_transparent_alias_target (tree *alias)
2032169689Skan{
2033169689Skan  tree target = *alias;
2034169689Skan
2035169689Skan  if (IDENTIFIER_TRANSPARENT_ALIAS (target))
2036169689Skan    {
2037169689Skan      gcc_assert (TREE_CHAIN (target));
2038169689Skan      target = ultimate_transparent_alias_target (&TREE_CHAIN (target));
2039169689Skan      gcc_assert (! IDENTIFIER_TRANSPARENT_ALIAS (target)
2040169689Skan		  && ! TREE_CHAIN (target));
2041169689Skan      *alias = target;
2042132718Skan    }
2043169689Skan
2044169689Skan  return target;
2045132718Skan}
2046132718Skan
2047169689Skan/* Output to FILE (an assembly file) a reference to NAME.  If NAME
2048169689Skan   starts with a *, the rest of NAME is output verbatim.  Otherwise
2049169689Skan   NAME is transformed in a target-specific way (usually by the
2050169689Skan   addition of an underscore).  */
205118334Speter
205218334Spetervoid
2053169689Skanassemble_name_raw (FILE *file, const char *name)
2054169689Skan{
2055169689Skan  if (name[0] == '*')
2056169689Skan    fputs (&name[1], file);
2057169689Skan  else
2058169689Skan    ASM_OUTPUT_LABELREF (file, name);
2059169689Skan}
2060169689Skan
2061169689Skan/* Like assemble_name_raw, but should be used when NAME might refer to
2062169689Skan   an entity that is also represented as a tree (like a function or
2063169689Skan   variable).  If NAME does refer to such an entity, that entity will
2064169689Skan   be marked as referenced.  */
2065169689Skan
2066169689Skanvoid
2067132718Skanassemble_name (FILE *file, const char *name)
206818334Speter{
206990075Sobrien  const char *real_name;
207050397Sobrien  tree id;
207118334Speter
2072169689Skan  real_name = targetm.strip_name_encoding (name);
207318334Speter
207450397Sobrien  id = maybe_get_identifier (real_name);
207550397Sobrien  if (id)
2076169689Skan    {
2077169689Skan      tree id_orig = id;
207818334Speter
2079169689Skan      mark_referenced (id);
2080169689Skan      ultimate_transparent_alias_target (&id);
2081169689Skan      if (id != id_orig)
2082169689Skan	name = IDENTIFIER_POINTER (id);
2083169689Skan      gcc_assert (! TREE_CHAIN (id));
2084169689Skan    }
2085169689Skan
2086169689Skan  assemble_name_raw (file, name);
208718334Speter}
208818334Speter
208918334Speter/* Allocate SIZE bytes writable static space with a gensym name
209018334Speter   and return an RTX to refer to its address.  */
209118334Speter
209218334Speterrtx
2093132718Skanassemble_static_space (unsigned HOST_WIDE_INT size)
209418334Speter{
209518334Speter  char name[12];
209690075Sobrien  const char *namestring;
209718334Speter  rtx x;
209818334Speter
209918334Speter  ASM_GENERATE_INTERNAL_LABEL (name, "LF", const_labelno);
210018334Speter  ++const_labelno;
210190075Sobrien  namestring = ggc_strdup (name);
210218334Speter
210350397Sobrien  x = gen_rtx_SYMBOL_REF (Pmode, namestring);
2104132718Skan  SYMBOL_REF_FLAGS (x) = SYMBOL_FLAG_LOCAL;
210518334Speter
210650397Sobrien#ifdef ASM_OUTPUT_ALIGNED_DECL_LOCAL
210750397Sobrien  ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, NULL_TREE, name, size,
210850397Sobrien				 BIGGEST_ALIGNMENT);
210950397Sobrien#else
211018334Speter#ifdef ASM_OUTPUT_ALIGNED_LOCAL
211150397Sobrien  ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, BIGGEST_ALIGNMENT);
211218334Speter#else
211350397Sobrien  {
211450397Sobrien    /* Round size up to multiple of BIGGEST_ALIGNMENT bits
211550397Sobrien       so that each uninitialized object starts on such a boundary.  */
211690075Sobrien    /* Variable `rounded' might or might not be used in ASM_OUTPUT_LOCAL.  */
2117132718Skan    unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED
211852284Sobrien      = ((size + (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1)
211952284Sobrien	 / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
212052284Sobrien	 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
212150397Sobrien    ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
212250397Sobrien  }
212318334Speter#endif
212450397Sobrien#endif
212518334Speter  return x;
212618334Speter}
212718334Speter
212818334Speter/* Assemble the static constant template for function entry trampolines.
212918334Speter   This is done at most once per compilation.
213018334Speter   Returns an RTX for the address of the template.  */
213118334Speter
2132169689Skanstatic GTY(()) rtx initial_trampoline;
2133169689Skan
213448743Sobrien#ifdef TRAMPOLINE_TEMPLATE
213518334Speterrtx
2136132718Skanassemble_trampoline_template (void)
213718334Speter{
213818334Speter  char label[256];
213990075Sobrien  const char *name;
214018334Speter  int align;
2141132718Skan  rtx symbol;
214218334Speter
2143169689Skan  if (initial_trampoline)
2144169689Skan    return initial_trampoline;
2145169689Skan
214618334Speter  /* By default, put trampoline templates in read-only data section.  */
214718334Speter
214818334Speter#ifdef TRAMPOLINE_SECTION
2149169689Skan  switch_to_section (TRAMPOLINE_SECTION);
215018334Speter#else
2151169689Skan  switch_to_section (readonly_data_section);
215218334Speter#endif
215318334Speter
215418334Speter  /* Write the assembler code to define one.  */
215550397Sobrien  align = floor_log2 (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
215618334Speter  if (align > 0)
215790075Sobrien    {
215890075Sobrien      ASM_OUTPUT_ALIGN (asm_out_file, align);
215990075Sobrien    }
216018334Speter
2161169689Skan  targetm.asm_out.internal_label (asm_out_file, "LTRAMP", 0);
216218334Speter  TRAMPOLINE_TEMPLATE (asm_out_file);
216318334Speter
216418334Speter  /* Record the rtl to refer to it.  */
216518334Speter  ASM_GENERATE_INTERNAL_LABEL (label, "LTRAMP", 0);
216690075Sobrien  name = ggc_strdup (label);
2167132718Skan  symbol = gen_rtx_SYMBOL_REF (Pmode, name);
2168132718Skan  SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_LOCAL;
2169132718Skan
2170169689Skan  initial_trampoline = gen_rtx_MEM (BLKmode, symbol);
2171169689Skan  set_mem_align (initial_trampoline, TRAMPOLINE_ALIGNMENT);
2172169689Skan
2173169689Skan  return initial_trampoline;
217418334Speter}
217548743Sobrien#endif
217618334Speter
217790075Sobrien/* A and B are either alignments or offsets.  Return the minimum alignment
217890075Sobrien   that may be assumed after adding the two together.  */
217918334Speter
218090075Sobrienstatic inline unsigned
2181132718Skanmin_align (unsigned int a, unsigned int b)
218290075Sobrien{
218390075Sobrien  return (a | b) & -(a | b);
218490075Sobrien}
218518334Speter
218690075Sobrien/* Return the assembler directive for creating a given kind of integer
218790075Sobrien   object.  SIZE is the number of bytes in the object and ALIGNED_P
218890075Sobrien   indicates whether it is known to be aligned.  Return NULL if the
218990075Sobrien   assembly dialect has no such directive.
219090075Sobrien
219190075Sobrien   The returned string should be printed at the start of a new line and
219290075Sobrien   be followed immediately by the object's initial value.  */
219390075Sobrien
219490075Sobrienconst char *
2195132718Skaninteger_asm_op (int size, int aligned_p)
219618334Speter{
219790075Sobrien  struct asm_int_op *ops;
219818334Speter
219990075Sobrien  if (aligned_p)
220090075Sobrien    ops = &targetm.asm_out.aligned_op;
220190075Sobrien  else
220290075Sobrien    ops = &targetm.asm_out.unaligned_op;
220390075Sobrien
220418334Speter  switch (size)
220518334Speter    {
220618334Speter    case 1:
220790075Sobrien      return targetm.asm_out.byte_op;
220818334Speter    case 2:
220990075Sobrien      return ops->hi;
221018334Speter    case 4:
221190075Sobrien      return ops->si;
221218334Speter    case 8:
221390075Sobrien      return ops->di;
221418334Speter    case 16:
221590075Sobrien      return ops->ti;
221690075Sobrien    default:
221790075Sobrien      return NULL;
221818334Speter    }
221990075Sobrien}
222018334Speter
222190075Sobrien/* Use directive OP to assemble an integer object X.  Print OP at the
222290075Sobrien   start of the line, followed immediately by the value of X.  */
222318334Speter
222490075Sobrienvoid
2225132718Skanassemble_integer_with_op (const char *op, rtx x)
222690075Sobrien{
222790075Sobrien  fputs (op, asm_out_file);
222890075Sobrien  output_addr_const (asm_out_file, x);
222990075Sobrien  fputc ('\n', asm_out_file);
223090075Sobrien}
223118334Speter
223290075Sobrien/* The default implementation of the asm_out.integer target hook.  */
223318334Speter
223490075Sobrienbool
2235132718Skandefault_assemble_integer (rtx x ATTRIBUTE_UNUSED,
2236132718Skan			  unsigned int size ATTRIBUTE_UNUSED,
2237132718Skan			  int aligned_p ATTRIBUTE_UNUSED)
223890075Sobrien{
223990075Sobrien  const char *op = integer_asm_op (size, aligned_p);
2240169689Skan  /* Avoid GAS bugs for large values.  Specifically negative values whose
2241169689Skan     absolute value fits in a bfd_vma, but not in a bfd_signed_vma.  */
2242169689Skan  if (size > UNITS_PER_WORD && size > POINTER_SIZE / BITS_PER_UNIT)
2243169689Skan    return false;
224490075Sobrien  return op && (assemble_integer_with_op (op, x), true);
224590075Sobrien}
224690075Sobrien
224790075Sobrien/* Assemble the integer constant X into an object of SIZE bytes.  ALIGN is
224890075Sobrien   the alignment of the integer in bits.  Return 1 if we were able to output
2249169689Skan   the constant, otherwise 0.  We must be able to output the constant,
2250169689Skan   if FORCE is nonzero.  */
225190075Sobrien
225290075Sobrienbool
2253132718Skanassemble_integer (rtx x, unsigned int size, unsigned int align, int force)
225490075Sobrien{
225590075Sobrien  int aligned_p;
225690075Sobrien
225790075Sobrien  aligned_p = (align >= MIN (size * BITS_PER_UNIT, BIGGEST_ALIGNMENT));
225890075Sobrien
225990075Sobrien  /* See if the target hook can handle this kind of object.  */
2260169689Skan  if (targetm.asm_out.integer (x, size, aligned_p))
226190075Sobrien    return true;
226290075Sobrien
226390075Sobrien  /* If the object is a multi-byte one, try splitting it up.  Split
226490075Sobrien     it into words it if is multi-word, otherwise split it into bytes.  */
226590075Sobrien  if (size > 1)
226618334Speter    {
226790075Sobrien      enum machine_mode omode, imode;
226890075Sobrien      unsigned int subalign;
226990075Sobrien      unsigned int subsize, i;
227018334Speter
227190075Sobrien      subsize = size > UNITS_PER_WORD? UNITS_PER_WORD : 1;
227290075Sobrien      subalign = MIN (align, subsize * BITS_PER_UNIT);
227390075Sobrien      omode = mode_for_size (subsize * BITS_PER_UNIT, MODE_INT, 0);
227490075Sobrien      imode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
227590075Sobrien
227690075Sobrien      for (i = 0; i < size; i += subsize)
227718334Speter	{
227890075Sobrien	  rtx partial = simplify_subreg (omode, x, imode, i);
227990075Sobrien	  if (!partial || !assemble_integer (partial, subsize, subalign, 0))
228018334Speter	    break;
228118334Speter	}
228290075Sobrien      if (i == size)
228390075Sobrien	return true;
228418334Speter
228590075Sobrien      /* If we've printed some of it, but not all of it, there's no going
228690075Sobrien	 back now.  */
2287169689Skan      gcc_assert (!i);
228818334Speter    }
228918334Speter
2290169689Skan  gcc_assert (!force);
229118334Speter
229290075Sobrien  return false;
229318334Speter}
229418334Speter
229518334Spetervoid
2296132718Skanassemble_real (REAL_VALUE_TYPE d, enum machine_mode mode, unsigned int align)
229718334Speter{
2298169689Skan  long data[4] = {0, 0, 0, 0};
2299132718Skan  int i;
2300132718Skan  int bitsize, nelts, nunits, units_per;
230118334Speter
2302132718Skan  /* This is hairy.  We have a quantity of known size.  real_to_target
2303132718Skan     will put it into an array of *host* longs, 32 bits per element
2304132718Skan     (even if long is more than 32 bits).  We need to determine the
2305132718Skan     number of array elements that are occupied (nelts) and the number
2306132718Skan     of *target* min-addressable units that will be occupied in the
2307132718Skan     object file (nunits).  We cannot assume that 32 divides the
2308132718Skan     mode's bitsize (size * BITS_PER_UNIT) evenly.
230918334Speter
2310132718Skan     size * BITS_PER_UNIT is used here to make sure that padding bits
2311132718Skan     (which might appear at either end of the value; real_to_target
2312132718Skan     will include the padding bits in its output array) are included.  */
231318334Speter
2314132718Skan  nunits = GET_MODE_SIZE (mode);
2315132718Skan  bitsize = nunits * BITS_PER_UNIT;
2316132718Skan  nelts = CEIL (bitsize, 32);
2317132718Skan  units_per = 32 / BITS_PER_UNIT;
231818334Speter
2319132718Skan  real_to_target (data, &d, mode);
2320132718Skan
2321132718Skan  /* Put out the first word with the specified alignment.  */
2322132718Skan  assemble_integer (GEN_INT (data[0]), MIN (nunits, units_per), align, 1);
2323132718Skan  nunits -= units_per;
2324132718Skan
2325132718Skan  /* Subsequent words need only 32-bit alignment.  */
2326132718Skan  align = min_align (align, 32);
2327132718Skan
2328132718Skan  for (i = 1; i < nelts; i++)
2329132718Skan    {
2330132718Skan      assemble_integer (GEN_INT (data[i]), MIN (nunits, units_per), align, 1);
2331132718Skan      nunits -= units_per;
233218334Speter    }
233318334Speter}
233418334Speter
233518334Speter/* Given an expression EXP with a constant value,
233618334Speter   reduce it to the sum of an assembler symbol and an integer.
233718334Speter   Store them both in the structure *VALUE.
2338169689Skan   EXP must be reducible.  */
233918334Speter
2340117395Skanstruct addr_const GTY(())
234118334Speter{
234218334Speter  rtx base;
234318334Speter  HOST_WIDE_INT offset;
234418334Speter};
234518334Speter
234618334Speterstatic void
2347132718Skandecode_addr_const (tree exp, struct addr_const *value)
234818334Speter{
234990075Sobrien  tree target = TREE_OPERAND (exp, 0);
235090075Sobrien  int offset = 0;
235190075Sobrien  rtx x;
235218334Speter
235318334Speter  while (1)
235418334Speter    {
235518334Speter      if (TREE_CODE (target) == COMPONENT_REF
235690075Sobrien	  && host_integerp (byte_position (TREE_OPERAND (target, 1)), 0))
235790075Sobrien
235818334Speter	{
235990075Sobrien	  offset += int_byte_position (TREE_OPERAND (target, 1));
236018334Speter	  target = TREE_OPERAND (target, 0);
236118334Speter	}
236290075Sobrien      else if (TREE_CODE (target) == ARRAY_REF
236390075Sobrien	       || TREE_CODE (target) == ARRAY_RANGE_REF)
236418334Speter	{
236590075Sobrien	  offset += (tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (target)), 1)
236690075Sobrien		     * tree_low_cst (TREE_OPERAND (target, 1), 0));
236718334Speter	  target = TREE_OPERAND (target, 0);
236818334Speter	}
236918334Speter      else
237018334Speter	break;
237118334Speter    }
237218334Speter
237318334Speter  switch (TREE_CODE (target))
237418334Speter    {
237518334Speter    case VAR_DECL:
237618334Speter    case FUNCTION_DECL:
237718334Speter      x = DECL_RTL (target);
237818334Speter      break;
237918334Speter
238018334Speter    case LABEL_DECL:
238150397Sobrien      x = gen_rtx_MEM (FUNCTION_MODE,
2382169689Skan		       gen_rtx_LABEL_REF (Pmode, force_label_rtx (target)));
238318334Speter      break;
238418334Speter
238518334Speter    case REAL_CST:
238618334Speter    case STRING_CST:
238718334Speter    case COMPLEX_CST:
238818334Speter    case CONSTRUCTOR:
238950397Sobrien    case INTEGER_CST:
239090075Sobrien      x = output_constant_def (target, 1);
239118334Speter      break;
239218334Speter
239318334Speter    default:
2394169689Skan      gcc_unreachable ();
239518334Speter    }
239618334Speter
2397169689Skan  gcc_assert (MEM_P (x));
239850397Sobrien  x = XEXP (x, 0);
239918334Speter
240018334Speter  value->base = x;
240118334Speter  value->offset = offset;
240218334Speter}
240318334Speter
240418334Speter/* Uniquize all constants that appear in memory.
240518334Speter   Each constant in memory thus far output is recorded
2406132718Skan   in `const_desc_table'.  */
240718334Speter
2408117395Skanstruct constant_descriptor_tree GTY(())
2409117395Skan{
2410117395Skan  /* A MEM for the constant.  */
241190075Sobrien  rtx rtl;
2412117395Skan
2413117395Skan  /* The value of the constant.  */
2414117395Skan  tree value;
2415169689Skan
2416169689Skan  /* Hash of value.  Computing the hash from value each time
2417169689Skan     hashfn is called can't work properly, as that means recursive
2418169689Skan     use of the hash table during hash table expansion.  */
2419169689Skan  hashval_t hash;
242018334Speter};
242118334Speter
2422132718Skanstatic GTY((param_is (struct constant_descriptor_tree)))
2423132718Skan     htab_t const_desc_htab;
242418334Speter
2425132718Skanstatic struct constant_descriptor_tree * build_constant_desc (tree);
2426132718Skanstatic void maybe_output_constant_def_contents (struct constant_descriptor_tree *, int);
242790075Sobrien
2428132718Skan/* Compute a hash code for a constant expression.  */
242990075Sobrien
243090075Sobrienstatic hashval_t
2431132718Skanconst_desc_hash (const void *ptr)
243290075Sobrien{
2433169689Skan  return ((struct constant_descriptor_tree *)ptr)->hash;
243490075Sobrien}
243590075Sobrien
2436132718Skanstatic hashval_t
2437132718Skanconst_hash_1 (const tree exp)
243890075Sobrien{
243990075Sobrien  const char *p;
2440132718Skan  hashval_t hi;
2441117395Skan  int len, i;
244290075Sobrien  enum tree_code code = TREE_CODE (exp);
244318334Speter
244450397Sobrien  /* Either set P and LEN to the address and len of something to hash and
244550397Sobrien     exit the switch or return a value.  */
244650397Sobrien
244750397Sobrien  switch (code)
244818334Speter    {
244950397Sobrien    case INTEGER_CST:
245090075Sobrien      p = (char *) &TREE_INT_CST (exp);
245190075Sobrien      len = sizeof TREE_INT_CST (exp);
245250397Sobrien      break;
245350397Sobrien
245450397Sobrien    case REAL_CST:
2455117395Skan      return real_hash (TREE_REAL_CST_PTR (exp));
245618334Speter
245750397Sobrien    case STRING_CST:
2458169689Skan      p = TREE_STRING_POINTER (exp);
2459169689Skan      len = TREE_STRING_LENGTH (exp);
246050397Sobrien      break;
246118334Speter
246250397Sobrien    case COMPLEX_CST:
2463117395Skan      return (const_hash_1 (TREE_REALPART (exp)) * 5
2464117395Skan	      + const_hash_1 (TREE_IMAGPART (exp)));
246518334Speter
246650397Sobrien    case CONSTRUCTOR:
2467169689Skan      {
2468169689Skan	unsigned HOST_WIDE_INT idx;
2469169689Skan	tree value;
247090075Sobrien
2471169689Skan	hi = 5 + int_size_in_bytes (TREE_TYPE (exp));
247218334Speter
2473169689Skan	FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
2474169689Skan	  if (value)
2475169689Skan	    hi = hi * 603 + const_hash_1 (value);
247650397Sobrien
2477169689Skan	return hi;
2478169689Skan      }
247950397Sobrien
248050397Sobrien    case ADDR_EXPR:
2481102780Skan    case FDESC_EXPR:
248250397Sobrien      {
248350397Sobrien	struct addr_const value;
248450397Sobrien
248550397Sobrien	decode_addr_const (exp, &value);
2486169689Skan	switch (GET_CODE (value.base))
248750397Sobrien	  {
2488169689Skan	  case SYMBOL_REF:
248950397Sobrien	    /* Don't hash the address of the SYMBOL_REF;
249050397Sobrien	       only use the offset and the symbol name.  */
249150397Sobrien	    hi = value.offset;
249250397Sobrien	    p = XSTR (value.base, 0);
249350397Sobrien	    for (i = 0; p[i] != 0; i++)
249450397Sobrien	      hi = ((hi * 613) + (unsigned) (p[i]));
2495169689Skan	    break;
2496169689Skan
2497169689Skan	  case LABEL_REF:
2498169689Skan	    hi = value.offset + CODE_LABEL_NUMBER (XEXP (value.base, 0)) * 13;
2499169689Skan	    break;
2500169689Skan
2501169689Skan	  default:
2502169689Skan	    gcc_unreachable ();
250350397Sobrien	  }
250450397Sobrien      }
250518334Speter      return hi;
250650397Sobrien
250750397Sobrien    case PLUS_EXPR:
250850397Sobrien    case MINUS_EXPR:
2509117395Skan      return (const_hash_1 (TREE_OPERAND (exp, 0)) * 9
2510117395Skan	      + const_hash_1 (TREE_OPERAND (exp, 1)));
251150397Sobrien
251250397Sobrien    case NOP_EXPR:
251350397Sobrien    case CONVERT_EXPR:
251450397Sobrien    case NON_LVALUE_EXPR:
2515117395Skan      return const_hash_1 (TREE_OPERAND (exp, 0)) * 7 + 2;
251690075Sobrien
251750397Sobrien    default:
251890075Sobrien      /* A language specific constant. Just hash the code.  */
2519117395Skan      return code;
252018334Speter    }
252118334Speter
2522132718Skan  /* Compute hashing function.  */
252318334Speter  hi = len;
252418334Speter  for (i = 0; i < len; i++)
252550397Sobrien    hi = ((hi * 613) + (unsigned) (p[i]));
252618334Speter
252718334Speter  return hi;
252818334Speter}
252918334Speter
2530132718Skan/* Wrapper of compare_constant, for the htab interface.  */
2531132718Skanstatic int
2532132718Skanconst_desc_eq (const void *p1, const void *p2)
2533132718Skan{
2534169689Skan  const struct constant_descriptor_tree *c1 = p1;
2535169689Skan  const struct constant_descriptor_tree *c2 = p2;
2536169689Skan  if (c1->hash != c2->hash)
2537169689Skan    return 0;
2538169689Skan  return compare_constant (c1->value, c2->value);
2539132718Skan}
2540132718Skan
2541117395Skan/* Compare t1 and t2, and return 1 only if they are known to result in
2542117395Skan   the same bit pattern on output.  */
2543117395Skan
254418334Speterstatic int
2545132718Skancompare_constant (const tree t1, const tree t2)
254618334Speter{
2547117395Skan  enum tree_code typecode;
254818334Speter
2549117395Skan  if (t1 == NULL_TREE)
2550117395Skan    return t2 == NULL_TREE;
2551117395Skan  if (t2 == NULL_TREE)
2552117395Skan    return 0;
255318334Speter
2554117395Skan  if (TREE_CODE (t1) != TREE_CODE (t2))
255518334Speter    return 0;
255618334Speter
2557117395Skan  switch (TREE_CODE (t1))
255818334Speter    {
255950397Sobrien    case INTEGER_CST:
256018334Speter      /* Integer constants are the same only if the same width of type.  */
2561117395Skan      if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
256218334Speter	return 0;
2563169689Skan      if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
2564169689Skan	return 0;
2565117395Skan      return tree_int_cst_equal (t1, t2);
256650397Sobrien
256750397Sobrien    case REAL_CST:
256818334Speter      /* Real constants are the same only if the same width of type.  */
2569117395Skan      if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
257018334Speter	return 0;
257150397Sobrien
2572117395Skan      return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
257350397Sobrien
257450397Sobrien    case STRING_CST:
2575117395Skan      if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
257650397Sobrien	return 0;
257750397Sobrien
2578117395Skan      return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
2579117395Skan	      && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
2580117395Skan			 TREE_STRING_LENGTH (t1)));
258150397Sobrien
2582117395Skan    case COMPLEX_CST:
2583117395Skan      return (compare_constant (TREE_REALPART (t1), TREE_REALPART (t2))
2584117395Skan	      && compare_constant (TREE_IMAGPART (t1), TREE_IMAGPART (t2)));
258550397Sobrien
2586117395Skan    case CONSTRUCTOR:
2587169689Skan      {
2588169689Skan	VEC(constructor_elt, gc) *v1, *v2;
2589169689Skan	unsigned HOST_WIDE_INT idx;
259018334Speter
2591169689Skan	typecode = TREE_CODE (TREE_TYPE (t1));
2592169689Skan	if (typecode != TREE_CODE (TREE_TYPE (t2)))
2593169689Skan	  return 0;
259450397Sobrien
2595169689Skan	if (typecode == ARRAY_TYPE)
2596169689Skan	  {
2597169689Skan	    HOST_WIDE_INT size_1 = int_size_in_bytes (TREE_TYPE (t1));
2598169689Skan	    /* For arrays, check that the sizes all match.  */
2599169689Skan	    if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))
2600169689Skan		|| size_1 == -1
2601169689Skan		|| size_1 != int_size_in_bytes (TREE_TYPE (t2)))
2602169689Skan	      return 0;
2603169689Skan	  }
2604169689Skan	else
2605169689Skan	  {
2606169689Skan	    /* For record and union constructors, require exact type
2607169689Skan               equality.  */
2608169689Skan	    if (TREE_TYPE (t1) != TREE_TYPE (t2))
2609169689Skan	      return 0;
2610169689Skan	  }
261150397Sobrien
2612169689Skan	v1 = CONSTRUCTOR_ELTS (t1);
2613169689Skan	v2 = CONSTRUCTOR_ELTS (t2);
2614169689Skan	if (VEC_length (constructor_elt, v1)
2615169689Skan	    != VEC_length (constructor_elt, v2))
2616117395Skan	    return 0;
2617117395Skan
2618169689Skan	for (idx = 0; idx < VEC_length (constructor_elt, v1); ++idx)
2619169689Skan	  {
2620169689Skan	    constructor_elt *c1 = VEC_index (constructor_elt, v1, idx);
2621169689Skan	    constructor_elt *c2 = VEC_index (constructor_elt, v2, idx);
262218334Speter
2623169689Skan	    /* Check that each value is the same...  */
2624169689Skan	    if (!compare_constant (c1->value, c2->value))
2625169689Skan	      return 0;
2626169689Skan	    /* ... and that they apply to the same fields!  */
2627169689Skan	    if (typecode == ARRAY_TYPE)
2628169689Skan	      {
2629169689Skan		if (!compare_constant (c1->index, c2->index))
2630169689Skan		  return 0;
2631169689Skan	      }
2632169689Skan	    else
2633169689Skan	      {
2634169689Skan		if (c1->index != c2->index)
2635169689Skan		  return 0;
2636169689Skan	      }
2637169689Skan	  }
263850397Sobrien
2639169689Skan	return 1;
2640169689Skan      }
264150397Sobrien
264250397Sobrien    case ADDR_EXPR:
2643102780Skan    case FDESC_EXPR:
264450397Sobrien      {
2645117395Skan	struct addr_const value1, value2;
264650397Sobrien
2647117395Skan	decode_addr_const (t1, &value1);
2648117395Skan	decode_addr_const (t2, &value2);
2649117395Skan	return (value1.offset == value2.offset
2650117395Skan		&& strcmp (XSTR (value1.base, 0), XSTR (value2.base, 0)) == 0);
265150397Sobrien      }
265250397Sobrien
265350397Sobrien    case PLUS_EXPR:
265450397Sobrien    case MINUS_EXPR:
265552284Sobrien    case RANGE_EXPR:
2656117395Skan      return (compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
2657117395Skan	      && compare_constant(TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
265850397Sobrien
265950397Sobrien    case NOP_EXPR:
266050397Sobrien    case CONVERT_EXPR:
266150397Sobrien    case NON_LVALUE_EXPR:
2662169689Skan    case VIEW_CONVERT_EXPR:
2663117395Skan      return compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
266450397Sobrien
266550397Sobrien    default:
266690075Sobrien      {
2667117395Skan	tree nt1, nt2;
2668169689Skan	nt1 = lang_hooks.expand_constant (t1);
2669169689Skan	nt2 = lang_hooks.expand_constant (t2);
2670117395Skan	if (nt1 != t1 || nt2 != t2)
2671117395Skan	  return compare_constant (nt1, nt2);
267290075Sobrien	else
267390075Sobrien	  return 0;
267490075Sobrien      }
267518334Speter    }
267618334Speter
2677169689Skan  gcc_unreachable ();
267818334Speter}
267918334Speter
2680117395Skan/* Make a copy of the whole tree structure for a constant.  This
2681169689Skan   handles the same types of nodes that compare_constant handles.  */
268218334Speter
268318334Speterstatic tree
2684132718Skancopy_constant (tree exp)
268518334Speter{
268618334Speter  switch (TREE_CODE (exp))
268718334Speter    {
268818334Speter    case ADDR_EXPR:
268918334Speter      /* For ADDR_EXPR, we do not want to copy the decl whose address
269018334Speter	 is requested.  We do want to copy constants though.  */
2691169689Skan      if (CONSTANT_CLASS_P (TREE_OPERAND (exp, 0)))
269218334Speter	return build1 (TREE_CODE (exp), TREE_TYPE (exp),
269318334Speter		       copy_constant (TREE_OPERAND (exp, 0)));
269418334Speter      else
269518334Speter	return copy_node (exp);
269618334Speter
269718334Speter    case INTEGER_CST:
269818334Speter    case REAL_CST:
2699169689Skan    case STRING_CST:
270018334Speter      return copy_node (exp);
270118334Speter
270218334Speter    case COMPLEX_CST:
270350397Sobrien      return build_complex (TREE_TYPE (exp),
270450397Sobrien			    copy_constant (TREE_REALPART (exp)),
270518334Speter			    copy_constant (TREE_IMAGPART (exp)));
270618334Speter
270718334Speter    case PLUS_EXPR:
270818334Speter    case MINUS_EXPR:
2709169689Skan      return build2 (TREE_CODE (exp), TREE_TYPE (exp),
2710169689Skan		     copy_constant (TREE_OPERAND (exp, 0)),
2711169689Skan		     copy_constant (TREE_OPERAND (exp, 1)));
271218334Speter
271318334Speter    case NOP_EXPR:
271418334Speter    case CONVERT_EXPR:
271550397Sobrien    case NON_LVALUE_EXPR:
2716132718Skan    case VIEW_CONVERT_EXPR:
271718334Speter      return build1 (TREE_CODE (exp), TREE_TYPE (exp),
271818334Speter		     copy_constant (TREE_OPERAND (exp, 0)));
271918334Speter
272018334Speter    case CONSTRUCTOR:
272118334Speter      {
272218334Speter	tree copy = copy_node (exp);
2723169689Skan	VEC(constructor_elt, gc) *v;
2724169689Skan	unsigned HOST_WIDE_INT idx;
2725169689Skan	tree purpose, value;
272618334Speter
2727169689Skan	v = VEC_alloc(constructor_elt, gc, VEC_length(constructor_elt,
2728169689Skan						      CONSTRUCTOR_ELTS (exp)));
2729169689Skan	FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, purpose, value)
2730169689Skan	  {
2731169689Skan	    constructor_elt *ce = VEC_quick_push (constructor_elt, v, NULL);
2732169689Skan	    ce->index = purpose;
2733169689Skan	    ce->value = copy_constant (value);
2734169689Skan	  }
2735169689Skan	CONSTRUCTOR_ELTS (copy) = v;
273618334Speter	return copy;
273718334Speter      }
273818334Speter
273918334Speter    default:
2740117395Skan      {
2741169689Skan	tree t = lang_hooks.expand_constant (exp);
2742169689Skan
2743169689Skan	gcc_assert (t != exp);
2744169689Skan	return copy_constant (t);
2745117395Skan      }
274618334Speter    }
274718334Speter}
274818334Speter
2749169689Skan/* Return the alignment of constant EXP in bits.  */
2750169689Skan
2751169689Skanstatic unsigned int
2752169689Skanget_constant_alignment (tree exp)
2753169689Skan{
2754169689Skan  unsigned int align;
2755169689Skan
2756169689Skan  align = TYPE_ALIGN (TREE_TYPE (exp));
2757169689Skan#ifdef CONSTANT_ALIGNMENT
2758169689Skan  align = CONSTANT_ALIGNMENT (exp, align);
2759169689Skan#endif
2760169689Skan  return align;
2761169689Skan}
2762169689Skan
2763169689Skan/* Return the section into which constant EXP should be placed.  */
2764169689Skan
2765169689Skanstatic section *
2766169689Skanget_constant_section (tree exp)
2767169689Skan{
2768169689Skan  if (IN_NAMED_SECTION (exp))
2769169689Skan    return get_named_section (exp, NULL, compute_reloc_for_constant (exp));
2770169689Skan  else
2771169689Skan    return targetm.asm_out.select_section (exp,
2772169689Skan					   compute_reloc_for_constant (exp),
2773169689Skan					   get_constant_alignment (exp));
2774169689Skan}
2775169689Skan
2776169689Skan/* Return the size of constant EXP in bytes.  */
2777169689Skan
2778169689Skanstatic HOST_WIDE_INT
2779169689Skanget_constant_size (tree exp)
2780169689Skan{
2781169689Skan  HOST_WIDE_INT size;
2782169689Skan
2783169689Skan  size = int_size_in_bytes (TREE_TYPE (exp));
2784169689Skan  if (TREE_CODE (exp) == STRING_CST)
2785169689Skan    size = MAX (TREE_STRING_LENGTH (exp), size);
2786169689Skan  return size;
2787169689Skan}
2788169689Skan
2789132718Skan/* Subroutine of output_constant_def:
2790132718Skan   No constant equal to EXP is known to have been output.
2791132718Skan   Make a constant descriptor to enter EXP in the hash table.
2792132718Skan   Assign the label number and construct RTL to refer to the
2793132718Skan   constant's location in memory.
2794132718Skan   Caller is responsible for updating the hash table.  */
279518334Speter
2796132718Skanstatic struct constant_descriptor_tree *
2797132718Skanbuild_constant_desc (tree exp)
279818334Speter{
2799132718Skan  rtx symbol;
2800132718Skan  rtx rtl;
2801132718Skan  char label[256];
2802132718Skan  int labelno;
2803117395Skan  struct constant_descriptor_tree *desc;
280418334Speter
2805132718Skan  desc = ggc_alloc (sizeof (*desc));
2806146895Skan  desc->value = copy_constant (exp);
2807122180Skan
2808169689Skan  /* Propagate marked-ness to copied constant.  */
2809169689Skan  if (flag_mudflap && mf_marked_p (exp))
2810169689Skan    mf_mark (desc->value);
2811169689Skan
2812132718Skan  /* Create a string containing the label name, in LABEL.  */
2813132718Skan  labelno = const_labelno++;
2814132718Skan  ASM_GENERATE_INTERNAL_LABEL (label, "LC", labelno);
281518334Speter
2816132718Skan  /* We have a symbol name; construct the SYMBOL_REF and the MEM.  */
2817169689Skan  if (use_object_blocks_p ())
2818169689Skan    {
2819169689Skan      section *sect = get_constant_section (exp);
2820169689Skan      symbol = create_block_symbol (ggc_strdup (label),
2821169689Skan				    get_block_for_section (sect), -1);
2822169689Skan    }
2823169689Skan  else
2824169689Skan    symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
2825169689Skan  SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
2826169689Skan  SET_SYMBOL_REF_DECL (symbol, desc->value);
2827132718Skan  TREE_CONSTANT_POOL_ADDRESS_P (symbol) = 1;
282818334Speter
2829132718Skan  rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (exp)), symbol);
2830132718Skan  set_mem_attributes (rtl, exp, 1);
2831132718Skan  set_mem_alias_set (rtl, 0);
2832132718Skan  set_mem_alias_set (rtl, const_alias_set);
283318334Speter
2834132718Skan  /* Set flags or add text to the name to record information, such as
2835132718Skan     that it is a local symbol.  If the name is changed, the macro
2836132718Skan     ASM_OUTPUT_LABELREF will have to know how to strip this
2837132718Skan     information.  This call might invalidate our local variable
2838132718Skan     SYMBOL; we can't use it afterward.  */
283918334Speter
2840169689Skan  targetm.encode_section_info (exp, rtl, true);
284190075Sobrien
2842132718Skan  desc->rtl = rtl;
284390075Sobrien
2844132718Skan  return desc;
2845132718Skan}
284690075Sobrien
2847132718Skan/* Return an rtx representing a reference to constant data in memory
2848132718Skan   for the constant expression EXP.
284918334Speter
2850132718Skan   If assembler code for such a constant has already been output,
2851132718Skan   return an rtx to refer to it.
2852132718Skan   Otherwise, output such a constant in memory
2853132718Skan   and generate an rtx for it.
285490075Sobrien
2855132718Skan   If DEFER is nonzero, this constant can be deferred and output only
2856132718Skan   if referenced in the function after all optimizations.
285790075Sobrien
2858132718Skan   `const_desc_table' records which constants already have label strings.  */
285990075Sobrien
2860132718Skanrtx
2861132718Skanoutput_constant_def (tree exp, int defer)
2862132718Skan{
2863132718Skan  struct constant_descriptor_tree *desc;
2864132718Skan  struct constant_descriptor_tree key;
2865132718Skan  void **loc;
286618334Speter
2867132718Skan  /* Look up EXP in the table of constant descriptors.  If we didn't find
2868132718Skan     it, create a new one.  */
2869132718Skan  key.value = exp;
2870169689Skan  key.hash = const_hash_1 (exp);
2871169689Skan  loc = htab_find_slot_with_hash (const_desc_htab, &key, key.hash, INSERT);
287218334Speter
2873132718Skan  desc = *loc;
2874132718Skan  if (desc == 0)
287590075Sobrien    {
2876132718Skan      desc = build_constant_desc (exp);
2877169689Skan      desc->hash = key.hash;
2878132718Skan      *loc = desc;
287990075Sobrien    }
288018334Speter
2881132718Skan  maybe_output_constant_def_contents (desc, defer);
2882132718Skan  return desc->rtl;
2883132718Skan}
288490075Sobrien
2885132718Skan/* Subroutine of output_constant_def: Decide whether or not we need to
2886132718Skan   output the constant DESC now, and if so, do it.  */
2887132718Skanstatic void
2888132718Skanmaybe_output_constant_def_contents (struct constant_descriptor_tree *desc,
2889132718Skan				    int defer)
2890132718Skan{
2891132718Skan  rtx symbol = XEXP (desc->rtl, 0);
2892132718Skan  tree exp = desc->value;
289390075Sobrien
2894132718Skan  if (flag_syntax_only)
2895132718Skan    return;
289618334Speter
2897132718Skan  if (TREE_ASM_WRITTEN (exp))
2898132718Skan    /* Already output; don't do it again.  */
2899132718Skan    return;
290090075Sobrien
2901169689Skan  /* We can always defer constants as long as the context allows
2902169689Skan     doing so.  */
2903169689Skan  if (defer)
2904132718Skan    {
2905132718Skan      /* Increment n_deferred_constants if it exists.  It needs to be at
2906132718Skan	 least as large as the number of constants actually referred to
2907132718Skan	 by the function.  If it's too small we'll stop looking too early
2908132718Skan	 and fail to emit constants; if it's too large we'll only look
2909132718Skan	 through the entire function when we could have stopped earlier.  */
2910132718Skan      if (cfun)
2911132718Skan	n_deferred_constants++;
2912132718Skan      return;
291318334Speter    }
291418334Speter
2915132718Skan  output_constant_def_contents (symbol);
291618334Speter}
291718334Speter
2918169689Skan/* Subroutine of output_constant_def_contents.  Output the definition
2919169689Skan   of constant EXP, which is pointed to by label LABEL.  ALIGN is the
2920169689Skan   constant's alignment in bits.  */
292118334Speter
292218334Speterstatic void
2923169689Skanassemble_constant_contents (tree exp, const char *label, unsigned int align)
292418334Speter{
2925122180Skan  HOST_WIDE_INT size;
292618334Speter
2927169689Skan  size = get_constant_size (exp);
2928132718Skan
2929122180Skan  /* Do any machine/system dependent processing of the constant.  */
2930122180Skan#ifdef ASM_DECLARE_CONSTANT_NAME
2931132718Skan  ASM_DECLARE_CONSTANT_NAME (asm_out_file, label, exp, size);
2932122180Skan#else
2933122180Skan  /* Standard thing is just output label for the constant.  */
2934132718Skan  ASM_OUTPUT_LABEL (asm_out_file, label);
2935122180Skan#endif /* ASM_DECLARE_CONSTANT_NAME */
293618334Speter
293718334Speter  /* Output the value of EXP.  */
2938122180Skan  output_constant (exp, size, align);
293918334Speter}
2940132718Skan
2941169689Skan/* We must output the constant data referred to by SYMBOL; do so.  */
2942169689Skan
2943169689Skanstatic void
2944169689Skanoutput_constant_def_contents (rtx symbol)
2945132718Skan{
2946169689Skan  tree exp = SYMBOL_REF_DECL (symbol);
2947169689Skan  unsigned int align;
2948169689Skan
2949169689Skan  /* Make sure any other constants whose addresses appear in EXP
2950169689Skan     are assigned label numbers.  */
2951169689Skan  output_addressed_constants (exp);
2952169689Skan
2953169689Skan  /* We are no longer deferring this constant.  */
2954169689Skan  TREE_ASM_WRITTEN (exp) = 1;
2955169689Skan
2956169689Skan  /* If the constant is part of an object block, make sure that the
2957169689Skan     decl has been positioned within its block, but do not write out
2958169689Skan     its definition yet.  output_object_blocks will do that later.  */
2959169689Skan  if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
2960169689Skan    place_block_symbol (symbol);
2961169689Skan  else
2962169689Skan    {
2963169689Skan      switch_to_section (get_constant_section (exp));
2964169689Skan      align = get_constant_alignment (exp);
2965169689Skan      if (align > BITS_PER_UNIT)
2966169689Skan	ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
2967169689Skan      assemble_constant_contents (exp, XSTR (symbol, 0), align);
2968169689Skan    }
2969169689Skan  if (flag_mudflap)
2970169689Skan    mudflap_enqueue_constant (exp);
2971132718Skan}
2972132718Skan
2973132718Skan/* Look up EXP in the table of constant descriptors.  Return the rtl
2974132718Skan   if it has been emitted, else null.  */
2975132718Skan
2976132718Skanrtx
2977132718Skanlookup_constant_def (tree exp)
2978132718Skan{
2979132718Skan  struct constant_descriptor_tree *desc;
2980132718Skan  struct constant_descriptor_tree key;
2981132718Skan
2982132718Skan  key.value = exp;
2983169689Skan  key.hash = const_hash_1 (exp);
2984169689Skan  desc = htab_find_with_hash (const_desc_htab, &key, key.hash);
2985132718Skan
2986132718Skan  return (desc ? desc->rtl : NULL_RTX);
2987132718Skan}
298818334Speter
2989117395Skan/* Used in the hash tables to avoid outputting the same constant
2990117395Skan   twice.  Unlike 'struct constant_descriptor_tree', RTX constants
2991169689Skan   are output once per function, not once per file.  */
2992169689Skan/* ??? Only a few targets need per-function constant pools.  Most
2993169689Skan   can use one per-file pool.  Should add a targetm bit to tell the
2994169689Skan   difference.  */
2995117395Skan
2996169689Skanstruct rtx_constant_pool GTY(())
2997117395Skan{
2998169689Skan  /* Pointers to first and last constant in pool, as ordered by offset.  */
2999169689Skan  struct constant_descriptor_rtx *first;
3000169689Skan  struct constant_descriptor_rtx *last;
3001117395Skan
3002169689Skan  /* Hash facility for making memory-constants from constant rtl-expressions.
3003169689Skan     It is used on RISC machines where immediate integer arguments and
3004169689Skan     constant addresses are restricted so that such constants must be stored
3005169689Skan     in memory.  */
3006169689Skan  htab_t GTY((param_is (struct constant_descriptor_rtx))) const_rtx_htab;
3007117395Skan
3008169689Skan  /* Current offset in constant pool (does not include any
3009169689Skan     machine-specific header).  */
3010169689Skan  HOST_WIDE_INT offset;
3011117395Skan};
3012117395Skan
3013169689Skanstruct constant_descriptor_rtx GTY((chain_next ("%h.next")))
301418334Speter{
3015169689Skan  struct constant_descriptor_rtx *next;
3016169689Skan  rtx mem;
3017169689Skan  rtx sym;
301890075Sobrien  rtx constant;
3019169689Skan  HOST_WIDE_INT offset;
3020169689Skan  hashval_t hash;
302118334Speter  enum machine_mode mode;
3022169689Skan  unsigned int align;
302318334Speter  int labelno;
302450397Sobrien  int mark;
302518334Speter};
302618334Speter
3027169689Skan/* Hash and compare functions for const_rtx_htab.  */
302818334Speter
3029169689Skanstatic hashval_t
3030169689Skanconst_desc_rtx_hash (const void *ptr)
3031169689Skan{
3032169689Skan  const struct constant_descriptor_rtx *desc = ptr;
3033169689Skan  return desc->hash;
3034169689Skan}
303518334Speter
3036169689Skanstatic int
3037169689Skanconst_desc_rtx_eq (const void *a, const void *b)
303818334Speter{
3039169689Skan  const struct constant_descriptor_rtx *x = a;
3040169689Skan  const struct constant_descriptor_rtx *y = b;
304118334Speter
3042169689Skan  if (x->mode != y->mode)
3043169689Skan    return 0;
3044169689Skan  return rtx_equal_p (x->constant, y->constant);
304518334Speter}
304618334Speter
3047169689Skan/* This is the worker function for const_rtx_hash, called via for_each_rtx.  */
304818334Speter
3049169689Skanstatic int
3050169689Skanconst_rtx_hash_1 (rtx *xp, void *data)
305118334Speter{
3052169689Skan  unsigned HOST_WIDE_INT hwi;
3053169689Skan  enum machine_mode mode;
3054169689Skan  enum rtx_code code;
3055169689Skan  hashval_t h, *hp;
3056169689Skan  rtx x;
305718334Speter
3058169689Skan  x = *xp;
3059169689Skan  code = GET_CODE (x);
3060169689Skan  mode = GET_MODE (x);
3061169689Skan  h = (hashval_t) code * 1048573 + mode;
306218334Speter
3063169689Skan  switch (code)
306418334Speter    {
3065169689Skan    case CONST_INT:
3066169689Skan      hwi = INTVAL (x);
3067169689Skan    fold_hwi:
3068169689Skan      {
3069169689Skan	const int shift = sizeof (hashval_t) * CHAR_BIT;
3070169689Skan	const int n = sizeof (HOST_WIDE_INT) / sizeof (hashval_t);
3071169689Skan	int i;
3072169689Skan
3073169689Skan	h ^= (hashval_t) hwi;
3074169689Skan	for (i = 1; i < n; ++i)
3075169689Skan	  {
3076169689Skan	    hwi >>= shift;
3077169689Skan	    h ^= (hashval_t) hwi;
3078169689Skan	  }
3079169689Skan      }
3080169689Skan      break;
3081169689Skan
308218334Speter    case CONST_DOUBLE:
3083169689Skan      if (mode == VOIDmode)
308418334Speter	{
3085169689Skan	  hwi = CONST_DOUBLE_LOW (x) ^ CONST_DOUBLE_HIGH (x);
3086169689Skan	  goto fold_hwi;
308718334Speter	}
308818334Speter      else
3089169689Skan	h ^= real_hash (CONST_DOUBLE_REAL_VALUE (x));
309018334Speter      break;
309118334Speter
309296263Sobrien    case CONST_VECTOR:
309396263Sobrien      {
3094169689Skan	int i;
3095169689Skan	for (i = XVECLEN (x, 0); i-- > 0; )
3096169689Skan	  h = h * 251 + const_rtx_hash_1 (&XVECEXP (x, 0, i), data);
309796263Sobrien      }
309896263Sobrien      break;
309996263Sobrien
3100169689Skan    case SYMBOL_REF:
3101169689Skan      h ^= htab_hash_string (XSTR (x, 0));
310218334Speter      break;
310318334Speter
310418334Speter    case LABEL_REF:
3105169689Skan      h = h * 251 + CODE_LABEL_NUMBER (XEXP (x, 0));
310618334Speter      break;
310718334Speter
3108169689Skan    case UNSPEC:
3109169689Skan    case UNSPEC_VOLATILE:
3110169689Skan      h = h * 251 + XINT (x, 1);
311118334Speter      break;
311218334Speter
311318334Speter    default:
311496263Sobrien      break;
311518334Speter    }
311618334Speter
3117169689Skan  hp = data;
3118169689Skan  *hp = *hp * 509 + h;
3119169689Skan  return 0;
312018334Speter}
312118334Speter
3122169689Skan/* Compute a hash value for X, which should be a constant.  */
312318334Speter
3124169689Skanstatic hashval_t
3125169689Skanconst_rtx_hash (rtx x)
312618334Speter{
3127169689Skan  hashval_t h = 0;
3128169689Skan  for_each_rtx (&x, const_rtx_hash_1, &h);
3129169689Skan  return h;
313018334Speter}
313118334Speter
3132169689Skan
3133169689Skan/* Create and return a new rtx constant pool.  */
313418334Speter
3135169689Skanstatic struct rtx_constant_pool *
3136169689Skancreate_constant_pool (void)
313718334Speter{
3138169689Skan  struct rtx_constant_pool *pool;
3139117395Skan
3140169689Skan  pool = ggc_alloc (sizeof (struct rtx_constant_pool));
3141169689Skan  pool->const_rtx_htab = htab_create_ggc (31, const_desc_rtx_hash,
3142169689Skan					  const_desc_rtx_eq, NULL);
3143169689Skan  pool->first = NULL;
3144169689Skan  pool->last = NULL;
3145169689Skan  pool->offset = 0;
3146169689Skan  return pool;
314718334Speter}
314818334Speter
3149169689Skan/* Initialize constant pool hashing for a new function.  */
315018334Speter
3151169689Skanvoid
3152169689Skaninit_varasm_status (struct function *f)
315318334Speter{
3154169689Skan  struct varasm_status *p;
315518334Speter
3156169689Skan  p = ggc_alloc (sizeof (struct varasm_status));
3157169689Skan  f->varasm = p;
315818334Speter
3159169689Skan  p->pool = create_constant_pool ();
3160169689Skan  p->deferred_constants = 0;
316118334Speter}
3162169689Skan
3163169689Skan/* Given a MINUS expression, simplify it if both sides
3164169689Skan   include the same symbol.  */
316518334Speter
3166169689Skanrtx
3167169689Skansimplify_subtraction (rtx x)
316818334Speter{
3169169689Skan  rtx r = simplify_rtx (x);
3170169689Skan  return r ? r : x;
317190075Sobrien}
317290075Sobrien
317318334Speter/* Given a constant rtx X, make (or find) a memory constant for its value
317418334Speter   and return a MEM rtx to refer to it in memory.  */
317518334Speter
317618334Speterrtx
3177132718Skanforce_const_mem (enum machine_mode mode, rtx x)
317818334Speter{
3179169689Skan  struct constant_descriptor_rtx *desc, tmp;
3180169689Skan  struct rtx_constant_pool *pool;
318118334Speter  char label[256];
3182132718Skan  rtx def, symbol;
3183169689Skan  hashval_t hash;
318490075Sobrien  unsigned int align;
3185169689Skan  void **slot;
318618334Speter
3187117395Skan  /* If we're not allowed to drop X into the constant pool, don't.  */
3188169689Skan  if (targetm.cannot_force_const_mem (x))
3189117395Skan    return NULL_RTX;
3190117395Skan
3191169689Skan  /* Record that this function has used a constant pool entry.  */
3192169689Skan  current_function_uses_const_pool = 1;
319318334Speter
3194169689Skan  /* Decide which pool to use.  */
3195169689Skan  pool = (targetm.use_blocks_for_constant_p (mode, x)
3196169689Skan	  ? shared_constant_pool
3197169689Skan	  : cfun->varasm->pool);
3198117395Skan
3199169689Skan  /* Lookup the value in the hashtable.  */
3200169689Skan  tmp.constant = x;
3201169689Skan  tmp.mode = mode;
3202169689Skan  hash = const_rtx_hash (x);
3203169689Skan  slot = htab_find_slot_with_hash (pool->const_rtx_htab, &tmp, hash, INSERT);
3204169689Skan  desc = *slot;
3205169689Skan
3206169689Skan  /* If the constant was already present, return its memory.  */
3207169689Skan  if (desc)
3208169689Skan    return copy_rtx (desc->mem);
3209169689Skan
3210169689Skan  /* Otherwise, create a new descriptor.  */
3211169689Skan  desc = ggc_alloc (sizeof (*desc));
3212169689Skan  *slot = desc;
3213169689Skan
321490075Sobrien  /* Align the location counter as required by EXP's data type.  */
321590075Sobrien  align = GET_MODE_ALIGNMENT (mode == VOIDmode ? word_mode : mode);
321650397Sobrien#ifdef CONSTANT_ALIGNMENT
3217132718Skan  {
3218169689Skan    tree type = lang_hooks.types.type_for_mode (mode, 0);
3219132718Skan    if (type != NULL_TREE)
3220132718Skan      align = CONSTANT_ALIGNMENT (make_tree (type, x), align);
3221132718Skan  }
322250397Sobrien#endif
322318334Speter
3224169689Skan  pool->offset += (align / BITS_PER_UNIT) - 1;
3225169689Skan  pool->offset &= ~ ((align / BITS_PER_UNIT) - 1);
322618334Speter
3227169689Skan  desc->next = NULL;
3228169689Skan  desc->constant = tmp.constant;
3229169689Skan  desc->offset = pool->offset;
3230169689Skan  desc->hash = hash;
3231169689Skan  desc->mode = mode;
3232169689Skan  desc->align = align;
3233169689Skan  desc->labelno = const_labelno;
3234169689Skan  desc->mark = 0;
323518334Speter
3236169689Skan  pool->offset += GET_MODE_SIZE (mode);
3237169689Skan  if (pool->last)
3238169689Skan    pool->last->next = desc;
323990075Sobrien  else
3240169689Skan    pool->first = pool->last = desc;
3241169689Skan  pool->last = desc;
3242117395Skan
324390075Sobrien  /* Create a string containing the label name, in LABEL.  */
324490075Sobrien  ASM_GENERATE_INTERNAL_LABEL (label, "LC", const_labelno);
324590075Sobrien  ++const_labelno;
324674722Sobrien
3247169689Skan  /* Construct the SYMBOL_REF.  Make sure to mark it as belonging to
3248169689Skan     the constants pool.  */
3249169689Skan  if (use_object_blocks_p () && targetm.use_blocks_for_constant_p (mode, x))
3250169689Skan    {
3251169689Skan      section *sect = targetm.asm_out.select_rtx_section (mode, x, align);
3252169689Skan      symbol = create_block_symbol (ggc_strdup (label),
3253169689Skan				    get_block_for_section (sect), -1);
3254169689Skan    }
3255169689Skan  else
3256169689Skan    symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3257169689Skan  desc->sym = symbol;
3258169689Skan  SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3259169689Skan  CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3260169689Skan  SET_SYMBOL_REF_CONSTANT (symbol, desc);
326118334Speter
3262169689Skan  /* Construct the MEM.  */
3263169689Skan  desc->mem = def = gen_const_mem (mode, symbol);
3264169689Skan  set_mem_attributes (def, lang_hooks.types.type_for_mode (mode, 0), 1);
3265169689Skan  set_mem_align (def, align);
3266132718Skan
3267169689Skan  /* If we're dropping a label to the constant pool, make sure we
3268169689Skan     don't delete it.  */
3269169689Skan  if (GET_CODE (x) == LABEL_REF)
3270169689Skan    LABEL_PRESERVE_P (XEXP (x, 0)) = 1;
327118334Speter
3272132718Skan  return copy_rtx (def);
327318334Speter}
327418334Speter
327518334Speter/* Given a constant pool SYMBOL_REF, return the corresponding constant.  */
327618334Speter
327718334Speterrtx
3278132718Skanget_pool_constant (rtx addr)
327918334Speter{
3280169689Skan  return SYMBOL_REF_CONSTANT (addr)->constant;
328118334Speter}
328218334Speter
328396263Sobrien/* Given a constant pool SYMBOL_REF, return the corresponding constant
328496263Sobrien   and whether it has been output or not.  */
328596263Sobrien
328696263Sobrienrtx
3287132718Skanget_pool_constant_mark (rtx addr, bool *pmarked)
328896263Sobrien{
3289169689Skan  struct constant_descriptor_rtx *desc;
329096263Sobrien
3291169689Skan  desc = SYMBOL_REF_CONSTANT (addr);
3292169689Skan  *pmarked = (desc->mark != 0);
3293169689Skan  return desc->constant;
329490075Sobrien}
329590075Sobrien
329618334Speter/* Similar, return the mode.  */
329718334Speter
329818334Speterenum machine_mode
3299132718Skanget_pool_mode (rtx addr)
330018334Speter{
3301169689Skan  return SYMBOL_REF_CONSTANT (addr)->mode;
330218334Speter}
330318334Speter
330418334Speter/* Return the size of the constant pool.  */
330518334Speter
330618334Speterint
3307132718Skanget_pool_size (void)
330818334Speter{
3309169689Skan  return cfun->varasm->pool->offset;
331018334Speter}
331118334Speter
3312169689Skan/* Worker function for output_constant_pool_1.  Emit assembly for X
3313169689Skan   in MODE with known alignment ALIGN.  */
331418334Speter
3315169689Skanstatic void
3316169689Skanoutput_constant_pool_2 (enum machine_mode mode, rtx x, unsigned int align)
331718334Speter{
3318169689Skan  switch (GET_MODE_CLASS (mode))
3319169689Skan    {
3320169689Skan    case MODE_FLOAT:
3321169689Skan    case MODE_DECIMAL_FLOAT:
3322169689Skan      {
3323169689Skan	REAL_VALUE_TYPE r;
332418334Speter
3325169689Skan	gcc_assert (GET_CODE (x) == CONST_DOUBLE);
3326169689Skan	REAL_VALUE_FROM_CONST_DOUBLE (r, x);
3327169689Skan	assemble_real (r, mode, align);
3328169689Skan	break;
3329169689Skan      }
333050397Sobrien
3331169689Skan    case MODE_INT:
3332169689Skan    case MODE_PARTIAL_INT:
3333169689Skan      assemble_integer (x, GET_MODE_SIZE (mode), align, 1);
3334169689Skan      break;
333518334Speter
3336169689Skan    case MODE_VECTOR_FLOAT:
3337169689Skan    case MODE_VECTOR_INT:
3338169689Skan      {
3339169689Skan	int i, units;
3340169689Skan        enum machine_mode submode = GET_MODE_INNER (mode);
3341169689Skan	unsigned int subalign = MIN (align, GET_MODE_BITSIZE (submode));
334290075Sobrien
3343169689Skan	gcc_assert (GET_CODE (x) == CONST_VECTOR);
3344169689Skan	units = CONST_VECTOR_NUNITS (x);
334518334Speter
3346169689Skan	for (i = 0; i < units; i++)
3347169689Skan	  {
3348169689Skan	    rtx elt = CONST_VECTOR_ELT (x, i);
3349169689Skan	    output_constant_pool_2 (submode, elt, i ? subalign : align);
3350169689Skan	  }
3351169689Skan      }
3352169689Skan      break;
335350397Sobrien
3354169689Skan    default:
3355169689Skan      gcc_unreachable ();
3356169689Skan    }
3357169689Skan}
335818334Speter
3359169689Skan/* Worker function for output_constant_pool.  Emit constant DESC,
3360169689Skan   giving it ALIGN bits of alignment.  */
336190075Sobrien
3362169689Skanstatic void
3363169689Skanoutput_constant_pool_1 (struct constant_descriptor_rtx *desc,
3364169689Skan			unsigned int align)
3365169689Skan{
3366169689Skan  rtx x, tmp;
336790075Sobrien
3368169689Skan  x = desc->constant;
336990075Sobrien
3370169689Skan  /* See if X is a LABEL_REF (or a CONST referring to a LABEL_REF)
3371169689Skan     whose CODE_LABEL has been deleted.  This can occur if a jump table
3372169689Skan     is eliminated by optimization.  If so, write a constant of zero
3373169689Skan     instead.  Note that this can also happen by turning the
3374169689Skan     CODE_LABEL into a NOTE.  */
3375169689Skan  /* ??? This seems completely and utterly wrong.  Certainly it's
3376169689Skan     not true for NOTE_INSN_DELETED_LABEL, but I disbelieve proper
3377169689Skan     functioning even with INSN_DELETED_P and friends.  */
337818334Speter
3379169689Skan  tmp = x;
3380169689Skan  switch (GET_CODE (x))
3381169689Skan    {
3382169689Skan    case CONST:
3383169689Skan      if (GET_CODE (XEXP (x, 0)) != PLUS
3384169689Skan	  || GET_CODE (XEXP (XEXP (x, 0), 0)) != LABEL_REF)
3385169689Skan	break;
3386169689Skan      tmp = XEXP (XEXP (x, 0), 0);
3387169689Skan      /* FALLTHRU  */
3388169689Skan
3389169689Skan    case LABEL_REF:
3390169689Skan      tmp = XEXP (x, 0);
3391169689Skan      gcc_assert (!INSN_DELETED_P (tmp));
3392169689Skan      gcc_assert (!NOTE_P (tmp)
3393169689Skan		  || NOTE_LINE_NUMBER (tmp) != NOTE_INSN_DELETED);
3394169689Skan      break;
3395169689Skan
3396169689Skan    default:
3397169689Skan      break;
3398169689Skan    }
3399169689Skan
340018334Speter#ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3401169689Skan  ASM_OUTPUT_SPECIAL_POOL_ENTRY (asm_out_file, x, desc->mode,
3402169689Skan				 align, desc->labelno, done);
340318334Speter#endif
340418334Speter
3405169689Skan  assemble_align (align);
340618334Speter
3407169689Skan  /* Output the label.  */
3408169689Skan  targetm.asm_out.internal_label (asm_out_file, "LC", desc->labelno);
340918334Speter
3410169689Skan  /* Output the data.  */
3411169689Skan  output_constant_pool_2 (desc->mode, x, align);
341218334Speter
3413169689Skan  /* Make sure all constants in SECTION_MERGE and not SECTION_STRINGS
3414169689Skan     sections have proper size.  */
3415169689Skan  if (align > GET_MODE_BITSIZE (desc->mode)
3416169689Skan      && in_section
3417169689Skan      && (in_section->common.flags & SECTION_MERGE))
3418169689Skan    assemble_align (align);
341918334Speter
3420169689Skan#ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3421169689Skan done:
3422169689Skan#endif
3423169689Skan  return;
3424169689Skan}
342518334Speter
3426169689Skan/* Given a SYMBOL_REF CURRENT_RTX, mark it and all constants it refers
3427169689Skan   to as used.  Emit referenced deferred strings.  This function can
3428169689Skan   be used with for_each_rtx to mark all SYMBOL_REFs in an rtx.  */
342996263Sobrien
3430169689Skanstatic int
3431169689Skanmark_constant (rtx *current_rtx, void *data ATTRIBUTE_UNUSED)
3432169689Skan{
3433169689Skan  rtx x = *current_rtx;
343496263Sobrien
3435169689Skan  if (x == NULL_RTX || GET_CODE (x) != SYMBOL_REF)
3436169689Skan    return 0;
343796263Sobrien
3438169689Skan  if (CONSTANT_POOL_ADDRESS_P (x))
3439169689Skan    {
3440169689Skan      struct constant_descriptor_rtx *desc = SYMBOL_REF_CONSTANT (x);
3441169689Skan      if (desc->mark == 0)
3442169689Skan	{
3443169689Skan	  desc->mark = 1;
3444169689Skan	  for_each_rtx (&desc->constant, mark_constant, NULL);
3445169689Skan	}
3446169689Skan    }
3447169689Skan  else if (TREE_CONSTANT_POOL_ADDRESS_P (x))
3448169689Skan    {
3449169689Skan      tree exp = SYMBOL_REF_DECL (x);
3450169689Skan      if (!TREE_ASM_WRITTEN (exp))
3451169689Skan	{
3452169689Skan	  n_deferred_constants--;
3453169689Skan	  output_constant_def_contents (x);
3454169689Skan	}
3455169689Skan    }
345696263Sobrien
3457169689Skan  return -1;
3458169689Skan}
345996263Sobrien
3460169689Skan/* Look through appropriate parts of INSN, marking all entries in the
3461169689Skan   constant pool which are actually being used.  Entries that are only
3462169689Skan   referenced by other constants are also marked as used.  Emit
3463169689Skan   deferred strings that are used.  */
346496263Sobrien
3465169689Skanstatic void
3466169689Skanmark_constants (rtx insn)
3467169689Skan{
3468169689Skan  if (!INSN_P (insn))
3469169689Skan    return;
347096263Sobrien
3471169689Skan  /* Insns may appear inside a SEQUENCE.  Only check the patterns of
3472169689Skan     insns, not any notes that may be attached.  We don't want to mark
3473169689Skan     a constant just because it happens to appear in a REG_EQUIV note.  */
3474169689Skan  if (GET_CODE (PATTERN (insn)) == SEQUENCE)
3475169689Skan    {
3476169689Skan      rtx seq = PATTERN (insn);
3477169689Skan      int i, n = XVECLEN (seq, 0);
3478169689Skan      for (i = 0; i < n; ++i)
3479169689Skan	{
3480169689Skan	  rtx subinsn = XVECEXP (seq, 0, i);
3481169689Skan	  if (INSN_P (subinsn))
3482169689Skan	    for_each_rtx (&PATTERN (subinsn), mark_constant, NULL);
348318334Speter	}
348418334Speter    }
3485169689Skan  else
3486169689Skan    for_each_rtx (&PATTERN (insn), mark_constant, NULL);
348718334Speter}
348850397Sobrien
348950397Sobrien/* Look through the instructions for this function, and mark all the
3490169689Skan   entries in POOL which are actually being used.  Emit deferred constants
3491169689Skan   which have indeed been used.  */
349250397Sobrien
349350397Sobrienstatic void
3494132718Skanmark_constant_pool (void)
349550397Sobrien{
3496169689Skan  rtx insn, link;
349750397Sobrien
3498169689Skan  if (!current_function_uses_const_pool && n_deferred_constants == 0)
349950397Sobrien    return;
350050397Sobrien
350150397Sobrien  for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3502169689Skan    mark_constants (insn);
350350397Sobrien
3504117395Skan  for (link = current_function_epilogue_delay_list;
3505117395Skan       link;
3506117395Skan       link = XEXP (link, 1))
3507169689Skan    mark_constants (XEXP (link, 0));
350890075Sobrien}
350952284Sobrien
3510169689Skan/* Write all the constants in POOL.  */
351152284Sobrien
351250397Sobrienstatic void
3513169689Skanoutput_constant_pool_contents (struct rtx_constant_pool *pool)
351450397Sobrien{
3515169689Skan  struct constant_descriptor_rtx *desc;
351650397Sobrien
3517169689Skan  for (desc = pool->first; desc ; desc = desc->next)
3518169689Skan    if (desc->mark)
3519169689Skan      {
3520169689Skan	/* If the constant is part of an object_block, make sure that
3521169689Skan	   the constant has been positioned within its block, but do not
3522169689Skan	   write out its definition yet.  output_object_blocks will do
3523169689Skan	   that later.  */
3524169689Skan	if (SYMBOL_REF_HAS_BLOCK_INFO_P (desc->sym)
3525169689Skan	    && SYMBOL_REF_BLOCK (desc->sym))
3526169689Skan	  place_block_symbol (desc->sym);
3527169689Skan	else
3528169689Skan	  {
3529169689Skan	    switch_to_section (targetm.asm_out.select_rtx_section
3530169689Skan			       (desc->mode, desc->constant, desc->align));
3531169689Skan	    output_constant_pool_1 (desc, desc->align);
3532169689Skan	  }
3533169689Skan      }
3534169689Skan}
353550397Sobrien
3536169689Skan/* Mark all constants that are used in the current function, then write
3537169689Skan   out the function's private constant pool.  */
353850397Sobrien
3539169689Skanstatic void
3540169689Skanoutput_constant_pool (const char *fnname ATTRIBUTE_UNUSED,
3541169689Skan		      tree fndecl ATTRIBUTE_UNUSED)
3542169689Skan{
3543169689Skan  struct rtx_constant_pool *pool = cfun->varasm->pool;
354450397Sobrien
3545169689Skan  /* It is possible for gcc to call force_const_mem and then to later
3546169689Skan     discard the instructions which refer to the constant.  In such a
3547169689Skan     case we do not need to output the constant.  */
3548169689Skan  mark_constant_pool ();
354950397Sobrien
3550169689Skan#ifdef ASM_OUTPUT_POOL_PROLOGUE
3551169689Skan  ASM_OUTPUT_POOL_PROLOGUE (asm_out_file, fnname, fndecl, pool->offset);
3552169689Skan#endif
355350397Sobrien
3554169689Skan  output_constant_pool_contents (pool);
355550397Sobrien
3556169689Skan#ifdef ASM_OUTPUT_POOL_EPILOGUE
3557169689Skan  ASM_OUTPUT_POOL_EPILOGUE (asm_out_file, fnname, fndecl, pool->offset);
3558169689Skan#endif
355950397Sobrien}
3560169689Skan
3561169689Skan/* Write the contents of the shared constant pool.  */
356290075Sobrien
3563169689Skanvoid
3564169689Skanoutput_shared_constant_pool (void)
356590075Sobrien{
3566169689Skan  output_constant_pool_contents (shared_constant_pool);
356790075Sobrien}
356818334Speter
3569132718Skan/* Determine what kind of relocations EXP may need.  */
357018334Speter
3571132718Skanint
3572132718Skancompute_reloc_for_constant (tree exp)
357318334Speter{
3574117395Skan  int reloc = 0, reloc2;
357590075Sobrien  tree tem;
357618334Speter
357790075Sobrien  /* Give the front-end a chance to convert VALUE to something that
357890075Sobrien     looks more like a constant to the back-end.  */
3579169689Skan  exp = lang_hooks.expand_constant (exp);
358090075Sobrien
358118334Speter  switch (TREE_CODE (exp))
358218334Speter    {
358318334Speter    case ADDR_EXPR:
3584102780Skan    case FDESC_EXPR:
358590075Sobrien      /* Go inside any operations that get_inner_reference can handle and see
358690075Sobrien	 if what's inside is a constant: no need to do anything here for
358790075Sobrien	 addresses of variables or functions.  */
358890075Sobrien      for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
358990075Sobrien	   tem = TREE_OPERAND (tem, 0))
359090075Sobrien	;
359118334Speter
359290075Sobrien      if (TREE_PUBLIC (tem))
359390075Sobrien	reloc |= 2;
359490075Sobrien      else
359590075Sobrien	reloc |= 1;
359618334Speter      break;
359718334Speter
359818334Speter    case PLUS_EXPR:
3599132718Skan      reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3600132718Skan      reloc |= compute_reloc_for_constant (TREE_OPERAND (exp, 1));
360118334Speter      break;
360218334Speter
3603117395Skan    case MINUS_EXPR:
3604132718Skan      reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3605132718Skan      reloc2 = compute_reloc_for_constant (TREE_OPERAND (exp, 1));
3606117395Skan      /* The difference of two local labels is computable at link time.  */
3607117395Skan      if (reloc == 1 && reloc2 == 1)
3608117395Skan	reloc = 0;
3609117395Skan      else
3610117395Skan	reloc |= reloc2;
3611117395Skan      break;
3612117395Skan
361318334Speter    case NOP_EXPR:
361418334Speter    case CONVERT_EXPR:
361518334Speter    case NON_LVALUE_EXPR:
3616169689Skan    case VIEW_CONVERT_EXPR:
3617132718Skan      reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
361818334Speter      break;
361918334Speter
362018334Speter    case CONSTRUCTOR:
3621169689Skan      {
3622169689Skan	unsigned HOST_WIDE_INT idx;
3623169689Skan	FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
3624169689Skan	  if (tem != 0)
3625169689Skan	    reloc |= compute_reloc_for_constant (tem);
3626169689Skan      }
362718334Speter      break;
362818334Speter
362950397Sobrien    default:
363018334Speter      break;
363118334Speter    }
363218334Speter  return reloc;
363318334Speter}
3634132718Skan
3635132718Skan/* Find all the constants whose addresses are referenced inside of EXP,
3636132718Skan   and make sure assembler code with a label has been output for each one.
3637132718Skan   Indicate whether an ADDR_EXPR has been encountered.  */
3638132718Skan
3639132718Skanstatic void
3640132718Skanoutput_addressed_constants (tree exp)
3641132718Skan{
3642132718Skan  tree tem;
3643132718Skan
3644132718Skan  /* Give the front-end a chance to convert VALUE to something that
3645132718Skan     looks more like a constant to the back-end.  */
3646169689Skan  exp = lang_hooks.expand_constant (exp);
3647132718Skan
3648132718Skan  switch (TREE_CODE (exp))
3649132718Skan    {
3650132718Skan    case ADDR_EXPR:
3651132718Skan    case FDESC_EXPR:
3652132718Skan      /* Go inside any operations that get_inner_reference can handle and see
3653132718Skan	 if what's inside is a constant: no need to do anything here for
3654132718Skan	 addresses of variables or functions.  */
3655132718Skan      for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
3656132718Skan	   tem = TREE_OPERAND (tem, 0))
3657132718Skan	;
3658132718Skan
3659169689Skan      /* If we have an initialized CONST_DECL, retrieve the initializer.  */
3660169689Skan      if (TREE_CODE (tem) == CONST_DECL && DECL_INITIAL (tem))
3661169689Skan	tem = DECL_INITIAL (tem);
3662169689Skan
3663169689Skan      if (CONSTANT_CLASS_P (tem) || TREE_CODE (tem) == CONSTRUCTOR)
3664132718Skan	output_constant_def (tem, 0);
3665132718Skan      break;
3666132718Skan
3667132718Skan    case PLUS_EXPR:
3668132718Skan    case MINUS_EXPR:
3669132718Skan      output_addressed_constants (TREE_OPERAND (exp, 1));
3670132718Skan      /* Fall through.  */
3671132718Skan
3672132718Skan    case NOP_EXPR:
3673132718Skan    case CONVERT_EXPR:
3674132718Skan    case NON_LVALUE_EXPR:
3675169689Skan    case VIEW_CONVERT_EXPR:
3676132718Skan      output_addressed_constants (TREE_OPERAND (exp, 0));
3677132718Skan      break;
3678132718Skan
3679132718Skan    case CONSTRUCTOR:
3680169689Skan      {
3681169689Skan	unsigned HOST_WIDE_INT idx;
3682169689Skan	FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
3683169689Skan	  if (tem != 0)
3684169689Skan	    output_addressed_constants (tem);
3685169689Skan      }
3686132718Skan      break;
3687132718Skan
3688132718Skan    default:
3689132718Skan      break;
3690132718Skan    }
3691132718Skan}
369218334Speter
3693169689Skan/* Whether a constructor CTOR is a valid static constant initializer if all
3694169689Skan   its elements are.  This used to be internal to initializer_constant_valid_p
3695169689Skan   and has been exposed to let other functions like categorize_ctor_elements
3696169689Skan   evaluate the property while walking a constructor for other purposes.  */
3697169689Skan
3698169689Skanbool
3699169689Skanconstructor_static_from_elts_p (tree ctor)
3700169689Skan{
3701169689Skan  return (TREE_CONSTANT (ctor)
3702169689Skan	  && (TREE_CODE (TREE_TYPE (ctor)) == UNION_TYPE
3703169689Skan	      || TREE_CODE (TREE_TYPE (ctor)) == RECORD_TYPE)
3704169689Skan	  && !VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (ctor)));
3705169689Skan}
3706169689Skan
370790075Sobrien/* Return nonzero if VALUE is a valid constant-valued expression
370890075Sobrien   for use in initializing a static variable; one that can be an
370990075Sobrien   element of a "constant" initializer.
371090075Sobrien
371190075Sobrien   Return null_pointer_node if the value is absolute;
371290075Sobrien   if it is relocatable, return the variable that determines the relocation.
371390075Sobrien   We assume that VALUE has been folded as much as possible;
371490075Sobrien   therefore, we do not need to check for such things as
371590075Sobrien   arithmetic-combinations of integers.  */
371690075Sobrien
371790075Sobrientree
3718132718Skaninitializer_constant_valid_p (tree value, tree endtype)
371990075Sobrien{
372090075Sobrien  /* Give the front-end a chance to convert VALUE to something that
372190075Sobrien     looks more like a constant to the back-end.  */
3722169689Skan  value = lang_hooks.expand_constant (value);
372390075Sobrien
372490075Sobrien  switch (TREE_CODE (value))
372590075Sobrien    {
372690075Sobrien    case CONSTRUCTOR:
3727169689Skan      if (constructor_static_from_elts_p (value))
3728122180Skan	{
3729169689Skan	  unsigned HOST_WIDE_INT idx;
3730122180Skan	  tree elt;
3731122180Skan	  bool absolute = true;
373290075Sobrien
3733169689Skan	  FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
3734122180Skan	    {
3735122180Skan	      tree reloc;
3736169689Skan	      reloc = initializer_constant_valid_p (elt, TREE_TYPE (elt));
3737122180Skan	      if (!reloc)
3738122180Skan		return NULL_TREE;
3739122180Skan	      if (reloc != null_pointer_node)
3740122180Skan		absolute = false;
3741122180Skan	    }
3742122180Skan	  /* For a non-absolute relocation, there is no single
3743122180Skan	     variable that can be "the variable that determines the
3744122180Skan	     relocation."  */
3745122180Skan	  return absolute ? null_pointer_node : error_mark_node;
3746122180Skan	}
374790075Sobrien
3748122180Skan      return TREE_STATIC (value) ? null_pointer_node : NULL_TREE;
3749122180Skan
375090075Sobrien    case INTEGER_CST:
375196263Sobrien    case VECTOR_CST:
375290075Sobrien    case REAL_CST:
375390075Sobrien    case STRING_CST:
375490075Sobrien    case COMPLEX_CST:
375590075Sobrien      return null_pointer_node;
375690075Sobrien
375790075Sobrien    case ADDR_EXPR:
375890075Sobrien    case FDESC_EXPR:
3759169689Skan      value = staticp (TREE_OPERAND (value, 0));
3760169689Skan      if (value)
3761169689Skan	{
3762169689Skan	  /* "&(*a).f" is like unto pointer arithmetic.  If "a" turns out to
3763169689Skan	     be a constant, this is old-skool offsetof-like nonsense.  */
3764169689Skan	  if (TREE_CODE (value) == INDIRECT_REF
3765169689Skan	      && TREE_CONSTANT (TREE_OPERAND (value, 0)))
3766169689Skan	    return null_pointer_node;
3767169689Skan	  /* Taking the address of a nested function involves a trampoline.  */
3768169689Skan	  if (TREE_CODE (value) == FUNCTION_DECL
3769169689Skan	      && ((decl_function_context (value)
3770169689Skan		   && !DECL_NO_STATIC_CHAIN (value))
3771169689Skan		  || DECL_DLLIMPORT_P (value)))
3772169689Skan	    return NULL_TREE;
3773169689Skan	  /* "&{...}" requires a temporary to hold the constructed
3774169689Skan	     object.  */
3775169689Skan	  if (TREE_CODE (value) == CONSTRUCTOR)
3776169689Skan	    return NULL_TREE;
3777169689Skan	}
3778169689Skan      return value;
377990075Sobrien
378090075Sobrien    case VIEW_CONVERT_EXPR:
378190075Sobrien    case NON_LVALUE_EXPR:
378290075Sobrien      return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
378390075Sobrien
378490075Sobrien    case CONVERT_EXPR:
378590075Sobrien    case NOP_EXPR:
3786146895Skan      {
3787146895Skan	tree src;
3788146895Skan	tree src_type;
3789146895Skan	tree dest_type;
379090075Sobrien
3791146895Skan	src = TREE_OPERAND (value, 0);
3792146895Skan	src_type = TREE_TYPE (src);
3793146895Skan	dest_type = TREE_TYPE (value);
379490075Sobrien
3795146895Skan	/* Allow conversions between pointer types, floating-point
3796146895Skan	   types, and offset types.  */
3797146895Skan	if ((POINTER_TYPE_P (dest_type) && POINTER_TYPE_P (src_type))
3798146895Skan	    || (FLOAT_TYPE_P (dest_type) && FLOAT_TYPE_P (src_type))
3799146895Skan	    || (TREE_CODE (dest_type) == OFFSET_TYPE
3800146895Skan		&& TREE_CODE (src_type) == OFFSET_TYPE))
3801146895Skan	  return initializer_constant_valid_p (src, endtype);
380290075Sobrien
3803146895Skan	/* Allow length-preserving conversions between integer types.  */
3804146895Skan	if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type)
3805146895Skan	    && (TYPE_PRECISION (dest_type) == TYPE_PRECISION (src_type)))
3806146895Skan	  return initializer_constant_valid_p (src, endtype);
380790075Sobrien
3808146895Skan	/* Allow conversions between other integer types only if
3809146895Skan	   explicit value.  */
3810146895Skan	if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type))
3811146895Skan	  {
3812146895Skan	    tree inner = initializer_constant_valid_p (src, endtype);
3813146895Skan	    if (inner == null_pointer_node)
3814146895Skan	      return null_pointer_node;
3815146895Skan	    break;
3816146895Skan	  }
381790075Sobrien
3818146895Skan	/* Allow (int) &foo provided int is as wide as a pointer.  */
3819146895Skan	if (INTEGRAL_TYPE_P (dest_type) && POINTER_TYPE_P (src_type)
3820146895Skan	    && (TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type)))
3821146895Skan	  return initializer_constant_valid_p (src, endtype);
382290075Sobrien
3823146895Skan	/* Likewise conversions from int to pointers, but also allow
3824146895Skan	   conversions from 0.  */
3825146895Skan	if ((POINTER_TYPE_P (dest_type)
3826146895Skan	     || TREE_CODE (dest_type) == OFFSET_TYPE)
3827146895Skan	    && INTEGRAL_TYPE_P (src_type))
3828146895Skan	  {
3829169689Skan	    if (TREE_CODE (src) == INTEGER_CST
3830169689Skan		&& TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type))
3831169689Skan	      return null_pointer_node;
3832146895Skan	    if (integer_zerop (src))
3833146895Skan	      return null_pointer_node;
3834146895Skan	    else if (TYPE_PRECISION (dest_type) <= TYPE_PRECISION (src_type))
3835146895Skan	      return initializer_constant_valid_p (src, endtype);
3836146895Skan	  }
3837146895Skan
3838146895Skan	/* Allow conversions to struct or union types if the value
3839146895Skan	   inside is okay.  */
3840146895Skan	if (TREE_CODE (dest_type) == RECORD_TYPE
3841146895Skan	    || TREE_CODE (dest_type) == UNION_TYPE)
3842146895Skan	  return initializer_constant_valid_p (src, endtype);
3843146895Skan      }
384490075Sobrien      break;
384590075Sobrien
384690075Sobrien    case PLUS_EXPR:
384790075Sobrien      if (! INTEGRAL_TYPE_P (endtype)
384890075Sobrien	  || TYPE_PRECISION (endtype) >= POINTER_SIZE)
3849117395Skan	{
385090075Sobrien	  tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
385190075Sobrien						      endtype);
385290075Sobrien	  tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
385390075Sobrien						      endtype);
385490075Sobrien	  /* If either term is absolute, use the other terms relocation.  */
385590075Sobrien	  if (valid0 == null_pointer_node)
385690075Sobrien	    return valid1;
385790075Sobrien	  if (valid1 == null_pointer_node)
385890075Sobrien	    return valid0;
3859117395Skan	}
386090075Sobrien      break;
386190075Sobrien
386290075Sobrien    case MINUS_EXPR:
386390075Sobrien      if (! INTEGRAL_TYPE_P (endtype)
386490075Sobrien	  || TYPE_PRECISION (endtype) >= POINTER_SIZE)
386590075Sobrien	{
386690075Sobrien	  tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
386790075Sobrien						      endtype);
386890075Sobrien	  tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
386990075Sobrien						      endtype);
387090075Sobrien	  /* Win if second argument is absolute.  */
387190075Sobrien	  if (valid1 == null_pointer_node)
387290075Sobrien	    return valid0;
387390075Sobrien	  /* Win if both arguments have the same relocation.
387490075Sobrien	     Then the value is absolute.  */
387590075Sobrien	  if (valid0 == valid1 && valid0 != 0)
387690075Sobrien	    return null_pointer_node;
387790075Sobrien
387890075Sobrien	  /* Since GCC guarantees that string constants are unique in the
387990075Sobrien	     generated code, a subtraction between two copies of the same
388090075Sobrien	     constant string is absolute.  */
3881169689Skan	  if (valid0 && TREE_CODE (valid0) == STRING_CST
3882169689Skan	      && valid1 && TREE_CODE (valid1) == STRING_CST
3883169689Skan	      && operand_equal_p (valid0, valid1, 1))
388490075Sobrien	    return null_pointer_node;
388590075Sobrien	}
388690075Sobrien
3887169689Skan      /* Support narrowing differences.  */
388890075Sobrien      if (INTEGRAL_TYPE_P (endtype))
388990075Sobrien	{
389090075Sobrien	  tree op0, op1;
3891169689Skan
389290075Sobrien	  op0 = TREE_OPERAND (value, 0);
389390075Sobrien	  op1 = TREE_OPERAND (value, 1);
389490075Sobrien
389596263Sobrien	  /* Like STRIP_NOPS except allow the operand mode to widen.
3896132718Skan	     This works around a feature of fold that simplifies
389796263Sobrien	     (int)(p1 - p2) to ((int)p1 - (int)p2) under the theory
389896263Sobrien	     that the narrower operation is cheaper.  */
389996263Sobrien
390096263Sobrien	  while (TREE_CODE (op0) == NOP_EXPR
390196263Sobrien		 || TREE_CODE (op0) == CONVERT_EXPR
390296263Sobrien		 || TREE_CODE (op0) == NON_LVALUE_EXPR)
390396263Sobrien	    {
390496263Sobrien	      tree inner = TREE_OPERAND (op0, 0);
390596263Sobrien	      if (inner == error_mark_node
390696263Sobrien	          || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
390796263Sobrien		  || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))
390896263Sobrien		      > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
390996263Sobrien		break;
391096263Sobrien	      op0 = inner;
391196263Sobrien	    }
391296263Sobrien
391396263Sobrien	  while (TREE_CODE (op1) == NOP_EXPR
391496263Sobrien		 || TREE_CODE (op1) == CONVERT_EXPR
391596263Sobrien		 || TREE_CODE (op1) == NON_LVALUE_EXPR)
391696263Sobrien	    {
391796263Sobrien	      tree inner = TREE_OPERAND (op1, 0);
391896263Sobrien	      if (inner == error_mark_node
391996263Sobrien	          || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
392096263Sobrien		  || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op1)))
392196263Sobrien		      > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
392296263Sobrien		break;
392396263Sobrien	      op1 = inner;
392496263Sobrien	    }
392596263Sobrien
3926169689Skan	  op0 = initializer_constant_valid_p (op0, endtype);
3927169689Skan	  op1 = initializer_constant_valid_p (op1, endtype);
3928169689Skan
3929169689Skan	  /* Both initializers must be known.  */
3930169689Skan	  if (op0 && op1)
3931169689Skan	    {
3932169689Skan	      if (op0 == op1)
3933169689Skan		return null_pointer_node;
3934169689Skan
3935169689Skan	      /* Support differences between labels.  */
3936169689Skan	      if (TREE_CODE (op0) == LABEL_DECL
3937169689Skan		  && TREE_CODE (op1) == LABEL_DECL)
3938169689Skan		return null_pointer_node;
3939169689Skan
3940169689Skan	      if (TREE_CODE (op0) == STRING_CST
3941169689Skan		  && TREE_CODE (op1) == STRING_CST
3942169689Skan		  && operand_equal_p (op0, op1, 1))
3943169689Skan		return null_pointer_node;
3944169689Skan	    }
394590075Sobrien	}
394690075Sobrien      break;
394790075Sobrien
394890075Sobrien    default:
394990075Sobrien      break;
395090075Sobrien    }
395190075Sobrien
395290075Sobrien  return 0;
395390075Sobrien}
395490075Sobrien
395518334Speter/* Output assembler code for constant EXP to FILE, with no label.
395618334Speter   This includes the pseudo-op such as ".int" or ".byte", and a newline.
395718334Speter   Assumes output_addressed_constants has been done on EXP already.
395818334Speter
395918334Speter   Generate exactly SIZE bytes of assembler data, padding at the end
396018334Speter   with zeros if necessary.  SIZE must always be specified.
396118334Speter
396218334Speter   SIZE is important for structure constructors,
396318334Speter   since trailing members may have been omitted from the constructor.
396418334Speter   It is also important for initialization of arrays from string constants
396518334Speter   since the full length of the string constant might not be wanted.
396618334Speter   It is also needed for initialization of unions, where the initializer's
396718334Speter   type is just one member, and that may not be as long as the union.
396818334Speter
396918334Speter   There a case in which we would fail to output exactly SIZE bytes:
397018334Speter   for a structure constructor that wants to produce more than SIZE bytes.
397190075Sobrien   But such constructors will never be generated for any possible input.
397218334Speter
397390075Sobrien   ALIGN is the alignment of the data in bits.  */
397490075Sobrien
397518334Spetervoid
3976132718Skanoutput_constant (tree exp, unsigned HOST_WIDE_INT size, unsigned int align)
397718334Speter{
397890075Sobrien  enum tree_code code;
3979132718Skan  unsigned HOST_WIDE_INT thissize;
398018334Speter
3981132718Skan  /* Some front-ends use constants other than the standard language-independent
398290075Sobrien     varieties, but which may still be output directly.  Give the front-end a
398390075Sobrien     chance to convert EXP to a language-independent representation.  */
3984169689Skan  exp = lang_hooks.expand_constant (exp);
398552284Sobrien
398652284Sobrien  if (size == 0 || flag_syntax_only)
398718334Speter    return;
398818334Speter
3989169689Skan  /* See if we're trying to initialize a pointer in a non-default mode
3990169689Skan     to the address of some declaration somewhere.  If the target says
3991169689Skan     the mode is valid for pointers, assume the target has a way of
3992169689Skan     resolving it.  */
3993169689Skan  if (TREE_CODE (exp) == NOP_EXPR
3994169689Skan      && POINTER_TYPE_P (TREE_TYPE (exp))
3995169689Skan      && targetm.valid_pointer_mode (TYPE_MODE (TREE_TYPE (exp))))
3996169689Skan    {
3997169689Skan      tree saved_type = TREE_TYPE (exp);
3998169689Skan
3999169689Skan      /* Peel off any intermediate conversions-to-pointer for valid
4000169689Skan	 pointer modes.  */
4001169689Skan      while (TREE_CODE (exp) == NOP_EXPR
4002169689Skan	     && POINTER_TYPE_P (TREE_TYPE (exp))
4003169689Skan	     && targetm.valid_pointer_mode (TYPE_MODE (TREE_TYPE (exp))))
4004169689Skan	exp = TREE_OPERAND (exp, 0);
4005169689Skan
4006169689Skan      /* If what we're left with is the address of something, we can
4007169689Skan	 convert the address to the final type and output it that
4008169689Skan	 way.  */
4009169689Skan      if (TREE_CODE (exp) == ADDR_EXPR)
4010169689Skan	exp = build1 (ADDR_EXPR, saved_type, TREE_OPERAND (exp, 0));
4011169689Skan      /* Likewise for constant ints.  */
4012169689Skan      else if (TREE_CODE (exp) == INTEGER_CST)
4013169689Skan	exp = build_int_cst_wide (saved_type, TREE_INT_CST_LOW (exp),
4014169689Skan				  TREE_INT_CST_HIGH (exp));
4015169689Skan
4016169689Skan    }
4017169689Skan
401890075Sobrien  /* Eliminate any conversions since we'll be outputting the underlying
401990075Sobrien     constant.  */
402090075Sobrien  while (TREE_CODE (exp) == NOP_EXPR || TREE_CODE (exp) == CONVERT_EXPR
402190075Sobrien	 || TREE_CODE (exp) == NON_LVALUE_EXPR
402290075Sobrien	 || TREE_CODE (exp) == VIEW_CONVERT_EXPR)
4023169689Skan    {
4024169689Skan      HOST_WIDE_INT type_size = int_size_in_bytes (TREE_TYPE (exp));
4025169689Skan      HOST_WIDE_INT op_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (exp, 0)));
402618334Speter
4027169689Skan      /* Make sure eliminating the conversion is really a no-op, except with
4028169689Skan	 VIEW_CONVERT_EXPRs to allow for wild Ada unchecked conversions and
4029169689Skan	 union types to allow for Ada unchecked unions.  */
4030169689Skan      if (type_size > op_size
4031169689Skan	  && TREE_CODE (exp) != VIEW_CONVERT_EXPR
4032169689Skan	  && TREE_CODE (TREE_TYPE (exp)) != UNION_TYPE)
4033169689Skan	/* Keep the conversion. */
4034169689Skan	break;
4035169689Skan      else
4036169689Skan	exp = TREE_OPERAND (exp, 0);
4037169689Skan    }
4038169689Skan
403990075Sobrien  code = TREE_CODE (TREE_TYPE (exp));
404090075Sobrien  thissize = int_size_in_bytes (TREE_TYPE (exp));
404190075Sobrien
4042169689Skan  /* Give the front end another chance to expand constants.  */
4043169689Skan  exp = lang_hooks.expand_constant (exp);
4044169689Skan
404518334Speter  /* Allow a constructor with no elements for any data type.
404618334Speter     This means to fill the space with zeros.  */
4047169689Skan  if (TREE_CODE (exp) == CONSTRUCTOR
4048169689Skan      && VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (exp)))
404918334Speter    {
405050397Sobrien      assemble_zeros (size);
405118334Speter      return;
405218334Speter    }
405318334Speter
405490075Sobrien  if (TREE_CODE (exp) == FDESC_EXPR)
405590075Sobrien    {
405690075Sobrien#ifdef ASM_OUTPUT_FDESC
405790075Sobrien      HOST_WIDE_INT part = tree_low_cst (TREE_OPERAND (exp, 1), 0);
405890075Sobrien      tree decl = TREE_OPERAND (exp, 0);
405990075Sobrien      ASM_OUTPUT_FDESC (asm_out_file, decl, part);
406090075Sobrien#else
4061169689Skan      gcc_unreachable ();
406290075Sobrien#endif
406390075Sobrien      return;
406490075Sobrien    }
406590075Sobrien
406690075Sobrien  /* Now output the underlying data.  If we've handling the padding, return.
4067169689Skan     Otherwise, break and ensure SIZE is the size written.  */
406818334Speter  switch (code)
406918334Speter    {
407018334Speter    case BOOLEAN_TYPE:
407118334Speter    case INTEGER_TYPE:
407218334Speter    case ENUMERAL_TYPE:
407318334Speter    case POINTER_TYPE:
407418334Speter    case REFERENCE_TYPE:
4075132718Skan    case OFFSET_TYPE:
407618334Speter      if (! assemble_integer (expand_expr (exp, NULL_RTX, VOIDmode,
407718334Speter					   EXPAND_INITIALIZER),
4078169689Skan			      MIN (size, thissize), align, 0))
407918334Speter	error ("initializer for integer value is too complicated");
408018334Speter      break;
408118334Speter
408218334Speter    case REAL_TYPE:
408318334Speter      if (TREE_CODE (exp) != REAL_CST)
408418334Speter	error ("initializer for floating value is not a floating constant");
408518334Speter
4086132718Skan      assemble_real (TREE_REAL_CST (exp), TYPE_MODE (TREE_TYPE (exp)), align);
408718334Speter      break;
408818334Speter
408918334Speter    case COMPLEX_TYPE:
409090075Sobrien      output_constant (TREE_REALPART (exp), thissize / 2, align);
409190075Sobrien      output_constant (TREE_IMAGPART (exp), thissize / 2,
409290075Sobrien		       min_align (align, BITS_PER_UNIT * (thissize / 2)));
409318334Speter      break;
409418334Speter
409518334Speter    case ARRAY_TYPE:
409696263Sobrien    case VECTOR_TYPE:
4097169689Skan      switch (TREE_CODE (exp))
409818334Speter	{
4099169689Skan	case CONSTRUCTOR:
410090075Sobrien	  output_constructor (exp, size, align);
410118334Speter	  return;
4102169689Skan	case STRING_CST:
4103132718Skan	  thissize = MIN ((unsigned HOST_WIDE_INT)TREE_STRING_LENGTH (exp),
4104132718Skan			  size);
410590075Sobrien	  assemble_string (TREE_STRING_POINTER (exp), thissize);
4106169689Skan	  break;
4107119256Skan
4108169689Skan	case VECTOR_CST:
4109169689Skan	  {
4110169689Skan	    int elt_size;
4111169689Skan	    tree link;
4112169689Skan	    unsigned int nalign;
4113169689Skan	    enum machine_mode inner;
4114119256Skan
4115169689Skan	    inner = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
4116169689Skan	    nalign = MIN (align, GET_MODE_ALIGNMENT (inner));
4117119256Skan
4118169689Skan	    elt_size = GET_MODE_SIZE (inner);
4119169689Skan
4120169689Skan	    link = TREE_VECTOR_CST_ELTS (exp);
4121169689Skan	    output_constant (TREE_VALUE (link), elt_size, align);
4122169689Skan	    thissize = elt_size;
4123169689Skan	    while ((link = TREE_CHAIN (link)) != NULL)
4124169689Skan	      {
4125169689Skan		output_constant (TREE_VALUE (link), elt_size, nalign);
4126169689Skan		thissize += elt_size;
4127169689Skan	      }
4128169689Skan	    break;
4129169689Skan	  }
4130169689Skan	default:
4131169689Skan	  gcc_unreachable ();
4132119256Skan	}
413318334Speter      break;
413418334Speter
413518334Speter    case RECORD_TYPE:
413618334Speter    case UNION_TYPE:
4137169689Skan      gcc_assert (TREE_CODE (exp) == CONSTRUCTOR);
4138169689Skan      output_constructor (exp, size, align);
413918334Speter      return;
414018334Speter
414190075Sobrien    case ERROR_MARK:
414290075Sobrien      return;
414390075Sobrien
414450397Sobrien    default:
4145169689Skan      gcc_unreachable ();
414618334Speter    }
414718334Speter
4148132718Skan  if (size > thissize)
4149132718Skan    assemble_zeros (size - thissize);
415018334Speter}
415118334Speter
415218334Speter
415390075Sobrien/* Subroutine of output_constructor, used for computing the size of
415490075Sobrien   arrays of unspecified length.  VAL must be a CONSTRUCTOR of an array
415590075Sobrien   type with an unspecified upper bound.  */
415690075Sobrien
415790075Sobrienstatic unsigned HOST_WIDE_INT
4158132718Skanarray_size_for_constructor (tree val)
415990075Sobrien{
416090075Sobrien  tree max_index, i;
4161169689Skan  unsigned HOST_WIDE_INT cnt;
4162169689Skan  tree index, value, tmp;
416390075Sobrien
416490075Sobrien  /* This code used to attempt to handle string constants that are not
416590075Sobrien     arrays of single-bytes, but nothing else does, so there's no point in
416690075Sobrien     doing it here.  */
416790075Sobrien  if (TREE_CODE (val) == STRING_CST)
416890075Sobrien    return TREE_STRING_LENGTH (val);
416990075Sobrien
417090075Sobrien  max_index = NULL_TREE;
4171169689Skan  FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (val), cnt, index, value)
417290075Sobrien    {
417390075Sobrien      if (TREE_CODE (index) == RANGE_EXPR)
417490075Sobrien	index = TREE_OPERAND (index, 1);
417590075Sobrien      if (max_index == NULL_TREE || tree_int_cst_lt (max_index, index))
417690075Sobrien	max_index = index;
417790075Sobrien    }
417890075Sobrien
417990075Sobrien  if (max_index == NULL_TREE)
418090075Sobrien    return 0;
418190075Sobrien
418290075Sobrien  /* Compute the total number of array elements.  */
4183169689Skan  tmp = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (val)));
4184169689Skan  i = size_binop (MINUS_EXPR, fold_convert (sizetype, max_index),
4185169689Skan		  fold_convert (sizetype, tmp));
4186169689Skan  i = size_binop (PLUS_EXPR, i, build_int_cst (sizetype, 1));
418790075Sobrien
418890075Sobrien  /* Multiply by the array element unit size to find number of bytes.  */
418990075Sobrien  i = size_binop (MULT_EXPR, i, TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (val))));
419090075Sobrien
419190075Sobrien  return tree_low_cst (i, 1);
419290075Sobrien}
419390075Sobrien
419490075Sobrien/* Subroutine of output_constant, used for CONSTRUCTORs (aggregate constants).
419518334Speter   Generate at least SIZE bytes, padding if necessary.  */
419618334Speter
419718334Speterstatic void
4198132718Skanoutput_constructor (tree exp, unsigned HOST_WIDE_INT size,
4199132718Skan		    unsigned int align)
420018334Speter{
420190075Sobrien  tree type = TREE_TYPE (exp);
4202169689Skan  tree field = 0;
420390075Sobrien  tree min_index = 0;
420418334Speter  /* Number of bytes output or skipped so far.
420518334Speter     In other words, current position within the constructor.  */
420690075Sobrien  HOST_WIDE_INT total_bytes = 0;
4207132718Skan  /* Nonzero means BYTE contains part of a byte, to be output.  */
420818334Speter  int byte_buffer_in_use = 0;
420990075Sobrien  int byte = 0;
4210169689Skan  unsigned HOST_WIDE_INT cnt;
4211169689Skan  constructor_elt *ce;
421218334Speter
4213169689Skan  gcc_assert (HOST_BITS_PER_WIDE_INT >= BITS_PER_UNIT);
421418334Speter
421590075Sobrien  if (TREE_CODE (type) == RECORD_TYPE)
421690075Sobrien    field = TYPE_FIELDS (type);
421718334Speter
421890075Sobrien  if (TREE_CODE (type) == ARRAY_TYPE
421990075Sobrien      && TYPE_DOMAIN (type) != 0)
422090075Sobrien    min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
422118334Speter
422218334Speter  /* As LINK goes through the elements of the constant,
422318334Speter     FIELD goes through the structure fields, if the constant is a structure.
422418334Speter     if the constant is a union, then we override this,
422518334Speter     by getting the field from the TREE_LIST element.
422652284Sobrien     But the constant could also be an array.  Then FIELD is zero.
422752284Sobrien
422852284Sobrien     There is always a maximum of one element in the chain LINK for unions
422952284Sobrien     (even if the initializer in a source program incorrectly contains
423090075Sobrien     more one).  */
4231169689Skan  for (cnt = 0;
4232169689Skan       VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (exp), cnt, ce);
4233169689Skan       cnt++, field = field ? TREE_CHAIN (field) : 0)
423418334Speter    {
4235169689Skan      tree val = ce->value;
423618334Speter      tree index = 0;
423718334Speter
423890075Sobrien      /* The element in a union constructor specifies the proper field
423990075Sobrien	 or index.  */
424090075Sobrien      if ((TREE_CODE (type) == RECORD_TYPE || TREE_CODE (type) == UNION_TYPE
424190075Sobrien	   || TREE_CODE (type) == QUAL_UNION_TYPE)
4242169689Skan	  && ce->index != 0)
4243169689Skan	field = ce->index;
424418334Speter
424590075Sobrien      else if (TREE_CODE (type) == ARRAY_TYPE)
4246169689Skan	index = ce->index;
424718334Speter
4248132718Skan#ifdef ASM_COMMENT_START
4249132718Skan      if (field && flag_verbose_asm)
4250132718Skan	fprintf (asm_out_file, "%s %s:\n",
4251169689Skan		 ASM_COMMENT_START,
4252169689Skan		 DECL_NAME (field)
4253132718Skan		 ? IDENTIFIER_POINTER (DECL_NAME (field))
4254132718Skan		 : "<anonymous>");
4255132718Skan#endif
4256132718Skan
425718334Speter      /* Eliminate the marker that makes a cast not be an lvalue.  */
425818334Speter      if (val != 0)
425918334Speter	STRIP_NOPS (val);
426018334Speter
426150397Sobrien      if (index && TREE_CODE (index) == RANGE_EXPR)
426218334Speter	{
426390075Sobrien	  unsigned HOST_WIDE_INT fieldsize
426490075Sobrien	    = int_size_in_bytes (TREE_TYPE (type));
426590075Sobrien	  HOST_WIDE_INT lo_index = tree_low_cst (TREE_OPERAND (index, 0), 0);
426690075Sobrien	  HOST_WIDE_INT hi_index = tree_low_cst (TREE_OPERAND (index, 1), 0);
426750397Sobrien	  HOST_WIDE_INT index;
426890075Sobrien	  unsigned int align2 = min_align (align, fieldsize * BITS_PER_UNIT);
426990075Sobrien
427050397Sobrien	  for (index = lo_index; index <= hi_index; index++)
427150397Sobrien	    {
427250397Sobrien	      /* Output the element's initial value.  */
427350397Sobrien	      if (val == 0)
427450397Sobrien		assemble_zeros (fieldsize);
427550397Sobrien	      else
427690075Sobrien		output_constant (val, fieldsize, align2);
427750397Sobrien
427850397Sobrien	      /* Count its size.  */
427950397Sobrien	      total_bytes += fieldsize;
428050397Sobrien	    }
428150397Sobrien	}
428250397Sobrien      else if (field == 0 || !DECL_BIT_FIELD (field))
428350397Sobrien	{
428418334Speter	  /* An element that is not a bit-field.  */
428518334Speter
428690075Sobrien	  unsigned HOST_WIDE_INT fieldsize;
428718334Speter	  /* Since this structure is static,
428818334Speter	     we know the positions are constant.  */
428990075Sobrien	  HOST_WIDE_INT pos = field ? int_byte_position (field) : 0;
429090075Sobrien	  unsigned int align2;
429190075Sobrien
429218334Speter	  if (index != 0)
429390075Sobrien	    pos = (tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (val)), 1)
429490075Sobrien		   * (tree_low_cst (index, 0) - tree_low_cst (min_index, 0)));
429518334Speter
429618334Speter	  /* Output any buffered-up bit-fields preceding this element.  */
429718334Speter	  if (byte_buffer_in_use)
429818334Speter	    {
429990075Sobrien	      assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
430018334Speter	      total_bytes++;
430118334Speter	      byte_buffer_in_use = 0;
430218334Speter	    }
430318334Speter
430418334Speter	  /* Advance to offset of this element.
430518334Speter	     Note no alignment needed in an array, since that is guaranteed
430618334Speter	     if each element has the proper size.  */
430790075Sobrien	  if ((field != 0 || index != 0) && pos != total_bytes)
430818334Speter	    {
4309169689Skan	      gcc_assert (pos >= total_bytes);
431090075Sobrien	      assemble_zeros (pos - total_bytes);
431190075Sobrien	      total_bytes = pos;
431218334Speter	    }
431318334Speter
431490075Sobrien	  /* Find the alignment of this element.  */
431590075Sobrien	  align2 = min_align (align, BITS_PER_UNIT * pos);
431690075Sobrien
431718334Speter	  /* Determine size this element should occupy.  */
431818334Speter	  if (field)
431918334Speter	    {
432090075Sobrien	      fieldsize = 0;
432190075Sobrien
432290075Sobrien	      /* If this is an array with an unspecified upper bound,
432390075Sobrien		 the initializer determines the size.  */
432490075Sobrien	      /* ??? This ought to only checked if DECL_SIZE_UNIT is NULL,
432590075Sobrien		 but we cannot do this until the deprecated support for
432690075Sobrien		 initializing zero-length array members is removed.  */
432790075Sobrien	      if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
432890075Sobrien		  && TYPE_DOMAIN (TREE_TYPE (field))
432990075Sobrien		  && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
433018334Speter		{
433190075Sobrien		  fieldsize = array_size_for_constructor (val);
433290075Sobrien		  /* Given a non-empty initialization, this field had
433390075Sobrien		     better be last.  */
4334169689Skan		  gcc_assert (!fieldsize || !TREE_CHAIN (field));
433518334Speter		}
433690075Sobrien	      else if (DECL_SIZE_UNIT (field))
433718334Speter		{
433890075Sobrien		  /* ??? This can't be right.  If the decl size overflows
433990075Sobrien		     a host integer we will silently emit no data.  */
434090075Sobrien		  if (host_integerp (DECL_SIZE_UNIT (field), 1))
434190075Sobrien		    fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 1);
434218334Speter		}
434318334Speter	    }
434418334Speter	  else
434590075Sobrien	    fieldsize = int_size_in_bytes (TREE_TYPE (type));
434618334Speter
434718334Speter	  /* Output the element's initial value.  */
434818334Speter	  if (val == 0)
434918334Speter	    assemble_zeros (fieldsize);
435018334Speter	  else
435190075Sobrien	    output_constant (val, fieldsize, align2);
435218334Speter
435318334Speter	  /* Count its size.  */
435418334Speter	  total_bytes += fieldsize;
435518334Speter	}
435618334Speter      else if (val != 0 && TREE_CODE (val) != INTEGER_CST)
4357169689Skan	error ("invalid initial value for member %qs",
435818334Speter	       IDENTIFIER_POINTER (DECL_NAME (field)));
435918334Speter      else
436018334Speter	{
436118334Speter	  /* Element that is a bit-field.  */
436218334Speter
436390075Sobrien	  HOST_WIDE_INT next_offset = int_bit_position (field);
436490075Sobrien	  HOST_WIDE_INT end_offset
436590075Sobrien	    = (next_offset + tree_low_cst (DECL_SIZE (field), 1));
436618334Speter
436718334Speter	  if (val == 0)
436818334Speter	    val = integer_zero_node;
436918334Speter
437018334Speter	  /* If this field does not start in this (or, next) byte,
437118334Speter	     skip some bytes.  */
437218334Speter	  if (next_offset / BITS_PER_UNIT != total_bytes)
437318334Speter	    {
437418334Speter	      /* Output remnant of any bit field in previous bytes.  */
437518334Speter	      if (byte_buffer_in_use)
437618334Speter		{
437790075Sobrien		  assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
437818334Speter		  total_bytes++;
437918334Speter		  byte_buffer_in_use = 0;
438018334Speter		}
438118334Speter
438218334Speter	      /* If still not at proper byte, advance to there.  */
438318334Speter	      if (next_offset / BITS_PER_UNIT != total_bytes)
438418334Speter		{
4385169689Skan		  gcc_assert (next_offset / BITS_PER_UNIT >= total_bytes);
438618334Speter		  assemble_zeros (next_offset / BITS_PER_UNIT - total_bytes);
438718334Speter		  total_bytes = next_offset / BITS_PER_UNIT;
438818334Speter		}
438918334Speter	    }
439018334Speter
439118334Speter	  if (! byte_buffer_in_use)
439218334Speter	    byte = 0;
439318334Speter
439418334Speter	  /* We must split the element into pieces that fall within
439518334Speter	     separate bytes, and combine each byte with previous or
439618334Speter	     following bit-fields.  */
439718334Speter
439818334Speter	  /* next_offset is the offset n fbits from the beginning of
439918334Speter	     the structure to the next bit of this element to be processed.
440018334Speter	     end_offset is the offset of the first bit past the end of
440118334Speter	     this element.  */
440218334Speter	  while (next_offset < end_offset)
440318334Speter	    {
440418334Speter	      int this_time;
440518334Speter	      int shift;
440618334Speter	      HOST_WIDE_INT value;
440790075Sobrien	      HOST_WIDE_INT next_byte = next_offset / BITS_PER_UNIT;
440890075Sobrien	      HOST_WIDE_INT next_bit = next_offset % BITS_PER_UNIT;
440918334Speter
441018334Speter	      /* Advance from byte to byte
441118334Speter		 within this element when necessary.  */
441218334Speter	      while (next_byte != total_bytes)
441318334Speter		{
441490075Sobrien		  assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
441518334Speter		  total_bytes++;
441618334Speter		  byte = 0;
441718334Speter		}
441818334Speter
441918334Speter	      /* Number of bits we can process at once
442018334Speter		 (all part of the same byte).  */
442118334Speter	      this_time = MIN (end_offset - next_offset,
442218334Speter			       BITS_PER_UNIT - next_bit);
442318334Speter	      if (BYTES_BIG_ENDIAN)
442418334Speter		{
442518334Speter		  /* On big-endian machine, take the most significant bits
442618334Speter		     first (of the bits that are significant)
442718334Speter		     and put them into bytes from the most significant end.  */
442818334Speter		  shift = end_offset - next_offset - this_time;
442990075Sobrien
443018334Speter		  /* Don't try to take a bunch of bits that cross
443190075Sobrien		     the word boundary in the INTEGER_CST. We can
443290075Sobrien		     only select bits from the LOW or HIGH part
443390075Sobrien		     not from both.  */
443418334Speter		  if (shift < HOST_BITS_PER_WIDE_INT
443518334Speter		      && shift + this_time > HOST_BITS_PER_WIDE_INT)
443618334Speter		    {
443790075Sobrien		      this_time = shift + this_time - HOST_BITS_PER_WIDE_INT;
443818334Speter		      shift = HOST_BITS_PER_WIDE_INT;
443918334Speter		    }
444018334Speter
444118334Speter		  /* Now get the bits from the appropriate constant word.  */
444218334Speter		  if (shift < HOST_BITS_PER_WIDE_INT)
444390075Sobrien		    value = TREE_INT_CST_LOW (val);
4444169689Skan		  else
444518334Speter		    {
4446169689Skan		      gcc_assert (shift < 2 * HOST_BITS_PER_WIDE_INT);
444718334Speter		      value = TREE_INT_CST_HIGH (val);
444818334Speter		      shift -= HOST_BITS_PER_WIDE_INT;
444918334Speter		    }
445090075Sobrien
445190075Sobrien		  /* Get the result. This works only when:
445290075Sobrien		     1 <= this_time <= HOST_BITS_PER_WIDE_INT.  */
445318334Speter		  byte |= (((value >> shift)
445490075Sobrien			    & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
445518334Speter			   << (BITS_PER_UNIT - this_time - next_bit));
445618334Speter		}
445718334Speter	      else
445818334Speter		{
445918334Speter		  /* On little-endian machines,
446018334Speter		     take first the least significant bits of the value
446118334Speter		     and pack them starting at the least significant
446218334Speter		     bits of the bytes.  */
446390075Sobrien		  shift = next_offset - int_bit_position (field);
446490075Sobrien
446518334Speter		  /* Don't try to take a bunch of bits that cross
446690075Sobrien		     the word boundary in the INTEGER_CST. We can
446790075Sobrien		     only select bits from the LOW or HIGH part
446890075Sobrien		     not from both.  */
446918334Speter		  if (shift < HOST_BITS_PER_WIDE_INT
447018334Speter		      && shift + this_time > HOST_BITS_PER_WIDE_INT)
447190075Sobrien		    this_time = (HOST_BITS_PER_WIDE_INT - shift);
447218334Speter
447318334Speter		  /* Now get the bits from the appropriate constant word.  */
447450397Sobrien		  if (shift < HOST_BITS_PER_WIDE_INT)
447518334Speter		    value = TREE_INT_CST_LOW (val);
4476169689Skan		  else
447718334Speter		    {
4478169689Skan		      gcc_assert (shift < 2 * HOST_BITS_PER_WIDE_INT);
447918334Speter		      value = TREE_INT_CST_HIGH (val);
448018334Speter		      shift -= HOST_BITS_PER_WIDE_INT;
448118334Speter		    }
448290075Sobrien
448390075Sobrien		  /* Get the result. This works only when:
448490075Sobrien		     1 <= this_time <= HOST_BITS_PER_WIDE_INT.  */
448518334Speter		  byte |= (((value >> shift)
448690075Sobrien			    & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
448718334Speter			   << next_bit);
448818334Speter		}
448990075Sobrien
449018334Speter	      next_offset += this_time;
449118334Speter	      byte_buffer_in_use = 1;
449218334Speter	    }
449318334Speter	}
449418334Speter    }
449590075Sobrien
449618334Speter  if (byte_buffer_in_use)
449718334Speter    {
449890075Sobrien      assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
449918334Speter      total_bytes++;
450018334Speter    }
450190075Sobrien
4502132718Skan  if ((unsigned HOST_WIDE_INT)total_bytes < size)
450318334Speter    assemble_zeros (size - total_bytes);
450418334Speter}
450518334Speter
450696263Sobrien/* This TREE_LIST contains any weak symbol declarations waiting
450796263Sobrien   to be emitted.  */
4508117395Skanstatic GTY(()) tree weak_decls;
450990075Sobrien
451096263Sobrien/* Mark DECL as weak.  */
451196263Sobrien
451296263Sobrienstatic void
4513132718Skanmark_weak (tree decl)
451490075Sobrien{
451596263Sobrien  DECL_WEAK (decl) = 1;
451690075Sobrien
451796263Sobrien  if (DECL_RTL_SET_P (decl)
4518169689Skan      && MEM_P (DECL_RTL (decl))
451996263Sobrien      && XEXP (DECL_RTL (decl), 0)
452096263Sobrien      && GET_CODE (XEXP (DECL_RTL (decl), 0)) == SYMBOL_REF)
452196263Sobrien    SYMBOL_REF_WEAK (XEXP (DECL_RTL (decl), 0)) = 1;
452296263Sobrien}
4523117395Skan
452496263Sobrien/* Merge weak status between NEWDECL and OLDDECL.  */
452590075Sobrien
452696263Sobrienvoid
4527132718Skanmerge_weak (tree newdecl, tree olddecl)
452818334Speter{
452996263Sobrien  if (DECL_WEAK (newdecl) == DECL_WEAK (olddecl))
4530169689Skan    {
4531169689Skan      if (DECL_WEAK (newdecl) && SUPPORTS_WEAK)
4532169689Skan        {
4533169689Skan          tree *pwd;
4534169689Skan          /* We put the NEWDECL on the weak_decls list at some point
4535169689Skan             and OLDDECL as well.  Keep just OLDDECL on the list.  */
4536169689Skan	  for (pwd = &weak_decls; *pwd; pwd = &TREE_CHAIN (*pwd))
4537169689Skan	    if (TREE_VALUE (*pwd) == newdecl)
4538169689Skan	      {
4539169689Skan	        *pwd = TREE_CHAIN (*pwd);
4540169689Skan		break;
4541169689Skan	      }
4542169689Skan        }
4543169689Skan      return;
4544169689Skan    }
454518334Speter
454696263Sobrien  if (DECL_WEAK (newdecl))
454796263Sobrien    {
454896263Sobrien      tree wd;
4549117395Skan
455096263Sobrien      /* NEWDECL is weak, but OLDDECL is not.  */
455118334Speter
455296263Sobrien      /* If we already output the OLDDECL, we're in trouble; we can't
455396263Sobrien	 go back and make it weak.  This error cannot caught in
455496263Sobrien	 declare_weak because the NEWDECL and OLDDECL was not yet
455596263Sobrien	 been merged; therefore, TREE_ASM_WRITTEN was not set.  */
4556102780Skan      if (TREE_ASM_WRITTEN (olddecl))
4557169689Skan	error ("weak declaration of %q+D must precede definition",
4558169689Skan	       newdecl);
4559102780Skan
4560102780Skan      /* If we've already generated rtl referencing OLDDECL, we may
4561102780Skan	 have done so in a way that will not function properly with
4562102780Skan	 a weak symbol.  */
4563102780Skan      else if (TREE_USED (olddecl)
4564102780Skan	       && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (olddecl)))
4565169689Skan	warning (0, "weak declaration of %q+D after first use results "
4566169689Skan                 "in unspecified behavior", newdecl);
4567102780Skan
456896263Sobrien      if (SUPPORTS_WEAK)
456996263Sobrien	{
457096263Sobrien	  /* We put the NEWDECL on the weak_decls list at some point.
457196263Sobrien	     Replace it with the OLDDECL.  */
457296263Sobrien	  for (wd = weak_decls; wd; wd = TREE_CHAIN (wd))
457396263Sobrien	    if (TREE_VALUE (wd) == newdecl)
457496263Sobrien	      {
457596263Sobrien		TREE_VALUE (wd) = olddecl;
457696263Sobrien		break;
457796263Sobrien	      }
457896263Sobrien	  /* We may not find the entry on the list.  If NEWDECL is a
457996263Sobrien	     weak alias, then we will have already called
458096263Sobrien	     globalize_decl to remove the entry; in that case, we do
458196263Sobrien	     not need to do anything.  */
458296263Sobrien	}
458352284Sobrien
458496263Sobrien      /* Make the OLDDECL weak; it's OLDDECL that we'll be keeping.  */
458596263Sobrien      mark_weak (olddecl);
458696263Sobrien    }
458796263Sobrien  else
458896263Sobrien    /* OLDDECL was weak, but NEWDECL was not explicitly marked as
458996263Sobrien       weak.  Just update NEWDECL to indicate that it's weak too.  */
459096263Sobrien    mark_weak (newdecl);
459152284Sobrien}
459218334Speter
459318334Speter/* Declare DECL to be a weak symbol.  */
459418334Speter
459518334Spetervoid
4596132718Skandeclare_weak (tree decl)
459718334Speter{
459818334Speter  if (! TREE_PUBLIC (decl))
4599169689Skan    error ("weak declaration of %q+D must be public", decl);
460096263Sobrien  else if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
4601169689Skan    error ("weak declaration of %q+D must precede definition", decl);
460218334Speter  else if (SUPPORTS_WEAK)
460396263Sobrien    {
460496263Sobrien      if (! DECL_WEAK (decl))
460596263Sobrien	weak_decls = tree_cons (NULL, decl, weak_decls);
460696263Sobrien    }
460790075Sobrien  else
4608169689Skan    warning (0, "weak declaration of %q+D not supported", decl);
460990075Sobrien
461096263Sobrien  mark_weak (decl);
461118334Speter}
461218334Speter
4613169689Skanstatic void
4614169689Skanweak_finish_1 (tree decl)
461518334Speter{
4616132718Skan#if defined (ASM_WEAKEN_DECL) || defined (ASM_WEAKEN_LABEL)
4617169689Skan  const char *const name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
4618132718Skan#endif
461996263Sobrien
4620169689Skan  if (! TREE_USED (decl))
4621169689Skan    return;
462296263Sobrien
462396263Sobrien#ifdef ASM_WEAKEN_DECL
4624169689Skan  ASM_WEAKEN_DECL (asm_out_file, decl, name, NULL);
462590075Sobrien#else
462690075Sobrien#ifdef ASM_WEAKEN_LABEL
4627169689Skan  ASM_WEAKEN_LABEL (asm_out_file, name);
462896263Sobrien#else
462996263Sobrien#ifdef ASM_OUTPUT_WEAK_ALIAS
4630169689Skan  {
4631169689Skan    static bool warn_once = 0;
4632169689Skan    if (! warn_once)
4633169689Skan      {
4634169689Skan	warning (0, "only weak aliases are supported in this configuration");
4635169689Skan	warn_once = 1;
4636169689Skan      }
4637169689Skan    return;
4638169689Skan  }
463990075Sobrien#endif
464090075Sobrien#endif
464196263Sobrien#endif
4642169689Skan}
4643169689Skan
4644169689Skan/* This TREE_LIST contains weakref targets.  */
4645169689Skan
4646169689Skanstatic GTY(()) tree weakref_targets;
4647169689Skan
4648169689Skan/* Forward declaration.  */
4649169689Skanstatic tree find_decl_and_mark_needed (tree decl, tree target);
4650169689Skan
4651169689Skan/* Emit any pending weak declarations.  */
4652169689Skan
4653169689Skanvoid
4654169689Skanweak_finish (void)
4655169689Skan{
4656169689Skan  tree t;
4657169689Skan
4658169689Skan  for (t = weakref_targets; t; t = TREE_CHAIN (t))
4659169689Skan    {
4660169689Skan      tree alias_decl = TREE_PURPOSE (t);
4661169689Skan      tree target = ultimate_transparent_alias_target (&TREE_VALUE (t));
4662169689Skan
4663169689Skan      if (! TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (alias_decl)))
4664169689Skan	/* Remove alias_decl from the weak list, but leave entries for
4665169689Skan	   the target alone.  */
4666169689Skan	target = NULL_TREE;
4667169689Skan#ifndef ASM_OUTPUT_WEAKREF
4668169689Skan      else if (! TREE_SYMBOL_REFERENCED (target))
4669169689Skan	{
4670169689Skan	  /* Use ASM_WEAKEN_LABEL only if ASM_WEAKEN_DECL is not
4671169689Skan	     defined, otherwise we and weak_finish_1 would use a
4672169689Skan	     different macros.  */
4673169689Skan# if defined ASM_WEAKEN_LABEL && ! defined ASM_WEAKEN_DECL
4674169689Skan	  ASM_WEAKEN_LABEL (asm_out_file, IDENTIFIER_POINTER (target));
4675169689Skan# else
4676169689Skan	  tree decl = find_decl_and_mark_needed (alias_decl, target);
4677169689Skan
4678169689Skan	  if (! decl)
4679169689Skan	    {
4680169689Skan	      decl = build_decl (TREE_CODE (alias_decl), target,
4681169689Skan				 TREE_TYPE (alias_decl));
4682169689Skan
4683169689Skan	      DECL_EXTERNAL (decl) = 1;
4684169689Skan	      TREE_PUBLIC (decl) = 1;
4685169689Skan	      DECL_ARTIFICIAL (decl) = 1;
4686169689Skan	      TREE_NOTHROW (decl) = TREE_NOTHROW (alias_decl);
4687169689Skan	      TREE_USED (decl) = 1;
4688169689Skan	    }
4689169689Skan
4690169689Skan	  weak_finish_1 (decl);
4691169689Skan# endif
4692169689Skan	}
4693169689Skan#endif
4694169689Skan
4695169689Skan      {
4696169689Skan	tree *p;
4697169689Skan	tree t2;
4698169689Skan
4699169689Skan	/* Remove the alias and the target from the pending weak list
4700169689Skan	   so that we do not emit any .weak directives for the former,
4701169689Skan	   nor multiple .weak directives for the latter.  */
4702169689Skan	for (p = &weak_decls; (t2 = *p) ; )
4703169689Skan	  {
4704169689Skan	    if (TREE_VALUE (t2) == alias_decl
4705169689Skan		|| target == DECL_ASSEMBLER_NAME (TREE_VALUE (t2)))
4706169689Skan	      *p = TREE_CHAIN (t2);
4707169689Skan	    else
4708169689Skan	      p = &TREE_CHAIN (t2);
4709169689Skan	  }
4710169689Skan
4711169689Skan	/* Remove other weakrefs to the same target, to speed things up.  */
4712169689Skan	for (p = &TREE_CHAIN (t); (t2 = *p) ; )
4713169689Skan	  {
4714169689Skan	    if (target == ultimate_transparent_alias_target (&TREE_VALUE (t2)))
4715169689Skan	      *p = TREE_CHAIN (t2);
4716169689Skan	    else
4717169689Skan	      p = &TREE_CHAIN (t2);
4718169689Skan	  }
4719169689Skan      }
472018334Speter    }
4721169689Skan
4722169689Skan  for (t = weak_decls; t; t = TREE_CHAIN (t))
4723169689Skan    {
4724169689Skan      tree decl = TREE_VALUE (t);
4725169689Skan
4726169689Skan      weak_finish_1 (decl);
4727169689Skan    }
472818334Speter}
472918334Speter
473096263Sobrien/* Emit the assembly bits to indicate that DECL is globally visible.  */
473196263Sobrien
473252284Sobrienstatic void
4733132718Skanglobalize_decl (tree decl)
473452284Sobrien{
473596263Sobrien  const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
473690075Sobrien
473796263Sobrien#if defined (ASM_WEAKEN_LABEL) || defined (ASM_WEAKEN_DECL)
473896263Sobrien  if (DECL_WEAK (decl))
473952284Sobrien    {
474096263Sobrien      tree *p, t;
474196263Sobrien
474296263Sobrien#ifdef ASM_WEAKEN_DECL
474396263Sobrien      ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
474496263Sobrien#else
474596263Sobrien      ASM_WEAKEN_LABEL (asm_out_file, name);
474696263Sobrien#endif
474796263Sobrien
474896263Sobrien      /* Remove this function from the pending weak list so that
474996263Sobrien	 we do not emit multiple .weak directives for it.  */
475096263Sobrien      for (p = &weak_decls; (t = *p) ; )
475196263Sobrien	{
475296263Sobrien	  if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
475396263Sobrien	    *p = TREE_CHAIN (t);
475496263Sobrien	  else
475596263Sobrien	    p = &TREE_CHAIN (t);
475696263Sobrien	}
4757169689Skan
4758169689Skan      /* Remove weakrefs to the same target from the pending weakref
4759169689Skan	 list, for the same reason.  */
4760169689Skan      for (p = &weakref_targets; (t = *p) ; )
4761169689Skan	{
4762169689Skan	  if (DECL_ASSEMBLER_NAME (decl)
4763169689Skan	      == ultimate_transparent_alias_target (&TREE_VALUE (t)))
4764169689Skan	    *p = TREE_CHAIN (t);
4765169689Skan	  else
4766169689Skan	    p = &TREE_CHAIN (t);
4767169689Skan	}
4768169689Skan
476996263Sobrien      return;
477052284Sobrien    }
4771169689Skan#elif defined(ASM_MAKE_LABEL_LINKONCE)
4772169689Skan  if (DECL_ONE_ONLY (decl))
4773169689Skan    ASM_MAKE_LABEL_LINKONCE (asm_out_file, name);
477496263Sobrien#endif
477596263Sobrien
4776169689Skan  targetm.asm_out.globalize_label (asm_out_file, name);
477752284Sobrien}
477852284Sobrien
4779169689Skan/* We have to be able to tell cgraph about the needed-ness of the target
4780169689Skan   of an alias.  This requires that the decl have been defined.  Aliases
4781169689Skan   that precede their definition have to be queued for later processing.  */
4782169689Skan
4783169689Skantypedef struct alias_pair GTY(())
4784146895Skan{
4785146895Skan  tree decl;
4786146895Skan  tree target;
4787169689Skan} alias_pair;
4788146895Skan
4789169689Skan/* Define gc'd vector type.  */
4790169689SkanDEF_VEC_O(alias_pair);
4791169689SkanDEF_VEC_ALLOC_O(alias_pair,gc);
4792146895Skan
4793169689Skanstatic GTY(()) VEC(alias_pair,gc) *alias_pairs;
4794169689Skan
4795169689Skan/* Given an assembly name, find the decl it is associated with.  At the
4796169689Skan   same time, mark it needed for cgraph.  */
4797169689Skan
4798169689Skanstatic tree
4799169689Skanfind_decl_and_mark_needed (tree decl, tree target)
4800169689Skan{
4801169689Skan  struct cgraph_node *fnode = NULL;
4802169689Skan  struct cgraph_varpool_node *vnode = NULL;
4803169689Skan
4804169689Skan  if (TREE_CODE (decl) == FUNCTION_DECL)
4805169689Skan    {
4806169689Skan      fnode = cgraph_node_for_asm (target);
4807169689Skan      if (fnode == NULL)
4808169689Skan	vnode = cgraph_varpool_node_for_asm (target);
4809169689Skan    }
4810169689Skan  else
4811169689Skan    {
4812169689Skan      vnode = cgraph_varpool_node_for_asm (target);
4813169689Skan      if (vnode == NULL)
4814169689Skan	fnode = cgraph_node_for_asm (target);
4815169689Skan    }
4816169689Skan
4817169689Skan  if (fnode)
4818169689Skan    {
4819169689Skan      /* We can't mark function nodes as used after cgraph global info
4820169689Skan	 is finished.  This wouldn't generally be necessary, but C++
4821169689Skan	 virtual table thunks are introduced late in the game and
4822169689Skan	 might seem like they need marking, although in fact they
4823169689Skan	 don't.  */
4824169689Skan      if (! cgraph_global_info_ready)
4825169689Skan	cgraph_mark_needed_node (fnode);
4826169689Skan      return fnode->decl;
4827169689Skan    }
4828169689Skan  else if (vnode)
4829169689Skan    {
4830169689Skan      cgraph_varpool_mark_needed_node (vnode);
4831169689Skan      return vnode->decl;
4832169689Skan    }
4833169689Skan  else
4834169689Skan    return NULL_TREE;
4835169689Skan}
4836169689Skan
4837146895Skan/* Output the assembler code for a define (equate) using ASM_OUTPUT_DEF
4838146895Skan   or ASM_OUTPUT_DEF_FROM_DECLS.  The function defines the symbol whose
4839146895Skan   tree node is DECL to have the value of the tree node TARGET.  */
4840146895Skan
4841146895Skanstatic void
4842169689Skando_assemble_alias (tree decl, tree target)
4843146895Skan{
4844169689Skan  if (TREE_ASM_WRITTEN (decl))
4845169689Skan    return;
4846169689Skan
4847169689Skan  TREE_ASM_WRITTEN (decl) = 1;
4848169689Skan  TREE_ASM_WRITTEN (DECL_ASSEMBLER_NAME (decl)) = 1;
4849169689Skan
4850169689Skan  if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
4851169689Skan    {
4852169689Skan      ultimate_transparent_alias_target (&target);
4853169689Skan
4854169689Skan      if (!TREE_SYMBOL_REFERENCED (target))
4855169689Skan	weakref_targets = tree_cons (decl, target, weakref_targets);
4856169689Skan
4857169689Skan#ifdef ASM_OUTPUT_WEAKREF
4858169689Skan      ASM_OUTPUT_WEAKREF (asm_out_file, decl,
4859169689Skan			  IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
4860169689Skan			  IDENTIFIER_POINTER (target));
4861169689Skan#else
4862169689Skan      if (!SUPPORTS_WEAK)
4863169689Skan	{
4864169689Skan	  error ("%Jweakref is not supported in this configuration", decl);
4865169689Skan	  return;
4866169689Skan	}
4867169689Skan#endif
4868169689Skan      return;
4869169689Skan    }
4870169689Skan
4871169689Skan#ifdef ASM_OUTPUT_DEF
4872169689Skan  /* Make name accessible from other files, if appropriate.  */
4873169689Skan
4874169689Skan  if (TREE_PUBLIC (decl))
4875169689Skan    {
4876169689Skan      globalize_decl (decl);
4877169689Skan      maybe_assemble_visibility (decl);
4878169689Skan    }
4879169689Skan
4880169689Skan# ifdef ASM_OUTPUT_DEF_FROM_DECLS
4881146895Skan  ASM_OUTPUT_DEF_FROM_DECLS (asm_out_file, decl, target);
4882169689Skan# else
4883146895Skan  ASM_OUTPUT_DEF (asm_out_file,
4884146895Skan		  IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
4885146895Skan		  IDENTIFIER_POINTER (target));
4886169689Skan# endif
4887169689Skan#elif defined (ASM_OUTPUT_WEAK_ALIAS) || defined (ASM_WEAKEN_DECL)
4888169689Skan  {
4889169689Skan    const char *name;
4890169689Skan    tree *p, t;
4891169689Skan
4892169689Skan    name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
4893169689Skan# ifdef ASM_WEAKEN_DECL
4894169689Skan    ASM_WEAKEN_DECL (asm_out_file, decl, name, IDENTIFIER_POINTER (target));
4895169689Skan# else
4896169689Skan    ASM_OUTPUT_WEAK_ALIAS (asm_out_file, name, IDENTIFIER_POINTER (target));
4897169689Skan# endif
4898169689Skan    /* Remove this function from the pending weak list so that
4899169689Skan       we do not emit multiple .weak directives for it.  */
4900169689Skan    for (p = &weak_decls; (t = *p) ; )
4901169689Skan      if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
4902169689Skan	*p = TREE_CHAIN (t);
4903169689Skan      else
4904169689Skan	p = &TREE_CHAIN (t);
4905169689Skan
4906169689Skan    /* Remove weakrefs to the same target from the pending weakref
4907169689Skan       list, for the same reason.  */
4908169689Skan    for (p = &weakref_targets; (t = *p) ; )
4909169689Skan      {
4910169689Skan	if (DECL_ASSEMBLER_NAME (decl)
4911169689Skan	    == ultimate_transparent_alias_target (&TREE_VALUE (t)))
4912169689Skan	  *p = TREE_CHAIN (t);
4913169689Skan	else
4914169689Skan	  p = &TREE_CHAIN (t);
4915169689Skan      }
4916169689Skan  }
4917146895Skan#endif
4918146895Skan}
4919146895Skan
4920169689Skan/* First pass of completing pending aliases.  Make sure that cgraph knows
4921169689Skan   which symbols will be required.  */
4922146895Skan
4923146895Skanvoid
4924169689Skanfinish_aliases_1 (void)
4925146895Skan{
4926169689Skan  unsigned i;
4927169689Skan  alias_pair *p;
4928146895Skan
4929169689Skan  for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); i++)
4930169689Skan    {
4931169689Skan      tree target_decl;
4932146895Skan
4933169689Skan      target_decl = find_decl_and_mark_needed (p->decl, p->target);
4934169689Skan      if (target_decl == NULL)
4935169689Skan	{
4936169689Skan	  if (! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
4937169689Skan	    error ("%q+D aliased to undefined symbol %qs",
4938169689Skan		   p->decl, IDENTIFIER_POINTER (p->target));
4939169689Skan	}
4940169689Skan      else if (DECL_EXTERNAL (target_decl)
4941169689Skan	       && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
4942169689Skan	error ("%q+D aliased to external symbol %qs",
4943169689Skan	       p->decl, IDENTIFIER_POINTER (p->target));
4944146895Skan    }
4945169689Skan}
4946146895Skan
4947169689Skan/* Second pass of completing pending aliases.  Emit the actual assembly.
4948169689Skan   This happens at the end of compilation and thus it is assured that the
4949169689Skan   target symbol has been emitted.  */
4950169689Skan
4951169689Skanvoid
4952169689Skanfinish_aliases_2 (void)
4953169689Skan{
4954169689Skan  unsigned i;
4955169689Skan  alias_pair *p;
4956169689Skan
4957169689Skan  for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); i++)
4958169689Skan    do_assemble_alias (p->decl, p->target);
4959169689Skan
4960169689Skan  VEC_truncate (alias_pair, alias_pairs, 0);
4961146895Skan}
4962146895Skan
496390075Sobrien/* Emit an assembler directive to make the symbol for DECL an alias to
496490075Sobrien   the symbol for TARGET.  */
496590075Sobrien
496618334Spetervoid
4967169689Skanassemble_alias (tree decl, tree target)
496818334Speter{
4969169689Skan  tree target_decl;
4970169689Skan  bool is_weakref = false;
497118334Speter
4972169689Skan  if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
4973117395Skan    {
4974169689Skan      tree alias = DECL_ASSEMBLER_NAME (decl);
497518334Speter
4976169689Skan      is_weakref = true;
4977146895Skan
4978169689Skan      ultimate_transparent_alias_target (&target);
4979169689Skan
4980169689Skan      if (alias == target)
4981169689Skan	error ("weakref %q+D ultimately targets itself", decl);
4982169689Skan      else
4983169689Skan	{
4984169689Skan#ifndef ASM_OUTPUT_WEAKREF
4985169689Skan	  IDENTIFIER_TRANSPARENT_ALIAS (alias) = 1;
4986169689Skan	  TREE_CHAIN (alias) = target;
4987169689Skan#endif
4988169689Skan	}
4989169689Skan      if (TREE_PUBLIC (decl))
4990169689Skan	error ("weakref %q+D must have static linkage", decl);
4991146895Skan    }
4992146895Skan  else
4993132718Skan    {
4994169689Skan#if !defined (ASM_OUTPUT_DEF)
4995169689Skan# if !defined(ASM_OUTPUT_WEAK_ALIAS) && !defined (ASM_WEAKEN_DECL)
4996169689Skan      error ("%Jalias definitions not supported in this configuration", decl);
4997169689Skan      return;
4998169689Skan# else
4999169689Skan      if (!DECL_WEAK (decl))
5000169689Skan	{
5001169689Skan	  error ("%Jonly weak aliases are supported in this configuration", decl);
5002169689Skan	  return;
5003169689Skan	}
5004169689Skan# endif
500596263Sobrien#endif
5006132718Skan    }
5007169689Skan
5008169689Skan  /* We must force creation of DECL_RTL for debug info generation, even though
5009169689Skan     we don't use it here.  */
5010169689Skan  make_decl_rtl (decl);
5011169689Skan  TREE_USED (decl) = 1;
5012169689Skan
5013169689Skan  /* A quirk of the initial implementation of aliases required that the user
5014169689Skan     add "extern" to all of them.  Which is silly, but now historical.  Do
5015169689Skan     note that the symbol is in fact locally defined.  */
5016169689Skan  if (! is_weakref)
5017169689Skan    DECL_EXTERNAL (decl) = 0;
5018169689Skan
5019169689Skan  /* Allow aliases to aliases.  */
5020169689Skan  if (TREE_CODE (decl) == FUNCTION_DECL)
5021169689Skan    cgraph_node (decl)->alias = true;
5022132718Skan  else
5023169689Skan    cgraph_varpool_node (decl)->alias = true;
5024132718Skan
5025169689Skan  /* If the target has already been emitted, we don't have to queue the
5026169689Skan     alias.  This saves a tad o memory.  */
5027169689Skan  target_decl = find_decl_and_mark_needed (decl, target);
5028169689Skan  if (target_decl && TREE_ASM_WRITTEN (target_decl))
5029169689Skan    do_assemble_alias (decl, target);
5030169689Skan  else
5031169689Skan    {
5032169689Skan      alias_pair *p = VEC_safe_push (alias_pair, gc, alias_pairs, NULL);
5033169689Skan      p->decl = decl;
5034169689Skan      p->target = target;
5035169689Skan    }
503618334Speter}
503750397Sobrien
5038117395Skan/* Emit an assembler directive to set symbol for DECL visibility to
5039117395Skan   the visibility type VIS, which must not be VISIBILITY_DEFAULT.  */
5040117395Skan
5041117395Skanvoid
5042132718Skandefault_assemble_visibility (tree decl, int vis)
5043117395Skan{
5044117395Skan  static const char * const visibility_types[] = {
5045169689Skan    NULL, "protected", "hidden", "internal"
5046117395Skan  };
5047117395Skan
5048117395Skan  const char *name, *type;
5049117395Skan
5050169689Skan  name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5051117395Skan  type = visibility_types[vis];
5052117395Skan
5053117395Skan#ifdef HAVE_GAS_HIDDEN
5054117395Skan  fprintf (asm_out_file, "\t.%s\t", type);
5055117395Skan  assemble_name (asm_out_file, name);
5056117395Skan  fprintf (asm_out_file, "\n");
5057117395Skan#else
5058169689Skan  warning (OPT_Wattributes, "visibility attribute not supported "
5059169689Skan	   "in this configuration; ignored");
5060117395Skan#endif
5061117395Skan}
5062117395Skan
5063117395Skan/* A helper function to call assemble_visibility when needed for a decl.  */
5064117395Skan
5065215840Sdimint
5066132718Skanmaybe_assemble_visibility (tree decl)
5067117395Skan{
5068132718Skan  enum symbol_visibility vis = DECL_VISIBILITY (decl);
5069117395Skan
5070117395Skan  if (vis != VISIBILITY_DEFAULT)
5071215840Sdim    {
5072215840Sdim      targetm.asm_out.visibility (decl, vis);
5073215840Sdim      return 1;
5074215840Sdim    }
5075215840Sdim  else
5076215840Sdim    return 0;
5077117395Skan}
5078117395Skan
507950397Sobrien/* Returns 1 if the target configuration supports defining public symbols
508050397Sobrien   so that one of them will be chosen at link time instead of generating a
508150397Sobrien   multiply-defined symbol error, whether through the use of weak symbols or
508250397Sobrien   a target-specific mechanism for having duplicates discarded.  */
508350397Sobrien
508450397Sobrienint
5085132718Skansupports_one_only (void)
508650397Sobrien{
508750397Sobrien  if (SUPPORTS_ONE_ONLY)
508850397Sobrien    return 1;
508950397Sobrien  return SUPPORTS_WEAK;
509050397Sobrien}
509150397Sobrien
509250397Sobrien/* Set up DECL as a public symbol that can be defined in multiple
509350397Sobrien   translation units without generating a linker error.  */
509450397Sobrien
509550397Sobrienvoid
5096132718Skanmake_decl_one_only (tree decl)
509750397Sobrien{
5098169689Skan  gcc_assert (TREE_CODE (decl) == VAR_DECL
5099169689Skan	      || TREE_CODE (decl) == FUNCTION_DECL);
510050397Sobrien
510150397Sobrien  TREE_PUBLIC (decl) = 1;
510250397Sobrien
5103132718Skan  if (SUPPORTS_ONE_ONLY)
510450397Sobrien    {
510550397Sobrien#ifdef MAKE_DECL_ONE_ONLY
510650397Sobrien      MAKE_DECL_ONE_ONLY (decl);
510750397Sobrien#endif
510850397Sobrien      DECL_ONE_ONLY (decl) = 1;
510950397Sobrien    }
5110132718Skan  else if (TREE_CODE (decl) == VAR_DECL
5111132718Skan      && (DECL_INITIAL (decl) == 0 || DECL_INITIAL (decl) == error_mark_node))
5112132718Skan    DECL_COMMON (decl) = 1;
511350397Sobrien  else
5114169689Skan    {
5115169689Skan      gcc_assert (SUPPORTS_WEAK);
5116169689Skan      DECL_WEAK (decl) = 1;
5117169689Skan    }
511850397Sobrien}
511990075Sobrien
512090075Sobrienvoid
5121132718Skaninit_varasm_once (void)
512290075Sobrien{
5123169689Skan  section_htab = htab_create_ggc (31, section_entry_hash,
5124169689Skan				  section_entry_eq, NULL);
5125169689Skan  object_block_htab = htab_create_ggc (31, object_block_entry_hash,
5126169689Skan				       object_block_entry_eq, NULL);
5127132718Skan  const_desc_htab = htab_create_ggc (1009, const_desc_hash,
5128132718Skan				     const_desc_eq, NULL);
512990075Sobrien
513090075Sobrien  const_alias_set = new_alias_set ();
5131169689Skan  shared_constant_pool = create_constant_pool ();
5132169689Skan
5133169689Skan#ifdef TEXT_SECTION_ASM_OP
5134169689Skan  text_section = get_unnamed_section (SECTION_CODE, output_section_asm_op,
5135169689Skan				      TEXT_SECTION_ASM_OP);
5136169689Skan#endif
5137169689Skan
5138169689Skan#ifdef DATA_SECTION_ASM_OP
5139169689Skan  data_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
5140169689Skan				      DATA_SECTION_ASM_OP);
5141169689Skan#endif
5142169689Skan
5143169689Skan#ifdef SDATA_SECTION_ASM_OP
5144169689Skan  sdata_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
5145169689Skan				       SDATA_SECTION_ASM_OP);
5146169689Skan#endif
5147169689Skan
5148169689Skan#ifdef READONLY_DATA_SECTION_ASM_OP
5149169689Skan  readonly_data_section = get_unnamed_section (0, output_section_asm_op,
5150169689Skan					       READONLY_DATA_SECTION_ASM_OP);
5151169689Skan#endif
5152169689Skan
5153169689Skan#ifdef CTORS_SECTION_ASM_OP
5154169689Skan  ctors_section = get_unnamed_section (0, output_section_asm_op,
5155169689Skan				       CTORS_SECTION_ASM_OP);
5156169689Skan#endif
5157169689Skan
5158169689Skan#ifdef DTORS_SECTION_ASM_OP
5159169689Skan  dtors_section = get_unnamed_section (0, output_section_asm_op,
5160169689Skan				       DTORS_SECTION_ASM_OP);
5161169689Skan#endif
5162169689Skan
5163169689Skan#ifdef BSS_SECTION_ASM_OP
5164169689Skan  bss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
5165169689Skan				     output_section_asm_op,
5166169689Skan				     BSS_SECTION_ASM_OP);
5167169689Skan#endif
5168169689Skan
5169169689Skan#ifdef SBSS_SECTION_ASM_OP
5170169689Skan  sbss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
5171169689Skan				      output_section_asm_op,
5172169689Skan				      SBSS_SECTION_ASM_OP);
5173169689Skan#endif
5174169689Skan
5175169689Skan  tls_comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
5176169689Skan					   | SECTION_COMMON, emit_tls_common);
5177169689Skan  lcomm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
5178169689Skan					| SECTION_COMMON, emit_local);
5179169689Skan  comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
5180169689Skan				       | SECTION_COMMON, emit_common);
5181169689Skan
5182169689Skan#if defined ASM_OUTPUT_ALIGNED_BSS || defined ASM_OUTPUT_BSS
5183169689Skan  bss_noswitch_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS,
5184169689Skan					       emit_bss);
5185169689Skan#endif
5186169689Skan
5187169689Skan  targetm.asm_out.init_sections ();
5188169689Skan
5189169689Skan  if (readonly_data_section == NULL)
5190169689Skan    readonly_data_section = text_section;
519190075Sobrien}
519290075Sobrien
5193117395Skanenum tls_model
5194169689Skandecl_default_tls_model (tree decl)
5195117395Skan{
5196117395Skan  enum tls_model kind;
5197117395Skan  bool is_local;
5198117395Skan
5199169689Skan  is_local = targetm.binds_local_p (decl);
5200169689Skan  if (!flag_shlib)
5201117395Skan    {
5202117395Skan      if (is_local)
5203117395Skan	kind = TLS_MODEL_LOCAL_EXEC;
5204117395Skan      else
5205117395Skan	kind = TLS_MODEL_INITIAL_EXEC;
5206117395Skan    }
5207169689Skan
5208117395Skan  /* Local dynamic is inefficient when we're not combining the
5209117395Skan     parts of the address.  */
5210117395Skan  else if (optimize && is_local)
5211117395Skan    kind = TLS_MODEL_LOCAL_DYNAMIC;
5212117395Skan  else
5213117395Skan    kind = TLS_MODEL_GLOBAL_DYNAMIC;
5214117395Skan  if (kind < flag_tls_default)
5215117395Skan    kind = flag_tls_default;
5216117395Skan
5217117395Skan  return kind;
5218117395Skan}
5219117395Skan
522090075Sobrien/* Select a set of attributes for section NAME based on the properties
522190075Sobrien   of DECL and whether or not RELOC indicates that DECL's initializer
522290075Sobrien   might contain runtime relocations.
522390075Sobrien
522490075Sobrien   We make the section read-only and executable for a function decl,
522590075Sobrien   read-only for a const data decl, and writable for a non-const data decl.  */
522690075Sobrien
522790075Sobrienunsigned int
5228132718Skandefault_section_type_flags (tree decl, const char *name, int reloc)
522990075Sobrien{
523090075Sobrien  unsigned int flags;
523190075Sobrien
523290075Sobrien  if (decl && TREE_CODE (decl) == FUNCTION_DECL)
523390075Sobrien    flags = SECTION_CODE;
5234169689Skan  else if (decl && decl_readonly_section (decl, reloc))
523590075Sobrien    flags = 0;
5236169689Skan  else if (current_function_decl
5237169689Skan	   && cfun
5238169689Skan	   && cfun->unlikely_text_section_name
5239169689Skan	   && strcmp (name, cfun->unlikely_text_section_name) == 0)
5240169689Skan    flags = SECTION_CODE;
5241169689Skan  else if (!decl
5242169689Skan	   && (!current_function_decl || !cfun)
5243169689Skan	   && strcmp (name, UNLIKELY_EXECUTED_TEXT_SECTION_NAME) == 0)
5244169689Skan    flags = SECTION_CODE;
524590075Sobrien  else
524690075Sobrien    flags = SECTION_WRITE;
524790075Sobrien
524890075Sobrien  if (decl && DECL_ONE_ONLY (decl))
524990075Sobrien    flags |= SECTION_LINKONCE;
525090075Sobrien
5251169689Skan  if (decl && TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
5252117395Skan    flags |= SECTION_TLS | SECTION_WRITE;
5253117395Skan
525490075Sobrien  if (strcmp (name, ".bss") == 0
525590075Sobrien      || strncmp (name, ".bss.", 5) == 0
525690075Sobrien      || strncmp (name, ".gnu.linkonce.b.", 16) == 0
525790075Sobrien      || strcmp (name, ".sbss") == 0
525890075Sobrien      || strncmp (name, ".sbss.", 6) == 0
5259169689Skan      || strncmp (name, ".gnu.linkonce.sb.", 17) == 0)
526090075Sobrien    flags |= SECTION_BSS;
526190075Sobrien
5262117395Skan  if (strcmp (name, ".tdata") == 0
5263169689Skan      || strncmp (name, ".tdata.", 7) == 0
5264169689Skan      || strncmp (name, ".gnu.linkonce.td.", 17) == 0)
5265117395Skan    flags |= SECTION_TLS;
5266117395Skan
5267169689Skan  if (strcmp (name, ".tbss") == 0
5268169689Skan      || strncmp (name, ".tbss.", 6) == 0
5269169689Skan      || strncmp (name, ".gnu.linkonce.tb.", 17) == 0)
5270169689Skan    flags |= SECTION_TLS | SECTION_BSS;
5271169689Skan
5272117395Skan  /* These three sections have special ELF types.  They are neither
5273117395Skan     SHT_PROGBITS nor SHT_NOBITS, so when changing sections we don't
5274117395Skan     want to print a section type (@progbits or @nobits).  If someone
5275117395Skan     is silly enough to emit code or TLS variables to one of these
5276117395Skan     sections, then don't handle them specially.  */
5277117395Skan  if (!(flags & (SECTION_CODE | SECTION_BSS | SECTION_TLS))
5278117395Skan      && (strcmp (name, ".init_array") == 0
5279117395Skan	  || strcmp (name, ".fini_array") == 0
5280117395Skan	  || strcmp (name, ".preinit_array") == 0))
5281117395Skan    flags |= SECTION_NOTYPE;
5282117395Skan
528390075Sobrien  return flags;
528490075Sobrien}
528590075Sobrien
5286169689Skan/* Return true if the target supports some form of global BSS,
5287169689Skan   either through bss_noswitch_section, or by selecting a BSS
5288169689Skan   section in TARGET_ASM_SELECT_SECTION.  */
5289169689Skan
5290169689Skanbool
5291169689Skanhave_global_bss_p (void)
5292169689Skan{
5293169689Skan  return bss_noswitch_section || targetm.have_switchable_bss_sections;
5294169689Skan}
5295169689Skan
529690075Sobrien/* Output assembly to switch to section NAME with attribute FLAGS.
529790075Sobrien   Four variants for common object file formats.  */
529890075Sobrien
529990075Sobrienvoid
5300132718Skandefault_no_named_section (const char *name ATTRIBUTE_UNUSED,
5301169689Skan			  unsigned int flags ATTRIBUTE_UNUSED,
5302169689Skan			  tree decl ATTRIBUTE_UNUSED)
530390075Sobrien{
530490075Sobrien  /* Some object formats don't support named sections at all.  The
530590075Sobrien     front-end should already have flagged this as an error.  */
5306169689Skan  gcc_unreachable ();
530790075Sobrien}
530890075Sobrien
530990075Sobrienvoid
5310169689Skandefault_elf_asm_named_section (const char *name, unsigned int flags,
5311169689Skan			       tree decl ATTRIBUTE_UNUSED)
531290075Sobrien{
531390075Sobrien  char flagchars[10], *f = flagchars;
531490075Sobrien
5315169689Skan  /* If we have already declared this section, we can use an
5316169689Skan     abbreviated form to switch back to it -- unless this section is
5317169689Skan     part of a COMDAT groups, in which case GAS requires the full
5318169689Skan     declaration every time.  */
5319169689Skan  if (!(HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
5320169689Skan      && (flags & SECTION_DECLARED))
532190075Sobrien    {
532290075Sobrien      fprintf (asm_out_file, "\t.section\t%s\n", name);
532390075Sobrien      return;
532490075Sobrien    }
532590075Sobrien
532690075Sobrien  if (!(flags & SECTION_DEBUG))
532790075Sobrien    *f++ = 'a';
532890075Sobrien  if (flags & SECTION_WRITE)
532990075Sobrien    *f++ = 'w';
533090075Sobrien  if (flags & SECTION_CODE)
533190075Sobrien    *f++ = 'x';
533290075Sobrien  if (flags & SECTION_SMALL)
533390075Sobrien    *f++ = 's';
533490075Sobrien  if (flags & SECTION_MERGE)
533590075Sobrien    *f++ = 'M';
533690075Sobrien  if (flags & SECTION_STRINGS)
533790075Sobrien    *f++ = 'S';
5338117395Skan  if (flags & SECTION_TLS)
5339117395Skan    *f++ = 'T';
5340169689Skan  if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
5341169689Skan    *f++ = 'G';
534290075Sobrien  *f = '\0';
534390075Sobrien
5344117395Skan  fprintf (asm_out_file, "\t.section\t%s,\"%s\"", name, flagchars);
534590075Sobrien
5346117395Skan  if (!(flags & SECTION_NOTYPE))
5347117395Skan    {
5348117395Skan      const char *type;
5349169689Skan      const char *format;
5350117395Skan
5351117395Skan      if (flags & SECTION_BSS)
5352117395Skan	type = "nobits";
5353117395Skan      else
5354117395Skan	type = "progbits";
5355117395Skan
5356169689Skan      format = ",@%s";
5357169689Skan#ifdef ASM_COMMENT_START
5358169689Skan      /* On platforms that use "@" as the assembly comment character,
5359169689Skan	 use "%" instead.  */
5360169689Skan      if (strcmp (ASM_COMMENT_START, "@") == 0)
5361169689Skan	format = ",%%%s";
5362169689Skan#endif
5363169689Skan      fprintf (asm_out_file, format, type);
5364117395Skan
5365117395Skan      if (flags & SECTION_ENTSIZE)
5366117395Skan	fprintf (asm_out_file, ",%d", flags & SECTION_ENTSIZE);
5367169689Skan      if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
5368169689Skan	fprintf (asm_out_file, ",%s,comdat",
5369169689Skan		 lang_hooks.decls.comdat_group (decl));
5370117395Skan    }
5371117395Skan
5372117395Skan  putc ('\n', asm_out_file);
537390075Sobrien}
537490075Sobrien
537590075Sobrienvoid
5376169689Skandefault_coff_asm_named_section (const char *name, unsigned int flags,
5377169689Skan				tree decl ATTRIBUTE_UNUSED)
537890075Sobrien{
537990075Sobrien  char flagchars[8], *f = flagchars;
538090075Sobrien
538190075Sobrien  if (flags & SECTION_WRITE)
538290075Sobrien    *f++ = 'w';
538390075Sobrien  if (flags & SECTION_CODE)
538490075Sobrien    *f++ = 'x';
538590075Sobrien  *f = '\0';
538690075Sobrien
538790075Sobrien  fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
538890075Sobrien}
538990075Sobrien
539090075Sobrienvoid
5391169689Skandefault_pe_asm_named_section (const char *name, unsigned int flags,
5392169689Skan			      tree decl)
539390075Sobrien{
5394169689Skan  default_coff_asm_named_section (name, flags, decl);
539590075Sobrien
539690075Sobrien  if (flags & SECTION_LINKONCE)
539790075Sobrien    {
539890075Sobrien      /* Functions may have been compiled at various levels of
539990075Sobrien         optimization so we can't use `same_size' here.
540090075Sobrien         Instead, have the linker pick one.  */
540190075Sobrien      fprintf (asm_out_file, "\t.linkonce %s\n",
540290075Sobrien	       (flags & SECTION_CODE ? "discard" : "same_size"));
540390075Sobrien    }
540490075Sobrien}
540590075Sobrien
5406117395Skan/* The lame default section selector.  */
5407117395Skan
5408169689Skansection *
5409132718Skandefault_select_section (tree decl, int reloc,
5410132718Skan			unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
5411117395Skan{
5412117395Skan  if (DECL_P (decl))
5413117395Skan    {
5414117395Skan      if (decl_readonly_section (decl, reloc))
5415169689Skan	return readonly_data_section;
5416117395Skan    }
5417117395Skan  else if (TREE_CODE (decl) == CONSTRUCTOR)
5418117395Skan    {
5419117395Skan      if (! ((flag_pic && reloc)
5420117395Skan	     || !TREE_READONLY (decl)
5421117395Skan	     || TREE_SIDE_EFFECTS (decl)
5422117395Skan	     || !TREE_CONSTANT (decl)))
5423169689Skan	return readonly_data_section;
5424117395Skan    }
5425117395Skan  else if (TREE_CODE (decl) == STRING_CST)
5426169689Skan    return readonly_data_section;
5427117395Skan  else if (! (flag_pic && reloc))
5428169689Skan    return readonly_data_section;
5429117395Skan
5430169689Skan  return data_section;
5431117395Skan}
5432117395Skan
5433117395Skanenum section_category
5434169689Skancategorize_decl_for_section (tree decl, int reloc)
5435117395Skan{
5436117395Skan  enum section_category ret;
5437117395Skan
5438117395Skan  if (TREE_CODE (decl) == FUNCTION_DECL)
5439117395Skan    return SECCAT_TEXT;
5440117395Skan  else if (TREE_CODE (decl) == STRING_CST)
5441117395Skan    {
5442169689Skan      if (flag_mudflap) /* or !flag_merge_constants */
5443169689Skan        return SECCAT_RODATA;
5444117395Skan      else
5445117395Skan	return SECCAT_RODATA_MERGE_STR;
5446117395Skan    }
5447117395Skan  else if (TREE_CODE (decl) == VAR_DECL)
5448117395Skan    {
5449169689Skan      if (bss_initializer_p (decl))
5450117395Skan	ret = SECCAT_BSS;
5451117395Skan      else if (! TREE_READONLY (decl)
5452117395Skan	       || TREE_SIDE_EFFECTS (decl)
5453117395Skan	       || ! TREE_CONSTANT (DECL_INITIAL (decl)))
5454117395Skan	{
5455169689Skan	  /* Here the reloc_rw_mask is not testing whether the section should
5456169689Skan	     be read-only or not, but whether the dynamic link will have to
5457169689Skan	     do something.  If so, we wish to segregate the data in order to
5458169689Skan	     minimize cache misses inside the dynamic linker.  */
5459169689Skan	  if (reloc & targetm.asm_out.reloc_rw_mask ())
5460169689Skan	    ret = reloc == 1 ? SECCAT_DATA_REL_LOCAL : SECCAT_DATA_REL;
5461117395Skan	  else
5462117395Skan	    ret = SECCAT_DATA;
5463117395Skan	}
5464169689Skan      else if (reloc & targetm.asm_out.reloc_rw_mask ())
5465169689Skan	ret = reloc == 1 ? SECCAT_DATA_REL_RO_LOCAL : SECCAT_DATA_REL_RO;
5466117395Skan      else if (reloc || flag_merge_constants < 2)
5467117395Skan	/* C and C++ don't allow different variables to share the same
5468117395Skan	   location.  -fmerge-all-constants allows even that (at the
5469117395Skan	   expense of not conforming).  */
5470117395Skan	ret = SECCAT_RODATA;
5471117395Skan      else if (TREE_CODE (DECL_INITIAL (decl)) == STRING_CST)
5472117395Skan	ret = SECCAT_RODATA_MERGE_STR_INIT;
5473117395Skan      else
5474117395Skan	ret = SECCAT_RODATA_MERGE_CONST;
5475117395Skan    }
5476117395Skan  else if (TREE_CODE (decl) == CONSTRUCTOR)
5477117395Skan    {
5478169689Skan      if ((reloc & targetm.asm_out.reloc_rw_mask ())
5479117395Skan	  || TREE_SIDE_EFFECTS (decl)
5480117395Skan	  || ! TREE_CONSTANT (decl))
5481117395Skan	ret = SECCAT_DATA;
5482117395Skan      else
5483117395Skan	ret = SECCAT_RODATA;
5484117395Skan    }
5485117395Skan  else
5486117395Skan    ret = SECCAT_RODATA;
5487117395Skan
5488117395Skan  /* There are no read-only thread-local sections.  */
5489169689Skan  if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
5490117395Skan    {
5491169689Skan      /* Note that this would be *just* SECCAT_BSS, except that there's
5492169689Skan	 no concept of a read-only thread-local-data section.  */
5493169689Skan      if (ret == SECCAT_BSS
5494169689Skan	  || (flag_zero_initialized_in_bss
5495169689Skan	      && initializer_zerop (DECL_INITIAL (decl))))
5496117395Skan	ret = SECCAT_TBSS;
5497117395Skan      else
5498117395Skan	ret = SECCAT_TDATA;
5499117395Skan    }
5500117395Skan
5501117395Skan  /* If the target uses small data sections, select it.  */
5502169689Skan  else if (targetm.in_small_data_p (decl))
5503117395Skan    {
5504117395Skan      if (ret == SECCAT_BSS)
5505117395Skan	ret = SECCAT_SBSS;
5506117395Skan      else if (targetm.have_srodata_section && ret == SECCAT_RODATA)
5507117395Skan	ret = SECCAT_SRODATA;
5508117395Skan      else
5509117395Skan	ret = SECCAT_SDATA;
5510117395Skan    }
5511117395Skan
5512117395Skan  return ret;
5513117395Skan}
5514117395Skan
5515117395Skanbool
5516132718Skandecl_readonly_section (tree decl, int reloc)
5517117395Skan{
5518169689Skan  switch (categorize_decl_for_section (decl, reloc))
5519117395Skan    {
5520117395Skan    case SECCAT_RODATA:
5521117395Skan    case SECCAT_RODATA_MERGE_STR:
5522117395Skan    case SECCAT_RODATA_MERGE_STR_INIT:
5523117395Skan    case SECCAT_RODATA_MERGE_CONST:
5524117395Skan    case SECCAT_SRODATA:
5525117395Skan      return true;
5526117395Skan      break;
5527117395Skan    default:
5528117395Skan      return false;
5529117395Skan      break;
5530117395Skan    }
5531117395Skan}
5532117395Skan
5533117395Skan/* Select a section based on the above categorization.  */
5534117395Skan
5535169689Skansection *
5536132718Skandefault_elf_select_section (tree decl, int reloc,
5537132718Skan			    unsigned HOST_WIDE_INT align)
5538117395Skan{
5539169689Skan  const char *sname;
5540169689Skan  switch (categorize_decl_for_section (decl, reloc))
5541117395Skan    {
5542117395Skan    case SECCAT_TEXT:
5543117395Skan      /* We're not supposed to be called on FUNCTION_DECLs.  */
5544169689Skan      gcc_unreachable ();
5545117395Skan    case SECCAT_RODATA:
5546169689Skan      return readonly_data_section;
5547117395Skan    case SECCAT_RODATA_MERGE_STR:
5548169689Skan      return mergeable_string_section (decl, align, 0);
5549117395Skan    case SECCAT_RODATA_MERGE_STR_INIT:
5550169689Skan      return mergeable_string_section (DECL_INITIAL (decl), align, 0);
5551117395Skan    case SECCAT_RODATA_MERGE_CONST:
5552169689Skan      return mergeable_constant_section (DECL_MODE (decl), align, 0);
5553117395Skan    case SECCAT_SRODATA:
5554169689Skan      sname = ".sdata2";
5555117395Skan      break;
5556117395Skan    case SECCAT_DATA:
5557169689Skan      return data_section;
5558117395Skan    case SECCAT_DATA_REL:
5559169689Skan      sname = ".data.rel";
5560117395Skan      break;
5561117395Skan    case SECCAT_DATA_REL_LOCAL:
5562169689Skan      sname = ".data.rel.local";
5563117395Skan      break;
5564117395Skan    case SECCAT_DATA_REL_RO:
5565169689Skan      sname = ".data.rel.ro";
5566117395Skan      break;
5567117395Skan    case SECCAT_DATA_REL_RO_LOCAL:
5568169689Skan      sname = ".data.rel.ro.local";
5569117395Skan      break;
5570117395Skan    case SECCAT_SDATA:
5571169689Skan      sname = ".sdata";
5572117395Skan      break;
5573117395Skan    case SECCAT_TDATA:
5574169689Skan      sname = ".tdata";
5575117395Skan      break;
5576117395Skan    case SECCAT_BSS:
5577169689Skan      if (bss_section)
5578169689Skan	return bss_section;
5579169689Skan      sname = ".bss";
5580117395Skan      break;
5581117395Skan    case SECCAT_SBSS:
5582169689Skan      sname = ".sbss";
5583117395Skan      break;
5584117395Skan    case SECCAT_TBSS:
5585169689Skan      sname = ".tbss";
5586117395Skan      break;
5587117395Skan    default:
5588169689Skan      gcc_unreachable ();
5589117395Skan    }
5590169689Skan
5591169689Skan  if (!DECL_P (decl))
5592169689Skan    decl = NULL_TREE;
5593169689Skan  return get_named_section (decl, sname, reloc);
5594117395Skan}
5595117395Skan
5596117395Skan/* Construct a unique section name based on the decl name and the
5597117395Skan   categorization performed above.  */
5598117395Skan
5599117395Skanvoid
5600132718Skandefault_unique_section (tree decl, int reloc)
5601117395Skan{
5602169689Skan  /* We only need to use .gnu.linkonce if we don't have COMDAT groups.  */
5603169689Skan  bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
5604117395Skan  const char *prefix, *name;
5605117395Skan  size_t nlen, plen;
5606117395Skan  char *string;
5607117395Skan
5608169689Skan  switch (categorize_decl_for_section (decl, reloc))
5609117395Skan    {
5610117395Skan    case SECCAT_TEXT:
5611117395Skan      prefix = one_only ? ".gnu.linkonce.t." : ".text.";
5612117395Skan      break;
5613117395Skan    case SECCAT_RODATA:
5614117395Skan    case SECCAT_RODATA_MERGE_STR:
5615117395Skan    case SECCAT_RODATA_MERGE_STR_INIT:
5616117395Skan    case SECCAT_RODATA_MERGE_CONST:
5617117395Skan      prefix = one_only ? ".gnu.linkonce.r." : ".rodata.";
5618117395Skan      break;
5619117395Skan    case SECCAT_SRODATA:
5620117395Skan      prefix = one_only ? ".gnu.linkonce.s2." : ".sdata2.";
5621117395Skan      break;
5622117395Skan    case SECCAT_DATA:
5623169689Skan      prefix = one_only ? ".gnu.linkonce.d." : ".data.";
5624169689Skan      break;
5625117395Skan    case SECCAT_DATA_REL:
5626169689Skan      prefix = one_only ? ".gnu.linkonce.d.rel." : ".data.rel.";
5627169689Skan      break;
5628117395Skan    case SECCAT_DATA_REL_LOCAL:
5629169689Skan      prefix = one_only ? ".gnu.linkonce.d.rel.local." : ".data.rel.local.";
5630169689Skan      break;
5631117395Skan    case SECCAT_DATA_REL_RO:
5632169689Skan      prefix = one_only ? ".gnu.linkonce.d.rel.ro." : ".data.rel.ro.";
5633169689Skan      break;
5634117395Skan    case SECCAT_DATA_REL_RO_LOCAL:
5635169689Skan      prefix = one_only ? ".gnu.linkonce.d.rel.ro.local."
5636169689Skan	       : ".data.rel.ro.local.";
5637117395Skan      break;
5638117395Skan    case SECCAT_SDATA:
5639117395Skan      prefix = one_only ? ".gnu.linkonce.s." : ".sdata.";
5640117395Skan      break;
5641117395Skan    case SECCAT_BSS:
5642117395Skan      prefix = one_only ? ".gnu.linkonce.b." : ".bss.";
5643117395Skan      break;
5644117395Skan    case SECCAT_SBSS:
5645117395Skan      prefix = one_only ? ".gnu.linkonce.sb." : ".sbss.";
5646117395Skan      break;
5647117395Skan    case SECCAT_TDATA:
5648117395Skan      prefix = one_only ? ".gnu.linkonce.td." : ".tdata.";
5649117395Skan      break;
5650117395Skan    case SECCAT_TBSS:
5651117395Skan      prefix = one_only ? ".gnu.linkonce.tb." : ".tbss.";
5652117395Skan      break;
5653117395Skan    default:
5654169689Skan      gcc_unreachable ();
5655117395Skan    }
5656117395Skan  plen = strlen (prefix);
5657117395Skan
5658117395Skan  name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5659169689Skan  name = targetm.strip_name_encoding (name);
5660117395Skan  nlen = strlen (name);
5661117395Skan
5662117395Skan  string = alloca (nlen + plen + 1);
5663117395Skan  memcpy (string, prefix, plen);
5664117395Skan  memcpy (string + plen, name, nlen + 1);
5665117395Skan
5666117395Skan  DECL_SECTION_NAME (decl) = build_string (nlen + plen, string);
5667117395Skan}
5668117395Skan
5669169689Skan/* Like compute_reloc_for_constant, except for an RTX.  The return value
5670169689Skan   is a mask for which bit 1 indicates a global relocation, and bit 0
5671169689Skan   indicates a local relocation.  */
5672169689Skan
5673169689Skanstatic int
5674169689Skancompute_reloc_for_rtx_1 (rtx *xp, void *data)
5675169689Skan{
5676169689Skan  int *preloc = data;
5677169689Skan  rtx x = *xp;
5678169689Skan
5679169689Skan  switch (GET_CODE (x))
5680169689Skan    {
5681169689Skan    case SYMBOL_REF:
5682169689Skan      *preloc |= SYMBOL_REF_LOCAL_P (x) ? 1 : 2;
5683169689Skan      break;
5684169689Skan    case LABEL_REF:
5685169689Skan      *preloc |= 1;
5686169689Skan      break;
5687169689Skan    default:
5688169689Skan      break;
5689169689Skan    }
5690169689Skan
5691169689Skan  return 0;
5692169689Skan}
5693169689Skan
5694169689Skanstatic int
5695169689Skancompute_reloc_for_rtx (rtx x)
5696169689Skan{
5697169689Skan  int reloc;
5698169689Skan
5699169689Skan  switch (GET_CODE (x))
5700169689Skan    {
5701169689Skan    case CONST:
5702169689Skan    case SYMBOL_REF:
5703169689Skan    case LABEL_REF:
5704169689Skan      reloc = 0;
5705169689Skan      for_each_rtx (&x, compute_reloc_for_rtx_1, &reloc);
5706169689Skan      return reloc;
5707169689Skan
5708169689Skan    default:
5709169689Skan      return 0;
5710169689Skan    }
5711169689Skan}
5712169689Skan
5713169689Skansection *
5714132718Skandefault_select_rtx_section (enum machine_mode mode ATTRIBUTE_UNUSED,
5715132718Skan			    rtx x,
5716132718Skan			    unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
5717117395Skan{
5718169689Skan  if (compute_reloc_for_rtx (x) & targetm.asm_out.reloc_rw_mask ())
5719169689Skan    return data_section;
5720169689Skan  else
5721169689Skan    return readonly_data_section;
5722117395Skan}
5723117395Skan
5724169689Skansection *
5725132718Skandefault_elf_select_rtx_section (enum machine_mode mode, rtx x,
5726132718Skan				unsigned HOST_WIDE_INT align)
5727117395Skan{
5728169689Skan  int reloc = compute_reloc_for_rtx (x);
5729169689Skan
5730117395Skan  /* ??? Handle small data here somehow.  */
5731117395Skan
5732169689Skan  if (reloc & targetm.asm_out.reloc_rw_mask ())
5733169689Skan    {
5734169689Skan      if (reloc == 1)
5735169689Skan	return get_named_section (NULL, ".data.rel.ro.local", 1);
5736169689Skan      else
5737169689Skan	return get_named_section (NULL, ".data.rel.ro", 3);
5738169689Skan    }
5739117395Skan
5740169689Skan  return mergeable_constant_section (mode, align, 0);
5741117395Skan}
5742117395Skan
5743132718Skan/* Set the generally applicable flags on the SYMBOL_REF for EXP.  */
5744132718Skan
5745132718Skanvoid
5746132718Skandefault_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
5747132718Skan{
5748132718Skan  rtx symbol;
5749132718Skan  int flags;
5750132718Skan
5751132718Skan  /* Careful not to prod global register variables.  */
5752169689Skan  if (!MEM_P (rtl))
5753132718Skan    return;
5754132718Skan  symbol = XEXP (rtl, 0);
5755132718Skan  if (GET_CODE (symbol) != SYMBOL_REF)
5756132718Skan    return;
5757132718Skan
5758169689Skan  flags = SYMBOL_REF_FLAGS (symbol) & SYMBOL_FLAG_HAS_BLOCK_INFO;
5759132718Skan  if (TREE_CODE (decl) == FUNCTION_DECL)
5760132718Skan    flags |= SYMBOL_FLAG_FUNCTION;
5761169689Skan  if (targetm.binds_local_p (decl))
5762132718Skan    flags |= SYMBOL_FLAG_LOCAL;
5763169689Skan  if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
5764169689Skan    flags |= DECL_TLS_MODEL (decl) << SYMBOL_FLAG_TLS_SHIFT;
5765169689Skan  else if (targetm.in_small_data_p (decl))
5766132718Skan    flags |= SYMBOL_FLAG_SMALL;
5767132718Skan  /* ??? Why is DECL_EXTERNAL ever set for non-PUBLIC names?  Without
5768132718Skan     being PUBLIC, the thing *must* be defined in this translation unit.
5769132718Skan     Prevent this buglet from being propagated into rtl code as well.  */
5770132718Skan  if (DECL_P (decl) && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl))
5771132718Skan    flags |= SYMBOL_FLAG_EXTERNAL;
5772132718Skan
5773132718Skan  SYMBOL_REF_FLAGS (symbol) = flags;
5774132718Skan}
5775132718Skan
5776117395Skan/* By default, we do nothing for encode_section_info, so we need not
5777117395Skan   do anything but discard the '*' marker.  */
5778117395Skan
5779117395Skanconst char *
5780132718Skandefault_strip_name_encoding (const char *str)
5781117395Skan{
5782117395Skan  return str + (*str == '*');
5783117395Skan}
5784117395Skan
5785169689Skan#ifdef ASM_OUTPUT_DEF
5786169689Skan/* The default implementation of TARGET_ASM_OUTPUT_ANCHOR.  Define the
5787169689Skan   anchor relative to ".", the current section position.  */
5788169689Skan
5789169689Skanvoid
5790169689Skandefault_asm_output_anchor (rtx symbol)
5791169689Skan{
5792169689Skan  char buffer[100];
5793169689Skan
5794169689Skan  sprintf (buffer, ". + " HOST_WIDE_INT_PRINT_DEC,
5795169689Skan	   SYMBOL_REF_BLOCK_OFFSET (symbol));
5796169689Skan  ASM_OUTPUT_DEF (asm_out_file, XSTR (symbol, 0), buffer);
5797169689Skan}
5798169689Skan#endif
5799169689Skan
5800169689Skan/* The default implementation of TARGET_USE_ANCHORS_FOR_SYMBOL_P.  */
5801169689Skan
5802169689Skanbool
5803169689Skandefault_use_anchors_for_symbol_p (rtx symbol)
5804169689Skan{
5805169689Skan  section *sect;
5806169689Skan  tree decl;
5807169689Skan
5808169689Skan  /* Don't use anchors for mergeable sections.  The linker might move
5809169689Skan     the objects around.  */
5810169689Skan  sect = SYMBOL_REF_BLOCK (symbol)->sect;
5811169689Skan  if (sect->common.flags & SECTION_MERGE)
5812169689Skan    return false;
5813169689Skan
5814169689Skan  /* Don't use anchors for small data sections.  The small data register
5815169689Skan     acts as an anchor for such sections.  */
5816169689Skan  if (sect->common.flags & SECTION_SMALL)
5817169689Skan    return false;
5818169689Skan
5819169689Skan  decl = SYMBOL_REF_DECL (symbol);
5820169689Skan  if (decl && DECL_P (decl))
5821169689Skan    {
5822169689Skan      /* Don't use section anchors for decls that might be defined by
5823169689Skan	 other modules.  */
5824169689Skan      if (!targetm.binds_local_p (decl))
5825169689Skan	return false;
5826169689Skan
5827169689Skan      /* Don't use section anchors for decls that will be placed in a
5828169689Skan	 small data section.  */
5829169689Skan      /* ??? Ideally, this check would be redundant with the SECTION_SMALL
5830169689Skan	 one above.  The problem is that we only use SECTION_SMALL for
5831169689Skan	 sections that should be marked as small in the section directive.  */
5832169689Skan      if (targetm.in_small_data_p (decl))
5833169689Skan	return false;
5834169689Skan    }
5835169689Skan  return true;
5836169689Skan}
5837169689Skan
5838117395Skan/* Assume ELF-ish defaults, since that's pretty much the most liberal
5839117395Skan   wrt cross-module name binding.  */
5840117395Skan
5841117395Skanbool
5842132718Skandefault_binds_local_p (tree exp)
5843117395Skan{
5844132718Skan  return default_binds_local_p_1 (exp, flag_shlib);
5845117395Skan}
5846117395Skan
5847117395Skanbool
5848132718Skandefault_binds_local_p_1 (tree exp, int shlib)
5849117395Skan{
5850117395Skan  bool local_p;
5851117395Skan
5852117395Skan  /* A non-decl is an entry in the constant pool.  */
5853117395Skan  if (!DECL_P (exp))
5854117395Skan    local_p = true;
5855169689Skan  /* Weakrefs may not bind locally, even though the weakref itself is
5856169689Skan     always static and therefore local.  */
5857169689Skan  else if (lookup_attribute ("weakref", DECL_ATTRIBUTES (exp)))
5858169689Skan    local_p = false;
5859117395Skan  /* Static variables are always local.  */
5860117395Skan  else if (! TREE_PUBLIC (exp))
5861117395Skan    local_p = true;
5862169689Skan  /* A variable is local if the user has said explicitly that it will
5863169689Skan     be.  */
5864169689Skan  else if (DECL_VISIBILITY_SPECIFIED (exp)
5865169689Skan	   && DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
5866117395Skan    local_p = true;
5867169689Skan  /* Variables defined outside this object might not be local.  */
5868117395Skan  else if (DECL_EXTERNAL (exp))
5869117395Skan    local_p = false;
5870169689Skan  /* If defined in this object and visibility is not default, must be
5871169689Skan     local.  */
5872169689Skan  else if (DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
5873169689Skan    local_p = true;
5874169689Skan  /* Default visibility weak data can be overridden by a strong symbol
5875169689Skan     in another module and so are not local.  */
5876169689Skan  else if (DECL_WEAK (exp))
5877117395Skan    local_p = false;
5878117395Skan  /* If PIC, then assume that any global name can be overridden by
5879235623Spfg     symbols resolved from other modules, unless we are compiling with
5880235623Spfg     -fwhole-program, which assumes that names are local.  */
5881117395Skan  else if (shlib)
5882235623Spfg    local_p = flag_whole_program;
5883117395Skan  /* Uninitialized COMMON variable may be unified with symbols
5884117395Skan     resolved from other modules.  */
5885117395Skan  else if (DECL_COMMON (exp)
5886117395Skan	   && (DECL_INITIAL (exp) == NULL
5887117395Skan	       || DECL_INITIAL (exp) == error_mark_node))
5888117395Skan    local_p = false;
5889117395Skan  /* Otherwise we're left with initialized (or non-common) global data
5890117395Skan     which is of necessity defined locally.  */
5891117395Skan  else
5892117395Skan    local_p = true;
5893117395Skan
5894117395Skan  return local_p;
5895117395Skan}
5896117395Skan
5897132718Skan/* Determine whether or not a pointer mode is valid. Assume defaults
5898132718Skan   of ptr_mode or Pmode - can be overridden.  */
5899132718Skanbool
5900132718Skandefault_valid_pointer_mode (enum machine_mode mode)
5901132718Skan{
5902132718Skan  return (mode == ptr_mode || mode == Pmode);
5903132718Skan}
5904132718Skan
5905117395Skan/* Default function to output code that will globalize a label.  A
5906169689Skan   target must define GLOBAL_ASM_OP or provide its own function to
5907117395Skan   globalize a label.  */
5908117395Skan#ifdef GLOBAL_ASM_OP
5909117395Skanvoid
5910132718Skandefault_globalize_label (FILE * stream, const char *name)
5911117395Skan{
5912117395Skan  fputs (GLOBAL_ASM_OP, stream);
5913117395Skan  assemble_name (stream, name);
5914117395Skan  putc ('\n', stream);
5915117395Skan}
5916117395Skan#endif /* GLOBAL_ASM_OP */
5917132718Skan
5918169689Skan/* Default function to output a label for unwind information.  The
5919169689Skan   default is to do nothing.  A target that needs nonlocal labels for
5920169689Skan   unwind information must provide its own function to do this.  */
5921169689Skanvoid
5922169689Skandefault_emit_unwind_label (FILE * stream ATTRIBUTE_UNUSED,
5923169689Skan			   tree decl ATTRIBUTE_UNUSED,
5924169689Skan			   int for_eh ATTRIBUTE_UNUSED,
5925169689Skan			   int empty ATTRIBUTE_UNUSED)
5926169689Skan{
5927169689Skan}
5928169689Skan
5929169689Skan/* Default function to output a label to divide up the exception table.
5930169689Skan   The default is to do nothing.  A target that needs/wants to divide
5931169689Skan   up the table must provide it's own function to do this.  */
5932169689Skanvoid
5933169689Skandefault_emit_except_table_label (FILE * stream ATTRIBUTE_UNUSED)
5934169689Skan{
5935169689Skan}
5936169689Skan
5937132718Skan/* This is how to output an internal numbered label where PREFIX is
5938132718Skan   the class of label and LABELNO is the number within the class.  */
5939132718Skan
5940132718Skanvoid
5941132718Skandefault_internal_label (FILE *stream, const char *prefix,
5942132718Skan			unsigned long labelno)
5943132718Skan{
5944132718Skan  char *const buf = alloca (40 + strlen (prefix));
5945132718Skan  ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
5946169689Skan  ASM_OUTPUT_INTERNAL_LABEL (stream, buf);
5947132718Skan}
5948132718Skan
5949132718Skan/* This is the default behavior at the beginning of a file.  It's
5950132718Skan   controlled by two other target-hook toggles.  */
5951132718Skanvoid
5952132718Skandefault_file_start (void)
5953132718Skan{
5954132718Skan  if (targetm.file_start_app_off && !flag_verbose_asm)
5955132718Skan    fputs (ASM_APP_OFF, asm_out_file);
5956132718Skan
5957132718Skan  if (targetm.file_start_file_directive)
5958132718Skan    output_file_directive (asm_out_file, main_input_filename);
5959132718Skan}
5960132718Skan
5961132718Skan/* This is a generic routine suitable for use as TARGET_ASM_FILE_END
5962132718Skan   which emits a special section directive used to indicate whether or
5963132718Skan   not this object file needs an executable stack.  This is primarily
5964132718Skan   a GNU extension to ELF but could be used on other targets.  */
5965169689Skan
5966169689Skanint trampolines_created;
5967169689Skan
5968132718Skanvoid
5969132718Skanfile_end_indicate_exec_stack (void)
5970132718Skan{
5971132718Skan  unsigned int flags = SECTION_DEBUG;
5972132718Skan  if (trampolines_created)
5973132718Skan    flags |= SECTION_CODE;
5974132718Skan
5975169689Skan  switch_to_section (get_section (".note.GNU-stack", flags, NULL));
5976132718Skan}
5977132718Skan
5978169689Skan/* Output DIRECTIVE (a C string) followed by a newline.  This is used as
5979169689Skan   a get_unnamed_section callback.  */
5980169689Skan
5981169689Skanvoid
5982169689Skanoutput_section_asm_op (const void *directive)
5983169689Skan{
5984169689Skan  fprintf (asm_out_file, "%s\n", (const char *) directive);
5985169689Skan}
5986169689Skan
5987169689Skan/* Emit assembly code to switch to section NEW_SECTION.  Do nothing if
5988169689Skan   the current section is NEW_SECTION.  */
5989169689Skan
5990169689Skanvoid
5991169689Skanswitch_to_section (section *new_section)
5992169689Skan{
5993169689Skan  if (in_section == new_section)
5994169689Skan    return;
5995169689Skan
5996169689Skan  if (new_section->common.flags & SECTION_FORGET)
5997169689Skan    in_section = NULL;
5998169689Skan  else
5999169689Skan    in_section = new_section;
6000169689Skan
6001169689Skan  switch (SECTION_STYLE (new_section))
6002169689Skan    {
6003169689Skan    case SECTION_NAMED:
6004169689Skan      if (cfun
6005169689Skan	  && !cfun->unlikely_text_section_name
6006169689Skan	  && strcmp (new_section->named.name,
6007169689Skan		     UNLIKELY_EXECUTED_TEXT_SECTION_NAME) == 0)
6008169689Skan	cfun->unlikely_text_section_name = UNLIKELY_EXECUTED_TEXT_SECTION_NAME;
6009169689Skan
6010169689Skan      targetm.asm_out.named_section (new_section->named.name,
6011169689Skan				     new_section->named.common.flags,
6012169689Skan				     new_section->named.decl);
6013169689Skan      break;
6014169689Skan
6015169689Skan    case SECTION_UNNAMED:
6016169689Skan      new_section->unnamed.callback (new_section->unnamed.data);
6017169689Skan      break;
6018169689Skan
6019169689Skan    case SECTION_NOSWITCH:
6020169689Skan      gcc_unreachable ();
6021169689Skan      break;
6022169689Skan    }
6023169689Skan
6024169689Skan  new_section->common.flags |= SECTION_DECLARED;
6025169689Skan}
6026169689Skan
6027169689Skan/* If block symbol SYMBOL has not yet been assigned an offset, place
6028169689Skan   it at the end of its block.  */
6029169689Skan
6030169689Skanvoid
6031169689Skanplace_block_symbol (rtx symbol)
6032169689Skan{
6033169689Skan  unsigned HOST_WIDE_INT size, mask, offset;
6034169689Skan  struct constant_descriptor_rtx *desc;
6035169689Skan  unsigned int alignment;
6036169689Skan  struct object_block *block;
6037169689Skan  tree decl;
6038169689Skan
6039169689Skan  gcc_assert (SYMBOL_REF_BLOCK (symbol));
6040169689Skan  if (SYMBOL_REF_BLOCK_OFFSET (symbol) >= 0)
6041169689Skan    return;
6042169689Skan
6043169689Skan  /* Work out the symbol's size and alignment.  */
6044169689Skan  if (CONSTANT_POOL_ADDRESS_P (symbol))
6045169689Skan    {
6046169689Skan      desc = SYMBOL_REF_CONSTANT (symbol);
6047169689Skan      alignment = desc->align;
6048169689Skan      size = GET_MODE_SIZE (desc->mode);
6049169689Skan    }
6050169689Skan  else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
6051169689Skan    {
6052169689Skan      decl = SYMBOL_REF_DECL (symbol);
6053169689Skan      alignment = get_constant_alignment (decl);
6054169689Skan      size = get_constant_size (decl);
6055169689Skan    }
6056169689Skan  else
6057169689Skan    {
6058169689Skan      decl = SYMBOL_REF_DECL (symbol);
6059169689Skan      alignment = DECL_ALIGN (decl);
6060169689Skan      size = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
6061169689Skan    }
6062169689Skan
6063169689Skan  /* Calculate the object's offset from the start of the block.  */
6064169689Skan  block = SYMBOL_REF_BLOCK (symbol);
6065169689Skan  mask = alignment / BITS_PER_UNIT - 1;
6066169689Skan  offset = (block->size + mask) & ~mask;
6067169689Skan  SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
6068169689Skan
6069169689Skan  /* Record the block's new alignment and size.  */
6070169689Skan  block->alignment = MAX (block->alignment, alignment);
6071169689Skan  block->size = offset + size;
6072169689Skan
6073169689Skan  VEC_safe_push (rtx, gc, block->objects, symbol);
6074169689Skan}
6075169689Skan
6076169689Skan/* Return the anchor that should be used to address byte offset OFFSET
6077169689Skan   from the first object in BLOCK.  MODEL is the TLS model used
6078169689Skan   to access it.  */
6079169689Skan
6080169689Skanrtx
6081169689Skanget_section_anchor (struct object_block *block, HOST_WIDE_INT offset,
6082169689Skan		    enum tls_model model)
6083169689Skan{
6084169689Skan  char label[100];
6085169689Skan  unsigned int begin, middle, end;
6086169689Skan  unsigned HOST_WIDE_INT min_offset, max_offset, range, bias, delta;
6087169689Skan  rtx anchor;
6088169689Skan
6089169689Skan  /* Work out the anchor's offset.  Use an offset of 0 for the first
6090169689Skan     anchor so that we don't pessimize the case where we take the address
6091169689Skan     of a variable at the beginning of the block.  This is particularly
6092169689Skan     useful when a block has only one variable assigned to it.
6093169689Skan
6094169689Skan     We try to place anchors RANGE bytes apart, so there can then be
6095169689Skan     anchors at +/-RANGE, +/-2 * RANGE, and so on, up to the limits of
6096169689Skan     a ptr_mode offset.  With some target settings, the lowest such
6097169689Skan     anchor might be out of range for the lowest ptr_mode offset;
6098169689Skan     likewise the highest anchor for the highest offset.  Use anchors
6099169689Skan     at the extreme ends of the ptr_mode range in such cases.
6100169689Skan
6101169689Skan     All arithmetic uses unsigned integers in order to avoid
6102169689Skan     signed overflow.  */
6103169689Skan  max_offset = (unsigned HOST_WIDE_INT) targetm.max_anchor_offset;
6104169689Skan  min_offset = (unsigned HOST_WIDE_INT) targetm.min_anchor_offset;
6105169689Skan  range = max_offset - min_offset + 1;
6106169689Skan  if (range == 0)
6107169689Skan    offset = 0;
6108169689Skan  else
6109169689Skan    {
6110169689Skan      bias = 1 << (GET_MODE_BITSIZE (ptr_mode) - 1);
6111169689Skan      if (offset < 0)
6112169689Skan	{
6113169689Skan	  delta = -(unsigned HOST_WIDE_INT) offset + max_offset;
6114169689Skan	  delta -= delta % range;
6115169689Skan	  if (delta > bias)
6116169689Skan	    delta = bias;
6117169689Skan	  offset = (HOST_WIDE_INT) (-delta);
6118169689Skan	}
6119169689Skan      else
6120169689Skan	{
6121169689Skan	  delta = (unsigned HOST_WIDE_INT) offset - min_offset;
6122169689Skan	  delta -= delta % range;
6123169689Skan	  if (delta > bias - 1)
6124169689Skan	    delta = bias - 1;
6125169689Skan	  offset = (HOST_WIDE_INT) delta;
6126169689Skan	}
6127169689Skan    }
6128169689Skan
6129169689Skan  /* Do a binary search to see if there's already an anchor we can use.
6130169689Skan     Set BEGIN to the new anchor's index if not.  */
6131169689Skan  begin = 0;
6132169689Skan  end = VEC_length (rtx, block->anchors);
6133169689Skan  while (begin != end)
6134169689Skan    {
6135169689Skan      middle = (end + begin) / 2;
6136169689Skan      anchor = VEC_index (rtx, block->anchors, middle);
6137169689Skan      if (SYMBOL_REF_BLOCK_OFFSET (anchor) > offset)
6138169689Skan	end = middle;
6139169689Skan      else if (SYMBOL_REF_BLOCK_OFFSET (anchor) < offset)
6140169689Skan	begin = middle + 1;
6141169689Skan      else if (SYMBOL_REF_TLS_MODEL (anchor) > model)
6142169689Skan	end = middle;
6143169689Skan      else if (SYMBOL_REF_TLS_MODEL (anchor) < model)
6144169689Skan	begin = middle + 1;
6145169689Skan      else
6146169689Skan	return anchor;
6147169689Skan    }
6148169689Skan
6149169689Skan  /* Create a new anchor with a unique label.  */
6150169689Skan  ASM_GENERATE_INTERNAL_LABEL (label, "LANCHOR", anchor_labelno++);
6151169689Skan  anchor = create_block_symbol (ggc_strdup (label), block, offset);
6152169689Skan  SYMBOL_REF_FLAGS (anchor) |= SYMBOL_FLAG_LOCAL | SYMBOL_FLAG_ANCHOR;
6153169689Skan  SYMBOL_REF_FLAGS (anchor) |= model << SYMBOL_FLAG_TLS_SHIFT;
6154169689Skan
6155169689Skan  /* Insert it at index BEGIN.  */
6156169689Skan  VEC_safe_insert (rtx, gc, block->anchors, begin, anchor);
6157169689Skan  return anchor;
6158169689Skan}
6159169689Skan
6160169689Skan/* Output the objects in BLOCK.  */
6161169689Skan
6162169689Skanstatic void
6163169689Skanoutput_object_block (struct object_block *block)
6164169689Skan{
6165169689Skan  struct constant_descriptor_rtx *desc;
6166169689Skan  unsigned int i;
6167169689Skan  HOST_WIDE_INT offset;
6168169689Skan  tree decl;
6169169689Skan  rtx symbol;
6170169689Skan
6171169689Skan  if (block->objects == NULL)
6172169689Skan    return;
6173169689Skan
6174169689Skan  /* Switch to the section and make sure that the first byte is
6175169689Skan     suitably aligned.  */
6176169689Skan  switch_to_section (block->sect);
6177169689Skan  assemble_align (block->alignment);
6178169689Skan
6179169689Skan  /* Define the values of all anchors relative to the current section
6180169689Skan     position.  */
6181169689Skan  for (i = 0; VEC_iterate (rtx, block->anchors, i, symbol); i++)
6182169689Skan    targetm.asm_out.output_anchor (symbol);
6183169689Skan
6184169689Skan  /* Output the objects themselves.  */
6185169689Skan  offset = 0;
6186169689Skan  for (i = 0; VEC_iterate (rtx, block->objects, i, symbol); i++)
6187169689Skan    {
6188169689Skan      /* Move to the object's offset, padding with zeros if necessary.  */
6189169689Skan      assemble_zeros (SYMBOL_REF_BLOCK_OFFSET (symbol) - offset);
6190169689Skan      offset = SYMBOL_REF_BLOCK_OFFSET (symbol);
6191169689Skan      if (CONSTANT_POOL_ADDRESS_P (symbol))
6192169689Skan	{
6193169689Skan	  desc = SYMBOL_REF_CONSTANT (symbol);
6194169689Skan	  output_constant_pool_1 (desc, 1);
6195169689Skan	  offset += GET_MODE_SIZE (desc->mode);
6196169689Skan	}
6197169689Skan      else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
6198169689Skan	{
6199169689Skan	  decl = SYMBOL_REF_DECL (symbol);
6200169689Skan	  assemble_constant_contents (decl, XSTR (symbol, 0),
6201169689Skan				      get_constant_alignment (decl));
6202169689Skan	  offset += get_constant_size (decl);
6203169689Skan	}
6204169689Skan      else
6205169689Skan	{
6206169689Skan	  decl = SYMBOL_REF_DECL (symbol);
6207169689Skan	  assemble_variable_contents (decl, XSTR (symbol, 0), false);
6208169689Skan	  offset += tree_low_cst (DECL_SIZE_UNIT (decl), 1);
6209169689Skan	}
6210169689Skan    }
6211169689Skan}
6212169689Skan
6213169689Skan/* A htab_traverse callback used to call output_object_block for
6214169689Skan   each member of object_block_htab.  */
6215169689Skan
6216169689Skanstatic int
6217169689Skanoutput_object_block_htab (void **slot, void *data ATTRIBUTE_UNUSED)
6218169689Skan{
6219169689Skan  output_object_block ((struct object_block *) (*slot));
6220169689Skan  return 1;
6221169689Skan}
6222169689Skan
6223169689Skan/* Output the definitions of all object_blocks.  */
6224169689Skan
6225169689Skanvoid
6226169689Skanoutput_object_blocks (void)
6227169689Skan{
6228169689Skan  htab_traverse (object_block_htab, output_object_block_htab, NULL);
6229169689Skan}
6230169689Skan
6231215840Sdim/* Emit text to declare externally defined symbols. It is needed to
6232215840Sdim   properly support non-default visibility.  */
6233215840Sdimvoid
6234215840Sdimdefault_elf_asm_output_external (FILE *file ATTRIBUTE_UNUSED,
6235215840Sdim				 tree decl,
6236215840Sdim				 const char *name ATTRIBUTE_UNUSED)
6237215840Sdim{
6238215840Sdim  /* We output the name if and only if TREE_SYMBOL_REFERENCED is
6239215840Sdim     set in order to avoid putting out names that are never really
6240215840Sdim     used. */
6241215840Sdim  if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
6242215840Sdim      && targetm.binds_local_p (decl))
6243215840Sdim    maybe_assemble_visibility (decl);
6244215840Sdim}
6245215840Sdim
6246117395Skan#include "gt-varasm.h"
6247