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 ());
1386258748Spfg      assemble_align (DECL_ALIGN (decl));
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);
1396258748Spfg	  assemble_align (DECL_ALIGN (decl));
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.  */
1427258748Spfg  align = floor_log2 (DECL_ALIGN (decl) / BITS_PER_UNIT);
142818334Speter  if (align > 0)
142990075Sobrien    {
143090075Sobrien      ASM_OUTPUT_ALIGN (asm_out_file, align);
143190075Sobrien    }
143218334Speter
143390075Sobrien  /* Handle a user-specified function alignment.
1434258748Spfg     Note that we still need to align to DECL_ALIGN, as above,
143590075Sobrien     because ASM_OUTPUT_MAX_SKIP_ALIGN might not do any alignment at all.  */
1436258748Spfg  if (! DECL_USER_ALIGN (decl)
1437258748Spfg      && align_functions_log > align
1438117395Skan      && cfun->function_frequency != FUNCTION_FREQUENCY_UNLIKELY_EXECUTED)
143990075Sobrien    {
144090075Sobrien#ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
144190075Sobrien      ASM_OUTPUT_MAX_SKIP_ALIGN (asm_out_file,
1442117395Skan				 align_functions_log, align_functions - 1);
144390075Sobrien#else
144490075Sobrien      ASM_OUTPUT_ALIGN (asm_out_file, align_functions_log);
144590075Sobrien#endif
144690075Sobrien    }
144790075Sobrien
144818334Speter#ifdef ASM_OUTPUT_FUNCTION_PREFIX
144918334Speter  ASM_OUTPUT_FUNCTION_PREFIX (asm_out_file, fnname);
145018334Speter#endif
145118334Speter
145290075Sobrien  (*debug_hooks->begin_function) (decl);
145318334Speter
145418334Speter  /* Make function name accessible from other files, if appropriate.  */
145518334Speter
145618334Speter  if (TREE_PUBLIC (decl))
145718334Speter    {
1458132718Skan      notice_global_symbol (decl);
145918334Speter
146096263Sobrien      globalize_decl (decl);
1461117395Skan
1462117395Skan      maybe_assemble_visibility (decl);
146318334Speter    }
146418334Speter
1465169689Skan  if (DECL_PRESERVE_P (decl))
1466169689Skan    targetm.asm_out.mark_decl_preserved (fnname);
1467169689Skan
1468132718Skan  /* Do any machine/system dependent processing of the function name.  */
146918334Speter#ifdef ASM_DECLARE_FUNCTION_NAME
147018334Speter  ASM_DECLARE_FUNCTION_NAME (asm_out_file, fnname, current_function_decl);
147118334Speter#else
147218334Speter  /* Standard thing is just output label for the function.  */
147350397Sobrien  ASM_OUTPUT_LABEL (asm_out_file, fnname);
147418334Speter#endif /* ASM_DECLARE_FUNCTION_NAME */
147518334Speter}
147618334Speter
147718334Speter/* Output assembler code associated with defining the size of the
147818334Speter   function.  DECL describes the function.  NAME is the function's name.  */
147918334Speter
148018334Spetervoid
1481169689Skanassemble_end_function (tree decl, const char *fnname ATTRIBUTE_UNUSED)
148218334Speter{
148318334Speter#ifdef ASM_DECLARE_FUNCTION_SIZE
1484169689Skan  /* We could have switched section in the middle of the function.  */
1485169689Skan  if (flag_reorder_blocks_and_partition)
1486169689Skan    switch_to_section (function_section (decl));
148718334Speter  ASM_DECLARE_FUNCTION_SIZE (asm_out_file, fnname, decl);
148818334Speter#endif
148950397Sobrien  if (! CONSTANT_POOL_BEFORE_FUNCTION)
149050397Sobrien    {
149150397Sobrien      output_constant_pool (fnname, decl);
1492169689Skan      switch_to_section (function_section (decl)); /* need to switch back */
149350397Sobrien    }
1494169689Skan  /* Output labels for end of hot/cold text sections (to be used by
1495169689Skan     debug info.)  */
1496169689Skan  if (flag_reorder_blocks_and_partition)
1497169689Skan    {
1498169689Skan      section *save_text_section;
1499169689Skan
1500169689Skan      save_text_section = in_section;
1501169689Skan      switch_to_section (unlikely_text_section ());
1502169689Skan      ASM_OUTPUT_LABEL (asm_out_file, cfun->cold_section_end_label);
1503169689Skan      if (first_function_block_is_cold)
1504169689Skan	switch_to_section (text_section);
1505169689Skan      else
1506169689Skan	switch_to_section (function_section (decl));
1507169689Skan      ASM_OUTPUT_LABEL (asm_out_file, cfun->hot_section_end_label);
1508169689Skan      switch_to_section (save_text_section);
1509169689Skan    }
151018334Speter}
151118334Speter
151218334Speter/* Assemble code to leave SIZE bytes of zeros.  */
151318334Speter
151418334Spetervoid
1515132718Skanassemble_zeros (unsigned HOST_WIDE_INT size)
151618334Speter{
151752284Sobrien  /* Do no output if -fsyntax-only.  */
151852284Sobrien  if (flag_syntax_only)
151952284Sobrien    return;
152052284Sobrien
152118334Speter#ifdef ASM_NO_SKIP_IN_TEXT
152218334Speter  /* The `space' pseudo in the text section outputs nop insns rather than 0s,
152318334Speter     so we must output 0s explicitly in the text section.  */
1524169689Skan  if (ASM_NO_SKIP_IN_TEXT && (in_section->common.flags & SECTION_CODE) != 0)
152518334Speter    {
1526132718Skan      unsigned HOST_WIDE_INT i;
152790075Sobrien      for (i = 0; i < size; i++)
152890075Sobrien	assemble_integer (const0_rtx, 1, BITS_PER_UNIT, 1);
152918334Speter    }
153018334Speter  else
153118334Speter#endif
153218334Speter    if (size > 0)
153350397Sobrien      ASM_OUTPUT_SKIP (asm_out_file, size);
153418334Speter}
153518334Speter
153618334Speter/* Assemble an alignment pseudo op for an ALIGN-bit boundary.  */
153718334Speter
153818334Spetervoid
1539132718Skanassemble_align (int align)
154018334Speter{
154118334Speter  if (align > BITS_PER_UNIT)
154290075Sobrien    {
154390075Sobrien      ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
154490075Sobrien    }
154518334Speter}
154618334Speter
154718334Speter/* Assemble a string constant with the specified C string as contents.  */
154818334Speter
154918334Spetervoid
1550132718Skanassemble_string (const char *p, int size)
155118334Speter{
155218334Speter  int pos = 0;
155318334Speter  int maximum = 2000;
155418334Speter
155518334Speter  /* If the string is very long, split it up.  */
155618334Speter
155718334Speter  while (pos < size)
155818334Speter    {
155918334Speter      int thissize = size - pos;
156018334Speter      if (thissize > maximum)
156118334Speter	thissize = maximum;
156218334Speter
156350397Sobrien      ASM_OUTPUT_ASCII (asm_out_file, p, thissize);
156418334Speter
156518334Speter      pos += thissize;
156618334Speter      p += thissize;
156718334Speter    }
156818334Speter}
156918334Speter
157018334Speter
1571169689Skan/* A noswitch_section_callback for lcomm_section.  */
1572169689Skan
1573169689Skanstatic bool
1574169689Skanemit_local (tree decl ATTRIBUTE_UNUSED,
1575169689Skan	    const char *name ATTRIBUTE_UNUSED,
1576169689Skan	    unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1577169689Skan	    unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1578169689Skan{
1579169689Skan#if defined ASM_OUTPUT_ALIGNED_DECL_LOCAL
1580169689Skan  ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, decl, name,
1581169689Skan				 size, DECL_ALIGN (decl));
1582169689Skan  return true;
1583169689Skan#elif defined ASM_OUTPUT_ALIGNED_LOCAL
1584169689Skan  ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, DECL_ALIGN (decl));
1585169689Skan  return true;
158690075Sobrien#else
1587169689Skan  ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
1588169689Skan  return false;
158990075Sobrien#endif
1590169689Skan}
159190075Sobrien
1592169689Skan/* A noswitch_section_callback for bss_noswitch_section.  */
1593169689Skan
1594169689Skan#if defined ASM_OUTPUT_ALIGNED_BSS || defined ASM_OUTPUT_BSS
1595169689Skanstatic bool
1596169689Skanemit_bss (tree decl ATTRIBUTE_UNUSED,
1597169689Skan	  const char *name ATTRIBUTE_UNUSED,
1598169689Skan	  unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1599169689Skan	  unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1600169689Skan{
160190075Sobrien#if defined ASM_OUTPUT_ALIGNED_BSS
1602169689Skan  ASM_OUTPUT_ALIGNED_BSS (asm_out_file, decl, name, size, DECL_ALIGN (decl));
1603169689Skan  return true;
160490075Sobrien#else
1605169689Skan  ASM_OUTPUT_BSS (asm_out_file, decl, name, size, rounded);
1606169689Skan  return false;
160790075Sobrien#endif
1608169689Skan}
160990075Sobrien#endif
161090075Sobrien
1611169689Skan/* A noswitch_section_callback for comm_section.  */
1612169689Skan
1613169689Skanstatic bool
1614169689Skanemit_common (tree decl ATTRIBUTE_UNUSED,
1615169689Skan	     const char *name ATTRIBUTE_UNUSED,
1616169689Skan	     unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1617169689Skan	     unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1618169689Skan{
161990075Sobrien#if defined ASM_OUTPUT_ALIGNED_DECL_COMMON
1620169689Skan  ASM_OUTPUT_ALIGNED_DECL_COMMON (asm_out_file, decl, name,
1621169689Skan				  size, DECL_ALIGN (decl));
1622169689Skan  return true;
1623169689Skan#elif defined ASM_OUTPUT_ALIGNED_COMMON
1624169689Skan  ASM_OUTPUT_ALIGNED_COMMON (asm_out_file, name, size, DECL_ALIGN (decl));
1625169689Skan  return true;
162690075Sobrien#else
1627169689Skan  ASM_OUTPUT_COMMON (asm_out_file, name, size, rounded);
1628169689Skan  return false;
162990075Sobrien#endif
1630169689Skan}
163190075Sobrien
1632169689Skan/* A noswitch_section_callback for tls_comm_section.  */
1633169689Skan
1634117395Skanstatic bool
1635169689Skanemit_tls_common (tree decl ATTRIBUTE_UNUSED,
1636169689Skan		 const char *name ATTRIBUTE_UNUSED,
1637169689Skan		 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1638169689Skan		 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
163990075Sobrien{
1640169689Skan#ifdef ASM_OUTPUT_TLS_COMMON
1641169689Skan  ASM_OUTPUT_TLS_COMMON (asm_out_file, decl, name, size);
1642169689Skan  return true;
1643117395Skan#else
1644169689Skan  sorry ("thread-local COMMON data not implemented");
1645169689Skan  return true;
1646117395Skan#endif
1647169689Skan}
164890075Sobrien
1649169689Skan/* Assemble DECL given that it belongs in SECTION_NOSWITCH section SECT.
1650169689Skan   NAME is the name of DECL's SYMBOL_REF.  */
165196263Sobrien
1652169689Skanstatic void
1653169689Skanassemble_noswitch_variable (tree decl, const char *name, section *sect)
1654169689Skan{
1655169689Skan  unsigned HOST_WIDE_INT size, rounded;
165690075Sobrien
1657169689Skan  size = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
1658169689Skan  rounded = size;
1659169689Skan
1660169689Skan  /* Don't allocate zero bytes of common,
1661169689Skan     since that means "undefined external" in the linker.  */
1662169689Skan  if (size == 0)
1663169689Skan    rounded = 1;
1664169689Skan
1665169689Skan  /* Round size up to multiple of BIGGEST_ALIGNMENT bits
1666169689Skan     so that each uninitialized object starts on such a boundary.  */
1667169689Skan  rounded += (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1;
1668169689Skan  rounded = (rounded / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
1669169689Skan	     * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
1670169689Skan
1671169689Skan  if (!sect->noswitch.callback (decl, name, size, rounded)
1672169689Skan      && (unsigned HOST_WIDE_INT) DECL_ALIGN_UNIT (decl) > rounded)
1673169689Skan    warning (0, "requested alignment for %q+D is greater than "
1674169689Skan	     "implemented alignment of %wu", decl, rounded);
1675169689Skan}
1676169689Skan
1677169689Skan/* A subroutine of assemble_variable.  Output the label and contents of
1678169689Skan   DECL, whose address is a SYMBOL_REF with name NAME.  DONT_OUTPUT_DATA
1679169689Skan   is as for assemble_variable.  */
1680169689Skan
1681169689Skanstatic void
1682169689Skanassemble_variable_contents (tree decl, const char *name,
1683169689Skan			    bool dont_output_data)
1684169689Skan{
1685169689Skan  /* Do any machine/system dependent processing of the object.  */
1686169689Skan#ifdef ASM_DECLARE_OBJECT_NAME
1687169689Skan  last_assemble_variable_decl = decl;
1688169689Skan  ASM_DECLARE_OBJECT_NAME (asm_out_file, name, decl);
1689169689Skan#else
1690169689Skan  /* Standard thing is just output label for the object.  */
1691169689Skan  ASM_OUTPUT_LABEL (asm_out_file, name);
1692169689Skan#endif /* ASM_DECLARE_OBJECT_NAME */
1693169689Skan
1694169689Skan  if (!dont_output_data)
169590075Sobrien    {
1696169689Skan      if (DECL_INITIAL (decl)
1697169689Skan	  && DECL_INITIAL (decl) != error_mark_node
1698169689Skan	  && !initializer_zerop (DECL_INITIAL (decl)))
1699169689Skan	/* Output the actual data.  */
1700169689Skan	output_constant (DECL_INITIAL (decl),
1701169689Skan			 tree_low_cst (DECL_SIZE_UNIT (decl), 1),
1702169689Skan			 DECL_ALIGN (decl));
1703169689Skan      else
1704169689Skan	/* Leave space for it.  */
1705169689Skan	assemble_zeros (tree_low_cst (DECL_SIZE_UNIT (decl), 1));
170690075Sobrien    }
170790075Sobrien}
170890075Sobrien
170918334Speter/* Assemble everything that is needed for a variable or function declaration.
171018334Speter   Not used for automatic variables, and not used for function definitions.
171118334Speter   Should not be called for variables of incomplete structure type.
171218334Speter
171318334Speter   TOP_LEVEL is nonzero if this variable has file scope.
171418334Speter   AT_END is nonzero if this is the special handling, at end of compilation,
171518334Speter   to define things that have had only tentative definitions.
171618334Speter   DONT_OUTPUT_DATA if nonzero means don't actually output the
171718334Speter   initial value (that will be done by the caller).  */
171818334Speter
171918334Spetervoid
1720132718Skanassemble_variable (tree decl, int top_level ATTRIBUTE_UNUSED,
1721132718Skan		   int at_end ATTRIBUTE_UNUSED, int dont_output_data)
172218334Speter{
172390075Sobrien  const char *name;
1724169689Skan  rtx decl_rtl, symbol;
1725169689Skan  section *sect;
172618334Speter
1727132718Skan  if (lang_hooks.decls.prepare_assemble_variable)
1728169689Skan    lang_hooks.decls.prepare_assemble_variable (decl);
1729132718Skan
173018334Speter  last_assemble_variable_decl = 0;
173118334Speter
173218334Speter  /* Normally no need to say anything here for external references,
173318334Speter     since assemble_external is called by the language-specific code
173418334Speter     when a declaration is first seen.  */
173518334Speter
173618334Speter  if (DECL_EXTERNAL (decl))
173718334Speter    return;
173818334Speter
173918334Speter  /* Output no assembler code for a function declaration.
174018334Speter     Only definitions of functions output anything.  */
174118334Speter
174218334Speter  if (TREE_CODE (decl) == FUNCTION_DECL)
174318334Speter    return;
174418334Speter
174590075Sobrien  /* Do nothing for global register variables.  */
1746169689Skan  if (DECL_RTL_SET_P (decl) && REG_P (DECL_RTL (decl)))
174790075Sobrien    {
174890075Sobrien      TREE_ASM_WRITTEN (decl) = 1;
174990075Sobrien      return;
175090075Sobrien    }
175190075Sobrien
175218334Speter  /* If type was incomplete when the variable was declared,
175318334Speter     see if it is complete now.  */
175418334Speter
175518334Speter  if (DECL_SIZE (decl) == 0)
175618334Speter    layout_decl (decl, 0);
175718334Speter
175818334Speter  /* Still incomplete => don't allocate it; treat the tentative defn
175918334Speter     (which is what it must have been) as an `extern' reference.  */
176018334Speter
176118334Speter  if (!dont_output_data && DECL_SIZE (decl) == 0)
176218334Speter    {
1763169689Skan      error ("storage size of %q+D isn%'t known", decl);
176418334Speter      TREE_ASM_WRITTEN (decl) = 1;
176518334Speter      return;
176618334Speter    }
176718334Speter
176818334Speter  /* The first declaration of a variable that comes through this function
176918334Speter     decides whether it is global (in C, has external linkage)
177018334Speter     or local (in C, has internal linkage).  So do nothing more
177118334Speter     if this function has already run.  */
177218334Speter
177318334Speter  if (TREE_ASM_WRITTEN (decl))
177418334Speter    return;
177518334Speter
1776117395Skan  /* Make sure targetm.encode_section_info is invoked before we set
1777117395Skan     ASM_WRITTEN.  */
177890075Sobrien  decl_rtl = DECL_RTL (decl);
177990075Sobrien
178018334Speter  TREE_ASM_WRITTEN (decl) = 1;
178118334Speter
178252284Sobrien  /* Do no output if -fsyntax-only.  */
178352284Sobrien  if (flag_syntax_only)
178452284Sobrien    return;
178552284Sobrien
178618334Speter  app_disable ();
178718334Speter
178890075Sobrien  if (! dont_output_data
178990075Sobrien      && ! host_integerp (DECL_SIZE_UNIT (decl), 1))
179018334Speter    {
1791169689Skan      error ("size of variable %q+D is too large", decl);
179290075Sobrien      return;
179318334Speter    }
179418334Speter
1795169689Skan  gcc_assert (MEM_P (decl_rtl));
1796169689Skan  gcc_assert (GET_CODE (XEXP (decl_rtl, 0)) == SYMBOL_REF);
1797169689Skan  symbol = XEXP (decl_rtl, 0);
1798169689Skan  name = XSTR (symbol, 0);
1799132718Skan  if (TREE_PUBLIC (decl) && DECL_NAME (decl))
1800132718Skan    notice_global_symbol (decl);
180150397Sobrien
180250397Sobrien  /* Compute the alignment of this data.  */
180350397Sobrien
1804169689Skan  align_variable (decl, dont_output_data);
1805169689Skan  set_mem_align (decl_rtl, DECL_ALIGN (decl));
180650397Sobrien
1807117395Skan  if (TREE_PUBLIC (decl))
1808117395Skan    maybe_assemble_visibility (decl);
1809117395Skan
1810169689Skan  if (DECL_PRESERVE_P (decl))
1811169689Skan    targetm.asm_out.mark_decl_preserved (name);
1812117395Skan
181318334Speter  /* First make the assembler name(s) global if appropriate.  */
1814169689Skan  sect = get_variable_section (decl, false);
1815169689Skan  if (TREE_PUBLIC (decl)
1816169689Skan      && DECL_NAME (decl)
1817169689Skan      && (sect->common.flags & SECTION_COMMON) == 0)
181896263Sobrien    globalize_decl (decl);
181918334Speter
1820169689Skan  /* Output any data that we will need to use the address of.  */
1821169689Skan  if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node)
1822169689Skan    output_addressed_constants (DECL_INITIAL (decl));
182350397Sobrien
182418334Speter  /* dbxout.c needs to know this.  */
1825169689Skan  if (sect && (sect->common.flags & SECTION_CODE) != 0)
182618334Speter    DECL_IN_TEXT_SECTION (decl) = 1;
182718334Speter
1828169689Skan  /* If the decl is part of an object_block, make sure that the decl
1829169689Skan     has been positioned within its block, but do not write out its
1830169689Skan     definition yet.  output_object_blocks will do that later.  */
1831169689Skan  if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
183290075Sobrien    {
1833169689Skan      gcc_assert (!dont_output_data);
1834169689Skan      place_block_symbol (symbol);
183590075Sobrien    }
1836169689Skan  else if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
1837169689Skan    assemble_noswitch_variable (decl, name, sect);
1838169689Skan  else
183918334Speter    {
1840169689Skan      switch_to_section (sect);
1841169689Skan      if (DECL_ALIGN (decl) > BITS_PER_UNIT)
1842169689Skan	ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (DECL_ALIGN_UNIT (decl)));
1843169689Skan      assemble_variable_contents (decl, name, dont_output_data);
184418334Speter    }
184518334Speter}
184618334Speter
184718334Speter/* Return 1 if type TYPE contains any pointers.  */
184818334Speter
184918334Speterstatic int
1850132718Skancontains_pointers_p (tree type)
185118334Speter{
185218334Speter  switch (TREE_CODE (type))
185318334Speter    {
185418334Speter    case POINTER_TYPE:
185518334Speter    case REFERENCE_TYPE:
185618334Speter      /* I'm not sure whether OFFSET_TYPE needs this treatment,
185718334Speter	 so I'll play safe and return 1.  */
185818334Speter    case OFFSET_TYPE:
185918334Speter      return 1;
186018334Speter
186118334Speter    case RECORD_TYPE:
186218334Speter    case UNION_TYPE:
186318334Speter    case QUAL_UNION_TYPE:
186418334Speter      {
186518334Speter	tree fields;
186618334Speter	/* For a type that has fields, see if the fields have pointers.  */
186718334Speter	for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
186818334Speter	  if (TREE_CODE (fields) == FIELD_DECL
186918334Speter	      && contains_pointers_p (TREE_TYPE (fields)))
187018334Speter	    return 1;
187118334Speter	return 0;
187218334Speter      }
187318334Speter
187418334Speter    case ARRAY_TYPE:
187518334Speter      /* An array type contains pointers if its element type does.  */
187618334Speter      return contains_pointers_p (TREE_TYPE (type));
187718334Speter
187818334Speter    default:
187918334Speter      return 0;
188018334Speter    }
188118334Speter}
188218334Speter
1883169689Skan/* In unit-at-a-time mode, we delay assemble_external processing until
1884169689Skan   the compilation unit is finalized.  This is the best we can do for
1885169689Skan   right now (i.e. stage 3 of GCC 4.0) - the right thing is to delay
1886169689Skan   it all the way to final.  See PR 17982 for further discussion.  */
1887169689Skanstatic GTY(()) tree pending_assemble_externals;
1888169689Skan
1889132718Skan#ifdef ASM_OUTPUT_EXTERNAL
1890132718Skan/* True if DECL is a function decl for which no out-of-line copy exists.
1891132718Skan   It is assumed that DECL's assembler name has been set.  */
1892132718Skan
1893132718Skanstatic bool
1894132718Skanincorporeal_function_p (tree decl)
1895132718Skan{
1896132718Skan  if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1897132718Skan    {
1898132718Skan      const char *name;
1899132718Skan
1900132718Skan      if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
1901132718Skan	  && DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA)
1902132718Skan	return true;
1903132718Skan
1904132718Skan      name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
1905132718Skan      if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
1906132718Skan	return true;
1907132718Skan    }
1908132718Skan  return false;
1909132718Skan}
1910169689Skan
1911169689Skan/* Actually do the tests to determine if this is necessary, and invoke
1912169689Skan   ASM_OUTPUT_EXTERNAL.  */
1913169689Skanstatic void
1914169689Skanassemble_external_real (tree decl)
1915169689Skan{
1916169689Skan  rtx rtl = DECL_RTL (decl);
1917169689Skan
1918169689Skan  if (MEM_P (rtl) && GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF
1919169689Skan      && !SYMBOL_REF_USED (XEXP (rtl, 0))
1920169689Skan      && !incorporeal_function_p (decl))
1921169689Skan    {
1922169689Skan      /* Some systems do require some output.  */
1923169689Skan      SYMBOL_REF_USED (XEXP (rtl, 0)) = 1;
1924169689Skan      ASM_OUTPUT_EXTERNAL (asm_out_file, decl, XSTR (XEXP (rtl, 0), 0));
1925169689Skan    }
1926169689Skan}
1927132718Skan#endif
1928132718Skan
1929169689Skanvoid
1930169689Skanprocess_pending_assemble_externals (void)
1931169689Skan{
1932169689Skan#ifdef ASM_OUTPUT_EXTERNAL
1933169689Skan  tree list;
1934169689Skan  for (list = pending_assemble_externals; list; list = TREE_CHAIN (list))
1935169689Skan    assemble_external_real (TREE_VALUE (list));
1936169689Skan
1937169689Skan  pending_assemble_externals = 0;
1938169689Skan#endif
1939169689Skan}
1940169689Skan
194118334Speter/* Output something to declare an external symbol to the assembler.
194218334Speter   (Most assemblers don't need this, so we normally output nothing.)
194318334Speter   Do nothing if DECL is not external.  */
194418334Speter
194518334Spetervoid
1946132718Skanassemble_external (tree decl ATTRIBUTE_UNUSED)
194718334Speter{
194890075Sobrien  /* Because most platforms do not define ASM_OUTPUT_EXTERNAL, the
194990075Sobrien     main body of this code is only rarely exercised.  To provide some
195090075Sobrien     testing, on all platforms, we make sure that the ASM_OUT_FILE is
195190075Sobrien     open.  If it's not, we should not be calling this function.  */
1952169689Skan  gcc_assert (asm_out_file);
195390075Sobrien
195418334Speter#ifdef ASM_OUTPUT_EXTERNAL
1955169689Skan  if (!DECL_P (decl) || !DECL_EXTERNAL (decl) || !TREE_PUBLIC (decl))
1956169689Skan    return;
195718334Speter
1958215840Sdim  /* We want to output external symbols at very last to check if they
1959215840Sdim     are references or not.  */
1960215840Sdim  pending_assemble_externals = tree_cons (0, decl,
1961215840Sdim					  pending_assemble_externals);
196218334Speter#endif
196318334Speter}
196418334Speter
196518334Speter/* Similar, for calling a library function FUN.  */
196618334Speter
196718334Spetervoid
1968132718Skanassemble_external_libcall (rtx fun)
196918334Speter{
197050397Sobrien  /* Declare library function name external when first used, if nec.  */
197150397Sobrien  if (! SYMBOL_REF_USED (fun))
197218334Speter    {
197350397Sobrien      SYMBOL_REF_USED (fun) = 1;
1974169689Skan      targetm.asm_out.external_libcall (fun);
197518334Speter    }
197618334Speter}
197718334Speter
197818334Speter/* Assemble a label named NAME.  */
197918334Speter
198018334Spetervoid
1981132718Skanassemble_label (const char *name)
198218334Speter{
198350397Sobrien  ASM_OUTPUT_LABEL (asm_out_file, name);
198418334Speter}
198518334Speter
1986169689Skan/* Set the symbol_referenced flag for ID.  */
1987132718Skanvoid
1988132718Skanmark_referenced (tree id)
1989132718Skan{
1990169689Skan  TREE_SYMBOL_REFERENCED (id) = 1;
1991169689Skan}
1992169689Skan
1993169689Skan/* Set the symbol_referenced flag for DECL and notify callgraph.  */
1994169689Skanvoid
1995169689Skanmark_decl_referenced (tree decl)
1996169689Skan{
1997169689Skan  if (TREE_CODE (decl) == FUNCTION_DECL)
1998132718Skan    {
1999169689Skan      /* Extern inline functions don't become needed when referenced.
2000169689Skan	 If we know a method will be emitted in other TU and no new
2001169689Skan	 functions can be marked reachable, just use the external
2002169689Skan	 definition.  */
2003169689Skan      struct cgraph_node *node = cgraph_node (decl);
2004169689Skan      if (!DECL_EXTERNAL (decl)
2005169689Skan	  && (!node->local.vtable_method || !cgraph_global_info_ready
2006169689Skan	      || !node->local.finalized))
2007169689Skan	cgraph_mark_needed_node (node);
2008169689Skan    }
2009169689Skan  else if (TREE_CODE (decl) == VAR_DECL)
2010169689Skan    {
2011169689Skan      struct cgraph_varpool_node *node = cgraph_varpool_node (decl);
2012169689Skan      cgraph_varpool_mark_needed_node (node);
2013169689Skan      /* C++ frontend use mark_decl_references to force COMDAT variables
2014169689Skan         to be output that might appear dead otherwise.  */
2015169689Skan      node->force_output = true;
2016169689Skan    }
2017169689Skan  /* else do nothing - we can get various sorts of CST nodes here,
2018169689Skan     which do not need to be marked.  */
2019169689Skan}
2020132718Skan
2021132718Skan
2022169689Skan/* Follow the IDENTIFIER_TRANSPARENT_ALIAS chain starting at *ALIAS
2023169689Skan   until we find an identifier that is not itself a transparent alias.
2024169689Skan   Modify the alias passed to it by reference (and all aliases on the
2025169689Skan   way to the ultimate target), such that they do not have to be
2026169689Skan   followed again, and return the ultimate target of the alias
2027169689Skan   chain.  */
2028169689Skan
2029169689Skanstatic inline tree
2030169689Skanultimate_transparent_alias_target (tree *alias)
2031169689Skan{
2032169689Skan  tree target = *alias;
2033169689Skan
2034169689Skan  if (IDENTIFIER_TRANSPARENT_ALIAS (target))
2035169689Skan    {
2036169689Skan      gcc_assert (TREE_CHAIN (target));
2037169689Skan      target = ultimate_transparent_alias_target (&TREE_CHAIN (target));
2038169689Skan      gcc_assert (! IDENTIFIER_TRANSPARENT_ALIAS (target)
2039169689Skan		  && ! TREE_CHAIN (target));
2040169689Skan      *alias = target;
2041132718Skan    }
2042169689Skan
2043169689Skan  return target;
2044132718Skan}
2045132718Skan
2046169689Skan/* Output to FILE (an assembly file) a reference to NAME.  If NAME
2047169689Skan   starts with a *, the rest of NAME is output verbatim.  Otherwise
2048169689Skan   NAME is transformed in a target-specific way (usually by the
2049169689Skan   addition of an underscore).  */
205018334Speter
205118334Spetervoid
2052169689Skanassemble_name_raw (FILE *file, const char *name)
2053169689Skan{
2054169689Skan  if (name[0] == '*')
2055169689Skan    fputs (&name[1], file);
2056169689Skan  else
2057169689Skan    ASM_OUTPUT_LABELREF (file, name);
2058169689Skan}
2059169689Skan
2060169689Skan/* Like assemble_name_raw, but should be used when NAME might refer to
2061169689Skan   an entity that is also represented as a tree (like a function or
2062169689Skan   variable).  If NAME does refer to such an entity, that entity will
2063169689Skan   be marked as referenced.  */
2064169689Skan
2065169689Skanvoid
2066132718Skanassemble_name (FILE *file, const char *name)
206718334Speter{
206890075Sobrien  const char *real_name;
206950397Sobrien  tree id;
207018334Speter
2071169689Skan  real_name = targetm.strip_name_encoding (name);
207218334Speter
207350397Sobrien  id = maybe_get_identifier (real_name);
207450397Sobrien  if (id)
2075169689Skan    {
2076169689Skan      tree id_orig = id;
207718334Speter
2078169689Skan      mark_referenced (id);
2079169689Skan      ultimate_transparent_alias_target (&id);
2080169689Skan      if (id != id_orig)
2081169689Skan	name = IDENTIFIER_POINTER (id);
2082169689Skan      gcc_assert (! TREE_CHAIN (id));
2083169689Skan    }
2084169689Skan
2085169689Skan  assemble_name_raw (file, name);
208618334Speter}
208718334Speter
208818334Speter/* Allocate SIZE bytes writable static space with a gensym name
208918334Speter   and return an RTX to refer to its address.  */
209018334Speter
209118334Speterrtx
2092132718Skanassemble_static_space (unsigned HOST_WIDE_INT size)
209318334Speter{
209418334Speter  char name[12];
209590075Sobrien  const char *namestring;
209618334Speter  rtx x;
209718334Speter
209818334Speter  ASM_GENERATE_INTERNAL_LABEL (name, "LF", const_labelno);
209918334Speter  ++const_labelno;
210090075Sobrien  namestring = ggc_strdup (name);
210118334Speter
210250397Sobrien  x = gen_rtx_SYMBOL_REF (Pmode, namestring);
2103132718Skan  SYMBOL_REF_FLAGS (x) = SYMBOL_FLAG_LOCAL;
210418334Speter
210550397Sobrien#ifdef ASM_OUTPUT_ALIGNED_DECL_LOCAL
210650397Sobrien  ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, NULL_TREE, name, size,
210750397Sobrien				 BIGGEST_ALIGNMENT);
210850397Sobrien#else
210918334Speter#ifdef ASM_OUTPUT_ALIGNED_LOCAL
211050397Sobrien  ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, BIGGEST_ALIGNMENT);
211118334Speter#else
211250397Sobrien  {
211350397Sobrien    /* Round size up to multiple of BIGGEST_ALIGNMENT bits
211450397Sobrien       so that each uninitialized object starts on such a boundary.  */
211590075Sobrien    /* Variable `rounded' might or might not be used in ASM_OUTPUT_LOCAL.  */
2116132718Skan    unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED
211752284Sobrien      = ((size + (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1)
211852284Sobrien	 / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
211952284Sobrien	 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
212050397Sobrien    ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
212150397Sobrien  }
212218334Speter#endif
212350397Sobrien#endif
212418334Speter  return x;
212518334Speter}
212618334Speter
212718334Speter/* Assemble the static constant template for function entry trampolines.
212818334Speter   This is done at most once per compilation.
212918334Speter   Returns an RTX for the address of the template.  */
213018334Speter
2131169689Skanstatic GTY(()) rtx initial_trampoline;
2132169689Skan
213348743Sobrien#ifdef TRAMPOLINE_TEMPLATE
213418334Speterrtx
2135132718Skanassemble_trampoline_template (void)
213618334Speter{
213718334Speter  char label[256];
213890075Sobrien  const char *name;
213918334Speter  int align;
2140132718Skan  rtx symbol;
214118334Speter
2142169689Skan  if (initial_trampoline)
2143169689Skan    return initial_trampoline;
2144169689Skan
214518334Speter  /* By default, put trampoline templates in read-only data section.  */
214618334Speter
214718334Speter#ifdef TRAMPOLINE_SECTION
2148169689Skan  switch_to_section (TRAMPOLINE_SECTION);
214918334Speter#else
2150169689Skan  switch_to_section (readonly_data_section);
215118334Speter#endif
215218334Speter
215318334Speter  /* Write the assembler code to define one.  */
215450397Sobrien  align = floor_log2 (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
215518334Speter  if (align > 0)
215690075Sobrien    {
215790075Sobrien      ASM_OUTPUT_ALIGN (asm_out_file, align);
215890075Sobrien    }
215918334Speter
2160169689Skan  targetm.asm_out.internal_label (asm_out_file, "LTRAMP", 0);
216118334Speter  TRAMPOLINE_TEMPLATE (asm_out_file);
216218334Speter
216318334Speter  /* Record the rtl to refer to it.  */
216418334Speter  ASM_GENERATE_INTERNAL_LABEL (label, "LTRAMP", 0);
216590075Sobrien  name = ggc_strdup (label);
2166132718Skan  symbol = gen_rtx_SYMBOL_REF (Pmode, name);
2167132718Skan  SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_LOCAL;
2168132718Skan
2169169689Skan  initial_trampoline = gen_rtx_MEM (BLKmode, symbol);
2170169689Skan  set_mem_align (initial_trampoline, TRAMPOLINE_ALIGNMENT);
2171169689Skan
2172169689Skan  return initial_trampoline;
217318334Speter}
217448743Sobrien#endif
217518334Speter
217690075Sobrien/* A and B are either alignments or offsets.  Return the minimum alignment
217790075Sobrien   that may be assumed after adding the two together.  */
217818334Speter
217990075Sobrienstatic inline unsigned
2180132718Skanmin_align (unsigned int a, unsigned int b)
218190075Sobrien{
218290075Sobrien  return (a | b) & -(a | b);
218390075Sobrien}
218418334Speter
218590075Sobrien/* Return the assembler directive for creating a given kind of integer
218690075Sobrien   object.  SIZE is the number of bytes in the object and ALIGNED_P
218790075Sobrien   indicates whether it is known to be aligned.  Return NULL if the
218890075Sobrien   assembly dialect has no such directive.
218990075Sobrien
219090075Sobrien   The returned string should be printed at the start of a new line and
219190075Sobrien   be followed immediately by the object's initial value.  */
219290075Sobrien
219390075Sobrienconst char *
2194132718Skaninteger_asm_op (int size, int aligned_p)
219518334Speter{
219690075Sobrien  struct asm_int_op *ops;
219718334Speter
219890075Sobrien  if (aligned_p)
219990075Sobrien    ops = &targetm.asm_out.aligned_op;
220090075Sobrien  else
220190075Sobrien    ops = &targetm.asm_out.unaligned_op;
220290075Sobrien
220318334Speter  switch (size)
220418334Speter    {
220518334Speter    case 1:
220690075Sobrien      return targetm.asm_out.byte_op;
220718334Speter    case 2:
220890075Sobrien      return ops->hi;
220918334Speter    case 4:
221090075Sobrien      return ops->si;
221118334Speter    case 8:
221290075Sobrien      return ops->di;
221318334Speter    case 16:
221490075Sobrien      return ops->ti;
221590075Sobrien    default:
221690075Sobrien      return NULL;
221718334Speter    }
221890075Sobrien}
221918334Speter
222090075Sobrien/* Use directive OP to assemble an integer object X.  Print OP at the
222190075Sobrien   start of the line, followed immediately by the value of X.  */
222218334Speter
222390075Sobrienvoid
2224132718Skanassemble_integer_with_op (const char *op, rtx x)
222590075Sobrien{
222690075Sobrien  fputs (op, asm_out_file);
222790075Sobrien  output_addr_const (asm_out_file, x);
222890075Sobrien  fputc ('\n', asm_out_file);
222990075Sobrien}
223018334Speter
223190075Sobrien/* The default implementation of the asm_out.integer target hook.  */
223218334Speter
223390075Sobrienbool
2234132718Skandefault_assemble_integer (rtx x ATTRIBUTE_UNUSED,
2235132718Skan			  unsigned int size ATTRIBUTE_UNUSED,
2236132718Skan			  int aligned_p ATTRIBUTE_UNUSED)
223790075Sobrien{
223890075Sobrien  const char *op = integer_asm_op (size, aligned_p);
2239169689Skan  /* Avoid GAS bugs for large values.  Specifically negative values whose
2240169689Skan     absolute value fits in a bfd_vma, but not in a bfd_signed_vma.  */
2241169689Skan  if (size > UNITS_PER_WORD && size > POINTER_SIZE / BITS_PER_UNIT)
2242169689Skan    return false;
224390075Sobrien  return op && (assemble_integer_with_op (op, x), true);
224490075Sobrien}
224590075Sobrien
224690075Sobrien/* Assemble the integer constant X into an object of SIZE bytes.  ALIGN is
224790075Sobrien   the alignment of the integer in bits.  Return 1 if we were able to output
2248169689Skan   the constant, otherwise 0.  We must be able to output the constant,
2249169689Skan   if FORCE is nonzero.  */
225090075Sobrien
225190075Sobrienbool
2252132718Skanassemble_integer (rtx x, unsigned int size, unsigned int align, int force)
225390075Sobrien{
225490075Sobrien  int aligned_p;
225590075Sobrien
225690075Sobrien  aligned_p = (align >= MIN (size * BITS_PER_UNIT, BIGGEST_ALIGNMENT));
225790075Sobrien
225890075Sobrien  /* See if the target hook can handle this kind of object.  */
2259169689Skan  if (targetm.asm_out.integer (x, size, aligned_p))
226090075Sobrien    return true;
226190075Sobrien
226290075Sobrien  /* If the object is a multi-byte one, try splitting it up.  Split
226390075Sobrien     it into words it if is multi-word, otherwise split it into bytes.  */
226490075Sobrien  if (size > 1)
226518334Speter    {
226690075Sobrien      enum machine_mode omode, imode;
226790075Sobrien      unsigned int subalign;
226890075Sobrien      unsigned int subsize, i;
226918334Speter
227090075Sobrien      subsize = size > UNITS_PER_WORD? UNITS_PER_WORD : 1;
227190075Sobrien      subalign = MIN (align, subsize * BITS_PER_UNIT);
227290075Sobrien      omode = mode_for_size (subsize * BITS_PER_UNIT, MODE_INT, 0);
227390075Sobrien      imode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
227490075Sobrien
227590075Sobrien      for (i = 0; i < size; i += subsize)
227618334Speter	{
227790075Sobrien	  rtx partial = simplify_subreg (omode, x, imode, i);
227890075Sobrien	  if (!partial || !assemble_integer (partial, subsize, subalign, 0))
227918334Speter	    break;
228018334Speter	}
228190075Sobrien      if (i == size)
228290075Sobrien	return true;
228318334Speter
228490075Sobrien      /* If we've printed some of it, but not all of it, there's no going
228590075Sobrien	 back now.  */
2286169689Skan      gcc_assert (!i);
228718334Speter    }
228818334Speter
2289169689Skan  gcc_assert (!force);
229018334Speter
229190075Sobrien  return false;
229218334Speter}
229318334Speter
229418334Spetervoid
2295132718Skanassemble_real (REAL_VALUE_TYPE d, enum machine_mode mode, unsigned int align)
229618334Speter{
2297169689Skan  long data[4] = {0, 0, 0, 0};
2298132718Skan  int i;
2299132718Skan  int bitsize, nelts, nunits, units_per;
230018334Speter
2301132718Skan  /* This is hairy.  We have a quantity of known size.  real_to_target
2302132718Skan     will put it into an array of *host* longs, 32 bits per element
2303132718Skan     (even if long is more than 32 bits).  We need to determine the
2304132718Skan     number of array elements that are occupied (nelts) and the number
2305132718Skan     of *target* min-addressable units that will be occupied in the
2306132718Skan     object file (nunits).  We cannot assume that 32 divides the
2307132718Skan     mode's bitsize (size * BITS_PER_UNIT) evenly.
230818334Speter
2309132718Skan     size * BITS_PER_UNIT is used here to make sure that padding bits
2310132718Skan     (which might appear at either end of the value; real_to_target
2311132718Skan     will include the padding bits in its output array) are included.  */
231218334Speter
2313132718Skan  nunits = GET_MODE_SIZE (mode);
2314132718Skan  bitsize = nunits * BITS_PER_UNIT;
2315132718Skan  nelts = CEIL (bitsize, 32);
2316132718Skan  units_per = 32 / BITS_PER_UNIT;
231718334Speter
2318132718Skan  real_to_target (data, &d, mode);
2319132718Skan
2320132718Skan  /* Put out the first word with the specified alignment.  */
2321132718Skan  assemble_integer (GEN_INT (data[0]), MIN (nunits, units_per), align, 1);
2322132718Skan  nunits -= units_per;
2323132718Skan
2324132718Skan  /* Subsequent words need only 32-bit alignment.  */
2325132718Skan  align = min_align (align, 32);
2326132718Skan
2327132718Skan  for (i = 1; i < nelts; i++)
2328132718Skan    {
2329132718Skan      assemble_integer (GEN_INT (data[i]), MIN (nunits, units_per), align, 1);
2330132718Skan      nunits -= units_per;
233118334Speter    }
233218334Speter}
233318334Speter
233418334Speter/* Given an expression EXP with a constant value,
233518334Speter   reduce it to the sum of an assembler symbol and an integer.
233618334Speter   Store them both in the structure *VALUE.
2337169689Skan   EXP must be reducible.  */
233818334Speter
2339117395Skanstruct addr_const GTY(())
234018334Speter{
234118334Speter  rtx base;
234218334Speter  HOST_WIDE_INT offset;
234318334Speter};
234418334Speter
234518334Speterstatic void
2346132718Skandecode_addr_const (tree exp, struct addr_const *value)
234718334Speter{
234890075Sobrien  tree target = TREE_OPERAND (exp, 0);
234990075Sobrien  int offset = 0;
235090075Sobrien  rtx x;
235118334Speter
235218334Speter  while (1)
235318334Speter    {
235418334Speter      if (TREE_CODE (target) == COMPONENT_REF
235590075Sobrien	  && host_integerp (byte_position (TREE_OPERAND (target, 1)), 0))
235690075Sobrien
235718334Speter	{
235890075Sobrien	  offset += int_byte_position (TREE_OPERAND (target, 1));
235918334Speter	  target = TREE_OPERAND (target, 0);
236018334Speter	}
236190075Sobrien      else if (TREE_CODE (target) == ARRAY_REF
236290075Sobrien	       || TREE_CODE (target) == ARRAY_RANGE_REF)
236318334Speter	{
236490075Sobrien	  offset += (tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (target)), 1)
236590075Sobrien		     * tree_low_cst (TREE_OPERAND (target, 1), 0));
236618334Speter	  target = TREE_OPERAND (target, 0);
236718334Speter	}
236818334Speter      else
236918334Speter	break;
237018334Speter    }
237118334Speter
237218334Speter  switch (TREE_CODE (target))
237318334Speter    {
237418334Speter    case VAR_DECL:
237518334Speter    case FUNCTION_DECL:
237618334Speter      x = DECL_RTL (target);
237718334Speter      break;
237818334Speter
237918334Speter    case LABEL_DECL:
238050397Sobrien      x = gen_rtx_MEM (FUNCTION_MODE,
2381169689Skan		       gen_rtx_LABEL_REF (Pmode, force_label_rtx (target)));
238218334Speter      break;
238318334Speter
238418334Speter    case REAL_CST:
238518334Speter    case STRING_CST:
238618334Speter    case COMPLEX_CST:
238718334Speter    case CONSTRUCTOR:
238850397Sobrien    case INTEGER_CST:
238990075Sobrien      x = output_constant_def (target, 1);
239018334Speter      break;
239118334Speter
239218334Speter    default:
2393169689Skan      gcc_unreachable ();
239418334Speter    }
239518334Speter
2396169689Skan  gcc_assert (MEM_P (x));
239750397Sobrien  x = XEXP (x, 0);
239818334Speter
239918334Speter  value->base = x;
240018334Speter  value->offset = offset;
240118334Speter}
240218334Speter
240318334Speter/* Uniquize all constants that appear in memory.
240418334Speter   Each constant in memory thus far output is recorded
2405132718Skan   in `const_desc_table'.  */
240618334Speter
2407117395Skanstruct constant_descriptor_tree GTY(())
2408117395Skan{
2409117395Skan  /* A MEM for the constant.  */
241090075Sobrien  rtx rtl;
2411117395Skan
2412117395Skan  /* The value of the constant.  */
2413117395Skan  tree value;
2414169689Skan
2415169689Skan  /* Hash of value.  Computing the hash from value each time
2416169689Skan     hashfn is called can't work properly, as that means recursive
2417169689Skan     use of the hash table during hash table expansion.  */
2418169689Skan  hashval_t hash;
241918334Speter};
242018334Speter
2421132718Skanstatic GTY((param_is (struct constant_descriptor_tree)))
2422132718Skan     htab_t const_desc_htab;
242318334Speter
2424132718Skanstatic struct constant_descriptor_tree * build_constant_desc (tree);
2425132718Skanstatic void maybe_output_constant_def_contents (struct constant_descriptor_tree *, int);
242690075Sobrien
2427132718Skan/* Compute a hash code for a constant expression.  */
242890075Sobrien
242990075Sobrienstatic hashval_t
2430132718Skanconst_desc_hash (const void *ptr)
243190075Sobrien{
2432169689Skan  return ((struct constant_descriptor_tree *)ptr)->hash;
243390075Sobrien}
243490075Sobrien
2435132718Skanstatic hashval_t
2436132718Skanconst_hash_1 (const tree exp)
243790075Sobrien{
243890075Sobrien  const char *p;
2439132718Skan  hashval_t hi;
2440117395Skan  int len, i;
244190075Sobrien  enum tree_code code = TREE_CODE (exp);
244218334Speter
244350397Sobrien  /* Either set P and LEN to the address and len of something to hash and
244450397Sobrien     exit the switch or return a value.  */
244550397Sobrien
244650397Sobrien  switch (code)
244718334Speter    {
244850397Sobrien    case INTEGER_CST:
244990075Sobrien      p = (char *) &TREE_INT_CST (exp);
245090075Sobrien      len = sizeof TREE_INT_CST (exp);
245150397Sobrien      break;
245250397Sobrien
245350397Sobrien    case REAL_CST:
2454117395Skan      return real_hash (TREE_REAL_CST_PTR (exp));
245518334Speter
245650397Sobrien    case STRING_CST:
2457169689Skan      p = TREE_STRING_POINTER (exp);
2458169689Skan      len = TREE_STRING_LENGTH (exp);
245950397Sobrien      break;
246018334Speter
246150397Sobrien    case COMPLEX_CST:
2462117395Skan      return (const_hash_1 (TREE_REALPART (exp)) * 5
2463117395Skan	      + const_hash_1 (TREE_IMAGPART (exp)));
246418334Speter
246550397Sobrien    case CONSTRUCTOR:
2466169689Skan      {
2467169689Skan	unsigned HOST_WIDE_INT idx;
2468169689Skan	tree value;
246990075Sobrien
2470169689Skan	hi = 5 + int_size_in_bytes (TREE_TYPE (exp));
247118334Speter
2472169689Skan	FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
2473169689Skan	  if (value)
2474169689Skan	    hi = hi * 603 + const_hash_1 (value);
247550397Sobrien
2476169689Skan	return hi;
2477169689Skan      }
247850397Sobrien
247950397Sobrien    case ADDR_EXPR:
2480102780Skan    case FDESC_EXPR:
248150397Sobrien      {
248250397Sobrien	struct addr_const value;
248350397Sobrien
248450397Sobrien	decode_addr_const (exp, &value);
2485169689Skan	switch (GET_CODE (value.base))
248650397Sobrien	  {
2487169689Skan	  case SYMBOL_REF:
248850397Sobrien	    /* Don't hash the address of the SYMBOL_REF;
248950397Sobrien	       only use the offset and the symbol name.  */
249050397Sobrien	    hi = value.offset;
249150397Sobrien	    p = XSTR (value.base, 0);
249250397Sobrien	    for (i = 0; p[i] != 0; i++)
249350397Sobrien	      hi = ((hi * 613) + (unsigned) (p[i]));
2494169689Skan	    break;
2495169689Skan
2496169689Skan	  case LABEL_REF:
2497169689Skan	    hi = value.offset + CODE_LABEL_NUMBER (XEXP (value.base, 0)) * 13;
2498169689Skan	    break;
2499169689Skan
2500169689Skan	  default:
2501169689Skan	    gcc_unreachable ();
250250397Sobrien	  }
250350397Sobrien      }
250418334Speter      return hi;
250550397Sobrien
250650397Sobrien    case PLUS_EXPR:
250750397Sobrien    case MINUS_EXPR:
2508117395Skan      return (const_hash_1 (TREE_OPERAND (exp, 0)) * 9
2509117395Skan	      + const_hash_1 (TREE_OPERAND (exp, 1)));
251050397Sobrien
251150397Sobrien    case NOP_EXPR:
251250397Sobrien    case CONVERT_EXPR:
251350397Sobrien    case NON_LVALUE_EXPR:
2514117395Skan      return const_hash_1 (TREE_OPERAND (exp, 0)) * 7 + 2;
251590075Sobrien
251650397Sobrien    default:
251790075Sobrien      /* A language specific constant. Just hash the code.  */
2518117395Skan      return code;
251918334Speter    }
252018334Speter
2521132718Skan  /* Compute hashing function.  */
252218334Speter  hi = len;
252318334Speter  for (i = 0; i < len; i++)
252450397Sobrien    hi = ((hi * 613) + (unsigned) (p[i]));
252518334Speter
252618334Speter  return hi;
252718334Speter}
252818334Speter
2529132718Skan/* Wrapper of compare_constant, for the htab interface.  */
2530132718Skanstatic int
2531132718Skanconst_desc_eq (const void *p1, const void *p2)
2532132718Skan{
2533169689Skan  const struct constant_descriptor_tree *c1 = p1;
2534169689Skan  const struct constant_descriptor_tree *c2 = p2;
2535169689Skan  if (c1->hash != c2->hash)
2536169689Skan    return 0;
2537169689Skan  return compare_constant (c1->value, c2->value);
2538132718Skan}
2539132718Skan
2540117395Skan/* Compare t1 and t2, and return 1 only if they are known to result in
2541117395Skan   the same bit pattern on output.  */
2542117395Skan
254318334Speterstatic int
2544132718Skancompare_constant (const tree t1, const tree t2)
254518334Speter{
2546117395Skan  enum tree_code typecode;
254718334Speter
2548117395Skan  if (t1 == NULL_TREE)
2549117395Skan    return t2 == NULL_TREE;
2550117395Skan  if (t2 == NULL_TREE)
2551117395Skan    return 0;
255218334Speter
2553117395Skan  if (TREE_CODE (t1) != TREE_CODE (t2))
255418334Speter    return 0;
255518334Speter
2556117395Skan  switch (TREE_CODE (t1))
255718334Speter    {
255850397Sobrien    case INTEGER_CST:
255918334Speter      /* Integer constants are the same only if the same width of type.  */
2560117395Skan      if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
256118334Speter	return 0;
2562169689Skan      if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
2563169689Skan	return 0;
2564117395Skan      return tree_int_cst_equal (t1, t2);
256550397Sobrien
256650397Sobrien    case REAL_CST:
256718334Speter      /* Real constants are the same only if the same width of type.  */
2568117395Skan      if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
256918334Speter	return 0;
257050397Sobrien
2571117395Skan      return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
257250397Sobrien
257350397Sobrien    case STRING_CST:
2574117395Skan      if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
257550397Sobrien	return 0;
257650397Sobrien
2577117395Skan      return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
2578117395Skan	      && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
2579117395Skan			 TREE_STRING_LENGTH (t1)));
258050397Sobrien
2581117395Skan    case COMPLEX_CST:
2582117395Skan      return (compare_constant (TREE_REALPART (t1), TREE_REALPART (t2))
2583117395Skan	      && compare_constant (TREE_IMAGPART (t1), TREE_IMAGPART (t2)));
258450397Sobrien
2585117395Skan    case CONSTRUCTOR:
2586169689Skan      {
2587169689Skan	VEC(constructor_elt, gc) *v1, *v2;
2588169689Skan	unsigned HOST_WIDE_INT idx;
258918334Speter
2590169689Skan	typecode = TREE_CODE (TREE_TYPE (t1));
2591169689Skan	if (typecode != TREE_CODE (TREE_TYPE (t2)))
2592169689Skan	  return 0;
259350397Sobrien
2594169689Skan	if (typecode == ARRAY_TYPE)
2595169689Skan	  {
2596169689Skan	    HOST_WIDE_INT size_1 = int_size_in_bytes (TREE_TYPE (t1));
2597169689Skan	    /* For arrays, check that the sizes all match.  */
2598169689Skan	    if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))
2599169689Skan		|| size_1 == -1
2600169689Skan		|| size_1 != int_size_in_bytes (TREE_TYPE (t2)))
2601169689Skan	      return 0;
2602169689Skan	  }
2603169689Skan	else
2604169689Skan	  {
2605169689Skan	    /* For record and union constructors, require exact type
2606169689Skan               equality.  */
2607169689Skan	    if (TREE_TYPE (t1) != TREE_TYPE (t2))
2608169689Skan	      return 0;
2609169689Skan	  }
261050397Sobrien
2611169689Skan	v1 = CONSTRUCTOR_ELTS (t1);
2612169689Skan	v2 = CONSTRUCTOR_ELTS (t2);
2613169689Skan	if (VEC_length (constructor_elt, v1)
2614169689Skan	    != VEC_length (constructor_elt, v2))
2615117395Skan	    return 0;
2616117395Skan
2617169689Skan	for (idx = 0; idx < VEC_length (constructor_elt, v1); ++idx)
2618169689Skan	  {
2619169689Skan	    constructor_elt *c1 = VEC_index (constructor_elt, v1, idx);
2620169689Skan	    constructor_elt *c2 = VEC_index (constructor_elt, v2, idx);
262118334Speter
2622169689Skan	    /* Check that each value is the same...  */
2623169689Skan	    if (!compare_constant (c1->value, c2->value))
2624169689Skan	      return 0;
2625169689Skan	    /* ... and that they apply to the same fields!  */
2626169689Skan	    if (typecode == ARRAY_TYPE)
2627169689Skan	      {
2628169689Skan		if (!compare_constant (c1->index, c2->index))
2629169689Skan		  return 0;
2630169689Skan	      }
2631169689Skan	    else
2632169689Skan	      {
2633169689Skan		if (c1->index != c2->index)
2634169689Skan		  return 0;
2635169689Skan	      }
2636169689Skan	  }
263750397Sobrien
2638169689Skan	return 1;
2639169689Skan      }
264050397Sobrien
264150397Sobrien    case ADDR_EXPR:
2642102780Skan    case FDESC_EXPR:
264350397Sobrien      {
2644117395Skan	struct addr_const value1, value2;
264550397Sobrien
2646117395Skan	decode_addr_const (t1, &value1);
2647117395Skan	decode_addr_const (t2, &value2);
2648117395Skan	return (value1.offset == value2.offset
2649117395Skan		&& strcmp (XSTR (value1.base, 0), XSTR (value2.base, 0)) == 0);
265050397Sobrien      }
265150397Sobrien
265250397Sobrien    case PLUS_EXPR:
265350397Sobrien    case MINUS_EXPR:
265452284Sobrien    case RANGE_EXPR:
2655117395Skan      return (compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
2656117395Skan	      && compare_constant(TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
265750397Sobrien
265850397Sobrien    case NOP_EXPR:
265950397Sobrien    case CONVERT_EXPR:
266050397Sobrien    case NON_LVALUE_EXPR:
2661169689Skan    case VIEW_CONVERT_EXPR:
2662117395Skan      return compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
266350397Sobrien
266450397Sobrien    default:
266590075Sobrien      {
2666117395Skan	tree nt1, nt2;
2667169689Skan	nt1 = lang_hooks.expand_constant (t1);
2668169689Skan	nt2 = lang_hooks.expand_constant (t2);
2669117395Skan	if (nt1 != t1 || nt2 != t2)
2670117395Skan	  return compare_constant (nt1, nt2);
267190075Sobrien	else
267290075Sobrien	  return 0;
267390075Sobrien      }
267418334Speter    }
267518334Speter
2676169689Skan  gcc_unreachable ();
267718334Speter}
267818334Speter
2679117395Skan/* Make a copy of the whole tree structure for a constant.  This
2680169689Skan   handles the same types of nodes that compare_constant handles.  */
268118334Speter
268218334Speterstatic tree
2683132718Skancopy_constant (tree exp)
268418334Speter{
268518334Speter  switch (TREE_CODE (exp))
268618334Speter    {
268718334Speter    case ADDR_EXPR:
268818334Speter      /* For ADDR_EXPR, we do not want to copy the decl whose address
268918334Speter	 is requested.  We do want to copy constants though.  */
2690169689Skan      if (CONSTANT_CLASS_P (TREE_OPERAND (exp, 0)))
269118334Speter	return build1 (TREE_CODE (exp), TREE_TYPE (exp),
269218334Speter		       copy_constant (TREE_OPERAND (exp, 0)));
269318334Speter      else
269418334Speter	return copy_node (exp);
269518334Speter
269618334Speter    case INTEGER_CST:
269718334Speter    case REAL_CST:
2698169689Skan    case STRING_CST:
269918334Speter      return copy_node (exp);
270018334Speter
270118334Speter    case COMPLEX_CST:
270250397Sobrien      return build_complex (TREE_TYPE (exp),
270350397Sobrien			    copy_constant (TREE_REALPART (exp)),
270418334Speter			    copy_constant (TREE_IMAGPART (exp)));
270518334Speter
270618334Speter    case PLUS_EXPR:
270718334Speter    case MINUS_EXPR:
2708169689Skan      return build2 (TREE_CODE (exp), TREE_TYPE (exp),
2709169689Skan		     copy_constant (TREE_OPERAND (exp, 0)),
2710169689Skan		     copy_constant (TREE_OPERAND (exp, 1)));
271118334Speter
271218334Speter    case NOP_EXPR:
271318334Speter    case CONVERT_EXPR:
271450397Sobrien    case NON_LVALUE_EXPR:
2715132718Skan    case VIEW_CONVERT_EXPR:
271618334Speter      return build1 (TREE_CODE (exp), TREE_TYPE (exp),
271718334Speter		     copy_constant (TREE_OPERAND (exp, 0)));
271818334Speter
271918334Speter    case CONSTRUCTOR:
272018334Speter      {
272118334Speter	tree copy = copy_node (exp);
2722169689Skan	VEC(constructor_elt, gc) *v;
2723169689Skan	unsigned HOST_WIDE_INT idx;
2724169689Skan	tree purpose, value;
272518334Speter
2726169689Skan	v = VEC_alloc(constructor_elt, gc, VEC_length(constructor_elt,
2727169689Skan						      CONSTRUCTOR_ELTS (exp)));
2728169689Skan	FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, purpose, value)
2729169689Skan	  {
2730169689Skan	    constructor_elt *ce = VEC_quick_push (constructor_elt, v, NULL);
2731169689Skan	    ce->index = purpose;
2732169689Skan	    ce->value = copy_constant (value);
2733169689Skan	  }
2734169689Skan	CONSTRUCTOR_ELTS (copy) = v;
273518334Speter	return copy;
273618334Speter      }
273718334Speter
273818334Speter    default:
2739117395Skan      {
2740169689Skan	tree t = lang_hooks.expand_constant (exp);
2741169689Skan
2742169689Skan	gcc_assert (t != exp);
2743169689Skan	return copy_constant (t);
2744117395Skan      }
274518334Speter    }
274618334Speter}
274718334Speter
2748169689Skan/* Return the alignment of constant EXP in bits.  */
2749169689Skan
2750169689Skanstatic unsigned int
2751169689Skanget_constant_alignment (tree exp)
2752169689Skan{
2753169689Skan  unsigned int align;
2754169689Skan
2755169689Skan  align = TYPE_ALIGN (TREE_TYPE (exp));
2756169689Skan#ifdef CONSTANT_ALIGNMENT
2757169689Skan  align = CONSTANT_ALIGNMENT (exp, align);
2758169689Skan#endif
2759169689Skan  return align;
2760169689Skan}
2761169689Skan
2762169689Skan/* Return the section into which constant EXP should be placed.  */
2763169689Skan
2764169689Skanstatic section *
2765169689Skanget_constant_section (tree exp)
2766169689Skan{
2767169689Skan  if (IN_NAMED_SECTION (exp))
2768169689Skan    return get_named_section (exp, NULL, compute_reloc_for_constant (exp));
2769169689Skan  else
2770169689Skan    return targetm.asm_out.select_section (exp,
2771169689Skan					   compute_reloc_for_constant (exp),
2772169689Skan					   get_constant_alignment (exp));
2773169689Skan}
2774169689Skan
2775169689Skan/* Return the size of constant EXP in bytes.  */
2776169689Skan
2777169689Skanstatic HOST_WIDE_INT
2778169689Skanget_constant_size (tree exp)
2779169689Skan{
2780169689Skan  HOST_WIDE_INT size;
2781169689Skan
2782169689Skan  size = int_size_in_bytes (TREE_TYPE (exp));
2783169689Skan  if (TREE_CODE (exp) == STRING_CST)
2784169689Skan    size = MAX (TREE_STRING_LENGTH (exp), size);
2785169689Skan  return size;
2786169689Skan}
2787169689Skan
2788132718Skan/* Subroutine of output_constant_def:
2789132718Skan   No constant equal to EXP is known to have been output.
2790132718Skan   Make a constant descriptor to enter EXP in the hash table.
2791132718Skan   Assign the label number and construct RTL to refer to the
2792132718Skan   constant's location in memory.
2793132718Skan   Caller is responsible for updating the hash table.  */
279418334Speter
2795132718Skanstatic struct constant_descriptor_tree *
2796132718Skanbuild_constant_desc (tree exp)
279718334Speter{
2798132718Skan  rtx symbol;
2799132718Skan  rtx rtl;
2800132718Skan  char label[256];
2801132718Skan  int labelno;
2802117395Skan  struct constant_descriptor_tree *desc;
280318334Speter
2804132718Skan  desc = ggc_alloc (sizeof (*desc));
2805146895Skan  desc->value = copy_constant (exp);
2806122180Skan
2807169689Skan  /* Propagate marked-ness to copied constant.  */
2808169689Skan  if (flag_mudflap && mf_marked_p (exp))
2809169689Skan    mf_mark (desc->value);
2810169689Skan
2811132718Skan  /* Create a string containing the label name, in LABEL.  */
2812132718Skan  labelno = const_labelno++;
2813132718Skan  ASM_GENERATE_INTERNAL_LABEL (label, "LC", labelno);
281418334Speter
2815132718Skan  /* We have a symbol name; construct the SYMBOL_REF and the MEM.  */
2816169689Skan  if (use_object_blocks_p ())
2817169689Skan    {
2818169689Skan      section *sect = get_constant_section (exp);
2819169689Skan      symbol = create_block_symbol (ggc_strdup (label),
2820169689Skan				    get_block_for_section (sect), -1);
2821169689Skan    }
2822169689Skan  else
2823169689Skan    symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
2824169689Skan  SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
2825169689Skan  SET_SYMBOL_REF_DECL (symbol, desc->value);
2826132718Skan  TREE_CONSTANT_POOL_ADDRESS_P (symbol) = 1;
282718334Speter
2828132718Skan  rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (exp)), symbol);
2829132718Skan  set_mem_attributes (rtl, exp, 1);
2830132718Skan  set_mem_alias_set (rtl, 0);
2831132718Skan  set_mem_alias_set (rtl, const_alias_set);
283218334Speter
2833132718Skan  /* Set flags or add text to the name to record information, such as
2834132718Skan     that it is a local symbol.  If the name is changed, the macro
2835132718Skan     ASM_OUTPUT_LABELREF will have to know how to strip this
2836132718Skan     information.  This call might invalidate our local variable
2837132718Skan     SYMBOL; we can't use it afterward.  */
283818334Speter
2839169689Skan  targetm.encode_section_info (exp, rtl, true);
284090075Sobrien
2841132718Skan  desc->rtl = rtl;
284290075Sobrien
2843132718Skan  return desc;
2844132718Skan}
284590075Sobrien
2846132718Skan/* Return an rtx representing a reference to constant data in memory
2847132718Skan   for the constant expression EXP.
284818334Speter
2849132718Skan   If assembler code for such a constant has already been output,
2850132718Skan   return an rtx to refer to it.
2851132718Skan   Otherwise, output such a constant in memory
2852132718Skan   and generate an rtx for it.
285390075Sobrien
2854132718Skan   If DEFER is nonzero, this constant can be deferred and output only
2855132718Skan   if referenced in the function after all optimizations.
285690075Sobrien
2857132718Skan   `const_desc_table' records which constants already have label strings.  */
285890075Sobrien
2859132718Skanrtx
2860132718Skanoutput_constant_def (tree exp, int defer)
2861132718Skan{
2862132718Skan  struct constant_descriptor_tree *desc;
2863132718Skan  struct constant_descriptor_tree key;
2864132718Skan  void **loc;
286518334Speter
2866132718Skan  /* Look up EXP in the table of constant descriptors.  If we didn't find
2867132718Skan     it, create a new one.  */
2868132718Skan  key.value = exp;
2869169689Skan  key.hash = const_hash_1 (exp);
2870169689Skan  loc = htab_find_slot_with_hash (const_desc_htab, &key, key.hash, INSERT);
287118334Speter
2872132718Skan  desc = *loc;
2873132718Skan  if (desc == 0)
287490075Sobrien    {
2875132718Skan      desc = build_constant_desc (exp);
2876169689Skan      desc->hash = key.hash;
2877132718Skan      *loc = desc;
287890075Sobrien    }
287918334Speter
2880132718Skan  maybe_output_constant_def_contents (desc, defer);
2881132718Skan  return desc->rtl;
2882132718Skan}
288390075Sobrien
2884132718Skan/* Subroutine of output_constant_def: Decide whether or not we need to
2885132718Skan   output the constant DESC now, and if so, do it.  */
2886132718Skanstatic void
2887132718Skanmaybe_output_constant_def_contents (struct constant_descriptor_tree *desc,
2888132718Skan				    int defer)
2889132718Skan{
2890132718Skan  rtx symbol = XEXP (desc->rtl, 0);
2891132718Skan  tree exp = desc->value;
289290075Sobrien
2893132718Skan  if (flag_syntax_only)
2894132718Skan    return;
289518334Speter
2896132718Skan  if (TREE_ASM_WRITTEN (exp))
2897132718Skan    /* Already output; don't do it again.  */
2898132718Skan    return;
289990075Sobrien
2900169689Skan  /* We can always defer constants as long as the context allows
2901169689Skan     doing so.  */
2902169689Skan  if (defer)
2903132718Skan    {
2904132718Skan      /* Increment n_deferred_constants if it exists.  It needs to be at
2905132718Skan	 least as large as the number of constants actually referred to
2906132718Skan	 by the function.  If it's too small we'll stop looking too early
2907132718Skan	 and fail to emit constants; if it's too large we'll only look
2908132718Skan	 through the entire function when we could have stopped earlier.  */
2909132718Skan      if (cfun)
2910132718Skan	n_deferred_constants++;
2911132718Skan      return;
291218334Speter    }
291318334Speter
2914132718Skan  output_constant_def_contents (symbol);
291518334Speter}
291618334Speter
2917169689Skan/* Subroutine of output_constant_def_contents.  Output the definition
2918169689Skan   of constant EXP, which is pointed to by label LABEL.  ALIGN is the
2919169689Skan   constant's alignment in bits.  */
292018334Speter
292118334Speterstatic void
2922169689Skanassemble_constant_contents (tree exp, const char *label, unsigned int align)
292318334Speter{
2924122180Skan  HOST_WIDE_INT size;
292518334Speter
2926169689Skan  size = get_constant_size (exp);
2927132718Skan
2928122180Skan  /* Do any machine/system dependent processing of the constant.  */
2929122180Skan#ifdef ASM_DECLARE_CONSTANT_NAME
2930132718Skan  ASM_DECLARE_CONSTANT_NAME (asm_out_file, label, exp, size);
2931122180Skan#else
2932122180Skan  /* Standard thing is just output label for the constant.  */
2933132718Skan  ASM_OUTPUT_LABEL (asm_out_file, label);
2934122180Skan#endif /* ASM_DECLARE_CONSTANT_NAME */
293518334Speter
293618334Speter  /* Output the value of EXP.  */
2937122180Skan  output_constant (exp, size, align);
293818334Speter}
2939132718Skan
2940169689Skan/* We must output the constant data referred to by SYMBOL; do so.  */
2941169689Skan
2942169689Skanstatic void
2943169689Skanoutput_constant_def_contents (rtx symbol)
2944132718Skan{
2945169689Skan  tree exp = SYMBOL_REF_DECL (symbol);
2946169689Skan  unsigned int align;
2947169689Skan
2948169689Skan  /* Make sure any other constants whose addresses appear in EXP
2949169689Skan     are assigned label numbers.  */
2950169689Skan  output_addressed_constants (exp);
2951169689Skan
2952169689Skan  /* We are no longer deferring this constant.  */
2953169689Skan  TREE_ASM_WRITTEN (exp) = 1;
2954169689Skan
2955169689Skan  /* If the constant is part of an object block, make sure that the
2956169689Skan     decl has been positioned within its block, but do not write out
2957169689Skan     its definition yet.  output_object_blocks will do that later.  */
2958169689Skan  if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
2959169689Skan    place_block_symbol (symbol);
2960169689Skan  else
2961169689Skan    {
2962169689Skan      switch_to_section (get_constant_section (exp));
2963169689Skan      align = get_constant_alignment (exp);
2964169689Skan      if (align > BITS_PER_UNIT)
2965169689Skan	ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
2966169689Skan      assemble_constant_contents (exp, XSTR (symbol, 0), align);
2967169689Skan    }
2968169689Skan  if (flag_mudflap)
2969169689Skan    mudflap_enqueue_constant (exp);
2970132718Skan}
2971132718Skan
2972132718Skan/* Look up EXP in the table of constant descriptors.  Return the rtl
2973132718Skan   if it has been emitted, else null.  */
2974132718Skan
2975132718Skanrtx
2976132718Skanlookup_constant_def (tree exp)
2977132718Skan{
2978132718Skan  struct constant_descriptor_tree *desc;
2979132718Skan  struct constant_descriptor_tree key;
2980132718Skan
2981132718Skan  key.value = exp;
2982169689Skan  key.hash = const_hash_1 (exp);
2983169689Skan  desc = htab_find_with_hash (const_desc_htab, &key, key.hash);
2984132718Skan
2985132718Skan  return (desc ? desc->rtl : NULL_RTX);
2986132718Skan}
298718334Speter
2988117395Skan/* Used in the hash tables to avoid outputting the same constant
2989117395Skan   twice.  Unlike 'struct constant_descriptor_tree', RTX constants
2990169689Skan   are output once per function, not once per file.  */
2991169689Skan/* ??? Only a few targets need per-function constant pools.  Most
2992169689Skan   can use one per-file pool.  Should add a targetm bit to tell the
2993169689Skan   difference.  */
2994117395Skan
2995169689Skanstruct rtx_constant_pool GTY(())
2996117395Skan{
2997169689Skan  /* Pointers to first and last constant in pool, as ordered by offset.  */
2998169689Skan  struct constant_descriptor_rtx *first;
2999169689Skan  struct constant_descriptor_rtx *last;
3000117395Skan
3001169689Skan  /* Hash facility for making memory-constants from constant rtl-expressions.
3002169689Skan     It is used on RISC machines where immediate integer arguments and
3003169689Skan     constant addresses are restricted so that such constants must be stored
3004169689Skan     in memory.  */
3005169689Skan  htab_t GTY((param_is (struct constant_descriptor_rtx))) const_rtx_htab;
3006117395Skan
3007169689Skan  /* Current offset in constant pool (does not include any
3008169689Skan     machine-specific header).  */
3009169689Skan  HOST_WIDE_INT offset;
3010117395Skan};
3011117395Skan
3012169689Skanstruct constant_descriptor_rtx GTY((chain_next ("%h.next")))
301318334Speter{
3014169689Skan  struct constant_descriptor_rtx *next;
3015169689Skan  rtx mem;
3016169689Skan  rtx sym;
301790075Sobrien  rtx constant;
3018169689Skan  HOST_WIDE_INT offset;
3019169689Skan  hashval_t hash;
302018334Speter  enum machine_mode mode;
3021169689Skan  unsigned int align;
302218334Speter  int labelno;
302350397Sobrien  int mark;
302418334Speter};
302518334Speter
3026169689Skan/* Hash and compare functions for const_rtx_htab.  */
302718334Speter
3028169689Skanstatic hashval_t
3029169689Skanconst_desc_rtx_hash (const void *ptr)
3030169689Skan{
3031169689Skan  const struct constant_descriptor_rtx *desc = ptr;
3032169689Skan  return desc->hash;
3033169689Skan}
303418334Speter
3035169689Skanstatic int
3036169689Skanconst_desc_rtx_eq (const void *a, const void *b)
303718334Speter{
3038169689Skan  const struct constant_descriptor_rtx *x = a;
3039169689Skan  const struct constant_descriptor_rtx *y = b;
304018334Speter
3041169689Skan  if (x->mode != y->mode)
3042169689Skan    return 0;
3043169689Skan  return rtx_equal_p (x->constant, y->constant);
304418334Speter}
304518334Speter
3046169689Skan/* This is the worker function for const_rtx_hash, called via for_each_rtx.  */
304718334Speter
3048169689Skanstatic int
3049169689Skanconst_rtx_hash_1 (rtx *xp, void *data)
305018334Speter{
3051169689Skan  unsigned HOST_WIDE_INT hwi;
3052169689Skan  enum machine_mode mode;
3053169689Skan  enum rtx_code code;
3054169689Skan  hashval_t h, *hp;
3055169689Skan  rtx x;
305618334Speter
3057169689Skan  x = *xp;
3058169689Skan  code = GET_CODE (x);
3059169689Skan  mode = GET_MODE (x);
3060169689Skan  h = (hashval_t) code * 1048573 + mode;
306118334Speter
3062169689Skan  switch (code)
306318334Speter    {
3064169689Skan    case CONST_INT:
3065169689Skan      hwi = INTVAL (x);
3066169689Skan    fold_hwi:
3067169689Skan      {
3068169689Skan	const int shift = sizeof (hashval_t) * CHAR_BIT;
3069169689Skan	const int n = sizeof (HOST_WIDE_INT) / sizeof (hashval_t);
3070169689Skan	int i;
3071169689Skan
3072169689Skan	h ^= (hashval_t) hwi;
3073169689Skan	for (i = 1; i < n; ++i)
3074169689Skan	  {
3075169689Skan	    hwi >>= shift;
3076169689Skan	    h ^= (hashval_t) hwi;
3077169689Skan	  }
3078169689Skan      }
3079169689Skan      break;
3080169689Skan
308118334Speter    case CONST_DOUBLE:
3082169689Skan      if (mode == VOIDmode)
308318334Speter	{
3084169689Skan	  hwi = CONST_DOUBLE_LOW (x) ^ CONST_DOUBLE_HIGH (x);
3085169689Skan	  goto fold_hwi;
308618334Speter	}
308718334Speter      else
3088169689Skan	h ^= real_hash (CONST_DOUBLE_REAL_VALUE (x));
308918334Speter      break;
309018334Speter
309196263Sobrien    case CONST_VECTOR:
309296263Sobrien      {
3093169689Skan	int i;
3094169689Skan	for (i = XVECLEN (x, 0); i-- > 0; )
3095169689Skan	  h = h * 251 + const_rtx_hash_1 (&XVECEXP (x, 0, i), data);
309696263Sobrien      }
309796263Sobrien      break;
309896263Sobrien
3099169689Skan    case SYMBOL_REF:
3100169689Skan      h ^= htab_hash_string (XSTR (x, 0));
310118334Speter      break;
310218334Speter
310318334Speter    case LABEL_REF:
3104169689Skan      h = h * 251 + CODE_LABEL_NUMBER (XEXP (x, 0));
310518334Speter      break;
310618334Speter
3107169689Skan    case UNSPEC:
3108169689Skan    case UNSPEC_VOLATILE:
3109169689Skan      h = h * 251 + XINT (x, 1);
311018334Speter      break;
311118334Speter
311218334Speter    default:
311396263Sobrien      break;
311418334Speter    }
311518334Speter
3116169689Skan  hp = data;
3117169689Skan  *hp = *hp * 509 + h;
3118169689Skan  return 0;
311918334Speter}
312018334Speter
3121169689Skan/* Compute a hash value for X, which should be a constant.  */
312218334Speter
3123169689Skanstatic hashval_t
3124169689Skanconst_rtx_hash (rtx x)
312518334Speter{
3126169689Skan  hashval_t h = 0;
3127169689Skan  for_each_rtx (&x, const_rtx_hash_1, &h);
3128169689Skan  return h;
312918334Speter}
313018334Speter
3131169689Skan
3132169689Skan/* Create and return a new rtx constant pool.  */
313318334Speter
3134169689Skanstatic struct rtx_constant_pool *
3135169689Skancreate_constant_pool (void)
313618334Speter{
3137169689Skan  struct rtx_constant_pool *pool;
3138117395Skan
3139169689Skan  pool = ggc_alloc (sizeof (struct rtx_constant_pool));
3140169689Skan  pool->const_rtx_htab = htab_create_ggc (31, const_desc_rtx_hash,
3141169689Skan					  const_desc_rtx_eq, NULL);
3142169689Skan  pool->first = NULL;
3143169689Skan  pool->last = NULL;
3144169689Skan  pool->offset = 0;
3145169689Skan  return pool;
314618334Speter}
314718334Speter
3148169689Skan/* Initialize constant pool hashing for a new function.  */
314918334Speter
3150169689Skanvoid
3151169689Skaninit_varasm_status (struct function *f)
315218334Speter{
3153169689Skan  struct varasm_status *p;
315418334Speter
3155169689Skan  p = ggc_alloc (sizeof (struct varasm_status));
3156169689Skan  f->varasm = p;
315718334Speter
3158169689Skan  p->pool = create_constant_pool ();
3159169689Skan  p->deferred_constants = 0;
316018334Speter}
3161169689Skan
3162169689Skan/* Given a MINUS expression, simplify it if both sides
3163169689Skan   include the same symbol.  */
316418334Speter
3165169689Skanrtx
3166169689Skansimplify_subtraction (rtx x)
316718334Speter{
3168169689Skan  rtx r = simplify_rtx (x);
3169169689Skan  return r ? r : x;
317090075Sobrien}
317190075Sobrien
317218334Speter/* Given a constant rtx X, make (or find) a memory constant for its value
317318334Speter   and return a MEM rtx to refer to it in memory.  */
317418334Speter
317518334Speterrtx
3176132718Skanforce_const_mem (enum machine_mode mode, rtx x)
317718334Speter{
3178169689Skan  struct constant_descriptor_rtx *desc, tmp;
3179169689Skan  struct rtx_constant_pool *pool;
318018334Speter  char label[256];
3181132718Skan  rtx def, symbol;
3182169689Skan  hashval_t hash;
318390075Sobrien  unsigned int align;
3184169689Skan  void **slot;
318518334Speter
3186117395Skan  /* If we're not allowed to drop X into the constant pool, don't.  */
3187169689Skan  if (targetm.cannot_force_const_mem (x))
3188117395Skan    return NULL_RTX;
3189117395Skan
3190169689Skan  /* Record that this function has used a constant pool entry.  */
3191169689Skan  current_function_uses_const_pool = 1;
319218334Speter
3193169689Skan  /* Decide which pool to use.  */
3194169689Skan  pool = (targetm.use_blocks_for_constant_p (mode, x)
3195169689Skan	  ? shared_constant_pool
3196169689Skan	  : cfun->varasm->pool);
3197117395Skan
3198169689Skan  /* Lookup the value in the hashtable.  */
3199169689Skan  tmp.constant = x;
3200169689Skan  tmp.mode = mode;
3201169689Skan  hash = const_rtx_hash (x);
3202169689Skan  slot = htab_find_slot_with_hash (pool->const_rtx_htab, &tmp, hash, INSERT);
3203169689Skan  desc = *slot;
3204169689Skan
3205169689Skan  /* If the constant was already present, return its memory.  */
3206169689Skan  if (desc)
3207169689Skan    return copy_rtx (desc->mem);
3208169689Skan
3209169689Skan  /* Otherwise, create a new descriptor.  */
3210169689Skan  desc = ggc_alloc (sizeof (*desc));
3211169689Skan  *slot = desc;
3212169689Skan
321390075Sobrien  /* Align the location counter as required by EXP's data type.  */
321490075Sobrien  align = GET_MODE_ALIGNMENT (mode == VOIDmode ? word_mode : mode);
321550397Sobrien#ifdef CONSTANT_ALIGNMENT
3216132718Skan  {
3217169689Skan    tree type = lang_hooks.types.type_for_mode (mode, 0);
3218132718Skan    if (type != NULL_TREE)
3219132718Skan      align = CONSTANT_ALIGNMENT (make_tree (type, x), align);
3220132718Skan  }
322150397Sobrien#endif
322218334Speter
3223169689Skan  pool->offset += (align / BITS_PER_UNIT) - 1;
3224169689Skan  pool->offset &= ~ ((align / BITS_PER_UNIT) - 1);
322518334Speter
3226169689Skan  desc->next = NULL;
3227169689Skan  desc->constant = tmp.constant;
3228169689Skan  desc->offset = pool->offset;
3229169689Skan  desc->hash = hash;
3230169689Skan  desc->mode = mode;
3231169689Skan  desc->align = align;
3232169689Skan  desc->labelno = const_labelno;
3233169689Skan  desc->mark = 0;
323418334Speter
3235169689Skan  pool->offset += GET_MODE_SIZE (mode);
3236169689Skan  if (pool->last)
3237169689Skan    pool->last->next = desc;
323890075Sobrien  else
3239169689Skan    pool->first = pool->last = desc;
3240169689Skan  pool->last = desc;
3241117395Skan
324290075Sobrien  /* Create a string containing the label name, in LABEL.  */
324390075Sobrien  ASM_GENERATE_INTERNAL_LABEL (label, "LC", const_labelno);
324490075Sobrien  ++const_labelno;
324574722Sobrien
3246169689Skan  /* Construct the SYMBOL_REF.  Make sure to mark it as belonging to
3247169689Skan     the constants pool.  */
3248169689Skan  if (use_object_blocks_p () && targetm.use_blocks_for_constant_p (mode, x))
3249169689Skan    {
3250169689Skan      section *sect = targetm.asm_out.select_rtx_section (mode, x, align);
3251169689Skan      symbol = create_block_symbol (ggc_strdup (label),
3252169689Skan				    get_block_for_section (sect), -1);
3253169689Skan    }
3254169689Skan  else
3255169689Skan    symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3256169689Skan  desc->sym = symbol;
3257169689Skan  SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3258169689Skan  CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3259169689Skan  SET_SYMBOL_REF_CONSTANT (symbol, desc);
326018334Speter
3261169689Skan  /* Construct the MEM.  */
3262169689Skan  desc->mem = def = gen_const_mem (mode, symbol);
3263169689Skan  set_mem_attributes (def, lang_hooks.types.type_for_mode (mode, 0), 1);
3264169689Skan  set_mem_align (def, align);
3265132718Skan
3266169689Skan  /* If we're dropping a label to the constant pool, make sure we
3267169689Skan     don't delete it.  */
3268169689Skan  if (GET_CODE (x) == LABEL_REF)
3269169689Skan    LABEL_PRESERVE_P (XEXP (x, 0)) = 1;
327018334Speter
3271132718Skan  return copy_rtx (def);
327218334Speter}
327318334Speter
327418334Speter/* Given a constant pool SYMBOL_REF, return the corresponding constant.  */
327518334Speter
327618334Speterrtx
3277132718Skanget_pool_constant (rtx addr)
327818334Speter{
3279169689Skan  return SYMBOL_REF_CONSTANT (addr)->constant;
328018334Speter}
328118334Speter
328296263Sobrien/* Given a constant pool SYMBOL_REF, return the corresponding constant
328396263Sobrien   and whether it has been output or not.  */
328496263Sobrien
328596263Sobrienrtx
3286132718Skanget_pool_constant_mark (rtx addr, bool *pmarked)
328796263Sobrien{
3288169689Skan  struct constant_descriptor_rtx *desc;
328996263Sobrien
3290169689Skan  desc = SYMBOL_REF_CONSTANT (addr);
3291169689Skan  *pmarked = (desc->mark != 0);
3292169689Skan  return desc->constant;
329390075Sobrien}
329490075Sobrien
329518334Speter/* Similar, return the mode.  */
329618334Speter
329718334Speterenum machine_mode
3298132718Skanget_pool_mode (rtx addr)
329918334Speter{
3300169689Skan  return SYMBOL_REF_CONSTANT (addr)->mode;
330118334Speter}
330218334Speter
330318334Speter/* Return the size of the constant pool.  */
330418334Speter
330518334Speterint
3306132718Skanget_pool_size (void)
330718334Speter{
3308169689Skan  return cfun->varasm->pool->offset;
330918334Speter}
331018334Speter
3311169689Skan/* Worker function for output_constant_pool_1.  Emit assembly for X
3312169689Skan   in MODE with known alignment ALIGN.  */
331318334Speter
3314169689Skanstatic void
3315169689Skanoutput_constant_pool_2 (enum machine_mode mode, rtx x, unsigned int align)
331618334Speter{
3317169689Skan  switch (GET_MODE_CLASS (mode))
3318169689Skan    {
3319169689Skan    case MODE_FLOAT:
3320169689Skan    case MODE_DECIMAL_FLOAT:
3321169689Skan      {
3322169689Skan	REAL_VALUE_TYPE r;
332318334Speter
3324169689Skan	gcc_assert (GET_CODE (x) == CONST_DOUBLE);
3325169689Skan	REAL_VALUE_FROM_CONST_DOUBLE (r, x);
3326169689Skan	assemble_real (r, mode, align);
3327169689Skan	break;
3328169689Skan      }
332950397Sobrien
3330169689Skan    case MODE_INT:
3331169689Skan    case MODE_PARTIAL_INT:
3332169689Skan      assemble_integer (x, GET_MODE_SIZE (mode), align, 1);
3333169689Skan      break;
333418334Speter
3335169689Skan    case MODE_VECTOR_FLOAT:
3336169689Skan    case MODE_VECTOR_INT:
3337169689Skan      {
3338169689Skan	int i, units;
3339169689Skan        enum machine_mode submode = GET_MODE_INNER (mode);
3340169689Skan	unsigned int subalign = MIN (align, GET_MODE_BITSIZE (submode));
334190075Sobrien
3342169689Skan	gcc_assert (GET_CODE (x) == CONST_VECTOR);
3343169689Skan	units = CONST_VECTOR_NUNITS (x);
334418334Speter
3345169689Skan	for (i = 0; i < units; i++)
3346169689Skan	  {
3347169689Skan	    rtx elt = CONST_VECTOR_ELT (x, i);
3348169689Skan	    output_constant_pool_2 (submode, elt, i ? subalign : align);
3349169689Skan	  }
3350169689Skan      }
3351169689Skan      break;
335250397Sobrien
3353169689Skan    default:
3354169689Skan      gcc_unreachable ();
3355169689Skan    }
3356169689Skan}
335718334Speter
3358169689Skan/* Worker function for output_constant_pool.  Emit constant DESC,
3359169689Skan   giving it ALIGN bits of alignment.  */
336090075Sobrien
3361169689Skanstatic void
3362169689Skanoutput_constant_pool_1 (struct constant_descriptor_rtx *desc,
3363169689Skan			unsigned int align)
3364169689Skan{
3365169689Skan  rtx x, tmp;
336690075Sobrien
3367169689Skan  x = desc->constant;
336890075Sobrien
3369169689Skan  /* See if X is a LABEL_REF (or a CONST referring to a LABEL_REF)
3370169689Skan     whose CODE_LABEL has been deleted.  This can occur if a jump table
3371169689Skan     is eliminated by optimization.  If so, write a constant of zero
3372169689Skan     instead.  Note that this can also happen by turning the
3373169689Skan     CODE_LABEL into a NOTE.  */
3374169689Skan  /* ??? This seems completely and utterly wrong.  Certainly it's
3375169689Skan     not true for NOTE_INSN_DELETED_LABEL, but I disbelieve proper
3376169689Skan     functioning even with INSN_DELETED_P and friends.  */
337718334Speter
3378169689Skan  tmp = x;
3379169689Skan  switch (GET_CODE (x))
3380169689Skan    {
3381169689Skan    case CONST:
3382169689Skan      if (GET_CODE (XEXP (x, 0)) != PLUS
3383169689Skan	  || GET_CODE (XEXP (XEXP (x, 0), 0)) != LABEL_REF)
3384169689Skan	break;
3385169689Skan      tmp = XEXP (XEXP (x, 0), 0);
3386169689Skan      /* FALLTHRU  */
3387169689Skan
3388169689Skan    case LABEL_REF:
3389169689Skan      tmp = XEXP (x, 0);
3390169689Skan      gcc_assert (!INSN_DELETED_P (tmp));
3391169689Skan      gcc_assert (!NOTE_P (tmp)
3392169689Skan		  || NOTE_LINE_NUMBER (tmp) != NOTE_INSN_DELETED);
3393169689Skan      break;
3394169689Skan
3395169689Skan    default:
3396169689Skan      break;
3397169689Skan    }
3398169689Skan
339918334Speter#ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3400169689Skan  ASM_OUTPUT_SPECIAL_POOL_ENTRY (asm_out_file, x, desc->mode,
3401169689Skan				 align, desc->labelno, done);
340218334Speter#endif
340318334Speter
3404169689Skan  assemble_align (align);
340518334Speter
3406169689Skan  /* Output the label.  */
3407169689Skan  targetm.asm_out.internal_label (asm_out_file, "LC", desc->labelno);
340818334Speter
3409169689Skan  /* Output the data.  */
3410169689Skan  output_constant_pool_2 (desc->mode, x, align);
341118334Speter
3412169689Skan  /* Make sure all constants in SECTION_MERGE and not SECTION_STRINGS
3413169689Skan     sections have proper size.  */
3414169689Skan  if (align > GET_MODE_BITSIZE (desc->mode)
3415169689Skan      && in_section
3416169689Skan      && (in_section->common.flags & SECTION_MERGE))
3417169689Skan    assemble_align (align);
341818334Speter
3419169689Skan#ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3420169689Skan done:
3421169689Skan#endif
3422169689Skan  return;
3423169689Skan}
342418334Speter
3425169689Skan/* Given a SYMBOL_REF CURRENT_RTX, mark it and all constants it refers
3426169689Skan   to as used.  Emit referenced deferred strings.  This function can
3427169689Skan   be used with for_each_rtx to mark all SYMBOL_REFs in an rtx.  */
342896263Sobrien
3429169689Skanstatic int
3430169689Skanmark_constant (rtx *current_rtx, void *data ATTRIBUTE_UNUSED)
3431169689Skan{
3432169689Skan  rtx x = *current_rtx;
343396263Sobrien
3434169689Skan  if (x == NULL_RTX || GET_CODE (x) != SYMBOL_REF)
3435169689Skan    return 0;
343696263Sobrien
3437169689Skan  if (CONSTANT_POOL_ADDRESS_P (x))
3438169689Skan    {
3439169689Skan      struct constant_descriptor_rtx *desc = SYMBOL_REF_CONSTANT (x);
3440169689Skan      if (desc->mark == 0)
3441169689Skan	{
3442169689Skan	  desc->mark = 1;
3443169689Skan	  for_each_rtx (&desc->constant, mark_constant, NULL);
3444169689Skan	}
3445169689Skan    }
3446169689Skan  else if (TREE_CONSTANT_POOL_ADDRESS_P (x))
3447169689Skan    {
3448169689Skan      tree exp = SYMBOL_REF_DECL (x);
3449169689Skan      if (!TREE_ASM_WRITTEN (exp))
3450169689Skan	{
3451169689Skan	  n_deferred_constants--;
3452169689Skan	  output_constant_def_contents (x);
3453169689Skan	}
3454169689Skan    }
345596263Sobrien
3456169689Skan  return -1;
3457169689Skan}
345896263Sobrien
3459169689Skan/* Look through appropriate parts of INSN, marking all entries in the
3460169689Skan   constant pool which are actually being used.  Entries that are only
3461169689Skan   referenced by other constants are also marked as used.  Emit
3462169689Skan   deferred strings that are used.  */
346396263Sobrien
3464169689Skanstatic void
3465169689Skanmark_constants (rtx insn)
3466169689Skan{
3467169689Skan  if (!INSN_P (insn))
3468169689Skan    return;
346996263Sobrien
3470169689Skan  /* Insns may appear inside a SEQUENCE.  Only check the patterns of
3471169689Skan     insns, not any notes that may be attached.  We don't want to mark
3472169689Skan     a constant just because it happens to appear in a REG_EQUIV note.  */
3473169689Skan  if (GET_CODE (PATTERN (insn)) == SEQUENCE)
3474169689Skan    {
3475169689Skan      rtx seq = PATTERN (insn);
3476169689Skan      int i, n = XVECLEN (seq, 0);
3477169689Skan      for (i = 0; i < n; ++i)
3478169689Skan	{
3479169689Skan	  rtx subinsn = XVECEXP (seq, 0, i);
3480169689Skan	  if (INSN_P (subinsn))
3481169689Skan	    for_each_rtx (&PATTERN (subinsn), mark_constant, NULL);
348218334Speter	}
348318334Speter    }
3484169689Skan  else
3485169689Skan    for_each_rtx (&PATTERN (insn), mark_constant, NULL);
348618334Speter}
348750397Sobrien
348850397Sobrien/* Look through the instructions for this function, and mark all the
3489169689Skan   entries in POOL which are actually being used.  Emit deferred constants
3490169689Skan   which have indeed been used.  */
349150397Sobrien
349250397Sobrienstatic void
3493132718Skanmark_constant_pool (void)
349450397Sobrien{
3495169689Skan  rtx insn, link;
349650397Sobrien
3497169689Skan  if (!current_function_uses_const_pool && n_deferred_constants == 0)
349850397Sobrien    return;
349950397Sobrien
350050397Sobrien  for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3501169689Skan    mark_constants (insn);
350250397Sobrien
3503117395Skan  for (link = current_function_epilogue_delay_list;
3504117395Skan       link;
3505117395Skan       link = XEXP (link, 1))
3506169689Skan    mark_constants (XEXP (link, 0));
350790075Sobrien}
350852284Sobrien
3509169689Skan/* Write all the constants in POOL.  */
351052284Sobrien
351150397Sobrienstatic void
3512169689Skanoutput_constant_pool_contents (struct rtx_constant_pool *pool)
351350397Sobrien{
3514169689Skan  struct constant_descriptor_rtx *desc;
351550397Sobrien
3516169689Skan  for (desc = pool->first; desc ; desc = desc->next)
3517169689Skan    if (desc->mark)
3518169689Skan      {
3519169689Skan	/* If the constant is part of an object_block, make sure that
3520169689Skan	   the constant has been positioned within its block, but do not
3521169689Skan	   write out its definition yet.  output_object_blocks will do
3522169689Skan	   that later.  */
3523169689Skan	if (SYMBOL_REF_HAS_BLOCK_INFO_P (desc->sym)
3524169689Skan	    && SYMBOL_REF_BLOCK (desc->sym))
3525169689Skan	  place_block_symbol (desc->sym);
3526169689Skan	else
3527169689Skan	  {
3528169689Skan	    switch_to_section (targetm.asm_out.select_rtx_section
3529169689Skan			       (desc->mode, desc->constant, desc->align));
3530169689Skan	    output_constant_pool_1 (desc, desc->align);
3531169689Skan	  }
3532169689Skan      }
3533169689Skan}
353450397Sobrien
3535169689Skan/* Mark all constants that are used in the current function, then write
3536169689Skan   out the function's private constant pool.  */
353750397Sobrien
3538169689Skanstatic void
3539169689Skanoutput_constant_pool (const char *fnname ATTRIBUTE_UNUSED,
3540169689Skan		      tree fndecl ATTRIBUTE_UNUSED)
3541169689Skan{
3542169689Skan  struct rtx_constant_pool *pool = cfun->varasm->pool;
354350397Sobrien
3544169689Skan  /* It is possible for gcc to call force_const_mem and then to later
3545169689Skan     discard the instructions which refer to the constant.  In such a
3546169689Skan     case we do not need to output the constant.  */
3547169689Skan  mark_constant_pool ();
354850397Sobrien
3549169689Skan#ifdef ASM_OUTPUT_POOL_PROLOGUE
3550169689Skan  ASM_OUTPUT_POOL_PROLOGUE (asm_out_file, fnname, fndecl, pool->offset);
3551169689Skan#endif
355250397Sobrien
3553169689Skan  output_constant_pool_contents (pool);
355450397Sobrien
3555169689Skan#ifdef ASM_OUTPUT_POOL_EPILOGUE
3556169689Skan  ASM_OUTPUT_POOL_EPILOGUE (asm_out_file, fnname, fndecl, pool->offset);
3557169689Skan#endif
355850397Sobrien}
3559169689Skan
3560169689Skan/* Write the contents of the shared constant pool.  */
356190075Sobrien
3562169689Skanvoid
3563169689Skanoutput_shared_constant_pool (void)
356490075Sobrien{
3565169689Skan  output_constant_pool_contents (shared_constant_pool);
356690075Sobrien}
356718334Speter
3568132718Skan/* Determine what kind of relocations EXP may need.  */
356918334Speter
3570132718Skanint
3571132718Skancompute_reloc_for_constant (tree exp)
357218334Speter{
3573117395Skan  int reloc = 0, reloc2;
357490075Sobrien  tree tem;
357518334Speter
357690075Sobrien  /* Give the front-end a chance to convert VALUE to something that
357790075Sobrien     looks more like a constant to the back-end.  */
3578169689Skan  exp = lang_hooks.expand_constant (exp);
357990075Sobrien
358018334Speter  switch (TREE_CODE (exp))
358118334Speter    {
358218334Speter    case ADDR_EXPR:
3583102780Skan    case FDESC_EXPR:
358490075Sobrien      /* Go inside any operations that get_inner_reference can handle and see
358590075Sobrien	 if what's inside is a constant: no need to do anything here for
358690075Sobrien	 addresses of variables or functions.  */
358790075Sobrien      for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
358890075Sobrien	   tem = TREE_OPERAND (tem, 0))
358990075Sobrien	;
359018334Speter
359190075Sobrien      if (TREE_PUBLIC (tem))
359290075Sobrien	reloc |= 2;
359390075Sobrien      else
359490075Sobrien	reloc |= 1;
359518334Speter      break;
359618334Speter
359718334Speter    case PLUS_EXPR:
3598132718Skan      reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3599132718Skan      reloc |= compute_reloc_for_constant (TREE_OPERAND (exp, 1));
360018334Speter      break;
360118334Speter
3602117395Skan    case MINUS_EXPR:
3603132718Skan      reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3604132718Skan      reloc2 = compute_reloc_for_constant (TREE_OPERAND (exp, 1));
3605117395Skan      /* The difference of two local labels is computable at link time.  */
3606117395Skan      if (reloc == 1 && reloc2 == 1)
3607117395Skan	reloc = 0;
3608117395Skan      else
3609117395Skan	reloc |= reloc2;
3610117395Skan      break;
3611117395Skan
361218334Speter    case NOP_EXPR:
361318334Speter    case CONVERT_EXPR:
361418334Speter    case NON_LVALUE_EXPR:
3615169689Skan    case VIEW_CONVERT_EXPR:
3616132718Skan      reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
361718334Speter      break;
361818334Speter
361918334Speter    case CONSTRUCTOR:
3620169689Skan      {
3621169689Skan	unsigned HOST_WIDE_INT idx;
3622169689Skan	FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
3623169689Skan	  if (tem != 0)
3624169689Skan	    reloc |= compute_reloc_for_constant (tem);
3625169689Skan      }
362618334Speter      break;
362718334Speter
362850397Sobrien    default:
362918334Speter      break;
363018334Speter    }
363118334Speter  return reloc;
363218334Speter}
3633132718Skan
3634132718Skan/* Find all the constants whose addresses are referenced inside of EXP,
3635132718Skan   and make sure assembler code with a label has been output for each one.
3636132718Skan   Indicate whether an ADDR_EXPR has been encountered.  */
3637132718Skan
3638132718Skanstatic void
3639132718Skanoutput_addressed_constants (tree exp)
3640132718Skan{
3641132718Skan  tree tem;
3642132718Skan
3643132718Skan  /* Give the front-end a chance to convert VALUE to something that
3644132718Skan     looks more like a constant to the back-end.  */
3645169689Skan  exp = lang_hooks.expand_constant (exp);
3646132718Skan
3647132718Skan  switch (TREE_CODE (exp))
3648132718Skan    {
3649132718Skan    case ADDR_EXPR:
3650132718Skan    case FDESC_EXPR:
3651132718Skan      /* Go inside any operations that get_inner_reference can handle and see
3652132718Skan	 if what's inside is a constant: no need to do anything here for
3653132718Skan	 addresses of variables or functions.  */
3654132718Skan      for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
3655132718Skan	   tem = TREE_OPERAND (tem, 0))
3656132718Skan	;
3657132718Skan
3658169689Skan      /* If we have an initialized CONST_DECL, retrieve the initializer.  */
3659169689Skan      if (TREE_CODE (tem) == CONST_DECL && DECL_INITIAL (tem))
3660169689Skan	tem = DECL_INITIAL (tem);
3661169689Skan
3662169689Skan      if (CONSTANT_CLASS_P (tem) || TREE_CODE (tem) == CONSTRUCTOR)
3663132718Skan	output_constant_def (tem, 0);
3664132718Skan      break;
3665132718Skan
3666132718Skan    case PLUS_EXPR:
3667132718Skan    case MINUS_EXPR:
3668132718Skan      output_addressed_constants (TREE_OPERAND (exp, 1));
3669132718Skan      /* Fall through.  */
3670132718Skan
3671132718Skan    case NOP_EXPR:
3672132718Skan    case CONVERT_EXPR:
3673132718Skan    case NON_LVALUE_EXPR:
3674169689Skan    case VIEW_CONVERT_EXPR:
3675132718Skan      output_addressed_constants (TREE_OPERAND (exp, 0));
3676132718Skan      break;
3677132718Skan
3678132718Skan    case CONSTRUCTOR:
3679169689Skan      {
3680169689Skan	unsigned HOST_WIDE_INT idx;
3681169689Skan	FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
3682169689Skan	  if (tem != 0)
3683169689Skan	    output_addressed_constants (tem);
3684169689Skan      }
3685132718Skan      break;
3686132718Skan
3687132718Skan    default:
3688132718Skan      break;
3689132718Skan    }
3690132718Skan}
369118334Speter
3692169689Skan/* Whether a constructor CTOR is a valid static constant initializer if all
3693169689Skan   its elements are.  This used to be internal to initializer_constant_valid_p
3694169689Skan   and has been exposed to let other functions like categorize_ctor_elements
3695169689Skan   evaluate the property while walking a constructor for other purposes.  */
3696169689Skan
3697169689Skanbool
3698169689Skanconstructor_static_from_elts_p (tree ctor)
3699169689Skan{
3700169689Skan  return (TREE_CONSTANT (ctor)
3701169689Skan	  && (TREE_CODE (TREE_TYPE (ctor)) == UNION_TYPE
3702169689Skan	      || TREE_CODE (TREE_TYPE (ctor)) == RECORD_TYPE)
3703169689Skan	  && !VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (ctor)));
3704169689Skan}
3705169689Skan
370690075Sobrien/* Return nonzero if VALUE is a valid constant-valued expression
370790075Sobrien   for use in initializing a static variable; one that can be an
370890075Sobrien   element of a "constant" initializer.
370990075Sobrien
371090075Sobrien   Return null_pointer_node if the value is absolute;
371190075Sobrien   if it is relocatable, return the variable that determines the relocation.
371290075Sobrien   We assume that VALUE has been folded as much as possible;
371390075Sobrien   therefore, we do not need to check for such things as
371490075Sobrien   arithmetic-combinations of integers.  */
371590075Sobrien
371690075Sobrientree
3717132718Skaninitializer_constant_valid_p (tree value, tree endtype)
371890075Sobrien{
371990075Sobrien  /* Give the front-end a chance to convert VALUE to something that
372090075Sobrien     looks more like a constant to the back-end.  */
3721169689Skan  value = lang_hooks.expand_constant (value);
372290075Sobrien
372390075Sobrien  switch (TREE_CODE (value))
372490075Sobrien    {
372590075Sobrien    case CONSTRUCTOR:
3726169689Skan      if (constructor_static_from_elts_p (value))
3727122180Skan	{
3728169689Skan	  unsigned HOST_WIDE_INT idx;
3729122180Skan	  tree elt;
3730122180Skan	  bool absolute = true;
373190075Sobrien
3732169689Skan	  FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
3733122180Skan	    {
3734122180Skan	      tree reloc;
3735169689Skan	      reloc = initializer_constant_valid_p (elt, TREE_TYPE (elt));
3736122180Skan	      if (!reloc)
3737122180Skan		return NULL_TREE;
3738122180Skan	      if (reloc != null_pointer_node)
3739122180Skan		absolute = false;
3740122180Skan	    }
3741122180Skan	  /* For a non-absolute relocation, there is no single
3742122180Skan	     variable that can be "the variable that determines the
3743122180Skan	     relocation."  */
3744122180Skan	  return absolute ? null_pointer_node : error_mark_node;
3745122180Skan	}
374690075Sobrien
3747122180Skan      return TREE_STATIC (value) ? null_pointer_node : NULL_TREE;
3748122180Skan
374990075Sobrien    case INTEGER_CST:
375096263Sobrien    case VECTOR_CST:
375190075Sobrien    case REAL_CST:
375290075Sobrien    case STRING_CST:
375390075Sobrien    case COMPLEX_CST:
375490075Sobrien      return null_pointer_node;
375590075Sobrien
375690075Sobrien    case ADDR_EXPR:
375790075Sobrien    case FDESC_EXPR:
3758169689Skan      value = staticp (TREE_OPERAND (value, 0));
3759169689Skan      if (value)
3760169689Skan	{
3761169689Skan	  /* "&(*a).f" is like unto pointer arithmetic.  If "a" turns out to
3762169689Skan	     be a constant, this is old-skool offsetof-like nonsense.  */
3763169689Skan	  if (TREE_CODE (value) == INDIRECT_REF
3764169689Skan	      && TREE_CONSTANT (TREE_OPERAND (value, 0)))
3765169689Skan	    return null_pointer_node;
3766169689Skan	  /* Taking the address of a nested function involves a trampoline.  */
3767169689Skan	  if (TREE_CODE (value) == FUNCTION_DECL
3768169689Skan	      && ((decl_function_context (value)
3769169689Skan		   && !DECL_NO_STATIC_CHAIN (value))
3770169689Skan		  || DECL_DLLIMPORT_P (value)))
3771169689Skan	    return NULL_TREE;
3772169689Skan	  /* "&{...}" requires a temporary to hold the constructed
3773169689Skan	     object.  */
3774169689Skan	  if (TREE_CODE (value) == CONSTRUCTOR)
3775169689Skan	    return NULL_TREE;
3776169689Skan	}
3777169689Skan      return value;
377890075Sobrien
377990075Sobrien    case VIEW_CONVERT_EXPR:
378090075Sobrien    case NON_LVALUE_EXPR:
378190075Sobrien      return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
378290075Sobrien
378390075Sobrien    case CONVERT_EXPR:
378490075Sobrien    case NOP_EXPR:
3785146895Skan      {
3786146895Skan	tree src;
3787146895Skan	tree src_type;
3788146895Skan	tree dest_type;
378990075Sobrien
3790146895Skan	src = TREE_OPERAND (value, 0);
3791146895Skan	src_type = TREE_TYPE (src);
3792146895Skan	dest_type = TREE_TYPE (value);
379390075Sobrien
3794146895Skan	/* Allow conversions between pointer types, floating-point
3795146895Skan	   types, and offset types.  */
3796146895Skan	if ((POINTER_TYPE_P (dest_type) && POINTER_TYPE_P (src_type))
3797146895Skan	    || (FLOAT_TYPE_P (dest_type) && FLOAT_TYPE_P (src_type))
3798146895Skan	    || (TREE_CODE (dest_type) == OFFSET_TYPE
3799146895Skan		&& TREE_CODE (src_type) == OFFSET_TYPE))
3800146895Skan	  return initializer_constant_valid_p (src, endtype);
380190075Sobrien
3802146895Skan	/* Allow length-preserving conversions between integer types.  */
3803146895Skan	if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type)
3804146895Skan	    && (TYPE_PRECISION (dest_type) == TYPE_PRECISION (src_type)))
3805146895Skan	  return initializer_constant_valid_p (src, endtype);
380690075Sobrien
3807146895Skan	/* Allow conversions between other integer types only if
3808146895Skan	   explicit value.  */
3809146895Skan	if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type))
3810146895Skan	  {
3811146895Skan	    tree inner = initializer_constant_valid_p (src, endtype);
3812146895Skan	    if (inner == null_pointer_node)
3813146895Skan	      return null_pointer_node;
3814146895Skan	    break;
3815146895Skan	  }
381690075Sobrien
3817146895Skan	/* Allow (int) &foo provided int is as wide as a pointer.  */
3818146895Skan	if (INTEGRAL_TYPE_P (dest_type) && POINTER_TYPE_P (src_type)
3819146895Skan	    && (TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type)))
3820146895Skan	  return initializer_constant_valid_p (src, endtype);
382190075Sobrien
3822146895Skan	/* Likewise conversions from int to pointers, but also allow
3823146895Skan	   conversions from 0.  */
3824146895Skan	if ((POINTER_TYPE_P (dest_type)
3825146895Skan	     || TREE_CODE (dest_type) == OFFSET_TYPE)
3826146895Skan	    && INTEGRAL_TYPE_P (src_type))
3827146895Skan	  {
3828169689Skan	    if (TREE_CODE (src) == INTEGER_CST
3829169689Skan		&& TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type))
3830169689Skan	      return null_pointer_node;
3831146895Skan	    if (integer_zerop (src))
3832146895Skan	      return null_pointer_node;
3833146895Skan	    else if (TYPE_PRECISION (dest_type) <= TYPE_PRECISION (src_type))
3834146895Skan	      return initializer_constant_valid_p (src, endtype);
3835146895Skan	  }
3836146895Skan
3837146895Skan	/* Allow conversions to struct or union types if the value
3838146895Skan	   inside is okay.  */
3839146895Skan	if (TREE_CODE (dest_type) == RECORD_TYPE
3840146895Skan	    || TREE_CODE (dest_type) == UNION_TYPE)
3841146895Skan	  return initializer_constant_valid_p (src, endtype);
3842146895Skan      }
384390075Sobrien      break;
384490075Sobrien
384590075Sobrien    case PLUS_EXPR:
384690075Sobrien      if (! INTEGRAL_TYPE_P (endtype)
384790075Sobrien	  || TYPE_PRECISION (endtype) >= POINTER_SIZE)
3848117395Skan	{
384990075Sobrien	  tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
385090075Sobrien						      endtype);
385190075Sobrien	  tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
385290075Sobrien						      endtype);
385390075Sobrien	  /* If either term is absolute, use the other terms relocation.  */
385490075Sobrien	  if (valid0 == null_pointer_node)
385590075Sobrien	    return valid1;
385690075Sobrien	  if (valid1 == null_pointer_node)
385790075Sobrien	    return valid0;
3858117395Skan	}
385990075Sobrien      break;
386090075Sobrien
386190075Sobrien    case MINUS_EXPR:
386290075Sobrien      if (! INTEGRAL_TYPE_P (endtype)
386390075Sobrien	  || TYPE_PRECISION (endtype) >= POINTER_SIZE)
386490075Sobrien	{
386590075Sobrien	  tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
386690075Sobrien						      endtype);
386790075Sobrien	  tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
386890075Sobrien						      endtype);
386990075Sobrien	  /* Win if second argument is absolute.  */
387090075Sobrien	  if (valid1 == null_pointer_node)
387190075Sobrien	    return valid0;
387290075Sobrien	  /* Win if both arguments have the same relocation.
387390075Sobrien	     Then the value is absolute.  */
387490075Sobrien	  if (valid0 == valid1 && valid0 != 0)
387590075Sobrien	    return null_pointer_node;
387690075Sobrien
387790075Sobrien	  /* Since GCC guarantees that string constants are unique in the
387890075Sobrien	     generated code, a subtraction between two copies of the same
387990075Sobrien	     constant string is absolute.  */
3880169689Skan	  if (valid0 && TREE_CODE (valid0) == STRING_CST
3881169689Skan	      && valid1 && TREE_CODE (valid1) == STRING_CST
3882169689Skan	      && operand_equal_p (valid0, valid1, 1))
388390075Sobrien	    return null_pointer_node;
388490075Sobrien	}
388590075Sobrien
3886169689Skan      /* Support narrowing differences.  */
388790075Sobrien      if (INTEGRAL_TYPE_P (endtype))
388890075Sobrien	{
388990075Sobrien	  tree op0, op1;
3890169689Skan
389190075Sobrien	  op0 = TREE_OPERAND (value, 0);
389290075Sobrien	  op1 = TREE_OPERAND (value, 1);
389390075Sobrien
389496263Sobrien	  /* Like STRIP_NOPS except allow the operand mode to widen.
3895132718Skan	     This works around a feature of fold that simplifies
389696263Sobrien	     (int)(p1 - p2) to ((int)p1 - (int)p2) under the theory
389796263Sobrien	     that the narrower operation is cheaper.  */
389896263Sobrien
389996263Sobrien	  while (TREE_CODE (op0) == NOP_EXPR
390096263Sobrien		 || TREE_CODE (op0) == CONVERT_EXPR
390196263Sobrien		 || TREE_CODE (op0) == NON_LVALUE_EXPR)
390296263Sobrien	    {
390396263Sobrien	      tree inner = TREE_OPERAND (op0, 0);
390496263Sobrien	      if (inner == error_mark_node
390596263Sobrien	          || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
390696263Sobrien		  || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))
390796263Sobrien		      > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
390896263Sobrien		break;
390996263Sobrien	      op0 = inner;
391096263Sobrien	    }
391196263Sobrien
391296263Sobrien	  while (TREE_CODE (op1) == NOP_EXPR
391396263Sobrien		 || TREE_CODE (op1) == CONVERT_EXPR
391496263Sobrien		 || TREE_CODE (op1) == NON_LVALUE_EXPR)
391596263Sobrien	    {
391696263Sobrien	      tree inner = TREE_OPERAND (op1, 0);
391796263Sobrien	      if (inner == error_mark_node
391896263Sobrien	          || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
391996263Sobrien		  || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op1)))
392096263Sobrien		      > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
392196263Sobrien		break;
392296263Sobrien	      op1 = inner;
392396263Sobrien	    }
392496263Sobrien
3925169689Skan	  op0 = initializer_constant_valid_p (op0, endtype);
3926169689Skan	  op1 = initializer_constant_valid_p (op1, endtype);
3927169689Skan
3928169689Skan	  /* Both initializers must be known.  */
3929169689Skan	  if (op0 && op1)
3930169689Skan	    {
3931169689Skan	      if (op0 == op1)
3932169689Skan		return null_pointer_node;
3933169689Skan
3934169689Skan	      /* Support differences between labels.  */
3935169689Skan	      if (TREE_CODE (op0) == LABEL_DECL
3936169689Skan		  && TREE_CODE (op1) == LABEL_DECL)
3937169689Skan		return null_pointer_node;
3938169689Skan
3939169689Skan	      if (TREE_CODE (op0) == STRING_CST
3940169689Skan		  && TREE_CODE (op1) == STRING_CST
3941169689Skan		  && operand_equal_p (op0, op1, 1))
3942169689Skan		return null_pointer_node;
3943169689Skan	    }
394490075Sobrien	}
394590075Sobrien      break;
394690075Sobrien
394790075Sobrien    default:
394890075Sobrien      break;
394990075Sobrien    }
395090075Sobrien
395190075Sobrien  return 0;
395290075Sobrien}
395390075Sobrien
395418334Speter/* Output assembler code for constant EXP to FILE, with no label.
395518334Speter   This includes the pseudo-op such as ".int" or ".byte", and a newline.
395618334Speter   Assumes output_addressed_constants has been done on EXP already.
395718334Speter
395818334Speter   Generate exactly SIZE bytes of assembler data, padding at the end
395918334Speter   with zeros if necessary.  SIZE must always be specified.
396018334Speter
396118334Speter   SIZE is important for structure constructors,
396218334Speter   since trailing members may have been omitted from the constructor.
396318334Speter   It is also important for initialization of arrays from string constants
396418334Speter   since the full length of the string constant might not be wanted.
396518334Speter   It is also needed for initialization of unions, where the initializer's
396618334Speter   type is just one member, and that may not be as long as the union.
396718334Speter
396818334Speter   There a case in which we would fail to output exactly SIZE bytes:
396918334Speter   for a structure constructor that wants to produce more than SIZE bytes.
397090075Sobrien   But such constructors will never be generated for any possible input.
397118334Speter
397290075Sobrien   ALIGN is the alignment of the data in bits.  */
397390075Sobrien
397418334Spetervoid
3975132718Skanoutput_constant (tree exp, unsigned HOST_WIDE_INT size, unsigned int align)
397618334Speter{
397790075Sobrien  enum tree_code code;
3978132718Skan  unsigned HOST_WIDE_INT thissize;
397918334Speter
3980132718Skan  /* Some front-ends use constants other than the standard language-independent
398190075Sobrien     varieties, but which may still be output directly.  Give the front-end a
398290075Sobrien     chance to convert EXP to a language-independent representation.  */
3983169689Skan  exp = lang_hooks.expand_constant (exp);
398452284Sobrien
398552284Sobrien  if (size == 0 || flag_syntax_only)
398618334Speter    return;
398718334Speter
3988169689Skan  /* See if we're trying to initialize a pointer in a non-default mode
3989169689Skan     to the address of some declaration somewhere.  If the target says
3990169689Skan     the mode is valid for pointers, assume the target has a way of
3991169689Skan     resolving it.  */
3992169689Skan  if (TREE_CODE (exp) == NOP_EXPR
3993169689Skan      && POINTER_TYPE_P (TREE_TYPE (exp))
3994169689Skan      && targetm.valid_pointer_mode (TYPE_MODE (TREE_TYPE (exp))))
3995169689Skan    {
3996169689Skan      tree saved_type = TREE_TYPE (exp);
3997169689Skan
3998169689Skan      /* Peel off any intermediate conversions-to-pointer for valid
3999169689Skan	 pointer modes.  */
4000169689Skan      while (TREE_CODE (exp) == NOP_EXPR
4001169689Skan	     && POINTER_TYPE_P (TREE_TYPE (exp))
4002169689Skan	     && targetm.valid_pointer_mode (TYPE_MODE (TREE_TYPE (exp))))
4003169689Skan	exp = TREE_OPERAND (exp, 0);
4004169689Skan
4005169689Skan      /* If what we're left with is the address of something, we can
4006169689Skan	 convert the address to the final type and output it that
4007169689Skan	 way.  */
4008169689Skan      if (TREE_CODE (exp) == ADDR_EXPR)
4009169689Skan	exp = build1 (ADDR_EXPR, saved_type, TREE_OPERAND (exp, 0));
4010169689Skan      /* Likewise for constant ints.  */
4011169689Skan      else if (TREE_CODE (exp) == INTEGER_CST)
4012169689Skan	exp = build_int_cst_wide (saved_type, TREE_INT_CST_LOW (exp),
4013169689Skan				  TREE_INT_CST_HIGH (exp));
4014169689Skan
4015169689Skan    }
4016169689Skan
401790075Sobrien  /* Eliminate any conversions since we'll be outputting the underlying
401890075Sobrien     constant.  */
401990075Sobrien  while (TREE_CODE (exp) == NOP_EXPR || TREE_CODE (exp) == CONVERT_EXPR
402090075Sobrien	 || TREE_CODE (exp) == NON_LVALUE_EXPR
402190075Sobrien	 || TREE_CODE (exp) == VIEW_CONVERT_EXPR)
4022169689Skan    {
4023169689Skan      HOST_WIDE_INT type_size = int_size_in_bytes (TREE_TYPE (exp));
4024169689Skan      HOST_WIDE_INT op_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (exp, 0)));
402518334Speter
4026169689Skan      /* Make sure eliminating the conversion is really a no-op, except with
4027169689Skan	 VIEW_CONVERT_EXPRs to allow for wild Ada unchecked conversions and
4028169689Skan	 union types to allow for Ada unchecked unions.  */
4029169689Skan      if (type_size > op_size
4030169689Skan	  && TREE_CODE (exp) != VIEW_CONVERT_EXPR
4031169689Skan	  && TREE_CODE (TREE_TYPE (exp)) != UNION_TYPE)
4032169689Skan	/* Keep the conversion. */
4033169689Skan	break;
4034169689Skan      else
4035169689Skan	exp = TREE_OPERAND (exp, 0);
4036169689Skan    }
4037169689Skan
403890075Sobrien  code = TREE_CODE (TREE_TYPE (exp));
403990075Sobrien  thissize = int_size_in_bytes (TREE_TYPE (exp));
404090075Sobrien
4041169689Skan  /* Give the front end another chance to expand constants.  */
4042169689Skan  exp = lang_hooks.expand_constant (exp);
4043169689Skan
404418334Speter  /* Allow a constructor with no elements for any data type.
404518334Speter     This means to fill the space with zeros.  */
4046169689Skan  if (TREE_CODE (exp) == CONSTRUCTOR
4047169689Skan      && VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (exp)))
404818334Speter    {
404950397Sobrien      assemble_zeros (size);
405018334Speter      return;
405118334Speter    }
405218334Speter
405390075Sobrien  if (TREE_CODE (exp) == FDESC_EXPR)
405490075Sobrien    {
405590075Sobrien#ifdef ASM_OUTPUT_FDESC
405690075Sobrien      HOST_WIDE_INT part = tree_low_cst (TREE_OPERAND (exp, 1), 0);
405790075Sobrien      tree decl = TREE_OPERAND (exp, 0);
405890075Sobrien      ASM_OUTPUT_FDESC (asm_out_file, decl, part);
405990075Sobrien#else
4060169689Skan      gcc_unreachable ();
406190075Sobrien#endif
406290075Sobrien      return;
406390075Sobrien    }
406490075Sobrien
406590075Sobrien  /* Now output the underlying data.  If we've handling the padding, return.
4066169689Skan     Otherwise, break and ensure SIZE is the size written.  */
406718334Speter  switch (code)
406818334Speter    {
406918334Speter    case BOOLEAN_TYPE:
407018334Speter    case INTEGER_TYPE:
407118334Speter    case ENUMERAL_TYPE:
407218334Speter    case POINTER_TYPE:
407318334Speter    case REFERENCE_TYPE:
4074260311Spfg    /* APPLE LOCAL radar 5822844 */
4075260311Spfg    case BLOCK_POINTER_TYPE:
4076132718Skan    case OFFSET_TYPE:
407718334Speter      if (! assemble_integer (expand_expr (exp, NULL_RTX, VOIDmode,
407818334Speter					   EXPAND_INITIALIZER),
4079169689Skan			      MIN (size, thissize), align, 0))
408018334Speter	error ("initializer for integer value is too complicated");
408118334Speter      break;
408218334Speter
408318334Speter    case REAL_TYPE:
408418334Speter      if (TREE_CODE (exp) != REAL_CST)
408518334Speter	error ("initializer for floating value is not a floating constant");
408618334Speter
4087132718Skan      assemble_real (TREE_REAL_CST (exp), TYPE_MODE (TREE_TYPE (exp)), align);
408818334Speter      break;
408918334Speter
409018334Speter    case COMPLEX_TYPE:
409190075Sobrien      output_constant (TREE_REALPART (exp), thissize / 2, align);
409290075Sobrien      output_constant (TREE_IMAGPART (exp), thissize / 2,
409390075Sobrien		       min_align (align, BITS_PER_UNIT * (thissize / 2)));
409418334Speter      break;
409518334Speter
409618334Speter    case ARRAY_TYPE:
409796263Sobrien    case VECTOR_TYPE:
4098169689Skan      switch (TREE_CODE (exp))
409918334Speter	{
4100169689Skan	case CONSTRUCTOR:
410190075Sobrien	  output_constructor (exp, size, align);
410218334Speter	  return;
4103169689Skan	case STRING_CST:
4104132718Skan	  thissize = MIN ((unsigned HOST_WIDE_INT)TREE_STRING_LENGTH (exp),
4105132718Skan			  size);
410690075Sobrien	  assemble_string (TREE_STRING_POINTER (exp), thissize);
4107169689Skan	  break;
4108119256Skan
4109169689Skan	case VECTOR_CST:
4110169689Skan	  {
4111169689Skan	    int elt_size;
4112169689Skan	    tree link;
4113169689Skan	    unsigned int nalign;
4114169689Skan	    enum machine_mode inner;
4115119256Skan
4116169689Skan	    inner = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
4117169689Skan	    nalign = MIN (align, GET_MODE_ALIGNMENT (inner));
4118119256Skan
4119169689Skan	    elt_size = GET_MODE_SIZE (inner);
4120169689Skan
4121169689Skan	    link = TREE_VECTOR_CST_ELTS (exp);
4122169689Skan	    output_constant (TREE_VALUE (link), elt_size, align);
4123169689Skan	    thissize = elt_size;
4124169689Skan	    while ((link = TREE_CHAIN (link)) != NULL)
4125169689Skan	      {
4126169689Skan		output_constant (TREE_VALUE (link), elt_size, nalign);
4127169689Skan		thissize += elt_size;
4128169689Skan	      }
4129169689Skan	    break;
4130169689Skan	  }
4131169689Skan	default:
4132169689Skan	  gcc_unreachable ();
4133119256Skan	}
413418334Speter      break;
413518334Speter
413618334Speter    case RECORD_TYPE:
413718334Speter    case UNION_TYPE:
4138169689Skan      gcc_assert (TREE_CODE (exp) == CONSTRUCTOR);
4139169689Skan      output_constructor (exp, size, align);
414018334Speter      return;
414118334Speter
414290075Sobrien    case ERROR_MARK:
414390075Sobrien      return;
414490075Sobrien
414550397Sobrien    default:
4146169689Skan      gcc_unreachable ();
414718334Speter    }
414818334Speter
4149132718Skan  if (size > thissize)
4150132718Skan    assemble_zeros (size - thissize);
415118334Speter}
415218334Speter
415318334Speter
415490075Sobrien/* Subroutine of output_constructor, used for computing the size of
415590075Sobrien   arrays of unspecified length.  VAL must be a CONSTRUCTOR of an array
415690075Sobrien   type with an unspecified upper bound.  */
415790075Sobrien
415890075Sobrienstatic unsigned HOST_WIDE_INT
4159132718Skanarray_size_for_constructor (tree val)
416090075Sobrien{
416190075Sobrien  tree max_index, i;
4162169689Skan  unsigned HOST_WIDE_INT cnt;
4163169689Skan  tree index, value, tmp;
416490075Sobrien
416590075Sobrien  /* This code used to attempt to handle string constants that are not
416690075Sobrien     arrays of single-bytes, but nothing else does, so there's no point in
416790075Sobrien     doing it here.  */
416890075Sobrien  if (TREE_CODE (val) == STRING_CST)
416990075Sobrien    return TREE_STRING_LENGTH (val);
417090075Sobrien
417190075Sobrien  max_index = NULL_TREE;
4172169689Skan  FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (val), cnt, index, value)
417390075Sobrien    {
417490075Sobrien      if (TREE_CODE (index) == RANGE_EXPR)
417590075Sobrien	index = TREE_OPERAND (index, 1);
417690075Sobrien      if (max_index == NULL_TREE || tree_int_cst_lt (max_index, index))
417790075Sobrien	max_index = index;
417890075Sobrien    }
417990075Sobrien
418090075Sobrien  if (max_index == NULL_TREE)
418190075Sobrien    return 0;
418290075Sobrien
418390075Sobrien  /* Compute the total number of array elements.  */
4184169689Skan  tmp = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (val)));
4185169689Skan  i = size_binop (MINUS_EXPR, fold_convert (sizetype, max_index),
4186169689Skan		  fold_convert (sizetype, tmp));
4187169689Skan  i = size_binop (PLUS_EXPR, i, build_int_cst (sizetype, 1));
418890075Sobrien
418990075Sobrien  /* Multiply by the array element unit size to find number of bytes.  */
419090075Sobrien  i = size_binop (MULT_EXPR, i, TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (val))));
419190075Sobrien
419290075Sobrien  return tree_low_cst (i, 1);
419390075Sobrien}
419490075Sobrien
419590075Sobrien/* Subroutine of output_constant, used for CONSTRUCTORs (aggregate constants).
419618334Speter   Generate at least SIZE bytes, padding if necessary.  */
419718334Speter
419818334Speterstatic void
4199132718Skanoutput_constructor (tree exp, unsigned HOST_WIDE_INT size,
4200132718Skan		    unsigned int align)
420118334Speter{
420290075Sobrien  tree type = TREE_TYPE (exp);
4203169689Skan  tree field = 0;
420490075Sobrien  tree min_index = 0;
420518334Speter  /* Number of bytes output or skipped so far.
420618334Speter     In other words, current position within the constructor.  */
420790075Sobrien  HOST_WIDE_INT total_bytes = 0;
4208132718Skan  /* Nonzero means BYTE contains part of a byte, to be output.  */
420918334Speter  int byte_buffer_in_use = 0;
421090075Sobrien  int byte = 0;
4211169689Skan  unsigned HOST_WIDE_INT cnt;
4212169689Skan  constructor_elt *ce;
421318334Speter
4214169689Skan  gcc_assert (HOST_BITS_PER_WIDE_INT >= BITS_PER_UNIT);
421518334Speter
421690075Sobrien  if (TREE_CODE (type) == RECORD_TYPE)
421790075Sobrien    field = TYPE_FIELDS (type);
421818334Speter
421990075Sobrien  if (TREE_CODE (type) == ARRAY_TYPE
422090075Sobrien      && TYPE_DOMAIN (type) != 0)
422190075Sobrien    min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
422218334Speter
422318334Speter  /* As LINK goes through the elements of the constant,
422418334Speter     FIELD goes through the structure fields, if the constant is a structure.
422518334Speter     if the constant is a union, then we override this,
422618334Speter     by getting the field from the TREE_LIST element.
422752284Sobrien     But the constant could also be an array.  Then FIELD is zero.
422852284Sobrien
422952284Sobrien     There is always a maximum of one element in the chain LINK for unions
423052284Sobrien     (even if the initializer in a source program incorrectly contains
423190075Sobrien     more one).  */
4232169689Skan  for (cnt = 0;
4233169689Skan       VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (exp), cnt, ce);
4234169689Skan       cnt++, field = field ? TREE_CHAIN (field) : 0)
423518334Speter    {
4236169689Skan      tree val = ce->value;
423718334Speter      tree index = 0;
423818334Speter
423990075Sobrien      /* The element in a union constructor specifies the proper field
424090075Sobrien	 or index.  */
424190075Sobrien      if ((TREE_CODE (type) == RECORD_TYPE || TREE_CODE (type) == UNION_TYPE
424290075Sobrien	   || TREE_CODE (type) == QUAL_UNION_TYPE)
4243169689Skan	  && ce->index != 0)
4244169689Skan	field = ce->index;
424518334Speter
424690075Sobrien      else if (TREE_CODE (type) == ARRAY_TYPE)
4247169689Skan	index = ce->index;
424818334Speter
4249132718Skan#ifdef ASM_COMMENT_START
4250132718Skan      if (field && flag_verbose_asm)
4251132718Skan	fprintf (asm_out_file, "%s %s:\n",
4252169689Skan		 ASM_COMMENT_START,
4253169689Skan		 DECL_NAME (field)
4254132718Skan		 ? IDENTIFIER_POINTER (DECL_NAME (field))
4255132718Skan		 : "<anonymous>");
4256132718Skan#endif
4257132718Skan
425818334Speter      /* Eliminate the marker that makes a cast not be an lvalue.  */
425918334Speter      if (val != 0)
426018334Speter	STRIP_NOPS (val);
426118334Speter
426250397Sobrien      if (index && TREE_CODE (index) == RANGE_EXPR)
426318334Speter	{
426490075Sobrien	  unsigned HOST_WIDE_INT fieldsize
426590075Sobrien	    = int_size_in_bytes (TREE_TYPE (type));
426690075Sobrien	  HOST_WIDE_INT lo_index = tree_low_cst (TREE_OPERAND (index, 0), 0);
426790075Sobrien	  HOST_WIDE_INT hi_index = tree_low_cst (TREE_OPERAND (index, 1), 0);
426850397Sobrien	  HOST_WIDE_INT index;
426990075Sobrien	  unsigned int align2 = min_align (align, fieldsize * BITS_PER_UNIT);
427090075Sobrien
427150397Sobrien	  for (index = lo_index; index <= hi_index; index++)
427250397Sobrien	    {
427350397Sobrien	      /* Output the element's initial value.  */
427450397Sobrien	      if (val == 0)
427550397Sobrien		assemble_zeros (fieldsize);
427650397Sobrien	      else
427790075Sobrien		output_constant (val, fieldsize, align2);
427850397Sobrien
427950397Sobrien	      /* Count its size.  */
428050397Sobrien	      total_bytes += fieldsize;
428150397Sobrien	    }
428250397Sobrien	}
428350397Sobrien      else if (field == 0 || !DECL_BIT_FIELD (field))
428450397Sobrien	{
428518334Speter	  /* An element that is not a bit-field.  */
428618334Speter
428790075Sobrien	  unsigned HOST_WIDE_INT fieldsize;
428818334Speter	  /* Since this structure is static,
428918334Speter	     we know the positions are constant.  */
429090075Sobrien	  HOST_WIDE_INT pos = field ? int_byte_position (field) : 0;
429190075Sobrien	  unsigned int align2;
429290075Sobrien
429318334Speter	  if (index != 0)
429490075Sobrien	    pos = (tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (val)), 1)
429590075Sobrien		   * (tree_low_cst (index, 0) - tree_low_cst (min_index, 0)));
429618334Speter
429718334Speter	  /* Output any buffered-up bit-fields preceding this element.  */
429818334Speter	  if (byte_buffer_in_use)
429918334Speter	    {
430090075Sobrien	      assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
430118334Speter	      total_bytes++;
430218334Speter	      byte_buffer_in_use = 0;
430318334Speter	    }
430418334Speter
430518334Speter	  /* Advance to offset of this element.
430618334Speter	     Note no alignment needed in an array, since that is guaranteed
430718334Speter	     if each element has the proper size.  */
430890075Sobrien	  if ((field != 0 || index != 0) && pos != total_bytes)
430918334Speter	    {
4310169689Skan	      gcc_assert (pos >= total_bytes);
431190075Sobrien	      assemble_zeros (pos - total_bytes);
431290075Sobrien	      total_bytes = pos;
431318334Speter	    }
431418334Speter
431590075Sobrien	  /* Find the alignment of this element.  */
431690075Sobrien	  align2 = min_align (align, BITS_PER_UNIT * pos);
431790075Sobrien
431818334Speter	  /* Determine size this element should occupy.  */
431918334Speter	  if (field)
432018334Speter	    {
432190075Sobrien	      fieldsize = 0;
432290075Sobrien
432390075Sobrien	      /* If this is an array with an unspecified upper bound,
432490075Sobrien		 the initializer determines the size.  */
432590075Sobrien	      /* ??? This ought to only checked if DECL_SIZE_UNIT is NULL,
432690075Sobrien		 but we cannot do this until the deprecated support for
432790075Sobrien		 initializing zero-length array members is removed.  */
432890075Sobrien	      if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
432990075Sobrien		  && TYPE_DOMAIN (TREE_TYPE (field))
433090075Sobrien		  && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
433118334Speter		{
433290075Sobrien		  fieldsize = array_size_for_constructor (val);
433390075Sobrien		  /* Given a non-empty initialization, this field had
433490075Sobrien		     better be last.  */
4335169689Skan		  gcc_assert (!fieldsize || !TREE_CHAIN (field));
433618334Speter		}
433790075Sobrien	      else if (DECL_SIZE_UNIT (field))
433818334Speter		{
433990075Sobrien		  /* ??? This can't be right.  If the decl size overflows
434090075Sobrien		     a host integer we will silently emit no data.  */
434190075Sobrien		  if (host_integerp (DECL_SIZE_UNIT (field), 1))
434290075Sobrien		    fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 1);
434318334Speter		}
434418334Speter	    }
434518334Speter	  else
434690075Sobrien	    fieldsize = int_size_in_bytes (TREE_TYPE (type));
434718334Speter
434818334Speter	  /* Output the element's initial value.  */
434918334Speter	  if (val == 0)
435018334Speter	    assemble_zeros (fieldsize);
435118334Speter	  else
435290075Sobrien	    output_constant (val, fieldsize, align2);
435318334Speter
435418334Speter	  /* Count its size.  */
435518334Speter	  total_bytes += fieldsize;
435618334Speter	}
435718334Speter      else if (val != 0 && TREE_CODE (val) != INTEGER_CST)
4358169689Skan	error ("invalid initial value for member %qs",
435918334Speter	       IDENTIFIER_POINTER (DECL_NAME (field)));
436018334Speter      else
436118334Speter	{
436218334Speter	  /* Element that is a bit-field.  */
436318334Speter
436490075Sobrien	  HOST_WIDE_INT next_offset = int_bit_position (field);
436590075Sobrien	  HOST_WIDE_INT end_offset
436690075Sobrien	    = (next_offset + tree_low_cst (DECL_SIZE (field), 1));
436718334Speter
436818334Speter	  if (val == 0)
436918334Speter	    val = integer_zero_node;
437018334Speter
437118334Speter	  /* If this field does not start in this (or, next) byte,
437218334Speter	     skip some bytes.  */
437318334Speter	  if (next_offset / BITS_PER_UNIT != total_bytes)
437418334Speter	    {
437518334Speter	      /* Output remnant of any bit field in previous bytes.  */
437618334Speter	      if (byte_buffer_in_use)
437718334Speter		{
437890075Sobrien		  assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
437918334Speter		  total_bytes++;
438018334Speter		  byte_buffer_in_use = 0;
438118334Speter		}
438218334Speter
438318334Speter	      /* If still not at proper byte, advance to there.  */
438418334Speter	      if (next_offset / BITS_PER_UNIT != total_bytes)
438518334Speter		{
4386169689Skan		  gcc_assert (next_offset / BITS_PER_UNIT >= total_bytes);
438718334Speter		  assemble_zeros (next_offset / BITS_PER_UNIT - total_bytes);
438818334Speter		  total_bytes = next_offset / BITS_PER_UNIT;
438918334Speter		}
439018334Speter	    }
439118334Speter
439218334Speter	  if (! byte_buffer_in_use)
439318334Speter	    byte = 0;
439418334Speter
439518334Speter	  /* We must split the element into pieces that fall within
439618334Speter	     separate bytes, and combine each byte with previous or
439718334Speter	     following bit-fields.  */
439818334Speter
439918334Speter	  /* next_offset is the offset n fbits from the beginning of
440018334Speter	     the structure to the next bit of this element to be processed.
440118334Speter	     end_offset is the offset of the first bit past the end of
440218334Speter	     this element.  */
440318334Speter	  while (next_offset < end_offset)
440418334Speter	    {
440518334Speter	      int this_time;
440618334Speter	      int shift;
440718334Speter	      HOST_WIDE_INT value;
440890075Sobrien	      HOST_WIDE_INT next_byte = next_offset / BITS_PER_UNIT;
440990075Sobrien	      HOST_WIDE_INT next_bit = next_offset % BITS_PER_UNIT;
441018334Speter
441118334Speter	      /* Advance from byte to byte
441218334Speter		 within this element when necessary.  */
441318334Speter	      while (next_byte != total_bytes)
441418334Speter		{
441590075Sobrien		  assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
441618334Speter		  total_bytes++;
441718334Speter		  byte = 0;
441818334Speter		}
441918334Speter
442018334Speter	      /* Number of bits we can process at once
442118334Speter		 (all part of the same byte).  */
442218334Speter	      this_time = MIN (end_offset - next_offset,
442318334Speter			       BITS_PER_UNIT - next_bit);
442418334Speter	      if (BYTES_BIG_ENDIAN)
442518334Speter		{
442618334Speter		  /* On big-endian machine, take the most significant bits
442718334Speter		     first (of the bits that are significant)
442818334Speter		     and put them into bytes from the most significant end.  */
442918334Speter		  shift = end_offset - next_offset - this_time;
443090075Sobrien
443118334Speter		  /* Don't try to take a bunch of bits that cross
443290075Sobrien		     the word boundary in the INTEGER_CST. We can
443390075Sobrien		     only select bits from the LOW or HIGH part
443490075Sobrien		     not from both.  */
443518334Speter		  if (shift < HOST_BITS_PER_WIDE_INT
443618334Speter		      && shift + this_time > HOST_BITS_PER_WIDE_INT)
443718334Speter		    {
443890075Sobrien		      this_time = shift + this_time - HOST_BITS_PER_WIDE_INT;
443918334Speter		      shift = HOST_BITS_PER_WIDE_INT;
444018334Speter		    }
444118334Speter
444218334Speter		  /* Now get the bits from the appropriate constant word.  */
444318334Speter		  if (shift < HOST_BITS_PER_WIDE_INT)
444490075Sobrien		    value = TREE_INT_CST_LOW (val);
4445169689Skan		  else
444618334Speter		    {
4447169689Skan		      gcc_assert (shift < 2 * HOST_BITS_PER_WIDE_INT);
444818334Speter		      value = TREE_INT_CST_HIGH (val);
444918334Speter		      shift -= HOST_BITS_PER_WIDE_INT;
445018334Speter		    }
445190075Sobrien
445290075Sobrien		  /* Get the result. This works only when:
445390075Sobrien		     1 <= this_time <= HOST_BITS_PER_WIDE_INT.  */
445418334Speter		  byte |= (((value >> shift)
445590075Sobrien			    & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
445618334Speter			   << (BITS_PER_UNIT - this_time - next_bit));
445718334Speter		}
445818334Speter	      else
445918334Speter		{
446018334Speter		  /* On little-endian machines,
446118334Speter		     take first the least significant bits of the value
446218334Speter		     and pack them starting at the least significant
446318334Speter		     bits of the bytes.  */
446490075Sobrien		  shift = next_offset - int_bit_position (field);
446590075Sobrien
446618334Speter		  /* Don't try to take a bunch of bits that cross
446790075Sobrien		     the word boundary in the INTEGER_CST. We can
446890075Sobrien		     only select bits from the LOW or HIGH part
446990075Sobrien		     not from both.  */
447018334Speter		  if (shift < HOST_BITS_PER_WIDE_INT
447118334Speter		      && shift + this_time > HOST_BITS_PER_WIDE_INT)
447290075Sobrien		    this_time = (HOST_BITS_PER_WIDE_INT - shift);
447318334Speter
447418334Speter		  /* Now get the bits from the appropriate constant word.  */
447550397Sobrien		  if (shift < HOST_BITS_PER_WIDE_INT)
447618334Speter		    value = TREE_INT_CST_LOW (val);
4477169689Skan		  else
447818334Speter		    {
4479169689Skan		      gcc_assert (shift < 2 * HOST_BITS_PER_WIDE_INT);
448018334Speter		      value = TREE_INT_CST_HIGH (val);
448118334Speter		      shift -= HOST_BITS_PER_WIDE_INT;
448218334Speter		    }
448390075Sobrien
448490075Sobrien		  /* Get the result. This works only when:
448590075Sobrien		     1 <= this_time <= HOST_BITS_PER_WIDE_INT.  */
448618334Speter		  byte |= (((value >> shift)
448790075Sobrien			    & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
448818334Speter			   << next_bit);
448918334Speter		}
449090075Sobrien
449118334Speter	      next_offset += this_time;
449218334Speter	      byte_buffer_in_use = 1;
449318334Speter	    }
449418334Speter	}
449518334Speter    }
449690075Sobrien
449718334Speter  if (byte_buffer_in_use)
449818334Speter    {
449990075Sobrien      assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
450018334Speter      total_bytes++;
450118334Speter    }
450290075Sobrien
4503132718Skan  if ((unsigned HOST_WIDE_INT)total_bytes < size)
450418334Speter    assemble_zeros (size - total_bytes);
450518334Speter}
450618334Speter
450796263Sobrien/* This TREE_LIST contains any weak symbol declarations waiting
450896263Sobrien   to be emitted.  */
4509117395Skanstatic GTY(()) tree weak_decls;
451090075Sobrien
451196263Sobrien/* Mark DECL as weak.  */
451296263Sobrien
451396263Sobrienstatic void
4514132718Skanmark_weak (tree decl)
451590075Sobrien{
451696263Sobrien  DECL_WEAK (decl) = 1;
451790075Sobrien
451896263Sobrien  if (DECL_RTL_SET_P (decl)
4519169689Skan      && MEM_P (DECL_RTL (decl))
452096263Sobrien      && XEXP (DECL_RTL (decl), 0)
452196263Sobrien      && GET_CODE (XEXP (DECL_RTL (decl), 0)) == SYMBOL_REF)
452296263Sobrien    SYMBOL_REF_WEAK (XEXP (DECL_RTL (decl), 0)) = 1;
452396263Sobrien}
4524117395Skan
452596263Sobrien/* Merge weak status between NEWDECL and OLDDECL.  */
452690075Sobrien
452796263Sobrienvoid
4528132718Skanmerge_weak (tree newdecl, tree olddecl)
452918334Speter{
453096263Sobrien  if (DECL_WEAK (newdecl) == DECL_WEAK (olddecl))
4531169689Skan    {
4532169689Skan      if (DECL_WEAK (newdecl) && SUPPORTS_WEAK)
4533169689Skan        {
4534169689Skan          tree *pwd;
4535169689Skan          /* We put the NEWDECL on the weak_decls list at some point
4536169689Skan             and OLDDECL as well.  Keep just OLDDECL on the list.  */
4537169689Skan	  for (pwd = &weak_decls; *pwd; pwd = &TREE_CHAIN (*pwd))
4538169689Skan	    if (TREE_VALUE (*pwd) == newdecl)
4539169689Skan	      {
4540169689Skan	        *pwd = TREE_CHAIN (*pwd);
4541169689Skan		break;
4542169689Skan	      }
4543169689Skan        }
4544169689Skan      return;
4545169689Skan    }
454618334Speter
454796263Sobrien  if (DECL_WEAK (newdecl))
454896263Sobrien    {
454996263Sobrien      tree wd;
4550117395Skan
455196263Sobrien      /* NEWDECL is weak, but OLDDECL is not.  */
455218334Speter
455396263Sobrien      /* If we already output the OLDDECL, we're in trouble; we can't
455496263Sobrien	 go back and make it weak.  This error cannot caught in
455596263Sobrien	 declare_weak because the NEWDECL and OLDDECL was not yet
455696263Sobrien	 been merged; therefore, TREE_ASM_WRITTEN was not set.  */
4557102780Skan      if (TREE_ASM_WRITTEN (olddecl))
4558169689Skan	error ("weak declaration of %q+D must precede definition",
4559169689Skan	       newdecl);
4560102780Skan
4561102780Skan      /* If we've already generated rtl referencing OLDDECL, we may
4562102780Skan	 have done so in a way that will not function properly with
4563102780Skan	 a weak symbol.  */
4564102780Skan      else if (TREE_USED (olddecl)
4565102780Skan	       && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (olddecl)))
4566169689Skan	warning (0, "weak declaration of %q+D after first use results "
4567169689Skan                 "in unspecified behavior", newdecl);
4568102780Skan
456996263Sobrien      if (SUPPORTS_WEAK)
457096263Sobrien	{
457196263Sobrien	  /* We put the NEWDECL on the weak_decls list at some point.
457296263Sobrien	     Replace it with the OLDDECL.  */
457396263Sobrien	  for (wd = weak_decls; wd; wd = TREE_CHAIN (wd))
457496263Sobrien	    if (TREE_VALUE (wd) == newdecl)
457596263Sobrien	      {
457696263Sobrien		TREE_VALUE (wd) = olddecl;
457796263Sobrien		break;
457896263Sobrien	      }
457996263Sobrien	  /* We may not find the entry on the list.  If NEWDECL is a
458096263Sobrien	     weak alias, then we will have already called
458196263Sobrien	     globalize_decl to remove the entry; in that case, we do
458296263Sobrien	     not need to do anything.  */
458396263Sobrien	}
458452284Sobrien
458596263Sobrien      /* Make the OLDDECL weak; it's OLDDECL that we'll be keeping.  */
458696263Sobrien      mark_weak (olddecl);
458796263Sobrien    }
458896263Sobrien  else
458996263Sobrien    /* OLDDECL was weak, but NEWDECL was not explicitly marked as
459096263Sobrien       weak.  Just update NEWDECL to indicate that it's weak too.  */
459196263Sobrien    mark_weak (newdecl);
459252284Sobrien}
459318334Speter
459418334Speter/* Declare DECL to be a weak symbol.  */
459518334Speter
459618334Spetervoid
4597132718Skandeclare_weak (tree decl)
459818334Speter{
459918334Speter  if (! TREE_PUBLIC (decl))
4600169689Skan    error ("weak declaration of %q+D must be public", decl);
460196263Sobrien  else if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
4602169689Skan    error ("weak declaration of %q+D must precede definition", decl);
460318334Speter  else if (SUPPORTS_WEAK)
460496263Sobrien    {
460596263Sobrien      if (! DECL_WEAK (decl))
460696263Sobrien	weak_decls = tree_cons (NULL, decl, weak_decls);
460796263Sobrien    }
460890075Sobrien  else
4609169689Skan    warning (0, "weak declaration of %q+D not supported", decl);
461090075Sobrien
461196263Sobrien  mark_weak (decl);
461218334Speter}
461318334Speter
4614169689Skanstatic void
4615169689Skanweak_finish_1 (tree decl)
461618334Speter{
4617132718Skan#if defined (ASM_WEAKEN_DECL) || defined (ASM_WEAKEN_LABEL)
4618169689Skan  const char *const name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
4619132718Skan#endif
462096263Sobrien
4621169689Skan  if (! TREE_USED (decl))
4622169689Skan    return;
462396263Sobrien
462496263Sobrien#ifdef ASM_WEAKEN_DECL
4625169689Skan  ASM_WEAKEN_DECL (asm_out_file, decl, name, NULL);
462690075Sobrien#else
462790075Sobrien#ifdef ASM_WEAKEN_LABEL
4628169689Skan  ASM_WEAKEN_LABEL (asm_out_file, name);
462996263Sobrien#else
463096263Sobrien#ifdef ASM_OUTPUT_WEAK_ALIAS
4631169689Skan  {
4632169689Skan    static bool warn_once = 0;
4633169689Skan    if (! warn_once)
4634169689Skan      {
4635169689Skan	warning (0, "only weak aliases are supported in this configuration");
4636169689Skan	warn_once = 1;
4637169689Skan      }
4638169689Skan    return;
4639169689Skan  }
464090075Sobrien#endif
464190075Sobrien#endif
464296263Sobrien#endif
4643169689Skan}
4644169689Skan
4645169689Skan/* This TREE_LIST contains weakref targets.  */
4646169689Skan
4647169689Skanstatic GTY(()) tree weakref_targets;
4648169689Skan
4649169689Skan/* Forward declaration.  */
4650169689Skanstatic tree find_decl_and_mark_needed (tree decl, tree target);
4651169689Skan
4652169689Skan/* Emit any pending weak declarations.  */
4653169689Skan
4654169689Skanvoid
4655169689Skanweak_finish (void)
4656169689Skan{
4657169689Skan  tree t;
4658169689Skan
4659169689Skan  for (t = weakref_targets; t; t = TREE_CHAIN (t))
4660169689Skan    {
4661169689Skan      tree alias_decl = TREE_PURPOSE (t);
4662169689Skan      tree target = ultimate_transparent_alias_target (&TREE_VALUE (t));
4663169689Skan
4664169689Skan      if (! TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (alias_decl)))
4665169689Skan	/* Remove alias_decl from the weak list, but leave entries for
4666169689Skan	   the target alone.  */
4667169689Skan	target = NULL_TREE;
4668169689Skan#ifndef ASM_OUTPUT_WEAKREF
4669169689Skan      else if (! TREE_SYMBOL_REFERENCED (target))
4670169689Skan	{
4671169689Skan	  /* Use ASM_WEAKEN_LABEL only if ASM_WEAKEN_DECL is not
4672169689Skan	     defined, otherwise we and weak_finish_1 would use a
4673169689Skan	     different macros.  */
4674169689Skan# if defined ASM_WEAKEN_LABEL && ! defined ASM_WEAKEN_DECL
4675169689Skan	  ASM_WEAKEN_LABEL (asm_out_file, IDENTIFIER_POINTER (target));
4676169689Skan# else
4677169689Skan	  tree decl = find_decl_and_mark_needed (alias_decl, target);
4678169689Skan
4679169689Skan	  if (! decl)
4680169689Skan	    {
4681169689Skan	      decl = build_decl (TREE_CODE (alias_decl), target,
4682169689Skan				 TREE_TYPE (alias_decl));
4683169689Skan
4684169689Skan	      DECL_EXTERNAL (decl) = 1;
4685169689Skan	      TREE_PUBLIC (decl) = 1;
4686169689Skan	      DECL_ARTIFICIAL (decl) = 1;
4687169689Skan	      TREE_NOTHROW (decl) = TREE_NOTHROW (alias_decl);
4688169689Skan	      TREE_USED (decl) = 1;
4689169689Skan	    }
4690169689Skan
4691169689Skan	  weak_finish_1 (decl);
4692169689Skan# endif
4693169689Skan	}
4694169689Skan#endif
4695169689Skan
4696169689Skan      {
4697169689Skan	tree *p;
4698169689Skan	tree t2;
4699169689Skan
4700169689Skan	/* Remove the alias and the target from the pending weak list
4701169689Skan	   so that we do not emit any .weak directives for the former,
4702169689Skan	   nor multiple .weak directives for the latter.  */
4703169689Skan	for (p = &weak_decls; (t2 = *p) ; )
4704169689Skan	  {
4705169689Skan	    if (TREE_VALUE (t2) == alias_decl
4706169689Skan		|| target == DECL_ASSEMBLER_NAME (TREE_VALUE (t2)))
4707169689Skan	      *p = TREE_CHAIN (t2);
4708169689Skan	    else
4709169689Skan	      p = &TREE_CHAIN (t2);
4710169689Skan	  }
4711169689Skan
4712169689Skan	/* Remove other weakrefs to the same target, to speed things up.  */
4713169689Skan	for (p = &TREE_CHAIN (t); (t2 = *p) ; )
4714169689Skan	  {
4715169689Skan	    if (target == ultimate_transparent_alias_target (&TREE_VALUE (t2)))
4716169689Skan	      *p = TREE_CHAIN (t2);
4717169689Skan	    else
4718169689Skan	      p = &TREE_CHAIN (t2);
4719169689Skan	  }
4720169689Skan      }
472118334Speter    }
4722169689Skan
4723169689Skan  for (t = weak_decls; t; t = TREE_CHAIN (t))
4724169689Skan    {
4725169689Skan      tree decl = TREE_VALUE (t);
4726169689Skan
4727169689Skan      weak_finish_1 (decl);
4728169689Skan    }
472918334Speter}
473018334Speter
473196263Sobrien/* Emit the assembly bits to indicate that DECL is globally visible.  */
473296263Sobrien
473352284Sobrienstatic void
4734132718Skanglobalize_decl (tree decl)
473552284Sobrien{
473696263Sobrien  const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
473790075Sobrien
473896263Sobrien#if defined (ASM_WEAKEN_LABEL) || defined (ASM_WEAKEN_DECL)
473996263Sobrien  if (DECL_WEAK (decl))
474052284Sobrien    {
474196263Sobrien      tree *p, t;
474296263Sobrien
474396263Sobrien#ifdef ASM_WEAKEN_DECL
474496263Sobrien      ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
474596263Sobrien#else
474696263Sobrien      ASM_WEAKEN_LABEL (asm_out_file, name);
474796263Sobrien#endif
474896263Sobrien
474996263Sobrien      /* Remove this function from the pending weak list so that
475096263Sobrien	 we do not emit multiple .weak directives for it.  */
475196263Sobrien      for (p = &weak_decls; (t = *p) ; )
475296263Sobrien	{
475396263Sobrien	  if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
475496263Sobrien	    *p = TREE_CHAIN (t);
475596263Sobrien	  else
475696263Sobrien	    p = &TREE_CHAIN (t);
475796263Sobrien	}
4758169689Skan
4759169689Skan      /* Remove weakrefs to the same target from the pending weakref
4760169689Skan	 list, for the same reason.  */
4761169689Skan      for (p = &weakref_targets; (t = *p) ; )
4762169689Skan	{
4763169689Skan	  if (DECL_ASSEMBLER_NAME (decl)
4764169689Skan	      == ultimate_transparent_alias_target (&TREE_VALUE (t)))
4765169689Skan	    *p = TREE_CHAIN (t);
4766169689Skan	  else
4767169689Skan	    p = &TREE_CHAIN (t);
4768169689Skan	}
4769169689Skan
477096263Sobrien      return;
477152284Sobrien    }
4772169689Skan#elif defined(ASM_MAKE_LABEL_LINKONCE)
4773169689Skan  if (DECL_ONE_ONLY (decl))
4774169689Skan    ASM_MAKE_LABEL_LINKONCE (asm_out_file, name);
477596263Sobrien#endif
477696263Sobrien
4777169689Skan  targetm.asm_out.globalize_label (asm_out_file, name);
477852284Sobrien}
477952284Sobrien
4780169689Skan/* We have to be able to tell cgraph about the needed-ness of the target
4781169689Skan   of an alias.  This requires that the decl have been defined.  Aliases
4782169689Skan   that precede their definition have to be queued for later processing.  */
4783169689Skan
4784169689Skantypedef struct alias_pair GTY(())
4785146895Skan{
4786146895Skan  tree decl;
4787146895Skan  tree target;
4788169689Skan} alias_pair;
4789146895Skan
4790169689Skan/* Define gc'd vector type.  */
4791169689SkanDEF_VEC_O(alias_pair);
4792169689SkanDEF_VEC_ALLOC_O(alias_pair,gc);
4793146895Skan
4794169689Skanstatic GTY(()) VEC(alias_pair,gc) *alias_pairs;
4795169689Skan
4796169689Skan/* Given an assembly name, find the decl it is associated with.  At the
4797169689Skan   same time, mark it needed for cgraph.  */
4798169689Skan
4799169689Skanstatic tree
4800169689Skanfind_decl_and_mark_needed (tree decl, tree target)
4801169689Skan{
4802169689Skan  struct cgraph_node *fnode = NULL;
4803169689Skan  struct cgraph_varpool_node *vnode = NULL;
4804169689Skan
4805169689Skan  if (TREE_CODE (decl) == FUNCTION_DECL)
4806169689Skan    {
4807169689Skan      fnode = cgraph_node_for_asm (target);
4808169689Skan      if (fnode == NULL)
4809169689Skan	vnode = cgraph_varpool_node_for_asm (target);
4810169689Skan    }
4811169689Skan  else
4812169689Skan    {
4813169689Skan      vnode = cgraph_varpool_node_for_asm (target);
4814169689Skan      if (vnode == NULL)
4815169689Skan	fnode = cgraph_node_for_asm (target);
4816169689Skan    }
4817169689Skan
4818169689Skan  if (fnode)
4819169689Skan    {
4820169689Skan      /* We can't mark function nodes as used after cgraph global info
4821169689Skan	 is finished.  This wouldn't generally be necessary, but C++
4822169689Skan	 virtual table thunks are introduced late in the game and
4823169689Skan	 might seem like they need marking, although in fact they
4824169689Skan	 don't.  */
4825169689Skan      if (! cgraph_global_info_ready)
4826169689Skan	cgraph_mark_needed_node (fnode);
4827169689Skan      return fnode->decl;
4828169689Skan    }
4829169689Skan  else if (vnode)
4830169689Skan    {
4831169689Skan      cgraph_varpool_mark_needed_node (vnode);
4832169689Skan      return vnode->decl;
4833169689Skan    }
4834169689Skan  else
4835169689Skan    return NULL_TREE;
4836169689Skan}
4837169689Skan
4838146895Skan/* Output the assembler code for a define (equate) using ASM_OUTPUT_DEF
4839146895Skan   or ASM_OUTPUT_DEF_FROM_DECLS.  The function defines the symbol whose
4840146895Skan   tree node is DECL to have the value of the tree node TARGET.  */
4841146895Skan
4842146895Skanstatic void
4843169689Skando_assemble_alias (tree decl, tree target)
4844146895Skan{
4845169689Skan  if (TREE_ASM_WRITTEN (decl))
4846169689Skan    return;
4847169689Skan
4848169689Skan  TREE_ASM_WRITTEN (decl) = 1;
4849169689Skan  TREE_ASM_WRITTEN (DECL_ASSEMBLER_NAME (decl)) = 1;
4850169689Skan
4851169689Skan  if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
4852169689Skan    {
4853169689Skan      ultimate_transparent_alias_target (&target);
4854169689Skan
4855169689Skan      if (!TREE_SYMBOL_REFERENCED (target))
4856169689Skan	weakref_targets = tree_cons (decl, target, weakref_targets);
4857169689Skan
4858169689Skan#ifdef ASM_OUTPUT_WEAKREF
4859169689Skan      ASM_OUTPUT_WEAKREF (asm_out_file, decl,
4860169689Skan			  IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
4861169689Skan			  IDENTIFIER_POINTER (target));
4862169689Skan#else
4863169689Skan      if (!SUPPORTS_WEAK)
4864169689Skan	{
4865169689Skan	  error ("%Jweakref is not supported in this configuration", decl);
4866169689Skan	  return;
4867169689Skan	}
4868169689Skan#endif
4869169689Skan      return;
4870169689Skan    }
4871169689Skan
4872169689Skan#ifdef ASM_OUTPUT_DEF
4873169689Skan  /* Make name accessible from other files, if appropriate.  */
4874169689Skan
4875169689Skan  if (TREE_PUBLIC (decl))
4876169689Skan    {
4877169689Skan      globalize_decl (decl);
4878169689Skan      maybe_assemble_visibility (decl);
4879169689Skan    }
4880169689Skan
4881169689Skan# ifdef ASM_OUTPUT_DEF_FROM_DECLS
4882146895Skan  ASM_OUTPUT_DEF_FROM_DECLS (asm_out_file, decl, target);
4883169689Skan# else
4884146895Skan  ASM_OUTPUT_DEF (asm_out_file,
4885146895Skan		  IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
4886146895Skan		  IDENTIFIER_POINTER (target));
4887169689Skan# endif
4888169689Skan#elif defined (ASM_OUTPUT_WEAK_ALIAS) || defined (ASM_WEAKEN_DECL)
4889169689Skan  {
4890169689Skan    const char *name;
4891169689Skan    tree *p, t;
4892169689Skan
4893169689Skan    name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
4894169689Skan# ifdef ASM_WEAKEN_DECL
4895169689Skan    ASM_WEAKEN_DECL (asm_out_file, decl, name, IDENTIFIER_POINTER (target));
4896169689Skan# else
4897169689Skan    ASM_OUTPUT_WEAK_ALIAS (asm_out_file, name, IDENTIFIER_POINTER (target));
4898169689Skan# endif
4899169689Skan    /* Remove this function from the pending weak list so that
4900169689Skan       we do not emit multiple .weak directives for it.  */
4901169689Skan    for (p = &weak_decls; (t = *p) ; )
4902169689Skan      if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
4903169689Skan	*p = TREE_CHAIN (t);
4904169689Skan      else
4905169689Skan	p = &TREE_CHAIN (t);
4906169689Skan
4907169689Skan    /* Remove weakrefs to the same target from the pending weakref
4908169689Skan       list, for the same reason.  */
4909169689Skan    for (p = &weakref_targets; (t = *p) ; )
4910169689Skan      {
4911169689Skan	if (DECL_ASSEMBLER_NAME (decl)
4912169689Skan	    == ultimate_transparent_alias_target (&TREE_VALUE (t)))
4913169689Skan	  *p = TREE_CHAIN (t);
4914169689Skan	else
4915169689Skan	  p = &TREE_CHAIN (t);
4916169689Skan      }
4917169689Skan  }
4918146895Skan#endif
4919146895Skan}
4920146895Skan
4921169689Skan/* First pass of completing pending aliases.  Make sure that cgraph knows
4922169689Skan   which symbols will be required.  */
4923146895Skan
4924146895Skanvoid
4925169689Skanfinish_aliases_1 (void)
4926146895Skan{
4927169689Skan  unsigned i;
4928169689Skan  alias_pair *p;
4929146895Skan
4930169689Skan  for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); i++)
4931169689Skan    {
4932169689Skan      tree target_decl;
4933146895Skan
4934169689Skan      target_decl = find_decl_and_mark_needed (p->decl, p->target);
4935169689Skan      if (target_decl == NULL)
4936169689Skan	{
4937169689Skan	  if (! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
4938169689Skan	    error ("%q+D aliased to undefined symbol %qs",
4939169689Skan		   p->decl, IDENTIFIER_POINTER (p->target));
4940169689Skan	}
4941169689Skan      else if (DECL_EXTERNAL (target_decl)
4942169689Skan	       && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
4943169689Skan	error ("%q+D aliased to external symbol %qs",
4944169689Skan	       p->decl, IDENTIFIER_POINTER (p->target));
4945146895Skan    }
4946169689Skan}
4947146895Skan
4948169689Skan/* Second pass of completing pending aliases.  Emit the actual assembly.
4949169689Skan   This happens at the end of compilation and thus it is assured that the
4950169689Skan   target symbol has been emitted.  */
4951169689Skan
4952169689Skanvoid
4953169689Skanfinish_aliases_2 (void)
4954169689Skan{
4955169689Skan  unsigned i;
4956169689Skan  alias_pair *p;
4957169689Skan
4958169689Skan  for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); i++)
4959169689Skan    do_assemble_alias (p->decl, p->target);
4960169689Skan
4961169689Skan  VEC_truncate (alias_pair, alias_pairs, 0);
4962146895Skan}
4963146895Skan
496490075Sobrien/* Emit an assembler directive to make the symbol for DECL an alias to
496590075Sobrien   the symbol for TARGET.  */
496690075Sobrien
496718334Spetervoid
4968169689Skanassemble_alias (tree decl, tree target)
496918334Speter{
4970169689Skan  tree target_decl;
4971169689Skan  bool is_weakref = false;
497218334Speter
4973169689Skan  if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
4974117395Skan    {
4975169689Skan      tree alias = DECL_ASSEMBLER_NAME (decl);
497618334Speter
4977169689Skan      is_weakref = true;
4978146895Skan
4979169689Skan      ultimate_transparent_alias_target (&target);
4980169689Skan
4981169689Skan      if (alias == target)
4982169689Skan	error ("weakref %q+D ultimately targets itself", decl);
4983169689Skan      else
4984169689Skan	{
4985169689Skan#ifndef ASM_OUTPUT_WEAKREF
4986169689Skan	  IDENTIFIER_TRANSPARENT_ALIAS (alias) = 1;
4987169689Skan	  TREE_CHAIN (alias) = target;
4988169689Skan#endif
4989169689Skan	}
4990169689Skan      if (TREE_PUBLIC (decl))
4991169689Skan	error ("weakref %q+D must have static linkage", decl);
4992146895Skan    }
4993146895Skan  else
4994132718Skan    {
4995169689Skan#if !defined (ASM_OUTPUT_DEF)
4996169689Skan# if !defined(ASM_OUTPUT_WEAK_ALIAS) && !defined (ASM_WEAKEN_DECL)
4997169689Skan      error ("%Jalias definitions not supported in this configuration", decl);
4998169689Skan      return;
4999169689Skan# else
5000169689Skan      if (!DECL_WEAK (decl))
5001169689Skan	{
5002169689Skan	  error ("%Jonly weak aliases are supported in this configuration", decl);
5003169689Skan	  return;
5004169689Skan	}
5005169689Skan# endif
500696263Sobrien#endif
5007132718Skan    }
5008169689Skan
5009169689Skan  /* We must force creation of DECL_RTL for debug info generation, even though
5010169689Skan     we don't use it here.  */
5011169689Skan  make_decl_rtl (decl);
5012169689Skan  TREE_USED (decl) = 1;
5013169689Skan
5014169689Skan  /* A quirk of the initial implementation of aliases required that the user
5015169689Skan     add "extern" to all of them.  Which is silly, but now historical.  Do
5016169689Skan     note that the symbol is in fact locally defined.  */
5017169689Skan  if (! is_weakref)
5018169689Skan    DECL_EXTERNAL (decl) = 0;
5019169689Skan
5020169689Skan  /* Allow aliases to aliases.  */
5021169689Skan  if (TREE_CODE (decl) == FUNCTION_DECL)
5022169689Skan    cgraph_node (decl)->alias = true;
5023132718Skan  else
5024169689Skan    cgraph_varpool_node (decl)->alias = true;
5025132718Skan
5026169689Skan  /* If the target has already been emitted, we don't have to queue the
5027169689Skan     alias.  This saves a tad o memory.  */
5028169689Skan  target_decl = find_decl_and_mark_needed (decl, target);
5029169689Skan  if (target_decl && TREE_ASM_WRITTEN (target_decl))
5030169689Skan    do_assemble_alias (decl, target);
5031169689Skan  else
5032169689Skan    {
5033169689Skan      alias_pair *p = VEC_safe_push (alias_pair, gc, alias_pairs, NULL);
5034169689Skan      p->decl = decl;
5035169689Skan      p->target = target;
5036169689Skan    }
503718334Speter}
503850397Sobrien
5039117395Skan/* Emit an assembler directive to set symbol for DECL visibility to
5040117395Skan   the visibility type VIS, which must not be VISIBILITY_DEFAULT.  */
5041117395Skan
5042117395Skanvoid
5043132718Skandefault_assemble_visibility (tree decl, int vis)
5044117395Skan{
5045117395Skan  static const char * const visibility_types[] = {
5046169689Skan    NULL, "protected", "hidden", "internal"
5047117395Skan  };
5048117395Skan
5049117395Skan  const char *name, *type;
5050117395Skan
5051169689Skan  name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5052117395Skan  type = visibility_types[vis];
5053117395Skan
5054117395Skan#ifdef HAVE_GAS_HIDDEN
5055117395Skan  fprintf (asm_out_file, "\t.%s\t", type);
5056117395Skan  assemble_name (asm_out_file, name);
5057117395Skan  fprintf (asm_out_file, "\n");
5058117395Skan#else
5059169689Skan  warning (OPT_Wattributes, "visibility attribute not supported "
5060169689Skan	   "in this configuration; ignored");
5061117395Skan#endif
5062117395Skan}
5063117395Skan
5064117395Skan/* A helper function to call assemble_visibility when needed for a decl.  */
5065117395Skan
5066215840Sdimint
5067132718Skanmaybe_assemble_visibility (tree decl)
5068117395Skan{
5069132718Skan  enum symbol_visibility vis = DECL_VISIBILITY (decl);
5070117395Skan
5071117395Skan  if (vis != VISIBILITY_DEFAULT)
5072215840Sdim    {
5073215840Sdim      targetm.asm_out.visibility (decl, vis);
5074215840Sdim      return 1;
5075215840Sdim    }
5076215840Sdim  else
5077215840Sdim    return 0;
5078117395Skan}
5079117395Skan
508050397Sobrien/* Returns 1 if the target configuration supports defining public symbols
508150397Sobrien   so that one of them will be chosen at link time instead of generating a
508250397Sobrien   multiply-defined symbol error, whether through the use of weak symbols or
508350397Sobrien   a target-specific mechanism for having duplicates discarded.  */
508450397Sobrien
508550397Sobrienint
5086132718Skansupports_one_only (void)
508750397Sobrien{
508850397Sobrien  if (SUPPORTS_ONE_ONLY)
508950397Sobrien    return 1;
509050397Sobrien  return SUPPORTS_WEAK;
509150397Sobrien}
509250397Sobrien
509350397Sobrien/* Set up DECL as a public symbol that can be defined in multiple
509450397Sobrien   translation units without generating a linker error.  */
509550397Sobrien
509650397Sobrienvoid
5097132718Skanmake_decl_one_only (tree decl)
509850397Sobrien{
5099169689Skan  gcc_assert (TREE_CODE (decl) == VAR_DECL
5100169689Skan	      || TREE_CODE (decl) == FUNCTION_DECL);
510150397Sobrien
510250397Sobrien  TREE_PUBLIC (decl) = 1;
510350397Sobrien
5104132718Skan  if (SUPPORTS_ONE_ONLY)
510550397Sobrien    {
510650397Sobrien#ifdef MAKE_DECL_ONE_ONLY
510750397Sobrien      MAKE_DECL_ONE_ONLY (decl);
510850397Sobrien#endif
510950397Sobrien      DECL_ONE_ONLY (decl) = 1;
511050397Sobrien    }
5111132718Skan  else if (TREE_CODE (decl) == VAR_DECL
5112132718Skan      && (DECL_INITIAL (decl) == 0 || DECL_INITIAL (decl) == error_mark_node))
5113132718Skan    DECL_COMMON (decl) = 1;
511450397Sobrien  else
5115169689Skan    {
5116169689Skan      gcc_assert (SUPPORTS_WEAK);
5117169689Skan      DECL_WEAK (decl) = 1;
5118169689Skan    }
511950397Sobrien}
512090075Sobrien
512190075Sobrienvoid
5122132718Skaninit_varasm_once (void)
512390075Sobrien{
5124169689Skan  section_htab = htab_create_ggc (31, section_entry_hash,
5125169689Skan				  section_entry_eq, NULL);
5126169689Skan  object_block_htab = htab_create_ggc (31, object_block_entry_hash,
5127169689Skan				       object_block_entry_eq, NULL);
5128132718Skan  const_desc_htab = htab_create_ggc (1009, const_desc_hash,
5129132718Skan				     const_desc_eq, NULL);
513090075Sobrien
513190075Sobrien  const_alias_set = new_alias_set ();
5132169689Skan  shared_constant_pool = create_constant_pool ();
5133169689Skan
5134169689Skan#ifdef TEXT_SECTION_ASM_OP
5135169689Skan  text_section = get_unnamed_section (SECTION_CODE, output_section_asm_op,
5136169689Skan				      TEXT_SECTION_ASM_OP);
5137169689Skan#endif
5138169689Skan
5139169689Skan#ifdef DATA_SECTION_ASM_OP
5140169689Skan  data_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
5141169689Skan				      DATA_SECTION_ASM_OP);
5142169689Skan#endif
5143169689Skan
5144169689Skan#ifdef SDATA_SECTION_ASM_OP
5145169689Skan  sdata_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
5146169689Skan				       SDATA_SECTION_ASM_OP);
5147169689Skan#endif
5148169689Skan
5149169689Skan#ifdef READONLY_DATA_SECTION_ASM_OP
5150169689Skan  readonly_data_section = get_unnamed_section (0, output_section_asm_op,
5151169689Skan					       READONLY_DATA_SECTION_ASM_OP);
5152169689Skan#endif
5153169689Skan
5154169689Skan#ifdef CTORS_SECTION_ASM_OP
5155169689Skan  ctors_section = get_unnamed_section (0, output_section_asm_op,
5156169689Skan				       CTORS_SECTION_ASM_OP);
5157169689Skan#endif
5158169689Skan
5159169689Skan#ifdef DTORS_SECTION_ASM_OP
5160169689Skan  dtors_section = get_unnamed_section (0, output_section_asm_op,
5161169689Skan				       DTORS_SECTION_ASM_OP);
5162169689Skan#endif
5163169689Skan
5164169689Skan#ifdef BSS_SECTION_ASM_OP
5165169689Skan  bss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
5166169689Skan				     output_section_asm_op,
5167169689Skan				     BSS_SECTION_ASM_OP);
5168169689Skan#endif
5169169689Skan
5170169689Skan#ifdef SBSS_SECTION_ASM_OP
5171169689Skan  sbss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
5172169689Skan				      output_section_asm_op,
5173169689Skan				      SBSS_SECTION_ASM_OP);
5174169689Skan#endif
5175169689Skan
5176169689Skan  tls_comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
5177169689Skan					   | SECTION_COMMON, emit_tls_common);
5178169689Skan  lcomm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
5179169689Skan					| SECTION_COMMON, emit_local);
5180169689Skan  comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
5181169689Skan				       | SECTION_COMMON, emit_common);
5182169689Skan
5183169689Skan#if defined ASM_OUTPUT_ALIGNED_BSS || defined ASM_OUTPUT_BSS
5184169689Skan  bss_noswitch_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS,
5185169689Skan					       emit_bss);
5186169689Skan#endif
5187169689Skan
5188169689Skan  targetm.asm_out.init_sections ();
5189169689Skan
5190169689Skan  if (readonly_data_section == NULL)
5191169689Skan    readonly_data_section = text_section;
519290075Sobrien}
519390075Sobrien
5194117395Skanenum tls_model
5195169689Skandecl_default_tls_model (tree decl)
5196117395Skan{
5197117395Skan  enum tls_model kind;
5198117395Skan  bool is_local;
5199117395Skan
5200169689Skan  is_local = targetm.binds_local_p (decl);
5201169689Skan  if (!flag_shlib)
5202117395Skan    {
5203117395Skan      if (is_local)
5204117395Skan	kind = TLS_MODEL_LOCAL_EXEC;
5205117395Skan      else
5206117395Skan	kind = TLS_MODEL_INITIAL_EXEC;
5207117395Skan    }
5208169689Skan
5209117395Skan  /* Local dynamic is inefficient when we're not combining the
5210117395Skan     parts of the address.  */
5211117395Skan  else if (optimize && is_local)
5212117395Skan    kind = TLS_MODEL_LOCAL_DYNAMIC;
5213117395Skan  else
5214117395Skan    kind = TLS_MODEL_GLOBAL_DYNAMIC;
5215117395Skan  if (kind < flag_tls_default)
5216117395Skan    kind = flag_tls_default;
5217117395Skan
5218117395Skan  return kind;
5219117395Skan}
5220117395Skan
522190075Sobrien/* Select a set of attributes for section NAME based on the properties
522290075Sobrien   of DECL and whether or not RELOC indicates that DECL's initializer
522390075Sobrien   might contain runtime relocations.
522490075Sobrien
522590075Sobrien   We make the section read-only and executable for a function decl,
522690075Sobrien   read-only for a const data decl, and writable for a non-const data decl.  */
522790075Sobrien
522890075Sobrienunsigned int
5229132718Skandefault_section_type_flags (tree decl, const char *name, int reloc)
523090075Sobrien{
523190075Sobrien  unsigned int flags;
523290075Sobrien
523390075Sobrien  if (decl && TREE_CODE (decl) == FUNCTION_DECL)
523490075Sobrien    flags = SECTION_CODE;
5235169689Skan  else if (decl && decl_readonly_section (decl, reloc))
523690075Sobrien    flags = 0;
5237169689Skan  else if (current_function_decl
5238169689Skan	   && cfun
5239169689Skan	   && cfun->unlikely_text_section_name
5240169689Skan	   && strcmp (name, cfun->unlikely_text_section_name) == 0)
5241169689Skan    flags = SECTION_CODE;
5242169689Skan  else if (!decl
5243169689Skan	   && (!current_function_decl || !cfun)
5244169689Skan	   && strcmp (name, UNLIKELY_EXECUTED_TEXT_SECTION_NAME) == 0)
5245169689Skan    flags = SECTION_CODE;
524690075Sobrien  else
524790075Sobrien    flags = SECTION_WRITE;
524890075Sobrien
524990075Sobrien  if (decl && DECL_ONE_ONLY (decl))
525090075Sobrien    flags |= SECTION_LINKONCE;
525190075Sobrien
5252169689Skan  if (decl && TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
5253117395Skan    flags |= SECTION_TLS | SECTION_WRITE;
5254117395Skan
525590075Sobrien  if (strcmp (name, ".bss") == 0
525690075Sobrien      || strncmp (name, ".bss.", 5) == 0
525790075Sobrien      || strncmp (name, ".gnu.linkonce.b.", 16) == 0
525890075Sobrien      || strcmp (name, ".sbss") == 0
525990075Sobrien      || strncmp (name, ".sbss.", 6) == 0
5260169689Skan      || strncmp (name, ".gnu.linkonce.sb.", 17) == 0)
526190075Sobrien    flags |= SECTION_BSS;
526290075Sobrien
5263117395Skan  if (strcmp (name, ".tdata") == 0
5264169689Skan      || strncmp (name, ".tdata.", 7) == 0
5265169689Skan      || strncmp (name, ".gnu.linkonce.td.", 17) == 0)
5266117395Skan    flags |= SECTION_TLS;
5267117395Skan
5268169689Skan  if (strcmp (name, ".tbss") == 0
5269169689Skan      || strncmp (name, ".tbss.", 6) == 0
5270169689Skan      || strncmp (name, ".gnu.linkonce.tb.", 17) == 0)
5271169689Skan    flags |= SECTION_TLS | SECTION_BSS;
5272169689Skan
5273117395Skan  /* These three sections have special ELF types.  They are neither
5274117395Skan     SHT_PROGBITS nor SHT_NOBITS, so when changing sections we don't
5275117395Skan     want to print a section type (@progbits or @nobits).  If someone
5276117395Skan     is silly enough to emit code or TLS variables to one of these
5277117395Skan     sections, then don't handle them specially.  */
5278117395Skan  if (!(flags & (SECTION_CODE | SECTION_BSS | SECTION_TLS))
5279117395Skan      && (strcmp (name, ".init_array") == 0
5280117395Skan	  || strcmp (name, ".fini_array") == 0
5281117395Skan	  || strcmp (name, ".preinit_array") == 0))
5282117395Skan    flags |= SECTION_NOTYPE;
5283117395Skan
528490075Sobrien  return flags;
528590075Sobrien}
528690075Sobrien
5287169689Skan/* Return true if the target supports some form of global BSS,
5288169689Skan   either through bss_noswitch_section, or by selecting a BSS
5289169689Skan   section in TARGET_ASM_SELECT_SECTION.  */
5290169689Skan
5291169689Skanbool
5292169689Skanhave_global_bss_p (void)
5293169689Skan{
5294169689Skan  return bss_noswitch_section || targetm.have_switchable_bss_sections;
5295169689Skan}
5296169689Skan
529790075Sobrien/* Output assembly to switch to section NAME with attribute FLAGS.
529890075Sobrien   Four variants for common object file formats.  */
529990075Sobrien
530090075Sobrienvoid
5301132718Skandefault_no_named_section (const char *name ATTRIBUTE_UNUSED,
5302169689Skan			  unsigned int flags ATTRIBUTE_UNUSED,
5303169689Skan			  tree decl ATTRIBUTE_UNUSED)
530490075Sobrien{
530590075Sobrien  /* Some object formats don't support named sections at all.  The
530690075Sobrien     front-end should already have flagged this as an error.  */
5307169689Skan  gcc_unreachable ();
530890075Sobrien}
530990075Sobrien
531090075Sobrienvoid
5311169689Skandefault_elf_asm_named_section (const char *name, unsigned int flags,
5312169689Skan			       tree decl ATTRIBUTE_UNUSED)
531390075Sobrien{
531490075Sobrien  char flagchars[10], *f = flagchars;
531590075Sobrien
5316169689Skan  /* If we have already declared this section, we can use an
5317169689Skan     abbreviated form to switch back to it -- unless this section is
5318169689Skan     part of a COMDAT groups, in which case GAS requires the full
5319169689Skan     declaration every time.  */
5320169689Skan  if (!(HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
5321169689Skan      && (flags & SECTION_DECLARED))
532290075Sobrien    {
532390075Sobrien      fprintf (asm_out_file, "\t.section\t%s\n", name);
532490075Sobrien      return;
532590075Sobrien    }
532690075Sobrien
532790075Sobrien  if (!(flags & SECTION_DEBUG))
532890075Sobrien    *f++ = 'a';
532990075Sobrien  if (flags & SECTION_WRITE)
533090075Sobrien    *f++ = 'w';
533190075Sobrien  if (flags & SECTION_CODE)
533290075Sobrien    *f++ = 'x';
533390075Sobrien  if (flags & SECTION_SMALL)
533490075Sobrien    *f++ = 's';
533590075Sobrien  if (flags & SECTION_MERGE)
533690075Sobrien    *f++ = 'M';
533790075Sobrien  if (flags & SECTION_STRINGS)
533890075Sobrien    *f++ = 'S';
5339117395Skan  if (flags & SECTION_TLS)
5340117395Skan    *f++ = 'T';
5341169689Skan  if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
5342169689Skan    *f++ = 'G';
534390075Sobrien  *f = '\0';
534490075Sobrien
5345117395Skan  fprintf (asm_out_file, "\t.section\t%s,\"%s\"", name, flagchars);
534690075Sobrien
5347117395Skan  if (!(flags & SECTION_NOTYPE))
5348117395Skan    {
5349117395Skan      const char *type;
5350169689Skan      const char *format;
5351117395Skan
5352117395Skan      if (flags & SECTION_BSS)
5353117395Skan	type = "nobits";
5354117395Skan      else
5355117395Skan	type = "progbits";
5356117395Skan
5357169689Skan      format = ",@%s";
5358169689Skan#ifdef ASM_COMMENT_START
5359169689Skan      /* On platforms that use "@" as the assembly comment character,
5360169689Skan	 use "%" instead.  */
5361169689Skan      if (strcmp (ASM_COMMENT_START, "@") == 0)
5362169689Skan	format = ",%%%s";
5363169689Skan#endif
5364169689Skan      fprintf (asm_out_file, format, type);
5365117395Skan
5366117395Skan      if (flags & SECTION_ENTSIZE)
5367117395Skan	fprintf (asm_out_file, ",%d", flags & SECTION_ENTSIZE);
5368169689Skan      if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
5369169689Skan	fprintf (asm_out_file, ",%s,comdat",
5370169689Skan		 lang_hooks.decls.comdat_group (decl));
5371117395Skan    }
5372117395Skan
5373117395Skan  putc ('\n', asm_out_file);
537490075Sobrien}
537590075Sobrien
537690075Sobrienvoid
5377169689Skandefault_coff_asm_named_section (const char *name, unsigned int flags,
5378169689Skan				tree decl ATTRIBUTE_UNUSED)
537990075Sobrien{
538090075Sobrien  char flagchars[8], *f = flagchars;
538190075Sobrien
538290075Sobrien  if (flags & SECTION_WRITE)
538390075Sobrien    *f++ = 'w';
538490075Sobrien  if (flags & SECTION_CODE)
538590075Sobrien    *f++ = 'x';
538690075Sobrien  *f = '\0';
538790075Sobrien
538890075Sobrien  fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
538990075Sobrien}
539090075Sobrien
539190075Sobrienvoid
5392169689Skandefault_pe_asm_named_section (const char *name, unsigned int flags,
5393169689Skan			      tree decl)
539490075Sobrien{
5395169689Skan  default_coff_asm_named_section (name, flags, decl);
539690075Sobrien
539790075Sobrien  if (flags & SECTION_LINKONCE)
539890075Sobrien    {
539990075Sobrien      /* Functions may have been compiled at various levels of
540090075Sobrien         optimization so we can't use `same_size' here.
540190075Sobrien         Instead, have the linker pick one.  */
540290075Sobrien      fprintf (asm_out_file, "\t.linkonce %s\n",
540390075Sobrien	       (flags & SECTION_CODE ? "discard" : "same_size"));
540490075Sobrien    }
540590075Sobrien}
540690075Sobrien
5407117395Skan/* The lame default section selector.  */
5408117395Skan
5409169689Skansection *
5410132718Skandefault_select_section (tree decl, int reloc,
5411132718Skan			unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
5412117395Skan{
5413117395Skan  if (DECL_P (decl))
5414117395Skan    {
5415117395Skan      if (decl_readonly_section (decl, reloc))
5416169689Skan	return readonly_data_section;
5417117395Skan    }
5418117395Skan  else if (TREE_CODE (decl) == CONSTRUCTOR)
5419117395Skan    {
5420117395Skan      if (! ((flag_pic && reloc)
5421117395Skan	     || !TREE_READONLY (decl)
5422117395Skan	     || TREE_SIDE_EFFECTS (decl)
5423117395Skan	     || !TREE_CONSTANT (decl)))
5424169689Skan	return readonly_data_section;
5425117395Skan    }
5426117395Skan  else if (TREE_CODE (decl) == STRING_CST)
5427169689Skan    return readonly_data_section;
5428117395Skan  else if (! (flag_pic && reloc))
5429169689Skan    return readonly_data_section;
5430117395Skan
5431169689Skan  return data_section;
5432117395Skan}
5433117395Skan
5434117395Skanenum section_category
5435169689Skancategorize_decl_for_section (tree decl, int reloc)
5436117395Skan{
5437117395Skan  enum section_category ret;
5438117395Skan
5439117395Skan  if (TREE_CODE (decl) == FUNCTION_DECL)
5440117395Skan    return SECCAT_TEXT;
5441117395Skan  else if (TREE_CODE (decl) == STRING_CST)
5442117395Skan    {
5443169689Skan      if (flag_mudflap) /* or !flag_merge_constants */
5444169689Skan        return SECCAT_RODATA;
5445117395Skan      else
5446117395Skan	return SECCAT_RODATA_MERGE_STR;
5447117395Skan    }
5448117395Skan  else if (TREE_CODE (decl) == VAR_DECL)
5449117395Skan    {
5450169689Skan      if (bss_initializer_p (decl))
5451117395Skan	ret = SECCAT_BSS;
5452117395Skan      else if (! TREE_READONLY (decl)
5453117395Skan	       || TREE_SIDE_EFFECTS (decl)
5454117395Skan	       || ! TREE_CONSTANT (DECL_INITIAL (decl)))
5455117395Skan	{
5456169689Skan	  /* Here the reloc_rw_mask is not testing whether the section should
5457169689Skan	     be read-only or not, but whether the dynamic link will have to
5458169689Skan	     do something.  If so, we wish to segregate the data in order to
5459169689Skan	     minimize cache misses inside the dynamic linker.  */
5460169689Skan	  if (reloc & targetm.asm_out.reloc_rw_mask ())
5461169689Skan	    ret = reloc == 1 ? SECCAT_DATA_REL_LOCAL : SECCAT_DATA_REL;
5462117395Skan	  else
5463117395Skan	    ret = SECCAT_DATA;
5464117395Skan	}
5465169689Skan      else if (reloc & targetm.asm_out.reloc_rw_mask ())
5466169689Skan	ret = reloc == 1 ? SECCAT_DATA_REL_RO_LOCAL : SECCAT_DATA_REL_RO;
5467117395Skan      else if (reloc || flag_merge_constants < 2)
5468117395Skan	/* C and C++ don't allow different variables to share the same
5469117395Skan	   location.  -fmerge-all-constants allows even that (at the
5470117395Skan	   expense of not conforming).  */
5471117395Skan	ret = SECCAT_RODATA;
5472117395Skan      else if (TREE_CODE (DECL_INITIAL (decl)) == STRING_CST)
5473117395Skan	ret = SECCAT_RODATA_MERGE_STR_INIT;
5474117395Skan      else
5475117395Skan	ret = SECCAT_RODATA_MERGE_CONST;
5476117395Skan    }
5477117395Skan  else if (TREE_CODE (decl) == CONSTRUCTOR)
5478117395Skan    {
5479169689Skan      if ((reloc & targetm.asm_out.reloc_rw_mask ())
5480117395Skan	  || TREE_SIDE_EFFECTS (decl)
5481117395Skan	  || ! TREE_CONSTANT (decl))
5482117395Skan	ret = SECCAT_DATA;
5483117395Skan      else
5484117395Skan	ret = SECCAT_RODATA;
5485117395Skan    }
5486117395Skan  else
5487117395Skan    ret = SECCAT_RODATA;
5488117395Skan
5489117395Skan  /* There are no read-only thread-local sections.  */
5490169689Skan  if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
5491117395Skan    {
5492169689Skan      /* Note that this would be *just* SECCAT_BSS, except that there's
5493169689Skan	 no concept of a read-only thread-local-data section.  */
5494169689Skan      if (ret == SECCAT_BSS
5495169689Skan	  || (flag_zero_initialized_in_bss
5496169689Skan	      && initializer_zerop (DECL_INITIAL (decl))))
5497117395Skan	ret = SECCAT_TBSS;
5498117395Skan      else
5499117395Skan	ret = SECCAT_TDATA;
5500117395Skan    }
5501117395Skan
5502117395Skan  /* If the target uses small data sections, select it.  */
5503169689Skan  else if (targetm.in_small_data_p (decl))
5504117395Skan    {
5505117395Skan      if (ret == SECCAT_BSS)
5506117395Skan	ret = SECCAT_SBSS;
5507117395Skan      else if (targetm.have_srodata_section && ret == SECCAT_RODATA)
5508117395Skan	ret = SECCAT_SRODATA;
5509117395Skan      else
5510117395Skan	ret = SECCAT_SDATA;
5511117395Skan    }
5512117395Skan
5513117395Skan  return ret;
5514117395Skan}
5515117395Skan
5516117395Skanbool
5517132718Skandecl_readonly_section (tree decl, int reloc)
5518117395Skan{
5519169689Skan  switch (categorize_decl_for_section (decl, reloc))
5520117395Skan    {
5521117395Skan    case SECCAT_RODATA:
5522117395Skan    case SECCAT_RODATA_MERGE_STR:
5523117395Skan    case SECCAT_RODATA_MERGE_STR_INIT:
5524117395Skan    case SECCAT_RODATA_MERGE_CONST:
5525117395Skan    case SECCAT_SRODATA:
5526117395Skan      return true;
5527117395Skan      break;
5528117395Skan    default:
5529117395Skan      return false;
5530117395Skan      break;
5531117395Skan    }
5532117395Skan}
5533117395Skan
5534117395Skan/* Select a section based on the above categorization.  */
5535117395Skan
5536169689Skansection *
5537132718Skandefault_elf_select_section (tree decl, int reloc,
5538132718Skan			    unsigned HOST_WIDE_INT align)
5539117395Skan{
5540169689Skan  const char *sname;
5541169689Skan  switch (categorize_decl_for_section (decl, reloc))
5542117395Skan    {
5543117395Skan    case SECCAT_TEXT:
5544117395Skan      /* We're not supposed to be called on FUNCTION_DECLs.  */
5545169689Skan      gcc_unreachable ();
5546117395Skan    case SECCAT_RODATA:
5547169689Skan      return readonly_data_section;
5548117395Skan    case SECCAT_RODATA_MERGE_STR:
5549169689Skan      return mergeable_string_section (decl, align, 0);
5550117395Skan    case SECCAT_RODATA_MERGE_STR_INIT:
5551169689Skan      return mergeable_string_section (DECL_INITIAL (decl), align, 0);
5552117395Skan    case SECCAT_RODATA_MERGE_CONST:
5553169689Skan      return mergeable_constant_section (DECL_MODE (decl), align, 0);
5554117395Skan    case SECCAT_SRODATA:
5555169689Skan      sname = ".sdata2";
5556117395Skan      break;
5557117395Skan    case SECCAT_DATA:
5558169689Skan      return data_section;
5559117395Skan    case SECCAT_DATA_REL:
5560169689Skan      sname = ".data.rel";
5561117395Skan      break;
5562117395Skan    case SECCAT_DATA_REL_LOCAL:
5563169689Skan      sname = ".data.rel.local";
5564117395Skan      break;
5565117395Skan    case SECCAT_DATA_REL_RO:
5566169689Skan      sname = ".data.rel.ro";
5567117395Skan      break;
5568117395Skan    case SECCAT_DATA_REL_RO_LOCAL:
5569169689Skan      sname = ".data.rel.ro.local";
5570117395Skan      break;
5571117395Skan    case SECCAT_SDATA:
5572169689Skan      sname = ".sdata";
5573117395Skan      break;
5574117395Skan    case SECCAT_TDATA:
5575169689Skan      sname = ".tdata";
5576117395Skan      break;
5577117395Skan    case SECCAT_BSS:
5578169689Skan      if (bss_section)
5579169689Skan	return bss_section;
5580169689Skan      sname = ".bss";
5581117395Skan      break;
5582117395Skan    case SECCAT_SBSS:
5583169689Skan      sname = ".sbss";
5584117395Skan      break;
5585117395Skan    case SECCAT_TBSS:
5586169689Skan      sname = ".tbss";
5587117395Skan      break;
5588117395Skan    default:
5589169689Skan      gcc_unreachable ();
5590117395Skan    }
5591169689Skan
5592169689Skan  if (!DECL_P (decl))
5593169689Skan    decl = NULL_TREE;
5594169689Skan  return get_named_section (decl, sname, reloc);
5595117395Skan}
5596117395Skan
5597117395Skan/* Construct a unique section name based on the decl name and the
5598117395Skan   categorization performed above.  */
5599117395Skan
5600117395Skanvoid
5601132718Skandefault_unique_section (tree decl, int reloc)
5602117395Skan{
5603169689Skan  /* We only need to use .gnu.linkonce if we don't have COMDAT groups.  */
5604169689Skan  bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
5605117395Skan  const char *prefix, *name;
5606117395Skan  size_t nlen, plen;
5607117395Skan  char *string;
5608117395Skan
5609169689Skan  switch (categorize_decl_for_section (decl, reloc))
5610117395Skan    {
5611117395Skan    case SECCAT_TEXT:
5612117395Skan      prefix = one_only ? ".gnu.linkonce.t." : ".text.";
5613117395Skan      break;
5614117395Skan    case SECCAT_RODATA:
5615117395Skan    case SECCAT_RODATA_MERGE_STR:
5616117395Skan    case SECCAT_RODATA_MERGE_STR_INIT:
5617117395Skan    case SECCAT_RODATA_MERGE_CONST:
5618117395Skan      prefix = one_only ? ".gnu.linkonce.r." : ".rodata.";
5619117395Skan      break;
5620117395Skan    case SECCAT_SRODATA:
5621117395Skan      prefix = one_only ? ".gnu.linkonce.s2." : ".sdata2.";
5622117395Skan      break;
5623117395Skan    case SECCAT_DATA:
5624169689Skan      prefix = one_only ? ".gnu.linkonce.d." : ".data.";
5625169689Skan      break;
5626117395Skan    case SECCAT_DATA_REL:
5627169689Skan      prefix = one_only ? ".gnu.linkonce.d.rel." : ".data.rel.";
5628169689Skan      break;
5629117395Skan    case SECCAT_DATA_REL_LOCAL:
5630169689Skan      prefix = one_only ? ".gnu.linkonce.d.rel.local." : ".data.rel.local.";
5631169689Skan      break;
5632117395Skan    case SECCAT_DATA_REL_RO:
5633169689Skan      prefix = one_only ? ".gnu.linkonce.d.rel.ro." : ".data.rel.ro.";
5634169689Skan      break;
5635117395Skan    case SECCAT_DATA_REL_RO_LOCAL:
5636169689Skan      prefix = one_only ? ".gnu.linkonce.d.rel.ro.local."
5637169689Skan	       : ".data.rel.ro.local.";
5638117395Skan      break;
5639117395Skan    case SECCAT_SDATA:
5640117395Skan      prefix = one_only ? ".gnu.linkonce.s." : ".sdata.";
5641117395Skan      break;
5642117395Skan    case SECCAT_BSS:
5643117395Skan      prefix = one_only ? ".gnu.linkonce.b." : ".bss.";
5644117395Skan      break;
5645117395Skan    case SECCAT_SBSS:
5646117395Skan      prefix = one_only ? ".gnu.linkonce.sb." : ".sbss.";
5647117395Skan      break;
5648117395Skan    case SECCAT_TDATA:
5649117395Skan      prefix = one_only ? ".gnu.linkonce.td." : ".tdata.";
5650117395Skan      break;
5651117395Skan    case SECCAT_TBSS:
5652117395Skan      prefix = one_only ? ".gnu.linkonce.tb." : ".tbss.";
5653117395Skan      break;
5654117395Skan    default:
5655169689Skan      gcc_unreachable ();
5656117395Skan    }
5657117395Skan  plen = strlen (prefix);
5658117395Skan
5659117395Skan  name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5660169689Skan  name = targetm.strip_name_encoding (name);
5661117395Skan  nlen = strlen (name);
5662117395Skan
5663117395Skan  string = alloca (nlen + plen + 1);
5664117395Skan  memcpy (string, prefix, plen);
5665117395Skan  memcpy (string + plen, name, nlen + 1);
5666117395Skan
5667117395Skan  DECL_SECTION_NAME (decl) = build_string (nlen + plen, string);
5668117395Skan}
5669117395Skan
5670169689Skan/* Like compute_reloc_for_constant, except for an RTX.  The return value
5671169689Skan   is a mask for which bit 1 indicates a global relocation, and bit 0
5672169689Skan   indicates a local relocation.  */
5673169689Skan
5674169689Skanstatic int
5675169689Skancompute_reloc_for_rtx_1 (rtx *xp, void *data)
5676169689Skan{
5677169689Skan  int *preloc = data;
5678169689Skan  rtx x = *xp;
5679169689Skan
5680169689Skan  switch (GET_CODE (x))
5681169689Skan    {
5682169689Skan    case SYMBOL_REF:
5683169689Skan      *preloc |= SYMBOL_REF_LOCAL_P (x) ? 1 : 2;
5684169689Skan      break;
5685169689Skan    case LABEL_REF:
5686169689Skan      *preloc |= 1;
5687169689Skan      break;
5688169689Skan    default:
5689169689Skan      break;
5690169689Skan    }
5691169689Skan
5692169689Skan  return 0;
5693169689Skan}
5694169689Skan
5695169689Skanstatic int
5696169689Skancompute_reloc_for_rtx (rtx x)
5697169689Skan{
5698169689Skan  int reloc;
5699169689Skan
5700169689Skan  switch (GET_CODE (x))
5701169689Skan    {
5702169689Skan    case CONST:
5703169689Skan    case SYMBOL_REF:
5704169689Skan    case LABEL_REF:
5705169689Skan      reloc = 0;
5706169689Skan      for_each_rtx (&x, compute_reloc_for_rtx_1, &reloc);
5707169689Skan      return reloc;
5708169689Skan
5709169689Skan    default:
5710169689Skan      return 0;
5711169689Skan    }
5712169689Skan}
5713169689Skan
5714169689Skansection *
5715132718Skandefault_select_rtx_section (enum machine_mode mode ATTRIBUTE_UNUSED,
5716132718Skan			    rtx x,
5717132718Skan			    unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
5718117395Skan{
5719169689Skan  if (compute_reloc_for_rtx (x) & targetm.asm_out.reloc_rw_mask ())
5720169689Skan    return data_section;
5721169689Skan  else
5722169689Skan    return readonly_data_section;
5723117395Skan}
5724117395Skan
5725169689Skansection *
5726132718Skandefault_elf_select_rtx_section (enum machine_mode mode, rtx x,
5727132718Skan				unsigned HOST_WIDE_INT align)
5728117395Skan{
5729169689Skan  int reloc = compute_reloc_for_rtx (x);
5730169689Skan
5731117395Skan  /* ??? Handle small data here somehow.  */
5732117395Skan
5733169689Skan  if (reloc & targetm.asm_out.reloc_rw_mask ())
5734169689Skan    {
5735169689Skan      if (reloc == 1)
5736169689Skan	return get_named_section (NULL, ".data.rel.ro.local", 1);
5737169689Skan      else
5738169689Skan	return get_named_section (NULL, ".data.rel.ro", 3);
5739169689Skan    }
5740117395Skan
5741169689Skan  return mergeable_constant_section (mode, align, 0);
5742117395Skan}
5743117395Skan
5744132718Skan/* Set the generally applicable flags on the SYMBOL_REF for EXP.  */
5745132718Skan
5746132718Skanvoid
5747132718Skandefault_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
5748132718Skan{
5749132718Skan  rtx symbol;
5750132718Skan  int flags;
5751132718Skan
5752132718Skan  /* Careful not to prod global register variables.  */
5753169689Skan  if (!MEM_P (rtl))
5754132718Skan    return;
5755132718Skan  symbol = XEXP (rtl, 0);
5756132718Skan  if (GET_CODE (symbol) != SYMBOL_REF)
5757132718Skan    return;
5758132718Skan
5759169689Skan  flags = SYMBOL_REF_FLAGS (symbol) & SYMBOL_FLAG_HAS_BLOCK_INFO;
5760132718Skan  if (TREE_CODE (decl) == FUNCTION_DECL)
5761132718Skan    flags |= SYMBOL_FLAG_FUNCTION;
5762169689Skan  if (targetm.binds_local_p (decl))
5763132718Skan    flags |= SYMBOL_FLAG_LOCAL;
5764169689Skan  if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
5765169689Skan    flags |= DECL_TLS_MODEL (decl) << SYMBOL_FLAG_TLS_SHIFT;
5766169689Skan  else if (targetm.in_small_data_p (decl))
5767132718Skan    flags |= SYMBOL_FLAG_SMALL;
5768132718Skan  /* ??? Why is DECL_EXTERNAL ever set for non-PUBLIC names?  Without
5769132718Skan     being PUBLIC, the thing *must* be defined in this translation unit.
5770132718Skan     Prevent this buglet from being propagated into rtl code as well.  */
5771132718Skan  if (DECL_P (decl) && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl))
5772132718Skan    flags |= SYMBOL_FLAG_EXTERNAL;
5773132718Skan
5774132718Skan  SYMBOL_REF_FLAGS (symbol) = flags;
5775132718Skan}
5776132718Skan
5777117395Skan/* By default, we do nothing for encode_section_info, so we need not
5778117395Skan   do anything but discard the '*' marker.  */
5779117395Skan
5780117395Skanconst char *
5781132718Skandefault_strip_name_encoding (const char *str)
5782117395Skan{
5783117395Skan  return str + (*str == '*');
5784117395Skan}
5785117395Skan
5786169689Skan#ifdef ASM_OUTPUT_DEF
5787169689Skan/* The default implementation of TARGET_ASM_OUTPUT_ANCHOR.  Define the
5788169689Skan   anchor relative to ".", the current section position.  */
5789169689Skan
5790169689Skanvoid
5791169689Skandefault_asm_output_anchor (rtx symbol)
5792169689Skan{
5793169689Skan  char buffer[100];
5794169689Skan
5795169689Skan  sprintf (buffer, ". + " HOST_WIDE_INT_PRINT_DEC,
5796169689Skan	   SYMBOL_REF_BLOCK_OFFSET (symbol));
5797169689Skan  ASM_OUTPUT_DEF (asm_out_file, XSTR (symbol, 0), buffer);
5798169689Skan}
5799169689Skan#endif
5800169689Skan
5801169689Skan/* The default implementation of TARGET_USE_ANCHORS_FOR_SYMBOL_P.  */
5802169689Skan
5803169689Skanbool
5804169689Skandefault_use_anchors_for_symbol_p (rtx symbol)
5805169689Skan{
5806169689Skan  section *sect;
5807169689Skan  tree decl;
5808169689Skan
5809169689Skan  /* Don't use anchors for mergeable sections.  The linker might move
5810169689Skan     the objects around.  */
5811169689Skan  sect = SYMBOL_REF_BLOCK (symbol)->sect;
5812169689Skan  if (sect->common.flags & SECTION_MERGE)
5813169689Skan    return false;
5814169689Skan
5815169689Skan  /* Don't use anchors for small data sections.  The small data register
5816169689Skan     acts as an anchor for such sections.  */
5817169689Skan  if (sect->common.flags & SECTION_SMALL)
5818169689Skan    return false;
5819169689Skan
5820169689Skan  decl = SYMBOL_REF_DECL (symbol);
5821169689Skan  if (decl && DECL_P (decl))
5822169689Skan    {
5823169689Skan      /* Don't use section anchors for decls that might be defined by
5824169689Skan	 other modules.  */
5825169689Skan      if (!targetm.binds_local_p (decl))
5826169689Skan	return false;
5827169689Skan
5828169689Skan      /* Don't use section anchors for decls that will be placed in a
5829169689Skan	 small data section.  */
5830169689Skan      /* ??? Ideally, this check would be redundant with the SECTION_SMALL
5831169689Skan	 one above.  The problem is that we only use SECTION_SMALL for
5832169689Skan	 sections that should be marked as small in the section directive.  */
5833169689Skan      if (targetm.in_small_data_p (decl))
5834169689Skan	return false;
5835169689Skan    }
5836169689Skan  return true;
5837169689Skan}
5838169689Skan
5839117395Skan/* Assume ELF-ish defaults, since that's pretty much the most liberal
5840117395Skan   wrt cross-module name binding.  */
5841117395Skan
5842117395Skanbool
5843132718Skandefault_binds_local_p (tree exp)
5844117395Skan{
5845132718Skan  return default_binds_local_p_1 (exp, flag_shlib);
5846117395Skan}
5847117395Skan
5848117395Skanbool
5849132718Skandefault_binds_local_p_1 (tree exp, int shlib)
5850117395Skan{
5851117395Skan  bool local_p;
5852117395Skan
5853117395Skan  /* A non-decl is an entry in the constant pool.  */
5854117395Skan  if (!DECL_P (exp))
5855117395Skan    local_p = true;
5856169689Skan  /* Weakrefs may not bind locally, even though the weakref itself is
5857169689Skan     always static and therefore local.  */
5858169689Skan  else if (lookup_attribute ("weakref", DECL_ATTRIBUTES (exp)))
5859169689Skan    local_p = false;
5860117395Skan  /* Static variables are always local.  */
5861117395Skan  else if (! TREE_PUBLIC (exp))
5862117395Skan    local_p = true;
5863169689Skan  /* A variable is local if the user has said explicitly that it will
5864169689Skan     be.  */
5865169689Skan  else if (DECL_VISIBILITY_SPECIFIED (exp)
5866169689Skan	   && DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
5867117395Skan    local_p = true;
5868169689Skan  /* Variables defined outside this object might not be local.  */
5869117395Skan  else if (DECL_EXTERNAL (exp))
5870117395Skan    local_p = false;
5871169689Skan  /* If defined in this object and visibility is not default, must be
5872169689Skan     local.  */
5873169689Skan  else if (DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
5874169689Skan    local_p = true;
5875169689Skan  /* Default visibility weak data can be overridden by a strong symbol
5876169689Skan     in another module and so are not local.  */
5877169689Skan  else if (DECL_WEAK (exp))
5878117395Skan    local_p = false;
5879117395Skan  /* If PIC, then assume that any global name can be overridden by
5880235623Spfg     symbols resolved from other modules, unless we are compiling with
5881235623Spfg     -fwhole-program, which assumes that names are local.  */
5882117395Skan  else if (shlib)
5883235623Spfg    local_p = flag_whole_program;
5884117395Skan  /* Uninitialized COMMON variable may be unified with symbols
5885117395Skan     resolved from other modules.  */
5886117395Skan  else if (DECL_COMMON (exp)
5887117395Skan	   && (DECL_INITIAL (exp) == NULL
5888117395Skan	       || DECL_INITIAL (exp) == error_mark_node))
5889117395Skan    local_p = false;
5890117395Skan  /* Otherwise we're left with initialized (or non-common) global data
5891117395Skan     which is of necessity defined locally.  */
5892117395Skan  else
5893117395Skan    local_p = true;
5894117395Skan
5895117395Skan  return local_p;
5896117395Skan}
5897117395Skan
5898132718Skan/* Determine whether or not a pointer mode is valid. Assume defaults
5899132718Skan   of ptr_mode or Pmode - can be overridden.  */
5900132718Skanbool
5901132718Skandefault_valid_pointer_mode (enum machine_mode mode)
5902132718Skan{
5903132718Skan  return (mode == ptr_mode || mode == Pmode);
5904132718Skan}
5905132718Skan
5906117395Skan/* Default function to output code that will globalize a label.  A
5907169689Skan   target must define GLOBAL_ASM_OP or provide its own function to
5908117395Skan   globalize a label.  */
5909117395Skan#ifdef GLOBAL_ASM_OP
5910117395Skanvoid
5911132718Skandefault_globalize_label (FILE * stream, const char *name)
5912117395Skan{
5913117395Skan  fputs (GLOBAL_ASM_OP, stream);
5914117395Skan  assemble_name (stream, name);
5915117395Skan  putc ('\n', stream);
5916117395Skan}
5917117395Skan#endif /* GLOBAL_ASM_OP */
5918132718Skan
5919169689Skan/* Default function to output a label for unwind information.  The
5920169689Skan   default is to do nothing.  A target that needs nonlocal labels for
5921169689Skan   unwind information must provide its own function to do this.  */
5922169689Skanvoid
5923169689Skandefault_emit_unwind_label (FILE * stream ATTRIBUTE_UNUSED,
5924169689Skan			   tree decl ATTRIBUTE_UNUSED,
5925169689Skan			   int for_eh ATTRIBUTE_UNUSED,
5926169689Skan			   int empty ATTRIBUTE_UNUSED)
5927169689Skan{
5928169689Skan}
5929169689Skan
5930169689Skan/* Default function to output a label to divide up the exception table.
5931169689Skan   The default is to do nothing.  A target that needs/wants to divide
5932169689Skan   up the table must provide it's own function to do this.  */
5933169689Skanvoid
5934169689Skandefault_emit_except_table_label (FILE * stream ATTRIBUTE_UNUSED)
5935169689Skan{
5936169689Skan}
5937169689Skan
5938132718Skan/* This is how to output an internal numbered label where PREFIX is
5939132718Skan   the class of label and LABELNO is the number within the class.  */
5940132718Skan
5941132718Skanvoid
5942132718Skandefault_internal_label (FILE *stream, const char *prefix,
5943132718Skan			unsigned long labelno)
5944132718Skan{
5945132718Skan  char *const buf = alloca (40 + strlen (prefix));
5946132718Skan  ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
5947169689Skan  ASM_OUTPUT_INTERNAL_LABEL (stream, buf);
5948132718Skan}
5949132718Skan
5950132718Skan/* This is the default behavior at the beginning of a file.  It's
5951132718Skan   controlled by two other target-hook toggles.  */
5952132718Skanvoid
5953132718Skandefault_file_start (void)
5954132718Skan{
5955132718Skan  if (targetm.file_start_app_off && !flag_verbose_asm)
5956132718Skan    fputs (ASM_APP_OFF, asm_out_file);
5957132718Skan
5958132718Skan  if (targetm.file_start_file_directive)
5959132718Skan    output_file_directive (asm_out_file, main_input_filename);
5960132718Skan}
5961132718Skan
5962132718Skan/* This is a generic routine suitable for use as TARGET_ASM_FILE_END
5963132718Skan   which emits a special section directive used to indicate whether or
5964132718Skan   not this object file needs an executable stack.  This is primarily
5965132718Skan   a GNU extension to ELF but could be used on other targets.  */
5966169689Skan
5967169689Skanint trampolines_created;
5968169689Skan
5969132718Skanvoid
5970132718Skanfile_end_indicate_exec_stack (void)
5971132718Skan{
5972132718Skan  unsigned int flags = SECTION_DEBUG;
5973132718Skan  if (trampolines_created)
5974132718Skan    flags |= SECTION_CODE;
5975132718Skan
5976169689Skan  switch_to_section (get_section (".note.GNU-stack", flags, NULL));
5977132718Skan}
5978132718Skan
5979169689Skan/* Output DIRECTIVE (a C string) followed by a newline.  This is used as
5980169689Skan   a get_unnamed_section callback.  */
5981169689Skan
5982169689Skanvoid
5983169689Skanoutput_section_asm_op (const void *directive)
5984169689Skan{
5985169689Skan  fprintf (asm_out_file, "%s\n", (const char *) directive);
5986169689Skan}
5987169689Skan
5988169689Skan/* Emit assembly code to switch to section NEW_SECTION.  Do nothing if
5989169689Skan   the current section is NEW_SECTION.  */
5990169689Skan
5991169689Skanvoid
5992169689Skanswitch_to_section (section *new_section)
5993169689Skan{
5994169689Skan  if (in_section == new_section)
5995169689Skan    return;
5996169689Skan
5997169689Skan  if (new_section->common.flags & SECTION_FORGET)
5998169689Skan    in_section = NULL;
5999169689Skan  else
6000169689Skan    in_section = new_section;
6001169689Skan
6002169689Skan  switch (SECTION_STYLE (new_section))
6003169689Skan    {
6004169689Skan    case SECTION_NAMED:
6005169689Skan      if (cfun
6006169689Skan	  && !cfun->unlikely_text_section_name
6007169689Skan	  && strcmp (new_section->named.name,
6008169689Skan		     UNLIKELY_EXECUTED_TEXT_SECTION_NAME) == 0)
6009169689Skan	cfun->unlikely_text_section_name = UNLIKELY_EXECUTED_TEXT_SECTION_NAME;
6010169689Skan
6011169689Skan      targetm.asm_out.named_section (new_section->named.name,
6012169689Skan				     new_section->named.common.flags,
6013169689Skan				     new_section->named.decl);
6014169689Skan      break;
6015169689Skan
6016169689Skan    case SECTION_UNNAMED:
6017169689Skan      new_section->unnamed.callback (new_section->unnamed.data);
6018169689Skan      break;
6019169689Skan
6020169689Skan    case SECTION_NOSWITCH:
6021169689Skan      gcc_unreachable ();
6022169689Skan      break;
6023169689Skan    }
6024169689Skan
6025169689Skan  new_section->common.flags |= SECTION_DECLARED;
6026169689Skan}
6027169689Skan
6028169689Skan/* If block symbol SYMBOL has not yet been assigned an offset, place
6029169689Skan   it at the end of its block.  */
6030169689Skan
6031169689Skanvoid
6032169689Skanplace_block_symbol (rtx symbol)
6033169689Skan{
6034169689Skan  unsigned HOST_WIDE_INT size, mask, offset;
6035169689Skan  struct constant_descriptor_rtx *desc;
6036169689Skan  unsigned int alignment;
6037169689Skan  struct object_block *block;
6038169689Skan  tree decl;
6039169689Skan
6040169689Skan  gcc_assert (SYMBOL_REF_BLOCK (symbol));
6041169689Skan  if (SYMBOL_REF_BLOCK_OFFSET (symbol) >= 0)
6042169689Skan    return;
6043169689Skan
6044169689Skan  /* Work out the symbol's size and alignment.  */
6045169689Skan  if (CONSTANT_POOL_ADDRESS_P (symbol))
6046169689Skan    {
6047169689Skan      desc = SYMBOL_REF_CONSTANT (symbol);
6048169689Skan      alignment = desc->align;
6049169689Skan      size = GET_MODE_SIZE (desc->mode);
6050169689Skan    }
6051169689Skan  else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
6052169689Skan    {
6053169689Skan      decl = SYMBOL_REF_DECL (symbol);
6054169689Skan      alignment = get_constant_alignment (decl);
6055169689Skan      size = get_constant_size (decl);
6056169689Skan    }
6057169689Skan  else
6058169689Skan    {
6059169689Skan      decl = SYMBOL_REF_DECL (symbol);
6060169689Skan      alignment = DECL_ALIGN (decl);
6061169689Skan      size = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
6062169689Skan    }
6063169689Skan
6064169689Skan  /* Calculate the object's offset from the start of the block.  */
6065169689Skan  block = SYMBOL_REF_BLOCK (symbol);
6066169689Skan  mask = alignment / BITS_PER_UNIT - 1;
6067169689Skan  offset = (block->size + mask) & ~mask;
6068169689Skan  SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
6069169689Skan
6070169689Skan  /* Record the block's new alignment and size.  */
6071169689Skan  block->alignment = MAX (block->alignment, alignment);
6072169689Skan  block->size = offset + size;
6073169689Skan
6074169689Skan  VEC_safe_push (rtx, gc, block->objects, symbol);
6075169689Skan}
6076169689Skan
6077169689Skan/* Return the anchor that should be used to address byte offset OFFSET
6078169689Skan   from the first object in BLOCK.  MODEL is the TLS model used
6079169689Skan   to access it.  */
6080169689Skan
6081169689Skanrtx
6082169689Skanget_section_anchor (struct object_block *block, HOST_WIDE_INT offset,
6083169689Skan		    enum tls_model model)
6084169689Skan{
6085169689Skan  char label[100];
6086169689Skan  unsigned int begin, middle, end;
6087169689Skan  unsigned HOST_WIDE_INT min_offset, max_offset, range, bias, delta;
6088169689Skan  rtx anchor;
6089169689Skan
6090169689Skan  /* Work out the anchor's offset.  Use an offset of 0 for the first
6091169689Skan     anchor so that we don't pessimize the case where we take the address
6092169689Skan     of a variable at the beginning of the block.  This is particularly
6093169689Skan     useful when a block has only one variable assigned to it.
6094169689Skan
6095169689Skan     We try to place anchors RANGE bytes apart, so there can then be
6096169689Skan     anchors at +/-RANGE, +/-2 * RANGE, and so on, up to the limits of
6097169689Skan     a ptr_mode offset.  With some target settings, the lowest such
6098169689Skan     anchor might be out of range for the lowest ptr_mode offset;
6099169689Skan     likewise the highest anchor for the highest offset.  Use anchors
6100169689Skan     at the extreme ends of the ptr_mode range in such cases.
6101169689Skan
6102169689Skan     All arithmetic uses unsigned integers in order to avoid
6103169689Skan     signed overflow.  */
6104169689Skan  max_offset = (unsigned HOST_WIDE_INT) targetm.max_anchor_offset;
6105169689Skan  min_offset = (unsigned HOST_WIDE_INT) targetm.min_anchor_offset;
6106169689Skan  range = max_offset - min_offset + 1;
6107169689Skan  if (range == 0)
6108169689Skan    offset = 0;
6109169689Skan  else
6110169689Skan    {
6111169689Skan      bias = 1 << (GET_MODE_BITSIZE (ptr_mode) - 1);
6112169689Skan      if (offset < 0)
6113169689Skan	{
6114169689Skan	  delta = -(unsigned HOST_WIDE_INT) offset + max_offset;
6115169689Skan	  delta -= delta % range;
6116169689Skan	  if (delta > bias)
6117169689Skan	    delta = bias;
6118169689Skan	  offset = (HOST_WIDE_INT) (-delta);
6119169689Skan	}
6120169689Skan      else
6121169689Skan	{
6122169689Skan	  delta = (unsigned HOST_WIDE_INT) offset - min_offset;
6123169689Skan	  delta -= delta % range;
6124169689Skan	  if (delta > bias - 1)
6125169689Skan	    delta = bias - 1;
6126169689Skan	  offset = (HOST_WIDE_INT) delta;
6127169689Skan	}
6128169689Skan    }
6129169689Skan
6130169689Skan  /* Do a binary search to see if there's already an anchor we can use.
6131169689Skan     Set BEGIN to the new anchor's index if not.  */
6132169689Skan  begin = 0;
6133169689Skan  end = VEC_length (rtx, block->anchors);
6134169689Skan  while (begin != end)
6135169689Skan    {
6136169689Skan      middle = (end + begin) / 2;
6137169689Skan      anchor = VEC_index (rtx, block->anchors, middle);
6138169689Skan      if (SYMBOL_REF_BLOCK_OFFSET (anchor) > offset)
6139169689Skan	end = middle;
6140169689Skan      else if (SYMBOL_REF_BLOCK_OFFSET (anchor) < offset)
6141169689Skan	begin = middle + 1;
6142169689Skan      else if (SYMBOL_REF_TLS_MODEL (anchor) > model)
6143169689Skan	end = middle;
6144169689Skan      else if (SYMBOL_REF_TLS_MODEL (anchor) < model)
6145169689Skan	begin = middle + 1;
6146169689Skan      else
6147169689Skan	return anchor;
6148169689Skan    }
6149169689Skan
6150169689Skan  /* Create a new anchor with a unique label.  */
6151169689Skan  ASM_GENERATE_INTERNAL_LABEL (label, "LANCHOR", anchor_labelno++);
6152169689Skan  anchor = create_block_symbol (ggc_strdup (label), block, offset);
6153169689Skan  SYMBOL_REF_FLAGS (anchor) |= SYMBOL_FLAG_LOCAL | SYMBOL_FLAG_ANCHOR;
6154169689Skan  SYMBOL_REF_FLAGS (anchor) |= model << SYMBOL_FLAG_TLS_SHIFT;
6155169689Skan
6156169689Skan  /* Insert it at index BEGIN.  */
6157169689Skan  VEC_safe_insert (rtx, gc, block->anchors, begin, anchor);
6158169689Skan  return anchor;
6159169689Skan}
6160169689Skan
6161169689Skan/* Output the objects in BLOCK.  */
6162169689Skan
6163169689Skanstatic void
6164169689Skanoutput_object_block (struct object_block *block)
6165169689Skan{
6166169689Skan  struct constant_descriptor_rtx *desc;
6167169689Skan  unsigned int i;
6168169689Skan  HOST_WIDE_INT offset;
6169169689Skan  tree decl;
6170169689Skan  rtx symbol;
6171169689Skan
6172169689Skan  if (block->objects == NULL)
6173169689Skan    return;
6174169689Skan
6175169689Skan  /* Switch to the section and make sure that the first byte is
6176169689Skan     suitably aligned.  */
6177169689Skan  switch_to_section (block->sect);
6178169689Skan  assemble_align (block->alignment);
6179169689Skan
6180169689Skan  /* Define the values of all anchors relative to the current section
6181169689Skan     position.  */
6182169689Skan  for (i = 0; VEC_iterate (rtx, block->anchors, i, symbol); i++)
6183169689Skan    targetm.asm_out.output_anchor (symbol);
6184169689Skan
6185169689Skan  /* Output the objects themselves.  */
6186169689Skan  offset = 0;
6187169689Skan  for (i = 0; VEC_iterate (rtx, block->objects, i, symbol); i++)
6188169689Skan    {
6189169689Skan      /* Move to the object's offset, padding with zeros if necessary.  */
6190169689Skan      assemble_zeros (SYMBOL_REF_BLOCK_OFFSET (symbol) - offset);
6191169689Skan      offset = SYMBOL_REF_BLOCK_OFFSET (symbol);
6192169689Skan      if (CONSTANT_POOL_ADDRESS_P (symbol))
6193169689Skan	{
6194169689Skan	  desc = SYMBOL_REF_CONSTANT (symbol);
6195169689Skan	  output_constant_pool_1 (desc, 1);
6196169689Skan	  offset += GET_MODE_SIZE (desc->mode);
6197169689Skan	}
6198169689Skan      else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
6199169689Skan	{
6200169689Skan	  decl = SYMBOL_REF_DECL (symbol);
6201169689Skan	  assemble_constant_contents (decl, XSTR (symbol, 0),
6202169689Skan				      get_constant_alignment (decl));
6203169689Skan	  offset += get_constant_size (decl);
6204169689Skan	}
6205169689Skan      else
6206169689Skan	{
6207169689Skan	  decl = SYMBOL_REF_DECL (symbol);
6208169689Skan	  assemble_variable_contents (decl, XSTR (symbol, 0), false);
6209169689Skan	  offset += tree_low_cst (DECL_SIZE_UNIT (decl), 1);
6210169689Skan	}
6211169689Skan    }
6212169689Skan}
6213169689Skan
6214169689Skan/* A htab_traverse callback used to call output_object_block for
6215169689Skan   each member of object_block_htab.  */
6216169689Skan
6217169689Skanstatic int
6218169689Skanoutput_object_block_htab (void **slot, void *data ATTRIBUTE_UNUSED)
6219169689Skan{
6220169689Skan  output_object_block ((struct object_block *) (*slot));
6221169689Skan  return 1;
6222169689Skan}
6223169689Skan
6224169689Skan/* Output the definitions of all object_blocks.  */
6225169689Skan
6226169689Skanvoid
6227169689Skanoutput_object_blocks (void)
6228169689Skan{
6229169689Skan  htab_traverse (object_block_htab, output_object_block_htab, NULL);
6230169689Skan}
6231169689Skan
6232215840Sdim/* Emit text to declare externally defined symbols. It is needed to
6233215840Sdim   properly support non-default visibility.  */
6234215840Sdimvoid
6235215840Sdimdefault_elf_asm_output_external (FILE *file ATTRIBUTE_UNUSED,
6236215840Sdim				 tree decl,
6237215840Sdim				 const char *name ATTRIBUTE_UNUSED)
6238215840Sdim{
6239215840Sdim  /* We output the name if and only if TREE_SYMBOL_REFERENCED is
6240215840Sdim     set in order to avoid putting out names that are never really
6241215840Sdim     used. */
6242215840Sdim  if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
6243215840Sdim      && targetm.binds_local_p (decl))
6244215840Sdim    maybe_assemble_visibility (decl);
6245215840Sdim}
6246215840Sdim
6247117395Skan#include "gt-varasm.h"
6248